Im trying to take a 1D array of 1's and 0's (mostly zeroes) and convolve it with a gaussian to get a more probablistic representation. When I use scipy.ndimage.gaussian_filter1d I keep getting back arrays that are just full of zeroes.
spike_bins, _ = np.histogram(spike_times,
bins=np.linspace(start_time, end_time, bin_number))
### CONVOLVE ##
#spike_bins_convolved = gaussian_filter1d(spike_bins, 1, mode = 'constant')
#print(spike_bins_convolved)
#print(np.sum(spike_bins_convolved))
###############
spike_bins.reshape(1, -1) #single sample
####### TRYING IT AFTER THIS STEP INSTEAD #######
spike_bins_convolved = gaussian_filter1d(spike_bins, 1, mode = 'constant')
print(np.sum(spike_bins_convolved))
Here is my code above, i tried to run it at two different places and saw that it returned an array of all zeroes after both places even tho the input arrays were not all zeroes.
The issue is that gaussian_filter1d()
returns an array of the same dtype as the argument. If you pass it an array of integers, it will give you back an array of integers, even if that requires rounding every value to zero.
The solution is to convert your counts to floating point, which can handle non-integer values. You can do this with spike_bins.astype('float64')
.
Example of this issue:
import scipy.ndimage
import numpy as np
spike_bins, _ = np.histogram([1, 2.5, 3.5], bins=[0, 1, 2, 3])
print(spike_bins)
print(spike_bins.dtype)
print(scipy.ndimage.gaussian_filter1d(spike_bins, 1, mode = 'constant'))
print(scipy.ndimage.gaussian_filter1d(spike_bins.astype('float64'), 1, mode = 'constant'))
This prints [0 0 0]
if the input to gaussian_filter1d()
has an integer dtype, but will print nonzero values if given a floating point dtype.