Esempio n. 1
0
def test_cluster_selection():
    indices = [10, 20, 25]
    clusters = generate_clusters(indices)
    clusters_selected = [1, 2]
    spikes = get_spikes_in_clusters(clusters_selected, clusters, False)
    assert np.array_equal(np.nonzero(spikes)[0], indices)
    spikes = get_spikes_in_clusters(clusters_selected, clusters, True)
    assert np.array_equal(spikes, indices)
Esempio n. 2
0
def test_select_array():
    # All spikes in cluster 1.
    indices = [10, 20, 25]
    
    # Indices in data excerpt.
    indices_data = [5, 10, 15, 20]
    
    # Generate clusters and data.
    clusters = generate_clusters(indices)
    data_raw = generate_data2D()
    data = pd.DataFrame(data_raw)
    
    # Excerpt of the data.
    data_excerpt = select(data, indices_data)
    
    # Get all spike indices in cluster 1.
    spikes_inclu1 = get_spikes_in_clusters([1], clusters)
    
    # We want to select all clusters in cluster 1 among those in data excerpt.
    data_excerpt_inclu1 = select(data_excerpt, spikes_inclu1)
    
    # There should be two rows: 4 in the excerpt, among which two are in
    # cluster 1.
    assert data_excerpt_inclu1.shape == (2, 5)
    
def cluster_quality(waveforms, features, clusters, masks,
    clusters_selected=None):
    # clusters = select(clusters, spikes)
    # features = select(features, spikes)
    # waveforms = select(waveforms, spikes)
    # masks = select(masks, spikes)

    nspikes, nsamples, nchannels = waveforms.shape
    quality = {}

    for cluster in clusters_selected:
        spikes = get_spikes_in_clusters(cluster, clusters)
        w = select(waveforms, spikes)
        m = select(masks, spikes)
        q = 1. / nsamples * ((w ** 2).sum(axis=1) * 1).mean(axis=1).max()
        quality[cluster] = q

    return quality
Esempio n. 4
0
def cluster_quality(waveforms, features, clusters, masks,
    clusters_selected=None):
    # clusters = select(clusters, spikes)
    # features = select(features, spikes)
    # waveforms = select(waveforms, spikes)
    # masks = select(masks, spikes)
    
    nspikes, nsamples, nchannels = waveforms.shape
    quality = {}
    
    for cluster in clusters_selected:
        spikes = get_spikes_in_clusters(cluster, clusters)
        w = select(waveforms, spikes)
        m = select(masks, spikes)
        q = 1. / nsamples * ((w ** 2).sum(axis=1) * 1).mean(axis=1).max()
        quality[cluster] = q
    
    return quality
Esempio n. 5
0
 def prepare_average_waveform_data(self):
     waveforms_avg = np.zeros((self.nclusters, self.nsamples, self.nchannels))
     waveforms_std = np.zeros((self.nclusters, self.nsamples, self.nchannels))
     self.masks_avg = np.zeros((self.nclusters, self.nchannels))
     for i, cluster in enumerate(self.clusters_unique):
         spike_indices = get_spikes_in_clusters(cluster, self.clusters)
         w = select(self.waveforms, spike_indices)
         m = select(self.masks, spike_indices)
         waveforms_avg[i,...] = w.mean(axis=0)
         waveforms_std[i,...] = w.std(axis=0).mean()
         self.masks_avg[i,...] = m.mean(axis=0)
     
     # create X coordinates
     X = np.tile(np.linspace(-1., 1., self.nsamples),
                     (self.nchannels * self.nclusters, 1))
     # create Y coordinates
     if self.nclusters == 0:
         Y = np.array([], dtype=np.float32)
         thickness = np.array([], dtype=np.float32)
     else:
         Y = np.vstack(waveforms_avg)
         thickness = np.vstack(waveforms_std).T.ravel()
     
     # concatenate data
     data = np.empty((X.size, 2), dtype=np.float32)
     data[:,0] = X.ravel()
     data[:,1] = Y.T.ravel()
     
     if self.nclusters > 0:
         # thicken
         w = thickness.reshape((-1, 1))
         n = waveforms_avg.size
         Y = np.zeros((2 * n, 2))
         u = np.zeros((n, 2))
         u[1:,0] = -np.diff(data[:,1])
         u[1:,1] = data[1,0] - data[0,0]
         u[0,:] = u[1,:]
         r = (u[:,0] ** 2 + u[:,1] ** 2) ** .5
         r[r == 0.] = 1
         u[:,0] /= r
         u[:,1] /= r
         Y[::2,:] = data - w * u
         Y[1::2,:] = data + w * u
         data_thickened = Y
     else:
         n = 0
         data_thickened = data
     
     self.nsamples_avg = self.nsamples * 2
     self.npoints_avg = waveforms_avg.size * 2
     self.nspikes_avg = self.nclusters
     self.nclusters_avg = self.nclusters
     self.nchannels_avg = self.nchannels
     self.clusters_rel_avg = np.arange(self.nclusters, dtype=np.int32)
     self.clusters_rel_ordered_avg = np.argsort(self.clusters_selected)[self.clusters_rel_avg]
     if self.keep_order:
         self.clusters_rel_ordered_avg2 = self.clusters_rel_ordered_avg
     else:
         self.clusters_rel_ordered_avg2 = self.clusters_rel_avg
     
     return data_thickened
Esempio n. 6
0
 def prepare_average_waveform_data(self):
     waveforms_avg = np.zeros((self.nclusters, self.nsamples, self.nchannels))
     waveforms_std = np.zeros((self.nclusters, self.nsamples, self.nchannels))
     self.masks_avg = np.zeros((self.nclusters, self.nchannels))
     for i, cluster in enumerate(self.clusters_unique):
         spike_indices = get_spikes_in_clusters(cluster, self.clusters)
         w = select(self.waveforms, spike_indices)
         m = select(self.masks, spike_indices)
         waveforms_avg[i,...] = w.mean(axis=0)
         waveforms_std[i,...] = w.std(axis=0).mean()
         self.masks_avg[i,...] = m.mean(axis=0)
     
     # create X coordinates
     X = np.tile(np.linspace(-1., 1., self.nsamples),
                     (self.nchannels * self.nclusters, 1))
     # create Y coordinates
     if self.nclusters == 0:
         Y = np.array([], dtype=np.float32)
         thickness = np.array([], dtype=np.float32)
     else:
         Y = np.vstack(waveforms_avg)
         thickness = np.vstack(waveforms_std).T.ravel()
     
     # concatenate data
     data = np.empty((X.size, 2), dtype=np.float32)
     data[:,0] = X.ravel()
     data[:,1] = Y.T.ravel()
     
     if self.nclusters > 0:
         # thicken
         w = thickness.reshape((-1, 1))
         n = waveforms_avg.size
         Y = np.zeros((2 * n, 2))
         u = np.zeros((n, 2))
         u[1:,0] = -np.diff(data[:,1])
         u[1:,1] = data[1,0] - data[0,0]
         u[0,:] = u[1,:]
         r = (u[:,0] ** 2 + u[:,1] ** 2) ** .5
         r[r == 0.] = 1
         u[:,0] /= r
         u[:,1] /= r
         Y[::2,:] = data - w * u
         Y[1::2,:] = data + w * u
         data_thickened = Y
     else:
         n = 0
         data_thickened = data
     
     self.nsamples_avg = self.nsamples * 2
     self.npoints_avg = waveforms_avg.size * 2
     self.nspikes_avg = self.nclusters
     self.nclusters_avg = self.nclusters
     self.nchannels_avg = self.nchannels
     self.clusters_rel_avg = np.arange(self.nclusters, dtype=np.int32)
     self.clusters_rel_ordered_avg = np.argsort(self.clusters_selected)[self.clusters_rel_avg]
     if self.keep_order:
         self.clusters_rel_ordered_avg2 = self.clusters_rel_ordered_avg
     else:
         self.clusters_rel_ordered_avg2 = self.clusters_rel_avg
     
     return data_thickened