scdynomics.utils.data

Data handling and splitting functions for scDynOmics

author: jy

class scdynomics.utils.data.Multimodal_Corpus(*args: Any, **kwargs: Any)

Bases: Dataset

A PyTorch Dataset designed for loading and handling multimodal single-cell data.

This dataset integrates with AnnData objects to extract specified modalities (layers) and labels, preparing the raw data to be tokenized and ingested by the model.

__init__(adata_path: str = None, adata: anndata.AnnData = None, backed: bool = True, monomodal: str = None, layers: list = ['X'], label_key: str = None, squeeze_multimodal: bool = False, dtype=numpy.float32)
Parameters:
adata_path: str (default: None)

The file path to the .h5ad file containing the dataset.

adata: ad.AnnData (default: None)

An existing AnnData object. If provided, overrides adata_path.

backed: bool (default: True)

If True, loads the AnnData file in backed mode to minimize memory usage.

monomodal: str (default: None)

Restricts the output to a single specified modality.

layers: list (default: [‘X’])

A list of layers to extract and concatenate from the AnnData object.

label_key: str (default: None)

The column name in adata.obs containing categorical target labels for supervised tasks.

squeeze_multimodal: bool (default: False)

If True, reshapes the output tensor to (1, -1, 1).

dtype: type (default: np.float32)

The numpy data type for the returned arrays.

stratify(class_label: int = None, actual_label: str = None, label_key: str = None) Multimodal_Corpus

Stratify the dataset by the label

Parameters:
class_label: int (default: None)

The integer label to stratify by. If provided, overrides actual_label.

actual_label: str (default: None)

The categorical label to stratify by. If provided, overrides class_label.

label_key: str (default: None)

The column name in adata.obs to use for stratification. If not provided, uses the dataset’s default label_key.

Returns: scdynomics.Multimodal_Corpus

An instance of Multimodal_Corpus containing only the samples corresponding to the specified label.

class scdynomics.utils.data.Repr_Corpus(*args: Any, **kwargs: Any)

Bases: Multimodal_Corpus

Representation data corpus

__init__(adata_path: str = None, adata: anndata.AnnData = None, rep_key: str = 'X_pca', label_key: str = None, dtype=numpy.float32)
Parameters:
adata_path: str (default: None)

The file path to the .h5ad file containing the dataset.

adata: ad.AnnData (default: None)

An existing AnnData object. If provided, overrides adata_path.

backed: bool (default: True)

If True, loads the AnnData file in backed mode to minimize memory usage.

monomodal: str (default: None)

Restricts the output to a single specified modality.

layers: list (default: [‘X’])

A list of layers to extract and concatenate from the AnnData object.

label_key: str (default: None)

The column name in adata.obs containing categorical target labels for supervised tasks.

squeeze_multimodal: bool (default: False)

If True, reshapes the output tensor to (1, -1, 1).

dtype: type (default: np.float32)

The numpy data type for the returned arrays.

class scdynomics.utils.data.Tensor_Corpus(*args: Any, **kwargs: Any)

Bases: Dataset

Dummy dataset for tensor data

__init__(data: torch.Tensor, labels: torch.Tensor = None, parent: torch.utils.data.Dataset = None)
get_class_distribution(translate_label: bool = True)

Get the class distribution

stratify(class_label: int = None)

Stratify the dataset by the label

scdynomics.utils.data.kfold_random_split(dataset, n_splits: int = 1, valid_fraction: float = 0.1, stratified_test: bool = False, stratified_valid: bool = True, random_seed: int = None) tuple

Splits the dataset into training, validation, and testing sets using K-Fold cross-validation.

Parameters:
dataset: scdynomics.Multimodal_Corpus

The input Dataset to be split.

n_splits: int (default: 1)

The total number of folds for cross-validation.

valid_fraction: float (default: 0.1)

Fraction of the fold’s training data held out for validation.

stratified_test: bool (default: False)

If True, utilizes StratifiedKFold instead of standard KFold for the test splits.

stratified_valid: bool (default: True)

If True, the validation split within the fold is stratified.

random_seed: int (default: None)

Random seed for reproducibility.

Returns: tuple

A tuple of lists (train_list, valid_list, test_list) containing the respective subsets for each fold.

scdynomics.utils.data.random_split(dataset, test_fraction: float = None, valid_fraction: float = 0.1, stratified_test: bool = False, stratified_valid: bool = False, random_seed: int = None) tuple

Randomly splits a dataset into training, validation, and test subsets.

Parameters:
dataset: scdynomics.Multimodal_Corpus

The input Dataset to be split.

test_fraction: float (default: None)

The proportion of data allocated to the test set.

valid_fraction: float (default: 0.1)

The proportion of the remaining training data allocated to the validation set.

stratified_test: bool (default: False)

If True, splits the test set stratifying according to the dataset’s label_key.

stratified_valid: bool (default: False)

If True, splits the validation set stratifying according to the dataset’s label_key.

random_seed: int (default: None)

Controls the randomness of the split for reproducibility.

Returns: tuple

A tuple containing lists of (train_list, valid_list, test_list) representing the subset DataLoaders.

Modules

corpus

Dataset corpus for scDynOmics

reader

Methods to read in data.

splitter

Data splitter for scDynOmics datasets