Skip to content

Commit

Permalink
Fix Numpy 1.20 and scipy 1.5 deprecationwarnings (soft-matter#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervdw authored Feb 21, 2021
1 parent 45c774c commit d394ef8
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 72 deletions.
7 changes: 7 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ versionfile_source = trackpy/_version.py
versionfile_build = trackpy/_version.py
tag_prefix = v
#parentdir_prefix =

[tool:pytest]
testpaths =
trackpy

[flake8]
ignore = E203, E266, E501, W503
10 changes: 5 additions & 5 deletions trackpy/artificial.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def feat_hat(r, ndim, disc_size):

def feat_step(r, ndim):
""" Solid disc. """
return (r <= 1).astype(np.float)
return (r <= 1).astype(float)


feat_disc = feat_hat
Expand Down Expand Up @@ -103,7 +103,7 @@ def draw_feature(image, position, size, max_value=None, feat_func='gauss',
upper_bound = min(int(np.ceil(c + m / 2 + 1)), lim)
rect.append(slice(lower_bound, upper_bound))
vectors.append(np.arange(lower_bound - c, upper_bound - c) / s)
coords = np.meshgrid(*vectors, indexing='ij', sparse=True)
coords = np.meshgrid(*vectors, indexing='ij')
r = np.sqrt(np.sum(np.array(coords)**2, axis=0))
spot = max_value * feat_func(r, ndim=image.ndim, **kwargs)
image[tuple(rect)] += spot.astype(image.dtype)
Expand Down Expand Up @@ -214,9 +214,9 @@ def draw_array(N, size, separation=None, ndim=2, **kwargs):
Nsqrt = int(N**(1/ndim) + 0.9999)
pos = np.meshgrid(*[np.arange(0, s * Nsqrt, s) for s in separation],
indexing='ij')
pos = np.array([p.ravel() for p in pos], dtype=np.float).T[:N] + margin
pos = np.array([p.ravel() for p in pos], dtype=float).T[:N] + margin
pos += (np.random.random(pos.shape) - 0.5) #randomize subpixel location
shape = tuple(np.max(pos, axis=0).astype(np.int) + margin)
shape = tuple(np.max(pos, axis=0).astype(int) + margin)
return pos, draw_spots(shape, pos, size, **kwargs)


Expand Down Expand Up @@ -468,7 +468,7 @@ def denoised(self, noise_level, noise_size, smoothing_size=None,
@property
def coords(self):
if len(self._coords) == 0:
return np.zeros((0, self.ndim), dtype=np.float)
return np.zeros((0, self.ndim), dtype=float)
return np.array(self._coords)

def f(self, noise=0):
Expand Down
2 changes: 1 addition & 1 deletion trackpy/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ def characterize(coords, image, radius, scale_factor=1.):
isotropic = is_isotropic(radius)

# largely based on trackpy.refine.center_of_mass._refine
coords_i = np.round(coords).astype(np.int)
coords_i = np.round(coords).astype(int)
mass = np.full(N, np.nan)
signal = np.full(N, np.nan)
ecc = np.full(N, np.nan)
Expand Down
2 changes: 1 addition & 1 deletion trackpy/linking/linking.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def link(f, search_range, pos_columns=None, t_column='frame', **kwargs):
f = f.copy()
# coerce t_column to integer type
if not np.issubdtype(f[t_column].dtype, np.integer):
f[t_column] = f[t_column].astype(np.integer)
f[t_column] = f[t_column].astype(int)
# sort on the t_column
pandas_sort(f, t_column, inplace=True)

Expand Down
4 changes: 2 additions & 2 deletions trackpy/masks.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_slice(coords, shape, radius):
# interpret parameters
ndim = len(shape)
radius = validate_tuple(radius, ndim)
coords = np.atleast_2d(np.round(coords).astype(np.int))
coords = np.atleast_2d(np.round(coords).astype(int))
# drop features that have no pixels inside the image
in_bounds = np.array([(coords[:, i] >= -r) & (coords[:, i] < sh + r)
for i, sh, r in zip(range(ndim), shape, radius)])
Expand Down Expand Up @@ -186,7 +186,7 @@ def get_mask(pos, shape, radius, include_edge=True, return_masks=False):
for p in pos]
mask_total = np.any(in_mask, axis=0).T
if return_masks:
masks_single = np.empty((len(pos), mask_total.sum()), dtype=np.bool)
masks_single = np.empty((len(pos), mask_total.sum()), dtype=bool)
for i, _in_mask in enumerate(in_mask):
masks_single[i] = _in_mask.T[mask_total]
return mask_total, masks_single
Expand Down
2 changes: 1 addition & 1 deletion trackpy/motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _msd_N(N, t):
tracking. Analysis of diffusion and flow in two-dimensional systems."
Biophysical journal 60.4 (1991): 910.
"""
t = np.array(t, dtype=np.float)
t = np.array(t, dtype=float)
return np.where(t > N/2,
1/(1+((N-t)**3+5*t-4*(N-t)**2*t-N)/(6*(N-t)*t**2)),
6*(N-t)**2*t/(2*N-t+4*N*t**2-5*t**3))
Expand Down
3 changes: 2 additions & 1 deletion trackpy/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def wrapper(*args, **kwargs):
import matplotlib.pyplot as plt
if kwargs.get('ax') is None:
kwargs['ax'] = plt.gca()
show_plot = True
# show plot unless the matplotlib backend is headless
show_plot = (plt.get_backend() != "agg")
else:
show_plot = False

Expand Down
2 changes: 1 addition & 1 deletion trackpy/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def lowpass(image, sigma=1, truncate=4):
bandpass
"""
sigma = validate_tuple(sigma, image.ndim)
result = np.array(image, dtype=np.float)
result = np.array(image, dtype=float)
for axis, _sigma in enumerate(sigma):
if _sigma > 0:
correlate1d(result, gaussian_kernel(_sigma, truncate), axis,
Expand Down
4 changes: 2 additions & 2 deletions trackpy/refine/brightfield_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def _min_edge(arr, threshold=0.45, max_dev=1, axis=1, bright_left=True,
if axis == 0:
arr = arr.T
if np.issubdtype(arr.dtype, np.unsignedinteger):
arr = arr.astype(np.int)
arr = arr.astype(int)

values = np.nanpercentile(arr, 5, axis=1)
rdev = []
Expand Down Expand Up @@ -312,7 +312,7 @@ def _unwrap_ellipse(image, params, rad_range, num_points=None, spline_order=4,
pos[:, :, np.newaxis]
# interpolate the image on computed coordinates
intensity = map_coordinates(image, coords, order=spline_order,
output=np.float, mode='constant',
output=float, mode='constant',
cval=fill_value)
return intensity, pos, normal

Expand Down
2 changes: 1 addition & 1 deletion trackpy/refine/center_of_mass.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def refine_com_arr(raw_image, image, radius, coords, max_iterations=10,
engine = 'python'

# In here, coord is an integer. Make a copy, will not modify inplace.
coords = np.round(coords).astype(np.int)
coords = np.round(coords).astype(int)

if engine == 'python':
results = _refine(raw_image, image, radius, coords, max_iterations,
Expand Down
17 changes: 12 additions & 5 deletions trackpy/refine/least_squares.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def prepare_subimage(coords, image, radius):
# to mask the image
mask_total = np.any(dist, axis=0).T
# to mask the masked image
masks_singles = np.empty((len(coords), mask_total.sum()), dtype=np.bool)
masks_singles = np.empty((len(coords), mask_total.sum()), dtype=bool)
for i, _dist in enumerate(dist):
masks_singles[i] = _dist.T[mask_total]

Expand Down Expand Up @@ -839,9 +839,16 @@ def refine_leastsq(f, reader, diameter, separation=None, fit_function='gauss',
residual, jacobian = ff.get_residual(sub_images, meshes, masks,
params, groups, norm)

result = minimize(residual, vect, bounds=f_bounds,
constraints=f_constraints, jac=jacobian,
**_kwargs)
with warnings.catch_warnings():
# see https://github.com/scipy/scipy/pull/13009 (issue in scipy 1.5)
warnings.filterwarnings(
"ignore",
message="Values in x were outside bounds during a minimize step, clipping to bounds",
category=RuntimeWarning,
)
result = minimize(residual, vect, bounds=f_bounds,
constraints=f_constraints, jac=jacobian,
**_kwargs)
if not result['success']:
raise RefineException(result['message'])

Expand Down Expand Up @@ -1246,7 +1253,7 @@ def _wrap_constraints(constraints, params_const, modes, groups=None):
return []

if groups is not None:
cl_sizes = np.array([len(g) for g in groups[0]], dtype=np.int)
cl_sizes = np.array([len(g) for g in groups[0]], dtype=int)

result = []
for cons in constraints:
Expand Down
4 changes: 2 additions & 2 deletions trackpy/tests/locate/test_brightfield_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_default_locate_multiple_more_noisy_dense_dip(self):
assert not equal_shape

def test_min_edge(self):
image = np.zeros(self.image_size, dtype=np.float)
image = np.zeros(self.image_size, dtype=float)

ix = int(np.round(float(self.image_size[1])/2.0))
image[:, :ix] += 230.0
Expand All @@ -244,7 +244,7 @@ def test_min_edge(self):
assert_allclose(result, float(ix)+0.5, atol=0.1)

def test_min_edge_noisy(self):
image = np.zeros(self.image_size, dtype=np.float)
image = np.zeros(self.image_size, dtype=float)
image += np.random.randint(1, 255, image.shape).astype(float)

ix = int(np.round(float(self.image_size[1])/2.0))
Expand Down
4 changes: 2 additions & 2 deletions trackpy/tests/test_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_oldmass_float(self):
old_minmass = 5500
im = draw_spots(self.shape, self.pos, self.size, bitdepth=8,
noise_level=50)
im = (im / im.max()).astype(np.float)
im = (im / im.max()).astype(float)

new_minmass = self.minmass_v02_to_v04(im, old_minmass)
f = tp.locate(im, self.tp_diameter, minmass=new_minmass)
Expand Down Expand Up @@ -674,7 +674,7 @@ def test_ep(self):
draw_size = 4.5
locate_diameter = 21
N = 200
noise_levels = (np.array([0.01, 0.02, 0.05, 0.1, 0.2, 0.3, 0.5]) * (2**12 - 1)).astype(np.int)
noise_levels = (np.array([0.01, 0.02, 0.05, 0.1, 0.2, 0.3, 0.5]) * (2**12 - 1)).astype(int)
real_rms_dev = []
eps = []
actual_black_level = []
Expand Down
6 changes: 3 additions & 3 deletions trackpy/tests/test_find_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def link(self, f, search_range, *args, **kwargs):
separation = kwargs['separation']
f = f.copy()
f[['y', 'x']] *= separation
topleft = (f[['y', 'x']].min().values - 4 * separation).astype(np.int)
topleft = (f[['y', 'x']].min().values - 4 * separation).astype(int)
f[['y', 'x']] -= topleft
shape = (f[['y', 'x']].max().values + 4 * separation).astype(np.int)
shape = (f[['y', 'x']].max().values + 4 * separation).astype(int)
reader = CoordinateReader(f, shape, size)
if kwargs.get('adaptive_stop', None) is not None:
kwargs['adaptive_stop'] *= separation
Expand Down Expand Up @@ -152,7 +152,7 @@ def link(self, f, shape, remove=None, **kwargs):
_kwargs.update(kwargs)
if remove is not None:
callback_coords = f.loc[f['frame'] == 1, ['y', 'x']].values
remove = np.array(remove, dtype=np.int)
remove = np.array(remove, dtype=int)
if np.any(remove < 0) or np.any(remove > len(callback_coords)):
raise RuntimeError('Invalid test: `remove` is out of bounds.')
callback_coords = np.delete(callback_coords, remove, axis=0)
Expand Down
18 changes: 9 additions & 9 deletions trackpy/tests/test_leastsq.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def get_image(self, noise=0, signal_dev=0., size_dev=0., separation=None,
Nsqrt = int(N**(1/self.ndim) + 0.9999)
pos = np.meshgrid(*[np.arange(0, s * Nsqrt, s) for s in separation],
indexing='ij')
pos = np.array([p.ravel() for p in pos], dtype=np.float).T[:N] + margin
pos = np.array([p.ravel() for p in pos], dtype=float).T[:N] + margin
pos += (np.random.random(pos.shape) - 0.5) #randomize subpixel location
shape = tuple(np.max(pos, axis=0).astype(np.int) + margin)
shape = tuple(np.max(pos, axis=0).astype(int) + margin)
if signal_dev > 0:
signal = self.signal * np.random.uniform(1-signal_dev, 1+signal_dev,
N)
Expand Down Expand Up @@ -133,7 +133,7 @@ def get_image_clusters(self, cluster_size, hard_radius=1., noise=0,
Nsqrt = int(N**(1/self.ndim) + 0.9999)
pos = np.meshgrid(*[np.arange(0, s * Nsqrt, s) for s in separation],
indexing='ij')
pos = np.array([p.ravel() for p in pos], dtype=np.float).T[:N] + margin
pos = np.array([p.ravel() for p in pos], dtype=float).T[:N] + margin
pos += (np.random.random(pos.shape) - 0.5) #randomize subpixel location
if self.ndim == 2 and angle is None:
angles = np.random.uniform(0, 2*np.pi, N)
Expand All @@ -144,7 +144,7 @@ def get_image_clusters(self, cluster_size, hard_radius=1., noise=0,
elif self.ndim == 3 and angle is not None:
angles = np.repeat([angle], N, axis=0)

shape = tuple(np.max(pos, axis=0).astype(np.int) + margin)
shape = tuple(np.max(pos, axis=0).astype(int) + margin)
if signal_dev > 0:
signal = self.signal * np.random.uniform(1-signal_dev, 1+signal_dev,
N)
Expand Down Expand Up @@ -180,14 +180,14 @@ def get_image_clusters(self, cluster_size, hard_radius=1., noise=0,
def to_dataframe(self, coords, signal, size, cluster_size=1):
f0 = pd.DataFrame(coords, columns=self.pos_columns)
f0['signal'] = signal
f0['signal'] = f0['signal'].astype(np.float)
f0['signal'] = f0['signal'].astype(float)
if self.isotropic:
f0[self.size_columns[0]] = size[0]
f0[self.size_columns[0]] = f0[self.size_columns[0]].astype(np.float)
f0[self.size_columns[0]] = f0[self.size_columns[0]].astype(float)
else:
for col, s in zip(self.size_columns, size):
f0[col] = s
f0[col] = f0[col].astype(np.float)
f0[col] = f0[col].astype(float)
f0['cluster'] = np.repeat(np.arange(len(coords) // cluster_size),
cluster_size)
f0['cluster_size'] = cluster_size
Expand Down Expand Up @@ -958,11 +958,11 @@ def gauss(p, y, x):
background, signal, yc, xc, size = p
return background + signal*np.exp(-((y - yc)**2/size**2 + (x - xc)**2/size**2))
ff = FitFunctions('gauss', ndim=2, isotropic=True)
params = np.array([[5, 200, 4, 5, 6]], dtype=np.float)
params = np.array([[5, 200, 4, 5, 6]], dtype=float)

image = np.random.random(self.repeats) * 200
mesh = np.random.random((ff.ndim, self.repeats)) * 10
masks = np.full((1, self.repeats), True, dtype=np.bool)
masks = np.full((1, self.repeats), True, dtype=bool)

residual, jacobian = ff.get_residual([image], [mesh], [masks], params)

Expand Down
8 changes: 4 additions & 4 deletions trackpy/tests/test_legacy_linking.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,14 @@ def test_penalty(self):
actual = self.link_df(f, 13)
pandas_sort(case1, ['x'], inplace=True)
pandas_sort(actual, ['x'], inplace=True)
assert_array_equal(actual['particle'].values.astype(np.int),
case1['particle'].values.astype(np.int))
assert_array_equal(actual['particle'].values.astype(int),
case1['particle'].values.astype(int))

actual = self.link_df(f, 12)
pandas_sort(case2, ['x'], inplace=True)
pandas_sort(actual, ['x'], inplace=True)
assert_array_equal(actual['particle'].values.astype(np.int),
case2['particle'].values.astype(np.int))
assert_array_equal(actual['particle'].values.astype(int),
case2['particle'].values.astype(int))

def test_memory(self):
"""A unit-stepping trajectory and a random walk are observed
Expand Down
20 changes: 10 additions & 10 deletions trackpy/tests/test_linking.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ def test_output_dtypes(self):
f = DataFrame({'x': np.arange(N), 'y': np.ones(N),
'frame': np.arange(N)})
# Integer-typed input
f['frame'] = f['frame'].astype(np.int)
f['frame'] = f['frame'].astype(int)
actual = self.link(f, 5)

# Particle and frame columns should be integer typed
assert np.issubdtype(actual['particle'], np.integer)
assert np.issubdtype(actual['frame'], np.integer)

# Float-typed input
f['frame'] = f['frame'].astype(np.float)
f['frame'] = f['frame'].astype(float)
actual = self.link(f, 5)

# Particle and frame columns should be integer typed
Expand Down Expand Up @@ -491,14 +491,14 @@ def test_penalty(self):
actual = self.link(f, 13)
pandas_sort(case1, ['x'], inplace=True)
pandas_sort(actual, ['x'], inplace=True)
assert_equal(actual['particle'].values.astype(np.int),
case1['particle'].values.astype(np.int))
assert_equal(actual['particle'].values.astype(int),
case1['particle'].values.astype(int))

actual = self.link(f, 12)
pandas_sort(case2, ['x'], inplace=True)
pandas_sort(actual, ['x'], inplace=True)
assert_equal(actual['particle'].values.astype(np.int),
case2['particle'].values.astype(np.int))
assert_equal(actual['particle'].values.astype(int),
case2['particle'].values.astype(int))

def test_memory(self):
"""A unit-stepping trajectory and a random walk are observed
Expand Down Expand Up @@ -626,15 +626,15 @@ def test_output_dtypes(self):
f = DataFrame({'x': np.arange(N), 'y': np.ones(N),
'frame': np.arange(N)})
# Integer-typed input
f['frame'] = f['frame'].astype(np.int)
f['frame'] = f['frame'].astype(int)
actual = self.link(f, 5)

# Particle and frame columns should be integer typed
assert np.issubdtype(actual['particle'], np.integer)
assert np.issubdtype(actual['frame'], np.integer)

# Float-typed input: frame column type is propagated in link_iter
f['frame'] = f['frame'].astype(np.float)
f['frame'] = f['frame'].astype(float)
actual = self.link(f, 5)
assert np.issubdtype(actual['particle'], np.integer)
assert np.issubdtype(actual['frame'], np.floating)
Expand All @@ -658,15 +658,15 @@ def test_output_dtypes(self):
f = DataFrame({'x': np.arange(N), 'y': np.ones(N),
'frame': np.arange(N)})
# Integer-typed input
f['frame'] = f['frame'].astype(np.int)
f['frame'] = f['frame'].astype(int)
actual = self.link(f, 5)

# Particle and frame columns should be integer typed
assert np.issubdtype(actual['particle'], np.integer)
assert np.issubdtype(actual['frame'], np.integer)

# Float-typed input: frame column type is propagated in link_df_iter
f['frame'] = f['frame'].astype(np.float)
f['frame'] = f['frame'].astype(float)
actual = self.link(f, 5)
assert np.issubdtype(actual['particle'], np.integer)
assert np.issubdtype(actual['frame'], np.floating)
Expand Down
Loading

0 comments on commit d394ef8

Please sign in to comment.