Skip to content

Commit

Permalink
put rotation angle and image key in processed data group
Browse files Browse the repository at this point in the history
  • Loading branch information
DolicaAkelloEgwel committed Mar 20, 2023
1 parent f4477fe commit 4ff6b6e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 6 additions & 3 deletions mantidimaging/core/io/saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ def _nexus_save(nexus_file: h5py.File, dataset: StrictDataset, sample_name: str)
_set_nx_class(detector, "NXdetector")
detector.create_dataset("image_key", data=dataset.image_keys)

_save_processed_data_to_nexus(nexus_file, dataset)

# sample field
sample_group = tomo_entry.create_group("sample")
_set_nx_class(sample_group, "NXsample")
Expand All @@ -234,6 +232,8 @@ def _nexus_save(nexus_file: h5py.File, dataset: StrictDataset, sample_name: str)
rotation_angle = sample_group.create_dataset("rotation_angle", data=np.concatenate(dataset.nexus_rotation_angles))
rotation_angle.attrs["units"] = "rad"

_save_processed_data_to_nexus(nexus_file, dataset, rotation_angle, detector["image_key"])

# data field
data = tomo_entry.create_group("data")
_set_nx_class(data, "NXdata")
Expand All @@ -245,8 +245,11 @@ def _nexus_save(nexus_file: h5py.File, dataset: StrictDataset, sample_name: str)
_save_recon_to_nexus(nexus_file, recon, dataset.sample.filenames[0])


def _save_processed_data_to_nexus(nexus_file: h5py.File, dataset: StrictDataset):
def _save_processed_data_to_nexus(nexus_file: h5py.File, dataset: StrictDataset, rotation_angle: h5py.Group,
image_key: h5py.Dataset):
data = nexus_file.create_group("processed-data")
data["rotation_angle"] = rotation_angle
data["image_key"] = image_key
_set_nx_class(data, "NXdata")
combined_data_shape = (sum([len(arr) for arr in dataset.nexus_arrays]), ) + dataset.nexus_arrays[0].shape[1:]
data.create_dataset("data", shape=combined_data_shape, dtype="float32")
Expand Down
8 changes: 5 additions & 3 deletions mantidimaging/core/io/test/io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,7 @@ def test_nexus_missing_projection_angles_save_as_zeros(self):
# test rotation angle links
self.assertEqual(tomo_entry["data"]["rotation_angle"], rotation_angle_entry)

@staticmethod
def test_nexus_complex_dataset_save():
def test_nexus_complex_dataset_save(self):
image_stacks = []
for _ in range(5):
image_stack = th.generate_images()
Expand All @@ -278,12 +277,12 @@ def test_nexus_complex_dataset_save():
saver._nexus_save(nexus_file, sd, "sample-name")
tomo_entry = nexus_file["entry1"]["tomo_entry"]

# test instrument field
npt.assert_array_equal(
np.array(nexus_file["processed-data"]["data"]),
np.concatenate(
[sd.dark_before.data, sd.flat_before.data, sd.sample.data, sd.flat_after.data,
sd.dark_after.data]).astype("float32"))
# test instrument field
npt.assert_array_equal(
np.array(tomo_entry["instrument"]["detector"]["image_key"]),
[2 for _ in range(sd.dark_before.data.shape[0])] + [1 for _ in range(sd.flat_before.data.shape[0])] +
Expand All @@ -293,6 +292,9 @@ def test_nexus_complex_dataset_save():
# test instrument/sample fields
npt.assert_array_equal(np.array(tomo_entry["sample"]["rotation_angle"]),
np.concatenate([images.projection_angles().value for images in image_stacks]))
self.assertEqual(nexus_file["processed-data"]["rotation_angle"], tomo_entry["sample"]["rotation_angle"])
self.assertEqual(nexus_file["processed-data"]["image_key"],
tomo_entry["instrument"]["detector"]["image_key"])

@mock.patch("mantidimaging.core.io.saver.h5py.File")
@mock.patch("mantidimaging.core.io.saver._nexus_save")
Expand Down

0 comments on commit 4ff6b6e

Please sign in to comment.