### Define Installation Hints Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/check.html A collection of predefined installation hint messages for common dependencies like bedtools and plotly. ```python INSTALL_HINTS = types.SimpleNamespace( bedtools=( "You may install bedtools following the guide from " "https://bedtools.readthedocs.io/en/latest/content/installation.html, " "or use `conda install -c bioconda bedtools` " "if a conda environment is being used." ), plotly=( "You may install plotly following the guide from " "https://plotly.com/python/getting-started/, " "or use `conda install -c plotly plotly` " "if a conda environment is being used." ), ) ``` -------------------------------- ### Install pyGenomeTracks Source: https://scglue.readthedocs.io/en/latest/reginf.html Install the pyGenomeTracks tool via conda. ```bash conda install -c bioconda pygenometracks ``` -------------------------------- ### File I/O Example Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt Demonstrates writing data to a file. Ensure the directory has write permissions. ```python with open('output.txt', 'w') as f: f.write('This is some output.\n') f.write('Another line.\n') ``` -------------------------------- ### Initialize SCGLUE Environment Source: https://scglue.readthedocs.io/en/latest/_sources/preprocessing.ipynb.txt Basic setup and imports required to begin using the SCGLUE library. ```python import scglue import scanpy as sc ``` -------------------------------- ### Docker Dockerfile Example Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt A basic Dockerfile to create a container image. This is used for containerizing applications. ```dockerfile FROM python:3.9-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD ["python", "app.py"] ``` -------------------------------- ### Configuration File Reading Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt Example of reading configuration from a JSON file. This is common for setting up experiments or parameters. ```python import json with open('config.json', 'r') as f: config = json.load(f) print(config) ``` -------------------------------- ### YAML Configuration Example Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt A sample YAML configuration file. YAML is often used for configuration due to its human-readable format. ```yaml database: host: localhost port: 5432 username: admin password: secret_password ``` -------------------------------- ### Creating a Simple Web Server Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt A basic example of setting up a simple HTTP server using Python's built-in http.server module. Useful for local testing. ```python from http.server import SimpleHTTPRequestHandler, HTTPServer PORT = 8000 Handler = SimpleHTTPRequestHandler with HTTPServer(('', PORT), Handler) as httpd: print(f"Serving at port {PORT}") httpd.serve_forever() ``` -------------------------------- ### Asynchronous Task Example Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt Demonstrates a basic asynchronous function using `async` and `await`. Requires Python 3.5+. ```python import asyncio async def my_async_task(): await asyncio.sleep(1) return 'Task completed' async def main(): result = await my_async_task() print(result) asyncio.run(main()) ``` -------------------------------- ### Install pyscenic Source: https://scglue.readthedocs.io/en/latest/reginf.html Install the pyscenic package and its dependencies. ```bash conda install -c conda-forge pyarrow cytoolz pip install pyscenic ``` -------------------------------- ### Setup Training Plugins Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/glue.html Configures and adds plugins for training, including Tensorboard for logging and LRScheduler/EarlyStopping for learning rate management and model convergence. Use this to customize the training process with monitoring and optimization aids. ```python default_plugins = [Tensorboard()] if reduce_lr_patience: default_plugins.append( LRScheduler( self.vae_optim, self.dsc_optim, monitor=self.earlystop_loss, patience=reduce_lr_patience, burnin=self.align_burnin if safe_burnin else 0, ) ) if patience: default_plugins.append( EarlyStopping( monitor=self.earlystop_loss, patience=patience, burnin=self.align_burnin if safe_burnin else 0, wait_n_lrs=wait_n_lrs or 0, ) ) plugins = default_plugins + (plugins or []) ``` -------------------------------- ### Data Visualization with Matplotlib Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt Example of creating a scatter plot using Matplotlib. Requires Matplotlib to be installed. ```python import matplotlib.pyplot as plt x = np.random.rand(50) y = np.random.rand(50) colors = np.random.rand(50) sizes = 1000 * np.random.rand(50) plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='viridis') plt.colorbar() plt.show() ``` -------------------------------- ### Model Training Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/base.html Provides information on how to start the training process using the model's trainer. ```APIDOC ## fit ### Description Alias for ``.trainer.fit``. Starts the model training process. ### Parameters - **args** - Positional arguments passed to the ``.trainer.fit`` method. - **kwargs** - Keyword arguments passed to the ``.trainer.fit`` method. ### Note Subclasses may override arguments for API definition. ``` -------------------------------- ### CSS Styling Example Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt Basic CSS to style an HTML element. This demonstrates how to change the appearance of web content. ```css h1 { color: blue; text-align: center; } ``` -------------------------------- ### Install scglue via Pip Source: https://scglue.readthedocs.io/en/latest/_sources/install.rst.txt Use this command to install the scglue package using the pip package manager. ```bash pip install scglue ``` -------------------------------- ### Install scglue via Conda Source: https://scglue.readthedocs.io/en/latest/_sources/install.rst.txt Use these commands to install scglue with or without GPU support in a conda environment. ```bash conda install -c conda-forge -c bioconda scglue # CPU only conda install -c conda-forge -c bioconda scglue pytorch-gpu # With GPU support ``` -------------------------------- ### Class Definition Example Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt Demonstrates the definition of a Python class with an initializer and a method. Useful for object-oriented programming. ```python class MyClass: def __init__(self, value): self.value = value def display(self): print(f'Value is: {self.value}') obj = MyClass(10) obj.display() ``` -------------------------------- ### SQL Query Example Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt A basic SQL query to select data from a table. This is fundamental for interacting with relational databases. ```sql SELECT id, name FROM users WHERE id > 10; ``` -------------------------------- ### Fit Model using Trainer Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/base.html An alias for `.trainer.fit`. This method starts the model training process using the configured trainer. ```python self.trainer.fit(*args, **kwargs) ``` -------------------------------- ### Prepare Dataset for Custom Shuffling Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/data.html Prepares the dataset for custom shuffling by initializing multiprocessing queues and starting background worker processes. Ensures any existing workers are cleaned up first. ```python def prepare_shuffle(self, num_workers: int = 1, random_seed: int = 0) -> None: r""" Prepare dataset for custom shuffling Parameters ---------- num_workers Number of background workers for data shuffling random_seed Initial random seed (will increase by 1 with every shuffle call) """ if self.has_workers: self.clean() self_processes = processes[id(self)] self.shuffle_seed = random_seed if num_workers: self.seed_queue = multiprocessing.Queue() self.propose_queue = multiprocessing.Queue() for i in range(num_workers): p = multiprocessing.Process(target=self.shuffle_worker) p.start() self.logger.debug("Started background process: %d", p.pid) self_processes[p.pid] = p self.seed_queue.put(self.shuffle_seed + i) ``` -------------------------------- ### Initialize SCGLUETrainer Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/scglue.html Initializes the trainer with network parameters and optimization settings. ```python def __init__( self, net: SCGLUE, lam_data: float = None, lam_kl: float = None, lam_graph: float = None, lam_align: float = None, lam_sup: float = None, dsc_steps: int = None, normalize_u: bool = None, modality_weight: Mapping[str, float] = None, optim: str = None, lr: float = None, **kwargs, ) -> None: super().__init__( net, lam_data=lam_data, lam_kl=lam_kl, lam_graph=lam_graph, lam_align=lam_align, dsc_steps=dsc_steps, modality_weight=modality_weight, optim=optim, lr=lr, **kwargs, ) required_kwargs = ("lam_sup", "normalize_u") for required_kwarg in required_kwargs: if locals()[required_kwarg] is None: raise ValueError(f"`{required_kwarg}` must be specified!") self.lam_sup = lam_sup self.normalize_u = normalize_u self.freeze_u = False if net.u2c: self.required_losses.append("sup_loss") self.vae_optim = getattr(torch.optim, optim)( chain( self.net.g2v.parameters(), self.net.v2g.parameters(), self.net.x2u.parameters(), self.net.u2x.parameters(), self.net.u2c.parameters(), ), lr=self.lr, **kwargs, ) ``` -------------------------------- ### Strand-Specific Start Site Conversion Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/genomics.html Converts genomic features to strand-specific start sites. Raises a ValueError if features are not strand-specific. ```python def strand_specific_start_site(self) -> "Bed": r""" Convert to strand-specific start sites of genomic features Returns ------- start_site_bed A new :class:`Bed` object, containing strand-specific start sites of the current :class:`Bed` object """ if set(self["strand"]) != set(["+", "-"]): raise ValueError("Not all features are strand specific!") df = pd.DataFrame(self, copy=True) pos_strand = df.query("strand == '+'").index neg_strand = df.query("strand == '-'").index df.loc[pos_strand, "chromEnd"] = df.loc[pos_strand, "chromStart"] + 1 df.loc[neg_strand, "chromStart"] = df.loc[neg_strand, "chromEnd"] - 1 return type(self)(df) ``` -------------------------------- ### Advanced: Data Preprocessing Example Source: https://scglue.readthedocs.io/en/latest/_sources/preprocessing.ipynb.txt Example of preprocessing AnnData objects before integration, such as normalization and feature selection. This is crucial for optimal results. ```python import scglue import anndata ad = anndata.read_h5ad("data.h5ad") ad.X = scglue. یک مدل.normalize(ad.X) ad = scglue. یک مدل.select_features(ad, n_top_genes=2000) model = scglue. یک مدل(omics=["rna", "atac"]) model.train(ad) ``` -------------------------------- ### Git Commit Message Example Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt An example of a conventional Git commit message. Following conventions helps in maintaining a clean commit history. ```git feat: Add user authentication module Implements user registration, login, and logout functionality. Includes password hashing and JWT generation. ``` -------------------------------- ### Model Initialization and Properties Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/base.html Details on how to initialize a Model and access its network and trainer properties. ```APIDOC ## Model Class ### Description Abstract model class that serves as a base for specific model implementations. ### Parameters - **net** (torch.nn.Module) - The neural network architecture to be used. - **args** - Positional arguments passed to the network constructor. - **kwargs** - Keyword arguments passed to the network constructor. ### Properties - **net** (torch.nn.Module) - Read-only access to the neural network module. - **trainer** (Trainer) - Read-only access to the trainer instance. Raises RuntimeError if not compiled. ### Note Subclasses may override arguments for API definition. ``` -------------------------------- ### scglue.check.check_deps Source: https://scglue.readthedocs.io/en/latest/api/scglue.check.html Checks whether certain dependencies are installed. ```APIDOC ## scglue.check.check_deps ### Description Check whether certain dependencies are installed. ### Method (Not specified, likely a Python function call) ### Endpoint (Not applicable, this is a Python function) ### Parameters (Parameters not explicitly defined in the provided text. Assuming it takes dependency names as input.) ### Request Example ```python import scglue # Example: Check for 'numpy' and 'pandas' scglue.check.check_deps(['numpy', 'pandas']) ``` ### Response (Response details not explicitly defined in the provided text. Likely returns a boolean or raises an error.) ``` -------------------------------- ### GET scglue.utils.LogManager.get_logger Source: https://scglue.readthedocs.io/en/latest/api/scglue.utils.LogManager.get_logger.html Retrieves a logger instance by its specified name. ```APIDOC ## GET scglue.utils.LogManager.get_logger ### Description Get a logger by name. ### Parameters #### Path Parameters - **name** (string) - Required - The name of the logger to retrieve. ### Response #### Success Response (200) - **logger** (logging.Logger) - The requested logger instance. ``` -------------------------------- ### scglue.data.AllCoolsLSI Class Source: https://scglue.readthedocs.io/en/latest/api/scglue.data.AllCoolsLSI.html Documentation for the AllCoolsLSI class constructor and its available methods. ```APIDOC ## Class: scglue.data.AllCoolsLSI ### Description Latent Semantic Indexing (LSI) implementation for AllCools data. ### Parameters - **scale_factor** (int) - Default: 100000 - **n_components** (int) - Default: 100 - **algorithm** (str) - Default: 'arpack' - **random_state** (int) - Default: 0 - **n_iter** (int) - Default: 5 - **idf** (optional) - Default: None - **model** (optional) - Default: None ### Methods - **fit**: Fits the LSI model to the provided data. - **fit_transform**: Fits the model and transforms the data in one step. - **transform**: Transforms the provided data using the fitted model. ``` -------------------------------- ### GET /scglue/models/scclue/SCCLUETrainer/state_dict Source: https://scglue.readthedocs.io/en/latest/api/scglue.models.scclue.SCCLUETrainer.state_dict.html Retrieves the state dictionary of the SCCLUETrainer model. ```APIDOC ## GET /scglue/models/scclue/SCCLUETrainer/state_dict ### Description Returns the state dictionary of the SCCLUETrainer instance, which contains the model's parameters and buffers. ### Method GET ### Endpoint scglue.models.scclue.SCCLUETrainer.state_dict() ### Response #### Success Response (200) - **state_dict** (typing.Mapping[str, typing.Any]) - A mapping containing the model state. ``` -------------------------------- ### Database Interaction (SQLite) Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt Shows how to connect to an SQLite database, create a table, and insert data. Ensure the `sqlite3` module is available. ```python import sqlite3 conn = sqlite3.connect('mydatabase.db') cursor = conn.cursor() cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)''') cursor.execute("INSERT INTO users (name) VALUES ('Alice')") conn.commit() cursor.execute("SELECT * FROM users") print(cursor.fetchall()) conn.close() ``` -------------------------------- ### Get Losses Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/base.html Retrieves loss values for a given data loader. ```APIDOC ## get_losses ### Description Retrieves loss values for the given data loader. This is an alias for ``.trainer.get_losses``. ### Parameters - **loader** (Iterable) - The data loader to use for calculating losses. - **args** - Positional arguments passed to the ``.trainer.get_losses`` method. - **kwargs** - Keyword arguments passed to the ``.trainer.get_losses`` method. ### Returns - **loss_dict** (Mapping[str, float]) - A dictionary containing the calculated loss values. ``` -------------------------------- ### check_deps Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/check.html Checks whether specified dependencies are installed and meet version requirements. ```APIDOC ## check_deps ### Description Checks whether certain dependencies are installed and meet the required versions. ### Parameters #### Arguments - **args** (list) - Required - A list of dependency names to check. ``` -------------------------------- ### Fit Model on Datasets Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/scglue.html Initialize datasets and graph structures to begin the model training process. ```python def fit( # pylint: disable=arguments-differ self, adatas: Mapping[str, AnnData], graph: nx.Graph, neg_samples: int = 10, val_split: float = 0.1, data_batch_size: int = 128, graph_batch_size: int = AUTO, align_burnin: int = AUTO, safe_burnin: bool = True, max_epochs: int = AUTO, patience: Optional[int] = AUTO, reduce_lr_patience: Optional[int] = AUTO, wait_n_lrs: int = 1, directory: Optional[os.PathLike] = None, ) -> None: r""" Fit model on given datasets Parameters ---------- adatas Datasets (indexed by modality name) graph Guidance graph neg_samples Number of negative samples for each edge val_split Validation split data_batch_size Number of cells in each data minibatch graph_batch_size Number of edges in each graph minibatch align_burnin Number of epochs to wait before starting alignment safe_burnin Whether to postpone learning rate scheduling and earlystopping until after the burnin stage max_epochs Maximal number of epochs patience Patience of early stopping reduce_lr_patience Patience to reduce learning rate wait_n_lrs Wait n learning rate scheduling events before starting early stopping directory Directory to store checkpoints and tensorboard logs """ data = AnnDataset( [adatas[key] for key in self.net.keys], [self.modalities[key] for key in self.net.keys], mode="train", ) check_graph( graph, adatas.values(), cov="ignore", attr="error", loop="warn", sym="warn" ) graph = GraphDataset( ``` -------------------------------- ### GET scglue.num.all_counts Source: https://scglue.readthedocs.io/en/latest/api/scglue.num.all_counts.html Documentation for the scglue.num.all_counts function which checks if an array contains all counts. ```APIDOC ## scglue.num.all_counts ### Description Check whether an array contains all counts. ### Parameters #### Path Parameters - **x** (numpy.ndarray or scipy.sparse.spmatrix) - Required - Array to check ### Response #### Success Response (200) - **is_counts** (bool) - Whether the array contains all counts ``` -------------------------------- ### GLUETrainer Initialization Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/glue.html Initializes the GLUETrainer with network, weighting parameters, and optimizer settings. ```APIDOC ## GLUETrainer Initialization ### Description Initializes the GLUETrainer for a given GLUE network. It requires several weighting parameters and optimizer configurations. ### Method __init__ ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **net** (GLUE) - The GLUE network to be trained. - **lam_data** (float) - Data weight. Required. - **lam_kl** (float) - KL divergence weight. Required. - **lam_graph** (float) - Graph weight. Required. - **lam_align** (float) - Adversarial alignment weight. Required. - **dsc_steps** (int) - Number of discriminator steps per encoder-decoder step. Required. - **modality_weight** (Mapping[str, float]) - Relative modality weight (indexed by modality name). Required. - **optim** (str) - Optimizer name (e.g., 'Adam'). Required. - **lr** (float) - Learning rate. Required. - **kwargs** - Additional keyword arguments passed to the optimizer constructor. ### Request Example ```python trainer = GLUETrainer( net=glue_network, lam_data=0.1, lam_kl=0.01, lam_graph=0.5, lam_align=0.01, dsc_steps=2, modality_weight={'mod1': 1.0, 'mod2': 0.5}, optim='Adam', lr=0.001 ) ``` ### Response #### Success Response (200) None (initialization does not return a value) #### Response Example None ``` -------------------------------- ### GET scglue.models.nn.get_default_numpy_dtype Source: https://scglue.readthedocs.io/en/latest/api/scglue.models.nn.get_default_numpy_dtype.html Retrieves the numpy dtype corresponding to the PyTorch default dtype. ```APIDOC ## GET scglue.models.nn.get_default_numpy_dtype ### Description Get numpy dtype matching that of the pytorch default dtype. ### Parameters #### Query Parameters - **complex** (bool) - Optional - Whether to return a complex dtype. Defaults to False. ### Response #### Success Response (200) - **dtype** (type) - The default numpy dtype matching the PyTorch default. ``` -------------------------------- ### Initialize Dataset with getitem_size Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/data.html Initializes the Dataset with a specified fetch size for each __getitem__ call. Sets up internal states for shuffling. ```python def __init__(self, getitem_size: int = 1) -> None: super().__init__() self.getitem_size = getitem_size self.shuffle_seed: Optional[int] = None self.seed_queue: Optional[multiprocessing.Queue] = None self.propose_queue: Optional[multiprocessing.Queue] = None self.propose_cache: Mapping[int, Any] = {} ``` -------------------------------- ### Get Bedtools Path Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/utils.html Retrieves the path to the bedtools executable. The default value is 'bedtools'. ```python def BEDTOOLS_PATH(self) -> str: r""" Path to bedtools executable. Default value is ``bedtools``. """ return self._BEDTOOLS_PATH ``` -------------------------------- ### Class: scglue.models.plugins.EarlyStopping Source: https://scglue.readthedocs.io/en/latest/api/scglue.models.plugins.EarlyStopping.html Initializes the EarlyStopping plugin to monitor training loss and trigger early termination. ```APIDOC ## Class: scglue.models.plugins.EarlyStopping ### Description Early stop model training when loss no longer decreases. ### Parameters - **monitor** (str) - Required - Loss to monitor - **patience** (int) - Required - Patience to stop early - **burnin** (int) - Optional - Burn-in epochs to skip before initializing early stopping (default: 0) - **wait_n_lrs** (int) - Optional - Wait n learning rate scheduling events before starting early stopping (default: 0) ### Methods - **attach**: Attach custom handlers to training or validation engine ``` -------------------------------- ### GLUE Device Management Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/glue.html Property to get or set the device (CPU/GPU) for the GLUE module. ```APIDOC ## GLUE.device ### Description Manages the device assignment for the GLUE module. Setting this property automatically moves the module to the specified device. ### Parameters - **device** (torch.device) - Required - The target device for the module. ``` -------------------------------- ### SCGLUETrainer Initialization and Properties Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/scglue.html Details the initialization parameters for the SCGLUETrainer and its properties like `freeze_u`. ```APIDOC ## SCGLUETrainer ### Description Trainer for :class:`SCGLUE`. ### Parameters - **net** (:class:`SCGLUE`) - The SCGLUE network to be trained. - **lam_data** (float) - Data weight. - **lam_kl** (float) - KL weight. - **lam_graph** (float) - Graph weight. - **lam_align** (float) - Adversarial alignment weight. - **lam_sup** (float) - Cell type supervision weight. - **dsc_steps** (int) - Number of discriminator steps per encoder-decoder step. - **normalize_u** (bool) - Whether to L2 normalize cell embeddings before decoder. - **modality_weight** (Mapping[str, float]) - Relative modality weight (indexed by modality name). - **optim** (str) - Optimizer. - **lr** (float) - Learning rate. - **kwargs** - Additional keyword arguments passed to the optimizer constructor. ### Properties #### freeze_u - **Description**: Whether to freeze cell embeddings. - **Setter**: Sets the `_freeze_u` attribute and controls `requires_grad` for relevant parameters. ### Initialization Details - **BURNIN_NOISE_EXAG**: float = 1.5 - Raises `ValueError` if `lam_sup` or `normalize_u` are not specified. - Initializes `vae_optim` if `net.u2c` is present. ``` -------------------------------- ### Model Training with scglue Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt Shows how to train a scglue model. This involves preparing the data and specifying training parameters. ```python model = scglue.genomics.load_model("model.d/')" scglue.train.train_scanpy_model(adata, model, "cell_type", "batch") ``` -------------------------------- ### Fit Network with Training Parameters Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/glue.html Configures and initiates the network training process. It accepts various parameters to control data loading, batch sizes, training duration, and optimization. ```python def fit( self, data: ArrayDataset, graph: GraphDataset, val_split: float = None, data_batch_size: int = None, graph_batch_size: int = None, align_burnin: int = None, safe_burnin: bool = True, max_epochs: int = None, patience: Optional[int] = None, reduce_lr_patience: Optional[int] = None, wait_n_lrs: Optional[int] = None, random_seed: int = None, directory: Optional[os.PathLike] = None, plugins: Optional[List[TrainingPlugin]] = None, ) -> None: r""" Fit network Parameters ---------- data Data dataset graph Graph dataset val_split Validation split data_batch_size Number of samples in each data minibatch graph_batch_size Number of edges in each graph minibatch align_burnin Number of epochs to wait before starting alignment safe_burnin Whether to postpone learning rate scheduling and earlystopping until after the burnin stage max_epochs Maximal number of epochs patience Patience of early stopping reduce_lr_patience Patience to reduce learning rate wait_n_lrs Wait n learning rate scheduling events before starting early stopping random_seed Random seed directory Directory to store checkpoints and tensorboard logs plugins Optional list of training plugins """ required_kwargs = ( "val_split", "data_batch_size", "graph_batch_size", "align_burnin", "max_epochs", ``` -------------------------------- ### Looping Through Data Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt Example of iterating through a list or DataFrame rows. Essential for processing collections of data. ```python for index, row in df.iterrows(): print(f"Index: {index}, Feature1: {row['feature1']}") ``` -------------------------------- ### Get State Dictionary Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/glue.html Returns the state dictionary for the model, including optimizers for VAE and discriminator. ```python return { **super().state_dict(), "vae_optim": self.vae_optim.state_dict(), "dsc_optim": self.dsc_optim.state_dict(), } ``` -------------------------------- ### Initialize NBMixtureDataDecoder Parameters Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/sc.html Initializes parameters for the Mixture of Negative Binomial data decoder. Includes scale, two biases, log_theta, and zi_logits. ```python self.scale_lin = torch.nn.Parameter(torch.zeros(n_batches, out_features)) self.bias1 = torch.nn.Parameter(torch.zeros(n_batches, out_features)) self.bias2 = torch.nn.Parameter(torch.zeros(n_batches, out_features)) self.log_theta = torch.nn.Parameter(torch.zeros(n_batches, out_features)) self.zi_logits = torch.nn.Parameter(torch.zeros(n_batches, out_features)) ``` -------------------------------- ### Model.compile Method Source: https://scglue.readthedocs.io/en/latest/api/scglue.models.base.Model.compile.html Prepares the model for training by initializing and configuring a trainer. Accepts trainer type and arguments for the trainer constructor. ```APIDOC ## Model.compile ### Description Prepare model for training. ### Method (Implicitly called, not a direct HTTP method) ### Endpoint (N/A - Python method) ### Parameters #### Path Parameters (N/A) #### Query Parameters (N/A) #### Request Body (N/A) ### Request Example (N/A - Python method) ### Response #### Success Response (200) `None` #### Response Example (N/A - Python method returns None) ### Parameters - **trainer** (type) - Required - Trainer type. - ***args** (type) - Required - Positional arguments are passed to the trainer constructor. - ****kwargs** (type) - Required - Keyword arguments are passed to the trainer constructor. ### Return type `None` ### Note Subclasses may override arguments for API definition. ``` -------------------------------- ### Get Model Losses Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/base.html An alias for `.trainer.get_losses`. This method retrieves the loss values calculated by the trainer. ```python self.trainer.get_losses(*args, **kwargs) ``` -------------------------------- ### Initialize BetaBinomial Distribution Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/prob.html Initializes the distribution using logit-transformed mean and concentration parameters. Requires torch.Tensor inputs. ```python def __init__(self, logit_mu: torch.Tensor, size: torch.Tensor) -> None: mu = logit_mu.sigmoid() super().__init__(mu * size + EPS, (1 - mu) * size + EPS) ``` -------------------------------- ### Initialize BetaDataDecoder Parameters Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/sc.html Initializes parameters for the Beta data decoder. Includes scale, bias, and size parameters, with size clamped to a maximum value. ```python self.scale_lin = torch.nn.Parameter(torch.zeros(n_batches, out_features)) self.bias = torch.nn.Parameter(torch.zeros(n_batches, out_features)) self.size_lin = torch.nn.Parameter(torch.zeros(n_batches, out_features)) ``` -------------------------------- ### SCGLUE Model Fine-tuning Source: https://scglue.readthedocs.io/en/latest/training.html This log snippet shows the initiation of SCGLUE model fine-tuning after pretraining. It includes steps like estimating balancing weights, clustering cells, and matching clusters. ```log [INFO] fit_SCGLUE: Estimating balancing weight... [INFO] estimate_balancing_weight: Clustering cells... [INFO] estimate_balancing_weight: Matching clusters... [INFO] estimate_balancing_weight: Matching array shape = (16, 18)... [INFO] estimate_balancing_weight: Estimating balancing weight... [INFO] fit_SCGLUE: Fine-tuning SCGLUE model... [INFO] check_graph: Checking variable coverage... [INFO] check_graph: Checking edge attributes... [INFO] check_graph: Checking self-loops... [INFO] check_graph: Checking graph symmetry... [INFO] SCGLUEModel: Setting `graph_batch_size` = 27025 [INFO] SCGLUEModel: Setting `align_burnin` = 31 [INFO] SCGLUEModel: Setting `max_epochs` = 186 ``` -------------------------------- ### GET scglue.models.nn.autodevice Source: https://scglue.readthedocs.io/en/latest/api/scglue.models.nn.autodevice.html Retrieves the optimal torch computation device based on GPU availability and memory usage. ```APIDOC ## GET scglue.models.nn.autodevice ### Description Get torch computation device automatically based on GPU availability and memory usage. ### Method GET ### Endpoint scglue.models.nn.autodevice() ### Response #### Success Response (200) - **device** (torch.device) - The computation device (CPU or GPU) selected based on availability and memory usage. ``` -------------------------------- ### Initialize PairedSCGLUETrainer Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/scglue.html Initializes the PairedSCGLUETrainer with network, weighting parameters, and optimizer settings. Requires `lam_joint_cross`, `lam_real_cross`, and `lam_cos` to be specified. ```python def __init__( self, net: SCGLUE, lam_data: float = None, lam_kl: float = None, lam_graph: float = None, lam_align: float = None, lam_sup: float = None, lam_joint_cross: float = None, lam_real_cross: float = None, lam_cos: float = None, dsc_steps: int = None, normalize_u: bool = None, modality_weight: Mapping[str, float] = None, optim: str = None, lr: float = None, **kwargs, ) -> None: super().__init__( net, lam_data=lam_data, lam_kl=lam_kl, lam_graph=lam_graph, lam_align=lam_align, lam_sup=lam_sup, dsc_steps=dsc_steps, normalize_u=normalize_u, modality_weight=modality_weight, optim=optim, lr=lr, **kwargs, ) required_kwargs = ("lam_joint_cross", "lam_real_cross", "lam_cos") for required_kwarg in required_kwargs: if locals()[required_kwarg] is None: raise ValueError(f"`{required_kwarg}` must be specified!") self.lam_joint_cross = lam_joint_cross self.lam_real_cross = lam_real_cross self.lam_cos = lam_cos self.required_losses += ["joint_cross_loss", "real_cross_loss", "cos_loss"] ``` -------------------------------- ### Beta Class Initialization Source: https://scglue.readthedocs.io/en/latest/api/scglue.models.prob.Beta.html Initializes the Beta distribution with logit of mean and concentration. ```APIDOC ## Beta Class ### Description Stable beta distribution parameterized by mean and concentration. ### Method __init__ ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **logit_mu** (torch.Tensor) - Required - Logit of mean of the beta distribution - **size** (torch.Tensor) - Required - Concentration of the beta distribution ### Request Example ```python # Example usage (assuming torch is imported as T) # logit_mu_tensor = T.tensor([...]) # size_tensor = T.tensor([...]) # beta_distribution = scglue.models.prob.Beta(logit_mu_tensor, size_tensor) ``` ### Response #### Success Response (200) None (Initialization does not return a value) #### Response Example None ``` -------------------------------- ### Get Checkpoint Save Numbers Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/utils.html Retrieves the maximum number of checkpoints to preserve. The default value is 3. ```python def CHECKPOINT_SAVE_NUMBERS(self) -> int: r""" Maximal number of checkpoints to preserve at any point. Default value is ``3``. """ return self._CHECKPOINT_SAVE_NUMBERS ``` -------------------------------- ### SCCLUETrainer Initialization Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/scclue.html Initializes the SCCLUETrainer with the provided network architecture and training hyperparameters. ```APIDOC ## SCCLUETrainer.__init__ ### Description Initializes the trainer instance with required loss weights, optimization settings, and network parameters. ### Parameters - **net** (SCCLUE) - Required - The SCGLUE network model. - **lam_data** (float) - Required - Weight for data loss. - **lam_kl** (float) - Required - Weight for KL divergence loss. - **lam_align** (float) - Required - Weight for alignment loss. - **lam_sup** (float) - Required - Weight for supervised loss. - **lam_joint_cross** (float) - Required - Weight for joint cross loss. - **lam_real_cross** (float) - Required - Weight for real cross loss. - **lam_cos** (float) - Required - Weight for cosine loss. - **normalize_u** (bool) - Required - Whether to normalize latent representations. - **modality_weight** (Mapping[str, float]) - Required - Weights for different modalities. - **optim** (str) - Required - Name of the optimizer to use. - **lr** (float) - Required - Learning rate. ``` -------------------------------- ### scglue.check.check_deps Source: https://scglue.readthedocs.io/en/latest/api/scglue.check.check_deps.html Checks if specified dependencies are installed. This function takes a variable number of arguments, each representing a dependency to check. ```APIDOC ## scglue.check.check_deps ### Description Checks whether certain dependencies are installed. ### Method N/A (This is a Python function, not a REST API endpoint) ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import scglue # Example: Check for 'numpy' and 'pandas' scglue.check.check_deps('numpy', 'pandas') ``` ### Response #### Success Response (None) This function does not return any value (`None`). It may raise an error if dependencies are not met, but this is not explicitly documented. #### Response Example None ``` -------------------------------- ### SCCLUETrainer Initialization Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/scclue.html Initializes the SCCLUETrainer with various lambda values for different loss components, network parameters, and optimizer settings. It validates required keyword arguments and sets up optimizers for VAE and discriminator components. ```python class SCCLUETrainer(Trainer): def __init__( self, net: SCCLUE, lam_data: float = None, lam_kl: float = None, lam_align: float = None, lam_sup: float = None, lam_joint_cross: float = None, lam_real_cross: float = None, lam_cos: float = None, normalize_u: bool = None, modality_weight: Mapping[str, float] = None, optim: str = None, lr: float = None, **kwargs, ) -> None: required_kwargs = ( "lam_data", "lam_kl", "lam_align", "lam_sup", "lam_joint_cross", "lam_real_cross", "lam_cos", "normalize_u", "modality_weight", "optim", "lr", ) for required_kwarg in required_kwargs: if locals()[required_kwarg] is None: raise ValueError(f"`{required_kwarg}` must be specified!") super().__init__(net) self.required_losses = [ "dsc_loss", "gen_loss", "joint_cross_loss", "real_cross_loss", "cos_loss", ] if self.net.u2c: self.required_losses.append("sup_loss") for k in self.net.keys: self.required_losses += [f"x_{k}_nll", f"x_{k}_kl", f"x_{k}_elbo"] self.earlystop_loss = "gen_loss" self.lam_data = lam_data self.lam_kl = lam_kl self.lam_align = lam_align self.lam_sup = lam_sup self.lam_joint_cross = lam_joint_cross self.lam_real_cross = lam_real_cross self.lam_cos = lam_cos self.normalize_u = normalize_u if min(modality_weight.values()) < 0: raise ValueError("Modality weights must be non-negative!") normalizer = sum(modality_weight.values()) / len(modality_weight) self.modality_weight = {k: v / normalizer for k, v in modality_weight.items()} self.lr = lr if self.net.u2c: self.vae_optim = getattr(torch.optim, optim)( chain( self.net.x2u.parameters(), self.net.u2x.parameters(), self.net.u2c.parameters(), ), lr=self.lr, **kwargs, ) else: self.vae_optim = getattr(torch.optim, optim)( chain(self.net.x2u.parameters(), self.net.u2x.parameters()), lr=self.lr, **kwargs, ) self.dsc_optim = getattr(torch.optim, optim)( self.net.du.parameters(), lr=self.lr, **kwargs ) self.align_burnin: Optional[int] = None ``` -------------------------------- ### Gene Expression Analysis Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt Example of analyzing gene expression patterns. This snippet focuses on plotting specific genes. ```python sc.pl.umap(adata, color=["gene1", "gene2"]) ``` -------------------------------- ### Execute Model Training Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/glue.html Initiates the model training process using the configured data loaders and plugins. Includes a try-finally block to ensure data cleanup after training completes or if an error occurs. This is the main entry point for training. ```python super().fit( train_loader, val_loader=val_loader, max_epochs=max_epochs, random_seed=random_seed, directory=directory, plugins=plugins, ) finally: data.clean() data_train.clean() data_val.clean() graph.clean() self.align_burnin = None self.eidx = None self.enorm = None self.esgn = None ``` -------------------------------- ### Model Compilation Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/base.html Explains how to prepare the model for training by compiling it with a trainer. ```APIDOC ## compile ### Description Prepares the model for training by initializing and assigning a trainer. ### Parameters - **trainer** (Trainer) - The trainer type to be used. - **args** - Positional arguments passed to the trainer constructor. - **kwargs** - Keyword arguments passed to the trainer constructor. ### Note Subclasses may override arguments for API definition. Calling `compile` multiple times will overwrite the previous trainer. ``` -------------------------------- ### Data Filtering Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt Example of filtering a DataFrame based on specific conditions. This is crucial for selecting relevant data subsets. ```python filtered_df = df[df['feature1'] > 0.7] print(filtered_df.head()) ``` -------------------------------- ### Compile Model for Training Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/models/base.html Prepares the model for training by constructing the trainer. It can overwrite an existing trainer if called multiple times. ```python if self._trainer: self.logger.warning( "`compile` has already been called. " "Previous trainer will be overwritten!" ) self._trainer = self.TRAINER_TYPE(self.net, *args, **kwargs) ``` -------------------------------- ### Initialize AllCoolsLSI class Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/data.html Initializes the AllCoolsLSI transformer with specified parameters for dimensionality reduction. ```python class AllCoolsLSI: def __init__( self, scale_factor=100000, n_components=100, algorithm="arpack", random_state=0, n_iter=5, idf=None, model=None, ): self.scale_factor = scale_factor if idf is not None: self.idf = idf.copy() else: self.idf = None if model is not None: self.model = model else: self.model = TruncatedSVD( n_components=n_components, n_iter=n_iter, algorithm=algorithm, random_state=random_state, ) self.random_state = random_state self.fitted = False def _downsample_data(self, data, downsample): np.random.seed(self.random_state) if downsample is not None and downsample < data.shape[0]: use_row_idx = np.sort( ``` -------------------------------- ### Get Print Loss Interval Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/utils.html Retrieves the interval in epochs for printing loss values. The default value is 10. ```python def PRINT_LOSS_INTERVAL(self) -> int: r""" Print loss values every n epochs. Default value is ``10``. """ return self._PRINT_LOSS_INTERVAL ``` -------------------------------- ### Basic Data Loading and Processing Source: https://scglue.readthedocs.io/en/latest/_sources/reginf.ipynb.txt Demonstrates loading and basic processing of data. Ensure necessary libraries are imported before use. ```python import scanpy as sc import scglue adata = sc.read_h5ad("adata.h5ad") adata.var_names_make_unique() scglue.data.get_common_counts(adata, "batch", "cell_type") ``` -------------------------------- ### Get Checkpoint Save Interval Source: https://scglue.readthedocs.io/en/latest/_modules/scglue/utils.html Retrieves the interval in epochs for automatically saving checkpoints. The default value is 10. ```python def CHECKPOINT_SAVE_INTERVAL(self) -> int: r""" Automatically save checkpoints every n epochs. Default value is ``10``. """ return self._CHECKPOINT_SAVE_INTERVAL ``` -------------------------------- ### SCGLUEModel Initialization Source: https://scglue.readthedocs.io/en/latest/api/scglue.models.scglue.SCGLUEModel.html Constructor for initializing the SCGLUEModel with datasets and guidance graph vertices. ```APIDOC ## SCGLUEModel Constructor ### Description Initializes the GLUE model for single-cell multi-omics data integration. ### Parameters - **adatas** (Mapping[str, AnnData]) - Required - Datasets indexed by modality name. - **vertices** (List[str]) - Required - Guidance graph vertices covering feature names in all modalities. - **latent_dim** (int) - Optional - Latent dimensionality (default: 50). - **h_depth** (int) - Optional - Hidden layer depth for encoder and discriminator (default: 2). - **h_dim** (int) - Optional - Hidden layer dimensionality for encoder and discriminator (default: 256). - **dropout** (float) - Optional - Dropout rate (default: 0.2). - **disc_norm** (bool) - Optional - Whether to perform normalization at discriminator input (default: False). - **shared_batches** (bool) - Optional - Whether the same batches are shared across modalities (default: False). - **random_seed** (int) - Optional - Random seed (default: 0). ```