Skip to content

Commit 74465d1

Browse files
Merge branch 'main' into nxmx-no-vds
2 parents 1d748c5 + 45f50b2 commit 74465d1

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

AUTHORS

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ Robert Oeffner
2929
Takanori Nakane
3030
Tara Michels-Clark
3131
Viktor Bengtsson
32-
32+
Yash Karan

newsfragments/789.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``dials.import``: Reduce excessive memory usage when importing many (>100s) FormatNXMX files.

src/dxtbx/format/FormatNXmx.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
from __future__ import annotations
22

3+
import weakref
4+
35
import h5py
46
import nxmx
57

8+
import scitbx.array_family.flex as flex
9+
610
import dxtbx.nexus
711
from dxtbx.format.FormatNexus import FormatNexus
812

913

14+
class _MaskCache:
15+
"""A singleton to hold unique static_mask objects to avoid duplications"""
16+
17+
def __init__(self):
18+
self.local_mask_cache = weakref.WeakValueDictionary()
19+
20+
def _mask_hasher(self, mask: flex.bool) -> int:
21+
return hash(mask.as_numpy_array().tobytes())
22+
23+
def store_unique_and_get(
24+
self, mask_tuple: tuple[flex.bool, ...] | None
25+
) -> tuple[flex.bool, ...] | None:
26+
if mask_tuple is None:
27+
return None
28+
output = []
29+
for mask in mask_tuple:
30+
mask_hash = self._mask_hasher(mask)
31+
mask = self.local_mask_cache.setdefault(mask_hash, mask)
32+
output.append(mask)
33+
return tuple(output)
34+
35+
36+
mask_cache = _MaskCache()
37+
38+
1039
def detector_between_sample_and_source(detector, beam):
1140
"""Check if the detector is perpendicular to beam and
1241
upstream of the sample."""
@@ -86,7 +115,9 @@ def _start(self):
86115
self._detector_model = inverted_distance_detector(self._detector_model)
87116

88117
self._scan_model = dxtbx.nexus.get_dxtbx_scan(nxsample, nxdetector)
89-
self._static_mask = dxtbx.nexus.get_static_mask(nxdetector)
118+
self._static_mask = mask_cache.store_unique_and_get(
119+
dxtbx.nexus.get_static_mask(nxdetector)
120+
)
90121
self._bit_depth_readout = nxdetector.bit_depth_readout
91122

92123
if self._scan_model:

0 commit comments

Comments
 (0)