bioio-openreadout 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.
- bioio_openreadout-0.1.0/.gitignore +40 -0
- bioio_openreadout-0.1.0/LICENSE-APACHE +202 -0
- bioio_openreadout-0.1.0/LICENSE-MIT +21 -0
- bioio_openreadout-0.1.0/PKG-INFO +83 -0
- bioio_openreadout-0.1.0/README.md +48 -0
- bioio_openreadout-0.1.0/bioio_openreadout/__init__.py +23 -0
- bioio_openreadout-0.1.0/bioio_openreadout/py.typed +0 -0
- bioio_openreadout-0.1.0/bioio_openreadout/reader.py +201 -0
- bioio_openreadout-0.1.0/bioio_openreadout/reader_metadata.py +41 -0
- bioio_openreadout-0.1.0/pyproject.toml +56 -0
- bioio_openreadout-0.1.0/tests/conftest.py +25 -0
- bioio_openreadout-0.1.0/tests/test_bioio_openreadout.py +312 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/target
|
|
2
|
+
/corpus/files/
|
|
3
|
+
/oracle/bftools/
|
|
4
|
+
/oracle/.venv/
|
|
5
|
+
/oracle/.cache/
|
|
6
|
+
/oracle/__pycache__/
|
|
7
|
+
**/__pycache__/
|
|
8
|
+
*.pyc
|
|
9
|
+
/dist/
|
|
10
|
+
/wheels/
|
|
11
|
+
*.ome.tiff
|
|
12
|
+
*.ome.tif
|
|
13
|
+
!/docs/**/*.ome.tiff
|
|
14
|
+
.DS_Store
|
|
15
|
+
/.env
|
|
16
|
+
# maturin develop drops the compiled extension next to the Python sources
|
|
17
|
+
/crates/openreadout-py/python/openreadout/_native*.so
|
|
18
|
+
/crates/openreadout-py/python/openreadout/_native*.pyd
|
|
19
|
+
*.egg-info/
|
|
20
|
+
.pytest_cache/
|
|
21
|
+
# cargo-fuzz build output, crash artifacts and the growing working corpus (seeds: fuzz/corpus)
|
|
22
|
+
/fuzz/target/
|
|
23
|
+
/fuzz/artifacts*/
|
|
24
|
+
/fuzz/work/
|
|
25
|
+
/fuzz/logs/
|
|
26
|
+
/fuzz/coverage/
|
|
27
|
+
/book/book/
|
|
28
|
+
/book/node_modules/
|
|
29
|
+
/book/.astro/
|
|
30
|
+
/.venv/
|
|
31
|
+
/fuzz/local-seeds/
|
|
32
|
+
/fuzz/found/
|
|
33
|
+
# WebAssembly package build output (scripts/wasm.sh) and npm installs
|
|
34
|
+
/packaging/wasm/pkg/
|
|
35
|
+
/packaging/wasm/node_modules/
|
|
36
|
+
/web/node_modules/
|
|
37
|
+
# Claude Code worktrees and local state
|
|
38
|
+
/.claude/
|
|
39
|
+
# eval run output (reports, per-run records, transcripts, logs)
|
|
40
|
+
/evals/results/
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The OpenReadout Authors
|
|
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,83 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: bioio-openreadout
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: bioio reader plugin that reads microscopy files with OpenReadout
|
|
5
|
+
Project-URL: Homepage, https://github.com/openreadout/openreadout
|
|
6
|
+
Project-URL: Repository, https://github.com/openreadout/openreadout
|
|
7
|
+
Project-URL: Documentation, https://openreadout.github.io/openreadout/guides/python.html
|
|
8
|
+
Author-email: The OpenReadout Authors <openreadout@gmail.com>
|
|
9
|
+
License-Expression: MIT OR Apache-2.0
|
|
10
|
+
License-File: LICENSE-APACHE
|
|
11
|
+
License-File: LICENSE-MIT
|
|
12
|
+
Keywords: bioio,czi,imaging,lif,microscopy,nd2
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Image Processing
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Requires-Dist: bioio-base<4,>=3.4
|
|
22
|
+
Requires-Dist: dask[array]>=2022.2
|
|
23
|
+
Requires-Dist: fsspec>=2022.8.0
|
|
24
|
+
Requires-Dist: numpy>=1.22
|
|
25
|
+
Requires-Dist: ome-types>=0.4
|
|
26
|
+
Requires-Dist: openreadout[xarray]<0.2,>=0.1.0
|
|
27
|
+
Requires-Dist: xarray>=2022.6
|
|
28
|
+
Provides-Extra: test
|
|
29
|
+
Requires-Dist: bioio-nd2; extra == 'test'
|
|
30
|
+
Requires-Dist: bioio>=3; extra == 'test'
|
|
31
|
+
Requires-Dist: czifile; extra == 'test'
|
|
32
|
+
Requires-Dist: liffile; extra == 'test'
|
|
33
|
+
Requires-Dist: pytest>=7; extra == 'test'
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
# bioio-openreadout
|
|
37
|
+
|
|
38
|
+
A [bioio](https://github.com/bioio-devs/bioio) reader plugin that reads microscopy files with [OpenReadout](https://github.com/openreadout/openreadout)'s Rust readers. It is licensed MIT OR Apache-2.0 and depends on no GPL plugin, vendor SDK or Java. Wheels need no compiler. Each plane is one dask chunk and is decoded only when a computation needs it.
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install bioio bioio-openreadout
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from bioio import BioImage
|
|
46
|
+
import bioio_openreadout
|
|
47
|
+
|
|
48
|
+
img = BioImage("run42.czi", reader=bioio_openreadout.Reader)
|
|
49
|
+
img.scenes # ('P2', 'P3', 'P1') — one per CZI scene / ND2 position / LIF series
|
|
50
|
+
img.dims # <Dimensions [T: 1, C: 3, Z: 5, Y: 325, X: 475]>
|
|
51
|
+
img.channel_names # ['EGFP', 'TaRFP', 'Bright']
|
|
52
|
+
img.physical_pixel_sizes # PhysicalPixelSizes(Z=1.0, Y=1.083, X=1.083) (µm)
|
|
53
|
+
img.set_scene(1)
|
|
54
|
+
zyx = img.get_image_dask_data("ZYX", T=0, C=1).compute() # decodes 5 planes, not the file
|
|
55
|
+
img.ome_metadata # ome_types.OME built from the normalized metadata
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`BioImage(path)` without `reader=` also works for `.czi`, `.nd2`, `.lif`, `.vsi`, `.svs`, `.ndpi`, `.qptiff`, `.ims`, `.oir`, `.oib`, `.oif` and `.zvi`: the plugin registers these extensions through the `bioio.readers` entry point. Other formats OpenReadout reads, such as OME-TIFF or OME-Zarr, open with `reader=bioio_openreadout.Reader`. When another plugin for the same extension is installed (for example `bioio-nd2`), bioio's own ordering decides which is tried first; pass `reader=` to choose.
|
|
59
|
+
|
|
60
|
+
## What you get
|
|
61
|
+
|
|
62
|
+
| bioio | from OpenReadout |
|
|
63
|
+
|---|---|
|
|
64
|
+
| `scenes` | image names (`Image:<n>` when the file has none; duplicates are suffixed with the index) |
|
|
65
|
+
| `dims` | `TCZYX`, or `TCZYXS` for RGB (interleaved samples) |
|
|
66
|
+
| `channel_names` | channel names (`Channel:<scene>:<c>` when the file has none) |
|
|
67
|
+
| `physical_pixel_sizes` | µm, `None` where the file records no size |
|
|
68
|
+
| `time_interval` | the recorded time increment |
|
|
69
|
+
| `metadata` / `ome_metadata` | `ome_types.OME` (one `Image` per scene, `MetadataOnly` pixels) |
|
|
70
|
+
| `xarray_dask_data.attrs["unprocessed"]` | the vendor's own metadata tree as JSON, names untouched |
|
|
71
|
+
|
|
72
|
+
Mosaics are returned stitched (no `M` dimension); pyramidal files at full resolution only. Remote (fsspec) paths are not supported: the file must be local.
|
|
73
|
+
|
|
74
|
+
## Tests
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pip install -e 'python/bioio-openreadout[test]'
|
|
78
|
+
OPENREADOUT_CORPUS_DIR=corpus/files pytest python/bioio-openreadout/tests
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
The tests compare `img.data` against `bioio-nd2`, `czifile` and `liffile` (all BSD) on files from the public test corpus and skip when those files or readers are absent.
|
|
82
|
+
|
|
83
|
+
Guide: <https://openreadout.github.io/openreadout/guides/python.html>. Part of [OpenReadout](https://github.com/openreadout/openreadout). Licensed MIT OR Apache-2.0. OpenReadout is not affiliated with any instrument vendor.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# bioio-openreadout
|
|
2
|
+
|
|
3
|
+
A [bioio](https://github.com/bioio-devs/bioio) reader plugin that reads microscopy files with [OpenReadout](https://github.com/openreadout/openreadout)'s Rust readers. It is licensed MIT OR Apache-2.0 and depends on no GPL plugin, vendor SDK or Java. Wheels need no compiler. Each plane is one dask chunk and is decoded only when a computation needs it.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install bioio bioio-openreadout
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
from bioio import BioImage
|
|
11
|
+
import bioio_openreadout
|
|
12
|
+
|
|
13
|
+
img = BioImage("run42.czi", reader=bioio_openreadout.Reader)
|
|
14
|
+
img.scenes # ('P2', 'P3', 'P1') — one per CZI scene / ND2 position / LIF series
|
|
15
|
+
img.dims # <Dimensions [T: 1, C: 3, Z: 5, Y: 325, X: 475]>
|
|
16
|
+
img.channel_names # ['EGFP', 'TaRFP', 'Bright']
|
|
17
|
+
img.physical_pixel_sizes # PhysicalPixelSizes(Z=1.0, Y=1.083, X=1.083) (µm)
|
|
18
|
+
img.set_scene(1)
|
|
19
|
+
zyx = img.get_image_dask_data("ZYX", T=0, C=1).compute() # decodes 5 planes, not the file
|
|
20
|
+
img.ome_metadata # ome_types.OME built from the normalized metadata
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`BioImage(path)` without `reader=` also works for `.czi`, `.nd2`, `.lif`, `.vsi`, `.svs`, `.ndpi`, `.qptiff`, `.ims`, `.oir`, `.oib`, `.oif` and `.zvi`: the plugin registers these extensions through the `bioio.readers` entry point. Other formats OpenReadout reads, such as OME-TIFF or OME-Zarr, open with `reader=bioio_openreadout.Reader`. When another plugin for the same extension is installed (for example `bioio-nd2`), bioio's own ordering decides which is tried first; pass `reader=` to choose.
|
|
24
|
+
|
|
25
|
+
## What you get
|
|
26
|
+
|
|
27
|
+
| bioio | from OpenReadout |
|
|
28
|
+
|---|---|
|
|
29
|
+
| `scenes` | image names (`Image:<n>` when the file has none; duplicates are suffixed with the index) |
|
|
30
|
+
| `dims` | `TCZYX`, or `TCZYXS` for RGB (interleaved samples) |
|
|
31
|
+
| `channel_names` | channel names (`Channel:<scene>:<c>` when the file has none) |
|
|
32
|
+
| `physical_pixel_sizes` | µm, `None` where the file records no size |
|
|
33
|
+
| `time_interval` | the recorded time increment |
|
|
34
|
+
| `metadata` / `ome_metadata` | `ome_types.OME` (one `Image` per scene, `MetadataOnly` pixels) |
|
|
35
|
+
| `xarray_dask_data.attrs["unprocessed"]` | the vendor's own metadata tree as JSON, names untouched |
|
|
36
|
+
|
|
37
|
+
Mosaics are returned stitched (no `M` dimension); pyramidal files at full resolution only. Remote (fsspec) paths are not supported: the file must be local.
|
|
38
|
+
|
|
39
|
+
## Tests
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install -e 'python/bioio-openreadout[test]'
|
|
43
|
+
OPENREADOUT_CORPUS_DIR=corpus/files pytest python/bioio-openreadout/tests
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The tests compare `img.data` against `bioio-nd2`, `czifile` and `liffile` (all BSD) on files from the public test corpus and skip when those files or readers are absent.
|
|
47
|
+
|
|
48
|
+
Guide: <https://openreadout.github.io/openreadout/guides/python.html>. Part of [OpenReadout](https://github.com/openreadout/openreadout). Licensed MIT OR Apache-2.0. OpenReadout is not affiliated with any instrument vendor.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""bioio reader plugin for Zeiss CZI, Nikon ND2 and Leica LIF, backed by OpenReadout.
|
|
2
|
+
|
|
3
|
+
Installed alongside ``bioio``, it is discovered through the ``bioio.readers`` entry point::
|
|
4
|
+
|
|
5
|
+
from bioio import BioImage
|
|
6
|
+
import bioio_openreadout
|
|
7
|
+
|
|
8
|
+
img = BioImage("run42.czi", reader=bioio_openreadout.Reader)
|
|
9
|
+
img.dims, img.channel_names, img.physical_pixel_sizes
|
|
10
|
+
stack = img.get_image_dask_data("ZYX", T=0, C=1).compute()
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
14
|
+
|
|
15
|
+
from .reader import Reader
|
|
16
|
+
from .reader_metadata import ReaderMetadata
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
__version__ = version("bioio-openreadout")
|
|
20
|
+
except PackageNotFoundError: # pragma: no cover - running from a source checkout
|
|
21
|
+
__version__ = "uninstalled"
|
|
22
|
+
|
|
23
|
+
__all__ = ["Reader", "ReaderMetadata", "__version__"]
|
|
File without changes
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"""The bioio ``Reader`` implemented on top of :mod:`openreadout`."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from datetime import timedelta
|
|
7
|
+
from numbers import Integral
|
|
8
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
import openreadout
|
|
12
|
+
import xarray as xr
|
|
13
|
+
from bioio_base import constants, exceptions, io, reader, types
|
|
14
|
+
from fsspec.implementations.local import LocalFileSystem # type: ignore[import-untyped]
|
|
15
|
+
from fsspec.spec import AbstractFileSystem # type: ignore[import-untyped]
|
|
16
|
+
|
|
17
|
+
__all__ = ["Reader"]
|
|
18
|
+
|
|
19
|
+
log = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
_PLUGIN = "bioio-openreadout"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Reader(reader.Reader):
|
|
25
|
+
"""Read Zeiss CZI, Nikon ND2, Leica LIF and the other microscopy formats of the clean-room
|
|
26
|
+
OpenReadout core (whole-slide SVS/NDPI/QPTIFF/VSI, Imaris, ...).
|
|
27
|
+
|
|
28
|
+
Each image in the file (CZI scene, ND2 position, LIF series) is a bioio scene. Data is
|
|
29
|
+
``TCZYX`` (``TCZYXS`` for RGB), read lazily: one plane per dask chunk, or the file's own
|
|
30
|
+
tiles grouped into chunks of about 16 MiB for large tiled planes, so only what a
|
|
31
|
+
computation touches is decoded. Mosaics are returned stitched. Pyramidal files expose their
|
|
32
|
+
levels as bioio resolution levels (``resolution_levels``, ``set_resolution_level``); physical
|
|
33
|
+
pixel sizes follow the level.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
image : Path or str
|
|
38
|
+
Path to a local file.
|
|
39
|
+
fs_kwargs : Dict[str, Any]
|
|
40
|
+
Passed to the fsspec filesystem. Only local files are supported.
|
|
41
|
+
keep_open : bool
|
|
42
|
+
Keep the file open between reads (faster on files with large indexes, e.g. whole-slide
|
|
43
|
+
CZIs). Default ``False``: as bioio expects of plugins, no file handle stays open; every
|
|
44
|
+
read reopens the file.
|
|
45
|
+
|
|
46
|
+
Raises
|
|
47
|
+
------
|
|
48
|
+
exceptions.UnsupportedFileFormatError
|
|
49
|
+
The path is not a local CZI, ND2 or LIF file that OpenReadout can open.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
NAME = _PLUGIN
|
|
53
|
+
|
|
54
|
+
_file: Optional[openreadout.File] = None
|
|
55
|
+
_scene_ids: Optional[Tuple[str, ...]] = None
|
|
56
|
+
_ome: Any = None
|
|
57
|
+
_vendor: Any = None
|
|
58
|
+
|
|
59
|
+
@staticmethod
|
|
60
|
+
def _is_supported_image(fs: AbstractFileSystem, path: str, **kwargs: Any) -> bool:
|
|
61
|
+
if not isinstance(fs, LocalFileSystem):
|
|
62
|
+
raise exceptions.UnsupportedFileFormatError(
|
|
63
|
+
_PLUGIN, path, "OpenReadout reads local files only."
|
|
64
|
+
)
|
|
65
|
+
try:
|
|
66
|
+
detected = openreadout.info(path, view="format")
|
|
67
|
+
except openreadout.OpenReadoutError as e:
|
|
68
|
+
raise exceptions.UnsupportedFileFormatError(_PLUGIN, path, str(e)) from e
|
|
69
|
+
if detected["confidence"] == "extension-only":
|
|
70
|
+
raise exceptions.UnsupportedFileFormatError(
|
|
71
|
+
_PLUGIN, path, "The extension matches but the file signature does not."
|
|
72
|
+
)
|
|
73
|
+
return True
|
|
74
|
+
|
|
75
|
+
def __init__(
|
|
76
|
+
self,
|
|
77
|
+
image: types.PathLike,
|
|
78
|
+
fs_kwargs: Dict[str, Any] = {},
|
|
79
|
+
keep_open: bool = False,
|
|
80
|
+
**kwargs: Any,
|
|
81
|
+
):
|
|
82
|
+
self._fs, self._path = io.pathlike_to_fs(image, enforce_exists=True, fs_kwargs=fs_kwargs)
|
|
83
|
+
self._is_supported_image(self._fs, self._path)
|
|
84
|
+
try:
|
|
85
|
+
self._file = openreadout.File(self._path, keep_open=keep_open)
|
|
86
|
+
except openreadout.OpenReadoutError as e:
|
|
87
|
+
raise exceptions.UnsupportedFileFormatError(_PLUGIN, self._path, str(e)) from e
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def file(self) -> openreadout.File:
|
|
91
|
+
"""The underlying :class:`openreadout.File`."""
|
|
92
|
+
assert self._file is not None
|
|
93
|
+
return self._file
|
|
94
|
+
|
|
95
|
+
@property
|
|
96
|
+
def scenes(self) -> Tuple[str, ...]:
|
|
97
|
+
"""Image names from the file, made unique; ``Image:<n>`` where the file has none."""
|
|
98
|
+
if self._scene_ids is None:
|
|
99
|
+
ids: List[str] = []
|
|
100
|
+
for i, im in enumerate(self.file.images):
|
|
101
|
+
name = (im.get("name") or "").strip() or f"Image:{i}"
|
|
102
|
+
if name in ids:
|
|
103
|
+
name = f"{name} ({i})"
|
|
104
|
+
ids.append(name)
|
|
105
|
+
self._scene_ids = tuple(ids)
|
|
106
|
+
return self._scene_ids
|
|
107
|
+
|
|
108
|
+
def _image(self) -> openreadout.ImageInfo:
|
|
109
|
+
return self.file.images[self.current_scene_index]
|
|
110
|
+
|
|
111
|
+
def _attach_metadata(self, xarr: xr.DataArray) -> xr.DataArray:
|
|
112
|
+
xarr.attrs[constants.METADATA_UNPROCESSED] = self._vendor_metadata()
|
|
113
|
+
try:
|
|
114
|
+
xarr.attrs[constants.METADATA_PROCESSED] = self.ome_metadata
|
|
115
|
+
except Exception as err: # ome-types missing or a document it rejects
|
|
116
|
+
log.debug("OME metadata unavailable: %s", err)
|
|
117
|
+
return xarr
|
|
118
|
+
|
|
119
|
+
@property
|
|
120
|
+
def resolution_levels(self) -> Tuple[int, ...]:
|
|
121
|
+
"""Pyramid levels of the current scene (0 = full resolution), as stored in the file."""
|
|
122
|
+
return tuple(lv["level"] for lv in self.file.levels(self.current_scene_index))
|
|
123
|
+
|
|
124
|
+
def _level(self) -> int:
|
|
125
|
+
return int(self.current_resolution_level)
|
|
126
|
+
|
|
127
|
+
def _read_delayed(self) -> xr.DataArray:
|
|
128
|
+
return self._attach_metadata(
|
|
129
|
+
self.file.to_xarray(self.current_scene_index, level=self._level(), delayed=True)
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
def _read_immediate(self) -> xr.DataArray:
|
|
133
|
+
return self._attach_metadata(
|
|
134
|
+
self.file.to_xarray(self.current_scene_index, level=self._level(), delayed=False)
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
def _read_indexed(self, given_dims: str, dim_specs: List[Any]) -> np.ndarray:
|
|
138
|
+
# Index the lazy array axis by axis (dask allows one list index at a time), so only
|
|
139
|
+
# the planes (and tiles) the selection touches are decoded.
|
|
140
|
+
arr = self.file.to_dask(self.current_scene_index, level=self._level())
|
|
141
|
+
axis = 0
|
|
142
|
+
for spec in dim_specs:
|
|
143
|
+
if isinstance(spec, Integral):
|
|
144
|
+
arr = arr[(slice(None),) * axis + (int(spec),)]
|
|
145
|
+
else:
|
|
146
|
+
arr = arr[(slice(None),) * axis + (spec,)]
|
|
147
|
+
axis += 1
|
|
148
|
+
return np.asarray(arr.compute()) # type: ignore[no-untyped-call]
|
|
149
|
+
|
|
150
|
+
def _vendor_metadata(self) -> Any:
|
|
151
|
+
if self._vendor is None:
|
|
152
|
+
self._vendor = self.file.vendor
|
|
153
|
+
return self._vendor
|
|
154
|
+
|
|
155
|
+
@property
|
|
156
|
+
def ome_metadata(self) -> Any:
|
|
157
|
+
"""OME model (``ome_types.OME``) built from OpenReadout's normalized metadata.
|
|
158
|
+
|
|
159
|
+
One ``Image`` per scene; ``Pixels`` are ``MetadataOnly``. RGB images report
|
|
160
|
+
``SizeC = 3 × channels`` as OME requires.
|
|
161
|
+
"""
|
|
162
|
+
if self._ome is None:
|
|
163
|
+
from ome_types import from_xml
|
|
164
|
+
|
|
165
|
+
self._ome = from_xml(self.file.ome_xml())
|
|
166
|
+
return self._ome
|
|
167
|
+
|
|
168
|
+
@property
|
|
169
|
+
def physical_pixel_sizes(self) -> types.PhysicalPixelSizes:
|
|
170
|
+
"""Pixel sizes in µm for Z, Y, X at the current resolution level (``None`` where the
|
|
171
|
+
file records none)."""
|
|
172
|
+
im = self._image()
|
|
173
|
+
ps = im["physical_size"]
|
|
174
|
+
lv = self.file.levels(self.current_scene_index)[self._level()]
|
|
175
|
+
fx, fy = float(lv.get("downsample_x") or 1.0), float(lv.get("downsample_y") or 1.0)
|
|
176
|
+
z, y, x = ps.get("z"), ps.get("y"), ps.get("x")
|
|
177
|
+
if z is not None and lv.get("size_z"):
|
|
178
|
+
z = z * im["size_z"] / lv["size_z"]
|
|
179
|
+
return types.PhysicalPixelSizes(
|
|
180
|
+
z,
|
|
181
|
+
y * fy if y is not None else None,
|
|
182
|
+
x * fx if x is not None else None,
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
@property
|
|
186
|
+
def channel_names(self) -> Optional[List[str]]:
|
|
187
|
+
"""Channel names; ``Channel:<scene>:<c>`` where the file records none."""
|
|
188
|
+
im = self._image()
|
|
189
|
+
by_index = {ch["index"]: ch for ch in im["channels"]}
|
|
190
|
+
names = []
|
|
191
|
+
for c in range(im["size_c"]):
|
|
192
|
+
ch = by_index.get(c)
|
|
193
|
+
name = ch.get("name") if ch is not None else None
|
|
194
|
+
names.append(name if name else f"Channel:{self.current_scene_index}:{c}")
|
|
195
|
+
return names
|
|
196
|
+
|
|
197
|
+
@property
|
|
198
|
+
def time_interval(self) -> types.TimeInterval:
|
|
199
|
+
"""Time between T frames, or ``None`` when the file records no increment."""
|
|
200
|
+
dt = self._image().get("time_increment_s")
|
|
201
|
+
return timedelta(seconds=dt) if dt else None
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""Plugin metadata bioio reads through the ``bioio.readers`` entry point."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import List
|
|
6
|
+
|
|
7
|
+
import bioio_base.reader
|
|
8
|
+
import bioio_base.reader_metadata
|
|
9
|
+
|
|
10
|
+
__all__ = ["ReaderMetadata"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ReaderMetadata(bioio_base.reader_metadata.ReaderMetadata):
|
|
14
|
+
"""Extensions this plugin reads and the reader class that reads them."""
|
|
15
|
+
|
|
16
|
+
@staticmethod
|
|
17
|
+
def get_supported_extensions() -> List[str]:
|
|
18
|
+
"""File extensions routed to this plugin: the proprietary microscopy formats (other
|
|
19
|
+
extensions, such as ``.tiff`` or ``.zarr``, are read with ``reader=`` as well, but left
|
|
20
|
+
to their dedicated plugins by default)."""
|
|
21
|
+
return [
|
|
22
|
+
".czi",
|
|
23
|
+
".nd2",
|
|
24
|
+
".lif",
|
|
25
|
+
".vsi",
|
|
26
|
+
".svs",
|
|
27
|
+
".ndpi",
|
|
28
|
+
".qptiff",
|
|
29
|
+
".ims",
|
|
30
|
+
".oir",
|
|
31
|
+
".oib",
|
|
32
|
+
".oif",
|
|
33
|
+
".zvi",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
@staticmethod
|
|
37
|
+
def get_reader() -> bioio_base.reader.Reader:
|
|
38
|
+
"""The :class:`bioio_openreadout.Reader` class."""
|
|
39
|
+
from .reader import Reader
|
|
40
|
+
|
|
41
|
+
return Reader # type: ignore[return-value] # bioio's annotation says instance
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.26"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "bioio-openreadout"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "bioio reader plugin that reads microscopy files with OpenReadout"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT OR Apache-2.0"
|
|
11
|
+
license-files = ["LICENSE-MIT", "LICENSE-APACHE"]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
authors = [{ name = "The OpenReadout Authors", email = "openreadout@gmail.com" }]
|
|
14
|
+
keywords = ["bioio", "microscopy", "czi", "nd2", "lif", "imaging"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Intended Audience :: Science/Research",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
20
|
+
"Topic :: Scientific/Engineering :: Bio-Informatics",
|
|
21
|
+
"Topic :: Scientific/Engineering :: Image Processing",
|
|
22
|
+
"Typing :: Typed",
|
|
23
|
+
]
|
|
24
|
+
# Exactly one `bioio-base` requirement: bioio reads it to check plugin compatibility.
|
|
25
|
+
# Deliberately no dependency on the GPL-licensed bioio-czi / bioio-lif.
|
|
26
|
+
dependencies = [
|
|
27
|
+
"bioio-base>=3.4,<4",
|
|
28
|
+
"openreadout[xarray]>=0.1.0,<0.2",
|
|
29
|
+
"dask[array]>=2022.2",
|
|
30
|
+
"fsspec>=2022.8.0",
|
|
31
|
+
"numpy>=1.22",
|
|
32
|
+
"ome-types>=0.4",
|
|
33
|
+
"xarray>=2022.6",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
[project.optional-dependencies]
|
|
37
|
+
# Oracles for the tests (all permissively licensed).
|
|
38
|
+
test = ["pytest>=7", "bioio>=3", "bioio-nd2", "czifile", "liffile"]
|
|
39
|
+
|
|
40
|
+
[project.entry-points."bioio.readers"]
|
|
41
|
+
# bioio uses the entry point name as the distribution name, so they must match.
|
|
42
|
+
bioio-openreadout = "bioio_openreadout"
|
|
43
|
+
|
|
44
|
+
[project.urls]
|
|
45
|
+
Homepage = "https://github.com/openreadout/openreadout"
|
|
46
|
+
Repository = "https://github.com/openreadout/openreadout"
|
|
47
|
+
Documentation = "https://openreadout.github.io/openreadout/guides/python.html"
|
|
48
|
+
|
|
49
|
+
[tool.hatch.build.targets.wheel]
|
|
50
|
+
packages = ["bioio_openreadout"]
|
|
51
|
+
|
|
52
|
+
[tool.hatch.build.targets.sdist]
|
|
53
|
+
include = ["bioio_openreadout", "tests", "README.md", "LICENSE-MIT", "LICENSE-APACHE"]
|
|
54
|
+
|
|
55
|
+
[tool.pytest.ini_options]
|
|
56
|
+
addopts = "--import-mode=importlib"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Fixtures: locate corpus files (``OPENREADOUT_CORPUS_DIR`` or ``<repo>/corpus/files``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Callable
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
REPO = Path(__file__).resolve().parents[3]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@pytest.fixture(scope="session")
|
|
15
|
+
def corpus() -> Callable[[str], Path]:
|
|
16
|
+
"""``corpus("name.czi")`` → path, or skip the test if the file is absent."""
|
|
17
|
+
root = Path(os.environ.get("OPENREADOUT_CORPUS_DIR") or REPO / "corpus" / "files")
|
|
18
|
+
|
|
19
|
+
def get(name: str) -> Path:
|
|
20
|
+
p = root / name
|
|
21
|
+
if not p.is_file():
|
|
22
|
+
pytest.skip(f"corpus file {name} not present (cargo xtask corpus fetch --tier smoke)")
|
|
23
|
+
return p
|
|
24
|
+
|
|
25
|
+
return get
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"""bioio-openreadout against bioio and permissively licensed oracle readers.
|
|
2
|
+
|
|
3
|
+
``img.data`` is compared with bioio-nd2 (ND2), czifile (CZI) and liffile (LIF) where those are
|
|
4
|
+
installed; each comparison skips when its reader or corpus file is absent.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from datetime import timedelta
|
|
10
|
+
from importlib.metadata import entry_points, requires
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Any, Callable, List
|
|
13
|
+
|
|
14
|
+
import numpy as np
|
|
15
|
+
import pytest
|
|
16
|
+
|
|
17
|
+
bioio = pytest.importorskip("bioio")
|
|
18
|
+
import bioio_openreadout # noqa: E402
|
|
19
|
+
from bioio import BioImage # noqa: E402
|
|
20
|
+
from bioio_base import exceptions # noqa: E402
|
|
21
|
+
from bioio_openreadout import Reader # noqa: E402
|
|
22
|
+
|
|
23
|
+
Corpus = Callable[[str], Path]
|
|
24
|
+
|
|
25
|
+
CZI = "aics-s-3-t-1-c-3-z-5.czi"
|
|
26
|
+
CZI_MOSAIC = "zenodo7015307-S-2-2x2-T-3-CH-1.czi"
|
|
27
|
+
ND2_RGB_MULTI = "aics-ND2-dims-rgb-t3p2c2z3x64y64.nd2"
|
|
28
|
+
ND2_MULTIPOS = "aics-ND2-dims-p4z5t3c2y32x32.nd2"
|
|
29
|
+
LIF = "aics-s-1-t-4-c-2-z-1.lif"
|
|
30
|
+
|
|
31
|
+
PLUGIN = "bioio-openreadout"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _to_tczyxs(dims: List[str], arr: np.ndarray) -> np.ndarray:
|
|
35
|
+
"""Reorder an oracle array to T, C, Z, Y, X, S (adding size-1 axes, dropping size-1 extras)."""
|
|
36
|
+
for i in reversed(range(len(dims))):
|
|
37
|
+
if dims[i] not in "TCZYXS":
|
|
38
|
+
assert arr.shape[i] == 1, (dims, arr.shape)
|
|
39
|
+
arr = arr.take(0, axis=i)
|
|
40
|
+
dims = dims[:i] + dims[i + 1 :]
|
|
41
|
+
for d in "TCZYXS":
|
|
42
|
+
if d not in dims:
|
|
43
|
+
arr = arr[np.newaxis]
|
|
44
|
+
dims = [d, *dims]
|
|
45
|
+
return arr.transpose([dims.index(d) for d in "TCZYXS"])
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _ours_tczyxs(img: BioImage) -> np.ndarray:
|
|
49
|
+
data = img.data
|
|
50
|
+
return data if data.ndim == 6 else data[..., np.newaxis]
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
# ----- packaging / discovery ---------------------------------------------------------------------
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_entry_point_is_registered() -> None:
|
|
57
|
+
eps = {ep.name: ep for ep in entry_points(group="bioio.readers")}
|
|
58
|
+
assert PLUGIN in eps and eps[PLUGIN].value == "bioio_openreadout"
|
|
59
|
+
assert eps[PLUGIN].load().ReaderMetadata.get_reader() is Reader
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_no_copyleft_dependencies() -> None:
|
|
63
|
+
reqs = " ".join(requires(PLUGIN) or []).lower()
|
|
64
|
+
for gpl in ("bioio-czi", "bioio-lif", "pylibczirw", "readlif", "bioformats"):
|
|
65
|
+
assert gpl not in reqs
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_plugin_is_offered_for_each_extension() -> None:
|
|
69
|
+
from bioio.plugins import get_plugins
|
|
70
|
+
|
|
71
|
+
plugins = get_plugins(use_cache=False)
|
|
72
|
+
for ext in (".czi", ".nd2", ".lif"):
|
|
73
|
+
assert PLUGIN in [p.entrypoint.name for p in plugins[ext]], ext
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@pytest.mark.parametrize("name", [CZI, ND2_MULTIPOS, LIF])
|
|
77
|
+
def test_bioimage_picks_plugin_by_extension(corpus: Corpus, monkeypatch: Any, name: str) -> None:
|
|
78
|
+
"""With bioio-openreadout the only plugin for the extension, BioImage(path) selects it."""
|
|
79
|
+
import bioio.bio_image
|
|
80
|
+
from bioio.plugins import get_plugins
|
|
81
|
+
|
|
82
|
+
path = corpus(name)
|
|
83
|
+
ours_only = {
|
|
84
|
+
ext: [p for p in plugins if p.entrypoint.name == PLUGIN]
|
|
85
|
+
for ext, plugins in get_plugins(use_cache=False).items()
|
|
86
|
+
}
|
|
87
|
+
monkeypatch.setattr(bioio.bio_image, "get_plugins", lambda use_cache=False: ours_only)
|
|
88
|
+
img = BioImage(path)
|
|
89
|
+
assert isinstance(img.reader, Reader)
|
|
90
|
+
assert img.reader.name == PLUGIN
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@pytest.mark.parametrize("name", [CZI, ND2_MULTIPOS, LIF])
|
|
94
|
+
def test_bioimage_plugin_order_in_this_environment(corpus: Corpus, name: str) -> None:
|
|
95
|
+
"""Without overrides, bioio picks us unless a more specific plugin for the extension exists."""
|
|
96
|
+
from bioio.plugins import get_plugins
|
|
97
|
+
|
|
98
|
+
path = corpus(name)
|
|
99
|
+
ext = "." + name.rsplit(".", 1)[1]
|
|
100
|
+
candidates = [p.entrypoint.name for p in get_plugins(use_cache=False)[ext]]
|
|
101
|
+
chosen = BioImage.determine_plugin(path).entrypoint.name
|
|
102
|
+
assert chosen == candidates[0] or chosen == PLUGIN
|
|
103
|
+
if candidates == [PLUGIN]:
|
|
104
|
+
assert isinstance(BioImage(path).reader, Reader)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def test_explicit_reader(corpus: Corpus) -> None:
|
|
108
|
+
img = BioImage(corpus(CZI), reader=Reader)
|
|
109
|
+
assert isinstance(img.reader, Reader)
|
|
110
|
+
assert img.scenes == ("P2", "P3", "P1")
|
|
111
|
+
assert img.dims.order == "TCZYX" and img.shape == (1, 3, 5, 325, 475)
|
|
112
|
+
assert img.channel_names == ["EGFP", "TaRFP", "Bright"]
|
|
113
|
+
ps = img.physical_pixel_sizes
|
|
114
|
+
assert ps.X == pytest.approx(1.0833333) and ps.Y == pytest.approx(1.0833333) and ps.Z == 1.0
|
|
115
|
+
assert img.dtype == np.uint16
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def test_unsupported_file_is_rejected(tmp_path: Path) -> None:
|
|
119
|
+
junk = tmp_path / "junk.czi"
|
|
120
|
+
junk.write_bytes(b"\0" * 4096)
|
|
121
|
+
with pytest.raises(exceptions.UnsupportedFileFormatError):
|
|
122
|
+
Reader(junk)
|
|
123
|
+
with pytest.raises(exceptions.UnsupportedFileFormatError):
|
|
124
|
+
Reader.is_supported_image(junk)
|
|
125
|
+
with pytest.raises(FileNotFoundError):
|
|
126
|
+
Reader(tmp_path / "missing.czi")
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
# ----- data equality against oracles --------------------------------------------------------------
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
@pytest.mark.parametrize("name", [ND2_RGB_MULTI, ND2_MULTIPOS])
|
|
133
|
+
def test_nd2_matches_bioio_nd2(corpus: Corpus, name: str) -> None:
|
|
134
|
+
bioio_nd2 = pytest.importorskip("bioio_nd2")
|
|
135
|
+
path = corpus(name)
|
|
136
|
+
ours = BioImage(path, reader=Reader)
|
|
137
|
+
ref = BioImage(path, reader=bioio_nd2.Reader)
|
|
138
|
+
assert len(ours.scenes) == len(ref.scenes)
|
|
139
|
+
for s in range(len(ref.scenes)):
|
|
140
|
+
ours.set_scene(s)
|
|
141
|
+
ref.set_scene(s)
|
|
142
|
+
assert ours.dims.order == ref.dims.order
|
|
143
|
+
assert ours.shape == ref.shape
|
|
144
|
+
assert ours.channel_names == [str(c) for c in ref.channel_names]
|
|
145
|
+
assert ours.physical_pixel_sizes.X == pytest.approx(ref.physical_pixel_sizes.X)
|
|
146
|
+
want = ref.data
|
|
147
|
+
if "S" in ref.dims.order and ref.dims.S == 3:
|
|
148
|
+
# bioio-nd2 (nd2) returns colour planes in stored B, G, R order; we return R, G, B
|
|
149
|
+
# (docs/formats/nd2.md § RGB sample order).
|
|
150
|
+
want = want[..., ::-1]
|
|
151
|
+
np.testing.assert_array_equal(ours.data, want)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
@pytest.mark.parametrize("name", [CZI, CZI_MOSAIC])
|
|
155
|
+
def test_czi_matches_czifile(corpus: Corpus, name: str) -> None:
|
|
156
|
+
czifile = pytest.importorskip("czifile")
|
|
157
|
+
if not hasattr(czifile.CziFile, "asxarray"): # pre-2025 czifile (e.g. on Python 3.10)
|
|
158
|
+
pytest.skip("czifile with scenes/asxarray (Python >= 3.11) is required")
|
|
159
|
+
path = corpus(name)
|
|
160
|
+
img = BioImage(path, reader=Reader)
|
|
161
|
+
with czifile.CziFile(path, squeeze=False) as czi:
|
|
162
|
+
scene_keys = list(czi.scenes.keys())
|
|
163
|
+
assert len(scene_keys) == len(img.scenes)
|
|
164
|
+
for s, key in enumerate(scene_keys):
|
|
165
|
+
ref = czi.asxarray(scene=key)
|
|
166
|
+
img.set_scene(s)
|
|
167
|
+
expected = _to_tczyxs(list(ref.dims), ref.values)
|
|
168
|
+
np.testing.assert_array_equal(_ours_tczyxs(img), expected)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def test_lif_matches_liffile(corpus: Corpus) -> None:
|
|
172
|
+
liffile = pytest.importorskip("liffile")
|
|
173
|
+
path = corpus(LIF)
|
|
174
|
+
img = BioImage(path, reader=Reader)
|
|
175
|
+
with liffile.LifFile(path) as lif:
|
|
176
|
+
assert len(lif.images) == len(img.scenes)
|
|
177
|
+
for s, series in enumerate(lif.images):
|
|
178
|
+
ref = series.asxarray()
|
|
179
|
+
img.set_scene(s)
|
|
180
|
+
np.testing.assert_array_equal(_ours_tczyxs(img), _to_tczyxs(list(ref.dims), ref.values))
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
# ----- lazy reads and metadata --------------------------------------------------------------------
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def test_lazy_selection_matches_full_read(corpus: Corpus) -> None:
|
|
187
|
+
img = BioImage(corpus(ND2_MULTIPOS), reader=Reader)
|
|
188
|
+
img.set_scene("point name 3")
|
|
189
|
+
full = img.data
|
|
190
|
+
lazy = img.get_image_dask_data("ZYX", T=2, C=1)
|
|
191
|
+
np.testing.assert_array_equal(lazy.compute(), full[2, 1])
|
|
192
|
+
np.testing.assert_array_equal(img.get_image_data("CYX", T=1, Z=4), full[1, :, 4])
|
|
193
|
+
np.testing.assert_array_equal(
|
|
194
|
+
img.reader._read_indexed("TCZYX", [0, [1, 0], slice(0, 2), 3, slice(None)]),
|
|
195
|
+
full[0][[1, 0]][:, 0:2, 3],
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def test_scene_names_fallback_and_rgb(corpus: Corpus) -> None:
|
|
200
|
+
img = BioImage(corpus(ND2_RGB_MULTI), reader=Reader)
|
|
201
|
+
assert img.scenes == ("Position 0", "Position 1")
|
|
202
|
+
assert img.dims.order == "TCZYXS" and img.dims.S == 3
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_metadata(corpus: Corpus) -> None:
|
|
206
|
+
img = BioImage(corpus(LIF), reader=Reader)
|
|
207
|
+
ome = img.ome_metadata
|
|
208
|
+
assert len(ome.images) == len(img.scenes)
|
|
209
|
+
assert ome.images[0].pixels.size_t == 4 and ome.images[0].pixels.size_c == 2
|
|
210
|
+
assert img.metadata is ome
|
|
211
|
+
assert img.xarray_dask_data.attrs["unprocessed"] # vendor tree
|
|
212
|
+
assert img.time_interval == timedelta(seconds=img.reader.file.images[0]["time_increment_s"])
|
|
213
|
+
std = img.standard_metadata
|
|
214
|
+
assert std.image_size_t == 4 and std.pixel_size_x == pytest.approx(img.physical_pixel_sizes.X)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def test_version() -> None:
|
|
218
|
+
assert isinstance(bioio_openreadout.__version__, str)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
# ----- bioio's own plugin checks, resolution levels ----------------------------------------------
|
|
222
|
+
|
|
223
|
+
KIDNEY = "zenodo10577621-Kidney-RAC-3color.czi" # 5 pyramid levels, 4 channels, uint16 mosaic
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def test_bioio_base_reader_checks(corpus: Corpus) -> None:
|
|
227
|
+
"""bioio-base's own plugin test suite: no file handle left open, scene and resolution-level
|
|
228
|
+
switching, dims, dtype, physical sizes, lazy vs in-memory reads, (de)serialization."""
|
|
229
|
+
tu = pytest.importorskip("bioio_base.test_utilities")
|
|
230
|
+
pytest.importorskip("distributed")
|
|
231
|
+
from ome_types import OME
|
|
232
|
+
|
|
233
|
+
scenes = Reader(corpus(CZI)).scenes
|
|
234
|
+
tu.run_image_file_checks(
|
|
235
|
+
ImageContainer=Reader,
|
|
236
|
+
image=corpus(CZI),
|
|
237
|
+
set_scene=scenes[1],
|
|
238
|
+
expected_scenes=scenes,
|
|
239
|
+
expected_current_scene=scenes[1],
|
|
240
|
+
expected_shape=(1, 3, 5, 325, 475),
|
|
241
|
+
expected_dtype=np.dtype(np.uint16),
|
|
242
|
+
expected_dims_order="TCZYX",
|
|
243
|
+
expected_channel_names=["EGFP", "TaRFP", "Bright"],
|
|
244
|
+
expected_physical_pixel_sizes=Reader(corpus(CZI)).physical_pixel_sizes,
|
|
245
|
+
expected_metadata_type=OME,
|
|
246
|
+
reader_kwargs={},
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def test_bioio_base_checks_at_a_resolution_level(corpus: Corpus) -> None:
|
|
251
|
+
tu = pytest.importorskip("bioio_base.test_utilities")
|
|
252
|
+
pytest.importorskip("distributed")
|
|
253
|
+
from ome_types import OME
|
|
254
|
+
|
|
255
|
+
path = corpus(KIDNEY)
|
|
256
|
+
base = Reader(path)
|
|
257
|
+
ps0 = base.physical_pixel_sizes
|
|
258
|
+
levels = base.file.levels(0)
|
|
259
|
+
lv = levels[3]
|
|
260
|
+
tu.run_image_file_checks(
|
|
261
|
+
ImageContainer=Reader,
|
|
262
|
+
image=path,
|
|
263
|
+
set_scene=base.scenes[0],
|
|
264
|
+
expected_scenes=base.scenes,
|
|
265
|
+
expected_current_scene=base.scenes[0],
|
|
266
|
+
expected_shape=(1, 4, 1, lv["size_y"], lv["size_x"]),
|
|
267
|
+
expected_dtype=np.dtype(np.uint16),
|
|
268
|
+
expected_dims_order="TCZYX",
|
|
269
|
+
expected_channel_names=base.channel_names,
|
|
270
|
+
expected_physical_pixel_sizes=type(ps0)(
|
|
271
|
+
ps0.Z, ps0.Y * lv["downsample_y"], ps0.X * lv["downsample_x"]
|
|
272
|
+
),
|
|
273
|
+
expected_metadata_type=OME,
|
|
274
|
+
set_resolution_level=3,
|
|
275
|
+
expected_current_resolution_level=3,
|
|
276
|
+
expected_resolution_levels=(0, 1, 2, 3, 4),
|
|
277
|
+
reader_kwargs={},
|
|
278
|
+
)
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def test_resolution_level_matches_oracle(corpus: Corpus) -> None:
|
|
282
|
+
"""Level 4 equals the committed ground truth: czifile's pixels on the level grid of
|
|
283
|
+
``oracle/czi_levels.py`` (``floor(w / 16) x floor(h / 16)`` from the scene origin, as
|
|
284
|
+
pylibCZIrw's scaled reads). czifile's own ``levels[4]`` spans the union of the subblocks, a
|
|
285
|
+
grid 1-2 px larger, so it is not compared directly."""
|
|
286
|
+
import json
|
|
287
|
+
|
|
288
|
+
xxhash = pytest.importorskip("xxhash")
|
|
289
|
+
path = corpus(KIDNEY)
|
|
290
|
+
repo = Path(__file__).resolve().parents[3]
|
|
291
|
+
oracle = repo / "corpus" / "oracle" / "zenodo10577621-Kidney-RAC-3color.json"
|
|
292
|
+
truth = json.loads(oracle.read_text())
|
|
293
|
+
lv = next(lv for lv in truth["images"][0]["levels"] if lv["level"] == 4)
|
|
294
|
+
img = BioImage(path, reader=Reader)
|
|
295
|
+
img.set_resolution_level(4)
|
|
296
|
+
ours = img.get_image_data("CYX")
|
|
297
|
+
assert ours.shape[1:] == (lv["size_y"], lv["size_x"])
|
|
298
|
+
assert len(lv["planes"]) == ours.shape[0]
|
|
299
|
+
for p in lv["planes"]:
|
|
300
|
+
plane = np.ascontiguousarray(ours[p["c"]], dtype=ours.dtype.newbyteorder("<"))
|
|
301
|
+
assert xxhash.xxh3_128_hexdigest(plane.tobytes()) == p["xxh3"], p
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def test_keep_open_reader_holds_the_file(corpus: Corpus) -> None:
|
|
305
|
+
psutil = pytest.importorskip("psutil")
|
|
306
|
+
path = str(corpus(CZI))
|
|
307
|
+
r = Reader(path, keep_open=True)
|
|
308
|
+
assert path in [f.path for f in psutil.Process().open_files()]
|
|
309
|
+
r.file.close()
|
|
310
|
+
lazy = Reader(path) # default: nothing stays open
|
|
311
|
+
lazy.get_image_data("YX", Z=1, C=2)
|
|
312
|
+
assert path not in [f.path for f in psutil.Process().open_files()]
|