def test_controller_misc(): l, c = load() # Select three clusters clusters = [2, 4, 6] spikes = l.get_spikes(clusters=clusters) cluster_spikes = l.get_clusters(clusters=clusters) # Merge these clusters. action, output = c.merge_clusters(clusters) cluster_new = output['cluster_merged'] assert np.array_equal(l.get_spikes(cluster_new), spikes) assert np.all(~np.in1d(clusters, get_indices(l.get_cluster_groups('all')))) # Undo. assert c.can_undo() c.undo() assert np.array_equal(l.get_spikes(cluster_new), []) assert np.all(np.in1d(clusters, get_indices(l.get_cluster_groups('all')))) assert np.array_equal(l.get_clusters(clusters=clusters), cluster_spikes) # Move clusters. action, output = c.move_clusters(clusters, 0) assert action == 'move_clusters' # assert np.array_equal(output['to_select'], [7]) assert np.all(l.get_cluster_groups(clusters) == 0) assert c.can_undo() assert not c.can_redo() # Undo action, output = c.undo() assert action == 'move_clusters_undo' # assert np.array_equal(output['to_select'], clusters) assert np.all(l.get_cluster_groups(clusters) == 3) assert not c.can_undo() assert c.can_redo() # Merge clusters. c.merge_clusters(clusters) assert c.can_undo() assert not c.can_redo() l.close()
def test_controller_merge(): l, c = load() # Select three clusters clusters = [2, 4, 6] spikes = l.get_spikes(clusters=clusters) cluster_spikes = l.get_clusters(clusters=clusters) # Select half of the spikes in these clusters. spikes_sample = spikes[::2] # Merge these clusters. action, output = c.merge_clusters(clusters) cluster_new = output['cluster_merged'] assert action == 'merge_clusters' assert np.array_equal(output['cluster_merged'], cluster_new) assert np.array_equal(output['clusters_to_merge'], sorted(set(clusters))) assert np.array_equal(l.get_spikes(cluster_new), spikes) assert np.all(~np.in1d(clusters, get_indices(l.get_cluster_groups('all')))) # Undo. assert c.can_undo() action, output = c.undo() assert action == 'merge_clusters_undo' assert np.array_equal(output['cluster_merged'], cluster_new) assert np.array_equal(output['clusters_to_merge'], sorted(set(clusters))) assert np.array_equal(l.get_spikes(cluster_new), []) assert np.all(np.in1d(clusters, get_indices(l.get_cluster_groups('all')))) assert np.array_equal(l.get_clusters(clusters=clusters), cluster_spikes) # Redo. assert c.can_redo() action, output = c.redo() assert action == 'merge_clusters' assert np.array_equal(output['cluster_merged'], cluster_new) assert np.array_equal(output['clusters_to_merge'], sorted(set(clusters))) assert np.array_equal(l.get_spikes(cluster_new), spikes) assert np.all(~np.in1d(clusters, get_indices(l.get_cluster_groups('all')))) l.close()
def test_clusterview(): keys = ('cluster_groups,group_colors,group_names,' 'cluster_sizes').split(',') data = get_data() kwargs = {k: data[k] for k in keys} kwargs['cluster_colors'] = data['cluster_colors_full'] # kwargs['background'] = {5: 1, 7: 2} clusters = get_indices(data['cluster_sizes']) quality = pd.Series(np.random.rand(len(clusters)), index=clusters) kwargs['cluster_quality'] = quality kwargs['operators'] = [ lambda self: self.view.set_quality(quality), lambda self: self.view.set_background({ 5: 1, 7: 2 }), lambda self: self.view.set_background({6: 3}), lambda self: self.view.set_background({}), lambda self: (self.close() if USERPREF['test_auto_close'] != False else None), ] # Show the view. window = show_view(ClusterView, **kwargs)
def set_data( self, similarity_matrix=None, cluster_colors_full=None, clusters_hidden=[], ): if similarity_matrix is None: similarity_matrix = np.zeros(0) cluster_colors_full = np.zeros(0) if similarity_matrix.size == 0: similarity_matrix = -np.ones((2, 2)) elif similarity_matrix.shape[0] == 1: similarity_matrix = -np.ones((2, 2)) self.similarity_matrix = similarity_matrix n = similarity_matrix.shape[0] self.texture = colormap(similarity_matrix) # similarity_matrix axes are originally (x, y) from the lower left corner # but when displayed, they are (i, j) from the upper left corner # so we need to transpose self.texture = np.swapaxes(self.texture, 0, 1)[::-1, :, :] # Hide some clusters. tex0 = self.texture.copy() for clu in clusters_hidden: # Inversion of axes in the y axis self.texture[-clu - 1, :, :] = tex0[-clu - 1, :, :] * .5 self.texture[:, clu, :] = tex0[:, clu, :] * .5 self.clusters_unique = get_indices(cluster_colors_full) self.cluster_colors = cluster_colors_full self.nclusters = len(self.clusters_unique)
def split_clusters(self, clusters, clusters_old, cluster_groups, cluster_colors, clusters_new): if not hasattr(clusters, '__len__'): clusters = [clusters] spikes = get_indices(clusters_old) # Find groups and colors of old clusters. cluster_indices_old = np.unique(clusters_old) cluster_indices_new = np.unique(clusters_new) # Get group and color of the new clusters, from the old clusters. groups = self.loader.get_cluster_groups(cluster_indices_old) # colors = self.loader.get_cluster_colors(cluster_indices_old) # Add clusters. for cluster_new, group in zip(cluster_indices_new, groups): self.loader.add_cluster(cluster_new, group, random_color()) # Set the new clusters to the corresponding spikes. self.loader.set_cluster(spikes, clusters_new) # Remove empty clusters. clusters_empty = self.loader.remove_empty_clusters() self.loader.unselect() clusters_to_select = sorted(set(cluster_indices_old).union( set(cluster_indices_new)) - set(clusters_empty)) return dict(clusters_to_split=clusters, clusters_split=get_array(cluster_indices_new), clusters_empty=clusters_empty)
def set_data(self, similarity_matrix=None, cluster_colors_full=None, clusters_hidden=[], ): if similarity_matrix is None: similarity_matrix = np.zeros(0) cluster_colors_full = np.zeros(0) if similarity_matrix.size == 0: similarity_matrix = -np.ones((2, 2)) elif similarity_matrix.shape[0] == 1: similarity_matrix = -np.ones((2, 2)) self.similarity_matrix = similarity_matrix n = similarity_matrix.shape[0] self.texture = colormap(self.similarity_matrix.copy()) # similarity_matrix axes are originally (x, y) from the lower left corner # but when displayed, they are (i, j) from the upper left corner # so we need to transpose self.texture = np.swapaxes(self.texture, 0, 1)[::-1, :, :] # Hide some clusters. tex0 = self.texture.copy() for clu in clusters_hidden: # Inversion of axes in the y axis self.texture[- clu - 1, :, :] = tex0[- clu - 1, :, :] * .5 self.texture[:, clu, :] = tex0[:, clu, :] * .5 self.clusters_unique = get_indices(cluster_colors_full) self.cluster_colors = cluster_colors_full self.nclusters = len(self.clusters_unique)
def split_clusters_undo(self, clusters, clusters_old, cluster_groups, cluster_colors, clusters_new): if not hasattr(clusters, '__len__'): clusters = [clusters] spikes = get_indices(clusters_old) # Find groups and colors of old clusters. cluster_indices_old = np.unique(clusters_old) cluster_indices_new = np.unique(clusters_new) # Add clusters that were removed after the split operation. clusters_empty = sorted(set(cluster_indices_old) - set(cluster_indices_new)) self.loader.add_clusters( clusters_empty, select(cluster_groups, clusters_empty), # select(cluster_colors, clusters_empty), ) # Set the new clusters to the corresponding spikes. self.loader.set_cluster(spikes, clusters_old) # Remove empty clusters. clusters_empty = self.loader.remove_empty_clusters() self.loader.unselect() return dict(clusters_to_split=clusters, clusters_split=get_array(cluster_indices_new), # clusters_empty=clusters_empty )
def split_clusters_undo(self, clusters, clusters_old, cluster_groups, cluster_colors, clusters_new): if not hasattr(clusters, '__len__'): clusters = [clusters] spikes = get_indices(clusters_old) # Find groups and colors of old clusters. cluster_indices_old = np.unique(clusters_old) cluster_indices_new = np.unique(clusters_new) # Add clusters that were removed after the split operation. clusters_empty = sorted( set(cluster_indices_old) - set(cluster_indices_new)) self.loader.add_clusters( clusters_empty, select(cluster_groups, clusters_empty), # select(cluster_colors, clusters_empty), ) # Set the new clusters to the corresponding spikes. self.loader.set_cluster(spikes, clusters_old) # Remove empty clusters. clusters_empty = self.loader.remove_empty_clusters() self.loader.unselect() return dict( clusters_to_split=clusters, clusters_split=get_array(cluster_indices_new), # clusters_empty=clusters_empty )
def split_clusters(self, clusters, clusters_old, cluster_groups, cluster_colors, clusters_new): if not hasattr(clusters, '__len__'): clusters = [clusters] spikes = get_indices(clusters_old) # Find groups and colors of old clusters. cluster_indices_old = np.unique(clusters_old) cluster_indices_new = np.unique(clusters_new) # Get group and color of the new clusters, from the old clusters. groups = self.loader.get_cluster_groups(cluster_indices_old) # colors = self.loader.get_cluster_colors(cluster_indices_old) # Add clusters. self.loader.add_clusters(cluster_indices_new, # HACK: take the group of the first cluster for all new clusters get_array(groups)[0]*np.ones(len(cluster_indices_new)), random_color(len(cluster_indices_new))) # Set the new clusters to the corresponding spikes. self.loader.set_cluster(spikes, clusters_new) # Remove empty clusters. clusters_empty = self.loader.remove_empty_clusters() self.loader.unselect() clusters_to_select = sorted(set(cluster_indices_old).union( set(cluster_indices_new)) - set(clusters_empty)) return dict(clusters_to_split=clusters, clusters_split=get_array(cluster_indices_new), clusters_empty=clusters_empty)
def test_clusterview(): keys = ('cluster_groups,group_colors,group_names,' 'cluster_sizes').split(',') data = get_data() kwargs = {k: data[k] for k in keys} kwargs['cluster_colors'] = data['cluster_colors_full'] # kwargs['background'] = {5: 1, 7: 2} clusters = get_indices(data['cluster_sizes']) quality = pd.Series(np.random.rand(len(clusters)), index=clusters) kwargs['cluster_quality'] = quality kwargs['operators'] = [ lambda self: self.view.set_quality(quality), lambda self: self.view.set_background({5: 1, 7: 2}), lambda self: self.view.set_background({6: 3}), lambda self: self.view.set_background({}), lambda self: (self.close() if USERPREF['test_auto_close'] != False else None), ] # Show the view. window = show_view(ClusterView, **kwargs)
def merge_clusters(self, clusters_old, cluster_groups, cluster_colors, cluster_merged): # Get spikes in clusters to merge. # spikes = self.loader.get_spikes(clusters=clusters_to_merge) spikes = get_indices(clusters_old) clusters_to_merge = get_indices(cluster_groups) group = np.max(get_array(cluster_groups)) # color_old = get_array(cluster_colors)[0] color_new = random_color() self.loader.add_cluster(cluster_merged, group, color_new) # Set the new cluster to the corresponding spikes. self.loader.set_cluster(spikes, cluster_merged) # Remove old clusters. for cluster in clusters_to_merge: self.loader.remove_cluster(cluster) self.loader.unselect() return dict(clusters_to_merge=clusters_to_merge, cluster_merged=cluster_merged, cluster_merged_colors=(color_new, color_new),)
def set_data(self, cluster_groups=None, similarity_matrix=None): """Update the data.""" if cluster_groups is not None: self.clusters_unique = get_array(get_indices(cluster_groups)) self.cluster_groups = get_array(cluster_groups) if (similarity_matrix is not None and similarity_matrix.size > 0): self.matrix = similarity_matrix self.quality = np.diag(self.matrix) assert len(self.cluster_groups) == self.matrix.shape[0]
def set_data(self, cluster_groups=None, similarity_matrix=None): """Update the data.""" if cluster_groups is not None: self.clusters_unique = get_array(get_indices(cluster_groups)) self.cluster_groups = get_array(cluster_groups) if (similarity_matrix is not None and similarity_matrix.size > 0): if len(get_array(cluster_groups)) != similarity_matrix.shape[0]: log.warn(("Cannot update the wizard: cluster_groups " "has {0:d} elements whereas the similarity matrix has {1:d}.").format( len(get_array(cluster_groups)), similarity_matrix.shape[0])) return self.matrix = similarity_matrix self.quality = np.diag(self.matrix)
def set_data( self, similarity_matrix=None, cluster_colors_full=None, clusters_hidden=[], # WARNING: relative indexing ): if similarity_matrix is None: similarity_matrix = np.zeros(0) cluster_colors_full = np.zeros(0) if similarity_matrix.size == 0: similarity_matrix = -np.ones((2, 2)) elif similarity_matrix.shape[0] == 1: similarity_matrix = -np.ones((2, 2)) self.similarity_matrix = similarity_matrix n = similarity_matrix.shape[0] self.texture = colormap(self.similarity_matrix.copy()) # similarity_matrix axes are originally (x, y) from the lower left corner # but when displayed, they are (i, j) from the upper left corner # so we need to transpose self.texture = np.swapaxes(self.texture, 0, 1) self.clusters_unique = get_indices(cluster_colors_full) self.cluster_colors = cluster_colors_full self.nclusters = len(self.clusters_unique) # Remove hidden clusters. indices = np.array( sorted(set(range(self.nclusters)) - set(clusters_hidden)), dtype=np.int32) self.indices = indices if len(indices) >= 2: tex0 = self.texture.copy() self.texture = tex0[[[_] for _ in (indices)], indices, :] else: indices = range(self.nclusters) self.clusters_displayed = self.clusters_unique[indices] self.nclusters_displayed = len(indices) self.texture = self.texture[::-1, :, :]
def set_data(self, cluster_groups=None, similarity_matrix=None): """Update the data.""" if cluster_groups is not None: self.clusters_unique = get_array(get_indices(cluster_groups)) self.cluster_groups = get_array(cluster_groups) if (similarity_matrix is not None and similarity_matrix.size > 0): if len(get_array(cluster_groups)) != similarity_matrix.shape[0]: log.warn(( "Cannot update the wizard: cluster_groups " "has {0:d} elements whereas the similarity matrix has {1:d}." ).format(len(get_array(cluster_groups)), similarity_matrix.shape[0])) return self.matrix = similarity_matrix self.quality = np.diag(self.matrix)
def set_data(self, similarity_matrix=None, cluster_colors_full=None, clusters_hidden=[], # WARNING: relative indexing ): if similarity_matrix is None: similarity_matrix = np.zeros(0) cluster_colors_full = np.zeros(0) if similarity_matrix.size == 0: similarity_matrix = -np.ones((2, 2)) elif similarity_matrix.shape[0] == 1: similarity_matrix = -np.ones((2, 2)) self.similarity_matrix = similarity_matrix n = similarity_matrix.shape[0] self.texture = colormap(self.similarity_matrix.copy()) # similarity_matrix axes are originally (x, y) from the lower left corner # but when displayed, they are (i, j) from the upper left corner # so we need to transpose self.texture = np.swapaxes(self.texture, 0, 1) self.clusters_unique = get_indices(cluster_colors_full) self.cluster_colors = cluster_colors_full self.nclusters = len(self.clusters_unique) # Remove hidden clusters. indices = np.array(sorted(set(range(self.nclusters)) - set(clusters_hidden)), dtype=np.int32) self.indices = indices if len(indices) >= 2: tex0 = self.texture.copy() self.texture = tex0[[[_] for _ in (indices)],indices,:] else: indices = range(self.nclusters) self.clusters_displayed = self.clusters_unique[indices] self.nclusters_displayed = len(indices) self.texture = self.texture[::-1,:,:]
def merge_clusters_undo(self, clusters_old, cluster_groups, cluster_colors, cluster_merged): # Get spikes in clusters to merge. spikes = self.loader.get_spikes(clusters=cluster_merged) clusters_to_merge = get_indices(cluster_groups) # Add old clusters. for cluster, group, color in zip( clusters_to_merge, cluster_groups, cluster_colors): self.loader.add_cluster(cluster, group, color) # Set the new clusters to the corresponding spikes. self.loader.set_cluster(spikes, clusters_old) # Remove merged cluster. self.loader.remove_cluster(cluster_merged) self.loader.unselect() color_old = self.loader.get_cluster_color(clusters_to_merge[0]) color_old2 = self.loader.get_cluster_color(clusters_to_merge[1]) return dict(clusters_to_merge=clusters_to_merge, cluster_merged=cluster_merged, cluster_to_merge_colors=(color_old, color_old2), )
def set_data(self, features=None, features_background=None, spiketimes=None, # a subset of all spikes, disregarding cluster masks=None, # masks for all spikes in selected clusters clusters=None, # clusters for all spikes in selected clusters clusters_selected=None, cluster_colors=None, fetdim=None, nchannels=None, channels=None, nextrafet=None, autozoom=None, # None, or the target cluster duration=None, freq=None, alpha_selected=.75, alpha_background=.25, time_unit=None, ): if features is None: features = np.zeros((0, 2)) features_background = np.zeros((0, 2)) masks = np.zeros((0, 1)) clusters = np.zeros(0, dtype=np.int32) clusters_selected = [] cluster_colors = np.zeros(0, dtype=np.int32) fetdim = 2 nchannels = 1 nextrafet = 0 if features.shape[1] == 1: features = np.tile(features, (1, 4)) if features_background.shape[1] == 1: features_background = np.tile(features_background, (1, 4)) assert fetdim is not None self.duration = duration self.spiketimes = spiketimes self.freq = freq self.interaction_manager.get_processor('grid').update_viewbox() # Feature background alpha value. self.alpha_selected = alpha_selected self.alpha_background = alpha_background # can be 'second' or 'samples' self.time_unit = time_unit # Extract the relevant spikes, but keep the other ones in features_full self.clusters = clusters # Contains all spikes, needed for splitting. self.features_full = features self.features_full_array = get_array(features) # Keep a subset of all spikes in the view. self.nspikes_full = len(features) # > features_nspikes_per_cluster_max spikes ==> take a selection nspikes_max = USERPREF.get('features_nspikes_per_cluster_max', 1000) k = self.nspikes_full // nspikes_max + 1 # self.features = features[::k] subsel = slice(None, None, k) self.features = select(features, subsel) self.features_array = get_array(self.features) # self.features_background contains all non-selected spikes self.features_background = features_background self.features_background_array = get_array(self.features_background) # Background spikes are those which do not belong to the selected clusters self.npoints_background = self.features_background_array.shape[0] self.nspikes_background = self.npoints_background if channels is None: channels = range(nchannels) self.nspikes, self.ndim = self.features.shape self.fetdim = fetdim self.nchannels = nchannels self.channels = channels self.nextrafet = nextrafet self.npoints = self.features.shape[0] if masks is None: masks = np.ones_like(self.features, dtype=np.float32) self.masks = masks # Subselection self.masks = select(self.masks, subsel) self.masks_array = get_array(self.masks) if self.spiketimes is not None: self.spiketimes = select(self.spiketimes, subsel) self.clusters = select(self.clusters, subsel) self.clusters_array = get_array(self.clusters) self.feature_indices = get_indices(self.features) self.feature_full_indices = get_indices(self.features_full) self.feature_indices_array = get_array(self.feature_indices) self.cluster_colors = get_array(cluster_colors, dosort=True) # Relative indexing. if self.npoints > 0: self.clusters_rel = np.digitize(self.clusters_array, sorted(clusters_selected)) - 1 self.clusters_rel_ordered = np.argsort(clusters_selected)[self.clusters_rel] else: self.clusters_rel = np.zeros(0, dtype=np.int32) self.clusters_rel_ordered = np.zeros(0, dtype=np.int32) self.clusters_unique = sorted(clusters_selected) self.nclusters = len(clusters_selected) self.masks_full = self.masks_array.T.ravel() self.clusters_full_depth = self.clusters_rel_ordered self.clusters_full = self.clusters_rel # prepare GPU data self.data = np.empty((self.nspikes, 2), dtype=np.float32) self.data_full = np.empty((self.nspikes_full, 2), dtype=np.float32) self.data_background = np.empty((self.nspikes_background, 2), dtype=np.float32) # set initial projection self.projection_manager.set_data() self.autozoom = autozoom if autozoom is None: self.projection_manager.reset_projection() else: self.projection_manager.auto_projection(autozoom) # update the highlight manager self.highlight_manager.initialize() self.selection_manager.initialize() self.selection_manager.cancel_selection()
def set_data(self, waveforms=None, channels=None, masks=None, clusters=None, # list of clusters that are selected, the order matters clusters_selected=None, cluster_colors=None, geometrical_positions=None, keep_order=None, autozoom=None, ): self.autozoom = autozoom if clusters_selected is None: clusters_selected = [] if waveforms is None: waveforms = np.zeros((0, 1, 1)) masks = np.zeros((0, 1)) clusters = np.zeros(0, dtype=np.int32) cluster_colors = np.zeros(0, dtype=np.int32) clusters_selected = [] self.keep_order = keep_order # Not all waveforms have been selected, so select the appropriate # samples in clusters and masks. self.waveform_indices = get_indices(waveforms) self.waveform_indices_array = get_array(self.waveform_indices) masks = select(masks, self.waveform_indices) clusters = select(clusters, self.waveform_indices) # Convert from Pandas into raw NumPy arrays. self.waveforms_array = get_array(waveforms) self.masks_array = get_array(masks) self.clusters_array = get_array(clusters) # Relative indexing. if len(clusters_selected) > 0 and len(self.waveform_indices) > 0: self.clusters_rel = np.array(np.digitize(self.clusters_array, sorted(clusters_selected)) - 1, dtype=np.int32) self.clusters_rel_ordered = (np.argsort(clusters_selected) [self.clusters_rel]).astype(np.int32) else: self.clusters_rel = np.zeros(0, dtype=np.int32) self.clusters_rel_ordered = np.zeros(0, dtype=np.int32) if self.keep_order: self.clusters_rel_ordered2 = self.clusters_rel_ordered self.cluster_colors_array = get_array(cluster_colors, dosort=True)[np.argsort(clusters_selected)] self.clusters_selected2 = clusters_selected else: self.clusters_rel_ordered2 = self.clusters_rel self.cluster_colors_array = get_array(cluster_colors, dosort=True) self.clusters_selected2 = sorted(clusters_selected) self.nspikes, self.nsamples, self.nchannels = self.waveforms_array.shape if channels is None: channels = range(self.nchannels) self.channels = channels self.npoints = self.waveforms_array.size self.geometrical_positions = geometrical_positions self.clusters_selected = clusters_selected self.clusters_unique = sorted(clusters_selected) self.nclusters = len(clusters_selected) self.waveforms = waveforms self.clusters = clusters # self.cluster_colors = cluster_colors self.masks = masks # Prepare GPU data. self.data = self.prepare_waveform_data() self.masks_full = np.repeat(self.masks_array.T.ravel(), self.nsamples) self.clusters_full = np.tile(np.repeat(self.clusters_rel_ordered2, self.nsamples), self.nchannels) self.clusters_full_depth = np.tile(np.repeat(self.clusters_rel_ordered, self.nsamples), self.nchannels) self.channels_full = np.repeat(np.arange(self.nchannels, dtype=np.int32), self.nspikes * self.nsamples) # Compute average waveforms. self.data_avg = self.prepare_average_waveform_data() self.masks_full_avg = np.repeat(self.masks_avg.T.ravel(), self.nsamples_avg) self.clusters_full_avg = np.tile(np.repeat(self.clusters_rel_ordered_avg2, self.nsamples_avg), self.nchannels_avg) self.clusters_full_depth_avg = np.tile(np.repeat(self.clusters_rel_ordered_avg, self.nsamples_avg), self.nchannels_avg) self.channels_full_avg = np.repeat(np.arange(self.nchannels_avg, dtype=np.int32), self.nspikes_avg * self.nsamples_avg) # position waveforms self.position_manager.set_info(self.nchannels, self.nclusters, geometrical_positions=self.geometrical_positions,) # update the highlight manager self.highlight_manager.initialize()
def set_data(self, waveforms=None, masks=None, clusters=None, # list of clusters that are selected, the order matters clusters_selected=None, cluster_colors=None, geometrical_positions=None, keep_order=None, autozoom=None, ): self.autozoom = autozoom if clusters_selected is None: clusters_selected = [] if waveforms is None: waveforms = np.zeros((0, 1, 1)) masks = np.zeros((0, 1)) clusters = np.zeros(0, dtype=np.int32) cluster_colors = np.zeros(0, dtype=np.int32) clusters_selected = [] self.keep_order = keep_order # Not all waveforms have been selected, so select the appropriate # samples in clusters and masks. self.waveform_indices = get_indices(waveforms) self.waveform_indices_array = get_array(self.waveform_indices) masks = select(masks, self.waveform_indices) clusters = select(clusters, self.waveform_indices) # Convert from Pandas into raw NumPy arrays. self.waveforms_array = get_array(waveforms) self.masks_array = get_array(masks) self.clusters_array = get_array(clusters) # Relative indexing. if len(clusters_selected) > 0: self.clusters_rel = np.array(np.digitize(self.clusters_array, sorted(clusters_selected)) - 1, dtype=np.int32) self.clusters_rel_ordered = (np.argsort(clusters_selected) [self.clusters_rel]).astype(np.int32) else: self.clusters_rel = np.zeros(0, dtype=np.int32) self.clusters_rel_ordered = np.zeros(0, dtype=np.int32) if self.keep_order: self.clusters_rel_ordered2 = self.clusters_rel_ordered self.cluster_colors_array = get_array(cluster_colors, dosort=True)[np.argsort(clusters_selected)] self.clusters_selected2 = clusters_selected else: self.clusters_rel_ordered2 = self.clusters_rel self.cluster_colors_array = get_array(cluster_colors, dosort=True) self.clusters_selected2 = sorted(clusters_selected) self.nspikes, self.nsamples, self.nchannels = self.waveforms_array.shape self.npoints = self.waveforms_array.size self.geometrical_positions = geometrical_positions self.clusters_selected = clusters_selected self.clusters_unique = sorted(clusters_selected) self.nclusters = len(clusters_selected) self.waveforms = waveforms self.clusters = clusters # self.cluster_colors = cluster_colors self.masks = masks # Prepare GPU data. self.data = self.prepare_waveform_data() self.masks_full = np.repeat(self.masks_array.T.ravel(), self.nsamples) self.clusters_full = np.tile(np.repeat(self.clusters_rel_ordered2, self.nsamples), self.nchannels) self.clusters_full_depth = np.tile(np.repeat(self.clusters_rel_ordered, self.nsamples), self.nchannels) self.channels_full = np.repeat(np.arange(self.nchannels, dtype=np.int32), self.nspikes * self.nsamples) # Compute average waveforms. self.data_avg = self.prepare_average_waveform_data() self.masks_full_avg = np.repeat(self.masks_avg.T.ravel(), self.nsamples_avg) self.clusters_full_avg = np.tile(np.repeat(self.clusters_rel_ordered_avg2, self.nsamples_avg), self.nchannels_avg) self.clusters_full_depth_avg = np.tile(np.repeat(self.clusters_rel_ordered_avg, self.nsamples_avg), self.nchannels_avg) self.channels_full_avg = np.repeat(np.arange(self.nchannels_avg, dtype=np.int32), self.nspikes_avg * self.nsamples_avg) # position waveforms self.position_manager.set_info(self.nchannels, self.nclusters, geometrical_positions=self.geometrical_positions,) # update the highlight manager self.highlight_manager.initialize()
def set_data(self, features=None, features_background=None, spiketimes=None, # a subset of all spikes, disregarding cluster masks=None, # masks for all spikes in selected clusters clusters=None, # clusters for all spikes in selected clusters clusters_selected=None, cluster_colors=None, fetdim=None, nchannels=None, nextrafet=None, autozoom=None, # None, or the target cluster duration=None, freq=None, alpha_selected=.75, alpha_background=.25, time_unit=None, ): if features is None: features = np.zeros((0, 2)) features_background = np.zeros((0, 2)) masks = np.zeros((0, 1)) clusters = np.zeros(0, dtype=np.int32) clusters_selected = [] cluster_colors = np.zeros(0, dtype=np.int32) fetdim = 2 nchannels = 1 nextrafet = 0 assert fetdim is not None self.duration = duration self.spiketimes = spiketimes self.freq = freq self.interaction_manager.get_processor('grid').update_viewbox() # Feature background alpha value. self.alpha_selected = alpha_selected self.alpha_background = alpha_background # can be 'second' or 'samples' self.time_unit = time_unit # # Indices of all subset spikes. # indices_all = get_indices(features) # # Select only the clusters for subset of spikes. # clusters = select(clusters, indices_all) # # Indices of subset spikes in selected clusters. # indices_selection = get_indices(clusters) # # Indices of subset spikes that are not in selected clusters. # indices_background = np.setdiff1d(indices_all, indices_selection, True) # Extract the relevant spikes, but keep the other ones in features_full self.clusters = clusters self.clusters_array = get_array(self.clusters) # self.features contains selected spikes. # self.features = select(features, indices_selection) self.features = features self.features_array = get_array(self.features) # self.features_background contains all non-selected spikes # self.features_background = select(features, indices_background) self.features_background = features_background self.features_background_array = get_array(self.features_background) # Background spikes are those which do not belong to the selected clusters self.npoints_background = self.features_background_array.shape[0] self.nspikes_background = self.npoints_background self.nspikes, self.ndim = self.features.shape self.fetdim = fetdim self.nchannels = nchannels self.nextrafet = nextrafet self.npoints = self.features.shape[0] self.masks = masks self.feature_indices = get_indices(self.features) self.feature_indices_array = get_array(self.feature_indices) self.masks_array = get_array(self.masks) self.cluster_colors = get_array(cluster_colors, dosort=True) # Relative indexing. if self.npoints > 0: self.clusters_rel = np.digitize(self.clusters_array, sorted(clusters_selected)) - 1 self.clusters_rel_ordered = np.argsort(clusters_selected)[self.clusters_rel] else: self.clusters_rel = np.zeros(0, dtype=np.int32) self.clusters_rel_ordered = np.zeros(0, dtype=np.int32) self.clusters_unique = sorted(clusters_selected) self.nclusters = len(clusters_selected) self.masks_full = self.masks_array.T.ravel() self.clusters_full_depth = self.clusters_rel_ordered self.clusters_full = self.clusters_rel # prepare GPU data self.data = np.empty((self.nspikes, 2), dtype=np.float32) self.data_background = np.empty((self.nspikes_background, 2), dtype=np.float32) # set initial projection self.projection_manager.set_data() self.autozoom = autozoom if autozoom is None: self.projection_manager.reset_projection() else: self.projection_manager.auto_projection(autozoom) # update the highlight manager self.highlight_manager.initialize() self.selection_manager.initialize() self.selection_manager.cancel_selection()
def set_data( self, features=None, features_background=None, spiketimes=None, # a subset of all spikes, disregarding cluster masks=None, # masks for all spikes in selected clusters clusters=None, # clusters for all spikes in selected clusters clusters_selected=None, cluster_colors=None, fetdim=None, nchannels=None, nextrafet=None, autozoom=None, # None, or the target cluster duration=None, freq=None, alpha_selected=.75, alpha_background=.25, time_unit=None, ): if features is None: features = np.zeros((0, 2)) features_background = np.zeros((0, 2)) masks = np.zeros((0, 1)) clusters = np.zeros(0, dtype=np.int32) clusters_selected = [] cluster_colors = np.zeros(0, dtype=np.int32) fetdim = 2 nchannels = 1 nextrafet = 0 assert fetdim is not None self.duration = duration self.spiketimes = spiketimes self.freq = freq self.interaction_manager.get_processor('grid').update_viewbox() # Feature background alpha value. self.alpha_selected = alpha_selected self.alpha_background = alpha_background # can be 'second' or 'samples' self.time_unit = time_unit # # Indices of all subset spikes. # indices_all = get_indices(features) # # Select only the clusters for subset of spikes. # clusters = select(clusters, indices_all) # # Indices of subset spikes in selected clusters. # indices_selection = get_indices(clusters) # # Indices of subset spikes that are not in selected clusters. # indices_background = np.setdiff1d(indices_all, indices_selection, True) # Extract the relevant spikes, but keep the other ones in features_full self.clusters = clusters self.clusters_array = get_array(self.clusters) # self.features contains selected spikes. # self.features = select(features, indices_selection) self.features = features self.features_array = get_array(self.features) # self.features_background contains all non-selected spikes # self.features_background = select(features, indices_background) self.features_background = features_background self.features_background_array = get_array(self.features_background) # Background spikes are those which do not belong to the selected clusters self.npoints_background = self.features_background_array.shape[0] self.nspikes_background = self.npoints_background self.nspikes, self.ndim = self.features.shape self.fetdim = fetdim self.nchannels = nchannels self.nextrafet = nextrafet self.npoints = self.features.shape[0] self.masks = masks self.feature_indices = get_indices(self.features) self.feature_indices_array = get_array(self.feature_indices) self.masks_array = get_array(self.masks) self.cluster_colors = get_array(cluster_colors, dosort=True) # Relative indexing. if self.npoints > 0: self.clusters_rel = np.digitize(self.clusters_array, sorted(clusters_selected)) - 1 self.clusters_rel_ordered = np.argsort(clusters_selected)[ self.clusters_rel] else: self.clusters_rel = np.zeros(0, dtype=np.int32) self.clusters_rel_ordered = np.zeros(0, dtype=np.int32) self.clusters_unique = sorted(clusters_selected) self.nclusters = len(clusters_selected) self.masks_full = self.masks_array.T.ravel() self.clusters_full_depth = self.clusters_rel_ordered self.clusters_full = self.clusters_rel # prepare GPU data self.data = np.empty((self.nspikes, 2), dtype=np.float32) self.data_background = np.empty((self.nspikes_background, 2), dtype=np.float32) # set initial projection self.projection_manager.set_data() self.autozoom = autozoom if autozoom is None: self.projection_manager.reset_projection() else: self.projection_manager.auto_projection(autozoom) # update the highlight manager self.highlight_manager.initialize() self.selection_manager.initialize() self.selection_manager.cancel_selection()