Skip to content

Commit

Permalink
Merge branch 'lightkurve:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Nschanche authored Nov 12, 2024
2 parents 3cf34cd + 681e523 commit 1d34343
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 46 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
1.1.0dev (2024-09-18)
1.1.1 (2024-09-24)
=====================
- Updated pyproject.toml

1.1.0 (2024-09-24)
=====================

- Added the option to use the initial TESS commissioning PRF files [#9]
Expand Down
Binary file removed docs/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ You can install lkprf using pip:
pip install lkprf --upgrade
```

> :question: **Note**: If installing in a new environment you may run into issues with the fitsio build that causes an installation error. If so, you can pip install numpy< 2.0.0 first, then pip install lkprf.
## Usage

> :question: **Note**: `lkprf` uses tuples to specify positions and uses the Python convention of `(row, column)`.
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "lkprf"
version = "1.1.0dev"
version = "1.1.1"
description = ""
authors = ["TESS Science Support Center <[email protected]>, Christina Hedges <[email protected]>"]
license = "MIT"
Expand All @@ -10,6 +10,7 @@ packages = [{include = "lkprf", from = "src"}]
[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
scipy = ">=1.8.0"
numpy = ">=1.20.0,<2.0.0"
fitsio = "^1.2.4"
requests = "^2.32.3"

Expand Down
13 changes: 8 additions & 5 deletions src/lkprf/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from scipy.ndimage import label, uniform_filter

from . import logger, PACKAGEDIR
CACHEDIR = PACKAGEDIR + '/data/'

CACHEDIR = PACKAGEDIR + "/data/"

__all__ = [
"download_kepler_prf_file",
Expand Down Expand Up @@ -138,7 +139,7 @@ def open_file(url: str):
)
prefix = _tess_prefixes[camera][ccd]
filename = f"tess-prf-cam{camera}-ccd{ccd}-sec4.fits"

file_path = f"{cache_dir}{filename}"
# ensure the file_path exists
if not os.path.exists(file_path):
Expand Down Expand Up @@ -170,12 +171,14 @@ def get_kepler_prf_file(module: int, output: int, cache_dir: str = CACHEDIR):
f"No local files found, building Kepler PRF for Module {module}, output {output}."
)
download_kepler_prf_file(module=module, output=output, cache_dir=cache_dir)

hdulist = fitsio.FITS(file_path)
return hdulist


def get_tess_prf_file(camera: int, ccd: int, sector: int = 4, cache_dir: str = CACHEDIR):
def get_tess_prf_file(
camera: int, ccd: int, sector: int = 4, cache_dir: str = CACHEDIR
):
"""Get a PRF file for a given camera/ccd/sector"""
if sector <= 3:
filename = f"tess-prf-cam{camera}-ccd{ccd}-sec1.fits"
Expand All @@ -187,7 +190,7 @@ def get_tess_prf_file(camera: int, ccd: int, sector: int = 4, cache_dir: str = C
f"No local files found, building TESS PRF for Camera {camera}, CCD {ccd}."
)
build_tess_prf_file(camera=camera, ccd=ccd, sector=sector, cache_dir=cache_dir)

hdulist = fitsio.FITS(file_path)
return hdulist

Expand Down
27 changes: 14 additions & 13 deletions src/lkprf/keplerprf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@ class KeplerPRF(PRF):
https://archive.stsci.edu/missions/kepler/commissioning_prfs/
"""

def __init__(self, channel: int, cache_dir: str = PACKAGEDIR + '/data/'):
def __init__(self, channel: int, cache_dir: str = PACKAGEDIR + "/data/"):
super().__init__()
self.channel = channel
self.mission = "Kepler"
self.cache_dir = cache_dir
self._prepare_prf()


def __repr__(self):
return f"KeplerPRF Object [Channel {self.channel}]"

def _get_prf_data(self):
module, output = channel_to_module_output(self.channel)
return get_kepler_prf_file(module=module, output=output, cache_dir = self.cache_dir)
return get_kepler_prf_file(
module=module, output=output, cache_dir=self.cache_dir
)

def check_coordinates(self, targets: List[Tuple], shape: Tuple):
row, column = self._unpack_targets(targets)
Expand All @@ -63,7 +64,7 @@ def check_coordinates(self, targets: List[Tuple], shape: Tuple):
LKPRFWarning,
)
return

def _prepare_supersamp_prf(self, targets: List[Tuple], shape: Tuple):
row, column = self._unpack_targets(targets)
# Set the row and column for the model
Expand All @@ -86,23 +87,23 @@ def _prepare_supersamp_prf(self, targets: List[Tuple], shape: Tuple):
# Kepler has 5 measurements, so nearest 3 triangulates the measurement
prf_weights = [
np.sqrt(
(ref_column - self.crval1p[i]) ** 2 + (ref_row - self.crval2p[i]) ** 2)
for i in range(self.PRFdata.shape[0])
]
(ref_column - self.crval1p[i]) ** 2 + (ref_row - self.crval2p[i]) ** 2
)
for i in range(self.PRFdata.shape[0])
]
idx = np.argpartition(prf_weights, 3)[:3]

for i in idx:
if prf_weights[i] < min_prf_weight:
prf_weights[i] = min_prf_weight
supersamp_prf += self.PRFdata[i] / prf_weights[i]


supersamp_prf /= np.nansum(supersamp_prf) * self.cdelt1p[0] * self.cdelt2p[0]

# Set up the interpolation function
self.interpolate = RectBivariateSpline(self.PRFrow, self.PRFcol, supersamp_prf)
return

def _update_coordinates(self, targets: List[Tuple], shape: Tuple):
row, column = self._unpack_targets(targets)
# Set the row and column for the model
Expand All @@ -123,17 +124,17 @@ def _update_coordinates(self, targets: List[Tuple], shape: Tuple):
# Kepler has 5 measurements, so nearest 3 triangulates the measurement
prf_weights = [
np.sqrt(
(ref_column - self.crval1p[i]) ** 2 + (ref_row - self.crval2p[i]) ** 2)
for i in range(self.PRFdata.shape[0])
]
(ref_column - self.crval1p[i]) ** 2 + (ref_row - self.crval2p[i]) ** 2
)
for i in range(self.PRFdata.shape[0])
]
idx = np.argpartition(prf_weights, 3)[:3]

for i in idx:
if prf_weights[i] < min_prf_weight:
prf_weights[i] = min_prf_weight
supersamp_prf += self.PRFdata[i] / prf_weights[i]


supersamp_prf /= np.nansum(supersamp_prf) * self.cdelt1p[0] * self.cdelt2p[0]
self.interpolate = RectBivariateSpline(self.PRFrow, self.PRFcol, supersamp_prf)
return
13 changes: 5 additions & 8 deletions src/lkprf/prfmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _evaluate(
The (row, column) origin of the image in pixels.
Combined with shape this sets the extent of the image.
shape : Tuple
The (N_row, N_col) shape of the image.
The (N_row, N_col) shape of the image.
Combined with the origin this sets the extent of the image.
Returns
Expand All @@ -76,8 +76,8 @@ def _evaluate(
Three dimensional array representing the PRF values parametrized by flux and centroids.
Has shape (ntargets, shape[0], shape[1])
"""
#self.update_coordinates(targets=targets, shape=shape)

# self.update_coordinates(targets=targets, shape=shape)
target_row, target_column = self._unpack_targets(targets)

# Integer extent from the PRF model
Expand Down Expand Up @@ -197,7 +197,7 @@ def _prepare_prf(self):
"""

hdulist = self._get_prf_data()
self.date = hdulist[0].read_header()['DATE']
self.date = hdulist[0].read_header()["DATE"]
PRFdata, crval1p, crval2p, cdelt1p, cdelt2p = [], [], [], [], []
for hdu in hdulist[1:]:
PRFdata.append(hdu.read())
Expand All @@ -223,7 +223,6 @@ def _prepare_prf(self):
PRFcol = (PRFcol - np.size(PRFcol) / 2) * cdelt1p[0]
PRFrow = (PRFrow - np.size(PRFrow) / 2) * cdelt2p[0]


(
self.PRFrow,
self.PRFcol,
Expand All @@ -234,8 +233,6 @@ def _prepare_prf(self):
self.cdelt2p,
) = (PRFrow, PRFcol, PRFdata, crval1p, crval2p, cdelt1p, cdelt2p)



@abstractmethod
def check_coordinates(self, targets, shape):
"""Method to check if selected pxels contain collatoral pixels
Expand All @@ -251,4 +248,4 @@ def _prepare_supersamp_prf(self, targets, shape):
This method sets up the RectBivariateSpline function to interpolate the supersampled PRF
"""
pass
pass
43 changes: 26 additions & 17 deletions src/lkprf/tessprf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@

class TESSPRF(PRF):
"""A TESSPRF class. The TESS PRF measurements are supersampled by a factor of 9.
Two PRF models were produced, one for sectors 1-3 and a second set for sectors 4+ """

def __init__(self, camera: int, ccd: int, sector: int = 4, cache_dir: str = PACKAGEDIR + '/data/'):
Two PRF models were produced, one for sectors 1-3 and a second set for sectors 4+"""

def __init__(
self,
camera: int,
ccd: int,
sector: int = 4,
cache_dir: str = PACKAGEDIR + "/data/",
):
super().__init__()
self.camera = camera
self.ccd = ccd
Expand All @@ -29,7 +35,12 @@ def __repr__(self):
return f"TESSPRF Object [Camera {self.camera}, CCD {self.ccd}, Sector {self.sector}]"

def _get_prf_data(self):
return get_tess_prf_file(camera=self.camera, ccd=self.ccd, sector=self.sector, cache_dir=self.cache_dir)
return get_tess_prf_file(
camera=self.camera,
ccd=self.ccd,
sector=self.sector,
cache_dir=self.cache_dir,
)

def check_coordinates(self, targets: List[Tuple], shape: Tuple):
row, column = self._unpack_targets(targets)
Expand All @@ -54,11 +65,10 @@ def check_coordinates(self, targets: List[Tuple], shape: Tuple):
)

return


def _prepare_supersamp_prf(self, targets: List[Tuple], shape: Tuple):
row, column = self._unpack_targets(targets)
# Set the row and column for the model.
# Set the row and column for the model.

# Create one supersampled PRF for each image, used for all targets in the image
row, column = np.atleast_1d(row), np.atleast_1d(column)
Expand All @@ -77,27 +87,26 @@ def _prepare_supersamp_prf(self, targets: List[Tuple], shape: Tuple):
# Find the 4 measured PRF models nearest the target location
prf_weights = [
np.sqrt(
(ref_column - self.crval1p[i]) ** 2 + (ref_row - self.crval2p[i]) ** 2)
for i in range(self.PRFdata.shape[0])
]
(ref_column - self.crval1p[i]) ** 2 + (ref_row - self.crval2p[i]) ** 2
)
for i in range(self.PRFdata.shape[0])
]
idx = np.argpartition(prf_weights, 4)[:4]

for i in idx:
if prf_weights[i] < min_prf_weight:
prf_weights[i] = min_prf_weight
supersamp_prf += self.PRFdata[i] / prf_weights[i]


supersamp_prf /= np.nansum(supersamp_prf) * self.cdelt1p[0] * self.cdelt2p[0]

# Set up the interpolation function
self.interpolate = RectBivariateSpline(self.PRFrow, self.PRFcol, supersamp_prf)
return


def _update_coordinates(self, targets: List[Tuple], shape: Tuple):
row, column = self._unpack_targets(targets)
# Set the row and column for the model.
# Set the row and column for the model.
# Disallows models in the collateral pixels
row, column = np.atleast_1d(row), np.atleast_1d(column)
row = np.min([np.max([row, row**0 * 0], axis=0), row**0 * 2048], axis=0).mean()
Expand All @@ -115,17 +124,17 @@ def _update_coordinates(self, targets: List[Tuple], shape: Tuple):
# Find the 4 measurements nearest the desired locations
prf_weights = [
np.sqrt(
(ref_column - self.crval1p[i]) ** 2 + (ref_row - self.crval2p[i]) ** 2)
for i in range(self.PRFdata.shape[0])
]
(ref_column - self.crval1p[i]) ** 2 + (ref_row - self.crval2p[i]) ** 2
)
for i in range(self.PRFdata.shape[0])
]
idx = np.argpartition(prf_weights, 4)[:4]

for i in idx:
if prf_weights[i] < min_prf_weight:
prf_weights[i] = min_prf_weight
supersamp_prf += self.PRFdata[i] / prf_weights[i]


supersamp_prf /= np.nansum(supersamp_prf) * self.cdelt1p[0] * self.cdelt2p[0]
self.interpolate = RectBivariateSpline(self.PRFrow, self.PRFcol, supersamp_prf)
return
2 changes: 1 addition & 1 deletion src/lkprf/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# It is important to store the version number in a separate file
# so that we can read it without importing the package
__version__ = "1.1.0dev"
__version__ = "1.1.1"

0 comments on commit 1d34343

Please sign in to comment.