def test_save_screens_touts_screen_arr(self): """ Writing screens with array elements to confirm padding :return: """ # Make a tout to write screen = {"x": np.linspace(0, 1, 11), "y": np.linspace(0, 1, 11), "Bx": np.linspace(0, 0.1, 11), "By": np.linspace(0, 0.1, 11)} # Write it to the temp directory test_file = os.path.join(tempfile.gettempdir(), "save_screens_touts_screen_arr.gdf") with open(test_file, "wb") as f: easygdf.save_screens_touts(f, screens=[screen, ]) # Read it back with open(test_file, "rb") as f: all_data = easygdf.load_screens_touts(f) # Confirm that the tout has the correctly shaped arrays arr2 = ["x", "y", "z", "G", "Bx", "By", "Bz", "rxy", "m", "q", "nmacro", "rmacro", "ID", "t"] for an in arr2: self.assertEqual(all_data["screens"][0][an].shape, all_data["screens"][0]["x"].shape) # Check that specific entries have autofilled correctly ss = all_data["screens"][0] np.testing.assert_almost_equal(ss["rxy"], np.sqrt(ss["x"] ** 2 + ss["y"] ** 2)) np.testing.assert_almost_equal(ss["G"], 1 / np.sqrt(1 - ss["Bx"] ** 2 - ss["By"] ** 2)) np.testing.assert_almost_equal(ss["ID"], np.linspace(0, 10, 11))
def test_save_screens_touts_tout_arr(self): """ Writing touts with array elements to confirm padding :return: """ # Make a tout to write tout = {"scat_x": np.linspace(0, 1, 7), "x": np.linspace(0, 1, 11), "y": np.linspace(0, 1, 11), "Bx": np.linspace(0, 0.1, 11), "By": np.linspace(0, 0.1, 11)} # Write it to the temp directory test_file = os.path.join(tempfile.gettempdir(), "save_screens_tout_tout_arr.gdf") with open(test_file, "wb") as f: easygdf.save_screens_touts(f, touts=[tout, ]) # Read it back with open(test_file, "rb") as f: all_data = easygdf.load_screens_touts(f) # Confirm that the tout has the correctly shaped arrays arr = ["scat_x", "scat_y", "scat_z", "scat_Qin", "scat_Qout", "scat_Qnet", "scat_Ein", "scat_Eout", "scat_Enet", "scat_inp"] for an in arr: self.assertEqual(all_data["touts"][0][an].shape, all_data["touts"][0]["scat_x"].shape) arr2 = ["x", "y", "z", "G", "Bx", "By", "Bz", "rxy", "m", "q", "nmacro", "rmacro", "ID", "fEx", "fEy", "fEz", "fBx", "fBy", "fBz"] for an in arr2: self.assertEqual(all_data["touts"][0][an].shape, all_data["touts"][0]["x"].shape) # Check that specific entries have autofilled correctly to = all_data["touts"][0] np.testing.assert_almost_equal(to["rxy"], np.sqrt(to["x"] ** 2 + to["y"] ** 2)) np.testing.assert_almost_equal(to["G"], 1 / np.sqrt(1 - to["Bx"] ** 2 - to["By"] ** 2)) np.testing.assert_almost_equal(to["ID"], np.linspace(0, 10, 11))
def test_save_screens_touts_scatter_wrong_dim(self): """ Confirms error is thrown when two array elements of incorrect length are given to screen/tout save. :return: """ # Write it to the temp directory test_file = os.path.join(tempfile.gettempdir(), "save_screens_touts_scatter_wrong_dim.gdf") with open(test_file, "wb") as f: with self.assertRaises(ValueError): easygdf.save_screens_touts(f, scat_Ein=np.linspace(0, 1, 10), scat_Enet=np.linspace(0, 1, 9))
def test_screens_touts_uniform_interface(self): """ Confirms that the output of the screen/tout loader can be dumped directly into the saver :return: """ # Load a file with pkg_resources.resource_stream("easygdf.tests", "data/screens_touts.gdf") as f: all_data = easygdf.load_screens_touts(f) # Try to save it test_file = os.path.join(tempfile.gettempdir(), "screens_touts_uniform_interface.gdf") with open(test_file, "wb") as f: easygdf.save_screens_touts(f, **all_data)
def test_save_screens_touts_screen_arr_wrong_dim(self): """ Confirms error is thrown when arrays with wrong dimension are included :return: """ # Make a tout to write screen = {"x": np.linspace(0, 1, 11), "y": np.linspace(0, 1, 7)} # Write it to the temp directory test_file = os.path.join(tempfile.gettempdir(), "save_screens_touts_screen_arr_wrong_dim.gdf") with open(test_file, "wb") as f: with self.assertRaises(ValueError): easygdf.save_screens_touts(f, screens=[screen, ])
def test_save_screens_touts(self): """ Writes our reference screen/tout object and confirms we get something similar back :return: """ # Write it to the temp directory test_file = os.path.join(tempfile.gettempdir(), "save_screens_touts.gdf") with open(test_file, "wb") as f: easygdf.save_screens_touts(f, **self.ref) # Read it back with open(test_file, "rb") as f: all_data = easygdf.load_screens_touts(f) # Ditch the header header_names = ["creation_time", "creator", "destination", "gdf_version", "creator_version", "destination_version", "dummy"] for hn in header_names: all_data.pop(hn) # Compare with the reference self.assertEqual(all_data.keys(), self.ref.keys()) # Go through each lower param for key in all_data: if key not in ["screens", "touts"]: if isinstance(all_data[key], np.ndarray): np.testing.assert_almost_equal(all_data[key], self.ref[key]) else: self.assertEqual(all_data[key], self.ref[key]) # Now check the screens and touts self.assertEqual(len(all_data["screens"]), len(self.ref["screens"])) for ts, rs in zip(all_data["screens"], self.ref["screens"]): self.assertEqual(ts.keys(), rs.keys()) for key in ts: if isinstance(ts[key], np.ndarray): np.testing.assert_almost_equal(ts[key], rs[key]) else: self.assertEqual(ts[key], rs[key]) self.assertEqual(len(all_data["touts"]), len(self.ref["touts"])) for ts, rs in zip(all_data["touts"], self.ref["touts"]): self.assertEqual(ts.keys(), rs.keys()) for key in ts: if isinstance(ts[key], np.ndarray): np.testing.assert_almost_equal(ts[key], rs[key]) else: self.assertEqual(ts[key], rs[key])
def test_save_screens_touts_screen_aux_elem(self): """ Makes sure we can save screens w/ auxiliary elements :return: """ # Make a tout to write screen = {"deadbeef": np.linspace(0, 1, 11)} # Write it to the temp directory test_file = os.path.join(tempfile.gettempdir(), "save_screens_touts_tout_wrong_elem.gdf") with open(test_file, "wb") as f: easygdf.save_screens_touts(f, screens=[screen, ]) # Load it back and check we saved it all_data = easygdf.load_screens_touts(test_file) self.assertEqual(all_data["screens"][0]["deadbeef"].size, 11)
def test_save_screens_touts_tout_aux_elem(self): """ Checks that we may save touts with auxiliary elements :return: """ # Make a tout to write tout = {"deadbeef": np.linspace(0, 1, 11), } # Write it to the temp directory test_file = os.path.join(tempfile.gettempdir(), "save_screens_touts_tout_wrong_elem.gdf") with open(test_file, "wb") as f: easygdf.save_screens_touts(f, touts=[tout, ]) # Load it back and check we saved it all_data = easygdf.load_screens_touts(test_file) self.assertEqual(all_data["touts"][0]["deadbeef"].size, 11)
def test_save_screens_touts_scatter(self): """ Writes a scatter object to confirm easygdf will auto-correct the array lengths. :return: """ # Write it to the temp directory test_file = os.path.join(tempfile.gettempdir(), "save_screens_touts_scatter.gdf") with open(test_file, "wb") as f: easygdf.save_screens_touts(f, scat_Ein=np.linspace(0, 1, 10)) # Read the file back with open(test_file, "rb") as f: all_data = easygdf.load_screens_touts(f) # Confirm the lengths of the array objects arr = ["scat_x", "scat_y", "scat_z", "scat_Qin", "scat_Qout", "scat_Qnet", "scat_Ein", "scat_Eout", "scat_Enet", "scat_inp"] for an in arr: self.assertEqual(all_data[an].shape, all_data["scat_Ein"].shape)