Skip to content

Commit 306f0e7

Browse files
authored
PET: Add subset algebra test (#1342)
* add algebra unit test for STIR AcquisitionData subsets for storage scheme memory and file. * added to changes * removed commented out code
1 parent 63e9996 commit 306f0e7

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
* CI
55
- made tests return value handling compatible with a future version of pytest.
6+
- added algebraic tests for STIR AcquisitionData subsets.
67

78
* SIRF/STIR
89
- `ScatterEstimation` has extra methods that allow setting masks for the tail-fitting.

src/xSTIR/pSTIR/tests/test_algebra.py

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import os
2121
import unittest
22+
import inspect
23+
import functools
2224
import sirf.STIR as pet
2325
from sirf.Utilities import examples_data_path, DataContainerAlgebraTests
2426

@@ -47,7 +49,6 @@ def setUp(self):
4749
template = pet.AcquisitionData(path)
4850
self.image1 = template.get_uniform_copy(0)
4951
self.image2 = template.get_uniform_copy(0)
50-
# assert False
5152
self.set_storage_scheme()
5253

5354

@@ -57,10 +58,6 @@ def tearDown(self):
5758
def set_storage_scheme(self):
5859
pet.AcquisitionData.set_storage_scheme('file')
5960

60-
def test_division_by_datacontainer_zero(self):
61-
# skip this test as currently cSIRF doesn't throw
62-
pass
63-
6461

6562
class TestSTIRAcquisitionDataAlgebraMemory(unittest.TestCase, DataContainerAlgebraTests):
6663
def setUp(self):
@@ -71,8 +68,53 @@ def setUp(self):
7168
template = pet.AcquisitionData(path)
7269
self.image1 = template.get_uniform_copy(0)
7370
self.image2 = template.get_uniform_copy(0)
74-
# assert False
7571
pet.AcquisitionData.set_storage_scheme('memory')
76-
def test_division_by_datacontainer_zero(self):
77-
# skip this test as currently cSIRF doesn't throw
78-
pass
72+
73+
class TestSTIRAcquisitionDataSubsetAlgebra():
74+
def setUp(self):
75+
path = os.path.join(
76+
examples_data_path('PET'), 'thorax_single_slice', 'template_sinogram.hs')
77+
if os.path.exists(path):
78+
template = pet.AcquisitionData(path)
79+
prompts = template.get_uniform_copy(0)
80+
# create a staggered list of views for 2 subsets
81+
views = prompts.dimensions()[2]
82+
indices = list(range(views))
83+
num_batches = 2
84+
batches = [indices[i::num_batches] for i in range(num_batches)]
85+
86+
self.image1 = prompts.get_subset(batches[0])
87+
self.image2 = prompts.get_subset(batches[0])
88+
89+
90+
class TestSTIRAcquisitionDataSubsetAlgebraMemory(TestSTIRAcquisitionDataSubsetAlgebra, unittest.TestCase, DataContainerAlgebraTests):
91+
def setUp(self):
92+
pet.AcquisitionData.set_storage_scheme('memory')
93+
super().setUp()
94+
95+
96+
class TestSTIRAcquisitionDataSubsetAlgebraFile(TestSTIRAcquisitionDataSubsetAlgebra, unittest.TestCase, DataContainerAlgebraTests):
97+
def setUp(self):
98+
pet.AcquisitionData.set_storage_scheme('file')
99+
super().setUp()
100+
101+
def __init__(self, *args, **kwargs):
102+
super().__init__(*args, **kwargs)
103+
# wrap all test methods that would raise exception as
104+
# storage_scheme file does not work
105+
a = inspect.getmembers(DataContainerAlgebraTests, predicate=inspect.isfunction)
106+
def wrapper(self, func):
107+
try:
108+
func(self)
109+
except Exception as e:
110+
print (f"Caught exception in {func.__name__}: {e}")
111+
return
112+
raise AssertionError(f"Expected exception in {func.__name__} but none was raised.")
113+
114+
for m in a:
115+
# test_division_by_datacontainer_zero and test_division_by_scalar_zero
116+
# do not raise exception, they just assert True. Skip them.
117+
if m[0] not in ["test_division_by_datacontainer_zero", "test_division_by_scalar_zero"]:
118+
self.__setattr__(m[0], functools.partial(wrapper, self, m[1]))
119+
120+
self.__setattr__('setUp', functools.partial(wrapper, self, self.setUp))

0 commit comments

Comments
 (0)