Skip to content

Commit

Permalink
Implements plotfunction #47 (#56)
Browse files Browse the repository at this point in the history
* fix labels, fix #39

* added a backward compatability with warning

* removed NaturalNeighbours for now until #55 is fixed

* added example of contourf and surface to docs

* remove extra file

* kwarg name tweak, tests

* docs fix

* add cache to documenter

---------

Co-authored-by: Phillip Alday <[email protected]>
  • Loading branch information
behinger and palday authored Nov 15, 2024
1 parent 4d6a64c commit eea824d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ jobs:
- uses: julia-actions/setup-julia@v2
with:
version: '1'
- uses: julia-actions/cache@v2
with:
cache-registries: "true"
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-docdeploy@v1
env:
Expand Down
22 changes: 22 additions & 0 deletions docs/src/general.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,25 @@ TopoPlots.topoplot(
label_scatter=(; strokewidth=2),
contours=(linewidth=2, color=:white))
```


## Different plotfunctions

It is possible to exchange the plotting function, from `heatmap!` to `contourf!` or `surface!`. Due to different keyword arguments, one needs to filter which keywords are passed to the plotting function manually.

```@example general
f = Figure()
TopoPlots.topoplot(f[1,1],
rand(10), rand(Point2f, 10),
axis=(; aspect=DataAspect()),
plotfnc! = contourf!, plotfnc_kwargs_names=[:colormap])
TopoPlots.topoplot(f[1,2],
rand(10), rand(Point2f, 10),
axis=(; aspect=DataAspect()),
plotfnc! = surface!) # surface can take all default kwargs similar to heatmap!
f
```
4 changes: 2 additions & 2 deletions src/TopoPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export GeomExtrapolation, NullExtrapolation
@compile_workload begin
# all calls in this block will be precompiled, regardless of whether
# they belong to your package or not (on Julia 1.8 and higher)
eeg_topoplot(view(data, :, 340, 1); positions)
eeg_topoplot(data[:, 340, 1]; positions)
eeg_topoplot(view(data, :, 340, 1); positions)
eeg_topoplot(data[:, 340, 1]; positions)
end
end

Expand Down
11 changes: 8 additions & 3 deletions src/core-recipe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
labels = nothing,
label_text = false,
label_scatter = false,
contours = false
contours = false,
plotfnc! = heatmap!,
plotfnc_kwargs_names= [:colorrange, :colormap, :interpolate],
)
end

Expand All @@ -39,6 +41,8 @@ Creates an irregular interpolation for each `data[i]` point at `positions[i]`.
* true: add point for each position with default attributes
* NamedTuple: Attributes get passed to the Makie.scatter! call.
* `markersize = 5`: size of the points defined by positions, shortcut for label_scatter=(markersize=5,)
* `plotfnc! = heatmap!`: function to use for plotting the interpolation
* `plotfnc_kwargs_names = [:colorrange, :colormap, :interpolate]`: different `plotfnc` support different kwargs, this array contains the keys to filter the full list which is [:colorrange, :colormap, :interpolate]
* `contours = false`:
* true: add scatter point for each position
Expand Down Expand Up @@ -110,14 +114,15 @@ function Makie.plot!(p::TopoPlot)
pts = Point2f.(xg' .* ones(length(yg)), ones(length(xg))' .* yg)
return in.(pts, Ref(geometry))
end

data = lift(p, p.interpolation, xg, yg, padded_pos_data_bb,mask) do interpolation, xg, yg, (points, data, _, _),mask
z = interpolation(xg, yg, points, data;mask=mask)
# z[mask] .= NaN
return z
end
kwargs_all = Dict(:colorrange => colorrange, :colormap => p.colormap, :interpolate => true)

heatmap!(p, xg, yg, data, colormap=p.colormap, colorrange=colorrange, interpolate=true)
p.plotfnc![](p, xg, yg, data; (p.plotfnc_kwargs_names[].=>getindex.(Ref(kwargs_all),p.plotfnc_kwargs_names[]))...)
contours = to_value(p.contours)
attributes = @plot_or_defaults contours Attributes(color=(:black, 0.5), linestyle=:dot, levels=6)
if !isnothing(attributes) && !(p.interpolation[] isa NullInterpolator)
Expand Down
3 changes: 0 additions & 3 deletions src/eeg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,12 @@ function labels2positions(labels)
return CHANNEL_TO_POSITION_10_20[key]
else
error("Currently only 10/20 is supported. Found label: $(label)")

end
end
end

#function Makie.convert_arguments(::Type{<:EEG_TopoPlot}, data::AbstractVector{<:Real})
# return (data, labels2positions(labels))#

#
#end

Expand All @@ -113,7 +111,6 @@ function Makie.plot!(plot::EEG_TopoPlot)
return labels
end
end

tplot = topoplot!(plot, Attributes(plot), plot.data, positions;)
head = plot_or_defaults(to_value(plot.head), Attributes(), :head)
if !isnothing(head)
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ begin
@test_figure("test-extrapolate-data-circle", f)
end

let
data, positions = TopoPlots.example_data()
f, ax, pl = topoplot(1:10,positions[1:10];plotfnc! = contourf!,plotfnc_kwargs_names=[:colormap])
@test_figure("test-contour-plotfnc!", f)
f, ax, pl = topoplot(1:10,positions[1:10];plotfnc! = (args...;kwargs...)->heatmap!(args...;alpha=0.3,kwargs...))
@test_figure("test-heatmap-with-alphs-plotfnc!", f)
end

begin
f = TopoPlots.eeg_topoplot(1:10; labels=TopoPlots.CHANNELS_10_20[1:10], label_text=true)
Expand Down

0 comments on commit eea824d

Please sign in to comment.