|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from utils import load_operator_class, load_operator_module |
| 4 | + |
| 5 | +from tomviz.external_dataset import Dataset |
| 6 | + |
| 7 | + |
| 8 | +def test_multi_arrays(hxn_xrf_example_dataset: Dataset): |
| 9 | + dataset = hxn_xrf_example_dataset |
| 10 | + |
| 11 | + # Test a few operators that use `@apply_to_each_array`, and verify |
| 12 | + # that they do indeed apply to each array. |
| 13 | + # Load the operator module |
| 14 | + bin_module = load_operator_module('BinTiltSeriesByTwo') |
| 15 | + |
| 16 | + # Verify the tilt angles won't change, but image shape decreases by 2x |
| 17 | + orig_shapes = {k: v.shape for k, v in dataset.arrays.items()} |
| 18 | + orig_tilt_angles = dataset.tilt_angles |
| 19 | + |
| 20 | + # Apply the transformation |
| 21 | + bin_module.transform(dataset) |
| 22 | + |
| 23 | + # New shapes should be binned by 2 in x and y, but not z |
| 24 | + # Tilt angles should be unaffected |
| 25 | + binned_shapes = {k: v.shape for k, v in dataset.arrays.items()} |
| 26 | + expected_shapes = {k: (shape[0] // 2, shape[1] // 2, shape[2]) |
| 27 | + for k, shape in orig_shapes.items()} |
| 28 | + |
| 29 | + assert binned_shapes == expected_shapes |
| 30 | + assert np.allclose(orig_tilt_angles, dataset.tilt_angles) |
| 31 | + |
| 32 | + # Delete 3 slices. This will delete tilt angles too. |
| 33 | + delete_slices_module = load_operator_module('DeleteSlices') |
| 34 | + |
| 35 | + first_slice = 3 |
| 36 | + last_slice = 5 |
| 37 | + num_deleted = last_slice - first_slice + 1 |
| 38 | + delete_slices_module.transform(dataset, |
| 39 | + firstSlice=first_slice, |
| 40 | + lastSlice=last_slice, |
| 41 | + axis=2) |
| 42 | + |
| 43 | + # New shapes should be the same, other than 3 less in Z |
| 44 | + expected_shapes = {k: (shape[0], shape[1], shape[2] - num_deleted) |
| 45 | + for k, shape in binned_shapes.items()} |
| 46 | + deleted_shapes = {k: v.shape for k, v in dataset.arrays.items()} |
| 47 | + |
| 48 | + expected_angles = np.hstack((orig_tilt_angles[:first_slice], |
| 49 | + orig_tilt_angles[last_slice + 1:])) |
| 50 | + |
| 51 | + assert deleted_shapes == expected_shapes |
| 52 | + assert np.allclose(dataset.tilt_angles, expected_angles) |
| 53 | + |
| 54 | + # Now run a reconstruction and verify we get all results |
| 55 | + recon_sirt_module = load_operator_module('Recon_SIRT') |
| 56 | + operator = load_operator_class(recon_sirt_module) |
| 57 | + |
| 58 | + results = operator.transform(dataset, Niter=2) |
| 59 | + |
| 60 | + recon_dataset = results['reconstruction'] |
| 61 | + |
| 62 | + # We should have the same scalar names |
| 63 | + assert sorted(recon_dataset.scalars_names) == sorted(dataset.scalars_names) |
| 64 | + |
| 65 | + # Verify the output appears valid. We are not testing the actual |
| 66 | + # reconstruction operator here, just that it ran on multiple arrays. |
| 67 | + means = [np.mean(array) for array in recon_dataset.arrays.values()] |
| 68 | + assert np.all(np.array(means) > 0) |
0 commit comments