rainbow-api 1.5.1__tar.gz → 1.5.3__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.
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/CHANGELOG.md +64 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/PKG-INFO +1 -1
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/pyproject.toml +1 -1
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/agilent/chemstation.py +8 -2
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/asm.py +416 -10
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow_api.egg-info/PKG-INFO +1 -1
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/COPYING +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/COPYING.LESSER +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/MANIFEST.in +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/README.md +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/__init__.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/_arguments.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/_binning.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/agilent/__init__.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/agilent/_chdelta.pyx +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/agilent/_msprofile.pyx +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/agilent/_uvdelta.pyx +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/agilent/acaml.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/agilent/masshunter.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/agilent/method.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/agilent/openlab.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/agilent/sequence.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/datadirectory.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/datafile.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/datasequence.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/debug/__init__.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/debug/_util.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/debug/chemstation_header.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/debug/chemstation_text.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/debug/dotnet.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/debug/ini.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/debug/method.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/debug/mzxml.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/debug/reg.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/debug/waters_header.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/debug/waters_inf.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/debug/xml.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/waters/__init__.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow/waters/masslynx.py +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow_api.egg-info/SOURCES.txt +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow_api.egg-info/dependency_links.txt +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow_api.egg-info/requires.txt +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/rainbow_api.egg-info/top_level.txt +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/setup.cfg +0 -0
- {rainbow_api-1.5.1 → rainbow_api-1.5.3}/setup.py +0 -0
|
@@ -3,6 +3,70 @@
|
|
|
3
3
|
All notable changes to `rainbow-api` are documented here. This project adheres
|
|
4
4
|
to [Semantic Versioning](https://semver.org/).
|
|
5
5
|
|
|
6
|
+
## [1.5.3] - 2026-10-01
|
|
7
|
+
|
|
8
|
+
### Fixed
|
|
9
|
+
- **`rb.from_asm` dropped a foreign sample's name and all of its custom
|
|
10
|
+
fields.** rainbow's own writer puts everything it knows about a sample in the
|
|
11
|
+
`sample identifier`, so reading only that was enough to round trip its own
|
|
12
|
+
output. A foreign writer distinguishes the two: an Empower or OpenLab export
|
|
13
|
+
names the injection in `written name` (`"NB5-P1A11"`) and keys it in `sample
|
|
14
|
+
identifier` (`"133872"`), and hangs the fields the schema has no place for —
|
|
15
|
+
`Notebook`, `MaterialIdentifier`, `SampleWeight` — off a custom information
|
|
16
|
+
aggregate document. rainbow read neither, leaving a run with no
|
|
17
|
+
human-readable handle on its sample and no way to reach those fields short of
|
|
18
|
+
re-walking the raw JSON. This is not cosmetic: Empower records the registered
|
|
19
|
+
sample's id in `MaterialIdentifier`, and that is the key that matches an
|
|
20
|
+
injection to its sample downstream. Both are now read into
|
|
21
|
+
`metadata["sample_name"]` and `metadata["sample_custom"]`, and written again
|
|
22
|
+
on export, so the lap is lossless.
|
|
23
|
+
|
|
24
|
+
All four datum types the schema admits are carried, each as the Python type
|
|
25
|
+
that distinguishes it — `str`, `bool`, a `datetime` for a timestamp (the datum
|
|
26
|
+
type is the only thing that tells one from the string it is spelled as), and a
|
|
27
|
+
number kept as an `int` where it came in as one. A double's optional `unit`
|
|
28
|
+
rides along as the `{"value", "unit"}` shape `injection_volume` already uses.
|
|
29
|
+
Either spelling of a value is read, the bare scalar or the `{"@type",
|
|
30
|
+
"value"}` object form. Nothing is coerced: an unreadable or ambiguous datum
|
|
31
|
+
type, a value not of the type its datum claims, a number JSON cannot carry, an
|
|
32
|
+
integer too large for a float, a duplicate or unusable `datum label`, and a
|
|
33
|
+
`unit` beside anything but a double are each warned about and dropped, once per
|
|
34
|
+
document. See the round-trip page for the full table.
|
|
35
|
+
|
|
36
|
+
- **A sample's fields could be composed from different samples.** The
|
|
37
|
+
identifier, name, custom fields and vial position were each read field by
|
|
38
|
+
field as the measurements went past, so a document holding several injections
|
|
39
|
+
— which `rb.from_asm` merges into one directory — could report an identifier
|
|
40
|
+
from the first beside a `MaterialIdentifier` from the second. A
|
|
41
|
+
`MaterialIdentifier` is a join key, and one silently attached to the wrong
|
|
42
|
+
sample is worse than the absent one it replaces; so is a vial position, which
|
|
43
|
+
is what a plate map is looked up by. All four now come from one measurement's
|
|
44
|
+
sample document or from none. The document chosen is the first carrying a
|
|
45
|
+
`sample identifier`, since that is what everything downstream falls back to,
|
|
46
|
+
else the first carrying anything at all. `rb.sequence_from_asm` is unaffected:
|
|
47
|
+
it already built one directory per injection.
|
|
48
|
+
|
|
49
|
+
`description`, `sample role type` and `blank` are still dropped, so a sample
|
|
50
|
+
document does not round trip whole — only the identifier, the name, the vial
|
|
51
|
+
position and the custom fields do.
|
|
52
|
+
|
|
53
|
+
## [1.5.2] - 2026-09-09
|
|
54
|
+
|
|
55
|
+
### Fixed
|
|
56
|
+
- **A ChemStation `.ms` file with busy scans failed to read at all.** Reading
|
|
57
|
+
an Agilent `.D` whose MS scans hold more than 16383 mz-intensity pairs raised
|
|
58
|
+
`ValueError: strides is incompatible with shape of requested array and size
|
|
59
|
+
of buffer`, and nothing in the run could be read. `parse_ms` held each scan's
|
|
60
|
+
pair count in a numpy `uint16` array, so the multiply that turns a pair count
|
|
61
|
+
into a byte count was `uint16` arithmetic and wrapped at 65536: a busy scan
|
|
62
|
+
asked for a fraction of its own bytes, and the reader carried on from inside
|
|
63
|
+
it. numpy 1 widened that multiply and hid the wrap, so this reaches anyone
|
|
64
|
+
who has moved to numpy 2. How busy a scan is tracks how many ions it
|
|
65
|
+
detected, which is why it presented as large or intense files failing while
|
|
66
|
+
smaller ones from the same batch read: in the reported case a run whose
|
|
67
|
+
busiest scan held 12705 pairs read, and one holding 25201 did not. Reported
|
|
68
|
+
with both files in [#75](https://github.com/evanyeyeye/rainbow/issues/75).
|
|
69
|
+
|
|
6
70
|
## [1.5.1] - 2026-08-27
|
|
7
71
|
|
|
8
72
|
### Fixed
|
|
@@ -842,9 +842,15 @@ def parse_ms(path, display_precision=0, bin_width=1.0, labels_only=False):
|
|
|
842
842
|
f.read(2)
|
|
843
843
|
times[i] = int_unpack(f.read(4))[0]
|
|
844
844
|
f.read(6)
|
|
845
|
-
|
|
845
|
+
# The multiply below has to happen on a Python int. Read back out of
|
|
846
|
+
# pair_counts it would be a numpy uint16, which wraps at 65536 under
|
|
847
|
+
# NEP 50, so a scan of more than 16383 pairs would ask for its byte
|
|
848
|
+
# count modulo 65536 and the reader would resume inside that scan.
|
|
849
|
+
# numpy 1 widened the multiply to int64, which hid this.
|
|
850
|
+
pair_count = short_unpack(f.read(2))[0]
|
|
851
|
+
pair_counts[i] = pair_count
|
|
846
852
|
f.read(4)
|
|
847
|
-
pair_bytes = f.read(
|
|
853
|
+
pair_bytes = f.read(pair_count * 4)
|
|
848
854
|
pair_bytearr.extend(pair_bytes)
|
|
849
855
|
f.read(10)
|
|
850
856
|
|
|
@@ -28,8 +28,10 @@ rainbow's data model already is an ASM data cube:
|
|
|
28
28
|
Where the metadata supports it, the envelope is filled in: the instrument and
|
|
29
29
|
its modules become the device system document, the injection volume an
|
|
30
30
|
injection document, and (for a sequence read with peaks) the integrated peaks a
|
|
31
|
-
processed data aggregate document.
|
|
32
|
-
|
|
31
|
+
processed data aggregate document. The `sample_name` and `sample_custom` keys
|
|
32
|
+
are inputs as much as outputs: set either on a run read from a vendor file and
|
|
33
|
+
the export carries it. A DataSequence becomes one aggregate document with one
|
|
34
|
+
chromatography document per injection.
|
|
33
35
|
|
|
34
36
|
Most detectors are exported faithfully: UV as absorbance, CAD and FID as
|
|
35
37
|
electric current, ELSD as light intensity, each a 1-D chromatogram cube with its
|
|
@@ -1492,6 +1494,149 @@ def _detector_measurement(datafile, metadata, descriptor, options,
|
|
|
1492
1494
|
return measurement
|
|
1493
1495
|
|
|
1494
1496
|
|
|
1497
|
+
def _warn_once(seen):
|
|
1498
|
+
"""A warn function that says each distinct message once within ``seen``."""
|
|
1499
|
+
def warn(message):
|
|
1500
|
+
if message in seen:
|
|
1501
|
+
return
|
|
1502
|
+
seen.add(message)
|
|
1503
|
+
warnings.warn(message)
|
|
1504
|
+
return warn
|
|
1505
|
+
|
|
1506
|
+
|
|
1507
|
+
def _warn_custom_write(options, label, complaint):
|
|
1508
|
+
"""Reports a custom field that cannot be written, once per document.
|
|
1509
|
+
|
|
1510
|
+
One ``sample_custom`` mapping is written into every measurement, so keyed by
|
|
1511
|
+
label and complaint rather than warned as each is built: otherwise a PDA run
|
|
1512
|
+
says the same thing four times, and two bad fields collapse into one.
|
|
1513
|
+
"""
|
|
1514
|
+
_warn_once_per_run(options, ("custom-write", label, complaint),
|
|
1515
|
+
"custom field {!r} {}".format(label, complaint))
|
|
1516
|
+
|
|
1517
|
+
|
|
1518
|
+
def _custom_datum_entry(label, value, options):
|
|
1519
|
+
"""One custom information document, or None if the value cannot be written.
|
|
1520
|
+
|
|
1521
|
+
The datum type follows the Python type, which is all a plain mapping records:
|
|
1522
|
+
a string, a bool, a datetime and a number each have exactly one of the four
|
|
1523
|
+
scalar spellings the schema admits. A value of no recognised type is reported
|
|
1524
|
+
rather than coerced -- ``str()`` on it would publish a number or a date as
|
|
1525
|
+
text and change what a reader gets back.
|
|
1526
|
+
"""
|
|
1527
|
+
from datetime import datetime
|
|
1528
|
+
|
|
1529
|
+
unit = None
|
|
1530
|
+
if isinstance(value, dict):
|
|
1531
|
+
# The {"value", "unit"} shape a double with a unit is read back as. Only
|
|
1532
|
+
# a double may carry a unit, so one arriving beside anything else is said
|
|
1533
|
+
# out loud rather than quietly thrown away with the rest of the dict.
|
|
1534
|
+
unit = _text(value.get("unit"))
|
|
1535
|
+
if value.get("value") is None:
|
|
1536
|
+
_warn_custom_write(
|
|
1537
|
+
options, label,
|
|
1538
|
+
"holds a mapping with no usable 'value' member ({!r}); it is "
|
|
1539
|
+
"dropped.".format(value))
|
|
1540
|
+
return None
|
|
1541
|
+
value = value["value"]
|
|
1542
|
+
if isinstance(value, bool):
|
|
1543
|
+
# Tested before the number branch: a bool is an int in Python and would
|
|
1544
|
+
# otherwise be written as 1.0, which is not what was recorded.
|
|
1545
|
+
datum = {"scalar boolean datum": value}
|
|
1546
|
+
elif isinstance(value, str):
|
|
1547
|
+
datum = {"scalar string datum": value}
|
|
1548
|
+
elif isinstance(value, datetime):
|
|
1549
|
+
# Through the same gate as every other timestamp rather than
|
|
1550
|
+
# isoformat() direct, so that a naive one picks up the caller's
|
|
1551
|
+
# utc_offset and warns once when there is none. The schema's date-time
|
|
1552
|
+
# format is RFC 3339 and requires an offset, so writing a naive
|
|
1553
|
+
# datetime's isoformat() emitted a value the document then failed to
|
|
1554
|
+
# validate on. A plain `date` is not handled at all: its isoformat() is
|
|
1555
|
+
# a date, not a date-time, and midnight in an invented zone is the kind
|
|
1556
|
+
# of guess this reader refuses elsewhere.
|
|
1557
|
+
stamped = options.timestamp(value.isoformat())
|
|
1558
|
+
if stamped is None:
|
|
1559
|
+
return None
|
|
1560
|
+
datum = {"scalar timestamp datum": stamped}
|
|
1561
|
+
elif isinstance(value, (int, float)):
|
|
1562
|
+
if isinstance(value, int):
|
|
1563
|
+
# Before isfinite, not after: JSON integer literals are unbounded and
|
|
1564
|
+
# math.isfinite raises OverflowError on one too large for a float,
|
|
1565
|
+
# out of the middle of the export, instead of reporting it.
|
|
1566
|
+
try:
|
|
1567
|
+
float(value)
|
|
1568
|
+
except OverflowError:
|
|
1569
|
+
_warn_custom_write(
|
|
1570
|
+
options, label,
|
|
1571
|
+
"holds an integer too large to carry as a double; it is "
|
|
1572
|
+
"dropped.")
|
|
1573
|
+
return None
|
|
1574
|
+
if not math.isfinite(value):
|
|
1575
|
+
# json.dumps writes NaN and Infinity as bare literals no conforming
|
|
1576
|
+
# reader accepts, so to_asm_str would raise after to_asm looked fine.
|
|
1577
|
+
_warn_custom_write(
|
|
1578
|
+
options, label,
|
|
1579
|
+
"holds {!r}, which is not a value JSON can carry; it is "
|
|
1580
|
+
"dropped.".format(value))
|
|
1581
|
+
return None
|
|
1582
|
+
# Rounded only when a precision was asked for, so an int stays an int
|
|
1583
|
+
# by default the way every other number rainbow reads back does.
|
|
1584
|
+
if options.decimals is not None:
|
|
1585
|
+
value = options.scalar(value)
|
|
1586
|
+
datum = {"scalar double datum": value}
|
|
1587
|
+
if unit:
|
|
1588
|
+
datum["unit"] = unit
|
|
1589
|
+
return datum
|
|
1590
|
+
else:
|
|
1591
|
+
_warn_custom_write(
|
|
1592
|
+
options, label,
|
|
1593
|
+
"holds {}, which rainbow cannot write as an ASM scalar datum; it "
|
|
1594
|
+
"is dropped rather than coerced to text."
|
|
1595
|
+
.format(type(value).__name__))
|
|
1596
|
+
return None
|
|
1597
|
+
if unit:
|
|
1598
|
+
# Reached only by the string, bool and timestamp branches, none of which
|
|
1599
|
+
# the schema lets carry a unit.
|
|
1600
|
+
_warn_custom_write(
|
|
1601
|
+
options, label,
|
|
1602
|
+
"carries the unit {!r} beside a {}, which only a double may do; "
|
|
1603
|
+
"the unit is dropped.".format(unit, next(iter(datum))))
|
|
1604
|
+
return datum
|
|
1605
|
+
|
|
1606
|
+
|
|
1607
|
+
def _custom_information_document(fields, options):
|
|
1608
|
+
"""A ``{label: value}`` mapping as a custom information aggregate document.
|
|
1609
|
+
|
|
1610
|
+
The inverse of :func:`_custom_fields`. ``@index`` is written 1..n in the
|
|
1611
|
+
mapping's order, which is the order the fields were read in.
|
|
1612
|
+
"""
|
|
1613
|
+
if fields is None:
|
|
1614
|
+
return None
|
|
1615
|
+
if not isinstance(fields, dict):
|
|
1616
|
+
_warn_once_per_run(
|
|
1617
|
+
options, "custom-not-a-mapping",
|
|
1618
|
+
"metadata['sample_custom'] holds {}, not a mapping of labels to "
|
|
1619
|
+
"values; no custom fields are written."
|
|
1620
|
+
.format(type(fields).__name__))
|
|
1621
|
+
return None
|
|
1622
|
+
entries = []
|
|
1623
|
+
for label, value in fields.items():
|
|
1624
|
+
if not isinstance(label, str) or not label:
|
|
1625
|
+
_warn_once_per_run(
|
|
1626
|
+
options, ("custom-label", repr(label)),
|
|
1627
|
+
"custom field labelled {!r} is not a usable datum label; the "
|
|
1628
|
+
"field is dropped.".format(label))
|
|
1629
|
+
continue
|
|
1630
|
+
datum = _custom_datum_entry(label, value, options)
|
|
1631
|
+
if datum is None:
|
|
1632
|
+
continue
|
|
1633
|
+
entries.append(dict({"@index": len(entries) + 1,
|
|
1634
|
+
"datum label": label}, **datum))
|
|
1635
|
+
if not entries:
|
|
1636
|
+
return None
|
|
1637
|
+
return {"custom information document": entries}
|
|
1638
|
+
|
|
1639
|
+
|
|
1495
1640
|
def _note_relabeled_measure(measurement, descriptor):
|
|
1496
1641
|
"""Records the real quantity of a channel relabeled to absorbance."""
|
|
1497
1642
|
measurement["custom information aggregate document"] = {
|
|
@@ -1602,6 +1747,13 @@ def _measurement(datafile, metadata, control, cube_key, cube, options,
|
|
|
1602
1747
|
identifier=None):
|
|
1603
1748
|
"""Wraps a data cube in the shared measurement envelope."""
|
|
1604
1749
|
sample = {"sample identifier": metadata.get("sample", "unknown")}
|
|
1750
|
+
name = metadata.get("sample_name")
|
|
1751
|
+
if isinstance(name, str) and name:
|
|
1752
|
+
sample["written name"] = name
|
|
1753
|
+
custom = _custom_information_document(
|
|
1754
|
+
metadata.get("sample_custom"), options)
|
|
1755
|
+
if custom:
|
|
1756
|
+
sample["custom information aggregate document"] = custom
|
|
1605
1757
|
if "vialpos" in metadata:
|
|
1606
1758
|
sample["location identifier"] = str(metadata["vialpos"])
|
|
1607
1759
|
measurement = {
|
|
@@ -2027,6 +2179,11 @@ def from_asm(document, name="asm"):
|
|
|
2027
2179
|
|
|
2028
2180
|
datafiles = []
|
|
2029
2181
|
peak_groups = []
|
|
2182
|
+
# Over every injection at once, not one at a time: this directory holds the
|
|
2183
|
+
# merge of all of them, so the sample has to be chosen with all of them in
|
|
2184
|
+
# view. Choosing per injection and keeping the first answer lost the
|
|
2185
|
+
# identifier whenever an earlier injection supplied only a name.
|
|
2186
|
+
_absorb_samples(_measurements_of(documents), metadata)
|
|
2030
2187
|
for lc_document in documents:
|
|
2031
2188
|
_absorb_lc_document(lc_document, metadata, datafiles, peak_groups)
|
|
2032
2189
|
return _directory(name, datafiles, metadata, peak_groups)
|
|
@@ -2191,6 +2348,7 @@ def sequence_from_asm(document, name="asm"):
|
|
|
2191
2348
|
injection_metadata = {}
|
|
2192
2349
|
datafiles = []
|
|
2193
2350
|
peak_groups = []
|
|
2351
|
+
_absorb_samples(_measurements_of([lc_document]), injection_metadata)
|
|
2194
2352
|
_absorb_lc_document(
|
|
2195
2353
|
lc_document, injection_metadata, datafiles, peak_groups)
|
|
2196
2354
|
# The injection identifier is the name the injection was exported
|
|
@@ -2242,6 +2400,18 @@ def _directory(name, datafiles, metadata, peak_groups):
|
|
|
2242
2400
|
return datadir
|
|
2243
2401
|
|
|
2244
2402
|
|
|
2403
|
+
def _measurements_of(lc_documents):
|
|
2404
|
+
"""Every measurement document across ``lc_documents``, in document order."""
|
|
2405
|
+
measurements = []
|
|
2406
|
+
for lc_document in lc_documents:
|
|
2407
|
+
if not isinstance(lc_document, dict):
|
|
2408
|
+
continue
|
|
2409
|
+
measurements += _as_documents(
|
|
2410
|
+
_first(lc_document.get("measurement aggregate document"))
|
|
2411
|
+
.get("measurement document"))
|
|
2412
|
+
return measurements
|
|
2413
|
+
|
|
2414
|
+
|
|
2245
2415
|
def _absorb_lc_document(lc_document, metadata, datafiles, peak_groups):
|
|
2246
2416
|
"""Reads one liquid chromatography document into datafiles and metadata."""
|
|
2247
2417
|
if not isinstance(lc_document, dict):
|
|
@@ -2274,15 +2444,86 @@ def _absorb_lc_document(lc_document, metadata, datafiles, peak_groups):
|
|
|
2274
2444
|
peak_groups.append(group)
|
|
2275
2445
|
|
|
2276
2446
|
|
|
2447
|
+
# The three metadata keys one sample document supplies. They are taken together
|
|
2448
|
+
# or not at all, so they always describe the same sample.
|
|
2449
|
+
_SAMPLE_KEYS = ("sample", "sample_name", "sample_custom")
|
|
2450
|
+
|
|
2451
|
+
|
|
2452
|
+
def _absorb_samples(measurements, metadata):
|
|
2453
|
+
"""Lifts ONE measurement's sample document up to directory metadata.
|
|
2454
|
+
|
|
2455
|
+
rainbow's own writer puts everything it knows about a sample in the
|
|
2456
|
+
identifier, so reading only that was enough to round trip its own output. A
|
|
2457
|
+
foreign writer distinguishes the two: an Empower export names the injection
|
|
2458
|
+
in ``written name`` ("NB5-P1A11") and keys it in ``sample identifier``
|
|
2459
|
+
("133872"). Dropping the name loses the only human-readable handle the
|
|
2460
|
+
document carries on its sample, and the custom fields go with it.
|
|
2461
|
+
|
|
2462
|
+
The sample document is read as a unit: identifier, name, custom fields and
|
|
2463
|
+
vial position all come from one of them or from none. Reading them field by
|
|
2464
|
+
field as each measurement went past composed them instead: :func:`from_asm`
|
|
2465
|
+
merges every injection in a document into one directory, so an identifier
|
|
2466
|
+
from the first injection could end up beside a name and a
|
|
2467
|
+
``MaterialIdentifier`` from the second. A ``MaterialIdentifier`` is a join
|
|
2468
|
+
key, and one silently attached to the wrong sample is worse than the absent
|
|
2469
|
+
one it replaces -- and so is a vial position, which is what a plate map is
|
|
2470
|
+
looked up by. Hence one pass here, over a list, rather than a field at a
|
|
2471
|
+
time as each measurement is read.
|
|
2472
|
+
|
|
2473
|
+
Which document: the first that carries a *sample identifier*, since that is
|
|
2474
|
+
what everything downstream falls back to, else the first that carries
|
|
2475
|
+
anything at all. A run whose opening channel records a name against an
|
|
2476
|
+
"unknown" identifier would otherwise lose the identifier a later channel had.
|
|
2477
|
+
|
|
2478
|
+
Called once per directory, over every measurement that directory will hold.
|
|
2479
|
+
:func:`from_asm` merges a file's injections into one directory and so passes
|
|
2480
|
+
all of them; :func:`sequence_from_asm` builds one directory per injection and
|
|
2481
|
+
passes one at a time. Running it per injection and keeping the first answer
|
|
2482
|
+
reintroduced the same loss one level up: an opening injection with only a
|
|
2483
|
+
name suppressed the identifier a later one carried.
|
|
2484
|
+
"""
|
|
2485
|
+
documents = [_first(measurement.get("sample document"))
|
|
2486
|
+
for measurement in measurements
|
|
2487
|
+
if isinstance(measurement, dict)]
|
|
2488
|
+
identified = [sample for sample in documents
|
|
2489
|
+
if (_text(sample.get("sample identifier")) or "unknown")
|
|
2490
|
+
!= "unknown"]
|
|
2491
|
+
said = set()
|
|
2492
|
+
chosen = None
|
|
2493
|
+
for sample in identified[:1] or documents:
|
|
2494
|
+
identifier = _text(sample.get("sample identifier"))
|
|
2495
|
+
name = _text(sample.get("written name"))
|
|
2496
|
+
custom = _custom_fields(sample, said)
|
|
2497
|
+
if identifier and identifier != "unknown":
|
|
2498
|
+
metadata["sample"] = identifier
|
|
2499
|
+
if name:
|
|
2500
|
+
metadata["sample_name"] = name
|
|
2501
|
+
if custom:
|
|
2502
|
+
metadata["sample_custom"] = custom
|
|
2503
|
+
# Keyed on what was actually absorbed, not on what the document held: a
|
|
2504
|
+
# sample document carrying only the placeholder "unknown" identifier
|
|
2505
|
+
# says something without supplying anything, and stopping on it left the
|
|
2506
|
+
# run with no sample at all.
|
|
2507
|
+
if any(key in metadata for key in _SAMPLE_KEYS):
|
|
2508
|
+
chosen = sample
|
|
2509
|
+
break
|
|
2510
|
+
if chosen is None:
|
|
2511
|
+
# Nothing identified the sample anywhere. The vial position is still
|
|
2512
|
+
# worth having, so it comes from the first document that records one.
|
|
2513
|
+
chosen = next((sample for sample in documents
|
|
2514
|
+
if _text(sample.get("location identifier"))), None)
|
|
2515
|
+
if chosen is not None:
|
|
2516
|
+
location = _text(chosen.get("location identifier"))
|
|
2517
|
+
if location:
|
|
2518
|
+
metadata["vialpos"] = location
|
|
2519
|
+
|
|
2520
|
+
|
|
2277
2521
|
def _absorb_envelope(measurement, metadata):
|
|
2278
|
-
"""Lifts a measurement's envelope fields up to directory metadata.
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
location = _text(sample.get("location identifier"))
|
|
2284
|
-
if location:
|
|
2285
|
-
metadata.setdefault("vialpos", location)
|
|
2522
|
+
"""Lifts a measurement's envelope fields up to directory metadata.
|
|
2523
|
+
|
|
2524
|
+
The sample document is not read here, vial position included: it is taken as
|
|
2525
|
+
a unit by :func:`_absorb_samples`, once per directory, before this runs.
|
|
2526
|
+
"""
|
|
2286
2527
|
date = _text(measurement.get("measurement time"))
|
|
2287
2528
|
if date:
|
|
2288
2529
|
metadata.setdefault("date", date)
|
|
@@ -2300,6 +2541,171 @@ def _absorb_envelope(measurement, metadata):
|
|
|
2300
2541
|
"injection_volume", {"value": volume, "unit": "µL"})
|
|
2301
2542
|
|
|
2302
2543
|
|
|
2544
|
+
# A date-time has a time of day; a date does not. ASM types every timestamp as a
|
|
2545
|
+
# full date-time, so the separator is what tells a usable value from a date. The
|
|
2546
|
+
# case is not fixed: RFC 3339 permits a lowercase `t`, and _parse_iso already
|
|
2547
|
+
# goes out of its way to accept a lowercase `z` for the same reason. A space is
|
|
2548
|
+
# accepted too -- the schema's format does not allow one, but _parse_iso reads it
|
|
2549
|
+
# and the re-export writes it back as `T`, so taking it in normalizes it. The
|
|
2550
|
+
# digits either side are what keep a bare date out.
|
|
2551
|
+
_HAS_TIME_OF_DAY = re.compile(r"\d[Tt ]\d")
|
|
2552
|
+
|
|
2553
|
+
|
|
2554
|
+
# A custom information document carries its value under a key naming the datum's
|
|
2555
|
+
# type, and the schema admits exactly these four. An entry declaring none of
|
|
2556
|
+
# them, or more than one, is reported rather than guessed at: the schema makes
|
|
2557
|
+
# them a `oneOf`, so more than one is malformed, and picking one of two
|
|
2558
|
+
# conflicting values would publish a number rainbow invented a preference for.
|
|
2559
|
+
_CUSTOM_DATUM_KEYS = (
|
|
2560
|
+
"scalar string datum",
|
|
2561
|
+
"scalar double datum",
|
|
2562
|
+
"scalar boolean datum",
|
|
2563
|
+
"scalar timestamp datum",
|
|
2564
|
+
)
|
|
2565
|
+
|
|
2566
|
+
# Returned instead of a value when a datum was recognised but unreadable, so a
|
|
2567
|
+
# field dropped after its own warning is not warned about a second time.
|
|
2568
|
+
_UNREADABLE = object()
|
|
2569
|
+
|
|
2570
|
+
|
|
2571
|
+
def _custom_datum_value(key, entry, label, warn):
|
|
2572
|
+
"""One custom entry's value, in the Python type its datum type calls for.
|
|
2573
|
+
|
|
2574
|
+
The datum type is what distinguishes a timestamp from the string it is
|
|
2575
|
+
spelled as, so it has to survive into the mapping or a round trip cannot put
|
|
2576
|
+
it back. A double carrying a ``unit`` sibling becomes ``{"value", "unit"}``,
|
|
2577
|
+
the shape rainbow already uses for ``injection_volume``.
|
|
2578
|
+
"""
|
|
2579
|
+
raw = entry[key]
|
|
2580
|
+
unit = _text(entry.get("unit"))
|
|
2581
|
+
if key == "scalar string datum":
|
|
2582
|
+
value = _text(raw)
|
|
2583
|
+
elif key == "scalar boolean datum":
|
|
2584
|
+
value = raw.get("value") if isinstance(raw, dict) else raw
|
|
2585
|
+
# Not bool(raw): "false" and 0 are both truthy-or-falsy in ways that
|
|
2586
|
+
# have nothing to do with what the document recorded.
|
|
2587
|
+
value = value if isinstance(value, bool) else None
|
|
2588
|
+
elif key == "scalar timestamp datum":
|
|
2589
|
+
# Read as a datetime, which is what tells it apart from a string datum
|
|
2590
|
+
# on the way back out. A date with no time of day is refused rather
|
|
2591
|
+
# than parsed: _parse_iso would return midnight, a value the document
|
|
2592
|
+
# never carried, and re-exporting it would publish that invention under
|
|
2593
|
+
# a datum type the schema requires to be a full date-time. The write
|
|
2594
|
+
# side refuses a Python `date` for the same reason.
|
|
2595
|
+
text = _text(raw)
|
|
2596
|
+
value = _parse_iso(text) if text and _HAS_TIME_OF_DAY.search(text) \
|
|
2597
|
+
else None
|
|
2598
|
+
else:
|
|
2599
|
+
# _number unwraps the object form, rejects a bool spelled as a number,
|
|
2600
|
+
# and drops an integer too large for a float rather than handing back a
|
|
2601
|
+
# value that reads but raises OverflowError on re-export.
|
|
2602
|
+
value = _number(raw)
|
|
2603
|
+
if value is not None and unit:
|
|
2604
|
+
value = {"value": value, "unit": unit}
|
|
2605
|
+
unit = None # consumed
|
|
2606
|
+
if unit and value is not None:
|
|
2607
|
+
# No subschema forbids it, so a conforming document may put a unit beside
|
|
2608
|
+
# a string, a bool or a timestamp. Only a double has anywhere to keep it.
|
|
2609
|
+
warn(
|
|
2610
|
+
"custom field {!r} carries the unit {!r} beside a {}, which only a "
|
|
2611
|
+
"double may do; the unit is dropped.".format(label, unit, key))
|
|
2612
|
+
if value is None:
|
|
2613
|
+
warn(
|
|
2614
|
+
"custom field {!r} holds a {} rainbow cannot read ({!r}); the "
|
|
2615
|
+
"field is dropped.".format(label, key, raw))
|
|
2616
|
+
return _UNREADABLE
|
|
2617
|
+
return value
|
|
2618
|
+
|
|
2619
|
+
|
|
2620
|
+
def _custom_fields(document, seen):
|
|
2621
|
+
"""A document's custom information fields as a ``{label: value}`` dict.
|
|
2622
|
+
|
|
2623
|
+
ASM lets a writer hang arbitrary named values off a sample or measurement in
|
|
2624
|
+
a *custom information aggregate document*. Empower uses it for the fields
|
|
2625
|
+
that have no home in the schema proper -- `Notebook`, `MaterialIdentifier`,
|
|
2626
|
+
`SampleWeight` -- and `MaterialIdentifier` is how an Empower injection is
|
|
2627
|
+
matched to a registered sample downstream, so these are not decoration.
|
|
2628
|
+
"""
|
|
2629
|
+
aggregate = _first(document.get("custom information aggregate document"))
|
|
2630
|
+
entries = _as_documents(aggregate.get("custom information document"))
|
|
2631
|
+
# One sample document is repeated in every measurement of an injection, so a
|
|
2632
|
+
# complaint about it would otherwise be made once per candidate channel. The
|
|
2633
|
+
# caller passes a set to say the complaints share one scope, as the write
|
|
2634
|
+
# side does through _warn_once_per_run.
|
|
2635
|
+
warn = _warn_once(seen)
|
|
2636
|
+
read = []
|
|
2637
|
+
for position, entry in enumerate(entries):
|
|
2638
|
+
if not isinstance(entry, dict):
|
|
2639
|
+
continue
|
|
2640
|
+
label = _text(entry.get("datum label"))
|
|
2641
|
+
if not label:
|
|
2642
|
+
# `datum label` is required, so an entry without a usable one is
|
|
2643
|
+
# malformed. The write side says so for the mirror case, and a
|
|
2644
|
+
# mapping has nowhere to put an unlabelled value, so it is dropped
|
|
2645
|
+
# out loud rather than quietly.
|
|
2646
|
+
# Positioned, because the message is otherwise identical for every
|
|
2647
|
+
# unlabelled entry and the dedupe would report only the first.
|
|
2648
|
+
warn("the custom field at position {} carries {!r} as its datum "
|
|
2649
|
+
"label, which cannot key a value; the field is dropped."
|
|
2650
|
+
.format(position + 1, entry.get("datum label")))
|
|
2651
|
+
continue
|
|
2652
|
+
spellings = [key for key in _CUSTOM_DATUM_KEYS if key in entry]
|
|
2653
|
+
if len(spellings) != 1:
|
|
2654
|
+
_warn_custom_datum_type(label, entry, spellings, warn)
|
|
2655
|
+
continue
|
|
2656
|
+
value = _custom_datum_value(spellings[0], entry, label, warn)
|
|
2657
|
+
if value is _UNREADABLE:
|
|
2658
|
+
continue
|
|
2659
|
+
read.append((_number(entry.get("@index")), label, value))
|
|
2660
|
+
fields = {}
|
|
2661
|
+
for _, label, value in _in_declared_order(read):
|
|
2662
|
+
if label in fields:
|
|
2663
|
+
# Duplicate labels are legal and a mapping cannot hold both. Keeping
|
|
2664
|
+
# the first matches how the rest of a read merges, but silently
|
|
2665
|
+
# discarding a value is the thing this reader otherwise refuses to
|
|
2666
|
+
# do, so it is said out loud.
|
|
2667
|
+
warn("custom field {!r} appears more than once; the first "
|
|
2668
|
+
"value is kept and the rest dropped.".format(label))
|
|
2669
|
+
continue
|
|
2670
|
+
fields[label] = value
|
|
2671
|
+
return fields
|
|
2672
|
+
|
|
2673
|
+
|
|
2674
|
+
def _warn_custom_datum_type(label, entry, spellings, warn):
|
|
2675
|
+
"""Reports an entry declaring no datum type rainbow reads, or several."""
|
|
2676
|
+
if spellings:
|
|
2677
|
+
warn(
|
|
2678
|
+
"custom field {!r} declares {} datum types ({}), which the schema "
|
|
2679
|
+
"admits only one of; the field is dropped rather than picking one "
|
|
2680
|
+
"of its values.".format(
|
|
2681
|
+
label, len(spellings),
|
|
2682
|
+
", ".join(repr(key) for key in spellings)))
|
|
2683
|
+
return
|
|
2684
|
+
unknown = sorted(key for key in entry
|
|
2685
|
+
if key not in ("@index", "datum label", "unit"))
|
|
2686
|
+
warn(
|
|
2687
|
+
"custom field {!r} records its value under {}, which rainbow does not "
|
|
2688
|
+
"read; the field is dropped rather than reported under a datum type "
|
|
2689
|
+
"that would change its meaning.".format(
|
|
2690
|
+
label, ", ".join(repr(key) for key in unknown) or "nothing"))
|
|
2691
|
+
|
|
2692
|
+
|
|
2693
|
+
def _in_declared_order(read):
|
|
2694
|
+
"""``read`` ordered by ``@index`` when every entry declares one.
|
|
2695
|
+
|
|
2696
|
+
``@index`` is 1-based and need not be dense; document position is 0-based
|
|
2697
|
+
and always is. Sorting a mix of the two puts an unindexed entry wherever its
|
|
2698
|
+
position happens to fall among someone else's indices -- three fields
|
|
2699
|
+
indexed 5, 6 and (none) came back as (none), 5, 6. Since the schema does not
|
|
2700
|
+
require ``@index``, a partially indexed document is reachable, so the two
|
|
2701
|
+
are never mixed: either the document orders every entry or its own order
|
|
2702
|
+
stands.
|
|
2703
|
+
"""
|
|
2704
|
+
if all(index is not None for index, _, _ in read):
|
|
2705
|
+
return sorted(read, key=lambda item: item[0])
|
|
2706
|
+
return read
|
|
2707
|
+
|
|
2708
|
+
|
|
2303
2709
|
# The two ADMs neither name nor measure the injection volume the same way.
|
|
2304
2710
|
_INJECTION_VOLUME_KEYS = (
|
|
2305
2711
|
# Liquid chromatography, in mm^3.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|