1919
2020import os
2121import unittest
22+ import inspect
23+ import functools
2224import sirf .STIR as pet
2325from 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
6562class 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