Skip to content

Commit 9bdee71

Browse files
committed
Minor bug fix. obj doesn't necessarily have attribute original.
1 parent 0a235fc commit 9bdee71

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

imsim/lsst_image.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import hashlib
23
import logging
34
import galsim
45
from galsim.config import RegisterImageType, GetAllParams, GetSky, AddNoise
@@ -293,11 +294,15 @@ def addNoise(self, image, config, base, image_num, obj_num, current_var, logger)
293294
camera = get_camera(self.camera_name)
294295
det_name = base['det_name']
295296
serial_number = camera[det_name].getSerial()
297+
# Note: the regular Python hash function is non-derterministic, which is not good.
298+
# Instead we use hashlib.sha256, which is deterministic and convert that to an integer.
299+
# https://stackoverflow.com/questions/27954892/deterministic-hashing-in-python-3
300+
seed = int(hashlib.sha256(serial_number.encode('UTF-8')).hexdigest(), 16) & 0xFFFFFFFF
296301
# Only apply fringing to e2v sensors.
297302
if serial_number[:3] == 'E2V':
298303
ccd_fringing = CCD_Fringing(true_center=image.wcs.toWorld(image.true_center),
299304
boresight=self.boresight,
300-
seed=hash(serial_number), spatial_vary=True)
305+
seed=seed, spatial_vary=True)
301306
ny, nx = sky.array.shape
302307
xarr, yarr = np.meshgrid(range(nx), range(ny))
303308
logger.info("Apply fringing")

imsim/stamp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def _getGoodPhotImageSize1(self, obj, keep_sb_level, pixel_scale):
293293
else:
294294
obj_list.append(item)
295295
obj = galsim.Add(obj_list)
296-
elif isinstance(obj.original, galsim.RandomKnots):
296+
elif hasattr(obj, 'original') and isinstance(obj.original, galsim.RandomKnots):
297297
# Handle RandomKnots object directly
298298
obj = obj.original._profile
299299

tests/test_fringing.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import hashlib
23
import pytest
34
from imsim import make_batoid_wcs, CCD_Fringing, get_camera
45
import galsim
@@ -18,6 +19,7 @@ def test_fringing():
1819
camera = get_camera()
1920
det_name = 'R22_S11'
2021
serial_num = camera[det_name].getSerial()
22+
seed = int(hashlib.sha256(serial_num.encode('UTF-8')).hexdigest(), 16) & 0xFFFFFFFF
2123

2224
xarr, yarr = np.meshgrid(range(4096), range(4004))
2325

@@ -41,7 +43,7 @@ def test_fringing():
4143

4244
ccd_fringing = CCD_Fringing(true_center=image.wcs.toWorld(image.true_center),
4345
boresight=world_center,
44-
seed=hash(serial_num), spatial_vary=True)
46+
seed=seed, spatial_vary=True)
4547
# Test zero value error
4648
with pytest.raises(ValueError):
4749
ccd_fringing.calculate_fringe_amplitude(xarr, yarr, amplitude=0)
@@ -91,7 +93,7 @@ def test_fringing():
9193

9294
ccd_fringing_1 = CCD_Fringing(true_center=image.wcs.toWorld(image.true_center),
9395
boresight=world_center,
94-
seed=hash(serial_num), spatial_vary=False)
96+
seed=seed, spatial_vary=False)
9597
fringe_map1 = ccd_fringing_1.calculate_fringe_amplitude(xarr,yarr)
9698

9799
# Try another random location on the focal plane.
@@ -101,7 +103,7 @@ def test_fringing():
101103

102104
ccd_fringing_2 = CCD_Fringing(true_center=image.wcs.toWorld(image.true_center),
103105
boresight=world_center,
104-
seed=hash(serial_num), spatial_vary=False)
106+
seed=seed, spatial_vary=False)
105107
fringe_map2 = ccd_fringing_2.calculate_fringe_amplitude(xarr,yarr)
106108
# Check if the two fringing maps are indentical.
107109
if np.array_equal(fringe_map1,fringe_map2) != True:

0 commit comments

Comments
 (0)