Skip to content
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
29 changes: 21 additions & 8 deletions flows/photometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,11 @@ def diff_psf_phot(self) -> Table:
init_table=self.init_guesses_diff.init_guess_diff)
return psf_tbl

def rescale_uncertainty(self, psfphot_tbl: Table, dynamic: bool = True, static_fwhm: float = 2.5):
def rescale_uncertainty(self, psfphot_tbl: Table, dynamic: bool = True,
static_fwhm: float = 2.5, epsilon_mag: float = 0.004,
ensure_greater: bool = True):
"""
Rescale the uncertainty of the PSF photometry to match the uncertainty of the
photometry.
Rescale the uncertainty of the PSF photometry using a variable fitsize.

Parameters
----------
Expand All @@ -329,6 +330,9 @@ def rescale_uncertainty(self, psfphot_tbl: Table, dynamic: bool = True, static_f
Dynamically decide FWHM multiple for rescaling.
static_fwhm : float
FWHM multiple to use incase dynamic fails or don't want to use it. Default 2.5 determined empirically.
epsilon_mag : float
Small magnitude change within which new and old uncertainties are considered the same.
Should be smaller than ~1/2 the expected uncertainty.
"""
# Rescale psf errors from fit iteratively
fit_shapes = self.photometry.get_fit_shapes(self.fwhm, self.psf_builder.star_size)
Expand Down Expand Up @@ -361,6 +365,12 @@ def rescale_uncertainty(self, psfphot_tbl: Table, dynamic: bool = True, static_f
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:
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:]
return psfphot_tbl

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

Expand Down Expand Up @@ -415,7 +425,7 @@ def create_from_fid(cls, fid: int, directories: Optional[DirectoryProtocol] = No

def do_phot(fileid: int, cm_timeout: Optional[float] = None, make_plots: bool = True,
directories: Optional[DirectoryProtocol] = None, datafile: Optional[Dict[str, Any]] = None,
rescale_dynamic: bool = True) -> ResultsTable:
rescale: bool = True, rescale_dynamic: bool = True) -> ResultsTable:
# Set up photometry runner
pm = PhotometryManager.create_from_fid(fileid, directories=directories, datafile=datafile, create_directories=True)

Expand All @@ -430,8 +440,10 @@ def do_phot(fileid: int, cm_timeout: Optional[float] = None, make_plots: bool =

# Do photometry
apphot_tbl = pm.apphot()
psfphot_tbl = ResultsTable.verify_uncertainty_column(pm.psfphot()) # Verify uncertainty exists after PSF phot.
psfphot_tbl = pm.rescale_uncertainty(psfphot_tbl, dynamic=rescale_dynamic) # Rescale uncertainties
# Verify uncertainty exists after PSF phot:
psfphot_tbl = ResultsTable.verify_uncertainty_column(pm.psfphot())
if rescale: # Rescale uncertainties
psfphot_tbl = pm.rescale_uncertainty(psfphot_tbl, dynamic=rescale_dynamic)

# Build results table and calculate magnitudes
pm.make_result_table(psfphot_tbl, apphot_tbl)
Expand All @@ -445,10 +457,11 @@ def do_phot(fileid: int, cm_timeout: Optional[float] = None, make_plots: bool =

def timed_photometry(fileid: int, cm_timeout: Optional[float] = None, make_plots: bool = True,
directories: Optional[DirectoryProtocol] = None, save: bool = True,
datafile: Optional[Dict[str, Any]] = None, rescale_dynamic: bool = True) -> ResultsTable:
datafile: Optional[Dict[str, Any]] = None, rescale: bool = True,
rescale_dynamic: bool = True) -> ResultsTable:
# TODO: Timer should be moved out of this function.
tic = default_timer()
results_table = do_phot(fileid, cm_timeout, make_plots, directories, datafile, rescale_dynamic)
results_table = do_phot(fileid, cm_timeout, make_plots, directories, datafile, rescale, rescale_dynamic)

# Save the results table:
if save:
Expand Down
14 changes: 9 additions & 5 deletions run_photometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from flows.utilities import create_logger, parse_log_level, create_warning_logger, remove_file_handlers


def process_fileid(fid, autoupload=False, cm_timeout=None, no_plots=False,
rescale_dynamic=True) -> result_model.ResultsTable:
def process_fileid(fid, autoupload: bool = False, cm_timeout=None, no_plots: bool = False,
rescale: bool = True, rescale_dynamic: bool = True) -> result_model.ResultsTable:
# Create the output directory if it doesn't exist:
datafile = api.get_datafile(fid)
directories = fileio.Directories.from_fid(fid, datafile=datafile)
Expand All @@ -25,7 +25,8 @@ def process_fileid(fid, autoupload=False, cm_timeout=None, no_plots=False,
api.set_photometry_status(fid, 'running')

table = photometry(fileid=fid, cm_timeout=cm_timeout, make_plots=not no_plots,
directories=directories, datafile=datafile, rescale_dynamic=rescale_dynamic)
directories=directories, datafile=datafile, rescale=rescale,
rescale_dynamic=rescale_dynamic)

except (SystemExit, KeyboardInterrupt):
logger.error("Aborted by user or system.")
Expand Down Expand Up @@ -79,6 +80,8 @@ def main():
group.add_argument('--fixposdiff', action='store_true',
help="Fix SN position during PSF photometry of difference image. "
"Useful when difference image is noisy.")
group.add_argument('--rescale-off', action='store_false',
help='Turn off uncertainty rescaling.')
group.add_argument('--wcstimeout', type=int, default=None, help="Timeout in Seconds for WCS.")
args = parser.parse_args()

Expand Down Expand Up @@ -118,8 +121,9 @@ def main():
fileids = list(set(fileids))

# Create function wrapper:
process_fileid_wrapper = functools.partial(process_fileid, autoupload=args.autoupload, cm_timeout=args.wcstimeout,
no_plots=args.noplots, rescale_dynamic=not args.rescale_static)
process_fileid_wrapper = functools.partial(process_fileid, autoupload=args.autoupload,
cm_timeout=args.wcstimeout, no_plots=args.noplots,
rescale=args.rescale_off, rescale_dynamic=not args.rescale_static)

if threads > 1:
# process in parallel:
Expand Down