From ec3a64f578a68c2a2ebca52b155a67bf928c1896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Akon=20Wiik=20A=CC=8Anes?= Date: Sun, 27 Oct 2024 16:14:38 +0100 Subject: [PATCH] Fix tests with now incorrect imports and other improvements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Håkon Wiik Ånes --- tests/test_io/test_emsoft_ebsd.py | 12 +++--- tests/test_io/test_emsoft_master_pattern.py | 4 +- tests/test_io/test_io.py | 42 ++++++++++----------- tests/test_io/test_oxford_binary.py | 3 +- 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/tests/test_io/test_emsoft_ebsd.py b/tests/test_io/test_emsoft_ebsd.py index ccfa1cc3..41a7efc2 100644 --- a/tests/test_io/test_emsoft_ebsd.py +++ b/tests/test_io/test_emsoft_ebsd.py @@ -16,7 +16,7 @@ # along with kikuchipy. If not, see . import dask.array as da -from h5py import File +import h5py import numpy as np from orix.crystal_map import CrystalMap import pytest @@ -57,7 +57,7 @@ def test_read_lazy(self, emsoft_ebsd_path): def test_check_file_format(self, save_path_hdf5): """Wrong file format raises an error.""" - with File(save_path_hdf5, mode="w") as f: + with h5py.File(save_path_hdf5, mode="w") as f: g1 = f.create_group("EMheader") g2 = g1.create_group("EBSD") g2.create_dataset( @@ -68,7 +68,7 @@ def test_check_file_format(self, save_path_hdf5): def test_crystaldata2phase(self, emsoft_ebsd_path): """A Phase object is correctly returned.""" - with File(emsoft_ebsd_path / "simulated_ebsd.h5") as f: + with h5py.File(emsoft_ebsd_path / "simulated_ebsd.h5") as f: xtal_dict = _hdf5group2dict(f["CrystalData"]) phase = _crystaldata2phase(xtal_dict) @@ -85,15 +85,13 @@ def test_crystaldata2phase(self, emsoft_ebsd_path): ) assert np.allclose(structure.occupancy, [1, 1]) assert np.allclose(structure.Bisoequiv, [0.5] * 2) - assert np.compare_chararrays( - structure.element, np.array(["13", "29"], dtype="|S2"), "==", rstrip=False - ).all() + assert structure.element.tolist() == [b"13", b"29"] def test_crystaldata2phase_single_atom(self, emsoft_ebsd_path): """A Phase object is correctly returned when there is only one atom present. """ - with File(emsoft_ebsd_path / "simulated_ebsd.h5") as f: + with h5py.File(emsoft_ebsd_path / "simulated_ebsd.h5") as f: xtal_dict = _hdf5group2dict(f["CrystalData"]) xtal_dict["Natomtypes"] = 1 xtal_dict["AtomData"] = xtal_dict["AtomData"][:, 0][..., np.newaxis] diff --git a/tests/test_io/test_emsoft_master_pattern.py b/tests/test_io/test_emsoft_master_pattern.py index b6be84a1..d70c2e9b 100644 --- a/tests/test_io/test_emsoft_master_pattern.py +++ b/tests/test_io/test_emsoft_master_pattern.py @@ -121,8 +121,8 @@ def test_get_datasets( @pytest.mark.parametrize( "projection, hemisphere, error_msg", [ - ("stereographicl", "upper", "'projection' value stereographicl "), - ("lambert", "east", "'hemisphere' value east "), + ("stereographicl", "upper", "'projection' value 'stereographicl' "), + ("lambert", "east", "'hemisphere' value 'east' "), ], ) def test_get_datasets_raises( diff --git a/tests/test_io/test_io.py b/tests/test_io/test_io.py index 2db3b59a..74faea7f 100644 --- a/tests/test_io/test_io.py +++ b/tests/test_io/test_io.py @@ -22,27 +22,27 @@ import kikuchipy as kp from kikuchipy.io._io import _assign_signal_subclass, _dict2signal +from kikuchipy.io.plugins.kikuchipy_h5ebsd._api import ( + file_reader as kp_h5ebsd_file_reader, +) class TestIO: - @pytest.mark.parametrize("filename", ("im_not_here.h5", "unsupported.h4")) - def test_load(self, kikuchipy_h5ebsd_path, tmpdir, filename): - if filename == "im_not_here.h5": - with pytest.raises(IOError, match="No filename matches"): - _ = kp.load(filename) - else: - s = kp.load(kikuchipy_h5ebsd_path / "patterns.h5") - file_path = tmpdir / "supported.h5" - s.save(file_path) - new_file_path = tmpdir / filename - file_path.rename(new_file_path) - with pytest.raises(IOError, match="Could not read"): - _ = kp.load(new_file_path) + def test_load_unsupported_extension(self, kikuchipy_h5ebsd_path, tmpdir): + s = kp.load(kikuchipy_h5ebsd_path / "patterns.h5") + file_path = tmpdir / "supported.h5" + s.save(file_path) + new_file_path = tmpdir / "unsupported.h4" + file_path.rename(new_file_path) + with pytest.raises(IOError, match="Could not read"): + kp.load(new_file_path) + + def test_load_missing_file(self, kikuchipy_h5ebsd_path, tmpdir): + with pytest.raises(IOError, match="No filename matches"): + kp.load("im_not_here.h5") def test_dict2signal(self, kikuchipy_h5ebsd_path): - scan_dict = kp.io.plugins._api.file_reader( - kikuchipy_h5ebsd_path / "patterns.h5" - )[0] + scan_dict, *_ = kp_h5ebsd_file_reader(kikuchipy_h5ebsd_path / "patterns.h5") scan_dict["metadata"]["Signal"]["record_by"] = "not-image" with pytest.raises(ValueError, match="kikuchipy only supports"): _ = _dict2signal(scan_dict) @@ -105,12 +105,10 @@ def test_save_extensions(self, kikuchipy_h5ebsd_path, extension, tmpdir): with pytest.raises(ValueError, match="'h4' does not"): s.save(file_path) - @pytest.mark.filterwarnings("ignore:Using `set_signal_dimension`") - def test_save_data_dimensions(self, kikuchipy_h5ebsd_path): - s = kp.load(kikuchipy_h5ebsd_path / "patterns.h5") - s.axes_manager.set_signal_dimension(3) - with pytest.raises(ValueError, match="This file format cannot write"): - s.save() + def test_save_data_dimensions(self, tmpdir): + s = kp.signals.EBSD(np.zeros((2, 3, 4, 5, 6))) + with pytest.raises(ValueError, match="Chosen IO plugin 'kikuchipy_h5ebsd' "): + s.save(tmpdir / "test.h5") def test_save_to_existing_file(self, save_path_hdf5, kikuchipy_h5ebsd_path): s = kp.load(kikuchipy_h5ebsd_path / "patterns.h5") diff --git a/tests/test_io/test_oxford_binary.py b/tests/test_io/test_oxford_binary.py index 77135cf1..7e74b88e 100644 --- a/tests/test_io/test_oxford_binary.py +++ b/tests/test_io/test_oxford_binary.py @@ -20,6 +20,7 @@ import pytest import kikuchipy as kp +from kikuchipy.io.plugins.oxford_binary._api import OxfordBinaryFileReader class TestOxfordBinaryReader: @@ -110,5 +111,5 @@ def test_guess_number_of_patterns(self, oxford_binary_file, n_patterns): the file works. """ with open(oxford_binary_file.name, mode="rb") as f: - fox = kp.io.plugins._api.OxfordBinaryFileReader(f) + fox = OxfordBinaryFileReader(f) assert fox.n_patterns == n_patterns