Skip to content

Commit

Permalink
change the padding strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
mpatankar6 committed Oct 8, 2023
1 parent b7add6f commit a63b5bc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
20 changes: 6 additions & 14 deletions lightcurve_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,20 @@ def get_images(self, region_event_list):
dealing with hundreds of thousands of blank pixels. The cropping bounds are written to the
FITS file for later use in plotting."""

def convert_bounds_object_to_hdu(bounds: CropBounds):
hdu = io.fits.table_to_hdu(
table.Table({field: [(getattr(bounds, field))] for field in bounds._fields})
)
hdu.header["EXTNAME"] = "BOUNDS"
return hdu

dmstat(infile=f"{region_event_list}[cols x,y]")
sky_bounds = CropBounds.from_source_region_bounds(
*dmstat.out_min.split(","), *dmstat.out_max.split(",")
)
sky_bounds = CropBounds.from_strings(*dmstat.out_min.split(","), *dmstat.out_max.split(","))
sky_bounds.double()
dmcopy(
infile=f"{self.event_list}"
f"[bin x={sky_bounds.x_min}:{sky_bounds.x_max}:0.5,"
f"y={sky_bounds.y_min}:{sky_bounds.y_max}:0.5]",
outfile=(sky_coords_image := f"{region_event_list}.skyimg.fits"),
)
with io.fits.open(sky_coords_image, mode="append") as hdu_list:
hdu_list.append(convert_bounds_object_to_hdu(sky_bounds))
dmstat(infile=f"{region_event_list}[cols detx,dety]")
hdu_list.append(sky_bounds.to_hdu())

detector_bounds = CropBounds.from_source_region_bounds(
dmstat(infile=f"{region_event_list}[cols detx,dety]")
detector_bounds = CropBounds.from_strings(
*dmstat.out_min.split(","), *dmstat.out_max.split(",")
)
dmcopy(
Expand All @@ -128,7 +120,7 @@ def convert_bounds_object_to_hdu(bounds: CropBounds):
outfile=(detector_coords_image := f"{region_event_list}.detimg.fits"),
)
with io.fits.open(detector_coords_image, mode="append") as hdu_list:
hdu_list.append(convert_bounds_object_to_hdu(detector_bounds))
hdu_list.append(detector_bounds.to_hdu())
self.sky_coords_image, self.detector_coords_image = sky_coords_image, detector_coords_image

def get_observation_details(self):
Expand Down
40 changes: 25 additions & 15 deletions postage_stamp_plotter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Mihir Patankar [[email protected]]"""
from dataclasses import dataclass, fields
from io import BytesIO
from typing import NamedTuple

import matplotlib
import numpy
Expand Down Expand Up @@ -36,7 +36,7 @@ def plot_postagestamps(sky_image, detector_image):
table (BINTABLE). The detector image is done on a square root scale to give the image more
contrast. The PNG data is returned in a BytesIO object."""
matplotlib.use("agg")
sky_image_data = io.fits.getdata(sky_image, ext=0)
sky_image_data = numpy.sqrt(io.fits.getdata(sky_image, ext=0))
detector_image_data = numpy.sqrt(io.fits.getdata(detector_image, ext=0))
figure, (sky_image_plot, detector_image_plot) = pyplot.subplots(nrows=1, ncols=2)
figure.set_figwidth(10)
Expand All @@ -62,7 +62,7 @@ def plot_postagestamps(sky_image, detector_image):
sky_image_plot.get_xlim(),
)
sky_image_plot.set_xticks(*sky_x_ticks, rotation=90)
detector_image_plot.set_title("Detector-Coordinates Postage Stamp (sqrt Scale)", y=1.05)
detector_image_plot.set_title("Detector-Coordinates Postage Stamp", y=1.05)
detector_image_plot.set_xlabel("Detector-Coordinates X Pixel")
detector_image_plot.set_ylabel("Detector-Coordinates Y Pixel")
detector_image_plot.grid(True, linestyle="-", color="gray")
Expand All @@ -88,26 +88,36 @@ def plot_postagestamps(sky_image, detector_image):
return png_data


class CropBounds(NamedTuple):
@dataclass
class CropBounds:
"""Holds the bounding box of our postage stamp images."""

x_min: float
y_min: float
x_max: float
y_max: float

@classmethod
def from_source_region_bounds(cls, x_min, y_min, x_max, y_max):
"""Gives the image some padding so the background region can be observed. This should be
immune to edge cases where the source region plus padding exceeds the dimensions of the
whole image."""
padding = 20 # In chandra pixels
return cls(
float(x_min) - padding,
float(y_min) - padding,
float(x_max) + padding,
float(y_max) + padding,
def double(self):
"""Double the bounds, useful for including background in the image."""
x_range = self.x_max - self.x_min
self.x_min -= x_range
self.x_max += x_range
y_range = self.y_max - self.y_min
self.y_min -= y_range
self.y_max += y_range

def to_hdu(self):
"""Convert the dataclass to a Header Data Unit for FITS files."""
hdu = io.fits.table_to_hdu(
table.Table({field.name: [(getattr(self, field.name))] for field in fields(self)})
)
hdu.header["EXTNAME"] = "BOUNDS"
return hdu

@classmethod
def from_strings(cls, x_min, y_min, x_max, y_max):
"""Casts a set of string values to floats."""
return cls(float(x_min), float(y_min), float(x_max), float(y_max))

@classmethod
def from_hdu_list(cls, hdu_list):
Expand Down

0 comments on commit a63b5bc

Please sign in to comment.