molclusters.analysis¶
The analyses run on the tracked clusters. FrameAnalysis, Run and Frame are
in molclusters.analysis.base; the registry of built-ins, BUILTINS, in
molclusters.analysis.builtins; each built-in in a module of its own. All of them
can be imported from molclusters.analysis.
base
¶
Provides FrameAnalysis, the base of the analyses run on the tracked clusters.
Classes:
- FrameAnalysis: The base class of an analysis, run frame by frame.
- Run: What an analysis sees of the run as a whole.
- Frame: What an analysis sees of the current frame.
FrameAnalysis
¶
Bases: ABC
An analysis of the tracked clusters, run frame by frame.
A run calls prepare once, then analyse for every frame (the first one
included) once the clusters are up to date with it, and finally finish.
The analyses of a run are called in order, so an analysis sees the results of
the ones before it for the same frame.
Only analyse must be implemented. An analysis keeps its results as its own
attributes; one that writes files does so through Run.output and declares
them in outputs. The same analysis may be run more than once, so it should
start its results over in prepare, not only in __init__.
An analysis logs with loguru's logger, as the rest of the package does;
the run tags whatever is logged during its hooks with its class name (the
analysis key of the record's extra), which the log shows. To keep the
log readable over long trajectories:
analysecounts what's worth reporting (e.g. in acollections.Counter) instead of logging it, andfinishlogs a summary: one INFO line, or one warning saying how to fix the problem.- Anything logged per frame is DEBUG or TRACE, with loguru's own arguments
(
logger.debug("cluster {}: ...", cls.id)) rather than an f-string: a message filtered out by the log level is then never built. - A warning that could repeat is only logged once.
An analysis adds its own fields to the JSON report (JsonReport, which runs
after every other analysis) by overriding report_frame, report_cluster, or
both: what they return goes in each frame's, or each cluster's, record under
the analysis' class name, its floats rounded as the report's are.
Attributes:
| Name | Type | Description |
|---|---|---|
outputs |
tuple[OutputFile, ...]
|
The files the analysis writes, to tell which results of an earlier run
they overwrite (or leave behind), and to report them at the end.
Usually a class attribute; an analysis whose files depend on its options
sets it in |
prepare
¶
prepare(run: Run) -> None
Get ready for a run, starting over any results. Does nothing unless overridden.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
Run
|
The run about to start. |
required |
analyse
abstractmethod
¶
analyse(frame: Frame) -> None
Analyse the clusters of the current frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The current frame. |
required |
finish
¶
finish(run: Run) -> None
Wrap up once every frame was analysed. Does nothing unless overridden.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
Run
|
The run that just ended. |
required |
report_frame
¶
Give what to add to the report's record of the current frame.
Called by JsonReport after every analysis has analysed the frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The current frame. |
required |
Returns:
| Type | Description |
|---|---|
Mapping[str, Any] | None
|
The fields to add under the analysis' class name: JSON types, and numpy scalars or arrays. None adds nothing (the default). |
Source code in src/molclusters/analysis/base.py
report_cluster
¶
Give what to add to the report's record of a cluster of the current frame.
Called by JsonReport, for every cluster of the frame, after every analysis
has analysed the frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The current frame. |
required |
cluster
|
Cluster
|
The cluster. |
required |
Returns:
| Type | Description |
|---|---|
Mapping[str, Any] | None
|
The fields to add under the analysis' class name: JSON types, and numpy scalars or arrays. None adds nothing (the default). |
Source code in src/molclusters/analysis/base.py
Run
¶
Run(universe: Universe, config: MolClsConfig, n_frames: int, output: RunOutput, analyses: Sequence[FrameAnalysis])
What an analysis sees of the run as a whole.
Attributes:
| Name | Type | Description |
|---|---|---|
universe |
Universe
|
The Universe being analysed. |
config |
MolClsConfig
|
The analysis configuration. |
n_frames |
int
|
The number of frames of the run. |
output |
RunOutput
|
Where the analyses write their files. |
durations |
list[float]
|
The time spent in each analysis' hooks so far, in seconds, in the order of the analyses. |
Initialize the context of a run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
universe
|
Universe
|
The Universe being analysed. |
required |
config
|
MolClsConfig
|
The analysis configuration. |
required |
n_frames
|
int
|
The number of frames of the run. |
required |
output
|
RunOutput
|
Where the analyses write their files. |
required |
analyses
|
Sequence[FrameAnalysis]
|
The analyses of the run, in the order they're called. |
required |
Source code in src/molclusters/analysis/base.py
analyses
property
¶
analyses: tuple[FrameAnalysis, ...]
The analyses of the run, in order; while preparing, only those before.
Returns:
| Type | Description |
|---|---|
tuple[FrameAnalysis, ...]
|
The analyses whose results |
prepare_analyses
¶
Prepare every analysis of the run, in order (called by the runner).
Source code in src/molclusters/analysis/base.py
analyse_frame
¶
analyse_frame(frame: Frame) -> None
Run every analysis of the run on frame, in order (called by the runner).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The current frame. |
required |
Source code in src/molclusters/analysis/base.py
finish_analyses
¶
Finish every analysis of the run, in order (called by the runner).
analysis
¶
analysis(kind: type[T]) -> T | None
Find the first analysis of the run of type kind, for its results.
While the analyses are being prepared, only those before the one being prepared are found, since only their results for a frame are ready when it analyses the frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
type[T]
|
The analysis class to look for (subclasses match too). |
required |
Returns:
| Type | Description |
|---|---|
T | None
|
The analysis, or None if the run has none (before this one). |
Source code in src/molclusters/analysis/base.py
Frame
¶
Frame(index: int, tracker: ClusterTracker, output: RunOutput)
What an analysis sees of the current frame.
Attributes:
| Name | Type | Description |
|---|---|---|
index |
int
|
The index of the frame in the run (0 for its first frame). |
time |
float
|
The time of the frame, as the trajectory reports it. |
universe |
Universe
|
The Universe, positioned at this frame. |
output |
RunOutput
|
Where the run's analyses write their files (the same as |
Initialize the current frame of a run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
The index of the frame in the run. |
required |
tracker
|
ClusterTracker
|
The tracker, up to date with the frame. |
required |
output
|
RunOutput
|
Where the run's analyses write their files. |
required |
Source code in src/molclusters/analysis/base.py
clusters
property
¶
transition
property
¶
transition: Transition
How the frame's clusters came from the previous frame's.
Returns:
| Type | Description |
|---|---|
Transition
|
Where each cluster's molecules came from, which clusters are new, and which ended, merged or dissolved (on the run's first frame, every cluster is new). |
builtins
¶
Provides BUILTINS, the built-in analyses, in the order they run.
Classes:
- Builtin: A built-in analysis, how the config builds it and what it needs.
Functions:
- build_builtins: Build the built-in analyses a config runs.
A built-in runs unless the config's analyses turns it off (by class name),
and only when the config options it needs are set. Adding one takes a line in
BUILTINS; the config checks analyses against it. They run in its order, the
report last (after the analyses given to MolClusters too).
Builtin
dataclass
¶
Builtin(kind: type[FrameAnalysis], build: Callable[[MolClsConfig], FrameAnalysis], needs: tuple[str, ...] = (), last: bool = False)
A built-in analysis, how the config builds it and what it needs.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
type[FrameAnalysis]
|
The analysis class; its name turns it on or off in the config's |
build |
Callable[[MolClsConfig], FrameAnalysis]
|
Builds the analysis from the config, once the options it needs are set. |
needs |
tuple[str, ...]
|
The config options it needs, without which it doesn't run. |
last |
bool
|
Whether it runs after every other analysis, the ones given to
|
name
property
¶
name: str
The analysis' class name, as the config's analyses and the log name it.
Returns:
| Type | Description |
|---|---|
str
|
E.g. |
runs
¶
runs(config: MolClsConfig) -> bool
Whether the config runs this analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
MolClsConfig
|
The analysis configuration. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True unless |
Source code in src/molclusters/analysis/builtins.py
build_builtins
¶
build_builtins(config: MolClsConfig, extra: Iterable[FrameAnalysis] = ()) -> list[FrameAnalysis]
Build the built-in analyses a config runs, and order them with extra.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
MolClsConfig
|
The analysis configuration, with the topology-dependent defaults filled in. |
required |
extra
|
Iterable[FrameAnalysis]
|
Other analyses to run, after the built-ins but those that run |
()
|
Returns:
| Type | Description |
|---|---|
list[FrameAnalysis]
|
The analyses in the order they run: the built-ins the config runs (see
|
Source code in src/molclusters/analysis/builtins.py
Built-in analyses¶
size
¶
Provides SizeEvolution, the number and sizes of the clusters over time (evo.txt).
SizeEvolution
¶
Bases: FrameAnalysis
Records the number of clusters and their sizes, frame by frame.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
ndarray
|
One row per frame: time, number of clusters, and the minimum, average and maximum cluster size (all 0 in a frame without clusters). |
Initialize an empty record, sized by prepare.
Source code in src/molclusters/analysis/size.py
prepare
¶
prepare(run: Run) -> None
Make room for every frame of the run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
Run
|
The run about to start. |
required |
analyse
¶
analyse(frame: Frame) -> None
Record the number of clusters and their sizes in the current frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The current frame. |
required |
Source code in src/molclusters/analysis/size.py
finish
¶
finish(run: Run) -> None
Log a summary of the cluster counts and sizes and write them to evo.txt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
Run
|
The run that just ended. |
required |
Source code in src/molclusters/analysis/size.py
lineage
¶
Provides Lineage, where the clusters came from, what became of them and when.
It writes cluster_events.csv, the clusters' formations, splits, merges and dissolutions frame by frame, and cluster_lifetimes.csv, each cluster's birth, end and lifetime.
Lineage
¶
Bases: FrameAnalysis
Records where each cluster came from, what became of it, and when.
Events are read from each frame's Frame.transition and stamped with the
first frame that shows them, so a cluster's DeathTime is the time of the
first frame it's gone from, and its lifetime, DeathTime - BirthTime, a
whole number of frame spacings (a cluster seen in a single frame lived one
spacing).
cluster_events.csv has one row per event (see EVENT_COLUMNS); Other is
the other cluster involved, if any, and NMols the molecules it concerns:
formation:Clusteris new, made only of free molecules (NMols, its size);split:Clusteris new, and tookNMolsmolecules from clusterOther. A cluster made of pieces of several clusters has a row for each, and one breaking into several pieces a row for each new piece;merge: clusterOtherended, merged intoClusterwithNMolsof its molecules; a merge of several clusters has a row for each one absorbed;dissolution:Clusterended without merging (NMols, its last size).
The clusters of the run's first frame have no event: whatever formed them happened before the run.
cluster_lifetimes.csv has one row per cluster (see LIFETIME_COLUMNS):
Origin is "initial" (there at the first frame), "formation" or "split",
Parents the clusters it took molecules from (";"-separated, most
molecules first), Fate "merge", "dissolution" or "alive" (at the last
frame), and MergedInto the cluster that absorbed it. The lifetimes of
clusters born at the start or alive at the end are censored: they lived at
least that long. One alive at the end has no DeathTime, and its
Lifetime is up to the last frame.
Each frame's Frame.transition also goes in the report (see
report_frame), for the flows of molecules between clusters.
Attributes:
| Name | Type | Description |
|---|---|---|
lives |
dict[int, _Life]
|
Cluster id -> what is known of the cluster so far, for every cluster seen. |
Initialize an empty record, started over by prepare.
Source code in src/molclusters/analysis/lineage.py
prepare
¶
prepare(run: Run) -> None
Start the record over, and cluster_events.csv with its header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
Run
|
The run about to start. |
required |
Source code in src/molclusters/analysis/lineage.py
analyse
¶
analyse(frame: Frame) -> None
Record the current frame's events, and the clusters' sizes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The current frame. |
required |
Source code in src/molclusters/analysis/lineage.py
report_frame
¶
Give how the frame's clusters came from the previous frame's, for the report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The current frame. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The frame's |
Source code in src/molclusters/analysis/lineage.py
lifetimes
¶
lifetimes() -> DataFrame
Tabulate every cluster seen so far, as cluster_lifetimes.csv has them.
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per cluster, by id (see |
Source code in src/molclusters/analysis/lineage.py
finish
¶
finish(run: Run) -> None
Log a summary of the events and lifetimes, and write cluster_lifetimes.csv.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
Run
|
The run that just ended. |
required |
Source code in src/molclusters/analysis/lineage.py
solute
¶
Provides SoluteSolvent, the clusters holding both solutes and solvents (solute_solvent.csv).
SoluteSolvent
¶
Bases: FrameAnalysis
Records the clusters that hold both solute and solvent molecules, frame by frame.
Attributes:
| Name | Type | Description |
|---|---|---|
solutes |
set[str]
|
The residue names of the solutes. |
solvents |
set[str]
|
The residue names of the solvents. |
data |
ndarray
|
One row per frame (see |
Initialize an empty record, sized by prepare.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
solutes
|
Iterable[str]
|
The residue names of the solutes. |
required |
solvents
|
Iterable[str]
|
The residue names of the solvents. |
required |
Source code in src/molclusters/analysis/solute.py
prepare
¶
prepare(run: Run) -> None
Make room for every frame of the run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
Run
|
The run about to start. |
required |
analyse
¶
analyse(frame: Frame) -> None
Record the clusters holding both solutes and solvents in the current frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The current frame. |
required |
Source code in src/molclusters/analysis/solute.py
coordinates
¶
Provides ClusterCoordinates, the coordinates of the clusters holding solutes (.gro files).
ClusterCoordinates
¶
Bases: FrameAnalysis
Writes the coordinates of every cluster holding a solute, frame by frame.
Each such cluster, made whole, is appended to cls-n<size>.gro (the clusters
of one size, pooled) and cls-id<id>.gro (one cluster's own trajectory). When
following the solutes, a cluster holding exactly one solute is also appended to
solute-<resid>.gro. The files go in the folder of the output directory.
Attributes:
| Name | Type | Description |
|---|---|---|
solutes |
set[str]
|
The residue names of the solutes. |
follow |
bool
|
Whether to write each solute's cluster to its own file. |
folder |
str
|
The folder of the output directory the files go in. |
solute_ids |
set[int]
|
The resids of the solute molecules, found by |
follow_skipped |
Counter[int]
|
Cluster id -> number of frames it wasn't followed in, for holding more than one solute. |
Initialize the analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
solutes
|
Iterable[str]
|
The residue names of the solutes. |
required |
follow
|
bool
|
Whether to write each solute's cluster to its own file. |
False
|
folder
|
str
|
The folder of the output directory the files go in ("" for the output directory itself). |
'coordinates'
|
Source code in src/molclusters/analysis/coordinates.py
prepare
¶
prepare(run: Run) -> None
Find the solute molecules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
Run
|
The run about to start. |
required |
Source code in src/molclusters/analysis/coordinates.py
analyse
¶
analyse(frame: Frame) -> None
Append the clusters of the current frame that hold a solute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The current frame. |
required |
Source code in src/molclusters/analysis/coordinates.py
finish
¶
finish(run: Run) -> None
Warn about the frames in which solutes weren't followed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
Run
|
The run that just ended. |
required |
Source code in src/molclusters/analysis/coordinates.py
nucleus
¶
Provides Nucleus, the nuclei inside the clusters (nucleus_data.csv).
Nucleus
¶
Bases: FrameAnalysis
Finds the nuclei inside the clusters, frame by frame.
A nucleus is a connected group, inside a cluster, of molecules with one of the
nucleus residue names. Each cluster's nuclei also go in the report (see
report_cluster).
Attributes:
| Name | Type | Description |
|---|---|---|
resnames |
set[str]
|
The residue names of the molecules that make up nuclei. |
nuclei |
dict[int, list[MolGroup]]
|
Cluster id -> the cluster's nuclei in the current frame (an empty list for a cluster without nuclei), for the analyses after this one. |
data |
ndarray
|
One row per frame (see |
Initialize an empty record, sized by prepare.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resnames
|
Iterable[str]
|
The residue names of the molecules that make up nuclei. |
required |
Source code in src/molclusters/analysis/nucleus.py
prepare
¶
prepare(run: Run) -> None
Make room for every frame of the run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
Run
|
The run about to start. |
required |
analyse
¶
analyse(frame: Frame) -> None
Find the nuclei of the current frame and record their averages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The current frame. |
required |
Source code in src/molclusters/analysis/nucleus.py
report_cluster
¶
Give the nuclei of a cluster, for the report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The current frame. |
required |
cluster
|
Cluster
|
The cluster. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
|
Source code in src/molclusters/analysis/nucleus.py
report
¶
Provides JsonReport, every cluster of every frame and its properties (molclusters.jsonl).
JsonReport
¶
Bases: FrameAnalysis
Records every cluster of every frame, with its properties.
The report is written frame by frame, as JSON Lines, compressed as asked; its
format is described in molclusters.report, which also reads it back. Every
other analysis of the run can add fields to it (see
FrameAnalysis.report_frame and report_cluster), so it runs after them all.
Attributes:
| Name | Type | Description |
|---|---|---|
compression |
ReportCompression
|
How the report is compressed: "zstd", "gzip" or "none". |
name |
str
|
The report's file name, e.g. |
Initialize the report, started by prepare.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compression
|
ReportCompression
|
How to compress the report: "zstd", "gzip" or "none". |
'zstd'
|
Source code in src/molclusters/analysis/report.py
prepare
¶
prepare(run: Run) -> None
Start the report with its header: the run's inputs and configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
Run
|
The run about to start. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If two analyses of the same class name would add to the report. |
Source code in src/molclusters/analysis/report.py
analyse
¶
analyse(frame: Frame) -> None
Add the clusters of the current frame to the report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The current frame. |
required |