Skip to content

Commit

Permalink
Merge pull request #811 from NathanKlineInstitute/development
Browse files Browse the repository at this point in the history
add default padding (==1) for plotCSD, to avoid top/bottom edge artifacts in color plots
  • Loading branch information
vvbragin authored May 1, 2024
2 parents e97ed5c + a79c85b commit c2279cf
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions netpyne/plotting/plotCSD.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
import numpy as np
import scipy

def getPaddedCSD (CSDData, pad):
# pad the first/last row of CSDData by replication (to avoid edge artifacts when drawing colors plots)
npcsd = []
for i in range(pad): npcsd.append(CSDData[0,:])
for i in range(CSDData.shape[0]): npcsd.append(CSDData[i,:])
for i in range(pad): npcsd.append(CSDData[-1,:])
npcsd=np.array(npcsd)
return npcsd

@exception
def plotCSD(
Expand All @@ -31,6 +39,7 @@ def plotCSD(
showFig=False,
smooth=True,
colorbar=True,
pad=1,
**kwargs
):
"""
Expand Down Expand Up @@ -113,9 +122,13 @@ def plotCSD(
**Default:** ``True``
colorbar : bool
Whetehr or not to plot the colorbar
Whether or not to plot the colorbar
**Default:** ``True``
pad : int
Amount to pad CSDData on top/bottom for more accurate interpolation at edges
**Default:** ``1``
"""

# If there is no input data, get the data from the NetPyNE sim object
Expand All @@ -137,6 +150,9 @@ def plotCSD(
else:
pass # TODO: ensure time slicing works properly in case CSDData is passed as an argument

npcsd = CSDData
if pad > 0: npcsd = getPaddedCSD(CSDData, pad) # apply padding (replicate first,last rows)

if timeRange is None:
timeRange = [0, sim.cfg.duration]

Expand All @@ -145,20 +161,21 @@ def plotCSD(

# PLOTTING
X = np.arange(timeRange[0], timeRange[1], dt) # X == tt
Y = np.arange(CSDData.shape[0])
Y = np.arange(npcsd.shape[0])

# interpolation
CSD_spline = scipy.interpolate.RectBivariateSpline(Y, X, CSDData)
Y_plot = np.linspace(0, CSDData.shape[0], num=1000)
Z = CSD_spline(Y_plot, X)
fctr = int(1000 / CSDData.shape[0])
CSD_spline = scipy.interpolate.RectBivariateSpline(Y, X, npcsd)
Y_plot = np.linspace(-pad, npcsd.shape[0] + pad, num=int(1000*npcsd.shape[0]/CSDData.shape[0]))
Z = CSD_spline(Y_plot, X)[pad*fctr:pad*fctr+1000,:]

# plotting options
plt.rcParams.update({'font.size': fontSize})
xmin = int(X[0])
xmax = int(X[-1]) + 1
ymin = 0
if ymax is None:
ymax = sim.cfg.recordLFP[-1][1] + spacing_um
ymax = sim.cfg.recordLFP[-1][1] + spacing_um + pad
extent_xy = [xmin, xmax, ymax, ymin]

# set up figure
Expand Down Expand Up @@ -229,7 +246,7 @@ def plotCSD(
subaxs[chan].margins(0.0, 0.01)
subaxs[chan].get_xaxis().set_visible(False)
subaxs[chan].get_yaxis().set_visible(False)
subaxs[chan].plot(X, CSDData[chan, :], color='green', linewidth=0.3, label='CSD time series')
subaxs[chan].plot(X, npcsd[pad+chan, :], color='green', linewidth=0.3, label='CSD time series')
if legendLabel:
subaxs[chan].legend(loc='upper right', fontsize=fontSize)
legendLabel = False
Expand Down

0 comments on commit c2279cf

Please sign in to comment.