scmidas.nn#

class scmidas.nn.ActivationRegistry[source]#

Bases: object

A registry for managing and dynamically extending activation functions.

get(name: str, **kwargs) Callable[source]#

Retrieve a registered activation function by name.

Parameters:
  • name – str The name of the activation function.

  • kwargs – dict, optional Additional parameters for the activation function (e.g., dim for Softmax).

Returns:

The corresponding activation function instance.

Return type:

Callable

Raises:
  • KeyError – If the specified activation function is not registered.

  • ValueError – If the activation function does not support dynamic parameters.

list_registered() List[str][source]#

List all registered activation functions.

Returns:

Names of all registered activation functions.

Return type:

List[str]

register(name: str, func: Callable)[source]#

Register a new activation function.

Parameters:
  • name – str The name of the activation function (key for retrieval).

  • func – Callable The activation function instance or a factory function.

class scmidas.nn.DistributionRegistry[source]#

Bases: object

A registry for managing and dynamically extending loss functions, sampling functions, and activation functions.

static bernoulli_sampling(data: Tensor) Tensor[source]#

Perform Bernoulli sampling on the input tensor.

Parameters:

data – torch.Tensor Input probabilities for Bernoulli sampling.

Returns:

Sampled binary tensor.

Return type:

torch.Tensor

get_activate(name: str) Callable[source]#

Retrieve a registered activation function by name.

Parameters:

name – str The name of the activation function.

Returns:

The corresponding activation function instance.

Return type:

Callable

Raises:

KeyError – If the activation function is not registered.

get_loss(name: str) Module[source]#

Retrieve a registered loss function by name.

Parameters:

name – str The name of the loss function.

Returns:

The corresponding loss function instance.

Return type:

nn.Module

Raises:

KeyError – If the loss function is not registered.

get_sampling(name: str) Callable[source]#

Retrieve a registered sampling function by name.

Parameters:

name – str The name of the sampling function.

Returns:

The corresponding sampling function instance.

Return type:

Callable

Raises:

KeyError – If the sampling function is not registered.

list_registered() List[str][source]#

List all registered distributions.

Returns:

Names of all registered distributions.

Return type:

List[str]

static null(data: Tensor) Tensor[source]#

A placeholder function that returns the input tensor unchanged.

Parameters:

data – torch.Tensor Input tensor.

Returns:

The same tensor without any modification.

Return type:

torch.Tensor

static poisson_sampling(data: Tensor) Tensor[source]#

Perform Poisson sampling on the input tensor.

Parameters:

data – torch.Tensor Input rates for Poisson sampling.

Returns:

Sampled tensor with Poisson-distributed values.

Return type:

torch.Tensor

register(name: str, loss_fn: Module, sampling_fn: Callable, activate_fn: Callable)[source]#

Register a new set of loss, sampling, and activation functions.

Parameters:
  • name – str The name of the distribution (key for retrieval).

  • loss_fn – nn.Module The loss function instance to register.

  • sampling_fn – Callable The sampling function instance to register.

  • activate_fn – Callable The activation function instance to register.

Raises:

ValueError – If the name is already registered in any of the maps.

class scmidas.nn.Layer1D(dim: int | bool = False, norm: str | bool = False, trans: str | bool = False, drop: float | bool = False)[source]#

Bases: Module

A single layer module that supports normalization, activation, and dropout.

Parameters:
  • dim – int, optional Dimension of the input tensor (required for normalization layers), default is False.

  • norm – str or bool, optional Type of normalization to apply (‘bn’ for BatchNorm, ‘ln’ for LayerNorm, or False), default is False.

  • trans – str or bool, optional Activation function name to apply. If False, no activation is applied, default is False.

  • drop – float or bool, optional Dropout rate. If False, no dropout is applied, default is False.

net#

nn.Sequential Sequential container for the components of the layer.

forward(x: Tensor) Tensor[source]#

Forward pass through the layer.

Parameters:

x – torch.Tensor Input tensor.

Returns:

Output tensor after applying normalization, activation, and dropout.

Return type:

torch.Tensor

class scmidas.nn.MLP(features: list, hid_trans: str = 'mish', out_trans: str | bool = False, norm: str | bool = False, hid_norm: str | bool = False, drop: float | bool = False, hid_drop: float | bool = False)[source]#

Bases: Module

A Multi-Layer Perceptron (MLP) module with customizable activation functions, normalization, and dropout layers.

Parameters:
  • features – list of int List of integers specifying the number of neurons in each layer.

  • hid_trans – str, optional Activation function for hidden layers, default is ‘mish’.

  • out_trans – str or bool, optional Activation function for the output layer. If False, no activation is applied, default is False.

  • norm – str or bool, optional Normalization type for all layers (‘bn’, ‘ln’, or False). Overrides hid_norm and out_norm.

  • hid_norm – str or bool, optional Normalization type for hidden layers (‘bn’, ‘ln’, or False), default is False.

  • drop – float or bool, optional Dropout rate for all layers. Overrides hid_drop and out_drop, default is False.

  • hid_drop – float or bool, optional Dropout rate for hidden layers, default is False.

net#

nn.Sequential Sequential container for the layers of the MLP.

forward(x: Tensor) Tensor[source]#

Forward pass through the MLP.

Parameters:

x – torch.Tensor Input tensor.

Returns:

Output tensor after passing through the MLP layers.

Return type:

torch.Tensor

class scmidas.nn.TransformRegistry[source]#

Bases: object

A registry for managing and dynamically extending transformation functions, with mandatory registration of inverse transformations.

static binarize(data: ndarray | Tensor, threshold: float = 0.5) ndarray | Tensor[source]#

Binarize the data using the specified threshold.

Parameters:
  • data – np.ndarray or torch.Tensor Input data to binarize.

  • threshold – float, optional Threshold for binarization, default is 0.5.

Returns:

Binarized data.

Return type:

Union[np.ndarray, torch.Tensor]

Raises:

TypeError – If the input data is neither a numpy array nor a torch tensor.

static exp(data: ndarray | Tensor) ndarray | Tensor[source]#

Apply exponential transformation (inverse of log1p) to the data.

Parameters:

data – np.ndarray or torch.Tensor Input data to transform.

Returns:

Transformed data.

Return type:

Union[np.ndarray, torch.Tensor]

Raises:

TypeError – If the input data is neither a numpy array nor a torch tensor.

get(name: str) Callable[source]#

Retrieve a registered transformation function by name.

Parameters:

name – str The name of the transformation function.

Returns:

The corresponding transformation function.

Return type:

Callable

Raises:

KeyError – If the specified transformation function is not registered.

get_inverse(name: str) Callable[source]#

Retrieve the inverse of a registered transformation function by name.

Parameters:

name – str The name of the transformation function.

Returns:

The corresponding inverse transformation function.

Return type:

Callable

Raises:

KeyError – If the specified inverse transformation function is not registered.

list_registered() List[str][source]#

List all registered transformation functions.

Returns:

Names of all registered transformation functions.

Return type:

List[str]

static log1p(data: ndarray | Tensor) ndarray | Tensor[source]#

Apply log1p transformation to the data.

Parameters:

data – np.ndarray or torch.Tensor Input data to transform.

Returns:

Transformed data.

Return type:

Union[np.ndarray, torch.Tensor]

Raises:

TypeError – If the input data is neither a numpy array nor a torch tensor.

static null(data: Tensor) Tensor[source]#

A placeholder function that returns the input tensor unchanged.

Parameters:

data – torch.Tensor Input tensor.

Returns:

The same tensor without any modification.

Return type:

torch.Tensor

register(name: str, fn: Callable, inverse_fn: Callable)[source]#

Register a new transformation function along with its inverse.

Parameters:
  • name – str The name of the transformation function (key for retrieval).

  • fn – callable The transformation function.

  • inverse_fn – callable The inverse of the transformation function.

Raises:

ValueError – If the transformation or its inverse is already registered.