lims-data-quality 0.1.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.
- lims_data_quality-0.1.0/LICENSE +21 -0
- lims_data_quality-0.1.0/PKG-INFO +132 -0
- lims_data_quality-0.1.0/README.md +113 -0
- lims_data_quality-0.1.0/pyproject.toml +35 -0
- lims_data_quality-0.1.0/setup.cfg +4 -0
- lims_data_quality-0.1.0/src/lims_data_quality.egg-info/PKG-INFO +132 -0
- lims_data_quality-0.1.0/src/lims_data_quality.egg-info/SOURCES.txt +20 -0
- lims_data_quality-0.1.0/src/lims_data_quality.egg-info/dependency_links.txt +1 -0
- lims_data_quality-0.1.0/src/lims_data_quality.egg-info/entry_points.txt +2 -0
- lims_data_quality-0.1.0/src/lims_data_quality.egg-info/requires.txt +2 -0
- lims_data_quality-0.1.0/src/lims_data_quality.egg-info/top_level.txt +1 -0
- lims_data_quality-0.1.0/src/lims_dq/__init__.py +21 -0
- lims_data_quality-0.1.0/src/lims_dq/audit.py +88 -0
- lims_data_quality-0.1.0/src/lims_dq/cli.py +131 -0
- lims_data_quality-0.1.0/src/lims_dq/report.py +88 -0
- lims_data_quality-0.1.0/src/lims_dq/schema.py +135 -0
- lims_data_quality-0.1.0/src/lims_dq/validators.py +145 -0
- lims_data_quality-0.1.0/tests/test_audit.py +73 -0
- lims_data_quality-0.1.0/tests/test_cli.py +103 -0
- lims_data_quality-0.1.0/tests/test_report.py +76 -0
- lims_data_quality-0.1.0/tests/test_schema.py +121 -0
- lims_data_quality-0.1.0/tests/test_validators.py +140 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sri Gorantla
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lims-data-quality
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Validate lab/LIMS data files before they hit your pipeline
|
|
5
|
+
Author: Sri Gorantla
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/sriranga13/lims-data-quality
|
|
8
|
+
Keywords: lims,lab,data-quality,validation
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: pandas>=2.0
|
|
17
|
+
Requires-Dist: openpyxl>=3.1
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# lims-data-quality
|
|
21
|
+
|
|
22
|
+
Validate lab/LIMS data files before they hit your pipeline — schema checks, row-level error reports, audit trails.
|
|
23
|
+
|
|
24
|
+
`lims-dq` checks CSV/Excel exports against a JSON schema (required columns, dtypes, ranges, regex ID patterns, allowed values) and tells you exactly which row, which column, and why it failed. Every run can append a tamper-evident audit entry (timestamp, file hash, rule set version, pass/fail counts) — a nod to 21 CFR Part 11 style traceability.
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install lims-data-quality
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Or from source:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
git clone https://github.com/sriranga13/lims-data-quality.git
|
|
36
|
+
cd lims-data-quality
|
|
37
|
+
pip install -e .
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Requires Python 3.10+.
|
|
41
|
+
|
|
42
|
+
## Quickstart
|
|
43
|
+
|
|
44
|
+
1. Describe your file with a schema (`schema.json`):
|
|
45
|
+
|
|
46
|
+
```json
|
|
47
|
+
{
|
|
48
|
+
"name": "lims-export",
|
|
49
|
+
"version": "1.0.0",
|
|
50
|
+
"columns": {
|
|
51
|
+
"sample_id": { "dtype": "string", "required": true, "pattern": "^SMP-[0-9]{6}$" },
|
|
52
|
+
"concentration": { "dtype": "float", "required": true, "min": 0.0, "max": 100.0 },
|
|
53
|
+
"unit": { "dtype": "string", "required": true, "allowed": ["mg/L", "ug/mL", "ng/uL"] },
|
|
54
|
+
"analyzed_at": { "dtype": "date", "required": false },
|
|
55
|
+
"replicates": { "dtype": "int", "required": false, "min": 1, "max": 12 }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
2. Validate:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
lims-dq validate samples.csv --schema schema.json
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Sample output:
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
lims-dq report: samples.csv
|
|
70
|
+
ruleset: lims-export v1.0.0
|
|
71
|
+
rows checked: 5 | passed: 3 | failed: 2 | errors: 7
|
|
72
|
+
|
|
73
|
+
row column rule message
|
|
74
|
+
------------------------------------------------------------------------
|
|
75
|
+
4 analyzed_at dtype 'not-a-date' is not a recognizable date
|
|
76
|
+
4 concentration constraint '150.0' is above maximum 100.0
|
|
77
|
+
4 replicates constraint '0' is below minimum 1
|
|
78
|
+
4 sample_id constraint 'BAD-ID' does not match pattern '^SMP-[0-9]{6}$'
|
|
79
|
+
4 unit constraint 'kg' is not one of ['mg/L', 'ug/mL', 'ng/uL']
|
|
80
|
+
5 concentration required required value is missing
|
|
81
|
+
5 replicates constraint '13' is above maximum 12
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
3. Machine-readable report and audit trail:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
lims-dq validate samples.csv --schema schema.json \
|
|
88
|
+
--report report.json \
|
|
89
|
+
--audit-log audit.jsonl
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
- `--report` writes the full report (summary counts + every error) as JSON.
|
|
93
|
+
- `--audit-log` appends one JSON-lines entry per run: UTC timestamp, SHA-256 of the input file, rule set name/version, rows checked/passed/failed, error count. Re-running against a modified file produces a different hash, so entries are tamper-evident.
|
|
94
|
+
|
|
95
|
+
Exit codes: `0` = all rows pass, `1` = validation failures, `2` = usage/file errors.
|
|
96
|
+
|
|
97
|
+
## Schema reference
|
|
98
|
+
|
|
99
|
+
| Key | Applies to | Meaning |
|
|
100
|
+
|------------|-------------------|--------------------------------------------------|
|
|
101
|
+
| `dtype` | all | `string`, `int`, `float`, or `date` |
|
|
102
|
+
| `required` | all | missing values fail (missing column fails too) |
|
|
103
|
+
| `pattern` | all | regex the full value must match |
|
|
104
|
+
| `min`/`max`| `int`, `float` | inclusive numeric bounds |
|
|
105
|
+
| `allowed` | all | value must be one of the listed strings |
|
|
106
|
+
|
|
107
|
+
Columns present in the file but absent from the schema are flagged as `unexpected_column` errors unless you pass `--no-strict`.
|
|
108
|
+
|
|
109
|
+
## Use as a library
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from lims_dq import load_schema, validate_dataframe, ErrorReport
|
|
113
|
+
|
|
114
|
+
ruleset = load_schema("schema.json")
|
|
115
|
+
import pandas as pd
|
|
116
|
+
df = pd.read_csv("samples.csv", dtype=str)
|
|
117
|
+
errors = validate_dataframe(df, ruleset)
|
|
118
|
+
report = ErrorReport("samples.csv", ruleset.name, ruleset.version, len(df), errors)
|
|
119
|
+
print(report.to_text()) # or report.to_json()
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Development
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
python -m venv .venv && source .venv/bin/activate
|
|
126
|
+
pip install -e . && pip install pytest
|
|
127
|
+
pytest
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# lims-data-quality
|
|
2
|
+
|
|
3
|
+
Validate lab/LIMS data files before they hit your pipeline — schema checks, row-level error reports, audit trails.
|
|
4
|
+
|
|
5
|
+
`lims-dq` checks CSV/Excel exports against a JSON schema (required columns, dtypes, ranges, regex ID patterns, allowed values) and tells you exactly which row, which column, and why it failed. Every run can append a tamper-evident audit entry (timestamp, file hash, rule set version, pass/fail counts) — a nod to 21 CFR Part 11 style traceability.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install lims-data-quality
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or from source:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
git clone https://github.com/sriranga13/lims-data-quality.git
|
|
17
|
+
cd lims-data-quality
|
|
18
|
+
pip install -e .
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Requires Python 3.10+.
|
|
22
|
+
|
|
23
|
+
## Quickstart
|
|
24
|
+
|
|
25
|
+
1. Describe your file with a schema (`schema.json`):
|
|
26
|
+
|
|
27
|
+
```json
|
|
28
|
+
{
|
|
29
|
+
"name": "lims-export",
|
|
30
|
+
"version": "1.0.0",
|
|
31
|
+
"columns": {
|
|
32
|
+
"sample_id": { "dtype": "string", "required": true, "pattern": "^SMP-[0-9]{6}$" },
|
|
33
|
+
"concentration": { "dtype": "float", "required": true, "min": 0.0, "max": 100.0 },
|
|
34
|
+
"unit": { "dtype": "string", "required": true, "allowed": ["mg/L", "ug/mL", "ng/uL"] },
|
|
35
|
+
"analyzed_at": { "dtype": "date", "required": false },
|
|
36
|
+
"replicates": { "dtype": "int", "required": false, "min": 1, "max": 12 }
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
2. Validate:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
lims-dq validate samples.csv --schema schema.json
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Sample output:
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
lims-dq report: samples.csv
|
|
51
|
+
ruleset: lims-export v1.0.0
|
|
52
|
+
rows checked: 5 | passed: 3 | failed: 2 | errors: 7
|
|
53
|
+
|
|
54
|
+
row column rule message
|
|
55
|
+
------------------------------------------------------------------------
|
|
56
|
+
4 analyzed_at dtype 'not-a-date' is not a recognizable date
|
|
57
|
+
4 concentration constraint '150.0' is above maximum 100.0
|
|
58
|
+
4 replicates constraint '0' is below minimum 1
|
|
59
|
+
4 sample_id constraint 'BAD-ID' does not match pattern '^SMP-[0-9]{6}$'
|
|
60
|
+
4 unit constraint 'kg' is not one of ['mg/L', 'ug/mL', 'ng/uL']
|
|
61
|
+
5 concentration required required value is missing
|
|
62
|
+
5 replicates constraint '13' is above maximum 12
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
3. Machine-readable report and audit trail:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
lims-dq validate samples.csv --schema schema.json \
|
|
69
|
+
--report report.json \
|
|
70
|
+
--audit-log audit.jsonl
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
- `--report` writes the full report (summary counts + every error) as JSON.
|
|
74
|
+
- `--audit-log` appends one JSON-lines entry per run: UTC timestamp, SHA-256 of the input file, rule set name/version, rows checked/passed/failed, error count. Re-running against a modified file produces a different hash, so entries are tamper-evident.
|
|
75
|
+
|
|
76
|
+
Exit codes: `0` = all rows pass, `1` = validation failures, `2` = usage/file errors.
|
|
77
|
+
|
|
78
|
+
## Schema reference
|
|
79
|
+
|
|
80
|
+
| Key | Applies to | Meaning |
|
|
81
|
+
|------------|-------------------|--------------------------------------------------|
|
|
82
|
+
| `dtype` | all | `string`, `int`, `float`, or `date` |
|
|
83
|
+
| `required` | all | missing values fail (missing column fails too) |
|
|
84
|
+
| `pattern` | all | regex the full value must match |
|
|
85
|
+
| `min`/`max`| `int`, `float` | inclusive numeric bounds |
|
|
86
|
+
| `allowed` | all | value must be one of the listed strings |
|
|
87
|
+
|
|
88
|
+
Columns present in the file but absent from the schema are flagged as `unexpected_column` errors unless you pass `--no-strict`.
|
|
89
|
+
|
|
90
|
+
## Use as a library
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from lims_dq import load_schema, validate_dataframe, ErrorReport
|
|
94
|
+
|
|
95
|
+
ruleset = load_schema("schema.json")
|
|
96
|
+
import pandas as pd
|
|
97
|
+
df = pd.read_csv("samples.csv", dtype=str)
|
|
98
|
+
errors = validate_dataframe(df, ruleset)
|
|
99
|
+
report = ErrorReport("samples.csv", ruleset.name, ruleset.version, len(df), errors)
|
|
100
|
+
print(report.to_text()) # or report.to_json()
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Development
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
python -m venv .venv && source .venv/bin/activate
|
|
107
|
+
pip install -e . && pip install pytest
|
|
108
|
+
pytest
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "lims-data-quality"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Validate lab/LIMS data files before they hit your pipeline"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Sri Gorantla" }]
|
|
13
|
+
keywords = ["lims", "lab", "data-quality", "validation"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Science/Research",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"pandas>=2.0",
|
|
22
|
+
"openpyxl>=3.1",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://github.com/sriranga13/lims-data-quality"
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
lims-dq = "lims_dq.cli:main"
|
|
30
|
+
|
|
31
|
+
[tool.setuptools.packages.find]
|
|
32
|
+
where = ["src"]
|
|
33
|
+
|
|
34
|
+
[tool.pytest.ini_options]
|
|
35
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lims-data-quality
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Validate lab/LIMS data files before they hit your pipeline
|
|
5
|
+
Author: Sri Gorantla
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/sriranga13/lims-data-quality
|
|
8
|
+
Keywords: lims,lab,data-quality,validation
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: pandas>=2.0
|
|
17
|
+
Requires-Dist: openpyxl>=3.1
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# lims-data-quality
|
|
21
|
+
|
|
22
|
+
Validate lab/LIMS data files before they hit your pipeline — schema checks, row-level error reports, audit trails.
|
|
23
|
+
|
|
24
|
+
`lims-dq` checks CSV/Excel exports against a JSON schema (required columns, dtypes, ranges, regex ID patterns, allowed values) and tells you exactly which row, which column, and why it failed. Every run can append a tamper-evident audit entry (timestamp, file hash, rule set version, pass/fail counts) — a nod to 21 CFR Part 11 style traceability.
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install lims-data-quality
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Or from source:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
git clone https://github.com/sriranga13/lims-data-quality.git
|
|
36
|
+
cd lims-data-quality
|
|
37
|
+
pip install -e .
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Requires Python 3.10+.
|
|
41
|
+
|
|
42
|
+
## Quickstart
|
|
43
|
+
|
|
44
|
+
1. Describe your file with a schema (`schema.json`):
|
|
45
|
+
|
|
46
|
+
```json
|
|
47
|
+
{
|
|
48
|
+
"name": "lims-export",
|
|
49
|
+
"version": "1.0.0",
|
|
50
|
+
"columns": {
|
|
51
|
+
"sample_id": { "dtype": "string", "required": true, "pattern": "^SMP-[0-9]{6}$" },
|
|
52
|
+
"concentration": { "dtype": "float", "required": true, "min": 0.0, "max": 100.0 },
|
|
53
|
+
"unit": { "dtype": "string", "required": true, "allowed": ["mg/L", "ug/mL", "ng/uL"] },
|
|
54
|
+
"analyzed_at": { "dtype": "date", "required": false },
|
|
55
|
+
"replicates": { "dtype": "int", "required": false, "min": 1, "max": 12 }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
2. Validate:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
lims-dq validate samples.csv --schema schema.json
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Sample output:
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
lims-dq report: samples.csv
|
|
70
|
+
ruleset: lims-export v1.0.0
|
|
71
|
+
rows checked: 5 | passed: 3 | failed: 2 | errors: 7
|
|
72
|
+
|
|
73
|
+
row column rule message
|
|
74
|
+
------------------------------------------------------------------------
|
|
75
|
+
4 analyzed_at dtype 'not-a-date' is not a recognizable date
|
|
76
|
+
4 concentration constraint '150.0' is above maximum 100.0
|
|
77
|
+
4 replicates constraint '0' is below minimum 1
|
|
78
|
+
4 sample_id constraint 'BAD-ID' does not match pattern '^SMP-[0-9]{6}$'
|
|
79
|
+
4 unit constraint 'kg' is not one of ['mg/L', 'ug/mL', 'ng/uL']
|
|
80
|
+
5 concentration required required value is missing
|
|
81
|
+
5 replicates constraint '13' is above maximum 12
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
3. Machine-readable report and audit trail:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
lims-dq validate samples.csv --schema schema.json \
|
|
88
|
+
--report report.json \
|
|
89
|
+
--audit-log audit.jsonl
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
- `--report` writes the full report (summary counts + every error) as JSON.
|
|
93
|
+
- `--audit-log` appends one JSON-lines entry per run: UTC timestamp, SHA-256 of the input file, rule set name/version, rows checked/passed/failed, error count. Re-running against a modified file produces a different hash, so entries are tamper-evident.
|
|
94
|
+
|
|
95
|
+
Exit codes: `0` = all rows pass, `1` = validation failures, `2` = usage/file errors.
|
|
96
|
+
|
|
97
|
+
## Schema reference
|
|
98
|
+
|
|
99
|
+
| Key | Applies to | Meaning |
|
|
100
|
+
|------------|-------------------|--------------------------------------------------|
|
|
101
|
+
| `dtype` | all | `string`, `int`, `float`, or `date` |
|
|
102
|
+
| `required` | all | missing values fail (missing column fails too) |
|
|
103
|
+
| `pattern` | all | regex the full value must match |
|
|
104
|
+
| `min`/`max`| `int`, `float` | inclusive numeric bounds |
|
|
105
|
+
| `allowed` | all | value must be one of the listed strings |
|
|
106
|
+
|
|
107
|
+
Columns present in the file but absent from the schema are flagged as `unexpected_column` errors unless you pass `--no-strict`.
|
|
108
|
+
|
|
109
|
+
## Use as a library
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from lims_dq import load_schema, validate_dataframe, ErrorReport
|
|
113
|
+
|
|
114
|
+
ruleset = load_schema("schema.json")
|
|
115
|
+
import pandas as pd
|
|
116
|
+
df = pd.read_csv("samples.csv", dtype=str)
|
|
117
|
+
errors = validate_dataframe(df, ruleset)
|
|
118
|
+
report = ErrorReport("samples.csv", ruleset.name, ruleset.version, len(df), errors)
|
|
119
|
+
print(report.to_text()) # or report.to_json()
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Development
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
python -m venv .venv && source .venv/bin/activate
|
|
126
|
+
pip install -e . && pip install pytest
|
|
127
|
+
pytest
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/lims_data_quality.egg-info/PKG-INFO
|
|
5
|
+
src/lims_data_quality.egg-info/SOURCES.txt
|
|
6
|
+
src/lims_data_quality.egg-info/dependency_links.txt
|
|
7
|
+
src/lims_data_quality.egg-info/entry_points.txt
|
|
8
|
+
src/lims_data_quality.egg-info/requires.txt
|
|
9
|
+
src/lims_data_quality.egg-info/top_level.txt
|
|
10
|
+
src/lims_dq/__init__.py
|
|
11
|
+
src/lims_dq/audit.py
|
|
12
|
+
src/lims_dq/cli.py
|
|
13
|
+
src/lims_dq/report.py
|
|
14
|
+
src/lims_dq/schema.py
|
|
15
|
+
src/lims_dq/validators.py
|
|
16
|
+
tests/test_audit.py
|
|
17
|
+
tests/test_cli.py
|
|
18
|
+
tests/test_report.py
|
|
19
|
+
tests/test_schema.py
|
|
20
|
+
tests/test_validators.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
lims_dq
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""lims-dq: validate lab/LIMS data files before they enter your pipeline."""
|
|
2
|
+
|
|
3
|
+
from .audit import AuditEntry, read_audit_log, write_audit_log
|
|
4
|
+
from .report import ErrorReport, ValidationError
|
|
5
|
+
from .schema import Rule, RuleSet, SchemaError, load_schema
|
|
6
|
+
from .validators import validate_dataframe
|
|
7
|
+
|
|
8
|
+
__version__ = "0.1.0"
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"AuditEntry",
|
|
12
|
+
"ErrorReport",
|
|
13
|
+
"Rule",
|
|
14
|
+
"RuleSet",
|
|
15
|
+
"SchemaError",
|
|
16
|
+
"ValidationError",
|
|
17
|
+
"load_schema",
|
|
18
|
+
"read_audit_log",
|
|
19
|
+
"validate_dataframe",
|
|
20
|
+
"write_audit_log",
|
|
21
|
+
]
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Minimal audit trail: who validated what, when, against which rules.
|
|
2
|
+
|
|
3
|
+
Each validation run appends one JSON-lines entry recording the UTC
|
|
4
|
+
timestamp, the SHA-256 hash of the input file, the rule set name and
|
|
5
|
+
version, and the pass/fail counts. The file hash makes the entry
|
|
6
|
+
tamper-evident: re-running against a modified file produces a different
|
|
7
|
+
hash. A nod to 21 CFR Part 11 style traceability — not a compliance
|
|
8
|
+
certification.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import hashlib
|
|
14
|
+
import json
|
|
15
|
+
from dataclasses import asdict, dataclass
|
|
16
|
+
from datetime import datetime, timezone
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class AuditEntry:
|
|
23
|
+
timestamp_utc: str
|
|
24
|
+
filename: str
|
|
25
|
+
file_sha256: str
|
|
26
|
+
ruleset_name: str
|
|
27
|
+
ruleset_version: str
|
|
28
|
+
rows_checked: int
|
|
29
|
+
rows_passed: int
|
|
30
|
+
rows_failed: int
|
|
31
|
+
error_count: int
|
|
32
|
+
passed: bool
|
|
33
|
+
|
|
34
|
+
def to_dict(self) -> dict[str, Any]:
|
|
35
|
+
return asdict(self)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def sha256_of_file(path: str | Path) -> str:
|
|
39
|
+
digest = hashlib.sha256()
|
|
40
|
+
with open(path, "rb") as fh:
|
|
41
|
+
for chunk in iter(lambda: fh.read(65536), b""):
|
|
42
|
+
digest.update(chunk)
|
|
43
|
+
return digest.hexdigest()
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def build_entry(
|
|
47
|
+
*,
|
|
48
|
+
filename: str | Path,
|
|
49
|
+
ruleset_name: str,
|
|
50
|
+
ruleset_version: str,
|
|
51
|
+
rows_checked: int,
|
|
52
|
+
rows_passed: int,
|
|
53
|
+
rows_failed: int,
|
|
54
|
+
error_count: int,
|
|
55
|
+
) -> AuditEntry:
|
|
56
|
+
path = Path(filename)
|
|
57
|
+
return AuditEntry(
|
|
58
|
+
timestamp_utc=datetime.now(timezone.utc).isoformat(timespec="seconds"),
|
|
59
|
+
filename=path.name,
|
|
60
|
+
file_sha256=sha256_of_file(path),
|
|
61
|
+
ruleset_name=ruleset_name,
|
|
62
|
+
ruleset_version=ruleset_version,
|
|
63
|
+
rows_checked=rows_checked,
|
|
64
|
+
rows_passed=rows_passed,
|
|
65
|
+
rows_failed=rows_failed,
|
|
66
|
+
error_count=error_count,
|
|
67
|
+
passed=error_count == 0,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def write_audit_log(entry: AuditEntry, log_path: str | Path) -> None:
|
|
72
|
+
"""Append one entry (as a single JSON line) to the audit log."""
|
|
73
|
+
log_path = Path(log_path)
|
|
74
|
+
if log_path.parent != Path("."):
|
|
75
|
+
log_path.parent.mkdir(parents=True, exist_ok=True)
|
|
76
|
+
with open(log_path, "a", encoding="utf-8") as fh:
|
|
77
|
+
fh.write(json.dumps(entry.to_dict()) + "\n")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def read_audit_log(log_path: str | Path) -> list[dict[str, Any]]:
|
|
81
|
+
"""Read all entries from a JSON-lines audit log."""
|
|
82
|
+
entries = []
|
|
83
|
+
with open(log_path, encoding="utf-8") as fh:
|
|
84
|
+
for line in fh:
|
|
85
|
+
line = line.strip()
|
|
86
|
+
if line:
|
|
87
|
+
entries.append(json.loads(line))
|
|
88
|
+
return entries
|