def data_frame_to_fbs_matrix(self, filter, axis): """ Retrieves data 'X' and returns in a flatbuffer Matrix. :param filter: filter: dictionary with filter params :param axis: string obs or var :return: flatbuffer Matrix Caveats: * currently only supports access on VAR axis * currently only supports filtering on VAR axis """ if axis != Axis.VAR: raise ValueError("Only VAR dimension access is supported") try: obs_selector, var_selector = self._filter_to_mask(filter, use_slices=False) except (KeyError, IndexError, TypeError) as e: raise FilterError(f"Error parsing filter: {e}") from e if obs_selector is not None: raise FilterError("filtering on obs unsupported") # Currently only handles VAR dimension X = self.slice_columns(self.data._X, var_selector) return encode_matrix_fbs(X, col_idx=np.nonzero(var_selector)[0], row_idx=None)
def annotation_to_fbs_matrix(self, axis, fields=None): if axis == Axis.OBS: df = self.data.obs else: df = self.data.var if fields is not None and len(fields) > 0: df = df[fields] return encode_matrix_fbs(df, col_idx=df.columns)
def annotation_to_fbs_matrix(self, axis, fields=None): if axis == Axis.OBS: if self.labels is not None and not self.labels.empty: df = self.data.obs.join(self.labels, self.config['obs_names']) else: df = self.data.obs else: df = self.data.var if fields is not None and len(fields) > 0: df = df[fields] return encode_matrix_fbs(df, col_idx=df.columns)
def layout_to_fbs_matrix(self): """ Return the default 2-D layout for cells as a FBS Matrix. Caveats: * does not support filtering * only returns Matrix in columnar layout """ try: df_layout = self.data.obsm[f"X_{self.layout_method}"] except ValueError as e: raise PrepareError( f"Layout has not been calculated using {self.layout_method}, " f"please prepare your datafile and relaunch cellxgene") from e normalized_layout = (df_layout - df_layout.min()) / (df_layout.max() - df_layout.min()) return encode_matrix_fbs(normalized_layout.astype(dtype=np.float32), col_idx=None, row_idx=None)
def layout_to_fbs_matrix(self): """ Return the default 2-D layout for cells as a FBS Matrix. Caveats: * does not support filtering * only returns Matrix in columnar layout All embeddings must be individually centered & scaled (isotropically) to a [0, 1] range. """ try: layout_data = [] for layout in self.config["layout"]: full_embedding = self.data.obsm[f"X_{layout}"] embedding = full_embedding[:, :2] # scale isotropically min = embedding.min(axis=0) max = embedding.max(axis=0) scale = np.amax(max - min) normalized_layout = (embedding - min) / scale # translate to center on both axis translate = 0.5 - ((max - min) / scale / 2) normalized_layout = normalized_layout + translate normalized_layout = normalized_layout.astype(dtype=np.float32) layout_data.append( pandas.DataFrame(normalized_layout, columns=[f"{layout}_0", f"{layout}_1"])) except ValueError as e: raise PrepareError( f"Layout has not been calculated using {self.config['layout']}, " f"please prepare your datafile and relaunch cellxgene") from e df = pandas.concat(layout_data, axis=1, copy=False) return encode_matrix_fbs(df, col_idx=df.columns, row_idx=None)
def annotation_to_fbs_matrix(self, axis, fields=None, uid=None, collection=None): if axis == Axis.OBS: if self.config["annotations"]: try: labels = read_labels(self.get_anno_fname(uid, collection)) except Exception as e: raise ScanpyFileError( f"Error while loading label file: {e}, File must be in the .csv format, please check " f"your input and try again." ) else: labels = None if labels is not None and not labels.empty: df = self.data.obs.join(labels, self.config['obs_names']) else: df = self.data.obs else: df = self.data.var if fields is not None and len(fields) > 0: df = df[fields] return encode_matrix_fbs(df, col_idx=df.columns)
def make_fbs(self, data): df = pd.DataFrame(data) return encode_matrix_fbs(matrix=df, row_idx=None, col_idx=df.columns)