positive_nonnegative_matrix_factorization
PositiveNMF
Bases: Simulator
Positive nonnegative matrix factorization (PNMF) simulator with gamma regression.
This simulator fits a low-rank positive factorization on log-transformed expression data and then models the resulting latent scores using a covariate-dependent shifted gamma distribution. Sampling proceeds by drawing gamma latent scores and mapping them back to the gene space via the learned PNMF weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
dict or str
|
Mean-model formula for the gamma regression. If a string is provided,
it is interpreted as the mean formula and stored under the key
|
required |
**kwargs
|
Keyword arguments forwarded to :func: |
{}
|
Attributes:
| Name | Type | Description |
|---|---|---|
formula |
dict
|
Standardized formula dictionary containing at least the |
parameters |
dict or None
|
Fitted parameters after calling :meth:
|
n_outcomes |
int
|
Number of simulated outcomes (cells) in the training data. |
columns |
Index
|
Column names of the design matrix produced from |
Examples:
Fit a PNMF simulator, inspect fitted parameters, and generate samples:
>>> import numpy as np
>>> import pandas as pd
>>> from anndata import AnnData
>>> from scdesigner.simulators import PositiveNMF
>>>
>>> rng = np.random.default_rng(0)
>>> X = rng.poisson(lam=2.0, size=(50, 20)).astype(float) # (cells × genes)
>>> obs = pd.DataFrame({"condition": rng.choice(["A", "B"], size=50)})
>>> adata = AnnData(X=X, obs=obs)
>>> adata.var_names = [f"g{i}" for i in range(adata.n_vars)]
>>>
>>> sim = PositiveNMF("~ 1 + condition", nbase=3, maxIter=50)
>>> sim.fit(adata, lr=0.1)
>>> isinstance(sim.parameters, dict)
True
>>>
>>> # Predict gamma parameters for new observations
>>> new_obs = pd.DataFrame({"condition": ["A", "B", "A"]})
>>> pred = sim.predict(new_obs)
>>> sorted(pred.keys())
['a', 'beta', 'loc']
>>>
>>> # Sample a new dataset with the same genes
>>> adata_sim = sim.sample(new_obs)
>>> adata_sim.n_obs == 3 and adata_sim.n_vars == adata.n_vars
True
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
__init__(formula, **kwargs)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
dict or str
|
Formula describing the mean model for the gamma regression. |
required |
**kwargs
|
Keyword arguments passed through to :func: |
{}
|
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
375 376 377 378 379 380 381 382 383 384 385 386 | |
fit(adata, lr=0.1)
Fit the PNMF marginals on the provided AnnData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
Dataset used to estimate PNMF weights and gamma coefficients. |
required |
lr
|
float
|
Learning rate for the gamma regression solver. |
0.1
|
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
predict(obs=None, **kwargs)
Predict gamma regression parameters for new observations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs
|
DataFrame
|
Observation metadata used to construct the design matrix. Defaults
to the training |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, ndarray]
|
Dictionary with |
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
sample(obs=None)
Generate expression samples from the fitted model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs
|
DataFrame
|
Metadata for the observations to simulate. Defaults to the training
|
None
|
Returns:
| Type | Description |
|---|---|
AnnData
|
Simulated :class: |
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
MatFind(A, ZeroThres)
Zero out values below a threshold.
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
190 191 192 193 194 195 196 | |
MatFindlb(A, lb)
Clamp matrix A's entries to be greater than or equal to a lower bound lb.
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
181 182 183 184 185 186 187 | |
class_generator(score, n_clusters=3)
Cluster PNMF scores and return discrete class labels. (This function is not used in the current implementation.)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score
|
ndarray
|
PNMF scores (latent factors) of shape (n_features, n_cells). |
required |
n_clusters
|
int
|
Number of target clusters for grouping the scores. |
3
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of cluster labels of length |
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
format_gamma_parameters(parameters, W_index, coef_index)
Format gamma regression parameters as DataFrames for downstream use.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
dict
|
Dictionary containing |
required |
W_index
|
list
|
Row labels for the PNMF weights (typically gene names). |
required |
coef_index
|
list
|
Row labels for the regression coefficients (typically covariate names). |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Updated dictionary with DataFrames stored under the original keys. |
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
gamma_regression_array(x, y, lr=0.1, epochs=40)
Fit gamma regression coefficients in a batched regression context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Design matrix for covariates (cells × covariates). |
required |
y
|
ndarray
|
Target matrix (cells × latent features) derived from PNMF scores. |
required |
lr
|
float
|
Learning rate for the Adam optimizer. |
0.1
|
epochs
|
int
|
Number of training epochs. |
40
|
Returns:
| Type | Description |
|---|---|
dict[str, ndarray]
|
Dictionary containing estimated |
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
left_singular(X, k)
Extract the top k left singular vectors of matrix X for initialization.
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
172 173 174 175 176 177 178 | |
negative_gamma_log_likelihood(log_a, log_beta, loc, X, y)
Compute the (negative) log likelihood over a gamma regression layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_a
|
Tensor
|
Log-scale shape coefficients of shape (covariates × outcomes). |
required |
log_beta
|
Tensor
|
Log-scale rate coefficients of shape (covariates × outcomes). |
required |
loc
|
Tensor
|
Location coefficients of shape (covariates × outcomes). |
required |
X
|
Tensor
|
Design matrix (cells × covariates). |
required |
y
|
Tensor
|
Observed PNMF scores (cells × outcomes). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar tensor representing the mean negative log-likelihood. |
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
pnmf(log_data, nbase=3, **kwargs)
Estimate PNMF components from log-transformed expression counts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_data
|
ndarray
|
Log-transformed expression matrix (genes × cells). |
required |
nbase
|
int
|
Number of latent PNMF bases to extract. |
3
|
**kwargs
|
Additional arguments forwarded to :func: |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
Tuple containing the learned PNMF weight matrix |
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
pnmf_eucdist(X, W_init, maxIter=500, threshold=0.0001, tol=1e-10, verbose=False, **kwargs)
Optimize PNMF weights via Euclidean distance minimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input expression matrix (genes × cells). |
required |
W_init
|
ndarray
|
Initial estimate of the weight matrix. |
required |
maxIter
|
int
|
Maximum number of iterations. |
500
|
threshold
|
float
|
Convergence threshold on relative weight change. |
0.0001
|
tol
|
float
|
Numeric tolerance for truncating small entries. |
1e-10
|
verbose
|
bool
|
If True, print progress every 10 iterations. |
False
|
**kwargs
|
Reserved for future options. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Normalized PNMF weight matrix with positive entries. |
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
shifted_gamma_pdf(x, alpha, beta, loc)
Compute penalized negative log probability density function for shifted gamma. A huge penalty is applied to values below the location parameter to ensure the parameters fall within the support of the gamma distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor or array - like
|
Observed values. |
required |
alpha
|
Tensor
|
Shape parameters. |
required |
beta
|
Tensor
|
Rate parameters. |
required |
loc
|
Tensor
|
Location parameters (shift). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Mean negative log likelihood over |
Source code in scdesigner/src/scdesigner/simulators/positive_nonnegative_matrix_factorization.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |