diff --git a/stereo/algorithm/community_detection.py b/stereo/algorithm/community_detection.py index 6525f4bf..50ee8fba 100644 --- a/stereo/algorithm/community_detection.py +++ b/stereo/algorithm/community_detection.py @@ -235,7 +235,8 @@ def _main(self, slices, annotation="annotation", **kwargs): f'tissue_{algo.method_key}'] if np.nan in self.slices[slice_id]._ann_data.obs['cell_communities'].values: if 'unknown' not in self.slices[slice_id]._ann_data.obs['cell_communities'].cat.categories: - self.slices[slice_id]._ann_data.obs['cell_communities'] = self.slices[slice_id]._ann_data.obs['cell_communities'].cat.add_categories('unknown') + self.slices[slice_id]._ann_data.obs['cell_communities'] = self.slices[slice_id]._ann_data.obs[ + 'cell_communities'].cat.add_categories('unknown') self.slices[slice_id]._ann_data.obs['cell_communities'].fillna('unknown', inplace=True) # self.slices[slice_id]._ann_data.obs['cell_communities'].fillna('unknown', inplace=True) diff --git a/stereo/algorithm/regulatory_network_inference/main.py b/stereo/algorithm/regulatory_network_inference/main.py index af115413..25c6b06c 100644 --- a/stereo/algorithm/regulatory_network_inference/main.py +++ b/stereo/algorithm/regulatory_network_inference/main.py @@ -92,7 +92,7 @@ def main(self, self.use_raw = use_raw if use_raw and self.stereo_exp_data.raw is None: raise Exception("The raw data is not found, you need to run 'raw_checkpoint()' first.") - + if use_raw: logger.info('the raw expression matrix will be used.') matrix = self.stereo_exp_data.raw.to_df() @@ -523,12 +523,12 @@ def regulons_to_csv(self, regulon_list: list, fn: str = 'regulon_list.csv', fn_p w.writerows(regulon_dict.items()) def to_loom( - self, - matrix: pd.DataFrame, - auc_matrix: pd.DataFrame, - regulons: list, - loom_fn: str = 'grn_output.loom', - fn_prefix: str = None + self, + matrix: pd.DataFrame, + auc_matrix: pd.DataFrame, + regulons: list, + loom_fn: str = 'grn_output.loom', + fn_prefix: str = None ): """ Save GRN results in one loom file diff --git a/stereo/core/ms_pipeline.py b/stereo/core/ms_pipeline.py index 8df651d7..0b96f471 100644 --- a/stereo/core/ms_pipeline.py +++ b/stereo/core/ms_pipeline.py @@ -34,15 +34,15 @@ def result(self): @result.setter def result(self, new_result): self._result = new_result - + @property def key_record(self): return self._key_record - + @key_record.setter def key_record(self, key_record): self._key_record = key_record - + # @property # def scope_data(self): # return self._scope_data @@ -65,7 +65,7 @@ def _use_integrate_method(self, item, *args, **kwargs): ms_data_view = self.ms_data[scope] if not ms_data_view.merged_data: ms_data_view.integrate(result=self.ms_data.tl.result) - + # key_name = "scope_[" + ",".join( # [str(self.ms_data._names.index(name)) for name in ms_data_view._names]) + "]" # self._scope_data[key_name] = self.ms_data._merged_data @@ -105,10 +105,10 @@ def contain_method(item): def reset_key_record(key, res_key): key_name = "scope_[" + ",".join( [str(self.ms_data._names.index(name)) for name in ms_data_view._names]) + "]" - + ms_data_view._merged_data.tl._reset_key_record(key, res_key) self._key_record[key_name] = ms_data_view._merged_data.tl.key_record - + ms_data_view._merged_data.tl.reset_key_record = reset_key_record new_attr = self.__class__.BASE_CLASS.__dict__.get(item, None) diff --git a/stereo/core/st_pipeline.py b/stereo/core/st_pipeline.py index 069e70af..dfd6f12a 100644 --- a/stereo/core/st_pipeline.py +++ b/stereo/core/st_pipeline.py @@ -1322,10 +1322,12 @@ def annotation( cluster_res: pd.DataFrame = self.result[cluster_res_key] - if isinstance(annotation_information, (list, np.ndarray)) and len(annotation_information) != cluster_res['group'].cat.categories.size: + if isinstance(annotation_information, (list, np.ndarray)) and \ + len(annotation_information) != cluster_res['group'].cat.categories.size: raise Exception(f"The length of annotation information is {len(annotation_information)}, \ - not equal to the categories of cluster result whoes lenght is {cluster_res['group'].cat.categories.size}.") - + not equal to the categories of cluster result whoes" + f" lenght is {cluster_res['group'].cat.categories.size}.") + if isinstance(annotation_information, (list, np.ndarray)): new_categories = np.array(annotation_information, dtype='U') elif isinstance(annotation_information, dict): @@ -1334,8 +1336,8 @@ def annotation( new_categories_list.append(annotation_information[i]) new_categories = np.array(new_categories_list, dtype='U') else: - raise TypeError(f"The type of 'annotation_information' only supports list, ndarray or dict.") - + raise TypeError("The type of 'annotation_information' only supports list, ndarray or dict.") + new_categories_values = new_categories[cluster_res['group'].cat.codes] self.result[res_key] = pd.DataFrame(data={ diff --git a/stereo/core/stereo_exp_data.py b/stereo/core/stereo_exp_data.py index 6c71e083..f44e48fa 100644 --- a/stereo/core/stereo_exp_data.py +++ b/stereo/core/stereo_exp_data.py @@ -601,7 +601,7 @@ def reset_position(self): self.position[idx] += self.position_min[bno] self.position_offset = None self.position_min = None - + def __add__(self, other): from stereo.core.ms_data import MSData if isinstance(other, StereoExpData): @@ -767,7 +767,7 @@ def position_z(self, position_z: np.ndarray): @property def bin_type(self): return self._ann_data.uns.get('bin_type', 'bins') - + @bin_type.setter def bin_type(self, bin_type): self.bin_type_check(bin_type) @@ -776,7 +776,7 @@ def bin_type(self, bin_type): @property def bin_size(self): return self._ann_data.uns.get('bin_size', 1) - + @bin_size.setter def bin_size(self, bin_size): self._ann_data.uns['bin_size'] = bin_size @@ -793,7 +793,7 @@ def sn(self): for _, row in sn_data.iterrows(): sn[row['batch']] = row['sn'] return sn - + @sn.setter def sn(self, sn): if isinstance(sn, str):