From cf96249d86ea312e663000f97dbdc892778d2240 Mon Sep 17 00:00:00 2001 From: Amir Sadeghi <54184877+amirhs1@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:10:16 -0500 Subject: [PATCH] refactor(projects): move the studies to polyphys-lab MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - projects/: removed. The four studies' parsers, specs, and tests are in polyphys-lab, installed as polyphys_lab. (amirhs1/polyphys-lab#1). - polyphys/tests/probe/, analyze/test_pipeline.py, viz/test_helpers.py: remove the 27 tests that need a study's parser or specs (16 tier B, 11 tier C) and the fixtures only they used; they are in amirhs1/polyphys-lab#2. The three tests that open the real frames with the library alone stay, with polyphys/tests/data/probe/. The module docstrings say where the moved tests are. - polyphys/tests/manage/test_parser.py: a ChainCyl test of the error a species too wide for the cylinder raises, which only a study parser test reached; the moved-parser test expects the new message. - polyphys/manage/parser.py: the ImportError of a moved study parser names polyphys_lab..parser and the lab's URL; versionchanged 1.1.0. - benchmarks/organizer_timing.py: --parser takes :. - .github/workflows/ci.yaml, .github/pull_request_template.md, pyproject.toml, AGENTS.md, CONTRIBUTING.md: drop projects from the lint, type-check, and test commands and from package discovery and mypy; retire the projects area and the projects/ rows of the tier tables in AGENTS.md and README.md. - README.md, docs/source/ (decisions, pipeline, probe, studies, testing, viz, workflow), polyphys/spec.py, analyze/pipeline.py, tests/strategies.py, tests/test_docs.py: point to polyphys-lab; studies.md says where the folder named by the 1.0.0 ImportError went; decisions.md adds a paragraph on the companion repository. - CHANGELOG.md: three lines under Unreleased. Why: the maintainer decided on 2026-11-08 that PolyPhys stands alone, and on 2026-11-14 how: move the tests that need a study to the lab, since a copy of the parsers in polyphys/tests would ship in the wheel; link the lab now; keep no stub projects/README.md; retire the projects area. Assisted-by: Claude Code, claude-opus-5-5 (full implementation) Checks-run: python -m flake8 polyphys — clean Checks-run: python -m mypy polyphys/analyze polyphys/manage polyphys/probe polyphys/simulate — no issues in 25 files Checks-run: python -m pytest polyphys README.md --cov=polyphys --doctest-modules --doctest-glob='README.md' — 2065 passed, 2 skipped; 5 lines missed against 6 on origin/main Checks-run: python -m sphinx -W -b html docs/source — built Checks-run: python -m build — sdist and wheel built; no projects/ in the wheel --- .github/pull_request_template.md | 6 +- .github/workflows/ci.yaml | 10 +- AGENTS.md | 8 +- CHANGELOG.md | 3 + CONTRIBUTING.md | 9 +- README.md | 7 +- benchmarks/organizer_timing.py | 33 +- docs/source/decisions.md | 8 + docs/source/pipeline.md | 20 +- docs/source/probe.md | 2 +- docs/source/studies.md | 26 +- docs/source/testing.md | 11 +- docs/source/viz.md | 5 +- docs/source/workflow.md | 11 +- polyphys/analyze/pipeline.py | 4 +- polyphys/manage/parser.py | 19 +- polyphys/spec.py | 4 +- polyphys/tests/analyze/test_pipeline.py | 200 +----- polyphys/tests/manage/test_parser.py | 26 +- polyphys/tests/probe/conftest.py | 172 +----- polyphys/tests/probe/test_binders.py | 76 +-- polyphys/tests/probe/test_chain_size.py | 176 +----- polyphys/tests/probe/test_core.py | 148 +---- polyphys/tests/probe/test_histograms.py | 127 +--- polyphys/tests/probe/test_pairs.py | 102 +-- polyphys/tests/strategies.py | 2 +- polyphys/tests/test_docs.py | 8 +- polyphys/tests/viz/test_helpers.py | 51 -- projects/README.md | 49 -- projects/conftest.py | 10 - projects/hns_cub/__init__.py | 11 - projects/hns_cub/parser.py | 145 ----- projects/hns_cub/spec.py | 155 ----- projects/hns_cub/test_parser.py | 788 ------------------------ projects/hns_cyl/__init__.py | 11 - projects/hns_cyl/parser.py | 163 ----- projects/hns_cyl/spec.py | 143 ----- projects/hns_cyl/test_parser.py | 496 --------------- projects/test_studies.py | 576 ----------------- projects/trans_foci_cub/__init__.py | 11 - projects/trans_foci_cub/parser.py | 157 ----- projects/trans_foci_cub/spec.py | 146 ----- projects/trans_foci_cub/test_parser.py | 544 ---------------- projects/trans_foci_cyl/__init__.py | 11 - projects/trans_foci_cyl/parser.py | 181 ------ projects/trans_foci_cyl/spec.py | 160 ----- projects/trans_foci_cyl/test_parser.py | 630 ------------------- pyproject.toml | 3 +- 48 files changed, 152 insertions(+), 5512 deletions(-) delete mode 100644 projects/README.md delete mode 100644 projects/conftest.py delete mode 100644 projects/hns_cub/__init__.py delete mode 100644 projects/hns_cub/parser.py delete mode 100644 projects/hns_cub/spec.py delete mode 100644 projects/hns_cub/test_parser.py delete mode 100644 projects/hns_cyl/__init__.py delete mode 100644 projects/hns_cyl/parser.py delete mode 100644 projects/hns_cyl/spec.py delete mode 100644 projects/hns_cyl/test_parser.py delete mode 100644 projects/test_studies.py delete mode 100644 projects/trans_foci_cub/__init__.py delete mode 100644 projects/trans_foci_cub/parser.py delete mode 100644 projects/trans_foci_cub/spec.py delete mode 100644 projects/trans_foci_cub/test_parser.py delete mode 100644 projects/trans_foci_cyl/__init__.py delete mode 100644 projects/trans_foci_cyl/parser.py delete mode 100644 projects/trans_foci_cyl/spec.py delete mode 100644 projects/trans_foci_cyl/test_parser.py diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 8696d46f..bb102fae 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -32,9 +32,9 @@ Each check actually run, as `command → result`, and any human review or independent reference used to judge correctness. Then a `Not verified:` line for anything not checked, and why. -- [ ] `python -m flake8 polyphys projects` -- [ ] `python -m mypy polyphys/analyze polyphys/manage polyphys/probe polyphys/simulate projects` -- [ ] `python -m pytest polyphys README.md projects --cov=polyphys --cov-report=term-missing --cov-report=xml --doctest-modules --doctest-glob='README.md'` +- [ ] `python -m flake8 polyphys` +- [ ] `python -m mypy polyphys/analyze polyphys/manage polyphys/probe polyphys/simulate` +- [ ] `python -m pytest polyphys README.md --cov=polyphys --cov-report=term-missing --cov-report=xml --doctest-modules --doctest-glob='README.md'` - [ ] `python -m build` if packaging metadata, package data, or package layout changed - [ ] `python -m sphinx -W -b html docs/source docs/_build` if `docs/source/` changed - [ ] `CHANGELOG.md` has an entry under `[Unreleased]`, or this pull request needs none (`CONTRIBUTING.md`, "Changelog") diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 05697a76..49437246 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -36,7 +36,7 @@ jobs: python -m pip install flake8 - name: Run flake8 - run: flake8 polyphys projects + run: flake8 polyphys typecheck: name: Type-check @@ -59,7 +59,7 @@ jobs: python -m pip install -e .[dev] - name: Run mypy - run: mypy polyphys/analyze polyphys/manage polyphys/probe polyphys/simulate projects + run: mypy polyphys/analyze polyphys/manage polyphys/probe polyphys/simulate test: name: Test Python ${{ matrix.python-version }} @@ -94,7 +94,7 @@ jobs: # tests, which cannot run here, out of the measured set; the # MDAnalysis job below measures them. run: | - pytest polyphys README.md projects \ + pytest polyphys README.md \ --cov=polyphys \ --cov-config=.coveragerc-no-mda \ --cov-report=term-missing \ @@ -143,7 +143,7 @@ jobs: # report with the matrix jobs', which leave the probe layer out; the # floor stays on the matrix job. run: | - pytest polyphys README.md projects \ + pytest polyphys README.md \ --cov=polyphys \ --cov-report=term-missing \ --cov-report=xml \ @@ -200,7 +200,7 @@ jobs: # Without MDAnalysis, like the matrix jobs, and without coverage: # this job checks the floors, and it uploads no coverage report. run: | - .venv/bin/python -m pytest polyphys README.md projects \ + .venv/bin/python -m pytest polyphys README.md \ --doctest-modules \ --doctest-glob="README.md" diff --git a/AGENTS.md b/AGENTS.md index d0ded4f1..bd5bd843 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -19,10 +19,10 @@ commit it, and never copy its contents into a tracked file. | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | | Install | `python -m pip install -e '.[dev,viz]'`; add the `mda` extra for the probe tests and `docs` for the docs build | | Build | `python -m build` | -| Run all tests | `python -m pytest polyphys README.md projects --cov=polyphys --cov-report=term-missing --cov-report=xml --doctest-modules --doctest-glob='README.md'` | +| Run all tests | `python -m pytest polyphys README.md --cov=polyphys --cov-report=term-missing --cov-report=xml --doctest-modules --doctest-glob='README.md'` | | Run one test | `python -m pytest polyphys/tests/manage/test_parser.py::` | -| Lint | `python -m flake8 polyphys projects` | -| Type-check | `python -m mypy polyphys/analyze polyphys/manage polyphys/probe polyphys/simulate projects` | +| Lint | `python -m flake8 polyphys` | +| Type-check | `python -m mypy polyphys/analyze polyphys/manage polyphys/probe polyphys/simulate` | | Docs | `python -m sphinx -W -b html docs/source docs/_build` | | Full pre-commit gate | Lint, type-check, and all tests; add the build after a packaging change and the docs build when `docs/source/` changed | @@ -58,7 +58,6 @@ PolyPhys/ polyphys/probe/ core, chain_size, binders, histograms, pairs: per-frame observables read from LAMMPS trajectories with MDAnalysis (the mda extra) polyphys/viz/ colors, helpers: color lists and matplotlib helpers for the studies' figures (the viz extra) polyphys/tests/ tests for analyze/, manage/, probe/, simulate/, viz/, packaging, the docs pages, and the shared fixtures - projects/ one folder per study: its ProjectSpec, ProbeSpec, and analysis tables; not installed docs/source/ Sphinx sources README.md user guide; its examples run as doctests AI-POLICY.md AI policy, for contributors @@ -161,7 +160,6 @@ your role in each tier. Change both in the same commit. | `polyphys/probe/` | Supervised | Draft against acceptance criteria the maintainer set; every line is read. | | `polyphys/viz/` | Supervised | Draft against acceptance criteria the maintainer set; every line is read. | | `polyphys/tests/`, `.github/workflows/`, `pyproject.toml` | Supervised | Draft against acceptance criteria the maintainer set; every line is read. | -| `projects/` | Supervised | Draft a study's spec from the research code's values; every line is read. | | `docs/` | Supervised | Draft against acceptance criteria the maintainer set; every line is read. | - A path not listed is Supervised: draft against acceptance criteria the diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dc4b390..e47d24ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,9 @@ deprecation is listed under "Changed" and starts with `**Deprecated:**`. - Rate every part of the repository as Supervised in the AI tier tables of `README.md` and `AGENTS.md`: AI drafts and the maintainer reads every line, as the 1.0 code, tests, workflows, and documentation were made ([#269](https://github.com/amirhs1/PolyPhys/issues/269), [#271](https://github.com/amirhs1/PolyPhys/pull/271)) - Say on the front page of the documentation, and in the README, what the research code is: the maintainer's private repository PolyPhys was migrated from, which nothing in PolyPhys needs; call it the research code, not SciOps or the research repository, in the study specs, the tests, and the design decisions; and name its functions instead of its files and their line ranges in the study specs and their tests ([#272](https://github.com/amirhs1/PolyPhys/pull/272)) - Install PolyPhys from conda-forge, where it is published from 1.0.0, on the installation pages, with `mdanalysis` and `matplotlib-base` alongside it for the probe and viz layers, instead of installing it with pip into a conda environment ([#236](https://github.com/amirhs1/PolyPhys/issues/236)) +- Move the four published studies' parsers, specs, and tests from `projects/` to the companion repository [polyphys-lab](https://github.com/amirhs1/polyphys-lab), where each installs as `polyphys_lab.`, together with the probe, pipeline, and viz tests that run a study's specs on real frames or compare with the research code; PolyPhys's tests no longer import any study, and the `ImportError` of a moved study parser now names its `polyphys_lab` module ([#275](https://github.com/amirhs1/PolyPhys/issues/275)) +- Take the parser of `benchmarks/organizer_timing.py` as `--parser :`, such as `polyphys_lab.trans_foci_cub.parser:TransFociCub`, instead of a study folder in `projects/` ([#275](https://github.com/amirhs1/PolyPhys/issues/275)) +- Retire the `projects` area from `CONTRIBUTING.md`, "Names", and the `projects/` rows of the tier tables in `README.md` and `AGENTS.md` ([#275](https://github.com/amirhs1/PolyPhys/issues/275)) ## [1.0.0](https://github.com/amirhs1/PolyPhys/releases/tag/v1.0.0) - 2026-11-07 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2764be88..bb2a510b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -33,9 +33,9 @@ Run these from the repository root before opening a pull request: | Check | Command | | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | -| Lint | `python -m flake8 polyphys projects` | -| Type-check | `python -m mypy polyphys/analyze polyphys/manage polyphys/probe polyphys/simulate projects` | -| All tests | `python -m pytest polyphys README.md projects --cov=polyphys --cov-report=term-missing --cov-report=xml --doctest-modules --doctest-glob='README.md'` | +| Lint | `python -m flake8 polyphys` | +| Type-check | `python -m mypy polyphys/analyze polyphys/manage polyphys/probe polyphys/simulate` | +| All tests | `python -m pytest polyphys README.md --cov=polyphys --cov-report=term-missing --cov-report=xml --doctest-modules --doctest-glob='README.md'` | | One test | `python -m pytest polyphys/tests/manage/test_parser.py::` | | Package build | `python -m build`, after a packaging change | | Documentation build | `python -m sphinx -W -b html docs/source docs/_build`, when `docs/source/` changed | @@ -109,7 +109,7 @@ part of the repository. A commit's scope is its area, and the labels are ```text types: feat fix docs test refactor chore release question -areas: analyze manage simulate probe viz projects packaging guide ci agents deps +areas: analyze manage simulate probe viz packaging guide ci agents deps ``` | Type | Means | @@ -130,7 +130,6 @@ areas: analyze manage simulate probe viz projects packaging guide ci agents deps | `simulate` | `polyphys/simulate/`, `polyphys/tests/simulate/` | | `probe` | `polyphys/probe/`, `polyphys/tests/probe/` | | `viz` | `polyphys/viz/`, `polyphys/tests/viz/` | -| `projects` | `projects/`: the studies' specs, drivers, and tests, outside the package | | `packaging` | `pyproject.toml`, `polyphys/__version__.py`, `polyphys/tests/test_packaging.py`, `CITATION.cff`, `AUTHORS.rst` | | `guide` | `README.md`, `CONTRIBUTING.md`, `CHANGELOG.md`, `SECURITY.md`, `docs/`, `.github/ISSUE_TEMPLATE/` | | `ci` | `.github/workflows/`, `.github/dependabot.yml` | diff --git a/README.md b/README.md index 401d610b..ddecfabf 100755 --- a/README.md +++ b/README.md @@ -209,7 +209,8 @@ details. `probe` needs the `mda` extra and `viz.helpers` the `viz` extra. The package ships no study's parser: each study declares its own `ParserBase` subclass in its own folder. The parsers of the maintainer's -published studies are in `projects/`, outside the installed package. +published studies are in the companion repository +[polyphys-lab](https://github.com/amirhs1/polyphys-lab). ## Documentation @@ -238,7 +239,8 @@ layer's tests run on Python 3.14 with MDAnalysis. The Python examples above run as doctests in the test suite. A study _uses_ PolyPhys from outside the package rather than extending it: -its parser and its specs live in its own folder, as `projects/` shows. +its parser and its specs live in its own folder, as +[polyphys-lab](https://github.com/amirhs1/polyphys-lab) shows. PolyPhys was migrated from the maintainer's private research code, which the documentation, docstrings, and tests call _the research code_; the @@ -279,7 +281,6 @@ records the checks or human review applied to each part: | `polyphys/probe/` | Supervised | CI: flake8, mypy; pytest with doctests on Python 3.14 with MDAnalysis installed; maintainer review of every line | | `polyphys/viz/` | Supervised | CI: flake8, pytest with doctests on Python 3.11–3.14 with matplotlib installed; maintainer review of every line | | `polyphys/tests/`, `.github/workflows/`, `pyproject.toml` | Supervised | CI on every pull request: lint, type-check, tests, docs build, package build; maintainer review of every line | -| `projects/` | Supervised | CI: flake8, mypy, pytest of each spec against its parser and the research code's values; maintainer review of every line | | `docs/` | Supervised | CI: Sphinx build with warnings as errors for `docs/source/`; maintainer review of every line | Nothing that handles security, credentials, private data, or published results, diff --git a/benchmarks/organizer_timing.py b/benchmarks/organizer_timing.py index 71a61b9b..4a461047 100644 --- a/benchmarks/organizer_timing.py +++ b/benchmarks/organizer_timing.py @@ -55,11 +55,12 @@ Usage ===== -Run from a checkout or an environment where ``polyphys`` imports:: +Run from a checkout or an environment where ``polyphys`` and the study's +parser import:: python benchmarks/organizer_timing.py \\ - --parser trans_foci_cub --group bug --prop gyrTMon \\ - --relation tseries --repeats 5 + --parser polyphys_lab.trans_foci_cub.parser:TransFociCub \\ + --group bug --prop gyrTMon --relation tseries --repeats 5 ```` is the probe directory of one space, as :func:`~polyphys.analyze.pipeline.reduce_space` takes it; ``--hierarchy`` @@ -89,7 +90,6 @@ import time import tracemalloc from collections import Counter -from pathlib import Path from typing import Any, Callable, Sequence import numpy as np @@ -274,16 +274,16 @@ def residual(end_to_end: dict[str, float], return end_to_end['median_s'] - sum(p['median_s'] for p in parts) -def _study_parser(study: str) -> type: - """Return the parser class that ``projects//parser.py`` - exports, the only class in its ``__all__``; ``projects/`` is not an - installed package, so its folder is put on the import path.""" - projects = Path(__file__).resolve().parents[1] / 'projects' - if str(projects) not in sys.path: - sys.path.insert(0, str(projects)) - module = importlib.import_module(f'{study}.parser') - (name,) = module.__all__ - parser: type = getattr(module, name) +def _study_parser(target: str) -> type: + """Return the parser class that `target`, ``:``, names, + such as ``polyphys_lab.trans_foci_cub.parser:TransFociCub``; the module + must be importable.""" + module_name, sep, class_name = target.partition(':') + if not sep or not module_name or not class_name: + raise SystemExit( + f"--parser takes ':', not {target!r}") + module = importlib.import_module(module_name) + parser: type = getattr(module, class_name) return parser @@ -504,8 +504,9 @@ def main(argv: Sequence[str] | None = None) -> None: 'Prints timings and counts only; writes nothing.') cli.add_argument('probe_dir', help='the probe directory of one space') cli.add_argument('--parser', required=True, - help="study folder in projects/ whose parser.py " - "reads the names, such as 'trans_foci_cub'") + help="the parser class that reads the names, as " + "':', such as " + "'polyphys_lab.trans_foci_cub.parser:TransFociCub'") cli.add_argument('--group', required=True, help="particle group, 'bug'") cli.add_argument('--prop', required=True, help="property in the file names, such as 'gyrTMon'") diff --git a/docs/source/decisions.md b/docs/source/decisions.md index 3e15fa78..7401c320 100644 --- a/docs/source/decisions.md +++ b/docs/source/decisions.md @@ -93,6 +93,14 @@ public; importing one now fails as for any name the module never defined. The package's own tests and examples use made-up parsers in `polyphys.tests.synthetic`. +**The companion repository.** On 2026-11-08 the maintainer decided that +PolyPhys stands alone: the package and its tests do not refer to the +studies. In 1.1.0 the `projects/` folder moved to the companion repository +[polyphys-lab](https://github.com/amirhs1/polyphys-lab) (amirhs1/PolyPhys#275), where each study is a +folder installed as `polyphys_lab.`, with the tests that run its +specs on real frames. The lab depends on a released PolyPhys, never the +reverse. + [^adorf2018]: C. S. Adorf, P. M. Dodd, V. Ramasubramani, and S. C. Glotzer, "Simple data and workflow management with the signac framework," *Comput. Mater. Sci.* **146**, 220–229 (2018), diff --git a/docs/source/pipeline.md b/docs/source/pipeline.md index c0534be8..198430b1 100644 --- a/docs/source/pipeline.md +++ b/docs/source/pipeline.md @@ -71,9 +71,9 @@ naming one output twice, among the outputs the hooks declare with their `outputs` method: a hook output that another entry also yields would overwrite it. A property whose files are not found, such as a typo, raises `FileNotFoundError` before any directory is made. The tables of the maintainer's -studies live beside each study's spec in the repository's `projects/` -folder, so a data correction or a special case is code in the study's -folder, never an option of the module. +studies live beside each study's spec in the companion repository +[polyphys-lab](https://github.com/amirhs1/polyphys-lab), so a data correction or a special case is code in +the study's folder, never an option of the module. ```python from polyphys.analyze.pipeline import Autocorrelation, LocalDensities @@ -149,11 +149,8 @@ through under the property's own name. ## Running a space ```python -import sys -sys.path.insert(0, "/projects") - from polyphys.analyze.pipeline import reduce_space -from trans_foci_cub.spec import ANALYSIS, SPEC +from polyphys_lab.trans_foci_cub.spec import ANALYSIS, SPEC for table in ANALYSIS: reduce_space(f'probes/ns400nl5al5ac1-{table.group}-probe', SPEC, table, @@ -165,10 +162,9 @@ for table in ANALYSIS: Each table reads the probe directory of its own group, here `ns400nl5al5ac1-bug-probe` and `ns400nl5al5ac1-all-probe`, whose prefix is -the space name the parser gives. The study's folder is not an installed -package, so the driver puts `projects/` on the import path, as -[`projects/README.md`](https://github.com/amirhs1/PolyPhys/tree/main/projects) -shows. +the space name the parser gives. The study's specs are the +`polyphys_lab.trans_foci_cub` package of +[polyphys-lab](https://github.com/amirhs1/polyphys-lab), installed beside PolyPhys. `reduce_space` lists the files under `/` (the default hierarchy is the spec's `dir/file` layout), combines the stamps if @@ -199,7 +195,7 @@ row has no parent name. ## Building the density kernel A table with a {class}`~polyphys.analyze.pipeline.LocalDensities` hook, -such as the `all` table of every study in `projects/`, needs the `density` +such as the `all` table of every study in polyphys-lab, needs the `density` argument of `reduce_space`; without it the hook raises. The argument is any callable of the {class}`~polyphys.analyze.pipeline.DensityKernel` protocol: it takes one whole's histogram, summed over its frames, with its edges, the diff --git a/docs/source/probe.md b/docs/source/probe.md index 4fd83597..1c5455ef 100644 --- a/docs/source/probe.md +++ b/docs/source/probe.md @@ -508,7 +508,7 @@ completes the name. The loop-length rows are the maintainer's 2026-11-03 convention for a per-frame histogram, as the 1-D histogram probes keep their int64 counts; the research probe saved their sum over the frames as `loopLengthHistMon`, which is `rows.sum(axis=0)`. The H-NS analysis tables -in `projects/` still read that research artifact through +in polyphys-lab still read that research artifact through `tseries('loopLengthHist', 'Mon')`; reading the per-frame rows is an analysis-side change not made yet. diff --git a/docs/source/studies.md b/docs/source/studies.md index 4665832e..6f477cbf 100644 --- a/docs/source/studies.md +++ b/docs/source/studies.md @@ -39,12 +39,12 @@ The [Lineage](lineage.md) page defines the lineage levels and the groups. ## The steps Adding a study changes nothing in `polyphys/`, which ships no study's -parser: a study declares its parser in its own folder, as -`projects//parser.py` does for each of the maintainer's published -studies. +parser: a study declares its parser in its own folder, as each of the +maintainer's published studies does in +[polyphys-lab](https://github.com/amirhs1/polyphys-lab). 1. Declare the study's parser class, as in "Declaring the parser" below. -2. Make a folder for the study, `projects//`, as a Python package. +2. Make a folder for the study as a Python package. Its `spec.py` builds the study's `ProjectSpec` (as `SPEC`), its `ProbeSpec` (as `PROBE`), and a tuple of its `AnalysisTable` records, one per particle group (as `ANALYSIS`); its `__init__.py` exports the @@ -54,17 +54,19 @@ studies. 3. Build the specs. Each attribute a `ProjectSpec` names is checked against the parser when the spec is built, so a typo raises when `spec.py` is imported, not when the analysis runs. -4. Test the study. In this repository, `projects/test_studies.py` checks - each study's specs against its parser and against the values of the - research code; add the new study to its parametrizations. CI lints, - type-checks, and tests `projects/` with the package. +4. Test the study: check its specs against its parser and against the + values its analysis used before, as polyphys-lab's `tests/test_studies.py` + does for the maintainer's studies. 5. Run the study's data through the phases, as the [Workflow](workflow.md) page describes. -The maintainer's published studies are in the repository's top-level -[`projects/`](https://github.com/amirhs1/PolyPhys/tree/main/projects) -folder, which is not installed with the package. A study elsewhere uses the -same layout in its own repository. The [Design decisions](decisions.md) +The maintainer's published studies are in the companion repository +[polyphys-lab](https://github.com/amirhs1/polyphys-lab), one folder per study, each installed as +`polyphys_lab.`. PolyPhys does not depend on it; a study elsewhere +uses the same layout in its own repository. In PolyPhys 1.0.0, importing +one of these parsers from `polyphys.manage.parser` raises an `ImportError` +that names `projects//parser.py` in the PolyPhys repository; that +folder is now `projects//python/` in polyphys-lab. The [Design decisions](decisions.md) page records why each study is a folder outside the package. ## Declaring the parser diff --git a/docs/source/testing.md b/docs/source/testing.md index 36d0ee9a..4abafb39 100644 --- a/docs/source/testing.md +++ b/docs/source/testing.md @@ -10,7 +10,7 @@ installed; `--doctest-modules` imports `polyphys/viz/`, which needs matplotlib, the `viz` extra: ```bash -python -m pytest polyphys README.md projects --cov=polyphys --cov-report=term-missing --doctest-modules --doctest-glob='README.md' +python -m pytest polyphys README.md --cov=polyphys --cov-report=term-missing --doctest-modules --doctest-glob='README.md' ``` The README's examples run as doctests, and so do the `Examples` sections of the @@ -33,7 +33,7 @@ suite without MDAnalysis or coverage. To run it locally, with uv installed: uv venv --python 3.11 .venv uv pip install --python .venv/bin/python --resolution lowest-direct -e ".[viz]" uv pip install --python .venv/bin/python pytest hypothesis -.venv/bin/python -m pytest polyphys README.md projects --doctest-modules --doctest-glob='README.md' +.venv/bin/python -m pytest polyphys README.md --doctest-modules --doctest-glob='README.md' ``` ## Test data: three tiers @@ -87,7 +87,7 @@ reference value: - a parser name built from the parser's own pattern parses to its values and rebuilds to itself, for every synthetic parser in - `polyphys/tests/synthetic.py` and every study parser in `projects/`; + `polyphys/tests/synthetic.py`; `polyphys.tests.strategies.lineage_names` draws the names; - `block_average` of `a x + b` keeps every blocking level, with the mean `a mean + b`, the variance `a² var`, the standard error `|a| sem`, and the @@ -259,9 +259,8 @@ Keep the path out of tracked files; set the variable in your shell. | `testpaths = ["polyphys/tests"]` | The directory pytest collects when no path is given | | `filterwarnings = ["error", ...]` | Every warning raised during a test is an error; a test that expects one wraps the call in `pytest.warns`, and a test module that must ignore one names it in a `filterwarnings` mark. One warning is ignored everywhere: pyparsing 3.3's deprecation of `enablePackrat`, which matplotlib 3.8.4, the floor of the `viz` extra, calls on import | -The documented command passes `polyphys README.md projects`, so the -modules' docstrings, the README, and the studies are collected as well as -`polyphys/tests`. The examples of five pages, `quickstart.md`, +The documented command passes `polyphys README.md`, so the modules' +docstrings and the README are collected as well as `polyphys/tests`. The examples of five pages, `quickstart.md`, `lineage.md`, `studies.md`, `usage.md`, and `viz.md`, run through `polyphys/tests/test_docs.py`. diff --git a/docs/source/viz.md b/docs/source/viz.md index d3623eea..7317ed97 100644 --- a/docs/source/viz.md +++ b/docs/source/viz.md @@ -5,8 +5,7 @@ color lists the studies' figures use, a way to cut a color map down to part of its range, a rule for placing an axis's ticks, two legend-handle makers, a legend mover, and a band shader. Everything here is a thin layer over matplotlib; the figures themselves, with their study-specific labels, -filters, and layouts, belong to each study under `projects//`, not to -the package. +filters, and layouts, belong to each study, not to the package. The layer is an optional extra, since the rest of PolyPhys never draws: @@ -162,7 +161,7 @@ titles, and per-project settings), fifteen figure scripts tied to the research code's column names, four end-to-end-distribution plotters that nothing called, and seven helpers and seven palettes with no callers. None of that is in PolyPhys: the tables and figures are study code, for -`projects//` when a study's figures are ported, and the rest had no +each study's folder when its figures are ported, and the rest had no use. The one bug the ported helpers carried, a legend attribute that matplotlib 3.9 removed, is fixed with its replacement, which matplotlib 3.7 introduced. The extra requires `matplotlib>=3.8.4`. diff --git a/docs/source/workflow.md b/docs/source/workflow.md index bdf4fec4..1893126b 100644 --- a/docs/source/workflow.md +++ b/docs/source/workflow.md @@ -60,17 +60,12 @@ its number of frames, with {func}`~polyphys.probe.core.write_stamps`. one space, the study's `SPEC`, and one `AnalysisTable` of its `ANALYSIS`. For each property of the table, it joins the segments of each whole, groups the wholes of each state point into an ensemble, and averages each ensemble over -its replicates, writing each level to its stage. A study's folder is not an -installed package, so its driver puts `projects/` on the import path, as -[`projects/README.md`](https://github.com/amirhs1/PolyPhys/tree/main/projects) -shows. Each table reads the probe directory of its own group: +its replicates, writing each level to its stage. Each table reads the probe +directory of its own group: ```python -import sys -sys.path.insert(0, "/projects") - from polyphys.analyze.pipeline import reduce_space -from trans_foci_cub import ANALYSIS, SPEC +from polyphys_lab.trans_foci_cub import ANALYSIS, SPEC for table in ANALYSIS: reduce_space(f'probes/ns400nl5al5ac1-{table.group}-probe', SPEC, table, diff --git a/polyphys/analyze/pipeline.py b/polyphys/analyze/pipeline.py index e78dbcac..21ba1a88 100644 --- a/polyphys/analyze/pipeline.py +++ b/polyphys/analyze/pipeline.py @@ -16,8 +16,8 @@ :class:`~polyphys.spec.AnalysisTable` per particle group, listing a :class:`~polyphys.spec.PropertySpec` per probe artifact; both records are in :mod:`polyphys.spec`. The tables of the maintainer's studies live beside each -study's :class:`~polyphys.spec.ProjectSpec` in the repository's -``projects/`` folder, so a data correction or a special case is code in the +study's :class:`~polyphys.spec.ProjectSpec` in the companion repository +polyphys-lab, so a data correction or a special case is code in the study's folder, never an option of this module. Notation diff --git a/polyphys/manage/parser.py b/polyphys/manage/parser.py index 79033e54..d4760d99 100755 --- a/polyphys/manage/parser.py +++ b/polyphys/manage/parser.py @@ -29,11 +29,17 @@ .. versionchanged:: 1.0.0 The parsers of nine studies left this module. ``TransFociCyl``, - ``TransFociCub``, ``HnsCub``, and ``HnsCyl`` are in the ``projects/`` + ``TransFociCub``, ``HnsCub``, and ``HnsCyl`` went to the ``projects/`` folder of the PolyPhys repository, which is not installed; those of five unpublished studies were removed. Importing any of the four from here raises :exc:`ImportError` that says where it went. +.. versionchanged:: 1.1.0 + The four parsers are now in the companion repository + `polyphys-lab `_, as + ``polyphys_lab..parser``, and the :exc:`ImportError` points + there. + Dependencies ============ - Python standard library: `os`, `re`, `collections`, `dataclasses`, @@ -54,7 +60,7 @@ >>> parser.HnsCub # doctest: +ELLIPSIS Traceback (most recent call last): ... -ImportError: HnsCub moved out of PolyPhys in 1.0.0, to projects/hns_cub/... +ImportError: HnsCub moved out of PolyPhys in 1.0.0, to polyphys_lab.hns_cub... Notes ===== @@ -86,7 +92,8 @@ ] # The parsers of the published studies that left this module in 1.0.0, -# each with its folder under projects/ in the PolyPhys repository. +# each with its package in polyphys-lab, polyphys_lab.. +_LAB_URL = 'https://github.com/amirhs1/polyphys-lab' _MOVED_STUDIES: dict[str, str] = { 'HnsCub': 'hns_cub', 'HnsCyl': 'hns_cyl', @@ -1246,8 +1253,8 @@ def __getattr__(name: str) -> object: if name in _MOVED_STUDIES: raise ImportError( f"{name} moved out of PolyPhys in 1.0.0, to " - f"projects/{_MOVED_STUDIES[name]}/parser.py in the PolyPhys " - f"repository, which is not installed; copy it into your study's " - f"code.") + f"polyphys_lab.{_MOVED_STUDIES[name]}.parser in the " + f"polyphys-lab repository, {_LAB_URL}; copy it into your " + f"study's code.") raise AttributeError( f"module 'polyphys.manage.parser' has no attribute {name!r}") diff --git a/polyphys/spec.py b/polyphys/spec.py index 50aa59e1..66179dab 100644 --- a/polyphys/spec.py +++ b/polyphys/spec.py @@ -28,8 +28,8 @@ the parser rather than stored twice. PolyPhys holds no table of studies: a study builds its own spec where its -analysis lives. The specs of the maintainer's studies are in the repository's -top-level ``projects/`` folder, which is not part of the installed package. +analysis lives. The specs of the maintainer's studies are in the companion +repository polyphys-lab, which PolyPhys does not depend on. .. versionadded:: 0.9.0 The records of ``polyphys.manage.projects`` and the table records and diff --git a/polyphys/tests/analyze/test_pipeline.py b/polyphys/tests/analyze/test_pipeline.py index 471fa168..4e1111c3 100644 --- a/polyphys/tests/analyze/test_pipeline.py +++ b/polyphys/tests/analyze/test_pipeline.py @@ -13,15 +13,11 @@ probe run in the test: ``ChainSize`` on chains written by ``make_chain_files``, which skip without MDAnalysis and take their expected names from the parser and their expected means from the probe's own -results. The golden comparison with the research code's output is written -blind and runs only with the maintainer's data. +results. """ import dataclasses -import importlib import logging import re -import sys -import warnings from pathlib import Path import numpy as np @@ -1619,200 +1615,6 @@ def test_equilibrium_wholes_empty_directory_raises(ens_dir): pd.DataFrame({"whole": ["w1"]})) -# Golden comparison with the research code, written blind - - -def _study_module(): # pragma: no cover - tier C helpers - """The TransFociCub spec module. The study folders are not an installed - package: import them from the repository root, as projects/conftest.py - does for their own tests.""" - sys.path.insert(0, str(Path(__file__).resolve().parents[3])) - return importlib.import_module("projects.trans_foci_cub.spec") - - -def _research_folder(private_data_dir, phase, suffix): # pragma: no cover - """The one ``TransFociCub//*-bug-`` folder of the research - code's output under the data directory, or a skip.""" - parent = private_data_dir / "TransFociCub" / phase - folders = sorted(parent.glob(f"*-bug-{suffix}")) - if len(folders) != 1: - pytest.skip(f"no single *-bug-{suffix} folder under {parent}") - return folders[0] - - -def _bug_table(module): # pragma: no cover - """The study's analysis table of the ``bug`` group.""" - (table,) = [t for t in module.ANALYSIS if t.group == "bug"] - return table - - -def _cap_autocorrelation_lags(table, n_frames): # pragma: no cover - """The table with each ``Autocorrelation`` hook's lags capped at - ``n_frames - 1``. The research code's statsmodels ``acf`` clamped lags - beyond the series silently, and PolyPhys's rejects them, so a fixture - of short wholes needs the cap; the spec itself is unchanged.""" - return dataclasses.replace(table, properties=tuple( - dataclasses.replace(prop, post_hook=Autocorrelation( - min(prop.post_hook.nlags, n_frames - 1))) - if isinstance(prop.post_hook, Autocorrelation) else prop - for prop in table.properties)) - - -def _assert_ens_avg_matches(produced, reference): # pragma: no cover - """Every ``*-ensAvg.csv`` of `produced` with a counterpart in `reference` - equals it, to float64 round-off, and at least one does. The - pair-distance artifacts and the ACF band are left out: they changed on - purpose (criteria section 4), and the ACFs run on capped lags.""" - compared = 0 - for path in sorted(produced.glob("*-ensAvg.csv")): - if "pairDist" in path.name or "-acf" in path.name: - continue - counterpart = reference / path.name - if not counterpart.exists(): - continue - pd.testing.assert_frame_equal( - pd.read_csv(path), pd.read_csv(counterpart), rtol=1e-10, - atol=1e-12, check_like=True) - compared += 1 - assert compared > 0, f"no ensAvg file of {produced} has a counterpart" - - -@pytest.mark.private_data -def test_ens_avg_matches_the_research_code( # pragma: no cover - needs data - private_data_dir, tmp_path): - """The ensemble averages of one TransFociCub space, reduced from the - research code's probe output, match the research code's, for every - property it reduced the same way. - - Expects ``/TransFociCub/probes/-bug-probe/`` (the probe - phase of one space) and - ``/TransFociCub/analysis/-bug-ensAvg/`` (the research - code's ensemble averages). The probe folder is linked into a temporary - tree so that nothing is written under the data directory. - """ - module = _study_module() - probe = _research_folder(private_data_dir, "probes", "probe") - reference = _research_folder(private_data_dir, "analysis", "ensAvg") - linked = tmp_path / "probes" / probe.name - linked.parent.mkdir() - linked.symlink_to(probe, target_is_directory=True) - # The frame count from one whole's gyrTMon header, not its values. - n_frames = np.load(next(probe.rglob("*-gyrTMon.npy")), - mmap_mode="r").shape[0] - table = _cap_autocorrelation_lags(_bug_table(module), n_frames) - - reduce_space(linked, module.SPEC, table) - - produced = tmp_path / "analysis" / f"{probe.name.split('-')[0]}-bug-ensAvg" - _assert_ens_avg_matches(produced, reference) - - -def _uncrowded(whole, wholes): # pragma: no cover - """The whole without crowders of the family and replicate of `whole`: - the name that differs only in the crowder count and box side, - ``ncl``, and has ``nc0``.""" - family = re.sub(r"nc\d+l[\d.]+", "", whole) - (match,) = [other for other in wholes if "nc0l" in other - and re.sub(r"nc\d+l[\d.]+", "", other) == family] - return match - - -def _probe_tier_b_wholes(module, wholes, probe_dir): # pragma: no cover - """Probe each whole's tier B polymer dump (``polyphys/tests/data/probe/``) - with the study's ``ProbeSpec``, opened with the data file of its whole - without crowders, and save the chain-size observables, the foci pair - quantities, and the stamps under the research names in - ``probe_dir/.ring/``.""" - from polyphys.probe import ( - ChainSize, PairDistances, make_universe, write_stamps) - - data = Path(__file__).resolve().parents[1] / "data" / "probe" - for whole in wholes: - topology = _uncrowded(whole, wholes) - with warnings.catch_warnings(): - warnings.filterwarnings( - "ignore", "Reader has no dt information", UserWarning) - universe = make_universe( - data / topology / f"{topology}.ring.all.data.gz", - data / whole / f"{whole}.ring.bug.last50.lammpstrj.gz") - parser = module.SPEC.parser(f"{whole}.ring", "whole", - module.SPEC.group) - folder = probe_dir / f"{whole}.ring" - folder.mkdir(parents=True) - name = f"{parser.name}-{parser.group}" - ChainSize.from_spec(universe, module.PROBE).run().save(folder, name) - PairDistances.from_spec(universe, module.PROBE, parser).run().save( - folder, name) - write_stamps(parser, universe.trajectory.n_frames, folder) - - -@pytest.mark.private_data -@pytest.mark.filterwarnings( - "ignore:Reader has no dt information:UserWarning") -def test_probe_and_reduce_match_the_research_ens_avg( # pragma: no cover - private_data_dir, tmp_path): - """Probing the TransFociCub polymer dumps with the study's ``ProbeSpec``, - saving under the research names, and reducing them with ``reduce_space`` - reproduces the research code's ensemble averages end to end. The wholes - are those of the research probe folder; the dumps are their tier B - copies, the same 50 frames the research probe read.""" - pytest.importorskip("MDAnalysis") - module = _study_module() - research = _research_folder(private_data_dir, "probes", "probe") - reference = _research_folder(private_data_dir, "analysis", "ensAvg") - wholes = sorted(path.name.removesuffix(".ring") - for path in research.iterdir() if path.is_dir()) - probe_dir = tmp_path / "probes" / research.name - _probe_tier_b_wholes(module, wholes, probe_dir) - # The tier B polymer dumps hold the wholes' last 50 frames. - table = _cap_autocorrelation_lags(_bug_table(module), 50) - - reduce_space(probe_dir, module.SPEC, table) - - produced = (tmp_path / "analysis" - / f"{research.name.split('-')[0]}-bug-ensAvg") - _assert_ens_avg_matches(produced, reference) - - -@pytest.mark.private_data -@pytest.mark.parametrize("prop", ["gyrTMon", "shapeTMon", "asphericityTMon"]) -def test_space_tseries_from_spec_matches_the_research_code( # pragma: no cover - private_data_dir, prop): - """The space table of a TransFociCub time series, built from the - research code's ensemble averages, equals the research code's space - table: the same rows by state point and time index, the same - statistics, time, and attributes, under the spec's renaming of the - crowder volume fraction and the documented mapping of the short names - (Usage, "Lineage names").""" - spec = _study_module().SPEC - ens_avg = _research_folder(private_data_dir, "analysis", "ensAvg") - space_dir = _research_folder(private_data_dir, "analysis", "space") - theirs = pd.read_csv( - space_dir / f"{space_dir.name.split('-')[0]}-bug-{prop}-space.csv") - theirs = theirs.rename(columns={"phi_c_bulk": "phi_bulk_c", - "phi_c_bulk_round": "phi_bulk_c_round"}) - # A research short name, parsed at its lineage and rebuilt, gives the - # current form. - theirs = theirs.assign(**{ - lineage: theirs[lineage].map( - lambda text, lineage=lineage: getattr( - spec.parser(text, lineage, spec.group), lineage)) - for lineage in ("ensemble", "space")}) - key = ["ensemble_long", "t_index"] - - ours = space_tseries_from_spec(ens_avg, spec, prop) - - ours = ours.sort_values(key).reset_index(drop=True) - theirs = theirs.sort_values(key).reset_index(drop=True) - assert set(theirs.columns) <= set(ours.columns) - # The statistics are float64 values read from the same CSV files; the - # integer attributes compare exactly. The research table wrote some - # attributes as integers that PolyPhys holds as floats, hence no dtype - # check. - pd.testing.assert_frame_equal(ours[theirs.columns], theirs, rtol=1e-10, - atol=1e-12, check_dtype=False) - - # The space stage from the spec diff --git a/polyphys/tests/manage/test_parser.py b/polyphys/tests/manage/test_parser.py index 53f2cb40..6da57939 100644 --- a/polyphys/tests/manage/test_parser.py +++ b/polyphys/tests/manage/test_parser.py @@ -1,7 +1,7 @@ """Tests for :mod:`polyphys.manage.parser`. The parsers here are the synthetic ones of :mod:`polyphys.tests.synthetic`; -each published study tests its own parser in ``projects//``. +each published study tests its own parser in the polyphys-lab repository. """ import importlib import os @@ -705,6 +705,22 @@ def test_crowder_bulk_values_are_sums_over_species(): rel=1e-12) +@pytest.mark.parametrize('dcrowd', [ + pytest.param('10', id='as-wide-as-the-cylinder'), + pytest.param('11', id='wider-than-the-cylinder'), +]) +def test_species_without_room_names_itself_and_the_lineage(dcrowd): + """A species too wide for the cylinder raises, naming it and the name.""" + # r5.5 gives dcyl = 2 * 5.5 - 1 = 10, so a crowder of size 10 or more + # leaves its centers no room across the cylinder. + artifact = f'N200r5.5lz10ac{dcrowd}nc5dt0.005bdump1adump1' + + with pytest.raises(ValueError, match=re.escape( + f"species 'c' of {artifact!r} (lineage 'ensemble_long'): " + "no room for particle centers")): + ChainCyl(artifact, 'ensemble_long', 'all') + + @pytest.mark.parametrize('parser_class, artifact', [ pytest.param(RingCub, 'al5nl5ml125ns400ac3nc7735l45dt0.005bdump2000adump5000', @@ -1156,11 +1172,11 @@ def test_keyword_spec_rejects_bad_declaration(kwargs, match): @pytest.mark.parametrize('name, message', [ - pytest.param('HnsCub', 'projects/hns_cub/parser.py', id='HnsCub'), - pytest.param('HnsCyl', 'projects/hns_cyl/parser.py', id='HnsCyl'), - pytest.param('TransFociCub', 'projects/trans_foci_cub/parser.py', + pytest.param('HnsCub', 'polyphys_lab.hns_cub.parser', id='HnsCub'), + pytest.param('HnsCyl', 'polyphys_lab.hns_cyl.parser', id='HnsCyl'), + pytest.param('TransFociCub', 'polyphys_lab.trans_foci_cub.parser', id='TransFociCub'), - pytest.param('TransFociCyl', 'projects/trans_foci_cyl/parser.py', + pytest.param('TransFociCyl', 'polyphys_lab.trans_foci_cyl.parser', id='TransFociCyl'), ]) def test_moved_study_parser_import_raises_import_error(name, message): diff --git a/polyphys/tests/probe/conftest.py b/polyphys/tests/probe/conftest.py index 1bf0526b..0a0e903b 100644 --- a/polyphys/tests/probe/conftest.py +++ b/polyphys/tests/probe/conftest.py @@ -11,31 +11,17 @@ make_chain_universe The files of ``make_chain_files``, in ``polyphys/tests/conftest.py``, opened with :func:`polyphys.probe.make_universe`. -study_spec - The spec module, ``projects//spec.py``, of a study. -real_polymer_whole, real_window_whole, real_crowded_whole, \ -real_foci_whole, real_hns_whole, real_nh0_whole - The real wholes of the tier B and tier C tests, one test per whole (see - "Real frames" below). +real_polymer_whole, real_window_whole + The real wholes of the tier B tests, one test per whole (see "Real + frames" below). real_files The files of a real whole: its polymer dump and the topology it is opened with, and its ``all`` window and data file when they are tier B. -private_window_files - The ``all`` window and data file of a crowded whole, tier C. -real_transfocicub_whole - The 4 TransFociCub wholes, whose research-code probe output is the - tier C reference of the pipeline tests. -research_probe_file - One property file of the research code's probe output of a whole, - tier C. real_universe A real dump opened with :func:`polyphys.probe.make_universe`. trajectory_arrays The positions, box lengths, and time steps of every frame of a universe, in one pass. -reference_distances - Minimum-image distances between two sets of positions, frame by frame, - from :func:`MDAnalysis.lib.distances.distance_array`. Real frames ----------- @@ -49,10 +35,10 @@ without ``epshc``; their copies here insert ``epshc1``, the value of their H-NS core-crowder pair coefficients, so that the parser reads them. -The tier C files, the ``all`` windows and data files of the other crowded -wholes, stay in the maintainer's folder -``//trjs/`` under the runs' own names, with -```` the study's project name, such as ``HnsCub``. +The tests here open these files with the library alone. The tests that +probe them with a study's specs, and those that compare them with the +research code's output, are in the polyphys-lab repository +(https://github.com/amirhs1/polyphys-lab), with the studies' parsers. A polymer dump holds only the polymer group, so it cannot be opened with its whole's full data file. It is opened with the data file of the whole @@ -61,12 +47,9 @@ H-NS proteins for Hns. The research code did the same. The polymer atoms carry the IDs 1 to n in every system, with the crowders after them. """ -import importlib -import sys import warnings from dataclasses import dataclass from pathlib import Path -from types import ModuleType from typing import Any, Callable, Mapping, Optional import numpy as np @@ -94,14 +77,14 @@ def _make(positions: Any, **kwargs: Any) -> Any: @dataclass(frozen=True) class RealWhole: - """One real whole of the tier B and tier C tests. + """One real whole of the tier B tests. Attributes ---------- id : str The test id, such as ``'HnsCub-nh30-nc10743-ens1'``. study : str - Its study's folder in ``projects/``, such as ``'hns_cub'``. + Its study's package name in polyphys-lab, such as ``'hns_cub'``. whole : str The whole's name without ``.ring``, as its parser reads it. topology : str @@ -143,22 +126,6 @@ class RealWhole: n_shared_frames: int source: str - def polymer_counts(self) -> dict[str, int]: - """The atoms of each species of the study's spec in the polymer - dump: two patches per H-NS core (Jung, Sadeghi, and Ha, Sci. Rep. - 14, 139 (2024)), the small monomers and the foci of a TransFoci - ring, and no crowders.""" - if self.group == 'nucleoid': - return {'Mon': self.n_mon, 'HnsPatch': 2 * self.n_hns, - 'Hns': self.n_hns, 'Crd': 0} - return {'Mon': self.n_mon, 'Foci': self.n_foci, - 'Dna': self.n_mon - self.n_foci, 'Crd': 0} - - def window_counts(self) -> dict[str, int]: - """The atoms of each species of the study's spec in the ``all`` - window: the polymer group plus the crowders.""" - return {**self.polymer_counts(), 'Crd': self.n_crowd} - def _real_wholes() -> list[RealWhole]: """The 22 real wholes: per study, two replicates without crowders and @@ -219,48 +186,6 @@ def real_window_whole(request: pytest.FixtureRequest) -> RealWhole: return request.param -@pytest.fixture(params=_params(lambda whole: whole.window_tier == 'C')) -def real_crowded_whole(request: pytest.FixtureRequest) -> RealWhole: - """Each of the 8 crowded wholes whose ``all`` window and data file are - tier C.""" - return request.param # pragma: no cover - needs data - - -@pytest.fixture(params=_params(lambda whole: whole.n_foci > 0)) -def real_foci_whole(request: pytest.FixtureRequest) -> RealWhole: - """Each of the 8 TransFoci wholes.""" - return request.param - - -@pytest.fixture(params=_params(lambda whole: whole.n_hns > 0)) -def real_hns_whole(request: pytest.FixtureRequest) -> RealWhole: - """Each of the 10 Hns wholes with H-NS proteins.""" - return request.param - - -@pytest.fixture(params=_params( - lambda whole: whole.group == 'nucleoid' and whole.n_hns == 0)) -def real_nh0_whole(request: pytest.FixtureRequest) -> RealWhole: - """Each of the 4 Hns wholes without H-NS proteins.""" - return request.param - - -@pytest.fixture(scope='session') -def study_spec() -> Callable[[str], ModuleType]: - """Factory: the spec module ``projects//spec.py`` of a study, - with its ``SPEC`` and ``PROBE``. The study folders are not an installed - package, so the repository root is put on the import path, as the - pipeline's tier C test does.""" - root = str(Path(__file__).resolve().parents[3]) - if root not in sys.path: - sys.path.insert(0, root) - - def _load(study: str) -> ModuleType: - return importlib.import_module(f"projects.{study}.spec") - - return _load - - @dataclass(frozen=True) class RealFiles: """The files of a real whole; `window` and `data` are None when they @@ -293,26 +218,6 @@ def _files(whole: RealWhole) -> RealFiles: return _files -@pytest.fixture -def private_window_files( - private_data_dir: Path, - study_spec: Callable[[str], ModuleType], -) -> Callable[[RealWhole], tuple[Path, Path]]: - """Factory: the tier C ``all`` window and data file of a crowded whole, - under ``//trjs//``, with - ```` the study's project name (``SPEC.name``, such as - ``HnsCub``). Use it only in a test marked ``private_data``.""" - - def _files(whole: RealWhole) -> tuple[Path, Path]: # pragma: no cover - project = study_spec(whole.study).SPEC.name - folder = private_data_dir / project / 'trjs' / whole.source - (window,) = sorted(folder.glob(f"{whole.source}.j*.ring.all.window." - "lammpstrj.gz")) - return window, folder / f"{whole.source}.ring.all.data.gz" - - return _files # pragma: no cover - needs data - - @pytest.fixture def real_universe() -> Callable[[Path, Path], Any]: """Factory: a data file and dump opened with @@ -346,62 +251,3 @@ def _arrays(universe: Any) -> Mapping[str, np.ndarray]: 'steps': np.array([frame[2] for frame in frames])} return _arrays - - -@pytest.fixture -def reference_distances() -> Callable[..., np.ndarray]: - """Factory: the distances between the rows of `first` and `second`, - shapes ``(n_frames, n, 3)`` and ``(n_frames, m, 3)``, frame by frame - with :func:`MDAnalysis.lib.distances.distance_array` and the minimum - image along the geometry's periodic directions: all three in a cube; - only z in a cylinder, whose box lengths across the axis are replaced by - 1e4, far beyond any separation in it, so that only z is imaged. - Returns shape ``(n_frames, n, m)``.""" - - def _distances(first: np.ndarray, second: np.ndarray, boxes: np.ndarray, - geometry: str) -> np.ndarray: - from MDAnalysis.lib.distances import distance_array - - lengths = np.array(boxes, dtype=np.float64) - if geometry == 'cylindrical': - lengths[:, :2] = 1e4 - return np.stack([ - distance_array(a, b, box=np.concatenate([box, [90.0] * 3])) - for a, b, box in zip(first, second, lengths)]) - - return _distances - - -@pytest.fixture(params=_params(lambda whole: whole.study == 'trans_foci_cub')) -def real_transfocicub_whole(request: pytest.FixtureRequest) -> RealWhole: - """Each of the 4 TransFociCub wholes: the research code probed the same - 50 frames of their polymer dumps, and that output, tier C, is the - reference of the pipeline tests.""" - return request.param # pragma: no cover - needs data - - -@pytest.fixture -def research_probe_file( - private_data_dir: Path, - study_spec: Callable[[str], ModuleType], -) -> Callable[..., Path]: - """Factory: the file of one property in the research code's probe output - of a real whole, - ``//probes/--probe/.ring/`` - ``.ring--.``, with ```` the - study's ``SPEC.name``; the research probe named a whole with its - topology suffix. The test is skipped when the study has no single such - probe folder. Use it only in a test marked ``private_data``.""" - - def _file(whole: RealWhole, prop: str, # pragma: no cover - needs data - ext: str = 'npy') -> Path: - project = study_spec(whole.study).SPEC.name - probes = private_data_dir / project / 'probes' - folders = sorted(probes.glob(f"*-{whole.group}-probe")) - if len(folders) != 1: - pytest.skip(f"no single research {whole.group} probe folder " - f"under {probes}") - return (folders[0] / f"{whole.whole}.ring" - / f"{whole.whole}.ring-{whole.group}-{prop}.{ext}") - - return _file # pragma: no cover - needs data diff --git a/polyphys/tests/probe/test_binders.py b/polyphys/tests/probe/test_binders.py index 69fc50d9..acd2c49c 100644 --- a/polyphys/tests/probe/test_binders.py +++ b/polyphys/tests/probe/test_binders.py @@ -12,11 +12,9 @@ the bridged pairs' distances. The minimum-image distances are checked against ``MDAnalysis.lib.distances.distance_array``. -The real-frame tests run the Hns studies' probe specs on the tier B polymer -dumps of their wholes (``polyphys/tests/probe/conftest.py``, "Real -frames"). Their expected values are the identities the counts obey by -definition, with the binder numbers of the wholes' names and two patches per -H-NS core, and the minimum-image distances of ``distance_array``. +The tests that run a study's probe spec on the real frames of +``polyphys/tests/data/probe/`` are in the polyphys-lab repository, with +the studies' parsers. """ from pathlib import Path from typing import Any, Callable, Sequence @@ -616,71 +614,3 @@ def test_save_before_run_raises(make_binder_universe, binding_frames, with pytest.raises(RuntimeError, match=r"Call run\(\) before save"): analysis.save(tmp_path, 'N10ens1-nucleoid') - - -# Real frames - - -@pytest.fixture -def real_binders(real_files, real_universe, study_spec): - """Factory: the study's :meth:`BinderStatistics.from_spec` on a real Hns - polymer dump, with the universe it reads; not yet run.""" - - def _build(whole): - files = real_files(whole) - universe = real_universe(files.topology, files.polymer) - return (BinderStatistics.from_spec( - universe, study_spec(whole.study).PROBE), universe) - - return _build - - -def test_real_binding_counts_obey_their_identities(real_hns_whole, - real_binders): - """In every real frame, each patch is free or engaged, each binder is - free, dangling, or bridging, the loop histogram counts every distinct - cis or trans pair, no more pairs than bridges form, and engaged patches - make at least one contact each.""" - analysis, _ = real_binders(real_hns_whole) - n_hns = real_hns_whole.n_hns - - results = analysis.run().results - - pairs = results['n_pairs_cis'] + results['n_pairs_trans'] - assert_array_equal(results['n_patches_free'] - + results['n_patches_engaged'], np.full(50, 2 * n_hns)) - assert_array_equal(results['n_binders_free'] - + results['n_binders_dangling'] - + results['n_binders_bridging'], np.full(50, n_hns)) - assert_array_equal(results['loop_length_histogram'].sum(axis=1), pairs) - assert np.all(pairs <= results['n_binders_bridging']) - assert np.all(results['n_patch_monomer_contacts'] - >= results['n_patches_engaged']) - - -def test_real_binder_distances_are_the_minimum_image_distances( - real_hns_whole, real_binders, study_spec, trajectory_arrays, - reference_distances): - """The stored monomer-to-patch distances of every real frame are the - minimum-image distances along the geometry's periodic directions.""" - probe = study_spec(real_hns_whole.study).PROBE - analysis, universe = real_binders(real_hns_whole) - - distances = analysis.run().results['distances'] - - arrays = trajectory_arrays(universe) - expected = reference_distances( - arrays['positions'][:, analysis.chain.indices], - arrays['positions'][:, analysis.patches.indices], arrays['boxes'], - probe.geometry) - # float32 positions of at most about 150 carry a round-off of about - # 1e-5 in a separation. - assert_allclose(distances, expected, rtol=0, atol=5e-5) - - -def test_real_whole_without_hns_has_no_binders_to_count(real_nh0_whole, - real_binders): - """A real Hns whole without H-NS proteins has no patches, which the - binder analysis rejects.""" - with pytest.raises(ValueError, match="positive multiple of 2"): - real_binders(real_nh0_whole) diff --git a/polyphys/tests/probe/test_chain_size.py b/polyphys/tests/probe/test_chain_size.py index e3d56149..cf7ccb7a 100644 --- a/polyphys/tests/probe/test_chain_size.py +++ b/polyphys/tests/probe/test_chain_size.py @@ -10,16 +10,9 @@ and Thirumalai, J. Phys. Chem. B 108, 6564 (2004), as MDAnalysis implements them. -The real-frame tests read the tier B and tier C files of the studies' -wholes (``polyphys/tests/probe/conftest.py``, "Real frames"). Their expected -values are properties or other files of the same run: the chain measured in -the ``all`` window at a time step the polymer dump shares, which holds the -same configuration; the FENE bound on a bond, :math:`R_0 = 1.5 \\sigma` -(``docs/source/design.md``), far below half the box, so a wrapped chain -whose bonds are all shorter than half the box is whole and unwrapping it -changes nothing; and the first level of the blocking transform of -Flyvbjerg and Petersen, J. Chem. Phys. 91, 461 (1989), whose standard -error is the naive one of the uncorrelated estimate. +The tests that run a study's probe spec on the real frames of +``polyphys/tests/data/probe/`` are in the polyphys-lab repository, with +the studies' parsers. """ import numpy as np import pytest @@ -29,7 +22,6 @@ assert_equal, ) -from polyphys.analyze.stats import block_average from polyphys.tests.synthetic import ChainCyl from polyphys.spec import ( CHAIN_SIZE_OBSERVABLES, @@ -393,165 +385,3 @@ def test_save_before_run_raises(make_chain_universe, straight_chain, with pytest.raises(RuntimeError, match=r"Call run\(\) before save"): analysis.save(tmp_path, 'N10ens1-bug') - - -# Real frames - - -def _at_shared_steps(polymer, window, probe, trajectory_arrays): - """The spec's observables of the chain in the polymer dump and in the - window, at the time steps both hold, each flattened and joined in the - spec's order; and the number of those steps.""" - polymer_steps = trajectory_arrays(polymer)['steps'] - window_steps = trajectory_arrays(window)['steps'] - shared = np.intersect1d(polymer_steps, window_steps) - values = [ - ChainSize.from_spec(universe, probe).run( - frames=np.flatnonzero(np.isin(steps, shared))).results - for universe, steps in ((polymer, polymer_steps), - (window, window_steps))] - return ([np.concatenate([result[obs].ravel() - for obs in probe.observables]) - for result in values], shared.size) - - -def test_real_chain_size_agrees_between_the_polymer_dump_and_the_window( - real_window_whole, real_files, real_universe, study_spec, - trajectory_arrays): - """At every time step the polymer dump and the ``all`` window share, - each chain-size observable of the spec is the same in both.""" - probe = study_spec(real_window_whole.study).PROBE - files = real_files(real_window_whole) - polymer = real_universe(files.topology, files.polymer) - window = real_universe(files.data, files.window) - - (from_polymer, from_window), n_shared = _at_shared_steps( - polymer, window, probe, trajectory_arrays) - - assert n_shared == real_window_whole.n_shared_frames - assert_allclose(from_polymer, from_window, rtol=RTOL, atol=ATOL) - - -@pytest.mark.private_data # pragma: no cover - needs data -def test_real_chain_size_agrees_with_the_crowded_window( - real_crowded_whole, real_files, private_window_files, real_universe, - study_spec, trajectory_arrays): - """In a crowded whole, the polymer dump opened with the borrowed - topology and the ``all`` window opened with the whole's own data file - give the same chain-size observables at every shared time step.""" - probe = study_spec(real_crowded_whole.study).PROBE - files = real_files(real_crowded_whole) - window_path, data_path = private_window_files(real_crowded_whole) - polymer = real_universe(files.topology, files.polymer) - window = real_universe(data_path, window_path) - - (from_polymer, from_window), n_shared = _at_shared_steps( - polymer, window, probe, trajectory_arrays) - - assert n_shared == real_crowded_whole.n_shared_frames - assert_allclose(from_polymer, from_window, rtol=RTOL, atol=ATOL) - - -def test_real_chain_bonds_are_shorter_than_half_the_periodic_box( - real_polymer_whole, real_files, real_universe, study_spec, - trajectory_arrays): - """In every frame of a polymer dump, every bond of the chain spans less - than half the box along each periodic direction: the wrapping splits no - chain. (Free H-NS proteins do cross the boundary of the periodic cube; - their core-patch bonds are not checked.)""" - probe = study_spec(real_polymer_whole.study).PROBE - files = real_files(real_polymer_whole) - universe = real_universe(files.topology, files.polymer) - arrays = trajectory_arrays(universe) - first, second = universe.select_atoms( - probe.selections[probe.chain]).bonds.indices.T - periodic = {'cubic': [0, 1, 2], 'cylindrical': [2]}[probe.geometry] - - spans = np.abs(arrays['positions'][:, first] - - arrays['positions'][:, second])[..., periodic] - halves = 0.5 * arrays['boxes'][:, None, periodic] - - assert np.count_nonzero(spans >= halves) == 0 - - -def test_real_asphericity_does_not_depend_on_unwrap( - real_polymer_whole, real_files, real_universe, study_spec): - """On real chains that no boundary splits, the asphericity is the same - whether or not the chain is unwrapped first, in every study.""" - probe = study_spec(real_polymer_whole.study).PROBE - files = real_files(real_polymer_whole) - chain = real_universe(files.topology, files.polymer).select_atoms( - probe.selections[probe.chain]) - - wrapped, unwrapped = ( - ChainSize(chain, ('asphericityT',), unwrap=unwrap).run().results[ - 'asphericityT'] for unwrap in (False, True)) - - assert_allclose(unwrapped, wrapped, rtol=RTOL, atol=ATOL) - - -def test_block_average_of_a_real_gyration_series_starts_at_the_naive_error( - real_polymer_whole, real_files, real_universe, study_spec): - """Block averaging runs on the 50-frame radius-of-gyration series of a - real chain, with finite errors at every level and the naive standard - error at the first.""" - probe = study_spec(real_polymer_whole.study).PROBE - files = real_files(real_polymer_whole) - chain = real_universe(files.topology, files.polymer).select_atoms( - probe.selections[probe.chain]) - series = ChainSize(chain, ('gyrT',)).run().results['gyrT'] - - result = block_average(series) - - assert np.all(np.isfinite(result.sem)) - # Both are float64 reductions of the same 50 values. - assert result.sem[0] == pytest.approx( - series.std(ddof=1) / np.sqrt(series.size), rel=1e-12) - - -# Against the research code's probe (tier C) - - -@pytest.mark.private_data # pragma: no cover - needs data -@pytest.mark.parametrize('observable', ['gyrT', 'shapeT', 'asphericityT']) -def test_real_chain_size_matches_the_research_code( - real_transfocicub_whole, real_files, real_universe, study_spec, - research_probe_file, observable): - """On the 50 frames of a TransFociCub polymer dump, each scalar - chain-size observable of the spec equals the research code's probe of - the same frames (``trans_foci_cub_bug``).""" - whole = real_transfocicub_whole - probe = study_spec(whole.study).PROBE - files = real_files(whole) - universe = real_universe(files.topology, files.polymer) - expected = np.load(research_probe_file(whole, f"{observable}Mon")) - - results = ChainSize.from_spec(universe, probe).run().results - - # The research code shifted the float32 positions by half the box - # before measuring, and this probe recenters the chain, so the two - # differ by the float32 round-off of coordinates of about 20: 1e-6. - assert_allclose(results[observable], expected, rtol=RTOL, atol=ATOL) - - -@pytest.mark.private_data # pragma: no cover - needs data -def test_real_principal_axes_match_the_research_code_up_to_sign( - real_transfocicub_whole, real_files, real_universe, study_spec, - research_probe_file): - """On the 50 frames of a TransFociCub polymer dump, each frame's - principal axes equal the research code's up to the sign of each axis, - which the eigenvector solver sets arbitrarily.""" - whole = real_transfocicub_whole - probe = study_spec(whole.study).PROBE - files = real_files(whole) - universe = real_universe(files.topology, files.polymer) - expected = np.load(research_probe_file(whole, 'principalTMon')) - - axes = ChainSize.from_spec(universe, probe).run().results['principalT'] - - # Per frame and axis, the smaller of |a - b| and |a + b|; the same - # float32 round-off as the scalar observables. - closest = np.minimum(np.linalg.norm(axes - expected, axis=-1), - np.linalg.norm(axes + expected, axis=-1)) - assert closest.shape == (50, 3) - assert_allclose(closest, np.zeros_like(closest), rtol=0, atol=ATOL) diff --git a/polyphys/tests/probe/test_core.py b/polyphys/tests/probe/test_core.py index c377b1a2..806f7683 100644 --- a/polyphys/tests/probe/test_core.py +++ b/polyphys/tests/probe/test_core.py @@ -5,12 +5,12 @@ names. Expected values are the positions the fixture wrote and the attributes the parser sets. -The real-frame tests read the tier B and tier C files of the studies' -wholes (``polyphys/tests/probe/conftest.py``, "Real frames"). Their -expected values are the dump headers and coordinates as the package's own -LAMMPS readers, :func:`polyphys.simulate.dump.iter_dump` and -:func:`polyphys.simulate.data.read_data`, read them, and the particle -counts in the wholes' names. +The real-frame tests open the tier B files of the studies' wholes +(``polyphys/tests/probe/conftest.py``, "Real frames"). Their expected +values are the dump headers and coordinates as the package's own LAMMPS +reader, :func:`polyphys.simulate.dump.iter_dump`, reads them. The tests +that run a study's probe spec on these frames are in the polyphys-lab +repository, with the studies' parsers. """ import os import subprocess @@ -26,7 +26,6 @@ import polyphys from polyphys.manage.organizer import children_stamps from polyphys.tests.synthetic import ChainCyl -from polyphys.simulate.data import read_data from polyphys.simulate.dump import iter_dump pytest.importorskip("MDAnalysis") @@ -254,138 +253,3 @@ def test_make_universe_positions_are_the_dump_coordinates_from_the_origin( # float32 positions of at most about 165 carry an absolute round-off # of about 1e-5. assert_allclose(positions, expected, rtol=0, atol=2e-5) - - -def test_spec_selections_pick_the_species_of_a_real_polymer_dump( - real_polymer_whole, real_files, real_universe, study_spec): - """Each selection of the study's probe spec picks, in the polymer dump, - the atoms the whole's name gives its species.""" - probe = study_spec(real_polymer_whole.study).PROBE - files = real_files(real_polymer_whole) - universe = real_universe(files.topology, files.polymer) - - counts = {species: universe.select_atoms(selection).n_atoms - for species, selection in probe.selections.items()} - - assert counts == real_polymer_whole.polymer_counts() - - -def test_spec_selections_pick_the_species_of_a_real_window( - real_window_whole, real_files, real_universe, study_spec): - """Each selection of the study's probe spec picks, in the ``all`` - window, the atoms the whole's name gives its species, crowders - included.""" - probe = study_spec(real_window_whole.study).PROBE - files = real_files(real_window_whole) - universe = real_universe(files.data, files.window) - - counts = {species: universe.select_atoms(selection).n_atoms - for species, selection in probe.selections.items()} - - assert counts == real_window_whole.window_counts() - - -@pytest.mark.parametrize('study, whole, expected', [ - pytest.param('hns_cub', 'N200kbmm2nh30ac2l25epshc1nc10743ens1', - {'nmon': 200, 'nhns': 30, 'dcrowd': 2.0, 'lcube': 50.0, - 'eps_hc': 1.0, 'ncrowd': 10743, 'ensemble_id': 1}, - id='HnsCub'), - pytest.param('hns_cyl', 'N200kbmm2r4.5nh30ac2lz75epshc1nc386ens2', - {'nmon': 200, 'nhns': 30, 'dcrowd': 2.0, 'dcyl': 8.0, - 'lcyl': 150.0, 'ncrowd': 386, 'ensemble_id': 2}, - id='HnsCyl'), - pytest.param('trans_foci_cub', - 'al6nl5ml216ns400ac1nc112776l27dt0.005bdump2000adump5000' - 'ens1', - {'dmon_large': 6.0, 'nmon_large': 5, 'mmon_large': 216.0, - 'nmon_small': 400, 'ncrowd': 112776, 'lcube': 54.0}, - id='TransFociCub'), - pytest.param('trans_foci_cyl', - 'epss5epsl5r10.5al6nl5ml216ns400ac1nc0lz82.5dt0.005' - 'bdump2000adump5000ens2', - {'dmon_large': 6.0, 'nmon_small': 400, 'ncrowd': 0, - 'dcyl': 20.0, 'lcyl': 165.0, 'ensemble_id': 2}, - id='TransFociCyl'), -]) -def test_write_stamps_of_a_real_whole_carry_its_name( - study_spec, tmp_path, study, whole, expected): - """The stamps of a real whole hold the values its name encodes and the - 50 frames of its polymer dump.""" - spec = study_spec(study).SPEC - parser = spec.parser(f"{whole}.ring", 'whole', spec.group) - - stamps = pd.read_csv(write_stamps(parser, 50, tmp_path)) - - assert {key: stamps.loc[0, key] for key in expected} == expected - # A whole's lineage name never carries the topology suffix ``.ring`` - # (ParserBase._set_parents). - assert (stamps.loc[0, 'whole'], int(stamps.loc[0, 'n_frames'])) == ( - whole, 50) - - -@pytest.mark.private_data # pragma: no cover - needs data -def test_borrowed_topology_is_the_crowded_whole_own_polymer_topology( - real_crowded_whole, real_files, private_window_files): - """The polymer atoms of a crowded whole's own data file have the IDs, - types, molecule IDs, masses, and bonds of the data file its polymer - dump borrows.""" - _, own_path = private_window_files(real_crowded_whole) - borrowed = read_data(real_files(real_crowded_whole).topology) - own = read_data(own_path) - n_polymer = borrowed.n_atoms - in_polymer = np.all(own.bonds[:, 1:] <= n_polymer, axis=1) - - def as_set(bonds): - # Each run's write_data numbers its bonds in its own order, so the - # bonds are compared as a set: sorted by type and atoms. - return bonds[np.lexsort((bonds[:, 2], bonds[:, 1], bonds[:, 0]))] - - assert_array_equal(own.ids[:n_polymer], borrowed.ids) - assert_array_equal(own.types[:n_polymer], borrowed.types) - assert_array_equal(own.mol_ids[:n_polymer], borrowed.mol_ids) - assert_array_equal(own.masses[borrowed.types - 1], - borrowed.masses[borrowed.types - 1]) - assert_array_equal(as_set(own.bonds[in_polymer]), - as_set(borrowed.bonds)) - - -@pytest.mark.private_data # pragma: no cover - needs data -def test_write_stamps_of_a_real_whole_match_the_research_code( - real_transfocicub_whole, study_spec, research_probe_file, tmp_path): - """The stamps of a TransFociCub whole hold the research code's lineage - names under the documented mapping (Usage, "Lineage names"), the same - attribute values under the spec's renaming, every research attribute - but ``eps_others``, and the 50 frames the research probe counted.""" - whole = real_transfocicub_whole - spec = study_spec(whole.study).SPEC - parser = spec.parser(f"{whole.whole}.ring", 'whole', spec.group) - theirs = pd.read_csv(research_probe_file(whole, 'stamps', ext='csv')) - # The research names of the bulk fractions and densities and their - # PolyPhys names (projects/trans_foci_cub/spec.py, module docstring). - renamed = {'phi_c_bulk': 'phi_bulk_c', 'rho_c_bulk': 'rho_bulk_c', - 'phi_m_bulk': 'phi_bulk_m', 'rho_m_bulk': 'rho_bulk_m'} - names = ('lineage_name', 'whole', 'ensemble_long', 'ensemble', 'space') - - ours = pd.read_csv(write_stamps(parser, 50, tmp_path)) - - # A research whole name keeps ``.ring``; a short name parsed at its - # lineage and rebuilt gives the current form. - assert ours.loc[0, 'whole'] == theirs.loc[0, 'whole'].removesuffix( - '.ring') - assert ours.loc[0, 'ensemble_long'] == theirs.loc[0, 'ensemble_long'] - assert {lineage: ours.loc[0, lineage] for lineage in ('ensemble', 'space') - } == {lineage: getattr(spec.parser(theirs.loc[0, lineage], - lineage, spec.group), lineage) - for lineage in ('ensemble', 'space')} - attributes = [column for column in theirs.columns - if column not in names and column != 'n_frames'] - unmatched = [column for column in attributes - if renamed.get(column, column) not in ours.columns] - assert unmatched == ['eps_others'] - matched = [column for column in attributes if column not in unmatched] - # Both codes compute the fractions in float64 from the same integers. - assert_allclose( - ours.loc[0, [renamed.get(column, column) for column in matched]] - .to_numpy(dtype=float), - theirs.loc[0, matched].to_numpy(dtype=float), rtol=1e-12, atol=0) - assert int(theirs.loc[0, 'n_frames']) == int(ours.loc[0, 'n_frames']) == 50 diff --git a/polyphys/tests/probe/test_histograms.py b/polyphys/tests/probe/test_histograms.py index bfcb631c..9955d2b1 100644 --- a/polyphys/tests/probe/test_histograms.py +++ b/polyphys/tests/probe/test_histograms.py @@ -12,12 +12,9 @@ ``trans_foci_cub_all``, extended to the wall surface and the cube's corner). -The real-frame tests bin the tier B and tier C frames of the studies' -wholes (``polyphys/tests/probe/conftest.py``, "Real frames") with each -study's own probe spec and parser. Their expected values are conservation: -every position of a real frame falls inside the edges, so each frame's -counts sum to the species' particle number in the whole's name, and the run's -counts to the number of frames times it. +The tests that run a study's probe spec on the real frames of +``polyphys/tests/data/probe/`` are in the polyphys-lab repository, with +the studies' parsers. """ import numpy as np import pytest @@ -659,121 +656,3 @@ def test_save_before_run_raises(make_analysis, tmp_path): with pytest.raises(RuntimeError, match="Call run"): analysis.save(tmp_path, 'N6ens1-all') - - -# Real frames - - -@pytest.fixture -def real_histograms(study_spec, real_universe): - """Factory: :meth:`SpatialHistograms.from_spec` run on a real dump with - its topology, with the edges of the whole's parser in `group`.""" - - def _run(whole, topology, trajectory, group): - module = study_spec(whole.study) - parser = module.SPEC.parser(f"{whole.whole}.ring", 'whole', group) - universe = real_universe(topology, trajectory) - return SpatialHistograms.from_spec(universe, module.PROBE, - parser).run() - - return _run - - -@pytest.fixture -def directions(): - """The one-dimensional histograms of each geometry.""" - return {'cubic': ('r',), 'cylindrical': ('r', 'z', 'theta')} - - -def _counted(analysis, species, directions): - """Per species and direction: the distinct per-frame sums, and the sum - over the run.""" - return { - f"{d}{s}": (np.unique(analysis.results[f"{d}HistT{s}"].sum(axis=1)) - .tolist(), int(analysis.results[f"{d}Hist{s}"].sum())) - for s in species for d in directions} - - -def test_real_window_histograms_conserve_every_species( - real_window_whole, real_files, real_histograms, study_spec, - directions): - """Every position of a real ``all`` window falls inside the edges: each - frame counts every particle of each species once, and the run counts it - once per frame.""" - probe = study_spec(real_window_whole.study).PROBE - files = real_files(real_window_whole) - counts = real_window_whole.window_counts() - - analysis = real_histograms(real_window_whole, files.data, files.window, - 'all') - - assert _counted(analysis, probe.histogram_species, - directions[probe.geometry]) == { - f"{d}{s}": ([counts[s]], counts[s] * real_window_whole.n_window_frames) - for s in probe.histogram_species - for d in directions[probe.geometry]} - - -def test_real_polymer_histograms_conserve_every_species( - real_polymer_whole, real_files, real_histograms, study_spec, - directions): - """Every position of a real polymer dump falls inside the edges of its - whole: each frame counts every particle of each species once, and the - crowders, absent from the dump, not at all.""" - probe = study_spec(real_polymer_whole.study).PROBE - files = real_files(real_polymer_whole) - counts = real_polymer_whole.polymer_counts() - - analysis = real_histograms(real_polymer_whole, files.topology, - files.polymer, real_polymer_whole.group) - - assert _counted(analysis, probe.histogram_species, - directions[probe.geometry]) == { - f"{d}{s}": ([counts[s]], counts[s] * 50) - for s in probe.histogram_species - for d in directions[probe.geometry]} - - -def test_real_window_histograms_have_one_int64_row_per_frame_and_bin( - real_window_whole, real_files, real_histograms, study_spec, - directions): - """Each per-frame histogram of a real window is int64 with one row per - frame and one column per bin of the whole's edges.""" - module = study_spec(real_window_whole.study) - probe = module.PROBE - files = real_files(real_window_whole) - edges = histogram_edges(probe, module.SPEC.parser( - f"{real_window_whole.whole}.ring", 'whole', 'all')) - - analysis = real_histograms(real_window_whole, files.data, files.window, - 'all') - - assert {f"{d}{s}": (analysis.results[f"{d}HistT{s}"].dtype, - analysis.results[f"{d}HistT{s}"].shape) - for s in probe.histogram_species - for d in directions[probe.geometry]} == { - f"{d}{s}": (np.dtype(np.int64), - (real_window_whole.n_window_frames, edges[d].size - 1)) - for s in probe.histogram_species - for d in directions[probe.geometry]} - - -@pytest.mark.private_data # pragma: no cover - needs data -def test_crowded_window_histograms_conserve_every_species( - real_crowded_whole, private_window_files, real_histograms, study_spec, - directions): - """Every position of a crowded ``all`` window, crowders at the wall and - in the corners included, falls inside the edges: each frame counts every - particle of each species once.""" - probe = study_spec(real_crowded_whole.study).PROBE - window, data = private_window_files(real_crowded_whole) - counts = real_crowded_whole.window_counts() - - analysis = real_histograms(real_crowded_whole, data, window, 'all') - - assert _counted(analysis, probe.histogram_species, - directions[probe.geometry]) == { - f"{d}{s}": ([counts[s]], - counts[s] * real_crowded_whole.n_window_frames) - for s in probe.histogram_species - for d in directions[probe.geometry]} diff --git a/polyphys/tests/probe/test_pairs.py b/polyphys/tests/probe/test_pairs.py index 3f0ef29a..12f970f5 100644 --- a/polyphys/tests/probe/test_pairs.py +++ b/polyphys/tests/probe/test_pairs.py @@ -8,12 +8,9 @@ boundary by hand, and the backbone indices of the foci from their places in the chain. -The real-frame tests run the TransFoci studies' probe specs on the tier B -polymer dumps of their wholes (``polyphys/tests/probe/conftest.py``, "Real -frames"). Their expected values are the foci's places along the ring in the -data file, read with :func:`polyphys.simulate.data.read_data`; the -minimum-image distances of :func:`MDAnalysis.lib.distances.distance_array`; -and the conservation of the foci among the bonds and the clusters. +The tests that run a study's probe spec on the real frames of +``polyphys/tests/data/probe/`` are in the polyphys-lab repository, with +the studies' parsers. """ import numpy as np import pytest @@ -25,7 +22,6 @@ from polyphys.tests.synthetic import RingCyl from polyphys.spec import ProbeSpec, ProjectSpec -from polyphys.simulate.data import read_data pytest.importorskip("MDAnalysis") @@ -381,95 +377,3 @@ def test_save_before_run_raises(make_analysis, tmp_path): with pytest.raises(RuntimeError, match="Call run"): analysis.save(tmp_path, 'ns7nl3ens1-bug') - - -# Real frames - - -@pytest.fixture -def real_pairs(real_files, real_universe, study_spec): - """Factory: the study's :meth:`PairDistances.from_spec` run on a real - TransFoci polymer dump, with the universe it read.""" - - def _run(whole): - module = study_spec(whole.study) - files = real_files(whole) - universe = real_universe(files.topology, files.polymer) - parser = module.SPEC.parser(f"{whole.whole}.ring", 'whole', - whole.group) - return (PairDistances.from_spec(universe, module.PROBE, - parser).run(), universe) - - return _run - - -def test_real_foci_diagonal_holds_their_places_along_the_ring( - real_foci_whole, real_files, real_pairs): - """Every frame's diagonal holds the 0-based places of the large - monomers along the ring, as the data file orders its atoms.""" - data = read_data(real_files(real_foci_whole).topology) - expected = np.flatnonzero(data.types[data.mol_ids == 1] == 2) - - analysis, _ = real_pairs(real_foci_whole) - - assert_array_equal( - np.diagonal(analysis.results['distMatTFoci'], axis1=1, axis2=2), - np.tile(expected, (50, 1))) - - -def test_real_foci_distances_are_the_minimum_image_distances( - real_foci_whole, real_pairs, study_spec, trajectory_arrays, - reference_distances): - """Above the diagonal, every frame holds the foci's minimum-image - distances along the geometry's periodic directions.""" - probe = study_spec(real_foci_whole.study).PROBE - - analysis, universe = real_pairs(real_foci_whole) - - arrays = trajectory_arrays(universe) - foci = arrays['positions'][:, analysis.atomgroup.indices] - expected = reference_distances(foci, foci, arrays['boxes'], - probe.geometry) - upper = np.triu_indices(analysis.atomgroup.n_atoms, k=1) - # float32 positions of at most about 165 carry a round-off of about - # 1e-5 in a separation. - assert_allclose(analysis.results['distMatTFoci'][:, upper[0], upper[1]], - expected[:, upper[0], upper[1]], rtol=0, atol=5e-5) - - -def test_real_foci_bonds_and_clusters_conserve_the_foci(real_foci_whole, - real_pairs): - """In every frame, the bonds histogram counts each large monomer once - and the clusters histogram, weighted by cluster size, does too.""" - analysis, _ = real_pairs(real_foci_whole) - clusters = analysis.results['clustersHistTFoci'] - sizes = np.arange(clusters.shape[1]) - - assert_array_equal(analysis.results['bondsHistTFoci'].sum(axis=1), - np.full(50, real_foci_whole.n_foci)) - assert_array_equal(clusters @ sizes, np.full(50, real_foci_whole.n_foci)) - - -# Against the research code's probe (tier C) - - -@pytest.mark.private_data # pragma: no cover - needs data -@pytest.mark.parametrize('histogram, sizes', [ - pytest.param('bondsHistTFoci', slice(None), id='bonds'), - pytest.param('clustersHistTFoci', slice(1, None), id='clusters'), -]) -def test_real_foci_histograms_match_the_research_code( - real_transfocicub_whole, real_pairs, research_probe_file, histogram, - sizes): - """On the 50 frames of a TransFociCub polymer dump, the foci bonds - histogram and the clusters histogram from size 1 up equal the research - code's probe of the same frames (``trans_foci_cub_bug``), whose - contacts took no minimum image: the ring is whole in these dumps, so - the image changes no contact. The research clusters histogram counted - the zero eigenvalues of the contact matrix in its size-0 column, which - the analysis table drops; this probe writes no cluster of size 0.""" - analysis, _ = real_pairs(real_transfocicub_whole) - expected = np.load(research_probe_file(real_transfocicub_whole, histogram)) - - assert_array_equal(analysis.results[histogram][:, sizes], - expected[:, sizes]) diff --git a/polyphys/tests/strategies.py b/polyphys/tests/strategies.py index 8c8b0730..6c865639 100644 --- a/polyphys/tests/strategies.py +++ b/polyphys/tests/strategies.py @@ -4,7 +4,7 @@ ========================================================== Hypothesis strategies shared by the package's tests and the study tests in -``projects/``. +the polyphys-lab repository. :func:`lineage_names` draws an artifact name of one lineage of a parser class from the parser's own declaration: one value per keyword of the diff --git a/polyphys/tests/test_docs.py b/polyphys/tests/test_docs.py index 92c63e6d..23a21fca 100644 --- a/polyphys/tests/test_docs.py +++ b/polyphys/tests/test_docs.py @@ -1,10 +1,10 @@ """ Tests for the examples of the documentation pages in ``docs/source/``. -pytest collects doctests from ``polyphys``, ``README.md``, and ``projects`` -only, so the ``>>>`` examples of a documentation page run here, one test -per page, through :func:`doctest.testfile`. The pages compute their expected -values by hand in the text beside each example. +pytest collects doctests from ``polyphys`` and ``README.md`` only, so the +``>>>`` examples of a documentation page run here, one test per page, +through :func:`doctest.testfile`. The pages compute their expected values +by hand in the text beside each example. """ import doctest import os diff --git a/polyphys/tests/viz/test_helpers.py b/polyphys/tests/viz/test_helpers.py index 8e79745d..bb9b6b0c 100644 --- a/polyphys/tests/viz/test_helpers.py +++ b/polyphys/tests/viz/test_helpers.py @@ -7,15 +7,11 @@ is the input the handle or legend was built from: the labels, colors, styles, and titles given, read back from the artists. """ -import importlib -import sys -from pathlib import Path import pytest pytest.importorskip("matplotlib") -import matplotlib # noqa: E402 import numpy as np # noqa: E402 from matplotlib.colors import to_rgba # noqa: E402 from matplotlib.lines import Line2D # noqa: E402 @@ -243,50 +239,3 @@ def test_band_under_lines_rejects_a_band_that_does_not_fit(ax, bands, with pytest.raises(ValueError, match=message): band_under_lines(ax, bands) - - -# On the research code's space table (tier C) - - -def _plot_state_points(ax, table, colors): # pragma: no cover - tier C - """One line per state point of a space table, ``gyrTMon-mean`` against - ``time``, in the given colors; returns the labels, the rounded crowder - volume fractions.""" - labels = [] - for (phi, rows), color in zip(table.groupby("phi_bulk_c_round"), colors): - ax.plot(rows["time"], rows["gyrTMon-mean"], color=color) - labels.append(f"{phi:g}") - return labels - - -@pytest.mark.private_data -def test_helpers_draw_the_research_space_table( # pragma: no cover - ax, private_data_dir): - """A figure of the research code's TransFociCub gyration space table has - one line per state point, one legend handle per line in its color from - ``color_handles``, and x ticks from ``set_xticks`` whose first and last - frame the time axis. A property check: there is no research figure to - compare with.""" - from polyphys.analyze.pipeline import space_tseries_from_spec - - sys.path.insert(0, str(Path(__file__).resolve().parents[3])) - spec = importlib.import_module("projects.trans_foci_cub.spec").SPEC - parent = private_data_dir / "TransFociCub" / "analysis" - folders = sorted(parent.glob("*-bug-ensAvg")) - if len(folders) != 1: - pytest.skip(f"no single *-bug-ensAvg folder under {parent}") - table = space_tseries_from_spec(folders[0], spec, "gyrTMon") - n_states = table["ensemble_long"].nunique() - colors = [tuple(color) for color in matplotlib.colormaps["viridis"]( - np.linspace(0.0, 1.0, n_states))] - t_max = float(table["time"].max()) - - labels = _plot_state_points(ax, table, colors) - handles = color_handles(labels, colors) - ticks = set_xticks(ax, 0.0, t_max, t_max / 5) - - assert len(ax.lines) == n_states == len(handles) - assert [to_rgba(handle.get_facecolor()) for handle in handles] == [ - to_rgba(line.get_color()) for line in ax.lines] - assert ticks[0] <= table["time"].min() and ticks[-1] >= t_max - assert ax.get_xlim()[0] <= 0.0 and ax.get_xlim()[1] >= t_max diff --git a/projects/README.md b/projects/README.md deleted file mode 100644 index c614bbf0..00000000 --- a/projects/README.md +++ /dev/null @@ -1,49 +0,0 @@ -# Study specs - -This folder holds the parser and the specs of each of the maintainer's -published studies, for analysis scripts run from a checkout of this -repository. It is part of the repository, not of the installed package -(`pyproject.toml` excludes `projects*`), so PolyPhys holds no table of studies. -The [Studies](../docs/source/studies.md) page shows how to declare a study's -parser and specs; a study elsewhere builds its own in the same way. - -| Folder | Study | -| ----------------- | -------------- | -| `trans_foci_cyl/` | `TransFociCyl` | -| `trans_foci_cub/` | `TransFociCub` | -| `hns_cub/` | `HnsCub` | -| `hns_cyl/` | `HnsCyl` | - -The parsers and specs of unpublished studies are kept out of this public -repository. - -Each folder is a package whose `parser.py` declares the study's `ParserBase` -subclass, whose `spec.py` builds its `ProjectSpec` (`SPEC`), `ProbeSpec` -(`PROBE`), and `AnalysisTable` records (`ANALYSIS`), and whose `__init__.py` -exports the parser class and the three specs; the study's drivers and figure -code will join them there. The `ProjectSpec` holds the facts the study's -analysis needs beyond the parser class, such as the polymer group, the -rounding step of the bulk crowder volume fraction, the attributes added as -columns, the species-to-size map of the density profiles, and the time -series. `ProjectSpec` itself is in `polyphys.spec`. - -The recipe below stacks a study's time series into a `space` table. It is not -self-contained: it needs the `ensAvg` files of an existing sweep of the study. -`database` is the `ensAvg` directory of one space, such as -`analysis/-nucleoid-ensAvg`, which holds the files -`---ensAvg.csv`. The -[Quickstart](../docs/source/quickstart.md) builds a `space` table from a -synthetic sweep, and its examples run as tests. - -```python -import sys -sys.path.insert(0, "/projects") -from hns_cub import SPEC -from polyphys.analyze.pipeline import space_tseries_from_spec - -database = "/analysis/-nucleoid-ensAvg" -space = space_tseries_from_spec(database, SPEC, "gyrTMon") -``` - -`test_studies.py` checks every spec against its parser and against the values -of the research code's registry; CI runs it with flake8 and strict mypy. diff --git a/projects/conftest.py b/projects/conftest.py deleted file mode 100644 index 4a317604..00000000 --- a/projects/conftest.py +++ /dev/null @@ -1,10 +0,0 @@ -""" -Make the study modules in this folder importable by their names in tests. - -pytest runs with ``--import-mode=importlib``, which does not put a test's -folder on :data:`sys.path`; this folder is not an installed package. -""" -import sys -from pathlib import Path - -sys.path.insert(0, str(Path(__file__).resolve().parent)) diff --git a/projects/hns_cub/__init__.py b/projects/hns_cub/__init__.py deleted file mode 100644 index 51f120b2..00000000 --- a/projects/hns_cub/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -""" -The *HnsCub* study: ``SPEC`` is its -:class:`~polyphys.spec.ProjectSpec`, ``PROBE`` its -:class:`~polyphys.spec.ProbeSpec`, and ``ANALYSIS`` its -:class:`~polyphys.spec.AnalysisTable` per particle group, and -``HnsCub`` its parser. -""" -from .parser import HnsCub -from .spec import ANALYSIS, PROBE, SPEC - -__all__ = ["ANALYSIS", "PROBE", "SPEC", "HnsCub"] diff --git a/projects/hns_cub/parser.py b/projects/hns_cub/parser.py deleted file mode 100644 index 843d543c..00000000 --- a/projects/hns_cub/parser.py +++ /dev/null @@ -1,145 +0,0 @@ -""" -The parser of the *HnsCub* study: a ring polymer with H-NS proteins among -crowders in a cube. - -.. versionchanged:: 1.0.0 - Moved here from :mod:`polyphys.manage.parser`, which ships no study. -""" -from polyphys.manage.parser import KeywordSpec, ParserBase, Species - -__all__ = ["HnsCub"] - - -class HnsCub(ParserBase): - """ - Parse an artifact name of the *HnsCub* project: a ring polymer with H-NS - proteins among crowders in a periodic cube. - - Each lineage has its own name pattern; ``#`` stands for a value: - - - ``segment``: N#kbmm#nh#ac#l#epshc#nc#ens#.j#.ring - - ``whole``: N#kbmm#nh#ac#l#epshc#nc#ens#.ring - - ``ensemble_long``: N#kbmm#nh#ac#l#epshc#nc# - - ``ensemble``: N#kbmm#nh#ac#epshc#nc# - - ``space``: N#kbmm#nh#ac#epshc# - - Parameters - ---------- - artifact : Pathish - Name to be parsed, or a file path with ``ispath=True``. - lineage : {'segment', 'whole', 'ensemble_long', 'ensemble', 'space'} - Lineage of the artifact. - group : {'nucleoid', 'all'} - Particle group: ``'nucleoid'`` or ``'all'`` for every particle. - ispath : bool, default False - Whether `artifact` is a file path. - - Attributes - ---------- - dmon : int - Monomer diameter, fixed at 1; not in the name. - nmon : int - Number of monomers; keyword ``N``. - dhns : int - Diameter of an H-NS protein, fixed at 1; not in the name. - dhns_patch : float - Diameter of the patch at each pole of an H-NS protein, fixed at - 0.178; not in the name. - nhns : int - Number of H-NS proteins; keyword ``nh``. - dcrowd : float - Crowder diameter; keyword ``ac``. - ncrowd : int - Number of crowders; keyword ``nc``. - bend_mm : float - Bending rigidity of the DNA monomers; keyword ``kbmm``. - eps_hm : int - Strength of the attractive LJ interaction between the H-NS poles and - the monomers, fixed at 29; not in the name. - eps_hc : float - Value of the keyword ``epshc``, named for the strength of the LJ - interaction between the H-NS cores and the crowders. The project's - LAMMPS inputs do not read it: they fix that strength at 1 with a - purely repulsive cutoff. - lcube : float - Side of the cube, :math:`L = 2 l`, from the half-side :math:`l` - (keyword ``l``). - dt : float - Simulation time step, fixed at 0.005; not in the name. - ndump : int - Dump period of the ``nucleoid`` trajectory, fixed at 5000 time steps; - not in the name. - adump : int - Dump period of the ``all`` trajectory, fixed at 10000 time steps; not - in the name. - ensemble_id : int - Index of the whole in its ensemble; keyword ``ens``. - segment_id : int - Index of the segment in its whole, zero-padded in the name; keyword - ``j``. - rho_bulk_m, phi_bulk_m : float - Bulk number density and volume fraction of monomers. - rho_bulk_hns, phi_bulk_hns : float - Bulk number density and volume fraction of H-NS proteins. - rho_bulk_c, phi_bulk_c : float - Bulk number density and volume fraction of crowders. - - Notes - ----- - The bulk values exist only at ``segment``, ``whole``, and - ``ensemble_long``. The lineage names are documented in - :class:`~polyphys.manage.parser.ParserBase`. - - The simulation design values that the name does not carry, such as - the interaction strengths, the bonds, and the thermostat, are listed - in :doc:`/design`. - - Examples - -------- - >>> artifact = HnsCub( - ... 'N200kbmm2.0nh8ac1.0l25.0epshc1.0nc0ens1.ring', - ... 'whole', - ... 'nucleoid' - ... ) - >>> print(artifact.nhns) - 8 - """ - _geometry = 'cubic' - _topology = 'ring' - _groups = ['nucleoid', 'all'] - _dump_keywords = {'nucleoid': 'ndump', 'all': 'adump'} - _keywords = { - 'N': KeywordSpec('nmon', int), - 'kbmm': KeywordSpec('bend_mm', float), - 'nh': KeywordSpec('nhns', int), - 'ac': KeywordSpec('dcrowd', float), - 'l': KeywordSpec('lcube', float, scale=2.0), - 'epshc': KeywordSpec('eps_hc', float), - 'nc': KeywordSpec('ncrowd', int), - 'ens': KeywordSpec('ensemble_id', int), - 'j': KeywordSpec('segment_id', int), - } - _patterns = { - 'segment': ('N', 'kbmm', 'nh', 'ac', 'l', 'epshc', 'nc', 'ens', 'j'), - 'whole': ('N', 'kbmm', 'nh', 'ac', 'l', 'epshc', 'nc', 'ens'), - 'ensemble_long': ('N', 'kbmm', 'nh', 'ac', 'l', 'epshc', 'nc'), - 'ensemble': ('N', 'kbmm', 'nh', 'ac', 'epshc', 'nc'), - 'space': ('N', 'kbmm', 'nh', 'ac', 'epshc'), - } - _defaults = {'dmon': 1, 'dhns': 1, 'dhns_patch': 0.178, 'dt': 0.005, - 'ndump': 5000, 'adump': 10000, 'eps_hm': 29} - _species = (Species('m', 'nmon', 'dmon'), Species('hns', 'nhns', 'dhns'), - Species('c', 'ncrowd', 'dcrowd')) - _project_attributes = { - 'segment': ['dmon', 'dhns', 'phi_bulk_m', 'rho_bulk_m', - 'phi_bulk_c', 'rho_bulk_c', 'phi_bulk_hns', - 'rho_bulk_hns', 'dt', 'ndump', 'adump', 'eps_hm'], - 'whole': ['dmon', 'dhns', 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', - 'rho_bulk_c', 'phi_bulk_hns', 'rho_bulk_hns', 'dt', 'ndump', - 'adump', 'eps_hm'], - 'ensemble_long': ['dmon', 'dhns', 'phi_bulk_m', 'rho_bulk_m', - 'phi_bulk_c', 'rho_bulk_c', 'phi_bulk_hns', - 'rho_bulk_hns', 'dt', 'ndump', 'adump', 'eps_hm'], - 'ensemble': ['dmon', 'dhns', 'dt', 'ndump', 'adump', 'eps_hm'], - 'space': ['dmon', 'dhns', 'dt', 'ndump', 'adump', 'eps_hm'] - } diff --git a/projects/hns_cub/spec.py b/projects/hns_cub/spec.py deleted file mode 100644 index 317ee330..00000000 --- a/projects/hns_cub/spec.py +++ /dev/null @@ -1,155 +0,0 @@ -""" -The *HnsCub* study: a ring polymer with H-NS proteins among crowders in a -periodic cube. ``SPEC`` is its -:class:`~polyphys.spec.ProjectSpec`, ``PROBE`` its -:class:`~polyphys.spec.ProbeSpec`, and ``ANALYSIS`` its -:class:`~polyphys.spec.AnalysisTable` per particle group. - -Values from the research code's registry, with the PolyPhys attribute names -``phi_bulk_c`` for ``phi_c_bulk`` and ``rho_bulk_hns`` for ``rho_hns_bulk``, -and from its analysis tables, with each artifact listed once. -""" -from polyphys.analyze.pipeline import ( - Autocorrelation, - LocalDensities, - TimeAverage, -) -from polyphys.manage.parser import ParserBase -from polyphys.spec import ( - AnalysisTable, - BinderSpec, - ProbeSpec, - ProjectSpec, - edge, - histogram, - histogram_2d, - matrix_tseries, - tseries, -) - -from .parser import HnsCub - -SPEC = ProjectSpec( - parser=HnsCub, - group='nucleoid', - divisor=0.01, - crowder_attr='phi_bulk_c', - physical_attrs=('space', 'ensemble_long', 'ensemble', 'bend_mm', - 'eps_hc', 'nmon', 'nhns', 'dcrowd', 'phi_bulk_c', - 'rho_bulk_hns'), - species_sizes={'Mon': 'dmon', 'Crd': 'dcrowd', 'Hns': 'dhns'}, - tseries_properties=('asphericityTMon', 'gyrTMon', 'shapeTMon', - 'bondLengthVecMon', 'nBoundTHnsPatch', - 'nFreeTHnsPatch', 'nEngagedTHnsPatch', - 'nFreeTHnsCore', 'nBridgeTHnsCore', - 'nDangleTHnsCore', 'nCisTHnsCore', 'nTransTHnsCore'), -) - - -def _r_bin(info: ParserBase) -> float: - """A tenth of the smallest of the monomer, H-NS core, and crowder - diameters: the bin size along r (the research code's - ``hns_cub_all``, ``bin_edges``).""" - return 0.1 * min(float(getattr(info, 'dmon')), - float(getattr(info, 'dhns')), - float(getattr(info, 'dcrowd'))) - - -def _l_bin(info: ParserBase) -> float: - """Half the smaller of the monomer and crowder diameters: the bin size - along x, y, and z (the research code's ``hns_cub_all``, - ``bin_edges``, ``lEdge``).""" - return 0.5 * min(float(getattr(info, 'dmon')), - float(getattr(info, 'dcrowd'))) - - -# The probe of the polymer group: the selections, the chain-size observables -# of its per-frame loop, and the unwrap convention of its asphericity (the -# research code's ``hns_cub_nucleoid``); the histograms of the all group -# (``hns_cub_all``): its selections, bin sizes, and species, the H-NS cores -# as 'Hns', with the block length and the recentering of the probe guide -# ("The spatial histograms" and "Recentering a frame"); and the binding of -# the H-NS patches (``hns_cub_nucleoid``, ``cis_threshold = 4`` and the -# distance matrices saved as ``distMatTMonHnsPatch``). The cutoff is the -# published binding criterion, r <= r_cut,mp = 0.685 (thesis, Section 5.3 -# and Table 5.1), which the maintainer chose on 2026-11-04 over the research -# probe's ``round(2**(1/6) * (dmon + dhns_patch) / 2, 3) = 0.661`` with a -# strict comparison; the probe guide, "Choosing the cutoff", gives both. -# The cis threshold is 3, the maintainer's definition (thesis, chapter 5: a -# bridge with at most two monomers between its sites, at most 3 bonds, is -# cis), which the maintainer chose on 2026-11-08 over the research probe's 4; -# the probe guide, "Choosing the cis threshold", gives both and why. -PROBE = ProbeSpec( - project=SPEC, - selections={'Mon': 'resid 1', 'HnsPatch': 'type 2', 'Crd': 'resid 0', - 'Hns': 'type 3'}, - chain='Mon', - observables=('asphericityT', 'gyrT', 'shapeT', 'principalT'), - unwrap=True, - bin_sizes={'r': _r_bin, 'x': _l_bin, 'y': _l_bin, 'z': _l_bin}, - histogram_species=('Crd', 'Mon', 'Hns'), - block_frames=100, - recenter='half', - binders=BinderSpec(patches='HnsPatch', binders='HnsCore', cutoff=0.685, - inclusive=True, cis_threshold=3, - store_distances=True), -) - -# The largest lag of the autocorrelation functions, in frames (the research -# code's ``analyze_measures``, ``nlags``). -NLAGS = 7000 - -ANALYSIS = ( - AnalysisTable(group='nucleoid', lineage='whole', stamps=True, properties=( - matrix_tseries('principalT', 'Mon'), - # The per-frame bond and cluster-size histograms of the H-NS cores, - # averaged over the frames; a cluster histogram's column 0 is the - # count of empty clusters. - matrix_tseries('bondsHistT', 'HnsCore', - post_hook=TimeAverage('bondsHist')), - matrix_tseries('bondsHistDangleT', 'HnsCore', - post_hook=TimeAverage('bondsHistDangle')), - matrix_tseries('bondsHistBridgeT', 'HnsCore', - post_hook=TimeAverage('bondsHistBridge')), - matrix_tseries('bondsHistDirDepT', 'HnsCore', - post_hook=TimeAverage('bondsHistDirDep')), - matrix_tseries('clustersHistT', 'HnsCore', - post_hook=TimeAverage('clustersHist', - drop_first_column=True)), - matrix_tseries('clustersHistDangleT', 'HnsCore', - post_hook=TimeAverage('clustersHistDangle', - drop_first_column=True)), - matrix_tseries('clustersHistBridgeT', 'HnsCore', - post_hook=TimeAverage('clustersHistBridge', - drop_first_column=True)), - matrix_tseries('clustersHistDirDepT', 'HnsCore', - post_hook=TimeAverage('clustersHistDirDep', - drop_first_column=True)), - # Per-whole vectors reduced as time series. - tseries('loopLengthHist', 'Mon'), - tseries('bondCosineCorrVec', 'Mon'), - tseries('bondLengthVec', 'Mon'), - tseries('nBoundT', 'HnsPatch'), - tseries('nFreeT', 'HnsPatch'), - tseries('nEngagedT', 'HnsPatch'), - tseries('nFreeT', 'HnsCore'), - tseries('nBridgeT', 'HnsCore'), - tseries('nDangleT', 'HnsCore'), - tseries('nCisT', 'HnsCore'), - tseries('nTransT', 'HnsCore'), - tseries('gyrT', 'Mon', post_hook=Autocorrelation(NLAGS)), - tseries('shapeT', 'Mon', post_hook=Autocorrelation(NLAGS)), - tseries('asphericityT', 'Mon', post_hook=Autocorrelation(NLAGS)), - )), - AnalysisTable(group='all', lineage='segment', stamps=False, properties=( - histogram('r', 'Crd', post_hook=LocalDensities()), - histogram('r', 'Mon', post_hook=LocalDensities()), - histogram('r', 'Hns', post_hook=LocalDensities()), - *(histogram_2d(plane, species) - for plane in ('xy', 'xz', 'yz') - for species in ('Crd', 'Mon', 'Hns')), - edge('x'), - edge('y'), - edge('z'), - )), -) diff --git a/projects/hns_cub/test_parser.py b/projects/hns_cub/test_parser.py deleted file mode 100644 index 8a5d4a4d..00000000 --- a/projects/hns_cub/test_parser.py +++ /dev/null @@ -1,788 +0,0 @@ -"""Tests for the *HnsCub* parser, :mod:`hns_cub.parser`.""" -import os -import re -from collections import OrderedDict -from collections.abc import Callable -from operator import attrgetter -from pathlib import Path -from typing import Any, NamedTuple, cast - -import pandas as pd -import pytest -from hypothesis import given, settings -from hypothesis import strategies as st - -from hns_cub.parser import HnsCub -from polyphys.manage.parser import ParserBase -from polyphys.manage.types import LineageT -from polyphys.tests.strategies import lineage_names, stored_values -from polyphys.manage.utils import number_density_cube, volume_fraction_cube - -DATA_DIR_ENV = 'POLYPHYS_DATA_DIR' - - -class ParseCase(NamedTuple): - """A hand-written artifact name and what its parser must give.""" - parser_class: type[ParserBase] - lineage: Any - group: Any - artifact: str - name: str - values: dict[str, Any] - physical: list[str] - - -class ValidCase(NamedTuple): - """A valid ``ensemble_long`` artifact name for one parser class.""" - parser_class: type[ParserBase] - artifact: str - lineage: Any - group: Any - - -@pytest.fixture(params=[ - pytest.param( - ParseCase( - HnsCub, 'segment', 'nucleoid', - artifact='N200kbmm2nh12ac2l25epshc1nc11937ens2.j5.ring.nucleoid', - name='N200kbmm2nh12ac2l25epshc1nc11937ens2.j5.ring', - values=dict(nmon=200, bend_mm=2.0, nhns=12, dcrowd=2.0, lcube=50, - eps_hc=1.0, ncrowd=11937, ensemble_id=2, segment_id=5), - physical=['dmon', 'dhns', 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', - 'rho_bulk_c', 'phi_bulk_hns', 'rho_bulk_hns', 'dt', - 'ndump', 'adump', 'eps_hm']), - id='HnsCub-segment'), - pytest.param( - ParseCase( - HnsCub, 'whole', 'nucleoid', - artifact='N200kbmm2nh12ac2l25epshc1nc11937ens2.ring.nucleoid', - name='N200kbmm2nh12ac2l25epshc1nc11937ens2.ring', - values=dict(nmon=200, bend_mm=2.0, nhns=12, dcrowd=2.0, lcube=50, - eps_hc=1.0, ncrowd=11937, ensemble_id=2), - physical=['dmon', 'dhns', 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', - 'rho_bulk_c', 'phi_bulk_hns', 'rho_bulk_hns', 'dt', - 'ndump', 'adump', 'eps_hm']), - id='HnsCub-whole'), - pytest.param( - ParseCase( - HnsCub, 'ensemble_long', 'nucleoid', - artifact='N200kbmm2nh12ac2l25epshc1nc11937', - name='N200kbmm2nh12ac2l25epshc1nc11937', - values=dict(nmon=200, bend_mm=2.0, nhns=12, dcrowd=2.0, lcube=50, - eps_hc=1.0, ncrowd=11937), - physical=['dmon', 'dhns', 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', - 'rho_bulk_c', 'phi_bulk_hns', 'rho_bulk_hns', 'dt', - 'ndump', 'adump', 'eps_hm']), - id='HnsCub-ensemble_long'), - pytest.param( - ParseCase( - HnsCub, 'ensemble', 'nucleoid', - artifact='N200kbmm2nh12ac2.0epshc1nc11937', - name='N200kbmm2nh12ac2.0epshc1nc11937', - values=dict(nmon=200, bend_mm=2.0, nhns=12, dcrowd=2.0, eps_hc=1.0, - ncrowd=11937), - physical=['dmon', 'dhns', 'dt', 'ndump', 'adump', 'eps_hm']), - id='HnsCub-ensemble'), - pytest.param( - ParseCase( - HnsCub, 'space', 'nucleoid', - artifact='N200kbmm2nh12ac2.0epshc1', - name='N200kbmm2nh12ac2.0epshc1', - values=dict(nmon=200, bend_mm=2.0, nhns=12, dcrowd=2.0, - eps_hc=1.0), - physical=['dmon', 'dhns', 'dt', 'ndump', 'adump', 'eps_hm']), - id='HnsCub-space'), -]) -def parse_case(request: pytest.FixtureRequest) -> ParseCase: - """ - One hand-written name per lineage, with the values the parser must give: - the digits in the name, converted as the class Notes state - (``lcube = 2 l``). - """ - case: ParseCase = request.param - return case - - -@pytest.fixture -def parsed(parse_case: ParseCase) -> ParserBase: - """The parser built from the name of `parse_case`.""" - return parse_case.parser_class(parse_case.artifact, parse_case.lineage, - parse_case.group) - - -@pytest.fixture(params=[ - pytest.param( - ValidCase(HnsCub, 'N200kbmm2nh12ac2l25epshc1nc11937', - 'ensemble_long', 'nucleoid'), - id='HnsCub'), -]) -def valid_case(request: pytest.FixtureRequest) -> ValidCase: - """A valid ``ensemble_long`` name of the parser class.""" - case: ValidCase = request.param - return case - - -@pytest.fixture(params=[ - pytest.param((parser_class, lineage), - id=f"{parser_class.__name__}-{lineage}") - for parser_class in (HnsCub,) - for lineage in ('segment', 'whole', 'ensemble_long', 'ensemble', 'space') -]) -def declared_case( - request: pytest.FixtureRequest -) -> tuple[type[ParserBase], Any, str, dict[str, Any]]: - """ - A name built from the class's declared pattern for one lineage, with 10.5 - as the value of every float keyword and 2 as the value of every integer - keyword, and the values the parser must store for them. With these - values every volume fraction stays below 1. - """ - parser_class, lineage = request.param - specs = [(keyword, parser_class._keywords[keyword]) - for keyword in parser_class._patterns[lineage]] - name = ''.join(f"{keyword}{'10.5' if spec.kind is float else '2'}" - for keyword, spec in specs) - expected = {spec.attr: (spec.scale * 10.5 + spec.offset - if spec.kind is float else 2) - for _, spec in specs} - return parser_class, lineage, name, expected - - -@pytest.fixture -def make_temp_file(tmp_path: Path) -> Callable[[str], str]: - """ - Create an empty temporary file with a given filename and return its full - path. Useful for testing parsers that expect a file path as input. - """ - - def _make_temp_file(filename: str) -> str: - f = tmp_path / filename - f.touch() - return str(f) - - return _make_temp_file - - -@pytest.fixture -def private_data_dir() -> Path: # pragma: no cover - needs data - """ - Return the directory named by ``POLYPHYS_DATA_DIR``, the maintainer's - local full-size data; skip the test when the variable is unset or - empty, and fail it when the variable names something that is not a - directory. - """ - data_dir = os.environ.get(DATA_DIR_ENV, '') - if data_dir == '': - pytest.skip(f"{DATA_DIR_ENV} is not set") - if not Path(data_dir).is_dir(): - pytest.fail(f"{DATA_DIR_ENV}={data_dir!r} is not a directory", - pytrace=False) - return Path(data_dir) - - -# Parsing hand-written names - - -def test_parser_sets_values_from_name( - parse_case: ParseCase, parsed: ParserBase) -> None: - """Each lineage's name parses to its digits, converted as documented.""" - actual = attrgetter(*parse_case.values)(parsed) - # Values are parsed from decimal text; equal to float64 round-off. - assert actual == pytest.approx(tuple(parse_case.values.values()), - rel=1e-12, abs=1e-12) - - -def test_parser_reduces_artifact_to_lineage_name( - parse_case: ParseCase, parsed: ParserBase) -> None: - """The name drops the group and topology suffixes after the lineage.""" - assert parsed.name == parse_case.name - - -def test_parser_lists_physical_attributes( - parse_case: ParseCase, parsed: ParserBase) -> None: - """Each lineage lists the fixed and computed attributes it sets.""" - assert parsed.physical_attributes == parse_case.physical - - -def test_parser_names_its_project( - parse_case: ParseCase, parsed: ParserBase) -> None: - """The project name is the parser class's name.""" - assert parsed.project_name == parse_case.parser_class.__name__ - - -def test_parser_without_path_reports_no_path( - parse_case: ParseCase, parsed: ParserBase) -> None: - """A parser built from a name keeps it and has no filepath or ext.""" - assert (parsed.filename, parsed.filepath, parsed.ext, parsed.ispath, - parsed.lineage, parsed.group) == ( - parse_case.artifact, None, None, False, parse_case.lineage, - parse_case.group) - - -def test_parser_reads_name_from_file_path( - parse_case: ParseCase, make_temp_file: Callable[[str], str]) -> None: - """A parser built from a file path parses the file's stem.""" - full_path = make_temp_file(parse_case.artifact + '.txt') - - parser = parse_case.parser_class(full_path, parse_case.lineage, - parse_case.group, ispath=True) - - assert (parser.filename, parser.filepath, parser.ext, parser.ispath, - parser.name) == ( - parse_case.artifact + '.txt', Path(os.path.dirname(full_path)), - '.txt', True, parse_case.name) - - -@pytest.mark.parametrize('parser_class, lineage, group, artifact, expected', [ - pytest.param(HnsCub, 'space', 'nucleoid', 'N200kbmm2nh12ac2.0epshc1', - ['nmon', 'bend_mm', 'nhns', 'dcrowd', 'eps_hc'], - id='HnsCub-space'), -]) -def test_parser_lists_lineage_attributes_in_pattern_order( - parser_class: type[ParserBase], lineage: LineageT, group: str, - artifact: str, expected: list[str]) -> None: - """The parsed attributes are listed in the order the name gives them.""" - parser = parser_class(artifact, lineage, group) - - assert parser.lineage_attributes == expected - - -def test_parser_lists_each_attribute_once(parsed: ParserBase) -> None: - """No attribute appears twice in `attributes`.""" - assert len(parsed.attributes) == len(set(parsed.attributes)) - - -def test_parser_sets_every_listed_attribute(parsed: ParserBase) -> None: - """Every attribute in `attributes` is set on the instance.""" - assert set(parsed.attributes) <= vars(parsed).keys() - - -@pytest.mark.parametrize( - 'parser_class, artifact, group, geometry, topology, groups', [ - pytest.param(HnsCub, 'N200kbmm2nh12ac2.0epshc1', 'nucleoid', 'cubic', - 'ring', ['nucleoid', 'all'], id='HnsCub'), - ]) -def test_parser_reports_geometry_topology_and_groups( - parser_class: type[ParserBase], artifact: str, group: str, - geometry: str, topology: str, groups: list[str]) -> None: - """The parser reports its project's geometry, topology, and groups.""" - parser = parser_class(artifact, 'space', group) - - assert (parser.geometry, parser.topology, parser.groups) == ( - geometry, topology, groups) - - -@pytest.mark.parametrize('parser_class, artifact, group, dump_keywords', [ - pytest.param(HnsCub, 'N200kbmm2nh12ac2.0epshc1', 'nucleoid', - {'nucleoid': 'ndump', 'all': 'adump'}, id='HnsCub'), -]) -def test_parser_maps_each_group_to_its_dump_keyword( - parser_class: type[ParserBase], artifact: str, group: str, - dump_keywords: dict[str, str]) -> None: - """Each group's dump keyword names the period of its own trajectory.""" - parser = parser_class(artifact, 'space', group) - - assert parser.dump_keywords == dump_keywords - - -@pytest.mark.parametrize('parser_class, artifact, group, keyword, period', [ - # HnsCub names carry no dump periods; they are fixed in _defaults. - pytest.param(HnsCub, 'N200kbmm2.0nh8ac1.0l25.0epshc1.0nc0ens1.ring', - 'nucleoid', 'ndump', 5000, id='HnsCub-nucleoid'), - pytest.param(HnsCub, 'N200kbmm2.0nh8ac1.0l25.0epshc1.0nc0ens1.ring', - 'all', 'adump', 10000, id='HnsCub-all'), -]) -def test_dump_keyword_reads_the_group_dump_period( - parser_class: type[ParserBase], artifact: str, group: str, - keyword: str, period: int) -> None: - """dump_keyword names the attribute holding the group's dump period.""" - parser = parser_class(artifact, 'whole', group) - - assert (parser.dump_keyword, getattr(parser, parser.dump_keyword)) == ( - keyword, period) - - -@pytest.mark.parametrize('parser_class, artifact, group, attrs, expected', [ - pytest.param(HnsCub, 'N200kbmm2nh12ac2l25epshc1.nc11937', 'nucleoid', - ('eps_hc', 'ncrowd'), (1.0, 11937), id='HnsCub'), -]) -def test_parser_reads_value_with_trailing_dot( - parser_class: type[ParserBase], artifact: str, group: str, - attrs: tuple[str, ...], expected: tuple[float, ...]) -> None: - """A value written with a trailing dot, such as ``1.``, parses.""" - parser = parser_class(artifact, 'ensemble_long', group) - - # float('1.') is exact. - assert attrgetter(*attrs)(parser) == pytest.approx(expected, rel=1e-12) - - -def test_declared_pattern_round_trips( - declared_case: tuple[type[ParserBase], Any, str, dict[str, Any]] -) -> None: - """ - A name built from a declared pattern parses back to the declared types - and conversions, so float keywords keep non-integer values (property). - """ - parser_class, lineage, name, expected = declared_case - - actual = attrgetter(*expected)( - parser_class(name, lineage, parser_class._groups[0])) - - # One multiplication and one addition in float64. - assert actual == pytest.approx(tuple(expected.values()), rel=1e-12) - assert tuple(map(type, actual)) == tuple(map(type, expected.values())) - - -# Error paths of parsing - - -@pytest.mark.parametrize('parser_class, artifact, group, keyword, attr', [ - pytest.param(HnsCub, 'N200kbmm2XX12ac2l25epshc1nc11937', 'nucleoid', - 'nh', 'nhns', id='HnsCub-replaced'), - pytest.param(HnsCub, '200kbmm2nh12ac2l25epshc1nc11937', 'nucleoid', - 'N', 'nmon', id='HnsCub-heading-digit'), -]) -def test_missing_keyword_raises_value_error( - parser_class: type[ParserBase], artifact: str, group: str, - keyword: str, attr: str) -> None: - """A name without a pattern keyword raises ValueError naming it.""" - message = (f"keyword '{keyword}' for attribute '{attr}' not found in " - f"'{artifact}' (lineage 'ensemble_long')") - - with pytest.raises(ValueError, match=re.escape(message)): - parser_class(artifact, 'ensemble_long', group) - - -@pytest.mark.parametrize('parser_class, artifact, group, keyword', [ - pytest.param(HnsCub, 'N.kbmm2nh12ac2l25epshc1nc11937', 'nucleoid', 'N', - id='HnsCub'), -]) -def test_non_numeric_value_raises_value_error( - parser_class: type[ParserBase], artifact: str, group: str, - keyword: str) -> None: - """A keyword whose value is not a number raises ValueError naming it.""" - message = (f"value of keyword '{keyword}' in '{artifact}' " - "(lineage 'ensemble_long'): '.' is not a number") - - with pytest.raises(ValueError, match=re.escape(message)): - parser_class(artifact, 'ensemble_long', group) - - -@pytest.mark.parametrize('parser_class, artifact, group, keyword, token', [ - pytest.param(HnsCub, 'N200kbmm2nh12ac2l25epshc1nc1.5', 'nucleoid', 'nc', - '1.5', id='HnsCub-nc'), -]) -def test_non_integral_integer_raises_value_error( - parser_class: type[ParserBase], artifact: str, group: str, - keyword: str, token: str) -> None: - """A non-integral value of an integer keyword raises ValueError.""" - message = (f"value of keyword '{keyword}' in '{artifact}' " - f"(lineage 'ensemble_long'): '{token}' is not an integer") - - with pytest.raises(ValueError, match=re.escape(message)): - parser_class(artifact, 'ensemble_long', group) - - -def test_empty_artifact_raises_value_error(valid_case: ValidCase) -> None: - """An empty artifact name raises ValueError.""" - with pytest.raises(ValueError, - match="'artifact' cannot be an empty string."): - valid_case.parser_class('', valid_case.lineage, valid_case.group) - - -def test_invalid_group_raises_value_error(valid_case: ValidCase) -> None: - """A group the project does not have raises ValueError.""" - with pytest.raises(ValueError, match='is an invalid option.'): - valid_case.parser_class(valid_case.artifact, valid_case.lineage, - 'invalid') - - -def test_invalid_lineage_raises_value_error(valid_case: ValidCase) -> None: - """A lineage outside the five lineages raises ValueError.""" - with pytest.raises(ValueError, match='is an invalid option.'): - valid_case.parser_class(valid_case.artifact, - cast(LineageT, 'invalid'), - valid_case.group) - - -# Text representations - - -def test_str_summarizes_artifact(valid_case: ValidCase) -> None: - """`str` lists the name, geometry, group, lineage, topology, project.""" - parser = valid_case.parser_class(valid_case.artifact, valid_case.lineage, - valid_case.group) - - assert str(parser) == ( - f"Artifact:\n" - f" Name: '{valid_case.artifact}',\n" - f" Geometry: '{parser.geometry}',\n" - f" Group: '{valid_case.group}',\n" - f" Lineage: '{valid_case.lineage}',\n" - f" Topology: '{parser.topology}',\n" - f" Project: '{valid_case.parser_class.__name__}'") - - -def test_repr_names_artifact(valid_case: ValidCase) -> None: - """`repr` names the file, geometry, group, lineage, topology, project.""" - parser = valid_case.parser_class(valid_case.artifact, valid_case.lineage, - valid_case.group) - - assert repr(parser) == ( - f"Artifact(filename='{valid_case.artifact}', " - f"geometry='{parser.geometry}', group='{valid_case.group}', " - f"lineage='{valid_case.lineage}', topology='{parser.topology}', " - f"project='{valid_case.parser_class.__name__}')") - - -# Bulk values - - -@pytest.mark.parametrize('parser_class, artifact, suffix, number, size', [ - pytest.param(HnsCub, 'N200kbmm2nh12ac2l25epshc1nc11937', 'm', 'nmon', - 'dmon', id='HnsCub-m'), - pytest.param(HnsCub, 'N200kbmm2nh12ac2l25epshc1nc11937', 'hns', 'nhns', - 'dhns', id='HnsCub-hns'), - pytest.param(HnsCub, 'N200kbmm2nh12ac2l25epshc1nc11937', 'c', 'ncrowd', - 'dcrowd', id='HnsCub-c'), -]) -def test_cube_bulk_values_match_utils( - parser_class: type[ParserBase], artifact: str, suffix: str, - number: str, size: str) -> None: - """Cubic bulk values are the cube functions of the parsed values.""" - parser = parser_class(artifact, 'ensemble_long', parser_class._groups[0]) - n, d, lcube = attrgetter(number, size, 'lcube')(parser) - - actual = attrgetter(f'rho_bulk_{suffix}', f'phi_bulk_{suffix}')(parser) - - # The same functions on the same inputs; float64 round-off only. - assert actual == pytest.approx( - (number_density_cube(n, d, lcube, pbc=True), - volume_fraction_cube(n, d, lcube, pbc=True)), rel=1e-12) - - -# HnsCub float keywords - - -def test_hnscub_keeps_fractional_kbmm_and_epshc() -> None: - """Fractional kbmm and epshc stay floats in every lineage name.""" - parser = HnsCub('N200kbmm2.5nh8ac1.0l25.0epshc1.5nc0ens1.ring', 'whole', - 'nucleoid') - - assert attrgetter('bend_mm', 'eps_hc', 'whole', 'ensemble_long', - 'ensemble', 'space')(parser) == ( - 2.5, 1.5, 'N200kbmm2.5nh8ac1.0l25.0epshc1.5nc0ens1', - 'N200kbmm2.5nh8ac1.0l25.0epshc1.5nc0', - 'N200kbmm2.5nh8ac1epshc1.5nc0', 'N200kbmm2.5nh8ac1epshc1.5') - - -@pytest.mark.parametrize('lineage', [ - pytest.param('whole', id='whole'), - pytest.param('ensemble_long', id='ensemble_long'), - pytest.param('ensemble', id='ensemble'), - pytest.param('space', id='space'), -]) -@pytest.mark.parametrize('epshc, other_epshc', [ - pytest.param('1.5', '1.0', id='half-step'), - # Six significant digits, as the '%g' format prints, would merge these. - pytest.param('1.000001', '1', id='seventh-digit'), -]) -def test_hnscub_names_differing_in_epshc_stay_distinct( - lineage: LineageT, epshc: str, other_epshc: str) -> None: - """Names that differ only in epshc keep distinct lineage names.""" - parser = HnsCub(f'N200kbmm2.5nh8ac1.0l25.0epshc{epshc}nc0ens1.ring', - 'whole', 'nucleoid') - other = HnsCub(f'N200kbmm2.5nh8ac1.0l25.0epshc{other_epshc}nc0ens1.ring', - 'whole', 'nucleoid') - - assert getattr(parser, lineage) != getattr(other, lineage) - - -# Lineage-name rules - - -@pytest.mark.parametrize( - 'parser_class, group, lineage, artifact, ensemble, space', - [ - pytest.param(HnsCub, 'nucleoid', 'whole', - 'N200kbmm2nh12ac2l25epshc1nc0ens1.ring', - 'N200kbmm2nh12ac2epshc1nc0', 'N200kbmm2nh12ac2epshc1', - id='HnsCub-integer-text'), - pytest.param(HnsCub, 'nucleoid', 'whole', - 'N200kbmm2.0nh12ac2.0l25.0epshc1.0nc0ens1.ring', - 'N200kbmm2nh12ac2epshc1nc0', 'N200kbmm2nh12ac2epshc1', - id='HnsCub-float-text'), - pytest.param(HnsCub, 'all', 'whole', - 'N200kbmm2nh12ac2l25epshc0nc7162ens1.ring', - 'N200kbmm2nh12ac2epshc0nc7162', 'N200kbmm2nh12ac2epshc0', - id='HnsCub-zero'), - pytest.param(HnsCub, 'all', 'whole', - 'N200kbmm2nh12ac2l25epshc0.0nc7162ens1.ring', - 'N200kbmm2nh12ac2epshc0nc7162', 'N200kbmm2nh12ac2epshc0', - id='HnsCub-zero-float-text'), - ], -) -def test_short_name_prints_whole_number_float_as_integer( - parser_class: type[ParserBase], group: str, lineage: LineageT, - artifact: str, ensemble: str, space: str) -> None: - """A whole-number float, zero included, prints as an integer.""" - parser = parser_class(artifact, lineage, group) - - assert attrgetter('ensemble', 'space')(parser) == (ensemble, space) - - -@pytest.mark.parametrize( - 'parser_class, group, lineage, artifact, ensemble, space', - [ - pytest.param(HnsCub, 'nucleoid', 'whole', - 'N200kbmm2.5nh8ac1.5l25epshc0.25nc0ens1.ring', - 'N200kbmm2.5nh8ac1.5epshc0.25nc0', - 'N200kbmm2.5nh8ac1.5epshc0.25', id='HnsCub'), - ], -) -def test_short_name_prints_fractional_float_as_repr( - parser_class: type[ParserBase], group: str, lineage: LineageT, - artifact: str, ensemble: str, space: str) -> None: - """A fractional float prints in full, as repr, in short names.""" - parser = parser_class(artifact, lineage, group) - - assert attrgetter('ensemble', 'space')(parser) == (ensemble, space) - - -@pytest.mark.parametrize('short_lineage', [ - pytest.param('ensemble', id='ensemble'), - pytest.param('space', id='space'), -]) -def test_short_name_reparses_to_itself( - valid_case: ValidCase, short_lineage: LineageT) -> None: - """A short name parsed at its own lineage rebuilds the same name.""" - parser = valid_case.parser_class(valid_case.artifact, valid_case.lineage, - valid_case.group) - name = getattr(parser, short_lineage) - - reparsed = valid_case.parser_class(name, short_lineage, valid_case.group) - - assert getattr(reparsed, short_lineage) == name - - -@pytest.mark.parametrize('parser_class, group, segment, whole, expected', [ - pytest.param(HnsCub, 'all', - 'N200kbmm2nh12ac2l25epshc0nc7162ens1.j07.ring.all', - 'N200kbmm2nh12ac2l25epshc0nc7162ens1.ring.all', - 'N200kbmm2nh12ac2l25epshc0nc7162ens1', id='HnsCub-ring'), -]) -def test_whole_name_drops_topology_suffix_from_segment_and_whole( - parser_class: type[ParserBase], group: str, segment: str, whole: str, - expected: str) -> None: - """A simulation's segment and whole files give one suffix-free whole.""" - from_segment = parser_class(segment, 'segment', group) - from_whole = parser_class(whole, 'whole', group) - - assert (getattr(from_segment, 'whole'), - getattr(from_whole, 'whole')) == (expected, expected) - - -def _stamp_mismatches(path: Path) -> list[tuple[str, ...]]: - """ - Compare the names in one research-code ``HnsCub`` stamp file with the - rebuilt names, under the mapping that the Usage page, "Lineage names", - documents. - - A stamp file is ``--stamps.csv``; its columns start - with ``lineage_name`` and the lineage names, with a ``segment`` column - only for a segment. The expected rebuilt name is the stamp's ``segment`` - or ``whole`` without ``.ring``, its ``ensemble_long`` unchanged, and its - ``ensemble`` or ``space`` parsed at that lineage and rebuilt; a short - key must also parse to the artifact's own values. Returns one - ``(file, column, stamp, expected, rebuilt)`` tuple per mismatch. - """ - group = path.name.removesuffix('-stamps.csv').rsplit('-', 1)[1] - frame = pd.read_csv(path, dtype=str) - lineage: LineageT = ('segment' if 'segment' in frame.columns - else 'whole') - mismatches: list[tuple[str, ...]] = [] - for row in frame.to_dict('records'): - parser = HnsCub(row['lineage_name'], lineage, group) - expected: dict[str, str] = { - column: row[column].removesuffix('.ring') - for column in ('segment', 'whole') if column in row} - expected['ensemble_long'] = row['ensemble_long'] - for column in ('ensemble', 'space'): - old = HnsCub(row[column], column, group) - expected[column] = getattr(old, column) - values = attrgetter(*HnsCub._genealogy_attributes[column]) - if values(old) != values(parser): - mismatches.append((path.name, f'{column} values', - row[column], str(values(parser)), - str(values(old)))) - mismatches.extend( - (path.name, column, row[column], name, getattr(parser, column)) - for column, name in expected.items() - if getattr(parser, column) != name) - return mismatches - - -@pytest.fixture -def write_stamp(tmp_path: Path) -> Callable[[str, dict[str, str]], Path]: - """ - Return a factory that writes a one-row stamp file in the research code's - format, ``--stamps.csv``, from a mapping of column - to value, and returns its path. - """ - def _write(group: str, columns: dict[str, str]) -> Path: - path = tmp_path / f"{columns['lineage_name']}-{group}-stamps.csv" - path.write_text(','.join(columns) + '\n' - + ','.join(columns.values()) + '\n') - return path - return _write - - -@pytest.mark.parametrize('group, columns, flagged', [ - pytest.param( - 'nucleoid', - {'lineage_name': 'N200kbmm2nh12ac2l25epshc0nc7162ens1.ring', - 'whole': 'N200kbmm2nh12ac2l25epshc0nc7162ens1.ring', - 'ensemble_long': 'N200kbmm2nh12ac2l25epshc0nc7162', - 'ensemble': 'N200kbmm2nh12ac2.0epshc0nc7162', - 'space': 'N200kbmm2nh12ac2.0epshc0', 'nmon': '200', 'n_frames': '10'}, - [], id='whole-matches'), - pytest.param( - 'all', - {'lineage_name': 'N200kbmm2nh12ac2l25epshc0nc7162ens1.j07.ring', - 'segment': 'N200kbmm2nh12ac2l25epshc0nc7162ens1.j07.ring', - 'whole': 'N200kbmm2nh12ac2l25epshc0nc7162ens1', - 'ensemble_long': 'N200kbmm2nh12ac2l25epshc0nc7162', - 'ensemble': 'N200kbmm2nh12ac2.0epshc0nc7162', - 'space': 'N200kbmm2nh12ac2.0epshc0', 'nmon': '200', 'n_frames': '10'}, - [], id='segment-matches'), - pytest.param( - 'nucleoid', - {'lineage_name': 'N200kbmm2nh12ac2l25epshc0nc7162ens1.ring', - 'whole': 'N200kbmm2nh12ac2l25epshc0nc7162ens1.ring', - 'ensemble_long': 'N200kbmm2nh12ac2l25epshc0nc7162', - 'ensemble': 'N200kbmm2nh12ac2.0epshc0nc7162', - 'space': 'N200kbmm2nh12ac2.0epshc1', 'nmon': '200', 'n_frames': '10'}, - ['space values', 'space'], id='space-value-differs'), -]) -def test_stamp_mismatches_flags_only_departures_from_mapping( - write_stamp: Callable[[str, dict[str, str]], Path], group: str, - columns: dict[str, str], flagged: list[str]) -> None: - """A stamp differing from the rebuilt names only by the mapping passes.""" - path = write_stamp(group, columns) - - mismatches = _stamp_mismatches(path) - - assert [mismatch[1] for mismatch in mismatches] == flagged - - -def test_hnscub_names_match_existing_stamps_under_mapping( # pragma: no cover - private_data_dir: Path) -> None: - """Rebuilt HnsCub names equal research-code stamps under the mapping.""" - stamps = sorted( - path for path in private_data_dir.rglob('*-stamps.csv') - if any(part.startswith('HnsCub') - for part in path.relative_to(private_data_dir).parts[:-1])) - - mismatches = [mismatch for path in stamps - for mismatch in _stamp_mismatches(path)] - - assert stamps, 'no stamp file under a directory named HnsCub*' - assert mismatches == [] - - -@pytest.mark.parametrize( - 'parser_class, group, child_lineage, child_name, parent_lineage', - [ - pytest.param(HnsCub, 'nucleoid', 'segment', - 'N200kbmm2nh12ac2l25epshc1nc11937ens2.j5.ring.nucleoid', - 'whole', id='HnsCub-segment-whole'), - ], -) -def test_parser_rebuilt_parent_name_keeps_attributes( - parser_class: type[ParserBase], group: str, child_lineage: LineageT, - child_name: str, parent_lineage: LineageT) -> None: - """A rebuilt parent name parses to its child's attribute values.""" - child = parser_class(child_name, child_lineage, group) - parent = parser_class(getattr(child, parent_lineage), parent_lineage, - group) - values = attrgetter(*parser_class._genealogy_attributes[parent_lineage]) - - # Both sides parse the same digits, so they agree to float64 round-off. - assert values(parent) == pytest.approx(values(child), rel=1e-12, - abs=1e-12) - - -@pytest.mark.parametrize( - 'parser_class, group, lineage, artifact, parent_lineage, expected', - [ - pytest.param(HnsCub, 'nucleoid', 'segment', - 'N200kbmm2nh12ac2l25epshc1nc11937ens2.j5.ring.nucleoid', - 'ensemble_long', 'N200kbmm2nh12ac2l25epshc1nc11937', - id='HnsCub-ensemble_long'), - ], -) -def test_parser_rebuilt_parent_name_is_name_prefix( - parser_class: type[ParserBase], group: str, lineage: LineageT, - artifact: str, parent_lineage: LineageT, expected: str) -> None: - """A long-form parent name is the artifact name cut after its last key.""" - parser = parser_class(artifact, lineage, group) - - assert getattr(parser, parent_lineage) == expected - - -# Declarations - - -@pytest.mark.parametrize('parser_class, lineage, expected', [ - pytest.param(HnsCub, 'space', - OrderedDict([('nmon', 'N'), ('bend_mm', 'kbmm'), - ('nhns', 'nh'), ('dcrowd', 'ac'), - ('eps_hc', 'epshc')]), - id='HnsCub-space'), -]) -def test_genealogy_attributes_follow_pattern_order( - parser_class: type[ParserBase], lineage: LineageT, - expected: OrderedDict[str, str]) -> None: - """Each lineage maps attributes to keywords in the pattern's order.""" - table = parser_class._genealogy_attributes[lineage] - - assert (type(table), list(table.items())) == (OrderedDict, - list(expected.items())) - - -def test_attributes_at_matches_parsed_instance( - parse_case: ParseCase, parsed: ParserBase) -> None: - """attributes_at names exactly the public attributes a parser sets.""" - public = {name for name in vars(parsed) if not name.startswith('_')} - - names = parse_case.parser_class.attributes_at(parse_case.lineage) - - assert names == public - - -# Generated names - - -@pytest.mark.parametrize('lineage', [ - pytest.param('segment', id='segment'), - pytest.param('whole', id='whole'), - pytest.param('ensemble_long', id='ensemble_long'), - pytest.param('ensemble', id='ensemble'), - pytest.param('space', id='space'), -]) -@settings(max_examples=50, deadline=None) -@given(data=st.data()) -def test_generated_name_parses_to_its_values_and_back( - lineage: LineageT, data: st.DataObject) -> None: - """A name built from HnsCub's own pattern parses to its values and - rebuilds to itself: a round-trip property, with no reference value.""" - name, values = data.draw(lineage_names(HnsCub, lineage)) - - parsed = HnsCub(name, lineage, HnsCub._groups[0]) - - assert getattr(parsed, lineage) == name - # The parser converts in exact decimal arithmetic, the expectation in - # float64: a few ulp apart; atol covers a value converted to zero. - expected = stored_values(HnsCub, values) - assert {attr: getattr(parsed, attr) for attr in expected} == \ - pytest.approx(expected, rel=1e-12, abs=1e-12) diff --git a/projects/hns_cyl/__init__.py b/projects/hns_cyl/__init__.py deleted file mode 100644 index 5da3398f..00000000 --- a/projects/hns_cyl/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -""" -The *HnsCyl* study: ``SPEC`` is its -:class:`~polyphys.spec.ProjectSpec`, ``PROBE`` its -:class:`~polyphys.spec.ProbeSpec`, and ``ANALYSIS`` its -:class:`~polyphys.spec.AnalysisTable` per particle group, and -``HnsCyl`` its parser. -""" -from .parser import HnsCyl -from .spec import ANALYSIS, PROBE, SPEC - -__all__ = ["ANALYSIS", "PROBE", "SPEC", "HnsCyl"] diff --git a/projects/hns_cyl/parser.py b/projects/hns_cyl/parser.py deleted file mode 100644 index acbe756a..00000000 --- a/projects/hns_cyl/parser.py +++ /dev/null @@ -1,163 +0,0 @@ -""" -The parser of the *HnsCyl* study: a ring polymer with H-NS proteins among -crowders in a cylinder. - -.. versionchanged:: 1.0.0 - Moved here from :mod:`polyphys.manage.parser`, which ships no study. -""" -from polyphys.manage.parser import KeywordSpec, ParserBase, Species - -__all__ = ["HnsCyl"] - - -class HnsCyl(ParserBase): - """ - Parse an artifact name of the *HnsCyl* project: a ring polymer with H-NS - proteins among crowders in a cylinder that is periodic along its axis. - - Each lineage has its own name pattern; ``#`` stands for a value: - - - ``segment``: N#kbmm#r#nh#ac#lz#epshc#nc#ens#.j#.ring - - ``whole``: N#kbmm#r#nh#ac#lz#epshc#nc#ens#.ring - - ``ensemble_long``: N#kbmm#r#nh#ac#lz#epshc#nc# - - ``ensemble``: N#D#nh#ac#epshc#nc# - - ``space``: N#D#nh#ac#epshc# - - Parameters - ---------- - artifact : Pathish - Name to be parsed, or a file path with ``ispath=True``. - lineage : {'segment', 'whole', 'ensemble_long', 'ensemble', 'space'} - Lineage of the artifact. - group : {'nucleoid', 'all'} - Particle group: ``'nucleoid'`` or ``'all'`` for every particle. - ispath : bool, default False - Whether `artifact` is a file path. - - Attributes - ---------- - dmon : int - Monomer diameter, fixed at 1; not in the name. - nmon : int - Number of monomers; keyword ``N``. - dhns : int - Diameter of an H-NS protein, fixed at 1; not in the name. - dhns_patch : float - Diameter of the patch at each pole of an H-NS protein, fixed at - 0.178; not in the name. - nhns : int - Number of H-NS proteins; keyword ``nh``. - dcrowd : float - Crowder diameter; keyword ``ac``. - ncrowd : int - Number of crowders; keyword ``nc``. - bend_mm : float - Bending rigidity of the DNA monomers; keyword ``kbmm``. - eps_hm : int - Strength of the attractive LJ interaction between the H-NS poles and - the monomers, fixed at 29; not in the name. - eps_hc : float - Value of the keyword ``epshc``, named for the strength of the LJ - interaction between the H-NS cores and the crowders. The project's - LAMMPS inputs do not read it: they fix that strength at 1 with a - purely repulsive cutoff. - lcyl : float - Length of the cylinder, :math:`Z = 2 l_z`, from the half-length - :math:`l_z` (keyword ``lz``). - dcyl : float - Size of the cylinder, :math:`D = 2r - 1`, from the radius :math:`r` - (keyword ``r``) in the long names; :math:`D` itself (keyword ``D``) - in the short ones. - dt : float - Simulation time step, fixed at 0.005; not in the name. - ndump : int - Dump period of the ``nucleoid`` trajectory, fixed at 5000 time steps; - not in the name. - adump : int - Dump period of the ``all`` trajectory, fixed at 10000 time steps; not - in the name. - ensemble_id : int - Index of the whole in its ensemble; keyword ``ens``. - segment_id : int - Index of the segment in its whole, zero-padded in the name; keyword - ``j``. - rho_bulk_m, phi_bulk_m : float - Bulk number density and volume fraction of monomers. - rho_bulk_hns, phi_bulk_hns : float - Bulk number density and volume fraction of H-NS proteins. - rho_bulk_c, phi_bulk_c : float - Bulk number density and volume fraction of crowders. - - Notes - ----- - The bulk values exist only at ``segment``, ``whole``, and - ``ensemble_long``. The lineage names are documented in - :class:`~polyphys.manage.parser.ParserBase`. - - The cylindrical wall is a layer of beads of size :math:`a_w = 1` whose - centers lie on the cylinder of radius :math:`r` set in LAMMPS (keyword - ``r``), so the cylinder size is :math:`D = 2r - a_w`, stored as `dcyl`. - The keyword ``lz`` is the half-length of the cylinder, so its length is - :math:`Z = 2 l_z`, stored as `lcyl`. The centers of particles of - species :math:`\\alpha` with size :math:`a_\\alpha` are confined to a - cylinder of size :math:`D_{\\mathrm{eff},\\alpha} = D - a_\\alpha`, and - the bulk number density and volume fraction of that species both use - the effective volume :math:`V_{\\mathrm{eff},\\alpha} = \\pi Z - D_{\\mathrm{eff},\\alpha}^2 / 4`. - - The simulation design values that the name does not carry, such as - the interaction strengths, the bonds, and the thermostat, are listed - in :doc:`/design`. - - Examples - -------- - >>> artifact = HnsCyl( - ... 'N200D20.0nh16ac1.0epshc1.0nc0', - ... 'space', - ... 'nucleoid' - ... ) - >>> print(artifact.eps_hc) - 1.0 - """ - _geometry = 'cylindrical' - _topology = 'ring' - _groups = ['nucleoid', 'all'] - _dump_keywords = {'nucleoid': 'ndump', 'all': 'adump'} - _keywords = { - 'N': KeywordSpec('nmon', int), - 'kbmm': KeywordSpec('bend_mm', float), - 'r': KeywordSpec('dcyl', float, scale=2.0, offset=-1.0), - 'D': KeywordSpec('dcyl', float), - 'nh': KeywordSpec('nhns', int), - 'ac': KeywordSpec('dcrowd', float), - 'lz': KeywordSpec('lcyl', float, scale=2.0), - 'epshc': KeywordSpec('eps_hc', float), - 'nc': KeywordSpec('ncrowd', int), - 'ens': KeywordSpec('ensemble_id', int), - 'j': KeywordSpec('segment_id', int), - } - _patterns = { - 'segment': ('N', 'kbmm', 'r', 'nh', 'ac', 'lz', 'epshc', 'nc', 'ens', - 'j'), - 'whole': ('N', 'kbmm', 'r', 'nh', 'ac', 'lz', 'epshc', 'nc', 'ens'), - 'ensemble_long': ('N', 'kbmm', 'r', 'nh', 'ac', 'lz', 'epshc', 'nc'), - 'ensemble': ('N', 'D', 'nh', 'ac', 'epshc', 'nc'), - 'space': ('N', 'D', 'nh', 'ac', 'epshc'), - } - _defaults = {'dmon': 1, 'dhns': 1, 'dhns_patch': 0.178, 'dt': 0.005, - 'ndump': 5000, 'adump': 10000, 'eps_hm': 29} - _species = (Species('m', 'nmon', 'dmon'), Species('hns', 'nhns', 'dhns'), - Species('c', 'ncrowd', 'dcrowd')) - _project_attributes = { - 'segment': ['dmon', 'dhns', 'phi_bulk_m', 'rho_bulk_m', - 'phi_bulk_c', 'rho_bulk_c', 'phi_bulk_hns', - 'rho_bulk_hns', 'dt', 'ndump', 'adump', 'eps_hm'], - 'whole': ['dmon', 'dhns', 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', - 'rho_bulk_c', 'phi_bulk_hns', 'rho_bulk_hns', 'dt', 'ndump', - 'adump', 'eps_hm'], - 'ensemble_long': ['dmon', 'dhns', 'phi_bulk_m', 'rho_bulk_m', - 'phi_bulk_c', 'rho_bulk_c', 'phi_bulk_hns', - 'rho_bulk_hns', 'dt', 'ndump', 'adump', 'eps_hm'], - 'ensemble': ['dmon', 'dhns', 'dt', 'ndump', 'adump', 'eps_hm'], - 'space': ['dmon', 'dhns', 'dt', 'ndump', 'adump', 'eps_hm'] - } diff --git a/projects/hns_cyl/spec.py b/projects/hns_cyl/spec.py deleted file mode 100644 index 8b8f7774..00000000 --- a/projects/hns_cyl/spec.py +++ /dev/null @@ -1,143 +0,0 @@ -""" -The *HnsCyl* study: a ring polymer with H-NS proteins among crowders in a -cylinder that is periodic along its axis. ``SPEC`` is its -:class:`~polyphys.spec.ProjectSpec`, ``PROBE`` its -:class:`~polyphys.spec.ProbeSpec`, and ``ANALYSIS`` its -:class:`~polyphys.spec.AnalysisTable` per particle group. - -Values from the research code's registry, with the PolyPhys attribute names -``phi_bulk_c`` for ``phi_c_bulk`` and ``rho_bulk_hns`` for ``rho_hns_bulk``, -and from its analysis tables, with each artifact listed once. -""" -from polyphys.analyze.pipeline import Autocorrelation, LocalDensities -import numpy as np - -from polyphys.manage.parser import ParserBase -from polyphys.spec import ( - AnalysisTable, - BinderSpec, - ProbeSpec, - ProjectSpec, - edge, - histogram, - histogram_2d, - matrix_tseries, - tseries, -) - -from .parser import HnsCyl - -SPEC = ProjectSpec( - parser=HnsCyl, - group='nucleoid', - divisor=0.01, - crowder_attr='phi_bulk_c', - physical_attrs=('space', 'ensemble_long', 'ensemble', 'dcyl', 'nmon', - 'nhns', 'dcrowd', 'phi_bulk_c', 'rho_bulk_hns'), - species_sizes={'Mon': 'dmon', 'Crd': 'dcrowd', 'Hns': 'dhns'}, - tseries_properties=('asphericityTMon', 'fsdTMon', 'shapeTMon', - 'gyrTMon', 'transSizeTMon', 'bondLengthVecMon', - 'nBoundTHnsPatch', 'nFreeTHnsPatch', - 'nEngagedTHnsPatch', 'nFreeTHnsCore', - 'nBridgeTHnsCore', 'nDangleTHnsCore', - 'nCisTHnsCore', 'nTransTHnsCore'), -) - - -def _r_bin(info: ParserBase) -> float: - """A tenth of the smallest of the monomer, H-NS core, and crowder - diameters: the bin size along r. The research probe left the H-NS core - out of the minimum here, unlike ``hns_cub_all``; the maintainer called - that a mistake on 2026-11-04 and both studies now agree.""" - return 0.1 * min(float(getattr(info, 'dmon')), - float(getattr(info, 'dhns')), - float(getattr(info, 'dcrowd'))) - - -def _l_bin(info: ParserBase) -> float: - """A tenth of the smaller of the monomer and crowder diameters: the bin - size along x and y (the research code's ``hns_cyl_all``, ``bin_edges``, - ``lEdge``).""" - return 0.1 * min(float(getattr(info, 'dmon')), - float(getattr(info, 'dcrowd'))) - - -def _z_bin(info: ParserBase) -> float: - """Half the smaller of the monomer and crowder diameters: the bin size - along z (the research code's ``hns_cyl_all``, ``bin_edges``).""" - return 0.5 * min(float(getattr(info, 'dmon')), - float(getattr(info, 'dcrowd'))) - - -# The probe of the polymer group: the selections, the chain-size observables -# of its per-frame loop, and the unwrap convention of its asphericity (the -# research code's ``hns_cyl_nucleoid``); the histograms of the all group -# (``hns_cyl_all``): its selections, bin sizes, and species, the H-NS cores -# as 'Hns', with the block length and the recentering of the probe guide -# ("The spatial histograms" and "Recentering a frame"); and the binding of -# the H-NS patches (``hns_cyl_nucleoid``, ``cis_threshold = 4`` and the -# distance matrices saved as ``distMatTMonHnsPatch``). The cutoff is the -# published binding criterion, r <= r_cut,mp = 0.685 (thesis, Section 5.3 -# and Table 5.1), which the maintainer chose on 2026-11-04 over the research -# probe's ``round(2**(1/6) * (dmon + dhns_patch) / 2, 3) = 0.661`` with a -# strict comparison; the probe guide, "Choosing the cutoff", gives both. -# The cis threshold is 3, the maintainer's definition (thesis, chapter 5: a -# bridge with at most two monomers between its sites, at most 3 bonds, is -# cis), which the maintainer chose on 2026-11-08 over the research probe's 4; -# the probe guide, "Choosing the cis threshold", gives both and why. -PROBE = ProbeSpec( - project=SPEC, - selections={'Mon': 'resid 1', 'HnsPatch': 'type 2', 'Crd': 'resid 0', - 'Hns': 'type 3'}, - chain='Mon', - observables=('asphericityT', 'fsdT', 'shapeT', 'gyrT', 'transSizeT', - 'principalT'), - unwrap=False, - bin_sizes={'r': _r_bin, 'z': _z_bin, 'theta': np.pi / 36, 'x': _l_bin, - 'y': _l_bin}, - histogram_species=('Crd', 'Mon', 'Hns'), - block_frames=100, - recenter='half', - binders=BinderSpec(patches='HnsPatch', binders='HnsCore', cutoff=0.685, - inclusive=True, cis_threshold=3, - store_distances=True), -) - -# The largest lag of the autocorrelation functions, in frames (the research -# code's ``analyze_measures``, ``nlags``). -NLAGS = 7000 - -ANALYSIS = ( - AnalysisTable(group='nucleoid', lineage='whole', stamps=True, properties=( - matrix_tseries('principalT', 'Mon'), - # Per-whole vectors reduced as time series. - tseries('loopLengthHist', 'Mon'), - tseries('bondCosineCorrVec', 'Mon'), - tseries('bondLengthVec', 'Mon'), - tseries('nBoundT', 'HnsPatch'), - tseries('nFreeT', 'HnsPatch'), - tseries('nEngagedT', 'HnsPatch'), - tseries('nFreeT', 'HnsCore'), - tseries('nBridgeT', 'HnsCore'), - tseries('nDangleT', 'HnsCore'), - tseries('nCisT', 'HnsCore'), - tseries('nTransT', 'HnsCore'), - tseries('fsdT', 'Mon', post_hook=Autocorrelation(NLAGS)), - tseries('gyrT', 'Mon', post_hook=Autocorrelation(NLAGS)), - tseries('transSizeT', 'Mon', post_hook=Autocorrelation(NLAGS)), - tseries('shapeT', 'Mon', post_hook=Autocorrelation(NLAGS)), - tseries('asphericityT', 'Mon', post_hook=Autocorrelation(NLAGS)), - )), - AnalysisTable(group='all', lineage='segment', stamps=False, properties=( - *(histogram(direction, species, post_hook=LocalDensities()) - for direction in ('r', 'z') - for species in ('Crd', 'Mon', 'Hns')), - *(histogram('theta', species) for species in ('Crd', 'Mon', 'Hns')), - *(histogram_2d(plane, species) - for plane in ('xy', 'xz', 'yz') - for species in ('Crd', 'Mon', 'Hns')), - edge('x'), - edge('y'), - edge('z'), - )), -) diff --git a/projects/hns_cyl/test_parser.py b/projects/hns_cyl/test_parser.py deleted file mode 100644 index 38c7f5db..00000000 --- a/projects/hns_cyl/test_parser.py +++ /dev/null @@ -1,496 +0,0 @@ -"""Tests for the *HnsCyl* parser, :mod:`hns_cyl.parser`.""" -import os -import re -from collections.abc import Callable -from operator import attrgetter -from pathlib import Path -from typing import Any, NamedTuple, cast - -import pytest -from hypothesis import given, settings -from hypothesis import strategies as st - -from hns_cyl.parser import HnsCyl -from polyphys.manage.parser import ParserBase -from polyphys.manage.types import LineageT -from polyphys.tests.strategies import lineage_names, stored_values -from polyphys.manage.utils import ( - number_density_cylinder, - volume_fraction_cylinder, -) - - -class ParseCase(NamedTuple): - """A hand-written artifact name and what its parser must give.""" - parser_class: type[ParserBase] - lineage: Any - group: Any - artifact: str - name: str - values: dict[str, Any] - physical: list[str] - - -class ValidCase(NamedTuple): - """A valid ``ensemble_long`` artifact name for one parser class.""" - parser_class: type[ParserBase] - artifact: str - lineage: Any - group: Any - - -@pytest.fixture(params=[ - pytest.param( - ParseCase( - HnsCyl, 'segment', 'all', - artifact='N200kbmm2r4.5nh4ac1lz75epshc1.0nc1152ens1.j01.ring.all', - name='N200kbmm2r4.5nh4ac1lz75epshc1.0nc1152ens1.j01.ring', - values=dict(nmon=200, bend_mm=2.0, dcyl=8, nhns=4, dcrowd=1.0, - lcyl=150, eps_hc=1.0, ncrowd=1152, ensemble_id=1, - segment_id=1), - physical=['dmon', 'dhns', 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', - 'rho_bulk_c', 'phi_bulk_hns', 'rho_bulk_hns', 'dt', - 'ndump', 'adump', 'eps_hm']), - id='HnsCyl-segment'), - pytest.param( - ParseCase( - HnsCyl, 'whole', 'all', - artifact='N200kbmm2r4.5nh4ac1lz75epshc1.0nc1152ens1.ring.all', - name='N200kbmm2r4.5nh4ac1lz75epshc1.0nc1152ens1.ring', - values=dict(nmon=200, bend_mm=2.0, dcyl=8, nhns=4, dcrowd=1.0, - lcyl=150, eps_hc=1.0, ncrowd=1152, ensemble_id=1), - physical=['dmon', 'dhns', 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', - 'rho_bulk_c', 'phi_bulk_hns', 'rho_bulk_hns', 'dt', - 'ndump', 'adump', 'eps_hm']), - id='HnsCyl-whole'), - pytest.param( - ParseCase( - HnsCyl, 'ensemble_long', 'all', - artifact='N200kbmm2r4.5nh4ac1lz75epshc1.0nc1152', - name='N200kbmm2r4.5nh4ac1lz75epshc1.0nc1152', - values=dict(nmon=200, bend_mm=2.0, dcyl=8, nhns=4, dcrowd=1.0, - lcyl=150, eps_hc=1.0, ncrowd=1152), - physical=['dmon', 'dhns', 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', - 'rho_bulk_c', 'phi_bulk_hns', 'rho_bulk_hns', 'dt', - 'ndump', 'adump', 'eps_hm']), - id='HnsCyl-ensemble_long'), - pytest.param( - ParseCase( - HnsCyl, 'ensemble', 'all', - artifact='N200D8nh4ac1epshc1.0nc1152', - name='N200D8nh4ac1epshc1.0nc1152', - values=dict(nmon=200, dcyl=8, nhns=4, dcrowd=1.0, eps_hc=1.0, - ncrowd=1152), - physical=['dmon', 'dhns', 'dt', 'ndump', 'adump', 'eps_hm']), - id='HnsCyl-ensemble'), - pytest.param( - ParseCase( - HnsCyl, 'space', 'all', - artifact='N200D8nh4ac1epshc1.0', - name='N200D8nh4ac1epshc1.0', - values=dict(nmon=200, dcyl=8, nhns=4, dcrowd=1.0, eps_hc=1.0), - physical=['dmon', 'dhns', 'dt', 'ndump', 'adump', 'eps_hm']), - id='HnsCyl-space'), -]) -def parse_case(request: pytest.FixtureRequest) -> ParseCase: - """ - One hand-written name per lineage, with the values the parser must give: - the digits in the name, converted as the class Notes state - (``lcyl = 2 lz``, ``dcyl = 2 r - 1``). - """ - case: ParseCase = request.param - return case - - -@pytest.fixture -def parsed(parse_case: ParseCase) -> ParserBase: - """The parser built from the name of `parse_case`.""" - return parse_case.parser_class(parse_case.artifact, parse_case.lineage, - parse_case.group) - - -@pytest.fixture(params=[ - pytest.param( - ValidCase(HnsCyl, 'N200kbmm2r4.5nh4ac1lz75epshc1.0nc1152', - 'ensemble_long', 'nucleoid'), - id='HnsCyl'), -]) -def valid_case(request: pytest.FixtureRequest) -> ValidCase: - """A valid ``ensemble_long`` name of the parser class.""" - case: ValidCase = request.param - return case - - -@pytest.fixture(params=[ - pytest.param((parser_class, lineage), - id=f"{parser_class.__name__}-{lineage}") - for parser_class in (HnsCyl,) - for lineage in ('segment', 'whole', 'ensemble_long', 'ensemble', 'space') -]) -def declared_case( - request: pytest.FixtureRequest -) -> tuple[type[ParserBase], Any, str, dict[str, Any]]: - """ - A name built from the class's declared pattern for one lineage, with 10.5 - as the value of every float keyword and 2 as the value of every integer - keyword, and the values the parser must store for them. With these - values every volume fraction stays below 1. - """ - parser_class, lineage = request.param - specs = [(keyword, parser_class._keywords[keyword]) - for keyword in parser_class._patterns[lineage]] - name = ''.join(f"{keyword}{'10.5' if spec.kind is float else '2'}" - for keyword, spec in specs) - expected = {spec.attr: (spec.scale * 10.5 + spec.offset - if spec.kind is float else 2) - for _, spec in specs} - return parser_class, lineage, name, expected - - -@pytest.fixture -def make_temp_file(tmp_path: Path) -> Callable[[str], str]: - """ - Create an empty temporary file with a given filename and return its full - path. Useful for testing parsers that expect a file path as input. - """ - - def _make_temp_file(filename: str) -> str: - f = tmp_path / filename - f.touch() - return str(f) - - return _make_temp_file - - -# Parsing hand-written names - - -def test_parser_sets_values_from_name( - parse_case: ParseCase, parsed: ParserBase) -> None: - """Each lineage's name parses to its digits, converted as documented.""" - actual = attrgetter(*parse_case.values)(parsed) - # Values are parsed from decimal text; equal to float64 round-off. - assert actual == pytest.approx(tuple(parse_case.values.values()), - rel=1e-12, abs=1e-12) - - -def test_parser_reduces_artifact_to_lineage_name( - parse_case: ParseCase, parsed: ParserBase) -> None: - """The name drops the group and topology suffixes after the lineage.""" - assert parsed.name == parse_case.name - - -def test_parser_lists_physical_attributes( - parse_case: ParseCase, parsed: ParserBase) -> None: - """Each lineage lists the fixed and computed attributes it sets.""" - assert parsed.physical_attributes == parse_case.physical - - -def test_parser_names_its_project( - parse_case: ParseCase, parsed: ParserBase) -> None: - """The project name is the parser class's name.""" - assert parsed.project_name == parse_case.parser_class.__name__ - - -def test_parser_without_path_reports_no_path( - parse_case: ParseCase, parsed: ParserBase) -> None: - """A parser built from a name keeps it and has no filepath or ext.""" - assert (parsed.filename, parsed.filepath, parsed.ext, parsed.ispath, - parsed.lineage, parsed.group) == ( - parse_case.artifact, None, None, False, parse_case.lineage, - parse_case.group) - - -def test_parser_reads_name_from_file_path( - parse_case: ParseCase, make_temp_file: Callable[[str], str]) -> None: - """A parser built from a file path parses the file's stem.""" - full_path = make_temp_file(parse_case.artifact + '.txt') - - parser = parse_case.parser_class(full_path, parse_case.lineage, - parse_case.group, ispath=True) - - assert (parser.filename, parser.filepath, parser.ext, parser.ispath, - parser.name) == ( - parse_case.artifact + '.txt', Path(os.path.dirname(full_path)), - '.txt', True, parse_case.name) - - -def test_parser_lists_each_attribute_once(parsed: ParserBase) -> None: - """No attribute appears twice in `attributes`.""" - assert len(parsed.attributes) == len(set(parsed.attributes)) - - -def test_parser_sets_every_listed_attribute(parsed: ParserBase) -> None: - """Every attribute in `attributes` is set on the instance.""" - assert set(parsed.attributes) <= vars(parsed).keys() - - -@pytest.mark.parametrize( - 'parser_class, artifact, group, geometry, topology, groups', [ - pytest.param(HnsCyl, 'N200D8nh4ac1epshc1.0', 'all', 'cylindrical', - 'ring', ['nucleoid', 'all'], id='HnsCyl'), - ]) -def test_parser_reports_geometry_topology_and_groups( - parser_class: type[ParserBase], artifact: str, group: str, - geometry: str, topology: str, groups: list[str]) -> None: - """The parser reports its project's geometry, topology, and groups.""" - parser = parser_class(artifact, 'space', group) - - assert (parser.geometry, parser.topology, parser.groups) == ( - geometry, topology, groups) - - -@pytest.mark.parametrize('parser_class, artifact, group, dump_keywords', [ - pytest.param(HnsCyl, 'N200D8nh4ac1epshc1.0', 'all', - {'nucleoid': 'ndump', 'all': 'adump'}, id='HnsCyl'), -]) -def test_parser_maps_each_group_to_its_dump_keyword( - parser_class: type[ParserBase], artifact: str, group: str, - dump_keywords: dict[str, str]) -> None: - """Each group's dump keyword names the period of its own trajectory.""" - parser = parser_class(artifact, 'space', group) - - assert parser.dump_keywords == dump_keywords - - -@pytest.mark.parametrize('parser_class, artifact, group, attrs, expected', [ - pytest.param(HnsCyl, 'N200kbmm2r4.5nh4ac1lz75epshc1.nc1152', 'nucleoid', - ('eps_hc', 'ncrowd'), (1.0, 1152), id='HnsCyl'), -]) -def test_parser_reads_value_with_trailing_dot( - parser_class: type[ParserBase], artifact: str, group: str, - attrs: tuple[str, ...], expected: tuple[float, ...]) -> None: - """A value written with a trailing dot, such as ``1.``, parses.""" - parser = parser_class(artifact, 'ensemble_long', group) - - # float('1.') is exact. - assert attrgetter(*attrs)(parser) == pytest.approx(expected, rel=1e-12) - - -def test_declared_pattern_round_trips( - declared_case: tuple[type[ParserBase], Any, str, dict[str, Any]] -) -> None: - """ - A name built from a declared pattern parses back to the declared types - and conversions, so float keywords keep non-integer values (property). - """ - parser_class, lineage, name, expected = declared_case - - actual = attrgetter(*expected)( - parser_class(name, lineage, parser_class._groups[0])) - - # One multiplication and one addition in float64. - assert actual == pytest.approx(tuple(expected.values()), rel=1e-12) - assert tuple(map(type, actual)) == tuple(map(type, expected.values())) - - -# Error paths of parsing - - -@pytest.mark.parametrize('parser_class, artifact, group, keyword, attr', [ - pytest.param(HnsCyl, 'N200kbmm2r4.5nh4ac1lz75XX1.0nc1152', 'nucleoid', - 'epshc', 'eps_hc', id='HnsCyl-replaced'), - pytest.param(HnsCyl, '200kbmm2r4.5nh4ac1lz75epshc1.0nc1152', 'nucleoid', - 'N', 'nmon', id='HnsCyl-heading-digit'), -]) -def test_missing_keyword_raises_value_error( - parser_class: type[ParserBase], artifact: str, group: str, - keyword: str, attr: str) -> None: - """A name without a pattern keyword raises ValueError naming it.""" - message = (f"keyword '{keyword}' for attribute '{attr}' not found in " - f"'{artifact}' (lineage 'ensemble_long')") - - with pytest.raises(ValueError, match=re.escape(message)): - parser_class(artifact, 'ensemble_long', group) - - -@pytest.mark.parametrize('parser_class, artifact, group, keyword', [ - pytest.param(HnsCyl, 'N.kbmm2r4.5nh4ac1lz75epshc1.0nc1152', 'nucleoid', - 'N', id='HnsCyl'), -]) -def test_non_numeric_value_raises_value_error( - parser_class: type[ParserBase], artifact: str, group: str, - keyword: str) -> None: - """A keyword whose value is not a number raises ValueError naming it.""" - message = (f"value of keyword '{keyword}' in '{artifact}' " - "(lineage 'ensemble_long'): '.' is not a number") - - with pytest.raises(ValueError, match=re.escape(message)): - parser_class(artifact, 'ensemble_long', group) - - -def test_empty_artifact_raises_value_error(valid_case: ValidCase) -> None: - """An empty artifact name raises ValueError.""" - with pytest.raises(ValueError, - match="'artifact' cannot be an empty string."): - valid_case.parser_class('', valid_case.lineage, valid_case.group) - - -def test_invalid_group_raises_value_error(valid_case: ValidCase) -> None: - """A group the project does not have raises ValueError.""" - with pytest.raises(ValueError, match='is an invalid option.'): - valid_case.parser_class(valid_case.artifact, valid_case.lineage, - 'invalid') - - -def test_invalid_lineage_raises_value_error(valid_case: ValidCase) -> None: - """A lineage outside the five lineages raises ValueError.""" - with pytest.raises(ValueError, match='is an invalid option.'): - valid_case.parser_class(valid_case.artifact, - cast(LineageT, 'invalid'), - valid_case.group) - - -# Text representations - - -def test_str_summarizes_artifact(valid_case: ValidCase) -> None: - """`str` lists the name, geometry, group, lineage, topology, project.""" - parser = valid_case.parser_class(valid_case.artifact, valid_case.lineage, - valid_case.group) - - assert str(parser) == ( - f"Artifact:\n" - f" Name: '{valid_case.artifact}',\n" - f" Geometry: '{parser.geometry}',\n" - f" Group: '{valid_case.group}',\n" - f" Lineage: '{valid_case.lineage}',\n" - f" Topology: '{parser.topology}',\n" - f" Project: '{valid_case.parser_class.__name__}'") - - -def test_repr_names_artifact(valid_case: ValidCase) -> None: - """`repr` names the file, geometry, group, lineage, topology, project.""" - parser = valid_case.parser_class(valid_case.artifact, valid_case.lineage, - valid_case.group) - - assert repr(parser) == ( - f"Artifact(filename='{valid_case.artifact}', " - f"geometry='{parser.geometry}', group='{valid_case.group}', " - f"lineage='{valid_case.lineage}', topology='{parser.topology}', " - f"project='{valid_case.parser_class.__name__}')") - - -# Bulk values - - -@pytest.mark.parametrize('parser_class, artifact, suffix, number, size', [ - pytest.param(HnsCyl, 'N200kbmm2r4.5nh4ac1lz75epshc1.0nc1152', 'm', - 'nmon', 'dmon', id='HnsCyl-m'), - pytest.param(HnsCyl, 'N200kbmm2r4.5nh4ac1lz75epshc1.0nc1152', 'hns', - 'nhns', 'dhns', id='HnsCyl-hns'), - pytest.param(HnsCyl, 'N200kbmm2r4.5nh4ac1lz75epshc1.0nc1152', 'c', - 'ncrowd', 'dcrowd', id='HnsCyl-c'), -]) -def test_cylinder_bulk_values_match_utils( - parser_class: type[ParserBase], artifact: str, suffix: str, - number: str, size: str) -> None: - """Cylindrical bulk values are the cylinder functions of the values.""" - parser = parser_class(artifact, 'ensemble_long', parser_class._groups[0]) - n, d, lcyl, dcyl = attrgetter(number, size, 'lcyl', 'dcyl')(parser) - - actual = attrgetter(f'rho_bulk_{suffix}', f'phi_bulk_{suffix}')(parser) - - # The same functions on the same inputs; float64 round-off only. - assert actual == pytest.approx( - (number_density_cylinder(n, d, lcyl, dcyl, pbc=True), - volume_fraction_cylinder(n, d, lcyl, dcyl, pbc=True)), rel=1e-12) - - -# Lineage-name rules - - -@pytest.mark.parametrize('parser_class, group, artifact, ensemble', [ - pytest.param(HnsCyl, 'nucleoid', 'N200kbmm2r4.1nh4ac1lz75epshc1.0nc1152', - 'N200D7.2nh4ac1epshc1nc1152', id='HnsCyl-r4.1'), -]) -def test_short_name_prints_cylinder_size_from_r_as_decimal( - parser_class: type[ParserBase], group: str, artifact: str, - ensemble: str) -> None: - """The size D = 2r - 1 converted from a decimal r prints as the decimal - result in the ensemble name, so it matches the files named with it.""" - parser = parser_class(artifact, 'ensemble_long', group) - - assert getattr(parser, 'ensemble') == ensemble - - -@pytest.mark.parametrize('short_lineage', [ - pytest.param('ensemble', id='ensemble'), - pytest.param('space', id='space'), -]) -def test_short_name_reparses_to_itself( - valid_case: ValidCase, short_lineage: LineageT) -> None: - """A short name parsed at its own lineage rebuilds the same name.""" - parser = valid_case.parser_class(valid_case.artifact, valid_case.lineage, - valid_case.group) - name = getattr(parser, short_lineage) - - reparsed = valid_case.parser_class(name, short_lineage, valid_case.group) - - assert getattr(reparsed, short_lineage) == name - - -@pytest.mark.parametrize( - 'parser_class, group, child_lineage, child_name, parent_lineage', - [ - pytest.param(HnsCyl, 'all', 'segment', - 'N200kbmm2r4.5nh4ac1lz75epshc1.0nc1152ens1.j01.ring.all', - 'whole', id='HnsCyl-segment-whole'), - pytest.param(HnsCyl, 'all', 'whole', - 'N200kbmm2r4.5nh4ac1lz75epshc1.0nc1152ens1.ring.all', - 'ensemble_long', id='HnsCyl-whole-ensemble_long'), - ], -) -def test_parser_rebuilt_parent_name_keeps_attributes( - parser_class: type[ParserBase], group: str, child_lineage: LineageT, - child_name: str, parent_lineage: LineageT) -> None: - """A rebuilt parent name parses to its child's attribute values.""" - child = parser_class(child_name, child_lineage, group) - parent = parser_class(getattr(child, parent_lineage), parent_lineage, - group) - values = attrgetter(*parser_class._genealogy_attributes[parent_lineage]) - - # Both sides parse the same digits, so they agree to float64 round-off. - assert values(parent) == pytest.approx(values(child), rel=1e-12, - abs=1e-12) - - -# Declarations - - -def test_attributes_at_matches_parsed_instance( - parse_case: ParseCase, parsed: ParserBase) -> None: - """attributes_at names exactly the public attributes a parser sets.""" - public = {name for name in vars(parsed) if not name.startswith('_')} - - names = parse_case.parser_class.attributes_at(parse_case.lineage) - - assert names == public - - -# Generated names - - -@pytest.mark.parametrize('lineage', [ - pytest.param('segment', id='segment'), - pytest.param('whole', id='whole'), - pytest.param('ensemble_long', id='ensemble_long'), - pytest.param('ensemble', id='ensemble'), - pytest.param('space', id='space'), -]) -@settings(max_examples=50, deadline=None) -@given(data=st.data()) -def test_generated_name_parses_to_its_values_and_back( - lineage: LineageT, data: st.DataObject) -> None: - """A name built from HnsCyl's own pattern parses to its values and - rebuilds to itself: a round-trip property, with no reference value.""" - name, values = data.draw(lineage_names(HnsCyl, lineage)) - - parsed = HnsCyl(name, lineage, HnsCyl._groups[0]) - - assert getattr(parsed, lineage) == name - # The parser converts in exact decimal arithmetic, the expectation in - # float64: a few ulp apart; atol covers a value converted to zero. - expected = stored_values(HnsCyl, values) - assert {attr: getattr(parsed, attr) for attr in expected} == \ - pytest.approx(expected, rel=1e-12, abs=1e-12) diff --git a/projects/test_studies.py b/projects/test_studies.py deleted file mode 100644 index e6f292e7..00000000 --- a/projects/test_studies.py +++ /dev/null @@ -1,576 +0,0 @@ -"""Tests for the study specs in this folder. - -The expected values come from the research code's registry, with the -PolyPhys names ``phi_bulk_c`` for ``phi_c_bulk`` and ``rho_bulk_hns`` for -``rho_hns_bulk``. -""" -import importlib -from fnmatch import fnmatch -from types import SimpleNamespace -from typing import cast - -import numpy as np -import pytest - -from polyphys.analyze.pipeline import FociPairDistances, LocalDensities -from hns_cub.parser import HnsCub -from hns_cyl.parser import HnsCyl -from polyphys.manage.parser import ParserBase -from trans_foci_cub.parser import TransFociCub -from trans_foci_cyl.parser import TransFociCyl -from polyphys.spec import AnalysisTable, BinderSpec, ProbeSpec, ProjectSpec - - -def _spec(module: str) -> ProjectSpec: - """Return the ``SPEC`` of the study module `module`.""" - spec: ProjectSpec = importlib.import_module(module).SPEC - return spec - - -@pytest.mark.parametrize('module, name', [ - pytest.param('trans_foci_cyl', 'TransFociCyl', id='TransFociCyl'), - pytest.param('trans_foci_cub', 'TransFociCub', id='TransFociCub'), - pytest.param('hns_cub', 'HnsCub', id='HnsCub'), - pytest.param('hns_cyl', 'HnsCyl', id='HnsCyl'), -]) -def test_study_spec_uses_its_parser(module: str, name: str) -> None: - """Each study module's spec is built on that study's parser class.""" - assert _spec(module).name == name - - -@pytest.mark.parametrize('module', [ - pytest.param('trans_foci_cyl', id='TransFociCyl'), - pytest.param('trans_foci_cub', id='TransFociCub'), - pytest.param('hns_cub', id='HnsCub'), - pytest.param('hns_cyl', id='HnsCyl'), -]) -def test_study_names_only_attributes_its_parser_sets(module: str) -> None: - """Every attribute a spec names is set at ensemble_long by its parser.""" - spec = _spec(module) - named = {*spec.physical_attrs, *spec.species_sizes.values(), 'phi_bulk_c'} - - available = spec.parser.attributes_at('ensemble_long') - - assert named <= available - - -@pytest.mark.parametrize('module, group, divisor', [ - # The research code's registry, 'group' and 'divisor' of each entry. - pytest.param('trans_foci_cyl', 'bug', 0.025, id='TransFociCyl'), - pytest.param('trans_foci_cub', 'bug', 0.025, id='TransFociCub'), - pytest.param('hns_cub', 'nucleoid', 0.01, id='HnsCub'), - pytest.param('hns_cyl', 'nucleoid', 0.01, id='HnsCyl'), -]) -def test_study_group_and_divisor(module: str, group: str, - divisor: float) -> None: - """Each study has its polymer group and rounding step.""" - spec = _spec(module) - - # Literals compared with ==: the stored float is the literal itself. - assert (spec.group, spec.divisor) == (group, divisor) - - -@pytest.mark.parametrize('module, geometry, topology, directions', [ - # The research code's registry, 'geometry', 'topology', and 'directions'. - pytest.param('trans_foci_cyl', 'cylindrical', 'ring', ('r', 'z'), - id='TransFociCyl'), - pytest.param('trans_foci_cub', 'cubic', 'ring', ('r',), - id='TransFociCub'), - pytest.param('hns_cub', 'cubic', 'ring', ('r',), id='HnsCub'), - pytest.param('hns_cyl', 'cylindrical', 'ring', ('r', 'z'), id='HnsCyl'), -]) -def test_study_geometry_follows_parser(module: str, geometry: str, - topology: str, - directions: tuple[str, ...]) -> None: - """Geometry, topology, and profile directions match the old registry.""" - spec = _spec(module) - - assert (spec.geometry, spec.topology, spec.directions) == ( - geometry, topology, directions) - - -@pytest.mark.parametrize('module, space_pattern, space_hierarchy', [ - # The research code's registry, 'space_pat' and 'space_hierarchy'. - pytest.param('trans_foci_cyl', 'ns*nl*al*D*ac*', 'ns*', - id='TransFociCyl'), - pytest.param('trans_foci_cub', 'ns*nl*al*ac*', 'ns*', id='TransFociCub'), - pytest.param('hns_cub', 'N*kbmm*nh*ac*epshc*', 'N*', id='HnsCub'), - pytest.param('hns_cyl', 'N*D*nh*ac*epshc*', 'N*', id='HnsCyl'), -]) -def test_study_space_patterns(module: str, space_pattern: str, - space_hierarchy: str) -> None: - """The space glob patterns derived from the parser match the old ones.""" - spec = _spec(module) - - assert (spec.space_pattern, spec.space_hierarchy) == ( - space_pattern, space_hierarchy) - - -@pytest.mark.parametrize('module, hierarchy', [ - # The research code's registry, 'hierarchy'; TransFociCyl differs (see the - # next test). - pytest.param('trans_foci_cub', 'al*', id='TransFociCub'), - pytest.param('hns_cub', 'N*', id='HnsCub'), - pytest.param('hns_cyl', 'N*', id='HnsCyl'), -]) -def test_study_hierarchy(module: str, hierarchy: str) -> None: - """The ensemble_long glob prefix derived from the parser matches.""" - assert _spec(module).hierarchy == hierarchy - - -def test_trans_foci_cyl_hierarchy_matches_its_names() -> None: - """TransFociCyl's derived prefix epss* matches its ensemble_long names.""" - # A hand-written TransFociCyl ensemble_long name from test_parser.py; the - # research code's prefix was 'eps*', which matches the same names. - parser = TransFociCyl( - 'epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', 'ensemble_long', 'bug') - name = getattr(parser, 'ensemble_long') - - hierarchy = _spec('trans_foci_cyl').hierarchy - - assert (hierarchy, fnmatch(name, hierarchy)) == ('epss*', True) - - -@pytest.mark.parametrize('module, expected', [ - # The research code's registry, 'attributes', with PolyPhys names. - pytest.param('trans_foci_cyl', - ('space', 'ensemble_long', 'ensemble', 'nmon_small', - 'nmon_large', 'dmon_large', 'dcyl', 'dcrowd', 'phi_bulk_c'), - id='TransFociCyl'), - pytest.param('trans_foci_cub', - ('space', 'ensemble_long', 'ensemble', 'nmon_small', - 'nmon_large', 'dmon_large', 'dcrowd', 'phi_bulk_c'), - id='TransFociCub'), - pytest.param('hns_cub', - ('space', 'ensemble_long', 'ensemble', 'bend_mm', 'eps_hc', - 'nmon', 'nhns', 'dcrowd', 'phi_bulk_c', 'rho_bulk_hns'), - id='HnsCub'), - pytest.param('hns_cyl', - ('space', 'ensemble_long', 'ensemble', 'dcyl', 'nmon', - 'nhns', 'dcrowd', 'phi_bulk_c', 'rho_bulk_hns'), - id='HnsCyl'), -]) -def test_study_physical_attrs(module: str, expected: tuple[str, ...]) -> None: - """Each study adds its attributes as columns, in order.""" - assert _spec(module).physical_attrs == expected - - -@pytest.mark.parametrize('module, expected', [ - # The research code's registry, 'rhosPhisNormalizedScaled', in its order. - pytest.param('trans_foci_cyl', - [('Mon', 'dmon_small'), ('Crd', 'dcrowd'), - ('Foci', 'dmon_large')], - id='TransFociCyl'), - pytest.param('trans_foci_cub', - [('Mon', 'dmon_small'), ('Crd', 'dcrowd'), - ('Foci', 'dmon_large')], - id='TransFociCub'), - pytest.param('hns_cub', [('Mon', 'dmon'), ('Crd', 'dcrowd'), - ('Hns', 'dhns')], - id='HnsCub'), - pytest.param('hns_cyl', [('Mon', 'dmon'), ('Crd', 'dcrowd'), - ('Hns', 'dhns')], - id='HnsCyl'), -]) -def test_study_species_sizes(module: str, - expected: list[tuple[str, str]]) -> None: - """Each study maps its species labels to size attributes, in order.""" - assert list(_spec(module).species_sizes.items()) == expected - - -@pytest.mark.parametrize('module, expected', [ - # The research code's registry, 'time_varying_props', in its order. - # TransFociCyl adds 'transSizeTMon', which the registry omitted although - # the research probe writes it and the analysis table reads it (maintainer, - # 2026-11-03). - pytest.param('trans_foci_cyl', - ('asphericityTMon', 'fsdTMon', 'gyrTMon', 'shapeTMon', - 'transSizeTMon'), - id='TransFociCyl'), - pytest.param('trans_foci_cub', - ('asphericityTMon', 'gyrTMon', 'shapeTMon'), - id='TransFociCub'), - pytest.param('hns_cub', - ('asphericityTMon', 'gyrTMon', 'shapeTMon', - 'bondLengthVecMon', 'nBoundTHnsPatch', 'nFreeTHnsPatch', - 'nEngagedTHnsPatch', 'nFreeTHnsCore', 'nBridgeTHnsCore', - 'nDangleTHnsCore', 'nCisTHnsCore', 'nTransTHnsCore'), - id='HnsCub'), - pytest.param('hns_cyl', - ('asphericityTMon', 'fsdTMon', 'shapeTMon', 'gyrTMon', - 'transSizeTMon', 'bondLengthVecMon', 'nBoundTHnsPatch', - 'nFreeTHnsPatch', 'nEngagedTHnsPatch', 'nFreeTHnsCore', - 'nBridgeTHnsCore', 'nDangleTHnsCore', 'nCisTHnsCore', - 'nTransTHnsCore'), - id='HnsCyl'), -]) -def test_study_tseries_properties(module: str, - expected: tuple[str, ...]) -> None: - """Each study lists its time series, in order.""" - assert _spec(module).tseries_properties == expected - - -# The analysis tables - - -STUDIES = [ - pytest.param('trans_foci_cyl', id='TransFociCyl'), - pytest.param('trans_foci_cub', id='TransFociCub'), - pytest.param('hns_cub', id='HnsCub'), - pytest.param('hns_cyl', id='HnsCyl'), -] - - -def _analysis(module: str) -> tuple[AnalysisTable, ...]: - """Return the ``ANALYSIS`` tables of the study module `module`.""" - tables: tuple[AnalysisTable, ...] = importlib.import_module( - module).ANALYSIS - return tables - - -def _properties(module: str, group: str) -> set[str]: - """Return the property names of the study's table for `group`.""" - (table,) = [t for t in _analysis(module) if t.group == group] - return {prop.prop for prop in table.properties} - - -@pytest.mark.parametrize('module', STUDIES) -def test_study_has_one_table_per_group(module: str) -> None: - """Each study has a table for its polymer group, as wholes with stamps, - and one for 'all', as segments without stamps (the research code's - ``is_segment`` and ``has_stamp``).""" - spec = _spec(module) - polymer, everything = _analysis(module) - - assert (polymer.group, polymer.lineage, polymer.stamps) == ( - spec.group, 'whole', True) - assert (everything.group, everything.lineage, everything.stamps) == ( - 'all', 'segment', False) - - -@pytest.mark.parametrize('module', STUDIES) -def test_study_density_species_have_sizes(module: str) -> None: - """Every species whose density profiles a table derives has a size - attribute in the spec, which the hook reads.""" - spec = _spec(module) - - for table in _analysis(module): - for prop in table.properties: - if isinstance(prop.post_hook, LocalDensities): - assert prop.species in spec.species_sizes - - -def _planar(species: tuple[str, ...]) -> set[str]: - """The planar histograms and edges of the 'all' group.""" - return ({f'{plane}Hist{sp}' for plane in ('xy', 'xz', 'yz') - for sp in species} | {'xEdge', 'yEdge', 'zEdge'}) - - -@pytest.mark.parametrize('module, group, expected', [ - # The research code's analysis tables, with each artifact listed once. - pytest.param('trans_foci_cub', 'bug', - {'bondsHistTFoci', 'clustersHistTFoci', 'distMatTFoci', - 'principalTMon', 'gyrTMon', 'shapeTMon', - 'asphericityTMon'}, - id='TransFociCub-bug'), - pytest.param('trans_foci_cub', 'all', - {'rHistCrd', 'rHistMon', 'rHistFoci', 'rHistDna'} - | _planar(('Crd', 'Mon', 'Dna', 'Foci')), - id='TransFociCub-all'), - pytest.param('trans_foci_cyl', 'bug', - {'bondsHistTFoci', 'clustersHistTFoci', 'distMatTFoci', - 'principalTMon', 'fsdTMon', 'gyrTMon', 'transSizeTMon', - 'shapeTMon', 'asphericityTMon'}, - id='TransFociCyl-bug'), - pytest.param('trans_foci_cyl', 'all', - {f'{d}Hist{sp}' for d in ('r', 'z') - for sp in ('Crd', 'Mon', 'Foci', 'Dna')} - | {f'thetaHist{sp}' for sp in ('Crd', 'Mon', 'Dna', 'Foci')} - | _planar(('Crd', 'Mon', 'Dna', 'Foci')), - id='TransFociCyl-all'), - pytest.param('hns_cub', 'nucleoid', - {'principalTMon', 'bondsHistTHnsCore', - 'bondsHistDangleTHnsCore', 'bondsHistBridgeTHnsCore', - 'bondsHistDirDepTHnsCore', 'clustersHistTHnsCore', - 'clustersHistDangleTHnsCore', 'clustersHistBridgeTHnsCore', - 'clustersHistDirDepTHnsCore', 'loopLengthHistMon', - 'bondCosineCorrVecMon', 'bondLengthVecMon', - 'nBoundTHnsPatch', 'nFreeTHnsPatch', 'nEngagedTHnsPatch', - 'nFreeTHnsCore', 'nBridgeTHnsCore', 'nDangleTHnsCore', - 'nCisTHnsCore', 'nTransTHnsCore', 'gyrTMon', 'shapeTMon', - 'asphericityTMon'}, - id='HnsCub-nucleoid'), - pytest.param('hns_cub', 'all', - {'rHistCrd', 'rHistMon', 'rHistHns'} - | _planar(('Crd', 'Mon', 'Hns')), - id='HnsCub-all'), - pytest.param('hns_cyl', 'nucleoid', - {'principalTMon', 'loopLengthHistMon', - 'bondCosineCorrVecMon', 'bondLengthVecMon', - 'nBoundTHnsPatch', 'nFreeTHnsPatch', 'nEngagedTHnsPatch', - 'nFreeTHnsCore', 'nBridgeTHnsCore', 'nDangleTHnsCore', - 'nCisTHnsCore', 'nTransTHnsCore', 'fsdTMon', 'gyrTMon', - 'transSizeTMon', 'shapeTMon', 'asphericityTMon'}, - id='HnsCyl-nucleoid'), - pytest.param('hns_cyl', 'all', - {f'{d}Hist{sp}' for d in ('r', 'z', 'theta') - for sp in ('Crd', 'Mon', 'Hns')} - | _planar(('Crd', 'Mon', 'Hns')), - id='HnsCyl-all'), -]) -def test_study_properties_match_the_research_tables( - module: str, group: str, expected: set[str]) -> None: - """Each table reduces the artifacts the research code's tables named.""" - assert _properties(module, group) == expected - - -@pytest.mark.parametrize('module', [ - pytest.param('trans_foci_cyl', id='TransFociCyl'), - pytest.param('trans_foci_cub', id='TransFociCub'), -]) -def test_foci_pair_distance_bound_is_the_half_contour(module: str) -> None: - """The foci pair histograms of the TransFoci studies bin at half the - small-monomer diameter up to half the ring contour at the FENE - maximum: 405 monomers, bonds up to ``1.5 (a_l + a_s) / 2``.""" - (table,) = [t for t in _analysis(module) if t.group == 'bug'] - (prop,) = [p for p in table.properties if p.prop == 'distMatTFoci'] - hook = prop.post_hook - assert isinstance(hook, FociPairDistances) - info = TransFociCub( - 'al3nl5ml27ns400ac1nc8340l50dt0.005bdump2000adump5000ens1', - 'whole', 'bug') - - assert callable(hook.n_monomers) and hook.n_monomers(info) == 405 - assert callable(hook.bin_size) and hook.bin_size(info) == 0.5 - # 202 bonds along the shorter arc, each at most 1.5 * (3 + 1) / 2 = 3. - assert callable(hook.r_max) and hook.r_max(info) == 202 * 3.0 - - -# The probe specs - - -def _probe(module: str) -> ProbeSpec: - """Return the ``PROBE`` of the study module `module`.""" - probe: ProbeSpec = importlib.import_module(module).PROBE - return probe - - -@pytest.mark.parametrize('module', STUDIES) -def test_study_probe_is_built_on_its_spec(module: str) -> None: - """Each study's probe spec reads its parser from that study's SPEC.""" - assert _probe(module).project is _spec(module) - - -@pytest.mark.parametrize('module, selections', [ - # The research probe: the select_atoms calls of each study's - # polymer-group probe, 'resid 1' for the chain and 'type 2' for the foci or - # the H-NS patches, then those of its all-group probe, 'resid 0' for the - # crowders, 'type 1' for the small monomers, and 'type 3' for the H-NS - # cores. - pytest.param('trans_foci_cyl', [('Mon', 'resid 1'), ('Foci', 'type 2'), - ('Crd', 'resid 0'), ('Dna', 'type 1')], - id='TransFociCyl'), - pytest.param('trans_foci_cub', [('Mon', 'resid 1'), ('Foci', 'type 2'), - ('Crd', 'resid 0'), ('Dna', 'type 1')], - id='TransFociCub'), - pytest.param('hns_cub', [('Mon', 'resid 1'), ('HnsPatch', 'type 2'), - ('Crd', 'resid 0'), ('Hns', 'type 3')], - id='HnsCub'), - pytest.param('hns_cyl', [('Mon', 'resid 1'), ('HnsPatch', 'type 2'), - ('Crd', 'resid 0'), ('Hns', 'type 3')], - id='HnsCyl'), -]) -def test_study_probe_selections(module: str, - selections: list[tuple[str, str]]) -> None: - """Each study selects its chain, its second species, and the species of - its all-group histograms as the research probes did, with the chain - labeled Mon.""" - probe = _probe(module) - - assert (list(probe.selections.items()), probe.chain) == ( - selections, 'Mon') - - -@pytest.mark.parametrize('module, observables', [ - # The research probe: the chain-size measures of each study's - # per-frame loop, in the order of the study's time series, with the - # principal axes last. - pytest.param('trans_foci_cyl', - ('asphericityT', 'fsdT', 'gyrT', 'shapeT', 'transSizeT', - 'principalT'), - id='TransFociCyl'), - pytest.param('trans_foci_cub', - ('asphericityT', 'gyrT', 'shapeT', 'principalT'), - id='TransFociCub'), - pytest.param('hns_cub', ('asphericityT', 'gyrT', 'shapeT', 'principalT'), - id='HnsCub'), - pytest.param('hns_cyl', - ('asphericityT', 'fsdT', 'shapeT', 'gyrT', 'transSizeT', - 'principalT'), - id='HnsCyl'), -]) -def test_study_probe_observables(module: str, - observables: tuple[str, ...]) -> None: - """Each study measures the chain-size observables of its research - probe.""" - assert _probe(module).observables == observables - - -@pytest.mark.parametrize('module, unwrap', [ - # The research probe: the unwrap argument of bug.asphericity in - # each study's probe; the cylinders are periodic along z only. - pytest.param('trans_foci_cyl', False, id='TransFociCyl'), - pytest.param('trans_foci_cub', True, id='TransFociCub'), - pytest.param('hns_cub', True, id='HnsCub'), - pytest.param('hns_cyl', False, id='HnsCyl'), -]) -def test_study_probe_unwraps_in_a_cube_only(module: str, - unwrap: bool) -> None: - """The cubic studies unwrap the chain for its asphericity; the - cylindrical ones do not.""" - assert _probe(module).unwrap is unwrap - - -@pytest.mark.parametrize('module, species, keys', [ - # The research probe: the species each study's all-group probe - # binned, as its analysis table reads them, and the directions of its - # geometry. - pytest.param('trans_foci_cyl', ('Crd', 'Mon', 'Dna', 'Foci'), - ['r', 'z', 'theta', 'x', 'y'], id='TransFociCyl'), - pytest.param('trans_foci_cub', ('Crd', 'Mon', 'Dna', 'Foci'), - ['r', 'x', 'y', 'z'], id='TransFociCub'), - pytest.param('hns_cub', ('Crd', 'Mon', 'Hns'), ['r', 'x', 'y', 'z'], - id='HnsCub'), - pytest.param('hns_cyl', ('Crd', 'Mon', 'Hns'), - ['r', 'z', 'theta', 'x', 'y'], id='HnsCyl'), -]) -def test_study_probe_histogram_fields(module: str, species: tuple[str, ...], - keys: list[str]) -> None: - """Each study bins the species its analysis table reads, with a bin size - along every direction its geometry needs, blocks of 100 frames, and the - frame recentered by half the box.""" - probe = _probe(module) - - assert probe.histogram_species == species - assert list(probe.bin_sizes) == keys - assert (probe.block_frames, probe.recenter) == (100, 'half') - - -@pytest.mark.parametrize('module, info, r, cartesian, theta', [ - # The research probe, the bin_edges of each study's all-group - # probe: a tenth of the smallest diameter along r, half of it along the - # Cartesian axes of a cube or along z of a cylinder, pi/36 along theta; for - # the Hns studies the radial minimum includes the H-NS core, as the - # maintainer settled on 2026-11-04. The wholes: small monomers of 1 with - # crowders of 1.5 or 3 (the minimum is the monomer), and a crowder of 0.5 - # (the minimum is the crowder). - pytest.param('trans_foci_cyl', TransFociCyl( - 'epss5epsl5r5al2nl3ml8ns7ac1.5nc0lz10dt0.005bdump1adump1ens1', - 'whole', 'all'), 0.1, 0.5, np.pi / 36, id='TransFociCyl'), - pytest.param('trans_foci_cub', TransFociCub( - 'al3nl5ml27ns400ac0.5nc8340l50dt0.005bdump2000adump5000ens1', - 'whole', 'all'), 0.05, 0.25, None, id='TransFociCub'), - pytest.param('hns_cub', HnsCub( - 'N200kbmm2.5nh8ac0.5l25.0epshc1.5nc0ens1.ring', 'whole', 'all'), - 0.05, 0.25, None, id='HnsCub'), - pytest.param('hns_cyl', HnsCyl( - 'N200kbmm2.0r4.5nh8ac3.0lz50epshc1.0nc0ens1', 'whole', 'all'), - 0.1, 0.5, np.pi / 36, id='HnsCyl'), -]) -def test_study_probe_bin_sizes_follow_the_whole( - module: str, info: ParserBase, r: float, cartesian: float, - theta: float | None) -> None: - """Each study's bin sizes resolve for a whole to a tenth of its smallest - diameter along r and the transverse axes of a cylinder, half of it along - the other Cartesian axes, and pi/36 along theta.""" - probe = _probe(module) - - sizes = {key: ProbeSpec.resolve(value, info) - for key, value in probe.bin_sizes.items()} - - assert sizes['r'] == pytest.approx(r) - if probe.geometry == 'cylindrical': - assert sizes['x'] == sizes['y'] == pytest.approx(r) - assert sizes['z'] == pytest.approx(cartesian) - assert sizes['theta'] == pytest.approx(theta) - else: - assert sizes['x'] == sizes['y'] == sizes['z'] == pytest.approx( - cartesian) - - -@pytest.mark.parametrize('module', [ - pytest.param('hns_cub', id='HnsCub'), - pytest.param('hns_cyl', id='HnsCyl'), -]) -def test_hns_radial_bin_includes_the_core_diameter(module: str) -> None: - """An H-NS core smaller than the monomer and the crowder sets the radial - bin size of both Hns studies, which the research code did for the cube - only; the Cartesian bins ignore the core as the research code did.""" - # The parsers fix dhns at 1, so a stand-in with the three diameters - # tells the formulas apart: dhns = 0.5 is the minimum for r alone. - info = cast(ParserBase, SimpleNamespace(name='stand-in', dmon=1.0, - dhns=0.5, dcrowd=2.0)) - probe = _probe(module) - - assert ProbeSpec.resolve(probe.bin_sizes['r'], info) == pytest.approx( - 0.05) - assert ProbeSpec.resolve(probe.bin_sizes['x'], info) == pytest.approx( - 0.1 if probe.geometry == 'cylindrical' else 0.5) - - -@pytest.mark.parametrize('module, pair, cutoff', [ - # The research probe: the foci of the TransFoci studies, with - # cluster_cutoff = dmon_large + dcrowd; the Hns studies measure no pairs. - pytest.param('trans_foci_cyl', 'Foci', 3.5, id='TransFociCyl'), - pytest.param('trans_foci_cub', 'Foci', 3.5, id='TransFociCub'), - pytest.param('hns_cub', None, None, id='HnsCub'), - pytest.param('hns_cyl', None, None, id='HnsCyl'), -]) -def test_study_probe_pair_fields(module: str, pair: str | None, - cutoff: float | None) -> None: - """The TransFoci studies probe the foci pairs with the research cutoff, - the large-monomer plus the crowder diameter, 2 + 1.5 here, and the - minimum image; the Hns studies probe no pairs.""" - probe = _probe(module) - info = TransFociCyl( - 'epss5epsl5r5al2nl3ml8ns7ac1.5nc0lz10dt0.005bdump1adump1ens1', - 'whole', 'bug') - - assert probe.pair_species == pair - assert probe.minimum_image is True - if cutoff is None: - assert probe.contact_cutoff is None - else: - assert probe.contact_cutoff is not None - assert ProbeSpec.resolve(probe.contact_cutoff, info) == pytest.approx( - cutoff) - - -@pytest.mark.parametrize('module, binders', [ - pytest.param('trans_foci_cyl', None, id='TransFociCyl'), - pytest.param('trans_foci_cub', None, id='TransFociCub'), - # The research probe, the H-NS binding blocks: the distance - # matrices saved per frame. The cis threshold is 3, the maintainer's - # definition (thesis, chapter 5), chosen on 2026-11-08 over the research - # probe's 4. The cutoff and the inclusive comparison are the thesis's - # binding criterion, r <= r_cut,mp = 0.685 (Section 5.3, Table 5.1), chosen - # by the maintainer on 2026-11-04 over the research probe's 0.661 with a - # strict comparison. - pytest.param('hns_cub', - BinderSpec('HnsPatch', 'HnsCore', cutoff=0.685, - inclusive=True, cis_threshold=3, - patches_per_binder=2, store_distances=True), - id='HnsCub'), - pytest.param('hns_cyl', - BinderSpec('HnsPatch', 'HnsCore', cutoff=0.685, - inclusive=True, cis_threshold=3, - patches_per_binder=2, store_distances=True), - id='HnsCyl'), -]) -def test_study_probe_binders(module: str, - binders: BinderSpec | None) -> None: - """The H-NS studies count their two-patch binders at the published - cutoff with the research cis threshold and keep the distance matrices; - the foci studies have no binders.""" - assert _probe(module).binders == binders diff --git a/projects/trans_foci_cub/__init__.py b/projects/trans_foci_cub/__init__.py deleted file mode 100644 index f6bdcea8..00000000 --- a/projects/trans_foci_cub/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -""" -The *TransFociCub* study: ``SPEC`` is its -:class:`~polyphys.spec.ProjectSpec`, ``PROBE`` its -:class:`~polyphys.spec.ProbeSpec`, and ``ANALYSIS`` its -:class:`~polyphys.spec.AnalysisTable` per particle group, and -``TransFociCub`` its parser. -""" -from .parser import TransFociCub -from .spec import ANALYSIS, PROBE, SPEC - -__all__ = ["ANALYSIS", "PROBE", "SPEC", "TransFociCub"] diff --git a/projects/trans_foci_cub/parser.py b/projects/trans_foci_cub/parser.py deleted file mode 100644 index 5618430e..00000000 --- a/projects/trans_foci_cub/parser.py +++ /dev/null @@ -1,157 +0,0 @@ -""" -The parser of the *TransFociCub* study: a ring of small and large monomers -among crowders in a cube. - -.. versionchanged:: 1.0.0 - Moved here from :mod:`polyphys.manage.parser`, which ships no study. -""" -from polyphys.manage.parser import KeywordSpec, ParserBase, Species - -__all__ = ["TransFociCub"] - - -class TransFociCub(ParserBase): - """ - Parse an artifact name of the *TransFociCub* project: a ring of small and - large monomers among crowders in a periodic cube. - - Each lineage has its own name pattern; ``#`` stands for a value: - - - ``segment``: al#nl#ml#ns#ac#nc#l#dt#bdump#adump#ens#.j#.ring - - ``whole``: al#nl#ml#ns#ac#nc#l#dt#bdump#adump#ens#.ring - - ``ensemble_long``: al#nl#ml#ns#ac#nc#l#dt#bdump#adump# - - ``ensemble``: ns#nl#al#ac#nc# - - ``space``: ns#nl#al#ac# - - Parameters - ---------- - artifact : Pathish - Name to be parsed, or a file path with ``ispath=True``. - lineage : {'segment', 'whole', 'ensemble_long', 'ensemble', 'space'} - Lineage of the artifact. - group : {'bug', 'all'} - Particle group: ``'bug'`` for the ring, ``'all'`` for every particle. - ispath : bool, default False - Whether `artifact` is a file path. - - Attributes - ---------- - dmon_small : int - Diameter of a small monomer, fixed at 1; not in the name. - nmon_small : int - Number of small monomers; keyword ``ns``. - mmon_small : int - Mass of a small monomer, fixed at 1; not in the name. - dmon_large : float - Diameter of a large monomer; keyword ``al``. - nmon_large : int - Number of large monomers; keyword ``nl``. - mmon_large : float - Mass of a large monomer; keyword ``ml``. - dcrowd : float - Crowder diameter; keyword ``ac``. - ncrowd : int - Number of crowders; keyword ``nc``. - mcrowd : float - Crowder mass, :math:`m_c = d_c^3`, at ``segment``, ``whole``, and - ``ensemble_long``; ``-1``, a placeholder, at ``ensemble`` and - ``space``. - lcube : float - Side of the cube, :math:`L = 2 l`, from the half-side :math:`l` - (keyword ``l``). - dt : float - Simulation time step; keyword ``dt``. - bdump : int - Dump period of the ``bug`` trajectory, in time steps; keyword - ``bdump``. - adump : int - Dump period of the ``all`` trajectory, in time steps; keyword - ``adump``. - ensemble_id : int - Index of the whole in its ensemble; keyword ``ens``. - segment_id : int - Index of the segment in its whole, zero-padded in the name; keyword - ``j``. - rho_bulk_m_small, phi_bulk_m_small : float - Bulk number density and volume fraction of small monomers. - rho_bulk_m_large, phi_bulk_m_large : float - Bulk number density and volume fraction of large monomers. - rho_bulk_m, phi_bulk_m : float - Bulk number density and volume fraction of all monomers: the sums of - the small and large values. - rho_bulk_c, phi_bulk_c : float - Bulk number density and volume fraction of crowders. - - Notes - ----- - The bulk values exist only at ``segment``, ``whole``, and - ``ensemble_long``. The lineage names are documented in - :class:`~polyphys.manage.parser.ParserBase`. - - The mass density is uniform across all species, so a species whose mass - is not in the name has the mass :math:`m_i = d_i^3`, where :math:`d_i` is - its diameter. - - The simulation design values that the name does not carry, such as - the interaction strengths, the bonds, and the thermostat, are listed - in :doc:`/design`. - - Examples - -------- - >>> artifact = TransFociCub( - ... 'al5.0nl5ml125.0ns400ac1.0nc0l100.0dt0.005bdump2000adump5000.ring', - ... 'ensemble_long', - ... 'all' - ... ) - >>> print(artifact.ncrowd) - 0 - """ - _geometry = 'cubic' - _topology = 'ring' - _groups = ['bug', 'all'] - _dump_keywords = {'bug': 'bdump', 'all': 'adump'} - _keywords = { - 'al': KeywordSpec('dmon_large', float), - 'nl': KeywordSpec('nmon_large', int), - 'ml': KeywordSpec('mmon_large', float), - 'ns': KeywordSpec('nmon_small', int), - 'ac': KeywordSpec('dcrowd', float), - 'nc': KeywordSpec('ncrowd', int), - 'l': KeywordSpec('lcube', float, scale=2.0), - 'dt': KeywordSpec('dt', float), - 'bdump': KeywordSpec('bdump', int), - 'adump': KeywordSpec('adump', int), - 'ens': KeywordSpec('ensemble_id', int), - 'j': KeywordSpec('segment_id', int), - } - _patterns = { - 'segment': ('al', 'nl', 'ml', 'ns', 'ac', 'nc', 'l', 'dt', 'bdump', - 'adump', 'ens', 'j'), - 'whole': ('al', 'nl', 'ml', 'ns', 'ac', 'nc', 'l', 'dt', 'bdump', - 'adump', 'ens'), - 'ensemble_long': ('al', 'nl', 'ml', 'ns', 'ac', 'nc', 'l', 'dt', - 'bdump', 'adump'), - 'ensemble': ('ns', 'nl', 'al', 'ac', 'nc'), - 'space': ('ns', 'nl', 'al', 'ac'), - } - _defaults = {'dmon_small': 1, 'mmon_small': 1, 'mcrowd': -1} - _species = (Species('m_small', 'nmon_small', 'dmon_small'), - Species('m_large', 'nmon_large', 'dmon_large'), - Species('c', 'ncrowd', 'dcrowd')) - _totals = {'m': ('m_small', 'm_large')} - _masses = {'mcrowd': 'dcrowd'} - _project_attributes = { - 'segment': ['dmon_small', 'mmon_small', 'mcrowd', 'phi_bulk_m_small', - 'rho_bulk_m_small', 'phi_bulk_m_large', 'rho_bulk_m_large', - 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', 'rho_bulk_c'], - 'whole': ['dmon_small', 'mmon_small', 'mcrowd', 'phi_bulk_m_small', - 'rho_bulk_m_small', 'phi_bulk_m_large', 'rho_bulk_m_large', - 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', 'rho_bulk_c'], - 'ensemble_long': ['dmon_small', 'mmon_small', 'mcrowd', - 'phi_bulk_m_small', 'rho_bulk_m_small', - 'phi_bulk_m_large', 'rho_bulk_m_large', - 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', - 'rho_bulk_c'], - 'ensemble': ['dmon_small', 'mmon_small', 'mcrowd'], - 'space': ['dmon_small', 'mmon_small', 'mcrowd'] - } diff --git a/projects/trans_foci_cub/spec.py b/projects/trans_foci_cub/spec.py deleted file mode 100644 index 9ffd2a1f..00000000 --- a/projects/trans_foci_cub/spec.py +++ /dev/null @@ -1,146 +0,0 @@ -""" -The *TransFociCub* study: a ring of small and large monomers among crowders in -a periodic cube. ``SPEC`` is its -:class:`~polyphys.spec.ProjectSpec`, ``PROBE`` its -:class:`~polyphys.spec.ProbeSpec`, and ``ANALYSIS`` its -:class:`~polyphys.spec.AnalysisTable` per particle group. - -Values from the research code's registry, with the PolyPhys attribute names -``phi_bulk_c`` for ``phi_c_bulk``, and from its analysis tables, with each -artifact listed once. -""" -from polyphys.analyze.pipeline import ( - Autocorrelation, - FociPairDistances, - LocalDensities, - TimeAverage, - half_contour, -) -from polyphys.manage.parser import ParserBase -from polyphys.spec import ( - AnalysisTable, - ProbeSpec, - ProjectSpec, - edge, - histogram, - histogram_2d, - matrix_tseries, - tseries, -) - -from .parser import TransFociCub - -SPEC = ProjectSpec( - parser=TransFociCub, - group='bug', - divisor=0.025, - crowder_attr='phi_bulk_c', - physical_attrs=('space', 'ensemble_long', 'ensemble', 'nmon_small', - 'nmon_large', 'dmon_large', 'dcrowd', 'phi_bulk_c'), - species_sizes={'Mon': 'dmon_small', 'Crd': 'dcrowd', - 'Foci': 'dmon_large'}, - tseries_properties=('asphericityTMon', 'gyrTMon', 'shapeTMon'), -) - - -def _r_bin(info: ParserBase) -> float: - """A tenth of the smaller of the small-monomer and crowder diameters: - the bin size along r (the research code's ``trans_foci_cub_all``, - ``bin_edges``).""" - return 0.1 * min(float(getattr(info, 'dmon_small')), - float(getattr(info, 'dcrowd'))) - - -def _l_bin(info: ParserBase) -> float: - """Half the smaller of the small-monomer and crowder diameters: the bin - size along x, y, and z (the research code's ``trans_foci_cub_all``, - ``bin_edges``).""" - return 0.5 * min(float(getattr(info, 'dmon_small')), - float(getattr(info, 'dcrowd'))) - - -def _contact_cutoff(info: ParserBase) -> float: - """The large-monomer diameter plus the crowder diameter: two foci are in - direct contact when a crowder no longer fits between them (the research - code's ``trans_foci_cub_bug``, ``cluster_cutoff``).""" - return float(getattr(info, 'dmon_large')) + float(getattr(info, 'dcrowd')) - - -# The probe of the polymer group: the selections, the chain-size observables -# of its per-frame loop, and the unwrap convention of its asphericity (the -# research code's ``trans_foci_cub_bug``); the foci pair distances of the -# same loop; and the histograms of the all group (``trans_foci_cub_all``): -# its selections, bin sizes, and species. The research all-group probe -# selected the crowders, the small monomers, and the foci; 'Mon', the whole -# ring, is added because the analysis table below reads its histograms. The -# block length and the recentering are the probe guide's ("The spatial -# histograms" and "Recentering a frame"). -PROBE = ProbeSpec( - project=SPEC, - selections={'Mon': 'resid 1', 'Foci': 'type 2', 'Crd': 'resid 0', - 'Dna': 'type 1'}, - chain='Mon', - observables=('asphericityT', 'gyrT', 'shapeT', 'principalT'), - unwrap=True, - bin_sizes={'r': _r_bin, 'x': _l_bin, 'y': _l_bin, 'z': _l_bin}, - histogram_species=('Crd', 'Mon', 'Dna', 'Foci'), - block_frames=100, - recenter='half', - pair_species='Foci', - contact_cutoff=_contact_cutoff, - minimum_image=True, -) - -# The largest lag of the autocorrelation functions, in frames (the research -# code's ``analyze_measures``, ``nlags``). -NLAGS = 7000 - - -def _n_monomers(info: ParserBase) -> int: - """The monomers on the ring: the small ones and the large ones.""" - return int(getattr(info, 'nmon_small') + getattr(info, 'nmon_large')) - - -def _bin_size(info: ParserBase) -> float: - """Half the small-monomer diameter (the research code's - ``whole_dist_mat_foci``).""" - return 0.5 * float(getattr(info, 'dmon_small')) - - -def _r_max(info: ParserBase) -> float: - """Half the ring contour at the FENE maximum ``1.5 sigma_ls``, with - ``sigma_ls`` the large-small collision diameter: the largest bond.""" - sigma_ls = 0.5 * (float(getattr(info, 'dmon_large')) - + float(getattr(info, 'dmon_small'))) - return half_contour(_n_monomers(info), 1.5 * sigma_ls) - - -ANALYSIS = ( - AnalysisTable(group='bug', lineage='whole', stamps=True, properties=( - matrix_tseries('bondsHistT', 'Foci', - post_hook=TimeAverage('bondsHist')), - matrix_tseries('clustersHistT', 'Foci', - post_hook=TimeAverage('clustersHist', - drop_first_column=True)), - matrix_tseries('distMatT', 'Foci', - post_hook=FociPairDistances( - bin_size=_bin_size, r_max=_r_max, - n_monomers=_n_monomers)), - matrix_tseries('principalT', 'Mon'), - tseries('gyrT', 'Mon', post_hook=Autocorrelation(NLAGS)), - tseries('shapeT', 'Mon', post_hook=Autocorrelation(NLAGS)), - tseries('asphericityT', 'Mon', post_hook=Autocorrelation(NLAGS)), - )), - AnalysisTable(group='all', lineage='segment', stamps=False, properties=( - histogram('r', 'Crd', post_hook=LocalDensities()), - histogram('r', 'Mon', post_hook=LocalDensities()), - histogram('r', 'Foci', post_hook=LocalDensities()), - histogram('r', 'Dna'), - *(histogram_2d(plane, species) - for plane in ('xy', 'xz', 'yz') - for species in ('Crd', 'Mon', 'Dna', 'Foci')), - edge('x'), - edge('y'), - edge('z'), - )), -) diff --git a/projects/trans_foci_cub/test_parser.py b/projects/trans_foci_cub/test_parser.py deleted file mode 100644 index af50250c..00000000 --- a/projects/trans_foci_cub/test_parser.py +++ /dev/null @@ -1,544 +0,0 @@ -"""Tests for the *TransFociCub* parser, :mod:`trans_foci_cub.parser`.""" -import os -import re -from collections.abc import Callable -from operator import attrgetter -from pathlib import Path -from typing import Any, NamedTuple, cast - -import pytest -from hypothesis import given, settings -from hypothesis import strategies as st - -from polyphys.manage.parser import ParserBase -from polyphys.manage.types import LineageT -from polyphys.tests.strategies import lineage_names, stored_values -from polyphys.manage.utils import number_density_cube, volume_fraction_cube -from trans_foci_cub.parser import TransFociCub - - -class ParseCase(NamedTuple): - """A hand-written artifact name and what its parser must give.""" - parser_class: type[ParserBase] - lineage: Any - group: Any - artifact: str - name: str - values: dict[str, Any] - physical: list[str] - - -class ValidCase(NamedTuple): - """A valid ``ensemble_long`` artifact name for one parser class.""" - parser_class: type[ParserBase] - artifact: str - lineage: Any - group: Any - - -@pytest.fixture(params=[ - pytest.param( - ParseCase( - TransFociCub, 'segment', 'all', - artifact='al5nl5ml125ns400ac1nc154699l30dt0.005bdump2000adump5000' - 'ens5.j13.ring.all', - name='al5nl5ml125ns400ac1nc154699l30dt0.005bdump2000adump5000' - 'ens5.j13.ring', - values=dict(dmon_large=5.0, nmon_large=5, mmon_large=125.0, - nmon_small=400, dcrowd=1.0, ncrowd=154699, lcube=60.0, - dt=0.005, bdump=2000, adump=5000, ensemble_id=5, - segment_id=13), - physical=['dmon_small', 'mmon_small', 'mcrowd', 'phi_bulk_m_small', - 'rho_bulk_m_small', 'phi_bulk_m_large', - 'rho_bulk_m_large', 'phi_bulk_m', 'rho_bulk_m', - 'phi_bulk_c', 'rho_bulk_c']), - id='TransFociCub-segment'), - pytest.param( - ParseCase( - TransFociCub, 'whole', 'all', - artifact='al5nl5ml125ns400ac1nc154699l30dt0.005bdump2000adump5000' - 'ens5.ring.all', - name='al5nl5ml125ns400ac1nc154699l30dt0.005bdump2000adump5000' - 'ens5.ring', - values=dict(dmon_large=5.0, nmon_large=5, mmon_large=125.0, - nmon_small=400, dcrowd=1.0, ncrowd=154699, lcube=60.0, - dt=0.005, bdump=2000, adump=5000, ensemble_id=5), - physical=['dmon_small', 'mmon_small', 'mcrowd', 'phi_bulk_m_small', - 'rho_bulk_m_small', 'phi_bulk_m_large', - 'rho_bulk_m_large', 'phi_bulk_m', 'rho_bulk_m', - 'phi_bulk_c', 'rho_bulk_c']), - id='TransFociCub-whole'), - pytest.param( - ParseCase( - TransFociCub, 'ensemble_long', 'all', - artifact='al5nl5ml125ns400ac1nc154699l30dt0.005bdump2000adump5000', - name='al5nl5ml125ns400ac1nc154699l30dt0.005bdump2000adump5000', - values=dict(dmon_large=5.0, nmon_large=5, mmon_large=125.0, - nmon_small=400, dcrowd=1.0, ncrowd=154699, lcube=60.0, - dt=0.005, bdump=2000, adump=5000), - physical=['dmon_small', 'mmon_small', 'mcrowd', 'phi_bulk_m_small', - 'rho_bulk_m_small', 'phi_bulk_m_large', - 'rho_bulk_m_large', 'phi_bulk_m', 'rho_bulk_m', - 'phi_bulk_c', 'rho_bulk_c']), - id='TransFociCub-ensemble_long'), - pytest.param( - ParseCase( - TransFociCub, 'ensemble', 'all', - artifact='ns400nl5al5ac1nc154699', - name='ns400nl5al5ac1nc154699', - values=dict(dmon_large=5.0, nmon_large=5, nmon_small=400, - dcrowd=1.0, ncrowd=154699), - physical=['dmon_small', 'mmon_small', 'mcrowd']), - id='TransFociCub-ensemble'), - pytest.param( - ParseCase( - TransFociCub, 'space', 'all', - artifact='ns400nl5al5ac1', - name='ns400nl5al5ac1', - values=dict(dmon_large=5.0, nmon_large=5, nmon_small=400, - dcrowd=1.0), - physical=['dmon_small', 'mmon_small', 'mcrowd']), - id='TransFociCub-space'), -]) -def parse_case(request: pytest.FixtureRequest) -> ParseCase: - """ - One hand-written name per lineage, with the values the parser must give: - the digits in the name, converted as the class Notes state - (``lcube = 2 l``). - """ - case: ParseCase = request.param - return case - - -@pytest.fixture -def parsed(parse_case: ParseCase) -> ParserBase: - """The parser built from the name of `parse_case`.""" - return parse_case.parser_class(parse_case.artifact, parse_case.lineage, - parse_case.group) - - -@pytest.fixture(params=[ - pytest.param( - ValidCase(TransFociCub, - 'al5nl5ml125ns400ac1nc154699l30dt0.005bdump2000' - 'adump5000', 'ensemble_long', 'bug'), - id='TransFociCub'), -]) -def valid_case(request: pytest.FixtureRequest) -> ValidCase: - """A valid ``ensemble_long`` name of the parser class.""" - case: ValidCase = request.param - return case - - -@pytest.fixture(params=[ - pytest.param((parser_class, lineage), - id=f"{parser_class.__name__}-{lineage}") - for parser_class in (TransFociCub,) - for lineage in ('segment', 'whole', 'ensemble_long', 'ensemble', 'space') -]) -def declared_case( - request: pytest.FixtureRequest -) -> tuple[type[ParserBase], Any, str, dict[str, Any]]: - """ - A name built from the class's declared pattern for one lineage, with 10.5 - as the value of every float keyword and 2 as the value of every integer - keyword, and the values the parser must store for them. With these - values every volume fraction stays below 1. - """ - parser_class, lineage = request.param - specs = [(keyword, parser_class._keywords[keyword]) - for keyword in parser_class._patterns[lineage]] - name = ''.join(f"{keyword}{'10.5' if spec.kind is float else '2'}" - for keyword, spec in specs) - expected = {spec.attr: (spec.scale * 10.5 + spec.offset - if spec.kind is float else 2) - for _, spec in specs} - return parser_class, lineage, name, expected - - -@pytest.fixture -def make_temp_file(tmp_path: Path) -> Callable[[str], str]: - """ - Create an empty temporary file with a given filename and return its full - path. Useful for testing parsers that expect a file path as input. - """ - - def _make_temp_file(filename: str) -> str: - f = tmp_path / filename - f.touch() - return str(f) - - return _make_temp_file - - -# Parsing hand-written names - - -def test_parser_sets_values_from_name( - parse_case: ParseCase, parsed: ParserBase) -> None: - """Each lineage's name parses to its digits, converted as documented.""" - actual = attrgetter(*parse_case.values)(parsed) - # Values are parsed from decimal text; equal to float64 round-off. - assert actual == pytest.approx(tuple(parse_case.values.values()), - rel=1e-12, abs=1e-12) - - -def test_parser_reduces_artifact_to_lineage_name( - parse_case: ParseCase, parsed: ParserBase) -> None: - """The name drops the group and topology suffixes after the lineage.""" - assert parsed.name == parse_case.name - - -def test_parser_lists_physical_attributes( - parse_case: ParseCase, parsed: ParserBase) -> None: - """Each lineage lists the fixed and computed attributes it sets.""" - assert parsed.physical_attributes == parse_case.physical - - -def test_parser_names_its_project( - parse_case: ParseCase, parsed: ParserBase) -> None: - """The project name is the parser class's name.""" - assert parsed.project_name == parse_case.parser_class.__name__ - - -def test_parser_without_path_reports_no_path( - parse_case: ParseCase, parsed: ParserBase) -> None: - """A parser built from a name keeps it and has no filepath or ext.""" - assert (parsed.filename, parsed.filepath, parsed.ext, parsed.ispath, - parsed.lineage, parsed.group) == ( - parse_case.artifact, None, None, False, parse_case.lineage, - parse_case.group) - - -def test_parser_reads_name_from_file_path( - parse_case: ParseCase, make_temp_file: Callable[[str], str]) -> None: - """A parser built from a file path parses the file's stem.""" - full_path = make_temp_file(parse_case.artifact + '.txt') - - parser = parse_case.parser_class(full_path, parse_case.lineage, - parse_case.group, ispath=True) - - assert (parser.filename, parser.filepath, parser.ext, parser.ispath, - parser.name) == ( - parse_case.artifact + '.txt', Path(os.path.dirname(full_path)), - '.txt', True, parse_case.name) - - -def test_parser_lists_each_attribute_once(parsed: ParserBase) -> None: - """No attribute appears twice in `attributes`.""" - assert len(parsed.attributes) == len(set(parsed.attributes)) - - -def test_parser_sets_every_listed_attribute(parsed: ParserBase) -> None: - """Every attribute in `attributes` is set on the instance.""" - assert set(parsed.attributes) <= vars(parsed).keys() - - -@pytest.mark.parametrize( - 'parser_class, artifact, group, geometry, topology, groups', [ - pytest.param(TransFociCub, 'ns400nl5al5ac1', 'all', 'cubic', 'ring', - ['bug', 'all'], id='TransFociCub'), - ]) -def test_parser_reports_geometry_topology_and_groups( - parser_class: type[ParserBase], artifact: str, group: str, - geometry: str, topology: str, groups: list[str]) -> None: - """The parser reports its project's geometry, topology, and groups.""" - parser = parser_class(artifact, 'space', group) - - assert (parser.geometry, parser.topology, parser.groups) == ( - geometry, topology, groups) - - -@pytest.mark.parametrize('parser_class, artifact, group, dump_keywords', [ - pytest.param(TransFociCub, 'ns400nl5al5ac1', 'all', - {'bug': 'bdump', 'all': 'adump'}, id='TransFociCub'), -]) -def test_parser_maps_each_group_to_its_dump_keyword( - parser_class: type[ParserBase], artifact: str, group: str, - dump_keywords: dict[str, str]) -> None: - """Each group's dump keyword names the period of its own trajectory.""" - parser = parser_class(artifact, 'space', group) - - assert parser.dump_keywords == dump_keywords - - -@pytest.mark.parametrize('parser_class, artifact, group, attrs, expected', [ - pytest.param(TransFociCub, - 'al5nl5ml125.ns400ac1nc154699l30dt0.005bdump2000adump5000', - 'bug', ('mmon_large', 'nmon_small'), (125.0, 400), - id='TransFociCub'), -]) -def test_parser_reads_value_with_trailing_dot( - parser_class: type[ParserBase], artifact: str, group: str, - attrs: tuple[str, ...], expected: tuple[float, ...]) -> None: - """A value written with a trailing dot, such as ``1.``, parses.""" - parser = parser_class(artifact, 'ensemble_long', group) - - # float('1.') is exact. - assert attrgetter(*attrs)(parser) == pytest.approx(expected, rel=1e-12) - - -def test_declared_pattern_round_trips( - declared_case: tuple[type[ParserBase], Any, str, dict[str, Any]] -) -> None: - """ - A name built from a declared pattern parses back to the declared types - and conversions, so float keywords keep non-integer values (property). - """ - parser_class, lineage, name, expected = declared_case - - actual = attrgetter(*expected)( - parser_class(name, lineage, parser_class._groups[0])) - - # One multiplication and one addition in float64. - assert actual == pytest.approx(tuple(expected.values()), rel=1e-12) - assert tuple(map(type, actual)) == tuple(map(type, expected.values())) - - -# Error paths of parsing - - -@pytest.mark.parametrize('parser_class, artifact, group, keyword, attr', [ - pytest.param(TransFociCub, - 'al5nl5ml125ns400ac1nc154699XX30dt0.005bdump2000adump5000', - 'bug', 'l', 'lcube', id='TransFociCub-replaced'), - pytest.param(TransFociCub, - '5nl5ml125ns400ac1nc154699l30dt0.005bdump2000adump5000', - 'bug', 'al', 'dmon_large', id='TransFociCub-heading-digit'), -]) -def test_missing_keyword_raises_value_error( - parser_class: type[ParserBase], artifact: str, group: str, - keyword: str, attr: str) -> None: - """A name without a pattern keyword raises ValueError naming it.""" - message = (f"keyword '{keyword}' for attribute '{attr}' not found in " - f"'{artifact}' (lineage 'ensemble_long')") - - with pytest.raises(ValueError, match=re.escape(message)): - parser_class(artifact, 'ensemble_long', group) - - -@pytest.mark.parametrize('parser_class, artifact, group, keyword', [ - pytest.param(TransFociCub, - 'al5nl.ml125ns400ac1nc154699l30dt0.005bdump2000adump5000', - 'bug', 'nl', id='TransFociCub'), -]) -def test_non_numeric_value_raises_value_error( - parser_class: type[ParserBase], artifact: str, group: str, - keyword: str) -> None: - """A keyword whose value is not a number raises ValueError naming it.""" - message = (f"value of keyword '{keyword}' in '{artifact}' " - "(lineage 'ensemble_long'): '.' is not a number") - - with pytest.raises(ValueError, match=re.escape(message)): - parser_class(artifact, 'ensemble_long', group) - - -def test_empty_artifact_raises_value_error(valid_case: ValidCase) -> None: - """An empty artifact name raises ValueError.""" - with pytest.raises(ValueError, - match="'artifact' cannot be an empty string."): - valid_case.parser_class('', valid_case.lineage, valid_case.group) - - -def test_invalid_group_raises_value_error(valid_case: ValidCase) -> None: - """A group the project does not have raises ValueError.""" - with pytest.raises(ValueError, match='is an invalid option.'): - valid_case.parser_class(valid_case.artifact, valid_case.lineage, - 'invalid') - - -def test_invalid_lineage_raises_value_error(valid_case: ValidCase) -> None: - """A lineage outside the five lineages raises ValueError.""" - with pytest.raises(ValueError, match='is an invalid option.'): - valid_case.parser_class(valid_case.artifact, - cast(LineageT, 'invalid'), - valid_case.group) - - -# Text representations - - -def test_str_summarizes_artifact(valid_case: ValidCase) -> None: - """`str` lists the name, geometry, group, lineage, topology, project.""" - parser = valid_case.parser_class(valid_case.artifact, valid_case.lineage, - valid_case.group) - - assert str(parser) == ( - f"Artifact:\n" - f" Name: '{valid_case.artifact}',\n" - f" Geometry: '{parser.geometry}',\n" - f" Group: '{valid_case.group}',\n" - f" Lineage: '{valid_case.lineage}',\n" - f" Topology: '{parser.topology}',\n" - f" Project: '{valid_case.parser_class.__name__}'") - - -def test_repr_names_artifact(valid_case: ValidCase) -> None: - """`repr` names the file, geometry, group, lineage, topology, project.""" - parser = valid_case.parser_class(valid_case.artifact, valid_case.lineage, - valid_case.group) - - assert repr(parser) == ( - f"Artifact(filename='{valid_case.artifact}', " - f"geometry='{parser.geometry}', group='{valid_case.group}', " - f"lineage='{valid_case.lineage}', topology='{parser.topology}', " - f"project='{valid_case.parser_class.__name__}')") - - -# Bulk values - - -@pytest.mark.parametrize('parser_class, artifact, suffix, number, size', [ - pytest.param(TransFociCub, - 'al5nl5ml125ns400ac1nc154699l30dt0.005bdump2000adump5000', - 'm_small', 'nmon_small', 'dmon_small', - id='TransFociCub-m_small'), - pytest.param(TransFociCub, - 'al5nl5ml125ns400ac1nc154699l30dt0.005bdump2000adump5000', - 'm_large', 'nmon_large', 'dmon_large', - id='TransFociCub-m_large'), - pytest.param(TransFociCub, - 'al5nl5ml125ns400ac1nc154699l30dt0.005bdump2000adump5000', - 'c', 'ncrowd', 'dcrowd', id='TransFociCub-c'), -]) -def test_cube_bulk_values_match_utils( - parser_class: type[ParserBase], artifact: str, suffix: str, - number: str, size: str) -> None: - """Cubic bulk values are the cube functions of the parsed values.""" - parser = parser_class(artifact, 'ensemble_long', parser_class._groups[0]) - n, d, lcube = attrgetter(number, size, 'lcube')(parser) - - actual = attrgetter(f'rho_bulk_{suffix}', f'phi_bulk_{suffix}')(parser) - - # The same functions on the same inputs; float64 round-off only. - assert actual == pytest.approx( - (number_density_cube(n, d, lcube, pbc=True), - volume_fraction_cube(n, d, lcube, pbc=True)), rel=1e-12) - - -@pytest.mark.parametrize('parser_class, artifact', [ - pytest.param(TransFociCub, - 'al5nl5ml125ns400ac1nc154699l30dt0.005bdump2000adump5000', - id='TransFociCub'), -]) -def test_monomer_bulk_values_are_sums_over_sizes( - parser_class: type[ParserBase], artifact: str) -> None: - """All-monomer bulk values are the sums of the small and large ones.""" - parser = parser_class(artifact, 'ensemble_long', 'bug') - small = attrgetter('rho_bulk_m_small', 'phi_bulk_m_small')(parser) - large = attrgetter('rho_bulk_m_large', 'phi_bulk_m_large')(parser) - - actual = attrgetter('rho_bulk_m', 'phi_bulk_m')(parser) - - # One float64 addition each. - assert actual == pytest.approx( - (small[0] + large[0], small[1] + large[1]), rel=1e-12) - - -@pytest.mark.parametrize('parser_class, artifact', [ - pytest.param(TransFociCub, - 'al5nl5ml125ns400ac2nc1000l30dt0.005bdump2000adump5000', - id='TransFociCub'), -]) -def test_crowder_mass_is_size_cubed( - parser_class: type[ParserBase], artifact: str) -> None: - """At a long lineage the crowder mass is its size cubed, m = d^3.""" - parser = parser_class(artifact, 'ensemble_long', 'bug') - - # One float64 power. - assert getattr(parser, 'mcrowd') == pytest.approx( - getattr(parser, 'dcrowd') ** 3, rel=1e-12) - - -@pytest.mark.parametrize('parser_class, artifact', [ - pytest.param(TransFociCub, 'ns400nl5al5ac1nc154699', id='TransFociCub'), -]) -def test_crowder_mass_is_placeholder_at_ensemble( - parser_class: type[ParserBase], artifact: str) -> None: - """At the ensemble lineage the crowder mass keeps the placeholder -1.""" - parser = parser_class(artifact, 'ensemble', 'bug') - - assert getattr(parser, 'mcrowd') == -1 - - -# Lineage-name rules - - -@pytest.mark.parametrize('short_lineage', [ - pytest.param('ensemble', id='ensemble'), - pytest.param('space', id='space'), -]) -def test_short_name_reparses_to_itself( - valid_case: ValidCase, short_lineage: LineageT) -> None: - """A short name parsed at its own lineage rebuilds the same name.""" - parser = valid_case.parser_class(valid_case.artifact, valid_case.lineage, - valid_case.group) - name = getattr(parser, short_lineage) - - reparsed = valid_case.parser_class(name, short_lineage, valid_case.group) - - assert getattr(reparsed, short_lineage) == name - - -@pytest.mark.parametrize( - 'parser_class, group, child_lineage, child_name, parent_lineage', - [ - pytest.param(TransFociCub, 'bug', 'whole', - 'al5.0nl5ml1.0ns400ac1.0nc100l15.0dt0.005bdump2000' - 'adump5000ens1', 'ensemble_long', - id='TransFociCub-whole-ensemble_long'), - ], -) -def test_parser_rebuilt_parent_name_keeps_attributes( - parser_class: type[ParserBase], group: str, child_lineage: LineageT, - child_name: str, parent_lineage: LineageT) -> None: - """A rebuilt parent name parses to its child's attribute values.""" - child = parser_class(child_name, child_lineage, group) - parent = parser_class(getattr(child, parent_lineage), parent_lineage, - group) - values = attrgetter(*parser_class._genealogy_attributes[parent_lineage]) - - # Both sides parse the same digits, so they agree to float64 round-off. - assert values(parent) == pytest.approx(values(child), rel=1e-12, - abs=1e-12) - - -# Declarations - - -def test_attributes_at_matches_parsed_instance( - parse_case: ParseCase, parsed: ParserBase) -> None: - """attributes_at names exactly the public attributes a parser sets.""" - public = {name for name in vars(parsed) if not name.startswith('_')} - - names = parse_case.parser_class.attributes_at(parse_case.lineage) - - assert names == public - - -# Generated names - - -@pytest.mark.parametrize('lineage', [ - pytest.param('segment', id='segment'), - pytest.param('whole', id='whole'), - pytest.param('ensemble_long', id='ensemble_long'), - pytest.param('ensemble', id='ensemble'), - pytest.param('space', id='space'), -]) -@settings(max_examples=50, deadline=None) -@given(data=st.data()) -def test_generated_name_parses_to_its_values_and_back( - lineage: LineageT, data: st.DataObject) -> None: - """A name built from TransFociCub's own pattern parses to its values and - rebuilds to itself: a round-trip property, with no reference value.""" - name, values = data.draw(lineage_names(TransFociCub, lineage)) - - parsed = TransFociCub(name, lineage, TransFociCub._groups[0]) - - assert getattr(parsed, lineage) == name - # The parser converts in exact decimal arithmetic, the expectation in - # float64: a few ulp apart; atol covers a value converted to zero. - expected = stored_values(TransFociCub, values) - assert {attr: getattr(parsed, attr) for attr in expected} == \ - pytest.approx(expected, rel=1e-12, abs=1e-12) diff --git a/projects/trans_foci_cyl/__init__.py b/projects/trans_foci_cyl/__init__.py deleted file mode 100644 index 3bfdb6b1..00000000 --- a/projects/trans_foci_cyl/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -""" -The *TransFociCyl* study: ``SPEC`` is its -:class:`~polyphys.spec.ProjectSpec`, ``PROBE`` its -:class:`~polyphys.spec.ProbeSpec`, and ``ANALYSIS`` its -:class:`~polyphys.spec.AnalysisTable` per particle group, and -``TransFociCyl`` its parser. -""" -from .parser import TransFociCyl -from .spec import ANALYSIS, PROBE, SPEC - -__all__ = ["ANALYSIS", "PROBE", "SPEC", "TransFociCyl"] diff --git a/projects/trans_foci_cyl/parser.py b/projects/trans_foci_cyl/parser.py deleted file mode 100644 index 61e931ba..00000000 --- a/projects/trans_foci_cyl/parser.py +++ /dev/null @@ -1,181 +0,0 @@ -""" -The parser of the *TransFociCyl* study: a ring of small and large monomers -among crowders in a cylinder. - -.. versionchanged:: 1.0.0 - Moved here from :mod:`polyphys.manage.parser`, which ships no study. -""" -from polyphys.manage.parser import KeywordSpec, ParserBase, Species - -__all__ = ["TransFociCyl"] - - -class TransFociCyl(ParserBase): - """ - Parse an artifact name of the *TransFociCyl* project: a ring of small and - large monomers among crowders in a cylinder that is periodic along its - axis. - - Each lineage has its own name pattern; ``#`` stands for a value: - - - ``segment``: epss#epsl#r#al#nl#ml#ns#ac#nc#lz#dt#bdump#adump#ens#.j#.ring - - ``whole``: epss#epsl#r#al#nl#ml#ns#ac#nc#lz#dt#bdump#adump#ens#.ring - - ``ensemble_long``: epss#epsl#r#al#nl#ml#ns#ac#nc#lz#dt#bdump#adump# - - ``ensemble``: ns#nl#al#D#ac#nc# - - ``space``: ns#nl#al#D#ac# - - Parameters - ---------- - artifact : Pathish - Name to be parsed, or a file path with ``ispath=True``. - lineage : {'segment', 'whole', 'ensemble_long', 'ensemble', 'space'} - Lineage of the artifact. - group : {'bug', 'all'} - Particle group: ``'bug'`` for the ring, ``'all'`` for every particle. - ispath : bool, default False - Whether `artifact` is a file path. - - Attributes - ---------- - dmon_small : int - Diameter of a small monomer, fixed at 1; not in the name. - nmon_small : int - Number of small monomers; keyword ``ns``. - mmon_small : int - Mass of a small monomer, fixed at 1; not in the name. - dmon_large : float - Diameter of a large monomer; keyword ``al``. - nmon_large : int - Number of large monomers; keyword ``nl``. - mmon_large : float - Mass of a large monomer; keyword ``ml``. - dcrowd : float - Crowder diameter; keyword ``ac``. - ncrowd : int - Number of crowders; keyword ``nc``. - mcrowd : float - Crowder mass, :math:`m_c = d_c^3`, at ``segment``, ``whole``, and - ``ensemble_long``; ``-1``, a placeholder, at ``ensemble`` and - ``space``. - lcyl : float - Length of the cylinder, :math:`Z = 2 l_z`, from the half-length - :math:`l_z` (keyword ``lz``). - dcyl : float - Size of the cylinder, :math:`D = 2r - 1`, from the radius :math:`r` - (keyword ``r``) in the long names; :math:`D` itself (keyword ``D``) - in the short ones. - epsilon_small : float - Strength of the wall-small-monomer LJ interaction; keyword ``epss``. - epsilon_large : float - Strength of the wall-large-monomer LJ interaction; keyword ``epsl``. - dt : float - Simulation time step; keyword ``dt``. - bdump : int - Dump period of the ``bug`` trajectory, in time steps; keyword - ``bdump``. - adump : int - Dump period of the ``all`` trajectory, in time steps; keyword - ``adump``. - ensemble_id : int - Index of the whole in its ensemble; keyword ``ens``. - segment_id : int - Index of the segment in its whole, zero-padded in the name; keyword - ``j``. - rho_bulk_m_small, phi_bulk_m_small : float - Bulk number density and volume fraction of small monomers. - rho_bulk_m_large, phi_bulk_m_large : float - Bulk number density and volume fraction of large monomers. - rho_bulk_m, phi_bulk_m : float - Bulk number density and volume fraction of all monomers: the sums of - the small and large values. - rho_bulk_c, phi_bulk_c : float - Bulk number density and volume fraction of crowders. - - Notes - ----- - - The bulk values exist only at ``segment``, ``whole``, and - ``ensemble_long``. The lineage names are documented in - :class:`~polyphys.manage.parser.ParserBase`. - - - The mass density is uniform across all species, so a species whose - mass is not in the name has the mass :math:`m_i = d_i^3`, where - :math:`d_i` is its diameter. - - - The cylindrical wall is a layer of beads of size :math:`a_w = 1` whose - centers lie on the cylinder of radius :math:`r` set in LAMMPS (keyword - ``r``), so the cylinder size is :math:`D = 2r - a_w`, stored as `dcyl`. - The keyword ``lz`` is the half-length of the cylinder, so its length is - :math:`Z = 2 l_z`, stored as `lcyl`. The centers of particles of - species :math:`\\alpha` with size :math:`a_\\alpha` are confined to a - cylinder of size :math:`D_{\\mathrm{eff},\\alpha} = D - a_\\alpha`, and - the bulk number density and volume fraction of that species both use - the effective volume :math:`V_{\\mathrm{eff},\\alpha} = \\pi Z - D_{\\mathrm{eff},\\alpha}^2 / 4`. - - - The simulation design values that the name does not carry, such as - the interaction strengths, the bonds, and the thermostat, are - listed in :doc:`/design`. - - Examples - -------- - >>> artifact = TransFociCyl( - ... 'ns500nl5al3.0D20.0ac2.0nc0', - ... 'ensemble', - ... 'bug' - ... ) - >>> print(artifact.dcyl) - 20.0 - """ - _geometry = 'cylindrical' - _topology = 'ring' - _groups = ['bug', 'all'] - _dump_keywords = {'bug': 'bdump', 'all': 'adump'} - _keywords = { - 'epss': KeywordSpec('epsilon_small', float), - 'epsl': KeywordSpec('epsilon_large', float), - 'r': KeywordSpec('dcyl', float, scale=2.0, offset=-1.0), - 'D': KeywordSpec('dcyl', float), - 'al': KeywordSpec('dmon_large', float), - 'nl': KeywordSpec('nmon_large', int), - 'ml': KeywordSpec('mmon_large', float), - 'ns': KeywordSpec('nmon_small', int), - 'ac': KeywordSpec('dcrowd', float), - 'nc': KeywordSpec('ncrowd', int), - 'lz': KeywordSpec('lcyl', float, scale=2.0), - 'dt': KeywordSpec('dt', float), - 'bdump': KeywordSpec('bdump', int), - 'adump': KeywordSpec('adump', int), - 'ens': KeywordSpec('ensemble_id', int), - 'j': KeywordSpec('segment_id', int), - } - _patterns = { - 'segment': ('epss', 'epsl', 'r', 'al', 'nl', 'ml', 'ns', 'ac', 'nc', - 'lz', 'dt', 'bdump', 'adump', 'ens', 'j'), - 'whole': ('epss', 'epsl', 'r', 'al', 'nl', 'ml', 'ns', 'ac', 'nc', - 'lz', 'dt', 'bdump', 'adump', 'ens'), - 'ensemble_long': ('epss', 'epsl', 'r', 'al', 'nl', 'ml', 'ns', 'ac', - 'nc', 'lz', 'dt', 'bdump', 'adump'), - 'ensemble': ('ns', 'nl', 'al', 'D', 'ac', 'nc'), - 'space': ('ns', 'nl', 'al', 'D', 'ac'), - } - _defaults = {'dmon_small': 1, 'mmon_small': 1, 'mcrowd': -1} - _species = (Species('m_small', 'nmon_small', 'dmon_small'), - Species('m_large', 'nmon_large', 'dmon_large'), - Species('c', 'ncrowd', 'dcrowd')) - _totals = {'m': ('m_small', 'm_large')} - _masses = {'mcrowd': 'dcrowd'} - _project_attributes = { - 'segment': ['dmon_small', 'mmon_small', 'mcrowd', 'phi_bulk_m_small', - 'rho_bulk_m_small', 'phi_bulk_m_large', 'rho_bulk_m_large', - 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', 'rho_bulk_c'], - 'whole': ['dmon_small', 'mmon_small', 'mcrowd', 'phi_bulk_m_small', - 'rho_bulk_m_small', 'phi_bulk_m_large', 'rho_bulk_m_large', - 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', 'rho_bulk_c'], - 'ensemble_long': ['dmon_small', 'mmon_small', 'mcrowd', - 'phi_bulk_m_small', 'rho_bulk_m_small', - 'phi_bulk_m_large', 'rho_bulk_m_large', - 'phi_bulk_m', 'rho_bulk_m', 'phi_bulk_c', - 'rho_bulk_c'], - 'ensemble': ['dmon_small', 'mmon_small', 'mcrowd'], - 'space': ['dmon_small', 'mmon_small', 'mcrowd'] - } diff --git a/projects/trans_foci_cyl/spec.py b/projects/trans_foci_cyl/spec.py deleted file mode 100644 index 30204b6b..00000000 --- a/projects/trans_foci_cyl/spec.py +++ /dev/null @@ -1,160 +0,0 @@ -""" -The *TransFociCyl* study: a ring of small and large monomers among crowders in -a cylinder that is periodic along its axis. ``SPEC`` is its -:class:`~polyphys.spec.ProjectSpec`, ``PROBE`` its -:class:`~polyphys.spec.ProbeSpec`, and ``ANALYSIS`` its -:class:`~polyphys.spec.AnalysisTable` per particle group. - -Values from the research code's registry, with the PolyPhys attribute names -``phi_bulk_c`` for ``phi_c_bulk``, and from its analysis tables, with each -artifact listed once. -""" -from polyphys.analyze.pipeline import ( - Autocorrelation, - FociPairDistances, - LocalDensities, - TimeAverage, - half_contour, -) -import numpy as np - -from polyphys.manage.parser import ParserBase -from polyphys.spec import ( - AnalysisTable, - ProbeSpec, - ProjectSpec, - edge, - histogram, - histogram_2d, - matrix_tseries, - tseries, -) - -from .parser import TransFociCyl - -SPEC = ProjectSpec( - parser=TransFociCyl, - group='bug', - divisor=0.025, - crowder_attr='phi_bulk_c', - physical_attrs=('space', 'ensemble_long', 'ensemble', 'nmon_small', - 'nmon_large', 'dmon_large', 'dcyl', 'dcrowd', - 'phi_bulk_c'), - species_sizes={'Mon': 'dmon_small', 'Crd': 'dcrowd', - 'Foci': 'dmon_large'}, - # 'transSizeTMon' is not in the research registry's list, although the - # research probe writes it and the analysis table below reads it; the - # maintainer added it on 2026-11-03. - tseries_properties=('asphericityTMon', 'fsdTMon', 'gyrTMon', - 'shapeTMon', 'transSizeTMon'), -) - - -def _r_bin(info: ParserBase) -> float: - """A tenth of the smaller of the small-monomer and crowder diameters: - the bin size along r, x, and y (the research code's - ``trans_foci_cyl_all``, ``bin_edges``).""" - return 0.1 * min(float(getattr(info, 'dmon_small')), - float(getattr(info, 'dcrowd'))) - - -def _z_bin(info: ParserBase) -> float: - """Half the smaller of the small-monomer and crowder diameters: the bin - size along z (the research code's ``trans_foci_cyl_all``, - ``bin_edges``).""" - return 0.5 * min(float(getattr(info, 'dmon_small')), - float(getattr(info, 'dcrowd'))) - - -def _contact_cutoff(info: ParserBase) -> float: - """The large-monomer diameter plus the crowder diameter: two foci are in - direct contact when a crowder no longer fits between them (the research - code's ``trans_foci_cyl_bug``, ``cluster_cutoff``).""" - return float(getattr(info, 'dmon_large')) + float(getattr(info, 'dcrowd')) - - -# The probe of the polymer group: the selections, the chain-size observables -# of its per-frame loop, and the unwrap convention of its asphericity (the -# research code's ``trans_foci_cyl_bug``); the foci pair distances of the -# same loop; and the histograms of the all group (``trans_foci_cyl_all``): -# its selections, bin sizes, and species. The research all-group probe -# selected the crowders, the small monomers, and the foci; 'Mon', the whole -# ring, is added because the analysis table below reads its histograms. The -# block length and the recentering are the probe guide's ("The spatial -# histograms" and "Recentering a frame"). -PROBE = ProbeSpec( - project=SPEC, - selections={'Mon': 'resid 1', 'Foci': 'type 2', 'Crd': 'resid 0', - 'Dna': 'type 1'}, - chain='Mon', - observables=('asphericityT', 'fsdT', 'gyrT', 'shapeT', 'transSizeT', - 'principalT'), - unwrap=False, - bin_sizes={'r': _r_bin, 'z': _z_bin, 'theta': np.pi / 36, 'x': _r_bin, - 'y': _r_bin}, - histogram_species=('Crd', 'Mon', 'Dna', 'Foci'), - block_frames=100, - recenter='half', - pair_species='Foci', - contact_cutoff=_contact_cutoff, - minimum_image=True, -) - -# The largest lag of the autocorrelation functions, in frames (the research -# code's ``analyze_measures``, ``nlags``). -NLAGS = 7000 - - -def _n_monomers(info: ParserBase) -> int: - """The monomers on the ring: the small ones and the large ones.""" - return int(getattr(info, 'nmon_small') + getattr(info, 'nmon_large')) - - -def _bin_size(info: ParserBase) -> float: - """Half the small-monomer diameter (the research code's - ``whole_dist_mat_foci``).""" - return 0.5 * float(getattr(info, 'dmon_small')) - - -def _r_max(info: ParserBase) -> float: - """Half the ring contour at the FENE maximum ``1.5 sigma_ls``, with - ``sigma_ls`` the large-small collision diameter: the largest bond.""" - sigma_ls = 0.5 * (float(getattr(info, 'dmon_large')) - + float(getattr(info, 'dmon_small'))) - return half_contour(_n_monomers(info), 1.5 * sigma_ls) - - -ANALYSIS = ( - AnalysisTable(group='bug', lineage='whole', stamps=True, properties=( - matrix_tseries('bondsHistT', 'Foci', - post_hook=TimeAverage('bondsHist')), - matrix_tseries('clustersHistT', 'Foci', - post_hook=TimeAverage('clustersHist', - drop_first_column=True)), - matrix_tseries('distMatT', 'Foci', - post_hook=FociPairDistances( - bin_size=_bin_size, r_max=_r_max, - n_monomers=_n_monomers)), - matrix_tseries('principalT', 'Mon'), - tseries('fsdT', 'Mon', post_hook=Autocorrelation(NLAGS)), - tseries('gyrT', 'Mon', post_hook=Autocorrelation(NLAGS)), - tseries('transSizeT', 'Mon', post_hook=Autocorrelation(NLAGS)), - tseries('shapeT', 'Mon', post_hook=Autocorrelation(NLAGS)), - tseries('asphericityT', 'Mon', post_hook=Autocorrelation(NLAGS)), - )), - AnalysisTable(group='all', lineage='segment', stamps=False, properties=( - *(histogram(direction, species, post_hook=LocalDensities()) - for direction in ('r', 'z') - for species in ('Crd', 'Mon', 'Foci')), - histogram('r', 'Dna'), - histogram('z', 'Dna'), - *(histogram('theta', species) - for species in ('Crd', 'Mon', 'Dna', 'Foci')), - *(histogram_2d(plane, species) - for plane in ('xy', 'xz', 'yz') - for species in ('Crd', 'Mon', 'Dna', 'Foci')), - edge('x'), - edge('y'), - edge('z'), - )), -) diff --git a/projects/trans_foci_cyl/test_parser.py b/projects/trans_foci_cyl/test_parser.py deleted file mode 100644 index feb5bdc4..00000000 --- a/projects/trans_foci_cyl/test_parser.py +++ /dev/null @@ -1,630 +0,0 @@ -"""Tests for the *TransFociCyl* parser, :mod:`trans_foci_cyl.parser`.""" -import os -import re -from collections.abc import Callable -from operator import attrgetter -from pathlib import Path -from typing import Any, NamedTuple, cast - -import pytest -from hypothesis import given, settings -from hypothesis import strategies as st - -from polyphys.manage.parser import ParserBase -from polyphys.manage.types import LineageT -from polyphys.tests.strategies import lineage_names, stored_values -from polyphys.manage.utils import ( - number_density_cylinder, - volume_fraction_cylinder, -) -from trans_foci_cyl.parser import TransFociCyl - - -class ParseCase(NamedTuple): - """A hand-written artifact name and what its parser must give.""" - parser_class: type[ParserBase] - lineage: Any - group: Any - artifact: str - name: str - values: dict[str, Any] - physical: list[str] - - -class ValidCase(NamedTuple): - """A valid ``ensemble_long`` artifact name for one parser class.""" - parser_class: type[ParserBase] - artifact: str - lineage: Any - group: Any - - -@pytest.fixture(params=[ - pytest.param( - ParseCase( - TransFociCyl, 'segment', 'bug', - artifact='epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000ens8.j05.ring.bug', - name='epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000ens8.j05.ring', - values=dict(epsilon_small=5.0, epsilon_large=5.0, dcyl=20.0, - dmon_large=3.0, nmon_large=5, mmon_large=27.0, - nmon_small=400, dcrowd=1.0, ncrowd=8340, lcyl=139.0, - dt=0.005, bdump=2000, adump=5000, ensemble_id=8, - segment_id=5), - physical=['dmon_small', 'mmon_small', 'mcrowd', 'phi_bulk_m_small', - 'rho_bulk_m_small', 'phi_bulk_m_large', - 'rho_bulk_m_large', 'phi_bulk_m', 'rho_bulk_m', - 'phi_bulk_c', 'rho_bulk_c']), - id='TransFociCyl-segment'), - pytest.param( - ParseCase( - TransFociCyl, 'whole', 'bug', - artifact='epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000ens8.ring.bug', - name='epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000ens8.ring', - values=dict(epsilon_small=5.0, epsilon_large=5.0, dcyl=20.0, - dmon_large=3.0, nmon_large=5, mmon_large=27.0, - nmon_small=400, dcrowd=1.0, ncrowd=8340, lcyl=139.0, - dt=0.005, bdump=2000, adump=5000, ensemble_id=8), - physical=['dmon_small', 'mmon_small', 'mcrowd', 'phi_bulk_m_small', - 'rho_bulk_m_small', 'phi_bulk_m_large', - 'rho_bulk_m_large', 'phi_bulk_m', 'rho_bulk_m', - 'phi_bulk_c', 'rho_bulk_c']), - id='TransFociCyl-whole'), - pytest.param( - ParseCase( - TransFociCyl, 'ensemble_long', 'bug', - artifact='epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', - name='epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', - values=dict(epsilon_small=5.0, epsilon_large=5.0, dcyl=20.0, - dmon_large=3.0, nmon_large=5, mmon_large=27.0, - nmon_small=400, dcrowd=1.0, ncrowd=8340, lcyl=139.0, - dt=0.005, bdump=2000, adump=5000), - physical=['dmon_small', 'mmon_small', 'mcrowd', 'phi_bulk_m_small', - 'rho_bulk_m_small', 'phi_bulk_m_large', - 'rho_bulk_m_large', 'phi_bulk_m', 'rho_bulk_m', - 'phi_bulk_c', 'rho_bulk_c']), - id='TransFociCyl-ensemble_long'), - pytest.param( - ParseCase( - TransFociCyl, 'ensemble', 'bug', - artifact='ns400nl5al3D20.0ac1nc8340', - name='ns400nl5al3D20.0ac1nc8340', - values=dict(dcyl=20.0, dmon_large=3.0, nmon_large=5, - nmon_small=400, dcrowd=1.0, ncrowd=8340), - physical=['dmon_small', 'mmon_small', 'mcrowd']), - id='TransFociCyl-ensemble'), - pytest.param( - ParseCase( - TransFociCyl, 'space', 'bug', - artifact='ns400nl5al3D20.0ac1', - name='ns400nl5al3D20.0ac1', - values=dict(dcyl=20.0, dmon_large=3.0, nmon_large=5, - nmon_small=400, dcrowd=1.0), - physical=['dmon_small', 'mmon_small', 'mcrowd']), - id='TransFociCyl-space'), -]) -def parse_case(request: pytest.FixtureRequest) -> ParseCase: - """ - One hand-written name per lineage, with the values the parser must give: - the digits in the name, converted as the class Notes state - (``lcyl = 2 lz``, ``dcyl = 2 r - 1``). - """ - case: ParseCase = request.param - return case - - -@pytest.fixture -def parsed(parse_case: ParseCase) -> ParserBase: - """The parser built from the name of `parse_case`.""" - return parse_case.parser_class(parse_case.artifact, parse_case.lineage, - parse_case.group) - - -@pytest.fixture(params=[ - pytest.param( - ValidCase(TransFociCyl, - 'epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', 'ensemble_long', 'bug'), - id='TransFociCyl'), -]) -def valid_case(request: pytest.FixtureRequest) -> ValidCase: - """A valid ``ensemble_long`` name of the parser class.""" - case: ValidCase = request.param - return case - - -@pytest.fixture(params=[ - pytest.param((parser_class, lineage), - id=f"{parser_class.__name__}-{lineage}") - for parser_class in (TransFociCyl,) - for lineage in ('segment', 'whole', 'ensemble_long', 'ensemble', 'space') -]) -def declared_case( - request: pytest.FixtureRequest -) -> tuple[type[ParserBase], Any, str, dict[str, Any]]: - """ - A name built from the class's declared pattern for one lineage, with 10.5 - as the value of every float keyword and 2 as the value of every integer - keyword, and the values the parser must store for them. With these - values every volume fraction stays below 1. - """ - parser_class, lineage = request.param - specs = [(keyword, parser_class._keywords[keyword]) - for keyword in parser_class._patterns[lineage]] - name = ''.join(f"{keyword}{'10.5' if spec.kind is float else '2'}" - for keyword, spec in specs) - expected = {spec.attr: (spec.scale * 10.5 + spec.offset - if spec.kind is float else 2) - for _, spec in specs} - return parser_class, lineage, name, expected - - -@pytest.fixture -def make_temp_file(tmp_path: Path) -> Callable[[str], str]: - """ - Create an empty temporary file with a given filename and return its full - path. Useful for testing parsers that expect a file path as input. - """ - - def _make_temp_file(filename: str) -> str: - f = tmp_path / filename - f.touch() - return str(f) - - return _make_temp_file - - -# Parsing hand-written names - - -def test_parser_sets_values_from_name( - parse_case: ParseCase, parsed: ParserBase) -> None: - """Each lineage's name parses to its digits, converted as documented.""" - actual = attrgetter(*parse_case.values)(parsed) - # Values are parsed from decimal text; equal to float64 round-off. - assert actual == pytest.approx(tuple(parse_case.values.values()), - rel=1e-12, abs=1e-12) - - -def test_parser_reduces_artifact_to_lineage_name( - parse_case: ParseCase, parsed: ParserBase) -> None: - """The name drops the group and topology suffixes after the lineage.""" - assert parsed.name == parse_case.name - - -def test_parser_lists_physical_attributes( - parse_case: ParseCase, parsed: ParserBase) -> None: - """Each lineage lists the fixed and computed attributes it sets.""" - assert parsed.physical_attributes == parse_case.physical - - -def test_parser_names_its_project( - parse_case: ParseCase, parsed: ParserBase) -> None: - """The project name is the parser class's name.""" - assert parsed.project_name == parse_case.parser_class.__name__ - - -def test_parser_without_path_reports_no_path( - parse_case: ParseCase, parsed: ParserBase) -> None: - """A parser built from a name keeps it and has no filepath or ext.""" - assert (parsed.filename, parsed.filepath, parsed.ext, parsed.ispath, - parsed.lineage, parsed.group) == ( - parse_case.artifact, None, None, False, parse_case.lineage, - parse_case.group) - - -def test_parser_reads_name_from_file_path( - parse_case: ParseCase, make_temp_file: Callable[[str], str]) -> None: - """A parser built from a file path parses the file's stem.""" - full_path = make_temp_file(parse_case.artifact + '.txt') - - parser = parse_case.parser_class(full_path, parse_case.lineage, - parse_case.group, ispath=True) - - assert (parser.filename, parser.filepath, parser.ext, parser.ispath, - parser.name) == ( - parse_case.artifact + '.txt', Path(os.path.dirname(full_path)), - '.txt', True, parse_case.name) - - -def test_parser_lists_each_attribute_once(parsed: ParserBase) -> None: - """No attribute appears twice in `attributes`.""" - assert len(parsed.attributes) == len(set(parsed.attributes)) - - -def test_parser_sets_every_listed_attribute(parsed: ParserBase) -> None: - """Every attribute in `attributes` is set on the instance.""" - assert set(parsed.attributes) <= vars(parsed).keys() - - -@pytest.mark.parametrize( - 'parser_class, artifact, group, geometry, topology, groups', [ - pytest.param(TransFociCyl, 'ns400nl5al3D20.0ac1', 'bug', - 'cylindrical', 'ring', ['bug', 'all'], - id='TransFociCyl'), - ]) -def test_parser_reports_geometry_topology_and_groups( - parser_class: type[ParserBase], artifact: str, group: str, - geometry: str, topology: str, groups: list[str]) -> None: - """The parser reports its project's geometry, topology, and groups.""" - parser = parser_class(artifact, 'space', group) - - assert (parser.geometry, parser.topology, parser.groups) == ( - geometry, topology, groups) - - -@pytest.mark.parametrize('parser_class, artifact, group, dump_keywords', [ - pytest.param(TransFociCyl, 'ns400nl5al3D20.0ac1', 'bug', - {'bug': 'bdump', 'all': 'adump'}, id='TransFociCyl'), -]) -def test_parser_maps_each_group_to_its_dump_keyword( - parser_class: type[ParserBase], artifact: str, group: str, - dump_keywords: dict[str, str]) -> None: - """Each group's dump keyword names the period of its own trajectory.""" - parser = parser_class(artifact, 'space', group) - - assert parser.dump_keywords == dump_keywords - - -@pytest.mark.parametrize('parser_class, artifact, group, attrs, expected', [ - pytest.param(TransFociCyl, - 'epss5epsl5r10.5al3.nl5ml27.ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', - 'bug', ('dmon_large', 'mmon_large', 'nmon_small'), - (3.0, 27.0, 400), id='TransFociCyl'), -]) -def test_parser_reads_value_with_trailing_dot( - parser_class: type[ParserBase], artifact: str, group: str, - attrs: tuple[str, ...], expected: tuple[float, ...]) -> None: - """A value written with a trailing dot, such as ``1.``, parses.""" - parser = parser_class(artifact, 'ensemble_long', group) - - # float('1.') is exact. - assert attrgetter(*attrs)(parser) == pytest.approx(expected, rel=1e-12) - - -def test_declared_pattern_round_trips( - declared_case: tuple[type[ParserBase], Any, str, dict[str, Any]] -) -> None: - """ - A name built from a declared pattern parses back to the declared types - and conversions, so float keywords keep non-integer values (property). - """ - parser_class, lineage, name, expected = declared_case - - actual = attrgetter(*expected)( - parser_class(name, lineage, parser_class._groups[0])) - - # One multiplication and one addition in float64. - assert actual == pytest.approx(tuple(expected.values()), rel=1e-12) - assert tuple(map(type, actual)) == tuple(map(type, expected.values())) - - -# Error paths of parsing - - -@pytest.mark.parametrize('parser_class, artifact, group, keyword, attr', [ - pytest.param(TransFociCyl, - 'epss5epsl5r10.5al3nl5xx27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', - 'bug', 'ml', 'mmon_large', id='TransFociCyl-replaced'), - pytest.param(TransFociCyl, - '5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', - 'bug', 'epss', 'epsilon_small', - id='TransFociCyl-heading-digit'), -]) -def test_missing_keyword_raises_value_error( - parser_class: type[ParserBase], artifact: str, group: str, - keyword: str, attr: str) -> None: - """A name without a pattern keyword raises ValueError naming it.""" - message = (f"keyword '{keyword}' for attribute '{attr}' not found in " - f"'{artifact}' (lineage 'ensemble_long')") - - with pytest.raises(ValueError, match=re.escape(message)): - parser_class(artifact, 'ensemble_long', group) - - -@pytest.mark.parametrize('parser_class, artifact, group, keyword', [ - pytest.param(TransFociCyl, - 'epss.epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', - 'bug', 'epss', id='TransFociCyl'), -]) -def test_non_numeric_value_raises_value_error( - parser_class: type[ParserBase], artifact: str, group: str, - keyword: str) -> None: - """A keyword whose value is not a number raises ValueError naming it.""" - message = (f"value of keyword '{keyword}' in '{artifact}' " - "(lineage 'ensemble_long'): '.' is not a number") - - with pytest.raises(ValueError, match=re.escape(message)): - parser_class(artifact, 'ensemble_long', group) - - -def test_empty_artifact_raises_value_error(valid_case: ValidCase) -> None: - """An empty artifact name raises ValueError.""" - with pytest.raises(ValueError, - match="'artifact' cannot be an empty string."): - valid_case.parser_class('', valid_case.lineage, valid_case.group) - - -def test_invalid_group_raises_value_error(valid_case: ValidCase) -> None: - """A group the project does not have raises ValueError.""" - with pytest.raises(ValueError, match='is an invalid option.'): - valid_case.parser_class(valid_case.artifact, valid_case.lineage, - 'invalid') - - -def test_invalid_lineage_raises_value_error(valid_case: ValidCase) -> None: - """A lineage outside the five lineages raises ValueError.""" - with pytest.raises(ValueError, match='is an invalid option.'): - valid_case.parser_class(valid_case.artifact, - cast(LineageT, 'invalid'), - valid_case.group) - - -# Text representations - - -def test_str_summarizes_artifact(valid_case: ValidCase) -> None: - """`str` lists the name, geometry, group, lineage, topology, project.""" - parser = valid_case.parser_class(valid_case.artifact, valid_case.lineage, - valid_case.group) - - assert str(parser) == ( - f"Artifact:\n" - f" Name: '{valid_case.artifact}',\n" - f" Geometry: '{parser.geometry}',\n" - f" Group: '{valid_case.group}',\n" - f" Lineage: '{valid_case.lineage}',\n" - f" Topology: '{parser.topology}',\n" - f" Project: '{valid_case.parser_class.__name__}'") - - -def test_repr_names_artifact(valid_case: ValidCase) -> None: - """`repr` names the file, geometry, group, lineage, topology, project.""" - parser = valid_case.parser_class(valid_case.artifact, valid_case.lineage, - valid_case.group) - - assert repr(parser) == ( - f"Artifact(filename='{valid_case.artifact}', " - f"geometry='{parser.geometry}', group='{valid_case.group}', " - f"lineage='{valid_case.lineage}', topology='{parser.topology}', " - f"project='{valid_case.parser_class.__name__}')") - - -# Bulk values - - -@pytest.mark.parametrize('parser_class, artifact, suffix, number, size', [ - pytest.param(TransFociCyl, - 'epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', - 'm_small', 'nmon_small', 'dmon_small', - id='TransFociCyl-m_small'), - pytest.param(TransFociCyl, - 'epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', - 'm_large', 'nmon_large', 'dmon_large', - id='TransFociCyl-m_large'), - pytest.param(TransFociCyl, - 'epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', - 'c', 'ncrowd', 'dcrowd', id='TransFociCyl-c'), -]) -def test_cylinder_bulk_values_match_utils( - parser_class: type[ParserBase], artifact: str, suffix: str, - number: str, size: str) -> None: - """Cylindrical bulk values are the cylinder functions of the values.""" - parser = parser_class(artifact, 'ensemble_long', parser_class._groups[0]) - n, d, lcyl, dcyl = attrgetter(number, size, 'lcyl', 'dcyl')(parser) - - actual = attrgetter(f'rho_bulk_{suffix}', f'phi_bulk_{suffix}')(parser) - - # The same functions on the same inputs; float64 round-off only. - assert actual == pytest.approx( - (number_density_cylinder(n, d, lcyl, dcyl, pbc=True), - volume_fraction_cylinder(n, d, lcyl, dcyl, pbc=True)), rel=1e-12) - - -def test_cylinder_bulk_fraction_above_one_is_nominal() -> None: - """Five large monomers of size 5 in a cylinder of size 6 and length 20 - parse, with the nominal fraction 5 (pi 5^3 / 6) / (pi 20 1^2 / 4) = - 125 / 6, above one.""" - parser = TransFociCyl( - 'epss1epsl5r3.5al5nl5ml125ns400ac1nc0lz10dt0.005bdump1000adump5000' - 'ens1.ring', 'whole', 'bug') - - actual = getattr(parser, 'phi_bulk_m_large') - - # float64 round-off of a few products and divisions: - assert actual == pytest.approx(125 / 6, rel=1e-12) - - -def test_cylinder_species_that_does_not_fit_raises_value_error() -> None: - """A large monomer as wide as the cylinder, D - a = 0, raises ValueError - naming the state point and the species instead of dividing by zero.""" - artifact = ('epss1epsl5r3al5nl5ml125ns400ac1nc0lz10dt0.005bdump1000' - 'adump5000ens1.ring') - - with pytest.raises(ValueError, match=re.escape( - "species 'm_large' of 'epss1epsl5r3al5nl5ml125ns400ac1nc0lz10" - "dt0.005bdump1000adump5000ens1.ring' (lineage 'whole')")): - TransFociCyl(artifact, 'whole', 'bug') - - -@pytest.mark.parametrize('parser_class, artifact', [ - pytest.param(TransFociCyl, - 'epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', - id='TransFociCyl'), -]) -def test_monomer_bulk_values_are_sums_over_sizes( - parser_class: type[ParserBase], artifact: str) -> None: - """All-monomer bulk values are the sums of the small and large ones.""" - parser = parser_class(artifact, 'ensemble_long', 'bug') - small = attrgetter('rho_bulk_m_small', 'phi_bulk_m_small')(parser) - large = attrgetter('rho_bulk_m_large', 'phi_bulk_m_large')(parser) - - actual = attrgetter('rho_bulk_m', 'phi_bulk_m')(parser) - - # One float64 addition each. - assert actual == pytest.approx( - (small[0] + large[0], small[1] + large[1]), rel=1e-12) - - -@pytest.mark.parametrize('parser_class, artifact', [ - pytest.param(TransFociCyl, - 'epss5epsl5r10.5al3nl5ml27ns400ac2nc1000lz69.5dt0.005' - 'bdump2000adump5000', - id='TransFociCyl'), -]) -def test_crowder_mass_is_size_cubed( - parser_class: type[ParserBase], artifact: str) -> None: - """At a long lineage the crowder mass is its size cubed, m = d^3.""" - parser = parser_class(artifact, 'ensemble_long', 'bug') - - # One float64 power. - assert getattr(parser, 'mcrowd') == pytest.approx( - getattr(parser, 'dcrowd') ** 3, rel=1e-12) - - -@pytest.mark.parametrize('parser_class, artifact', [ - pytest.param(TransFociCyl, 'ns400nl5al3D20.0ac1nc8340', - id='TransFociCyl'), -]) -def test_crowder_mass_is_placeholder_at_ensemble( - parser_class: type[ParserBase], artifact: str) -> None: - """At the ensemble lineage the crowder mass keeps the placeholder -1.""" - parser = parser_class(artifact, 'ensemble', 'bug') - - assert getattr(parser, 'mcrowd') == -1 - - -# Lineage-name rules - - -@pytest.mark.parametrize('parser_class, group, artifact, ensemble', [ - pytest.param(TransFociCyl, 'bug', - 'epss5epsl5r4.1al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000', - 'ns400nl5al3D7.2ac1nc8340', id='TransFociCyl-r4.1'), -]) -def test_short_name_prints_cylinder_size_from_r_as_decimal( - parser_class: type[ParserBase], group: str, artifact: str, - ensemble: str) -> None: - """The size D = 2r - 1 converted from a decimal r prints as the decimal - result in the ensemble name, so it matches the files named with it.""" - parser = parser_class(artifact, 'ensemble_long', group) - - assert getattr(parser, 'ensemble') == ensemble - - -@pytest.mark.parametrize('short_lineage', [ - pytest.param('ensemble', id='ensemble'), - pytest.param('space', id='space'), -]) -def test_short_name_reparses_to_itself( - valid_case: ValidCase, short_lineage: LineageT) -> None: - """A short name parsed at its own lineage rebuilds the same name.""" - parser = valid_case.parser_class(valid_case.artifact, valid_case.lineage, - valid_case.group) - name = getattr(parser, short_lineage) - - reparsed = valid_case.parser_class(name, short_lineage, valid_case.group) - - assert getattr(reparsed, short_lineage) == name - - -@pytest.mark.parametrize( - 'parser_class, group, child_lineage, child_name, parent_lineage', - [ - pytest.param(TransFociCyl, 'bug', 'segment', - 'epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000ens8.j05.ring.bug', 'whole', - id='TransFociCyl-segment-whole'), - ], -) -def test_parser_rebuilt_parent_name_keeps_attributes( - parser_class: type[ParserBase], group: str, child_lineage: LineageT, - child_name: str, parent_lineage: LineageT) -> None: - """A rebuilt parent name parses to its child's attribute values.""" - child = parser_class(child_name, child_lineage, group) - parent = parser_class(getattr(child, parent_lineage), parent_lineage, - group) - values = attrgetter(*parser_class._genealogy_attributes[parent_lineage]) - - # Both sides parse the same digits, so they agree to float64 round-off. - assert values(parent) == pytest.approx(values(child), rel=1e-12, - abs=1e-12) - - -@pytest.mark.parametrize( - 'parser_class, group, lineage, artifact, parent_lineage, expected', - [ - pytest.param(TransFociCyl, 'bug', 'segment', - 'epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000ens8.j05.ring.bug', 'segment', - 'epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000ens8.j05', id='TransFociCyl-segment'), - pytest.param(TransFociCyl, 'bug', 'whole', - 'epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000ens8.ring.bug', 'whole', - 'epss5epsl5r10.5al3nl5ml27ns400ac1nc8340lz69.5dt0.005' - 'bdump2000adump5000ens8', id='TransFociCyl-whole'), - ], -) -def test_parser_rebuilt_parent_name_is_name_prefix( - parser_class: type[ParserBase], group: str, lineage: LineageT, - artifact: str, parent_lineage: LineageT, expected: str) -> None: - """A long-form parent name is the artifact name cut after its last key.""" - parser = parser_class(artifact, lineage, group) - - assert getattr(parser, parent_lineage) == expected - - -# Declarations - - -def test_attributes_at_matches_parsed_instance( - parse_case: ParseCase, parsed: ParserBase) -> None: - """attributes_at names exactly the public attributes a parser sets.""" - public = {name for name in vars(parsed) if not name.startswith('_')} - - names = parse_case.parser_class.attributes_at(parse_case.lineage) - - assert names == public - - -# Generated names - - -@pytest.mark.parametrize('lineage', [ - pytest.param('segment', id='segment'), - pytest.param('whole', id='whole'), - pytest.param('ensemble_long', id='ensemble_long'), - pytest.param('ensemble', id='ensemble'), - pytest.param('space', id='space'), -]) -@settings(max_examples=50, deadline=None) -@given(data=st.data()) -def test_generated_name_parses_to_its_values_and_back( - lineage: LineageT, data: st.DataObject) -> None: - """A name built from TransFociCyl's own pattern parses to its values and - rebuilds to itself: a round-trip property, with no reference value.""" - name, values = data.draw(lineage_names(TransFociCyl, lineage)) - - parsed = TransFociCyl(name, lineage, TransFociCyl._groups[0]) - - assert getattr(parsed, lineage) == name - # The parser converts in exact decimal arithmetic, the expectation in - # float64: a few ulp apart; atol covers a value converted to zero. - expected = stored_values(TransFociCyl, values) - assert {attr: getattr(parsed, attr) for attr in expected} == \ - pytest.approx(expected, rel=1e-12, abs=1e-12) diff --git a/pyproject.toml b/pyproject.toml index e39af1b8..6baf2c8f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,7 +67,7 @@ [tool.setuptools.packages.find] include = ["polyphys*"] - exclude = ["projects*", "under_review*"] + exclude = ["under_review*"] [tool.pytest.ini_options] addopts = """\ @@ -95,7 +95,6 @@ "polyphys/probe", "polyphys/simulate", "polyphys/viz", - "projects", ] strict = true