Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests #3

Merged
merged 5 commits into from
Apr 27, 2015
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 98 additions & 3 deletions skxray/roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
########################################################################

"""
This module is to get informations of different region of interests(roi's).
Information : the number of pixels, pixel indices, indices
This module is to get information of different region of interests(roi's).
Information : pixel indices, indices array
"""


Expand All @@ -62,6 +62,8 @@

def rectangles(coords, shape):
"""
This function wil provide the indices array for rectangle region of interests.

Parameters
----------
coords : iterable
Expand Down Expand Up @@ -214,7 +216,7 @@ def ring_edges(inner_radius, width, spacing=0, num_rings=None):
if num_rings != len(width):
raise ValueError("num_rings does not match width list")
if spacing_is_list:
if num_rings != len(spacing):
if num_rings-1 != len(spacing):
raise ValueError("num_rings does not match spacing list")

# Now regularlize the input.
Expand All @@ -229,3 +231,96 @@ def ring_edges(inner_radius, width, spacing=0, num_rings=None):
edges = np.cumsum(steps).reshape(-1, 2)

return edges


def pie_slices(edges, slicing, center, shape, theta=0):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielballan I added a pie_slices function and it draw the mask we required. I will add some tests to it

"""
Parameters
----------
edges : array
inner and outer radius for each ring

slicing : int or list
number of pie slices or list of angles in degrees

center : rr, cc : tuple
point in image where r=0; may be a float giving subpixel precision

shape: rr, cc: tuple
Image shape which is used to determine the maximum extent of output
pixel coordinates.

theta : float, optional
angle in degrees

Returns
-------
label_array : array
Elements not inside any ROI are zero; elements inside each
ROI are 1, 2, 3, corresponding to the order they are specified
in edges and slicing

"""
angle_grid = get_angle_grid(shape, center)

slicing_is_list = isinstance(slicing, collections.Iterable)
# required angles

if slicing_is_list:
slicing = np.asarray(slicing) + theta
else:
slicing = np.linspace(0, 360, slicing) + theta

# the indices of the bins(angles) to which each value in input
# array(angle_grid) belongs.
ind_grid = (np.digitize(np.ravel(angle_grid), slicing,
right=False)).reshape(shape)

label_array = np.zeros(shape, dtype=np.int64)
# radius grid for the image_shape
grid_values = core.pixel_to_radius(shape, center)

# assign indices value according to angles then rings
for r in range(len(edges)):
vl = (edges[r][0] <= grid_values) & (grid_values
< edges[r][1])
label_array[vl] = ind_grid[vl] + len(slicing)*(r)

return label_array


def get_angle_grid(shape, center):
"""
This function will provide angle values for the whole grid
from the calibrated center

Parameters
----------
shape : tuple
shape of the image (detector X and Y direction)
Order is (num_rows, num_columns)

center : rr, cc : tuple
point in image where r=0; may be a float giving
subpixel precision

pixel_size : tuple, optional
The size of a pixel (really the pitch) in real units. (height, width).
Defaults to 1 pixel/pixel if not specified.

Returns
-------
angle_grid : array
angle values from the calibrated center
shape image_shape

"""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sameera2004 and I benchmarked this against trackpy's theta_mask (https://github.com/soft-matter/trackpy/blob/master/trackpy/masks.py#L40) and found theta_mask to be 30X times faster. Note that theta_mask assumes r=0 is in the center of the image, which may be the source of perf gains.

X, Y = np.meshgrid((np.arange(shape[1]) -
center[1]),
(np.arange(shape[0]) -
center[0]))
angle_grid = np.rad2deg(np.arctan2(Y, X))

angle_grid[angle_grid < 0] = 360 + angle_grid[angle_grid < 0]

return angle_grid
58 changes: 51 additions & 7 deletions skxray/tests/test_roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import numpy.testing as npt


def test_roi_rectangles():
def test_rectangles():
shape = (15, 26)
roi_data = np.array(([2, 2, 6, 3], [6, 7, 8, 5], [8, 18, 5, 10]),
dtype=np.int64)
Expand All @@ -80,17 +80,41 @@ def test_roi_rectangles():
assert_almost_equal(bottom-1, ind_co[-1][-1])


def test_roi_rings():
calibrated_center = (100, 100)
def test_rings():
calibrated_center = (100., 100.)
img_dim = (200, 205)
first_q = 2
delta_q = 3
first_q = 10.
delta_q = 5.
num_rings = 7 # number of Q rings
one_step_q = 5.0
step_q = [2.5, 3.0, 5.8]

# test when there is same spacing between rings
edges = roi.ring_edges(first_q, width=delta_q, spacing=one_step_q,
num_rings=num_rings)
print("edges there is same spacing between rings ", edges)
label_array = roi.rings(edges, calibrated_center, img_dim)
print("label_array there is same spacing between rings", label_array)

# test when there is same spacing between rings
edges = roi.ring_edges(first_q, width=delta_q, spacing=2.5,
num_rings=num_rings)
print("edges there is same spacing between rings ", edges)
label_array = roi.rings(edges, calibrated_center, img_dim)
print("label_array there is same spacing between rings", label_array)

# test when there is different spacing between rings
edges = roi.ring_edges(first_q, width=delta_q, spacing=step_q,
num_rings=4)
print("edges when there is different spacing between rings", edges)
label_array = roi.rings(edges, calibrated_center, img_dim)
print("label_array there is different spacing between rings", label_array)

# test when there is no spacing between rings
edges = roi.ring_edges(first_q, width=delta_q, num_rings=num_rings)
print(edges)
print("edges", edges)
label_array = roi.rings(edges, calibrated_center, img_dim)
print(label_array)
print("label_array", label_array)

# Did we draw the right number of rings?
print(np.unique(label_array))
Expand Down Expand Up @@ -147,3 +171,23 @@ def _helper_check(pixel_list, inds, num_pix, q_ring_val, calib_center,
- 0.000001)]]))[0][0]))
assert_array_equal(zero_grid, data)
assert_array_equal(num_pix, num_pixels)


def test_pie_slices():
calibrated_center = (20., 25.)
img_dim = (100, 80)
first_q = 5.
delta_q = 5.
num_rings = 4 # number of Q rings
slicing = 4

edges = roi.ring_edges(first_q, width=delta_q, spacing=4,
num_rings=num_rings)
label_array = roi.pie_slices(edges, slicing, calibrated_center, img_dim,
theta=0)



if __name__ == "__main__":
import matplotlib.pyplot as plt
test_pie_slices()