Skip to content

Commit

Permalink
Fix tests with now incorrect imports and other improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Håkon Wiik Ånes <[email protected]>
  • Loading branch information
hakonanes committed Oct 27, 2024
1 parent f991c00 commit ec3a64f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
12 changes: 5 additions & 7 deletions tests/test_io/test_emsoft_ebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# along with kikuchipy. If not, see <http://www.gnu.org/licenses/>.

import dask.array as da
from h5py import File
import h5py
import numpy as np
from orix.crystal_map import CrystalMap
import pytest
Expand Down Expand Up @@ -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(
Expand All @@ -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)

Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_io/test_emsoft_master_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
42 changes: 20 additions & 22 deletions tests/test_io/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion tests/test_io/test_oxford_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import pytest

import kikuchipy as kp
from kikuchipy.io.plugins.oxford_binary._api import OxfordBinaryFileReader


class TestOxfordBinaryReader:
Expand Down Expand Up @@ -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

0 comments on commit ec3a64f

Please sign in to comment.