Skip to content

Commit 90aa7de

Browse files
committed
final_touches
1 parent dda5f73 commit 90aa7de

File tree

2 files changed

+67
-93
lines changed

2 files changed

+67
-93
lines changed

src/fourier.jl

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -224,24 +224,15 @@ end
224224
end
225225
end
226226

227-
"""
228-
avg_pds_from_iterable(flux_iterable, dt::Real; kwargs...)
229-
230-
Compute averaged power density spectrum from an iterable of flux segments.
231-
232-
## Parameters
233-
- `flux_iterable`: Iterator of flux segments
234-
- `dt::Real`: Time resolution
235-
- `norm::String="frac"`: Normalization type
236-
- `use_common_mean::Bool=true`: Use common mean across segments
237-
- `silent::Bool=false`: Suppress progress output
238-
"""
239-
function avg_pds_from_iterable(flux_iterable, dt::Real;
240-
norm::String="frac",
241-
use_common_mean::Bool=true,
242-
silent::Bool=false)
243-
244-
# Initialize accumulators
227+
function avg_pds_from_iterable(flux_iterable, dt::Real; norm::String="frac",
228+
use_common_mean::Bool=true,
229+
silent::Bool=false)
230+
local_show_progress = show_progress
231+
if silent
232+
local_show_progress = identity
233+
end
234+
235+
# Initialize stuff
245236
cross = unnorm_cross = nothing
246237
n_ave = 0
247238
freq = nothing
@@ -251,9 +242,8 @@ function avg_pds_from_iterable(flux_iterable, dt::Real;
251242
fgt0 = nothing
252243
n_bin = nothing
253244

254-
for flux in flux_iterable
255-
# Validate segment
256-
if isnothing(flux) || all(iszero, flux)
245+
for flux in local_show_progress(flux_iterable)
246+
if isnothing(flux) || all(iszero,flux)
257247
continue
258248
end
259249

@@ -262,7 +252,7 @@ function avg_pds_from_iterable(flux_iterable, dt::Real;
262252
variance = nothing
263253
if flux isa Tuple
264254
flux, err = flux
265-
variance = Statistics.mean(err)^2
255+
variance = Statistics.mean(err) ^ 2
266256
end
267257

268258
# Calculate the FFT
@@ -279,43 +269,40 @@ function avg_pds_from_iterable(flux_iterable, dt::Real;
279269
sum_of_photons += n_ph
280270

281271
if !isnothing(variance)
282-
common_variance = isnothing(common_variance) ? variance : common_variance + variance
272+
common_variance = sum_if_not_none_or_initialize(common_variance, variance)
283273
end
284274

285275
# In the first loop, define the frequency and the freq. interval > 0
286-
# Initialize frequency arrays
287276
if isnothing(cross)
288277
fgt0 = positive_fft_bins(n_bin)
289-
freq = FFTW.fftfreq(n_bin, dt)[fgt0]
278+
freq = fftfreq(n_bin, dt)[fgt0]
290279
end
291280

292-
keepat!(unnorm_power, fgt0)
281+
# No need for the negative frequencies
282+
keepat!(unnorm_power,fgt0)
293283

294284
# If the user wants to normalize using the mean of the total
295285
# lightcurve, normalize it here
296286
cs_seg = unnorm_power
297-
if !use_common_mean
298-
mean_flux = n_ph / n_bin
287+
if !(use_common_mean)
288+
mean = n_ph / n_bin
289+
299290
cs_seg = normalize_periodograms(
300-
unnorm_power,
301-
dt,
302-
n_bin;
303-
mean_flux = mean_flux,
304-
n_ph = n_ph,
305-
norm = norm,
306-
power_type = "real"
291+
unnorm_power, dt, n_bin; mean_flux = mean, n_ph=n_ph,
292+
norm=norm, variance=variance,
307293
)
308294
end
309295

310296
# Accumulate the total sum cross spectrum
311297
cross = sum_if_not_none_or_initialize(cross, cs_seg)
312-
unnorm_cross = sum_if_not_none_or_initialize(unnorm_cross, unnorm_power)
298+
unnorm_cross = sum_if_not_none_or_initialize(unnorm_cross,
299+
unnorm_power)
313300

314301
n_ave += 1
315302
end
316303

317-
# Validate results
318-
if isnothing(cross) || n_ave == 0
304+
# If there were no good intervals, return nothing
305+
if isnothing(cross)
319306
return nothing
320307
end
321308

@@ -337,28 +324,19 @@ function avg_pds_from_iterable(flux_iterable, dt::Real;
337324
# Final normalization (If not done already!)
338325
if use_common_mean
339326
cross = normalize_periodograms(
340-
unnorm_cross,
341-
dt,
342-
n_bin;
343-
mean_flux = common_mean,
344-
n_ph = n_ph,
345-
norm = norm,
346-
power_type = "real"
327+
unnorm_cross, dt, n_bin; mean_flux=common_mean, n_ph=n_ph,
328+
norm=norm, variance=common_variance
347329
)
348330
end
349331

350-
# Calculate power errors
351-
power_errors = cross ./ sqrt(n_ave)
332+
results = DataFrame()
333+
results[!,"freq"] = freq
334+
results[!,"power"] = cross
335+
results[!,"unnorm_power"] = unnorm_cross
336+
results = (; results , n = n_bin, m = n_ave, dt, norm, df = 1 / (dt * n_bin), nphots = n_ph, setment_size = dt * n_bin, mean = common_mean, variance = common_variance)
337+
results = DataFrame(results.results)
338+
return results
352339

353-
# Return AveragedPowerspectrum
354-
return AveragedPowerspectrum(
355-
freq[2:end],
356-
cross[2:end],
357-
power_errors[2:end],
358-
n_ave,
359-
n_bin,
360-
dt
361-
)
362340
end
363341

364342
function avg_cs_from_iterables_quick(flux_iterable1 ,flux_iterable2,
@@ -698,24 +676,33 @@ function avg_cs_from_iterables(
698676
return results
699677

700678
end
701-
function avg_pds_from_events(times::AbstractVector{<:Real}, gti::AbstractMatrix{<:Real},
702-
segment_size::Real, dt::Real; norm::String="frac",
703-
use_common_mean::Bool=true, silent::Bool=false,
704-
fluxes=nothing, errors=nothing)
679+
680+
function avg_pds_from_events(times:: AbstractVector{<:Real}, gti::AbstractMatrix{<:Real},
681+
segment_size::Real, dt::Real; norm::String="frac",
682+
use_common_mean::Bool=true, silent::Bool=false,
683+
fluxes=nothing, errors=nothing)
705684
if isnothing(segment_size)
706685
segment_size = max(gti) - min(gti)
707686
end
708-
n_bin = round(Int, segment_size / dt)
687+
n_bin = round(Int,segment_size / dt)
709688
dt = segment_size / n_bin
710689

711690
flux_iterable = get_flux_iterable_from_segments(times, gti, segment_size;
712-
n_bin, fluxes=fluxes,
713-
errors=errors)
691+
n_bin, fluxes=fluxes,
692+
errors=errors)
693+
cross = avg_pds_from_iterable(flux_iterable, dt, norm=norm,
694+
use_common_mean=use_common_mean,
695+
silent=silent)
696+
697+
if !isnothing(cross)
698+
cross = (; cross, gti=gti)
699+
cross = DataFrame(cross.cross)
700+
end
701+
702+
return cross
714703

715-
return avg_pds_from_iterable(flux_iterable, dt, norm=norm,
716-
use_common_mean=use_common_mean,
717-
silent=silent)
718704
end
705+
719706
function avg_cs_from_events(times1:: AbstractVector{<:Real}, times2:: AbstractVector{<:Real},
720707
gti::AbstractMatrix{<:Real}, segment_size::Real, dt::Real;
721708
norm::String="frac", use_common_mean::Bool=true,
@@ -771,4 +758,4 @@ function avg_cs_from_events(times1:: AbstractVector{<:Real}, times2:: AbstractVe
771758
end
772759

773760
return results
774-
end
761+
end

test/test_fourier.jl

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
function compare_tablespds(ps1::AveragedPowerspectrum, ps2::AveragedPowerspectrum; rtol=0.001, discard=[])
2-
test_result = true
3-
4-
if !isapprox(ps1.freqs, ps2.freqs, rtol=rtol) ||
5-
!isapprox(ps1.power, ps2.power, rtol=rtol) ||
6-
!isapprox(ps1.power_errors, ps2.power_errors, rtol=rtol) ||
7-
ps1.m != ps2.m ||
8-
ps1.n != ps2.n ||
9-
!isapprox(ps1.dt, ps2.dt, rtol=rtol)
10-
test_result = false
11-
end
12-
13-
@test test_result
14-
end
15-
# ----------------------------------------------------------------
16-
# here there are two function's compare_tables(){"with dataframe"} and compare_tablespds(){"without dataframe"} which are meant to be merged
17-
# in future while converting cross spectrum with Struct{}
18-
# ----------------------------------------------------------------
19-
201
function compare_tables(table1, table2; rtol=0.001, discard = [])
212

223
s_discard = Symbol.(discard)
@@ -64,6 +45,7 @@ function compare_tables(table1, table2; rtol=0.001, discard = [])
6445
end
6546
@test test_result
6647
end
48+
6749
@testset "positive_fft_bins" begin
6850
freq = fftfreq(11)
6951
goodbins = positive_fft_bins(11)
@@ -181,7 +163,7 @@ end
181163
@test isnothing(out_ev)
182164
end
183165

184-
@testset "test_avg_pds_use_common_mean_similar_stats" for norm in ["frac", "abs", "none", "leahy"]
166+
@testset "test_avg_pds_use_common_mean_similar_stats" for norm in ["frac", "abs", "none", "leahy"]
185167
out_comm = avg_pds_from_events(
186168
times,
187169
gti,
@@ -191,7 +173,7 @@ end
191173
use_common_mean=true,
192174
silent=true,
193175
fluxes=nothing,
194-
)
176+
).power
195177
out = avg_pds_from_events(
196178
times,
197179
gti,
@@ -201,8 +183,8 @@ end
201183
use_common_mean=false,
202184
silent=true,
203185
fluxes=nothing,
204-
)
205-
@test std(out_comm.power) std(out.power) rtol=0.1
186+
).power
187+
@test std(out_comm)std(out) rtol=0.1
206188
end
207189

208190
@testset "test_avg_cs_use_common_mean_similar_stats" for
@@ -256,7 +238,7 @@ end
256238
silent=true,
257239
fluxes=counts,
258240
)
259-
compare_tablespds(out_ev, out_ct)
241+
compare_tables(out_ev, out_ct)
260242
end
261243

262244
@testset "test_avg_pds_cts_and_err_and_events_are_equal" for use_common_mean in [true,false], norm in ["frac", "abs", "none", "leahy"]
@@ -281,7 +263,12 @@ end
281263
fluxes=counts,
282264
errors=errs,
283265
)
284-
compare_tablespds(out_ev, out_ct)
266+
# The variance is not _supposed_ to be equal, when we specify errors
267+
if use_common_mean
268+
compare_tables(out_ev, out_ct, rtol=0.01, discard=["variance"])
269+
else
270+
compare_tables(out_ev, out_ct, rtol=0.1, discard=["variance"])
271+
end
285272
end
286273

287274
@testset "test_avg_cs_cts_and_events_are_equal" for use_common_mean in [true,false], norm in ["frac", "abs", "none", "leahy"]
@@ -435,4 +422,4 @@ end
435422
pdsnorm = normalize_periodograms(pds, dt, N; mean_flux=mean, n_ph=nph, norm="none")
436423
@test Statistics.mean(pdsnorm)Statistics.mean(pds) rtol=0.01
437424
end
438-
end
425+
end

0 commit comments

Comments
 (0)