Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve recursive bang-strings #351

Merged
merged 12 commits into from
Jan 31, 2024
5 changes: 4 additions & 1 deletion scopesim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@
# VERSION INFORMATION #
###############################################################################

__version__ = metadata.version(__package__)
try:
__version__ = metadata.version(__package__)
except metadata.PackageNotFoundError:
__version__ = "undetermined"

Check warning on line 69 in scopesim/__init__.py

View check run for this annotation

Codecov / codecov/patch

scopesim/__init__.py#L68-L69

Added lines #L68 - L69 were not covered by tests
2 changes: 1 addition & 1 deletion scopesim/effects/effects_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def combine_surface_effects(surface_effects):
surflist_list = [eff for eff in surface_effects
if isinstance(eff, efs.SurfaceList)]
surf_list = [eff for eff in surface_effects
if isinstance(eff, (efs.TERCurve, efs.FilterWheel))
if isinstance(eff, (efs.TERCurve, efs.FilterWheelBase))
and not isinstance(eff, efs.SurfaceList)]

if not surflist_list:
Expand Down
4 changes: 2 additions & 2 deletions scopesim/effects/surface_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from astropy import units as u

from .ter_curves import TERCurve
from .ter_curves import TERCurve, FilterWheelBase
from ..optics import radiometry_utils as rad_utils
from ..optics.surface import PoorMansSurface
from ..utils import quantify, from_currsys, figure_factory
Expand Down Expand Up @@ -122,7 +122,7 @@ def is_empty(self):
def add_surface(self, surface, name=None, position=-1, add_to_table=True):
if name is None:
name = surface.meta.get("name", "<unknown surface>")
if isinstance(surface, TERCurve):
if isinstance(surface, (TERCurve, FilterWheelBase)):
ter_meta = surface.meta
surface = surface.surface
surface.meta.update(ter_meta)
Expand Down
25 changes: 22 additions & 3 deletions scopesim/effects/ter_curves.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Transmission, emissivity, reflection curves."""

import warnings
from collections.abc import Collection
from collections.abc import Collection, Iterable

import numpy as np
import skycalc_ipy
Expand All @@ -27,6 +26,9 @@
"""
Transmission, Emissivity, Reflection Curve.

note:: This is basically an ``Effect`` wrapper for the
``SpectralSurface`` object

Must contain a wavelength column, and one or more of the following:
``transmission``, ``emissivity``, ``reflection``.
Additionally, in the header there
Expand Down Expand Up @@ -202,7 +204,14 @@
if wavelength is None:
wunit = params["wave_unit"]
# TODO: shouldn't need both, make sure they're equal
assert wunit == wave_unit
if wunit != wave_unit:
logger.warning("wavelength units in the meta dict of "

Check warning on line 208 in scopesim/effects/ter_curves.py

View check run for this annotation

Codecov / codecov/patch

scopesim/effects/ter_curves.py#L207-L208

Added lines #L207 - L208 were not covered by tests
"%s are inconsistent:\n"
"- wavelength_unit : %s\n"
"- wave_unit : %s",
{self.meta.get("name")},
wave_unit, wunit)

wave = np.arange(quantify(params["wave_min"], wunit).value,
quantify(params["wave_max"], wunit).value,
quantify(params["wave_bin"], wunit).value)
Expand All @@ -214,6 +223,8 @@
abbrs = {"t": "transmission", "e": "emission",
"r": "reflection", "x": "throughput"}

if not isinstance(axes, Iterable):
axes = [axes]

Check warning on line 227 in scopesim/effects/ter_curves.py

View check run for this annotation

Codecov / codecov/patch

scopesim/effects/ter_curves.py#L226-L227

Added lines #L226 - L227 were not covered by tests
for ter, ax in zip(which, axes):
y_name = abbrs.get(ter, "throughput")
y = getattr(self.surface, y_name)
Expand Down Expand Up @@ -584,6 +595,14 @@
"""Use apply_to of current filter."""
return self.current_filter.apply_to(obj, **kwargs)

@property
def surface(self):
return self.current_filter.surface

Check warning on line 600 in scopesim/effects/ter_curves.py

View check run for this annotation

Codecov / codecov/patch

scopesim/effects/ter_curves.py#L600

Added line #L600 was not covered by tests

@property
def throughput(self):
return self.current_filter.throughput

Check warning on line 604 in scopesim/effects/ter_curves.py

View check run for this annotation

Codecov / codecov/patch

scopesim/effects/ter_curves.py#L604

Added line #L604 was not covered by tests

def fov_grid(self, which="waveset", **kwargs):
warnings.warn("The fov_grid method is deprecated and will be removed "
"in a future release.", DeprecationWarning, stacklevel=2)
Expand Down
18 changes: 14 additions & 4 deletions scopesim/optics/optics_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,20 @@ def fov_setup_effects(self):
@property
def surfaces_table(self):
"""Get combined surface table from effects with z_order = 100...199."""
if self._surfaces_table is None:
surface_like_effects = self.get_z_order_effects(100)
self._surfaces_table = combine_surface_effects(surface_like_effects)
return self._surfaces_table
from copy import deepcopy
sle_list = self.get_z_order_effects(100)
sle_list_copy = []
for eff in sle_list:
if isinstance(eff, efs.SurfaceList):
eff_copy = deepcopy(eff)
eff_copy.table = from_currsys(eff.table)
else:
# Avoid infinite recursion in Wheel effects (filter, adc)
eff_copy = eff
sle_list_copy.append(eff_copy)

comb_table = combine_surface_effects(sle_list_copy)
teutoburg marked this conversation as resolved.
Show resolved Hide resolved
return comb_table

@property
def all_effects(self):
Expand Down
13 changes: 13 additions & 0 deletions scopesim/tests/test_utils_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,24 @@ def test_converts_numpy_array(self):
def test_converts_dict(self):
assert utils.from_currsys({"seed": "!SIM.random.seed"})["seed"] is None

def test_converts_layered_bang_strings(self):
patched = {"!SIM.sub_pixel.flag": "!SIM.sub_pixel.fraction"}
with patch.dict("scopesim.rc.__currsys__", patched):
result = utils.from_currsys("!SIM.sub_pixel.flag")
assert not isinstance(result, str)
assert result == 1

def test_converts_astropy_table(self):
tbl = Table(data=[["!SIM.random.seed"]*2, ["!SIM.random.seed"]*2],
names=["seeds", "seeds2"])
assert utils.from_currsys(tbl["seeds2"][1]) is None

def test_converts_string_numericals_to_floats(self):
patched = {"!SIM.sub_pixel.fraction": "1e0"}
with patch.dict("scopesim.rc.__currsys__", patched):
result = utils.from_currsys("!SIM.sub_pixel.fraction")
assert isinstance(result, float)
assert result == 1


# load_example_optical_train modifies __currsys__!
Expand Down
11 changes: 9 additions & 2 deletions scopesim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,11 +864,18 @@
if isinstance(item, str) and len(item) and item.startswith("!"):
if item in rc.__currsys__:
item = rc.__currsys__[item]
if isinstance(item, str) and item.startswith("!"):
item = from_currsys(item)
astronomyk marked this conversation as resolved.
Show resolved Hide resolved
else:
raise ValueError(f"{item} was not found in rc.__currsys__")

if isinstance(item, str) and item.lower() == "none":
item = None
if isinstance(item, str):
if item.lower() == "none":
item = None

Check warning on line 874 in scopesim/utils.py

View check run for this annotation

Codecov / codecov/patch

scopesim/utils.py#L874

Added line #L874 was not covered by tests
try:
item = float(item)
except (TypeError, ValueError):
pass

return item

Expand Down
5 changes: 4 additions & 1 deletion scopesim/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
from importlib import metadata
version = metadata.version(__package__)
try:
version = metadata.version(__package__)
except:
version = 0.8

Check warning on line 5 in scopesim/version.py

View check run for this annotation

Codecov / codecov/patch

scopesim/version.py#L4-L5

Added lines #L4 - L5 were not covered by tests
astronomyk marked this conversation as resolved.
Show resolved Hide resolved
Loading