pegasusio/unimodal_data.py
def from_anndata(self, data: anndata.AnnData, genome: str = None, modality: str = None) -> None:
        """ Initialize from an anndata object, try best not to copy
            If genome/modality is not None, set 'genome'/'modality' as genome/modality
        """
        if data.is_view:
            data = data.copy()

        self.barcode_metadata = data.obs
        self.barcode_metadata.index.name = "barcodekey"

        self.feature_metadata = data.var
        self.feature_metadata.index.name = "featurekey"
        if "gene_ids" in self.feature_metadata:
            self.feature_metadata.rename(columns = {"gene_ids": "featureid"}, inplace = True)

        self.matrices = DataDict({"X": csr_matrix(data.X)}) # csr_matrix will not copy X.data
        if data.raw is not None:
            self.matrices["raw.X"] = csr_matrix(data.raw.X)
        for key, value in data.layers.items():
            self.matrices[key] = csr_matrix(value)

        def _create_data_dict(old_dict: dict) -> DataDict:
            from pandas.api.types import is_dict_like
            new_dict = dict()
            for key, value in old_dict.items():
                if str(type(value)).find("anndata") >= 0:
                    # This is anndata defined type
                    if is_dict_like(value):
                        new_dict[key] = dict(value)
                    else:
                        logger.warning(f"{key} is in anndata-defined data type {type(value)} and thus skipped!")
                else:
                    new_dict[key] = value
            return DataDict(new_dict)

        self.barcode_multiarrays = _create_data_dict(data.obsm)
        self.feature_multiarrays = _create_data_dict(data.varm)
        self.barcode_multigraphs = _create_data_dict(data.obsp) # Note that we do not check if data.obsp contains sparse matrix
        self.feature_multigraphs = _create_data_dict(data.varp) # Note that we do not check if data.obsp contains sparse matrix
        self.metadata = _create_data_dict(data.uns)
        _set_genome(self.metadata, genome)
        _set_modality(self.metadata, modality)


        self._cur_matrix = "X"
        self._shape = data.shape
 Initialize from an anndata object, try best not to copy
            If genome/modality is not None, set 'genome'/'modality' as genome/modality
        
Initialize from an anndata object, try best not to copy
If genome/modality is not None, set 'genome'/'modality' as genome/modality
Initialize from an anndata object try best not to copy If genome / modality is not None set ' genome ' / ' modality ' as genome / modality
def from_anndata(self, data: anndata.AnnData, genome: str = None, modality: str = None) -> None:
        if data.is_view:
            data = data.copy()
        self.barcode_metadata = data.obs
        self.barcode_metadata.index.name = "barcodekey"
        self.feature_metadata = data.var
        self.feature_metadata.index.name = "featurekey"
        if "gene_ids" in self.feature_metadata:
            self.feature_metadata.rename(columns = {"gene_ids": "featureid"}, inplace = True)
        self.matrices = DataDict({"X": csr_matrix(data.X)}) 
        if data.raw is not None:
            self.matrices["raw.X"] = csr_matrix(data.raw.X)
        for key, value in data.layers.items():
            self.matrices[key] = csr_matrix(value)
        def _create_data_dict(old_dict: dict) -> DataDict:
            from pandas.api.types import is_dict_like
            new_dict = dict()
            for key, value in old_dict.items():
                if str(type(value)).find("anndata") >= 0:
                    if is_dict_like(value):
                        new_dict[key] = dict(value)
                    else:
                        logger.warning(f"{key} is in anndata-defined data type {type(value)} and thus skipped!")
                else:
                    new_dict[key] = value
            return DataDict(new_dict)
        self.barcode_multiarrays = _create_data_dict(data.obsm)
        self.feature_multiarrays = _create_data_dict(data.varm)
        self.barcode_multigraphs = _create_data_dict(data.obsp) 
        self.feature_multigraphs = _create_data_dict(data.varp) 
        self.metadata = _create_data_dict(data.uns)
        _set_genome(self.metadata, genome)
        _set_modality(self.metadata, modality)
        self._cur_matrix = "X"
        self._shape = data.shape
def from_anndata ( self , data : anndata . AnnData , genome : str = None , modality : str = None ) -> None : if data . is_view : data = data . copy ( ) self . barcode_metadata = data . obs self . barcode_metadata . index . name = "barcodekey" self . feature_metadata = data . var self . feature_metadata . index . name = "featurekey" if "gene_ids" in self . feature_metadata : self . feature_metadata . rename ( columns = { "gene_ids" : "featureid" } , inplace = True ) self . matrices = DataDict ( { "X" : csr_matrix ( data . X ) } ) if data . raw is not None : self . matrices [ "raw.X" ] = csr_matrix ( data . raw . X ) for key , value in data . layers . items ( ) : self . matrices [ key ] = csr_matrix ( value ) def _create_data_dict ( old_dict : dict ) -> DataDict : from pandas . api . types import is_dict_like new_dict = dict ( ) for key , value in old_dict . items ( ) : if str ( type ( value ) ) . find ( "anndata" ) >= 0 : if is_dict_like ( value ) : new_dict [ key ] = dict ( value ) else : logger . warning ( f"{key} is in anndata-defined data type {type(value)} and thus skipped!" ) else : new_dict [ key ] = value return DataDict ( new_dict ) self . barcode_multiarrays = _create_data_dict ( data . obsm ) self . feature_multiarrays = _create_data_dict ( data . varm ) self . barcode_multigraphs = _create_data_dict ( data . obsp ) self . feature_multigraphs = _create_data_dict ( data . varp ) self . metadata = _create_data_dict ( data . uns ) _set_genome ( self . metadata , genome ) _set_modality ( self . metadata , modality ) self . _cur_matrix = "X" self . _shape = data . shape
Initialize from an anndata object, try best not to copy
If genome/modality is not None, set 'genome'/'modality' as genome/modality
Initialize from an anndata object try best not to copy If genome / modality is not None set ' genome ' / ' modality ' as genome / modality
""" Initialize from an anndata object, try best not to copy
            If genome/modality is not None, set 'genome'/'modality' as genome/modality
        """ # csr_matrix will not copy X.data # This is anndata defined type # Note that we do not check if data.obsp contains sparse matrix # Note that we do not check if data.obsp contains sparse matrix
self
data
anndata.AnnData
genome
str
modality
str