nxxrdct 0.1.0a0__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.
Files changed (31) hide show
  1. nxxrdct-0.1.0a0/LICENSE +25 -0
  2. nxxrdct-0.1.0a0/PKG-INFO +129 -0
  3. nxxrdct-0.1.0a0/README.md +49 -0
  4. nxxrdct-0.1.0a0/pyproject.toml +84 -0
  5. nxxrdct-0.1.0a0/setup.cfg +4 -0
  6. nxxrdct-0.1.0a0/src/nxxrdct/__init__.py +7 -0
  7. nxxrdct-0.1.0a0/src/nxxrdct/application/__init__.py +1 -0
  8. nxxrdct-0.1.0a0/src/nxxrdct/application/nxxrdct.py +359 -0
  9. nxxrdct-0.1.0a0/src/nxxrdct/nxobject/__init__.py +8 -0
  10. nxxrdct-0.1.0a0/src/nxxrdct/nxobject/nxbeam.py +62 -0
  11. nxxrdct-0.1.0a0/src/nxxrdct/nxobject/nxdetector.py +167 -0
  12. nxxrdct-0.1.0a0/src/nxxrdct/nxobject/nxinstrument.py +111 -0
  13. nxxrdct-0.1.0a0/src/nxxrdct/nxobject/nxmonitor.py +86 -0
  14. nxxrdct-0.1.0a0/src/nxxrdct/nxobject/nxmonochromator.py +63 -0
  15. nxxrdct-0.1.0a0/src/nxxrdct/nxobject/nxobject.py +114 -0
  16. nxxrdct-0.1.0a0/src/nxxrdct/nxobject/nxsample.py +152 -0
  17. nxxrdct-0.1.0a0/src/nxxrdct/nxobject/nxsource.py +77 -0
  18. nxxrdct-0.1.0a0/src/nxxrdct/paths/__init__.py +1 -0
  19. nxxrdct-0.1.0a0/src/nxxrdct/paths/nxxrdct.py +68 -0
  20. nxxrdct-0.1.0a0/src/nxxrdct/utils.py +54 -0
  21. nxxrdct-0.1.0a0/src/nxxrdct/version.py +1 -0
  22. nxxrdct-0.1.0a0/src/nxxrdct.egg-info/PKG-INFO +129 -0
  23. nxxrdct-0.1.0a0/src/nxxrdct.egg-info/SOURCES.txt +29 -0
  24. nxxrdct-0.1.0a0/src/nxxrdct.egg-info/dependency_links.txt +1 -0
  25. nxxrdct-0.1.0a0/src/nxxrdct.egg-info/requires.txt +25 -0
  26. nxxrdct-0.1.0a0/src/nxxrdct.egg-info/top_level.txt +1 -0
  27. nxxrdct-0.1.0a0/tests/test_application.py +108 -0
  28. nxxrdct-0.1.0a0/tests/test_nxxrdct.py +84 -0
  29. nxxrdct-0.1.0a0/tests/test_objects.py +81 -0
  30. nxxrdct-0.1.0a0/tests/test_paths.py +15 -0
  31. nxxrdct-0.1.0a0/tests/test_utils.py +34 -0
@@ -0,0 +1,25 @@
1
+
2
+ The nxxrdct library goal is to provide a Python API to create, load, edit, and save files following the NXxrdct application definition
3
+
4
+ nxxrdct is distributed under the MIT license.
5
+
6
+ The MIT license follows:
7
+
8
+ Copyright (c) European Synchrotron Radiation Facility (ESRF)
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
11
+ this software and associated documentation files (the "Software"), to deal in
12
+ the Software without restriction, including without limitation the rights to
13
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
14
+ the Software, and to permit persons to whom the Software is furnished to do so,
15
+ subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
22
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
23
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,129 @@
1
+ Metadata-Version: 2.4
2
+ Name: nxxrdct
3
+ Version: 0.1.0a0
4
+ Summary: Package to create, load, edit, and save files following the nxxrdct application definition.
5
+ Author-email: Pierre-Olivier Autran <pierre-olivier.autran@esrf.fr>
6
+ License:
7
+ The nxxrdct library goal is to provide a Python API to create, load, edit, and save files following the NXxrdct application definition
8
+
9
+ nxxrdct is distributed under the MIT license.
10
+
11
+ The MIT license follows:
12
+
13
+ Copyright (c) European Synchrotron Radiation Facility (ESRF)
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
16
+ this software and associated documentation files (the "Software"), to deal in
17
+ the Software without restriction, including without limitation the rights to
18
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
19
+ the Software, and to permit persons to whom the Software is furnished to do so,
20
+ subject to the following conditions:
21
+
22
+ The above copyright notice and this permission notice shall be included in all
23
+ copies or substantial portions of the Software.
24
+
25
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
27
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
28
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
29
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
+
32
+ Project-URL: Homepage, https://github.com/nexuscontributions/nxxrdct
33
+ Project-URL: Documentation, https://nxxrdct.readthedocs.io/en/stable/
34
+ Project-URL: Repository, https://github.com/nexuscontributions/nxxrdct
35
+ Project-URL: Changelog, https://github.com/nexuscontributions/nxxrdct/-/blob/main/CHANGELOG.md
36
+ Keywords: nxxrdct,Nexus,XRD-CT,Diffraction Tomography,ESRF
37
+ Classifier: Development Status :: 3 - Alpha
38
+ Classifier: Intended Audience :: Developers
39
+ Classifier: Intended Audience :: Science/Research
40
+ Classifier: Programming Language :: Python :: 3
41
+ Classifier: Programming Language :: Python :: 3.10
42
+ Classifier: Programming Language :: Python :: 3.11
43
+ Classifier: Programming Language :: Python :: 3.12
44
+ Classifier: Programming Language :: Python :: 3.13
45
+ Classifier: Programming Language :: Python :: 3.14
46
+ Classifier: Environment :: Console
47
+ Classifier: License :: OSI Approved :: MIT License
48
+ Classifier: Operating System :: Unix
49
+ Classifier: Operating System :: MacOS :: MacOS X
50
+ Classifier: Operating System :: POSIX
51
+ Classifier: Topic :: Scientific/Engineering :: Physics
52
+ Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
53
+ Requires-Python: >=3.10
54
+ Description-Content-Type: text/markdown
55
+ License-File: LICENSE
56
+ Requires-Dist: numpy
57
+ Requires-Dist: h5py>=3.0
58
+ Requires-Dist: silx>=2.0
59
+ Requires-Dist: pint
60
+ Requires-Dist: packaging
61
+ Provides-Extra: test
62
+ Requires-Dist: pytest; extra == "test"
63
+ Requires-Dist: black; extra == "test"
64
+ Requires-Dist: isort; extra == "test"
65
+ Requires-Dist: ruff; extra == "test"
66
+ Provides-Extra: doc
67
+ Requires-Dist: Sphinx; extra == "doc"
68
+ Requires-Dist: nbsphinx; extra == "doc"
69
+ Requires-Dist: jupyterlab; extra == "doc"
70
+ Requires-Dist: ipykernel; extra == "doc"
71
+ Requires-Dist: nbconvert; extra == "doc"
72
+ Requires-Dist: pandoc; extra == "doc"
73
+ Requires-Dist: scikit-image; extra == "doc"
74
+ Requires-Dist: h5glance; extra == "doc"
75
+ Requires-Dist: jupyter_client; extra == "doc"
76
+ Requires-Dist: pydata_sphinx_theme; extra == "doc"
77
+ Requires-Dist: sphinx_autodoc_typehints; extra == "doc"
78
+ Requires-Dist: myst-parser; extra == "doc"
79
+ Dynamic: license-file
80
+
81
+ <p align="left">
82
+ <img src="doc/_static/nxxrdct.png" alt="NXxrdct" width="180">
83
+ </p>
84
+
85
+ The goal of the `nxxrdct` project is to provide a python API to create, load, edit, and save [NXxrdct](https://manual.nexusformat.org/classes/applications/nxxrdct.html) application definition files.
86
+
87
+
88
+ Please find at https://nxxrdct.readthedocs.io/en/latest/ the latest documentation
89
+
90
+ ```bash
91
+ pip install nxxrdct
92
+ ```
93
+
94
+ Add the optional extras when you need documentation or development tooling:
95
+
96
+ ```bash
97
+ pip install nxxrdct[doc,test]
98
+ ```
99
+
100
+ ## Quick Start
101
+ ```python
102
+ import numpy as np
103
+ import pint
104
+
105
+ from nxxrdct import NXxrdct
106
+
107
+ ureg = pint.get_application_registry()
108
+
109
+ nx = NXxrdct()
110
+ nx.title = "Demo XRD-CT"
111
+ nx.beam.incident_energy = 60 * ureg.keV
112
+ nx.sample.name = "sample-01"
113
+ nx.sample.rotation_angle = np.linspace(0, 180, 181) * ureg.degree
114
+ nx.instrument.detector.data = np.zeros((181, 256, 256))
115
+ nx.instrument.detector.count_time = 0.1 * ureg.second
116
+
117
+ nx.save("demo_xrdct.h5", data_path="entry")
118
+ ```
119
+
120
+ ## Documentation and Support
121
+ - Latest documentation: https://nxxrdct.readthedocs.io/en/latest/
122
+ - API reference: https://nxxrdct.readthedocs.io/en/latest/api.html
123
+ - Report issues and follow development on GitHub: https://github.com/nexuscontributions/nxxrdct/issues
124
+
125
+ ## Contributing
126
+ Contributions and feedback are welcome. Please open an issue or submit a merge request on Github. See the development guide in `doc/development` for details on setting up a local environment and running the test suite.
127
+
128
+ ## License
129
+ `nxxrdct` is released under the MIT License. See `LICENSE` for the full text.
@@ -0,0 +1,49 @@
1
+ <p align="left">
2
+ <img src="doc/_static/nxxrdct.png" alt="NXxrdct" width="180">
3
+ </p>
4
+
5
+ The goal of the `nxxrdct` project is to provide a python API to create, load, edit, and save [NXxrdct](https://manual.nexusformat.org/classes/applications/nxxrdct.html) application definition files.
6
+
7
+
8
+ Please find at https://nxxrdct.readthedocs.io/en/latest/ the latest documentation
9
+
10
+ ```bash
11
+ pip install nxxrdct
12
+ ```
13
+
14
+ Add the optional extras when you need documentation or development tooling:
15
+
16
+ ```bash
17
+ pip install nxxrdct[doc,test]
18
+ ```
19
+
20
+ ## Quick Start
21
+ ```python
22
+ import numpy as np
23
+ import pint
24
+
25
+ from nxxrdct import NXxrdct
26
+
27
+ ureg = pint.get_application_registry()
28
+
29
+ nx = NXxrdct()
30
+ nx.title = "Demo XRD-CT"
31
+ nx.beam.incident_energy = 60 * ureg.keV
32
+ nx.sample.name = "sample-01"
33
+ nx.sample.rotation_angle = np.linspace(0, 180, 181) * ureg.degree
34
+ nx.instrument.detector.data = np.zeros((181, 256, 256))
35
+ nx.instrument.detector.count_time = 0.1 * ureg.second
36
+
37
+ nx.save("demo_xrdct.h5", data_path="entry")
38
+ ```
39
+
40
+ ## Documentation and Support
41
+ - Latest documentation: https://nxxrdct.readthedocs.io/en/latest/
42
+ - API reference: https://nxxrdct.readthedocs.io/en/latest/api.html
43
+ - Report issues and follow development on GitHub: https://github.com/nexuscontributions/nxxrdct/issues
44
+
45
+ ## Contributing
46
+ Contributions and feedback are welcome. Please open an issue or submit a merge request on Github. See the development guide in `doc/development` for details on setting up a local environment and running the test suite.
47
+
48
+ ## License
49
+ `nxxrdct` is released under the MIT License. See `LICENSE` for the full text.
@@ -0,0 +1,84 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "nxxrdct"
7
+ authors = [
8
+ {name = "Pierre-Olivier Autran", email = "pierre-olivier.autran@esrf.fr"},
9
+ ]
10
+
11
+ dynamic = ["version"]
12
+ description = "Package to create, load, edit, and save files following the nxxrdct application definition."
13
+ readme = "README.md"
14
+
15
+ requires-python = ">=3.10"
16
+ keywords = ["nxxrdct", "Nexus", "XRD-CT", "Diffraction Tomography", "ESRF"]
17
+ license = {file = "LICENSE"}
18
+ classifiers = [
19
+ "Development Status :: 3 - Alpha",
20
+ "Intended Audience :: Developers",
21
+ "Intended Audience :: Science/Research",
22
+ "Programming Language :: Python :: 3",
23
+ "Programming Language :: Python :: 3.10",
24
+ "Programming Language :: Python :: 3.11",
25
+ "Programming Language :: Python :: 3.12",
26
+ "Programming Language :: Python :: 3.13",
27
+ "Programming Language :: Python :: 3.14",
28
+ "Environment :: Console",
29
+ "License :: OSI Approved :: MIT License",
30
+ "Operating System :: Unix",
31
+ "Operating System :: MacOS :: MacOS X",
32
+ "Operating System :: POSIX",
33
+ "Topic :: Scientific/Engineering :: Physics",
34
+ "Topic :: Scientific/Engineering :: Medical Science Apps.",
35
+ ]
36
+
37
+ dependencies = [
38
+ "numpy",
39
+ "h5py>=3.0",
40
+ "silx>=2.0",
41
+ "pint",
42
+ "packaging",
43
+ ]
44
+
45
+ [project.urls]
46
+ Homepage = "https://github.com/nexuscontributions/nxxrdct"
47
+ Documentation = "https://nxxrdct.readthedocs.io/en/stable/"
48
+ Repository = "https://github.com/nexuscontributions/nxxrdct"
49
+ Changelog = "https://github.com/nexuscontributions/nxxrdct/-/blob/main/CHANGELOG.md"
50
+
51
+ [project.optional-dependencies]
52
+ test = [
53
+ "pytest",
54
+ "black",
55
+ "isort",
56
+ "ruff"
57
+ ]
58
+ doc = [
59
+ "Sphinx",
60
+ "nbsphinx",
61
+ "jupyterlab",
62
+ "ipykernel",
63
+ "nbconvert",
64
+ "pandoc",
65
+ "scikit-image",
66
+ "h5glance",
67
+ "jupyter_client",
68
+ "pydata_sphinx_theme",
69
+ "sphinx_autodoc_typehints",
70
+ "myst-parser",
71
+ ]
72
+
73
+ [build_sphinx]
74
+ source_dir = "doc"
75
+ build_dir = "build/sphinx"
76
+
77
+ [tool.setuptools.dynamic]
78
+ version = {attr = "nxxrdct.version.version"}
79
+
80
+ [tool.setuptools.package-dir]
81
+ "" = "src"
82
+
83
+ [tool.setuptools.packages.find]
84
+ where = ["src"] # list of folders that contain the packages (["."] by default)
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,7 @@
1
+ """
2
+ API to create, load, edit, and save NXxrdct application definition files.
3
+ """
4
+
5
+ from nxxrdct.version import version as __version__ # noqa: F401
6
+
7
+ from .application.nxxrdct import NXxrdct # noqa: F401
@@ -0,0 +1 @@
1
+ from .nxxrdct import NXxrdct, copy_nxxrdct_file # noqa: F401
@@ -0,0 +1,359 @@
1
+ """Define NXxrdct application and related functions and classes."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ import os
7
+ from datetime import datetime
8
+
9
+ import h5py
10
+ from silx.io.utils import open as hdf5_open
11
+
12
+ from nxxrdct.nxobject.nxbeam import NXbeam
13
+ from nxxrdct.nxobject.nxinstrument import NXinstrument
14
+ from nxxrdct.nxobject.nxmonitor import NXmonitor
15
+ from nxxrdct.nxobject.nxobject import NXobject
16
+ from nxxrdct.nxobject.nxsample import NXsample
17
+ from nxxrdct.paths.nxxrdct import LATEST_VERSION, get_paths
18
+ from nxxrdct.utils import get_data
19
+
20
+ _logger = logging.getLogger(__name__)
21
+
22
+ __all__ = ["NXxrdct", "copy_nxxrdct_file"]
23
+
24
+
25
+ class NXxrdct(NXobject):
26
+ """
27
+ Class defining an NXxrdct entry.
28
+ """
29
+
30
+ def __init__(self, parent: NXobject | None = None) -> None:
31
+ super().__init__(node_name="", parent=parent)
32
+ self._set_freeze(False)
33
+ self._start_time = None
34
+ self._end_time = None
35
+ self._title = None
36
+ self._intensity = None
37
+ self._beam = NXbeam(node_name="beam", parent=self)
38
+ self._instrument = NXinstrument(node_name="instrument", parent=self)
39
+ self._sample = NXsample(node_name="sample", parent=self)
40
+ self._control = NXmonitor(node_name="control", parent=self)
41
+ self._set_freeze(True)
42
+
43
+ @property
44
+ def start_time(self) -> datetime | str | None:
45
+ return self._start_time
46
+
47
+ @start_time.setter
48
+ def start_time(self, value: datetime | str | None):
49
+ if not isinstance(value, (type(None), datetime, str)):
50
+ raise TypeError(
51
+ f"start_time is expected to be datetime, str, or None. Not {type(value)}"
52
+ )
53
+ self._start_time = value
54
+
55
+ @property
56
+ def end_time(self) -> datetime | str | None:
57
+ return self._end_time
58
+
59
+ @end_time.setter
60
+ def end_time(self, value: datetime | str | None):
61
+ if not isinstance(value, (type(None), datetime, str)):
62
+ raise TypeError(
63
+ f"end_time is expected to be datetime, str, or None. Not {type(value)}"
64
+ )
65
+ self._end_time = value
66
+
67
+ @property
68
+ def title(self) -> str | None:
69
+ return self._title
70
+
71
+ @title.setter
72
+ def title(self, value: str | None):
73
+ if not isinstance(value, (type(None), str)):
74
+ raise TypeError(f"title is expected to be str or None. Not {type(value)}")
75
+ self._title = value
76
+
77
+ @property
78
+ def intensity(self):
79
+ return self._intensity
80
+
81
+ @intensity.setter
82
+ def intensity(self, value):
83
+ self._intensity = value
84
+
85
+ @property
86
+ def beam(self) -> NXbeam | None:
87
+ return self._beam
88
+
89
+ @beam.setter
90
+ def beam(self, value: NXbeam | None) -> None:
91
+ if not isinstance(value, (type(None), NXbeam)):
92
+ raise TypeError(
93
+ f"beam is expected to be {NXbeam} or None. Not {type(value)}"
94
+ )
95
+ self._beam = value
96
+
97
+ @property
98
+ def instrument(self) -> NXinstrument | None:
99
+ return self._instrument
100
+
101
+ @instrument.setter
102
+ def instrument(self, value: NXinstrument | None) -> None:
103
+ if not isinstance(value, (type(None), NXinstrument)):
104
+ raise TypeError(
105
+ f"instrument is expected to be {NXinstrument} or None. Not {type(value)}"
106
+ )
107
+ self._instrument = value
108
+
109
+ @property
110
+ def sample(self) -> NXsample | None:
111
+ return self._sample
112
+
113
+ @sample.setter
114
+ def sample(self, value: NXsample | None) -> None:
115
+ if not isinstance(value, (type(None), NXsample)):
116
+ raise TypeError(
117
+ f"sample is expected to be {NXsample} or None. Not {type(value)}"
118
+ )
119
+ self._sample = value
120
+
121
+ @property
122
+ def control(self) -> NXmonitor | None:
123
+ return self._control
124
+
125
+ @control.setter
126
+ def control(self, value: NXmonitor | None) -> None:
127
+ if not isinstance(value, (type(None), NXmonitor)):
128
+ raise TypeError(
129
+ f"control is expected to be {NXmonitor} or None. Not {type(value)}"
130
+ )
131
+ self._control = value
132
+
133
+ def to_nx_dict(
134
+ self,
135
+ nexus_path_version: float | None = None,
136
+ data_path: str | None = None,
137
+ ) -> dict:
138
+ if data_path is None:
139
+ data_path = ""
140
+
141
+ nexus_paths = get_paths(nexus_path_version)
142
+ nx_dict = {}
143
+
144
+ if self.sample is not None:
145
+ nx_dict.update(self.sample.to_nx_dict(nexus_path_version))
146
+ else:
147
+ _logger.info("no sample found. Won't be saved")
148
+
149
+ if self.instrument is not None:
150
+ nx_dict.update(self.instrument.to_nx_dict(nexus_path_version))
151
+ else:
152
+ _logger.info("no instrument found. Won't be saved")
153
+
154
+ if self.control is not None:
155
+ nx_dict.update(self.control.to_nx_dict(nexus_path_version))
156
+ else:
157
+ _logger.info("no control found. Won't be saved")
158
+
159
+ if self.beam is not None:
160
+ nx_dict.update(self.beam.to_nx_dict(nexus_path_version))
161
+ else:
162
+ _logger.info("no beam found. Won't be saved")
163
+
164
+ if self.start_time is not None:
165
+ path = f"{self.path}/{nexus_paths.START_TIME_PATH}"
166
+ start_time = (
167
+ self.start_time.isoformat()
168
+ if isinstance(self.start_time, datetime)
169
+ else self.start_time
170
+ )
171
+ nx_dict[path] = start_time
172
+ if self.end_time is not None:
173
+ path = f"{self.path}/{nexus_paths.END_TIME_PATH}"
174
+ end_time = (
175
+ self.end_time.isoformat()
176
+ if isinstance(self.end_time, datetime)
177
+ else self.end_time
178
+ )
179
+ nx_dict[path] = end_time
180
+ if self.title is not None:
181
+ path_title = f"{self.path}/{nexus_paths.NAME_PATH}"
182
+ nx_dict[path_title] = self.title
183
+
184
+ if self.intensity is not None:
185
+ data_path_group = f"{self.path}/{nexus_paths.DATA_GROUP}"
186
+ nx_dict[f"{data_path_group}/intensity"] = self.intensity
187
+ nx_dict[f"{data_path_group}/intensity@signal"] = 1
188
+ nx_dict[f"{data_path_group}@NX_class"] = "NXdata"
189
+ nx_dict[f"{data_path_group}@signal"] = "intensity"
190
+ nx_dict[f"{self.path}@default"] = nexus_paths.DATA_GROUP
191
+
192
+ axes = []
193
+ if self.sample is not None and self.sample.translation_values is not None:
194
+ translation_path = (
195
+ f"/{data_path}/{self.sample.path}/translation_values"
196
+ ).replace("//", "/")
197
+ nx_dict[f">/{data_path_group}/translation_values"] = translation_path
198
+ axes.append("translation_values")
199
+ if self.sample is not None and self.sample.rotation_angle is not None:
200
+ rotation_path = (
201
+ f"/{data_path}/{self.sample.path}/rotation_angle"
202
+ ).replace("//", "/")
203
+ nx_dict[f">/{data_path_group}/rotation_angles"] = rotation_path
204
+ axes.append("rotation_angles")
205
+ if (
206
+ self.instrument is not None
207
+ and self.instrument.detector is not None
208
+ and self.instrument.detector.diffraction_channel is not None
209
+ ):
210
+ channel_path = (
211
+ f"/{data_path}/{self.instrument.detector.path}/diffraction_channel"
212
+ ).replace("//", "/")
213
+ nx_dict[f">/{data_path_group}/diffraction_channel"] = channel_path
214
+ axes.append("diffraction_channel")
215
+ if axes:
216
+ nx_dict[f"{data_path_group}@axes"] = ":".join(axes)
217
+
218
+ if nx_dict:
219
+ nx_dict[f"{self.path}@NX_class"] = "NXentry"
220
+ nx_dict[f"{self.path}@definition"] = "NXxrdct"
221
+ nx_dict[f"{self.path}/definition"] = "NXxrdct"
222
+ nx_dict[f"{self.path}@version"] = nexus_paths.VERSION
223
+
224
+ return nx_dict
225
+
226
+ def load(self, file_path: str, data_path: str) -> NXobject:
227
+ if not os.path.exists(file_path):
228
+ raise IOError(f"{file_path} does not exist")
229
+ with hdf5_open(file_path) as h5f:
230
+ if data_path not in h5f:
231
+ raise ValueError(f"{data_path} cannot be found in {file_path}")
232
+ root_node = h5f[data_path]
233
+ if "version" in root_node.attrs:
234
+ nexus_version = root_node.attrs["version"]
235
+ else:
236
+ _logger.warning(
237
+ f"Unable to find nexus version associated with {data_path}@{file_path}"
238
+ )
239
+ nexus_version = LATEST_VERSION
240
+
241
+ nexus_paths = get_paths(nexus_version)
242
+ start_time = get_data(
243
+ file_path=file_path,
244
+ data_path="/".join([data_path, nexus_paths.START_TIME_PATH]),
245
+ )
246
+ try:
247
+ start_time = datetime.fromisoformat(start_time)
248
+ except Exception:
249
+ start_time = str(start_time) if start_time is not None else None
250
+ self.start_time = start_time
251
+
252
+ end_time = get_data(
253
+ file_path=file_path,
254
+ data_path="/".join([data_path, nexus_paths.END_TIME_PATH]),
255
+ )
256
+ try:
257
+ end_time = datetime.fromisoformat(end_time)
258
+ except Exception:
259
+ end_time = str(end_time) if end_time is not None else None
260
+ self.end_time = end_time
261
+
262
+ self.title = get_data(
263
+ file_path=file_path, data_path="/".join([data_path, nexus_paths.NAME_PATH])
264
+ )
265
+
266
+ if self.sample is not None:
267
+ self.sample._load(
268
+ file_path, "/".join([data_path, "sample"]), nexus_version=nexus_version
269
+ )
270
+ if self.beam is not None:
271
+ self.beam._load(
272
+ file_path, "/".join([data_path, nexus_paths.BEAM_PATH]), nexus_version
273
+ )
274
+ if self.instrument is not None:
275
+ self.instrument._load(
276
+ file_path,
277
+ "/".join([data_path, "instrument"]),
278
+ nexus_version=nexus_version,
279
+ )
280
+ if self.control is not None:
281
+ self.control._load(
282
+ file_path, "/".join([data_path, "control"]), nexus_version=nexus_version
283
+ )
284
+ self.intensity = get_data(
285
+ file_path,
286
+ "/".join([data_path, nexus_paths.DATA_GROUP, "intensity"]),
287
+ )
288
+ return self
289
+
290
+ def save(
291
+ self,
292
+ file_path: str,
293
+ data_path: str,
294
+ nexus_path_version: float | None = None,
295
+ overwrite: bool = False,
296
+ ) -> None:
297
+ super().save(
298
+ file_path=file_path,
299
+ data_path=data_path,
300
+ nexus_path_version=nexus_path_version,
301
+ overwrite=overwrite,
302
+ )
303
+
304
+ @staticmethod
305
+ def get_valid_entries(file_path: str) -> tuple:
306
+ if not os.path.isfile(file_path):
307
+ raise ValueError("given file path should be a file")
308
+
309
+ def browse_group(group):
310
+ res_buf = []
311
+ for entry_alias in group.keys():
312
+ entry = group.get(entry_alias)
313
+ if isinstance(entry, h5py.Group):
314
+ if NXxrdct.node_is_nxxrdct(entry):
315
+ res_buf.append(entry.name)
316
+ else:
317
+ res_buf.extend(browse_group(entry))
318
+ return res_buf
319
+
320
+ with hdf5_open(file_path) as h5f:
321
+ res = browse_group(h5f)
322
+ res.sort()
323
+ return tuple(res)
324
+
325
+ @staticmethod
326
+ def node_is_nxxrdct(node: h5py.Group) -> bool:
327
+ if "definition" in node.attrs and str(node.attrs["definition"]).lower() == "nxxrdct":
328
+ return True
329
+ if "NX_class" not in node.attrs and "NXclass" not in node.attrs:
330
+ return False
331
+ if "instrument" in node:
332
+ instrument = node["instrument"]
333
+ if "NX_class" in instrument.attrs and instrument.attrs["NX_class"] in (
334
+ "NXinstrument",
335
+ b"NXinstrument",
336
+ ):
337
+ return "detector" in instrument
338
+ return False
339
+
340
+
341
+ def copy_nxxrdct_file(
342
+ input_file: str,
343
+ output_file: str,
344
+ entries: tuple | None,
345
+ overwrite: bool = False,
346
+ ):
347
+ input_file = os.path.abspath(input_file)
348
+ output_file = os.path.abspath(output_file)
349
+ if input_file == output_file:
350
+ raise ValueError("input file and output file are the same")
351
+
352
+ if entries is None:
353
+ entries = NXxrdct.get_valid_entries(file_path=input_file)
354
+ if len(entries) == 0:
355
+ _logger.warning(f"no valid entries for {input_file}")
356
+
357
+ for entry in entries:
358
+ nx_xrdct = NXxrdct().load(input_file, entry)
359
+ nx_xrdct.save(output_file, entry, overwrite=overwrite)
@@ -0,0 +1,8 @@
1
+ from .nxbeam import NXbeam # noqa: F401
2
+ from .nxdetector import NXdetector # noqa: F401
3
+ from .nxinstrument import NXinstrument # noqa: F401
4
+ from .nxmonitor import NXmonitor # noqa: F401
5
+ from .nxmonochromator import NXmonochromator # noqa: F401
6
+ from .nxobject import NXobject # noqa: F401
7
+ from .nxsample import NXsample # noqa: F401
8
+ from .nxsource import NXsource # noqa: F401