Skip to content

Commit

Permalink
pytest-flakes (fixes #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x00b1 committed Oct 7, 2015
1 parent ea379ec commit 5a4c610
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 123 deletions.
4 changes: 1 addition & 3 deletions centrosome/bg_compensate.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ def spline_matrix2d(x,y,px,py,mask=None):
'''For boundary constraints, the first two and last two spline pieces are constrained
to be part of the same cubic curve.'''
V = np.kron(spline_matrix(x,px),spline_matrix(y,py))

lenV = len(V)


if mask is not None:
indices = np.nonzero(mask.T.flatten())
if len(indices)>1:
Expand Down
21 changes: 4 additions & 17 deletions centrosome/cpmorphology.py
Original file line number Diff line number Diff line change
Expand Up @@ -1512,12 +1512,6 @@ def minimum_enclosing_circle(labels, indexes = None,
#
v_indexes=np.argwhere(keep_me_vertices).flatten().astype(np.int32)
#
# anti_indexes_per_vertex gives the index into "indexes" and
# any similarly shaped array of per-label values
# (for instance s0_idx) for each vertex being considered
#
anti_indexes_per_vertex = anti_indexes_per_point[keep_me_vertices]
#
# anti_indexes_to_consider_per_vertex gives the index into any
# vector shaped similarly to labels_to_consider for each
# vertex being analyzed
Expand Down Expand Up @@ -1563,7 +1557,6 @@ def minimum_enclosing_circle(labels, indexes = None,
min_angle = scind.minimum(angle_s0vs1,v_labels,
labels_to_consider)
min_angle = fixup_scipy_ndimage_result(min_angle)
min_angle_per_vertex = min_angle[anti_indexes_to_consider_per_vertex]
#
# Calculate the index into V of the minimum angle per label.
# Use "indexes" instead of labels_to_consider so we get something
Expand Down Expand Up @@ -1656,7 +1649,6 @@ def minimum_enclosing_circle(labels, indexes = None,
#
v_obtuse_indexes = v_indexes[min_position[keep_me]]
angle_vs0s1_to_consider = angle_vs0s1[min_position[keep_me]]
angle_vs1s0_to_consider = angle_vs1s0[min_position[keep_me]]
#
# Do the cases where S0 is larger
#
Expand Down Expand Up @@ -1796,7 +1788,6 @@ def associate_by_distance(labels_a, labels_b, distance):
# For each i,j, we have to compare the centers_a against point_counts_b[j]
# crosses.
#
b_len = point_counts_b[ij_consider[:,1]]
b_index = np.cumsum(point_counts_b)
b_elems = b_index[-1]
b_index[1:] = b_index[:-1]
Expand Down Expand Up @@ -2178,7 +2169,6 @@ def ellipse_from_second_moments_ijv(i,j, image, labels, indexes, wants_compactne
#
# Normalize to center of object for stability
#
nlabels = np.max(indexes)+1
m = np.array([[None, 0, None],
[0, None, None],
[None, None, None]], object)
Expand Down Expand Up @@ -2523,10 +2513,10 @@ def euler_number(labels, indexes=None):
#
# There are 6 binary comparisons among the four bits
#
EQ00_01 = I00 == I01; EQ01_00 = EQ00_01
EQ00_10 = I00 == I10; EQ10_00 = EQ00_10
EQ00_11 = I00 == I11; EQ11_00 = EQ00_11
EQ01_10 = I01 == I10; EQ10_01 = EQ01_10
EQ00_01 = I00 == I01;
EQ00_10 = I00 == I10;
EQ00_11 = I00 == I11;
EQ01_10 = I01 == I10;
EQ01_11 = I01 == I11; EQ11_01 = EQ01_11
EQ10_11 = I10 == I11; EQ11_10 = EQ10_11
NE00_01 = np.logical_not(EQ00_01); NE01_00 = NE00_01
Expand Down Expand Up @@ -2677,13 +2667,10 @@ def grey_dilation(image, radius=None, mask=None, footprint=None):
if footprint is None:
if radius is None:
footprint = np.ones((3,3),bool)
footprint_size = (3,3)
radius = 1
else:
footprint = strel_disk(radius)==1
footprint_size = (radius*2+1,radius*2+1)
else:
footprint_size = footprint.shape
radius = max(np.max(np.array(footprint.shape) / 2),1)
iradius = int(np.ceil(radius))
#
Expand Down
3 changes: 1 addition & 2 deletions centrosome/fastemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@
year = {2009}
}
"""
from _fastemd import EMD_NO_FLOW, EMD_WITHOUT_TRANSHIPMENT_FLOW,\
EMD_WITHOUT_EXTRA_MASS_FLOW, emd_hat_int32
from _fastemd import EMD_NO_FLOW, EMD_WITHOUT_TRANSHIPMENT_FLOW, EMD_WITHOUT_EXTRA_MASS_FLOW, emd_hat_int32
5 changes: 0 additions & 5 deletions centrosome/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def stretch(image, mask=None):
else:
transformed_image = ((significant_pixels - minval) /
(maxval - minval))
result = image.copy()
image[mask] = transformed_image
return image

Expand Down Expand Up @@ -422,7 +421,6 @@ def canny(image, mask, sigma, low_threshold, high_threshold):
c1 = magnitude[:,:-1][pts[:,1:]]
c2 = magnitude[1:,:-1][pts[:-1,1:]]
c_minus = c2 * w + c1 * (1.0-w) <= m
cc = np.logical_and(c_plus,c_minus)
local_maxima[pts] = np.logical_and(c_plus, c_minus)
#----- 135 to 180 degrees ------
# Mix anti-diagonal and anti-horizontal
Expand Down Expand Up @@ -1100,8 +1098,6 @@ def kalman_filter(kalman_state, old_indices, coordinates, q, r):
noise_var[:, i] = fix(scind.variance(all_state_noise[:, i],
all_state_noise_idx,
idx))
obs_vec = dot_n(kalman_state.observation_matrix,
state_vec[:,:,np.newaxis])[:,:,0]
kalman_state = KalmanState(kalman_state.observation_matrix,
kalman_state.translation_matrix,
state_vec, state_cov, noise_var,
Expand All @@ -1121,7 +1117,6 @@ def kalman_filter(kalman_state, old_indices, coordinates, q, r):
# The COV for the hidden, undetermined features should be large
# and the COV for others should be small
#
nstates = kalman_state.state_len
nnew_features = len(new_indices)
cov_vec = SMALL_KALMAN_COV / np.dot(observation_matrix_t,
np.ones(kalman_state.obs_len))
Expand Down
1 change: 0 additions & 1 deletion centrosome/lapjv.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def lapjv(i, j, costs, wants_dual_variables = False, augmenting_row_reductions =
returns (x, y), the pairs of assignments that represent the solution
or (x, y, u, v) if the dual variables are requested.
'''
import os
i = np.atleast_1d(i).astype(int)
j = np.atleast_1d(j).astype(int)
costs = np.atleast_1d(costs)
Expand Down
1 change: 0 additions & 1 deletion centrosome/smooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def smooth_with_function_and_mask(image, function, mask):
fraction, so you can recalibrate by dividing by the function on the mask
to recover the effect of smoothing from just the significant pixels.
"""
not_mask = np.logical_not(mask)
bleed_over = function(mask.astype(float))
masked_image = np.zeros(image.shape, image.dtype)
masked_image[mask] = image[mask]
Expand Down
1 change: 0 additions & 1 deletion centrosome/threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ def get_mog_threshold(image, mask=None, object_fraction = 0.2):
return 1
if np.max(cropped_image)==np.min(cropped_image):
return cropped_image[0]
number_of_classes = 3
if pixel_count > max_count:
np.random.seed(0)
pixel_indices = np.random.permutation(pixel_count)[:max_count]
Expand Down
3 changes: 1 addition & 2 deletions centrosome/watershed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from _heapq import heapify, heappush, heappop
from _heapq import heapify
import numpy
import scipy.ndimage
from centrosome.rankorder import rank_order
Expand Down Expand Up @@ -137,7 +137,6 @@ def pad(im):
indexes = []
ignore = True
for j in range(c_connectivity.ndim):
elems = c_image.shape[j]
idx = (i / multiplier) % c_connectivity.shape[j]
off = idx - offset[j]
if off:
Expand Down
1 change: 0 additions & 1 deletion centrosome/zernike.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import scipy.ndimage

from centrosome.cpmorphology import minimum_enclosing_circle,fixup_scipy_ndimage_result
from centrosome.cpmorphology import fill_labeled_holes,draw_line


def construct_zernike_lookuptable(zernike_indexes):
Expand Down
17 changes: 0 additions & 17 deletions tests/test_cpmorphology.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import centrosome.cpmorphology as morph
from centrosome.cpmorphology import fixup_scipy_ndimage_result as fix
from centrosome.filter import permutations


class TestFillLabeledHoles(unittest.TestCase):
Expand Down Expand Up @@ -152,18 +151,6 @@ def test_11_dont_fill_if_touches_2(self):
[ 0, 0, 0, 0, 0, 0, 0, 0 ]])
result = morph.fill_labeled_holes(labels)
self.assertTrue(np.all(labels == result))

def test_12_too_many_objects(self):
# Regression test of issue # 352 - code failed if
# more than 64K objects in background 4-labeling
#
# Create a checkerboard image. The 4-labeling will have > 64K
# labels.
#
i, j = np.mgrid[0:513, 0:513]
labels = (i % 2) != (j % 2)
# Program would segfault within this call
result = morph.fill_labeled_holes(labels)

def test_13_issue_1116(self):
# Regression test of issue # 1116. Object 727 is next to 762, but 762
Expand Down Expand Up @@ -1149,10 +1136,6 @@ def test_02_01_compactness_square(self):


class TestCalculateExtents(unittest.TestCase):
def test_00_00_zeros(self):
"""Make sure calculate_extents doesn't throw an exception if no image"""
extents = morph.calculate_extents(np.zeros((10,10),int), [1])

def test_01_01_square(self):
"""A square should have an extent of 1"""
labels = np.zeros((10,10),int)
Expand Down
63 changes: 31 additions & 32 deletions tests/test_fastemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,24 @@
Website: http://www.cellprofiler.org
"""

import numpy as np
import unittest
from centrosome._fastemd import *
import centrosome._fastemd

class TestFastEMD(unittest.TestCase):
def check(
self, p, q, c, expected_result,
expected_flow = None,
extra_mass_penalty = None,
flow_type = EMD_NO_FLOW,
flow_type = centrosome._fastemd.EMD_NO_FLOW,
gd_metric = False):
if p.dtype == np.int32:
fn = emd_hat_int32
if p.dtype == centrosome._fastemd.np.int32:
fn = centrosome._fastemd.emd_hat_int32
equal_test = self.assertEqual
array_equal_test = np.testing.assert_array_equal
array_equal_test = centrosome._fastemd.np.testing.assert_array_equal
else:
self.fail("Unsupported dtype: %s" % repr(p.dtype))

if flow_type == EMD_NO_FLOW:
if flow_type == centrosome._fastemd.EMD_NO_FLOW:
result = fn(p, q, c,
extra_mass_penalty=extra_mass_penalty,
flow_type=flow_type,
Expand All @@ -49,10 +48,10 @@ def test_01_01_no_flow(self):
tests = (
([1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
np.zeros((5, 5), np.int32), 0),
centrosome._fastemd.np.zeros((5, 5), centrosome._fastemd.np.int32), 0),
([1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
2 - np.eye(5, dtype=np.int32), 5),
2 - centrosome._fastemd.np.eye(5, dtype=centrosome._fastemd.np.int32), 5),
([1, 2, 3, 4, 5],
[3, 3, 3, 3, 3],
[[1, 9, 9, 9, 9],
Expand All @@ -71,50 +70,50 @@ def test_01_01_no_flow(self):
1*1+2*5+(2*3+1*4)+4*5+5*6),
([5, 10],
[1, 2, 3, 4, 5],
np.array([[1, 9],
centrosome._fastemd.np.array([[1, 9],
[5, 9],
[3, 4],
[9, 5],
[9, 6]], np.int32).T,
[9, 6]], centrosome._fastemd.np.int32).T,
1*1+2*5+(2*3+1*4)+4*5+5*6)

)

for p, q, c, expected in tests:
self.check(np.array(p, np.int32),
np.array(q, np.int32),
np.array(c, np.int32),
self.check(centrosome._fastemd.np.array(p, centrosome._fastemd.np.int32),
centrosome._fastemd.np.array(q, centrosome._fastemd.np.int32),
centrosome._fastemd.np.array(c, centrosome._fastemd.np.int32),
expected)

def test_01_02_extra_default(self):
self.check(
np.array([1, 2, 3, 4, 5], np.int32),
np.array([5, 15], np.int32),
np.array([[ 1, 10],
centrosome._fastemd.np.array([1, 2, 3, 4, 5], centrosome._fastemd.np.int32),
centrosome._fastemd.np.array([5, 15], centrosome._fastemd.np.int32),
centrosome._fastemd.np.array([[ 1, 10],
[ 5, 10],
[ 2, 3],
[10, 4],
[10, 6]], np.int32),
[10, 6]], centrosome._fastemd.np.int32),
1*1+2*5+(2*2+1*3)+4*4+5*6+5*10)

def test_01_03_threshold(self):
self.check(
np.array([1, 2, 3, 4, 5], np.int32),
np.array([5, 15], np.int32),
np.array([[ 1, 10],
centrosome._fastemd.np.array([1, 2, 3, 4, 5], centrosome._fastemd.np.int32),
centrosome._fastemd.np.array([5, 15], centrosome._fastemd.np.int32),
centrosome._fastemd.np.array([[ 1, 10],
[ 5, 10],
[ 2, 3],
[10, 4],
[10, 6]], np.int32),
[10, 6]], centrosome._fastemd.np.int32),
1*1+2*5+(2*2+1*3)+4*4+5*6+5*6,
extra_mass_penalty=6)

def test_02_01_flow(self):
tests = (
([1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
2 - np.eye(5, dtype=np.int32), 5,
np.eye(5, dtype=np.int32)),
2 - centrosome._fastemd.np.eye(5, dtype=centrosome._fastemd.np.int32), 5,
centrosome._fastemd.np.eye(5, dtype=centrosome._fastemd.np.int32)),
([1, 2, 3, 4, 5],
[3, 3, 3, 3, 3],
[[1, 9, 9, 9, 9],
Expand Down Expand Up @@ -143,13 +142,13 @@ def test_02_01_flow(self):
[0, 5]]),
([5, 10],
[1, 2, 3, 4, 5],
np.array([[1, 9],
centrosome._fastemd.np.array([[1, 9],
[5, 9],
[3, 4],
[9, 5],
[9, 6]], np.int32).T,
[9, 6]], centrosome._fastemd.np.int32).T,
1*1+2*5+(2*3+1*4)+4*5+5*6,
np.array([[1, 0],
centrosome._fastemd.np.array([[1, 0],
[2, 0],
[2, 1],
[0, 4],
Expand All @@ -158,11 +157,11 @@ def test_02_01_flow(self):
)

for p, q, c, expected, expected_flow in tests:
self.check(np.array(p, np.int32),
np.array(q, np.int32),
np.array(c, np.int32),
self.check(centrosome._fastemd.np.array(p, centrosome._fastemd.np.int32),
centrosome._fastemd.np.array(q, centrosome._fastemd.np.int32),
centrosome._fastemd.np.array(c, centrosome._fastemd.np.int32),
expected,
expected_flow = np.array(expected_flow),
flow_type=EMD_WITHOUT_EXTRA_MASS_FLOW)
expected_flow = centrosome._fastemd.np.array(expected_flow),
flow_type=centrosome._fastemd.EMD_WITHOUT_EXTRA_MASS_FLOW)


8 changes: 1 addition & 7 deletions tests/test_filter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64
import numpy as np
from scipy.ndimage import binary_dilation, binary_erosion, convolve
from scipy.ndimage import binary_dilation, binary_erosion
import unittest

import centrosome.filter as F
Expand Down Expand Up @@ -73,11 +73,6 @@ def test_00_01_all_masked(self):
Regression test of IMG-1029'''
result = F.median_filter(np.zeros((10,10)), np.zeros((10,10), bool), 3)
self.assertTrue(np.all(result == 0))

def test_00_02_all_but_one_masked(self):
mask = np.zeros((10,10), bool)
mask[5,5] = True
result = F.median_filter(np.zeros((10,10)), mask, 3)

def test_01_01_mask(self):
'''The median filter, masking a single value'''
Expand Down Expand Up @@ -1739,7 +1734,6 @@ def test_01_01_nothing(self):
def test_01_02_circle(self):
i,j = np.mgrid[-15:16,-15:16]
circle = np.abs(np.sqrt(i*i+j*j) - 6) <= 1.5
expected = convolve(circle.astype(float), circle.astype(float)) / np.sum(circle)
img = F.circular_hough(circle, 6)
self.assertEqual(img[15,15], 1)
self.assertTrue(np.all(img[np.abs(np.sqrt(i*i+j*j) - 6) < 1.5] < .25))
Expand Down
4 changes: 0 additions & 4 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ def test_02_02_multiple_objects_and_one_is_0x0(self):
ind.idx[1,start:(start + n)]] = np.arange(np.prod(count))
self.assertEqual(len(np.unique(hits.ravel())), n)
start += n

def test_02_03_one_at_end(self):
ind = I.Indexes(np.array([[0,0,1]]))
pass

def test_02_04_none_at_start(self):
ind = I.Indexes(np.array([[0, 1, 1]]))
Expand Down
Loading

0 comments on commit 5a4c610

Please sign in to comment.