Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding additions checks for other pixel bin conditions for timerange for create_meta_pixel #138

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/138.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug in stixpy.calibration.visibility.create_meta_pixels to correctly determine which time bins to include in the sum based on their overlap with the specified time range.
41 changes: 41 additions & 0 deletions stixpy/calibration/tests/test_visibility.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import astropy.units as u
import numpy as np
import pytest
from astropy.tests.helper import assert_quantity_allclose
from astropy.time import Time
Expand Down Expand Up @@ -54,3 +55,43 @@ def test_create_meta_pixels(background_cpd):
[0.18701911, 0.18205339, 0.18328145, 0.17945563] * u.ct / (u.keV * u.cm**2 * u.s),
meta_pixels["abcd_rate_kev_cm"][-1, :],
)


def test_create_meta_pixels_timebins(flare_cpd):
energy_range = [6, 12] * u.keV

# check time_range within one bin (i.e. timerange passed is within one bin)
time_range = [flare_cpd.times[0], flare_cpd.times[0] + flare_cpd.duration / 4]
meta_pixels = create_meta_pixels(
flare_cpd,
time_range=time_range,
energy_range=energy_range,
flare_location=STIXImaging(0 * u.arcsec, 0 * u.arcsec),
no_shadowing=True,
)

assert_quantity_allclose(meta_pixels["time_range"].dt.to(u.s), flare_cpd.duration[0].to(u.s))

# check time_range fully outside bins (i.e. all bins contained with timerange)
time_range = [flare_cpd.times[0] - 10 * u.s, flare_cpd.times[-1] + 10 * u.s]
meta_pixels = create_meta_pixels(
flare_cpd,
time_range=time_range,
energy_range=energy_range,
flare_location=STIXImaging(0 * u.arcsec, 0 * u.arcsec),
no_shadowing=True,
)

assert_quantity_allclose(np.sum(flare_cpd.duration), meta_pixels["time_range"].dt.to(u.s))

# check time_range start and end are within bins
time_range = [flare_cpd.times[0], flare_cpd.times[2]]
meta_pixels = create_meta_pixels(
flare_cpd,
time_range=time_range,
energy_range=energy_range,
flare_location=STIXImaging(0 * u.arcsec, 0 * u.arcsec),
no_shadowing=True,
)

assert_quantity_allclose(np.sum(flare_cpd.duration[0:3]), meta_pixels["time_range"].dt.to(u.s))
16 changes: 15 additions & 1 deletion stixpy/calibration/visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,21 @@ def create_meta_pixels(
`dict`
Visibility data and sub-collimator information
"""
t_mask = (pixel_data.times >= Time(time_range[0])) & (pixel_data.times <= Time(time_range[1]))

# checks if a time bin fully overlaps, is fully within, starts within, or ends within the specified time range.
pixel_starts = pixel_data.times - pixel_data.duration / 2
pixel_ends = pixel_data.times + pixel_data.duration / 2

time_range_start = Time(time_range[0])
time_range_end = Time(time_range[1])

t_mask = (
(pixel_starts >= time_range_start) & (pixel_ends <= time_range_end) # fully within the time range
| (time_range_start <= pixel_starts) & (time_range_end >= pixel_ends) # fully overlaps the time range
| (pixel_starts <= time_range_start) & (pixel_ends >= time_range_start) # starts within the time range
| (pixel_starts <= time_range_end) & (pixel_ends >= time_range_end) # ends within the time range
)

e_mask = (pixel_data.energies["e_low"] >= energy_range[0]) & (pixel_data.energies["e_high"] <= energy_range[1])

t_ind = np.argwhere(t_mask).ravel()
Expand Down
Loading