Skip to content

Commit

Permalink
new tools for photometry and background check
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomás E. Müller Bravo authored Nov 10, 2023
1 parent 6b6dbb4 commit 322665b
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions check_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,8 @@ def get_target_stats(data, img_wcs, ra, dec, r_in, r_out):
Returns
-------
aperture: ~astropy.aperture
Annulus aperture used for the background.
mean: float
Background mean.
mean: float
Background median.
std: float
Background standard deviation.
aperstats: ~astropy.aperture.ApertureStats
Annulus statistics.
"""
coords = SkyCoord(ra=ra, dec=dec, unit=(u.degree, u.degree), frame="icrs")

Expand All @@ -137,7 +131,7 @@ def get_target_stats(data, img_wcs, ra, dec, r_in, r_out):
aperture = SkyCircularAnnulus(coords, r_in=r_in, r_out=r_out)
aperstats = ApertureStats(data, aperture, wcs=img_wcs)

return aperture, aperstats.mean, aperstats.median, aperstats.std
return aperstats

def plot_target(
hdu,
Expand Down Expand Up @@ -199,14 +193,14 @@ def plot_target(

sep_mean, sep_std = info_dict['sep']
astro_mean, astro_median, astro_std = info_dict['astro']
target_mean, target_median, target_std = info_dict['target']
target_bkg, target_std, percent = info_dict['target']
sep_diff = info_dict['sep_diff']
astro_diff = info_dict['astro_diff']

text = 'Background stats\n'
text += f'SEP: mean={sep_mean:.2f}, std={sep_std:.2f}\n'
text += f'ASTROPY: mean={astro_mean:.2f}, median={astro_median:.2f}, std={astro_std:.2f}\n'
text += f'Annulus: mean={target_mean:.2f}, median={target_median:.2f}, std={target_std:.2f}\n'
text += f'Annulus: percentile ({percent}%)={target_bkg:.2f}, std={target_std:.2f}\n'
text += f'$\Delta$(SEP): {sep_diff:.2f}$\sigma$, $\Delta$(ASTROPY): {astro_diff:.2f}$\sigma$'

fig.add_label(0.04, 0.11, text, relative=True, **{"family": font_family,
Expand All @@ -228,11 +222,13 @@ def plot_target(

plt.show()

def check_background(file, ra, dec, r_in=3, r_out=6, method='mean', show_plot=True, size=1.0):
def check_background(file, ra, dec, r_in=3, r_out=6, method='mean', percent=90, show_plot=True, size=1.0):
"""Calculates the difference, in sigmas, between an image global
background and the background around the given coordinates.
diff = np.abs(bkg_mean1 - bkg_mean2)/np.sqrt(bkg_std1**2 + bkg_std2**2)
Examples:
diff = np.abs(target_percentile - bkg_mean)/bkg_std
diff = np.abs(target_percentile - bkg_median)/bkg_std
Parameters
----------
Expand All @@ -249,38 +245,40 @@ def check_background(file, ra, dec, r_in=3, r_out=6, method='mean', show_plot=Tr
method: str, default 'mean'
Method used to estimate the difference in background.
Either 'mean' or 'median'. SEP only uses 'mean'.
percent: int, default '90'
Percentile used for the background around the target.
show_plot: bool default 'True'
Whether to show the image with the annulus used.
size: float, default '1.0'
Size of the image to be plotted, in arcminutes.
"""
data, header, img_wcs, hdu = extract_image(file)

# background statistics
# sep and astropy background statistics
sep_mean, sep_std = get_sep_stats(data)
astro_mean, astro_median, astro_std = get_astropy_stats(data)
aperture, target_mean, target_median, target_std = get_target_stats(data,
img_wcs,
ra, dec,
r_in, r_out)
# target's background statistics using an annulus
aperstats = get_target_stats(data, img_wcs, ra, dec, r_in, r_out)
aperture = aperstats.aperture
target_bkg = np.percentile(aperstats.data_cutout.data, percent)
target_std = aperstats.std # not used as it is not a good statistic

# calculate difference in background level, in units of sigmas
assert method in ['mean', 'median'], "Not a valid method!"

# calculate difference in background level, in units of sigmas
sep_diff = np.abs(target_bkg-sep_mean)/sep_std
if method=='mean':
sep_diff = np.abs(target_mean-sep_mean)/np.sqrt(sep_std**2 + target_std)
astro_diff = np.abs(target_mean-astro_mean)/np.sqrt(astro_std**2 + target_std)
astro_diff = np.abs(target_bkg-astro_mean)/astro_std
elif method=='median':
sep_diff = np.abs(target_median-sep_mean)/np.sqrt(sep_std**2 + target_std)
astro_diff = np.abs(target_median-astro_median)/np.sqrt(astro_std**2 + target_std)
astro_diff = np.abs(target_bkg-astro_median)/astro_std

print(f'SEP: {np.round(sep_diff, 2)} sigmas')
print(f'ASTROPY: {np.round(astro_diff, 2)} sigmas')

if show_plot is True:
info_dict = {'sep':[sep_mean, sep_std],
'astro':[astro_mean, astro_median, astro_std],
'target':[target_mean, target_median, target_std],
'target':[target_bkg, target_std, percent],
'sep_diff':sep_diff,
'astro_diff':astro_diff,
}
Expand All @@ -300,9 +298,9 @@ def check_background(file, ra, dec, r_in=3, r_out=6, method='mean', show_plot=Tr
'astro_mean':[astro_mean],
'astro_median':[astro_median],
'astro_std':[astro_std],
'target_mean':[target_mean],
'target_median':[target_median],
'target_std':[target_std]
'annulus_bkg':[target_bkg],
'annulus_std':[target_std],
'annulus_percent':[percent]
}

df = pd.DataFrame(out_dict)
Expand Down Expand Up @@ -348,6 +346,14 @@ def main(args=None):
type=float,
help="Outer radius of the annulus."
)
parser.add_argument("-p",
"--percent",
dest="percent",
action="store",
default=90,
type=int,
help="Annulus percentile."
)
parser.add_argument("-m"
"--method",
dest="method",
Expand All @@ -358,8 +364,7 @@ def main(args=None):
help=("Method used to estimate the difference in background."
"Either 'mean' or 'median'. SEP only uses 'mean'.")
)
parser.add_argument("-p"
"--show_plot",
parser.add_argument("--show_plot",
dest="show_plot",
action="store",
default=True,
Expand All @@ -378,7 +383,7 @@ def main(args=None):

args = parser.parse_args(args)
check_background(args.file, args.ra, args.dec, args.r_in, args.r_out,
args.method, args.show_plot, args.size)
args.method, args.percent, args.show_plot, args.size)

if __name__ == "__main__":
main(sys.argv[1:])

1 comment on commit 322665b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests Coverage

Coverage Report
FileStmtsMissCoverMissing
flows_finder
   __init__.py30100% 
   argparser.py781185%104, 109, 120, 147, 150, 153, 172–176
   auth.py19952%13–16, 20, 22–23, 35–36
   catalogs.py66690%45, 74, 136–137, 145–146
   corner.py14750%8–12, 18, 28
   instruments.py1403078%21, 25, 29, 33, 37, 42, 47, 51, 55, 104, 107–114, 140–144, 186, 189–194
   make_fc.py21861%21, 24, 27, 30, 33, 35–36, 40
   observer.py921188%43–44, 57–58, 71, 83–84, 86, 90, 92–93
   plan.py380100% 
   plots.py1687455%30, 51, 125, 195, 201–208, 213–222, 224, 230, 233–242, 251–252, 254, 257, 267, 272, 281, 283, 285, 290–303, 305–307, 309, 311–316, 318–323, 327
   run_get_brightest.py22959%21, 24, 27–29, 32–34, 38
   target.py28775%19–20, 24–25, 27–29
   tests.py132596%28–30, 144–145
   utils.py381560%33–36, 38–41, 45–48, 55, 59–60
   version.py20100% 
TOTAL86119277% 

Please sign in to comment.