torch_staintools.functional.concentration package
Submodules
torch_staintools.functional.concentration.implementation module
- class torch_staintools.functional.concentration.implementation.ConcentCfg(algorithm: Literal['ista', 'cd', 'fista', 'ls'] = 'fista', regularizer: float = 0.01, rng: torch._C.Generator | None = None, maxiter: int = 50, lr: float | None = None, tol: float = 1e-05, positive: bool = True)
Bases:
object- algorithm: Literal['ista', 'cd', 'fista', 'ls'] = 'fista'
- lr: float | None = None
- maxiter: int = 50
- positive: bool = True
- regularizer: float = 0.01
- rng: Generator | None = None
- tol: float = 1e-05
- class torch_staintools.functional.concentration.implementation.ConcentrationSolver(cfg: ConcentCfg)
Bases:
Callable- cfg: ConcentCfg
- torch_staintools.functional.concentration.implementation.get_concentration_batch(od_flatten: Tensor, stain_matrix: Tensor, regularizer: float, algorithm: Literal['ista', 'cd', 'fista', 'ls'], lr: float | None, maxiter: int, tol: float, rng: Generator | None, positive: bool)
- torch_staintools.functional.concentration.implementation.get_concentration_one_by_one(od_flatten: Tensor, stain_matrix: Tensor, regularizer: float, algorithm: Literal['ista', 'cd', 'fista', 'ls'], lr: float | None, maxiter: int, tol: float, rng: Generator | None, positive: bool)
- torch_staintools.functional.concentration.implementation.get_concentrations(image: Tensor, stain_matrix: Tensor, regularizer: float, algorithm: Literal['ista', 'cd', 'fista', 'ls'], lr: float | None, maxiter: int, tol: float, rng: Generator | None, positive: bool)
Estimate concentration matrix given an image and stain matrix.
Warning
algorithm = ‘ls’ May fail on GPU for individual large input (e.g., 1000 x 1000), regardless of batch size. Better for multiple small inputs in terms of H and W.
- Parameters:
image – batched image(s) in shape of BxCxHxW
stain_matrix – B x num_stain x input channel
regularizer – regularization term if ISTA algorithm is used
algorithm – which method to compute the concentration: Solve min||HExC - OD||p support ‘ista’, ‘cd’, and ‘ls’. ‘ls’ simply solves the least square problem for factorization of min||HExC - OD||F (Frobenius norm) but is faster. ‘ista’/cd enforce the sparse penalty (L1 norm) but slower.
lr – learning rate for FISTA and ISTA. If None then computed from 1 / Lipschitz constant.
maxiter – max iteration for sparse code optimization.
tol – tolerance for early convergence for sparse code optimization.
rng – torch.Generator for random initializations
positive – whether to enforce positive concentration. Default: True
- Returns:
B x num_stains x num_pixel_in_tissue_mask
- Return type:
concentration matrix
- torch_staintools.functional.concentration.implementation.get_concentrations_single(od_flatten: Tensor, stain_matrix: Tensor, regularizer: float, algorithm: Literal['ista', 'cd', 'fista', 'ls'], lr: float | None, maxiter: int, tol: float, rng: Generator | None, positive: bool)
Helper function to estimate concentration matrix given an image and stain matrix with shape: 2 x (H*W)
For solvers without batch support. Inputs are individual data points from a batch
- Parameters:
od_flatten – Flattened optical density vectors in shape of (H*W) x C (H and W dimensions flattened).
stain_matrix – the computed stain matrices in shape of num_stain x input channel
regularizer – regularization term if ISTA algorithm is used
algorithm – which method to compute the concentration: coordinate descent (‘cd’) or iterative-shrinkage soft thresholding algorithm (‘ista’)
lr – learning rate for ISTA and FISTA. No effect for other methods. If None, will be computed by 1 / Lipschitz constant.
maxiter – maximum number of iterations for CD, ISTA, and FISTA
tol – tolerance of convergence
rng – torch.Generator for random initializations
positive – enforce positive concentration
- Returns:
num_stains x num_pixel_in_tissue_mask
- Return type:
computed concentration