Skip to content

Commit ef0d287

Browse files
authored
allows user to specify polar_angle direction (#37)
1 parent b0eaeb0 commit ef0d287

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

TESTS/unitTests.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,27 @@ def test10(self):
10691069
res = pt.synthetic_images.square_wave(20, frequency=(1,2), amplitude=3.2, phase=-2, origin=(2,3), twidth=.55)
10701070
self.assertTrue(pt.compareRecon(matPyr['res'], res))
10711071

1072+
class polarAngleTests(unittest.TestCase):
1073+
def test0(self):
1074+
ang = pt.synthetic_images.polar_angle(100, direction='clockwise')
1075+
idx = np.argmin((ang - np.pi/2)**2)
1076+
# check that pi/2 is in the bottom half of the image
1077+
self.assertTrue(np.unravel_index(idx, (100, 100))[0] > 50)
1078+
idx = np.argmin((ang + np.pi/2)**2)
1079+
# check that -pi/2 is in the top half of the image
1080+
self.assertTrue(np.unravel_index(idx, (100, 100))[0] < 50)
1081+
def test1(self):
1082+
ang = pt.synthetic_images.polar_angle(100, direction='counter-clockwise')
1083+
idx = np.argmin((ang - np.pi/2)**2)
1084+
# check that pi/2 is in the top half of the image
1085+
self.assertTrue(np.unravel_index(idx, (100, 100))[0] < 50)
1086+
idx = np.argmin((ang + np.pi/2)**2)
1087+
# check that -pi/2 is in the bottom half of the image
1088+
self.assertTrue(np.unravel_index(idx, (100, 100))[0] > 50)
1089+
def test2(self):
1090+
with self.assertRaises(ValueError):
1091+
pt.synthetic_images.polar_angle(100, direction='-clockwise')
1092+
10721093
# TODO
10731094

10741095
# python version of histo

src/pyrtools/tools/synthetic_images.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ def polar_radius(size, exponent=1, origin=None):
136136
return res
137137

138138

139-
def polar_angle(size, phase=0, origin=None):
139+
def polar_angle(size, phase=0, origin=None, direction='clockwise'):
140140
'''make polar angle matrix (in radians)
141141
142-
Compute a matrix of given size containing samples of the polar angle (in radians, CW from the
143-
X-axis, ranging from -pi to pi), relative to given phase, about the given origin pixel.
142+
Compute a matrix of given size containing samples of the polar angle (in radians,
143+
increasing in user-defined direction from the X-axis, ranging from -pi to pi), relative to
144+
given phase, about the given origin pixel.
144145
145146
Arguments
146147
---------
@@ -153,13 +154,20 @@ def polar_angle(size, phase=0, origin=None):
153154
the center of the image. if an int, we assume the origin is at `(origin, origin)`. if a
154155
tuple, must be a 2-tuple of ints specifying the origin (where `(0, 0)` is the upper left).
155156
if None, we assume the origin lies at the center of the matrix, `(size+1)/2`.
157+
direction : {'clockwise', 'counter-clockwise'}
158+
Whether the angle increases in a clockwise or counter-clockwise direction from
159+
the x-axis. The standard mathematical convention is to increase
160+
counter-clockwise, so that 90 degrees corresponds to the positive y-axis.
156161
157162
Returns
158163
-------
159164
res : `np.array`
160165
the polar angle matrix
161166
162167
'''
168+
if direction not in ['clockwise', 'counter-clockwise']:
169+
raise ValueError("direction must be one of {'clockwise', 'counter-clockwise'}, "
170+
f"but received {direction}!")
163171
if not hasattr(size, '__iter__'):
164172
size = (size, size)
165173

@@ -172,6 +180,8 @@ def polar_angle(size, phase=0, origin=None):
172180
np.arange(1, size[0]+1)-origin[0])
173181
xramp = np.array(xramp)
174182
yramp = np.array(yramp)
183+
if direction == 'counter-clockwise':
184+
yramp = np.flip(yramp, 0)
175185

176186
res = np.arctan2(yramp, xramp)
177187

0 commit comments

Comments
 (0)