Commit 02d0f213 authored by LUSTIG Peter's avatar LUSTIG Peter

added files

parent 2397d998
This diff is collapsed.
This diff is collapsed.
import numpy as np
import matplotlib.pyplot as plt
import sys
import astropy.units as u
from astropy.table import Table
from astropy.io import fits
from astropy.wcs import WCS
sys.path.append('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/Completness')
from completness2d import CompletenessArea, LogBinEdges, ChiSquaredPerNDF
from completness2d import ModifiedErrorFunction, LevMarLSQFitter, SaveFigure
#%autoreload 2
# Load HLS mask
maskfilename = ('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/'
'Completness/masks.fits')
with fits.open(maskfilename) as hdul:
hitmask = hdul['hitmask'].data.astype(bool)
hlsmask = hdul['hls3fwhm'].data.astype(bool)
wcs = WCS(hdul['hitmask'].header)
# Load SNR masks
anothermaskfile = ('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/verify/'
'snrmasks_fine_new.fits')
snrmasks = []
with fits.open(anothermaskfile) as hdul:
i = 0
while True:
try:
hdu = hdul['SNRMASK{}'.format(i)]
snrmasks.append(hdu.data.astype(bool))
if i == 0:
wcs = WCS(hdu.header)
shape = hdu.data.shape
except KeyError:
break
i += 1
# Load Monte Carlo Data
filename = ('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/'
'Completness/DetectionNewrealisationPixmeans.fits')
#filename = ('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/Completness/'
# 'montecarlo_results/Newrealisation/2mm/AllRealdetections.fits')
# DetectionNewrealisationPixmeans.fits
with fits.open(filename) as hdul:
sources = Table.read(hdul, 'DETECTED_SOURCES')
fake_sources = Table.read(hdul, 'FAKE_SOURCES')
fluxedges = LogBinEdges(fake_sources['amplitude']) * u.Jy
fluxcenters = u.Quantity(np.squeeze(np.sort(np.unique(
fake_sources['amplitude']))))
threshbins = np.arange(2.5, 4.501, .01)
threshbins = np.linspace(2.5, 10, 1001)
threshbins = np.linspace(2.5, 5, 6)
# %%
%matplotlib tk
totmask = snrmasks[0] | hlsmask
obj = CompletenessArea(sources, fake_sources, shape=(501, 501), wcs=wcs,
threshold_edges=threshbins, flux_edges=fluxedges,
mask=totmask, flux_centers=fluxcenters)
plt.close('all')
fct, (ax0, ax1) = obj.FitCompleteness(idx=2, constant='thresh', weighted=True)
# SaveFigure(ax0.get_figure(), 'completeness_inner_fit')
# %%
fig = plt.figure(figsize=(6, 4))
ax = fig.add_axes([.2, .18, .75, .7])
ax2D = obj.Plot2D(ax=ax)
ax2D.set_ylim([10, 40])
# %%
'''
totmask2 = snrmasks[2] | hlsmask
obj2 = CompletenessArea(sources, fake_sources, shape=(501, 501), wcs=wcs,
threshold_edges=threshbins, flux_edges=fluxedges,
mask=totmask2, flux_centers=fluxcenters)
# %%
fig = plt.figure(figsize=(6, 4))
ax = fig.add_axes([.2, .18, .75, .7])
ax2D = obj2.Plot2D(ax=ax)
ax2D.set_ylim([10, 40])
obj2.FitCompleteness(idx=2)
'''
import numpy as np
import matplotlib.pyplot as plt
import sys
import astropy.units as u
from astropy.table import Table, vstack
from astropy.io import fits
from astropy.wcs import WCS
sys.path.append('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/Completness')
from completness2d import CompletenessArea, LogBinEdges, ChiSquaredPerNDF
from completness2d import ModifiedErrorFunction, LevMarLSQFitter, FluxArea
from completness2d import SaveFigure, FluxArea
from fitmodels import FluxRelativeBoosting, FluxBoosting
from pathlib import Path
# Load HLS mask
maskfilename = ('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/'
'Completness/masks.fits')
with fits.open(maskfilename) as hdul:
hitmask = hdul['hitmask'].data.astype(bool)
hlsmask = hdul['hls3fwhm'].data.astype(bool)
wcs = WCS(hdul['hitmask'].header)
# Load SNR masks
anothermaskfile = ('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/verify/'
'snrmasks_fine_new.fits')
snrmasks = []
with fits.open(anothermaskfile) as hdul:
i = 0
while True:
try:
hdu = hdul['SNRMASK{}'.format(i)]
snrmasks.append(hdu.data.astype(bool))
if i == 0:
wcs = WCS(hdu.header)
shape = hdu.data.shape
except KeyError:
break
i += 1
# Load Monte Carlo Data
filename = ('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/'
'Completness/montecarlo_results/Newrealisation/2mm/'
'allphotometry_3.5.fits')
# DetectionNewrealisationPixmeans.fits
with fits.open(filename) as hdul:
sources = Table.read(hdul, 'DETECTED_SOURCES')
fake_sources = Table.read(hdul, 'FAKE_SOURCES')
fluxedges = LogBinEdges(fake_sources['amplitude']) * u.Jy
fluxcenters = u.Quantity(np.squeeze(np.sort(np.unique(
fake_sources['amplitude']))))
threshbins = np.arange(3.5, 7, .5)
len(snrmasks)
# %%
'''
class Flux(FluxArea):
pass
'''
totmask = snrmasks[0] | hlsmask
obj = FluxArea(sources, fake_sources, shape=(501, 501), wcs=wcs,
threshold_edges=threshbins, flux_edges=fluxedges,
mask=totmask, flux_centers=fluxcenters)
cp_phot = obj
# %%
if __name__ == '__main__':
# %%
comptbl = Table(names=['maskidx', 'x0', 'x0_err', 'sigma', 'sigma_err'])
phottble = Table(names=['maskidx', 'sigma', 'sigma_err', 'Sd', 'Sd_err'])
for i, snrmask in enumerate(snrmasks):
print(i)
totmask = snrmask | hlsmask
if not np.all(obj.mask == totmask):
obj.ChangeMask(totmask)
(compfct, compfit), _ = obj.FitCompleteness(0)
cx0 = compfct.x0.value
csigma = compfct.sigma.value
cx0_err, csigma_err = np.sqrt(
np.diagonal(compfit.fit_info['param_cov']))
(photfct, photfit), _ = obj.FitFluxboost(mincompleteness=.1)
pSd = photfct.Sd.value
psigma = photfct.sigma.value
pSd_err, psigma_err = np.sqrt(
np.diagonal(photfit.fit_info['param_cov']))
comptbl = vstack([comptbl, Table(data=[[i], [cx0], [cx0_err],
[csigma], [csigma_err]],
names=['maskidx', 'x0', 'x0_err',
'sigma', 'sigma_err'])])
phottble = vstack([phottble, Table(data=[[i], [pSd], [pSd_err],
[psigma], [psigma_err]],
names=['maskidx', 'sigma',
'sigma_err', 'Sd',
'Sd_err'])])
print('Done')
# from pathlib import Path
len(comptbl)
comptbl
here = Path('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/Completness/')
phottble.write(here / '2mm_photfit_results.fits', overwrite=True)
comptbl.write(here / '2mm_compfit_results.fits', overwrite=True)
if 0:
# %%
snrpercentiles = np.linspace(0, 100, 150)
snredges = np.nanpercentile(obj.sources['SNR'], snrpercentiles)
# %matplotlib tk
ax = obj.PlotPercentiles(mean=False)
ax.set_xscale('log')
ax.set_xlabel('Injected Flux [mJy / beam]', fontsize=20)
# SaveFigure(ax.get_figure(), 'fluxboost_median')
# %%
# %matplotlib tk
# obj.FluxPercentiles() # snrbins=snredges)
fitfunc, (ax, axres) = obj.FitFluxboost(weighted=True)
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
# axins = zoomed_inset_axes(ax, 1, loc=2)
fig = ax.get_figure()
axpos = ax.get_position()
axins = fig.add_axes([.5*(axpos.x1+axpos.x0)+0.015, axpos.y0+.06, .35, .15])
axins.set_xscale('log')
axins.axhline(0, color='r')
axins.set_xlim(2, 10)
axins.set_ylim(-.017, .002)
axins.tick_params('both', labelsize=12)
axins.get_xaxis().set_visible(False)
axins.errorbar(obj.percentile_center,
obj.fluxmean-fitfunc(obj.percentile_center),
obj.fluxmean_unc, linestyle='None', marker='o')
# axins.plot([1,2,3,4], 1,2,3,4)
# axins.set_xlim(1, 5)
# axins.set_xlim(0, 5)
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
mark_inset(axres, axins, loc1=1, loc2=3, fc="none", ec="0.5")
# axins.axhline(0)
obj.fluxmean_unc
# SaveFigure(ax.get_figure(), 'fluxboost_fit')
obj.percentile_center
import numpy as np
import matplotlib.pyplot as plt
import sys
import astropy.units as u
from astropy.table import Table, vstack
from astropy.io import fits
from astropy.wcs import WCS
sys.path.append('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/Completness')
from completness2d import CompletenessArea, LogBinEdges, ChiSquaredPerNDF
from completness2d import ModifiedErrorFunction, LevMarLSQFitter, SaveFigure
from completness2d import CountsAreaEvaluation, Purity
from pathlib import Path
# Load HLS mask
maskfilename = ('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/'
'Completness/masks.fits')
with fits.open(maskfilename) as hdul:
hitmask = hdul['hitmask'].data.astype(bool)
hlsmask = hdul['hls3fwhm'].data.astype(bool)
wcs = WCS(hdul['hitmask'].header)
# Load SNR masks
anothermaskfile = ('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/verify/'
'snrmasks_fine_new.fits')
snrmasks = []
with fits.open(anothermaskfile) as hdul:
i = 0
while True:
try:
hdu = hdul['SNRMASK{}'.format(i)]
snrmasks.append(hdu.data.astype(bool))
if i == 0:
wcs = WCS(hdu.header)
shape = hdu.data.shape
except KeyError:
break
i += 1
# Select mask
# 3.5 SNR
emptycountsfile = ('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/'
'Completness/montecarlo_results/Newrealisation/2mm/'
'falsedetections/180607_155758_NewRealisation_2mm_'
'flux0.00mJy_thresh3.5_nsources0_nsim1000_photTrue_'
'detectTrue.fits')
'''
# 2.5 SNR
emptycountsfile = ('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/'
'Completness/montecarlo_results/Newrealisation/2mm/'
'falsedetections/180531_115409_NewRealisation_2mm_'
'flux0.00mJy_thresh2.5_nsources0_nsim1000_photFalse'
'_detectTrue.fits')
'''
emptysources = Table.read(emptycountsfile)
# %matplotlib tk
realcountsfile = ('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/'
'Completness/montecarlo_results/Newrealisation/2mm/'
'NewrealisationSources_2mm_threshold3.5.fits')
realsources = Table.read(realcountsfile)
# %%
# %matplotlib tk
threshbins = np.linspace(3.5, 10, 50)
totmask = snrmasks[0] | hlsmask
fc = CountsAreaEvaluation(
emptysources,
shape=shape, wcs=wcs, threshold_edges=threshbins,
mask=totmask, no_sim=1000)
false_counts = fc.Counts()
# fc.PlotCounts()
rc = CountsAreaEvaluation(
realsources,
shape=shape, wcs=wcs, threshold_edges=threshbins,
mask=totmask, no_sim=1)
abs_counts = rc.Counts()
# %%
pur = Purity(abs_counts, false_counts, threshbins[0:-1])
# rc.PlotMask()
# fc.Counts()
# fc.threshold_edges
if 0:
fc.PlotMask()
plt.close('all')
fig = plt.figure(figsize=(6, 5))
ax = fig.add_axes([.16, .12, .8, .78])
nax = fc.PlotCounts(label='False\nDetections', linewidth=3, ax=ax)
ax = rc.PlotCounts(ax=nax, label='Absolute\nDetections', linestyle='--',
color='r', linewidth=3)
ax.grid()
# %%
pur.Plot()
# SaveFigure(ax.get_figure(), filename='FalseCountsRealCounts')
# plt.close('all')
# %matplotlib tk
if 0:
ax1x0 = .1
ax1y0 = .145
ax1width = .37
ax1height = .75
bothplots = plt.figure(figsize=(10, 4.5))
ax1 = bothplots.add_axes([ax1x0, ax1y0, ax1width, ax1height])
fc.PlotCounts(ax=ax1, label='False\nDetections', linewidth=3)
rc.PlotCounts(ax=ax1, label='Absolute\nDetections', linestyle='--',
color='r', linewidth=3)
ax1.grid()
ax1pos = ax1.get_position()
ax2 = bothplots.add_axes([ax1pos.x1+0.13, ax1pos.y0,
ax1pos.width, ax1pos.height])
pur = Purity(abs_counts, false_counts, threshbins[0:-1])
x = threshbins[0:-1]
y = (abs_counts-false_counts) / abs_counts
completeness35 = np.interp(3.5, x, y)
pur.Plot(ax=ax2, linewidth=3)
ylims = ax2.get_ylim()
xlims = ax2.get_xlim()
ax2.plot([3.5, 3.5], [0, completeness35], color='r', linewidth=3)
ax2.plot([0, 3.5], [completeness35, completeness35], color='r', linewidth=3)
# boxprops = dict(facecolor='white', boxstyle='round, pad')
boxprops = dict(facecolor='white', edgecolor='black', boxstyle='round, pad=.3',
alpha=1)
ax2.set_ylim(.5, ylims[1])
ax2.set_xlim(xmin=xlims[0])
text = r'$\Pi(3.5)={{{:.2f}}}$'.format(completeness35)
ax2.text(.9, .1, text, ha='right', va='bottom', fontsize=20,
transform=ax2.transAxes, bbox=boxprops)
ax2.grid()
# SaveFigure(bothplots, filename='FalseCountsAndPurity_2mm')
#%%
if __name__ == '__main__':
cn_mask = 'Mask'
cn_false = r'$N_{\mathrm{false}}/\mathrm{sim}$'
cn_abs = r'$N_{\mathrm{abs}}$'
cn_pur = 'Purity'
t = Table(names=[cn_mask, cn_false, cn_abs, cn_pur],
dtype=[int, float, int, float])
# t.write('testlatextablenames.fits')
# t.read('testlatextablenames.fits')
for i, snrmask in enumerate(snrmasks):
totmask = snrmask | hlsmask
if not np.all(fc.mask == totmask):
fc.ChangeMask(totmask)
if not np.all(rc.mask == totmask):
rc.ChangeMask(totmask)
_fc = fc.Counts()[0]
_rc = rc.Counts()[0]
tt = Table(names=[cn_mask, cn_false, cn_abs, cn_pur],
data=[[i], [_fc], [_rc], [(_rc-_fc) / _rc]])
t = vstack([t, tt])
here = Path('/home/peter/Dokumente/Uni/Paris/Stage/FirstSteps/Completness/')
t.write(here / '2mm_purity_table_newres.fits', overwrite=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment