Skip to content

Commit 81c60cc

Browse files
committed
Fix error handling in void_prob_func.py
1 parent 4ee028b commit 81c60cc

5 files changed

Lines changed: 83 additions & 85 deletions

File tree

halotools/mock_observables/pair_counters/pairs.py

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
"""
66

77
from __future__ import absolute_import, division, print_function, unicode_literals
8+
89
import numpy as np
910

10-
__all__ = ['npairs', 'wnpairs', 'xy_z_npairs', 'xy_z_wnpairs', 's_mu_npairs']
11-
__author__ = ['Duncan Campbell']
11+
__all__ = ["npairs", "wnpairs", "xy_z_npairs", "xy_z_wnpairs", "s_mu_npairs"]
12+
__author__ = ["Duncan Campbell"]
1213

1314

1415
def npairs(sample1, sample2, rbins, period=None):
@@ -51,22 +52,24 @@ def npairs(sample1, sample2, rbins, period=None):
5152

5253
# Process period entry and check for consistency.
5354
if period is None:
54-
period = np.array([np.inf]*np.shape(sample1)[-1])
55+
period = np.array([np.inf] * np.shape(sample1)[-1])
5556
else:
5657
period = np.asarray(period).astype("float64")
5758
if np.shape(period) == ():
58-
period = np.array([period]*np.shape(sample1)[-1])
59+
period = np.array([period] * np.shape(sample1)[-1])
5960
elif np.shape(period)[0] != np.shape(sample1)[-1]:
6061
raise ValueError("period should have len == dimension of points")
6162
return None
6263

6364
N1 = len(sample1)
6465
N2 = len(sample2)
65-
dd = np.zeros((N1*N2,)) # store radial pair separations
66-
for i in range(0, N1): # calculate distance between every point and every other point
66+
dd = np.zeros((N1 * N2,)) # store radial pair separations
67+
for i in range(
68+
0, N1
69+
): # calculate distance between every point and every other point
6770
x1 = sample1[i, :]
6871
x2 = sample2
69-
dd[i*N2:i*N2+N2] = distance(x1, x2, period)
72+
dd[i * N2 : i * N2 + N2] = distance(x1, x2, period)
7073

7174
# sort results
7275
dd.sort()
@@ -127,23 +130,25 @@ def xy_z_npairs(sample1, sample2, rp_bins, pi_bins, period=None):
127130

128131
# Process period entry and check for consistency.
129132
if period is None:
130-
period = np.array([np.inf]*np.shape(sample1)[-1])
133+
period = np.array([np.inf] * np.shape(sample1)[-1])
131134
else:
132135
period = np.asarray(period).astype("float64")
133136
if np.shape(period) == ():
134-
period = np.array([period]*np.shape(sample1)[-1])
137+
period = np.array([period] * np.shape(sample1)[-1])
135138
elif np.shape(period)[0] != np.shape(sample1)[-1]:
136139
raise ValueError("period should have len == dimension of points")
137140
return None
138141

139142
N1 = len(sample1)
140143
N2 = len(sample2)
141-
dd = np.zeros((N1*N2, 2)) # store pair separations
142-
for i in range(0, N1): # calculate distance between every point and every other point
144+
dd = np.zeros((N1 * N2, 2)) # store pair separations
145+
for i in range(
146+
0, N1
147+
): # calculate distance between every point and every other point
143148
x1 = sample1[i, :]
144149
x2 = sample2
145-
dd[i*N2:i*N2+N2, 1] = parallel_distance(x1, x2, period)
146-
dd[i*N2:i*N2+N2, 0] = perpendicular_distance(x1, x2, period)
150+
dd[i * N2 : i * N2 + N2, 1] = parallel_distance(x1, x2, period)
151+
dd[i * N2 : i * N2 + N2, 0] = perpendicular_distance(x1, x2, period)
147152

148153
# count number less than r
149154
n = np.zeros((rp_bins.size, pi_bins.size), dtype=np.int64)
@@ -200,26 +205,26 @@ def wnpairs(sample1, sample2, r, period=None, weights1=None, weights2=None):
200205

201206
# Process period entry and check for consistency.
202207
if period is None:
203-
period = np.array([np.inf]*np.shape(sample1)[-1])
208+
period = np.array([np.inf] * np.shape(sample1)[-1])
204209
else:
205210
period = np.asarray(period).astype("float64")
206211
if np.shape(period) == ():
207-
period = np.array([period]*np.shape(sample1)[-1])
212+
period = np.array([period] * np.shape(sample1)[-1])
208213
if np.shape(period)[0] != np.shape(sample1)[-1]:
209214
raise ValueError("period should have len == dimension of points")
210215
return None
211216

212217
# Process weights1 entry and check for consistency.
213218
if weights1 is None:
214-
weights1 = np.array([1.0]*np.shape(sample1)[0], dtype=np.float64)
219+
weights1 = np.array([1.0] * np.shape(sample1)[0], dtype=np.float64)
215220
else:
216221
weights1 = np.asarray(weights1).astype("float64")
217222
if np.shape(weights1)[0] != np.shape(sample1)[0]:
218223
raise ValueError("weights1 should have same len as sample1")
219224
return None
220225
# Process weights2 entry and check for consistency.
221226
if weights2 is None:
222-
weights2 = np.array([1.0]*np.shape(sample2)[0], dtype=np.float64)
227+
weights2 = np.array([1.0] * np.shape(sample2)[0], dtype=np.float64)
223228
else:
224229
weights2 = np.asarray(weights2).astype("float64")
225230
if np.shape(weights2)[0] != np.shape(sample2)[0]:
@@ -229,7 +234,9 @@ def wnpairs(sample1, sample2, r, period=None, weights1=None, weights2=None):
229234
N1 = len(sample1)
230235
N2 = len(sample2)
231236
dd = np.zeros((N1, N2), dtype=np.float64) # store radial pair separations
232-
for i in range(0, N1): # calculate distance between every point and every other point
237+
for i in range(
238+
0, N1
239+
): # calculate distance between every point and every other point
233240
x1 = sample1[i, :]
234241
x2 = sample2
235242
dd[i, :] = distance(x1, x2, period)
@@ -238,12 +245,14 @@ def wnpairs(sample1, sample2, r, period=None, weights1=None, weights2=None):
238245
n = np.zeros((r.size,), dtype=np.float64)
239246
for i in range(r.size):
240247
for j in range(N1):
241-
n[i] += np.sum(np.extract(dd[j, :] <= r[i], weights2))*weights1[j]
248+
n[i] += np.sum(np.extract(dd[j, :] <= r[i], weights2)) * weights1[j]
242249

243250
return n
244251

245252

246-
def xy_z_wnpairs(sample1, sample2, rp_bins, pi_bins, period=None, weights1=None, weights2=None):
253+
def xy_z_wnpairs(
254+
sample1, sample2, rp_bins, pi_bins, period=None, weights1=None, weights2=None
255+
):
247256
r"""
248257
Calculate the number of weighted pairs with parellal separations less than or equal to
249258
pi_bins[i], and perpendicular separations less than or equal to rp_bins[i].
@@ -298,26 +307,26 @@ def xy_z_wnpairs(sample1, sample2, rp_bins, pi_bins, period=None, weights1=None,
298307

299308
# Process period entry and check for consistency.
300309
if period is None:
301-
period = np.array([np.inf]*np.shape(sample1)[-1])
310+
period = np.array([np.inf] * np.shape(sample1)[-1])
302311
else:
303312
period = np.asarray(period).astype("float64")
304313
if np.shape(period) == ():
305-
period = np.array([period]*np.shape(sample1)[-1])
314+
period = np.array([period] * np.shape(sample1)[-1])
306315
elif np.shape(period)[0] != np.shape(sample1)[-1]:
307316
raise ValueError("period should have len == dimension of points")
308317
return None
309318

310319
# Process weights1 entry and check for consistency.
311320
if weights1 is None:
312-
weights1 = np.array([1.0]*np.shape(sample1)[0], dtype=np.float64)
321+
weights1 = np.array([1.0] * np.shape(sample1)[0], dtype=np.float64)
313322
else:
314323
weights1 = np.asarray(weights1).astype("float64")
315324
if np.shape(weights1)[0] != np.shape(sample1)[0]:
316325
raise ValueError("weights1 should have same len as sample1")
317326
return None
318327
# Process weights2 entry and check for consistency.
319328
if weights2 is None:
320-
weights2 = np.array([1.0]*np.shape(sample2)[0], dtype=np.float64)
329+
weights2 = np.array([1.0] * np.shape(sample2)[0], dtype=np.float64)
321330
else:
322331
weights2 = np.asarray(weights2).astype("float64")
323332
if np.shape(weights2)[0] != np.shape(sample2)[0]:
@@ -326,20 +335,24 @@ def xy_z_wnpairs(sample1, sample2, rp_bins, pi_bins, period=None, weights1=None,
326335

327336
N1 = len(sample1)
328337
N2 = len(sample2)
329-
dd = np.zeros((N1*N2, 2)) # store pair separations
330-
ww = np.zeros((N1*N2, 1)) # store pair separations
331-
for i in range(0, N1): # calculate distance between every point and every other point
338+
dd = np.zeros((N1 * N2, 2)) # store pair separations
339+
ww = np.zeros((N1 * N2, 1)) # store pair separations
340+
for i in range(
341+
0, N1
342+
): # calculate distance between every point and every other point
332343
x1 = sample1[i, :]
333344
x2 = sample2
334-
dd[i*N2:i*N2+N2, 1] = parallel_distance(x1, x2, period)
335-
dd[i*N2:i*N2+N2, 0] = perpendicular_distance(x1, x2, period)
336-
ww[i*N2:i*N2+N2] = weights1[i]*weights2
345+
dd[i * N2 : i * N2 + N2, 1] = parallel_distance(x1, x2, period)
346+
dd[i * N2 : i * N2 + N2, 0] = perpendicular_distance(x1, x2, period)
347+
ww[i * N2 : i * N2 + N2] = weights1[i] * weights2
337348

338349
# count number less than r
339350
n = np.zeros((rp_bins.size, pi_bins.size), dtype=np.float64)
340351
for i in range(rp_bins.size):
341352
for j in range(pi_bins.size):
342-
n[i, j] += np.sum(np.extract((dd[:, 0] <= rp_bins[i]) & (dd[:, 1] <= pi_bins[j]), ww))
353+
n[i, j] += np.sum(
354+
np.extract((dd[:, 0] <= rp_bins[i]) & (dd[:, 1] <= pi_bins[j]), ww)
355+
)
343356

344357
return n
345358

@@ -400,11 +413,11 @@ def s_mu_npairs(sample1, sample2, s_bins, mu_bins, period=None):
400413

401414
# Process period entry and check for consistency.
402415
if period is None:
403-
period = np.array([np.inf]*np.shape(sample1)[-1])
416+
period = np.array([np.inf] * np.shape(sample1)[-1])
404417
else:
405418
period = np.asarray(period).astype("float64")
406419
if np.shape(period) == ():
407-
period = np.array([period]*np.shape(sample1)[-1])
420+
period = np.array([period] * np.shape(sample1)[-1])
408421
elif np.shape(period)[0] != np.shape(sample1)[-1]:
409422
raise ValueError("period should have len == dimension of points")
410423
return None
@@ -413,14 +426,14 @@ def s_mu_npairs(sample1, sample2, s_bins, mu_bins, period=None):
413426
# note that this array can be very large for large N1 and N2
414427
N1 = len(sample1)
415428
N2 = len(sample2)
416-
dd = np.zeros((N1*N2, 2))
429+
dd = np.zeros((N1 * N2, 2))
417430

418431
# calculate distance between every point and every other point
419432
for i in range(0, N1):
420433
x1 = sample1[i, :]
421434
x2 = sample2
422-
dd[i*N2:i*N2+N2, 0] = distance(x1, x2, period)
423-
dd[i*N2:i*N2+N2, 1] = np.cos(theta_LOS(x1, x2, period))
435+
dd[i * N2 : i * N2 + N2, 0] = distance(x1, x2, period)
436+
dd[i * N2 : i * N2 + N2, 1] = np.cos(theta_LOS(x1, x2, period))
424437

425438
# put mu bins in increasing theta_LOS order
426439
mu_bins = np.sort(mu_bins)[::-1]
@@ -458,7 +471,7 @@ def distance(x1, x2, period=None):
458471
x1 = np.atleast_2d(x1)
459472
x2 = np.atleast_2d(x2)
460473
if period is None:
461-
period = np.array([np.inf]*np.shape(x1)[-1])
474+
period = np.array([np.inf] * np.shape(x1)[-1])
462475

463476
# check for consistency
464477
if np.shape(x1)[-1] != np.shape(x2)[-1]:
@@ -469,7 +482,7 @@ def distance(x1, x2, period=None):
469482
raise ValueError("period must have length equal to the dimension of x1 and x2.")
470483

471484
m = np.minimum(np.fabs(x1 - x2), period - np.fabs(x1 - x2))
472-
distance = np.sqrt(np.sum(m*m, axis=len(np.shape(m))-1))
485+
distance = np.sqrt(np.sum(m * m, axis=len(np.shape(m)) - 1))
473486

474487
return distance
475488

@@ -500,7 +513,7 @@ def parallel_distance(x1, x2, period=None):
500513
x1 = np.atleast_2d(x1)
501514
x2 = np.atleast_2d(x2)
502515
if period is None:
503-
period = np.array([np.inf]*np.shape(x1)[-1])
516+
period = np.array([np.inf] * np.shape(x1)[-1])
504517

505518
# check for consistency
506519
if np.shape(x1)[-1] != np.shape(x2)[-1]:
@@ -510,8 +523,10 @@ def parallel_distance(x1, x2, period=None):
510523
if np.shape(period)[0] != np.shape(x1)[-1]:
511524
raise ValueError("period must have length equal to the dimension of x1 and x2.")
512525

513-
m = np.minimum(np.fabs(x1[:, -1] - x2[:, -1]), period[-1] - np.fabs(x1[:, -1] - x2[:, -1]))
514-
distance = np.sqrt(m*m)
526+
m = np.minimum(
527+
np.fabs(x1[:, -1] - x2[:, -1]), period[-1] - np.fabs(x1[:, -1] - x2[:, -1])
528+
)
529+
distance = np.sqrt(m * m)
515530

516531
return distance
517532

@@ -542,7 +557,7 @@ def perpendicular_distance(x1, x2, period=None):
542557
x1 = np.atleast_2d(x1)
543558
x2 = np.atleast_2d(x2)
544559
if period is None:
545-
period = np.array([np.inf]*np.shape(x1)[-1])
560+
period = np.array([np.inf] * np.shape(x1)[-1])
546561

547562
# check for consistency
548563
if np.shape(x1)[-1] != np.shape(x2)[-1]:
@@ -552,8 +567,10 @@ def perpendicular_distance(x1, x2, period=None):
552567
if np.shape(period)[0] != np.shape(x1)[-1]:
553568
raise ValueError("period must have length equal to the dimension of x1 and x2.")
554569

555-
m = np.minimum(np.fabs(x1[:, :-1] - x2[:, :-1]), period[:-1] - np.fabs(x1[:, :-1] - x2[:, :-1]))
556-
distance = np.sqrt(np.sum(m*m, axis=len(np.shape(m))-1))
570+
m = np.minimum(
571+
np.fabs(x1[:, :-1] - x2[:, :-1]), period[:-1] - np.fabs(x1[:, :-1] - x2[:, :-1])
572+
)
573+
distance = np.sqrt(np.sum(m * m, axis=len(np.shape(m)) - 1))
557574

558575
return distance
559576

@@ -589,7 +606,7 @@ def theta_LOS(x1, x2, period=None):
589606
x1 = np.atleast_2d(x1)
590607
x2 = np.atleast_2d(x2)
591608
if period is None:
592-
period = np.array([np.inf]*np.shape(x1)[-1])
609+
period = np.array([np.inf] * np.shape(x1)[-1])
593610

594611
# check for consistency
595612
if np.shape(x1)[-1] != np.shape(x2)[-1]:
@@ -604,9 +621,9 @@ def theta_LOS(x1, x2, period=None):
604621

605622
# deal with zero separation
606623
r = np.sqrt(r_perp**2 + r_parallel**2)
607-
mask = (r>0.0)
624+
mask = r > 0.0
608625

609-
theta = np.zeros(len(r)) # set to zero if r==0
610-
theta[mask] = np.pi/2.0 - np.arctan2(r_parallel[mask],r_perp[mask])
626+
theta = np.zeros(len(r)) # set to zero if r==0
627+
theta[mask] = np.pi / 2.0 - np.arctan2(r_parallel[mask], r_perp[mask])
611628

612629
return theta

halotools/mock_observables/pair_counters/test_pair_counters/test_marked_npairs_3d.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
"""
2-
"""
1+
""" """
2+
33
from __future__ import absolute_import, division, print_function, unicode_literals
44

5+
from pathlib import Path
6+
57
import numpy as np
68
import pytest
79
from astropy.utils.misc import NumpyRNGContext
8-
from pathlib import Path
9-
10-
from ..pairs import wnpairs as pure_python_weighted_pairs
11-
from ..marked_npairs_3d import marked_npairs_3d, _func_signature_int_from_wfunc
1210

1311
from ....custom_exceptions import HalotoolsError
14-
12+
from ..marked_npairs_3d import _func_signature_int_from_wfunc, marked_npairs_3d
13+
from ..pairs import wnpairs as pure_python_weighted_pairs
1514

1615
error_msg = (
1716
"\nThe `test_marked_npairs_wfuncs_behavior` function performs \n"

halotools/mock_observables/void_statistics/tests/test_underdensity_prob_func.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
""" Module providing unit-testing for the
1+
"""Module providing unit-testing for the
22
`~halotools.mock_observables.underdensity_prob_func function.
33
"""
4+
45
from __future__ import absolute_import, division, print_function
56

67
import numpy as np
78
import pytest
89
from astropy.utils.misc import NumpyRNGContext
910

11+
from ....custom_exceptions import HalotoolsError
12+
from ...tests.cf_helpers import generate_locus_of_3d_points
1013
from ..underdensity_prob_func import underdensity_prob_func
1114
from ..void_prob_func import void_prob_func
1215

13-
from ...tests.cf_helpers import generate_locus_of_3d_points
14-
from ....custom_exceptions import HalotoolsError
15-
1616
__all__ = ("test_upf1", "test_upf2", "test_upf3", "test_upf4")
1717

1818
fixed_seed = 43

0 commit comments

Comments
 (0)