modacor 1.0.0__py3-none-any.whl

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 (120) hide show
  1. modacor/__init__.py +30 -0
  2. modacor/dataclasses/__init__.py +0 -0
  3. modacor/dataclasses/basedata.py +973 -0
  4. modacor/dataclasses/databundle.py +23 -0
  5. modacor/dataclasses/helpers.py +45 -0
  6. modacor/dataclasses/messagehandler.py +75 -0
  7. modacor/dataclasses/process_step.py +233 -0
  8. modacor/dataclasses/process_step_describer.py +146 -0
  9. modacor/dataclasses/processing_data.py +59 -0
  10. modacor/dataclasses/trace_event.py +118 -0
  11. modacor/dataclasses/uncertainty_tools.py +132 -0
  12. modacor/dataclasses/validators.py +84 -0
  13. modacor/debug/pipeline_tracer.py +548 -0
  14. modacor/io/__init__.py +33 -0
  15. modacor/io/csv/__init__.py +0 -0
  16. modacor/io/csv/csv_sink.py +114 -0
  17. modacor/io/csv/csv_source.py +210 -0
  18. modacor/io/hdf/__init__.py +27 -0
  19. modacor/io/hdf/hdf_source.py +120 -0
  20. modacor/io/io_sink.py +41 -0
  21. modacor/io/io_sinks.py +61 -0
  22. modacor/io/io_source.py +164 -0
  23. modacor/io/io_sources.py +208 -0
  24. modacor/io/processing_path.py +113 -0
  25. modacor/io/tiled/__init__.py +16 -0
  26. modacor/io/tiled/tiled_source.py +403 -0
  27. modacor/io/yaml/__init__.py +27 -0
  28. modacor/io/yaml/yaml_source.py +116 -0
  29. modacor/modules/__init__.py +53 -0
  30. modacor/modules/base_modules/__init__.py +0 -0
  31. modacor/modules/base_modules/append_processing_data.py +329 -0
  32. modacor/modules/base_modules/append_sink.py +141 -0
  33. modacor/modules/base_modules/append_source.py +181 -0
  34. modacor/modules/base_modules/bitwise_or_masks.py +113 -0
  35. modacor/modules/base_modules/combine_uncertainties.py +120 -0
  36. modacor/modules/base_modules/combine_uncertainties_max.py +105 -0
  37. modacor/modules/base_modules/divide.py +82 -0
  38. modacor/modules/base_modules/find_scale_factor1d.py +373 -0
  39. modacor/modules/base_modules/multiply.py +77 -0
  40. modacor/modules/base_modules/multiply_databundles.py +73 -0
  41. modacor/modules/base_modules/poisson_uncertainties.py +69 -0
  42. modacor/modules/base_modules/reduce_dimensionality.py +252 -0
  43. modacor/modules/base_modules/sink_processing_data.py +80 -0
  44. modacor/modules/base_modules/subtract.py +80 -0
  45. modacor/modules/base_modules/subtract_databundles.py +67 -0
  46. modacor/modules/base_modules/units_label_update.py +66 -0
  47. modacor/modules/instrument_modules/__init__.py +0 -0
  48. modacor/modules/instrument_modules/readme.md +9 -0
  49. modacor/modules/technique_modules/__init__.py +0 -0
  50. modacor/modules/technique_modules/scattering/__init__.py +0 -0
  51. modacor/modules/technique_modules/scattering/geometry_helpers.py +114 -0
  52. modacor/modules/technique_modules/scattering/index_pixels.py +492 -0
  53. modacor/modules/technique_modules/scattering/indexed_averager.py +628 -0
  54. modacor/modules/technique_modules/scattering/pixel_coordinates_3d.py +417 -0
  55. modacor/modules/technique_modules/scattering/solid_angle_correction.py +63 -0
  56. modacor/modules/technique_modules/scattering/xs_geometry.py +571 -0
  57. modacor/modules/technique_modules/scattering/xs_geometry_from_pixel_coordinates.py +293 -0
  58. modacor/runner/__init__.py +0 -0
  59. modacor/runner/pipeline.py +749 -0
  60. modacor/runner/process_step_registry.py +224 -0
  61. modacor/tests/__init__.py +27 -0
  62. modacor/tests/dataclasses/test_basedata.py +519 -0
  63. modacor/tests/dataclasses/test_basedata_operations.py +439 -0
  64. modacor/tests/dataclasses/test_basedata_to_base_units.py +57 -0
  65. modacor/tests/dataclasses/test_process_step_describer.py +73 -0
  66. modacor/tests/dataclasses/test_processstep.py +282 -0
  67. modacor/tests/debug/test_tracing_integration.py +188 -0
  68. modacor/tests/integration/__init__.py +0 -0
  69. modacor/tests/integration/test_pipeline_run.py +238 -0
  70. modacor/tests/io/__init__.py +27 -0
  71. modacor/tests/io/csv/__init__.py +0 -0
  72. modacor/tests/io/csv/test_csv_source.py +156 -0
  73. modacor/tests/io/hdf/__init__.py +27 -0
  74. modacor/tests/io/hdf/test_hdf_source.py +92 -0
  75. modacor/tests/io/test_io_sources.py +119 -0
  76. modacor/tests/io/tiled/__init__.py +12 -0
  77. modacor/tests/io/tiled/test_tiled_source.py +120 -0
  78. modacor/tests/io/yaml/__init__.py +27 -0
  79. modacor/tests/io/yaml/static_data_example.yaml +26 -0
  80. modacor/tests/io/yaml/test_yaml_source.py +47 -0
  81. modacor/tests/modules/__init__.py +27 -0
  82. modacor/tests/modules/base_modules/__init__.py +27 -0
  83. modacor/tests/modules/base_modules/test_append_processing_data.py +219 -0
  84. modacor/tests/modules/base_modules/test_append_sink.py +76 -0
  85. modacor/tests/modules/base_modules/test_append_source.py +180 -0
  86. modacor/tests/modules/base_modules/test_bitwise_or_masks.py +264 -0
  87. modacor/tests/modules/base_modules/test_combine_uncertainties.py +105 -0
  88. modacor/tests/modules/base_modules/test_combine_uncertainties_max.py +109 -0
  89. modacor/tests/modules/base_modules/test_divide.py +140 -0
  90. modacor/tests/modules/base_modules/test_find_scale_factor1d.py +220 -0
  91. modacor/tests/modules/base_modules/test_multiply.py +113 -0
  92. modacor/tests/modules/base_modules/test_multiply_databundles.py +136 -0
  93. modacor/tests/modules/base_modules/test_poisson_uncertainties.py +61 -0
  94. modacor/tests/modules/base_modules/test_reduce_dimensionality.py +358 -0
  95. modacor/tests/modules/base_modules/test_sink_processing_data.py +119 -0
  96. modacor/tests/modules/base_modules/test_subtract.py +111 -0
  97. modacor/tests/modules/base_modules/test_subtract_databundles.py +136 -0
  98. modacor/tests/modules/base_modules/test_units_label_update.py +91 -0
  99. modacor/tests/modules/technique_modules/__init__.py +0 -0
  100. modacor/tests/modules/technique_modules/scattering/__init__.py +0 -0
  101. modacor/tests/modules/technique_modules/scattering/test_geometry_helpers.py +198 -0
  102. modacor/tests/modules/technique_modules/scattering/test_index_pixels.py +426 -0
  103. modacor/tests/modules/technique_modules/scattering/test_indexed_averaging.py +559 -0
  104. modacor/tests/modules/technique_modules/scattering/test_pixel_coordinates_3d.py +282 -0
  105. modacor/tests/modules/technique_modules/scattering/test_xs_geometry_from_pixel_coordinates.py +224 -0
  106. modacor/tests/modules/technique_modules/scattering/test_xsgeometry.py +635 -0
  107. modacor/tests/requirements.txt +12 -0
  108. modacor/tests/runner/test_pipeline.py +438 -0
  109. modacor/tests/runner/test_process_step_registry.py +65 -0
  110. modacor/tests/test_import.py +43 -0
  111. modacor/tests/test_modacor.py +17 -0
  112. modacor/tests/test_units.py +79 -0
  113. modacor/units.py +97 -0
  114. modacor-1.0.0.dist-info/METADATA +482 -0
  115. modacor-1.0.0.dist-info/RECORD +120 -0
  116. modacor-1.0.0.dist-info/WHEEL +5 -0
  117. modacor-1.0.0.dist-info/licenses/AUTHORS.md +11 -0
  118. modacor-1.0.0.dist-info/licenses/LICENSE +11 -0
  119. modacor-1.0.0.dist-info/licenses/LICENSE.txt +11 -0
  120. modacor-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,224 @@
1
+ # SPDX-License-Identifier: BSD-3-Clause
2
+ # /usr/bin/env python3
3
+ # -*- coding: utf-8 -*-
4
+
5
+ from __future__ import annotations
6
+
7
+ __coding__ = "utf-8"
8
+ __authors__ = ["Brian R. Pauw"] # add names to the list as appropriate
9
+ __copyright__ = "Copyright 2025, The MoDaCor team"
10
+ __date__ = "25/11/2025"
11
+ __status__ = "Development" # "Development", "Production"
12
+ # end of header and standard imports
13
+
14
+ import importlib
15
+ import re
16
+ from pathlib import Path
17
+ from typing import Dict, Type
18
+
19
+ from ..dataclasses.process_step import ProcessStep
20
+
21
+ # ---------------------------------------------------------------------------
22
+ # Name / path helpers
23
+ # ---------------------------------------------------------------------------
24
+
25
+
26
+ def _pascal_to_snake(name: str) -> str:
27
+ """
28
+ Convert from PascalCase to snake_case module name.
29
+
30
+ Examples
31
+ --------
32
+ XSGeometry -> xs_geometry
33
+ Divide -> divide
34
+ Q2Mapper -> q2_mapper
35
+ APIClient -> api_client
36
+ """
37
+ return re.sub(r"(?<!^)(?=[A-Z][a-z])", "_", name).lower()
38
+
39
+
40
+ def _path_to_module_name(py_file: Path, package_root: Path) -> str:
41
+ """
42
+ Convert a file path under the given package_root into a dotted module name.
43
+
44
+ Example
45
+ -------
46
+ py_file = /.../modacor/modules/technique_modules/scattering/xs_geometry.py
47
+ package_root = /.../modacor
48
+
49
+ -> "modacor.modules.technique_modules.scattering.xs_geometry"
50
+ """
51
+ rel = py_file.with_suffix("").relative_to(package_root)
52
+ return ".".join((package_root.name, *rel.parts))
53
+
54
+
55
+ def find_module(modules_root: Path, module_name: str) -> str:
56
+ """
57
+ Find the fully-qualified module path for a snake_case module name.
58
+
59
+ Searches under: modules/**/<module_name>.py
60
+
61
+ Parameters
62
+ ----------
63
+ modules_root :
64
+ Path to the 'modules' directory, e.g. .../modacor/modules
65
+ module_name :
66
+ Snake_case module name, e.g. "xs_geometry", "divide".
67
+
68
+ Returns
69
+ -------
70
+ str
71
+ Fully-qualified module path, e.g.
72
+ "modacor.modules.technique_modules.scattering.xs_geometry".
73
+
74
+ Raises
75
+ ------
76
+ ModuleNotFoundError
77
+ If no matching file is found.
78
+ RuntimeError
79
+ If multiple matching files are found (ambiguous).
80
+ """
81
+ package_root = modules_root.parent # e.g. .../modacor
82
+
83
+ candidates = [p for p in modules_root.rglob(f"{module_name}.py") if p.is_file()]
84
+
85
+ if not candidates:
86
+ raise ModuleNotFoundError(f"No module file '{module_name}.py' found under {modules_root}.")
87
+
88
+ if len(candidates) > 1:
89
+ details = ", ".join(str(c) for c in candidates)
90
+ raise RuntimeError(f"Ambiguous module name '{module_name}': found multiple candidates: {details}")
91
+
92
+ return _path_to_module_name(candidates[0], package_root)
93
+
94
+
95
+ # Default locations
96
+ _DEFAULT_MODULES_ROOT = (Path(__file__).resolve().parents[1] / "modules").resolve()
97
+ _DEFAULT_CURATED_MODULE_NAME = "modacor.modules"
98
+
99
+
100
+ class ProcessStepRegistry:
101
+ """
102
+ Registry for ProcessStep subclasses.
103
+
104
+ Resolution strategy
105
+ -------------------
106
+ 1. Check internal registry cache.
107
+ 2. Try to resolve the class from the curated root module
108
+ (by default: ``modacor.modules``).
109
+ 3. If still not found, search the filesystem under
110
+ ``modules_root/**/<snake_case(name)>.py``, import it, and grab the class.
111
+ """
112
+
113
+ def __init__(
114
+ self,
115
+ modules_root: Path | None = None,
116
+ curated_module: str | None = _DEFAULT_CURATED_MODULE_NAME,
117
+ ) -> None:
118
+ """
119
+ Parameters
120
+ ----------
121
+ modules_root :
122
+ Root directory for filesystem discovery. Defaults to
123
+ the 'modules' directory alongside this file, i.e. ".../modacor/modules".
124
+ curated_module :
125
+ Dotted module path for the curated/explicit process steps module.
126
+ Defaults to "modacor.modules". Set to None to disable the curated
127
+ lookup step.
128
+ """
129
+ self._registry: Dict[str, Type[ProcessStep]] = {}
130
+ self._modules_root = (modules_root or _DEFAULT_MODULES_ROOT).resolve()
131
+
132
+ self._curated_module_name: str | None = curated_module
133
+ self._curated_module = None
134
+ if curated_module is not None:
135
+ try:
136
+ self._curated_module = importlib.import_module(curated_module)
137
+ except ModuleNotFoundError:
138
+ # If it doesn't exist, we silently disable the curated step.
139
+ self._curated_module = None
140
+
141
+ # ------------------------------------------------------------------ #
142
+ # Registration / lookup API
143
+ # ------------------------------------------------------------------ #
144
+
145
+ def register(self, cls: Type[ProcessStep], name: str | None = None) -> None:
146
+ """
147
+ Register a ProcessStep subclass.
148
+
149
+ Parameters
150
+ ----------
151
+ cls :
152
+ The ProcessStep subclass to register.
153
+ name :
154
+ Optional explicit name. If omitted, `cls.__name__` is used.
155
+ """
156
+ if not issubclass(cls, ProcessStep):
157
+ raise TypeError(f"Can only register ProcessStep subclasses, got {cls!r}.")
158
+ key = name or cls.__name__
159
+ self._registry[key] = cls
160
+
161
+ def get(self, name: str) -> Type[ProcessStep]:
162
+ """
163
+ Retrieve a ProcessStep subclass by name.
164
+
165
+ Resolution order
166
+ ----------------
167
+ 1. If `name` has been explicitly registered, return it.
168
+ 2. If the curated module (e.g. modacor.modules) exposes an attribute `name`,
169
+ validate it as a ProcessStep subclass, cache and return it.
170
+ 3. Else, convert `name` from PascalCase to snake_case and search for a
171
+ module named `<snake_case(name)>.py` under `modules_root/**`.
172
+ Import that module, fetch `name`, validate it, cache and return.
173
+ """
174
+ # 1. Already registered?
175
+ if name in self._registry:
176
+ return self._registry[name]
177
+
178
+ # 2. Curated module lookup (modacor.modules by default)
179
+ if self._curated_module is not None:
180
+ try:
181
+ cls = getattr(self._curated_module, name)
182
+ except AttributeError:
183
+ cls = None
184
+ else:
185
+ if not issubclass(cls, ProcessStep):
186
+ raise TypeError(
187
+ f"Object {name!r} in curated module "
188
+ f"{self._curated_module_name!r} is not a ProcessStep subclass."
189
+ )
190
+ self._registry[name] = cls
191
+ return cls
192
+
193
+ # 3. Filesystem-based discovery under modules_root/**/<snake_case(name)>.py
194
+ module_name = _pascal_to_snake(name)
195
+
196
+ try:
197
+ module_path = find_module(self._modules_root, module_name)
198
+ except (ModuleNotFoundError, RuntimeError) as exc:
199
+ raise KeyError(
200
+ f"ProcessStep {name!r} not found in registry, not exported from " # noqa: E713
201
+ f"{self._curated_module_name!r}, and no module file '{module_name}.py' "
202
+ f"could be resolved under {self._modules_root}."
203
+ ) from exc
204
+
205
+ module = importlib.import_module(module_path)
206
+ try:
207
+ cls = getattr(module, name)
208
+ except AttributeError as exc:
209
+ raise KeyError(f"ProcessStep class {name!r} not found in module {module_path!r}.") from exc # noqa: E713
210
+
211
+ if not issubclass(cls, ProcessStep):
212
+ raise TypeError(f"Object {name!r} in module {module_path!r} is not a ProcessStep subclass.")
213
+
214
+ self._registry[name] = cls
215
+ return cls
216
+
217
+ def __contains__(self, name: str) -> bool:
218
+ return name in self._registry
219
+
220
+
221
+ # Default registry used by the Pipeline:
222
+ # - curated lookups via modacor.modules
223
+ # - filesystem discovery under src/modacor/modules/**/snake_case.py
224
+ DEFAULT_PROCESS_STEP_REGISTRY = ProcessStepRegistry()
@@ -0,0 +1,27 @@
1
+ # SPDX-License-Identifier: BSD-3-Clause
2
+ # Copyright 2025 MoDaCor Authors
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without modification,
5
+ # are permitted provided that the following conditions are met:
6
+ # 1. Redistributions of source code must retain the above copyright notice, this
7
+ # list of conditions and the following disclaimer.
8
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
9
+ # this list of conditions and the following disclaimer in the documentation
10
+ # and/or other materials provided with the distribution.
11
+ # 3. Neither the name of the copyright holder nor the names of its contributors
12
+ # may be used to endorse or promote products derived from this software without
13
+ # specific prior written permission.
14
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
15
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18
+ # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19
+ # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20
+ # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21
+ # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
+
25
+ __license__ = "BSD-3-Clause"
26
+ __copyright__ = "Copyright 2025 MoDaCor Authors"
27
+ __status__ = "Alpha"