Skip to content

Commit

Permalink
Works on new updated library versions: run_photometry.py --targetid 5…
Browse files Browse the repository at this point in the history
…06 --fileid 2966 --filter all
  • Loading branch information
Your Name committed Sep 11, 2024
1 parent dcd1084 commit 84119d7
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 31 deletions.
8 changes: 4 additions & 4 deletions flows/catalogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,10 @@ def query_all(coo_centre, radius=24 * u.arcmin, dist_cutoff=2 * u.arcsec):

# REFCAT results table does not have uBV
N = len(AT_results)
d = np.full(N, np.NaN)
AT_results.add_column(MaskedColumn(name='B_mag', unit='mag', dtype='float32', fill_value=np.NaN, data=d))
AT_results.add_column(MaskedColumn(name='V_mag', unit='mag', dtype='float32', fill_value=np.NaN, data=d))
AT_results.add_column(MaskedColumn(name='u_mag', unit='mag', dtype='float32', fill_value=np.NaN, data=d))
d = np.full(N, np.nan)
AT_results.add_column(MaskedColumn(name='B_mag', unit='mag', dtype='float32', fill_value=np.nan, data=d))
AT_results.add_column(MaskedColumn(name='V_mag', unit='mag', dtype='float32', fill_value=np.nan, data=d))
AT_results.add_column(MaskedColumn(name='u_mag', unit='mag', dtype='float32', fill_value=np.nan, data=d))

# Query APASS around the target position:
results_apass = query_apass(coo_centre, radius=radius)
Expand Down
2 changes: 1 addition & 1 deletion flows/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def create_wcs(self) -> WCS:

def create_masked_image(self) -> None:
"""Warning: this is destructive and will overwrite image data setting masked values to NaN"""
self.image[self.mask] = np.NaN
self.image[self.mask] = np.nan
self.clean = np.ma.masked_array(data=self.image, mask=self.mask, copy=False)

def set_edge_rows_to_value(self, y: Tuple[float] = None, value: Union[int, float, np.float64] = 0) -> None:
Expand Down
2 changes: 1 addition & 1 deletion flows/magnitudes.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def instrumental_mag(tab: Table, target: Target, make_fig: bool = False) -> Tupl
if n_weights > 1:
zp_error = np.sqrt(n_weights * nansum(weights * (y - best_fit(x)) ** 2) / nansum(weights) / (n_weights - 1))
else:
zp_error = np.NaN
zp_error = np.nan
logger.info('Leastsquare ZP = %.3f, ZP_error = %.3f', zp, zp_error)

# Determine sigma clipping sigma according to Chauvenet method
Expand Down
43 changes: 33 additions & 10 deletions flows/photometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,15 @@ def make_epsf(self, stars):
"""
# Build ePSF
logger.info("Building ePSF...")

# from photutils docs: recentering_boxsize must have odd values and be greater than or equal to 3
discrete_width = int(np.round(2 * self.fwhm))
rcb = discrete_width if discrete_width % 2 else discrete_width + 1

builder = self.epsf_builder(
oversampling=1, shape=1 * self.star_size,
fitter=EPSFFitter(fit_boxsize=max(int(np.round(1.5 * self.fwhm)), 5)),
recentering_boxsize=max(int(np.round(2 * self.fwhm)), 5),
recentering_boxsize=max(rcb, 5),
norm_radius=max(self.fwhm, 5), maxiters=100,
progress_bar=multiprocessing.parent_process() is None and logger.getEffectiveLevel() <= 20
)
Expand Down Expand Up @@ -161,16 +166,32 @@ def apphot(self, coordinates: ArrayLike, image: FlowsImage, fwhm: float, use_raw
def create_photometry_object(self, fwhm: Union[float, u.Quantity], psf_model: photutils.psf.EPSFModel,
fitsize: Union[int, Tuple[int]], fitter: Callable = fitting.LevMarLSQFitter(),
bkg: PhotutilsBackground = MedianBackground()):
self.photometry_obj = PSFPhotometry(group_maker=SourceGrouper(fwhm), bkg_estimator=bkg, psf_model=psf_model,
fitter=fitter, fitshape=fitsize, aperture_radius=fwhm)
# 2024-09 erik:
# photutils version 1.1.0 (or possibly lower?):
# self.photometry_obj = PSFPhotometry(group_maker=SourceGrouper(fwhm), bkg_estimator=bkg, psf_model=psf_model,
# fitter=fitter, fitshape=fitsize, aperture_radius=fwhm)
# It seems that 'group_maker' -> 'grouper', 'bkg_estimator' -> 'localbkg_estimator', 'fitshape' -> 'fit_shape'
# in newer photutils versions.
# In addition, localbkg_estimator requires a LocalBackground object which in turn requires us to specify an
# inner and an outer radius, which we have not been doing previously... Comments/suggestions, anyone?
self.photometry_obj = PSFPhotometry(psf_model=psf_model, fit_shape=fitsize, grouper=SourceGrouper(fwhm),
fitter=fitter,
localbkg_estimator=photutils.background.LocalBackground(inner_radius=5,
outer_radius=10,
bkg_estimator=bkg),
aperture_radius=fwhm)

def psfphot(self, image: ArrayLike, init_table: Table) -> Tuple[PSFPhotometry, Table]:
"""PSF photometry on init guesses table/row.
"""
if self.photometry_obj is None:
raise ValueError('Photometry object not initialized.')
# logger.info(f"{init_table}")
output: Table = self.photometry_obj(image=image, init_guesses=init_table)
# 2024-09 erik:
# 'image' and 'init_guesses' not recognised.
# Trying to supply image as implicit 'data' and doing 'init_guesses' -> 'init_params'.
# output: Table = self.photometry_obj(image=image, init_guesses=init_table)
output: Table = self.photometry_obj(image, init_params=init_table)
return self.photometry_obj, output

@staticmethod
Expand All @@ -182,7 +203,9 @@ def rescale_flux_error(phot_tables: Dict[int, Table],

for fit_shape, row in phot_tables.items():
row = row[0] if select_first_row else row
new_err = row['flux_unc'] / exptime
# 'flux_unc' -> 'flux_err' at some version transition of photutils (not documented in changelog,
# found with debugger).
new_err = row['flux_err'] / exptime
new_flux = row['flux_fit'] / exptime
if new_flux <= flux + flux_err:
return fit_shape, new_err
Expand Down Expand Up @@ -355,7 +378,7 @@ def rescale_uncertainty(self, psfphot_tbl: Table, dynamic: bool = True,
if self.diff_im_exists:
_table = self.diff_psf_phot()
_table = self.raw_psf_phot(self.init_guesses.init_guess_target)
if "flux_unc" in _table.colnames:
if "flux_err" in _table.colnames:
_phot_tables_dict[fitshape] = _table

if len(_phot_tables_dict) == 0:
Expand All @@ -364,7 +387,7 @@ def rescale_uncertainty(self, psfphot_tbl: Table, dynamic: bool = True,
else:
# Find the fit shape elbow:
flux = psfphot_tbl[0]['flux_fit']
flux_err = psfphot_tbl[0]['flux_unc']
flux_err = psfphot_tbl[0]['flux_err']
exptime = self.image.exptime
dynamic_fit_shape, new_err = self.photometry.rescale_flux_error(_phot_tables_dict, flux, flux_err,
exptime)
Expand All @@ -374,13 +397,13 @@ def rescale_uncertainty(self, psfphot_tbl: Table, dynamic: bool = True,
logger.info(f"Recalculating all reference uncertainties using new fitsize:"
f" {fit_shape} pixels, ({fit_shape/self.fwhm if dynamic else static_fwhm :.2} * FWHM).")
psfphot_tbl_rescaled = self.psfphot(fit_shape)
if psfphot_tbl['flux_unc'][0] > psfphot_tbl_rescaled['flux_unc'][0] + epsilon_mag and ensure_greater:
if psfphot_tbl['flux_err'][0] > psfphot_tbl_rescaled['flux_err'][0] + epsilon_mag and ensure_greater:
logger.info("Recalculated uncertainties were smaller than original and ``ensure_greater`` was True:"
"Not using rescaled uncertainties for the SN.")
psfphot_tbl['flux_unc'][1:] = psfphot_tbl_rescaled['flux_unc'][1:]
psfphot_tbl['flux_err'][1:] = psfphot_tbl_rescaled['flux_err'][1:]
return psfphot_tbl

psfphot_tbl['flux_unc'] = psfphot_tbl_rescaled['flux_unc']
psfphot_tbl['flux_err'] = psfphot_tbl_rescaled['flux_err']
return psfphot_tbl

def make_result_table(self, psfphot_tbl: Table, apphot_tbl: Table):
Expand Down
18 changes: 9 additions & 9 deletions flows/reference_cleaning.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class References:
xy: Optional[RefTable] = None

def replace_nans_pm(self) -> None:
replace(self.table['pm_ra'], np.NaN, 0.)
replace(self.table['pm_dec'], np.NaN, 0.)
replace(self.table['pm_ra'], np.nan, 0.)
replace(self.table['pm_dec'], np.nan, 0.)

def make_sky_coords(self, reference_time: float = 2015.5) -> None:
self.replace_nans_pm()
Expand Down Expand Up @@ -105,9 +105,9 @@ def add_target(self, target: Target, starid: int = 0) -> None:
def use_sep(image: FlowsImage, tries: int = 5, thresh: float = 5.):

# Use sep to for soure extraction
sep_background = sep.Background(image.image, mask=image.mask)
sep_background = sep_pjw.Background(image.image, mask=image.mask)
try:
objects = sep.extract(image.image - sep_background, thresh=thresh, err=sep_background.globalrms,
objects = sep_pjw.extract(image.image - sep_background, thresh=thresh, err=sep_background.globalrms,
mask=image.mask, deblend_cont=0.1, minarea=9, clean_param=2.0)
except KeyboardInterrupt as e:
raise e
Expand Down Expand Up @@ -161,9 +161,9 @@ def force_reject_g2d(xarray: ArrayLike, yarray: ArrayLike, image: Union[NDArray,

# Stars reject
N = len(xarray)
fwhms = np.full((N, 2), np.NaN)
xys = np.full((N, 2), np.NaN)
rsqs = np.full(N, np.NaN)
fwhms = np.full((N, 2), np.nan)
xys = np.full((N, 2), np.nan)
rsqs = np.full(N, np.nan)
for i, (x, y) in enumerate(zip(xarray, yarray)):
x = int(np.round(x))
y = int(np.round(y))
Expand All @@ -185,8 +185,8 @@ def force_reject_g2d(xarray: ArrayLike, yarray: ArrayLike, image: Union[NDArray,
nan_filter = nan_filter & np.isfinite(curr_star)
if len(curr_star[nan_filter]) < 3: # Not enough pixels to fit
logger.debug(f"Not enough pixels to fit star, curr_star[nan_filter]:{curr_star[nan_filter]}")
rsqs[i] = np.NaN
fwhms[i] = np.NaN
rsqs[i] = np.nan
fwhms[i] = np.nan
continue

gfit = gfitter(g2d, x=xpos[nan_filter], y=ypos[nan_filter], z=curr_star[nan_filter])
Expand Down
12 changes: 7 additions & 5 deletions flows/result_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,19 @@ def make_results_table(cls, ref_table: Table, apphot_tbl: Table, psfphot_tbl: Ta
results_table['flux_aperture'] = apphot_tbl['flux_aperture'] / image.exptime
results_table['flux_aperture_error'] = apphot_tbl['flux_aperture_error'] / image.exptime
results_table['flux_psf'] = psfphot_tbl['flux_fit'] / image.exptime
results_table['flux_psf_error'] = psfphot_tbl['flux_unc'] / image.exptime
results_table['flux_psf_error'] = psfphot_tbl['flux_err'] / image.exptime
results_table['pixel_column_psf_fit'] = psfphot_tbl['x_fit']
results_table['pixel_row_psf_fit'] = psfphot_tbl['y_fit']
results_table['pixel_column_psf_fit_error'] = psfphot_tbl['x_0_unc']
results_table['pixel_row_psf_fit_error'] = psfphot_tbl['y_0_unc']
# 'x_0_unc' -> 'x_err' and 'y_0_unc' -> 'y_err' at some version transition of photutils (not documented in
# changelog, found with debugger).
results_table['pixel_column_psf_fit_error'] = psfphot_tbl['x_err']
results_table['pixel_row_psf_fit_error'] = psfphot_tbl['y_err']

return results_table

@staticmethod
def verify_uncertainty_column(tab):
if "flux_unc" in tab.colnames:
if "flux_err" in tab.colnames:
return tab
tab['flux_unc'] = tab['flux_fit'] * 0.04 # Assume 4% errors
tab['flux_err'] = tab['flux_fit'] * 0.04 # Assume 4% errors
logger.warning("Flux uncertainty not found from PSF fit, assuming 4% error.")
2 changes: 1 addition & 1 deletion notes/LCOGT_Reduce_SingleStandards.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@
" dict(x_0=coordinates.centered_x[i], y_0=coordinates.centered_y[i])\n",
" ])\n",
" )\n",
" coordinates.psf.append((res['flux_fit'].data[0], res['flux_unc'].data[0]))\n",
" coordinates.psf.append((res['flux_fit'].data[0], res['flux_err'].data[0]))\n",
"\n",
"if verbose: \n",
" print('Psf Phot Success')\n",
Expand Down
21 changes: 21 additions & 0 deletions requirements.txt.erik
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
numpy-2.1.1
scipy-1.14.1
astropy-6.1.3
photutils-1.13.0
bottleneck-1.4.0
matplotlib-3.9.2
mplcursors-0.5.3
seaborn-0.13.2
pandas-2.2.2
requests-2.32.3
pyyaml-6.0.2
psycopg2-binary-2.9.9
jplephem-2.22
scikit_image-0.24.0
tqdm-4.66.5
pytz-2024.2
sep_pjw-1.3.5
astroalign-2.5.1
networkx-3.3
astroquery-0.4.7
tendrils-0.3.2

0 comments on commit 84119d7

Please sign in to comment.