Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ngreenwald committed Aug 7, 2020
1 parent 8280190 commit 1d89216
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
41 changes: 16 additions & 25 deletions caliban_toolbox/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,19 @@ def compute_cell_size(npz_file, method='median', by_image=True):
the cell size across the entire npz is returned
Returns:
cell_sizes: list of typical cell size in NPZ
average_sizes: list of typical cell size in NPZ
Raises: ValueError if invalid method supplied
Raises: ValueError if data does have len(shape) of 4
"""

valid_methods = set(['median', 'mean'])
if method not in valid_methods:
if method.lower() not in valid_methods:
raise ValueError('Invalid method supplied: got {}, '
'method must be one of {}'.format(method, valid_methods))

# initialize variables
cell_sizes = []

if not by_image:
cell_size_acum = []

labels = npz_file['y']

if len(labels.shape) != 4:
Expand All @@ -69,32 +65,28 @@ def compute_cell_size(npz_file, method='median', by_image=True):
current_label = labels[i, :, :, 0]
area = regionprops_table(current_label.astype('int'), properties=['area'])['area']

# compute separately for each image
if by_image:
cell_sizes.append(area)

# compute for each list corresponding to each image
if by_image:
average_cell_sizes = []
for size_list in cell_sizes:
if method == 'mean':
current_cell_size = np.mean(area)
average_cell_sizes.append(np.mean(size_list))
elif method == 'median':
current_cell_size = np.median(area)
else:
raise ValueError('Invalid method supplied')

cell_sizes.append(current_cell_size)
average_cell_sizes.append(np.median(size_list))

# compute for all images at once after loop finishes
else:
cell_size_acum.append(area)

# compute summary across entire NPZ
if not by_image:
all_cell_sizes = np.concatenate(cell_size_acum)
# compute across all lists from all images
else:
all_cell_sizes = np.concatenate(cell_sizes)
if method == 'mean':
cell_sizes = [np.mean(all_cell_sizes)]
average_cell_sizes = [np.mean(all_cell_sizes)]
elif method == 'median':
cell_sizes = [np.median(all_cell_sizes)]
average_cell_sizes = [np.median(all_cell_sizes)]
else:
raise ValueError('Invalid method supplied')

return cell_sizes
return average_cell_sizes


def reshape_training_image(X_data, y_data, resize_ratio, final_size, stride_ratio):
Expand All @@ -107,7 +99,6 @@ def reshape_training_image(X_data, y_data, resize_ratio, final_size, stride_rati
final_size: the desired shape of the output image
stride_ratio: amount of overlap between crops (1 is no overlap, 0.5 is half crop size)
Returns:
reshaped_X, reshaped_y: resized and cropped version of input images
"""
Expand Down
25 changes: 21 additions & 4 deletions caliban_toolbox/build_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,33 @@ def _make_npzs(sizes, num_images):

def test_compute_cell_size():
labels = np.zeros((3, 40, 40, 1), dtype='int')

# create cell of size 10x10, 5x5, and 2x2
labels[0, :10, :10, 0] = 1
labels[0, 20:25, 20:25, 0] = 2
labels[0, 12:14, 16:18] = 3

# create cell of size 4x4, 5x5, 3x3
labels[1, :4, :4, 0] = 2
labels[1, 30:35, 35:40, 0] = 3
labels[1, 36:39, 30:33, 0] = 1

# create cell of size 9x9
labels[2, 30:39, 20:29] = 1

example_npz = {'y': labels}

cell_sizes = build.compute_cell_size(npz_file=example_npz, method='median', by_image=True)
assert np.all(cell_sizes == [25, 16, 81])
assert np.all(cell_sizes == [25, 16, 81]) # median within each individual image

cell_sizes = build.compute_cell_size(npz_file=example_npz, method='median', by_image=False)
assert cell_sizes == [25]
assert cell_sizes == [25] # median across all images

cell_sizes = build.compute_cell_size(npz_file=example_npz, method='mean', by_image=True)
assert np.all(np.round(cell_sizes, 2) == [43, 16.67, 81])
assert np.all(np.round(cell_sizes, 2) == [43, 16.67, 81]) # mean within each individual image

cell_sizes = build.compute_cell_size(npz_file=example_npz, method='mean', by_image=False)
assert np.round(cell_sizes, 2) == [37.14]
assert np.round(cell_sizes, 2) == [37.14] # mean across all images

# incorrect method
with pytest.raises(ValueError):
Expand Down Expand Up @@ -247,3 +251,16 @@ def test_combine_npz_files():

# check correct size of NPZs
assert combined_x.shape[1:3] == final_size

# mismatch between resize_ratios and npz size
with pytest.raises(ValueError):
# different resizing for each image in the NPZ
num_images = [4, 2]
sizes = [(256, 256), (256, 256)]
npz_resize_list = _make_npzs(sizes=sizes, num_images=num_images)
resize_ratios = [[1, 1, 1], [1, 2]]
final_size = (256, 256)

_, _ = build.combine_npz_files(npz_list=npz_resize_list,
resize_ratios=resize_ratios,
final_size=final_size)

0 comments on commit 1d89216

Please sign in to comment.