Public Access
fix serialization of numpy scalars in job XML under numpy 2
numpy 2 changed repr(np.float64(1e-06)) to 'np.float64(1e-06)', so values taken from lin_range() ended up unparsable in the job file: the result script failed with "could not convert string to float". Description items and state times are now converted to builtin types before writing.
This commit is contained in:
1 parent
ef11824f8b
commit
1fb56e20f9
2 files changed
+34
-6
No files matched your search
@@ -42,6 +42,34 @@ class TestExperimentPhase(unittest.TestCase):
|
||||
e.rf_pulse(length=1e-6, phase=-90, source=0)
|
||||
self.assertEqual(phase_of_state(e.state_list.list[0]), 270.0)
|
||||
|
||||
def test_numpy_scalars_in_description(self):
|
||||
"""Test that description items with numpy scalars (e.g. from lin_range)
|
||||
are written as plain numbers so the result script can parse them again."""
|
||||
import numpy as np
|
||||
from damaris.tools.ranges import lin_range
|
||||
e = Experiment()
|
||||
e.set_description("tau", lin_range(1e-6, 3e-6, 1e-6)[0])
|
||||
e.set_description("n", np.int64(42))
|
||||
xml = e.write_xml_string()
|
||||
items = {key: (typ, text)
|
||||
for key, typ, text in
|
||||
re.findall(r'<item key="([^"]+)" type="([^"]+)">([^<]*)</item>', xml)}
|
||||
self.assertEqual(items["tau"][0], "Float")
|
||||
self.assertEqual(items["n"][0], "Int")
|
||||
self.assertEqual(float(items["tau"][1]), 1e-6)
|
||||
self.assertEqual(int(items["n"][1]), 42)
|
||||
|
||||
def test_numpy_scalar_state_times(self):
|
||||
"""Test that numpy scalar times do not end up as np.float64(...) in the XML."""
|
||||
import numpy as np
|
||||
e = Experiment()
|
||||
e.wait(np.float64(1e-6))
|
||||
e.state_start(np.float64(2e-6))
|
||||
xml = e.write_xml_string()
|
||||
self.assertNotIn("np.float64", xml)
|
||||
self.assertIn('<state time="1e-06"/>', xml)
|
||||
self.assertIn('<state time="2e-06">', xml)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user