eosquality 0.2.1__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {eosquality-0.2.1/src/eosquality.egg-info → eosquality-0.3.0}/PKG-INFO +7 -19
- {eosquality-0.2.1 → eosquality-0.3.0}/README.md +6 -18
- {eosquality-0.2.1 → eosquality-0.3.0}/pyproject.toml +1 -1
- eosquality-0.3.0/src/eosquality/_artifacts.py +480 -0
- eosquality-0.3.0/src/eosquality/_files.py +176 -0
- eosquality-0.3.0/src/eosquality/_layout.py +64 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/_registry.py +1 -1
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/cli/__init__.py +3 -3
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/cli/_common.py +5 -2
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/cli/fit.py +57 -13
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/cli/run.py +16 -7
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/exceptions.py +9 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/quality.py +10 -5
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/_base.py +7 -24
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/_match_keys.py +1 -1
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/_percentile_score.py +3 -6
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/_physchem_domain.py +24 -35
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/extremity.py +5 -4
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/reference_match.py +6 -6
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/training_distance.py +29 -9
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/training_match.py +5 -2
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/training_physchem.py +12 -11
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/typicality.py +1 -2
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/shared/load.py +19 -14
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/shared/metadata.py +1 -1
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/shared/save.py +18 -11
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/training/state.py +49 -48
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/utils/identifiers.py +31 -1
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/utils/logging.py +1 -1
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/vectorindex.py +29 -19
- {eosquality-0.2.1 → eosquality-0.3.0/src/eosquality.egg-info}/PKG-INFO +7 -19
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality.egg-info/SOURCES.txt +3 -0
- eosquality-0.3.0/tests/test_artifacts.py +165 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_cli.py +69 -33
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_robustness.py +9 -6
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_roundtrip.py +35 -18
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_training.py +22 -9
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_vectorindex.py +5 -5
- eosquality-0.2.1/src/eosquality/_artifacts.py +0 -263
- {eosquality-0.2.1 → eosquality-0.3.0}/LICENSE +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/setup.cfg +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/__init__.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/_reference_modality.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/_training_modality.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/cli/build.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/cli/setup.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/library/__init__.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/library/download.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/library/identity.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/library/physchem.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/library/physchem_cache.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/library/physchem_scaler.json +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/library/reference.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/preprocess.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/py.typed +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/results.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/schema/__init__.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/schema/infer.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/schema/models.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/__init__.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/_helpers.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/scores/_training_helpers.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/shared/__init__.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/shared/feature_selection.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/shared/fit.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/shared/state.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/training/__init__.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/training/data.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/utils/__init__.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/utils/console/__init__.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/utils/console/_core.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/utils/console/bars.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/utils/console/display.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/utils/console/fmt.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/utils/console/steps.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality/utils/parallel.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality.egg-info/dependency_links.txt +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality.egg-info/entry_points.txt +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality.egg-info/requires.txt +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/src/eosquality.egg-info/top_level.txt +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_cdf.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_console.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_extremity.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_library.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_logging.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_nan_policy.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_physchem.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_reference_match.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_scripts.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_startup.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_training_physchem.py +0 -0
- {eosquality-0.2.1 → eosquality-0.3.0}/tests/test_typicality.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: eosquality
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Assess the quality of query data against a fitted reference population
|
|
5
5
|
Author-email: Ersilia Open Source Initiative <hello@ersilia.io>
|
|
6
6
|
License-Expression: GPL-3.0-or-later
|
|
@@ -60,7 +60,7 @@ The CLI is then available as `eosquality`. Start by setting it up, which fetches
|
|
|
60
60
|
eosquality setup
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
-
This downloads
|
|
63
|
+
This downloads the reference library and stores it under `~/.eosquality/indices/`.
|
|
64
64
|
|
|
65
65
|
## Quick start
|
|
66
66
|
|
|
@@ -73,12 +73,12 @@ The input CSV must hold the model's predictions on the **exact** molecules of th
|
|
|
73
73
|
There is one and only one reference library per major version of `eosquality`, so the molecule set is fixed by your install. If the SMILES entries in the input CSV don't match that library, `fit` refuses with an error.
|
|
74
74
|
|
|
75
75
|
```bash
|
|
76
|
-
eosquality fit -r reference_eos4e40_v1.csv -a
|
|
76
|
+
eosquality fit -r reference_eos4e40_v1.csv -a eos4e40_v1_domain.zip
|
|
77
77
|
# optionally with training sets: -t training_eos4e40_v1/
|
|
78
78
|
# skip a score: --exclude ref_match
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
Please check [Isaura](https://github.com/ersilia-os/isaura) for a large store of pre-calculations across Ersilia models.
|
|
81
|
+
Please check [Isaura](https://github.com/ersilia-os/isaura) for a large store of pre-calculations across Ersilia models. Pre-fitted artifacts of Ersilia Model Hub models are stored as `<eos_id>_<vN>_domain.zip` files; see the [pre-fitted store](docs/reference-library.md#pre-fitted-store).
|
|
82
82
|
|
|
83
83
|
### Fitting training sets
|
|
84
84
|
|
|
@@ -87,15 +87,15 @@ with a `smiles` column. Given both `-r` and `-t`, the scores cover only the
|
|
|
87
87
|
output columns that have a training set.
|
|
88
88
|
|
|
89
89
|
```bash
|
|
90
|
-
eosquality fit -r reference_eos4e40_v1.csv -t training_eos4e40_v1/ -a
|
|
90
|
+
eosquality fit -r reference_eos4e40_v1.csv -t training_eos4e40_v1/ -a eos4e40_v1_domain.zip
|
|
91
91
|
```
|
|
92
92
|
|
|
93
93
|
### Running against new samples
|
|
94
94
|
|
|
95
|
-
At querying time, `run` loads a fitted artifacts
|
|
95
|
+
At querying time, `run` loads a fitted artifacts file and scores any query CSV containing Ersilia results for the same model. A training-only artifact needs nothing but SMILES, in an `input` or `smiles` column. The output CSV has `key`, `input`, and the columns of each fitted score (see below): percentiles in `(0, 1]` with their `*_raw` values, and 1 / 0 match flags. Reference scores are named `ref_…`, training scores `trn_…`.
|
|
96
96
|
|
|
97
97
|
```bash
|
|
98
|
-
eosquality run -i query_eos4e40_v1.csv -a
|
|
98
|
+
eosquality run -i query_eos4e40_v1.csv -a eos4e40_v1_domain.zip -o quality_eos4e40_v1.csv
|
|
99
99
|
```
|
|
100
100
|
|
|
101
101
|
## Scores
|
|
@@ -113,18 +113,6 @@ Typicality and extremity compare a query against the model's own predictions on
|
|
|
113
113
|
|
|
114
114
|
The `_pct` columns are calibrated percentiles and the `_raw` columns the underlying values. `run --details` also writes the per-column values and the nearest training molecules.
|
|
115
115
|
|
|
116
|
-
## Documentation
|
|
117
|
-
|
|
118
|
-
- [Concepts](docs/concepts.md): how each score is computed and calibrated
|
|
119
|
-
- [CLI](docs/cli.md) and [Python API](docs/api.md)
|
|
120
|
-
- [Architecture](docs/diagram.md): fit/run data flow and save layout
|
|
121
|
-
- [Reference library](docs/reference-library.md): versioning, compatibility, maintainer release steps
|
|
122
|
-
- [Project status](docs/status.md): current results, known limitations, open questions
|
|
123
|
-
|
|
124
|
-
## License
|
|
125
|
-
|
|
126
|
-
GPL-3.0-or-later; see [LICENSE](LICENSE).
|
|
127
|
-
|
|
128
116
|
## About the Ersilia Open Source Initiative
|
|
129
117
|
|
|
130
118
|
The [Ersilia Open Source Initiative](https://ersilia.io) is a tech-nonprofit organization fueling sustainable research in the Global South. Ersilia's main asset is the [Ersilia Model Hub](https://github.com/ersilia-os/ersilia), an open-source repository of AI/ML models for antimicrobial drug discovery.
|
|
@@ -18,7 +18,7 @@ The CLI is then available as `eosquality`. Start by setting it up, which fetches
|
|
|
18
18
|
eosquality setup
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
This downloads
|
|
21
|
+
This downloads the reference library and stores it under `~/.eosquality/indices/`.
|
|
22
22
|
|
|
23
23
|
## Quick start
|
|
24
24
|
|
|
@@ -31,12 +31,12 @@ The input CSV must hold the model's predictions on the **exact** molecules of th
|
|
|
31
31
|
There is one and only one reference library per major version of `eosquality`, so the molecule set is fixed by your install. If the SMILES entries in the input CSV don't match that library, `fit` refuses with an error.
|
|
32
32
|
|
|
33
33
|
```bash
|
|
34
|
-
eosquality fit -r reference_eos4e40_v1.csv -a
|
|
34
|
+
eosquality fit -r reference_eos4e40_v1.csv -a eos4e40_v1_domain.zip
|
|
35
35
|
# optionally with training sets: -t training_eos4e40_v1/
|
|
36
36
|
# skip a score: --exclude ref_match
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
Please check [Isaura](https://github.com/ersilia-os/isaura) for a large store of pre-calculations across Ersilia models.
|
|
39
|
+
Please check [Isaura](https://github.com/ersilia-os/isaura) for a large store of pre-calculations across Ersilia models. Pre-fitted artifacts of Ersilia Model Hub models are stored as `<eos_id>_<vN>_domain.zip` files; see the [pre-fitted store](docs/reference-library.md#pre-fitted-store).
|
|
40
40
|
|
|
41
41
|
### Fitting training sets
|
|
42
42
|
|
|
@@ -45,15 +45,15 @@ with a `smiles` column. Given both `-r` and `-t`, the scores cover only the
|
|
|
45
45
|
output columns that have a training set.
|
|
46
46
|
|
|
47
47
|
```bash
|
|
48
|
-
eosquality fit -r reference_eos4e40_v1.csv -t training_eos4e40_v1/ -a
|
|
48
|
+
eosquality fit -r reference_eos4e40_v1.csv -t training_eos4e40_v1/ -a eos4e40_v1_domain.zip
|
|
49
49
|
```
|
|
50
50
|
|
|
51
51
|
### Running against new samples
|
|
52
52
|
|
|
53
|
-
At querying time, `run` loads a fitted artifacts
|
|
53
|
+
At querying time, `run` loads a fitted artifacts file and scores any query CSV containing Ersilia results for the same model. A training-only artifact needs nothing but SMILES, in an `input` or `smiles` column. The output CSV has `key`, `input`, and the columns of each fitted score (see below): percentiles in `(0, 1]` with their `*_raw` values, and 1 / 0 match flags. Reference scores are named `ref_…`, training scores `trn_…`.
|
|
54
54
|
|
|
55
55
|
```bash
|
|
56
|
-
eosquality run -i query_eos4e40_v1.csv -a
|
|
56
|
+
eosquality run -i query_eos4e40_v1.csv -a eos4e40_v1_domain.zip -o quality_eos4e40_v1.csv
|
|
57
57
|
```
|
|
58
58
|
|
|
59
59
|
## Scores
|
|
@@ -71,18 +71,6 @@ Typicality and extremity compare a query against the model's own predictions on
|
|
|
71
71
|
|
|
72
72
|
The `_pct` columns are calibrated percentiles and the `_raw` columns the underlying values. `run --details` also writes the per-column values and the nearest training molecules.
|
|
73
73
|
|
|
74
|
-
## Documentation
|
|
75
|
-
|
|
76
|
-
- [Concepts](docs/concepts.md): how each score is computed and calibrated
|
|
77
|
-
- [CLI](docs/cli.md) and [Python API](docs/api.md)
|
|
78
|
-
- [Architecture](docs/diagram.md): fit/run data flow and save layout
|
|
79
|
-
- [Reference library](docs/reference-library.md): versioning, compatibility, maintainer release steps
|
|
80
|
-
- [Project status](docs/status.md): current results, known limitations, open questions
|
|
81
|
-
|
|
82
|
-
## License
|
|
83
|
-
|
|
84
|
-
GPL-3.0-or-later; see [LICENSE](LICENSE).
|
|
85
|
-
|
|
86
74
|
## About the Ersilia Open Source Initiative
|
|
87
75
|
|
|
88
76
|
The [Ersilia Open Source Initiative](https://ersilia.io) is a tech-nonprofit organization fueling sustainable research in the Global South. Ersilia's main asset is the [Ersilia Model Hub](https://github.com/ersilia-os/ersilia), an open-source repository of AI/ML models for antimicrobial drug discovery.
|
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
"""Saving and loading :class:`~eosquality.quality.ErsiliaQuality` artifacts."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import pathlib
|
|
6
|
+
import tempfile
|
|
7
|
+
import zipfile
|
|
8
|
+
from typing import TYPE_CHECKING, Any
|
|
9
|
+
|
|
10
|
+
from eosquality._files import (
|
|
11
|
+
file_digest,
|
|
12
|
+
pack_folder,
|
|
13
|
+
read_json,
|
|
14
|
+
unpack_zip,
|
|
15
|
+
write_json,
|
|
16
|
+
)
|
|
17
|
+
from eosquality._layout import MANIFEST_FILE, REFERENCE_DIR, TRAINING_DIR
|
|
18
|
+
from eosquality._registry import SCORE_ORDER
|
|
19
|
+
from eosquality.exceptions import (
|
|
20
|
+
ArtifactIntegrityError,
|
|
21
|
+
ArtifactVersionError,
|
|
22
|
+
IncompatibleArtifactsError,
|
|
23
|
+
)
|
|
24
|
+
from eosquality.library.identity import LIBRARY_ID
|
|
25
|
+
from eosquality.scores.extremity import Extremity
|
|
26
|
+
from eosquality.scores.reference_match import ReferenceMatch
|
|
27
|
+
from eosquality.scores.training_distance import TrainingDistance
|
|
28
|
+
from eosquality.scores.training_match import TrainingMatch
|
|
29
|
+
from eosquality.scores.training_physchem import TrainingPhyschem
|
|
30
|
+
from eosquality.scores.typicality import Typicality
|
|
31
|
+
from eosquality.shared.load import load_shared
|
|
32
|
+
from eosquality.shared.save import save_shared
|
|
33
|
+
from eosquality.shared.state import SharedFitState
|
|
34
|
+
from eosquality.training import load_training_state, save_training_state
|
|
35
|
+
from eosquality.utils.logging import logger
|
|
36
|
+
|
|
37
|
+
if TYPE_CHECKING:
|
|
38
|
+
from eosquality.quality import ErsiliaQuality
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
SCORE_CLASSES = {
|
|
42
|
+
"typicality": Typicality,
|
|
43
|
+
"extremity": Extremity,
|
|
44
|
+
"match": ReferenceMatch,
|
|
45
|
+
}
|
|
46
|
+
TRAINING_CLASSES = {
|
|
47
|
+
cls.NAME: cls for cls in (TrainingDistance, TrainingPhyschem, TrainingMatch)
|
|
48
|
+
}
|
|
49
|
+
# Packages whose version is recorded in the manifest. Only RDKit's is
|
|
50
|
+
# enforced (by the files that depend on it); the rest document the fit.
|
|
51
|
+
_RECORDED_PACKAGES = ("eosquality", "rdkit", "eosframes", "FPSim2", "numpy")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def save(eq, path: str | pathlib.Path) -> pathlib.Path:
|
|
55
|
+
"""Write fitted artifacts as one file: a zip of the artifacts folder.
|
|
56
|
+
|
|
57
|
+
The files of :func:`save_folder` are written to a temporary folder and
|
|
58
|
+
packed, at the zip's root, into ``path``. The same fit always gives the
|
|
59
|
+
same file, apart from the fit time in its manifest.
|
|
60
|
+
|
|
61
|
+
Parameters
|
|
62
|
+
----------
|
|
63
|
+
eq : ErsiliaQuality
|
|
64
|
+
A fitted orchestrator.
|
|
65
|
+
path : str or pathlib.Path
|
|
66
|
+
Package file to write, by convention ``<eos_id>_<vN>_domain.zip``.
|
|
67
|
+
|
|
68
|
+
Returns
|
|
69
|
+
-------
|
|
70
|
+
pathlib.Path
|
|
71
|
+
The package file.
|
|
72
|
+
|
|
73
|
+
Raises
|
|
74
|
+
------
|
|
75
|
+
FileExistsError
|
|
76
|
+
If ``path`` already exists.
|
|
77
|
+
"""
|
|
78
|
+
eq._check_fitted()
|
|
79
|
+
target = pathlib.Path(path)
|
|
80
|
+
if target.exists():
|
|
81
|
+
raise FileExistsError(f"{target} already exists; delete it or save elsewhere.")
|
|
82
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
83
|
+
with tempfile.TemporaryDirectory(prefix="eosquality_save_") as work:
|
|
84
|
+
pack_folder(save_folder(eq, pathlib.Path(work) / "artifacts"), target)
|
|
85
|
+
logger.info(f"Artifacts saved → {target}")
|
|
86
|
+
return target
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def save_folder(eq, path: str | pathlib.Path) -> pathlib.Path:
|
|
90
|
+
"""Write fitted artifacts to a folder, one subfolder per modality.
|
|
91
|
+
|
|
92
|
+
The unpacked form of a package: :func:`save` zips exactly these files.
|
|
93
|
+
|
|
94
|
+
``reference_mode/`` holds ``shared/`` and each reference score's subfolder;
|
|
95
|
+
``training_mode/`` holds ``training_sets/`` and each training score's
|
|
96
|
+
subfolder. ``manifest.json`` at the top lists every file with its size
|
|
97
|
+
and SHA-256; :func:`load` checks them.
|
|
98
|
+
|
|
99
|
+
Parameters
|
|
100
|
+
----------
|
|
101
|
+
eq : ErsiliaQuality
|
|
102
|
+
A fitted orchestrator.
|
|
103
|
+
path : str or pathlib.Path
|
|
104
|
+
Artifacts folder (created if needed). It must not already hold
|
|
105
|
+
artifacts: saving over them would mix the old and the new fit.
|
|
106
|
+
|
|
107
|
+
Returns
|
|
108
|
+
-------
|
|
109
|
+
pathlib.Path
|
|
110
|
+
The artifacts folder.
|
|
111
|
+
|
|
112
|
+
Raises
|
|
113
|
+
------
|
|
114
|
+
FileExistsError
|
|
115
|
+
If ``path`` already holds a ``reference_mode/`` or ``training_mode/``.
|
|
116
|
+
"""
|
|
117
|
+
eq._check_fitted()
|
|
118
|
+
folder = pathlib.Path(path)
|
|
119
|
+
for mode in (REFERENCE_DIR, TRAINING_DIR):
|
|
120
|
+
if (folder / mode).exists():
|
|
121
|
+
raise FileExistsError(
|
|
122
|
+
f"{folder} already holds artifacts ({mode}/); delete it or save "
|
|
123
|
+
"to a new folder."
|
|
124
|
+
)
|
|
125
|
+
folder.mkdir(parents=True, exist_ok=True)
|
|
126
|
+
if eq._shared is not None:
|
|
127
|
+
root = folder / REFERENCE_DIR
|
|
128
|
+
save_shared(eq._shared, root)
|
|
129
|
+
for component in eq._components().values():
|
|
130
|
+
component.save_component(root)
|
|
131
|
+
save_training(eq, folder)
|
|
132
|
+
write_manifest(eq, folder)
|
|
133
|
+
return folder
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def save_training(eq, folder: pathlib.Path) -> None:
|
|
137
|
+
"""Write ``<folder>/training_mode/`` if the training modality is fitted.
|
|
138
|
+
|
|
139
|
+
Parameters
|
|
140
|
+
----------
|
|
141
|
+
eq : ErsiliaQuality
|
|
142
|
+
The orchestrator.
|
|
143
|
+
folder : pathlib.Path
|
|
144
|
+
Artifacts folder.
|
|
145
|
+
"""
|
|
146
|
+
if eq._training is None:
|
|
147
|
+
return
|
|
148
|
+
root = folder / TRAINING_DIR
|
|
149
|
+
save_training_state(eq._training, root)
|
|
150
|
+
for component in eq._training_components().values():
|
|
151
|
+
component.save_component(root)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def write_manifest(eq, folder: pathlib.Path) -> None:
|
|
155
|
+
"""Write the top-level ``manifest.json`` summary.
|
|
156
|
+
|
|
157
|
+
Parameters
|
|
158
|
+
----------
|
|
159
|
+
eq : ErsiliaQuality
|
|
160
|
+
The fitted orchestrator.
|
|
161
|
+
folder : pathlib.Path
|
|
162
|
+
Artifacts folder.
|
|
163
|
+
"""
|
|
164
|
+
eos_id, version = eq._model_id()
|
|
165
|
+
manifest: dict[str, Any] = {
|
|
166
|
+
"eos_id": eos_id,
|
|
167
|
+
"version": version,
|
|
168
|
+
"modalities": [
|
|
169
|
+
m
|
|
170
|
+
for m, present in (
|
|
171
|
+
("reference", eq._shared is not None),
|
|
172
|
+
("training", eq._training is not None),
|
|
173
|
+
)
|
|
174
|
+
if present
|
|
175
|
+
],
|
|
176
|
+
"scores": list(eq._components()) + list(eq._training_components()),
|
|
177
|
+
"max_features": eq._max_features, # null: no cap
|
|
178
|
+
"fit_timestamp": _fit_timestamp(eq),
|
|
179
|
+
"versions": _versions(),
|
|
180
|
+
}
|
|
181
|
+
if eq._shared is not None:
|
|
182
|
+
meta = eq._shared.metadata
|
|
183
|
+
manifest["reference"] = {
|
|
184
|
+
"format_version": meta.format_version,
|
|
185
|
+
"n_samples": meta.n_samples,
|
|
186
|
+
"n_features": meta.n_features,
|
|
187
|
+
"n_features_selected": len(eq._shared.selected_columns),
|
|
188
|
+
"library_id": meta.library_id,
|
|
189
|
+
}
|
|
190
|
+
if eq._training is not None:
|
|
191
|
+
from eosquality.training.state import TRAINING_FORMAT_VERSION
|
|
192
|
+
|
|
193
|
+
manifest["training"] = {
|
|
194
|
+
"format_version": TRAINING_FORMAT_VERSION,
|
|
195
|
+
"columns": {
|
|
196
|
+
name: {"n": col.n} for name, col in eq._training.columns.items()
|
|
197
|
+
},
|
|
198
|
+
}
|
|
199
|
+
manifest["files"] = file_table(folder)
|
|
200
|
+
write_json(folder / MANIFEST_FILE, manifest)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def file_table(folder: pathlib.Path) -> dict[str, dict[str, Any]]:
|
|
204
|
+
"""Size and SHA-256 of every file of the two modality folders.
|
|
205
|
+
|
|
206
|
+
Parameters
|
|
207
|
+
----------
|
|
208
|
+
folder : pathlib.Path
|
|
209
|
+
Artifacts folder.
|
|
210
|
+
|
|
211
|
+
Returns
|
|
212
|
+
-------
|
|
213
|
+
dict
|
|
214
|
+
Relative POSIX path → ``{"size", "sha256"}``, sorted by path.
|
|
215
|
+
"""
|
|
216
|
+
files = sorted(
|
|
217
|
+
path
|
|
218
|
+
for mode in (REFERENCE_DIR, TRAINING_DIR)
|
|
219
|
+
for path in (folder / mode).rglob("*")
|
|
220
|
+
if path.is_file()
|
|
221
|
+
)
|
|
222
|
+
return {
|
|
223
|
+
path.relative_to(folder).as_posix(): {
|
|
224
|
+
"size": path.stat().st_size,
|
|
225
|
+
"sha256": file_digest(path),
|
|
226
|
+
}
|
|
227
|
+
for path in files
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def verify_manifest(folder: pathlib.Path) -> dict[str, Any]:
|
|
232
|
+
"""Check an artifacts folder against its ``manifest.json``.
|
|
233
|
+
|
|
234
|
+
A partial download or extraction would otherwise load as an artifact
|
|
235
|
+
with fewer scores, or with a damaged table.
|
|
236
|
+
|
|
237
|
+
Parameters
|
|
238
|
+
----------
|
|
239
|
+
folder : pathlib.Path
|
|
240
|
+
Artifacts folder.
|
|
241
|
+
|
|
242
|
+
Returns
|
|
243
|
+
-------
|
|
244
|
+
dict
|
|
245
|
+
The manifest.
|
|
246
|
+
|
|
247
|
+
Raises
|
|
248
|
+
------
|
|
249
|
+
ArtifactVersionError
|
|
250
|
+
If the manifest has no file list (written before format 13).
|
|
251
|
+
ArtifactIntegrityError
|
|
252
|
+
If the manifest is missing, or a listed file is missing or differs.
|
|
253
|
+
"""
|
|
254
|
+
path = folder / MANIFEST_FILE
|
|
255
|
+
if not path.is_file():
|
|
256
|
+
raise ArtifactIntegrityError(
|
|
257
|
+
f"No {MANIFEST_FILE} in {folder}: the artifacts are incomplete. "
|
|
258
|
+
"Download or copy them again, or refit."
|
|
259
|
+
)
|
|
260
|
+
manifest = read_json(path)
|
|
261
|
+
if "files" not in manifest:
|
|
262
|
+
raise ArtifactVersionError(
|
|
263
|
+
f"Artifacts at {folder} were written by an older eosquality (their "
|
|
264
|
+
f"{MANIFEST_FILE} lists no files). Refit with the current version."
|
|
265
|
+
)
|
|
266
|
+
for relative, expected in manifest["files"].items():
|
|
267
|
+
file = folder / relative
|
|
268
|
+
if not file.is_file():
|
|
269
|
+
problem = "is missing"
|
|
270
|
+
elif file.stat().st_size != expected["size"]:
|
|
271
|
+
problem = f"has {file.stat().st_size} bytes, not {expected['size']}"
|
|
272
|
+
elif file_digest(file) != expected["sha256"]:
|
|
273
|
+
problem = "does not match its checksum"
|
|
274
|
+
else:
|
|
275
|
+
continue
|
|
276
|
+
raise ArtifactIntegrityError(
|
|
277
|
+
f"Artifacts at {folder} are incomplete or corrupted: {relative} "
|
|
278
|
+
f"{problem}. Download or copy them again, or refit."
|
|
279
|
+
)
|
|
280
|
+
return manifest
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def _fit_timestamp(eq) -> str:
|
|
284
|
+
"""Time of the fit (ISO 8601, UTC): the reference's, else the first training score's."""
|
|
285
|
+
if eq._shared is not None and eq._shared.metadata.fit_timestamp:
|
|
286
|
+
return eq._shared.metadata.fit_timestamp
|
|
287
|
+
for component in eq._training_components().values():
|
|
288
|
+
if component._fit_timestamp:
|
|
289
|
+
return component._fit_timestamp
|
|
290
|
+
return ""
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def _versions() -> dict[str, str]:
|
|
294
|
+
"""Versions of Python and of the packages a fit depends on."""
|
|
295
|
+
import importlib.metadata as _md
|
|
296
|
+
import platform
|
|
297
|
+
|
|
298
|
+
versions = {"python": platform.python_version()}
|
|
299
|
+
for package in _RECORDED_PACKAGES:
|
|
300
|
+
try:
|
|
301
|
+
versions[package] = _md.version(package)
|
|
302
|
+
except _md.PackageNotFoundError:
|
|
303
|
+
versions[package] = "unknown"
|
|
304
|
+
return versions
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def load(eq_cls, path: str | pathlib.Path) -> ErsiliaQuality:
|
|
308
|
+
"""Reconstruct an orchestrator from a package file written by :func:`save`.
|
|
309
|
+
|
|
310
|
+
The package is unpacked into a temporary folder that lives as long as the
|
|
311
|
+
returned instance (the training index is read from it on demand), then
|
|
312
|
+
checked and loaded by :func:`load_folder`.
|
|
313
|
+
|
|
314
|
+
Parameters
|
|
315
|
+
----------
|
|
316
|
+
eq_cls : type
|
|
317
|
+
:class:`ErsiliaQuality` (or a subclass).
|
|
318
|
+
path : str or pathlib.Path
|
|
319
|
+
Package file.
|
|
320
|
+
|
|
321
|
+
Returns
|
|
322
|
+
-------
|
|
323
|
+
ErsiliaQuality
|
|
324
|
+
A fitted instance with every score of the package.
|
|
325
|
+
|
|
326
|
+
Raises
|
|
327
|
+
------
|
|
328
|
+
FileNotFoundError
|
|
329
|
+
If ``path`` does not exist.
|
|
330
|
+
ArtifactVersionError
|
|
331
|
+
If ``path`` is a folder (the form written before format 14).
|
|
332
|
+
ArtifactIntegrityError
|
|
333
|
+
If ``path`` is not a readable zip, or its files fail the manifest check.
|
|
334
|
+
"""
|
|
335
|
+
source = pathlib.Path(path)
|
|
336
|
+
if not source.exists():
|
|
337
|
+
raise FileNotFoundError(f"No artifacts package found at: {source}")
|
|
338
|
+
if source.is_dir():
|
|
339
|
+
raise ArtifactVersionError(
|
|
340
|
+
f"{source} is a folder; this eosquality install reads artifacts "
|
|
341
|
+
"packaged as one '<eos_id>_<vN>_domain.zip' file. Refit."
|
|
342
|
+
)
|
|
343
|
+
work = tempfile.TemporaryDirectory(prefix="eosquality_load_")
|
|
344
|
+
try:
|
|
345
|
+
try:
|
|
346
|
+
unpack_zip(source, pathlib.Path(work.name))
|
|
347
|
+
except (zipfile.BadZipFile, EOFError, ValueError) as exc:
|
|
348
|
+
raise ArtifactIntegrityError(
|
|
349
|
+
f"{source} is not a readable artifacts package ({exc}). "
|
|
350
|
+
"Download or copy it again, or refit."
|
|
351
|
+
) from exc
|
|
352
|
+
instance = load_folder(eq_cls, work.name)
|
|
353
|
+
except BaseException:
|
|
354
|
+
work.cleanup()
|
|
355
|
+
raise
|
|
356
|
+
instance._unpacked = work # removed with the instance
|
|
357
|
+
return instance
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def load_folder(eq_cls, path: str | pathlib.Path) -> ErsiliaQuality:
|
|
361
|
+
"""Reconstruct an orchestrator from an unpacked artifacts folder.
|
|
362
|
+
|
|
363
|
+
Checks every file against ``manifest.json`` first, then loads
|
|
364
|
+
``reference_mode/`` and/or ``training_mode/``, whichever the fit wrote.
|
|
365
|
+
|
|
366
|
+
Parameters
|
|
367
|
+
----------
|
|
368
|
+
eq_cls : type
|
|
369
|
+
:class:`ErsiliaQuality` (or a subclass).
|
|
370
|
+
path : str or pathlib.Path
|
|
371
|
+
Artifacts folder.
|
|
372
|
+
|
|
373
|
+
Returns
|
|
374
|
+
-------
|
|
375
|
+
ErsiliaQuality
|
|
376
|
+
A fitted instance with every component found.
|
|
377
|
+
"""
|
|
378
|
+
folder = pathlib.Path(path)
|
|
379
|
+
if not folder.exists():
|
|
380
|
+
raise FileNotFoundError(f"No artifacts folder found at: {folder}")
|
|
381
|
+
if not folder.is_dir():
|
|
382
|
+
raise ValueError(
|
|
383
|
+
f"Expected a directory, got a file: {folder}. "
|
|
384
|
+
"Artifacts are stored as a folder — pass the folder path."
|
|
385
|
+
)
|
|
386
|
+
if (folder / "shared").is_dir() or (folder / "training").is_dir():
|
|
387
|
+
raise ArtifactVersionError(
|
|
388
|
+
f"Artifacts at {folder} use the old flat layout; this eosquality "
|
|
389
|
+
f"install expects {REFERENCE_DIR}/ and {TRAINING_DIR}/ subfolders. Refit."
|
|
390
|
+
)
|
|
391
|
+
has_reference = (folder / REFERENCE_DIR).is_dir()
|
|
392
|
+
has_training = (folder / TRAINING_DIR).is_dir()
|
|
393
|
+
if not has_reference and not has_training:
|
|
394
|
+
raise FileNotFoundError(
|
|
395
|
+
f"No {REFERENCE_DIR}/ or {TRAINING_DIR}/ under {folder} — nothing to load."
|
|
396
|
+
)
|
|
397
|
+
manifest = verify_manifest(folder)
|
|
398
|
+
scores = set(manifest.get("scores", []))
|
|
399
|
+
instance = eq_cls()
|
|
400
|
+
instance._max_features = manifest.get("max_features")
|
|
401
|
+
if has_reference:
|
|
402
|
+
_load_reference(instance, folder / REFERENCE_DIR, scores)
|
|
403
|
+
instance._shared.metadata.fit_timestamp = manifest.get("fit_timestamp", "")
|
|
404
|
+
if has_training:
|
|
405
|
+
_load_training(instance, folder / TRAINING_DIR, scores)
|
|
406
|
+
instance.is_fitted_ = True
|
|
407
|
+
logger.success(
|
|
408
|
+
f"Artifacts loaded from {folder} | modalities={instance.modalities_}"
|
|
409
|
+
)
|
|
410
|
+
return instance
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
def _load_training(instance, root: pathlib.Path, scores: set[str]) -> None:
|
|
414
|
+
"""Fill ``instance`` with the training scores of the manifest, under ``root``."""
|
|
415
|
+
instance._training = load_training_state(root)
|
|
416
|
+
for name, cls in TRAINING_CLASSES.items():
|
|
417
|
+
if name in scores:
|
|
418
|
+
component = cls.load(
|
|
419
|
+
root, shared=instance._shared, training=instance._training
|
|
420
|
+
)
|
|
421
|
+
setattr(instance, name, component)
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def _load_reference(instance, root: pathlib.Path, scores: set[str]) -> None:
|
|
425
|
+
"""Fill ``instance`` with the reference scores of the manifest, under ``root``."""
|
|
426
|
+
present = [n for n in SCORE_ORDER if n in scores]
|
|
427
|
+
if not present:
|
|
428
|
+
raise FileNotFoundError(f"No reference scores recorded for {root}.")
|
|
429
|
+
shared = load_shared(root)
|
|
430
|
+
check_artifacts_compatibility(shared)
|
|
431
|
+
instance._shared = shared
|
|
432
|
+
for name in present:
|
|
433
|
+
setattr(instance, name, SCORE_CLASSES[name].load(root, shared=shared))
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
def check_artifacts_compatibility(shared: SharedFitState) -> None:
|
|
437
|
+
"""Reject artifacts fit against a different reference library or major.
|
|
438
|
+
|
|
439
|
+
Every reference score is calibrated on the library, so every reference
|
|
440
|
+
artifact is checked, whether or not it reads the library at run time.
|
|
441
|
+
Artifacts fit on the canonical library must match this install's
|
|
442
|
+
:data:`LIBRARY_ID`; those fit on a custom library must record its path.
|
|
443
|
+
The package major version must also match.
|
|
444
|
+
|
|
445
|
+
Parameters
|
|
446
|
+
----------
|
|
447
|
+
shared : SharedFitState
|
|
448
|
+
Loaded shared state of the reference modality.
|
|
449
|
+
"""
|
|
450
|
+
import importlib.metadata as _md
|
|
451
|
+
|
|
452
|
+
from packaging.version import InvalidVersion, Version
|
|
453
|
+
|
|
454
|
+
library_id = shared.metadata.library_id
|
|
455
|
+
if library_id != LIBRARY_ID and not shared.metadata.library_path:
|
|
456
|
+
raise IncompatibleArtifactsError(
|
|
457
|
+
f"Artifacts were fit against reference library {library_id!r} but "
|
|
458
|
+
f"this install ships {LIBRARY_ID!r}. Install a compatible "
|
|
459
|
+
"eosquality release or refit against the current library."
|
|
460
|
+
)
|
|
461
|
+
saved = shared.metadata.eosquality_version
|
|
462
|
+
try:
|
|
463
|
+
current = _md.version("eosquality")
|
|
464
|
+
except _md.PackageNotFoundError: # running from a source tree
|
|
465
|
+
return
|
|
466
|
+
if saved == "unknown": # fitted from a source tree
|
|
467
|
+
return
|
|
468
|
+
try:
|
|
469
|
+
saved_major = Version(saved).major
|
|
470
|
+
current_major = Version(current).major
|
|
471
|
+
except InvalidVersion as exc:
|
|
472
|
+
raise IncompatibleArtifactsError(
|
|
473
|
+
f"Artifacts record an unreadable eosquality version ({saved!r}). Refit."
|
|
474
|
+
) from exc
|
|
475
|
+
if saved_major != current_major:
|
|
476
|
+
raise IncompatibleArtifactsError(
|
|
477
|
+
f"Artifacts were fit with eosquality {shared.metadata.eosquality_version} "
|
|
478
|
+
f"(major={saved_major}) but this install is {current} "
|
|
479
|
+
f"(major={current_major}). Install a matching eosquality release or refit."
|
|
480
|
+
)
|