midas-tomo 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.
Files changed (50) hide show
  1. midas_tomo-0.1.0/.gitignore +11 -0
  2. midas_tomo-0.1.0/CMakeLists.txt +281 -0
  3. midas_tomo-0.1.0/LICENSE +53 -0
  4. midas_tomo-0.1.0/PKG-INFO +133 -0
  5. midas_tomo-0.1.0/README.md +107 -0
  6. midas_tomo-0.1.0/RELEASING.md +82 -0
  7. midas_tomo-0.1.0/c_src/FORK.txt +282 -0
  8. midas_tomo-0.1.0/c_src/midas_fft.h +116 -0
  9. midas_tomo-0.1.0/c_src/midas_fft_pocket.cpp +68 -0
  10. midas_tomo-0.1.0/c_src/tomo_cleanup.c +862 -0
  11. midas_tomo-0.1.0/c_src/tomo_gpu.cu +1468 -0
  12. midas_tomo-0.1.0/c_src/tomo_gpu.h +220 -0
  13. midas_tomo-0.1.0/c_src/tomo_gridrec.c +736 -0
  14. midas_tomo-0.1.0/c_src/tomo_heads.h +303 -0
  15. midas_tomo-0.1.0/c_src/tomo_init.c +1296 -0
  16. midas_tomo-0.1.0/c_src/tomo_utils.c +1634 -0
  17. midas_tomo-0.1.0/c_src/vendor/pocketfft_hdronly.h +3853 -0
  18. midas_tomo-0.1.0/midas_tomo/__init__.py +71 -0
  19. midas_tomo-0.1.0/midas_tomo/api.py +405 -0
  20. midas_tomo-0.1.0/midas_tomo/backend_c.py +263 -0
  21. midas_tomo-0.1.0/midas_tomo/backend_lib.py +406 -0
  22. midas_tomo-0.1.0/midas_tomo/center.py +144 -0
  23. midas_tomo-0.1.0/midas_tomo/cleanup.py +314 -0
  24. midas_tomo-0.1.0/midas_tomo/cli.py +184 -0
  25. midas_tomo-0.1.0/midas_tomo/config.py +381 -0
  26. midas_tomo-0.1.0/midas_tomo/hdf5.py +349 -0
  27. midas_tomo-0.1.0/notebooks/01_quickstart_hdf5.ipynb +567 -0
  28. midas_tomo-0.1.0/notebooks/02_from_tiff_stack.ipynb +559 -0
  29. midas_tomo-0.1.0/notebooks/03_cleanup_tuning.ipynb +756 -0
  30. midas_tomo-0.1.0/notebooks/04_shift_search.ipynb +519 -0
  31. midas_tomo-0.1.0/notebooks/05_from_sinograms.ipynb +644 -0
  32. midas_tomo-0.1.0/notebooks/06_troubleshooting.ipynb +588 -0
  33. midas_tomo-0.1.0/notebooks/README.md +47 -0
  34. midas_tomo-0.1.0/notebooks/_phantom.py +333 -0
  35. midas_tomo-0.1.0/pyproject.toml +65 -0
  36. midas_tomo-0.1.0/release.sh +199 -0
  37. midas_tomo-0.1.0/scripts/build_reference_binary.sh +67 -0
  38. midas_tomo-0.1.0/scripts/verify_fft_engines.py +96 -0
  39. midas_tomo-0.1.0/scripts/verify_gpu.py +168 -0
  40. midas_tomo-0.1.0/tests/__init__.py +0 -0
  41. midas_tomo-0.1.0/tests/phantom.py +86 -0
  42. midas_tomo-0.1.0/tests/test_center_cleanup.py +160 -0
  43. midas_tomo-0.1.0/tests/test_config.py +244 -0
  44. midas_tomo-0.1.0/tests/test_engine.py +149 -0
  45. midas_tomo-0.1.0/tests/test_fft_engines.py +158 -0
  46. midas_tomo-0.1.0/tests/test_gpu_routing.py +93 -0
  47. midas_tomo-0.1.0/tests/test_hdf5_cli.py +223 -0
  48. midas_tomo-0.1.0/tests/test_library.py +334 -0
  49. midas_tomo-0.1.0/tests/test_notebooks.py +99 -0
  50. midas_tomo-0.1.0/tests/test_parity_vs_fork.py +230 -0
@@ -0,0 +1,11 @@
1
+ build/
2
+ dist/
3
+ *.egg-info/
4
+ __pycache__/
5
+ *.pyc
6
+ midas_tomo/bin/
7
+ .pytest_cache/
8
+ # FFTW planner wisdom is written to CWD by the binary (see FORK.txt / §1.1 of
9
+ # the implementation plan). Test fixtures commit theirs explicitly under
10
+ # tests/data/; stray ones from ad-hoc runs are noise.
11
+ fftwf_wisdom_*d_*.txt
@@ -0,0 +1,281 @@
1
+ # packages/midas_tomo/CMakeLists.txt
2
+ #
3
+ # scikit-build-core hook: compiles the gridrec CT engine (c_src/tomo_*.c) into
4
+ # a standalone binary `MIDAS_TOMO`, and — when CUDA is available — the GPU
5
+ # variant `MIDAS_TOMO_GPU`. Both install under <site-packages>/midas_tomo/bin/.
6
+ # The Python wrapper (midas_tomo/backend_c.py) locates them via
7
+ # importlib.resources.
8
+ #
9
+ # EVERY dependency probe here is FAIL-SOFT: if a compiler, FFTW, HDF5 or
10
+ # OpenMP is missing we `return()` with a message(WARNING) rather than erroring,
11
+ # so `pip install midas-tomo` still succeeds. backend_c.available() then
12
+ # reports False at runtime and tells the user what to install.
13
+ #
14
+ # The sources under c_src/ are a byte-identical mirror of TOMO/src/ at the
15
+ # fork commit (see FORK.txt). Do not edit them to satisfy a build problem —
16
+ # solve it here instead, so the drift check stays meaningful.
17
+
18
+ cmake_minimum_required(VERSION 3.20)
19
+
20
+ # Declare no languages up front: `LANGUAGES C` makes CMake probe for a C
21
+ # compiler at this line and hard-error if none exists (e.g. Windows without
22
+ # MSVC Build Tools). That would fail the wheel build before we reach the
23
+ # graceful fallbacks below.
24
+ project(midas_tomo_c NONE)
25
+
26
+ include(CheckLanguage)
27
+ check_language(C)
28
+ if(NOT CMAKE_C_COMPILER)
29
+ message(WARNING
30
+ "midas-tomo: no C compiler found — MIDAS_TOMO will not be built. On macOS "
31
+ "install the Xcode command line tools; on Linux install gcc. The Python "
32
+ "package still installs; backend_c.available() will report False.")
33
+ return()
34
+ endif()
35
+ enable_language(C)
36
+
37
+ set(MIDAS_TOMO_C_SRC ${CMAKE_CURRENT_SOURCE_DIR}/c_src)
38
+
39
+ # ---------------------------------------------------------------- FFTW3f
40
+ # gridrec's CPU path uses the SINGLE-precision FFTW interface (fftwf_*), so we
41
+ # need the fftw3f library specifically, not fftw3. There is no FindFFTW module
42
+ # shipped with CMake, so: pkg-config first (how conda/homebrew/apt all ship
43
+ # it), then a plain find_library fallback for hand-built installs.
44
+ find_package(PkgConfig QUIET)
45
+ if(PkgConfig_FOUND)
46
+ pkg_check_modules(FFTW3F QUIET fftw3f)
47
+ endif()
48
+
49
+ if(FFTW3F_FOUND)
50
+ set(TOMO_FFTW_INCLUDE_DIRS ${FFTW3F_INCLUDE_DIRS})
51
+ set(TOMO_FFTW_LIBRARIES ${FFTW3F_LINK_LIBRARIES})
52
+ message(STATUS "midas-tomo: FFTW3f found via pkg-config (${FFTW3F_VERSION})")
53
+ else()
54
+ find_path(TOMO_FFTW_INCLUDE_DIRS fftw3.h)
55
+ find_library(TOMO_FFTW_LIBRARIES NAMES fftw3f libfftw3f)
56
+ if(TOMO_FFTW_INCLUDE_DIRS AND TOMO_FFTW_LIBRARIES)
57
+ message(STATUS "midas-tomo: FFTW3f found at ${TOMO_FFTW_LIBRARIES}")
58
+ set(TOMO_HAVE_FFTW ON)
59
+ else()
60
+ message(STATUS
61
+ "midas-tomo: FFTW3f not found — building with the vendored pocketfft "
62
+ "backend only. This is fully supported; pocketfft is BSD-licensed and "
63
+ "needs no external library. Install FFTW if you need bit-comparability "
64
+ "with historical runs.")
65
+ set(TOMO_FFTW_INCLUDE_DIRS "")
66
+ set(TOMO_FFTW_LIBRARIES "")
67
+ set(TOMO_HAVE_FFTW OFF)
68
+ endif()
69
+ endif()
70
+ if(NOT DEFINED TOMO_HAVE_FFTW)
71
+ set(TOMO_HAVE_FFTW ON)
72
+ endif()
73
+
74
+ # ---------------------------------------------------------------- HDF5
75
+ # OPTIONAL. midas-tomo reads HDF5 in Python (h5py, midas_tomo/hdf5.py) and
76
+ # hands the engine the staged binary layout, so the C's own readRawHDF5 path
77
+ # is unused by the package. Building without HDF5 costs nothing the package
78
+ # relies on; it only disables hand-written parameter files that set
79
+ # HDF5FileName, and that case gets an explicit runtime error pointing at the
80
+ # Python reader.
81
+ find_package(HDF5 QUIET COMPONENTS C)
82
+ if(HDF5_FOUND)
83
+ message(STATUS "midas-tomo: HDF5 ${HDF5_VERSION} found — HDF5FileName input enabled")
84
+ else()
85
+ message(STATUS
86
+ "midas-tomo: HDF5 not found — building without the C HDF5 reader. This is "
87
+ "fully supported: read HDF5 with midas_tomo.hdf5.read_exchange() instead.")
88
+ endif()
89
+
90
+ # ---------------------------------------------------------------- OpenMP
91
+ # gridrec parallelises over slices with OpenMP. Without it the binary would
92
+ # still be correct but single-threaded; the vendored tomo_init.c includes
93
+ # <omp.h> unconditionally, so it is a hard requirement for the build.
94
+ find_package(OpenMP QUIET COMPONENTS C)
95
+ if(NOT OpenMP_C_FOUND)
96
+ message(WARNING
97
+ "midas-tomo: OpenMP not found — MIDAS_TOMO will not be built. On macOS "
98
+ "install libomp via `brew install libomp`; on Linux libgomp usually comes "
99
+ "with gcc. The Python package still installs; backend_c.available() "
100
+ "reports False.")
101
+ return()
102
+ endif()
103
+
104
+ # ------------------------------------------------- generated midas_version.h
105
+ # The legacy MIDAS build derives this from git. For the pip package we bake in
106
+ # the package version, overridable at cmake-invoke time.
107
+ set(MIDAS_PIP_VERSION "${SKBUILD_PROJECT_VERSION}" CACHE STRING
108
+ "Version string baked into MIDAS_TOMO at build time")
109
+ if(NOT MIDAS_PIP_VERSION)
110
+ set(MIDAS_PIP_VERSION "pip-build")
111
+ endif()
112
+ file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/midas_version.h"
113
+ "#ifndef MIDAS_VERSION_H\n"
114
+ "#define MIDAS_VERSION_H\n"
115
+ "#define MIDAS_VERSION \"${MIDAS_PIP_VERSION}\"\n"
116
+ "#define MIDAS_GIT_HASH \"\"\n"
117
+ "#define MIDAS_GIT_DATE \"\"\n"
118
+ "#define MIDAS_VERSION_STRING \"midas-tomo v\" MIDAS_VERSION\n"
119
+ "#endif\n")
120
+
121
+ # pocketfft is a C++17 header, so the FFT shim is the one C++ file here.
122
+ # Enabling CXX is unconditional: pocketfft is what makes a build possible with
123
+ # no external FFT library, so it is always compiled in, and FFTW becomes the
124
+ # optional one.
125
+ enable_language(CXX)
126
+
127
+ set(TOMO_SOURCES
128
+ ${MIDAS_TOMO_C_SRC}/tomo_init.c
129
+ ${MIDAS_TOMO_C_SRC}/tomo_gridrec.c
130
+ ${MIDAS_TOMO_C_SRC}/tomo_utils.c
131
+ ${MIDAS_TOMO_C_SRC}/tomo_cleanup.c
132
+ ${MIDAS_TOMO_C_SRC}/midas_fft_pocket.cpp
133
+ )
134
+
135
+ # ------------------------------------------------- legacy-matching C flags
136
+ # These reproduce the root MIDAS CMakeLists.txt (`-fPIC -O3 -w -g`, C99) and
137
+ # are applied unconditionally, independent of CMAKE_BUILD_TYPE. Two reasons,
138
+ # both load-bearing:
139
+ #
140
+ # 1. CORRECTNESS. tomo_gridrec.c defines Cnvlvnt() as C99 `inline` with a
141
+ # matching `inline` declaration in tomo_heads.h and no `extern` definition
142
+ # anywhere. Under C99 semantics that is an *inline definition* only: no
143
+ # external symbol is emitted, so any call the optimiser does not inline is
144
+ # an undefined reference at link time. At -O0 GCC inlines nothing and the
145
+ # link fails outright. -O3 is therefore not a preference here, it is what
146
+ # makes the vendored source link at all. Fixing this properly means
147
+ # editing the C (adding an extern definition), which would break the
148
+ # byte-identical mirror -- so we match the legacy build instead and record
149
+ # the constraint here.
150
+ #
151
+ # 2. BIT-PARITY. Optimisation level changes instruction selection, including
152
+ # FMA contraction and vectorisation of the gridrec inner loops, so it
153
+ # changes the low-order bits of the reconstruction. Reproducing the 2023
154
+ # reference output requires the same flags, not merely a working binary.
155
+ # Anything that alters this list invalidates the golden-file test.
156
+ if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang|AppleClang|IntelLLVM")
157
+ set(TOMO_LEGACY_C_FLAGS -fPIC -O3 -w -g)
158
+ else()
159
+ set(TOMO_LEGACY_C_FLAGS "")
160
+ message(WARNING
161
+ "midas-tomo: unrecognised C compiler '${CMAKE_C_COMPILER_ID}' — cannot "
162
+ "apply the legacy -O3 flags. The build may fail to link (Cnvlvnt) and "
163
+ "will not be bit-comparable to the reference output.")
164
+ endif()
165
+
166
+ # ---------------------------------------------------------------- CPU target
167
+ add_executable(MIDAS_TOMO ${TOMO_SOURCES})
168
+ target_include_directories(MIDAS_TOMO PRIVATE
169
+ ${MIDAS_TOMO_C_SRC}
170
+ ${CMAKE_CURRENT_BINARY_DIR}
171
+ ${TOMO_FFTW_INCLUDE_DIRS}
172
+ )
173
+ # PI=M_PI matches the legacy TOMO/CMakeLists.txt definition — the sources rely
174
+ # on PI being defined by the build, not by math.h.
175
+ target_compile_definitions(MIDAS_TOMO PRIVATE PI=M_PI)
176
+ target_compile_options(MIDAS_TOMO PRIVATE ${TOMO_LEGACY_C_FLAGS})
177
+ target_link_libraries(MIDAS_TOMO PRIVATE
178
+ ${TOMO_FFTW_LIBRARIES}
179
+ OpenMP::OpenMP_C
180
+ m
181
+ )
182
+ if(TOMO_HAVE_FFTW)
183
+ target_compile_definitions(MIDAS_TOMO PRIVATE MIDAS_TOMO_HAVE_FFTW)
184
+ endif()
185
+ set_target_properties(MIDAS_TOMO PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON)
186
+ if(HDF5_FOUND)
187
+ target_include_directories(MIDAS_TOMO PRIVATE ${HDF5_INCLUDE_DIRS})
188
+ target_link_libraries(MIDAS_TOMO PRIVATE ${HDF5_C_LIBRARIES})
189
+ target_compile_definitions(MIDAS_TOMO PRIVATE MIDAS_TOMO_HAVE_HDF5)
190
+ endif()
191
+ set_target_properties(MIDAS_TOMO PROPERTIES C_STANDARD 99 C_STANDARD_REQUIRED ON)
192
+
193
+ install(TARGETS MIDAS_TOMO RUNTIME DESTINATION midas_tomo/bin)
194
+
195
+ # ------------------------------------------------------------ shared library
196
+ # The same sources built as a .so/.dylib so Python can call midas_tomo_run()
197
+ # in-process via ctypes instead of spawning the binary and round-tripping
198
+ # through disk. Built from the identical source list and the identical flags,
199
+ # so the two agree by construction; tests/test_library.py asserts that
200
+ # bitwise rather than trusting it.
201
+ #
202
+ # The CLI binary above is deliberately kept: dev/build_reference_binary.sh
203
+ # compares against it, which is what keeps the parity gate against a pre-fork
204
+ # build runnable.
205
+ add_library(midastomo SHARED ${TOMO_SOURCES})
206
+ target_include_directories(midastomo PRIVATE
207
+ ${MIDAS_TOMO_C_SRC}
208
+ ${CMAKE_CURRENT_BINARY_DIR}
209
+ ${TOMO_FFTW_INCLUDE_DIRS}
210
+ )
211
+ # MIDAS_TOMO_LIBRARY_BUILD compiles out main(): a shared object with an
212
+ # entry-point symbol is legal but confuses some loaders, and there is no
213
+ # reason to carry it.
214
+ target_compile_definitions(midastomo PRIVATE PI=M_PI MIDAS_TOMO_LIBRARY_BUILD)
215
+ target_compile_options(midastomo PRIVATE ${TOMO_LEGACY_C_FLAGS})
216
+ target_link_libraries(midastomo PRIVATE
217
+ ${TOMO_FFTW_LIBRARIES}
218
+ OpenMP::OpenMP_C
219
+ m
220
+ )
221
+ set_target_properties(midastomo PROPERTIES
222
+ C_STANDARD 99 C_STANDARD_REQUIRED ON
223
+ POSITION_INDEPENDENT_CODE ON
224
+ )
225
+ if(TOMO_HAVE_FFTW)
226
+ target_compile_definitions(midastomo PRIVATE MIDAS_TOMO_HAVE_FFTW)
227
+ endif()
228
+ set_target_properties(midastomo PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON)
229
+ if(HDF5_FOUND)
230
+ target_include_directories(midastomo PRIVATE ${HDF5_INCLUDE_DIRS})
231
+ target_link_libraries(midastomo PRIVATE ${HDF5_C_LIBRARIES})
232
+ target_compile_definitions(midastomo PRIVATE MIDAS_TOMO_HAVE_HDF5)
233
+ endif()
234
+
235
+ install(TARGETS midastomo LIBRARY DESTINATION midas_tomo/bin
236
+ RUNTIME DESTINATION midas_tomo/bin)
237
+
238
+ # ---------------------------------------------------------------- GPU target
239
+ # Optional in every sense: no CUDA toolkit → skip with a status message, never
240
+ # an error. The Mac has no CUDA, so this path is exercised only on the
241
+ # beamline GPU hosts.
242
+ check_language(CUDA)
243
+ if(CMAKE_CUDA_COMPILER)
244
+ enable_language(CUDA)
245
+ find_package(CUDAToolkit QUIET)
246
+ if(CUDAToolkit_FOUND)
247
+ add_executable(MIDAS_TOMO_GPU ${TOMO_SOURCES} ${MIDAS_TOMO_C_SRC}/tomo_gpu.cu)
248
+ target_include_directories(MIDAS_TOMO_GPU PRIVATE
249
+ ${MIDAS_TOMO_C_SRC}
250
+ ${CMAKE_CURRENT_BINARY_DIR}
251
+ ${TOMO_FFTW_INCLUDE_DIRS}
252
+ )
253
+ target_compile_definitions(MIDAS_TOMO_GPU PRIVATE PI=M_PI ENABLE_CUDA)
254
+ # Same legacy flags for the C sources; nvcc rejects them on .cu files, so
255
+ # gate on COMPILE_LANGUAGE.
256
+ target_compile_options(MIDAS_TOMO_GPU PRIVATE
257
+ $<$<COMPILE_LANGUAGE:C>:${TOMO_LEGACY_C_FLAGS}>)
258
+ if(HDF5_FOUND)
259
+ target_include_directories(MIDAS_TOMO_GPU PRIVATE ${HDF5_INCLUDE_DIRS})
260
+ target_link_libraries(MIDAS_TOMO_GPU PRIVATE ${HDF5_C_LIBRARIES})
261
+ target_compile_definitions(MIDAS_TOMO_GPU PRIVATE MIDAS_TOMO_HAVE_HDF5)
262
+ endif()
263
+ target_link_libraries(MIDAS_TOMO_GPU PRIVATE
264
+ ${TOMO_FFTW_LIBRARIES}
265
+ CUDA::cufft
266
+ CUDA::cudart
267
+ OpenMP::OpenMP_C
268
+ m
269
+ )
270
+ # nvcc does not pass -fopenmp to the host compiler on its own.
271
+ target_compile_options(MIDAS_TOMO_GPU PRIVATE
272
+ $<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=-fopenmp>)
273
+ set_target_properties(MIDAS_TOMO_GPU PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
274
+ install(TARGETS MIDAS_TOMO_GPU RUNTIME DESTINATION midas_tomo/bin)
275
+ message(STATUS "midas-tomo: CUDA found — MIDAS_TOMO_GPU enabled")
276
+ else()
277
+ message(STATUS "midas-tomo: nvcc present but CUDAToolkit not found — GPU target skipped")
278
+ endif()
279
+ else()
280
+ message(STATUS "midas-tomo: no CUDA compiler — GPU target skipped (CPU build is unaffected)")
281
+ endif()
@@ -0,0 +1,53 @@
1
+ Copyright (c) 2012, UChicago Argonne, LLC
2
+
3
+ All Rights Reserved
4
+
5
+ MIDAS Microstructural Imaging using Diffraction Analysis Software
6
+
7
+ Materials Physics and Engineering
8
+ Computational X-ray Science
9
+ Advanced Photon Source
10
+ Argonne National Laboratory
11
+
12
+ Contributing Authors:
13
+ Hemant Sharma (hsharma@anl.gov)
14
+
15
+ OPEN SOURCE LICENSE
16
+
17
+ Redistribution and use in source and binary forms, with or without
18
+ modification, are permitted provided that the following conditions are met:
19
+
20
+ 1. Redistributions of source code must retain the above copyright notice,
21
+ this list of conditions and the following disclaimer. Software changes,
22
+ modifications, or derivative works, should be noted with comments and
23
+ the author and organization's name.
24
+
25
+ 2. Redistributions in binary form must reproduce the above copyright notice,
26
+ this list of conditions and the following disclaimer in the documentation
27
+ and/or other materials provided with the distribution.
28
+
29
+ 3. Neither the names of UChicago Argonne, LLC or the Department of Energy
30
+ nor the names of its contributors may be used to endorse or promote
31
+ products derived from this software without specific prior written
32
+ permission.
33
+
34
+ 4. The software and the end-user documentation included with the
35
+ redistribution, if any, must include the following acknowledgment:
36
+
37
+ "This product includes software produced by UChicago Argonne, LLC
38
+ under Contract No. DE-AC02-06CH11357 with the Department of Energy."
39
+
40
+ ****************************************************************************
41
+
42
+ DISCLAIMER
43
+
44
+ THE SOFTWARE IS SUPPLIED "AS IS" WITHOUT WARRANTY OF ANY KIND.
45
+
46
+ Neither the United States GOVERNMENT, nor the United States Department
47
+ of Energy, NOR UChicago Argonne, LLC, nor any of their employees, makes
48
+ any warranty, express or implied, or assumes any legal liability or
49
+ responsibility for the accuracy, completeness, or usefulness of any
50
+ information, data, apparatus, product, or process disclosed, or
51
+ represents that its use would not infringe privately owned rights.
52
+
53
+ ****************************************************************************
@@ -0,0 +1,133 @@
1
+ Metadata-Version: 2.4
2
+ Name: midas-tomo
3
+ Version: 0.1.0
4
+ Summary: Gridrec filtered back-projection CT reconstruction (CPU + CUDA) with a NumPy API
5
+ Keywords: MIDAS,tomography,CT,gridrec,reconstruction,filtered-back-projection,synchrotron,XRD-CT
6
+ Author-Email: Hemant Sharma <hsharma@anl.gov>
7
+ License-Expression: BSD-3-Clause
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Intended Audience :: Science/Research
10
+ Classifier: Programming Language :: C
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Operating System :: POSIX :: Linux
13
+ Classifier: Operating System :: MacOS
14
+ Classifier: Topic :: Scientific/Engineering :: Physics
15
+ Project-URL: Homepage, https://github.com/marinerhemant/MIDAS
16
+ Project-URL: Documentation, https://github.com/marinerhemant/MIDAS/tree/master/packages/midas_tomo
17
+ Project-URL: Issues, https://github.com/marinerhemant/MIDAS/issues
18
+ Requires-Python: >=3.9
19
+ Requires-Dist: numpy>=1.22
20
+ Provides-Extra: dev
21
+ Requires-Dist: pytest>=7.0; extra == "dev"
22
+ Requires-Dist: pytest-cov; extra == "dev"
23
+ Provides-Extra: hdf5
24
+ Requires-Dist: h5py>=3.0; extra == "hdf5"
25
+ Description-Content-Type: text/markdown
26
+
27
+ # midas-tomo
28
+
29
+ Gridrec filtered back-projection CT reconstruction, with a NumPy API.
30
+
31
+ Gridrec is a Fourier-domain FBP that uses prolate spheroidal wave function
32
+ (PSWF) interpolation onto the polar grid. This package wraps the MIDAS
33
+ implementation: a `MIDAS_TOMO` binary compiled at install time from the C in
34
+ `c_src/`, plus a CUDA variant (`MIDAS_TOMO_GPU`) where a CUDA toolkit is
35
+ present.
36
+
37
+ ```python
38
+ from midas_tomo import run_tomo
39
+ recon = run_tomo(data, dark, whites, "/path/to/work", thetas, shifts=1.0)
40
+ ```
41
+
42
+ ## Installing
43
+
44
+ ```bash
45
+ pip install midas-tomo
46
+ ```
47
+
48
+ The binary needs a C/C++ compiler and **OpenMP**. That is all — the FFT comes
49
+ from **pocketfft**, which is BSD-3-Clause and vendored in `c_src/vendor/`, so
50
+ no external FFT library is required.
51
+
52
+ ```bash
53
+ brew install libomp # macOS only; Linux gcc has libgomp
54
+ ```
55
+
56
+ **pocketfft is the default engine.** FFTW3f is optional and selectable with
57
+ `--fft-engine=fftw`; use it to reproduce historical runs bit-for-bit. It is
58
+ not shipped, because FFTW is GPL-2.0-or-later and this package is BSD-3-Clause
59
+ — a wheel containing FFTW would have to be distributed under the GPL.
60
+
61
+ Measured on one machine (128×180 phantom): pocketfft agrees with FFTW to
62
+ 2.9e-07 relative, is **reproducible run-to-run where FFTW is not**, and ran
63
+ **0.86× the time** of FFTW — slightly faster, not slower.
64
+
65
+ **HDF5 is not required.** This package reads HDF5 in Python with `h5py` and hands
66
+ the engine a staged binary, so the C never needs an HDF5 reader — and h5py copes
67
+ with more layouts, compression filters and chunkings than the C one did. Use
68
+ `stage_exchange_to_binary()` for scans too large to hold in memory; it streams
69
+ the conversion a chunk of frames at a time. If HDF5 *is* present at build time the
70
+ C reader is compiled in as well, for hand-written parameter files that set
71
+ `HDF5FileName`.
72
+
73
+ then reinstall. To check what you have:
74
+
75
+ ```python
76
+ import midas_tomo
77
+ midas_tomo.backend_c.available() # CPU engine present?
78
+ midas_tomo.backend_c.available(gpu=True) # CUDA engine present?
79
+ print(midas_tomo.backend_c.why_unavailable())
80
+ ```
81
+
82
+ There are no binary wheels. The sdist compiles from source at install time,
83
+ which is deliberate: FFTW is GPL-2.0-or-later, so a wheel with FFTW linked
84
+ into it would have to be distributed under the GPL, while this package is
85
+ BSD-3-Clause. Building against *your* FFTW keeps that boundary clean.
86
+
87
+ ## Relationship to `TOMO/`
88
+
89
+ `c_src/` is a byte-identical mirror of `TOMO/src/` at commit `0a426739`, and
90
+ is **canonical** from that point on — future fixes land here, and `TOMO/`
91
+ stays frozen. See `c_src/FORK.txt`. A CI job diffs the two and warns on any
92
+ divergence, so the mirror is kept clean on purpose: an unexplained diff is a
93
+ signal, not noise.
94
+
95
+ Two things recorded there are worth knowing before you touch the build:
96
+
97
+ - The binary is compiled `-fPIC -O3 -w -g` **unconditionally**, not because
98
+ of a build type. `Cnvlvnt()` is a C99 `inline` with no extern definition, so
99
+ the link only succeeds when the optimiser inlines every call — at `-O0` it
100
+ fails outright.
101
+ - Those flags are part of the bit-parity pin. Optimisation level changes
102
+ floating-point instruction selection in the gridrec inner loops, so changing
103
+ them changes the reconstruction's low-order bits.
104
+
105
+ ## Reproducibility
106
+
107
+ By default the CPU engine plans its FFTs with `FFTW_MEASURE` and caches the
108
+ result in `fftwf_wisdom_{1,2}d_<N>.txt` **in the current working directory**.
109
+ `FFTW_MEASURE` chooses a plan by *timing* candidates, so the choice depends on
110
+ the machine and on transient load.
111
+
112
+ Measured on one machine, three trials: **two identical runs in two fresh
113
+ directories disagreed in 2 of 3 trials** — at the ~1e-7 relative level, i.e. a
114
+ few float32 ulps. So the default path is not reproducible run to run, let alone
115
+ across machines. Reconstructions are unaffected scientifically; byte-comparisons
116
+ are not.
117
+
118
+ For reproducibility use the deterministic planner (`FFTW_ESTIMATE`, no timing,
119
+ no wisdom side-effect):
120
+
121
+ ```python
122
+ run_tomo(..., deterministic=True)
123
+ ```
124
+
125
+ Be precise about what that buys. Measured: repeated runs of the *same* backend
126
+ are then bitwise identical. It does **not** make different contexts agree — the
127
+ in-process library and the CLI binary still differ by ~3e-7 relative, and
128
+ consistently so. Reproducibility is scoped to one build and one process
129
+ context, not absolute.
130
+
131
+ ## License
132
+
133
+ BSD-3-Clause. See `LICENSE`.
@@ -0,0 +1,107 @@
1
+ # midas-tomo
2
+
3
+ Gridrec filtered back-projection CT reconstruction, with a NumPy API.
4
+
5
+ Gridrec is a Fourier-domain FBP that uses prolate spheroidal wave function
6
+ (PSWF) interpolation onto the polar grid. This package wraps the MIDAS
7
+ implementation: a `MIDAS_TOMO` binary compiled at install time from the C in
8
+ `c_src/`, plus a CUDA variant (`MIDAS_TOMO_GPU`) where a CUDA toolkit is
9
+ present.
10
+
11
+ ```python
12
+ from midas_tomo import run_tomo
13
+ recon = run_tomo(data, dark, whites, "/path/to/work", thetas, shifts=1.0)
14
+ ```
15
+
16
+ ## Installing
17
+
18
+ ```bash
19
+ pip install midas-tomo
20
+ ```
21
+
22
+ The binary needs a C/C++ compiler and **OpenMP**. That is all — the FFT comes
23
+ from **pocketfft**, which is BSD-3-Clause and vendored in `c_src/vendor/`, so
24
+ no external FFT library is required.
25
+
26
+ ```bash
27
+ brew install libomp # macOS only; Linux gcc has libgomp
28
+ ```
29
+
30
+ **pocketfft is the default engine.** FFTW3f is optional and selectable with
31
+ `--fft-engine=fftw`; use it to reproduce historical runs bit-for-bit. It is
32
+ not shipped, because FFTW is GPL-2.0-or-later and this package is BSD-3-Clause
33
+ — a wheel containing FFTW would have to be distributed under the GPL.
34
+
35
+ Measured on one machine (128×180 phantom): pocketfft agrees with FFTW to
36
+ 2.9e-07 relative, is **reproducible run-to-run where FFTW is not**, and ran
37
+ **0.86× the time** of FFTW — slightly faster, not slower.
38
+
39
+ **HDF5 is not required.** This package reads HDF5 in Python with `h5py` and hands
40
+ the engine a staged binary, so the C never needs an HDF5 reader — and h5py copes
41
+ with more layouts, compression filters and chunkings than the C one did. Use
42
+ `stage_exchange_to_binary()` for scans too large to hold in memory; it streams
43
+ the conversion a chunk of frames at a time. If HDF5 *is* present at build time the
44
+ C reader is compiled in as well, for hand-written parameter files that set
45
+ `HDF5FileName`.
46
+
47
+ then reinstall. To check what you have:
48
+
49
+ ```python
50
+ import midas_tomo
51
+ midas_tomo.backend_c.available() # CPU engine present?
52
+ midas_tomo.backend_c.available(gpu=True) # CUDA engine present?
53
+ print(midas_tomo.backend_c.why_unavailable())
54
+ ```
55
+
56
+ There are no binary wheels. The sdist compiles from source at install time,
57
+ which is deliberate: FFTW is GPL-2.0-or-later, so a wheel with FFTW linked
58
+ into it would have to be distributed under the GPL, while this package is
59
+ BSD-3-Clause. Building against *your* FFTW keeps that boundary clean.
60
+
61
+ ## Relationship to `TOMO/`
62
+
63
+ `c_src/` is a byte-identical mirror of `TOMO/src/` at commit `0a426739`, and
64
+ is **canonical** from that point on — future fixes land here, and `TOMO/`
65
+ stays frozen. See `c_src/FORK.txt`. A CI job diffs the two and warns on any
66
+ divergence, so the mirror is kept clean on purpose: an unexplained diff is a
67
+ signal, not noise.
68
+
69
+ Two things recorded there are worth knowing before you touch the build:
70
+
71
+ - The binary is compiled `-fPIC -O3 -w -g` **unconditionally**, not because
72
+ of a build type. `Cnvlvnt()` is a C99 `inline` with no extern definition, so
73
+ the link only succeeds when the optimiser inlines every call — at `-O0` it
74
+ fails outright.
75
+ - Those flags are part of the bit-parity pin. Optimisation level changes
76
+ floating-point instruction selection in the gridrec inner loops, so changing
77
+ them changes the reconstruction's low-order bits.
78
+
79
+ ## Reproducibility
80
+
81
+ By default the CPU engine plans its FFTs with `FFTW_MEASURE` and caches the
82
+ result in `fftwf_wisdom_{1,2}d_<N>.txt` **in the current working directory**.
83
+ `FFTW_MEASURE` chooses a plan by *timing* candidates, so the choice depends on
84
+ the machine and on transient load.
85
+
86
+ Measured on one machine, three trials: **two identical runs in two fresh
87
+ directories disagreed in 2 of 3 trials** — at the ~1e-7 relative level, i.e. a
88
+ few float32 ulps. So the default path is not reproducible run to run, let alone
89
+ across machines. Reconstructions are unaffected scientifically; byte-comparisons
90
+ are not.
91
+
92
+ For reproducibility use the deterministic planner (`FFTW_ESTIMATE`, no timing,
93
+ no wisdom side-effect):
94
+
95
+ ```python
96
+ run_tomo(..., deterministic=True)
97
+ ```
98
+
99
+ Be precise about what that buys. Measured: repeated runs of the *same* backend
100
+ are then bitwise identical. It does **not** make different contexts agree — the
101
+ in-process library and the CLI binary still differ by ~3e-7 relative, and
102
+ consistently so. Reproducibility is scoped to one build and one process
103
+ context, not absolute.
104
+
105
+ ## License
106
+
107
+ BSD-3-Clause. See `LICENSE`.
@@ -0,0 +1,82 @@
1
+ # Releasing midas-tomo
2
+
3
+ ```bash
4
+ cd packages/midas_tomo
5
+ ./release.sh 0.1.0 --dry-run # build + test, no commit or tag
6
+ ./release.sh 0.1.0 # + commit and tag locally
7
+ ./release.sh 0.1.0 --publish # + push, GitHub release, CI publishes to PyPI
8
+ ```
9
+
10
+ The tag is `midas-tomo-v<version>`; `.github/workflows/python-packages.yml`
11
+ maps it to this package and publishes to PyPI via trusted publishing.
12
+
13
+ ## Release from a machine that can build the engine
14
+
15
+ This is the one thing about this package that differs from the pure-Python
16
+ ones, and it is easy to get wrong.
17
+
18
+ The engine tests **skip** when `MIDAS_TOMO` is not built, so `pytest` goes
19
+ green on a laptop without FFTW while never once exercising the C. Expected
20
+ counts:
21
+
22
+ | environment | result |
23
+ |---|---|
24
+ | engine + `MIDAS_TOMO_REFERENCE_BIN` set | **112 passed** |
25
+ | no engine at all (e.g. a Mac with no FFTW) | 89 passed, 3 skipped |
26
+
27
+ `release.sh` passes `-rs` so the skip reasons are printed. If you see the
28
+ 89-passed line, you tested the wrapper and nothing else — release from a host
29
+ with FFTW, or run the suite there first.
30
+
31
+ ## The parity gate
32
+
33
+ `tests/test_parity_vs_fork.py` is the evidence that this package still
34
+ computes what `TOMO/` computed. It needs a reference binary built from the
35
+ pre-fork source:
36
+
37
+ ```bash
38
+ ./scripts/build_reference_binary.sh /path/to/a/MIDAS/checkout
39
+ export MIDAS_TOMO_REFERENCE_BIN=$PWD/dev/refbin/MIDAS_TOMO_REF
40
+ ```
41
+
42
+ Set `FFTW_PREFIX` if FFTW is not on the default search path. Run this before
43
+ any release that touched `c_src/`.
44
+
45
+ **Both binaries must load the same FFTW build.** `build_reference_binary.sh`
46
+ links with a bare `-lfftw3f` and embeds no RPATH, so the reference falls back
47
+ to the system `/lib64/libfftw3f.so.3`, while the CMake-built package carries a
48
+ `DT_RPATH` to whatever FFTW it was configured against. Two different FFTW
49
+ builds disagree by ~2 float32 ULP (2.8e-07 relative), and the parity test then
50
+ fails pointing at `c_src/` and `FORK.txt` — none of which is wrong. Export
51
+ `LD_LIBRARY_PATH` to the FFTW the package was built against:
52
+
53
+ ```bash
54
+ export LD_LIBRARY_PATH=$FFTW_PREFIX/lib:$LD_LIBRARY_PATH
55
+ ```
56
+
57
+ `_require_same_fftw()` in the test now compares the two resolved libraries by
58
+ sha256 and fails with that diagnosis instead, so this should not cost anyone an
59
+ hour twice. It compares content rather than path deliberately: on the beamline
60
+ filesystem the same FFTW is reachable as both `/home/beams/...` and
61
+ `/home/beams12/...`, and a path comparison red-flags a healthy environment.
62
+
63
+ Measured on chiltepin with the environment right: **130 passed**, parity
64
+ bitwise.
65
+
66
+ ## Reproducibility caveat when comparing releases
67
+
68
+ Do not diff reconstructions byte-for-byte across builds or machines and treat
69
+ a difference as a regression. Measured: the default `FFTW_MEASURE` planner
70
+ picks by timing, so two identical runs disagreed in 2 of 3 trials at ~1e-7
71
+ relative. `deterministic=True` fixes that *within* one backend and one build,
72
+ not across them. See the Reproducibility section of `README.md`.
73
+
74
+ ## Before a release that touched `c_src/`
75
+
76
+ - [ ] `scripts/build_reference_binary.sh` + parity test pass
77
+ - [ ] builds **with and without** HDF5 (it is optional; the Python reader is
78
+ the supported path)
79
+ - [ ] any new divergence from the fork is recorded in `c_src/FORK.txt` with
80
+ its reason
81
+ - [ ] `tests/test_library.py` passes — it covers the in-process path, where
82
+ an error the CLI merely printed becomes a silent wrong answer