Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #53

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ WGLMakie = "276b4fcb-3e11-5398-bf8b-a0c2d153d008"

[compat]
DSP = "0.7"
Makie = "0.21"
JLD2 = "0.5"
Makie = "0.20, 0.21, 0.22"
TopoPlots = "0.2"
Binary file added data/channel_123.jld2
Binary file not shown.
Binary file added data/channels_1_123.jld2
Binary file not shown.
7 changes: 7 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DataFramesMeta = "1313f7d8-7da2-5740-9ea0-a2ca25f37964"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
DocumenterTools = "35a29f4d-8980-5a13-9543-d66fff28ecb8"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
MakieThemes = "e296ed71-da82-5faf-88ab-0034a9761098"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -18,3 +20,8 @@ TopoPlots = "2bdbdf9c-dbd8-403f-947b-1a4e0dd41a7a"
Unfold = "181c99d8-e21b-4ff3-b70b-c233eddec679"
UnfoldMakie = "69a5ce3b-64fb-4f22-ae69-36dd4416af2a"
UnfoldSim = "ed8ae6d2-84d3-44c6-ab46-0baf21700804"

[compat]
DSP = "0.7"
JLD2 = "0.5"
TopoPlots = "0.2"
35 changes: 35 additions & 0 deletions docs/example_data.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using UnfoldSim
using TopoPlots
using Unfold
using Random

"""
example_data(String)

Creates example data or model. Currently, 3 datasets and 6 models are available.

Datasets:
- `pattern_detection_values` (default) - Dataframe with 5 fields:\\
sorting conditions, estimate (pattern detection values) lables, timing, rows.\\


**Return Value:** `DataFrame`.
"""
function example_data(example = "pattern_detection_values")
if example == "pattern_detection_values"
evts_d = CSV.read("../data/evts_d.csv", DataFrame)
pattern_detection_values = stack(evts_d)
pattern_detection_values.timing = 1:nrow(pattern_detection_values)
pattern_detection_values.label = 1:nrow(pattern_detection_values)
rename!(pattern_detection_values, :variable => :condition, :value => :estimate)
pattern_detection_values.rows = vcat(
repeat(["A"], size(pattern_detection_values, 1) ÷ 4),
repeat(["B"], size(pattern_detection_values, 1) ÷ 4),
repeat(["C"], size(pattern_detection_values, 1) ÷ 4),
repeat(["D"], size(pattern_detection_values, 1) ÷ 4),
)
return pattern_detection_values
else
error("unknown example data")
end
end
16 changes: 16 additions & 0 deletions docs/literate/intro/plot.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using CairoMakie
using DataFrames
using UnfoldMakie
using JLD2

include("../../../example_data.jl")

positions_128 = JLD2.load_object("../data/positions_128.jld2")
pattern_detection_values = example_data()

# Example of interactive topoplot series

inter_toposeries(
filter(x -> x.rows == "A", pattern_detection_values);
positions = positions_128,
)
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ makedocs(;
assets = String[],
),
pages = [
"Plotting" => "generated/intro/plot.md",
"ERPgnostics Documentation" => "index.md",
"API / DocStrings" => "api.md",
],
Expand Down
14 changes: 9 additions & 5 deletions src/ERPgnostics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using Statistics, StatsBase, Distributions
using HDF5, FileIO
using Printf
using Images
using TopoPlots
using TopoPlots
using ImageFiltering
using ComputationalResources
using Observables
Expand All @@ -22,18 +22,22 @@ using ProgressBars

include("interactive_heatmap.jl")
include("interactive_topoplots.jl")
include("pattern_detection_mean_filter.jl")
include("pattern_detection_probability.jl")
include("pattern_detection.jl")
include("pattern_simulation.jl")
include("filters.jl")

export inter_toposeries # or better toposeries_inter
export inter_toposeries_image

export inter_heatmap
export inter_heatmap_image

export mult_chan_pattern_detector_probability
export mult_chan_pattern_detector_probability_meanfilter

export simulate_6patterns

export pattern_detector
export complex_pattern_detector

export slow_filter
export mean_filter
end
47 changes: 47 additions & 0 deletions src/filters.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

function slow_filter(data_init)
return UnfoldMakie.imfilter(data_init, UnfoldMakie.Kernel.gaussian((0, max(50, 0))))
end

function fast_filter!(dat_filtered, kernel, dat) # broken
#r = Images.ImageFiltering.ComputationalResources.CPU1(Images.ImageFiltering.FIR())
DSP.filt!(dat_filtered, kernel[1].data.parent, dat)
return dat_filtered
end

function mean_filter(dat; output_dim = 20)
mean_filter!(similar(dat, output_dim, size(dat, 2)), dat)
end

function mean_filter!(dat_filtered, dat)
n_out = size(dat_filtered, 1)
dat_nrows = size(dat, 1)
bins = Int.(round.(collect(range(1, stop = dat_nrows, length = n_out + 1))))
bins[1] = 1
bins[end] = dat_nrows
for b = 1:length(bins)-1
bin_start = bins[b]
bin_stop = bins[b+1]
dat_filtered[b, :] .= mean(@view(dat[bin_start:bin_stop, :]), dims = 1)[1, :]
end
return dat_filtered
end

#=
function range_mean(dat_filt)
a = extrema(dat_filt, dims = 2)
b = last.(a) .- first.(a)
return mean(b)
end =#

entropy_robust(img::AbstractArray; kind = :shannon, nbins = 256) =
entropy_robust(Images.ImageQualityIndexes._log(kind), img; nbins = nbins)
function entropy_robust(logᵦ::Log, img; nbins = 256) where {Log<:Function}
img_trimmed = collect(trim(img[:], prop = 0.1))
_, counts = Images.ImageContrastAdjustment.build_histogram(img_trimmed, nbins)
n = length(img)
_zero = zero(first(counts) / n)
-sum(counts) do c
c > 0 ? c / n * logᵦ(c / n) : _zero
end
end
8 changes: 4 additions & 4 deletions src/interactive_heatmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function inter_heatmap(pattern_detection_values::DataFrame)
if event.button == Mouse.left && event.action == Mouse.press
plot, _ = pick(ax.scene)
a = DataInspector(plot)
pos = Makie.position_on_plot(plot, -1, apply_transform = false)[Vec(1, 2)]
b = Makie._pixelated_getindex(plot[1][], plot[2][], plot[3][], pos, true)
pos = WGLMakie.position_on_plot(plot, -1, apply_transform = false)[Vec(1, 2)]
b = WGLMakie._pixelated_getindex(plot[1][], plot[2][], plot[3][], pos, true)
chan_i[], var_i[] = b[1], b[2]
end
end
Expand Down Expand Up @@ -118,8 +118,8 @@ function inter_heatmap_image(
on(events(f).mousebutton, priority = 1) do event
if event.button == Mouse.left && event.action == Mouse.press
plot, _ = pick(ax.scene)
pos = Makie.position_on_plot(plot, -1, apply_transform = false)[Vec(1, 2)]
b = Makie._pixelated_getindex(plot[1][], plot[2][], plot[3][], pos, true)
pos = WGLMakie.position_on_plot(plot, -1, apply_transform = false)[Vec(1, 2)]
b = WGLMakie._pixelated_getindex(plot[1][], plot[2][], plot[3][], pos, true)
chan_i[], var_i[] = b[1], b[2]
#a = DataInspector(plot)
end
Expand Down
56 changes: 21 additions & 35 deletions src/interactive_topoplots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@ function inter_toposeries(
pattern_detection_values::DataFrame;
positions::Vector{Point{2,Float64}} = positions_128,
)

names = unique(pattern_detection_values.condition)
obs_tuple = Observable((0, 1, 0))
f = Figure(size = (1500, 800))
obs_tuple = Observable((0, 1, 1)) # row, col, channel
f = Makie.Figure(size = (1500, 400))
str = @lift(
"Interactive topoplots: channel - " *
string($obs_tuple[3]) *
", variable - " *
string(names[$obs_tuple[2]])
)

ax = WGLMakie.Axis(
ax = Makie.Axis(
f[1, 1],
xautolimitmargin = (0, 0),
yautolimitmargin = (0, 0),
title = str,
xlabel = "Channels",
ylabel = "Index of event variable",
Expand All @@ -44,32 +42,25 @@ function inter_toposeries(
xrectzoom = false,
yrectzoom = false,
)

on(events(f).mousebutton, priority = 1) do event
if event.button == Mouse.left && event.action == Mouse.press
end
end
hidespines!(ax)
hidedecorations!(ax)
plot_topoplotseries!(
f[1, 1],
pattern_detection_values;
mapping = (; col = :condition),
positions = positions,
col_labels = true,
interactive_scatter = obs_tuple,
visual = (label_scatter = (markersize = 15, strokewidth = 2),),
visual = (label_scatter = (markersize = 10, strokewidth = 2),),
layout = (; use_colorbar = true),
axis = (;
xpanlock = true,
ypanlock = true,
xzoomlock = true,
yzoomlock = true,
xrectzoom = false,
yrectzoom = false,
),
)

on(events(f).mousebutton, priority = 1) do event
if event.button == Mouse.left && event.action == Mouse.press
end
end
f

end

"""
Expand Down Expand Up @@ -99,31 +90,27 @@ ERP image will have trials on y-axis and time on x-axis
**Return Value:** Interactive `Figure` displaying topoplot series and interactive ERP image.
"""
function inter_toposeries_image(
pattern_detection_values::DataFrame,
events::DataFrame,
erps::Array{Float64,3},
pattern_detection_values,
events,
erps, #::Array{Float64,3},
timing;
positions = positions_128,
)

cond_names = unique(pattern_detection_values.condition)
obs_tuple = Observable((0, 1, 1))
f = Figure()#size = (3000, 1600))
f = Figure() #size = (3000, 1600))
str = @lift(
"Entropy topoplots: channel - " *
string($obs_tuple[3]) *
", sorting variable - " *
string(cond_names[$obs_tuple[2]])
)

ax = WGLMakie.Axis(
f[1, 1:5],
#xautolimitmargin = (0, 0),
#yautolimitmargin = (0, 0),
xlabelvisible = false,
title = str,
)
ax = Makie.Axis(f[1, 1:5], xlabelvisible = false, title = str)
hidespines!(ax)
hidedecorations!(ax)

plot_topoplotseries!(
f[1, 1:5],
pattern_detection_values;
Expand All @@ -132,11 +119,10 @@ function inter_toposeries_image(
mapping = (; col = :condition),
axis = (; xlabel = "Conditions", xlabelvisible = false),
visual = (
label_scatter = (markersize = 15, strokewidth = 2),
label_scatter = (markersize = 10, strokewidth = 2),
contours = (; levels = 0),
colormap = Reverse(:RdGy_4),
),
layout = (; use_colorbar = true),
interactive_scatter = obs_tuple,
colorbar = (;
label = "Pattern detection function value",
Expand All @@ -160,9 +146,9 @@ function inter_toposeries_image(
axis = (; title = "ERP image"),
)

on(events(f).mousebutton, priority = 1) do event
#= on(events(f).mousebutton, priority = 1) do event
if event.button == Mouse.left && event.action == Mouse.press
end
end
end =#
f
end
Loading
Loading