diff --git a/hyperspy/tests/io/test_hdf5.py b/hyperspy/tests/io/test_hdf5.py index 5e1e83e875..078854b14c 100644 --- a/hyperspy/tests/io/test_hdf5.py +++ b/hyperspy/tests/io/test_hdf5.py @@ -16,12 +16,9 @@ # You should have received a copy of the GNU General Public License # along with HyperSpy. If not, see . -import gc -import os.path +from pathlib import Path import sys -import tempfile import time -from os import remove import dask.array as da import h5py @@ -40,7 +37,8 @@ from hyperspy.roi import Point2DROI from hyperspy.utils import markers -my_path = os.path.dirname(__file__) + +my_path = Path(__file__).parent data = np.array([4066., 3996., 3932., 3923., 5602., 5288., 7234., 7809., @@ -112,10 +110,7 @@ def test_original_metadata(self): class TestExample1_12(Example1): def setup_method(self, method): - self.s = load(os.path.join( - my_path, - "hdf5_files", - "example1_v1.2.hdf5")) + self.s = load(my_path / "hdf5_files" / "example1_v1.2.hdf5") def test_date(self): assert ( @@ -128,33 +123,26 @@ def test_time(self): class TestExample1_10(Example1): def setup_method(self, method): - self.s = load(os.path.join( - my_path, - "hdf5_files", - "example1_v1.0.hdf5")) + self.s = load(my_path / "hdf5_files" / "example1_v1.0.hdf5") class TestExample1_11(Example1): def setup_method(self, method): - self.s = load(os.path.join( - my_path, - "hdf5_files", - "example1_v1.1.hdf5")) + self.s = load(my_path / "hdf5_files" / "example1_v1.1.hdf5") class TestLoadingNewSavedMetadata: def setup_method(self, method): with pytest.warns(VisibleDeprecationWarning): - self.s = load(os.path.join( - my_path, - "hdf5_files", - "with_lists_etc.hdf5")) + self.s = load(my_path / "hdf5_files" / "with_lists_etc.hdf5") def test_signal_inside(self): - np.testing.assert_array_almost_equal(self.s.data, - self.s.metadata.Signal.Noise_properties.variance.data) + np.testing.assert_array_almost_equal( + self.s.data, + self.s.metadata.Signal.Noise_properties.variance.data + ) def test_empty_things(self): assert self.s.metadata.test.empty_list == [] @@ -302,7 +290,7 @@ def test_quantity(self, tmp_path): def test_metadata_binned_deprecate(self): with pytest.warns(UserWarning, match="Loading old file"): - s = load(os.path.join(my_path, "hdf5_files", 'example2_v2.2.hspy')) + s = load(my_path / "hdf5_files" / 'example2_v2.2.hspy') assert s.metadata.has_item('Signal.binned') == False assert s.axes_manager[-1].is_binned == False @@ -328,22 +316,20 @@ def test_metadata_update_to_v3_1(self): 'original_shape': None, 'signal_unfolded': False, 'unfolded': False}}} - s = load(os.path.join( - my_path, - "hdf5_files", - 'example2_v3.1.hspy')) + s = load(my_path / "hdf5_files" / 'example2_v3.1.hspy') assert_deep_almost_equal(s.metadata.as_dictionary(), md) def test_none_metadata(): - s = load(os.path.join( my_path, "hdf5_files", "none_metadata.hdf5")) + s = load(my_path / "hdf5_files" / "none_metadata.hdf5") assert s.metadata.should_be_None is None def test_rgba16(): with pytest.warns(VisibleDeprecationWarning): - s = load(os.path.join(my_path, "hdf5_files", "test_rgba16.hdf5")) - data = np.load(os.path.join( my_path, "npy_files", "test_rgba16.npy")) + print(my_path) + s = load(my_path / "hdf5_files" / "test_rgba16.hdf5") + data = np.load(my_path / "npy_files" / "test_rgba16.npy") assert (s.data == data).all() @@ -433,12 +419,11 @@ def test_axes_configuration_binning(tmp_path): class Test_permanent_markers_io: - def test_save_permanent_marker(self): + def test_save_permanent_marker(self, tmp_path): s = Signal2D(np.arange(100).reshape(10, 10)) m = markers.point(x=5, y=5) s.add_marker(m, permanent=True) - with tempfile.TemporaryDirectory() as tmp: - filename = tmp + '/testsavefile.hdf5' + filename = tmp_path / 'testsavefile.hdf5' s.save(filename) def test_save_load_empty_metadata_markers(self, tmp_path): @@ -503,7 +488,7 @@ def test_save_load_permanent_marker_all_types(self, tmp_path): for m0_dict, m1_dict in zip(m0_dict_list, m1_dict_list): assert m0_dict == m1_dict - def test_save_load_horizontal_line_marker(self): + def test_save_load_horizontal_line_marker(self, tmp_path): y = 8 color = 'blue' linewidth = 2.5 @@ -512,14 +497,13 @@ def test_save_load_horizontal_line_marker(self): m = markers.horizontal_line(y=y, color=color, linewidth=linewidth) m.name = name s.add_marker(m, permanent=True) - with tempfile.TemporaryDirectory() as tmp: - filename = tmp + '/test_save_horizontal_line_marker.hdf5' + filename = tmp_path / 'test_save_horizontal_line_marker.hdf5' s.save(filename) s1 = load(filename) m1 = s1.metadata.Markers.get_item(name) assert san_dict(m1._to_dictionary()) == san_dict(m._to_dictionary()) - def test_save_load_horizontal_line_segment_marker(self): + def test_save_load_horizontal_line_segment_marker(self, tmp_path): x1, x2, y = 1, 5, 8 color = 'red' linewidth = 1.2 @@ -529,14 +513,13 @@ def test_save_load_horizontal_line_segment_marker(self): x1=x1, x2=x2, y=y, color=color, linewidth=linewidth) m.name = name s.add_marker(m, permanent=True) - with tempfile.TemporaryDirectory() as tmp: - filename = tmp + '/test_save_horizontal_line_segment_marker.hdf5' + filename = tmp_path / 'test_save_horizontal_line_segment_marker.hdf5' s.save(filename) s1 = load(filename) m1 = s1.metadata.Markers.get_item(name) assert san_dict(m1._to_dictionary()) == san_dict(m._to_dictionary()) - def test_save_load_vertical_line_marker(self): + def test_save_load_vertical_line_marker(self, tmp_path): x = 9 color = 'black' linewidth = 3.5 @@ -545,14 +528,13 @@ def test_save_load_vertical_line_marker(self): m = markers.vertical_line(x=x, color=color, linewidth=linewidth) m.name = name s.add_marker(m, permanent=True) - with tempfile.TemporaryDirectory() as tmp: - filename = tmp + '/test_save_vertical_line_marker.hdf5' + filename = tmp_path / 'test_save_vertical_line_marker.hdf5' s.save(filename) s1 = load(filename) m1 = s1.metadata.Markers.get_item(name) assert san_dict(m1._to_dictionary()) == san_dict(m._to_dictionary()) - def test_save_load_vertical_line_segment_marker(self): + def test_save_load_vertical_line_segment_marker(self, tmp_path): x, y1, y2 = 2, 1, 3 color = 'white' linewidth = 4.2 @@ -562,14 +544,13 @@ def test_save_load_vertical_line_segment_marker(self): x=x, y1=y1, y2=y2, color=color, linewidth=linewidth) m.name = name s.add_marker(m, permanent=True) - with tempfile.TemporaryDirectory() as tmp: - filename = tmp + '/test_save_vertical_line_segment_marker.hdf5' + filename = tmp_path / 'test_save_vertical_line_segment_marker.hdf5' s.save(filename) s1 = load(filename) m1 = s1.metadata.Markers.get_item(name) assert san_dict(m1._to_dictionary()) == san_dict(m._to_dictionary()) - def test_save_load_line_segment_marker(self): + def test_save_load_line_segment_marker(self, tmp_path): x1, x2, y1, y2 = 1, 9, 4, 7 color = 'cyan' linewidth = 0.7 @@ -579,14 +560,13 @@ def test_save_load_line_segment_marker(self): x1=x1, x2=x2, y1=y1, y2=y2, color=color, linewidth=linewidth) m.name = name s.add_marker(m, permanent=True) - with tempfile.TemporaryDirectory() as tmp: - filename = tmp + '/test_save_line_segment_marker.hdf5' + filename = tmp_path / 'test_save_line_segment_marker.hdf5' s.save(filename) s1 = load(filename) m1 = s1.metadata.Markers.get_item(name) assert san_dict(m1._to_dictionary()) == san_dict(m._to_dictionary()) - def test_save_load_point_marker(self): + def test_save_load_point_marker(self, tmp_path): x, y = 9, 8 color = 'purple' name = "point test" @@ -595,14 +575,13 @@ def test_save_load_point_marker(self): x=x, y=y, color=color) m.name = name s.add_marker(m, permanent=True) - with tempfile.TemporaryDirectory() as tmp: - filename = tmp + '/test_save_point_marker.hdf5' + filename = tmp_path / 'test_save_point_marker.hdf5' s.save(filename) s1 = load(filename) m1 = s1.metadata.Markers.get_item(name) assert san_dict(m1._to_dictionary()) == san_dict(m._to_dictionary()) - def test_save_load_rectangle_marker(self): + def test_save_load_rectangle_marker(self, tmp_path): x1, x2, y1, y2 = 2, 4, 1, 3 color = 'yellow' linewidth = 5 @@ -612,14 +591,13 @@ def test_save_load_rectangle_marker(self): x1=x1, x2=x2, y1=y1, y2=y2, color=color, linewidth=linewidth) m.name = name s.add_marker(m, permanent=True) - with tempfile.TemporaryDirectory() as tmp: - filename = tmp + '/test_save_rectangle_marker.hdf5' + filename = tmp_path / 'test_save_rectangle_marker.hdf5' s.save(filename) s1 = load(filename) m1 = s1.metadata.Markers.get_item(name) assert san_dict(m1._to_dictionary()) == san_dict(m._to_dictionary()) - def test_save_load_text_marker(self): + def test_save_load_text_marker(self, tmp_path): x, y = 3, 9.5 color = 'brown' name = "text_test" @@ -629,22 +607,20 @@ def test_save_load_text_marker(self): x=x, y=y, text=text, color=color) m.name = name s.add_marker(m, permanent=True) - with tempfile.TemporaryDirectory() as tmp: - filename = tmp + '/test_save_text_marker.hdf5' + filename = tmp_path / 'test_save_text_marker.hdf5' s.save(filename) s1 = load(filename) m1 = s1.metadata.Markers.get_item(name) assert san_dict(m1._to_dictionary()) == san_dict(m._to_dictionary()) - def test_save_load_multidim_navigation_marker(self): + def test_save_load_multidim_navigation_marker(self, tmp_path): x, y = (1, 2, 3), (5, 6, 7) name = 'test point' s = Signal2D(np.arange(300).reshape(3, 10, 10)) m = markers.point(x=x, y=y) m.name = name s.add_marker(m, permanent=True) - with tempfile.TemporaryDirectory() as tmp: - filename = tmp + '/test_save_multidim_nav_marker.hdf5' + filename = tmp_path / 'test_save_multidim_nav_marker.hdf5' s.save(filename) s1 = load(filename) m1 = s1.metadata.Markers.get_item(name) @@ -662,10 +638,8 @@ def test_load_unknown_marker_type(self): # test_marker_bad_marker_type.hdf5 has 5 markers, # where one of them has an unknown marker type with pytest.warns(VisibleDeprecationWarning): - s = load(os.path.join( - my_path, - "hdf5_files", - "test_marker_bad_marker_type.hdf5")) + fname = my_path / "hdf5_files" / "test_marker_bad_marker_type.hdf5" + s = load(fname) assert len(s.metadata.Markers) == 4 def test_load_missing_y2_value(self): @@ -673,11 +647,9 @@ def test_load_missing_y2_value(self): # where one of them is missing the y2 value, however the # the point marker only needs the x1 and y1 value to work # so this should load + fname = my_path / "hdf5_files" / "test_marker_point_y2_data_deleted.hdf5" with pytest.warns(VisibleDeprecationWarning): - s = load(os.path.join( - my_path, - "hdf5_files", - "test_marker_point_y2_data_deleted.hdf5")) + s = load(fname) assert len(s.metadata.Markers) == 5 @@ -707,7 +679,7 @@ def test_save_ragged_array(tmp_path): def test_load_missing_extension(caplog): - path = os.path.join(my_path, "hdf5_files", "hspy_ext_missing.hspy") + path = my_path / "hdf5_files" / "hspy_ext_missing.hspy" with pytest.warns(UserWarning): s = load(path) assert "This file contains a signal provided by the hspy_ext_missing" in caplog.text