Skip to content

Commit 7566f69

Browse files
committed
adds basic APR and IO tests
1 parent 4239084 commit 7566f69

File tree

2 files changed

+86
-9
lines changed

2 files changed

+86
-9
lines changed

pyapr/converter/converter_methods.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ def get_apr(image, rel_error=0.1, gradient_smoothing=2, verbose=True, params=Non
2121
else:
2222
par = params
2323

24-
if image.dtype in ('float', 'float32'):
24+
if image.dtype == np.float32:
2525
parts = pyapr.FloatParticles()
2626
converter = pyapr.converter.FloatConverter()
27-
elif image.dtype in ('short', 'uint16'):
27+
elif image.dtype == np.uint16:
2828
parts = pyapr.ShortParticles()
2929
converter = pyapr.converter.ShortConverter()
3030
# elif image.dtype in {'byte', 'uint8'}: # currently not working
3131
# parts = pyapr.ByteParticles()
3232
# converter = pyapr.converter.ByteConverter()
3333
else:
34-
errstr = 'get_apr_interactive image dtype must be one of (float, float32, short, uint16), ' \
34+
errstr = 'pyapr.converter.get_apr: input image dtype must be numpy.uint16 or numpy.float32, ' \
3535
'but {} was given'.format(image.dtype)
3636
raise TypeError(errstr)
3737

@@ -68,17 +68,17 @@ def get_apr_interactive(image, rel_error=0.1, gradient_smoothing=2, verbose=True
6868
else:
6969
par = params
7070

71-
if image.dtype in ('float', 'float32'):
71+
if image.dtype == np.float32:
7272
parts = pyapr.FloatParticles()
7373
converter = pyapr.converter.FloatConverter()
74-
elif image.dtype in ('short', 'uint16'):
74+
elif image.dtype == np.uint16:
7575
parts = pyapr.ShortParticles()
7676
converter = pyapr.converter.ShortConverter()
7777
# elif image.dtype in {'byte', 'uint8'}: # currently not working
7878
# parts = pyapr.ByteParticles()
7979
# converter = pyapr.converter.ByteConverter()
8080
else:
81-
errstr = 'pyapr.converter.get_apr_interactive: image dtype must be one of (float, float32, short, uint16), ' \
81+
errstr = 'pyapr.converter.get_apr_interactive: input image dtype must be numpy.uint16 or numpy.float32, ' \
8282
'but {} was given'.format(image.dtype)
8383
raise TypeError(errstr)
8484

@@ -122,14 +122,14 @@ def find_parameters_interactive(image, rel_error=0.1, gradient_smoothing=0, verb
122122
else:
123123
par = params
124124

125-
if image.dtype in ('float', 'float32'):
125+
if image.dtype == np.float32:
126126
converter = pyapr.converter.FloatConverter()
127-
elif image.dtype in ('short', 'uint16'):
127+
elif image.dtype == np.uint16:
128128
converter = pyapr.converter.ShortConverter()
129129
# elif image.dtype in {'byte', 'uint8'}: # currently not working
130130
# converter = pyapr.converter.ByteConverter()
131131
else:
132-
errstr = 'find_parameters_interactive image.dtype must be one of (float, float32, short, uint16), ' \
132+
errstr = 'pyapr.converter.find_parameters_interactive: input image dtype must be numpy.uint16 or numpy.float32, ' \
133133
'but {} was given'.format(image.dtype)
134134
raise TypeError(errstr)
135135

pyapr/tests/testAPR.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import unittest
2+
import pyapr
3+
from skimage import io as skio
4+
import numpy as np
5+
import os
6+
7+
8+
class BasicTests(unittest.TestCase):
9+
10+
def setUp(self):
11+
self.this_dir = os.path.dirname(os.path.abspath(__file__))
12+
self.impath_3D = os.path.abspath(os.path.join(self.this_dir, '../../LibAPR/test/files/Apr/sphere_120/sphere_original.tif'))
13+
self.impath_2D = os.path.abspath(os.path.join(self.this_dir, '../../LibAPR/test/files/Apr/sphere_2D/original.tif'))
14+
self.impath_1D = os.path.abspath(os.path.join(self.this_dir, '../../LibAPR/test/files/Apr/sphere_1D/original.tif'))
15+
16+
self.apr_parameters = pyapr.APRParameters()
17+
self.apr_parameters.sigma_th = 30
18+
self.apr_parameters.grad_th = 15
19+
20+
def __convert_image(self, impath, dtype):
21+
img = skio.imread(impath).astype(dtype)
22+
apr, parts = pyapr.converter.get_apr(img, verbose=False, params=self.apr_parameters)
23+
24+
# TODO: use output_steps flag and check reconstruction condition
25+
26+
self.assertGreater(apr.computational_ratio(), 1)
27+
self.assertGreater(len(parts), 1)
28+
29+
def test_get_apr(self):
30+
self.__convert_image(self.impath_1D, np.uint16)
31+
self.__convert_image(self.impath_2D, np.uint16)
32+
self.__convert_image(self.impath_3D, np.uint16)
33+
34+
self.__convert_image(self.impath_1D, np.float32)
35+
self.__convert_image(self.impath_2D, np.float32)
36+
self.__convert_image(self.impath_3D, np.float32)
37+
38+
with self.assertRaises(TypeError):
39+
self.__convert_image(self.impath_1D, np.float64)
40+
41+
with self.assertRaises(TypeError):
42+
self.__convert_image(self.impath_1D, np.uint8)
43+
44+
with self.assertRaises(TypeError):
45+
self.__convert_image(self.impath_1D, int)
46+
47+
def test_io(self):
48+
49+
# writes files to this path, later removes the file # TODO: find a better way to do this
50+
fpath = os.path.join(self.this_dir, 'temporary_apr_file.apr')
51+
52+
for impath in (self.impath_1D, self.impath_2D, self.impath_3D):
53+
img = skio.imread(impath)
54+
apr, parts = pyapr.converter.get_apr(img, verbose=False, params=self.apr_parameters)
55+
56+
pyapr.io.write(fpath, apr, parts)
57+
58+
apr2 = pyapr.APR()
59+
parts2 = pyapr.ShortParticles()
60+
61+
pyapr.io.read(fpath, apr2, parts2)
62+
63+
self.assertEqual(apr.org_dims(), apr2.org_dims())
64+
self.assertEqual(apr.total_number_particles(), apr2.total_number_particles())
65+
self.assertEqual(apr.total_number_particles(), len(parts2))
66+
67+
self.assertTrue(
68+
np.alltrue(
69+
np.array(parts, copy=False) == np.array(parts2, copy=False)
70+
)
71+
)
72+
73+
os.remove(fpath) # remove temporary file
74+
75+
76+
if __name__ == '__main__':
77+
unittest.main()

0 commit comments

Comments
 (0)