FCSBridge 0.1.0__tar.gz → 0.2.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.
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/.zenodo.json +1 -1
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/CITATION.cff +1 -1
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/FCSBridge/__init__.py +11 -1
- fcsbridge-0.2.0/FCSBridge/_records.py +169 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/FCSBridge/_version.py +1 -1
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/FCSBridge/fcs.py +4 -6
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/FCSBridge.egg-info/PKG-INFO +1 -1
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/PKG-INFO +1 -1
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/conda.recipe/meta.yaml +1 -1
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/pyproject.toml +1 -1
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/tests/test_fcs_versions.py +16 -1
- fcsbridge-0.1.0/FCSBridge/_records.py +0 -118
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/.coveragerc +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/AGENTS.md +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/CONTRIBUTING.md +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/FCSBridge/csv.py +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/FCSBridge/dataset.py +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/FCSBridge.egg-info/SOURCES.txt +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/FCSBridge.egg-info/dependency_links.txt +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/FCSBridge.egg-info/requires.txt +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/FCSBridge.egg-info/top_level.txt +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/LICENSE +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/MANIFEST.in +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/Makefile +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/README.rst +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/USAGE.rst +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/docs/examples/dataframe_to_fcs.py +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/docs/examples/fcs_to_csv.py +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/docs/images/favicon.png +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/docs/images/favicon.svg +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/docs/images/icon.png +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/docs/images/icon.svg +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/docs/images/logo.png +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/docs/images/logo.svg +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/docs/source/conf.py +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/docs/source/index.rst +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/pytest.ini +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/readme.yml +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/setup.cfg +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/tests/data/LICENSE-fcsparser +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/tests/data/README.rst +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/tests/data/cyflow_cube_8.fcs +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/tests/test_csv.py +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/tests/test_dataset.py +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/tests/test_fcs.py +0 -0
- {fcsbridge-0.1.0 → fcsbridge-0.2.0}/tests/test_rosettax.py +0 -0
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
"""Exchange flow cytometry event tables between FCS, pandas, NumPy and CSV."""
|
|
2
2
|
|
|
3
|
+
from ._records import FCSReader
|
|
3
4
|
from ._version import __version__
|
|
4
5
|
from .csv import read_csv, write_csv
|
|
5
6
|
from .dataset import Channel, FCSDataset
|
|
6
7
|
from .fcs import read_fcs, write_fcs
|
|
7
8
|
|
|
8
|
-
__all__ = [
|
|
9
|
+
__all__ = [
|
|
10
|
+
"Channel",
|
|
11
|
+
"FCSDataset",
|
|
12
|
+
"FCSReader",
|
|
13
|
+
"__version__",
|
|
14
|
+
"read_csv",
|
|
15
|
+
"read_fcs",
|
|
16
|
+
"write_csv",
|
|
17
|
+
"write_fcs",
|
|
18
|
+
]
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"""Read byte-aligned FCS records with channel-specific numerical types."""
|
|
2
|
+
|
|
3
|
+
import mmap
|
|
4
|
+
import os
|
|
5
|
+
import re
|
|
6
|
+
from decimal import Decimal
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _decode_keywords(*, raw):
|
|
12
|
+
"""Decode the delimited primary TEXT keywords, including vendor empty fields."""
|
|
13
|
+
try:
|
|
14
|
+
text = raw.decode("utf-8")
|
|
15
|
+
except UnicodeDecodeError:
|
|
16
|
+
text = raw.decode("latin-1")
|
|
17
|
+
delimiter = text[0]
|
|
18
|
+
end = text.rfind(delimiter)
|
|
19
|
+
if not end or text[end + 1 :].strip(" \r\n\t\0"):
|
|
20
|
+
raise ValueError("Unterminated FCS TEXT")
|
|
21
|
+
body = text[1:end]
|
|
22
|
+
# A doubled delimiter inside a keyword value represents one literal delimiter.
|
|
23
|
+
tokens = re.findall(
|
|
24
|
+
f"(?:{re.escape(delimiter * 2)}|[^{re.escape(delimiter)}])*{re.escape(delimiter)}", body + delimiter
|
|
25
|
+
)
|
|
26
|
+
fields = [token[:-1].replace(delimiter * 2, delimiter) for token in tokens]
|
|
27
|
+
keys = fields[::2]
|
|
28
|
+
if len(fields) % 2 or any(delimiter in key for key in keys):
|
|
29
|
+
# Some Apogee files write empty optional values between standard keywords.
|
|
30
|
+
fields = body.split(delimiter)
|
|
31
|
+
if len(fields) % 2 or not all(key.startswith("$") for key in fields[::2]):
|
|
32
|
+
raise ValueError("Ambiguous FCS TEXT keywords")
|
|
33
|
+
return delimiter, dict(zip(fields[::2], fields[1::2], strict=True))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class FCSReader:
|
|
37
|
+
"""Access raw metadata and bounded event columns from one list-mode file.
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
path : path-like
|
|
42
|
+
FCS 2.0, 3.0, 3.1 or 3.2 file. Use a context manager to release
|
|
43
|
+
the read-only mapping immediately after the last column read.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def __init__(self, *, path):
|
|
47
|
+
"""Read the primary keywords and validate the binary record layout."""
|
|
48
|
+
with Path(path).open("rb") as stream:
|
|
49
|
+
header = stream.read(58)
|
|
50
|
+
version = header[:6]
|
|
51
|
+
if version not in {b"FCS2.0", b"FCS3.0", b"FCS3.1", b"FCS3.2"}:
|
|
52
|
+
raise ValueError(f"Unsupported FCS signature {version!r}")
|
|
53
|
+
text_start, text_end, header_start, header_end = (
|
|
54
|
+
int(header[index : index + 8].strip() or b"0") for index in range(10, 42, 8)
|
|
55
|
+
)
|
|
56
|
+
size = os.fstat(stream.fileno()).st_size
|
|
57
|
+
if not 58 <= text_start <= text_end < size:
|
|
58
|
+
raise ValueError("Invalid FCS TEXT offsets")
|
|
59
|
+
stream.seek(text_start)
|
|
60
|
+
raw = stream.read(text_end - text_start + 1)
|
|
61
|
+
self.delimiter, self.keywords = _decode_keywords(raw=raw)
|
|
62
|
+
self.version = version.decode("ascii")
|
|
63
|
+
self.text_start, self.text_end = text_start, text_end
|
|
64
|
+
self.data_start, self.data_end = header_start, header_end
|
|
65
|
+
metadata = {key.removeprefix("$").lower(): value for key, value in self.keywords.items()}
|
|
66
|
+
if int(metadata.get("nextdata", "0")) or metadata.get("mode", "L").upper() != "L":
|
|
67
|
+
raise ValueError("Expected one list-mode FCS dataset")
|
|
68
|
+
count, channels = int(metadata["tot"]), int(metadata["par"])
|
|
69
|
+
if count < 0 or channels < 1:
|
|
70
|
+
raise ValueError("Invalid FCS event or channel count")
|
|
71
|
+
endian = {"1,2": "<", "1,2,3,4": "<", "2,1": ">", "4,3,2,1": ">"}.get(metadata["byteord"])
|
|
72
|
+
if not endian:
|
|
73
|
+
raise ValueError("Unsupported FCS byte order")
|
|
74
|
+
formats = []
|
|
75
|
+
masks = []
|
|
76
|
+
for index in range(1, channels + 1):
|
|
77
|
+
prefix = f"p{index}"
|
|
78
|
+
kind = metadata["datatype"]
|
|
79
|
+
if version == b"FCS3.2":
|
|
80
|
+
kind = metadata.get(prefix + "datatype", kind)
|
|
81
|
+
kind = kind.upper()
|
|
82
|
+
width = int(metadata[prefix + "b"])
|
|
83
|
+
if kind == "I" and width in {8, 16, 32, 64}:
|
|
84
|
+
limit = Decimal(metadata[prefix + "r"])
|
|
85
|
+
if (
|
|
86
|
+
not limit.is_finite()
|
|
87
|
+
or not 0 < limit <= 2**width
|
|
88
|
+
or limit != limit.to_integral_value()
|
|
89
|
+
):
|
|
90
|
+
raise ValueError("Invalid FCS integer channel range")
|
|
91
|
+
masks.append((1 << (int(limit) - 1).bit_length()) - 1)
|
|
92
|
+
code = "u"
|
|
93
|
+
elif (kind, width) in {("F", 32), ("D", 64)}:
|
|
94
|
+
masks.append(None)
|
|
95
|
+
code = "f"
|
|
96
|
+
else:
|
|
97
|
+
raise ValueError(f"Unsupported FCS channel encoding {kind}/{width}")
|
|
98
|
+
formats.append(np.dtype(f"{endian}{code}{width // 8}"))
|
|
99
|
+
record_type = np.dtype([(str(i), fmt) for i, fmt in enumerate(formats)])
|
|
100
|
+
start, stop = (
|
|
101
|
+
(header_start, header_end)
|
|
102
|
+
if version == b"FCS2.0"
|
|
103
|
+
else (int(metadata["begindata"]), int(metadata["enddata"]))
|
|
104
|
+
)
|
|
105
|
+
if version != b"FCS2.0" and (header_start, header_end) not in {(0, 0), (start, stop)}:
|
|
106
|
+
raise ValueError("Conflicting FCS HEADER and TEXT DATA offsets")
|
|
107
|
+
expected_end = start + count * record_type.itemsize
|
|
108
|
+
if start <= text_end or expected_end > size or stop not in (expected_end - 1, expected_end):
|
|
109
|
+
raise ValueError("DATA offsets disagree with the FCS record size and event count")
|
|
110
|
+
self._mapping = mmap.mmap(stream.fileno(), 0, access=mmap.ACCESS_READ)
|
|
111
|
+
self._records = np.ndarray(count, dtype=record_type, buffer=self._mapping, offset=start)
|
|
112
|
+
self._masks = masks
|
|
113
|
+
self._matrix_dtype = np.result_type(*formats) if version == b"FCS3.2" else np.dtype("float64")
|
|
114
|
+
|
|
115
|
+
def __enter__(self):
|
|
116
|
+
"""Return the open reader."""
|
|
117
|
+
return self
|
|
118
|
+
|
|
119
|
+
def __exit__(self, exc_type, exc, traceback):
|
|
120
|
+
"""Release the mapping on context exit."""
|
|
121
|
+
self.close()
|
|
122
|
+
|
|
123
|
+
def close(self):
|
|
124
|
+
"""Release mapped records. Previously returned arrays remain usable."""
|
|
125
|
+
self._records = None
|
|
126
|
+
self._mapping.close()
|
|
127
|
+
|
|
128
|
+
def column_copy(self, *, index, start=0, count=None, dtype=None):
|
|
129
|
+
"""Copy stored values from a zero-based channel, applying its range mask.
|
|
130
|
+
|
|
131
|
+
Parameters
|
|
132
|
+
----------
|
|
133
|
+
index : int
|
|
134
|
+
Zero-based channel position.
|
|
135
|
+
start : int
|
|
136
|
+
First event, using Python slice indexing.
|
|
137
|
+
count : int, optional
|
|
138
|
+
Slice stop relative to the remaining events.
|
|
139
|
+
dtype : numpy dtype, optional
|
|
140
|
+
Output type. Defaults to the channel's stored type.
|
|
141
|
+
|
|
142
|
+
Returns
|
|
143
|
+
-------
|
|
144
|
+
numpy.ndarray
|
|
145
|
+
Owned one-dimensional array in stored channel units.
|
|
146
|
+
"""
|
|
147
|
+
if self._records is None:
|
|
148
|
+
raise RuntimeError("File is closed.")
|
|
149
|
+
values = self._records[str(index)][start:][:count]
|
|
150
|
+
mask = self._masks[index]
|
|
151
|
+
if mask is not None:
|
|
152
|
+
values = values & mask
|
|
153
|
+
return np.array(values, dtype=dtype, copy=True)
|
|
154
|
+
|
|
155
|
+
def read_events(self):
|
|
156
|
+
"""Copy all events into a numerical matrix shaped (events, channels)."""
|
|
157
|
+
if self._records is None:
|
|
158
|
+
raise RuntimeError("File is closed.")
|
|
159
|
+
events = np.empty((len(self._records), len(self._masks)), dtype=self._matrix_dtype)
|
|
160
|
+
for index, mask in enumerate(self._masks):
|
|
161
|
+
values = self.column_copy(index=index)
|
|
162
|
+
events[:, index] = values
|
|
163
|
+
if mask is not None and values.dtype.itemsize == 8 and events.dtype.kind == "f":
|
|
164
|
+
with np.errstate(invalid="ignore"):
|
|
165
|
+
if not np.array_equal(events[:, index].astype(values.dtype), values):
|
|
166
|
+
raise ValueError(
|
|
167
|
+
"Mixed FCS record cannot be represented exactly in one numerical matrix"
|
|
168
|
+
)
|
|
169
|
+
return events
|
|
@@ -7,7 +7,7 @@ from tempfile import TemporaryDirectory
|
|
|
7
7
|
import flowio
|
|
8
8
|
import numpy as np
|
|
9
9
|
from flowio.fcs_keywords import FCS_STANDARD_REQUIRED_KEYWORDS
|
|
10
|
-
from ._records import
|
|
10
|
+
from ._records import FCSReader
|
|
11
11
|
from .dataset import Channel, FCSDataset, as_dataset
|
|
12
12
|
|
|
13
13
|
|
|
@@ -24,11 +24,9 @@ def read_fcs(*, path):
|
|
|
24
24
|
FCSDataset
|
|
25
25
|
Stored event values without gain, time, log or compensation transforms.
|
|
26
26
|
"""
|
|
27
|
-
with
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
raise ValueError(f"Unsupported FCS signature {version!r}")
|
|
31
|
-
text, events = read_records(stream=stream, version=version)
|
|
27
|
+
with FCSReader(path=path) as reader:
|
|
28
|
+
text = {key.removeprefix("$").lower(): value for key, value in reader.keywords.items()}
|
|
29
|
+
events = reader.read_events()
|
|
32
30
|
parameters = [{} for _ in range(int(text["par"]))]
|
|
33
31
|
metadata = {}
|
|
34
32
|
for key, value in text.items():
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import struct
|
|
4
4
|
import numpy as np
|
|
5
5
|
import pytest
|
|
6
|
-
from FCSBridge import read_fcs
|
|
6
|
+
from FCSBridge import FCSReader, read_fcs
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
def instrument_bytes(*, signature, order, mixed=False):
|
|
@@ -88,3 +88,18 @@ def test_double_and_uint64_mixed_precision(tmp_path):
|
|
|
88
88
|
path.write_bytes(prefix + struct.pack("<dQdQ", exact, 2**53 + 1, -exact, 17))
|
|
89
89
|
with pytest.raises(ValueError, match="represented exactly"):
|
|
90
90
|
read_fcs(path=path)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def test_bounded_reader_exposes_raw_metadata_and_owned_columns(tmp_path):
|
|
94
|
+
"""Preserve keyword spelling, escaped delimiters and masked integer values after close."""
|
|
95
|
+
path = tmp_path / "bounded.fcs"
|
|
96
|
+
path.write_bytes(instrument_bytes(signature="FCS3.2", order=">", mixed=True))
|
|
97
|
+
with FCSReader(path=path) as reader:
|
|
98
|
+
assert reader.delimiter == "|"
|
|
99
|
+
assert reader.keywords["$COM"] == "A|B"
|
|
100
|
+
assert reader.data_start == 512
|
|
101
|
+
values = reader.column_copy(index=1, count=1)
|
|
102
|
+
assert values.flags.owndata
|
|
103
|
+
np.testing.assert_array_equal(values, [517])
|
|
104
|
+
with pytest.raises(RuntimeError, match="closed"):
|
|
105
|
+
reader.column_copy(index=0)
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
"""Read byte-aligned FCS records with channel-specific numerical types."""
|
|
2
|
-
|
|
3
|
-
import os
|
|
4
|
-
import re
|
|
5
|
-
from decimal import Decimal
|
|
6
|
-
import numpy as np
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def _decode_keywords(*, raw):
|
|
10
|
-
"""Decode the delimited primary TEXT keywords, including vendor empty fields."""
|
|
11
|
-
try:
|
|
12
|
-
text = raw.decode("utf-8")
|
|
13
|
-
except UnicodeDecodeError:
|
|
14
|
-
text = raw.decode("latin-1")
|
|
15
|
-
delimiter = text[0]
|
|
16
|
-
end = text.rfind(delimiter)
|
|
17
|
-
if not end or text[end + 1 :].strip(" \r\n\t\0"):
|
|
18
|
-
raise ValueError("Unterminated FCS TEXT")
|
|
19
|
-
body = text[1:end]
|
|
20
|
-
# A doubled delimiter inside a keyword value represents one literal delimiter.
|
|
21
|
-
tokens = re.findall(
|
|
22
|
-
f"(?:{re.escape(delimiter * 2)}|[^{re.escape(delimiter)}])*{re.escape(delimiter)}", body + delimiter
|
|
23
|
-
)
|
|
24
|
-
fields = [token[:-1].replace(delimiter * 2, delimiter) for token in tokens]
|
|
25
|
-
keys = fields[::2]
|
|
26
|
-
if len(fields) % 2 or any(delimiter in key for key in keys):
|
|
27
|
-
# Some Apogee files write empty optional values between standard keywords.
|
|
28
|
-
fields = body.split(delimiter)
|
|
29
|
-
if len(fields) % 2 or not all(key.startswith("$") for key in fields[::2]):
|
|
30
|
-
raise ValueError("Ambiguous FCS TEXT keywords")
|
|
31
|
-
return {
|
|
32
|
-
key.removeprefix("$").lower(): value for key, value in zip(fields[::2], fields[1::2], strict=True)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
def read_records(*, stream, version):
|
|
37
|
-
"""Decode one binary list-mode DATA segment using the declared record layout.
|
|
38
|
-
|
|
39
|
-
Parameters
|
|
40
|
-
----------
|
|
41
|
-
stream : binary file
|
|
42
|
-
Seekable instrument file positioned anywhere.
|
|
43
|
-
version : bytes
|
|
44
|
-
Validated six-byte FCS signature.
|
|
45
|
-
|
|
46
|
-
Returns
|
|
47
|
-
-------
|
|
48
|
-
tuple
|
|
49
|
-
Normalized TEXT keywords and numerical matrix of shape (events, channels).
|
|
50
|
-
"""
|
|
51
|
-
stream.seek(0)
|
|
52
|
-
header = stream.read(58)
|
|
53
|
-
text_start, text_end, header_start, header_end = (
|
|
54
|
-
int(header[index : index + 8].strip() or b"0") for index in range(10, 42, 8)
|
|
55
|
-
)
|
|
56
|
-
size = os.fstat(stream.fileno()).st_size
|
|
57
|
-
if not 58 <= text_start <= text_end < size:
|
|
58
|
-
raise ValueError("Invalid FCS TEXT offsets")
|
|
59
|
-
stream.seek(text_start)
|
|
60
|
-
raw = stream.read(text_end - text_start + 1)
|
|
61
|
-
metadata = _decode_keywords(raw=raw)
|
|
62
|
-
if int(metadata.get("nextdata", "0")) or metadata.get("mode", "L").upper() != "L":
|
|
63
|
-
raise ValueError("Expected one list-mode FCS dataset")
|
|
64
|
-
count, channels = int(metadata["tot"]), int(metadata["par"])
|
|
65
|
-
if count < 0 or channels < 1:
|
|
66
|
-
raise ValueError("Invalid FCS event or channel count")
|
|
67
|
-
endian = {"1,2": "<", "1,2,3,4": "<", "2,1": ">", "4,3,2,1": ">"}.get(metadata["byteord"])
|
|
68
|
-
if not endian:
|
|
69
|
-
raise ValueError("Unsupported FCS byte order")
|
|
70
|
-
formats = []
|
|
71
|
-
masks = []
|
|
72
|
-
for index in range(1, channels + 1):
|
|
73
|
-
prefix = f"p{index}"
|
|
74
|
-
kind = metadata["datatype"]
|
|
75
|
-
if version == b"FCS3.2":
|
|
76
|
-
kind = metadata.get(prefix + "datatype", kind)
|
|
77
|
-
kind = kind.upper()
|
|
78
|
-
width = int(metadata[prefix + "b"])
|
|
79
|
-
if kind == "I" and width in {8, 16, 32, 64}:
|
|
80
|
-
limit = Decimal(metadata[prefix + "r"])
|
|
81
|
-
if not limit.is_finite() or not 0 < limit <= 2**width or limit != limit.to_integral_value():
|
|
82
|
-
raise ValueError("Invalid FCS integer channel range")
|
|
83
|
-
masks.append((1 << (int(limit) - 1).bit_length()) - 1)
|
|
84
|
-
code = "u"
|
|
85
|
-
elif (kind, width) in {("F", 32), ("D", 64)}:
|
|
86
|
-
masks.append(None)
|
|
87
|
-
code = "f"
|
|
88
|
-
else:
|
|
89
|
-
raise ValueError(f"Unsupported FCS channel encoding {kind}/{width}")
|
|
90
|
-
formats.append(np.dtype(f"{endian}{code}{width // 8}"))
|
|
91
|
-
record_type = np.dtype([(str(i), fmt) for i, fmt in enumerate(formats)])
|
|
92
|
-
start, stop = (
|
|
93
|
-
(header_start, header_end)
|
|
94
|
-
if version == b"FCS2.0"
|
|
95
|
-
else (int(metadata["begindata"]), int(metadata["enddata"]))
|
|
96
|
-
)
|
|
97
|
-
if version != b"FCS2.0" and (header_start, header_end) not in {(0, 0), (start, stop)}:
|
|
98
|
-
raise ValueError("Conflicting FCS HEADER and TEXT DATA offsets")
|
|
99
|
-
expected_end = start + count * record_type.itemsize
|
|
100
|
-
if start <= text_end or expected_end > size or stop not in (expected_end - 1, expected_end):
|
|
101
|
-
raise ValueError("DATA offsets disagree with the FCS record size and event count")
|
|
102
|
-
stream.seek(start)
|
|
103
|
-
records = np.fromfile(stream, dtype=record_type, count=count)
|
|
104
|
-
if len(records) != count:
|
|
105
|
-
raise ValueError("Truncated FCS DATA")
|
|
106
|
-
# Keep the public float64 matrix convention for earlier revisions.
|
|
107
|
-
matrix_type = np.result_type(*formats) if version == b"FCS3.2" else np.float64
|
|
108
|
-
events = np.empty((count, channels), dtype=matrix_type)
|
|
109
|
-
for index, mask in enumerate(masks):
|
|
110
|
-
values = records[str(index)]
|
|
111
|
-
if mask is not None:
|
|
112
|
-
values = values & mask
|
|
113
|
-
events[:, index] = values
|
|
114
|
-
if mask is not None and values.dtype.itemsize == 8 and events.dtype.kind == "f":
|
|
115
|
-
with np.errstate(invalid="ignore"):
|
|
116
|
-
if not np.array_equal(events[:, index].astype(values.dtype), values):
|
|
117
|
-
raise ValueError("Mixed FCS record cannot be represented exactly in one numerical matrix")
|
|
118
|
-
return metadata, events
|
|
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
|