# Interpolating scattered data points to create topological EEG voltage map

**URL:** <https://discourse.julialang.org/t/interpolating-scattered-data-points-to-create-topological-eeg-voltage-map/78622>\
**Category:** Visualization\
**Tags:** visualization, interpolations\
**Created:** [March 28, 2022, 3:14pm UTC](https://discourse.julialang.org/t/interpolating-scattered-data-points-to-create-topological-eeg-voltage-map/78622 "2022-03-28T15:14:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AAlshammaa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aalshammaa/32/23499_2.png) [@AAlshammaa](https://discourse.julialang.org/u/AAlshammaa)\
**Post date:** [March 28, 2022, 3:14pm UTC](https://discourse.julialang.org/t/interpolating-scattered-data-points-to-create-topological-eeg-voltage-map/78622/1 "2022-03-28T15:14:46Z")

</div>

I have a dataset with EEG data that I read into Julia from Matlab file. The data is for a single time point from simultaneous 64 channels. Each channel has `x`, `y` coordinates, and value at those coordinates. My aim is to interpolate those scattered points to a regular grid and plot the resulting grid on a contour plot.

TL;DR: My questions are:  
1- Which is better for this application, `Dierckx` or `ScatteredInterpolation`? Or something else? I couldnt make the following work with `Interpolations.jl`  
2- If I use `Dierckx`, I would use the `Spline2D` function, but it requires an argument `s`, which throws an error, and the workaround being setting it to be in the thousands. I feel uneasy with arbitrarily setting a high value. Is there an optimal way to calculate/set it?  
3- If I use `ScatteredInterpolation`, I have to specify a basis function, such as `ThinPlate()`. I do not know which is the optimal method for my data. I am looking for something similar to what cubic or ‘v4’ interpolation using `griddata` in MATLAB. The documentation of `ScatteredInterpolation` is pretty sparse.  
4- The scatter points marker color doesnt match the underlying contour.

```julia
using Plots
using MAT
using ScatteredInterpolation
using Dierckx

EEG = matread("MATLAB_data_files/sampleEEGdata.mat")["EEG"];
timeidx = 315
trialidx = 10
data = EEG["data"][:, timeidx, trialidx] #64 data points at time index 315, from trial index 10
labels = EEG["chanlocs"]["labels"][:]
Th = EEG["chanlocs"]["theta"][:]
Rd = EEG["chanlocs"]["radius"][:]
x = Rd .* cos.(deg2rad.(Th))
y = Rd .* sin.(deg2rad.(Th))

```

`x`, `y`, and `data` are vectors with 64 elements each.

I used both `ScatteredInterpolation.jl` and `Dierckx.jl` to see which one results in better plots.

### Method 1: `Dierckx`

```julia
n = 300 # to end up with n x n grid
xrange = LinRange(minimum(x), maximum(x), n)
yrange = LinRange(minimum(y), maximum(y), n)

spl = Spline2D(y, x, data, s=2000); # From Dierckx.jl. s is set to 2000 to avoid errors

grid2 = evalgrid(spl, yrange, xrange)

#create a mask to end up with circular plot
Xi = ones(n) .* xrange'
Yi = ones(n)' .* yrange
mask = sqrt.((Xi .- median(xrange)) .^ 2 + (Yi .- median(yrange)) .^ 2) .<= 0.45

grid2[mask.==0] .= NaN

limits = (-1.05, 1.05).*maximum(abs, data)
xlimits = (-1.05, 1.05).*maximum(abs, x)
	
contourf(yrange, xrange, grid2, clims=limits, c=:jet, xlim=xlimits, aspect_ratio=1)
scatter!(y, x, series_annotations = text.(labels, :bottom, 11), marker_z=data, ms=3, legend=false, title = "title", showaxis=false, ticks=[])

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/4/d46f3f22119050c6697f15f39bb307d3f387c8a5.png)

### Issues with Method 1

I have two issues with this plot.  
1- This doesnt work unless i set the `s` parameter of the `Spline2D` function to arbitrarily high number. I do not understand what is the optimal way to specify this parameter. If it is too small, i get an error

> The required storage space exceeds the available storage space: nxest or nyest too small, or s too small. Try increasing s

2- If you see the scatter points marker color, they do not match the underlying field topology.

### Method 2: `ScatteredInterpolation`

Then I tried doing the same thing with `ScatteredInterpolation`

```julia
points = [y x]'
itp = ScatteredInterpolation.interpolate(ThinPlate(), points, data);
interpolatedgrid = [ScatteredInterpolation.evaluate(itp, [j i]')[1] for j in yrange, i in xrange]
interpolatedgrid[mask.==0] .= NaN
contourf(yrange, xrange, interpolatedgrid, clim=limits, c=:jet, xlim=xlimits, aspect_ratio=1)
scatter!(y, x, series_annotations = text.(labels, :bottom, 11), marker_z=data, ms=3, legend=false, title = "title", showaxis=false, ticks=[])

```

With this output

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/d/4d3fee87d8ac3b3f762b2249b497752191ade5c0.png)

### Issues with Method 2

1- Doesn’t look as nice as Method 1. Probably has to do with the large smoothing value I am applying there  
2- I do not know which optimal basis function to use. My goal is something like the cubic or ‘v4’ interpolation of `griddata` in MATLAB.  
3- Again, the scatter points marker color doesnt match the underlying contour

Thanks!

---

<div class="post-metadata">

**Author:** ![palday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palday/32/12640_2.png) [@palday](https://discourse.julialang.org/u/palday)\
**Post date:** [May 23, 2022, 5:16pm UTC](https://discourse.julialang.org/t/interpolating-scattered-data-points-to-create-topological-eeg-voltage-map/78622/2 "2022-05-23T17:16:10Z")

</div>

This doesn’t directly answer your question but is maybe still useful – have you seen [plot topography by likanzhan · Pull Request #2 · JuliaNeuroscience/NeuroPlots.jl · GitHub](https://github.com/JuliaNeuroscience/NeuroPlots.jl/pull/2) ?

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [May 23, 2022, 6:11pm UTC](https://discourse.julialang.org/t/interpolating-scattered-data-points-to-create-topological-eeg-voltage-map/78622/3 "2022-05-23T18:11:01Z")

</div>

PRs are welcome in that repo BTW. I’m coming off of doing a 10 week course in 4 weeks so I’m a bit behind on making my own contributions to repos right now, but I think I’ve caught most github mentions.
