Skip to content

Commit ff33397

Browse files
authored
Merge pull request #2751 from kif/2750_Sensor_config_copy
Ensure SensorConfig can be copied
2 parents 8014088 + 28821f1 commit ff33397

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/pyFAI/detectors/_common.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Project: Azimuthal integration
55
# https://github.com/silx-kit/pyFAI
66
#
7-
# Copyright (C) 2014-2025 European Synchrotron Radiation Facility, Grenoble, France
7+
# Copyright (C) 2014-2026 European Synchrotron Radiation Facility, Grenoble, France
88
#
99
# Principal author: Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
1010
#
@@ -33,7 +33,7 @@
3333
__contact__ = "Jerome.Kieffer@ESRF.eu"
3434
__license__ = "MIT"
3535
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
36-
__date__ = "21/11/2025"
36+
__date__ = "05/01/2026"
3737
__status__ = "stable"
3838

3939
import logging
@@ -44,6 +44,8 @@
4444
import json
4545
from typing import Dict, Any, Union
4646
import inspect
47+
import copy
48+
import types
4749

4850
from .orientation import Orientation
4951
from .sensors import SensorConfig
@@ -317,12 +319,12 @@ def __deepcopy__(self, memo=None):
317319
new.__setattr__(key, old)
318320
for key in self._MUTABLE_ATTRS:
319321
value = self.__getattribute__(key)
320-
if (value is None) or (value is False):
322+
if type(value) in (types.NoneType, bool, int, float):
321323
new_value = value
322324
elif "copy" in dir(value):
323325
new_value = value.copy()
324-
else:
325-
new_value = 1 * value
326+
else: # poor's men copy
327+
new_value = value * 1
326328
memo[id(value)] = new_value
327329
new.__setattr__(key, new_value)
328330
if self._splinefile:
@@ -1492,7 +1494,6 @@ def __copy__(self):
14921494
return cloned
14931495

14941496
def __deepcopy__(self, memo=None):
1495-
import copy
14961497
cloned = self.__class__()
14971498
if memo is not None:
14981499
memo[id(self)] = cloned

src/pyFAI/detectors/sensors.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Project: Azimuthal integration
55
# https://github.com/silx-kit/pyFAI
66
#
7-
# Copyright (C) 2025-2025 European Synchrotron Radiation Facility, Grenoble, France
7+
# Copyright (C) 2025-2026 European Synchrotron Radiation Facility, Grenoble, France
88
#
99
# Principal author: Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
1010
#
@@ -37,7 +37,7 @@
3737
__contact__ = "Jerome.Kieffer@ESRF.eu"
3838
__license__ = "MIT"
3939
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
40-
__date__ = "03/10/2025"
40+
__date__ = "05/01/2026"
4141
__status__ = "stable"
4242

4343
import os
@@ -156,7 +156,7 @@ def absorbance(self, energy:float, length: float, unit:str="m") -> float:
156156
ALL_MATERIALS["CdTe"] = CdTe_MATERIAL = SensorMaterial("CdTe", density=5.85)
157157
ALL_MATERIALS["GaAs"] = GaAs_MATERIAL = SensorMaterial("GaAs", density=5.3176)
158158
ALL_MATERIALS["Gd2O2S"] = Gd2O2S_MATERIAL = SensorMaterial("Gd2O2S", density=7.32)
159-
ALL_MATERIALS["BaFBr0.85I0.15"] = BaFBr085I015_MATERIAL = SensorMaterial("BaFBr0.85I0.15", density=3.18)
159+
ALL_MATERIALS["BaFBr0.85I0.15"] = BaFBr085I015_MATERIAL = SensorMaterial("BaFBr0.85I0.15", density=3.18)
160160
ALL_MATERIALS["Se"] = Se_MATERIAL = SensorMaterial("Se", density=4.26)
161161

162162

@@ -174,6 +174,11 @@ def __str__(self):
174174
thick = to_eng(self.thickness, space="")+"m" if self.thickness else "\N{INFINITY}"
175175
return f"{name},{thick}"
176176

177+
def copy(self):
178+
"helper function for the `detector` module"
179+
return self.__class__(self.material, # expected to be immutable
180+
self.thickness)
181+
177182
def as_dict(self):
178183
"""Like asdict, but with some more features:
179184
"""

src/pyFAI/test/test_bug_regression.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Project: Azimuthal integration
55
# https://github.com/silx-kit/pyFAI
66
#
7-
# Copyright (C) 2015-2025 European Synchrotron Radiation Facility, Grenoble, France
7+
# Copyright (C) 2015-2026 European Synchrotron Radiation Facility, Grenoble, France
88
#
99
# Principal author: Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
1010
#
@@ -36,7 +36,7 @@
3636
__contact__ = "Jerome.Kieffer@esrf.fr"
3737
__license__ = "MIT"
3838
__copyright__ = "2015-2025 European Synchrotron Radiation Facility, Grenoble, France"
39-
__date__ = "17/12/2025"
39+
__date__ = "05/01/2026"
4040

4141
import sys
4242
import os
@@ -726,6 +726,12 @@ def test_bug_2736(self):
726726
self.assertEqual((pos[..., 1]< 0).sum(), 1, "1 pixels below pi azim angle")
727727
self.assertEqual((pos[..., 1]> 2*pi).sum(), 3, "3 pixels above pi azim angle")
728728

729+
def test_bug_2750(self):
730+
"""Checks a detector with a sensor config can be deep-copied"""
731+
ai = load({"detector":"Pilatus100k","detector_config":{"sensor":{"material":"Si","thickness":450e-6}}})
732+
cpy = copy.deepcopy(ai)
733+
self.assertEqual(ai.detector, cpy.detector)
734+
self.assertEqual(ai.detector.sensor, cpy.detector.sensor)
729735

730736

731737
class TestBug1703(unittest.TestCase):

0 commit comments

Comments
 (0)