Skip to content

Commit

Permalink
Testing:Expand Test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
CSSFrancis committed Oct 7, 2024
1 parent 4c7d887 commit 55adc6a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
31 changes: 19 additions & 12 deletions rsciio/dm5/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,6 @@ def __init__(self, image_group, tags=None, unique_id=None, file=None):
self.unique_id = image_group["UniqueID"]
self.file = file

def __str__(self):
return f"Image: {self.image_data['Data'].shape}"

@property
def ndim(self):
return len(self.image_data["Data"].shape)
Expand Down Expand Up @@ -380,9 +377,6 @@ def signal_dimensions(self):
"Meta Data.Format attribute in the ImageTags group."
)

def navigation_dimensions(self):
return self.ndim - self.signal_dimensions()

def get_axis_dict(self, axis):
"""
Get the calibration data for a given axis.
Expand Down Expand Up @@ -596,13 +590,26 @@ def update_metadata(
dict2group(formatted_metadata, self.image_tags)

# Update Microscope Info
if "Acquisition_instrument" in metadata and "TEM" in metadata["Acquisition_instrument"]:
if (
"Acquisition_instrument" in metadata
and "TEM" in metadata["Acquisition_instrument"]
):
self.image_tags.create_group("Microscope Info")
microscope_info_dict = {"Voltage": metadata["Acquisition_instrument"]["TEM"].get("beam_energy",0) * 1000,
"Illumination Mode": metadata["Acquisition_instrument"]["TEM"].get("acquisition_mode", "Unknown"),
"Indicated Magnification": metadata["Acquisition_instrument"]["TEM"].get("magnification", 0),
"STEM Camera Length": metadata["Acquisition_instrument"]["TEM"].get("camera_length", 0)}

microscope_info_dict = {
"Voltage": metadata["Acquisition_instrument"]["TEM"].get(
"beam_energy", 0
)
* 1000,
"Illumination Mode": metadata["Acquisition_instrument"]["TEM"].get(
"acquisition_mode", "Unknown"
),
"Indicated Magnification": metadata["Acquisition_instrument"][
"TEM"
].get("magnification", 0),
"STEM Camera Length": metadata["Acquisition_instrument"]["TEM"].get(
"camera_length", 0
),
}

dict2group(microscope_info_dict, self.image_tags["Microscope Info"])

Expand Down
30 changes: 18 additions & 12 deletions rsciio/tests/test_dm5.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from pathlib import Path

import dask.array as da
import numpy as np
import pytest

Expand Down Expand Up @@ -84,10 +85,8 @@ def test_save_load_files(
assert s.axes_manager[i].scale == 0.1
assert s.axes_manager[i].size == int(original[i][-2:])

def test_save_load_undefined_axes(
self, tmp_path
):
fname = tmp_path/ f"test_save_undefined.dm5"
def test_save_load_undefined_axes(self, tmp_path):
fname = tmp_path / "test_save_undefined.dm5"

data_shape = [10, 11, 12, 13]
data = np.ones(data_shape, dtype=np.float32)
Expand All @@ -98,23 +97,30 @@ def test_save_load_undefined_axes(
assert s.axes_manager[i].name == ""
assert s.axes_manager[i].units == ""

def test_save_load_metadata(
self, tmp_path
):
fname = tmp_path/ f"test_save_undefined.dm5"
def test_save_load_metadata(self, tmp_path):
fname = tmp_path / "test_save_undefined.dm5"

data_shape = [10, 11, 12, 13]
data = np.ones(data_shape, dtype=np.float32)
signal = hs.signals.Signal2D(data)
signal.metadata.General.title = "test"
signal.metadata.add_node("Acquisition_instrument.TEM")
signal.metadata.Acquisition_instrument.TEM.beam_energy = 200
signal.metadata.Acquisition_instrument.TEM.beam_energy = 200
signal.metadata.Acquisition_instrument.TEM.magnification = 100
signal.metadata.Acquisition_instrument.TEM.camera_length = 10

signal.save(fname, overwrite=True)
s = hs.load(fname)
assert s.metadata.Acquisition_instrument.TEM.beam_energy == 200
assert s.metadata.Acquisition_instrument.TEM.camera_length == 10
assert s.metadata.Acquisition_instrument.TEM.magnification == 100
assert s.metadata.Acquisition_instrument.TEM.beam_energy == 200
assert s.metadata.Acquisition_instrument.TEM.camera_length == 10
assert s.metadata.Acquisition_instrument.TEM.magnification == 100

def test_save_load_lazy(self, tmp_path):
fname = tmp_path / "test_save_lazy.dm5"

data_shape = [10, 11, 12, 13]
data = np.ones(data_shape, dtype=np.float32)
signal = hs.signals.Signal2D(data)
signal.save(fname, overwrite=True)
s = hs.load(fname, lazy=True)
assert isinstance(s.data, da.Array)

0 comments on commit 55adc6a

Please sign in to comment.