Skip to content

Commit

Permalink
Test input.telescope using input.table_row
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeyers314 committed May 17, 2024
1 parent 948fec3 commit 1beea86
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
10 changes: 8 additions & 2 deletions imsim/telescope_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@


def parse_xyz(xyz, base):
# If xyz is a dict, see if we can parse it into a list first
safe = True
if isinstance(xyz, dict):
xyz, safe1 = ParseValue({"xyz":xyz}, 'xyz', base, list)
safe &= safe1
if not isinstance(xyz, list) or len(xyz) != 3:
raise ValueError("Expecting a list of 3 elements")
parsed_xyz, safe = zip(*[ParseValue(xyz, i, base, float) for i in range(3)])
return parsed_xyz, all(safe)
parsed_xyz, safe1 = zip(*[ParseValue(xyz, i, base, float) for i in range(3)])
safe &= all(safe1)
return parsed_xyz, safe


def apply_fea(
Expand Down
70 changes: 65 additions & 5 deletions tests/test_table_row.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from tempfile import TemporaryDirectory

import astropy.units as u
import batoid
import galsim
import imsim
import numpy as np
Expand Down Expand Up @@ -32,7 +33,10 @@ def create_table():
table["shift"] = np.array(
[(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5)], dtype=shift_dtype
)
table["shift"].unit = u.m
table["shift"].unit = u.mm
# Some more values for setting up a telescope
table["camera"] = ["LsstCam", "LsstCam", "LsstComCamSim", "LsstComCamSim"]
table["focusZ"] = [-1.0, -0.5, 0.0, 0.5] * u.mm

table.pprint_all()
return table
Expand Down Expand Up @@ -146,7 +150,7 @@ def check_row_data(config, idx):
# Read the str column
s, safe = RowData({"field": "str"}, config, str)
assert safe == True
assert s == chr(ord("a") + idx)
assert s == "abcd"[idx]

# Can't specify to_unit for str
with np.testing.assert_raises(ValueError):
Expand Down Expand Up @@ -183,9 +187,9 @@ def check_row_data(config, idx):
shift, safe = RowData({"field": "shift", "to_unit": "cm"}, config, list)
assert safe == True
assert len(shift) == 3
np.testing.assert_allclose(shift[0], idx * 100, rtol=0, atol=1e-15)
np.testing.assert_allclose(shift[1], (idx + 1) * 100, rtol=0, atol=1e-15)
np.testing.assert_allclose(shift[2], (idx + 2) * 100, rtol=0, atol=1e-15)
np.testing.assert_allclose(shift[0], idx / 10, rtol=0, atol=1e-15)
np.testing.assert_allclose(shift[1], (idx + 1) / 10, rtol=0, atol=1e-15)
np.testing.assert_allclose(shift[2], (idx + 2) / 10, rtol=0, atol=1e-15)


def test_table_row():
Expand Down Expand Up @@ -227,6 +231,62 @@ def test_table_row():
idx = idx0 * 2 + idx1
check_row_data(config, idx)

# Try loading a telescope using row_data for just one idx
config["input"]["telescope"] = {
"file_name": "LSST_r.yaml",
"perturbations": {
"LSSTCamera": {
"shift": {
"type":"RowData",
"field": "shift",
"to_unit": "m"
},
"rotX": {
"type": "RowData",
"field": "tilt",
"subfield": "rx",
}
}
},
"rotTelPos": {
"type": "RowData",
"field": "angle1",
},
}
galsim.config.ProcessInput(config)
telescope = galsim.config.GetInputObj(
'telescope',
config['input']['telescope'],
config,
'telescope'
).fiducial

telescope0 = batoid.Optic.fromYaml("LSST_r.yaml")
telescope0 = telescope0.withGloballyShiftedOptic(
"LSSTCamera",
RowData({"field": "shift", "to_unit": "m"}, config, list)[0]
)
telescope0 = telescope0.withLocallyRotatedOptic(
"LSSTCamera",
batoid.RotX(RowData({"field": "tilt", "subfield": "rx", "to_unit":"rad"}, config, float)[0]),
)
telescope0 = telescope0.withLocallyRotatedOptic(
"LSSTCamera",
batoid.RotZ(RowData({"field": "angle1", "to_unit":"rad"}, config, float)[0])
)
np.testing.assert_allclose(
telescope0['LSSTCamera'].coordSys.origin,
telescope['LSSTCamera'].coordSys.origin,
rtol=0,
atol=1e-15,
)
np.testing.assert_allclose(
telescope0['LSSTCamera'].coordSys.rot,
telescope['LSSTCamera'].coordSys.rot,
rtol=0,
atol=1e-15,
)


if __name__ == "__main__":
testfns = [v for k, v in vars().items() if k[:5] == 'test_' and callable(v)]
Expand Down

0 comments on commit 1beea86

Please sign in to comment.