Skip to content

Commit

Permalink
Add function to dynamical power spectrum to bin by number of time int…
Browse files Browse the repository at this point in the history
…ervals
  • Loading branch information
matteobachetti committed Dec 1, 2023
1 parent 1370677 commit 77f98c8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
75 changes: 73 additions & 2 deletions stingray/crossspectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,7 @@ def _make_matrix(self, data1, data2):
self.time = tstart + 0.5 * self.segment_size
self.df = avg.df
self.dt = self.segment_size
self.m = 1

Check warning on line 2016 in stingray/crossspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/crossspectrum.py#L2016

Added line #L2016 was not covered by tests

def rebin_frequency(self, df_new, method="sum"):
"""
Expand All @@ -2038,14 +2039,16 @@ def rebin_frequency(self, df_new, method="sum"):
new_dynspec_object = copy.deepcopy(self)
dynspec_new = []
for data in self.dyn_ps.T:
freq_new, bin_counts, bin_err, _ = rebin_data(
freq_new, bin_counts, bin_err, step = rebin_data(

Check warning on line 2042 in stingray/crossspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/crossspectrum.py#L2042

Added line #L2042 was not covered by tests
self.freq, data, dx_new=df_new, method=method
)
dynspec_new.append(bin_counts)

new_dynspec_object.freq = freq_new
new_dynspec_object.dyn_ps = np.array(dynspec_new).T
new_dynspec_object.df = df_new
new_dynspec_object.m = int(step) * self.m

Check warning on line 2050 in stingray/crossspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/crossspectrum.py#L2050

Added line #L2050 was not covered by tests

return new_dynspec_object

def rebin_time(self, dt_new, method="sum"):
Expand Down Expand Up @@ -2086,14 +2089,81 @@ def rebin_time(self, dt_new, method="sum"):

dynspec_new = []
for data in self.dyn_ps:
time_new, bin_counts, _, _ = rebin_data(
time_new, bin_counts, _, step = rebin_data(

Check warning on line 2092 in stingray/crossspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/crossspectrum.py#L2092

Added line #L2092 was not covered by tests
self.time, data, dt_new, method=method, dx=self.dt
)
dynspec_new.append(bin_counts)

new_dynspec_object.time = time_new
new_dynspec_object.dyn_ps = np.array(dynspec_new)
new_dynspec_object.dt = dt_new
new_dynspec_object.m = int(step) * self.m
return new_dynspec_object

Check warning on line 2101 in stingray/crossspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/crossspectrum.py#L2100-L2101

Added lines #L2100 - L2101 were not covered by tests

def rebin_by_n_intervals(self, n, method="sum"):
"""
Rebin the Dynamic Power Spectrum to a new time resolution.
Note: this is *not* changing the time resolution of the input light
curve! ``dt`` is the integration time of each line of the dynamical power
spectrum (typically, an integer multiple of ``segment_size``).
While the new resolution does not need to be an integer of the previous time
resolution, be aware that if this is the case, the last time bin will be cut
off by the fraction left over by the integer division
Parameters
----------
n: int
The number of intervals to be combined into one.
method: {"sum" | "mean" | "average"}, optional, default "sum"
This keyword argument sets whether the counts in the new bins
should be summed or averaged.
Returns
-------
time_new: numpy.ndarray
Time axis with new rebinned time resolution.
dynspec_new: numpy.ndarray
New rebinned Dynamical Cross Spectrum.
"""
if not np.issubdtype(type(n), np.integer):
warnings.warn("n must be an integer. Converting")

Check warning on line 2133 in stingray/crossspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/crossspectrum.py#L2132-L2133

Added lines #L2132 - L2133 were not covered by tests

n = int(n)

Check warning on line 2135 in stingray/crossspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/crossspectrum.py#L2135

Added line #L2135 was not covered by tests

if n == 1:
return copy.deepcopy(self)
if n < 1:
raise ValueError("n must be >= 1")

Check warning on line 2140 in stingray/crossspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/crossspectrum.py#L2137-L2140

Added lines #L2137 - L2140 were not covered by tests

new_dynspec_object = copy.deepcopy(self)

Check warning on line 2142 in stingray/crossspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/crossspectrum.py#L2142

Added line #L2142 was not covered by tests

dynspec_new = []
time_new = []
for i, data in enumerate(self.dyn_ps.T):
if i % n == 0:
count = 1
bin_counts = data
bin_times = self.time[i]

Check warning on line 2150 in stingray/crossspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/crossspectrum.py#L2144-L2150

Added lines #L2144 - L2150 were not covered by tests
else:
count += 1
bin_counts += data
bin_times += self.time[i]
if count == n:
if method in ["mean", "average"]:
bin_counts /= n
dynspec_new.append(bin_counts)
bin_times /= n
time_new.append(bin_times)

Check warning on line 2160 in stingray/crossspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/crossspectrum.py#L2152-L2160

Added lines #L2152 - L2160 were not covered by tests

new_dynspec_object.time = time_new
new_dynspec_object.dyn_ps = np.array(dynspec_new).T
new_dynspec_object.dt *= n
new_dynspec_object.m = n * self.m

Check warning on line 2165 in stingray/crossspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/crossspectrum.py#L2162-L2165

Added lines #L2162 - L2165 were not covered by tests

return new_dynspec_object

def trace_maximum(self, min_freq=None, max_freq=None):
Expand Down Expand Up @@ -2175,6 +2245,7 @@ def power_colors(
frequencies_to_exclude=frequencies_to_exclude,
df=self.df,
poisson_power=poisson_power,
m=self.m,
)
)

Expand Down
1 change: 1 addition & 0 deletions stingray/powerspectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ def _make_matrix(self, lc):
self.dt = self.segment_size
self.meanrate = avg.nphots / avg.n / avg.dt
self.nphots = avg.nphots
self.m = 1

Check warning on line 1010 in stingray/powerspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/powerspectrum.py#L1008-L1010

Added lines #L1008 - L1010 were not covered by tests

def power_colors(
self,
Expand Down

0 comments on commit 77f98c8

Please sign in to comment.