Skip to content

Commit 8f8963e

Browse files
authored
Merge pull request #377 from AstarVienna/hb/nomorecurrsysusercommands
Do not set user commands as rc.__currsys__
2 parents 88ee6d6 + e2026d4 commit 8f8963e

13 files changed

Lines changed: 83 additions & 61 deletions

File tree

scopesim/effects/data_container.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class DataContainer:
6565
def __init__(self, filename=None, table=None, array_dict=None, cmds=None,
6666
**kwargs):
6767
self.cmds = cmds
68+
# Setting a default for cmds cannot be done here, because from_currsys
69+
# checks whether cmds is None. TODO: make this possible.
70+
# if self.cmds is None:
71+
# from scopesim import UserCommands
72+
# self.cmds = UserCommands()
73+
6874
if filename is None and "file_name" in kwargs:
6975
warn("The 'file_name' kwarg is deprecated and will raise an error "
7076
"in the future, please use 'filename' instead!",

scopesim/effects/electronic.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ def apply_to(self, obj, **kwargs):
105105

106106
self.meta["detector_readout_mode"] = mode_name
107107
props_dict = self.mode_properties[mode_name]
108-
rc.__currsys__["!OBS.detector_readout_mode"] = mode_name
108+
self.cmds["!OBS.detector_readout_mode"] = mode_name
109109
for key, value in props_dict.items():
110-
rc.__currsys__[key] = value
110+
self.cmds[key] = value
111111

112112
return obj
113113

@@ -185,6 +185,9 @@ def __init__(self, **kwargs):
185185
params = {"z_order": [902]}
186186
self.meta.update(params)
187187
self.meta.update(kwargs)
188+
if self.cmds is None:
189+
from scopesim import UserCommands
190+
self.cmds = UserCommands()
188191

189192
required_keys = ["fill_frac", "full_well", "mindit"]
190193
check_keys(self.meta, required_keys, action="error")
@@ -226,8 +229,8 @@ def apply_to(self, obj, **kwargs):
226229
logger.info("Exposure parameters: DIT=%.3f s NDIT=%d", dit, ndit)
227230
logger.info("Total exposure time: %.3f s", dit * ndit)
228231

229-
rc.__currsys__["!OBS.dit"] = dit
230-
rc.__currsys__["!OBS.ndit"] = ndit
232+
self.cmds["!OBS.dit"] = dit
233+
self.cmds["!OBS.ndit"] = ndit
231234

232235
return obj
233236

scopesim/effects/fits_headers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ class EffectsMetaKeywords(ExtraFitsKeywords):
425425
"""
426426

427427
def __init__(self, cmds=None, **kwargs):
428-
super(ExtraFitsKeywords, self).__init__()
428+
super(ExtraFitsKeywords, self).__init__(cmds=cmds, **kwargs)
429429
params = {"name": "effects_fits_keywords",
430430
"description": "Effect Meta FITS headers",
431431
"z_order": [998],
@@ -512,7 +512,7 @@ class SourceDescriptionFitsKeywords(ExtraFitsKeywords):
512512
"""
513513

514514
def __init__(self, cmds=None, **kwargs):
515-
super(ExtraFitsKeywords, self).__init__()
515+
super(ExtraFitsKeywords, self).__init__(cmds=cmds, **kwargs)
516516
params = {"name": "source_fits_keywords",
517517
"description": "Source description FITS headers",
518518
"z_order": [997],
@@ -596,7 +596,7 @@ class SimulationConfigFitsKeywords(ExtraFitsKeywords):
596596
"""
597597

598598
def __init__(self, cmds=None, **kwargs):
599-
super(ExtraFitsKeywords, self).__init__()
599+
super(ExtraFitsKeywords, self).__init__(cmds=cmds, **kwargs)
600600
params = {"name": "simulation_fits_keywords",
601601
"description": "Simulation Config FITS headers",
602602
"z_order": [996],

scopesim/effects/psf_utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ def make_strehl_map_from_table(tbl, pixel_scale=1*u.arcsec):
109109
return map_hdu
110110

111111

112-
def rescale_kernel(image, scale_factor, spline_order=None, cmds=None):
113-
if spline_order is None:
114-
spline_order = utils.from_currsys("!SIM.computing.spline_order", cmds=cmds)
112+
def rescale_kernel(image, scale_factor, spline_order):
115113
sum_image = np.sum(image)
116114
image = zoom(image, scale_factor, order=spline_order)
117115
image = np.nan_to_num(image, copy=False) # numpy version >=1.13

scopesim/effects/psfs.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,8 @@ def remake_kernel(self, x):
495495
def wavelength(self):
496496
wave = from_currsys(self.meta["wavelength"], self.cmds)
497497
if isinstance(wave, str) and wave in tu.FILTER_DEFAULTS:
498-
wave = tu.get_filter_effective_wavelength(wave, cmds=self.cmds)
498+
filter_name = from_currsys(wave, cmds=self.cmds)
499+
wave = tu.get_filter_effective_wavelength(filter_name)
499500
wave = quantify(wave, u.um).value
500501

501502
return wave
@@ -634,7 +635,8 @@ def get_kernel(self, fov):
634635
# rescaling kept inside loop to avoid rescaling for every fov
635636
pix_ratio = kernel_pixel_scale / fov_pixel_scale
636637
if abs(pix_ratio - 1) > self.meta["flux_accuracy"]:
637-
self.kernel = pu.rescale_kernel(self.kernel, pix_ratio)
638+
spline_order = from_currsys("!SIM.computing.spline_order", cmds=self.cmds)
639+
self.kernel = pu.rescale_kernel(self.kernel, pix_ratio, spline_order)
638640

639641
if ((fov.header["NAXIS1"] < hdr["NAXIS1"]) or
640642
(fov.header["NAXIS2"] < hdr["NAXIS2"])):
@@ -830,8 +832,9 @@ def get_kernel(self, fov):
830832
# rescale the pixel scale of the kernel to match the fov images
831833
pix_ratio = fov_pixel_scale / kernel_pixel_scale
832834
if abs(pix_ratio - 1) > self.meta["flux_accuracy"]:
835+
spline_order = from_currsys("!SIM.computing.spline_order", cmds=self.cmds)
833836
for ii, kern in enumerate(self.kernel):
834-
self.kernel[ii][0] = pu.rescale_kernel(kern[0], pix_ratio)
837+
self.kernel[ii][0] = pu.rescale_kernel(kern[0], pix_ratio, spline_order)
835838

836839
for i, kern in enumerate(self.kernel):
837840
self.kernel[i][0] /= np.sum(kern[0])

scopesim/effects/ter_curves.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def __init__(self, filename=None, **kwargs):
8989
self.meta.update(params)
9090
self.meta.update(kwargs)
9191

92-
self.surface = SpectralSurface()
92+
self.surface = SpectralSurface(cmds=self.cmds)
9393
self.surface.meta.update(self.meta)
9494
self._background_source = None
9595

@@ -918,6 +918,7 @@ def __init__(self, cmds=None, **kwargs):
918918
for name in from_currsys(self.meta["adc_names"], cmds=self.cmds):
919919
kwargs["name"] = name
920920
self.adcs[name] = TERCurve(filename=str(path).format(name),
921+
cmds=cmds,
921922
**kwargs)
922923

923924
self.table = self.get_table()

scopesim/effects/ter_curves_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
def get_filter_effective_wavelength(filter_name, cmds=None):
5454
if isinstance(filter_name, str):
55-
filter_name = from_currsys(filter_name, cmds=cmds)
55+
assert FILTER_DEFAULTS.get(filter_name), f"{filter_name} not found in FILTER_DEFAULTS"
5656
wave, trans = download_svo_filter(FILTER_DEFAULTS[filter_name],
5757
return_style="quantity")
5858
eff_wave = np.sum(wave * trans) / np.sum(trans) # convert from Angstrom

scopesim/optics/fov_manager.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,13 @@ def generate_fovs_list(self):
166166
det_eff = eu.get_all_effects(self.effects, DetectorList)[0]
167167
dethdr = det_eff.image_plane_header
168168

169-
fovs.append(FieldOfView(skyhdr, waverange, detector_header=dethdr,
170-
**vol["meta"]))
169+
fovs.append(FieldOfView(
170+
skyhdr,
171+
waverange,
172+
detector_header=dethdr,
173+
cmds=self.cmds,
174+
**vol["meta"],
175+
))
171176

172177
return fovs
173178

scopesim/optics/optical_train.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ def load(self, user_commands):
139139
# UserCommands via rc.__currsys__, but on the other hand some
140140
# tests (now with proper patching) fail because of this type
141141
# change. THIS IS A PROBLEM!
142-
rc.__currsys__ = user_commands
142+
# NOTE: All tests pass without setting rc.__currsys__ to user_commands.
143+
# Nevertheless, I'm a bit reluctant to removing this code just
144+
# yet. So it is commented out.
145+
# rc.__currsys__ = user_commands
143146
self.yaml_dicts = self.cmds.yaml_dicts
144147
self.optics_manager = OpticsManager(self.yaml_dicts, self.cmds)
145148
self.update()

scopesim/tests/tests_effects/test_AutoExposure.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from scopesim import UserCommands
77
from scopesim.optics.image_plane import ImagePlane
88
from scopesim.effects.electronic import AutoExposure
9-
from scopesim.utils import from_currsys
109

1110
from scopesim.tests.mocks.py_objects.imagehdu_objects import _image_hdu_square
1211

@@ -28,7 +27,7 @@ def fixture_imageplane():
2827
return implane
2928

3029

31-
@pytest.fixture(name="autoexposure", scope="class")
30+
@pytest.fixture(name="autoexposure", scope="function")
3231
def fixture_autoexposure():
3332
"""Instantiate an AutoExposure object"""
3433
return AutoExposure(fill_frac=0.75,
@@ -45,8 +44,8 @@ def test_initialises_correctly(self):
4544
assert isinstance(autoexposure, AutoExposure)
4645

4746
def test_returns_imageplane(self, autoexposure, imageplane):
48-
with patch("scopesim.rc.__currsys__", _patched_cmds()):
49-
outimpl = autoexposure.apply_to(imageplane)
47+
autoexposure.cmds = _patched_cmds()
48+
outimpl = autoexposure.apply_to(imageplane)
5049

5150
assert isinstance(outimpl, ImagePlane)
5251

@@ -59,24 +58,24 @@ def test_produces_correct_values(self, autoexposure, imageplane):
5958
ref_dit = 0.75
6059

6160
# TODO: Change AutoExposure to read exptime like dit and ndit
62-
with patch("scopesim.rc.__currsys__", _patched_cmds(exptime=exptime)):
63-
autoexposure.apply_to(imageplane)
61+
autoexposure.cmds = _patched_cmds(exptime=exptime)
62+
autoexposure.apply_to(imageplane)
6463

65-
out_dit = from_currsys("!OBS.dit")
66-
out_ndit = from_currsys("!OBS.ndit")
64+
out_dit = autoexposure.cmds["!OBS.dit"]
65+
out_ndit = autoexposure.cmds["!OBS.ndit"]
6766

6867
assert out_dit == pytest.approx(ref_dit)
6968
assert out_dit * out_ndit == pytest.approx(exptime)
7069

7170
def test_detects_saturation(self, imageplane):
7271
mindit = 0.011
73-
with patch("scopesim.rc.__currsys__", _patched_cmds(exptime=100.)):
74-
autoexposure = AutoExposure(fill_frac=0.75,
75-
full_well=10.,
76-
mindit=mindit)
77-
autoexposure.apply_to(imageplane)
72+
autoexposure = AutoExposure(fill_frac=0.75,
73+
full_well=10.,
74+
mindit=mindit,
75+
cmds=_patched_cmds(exptime=100.))
76+
autoexposure.apply_to(imageplane)
7877

79-
out_dit = from_currsys("!OBS.dit")
78+
out_dit = autoexposure.cmds["!OBS.dit"]
8079

8180
assert out_dit == mindit
8281

@@ -85,19 +84,19 @@ def test_fill_frac_acts_correctly(self, imageplane):
8584
fill_2 = 0.5
8685
autoexp_1 = AutoExposure(fill_frac=fill_1,
8786
full_well=1e5,
88-
mindit=0.011)
89-
with patch("scopesim.rc.__currsys__", _patched_cmds()):
90-
autoexp_1.apply_to(imageplane)
91-
out_dit_1 = from_currsys("!OBS.dit")
92-
out_ndit_1 = from_currsys("!OBS.ndit")
87+
mindit=0.011,
88+
cmds=_patched_cmds())
89+
autoexp_1.apply_to(imageplane)
90+
out_dit_1 = autoexp_1.cmds["!OBS.dit"]
91+
out_ndit_1 = autoexp_1.cmds["!OBS.ndit"]
9392

9493
autoexp_2 = AutoExposure(fill_frac=fill_2,
9594
full_well=1e5,
96-
mindit=0.011)
97-
with patch("scopesim.rc.__currsys__", _patched_cmds()):
98-
autoexp_2.apply_to(imageplane)
99-
out_dit_2 = from_currsys("!OBS.dit")
100-
out_ndit_2 = from_currsys("!OBS.ndit")
95+
mindit=0.011,
96+
cmds=_patched_cmds())
97+
autoexp_2.apply_to(imageplane)
98+
out_dit_2 = autoexp_2.cmds["!OBS.dit"]
99+
out_ndit_2 = autoexp_2.cmds["!OBS.ndit"]
101100

102101
assert out_dit_1 == fill_1 / fill_2 * out_dit_2
103102
assert out_ndit_1 == fill_2 / fill_1 * out_ndit_2
@@ -108,17 +107,18 @@ def test_exptime_specified_by_dit_ndit(self, autoexposure, imageplane):
108107
instead of `!OBS.exptime`.
109108
"""
110109
# 1. use exptime
111-
with patch("scopesim.rc.__currsys__", _patched_cmds(exptime=10.)):
112-
autoexposure.apply_to(imageplane)
113-
dit_1 = from_currsys("!OBS.dit")
114-
ndit_1 = from_currsys("!OBS.ndit")
110+
autoexposure.cmds["!OBS.exptime"] = 10.0
111+
autoexposure.apply_to(imageplane)
112+
dit_1 = autoexposure.cmds["!OBS.dit"]
113+
ndit_1 = autoexposure.cmds["!OBS.ndit"]
115114

116115
# 2. use dit and ndit
117-
with patch("scopesim.rc.__currsys__",
118-
_patched_cmds(exptime=None, dit=5, ndit=2)):
119-
autoexposure.apply_to(imageplane)
120-
dit_2 = from_currsys("!OBS.dit")
121-
ndit_2 = from_currsys("!OBS.ndit")
116+
autoexposure.cmds["!OBS.exptime"] = None
117+
autoexposure.cmds["!OBS.dit"] = 5
118+
autoexposure.cmds["!OBS.ndit"] = 2
119+
autoexposure.apply_to(imageplane)
120+
dit_2 = autoexposure.cmds["!OBS.dit"]
121+
ndit_2 = autoexposure.cmds["!OBS.ndit"]
122122

123123
assert dit_1 == dit_2
124124
assert ndit_1 == ndit_2
@@ -129,10 +129,10 @@ def test_exptime_at_least_mindit(self, imageplane):
129129
autoexposure = AutoExposure(fill_frac=0.75,
130130
full_well=1e5,
131131
mindit=mindit)
132-
with patch("scopesim.rc.__currsys__", _patched_cmds(exptime=exptime)):
133-
autoexposure.apply_to(imageplane)
134-
dit = from_currsys("!OBS.dit")
135-
ndit = from_currsys("!OBS.ndit")
132+
autoexposure.cmds["!OBS.exptime"] = exptime
133+
autoexposure.apply_to(imageplane)
134+
dit = autoexposure.cmds["!OBS.dit"]
135+
ndit = autoexposure.cmds["!OBS.ndit"]
136136

137137
assert dit == mindit
138138
assert ndit == 1

0 commit comments

Comments
 (0)