def renumber_clusters(clusters, cluster_info): clusters_unique = get_array(get_indices(cluster_info)) nclusters = len(clusters_unique) assert np.array_equal(clusters_unique, np.unique(clusters)) clusters_array = get_array(clusters) groups = get_array(cluster_info['group']) colors = get_array(cluster_info['color']) groups_unique = np.unique(groups) # Reorder clusters according to the group. clusters_unique_reordered = np.hstack( [sorted(clusters_unique[groups == group]) for group in groups_unique]) # WARNING: there's a +2 offset to avoid conflicts with the old convention # cluster 0 = noise, cluster 1 = MUA. clusters_renumbered = reorder(clusters_array, clusters_unique_reordered) + 2 cluster_permutation = reorder(clusters_unique_reordered, clusters_unique) # Reorder cluster info. groups_reordered = groups[cluster_permutation] colors_reordered = colors[cluster_permutation] # Recreate new cluster info. cluster_info_reordered = pd.DataFrame( { 'color': colors_reordered, 'group': groups_reordered }, dtype=np.int32, index=(np.arange(nclusters) + 2)) return clusters_renumbered, cluster_info_reordered
def kwa_to_json(kwa_dict): """Convert a KWA dictionary to JSON. cluster_colors and group_colors are pandas.Series objects.""" kwa_full = {} kwa_full['shanks'] = [] for shank, kwa in kwa_dict['shanks'].iteritems(): cluster_colors = kwa['cluster_colors'] group_colors = kwa['group_colors'] clusters = get_indices(cluster_colors) groups = get_indices(group_colors) kwa_shank = dict( clusters=[{'cluster': str(cluster), 'color': str(cluster_colors[cluster])} for cluster in clusters], groups_of_clusters=[{'group': str(group), 'color': str(group_colors[group])} for group in groups], shank_index=shank ) kwa_full['shanks'].append(kwa_shank) return json.dumps(kwa_full, indent=4)
def get_next_cluster(self, cluster): cluster_groups = self.get_cluster_groups('all') group = get_array(self.get_cluster_groups(cluster)) clusters = get_indices(cluster_groups) cluster_groups = get_array(cluster_groups) samegroup = (cluster_groups == group) & (clusters > cluster) i = np.nonzero(samegroup)[0] if len(i) > 0: return clusters[i[0]] else: return cluster
def get_next_cluster(self, cluster): cluster_groups = self.get_cluster_groups('all') group = self.get_cluster_groups(cluster) clusters = get_indices(cluster_groups) cluster_groups = get_array(cluster_groups) samegroup = (cluster_groups == group) & (clusters > cluster) i = np.nonzero(samegroup)[0] if len(i) > 0: return clusters[i[0]] else: return cluster
def renumber_clusters(clusters, cluster_info): clusters_unique = get_array(get_indices(cluster_info)) nclusters = len(clusters_unique) assert np.array_equal(clusters_unique, np.unique(clusters)) clusters_array = get_array(clusters) groups = get_array(cluster_info["group"]) colors = get_array(cluster_info["color"]) groups_unique = np.unique(groups) # Reorder clusters according to the group. clusters_unique_reordered = np.hstack([sorted(clusters_unique[groups == group]) for group in groups_unique]) # WARNING: there's a +2 offset to avoid conflicts with the old convention # cluster 0 = noise, cluster 1 = MUA. clusters_renumbered = reorder(clusters_array, clusters_unique_reordered) + 2 cluster_permutation = reorder(clusters_unique_reordered, clusters_unique) # Reorder cluster info. groups_reordered = groups[cluster_permutation] colors_reordered = colors[cluster_permutation] # Recreate new cluster info. cluster_info_reordered = pd.DataFrame( {"color": colors_reordered, "group": groups_reordered}, dtype=np.int32, index=(np.arange(nclusters) + 2) ) return clusters_renumbered, cluster_info_reordered
def get_new_group(self): groups = get_indices(self.group_names).values if len(groups) > 0: return groups.max() + 1 else: return 0
def get_spikes(self, clusters=None): if clusters is None: clusters = self.clusters_selected return get_indices(self.get_clusters(clusters=clusters))
def save(self, renumber=False): # Report progress. self.report_progress_save(1, 6) self.update_cluster_info() self.update_group_info() # Renumber internal variables, knowing that in this case the file # will be automatically reloaded right afterwards. if renumber: self.renumber() self.clusters = self.clusters_renumbered self.cluster_info = self.cluster_info_renumbered self._update_data() # Update the changes in the HDF5 tables. self.spike_table.cols.cluster_manual[:] = get_array(self.clusters) # Report progress. self.report_progress_save(2, 6) # Update the clusters table. # -------------------------- # Add/remove rows to match the new number of clusters. self._update_table_size(self.clusters_table, len(self.get_clusters_unique())) self.clusters_table.cols.cluster[:] = self.get_clusters_unique() self.clusters_table.cols.group[:] = np.array(self.cluster_info['group']) # Report progress. self.report_progress_save(3, 6) # Update the group table. # ----------------------- # Add/remove rows to match the new number of clusters. groups = get_array(get_indices(self.group_info)) self._update_table_size( self.groups_table, len(groups), ) self.groups_table.cols.group[:] = groups self.groups_table.cols.name[:] = list(self.group_info['name']) # Commit the changes on disk. self.kwik.flush() # Report progress. self.report_progress_save(4, 6) # Save the CLU file. # ------------------ save_clusters(self.filename_clu, convert_to_clu(self.clusters, self.cluster_info['group'])) # Report progress. self.report_progress_save(5, 6) # Update the KWA file. # -------------------- kwa={} kwa['shanks'] = { shank: dict( cluster_colors=self.cluster_info['color'], group_colors=self.group_info['color'], ) for shank in self.shanks } write_kwa(self.filename_kwa, kwa) # Report progress. self.report_progress_save(6, 6)