Skip to content

Commit 3c1ae79

Browse files
author
Nabil Brice
committed
reformatted according to black
1 parent a106f04 commit 3c1ae79

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

stingray/pulse/pulsar.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,17 @@ def phase_exposure(start_time, stop_time, period, nbin=16, gti=None):
209209
return expo / np.max(expo)
210210

211211

212-
def fold_events(times, *frequency_derivatives,
213-
mode = "ef",
214-
nbin = 16,
215-
weights = 1,
216-
gti = None,
217-
ref_time = 0,
218-
expocorr = False,
219-
variances = None):
212+
def fold_events(
213+
times,
214+
*frequency_derivatives,
215+
mode="ef",
216+
nbin=16,
217+
weights=1,
218+
gti=None,
219+
ref_time=0,
220+
expocorr=False,
221+
variances=None,
222+
):
220223
"""Epoch folding with exposure correction.
221224
222225
By default, the keyword `times` accepts a list of
@@ -321,7 +324,9 @@ def fold_events(times, *frequency_derivatives,
321324

322325
# For weighted pdm, the measured uncertainties (variances) for each event are required
323326
# set the event_weights as 1/variances or else 1s if variances=None
324-
event_weights = (1.0/np.asarray(variances)) if variances is not None else np.ones_like(weights)
327+
event_weights = (
328+
(1.0 / np.asarray(variances)) if variances is not None else np.ones_like(weights)
329+
)
325330

326331
bins = np.linspace(0, 1, nbin + 1)
327332
# phases are already fractional values in [0, 1]
@@ -331,19 +336,16 @@ def fold_events(times, *frequency_derivatives,
331336
bin_indices = np.clip(bin_indices, 0, nbin - 1)
332337

333338
# for efficiency, the sum-of-squared deviations for each bin is split
334-
# ss_dev = sum_in_bin( (values_in_bin - mean_in_bin)**2 )
339+
# sosdev = sum_in_bin( (values_in_bin - mean_in_bin)**2 )
335340
# mean_in_bin = sum_in_bin(values_in_bin) / n_in_bin
336-
# so need n_in_bin, sum_in_bin, sumofsquares_in_bin
341+
# so need n_in_bin, sum_in_bin, sos_in_bin (sum-of-squares)
337342
# now compute the stats for each bin using bincount
338343
# minlength=nbin prevents array shortening when a bin index is not represented in bin_indices
339-
n_in_bin = np.bincount(bin_indices, weights=event_weights,
340-
minlength=nbin)
341-
sum_in_bin = np.bincount(bin_indices, weights=weights * event_weights,
342-
minlength=nbin)
343-
sumofsquares_in_bin = np.bincount(bin_indices, weights=weights**2 * event_weights,
344-
minlength=nbin)
345-
# put it together; avoid division by zero
346-
raw_profile = sumofsquares_in_bin - sum_in_bin**2 / np.maximum(n_in_bin, 1)
344+
n_in_bin = np.bincount(bin_indices, weights=event_weights, minlength=nbin)
345+
sum_in_bin = np.bincount(bin_indices, weights=weights * event_weights, minlength=nbin)
346+
sos_in_bin = np.bincount(bin_indices, weights=weights**2 * event_weights, minlength=nbin)
347+
# put it together to make sum-of-squared deviations: sosdev; avoid division by zero
348+
raw_profile = sos_in_bin - sum_in_bin**2 / np.maximum(n_in_bin, 1)
347349

348350
# dummy array for the error, which we don't have for the variance
349351
raw_profile_err = np.zeros_like(raw_profile)

stingray/pulse/search.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,15 @@ def stat_fun(t, f, fd=0, **kwargs):
172172

173173

174174
def phase_dispersion_search(
175-
times, flux, frequencies, nbin=128, segment_size=5000, expocorr=False, gti=None, fdots=0,
176-
variances=None
175+
times,
176+
flux,
177+
frequencies,
178+
nbin=128,
179+
segment_size=5000,
180+
expocorr=False,
181+
gti=None,
182+
fdots=0,
183+
variances=None,
177184
):
178185
"""Performs folding at trial frequencies in time series data (i.e.~a light curve
179186
of flux or photon counts) and computes the Phase Dispersion Minimization statistic.
@@ -232,13 +239,17 @@ def stat_fun(t, f, fd=0, **kwargs):
232239
len_flux = len(flux)
233240
# Get the local (possibly modified copy of variances) from kwargs rather than as a closure
234241
variances_local = kwargs.get("variances", None)
235-
event_weights = (1.0/np.asarray(variances_local)) if variances_local is not None else np.ones_like(flux)
242+
event_weights = (
243+
(1.0 / np.asarray(variances_local))
244+
if variances_local is not None
245+
else np.ones_like(flux)
246+
)
236247
mean_grand = np.average(flux, weights=event_weights)
237-
dev2_grand = (flux - mean_grand)**2
238-
239-
sum_dev2_grand = np.dot( dev2_grand, event_weights )
248+
dev2_grand = (flux - mean_grand) ** 2
249+
250+
sosdev_grand = np.dot(dev2_grand, event_weights)
240251

241-
return pdm_profile_stat(profile, sum_dev2_grand, len_flux)
252+
return pdm_profile_stat(profile, sosdev_grand, len_flux)
242253

243254
return _folding_search(
244255
stat_fun,

0 commit comments

Comments
 (0)