torchref.kinetic.kinetics module
- class torchref.kinetic.kinetics.KineticModel(flow_chart, timepoints, rate_constants=None, efficiencies=None, instrument_function='gaussian', instrument_width=10, initial_state=None, light_activated=False, activation_level=0.5, verbose=1)[source]
Bases:
DeviceMixin,ModuleConfigurable PyTorch module for fitting kinetic behavior.
Supports arbitrary kinetic schemes defined by relational strings: - “A->B,B->C” (sequential) - “A->B,B->A,B->C” (with back reactions) - “A->B,A->C,B->D,C->D” (parallel pathways) - “A->B,B->C,D” (D is non-reactive state)
Each transition has TWO parameters: - Reactivity constant k (rate) - Reaction efficiency η (0 to 1, controls maximum conversion)
States can have baseline occupancy offsets (default: 0, not refined).
The initial transfer is driven by photoabsorption with quasi-instant conversion around zero, with spread accounted for by an instrument function.
- Parameters:
flow_chart (str) – Relational string describing the kinetic scheme using comma-separated transitions. Standalone states (non-reactive) can be included without transitions. Example: “A->B,B->C” or “A->B,B->A,B->C,D” (D is non-reactive)
timepoints (torch.Tensor or array-like) – Time points at which to evaluate the kinetics
rate_constants (dict or list, optional) – Initial rate constants. Can be: - Dict mapping “A->B” to float value - List of floats (same order as transitions in flow_chart) - None (random initialization)
efficiencies (dict or list, optional) – Initial reaction efficiencies (0-1). Same format as rate_constants. Default: all 1.0 (100% efficient)
instrument_function (str, optional) – Type of instrument response function. Options: ‘gaussian’, ‘none’ Default: ‘gaussian’
instrument_width (float, optional) – Width parameter for the instrument function (e.g., sigma for gaussian) Default: 0.1
initial_state (str, optional) – Which state starts with population 1. Default: first state in flow chart
light_activated (bool, optional) – If True, treats this as a light-activated reaction where the initial photoexcitation can only happen once. Products returning to the initial state become inactive (A*) and cannot undergo photoactivation again. Default: False
verbose (int, optional) – Verbosity level. Default: 1
- __init__(flow_chart, timepoints, rate_constants=None, efficiencies=None, instrument_function='gaussian', instrument_width=10, initial_state=None, light_activated=False, activation_level=0.5, verbose=1)[source]
- forward()[source]
Forward pass: compute populations at all timepoints.
- Returns:
populations – Population of each state at each timepoint Shape: (n_timepoints, n_states)
- Return type:
- set_baseline(state, occupancy, refinable=False)[source]
Set baseline occupancy offset for a state.
Baseline occupancies are constant offsets added to the population of a state. This is useful for non-reactive background states.
- Parameters:
Examples
>>> model.set_baseline('D', 0.1, refinable=False) # Constant 10% background >>> model.set_baseline('E', 0.05, refinable=True) # Refinable baseline
- parameters()[source]
Get all flexible (learnable) parameters as a dictionary.
- Returns:
params – Dictionary mapping parameter names to their tensors: - ‘log_rate_constants’: log-transformed rate constants - ‘log_instrument_width’: log-transformed instrument width (if refinable) - ‘baseline_{state}’: refinable baseline for specific states (if any)
- Return type:
Dict[str, torch.Tensor]
Examples
>>> model = KineticModel(...) >>> params = model.parameters() >>> print(params.keys()) >>> # Use with optimizer: optimizer = torch.optim.Adam(params.values(), lr=0.01)
- plot_occupancies(outpath, times=None, log=False, figsize=(10, 6), dpi=150, title=None)[source]
Plot state occupancies over time and save to file.
- Parameters:
outpath (str) – Path to save the plot (e.g., ‘kinetics.png’)
log (bool, optional) – If True, use log scale for x-axis. Default: False
figsize (Tuple[int, int], optional) – Figure size (width, height). Default: (10, 6)
dpi (int, optional) – DPI for saving figure. Default: 150
title (str, optional) – Custom title for the plot. If None, uses flow chart string