# Interpolating scattered data points to create topological EEG voltage map

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.

``````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`

``````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=[])
``````

### 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`

``````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

### 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!

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 ?

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.