Commit 995bafe5 authored by LUSTIG Peter's avatar LUSTIG Peter

Integrated all existant plot functions

parent ab75b24c
......@@ -45,7 +45,6 @@ class PCEvaluation:
def __init__(self, sources, fake_sources, shape, wcs, flux=None,
mapbins=19, threshold_bins=5, threshold_range=(3, 5)):
print('sorting values')
idxsort = np.argsort(flux.to_value(u.mJy))
self.flux = flux[idxsort]
self.sources = [sources[i] for i in idxsort]
......@@ -61,8 +60,8 @@ class PCEvaluation:
assert type(mapbins) is int, "number of bins must be an integer"
self.bins = mapbins
# threshold = np.linspace(threshold_range[0], threshold_range[1],
# threshold_bins)
self.thresholds = np.linspace(threshold_range[0], threshold_range[1],
threshold_bins)
threshold_edges = np.linspace(threshold_range[0], threshold_range[1],
threshold_bins+1)
......@@ -71,17 +70,13 @@ class PCEvaluation:
bins=self.bins,
threshold_range=threshold_range,
threshold_bins=threshold_bins)
#print(self.wcs_3D)
#sys.exit()
print('wcs created')
# Testing the lower edges
wcs_threshold = self.wcs_3D.sub([3])
assert np.all(np.abs(wcs_threshold.all_pix2world(
np.arange(threshold_bins+1)-0.5, 0)
- threshold_edges) < 1e-15)
self.completness, self.purity, self.hitmap = self.GetCP()
print(self.completness.shape)
def GetCP(self, sources=None, fake_sources=None, wcs=None, shape=None,
pool=None):
......@@ -140,19 +135,16 @@ class PCEvaluation:
# combine several fluxes
return completness, purity, norm_comp
def GetBinResults(self, ix, iy):
#print(self.completness)
return (self.completness[:, iy, ix, :],
self.purity[:, iy, ix, :],
self.hitmap[:, iy, ix])
def PlotBin(self, data, title='', flux=np.array([]), thresh=[],
nfluxlabels=None, nthreshlabels=None, **kwargs):
def PlotBin(self, data, title='', flux=None, thresh=None,
nfluxlabels=None, nthreshlabels=None, **kwargs):
tickfs = 20
labelfs = 25
if flux is None:
flux = self.flux.to_value(u.mJy)
if thresh is None:
thresh = self.threshold
if nfluxlabels is not None:
_label_flux = np.geomspace(flux[0], flux[-1], nfluxlabels)
_f_idx = find_nearest(flux, _label_flux)
......@@ -182,12 +174,73 @@ class PCEvaluation:
plt.ylabel('Flux [mJy]', fontsize=labelfs)
plt.xticks(tlblpos, tlbl, fontsize=tickfs)
plt.yticks(flblpos, flbl, fontsize=tickfs)
plt.imshow(data, origin='lower', **kwargs)
plt.imshow(data, origin='lower', aspect='auto', **kwargs)
cbar = plt.colorbar()
cbar.ax.tick_params(labelsize=tickfs)
def PlotFixedThreshold(self, data, thresholds, nfluxlabels=None,
hlines=None):
linestyles = ['-', '--', '-.', ':']
real_thresholds = find_nearest(self.thresholds, thresholds)
_x = self.flux.to_value(u.mJy)
for i in range(len(real_thresholds)):
_y = data[:, real_thresholds[i]]
plt.plot(_x, _y, linestyle=linestyles[i],
label='{:.1f}'.format(
self.thresholds[real_thresholds[i]]))
if hlines is not None:
for i, val in enumerate(hlines):
plt.axhline(val, color='r')
plt.title('Fixed Threshold', fontsize=30, y=1.02)
plt.xlabel('Source Flux [mJy]', fontsize=25)
plt.ylabel('Completness', fontsize=25)
plt.yticks(fontsize=20)
plt.xticks(fontsize=20)
plt.subplots_adjust(left=0.12)
ax = plt.gca()
ax.set_xscale("log", nonposx='clip')
# legend = plt.legend(fontsize=25, title='SNR', loc='lower right')
legend = plt.legend(fontsize=25, title='SNR', loc='upper left',
framealpha=1)
plt.setp(legend.get_title(), fontsize=25)
plt.show(block=True)
def PlotOverview(self, completness=None, purity=None, threshold=None,
flux=None):
if completness is None:
completness = self.completness
if purity is None:
purity = self.purity
if threshold is None:
threshold = self.thresholds
threshold_bins = completness.shape[-1]
fluxidx = find_nearest(self.flux.to_value(u.mJy), flux.to_value(u.mJy))
realflux = self.flux[fluxidx]
for iimg in range(len(fluxidx)):
_completness = completness[fluxidx[iimg]]
_purity = purity[fluxidx[iimg]]
fig, axes = plt.subplots(nrows=2, ncols=threshold_bins,
sharex=True, sharey=True)
for i in range(threshold_bins):
print(i)
axes[0, i].imshow(_completness[:, :, i], vmin=0, vmax=1)
im = axes[1, i].imshow(_purity[:, :, i], vmin=0, vmax=1)
axes[1, i].set_xlabel("thresh={:.2f}".format(threshold[i]))
if i == (threshold_bins-1):
# print('-----------')
divider = make_axes_locatable(axes[1, i])
cax = divider.append_axes('right', size='5%', pad=0.0)
fig = plt.gcf()
fig.colorbar(im, cax=cax, orientation='vertical')
if flux is not None:
axes[0, 1].set_title("{:.1f}".format(realflux[iimg]))
axes[0, 0].set_ylabel("completness")
axes[1, 0].set_ylabel("purity")
plt.close('all')
DATA_DIR = "/home/peter/Dokumente/Uni/Paris/Stage/data/v_1"
data = NikaMap.read(Path(DATA_DIR) / '..' / 'map.fits')
......@@ -200,14 +253,12 @@ hdul = fits.HDUList(fits.open('/home/peter/Dokumente/Uni/Paris/Stage/'
nfluxes = hdul[0].header['NFLUXES']
print('{} different fluxes found'.format(nfluxes))
indata = []
FLUX = []
SOURCE = []
FSOURCE = []
# for isimu in range(nfluxes):
for isimu in range(6):
for isimu in range(nfluxes):
FLUX.append(u.Quantity(hdul[0].header['flux{}'.format(isimu)]))
SOURCE.append(Table.read(hdul['DETECTED_SOURCES{}'
......@@ -216,155 +267,13 @@ for isimu in range(6):
.format(FLUX[isimu])]))
xx = PCEvaluation(SOURCE, FSOURCE, sh, wcs, u.Quantity(FLUX))
print('done')
# %% testfunctions
# p = Pool(2)
# res = xx.GetCP()
dd = xx.GetBinResults(9, 9)
dd = xx.completness
print('comp shape', dd.shape)
xx.PlotBin(xx.completness[:, 9, 9, :])
plt.show(block=True)
sys.exit()
# %%
# midbin = int(bins / 2)
def PlotEvaluation(data, title='', flux=np.array([]), thresh=[],
nfluxlabels=None, nthreshlabels=None, **kwargs):
tickfs = 20
labelfs = 25
if nfluxlabels is not None:
_label_flux = np.geomspace(flux[0], flux[-1], nfluxlabels)
_f_idx = find_nearest(flux, _label_flux)
flblpos, _flbl = _f_idx, flux[_f_idx]
else:
flblpos, _flbl = np.arange(len(flux)), flux
if nthreshlabels is not None:
_label_thresh = np.linspace(thresh[0], thresh[-1], nthreshlabels)
_t_idx = find_nearest(thresh, _label_thresh)
print(_t_idx)
tlblpos, _tlbl = _t_idx, thresh[_t_idx]
else:
tlblpos, _tlbl = np.arange(len(thresh)), thresh
flbl = []
for i in range(len(_flbl)):
flbl.append('{:.1f}'.format(_flbl[i]))
tlbl = []
for i in range(len(_tlbl)):
tlbl.append('{:.1f}'.format(_tlbl[i]))
plt.figure()
plt.title(title, fontsize=30)
plt.xlabel('Detection Threshold [SNR]', fontsize=labelfs)
plt.ylabel('Flux [mJy]', fontsize=labelfs)
plt.xticks(tlblpos, tlbl, fontsize=tickfs)
plt.yticks(flblpos, flbl, fontsize=tickfs)
plt.imshow(data, origin='lower', **kwargs)
cbar = plt.colorbar()
cbar.ax.tick_params(labelsize=tickfs)
PlotEvaluation(COMPLETNESS[:, midbin, midbin, :], title='Completness',
flux=np.array(FLUX), thresh=threshold, cmap='bone',
nfluxlabels=10, nthreshlabels=5, aspect='auto')
PlotEvaluation(PURITY[:, midbin, midbin, :], title='Purity',
flux=np.array(FLUX), thresh=threshold, cmap='bone',
nfluxlabels=10, nthreshlabels=5, aspect='auto')
plt.show(block=True)
# %% 2D plot
def PlotFixedThreshold(thresholds, bin, completness, allthresholds, flux,
nfluxlabels=None, hlines=None):
linestyles = ['-', '--', '-.', ':']
real_thresholds = find_nearest(allthresholds, thresholds)
for i in range(len(real_thresholds)):
_x = flux
_y = completness[:, bin[0], bin[1], real_thresholds[i]]
plt.plot(_x, _y, linestyle=linestyles[i],
label='{:.1f}'.format(allthresholds[real_thresholds[i]]))
if hlines is not None:
for i, val in enumerate(hlines):
plt.axhline(val, color='r')
plt.title('Fixed Threshold', fontsize=30, y=1.02)
plt.xlabel('Source Flux [mJy]', fontsize=25)
plt.ylabel('Completness', fontsize=25)
plt.yticks(fontsize=20)
plt.xticks(fontsize=20)
plt.subplots_adjust(left=0.12)
ax = plt.gca()
ax.set_xscale("log", nonposx='clip')
# legend = plt.legend(fontsize=25, title='SNR', loc='lower right')
legend = plt.legend(fontsize=25, title='SNR', loc='upper left',
framealpha=1)
plt.setp(legend.get_title(), fontsize=25)
plt.show(block=True)
# cmap bone hot
# print(np.array(FLUX))
PlotFixedThreshold(np.array((3, 5, 7)), (midbin, midbin), COMPLETNESS,
threshold, np.array(FLUX), nfluxlabels=None,
hlines=[.5, .8, .9])
# %% Plot map
# Plot_CompPur(completness, purity, threshold, nsim=None, savename=None,
# flux=None):
plotidx = np.array([24, 30, 40, 49])
# plotthreshidx = np.array([0, 4, 8])
plotcomp = COMPLETNESS[plotidx]
plotpur = PURITY[plotidx]
print(plotpur.shape)
# plotthresh = threshold[plotthreshidx]
plotflux = np.array(FLUX[plotidx])
def Plot_CompPur(completness, purity, threshold, nsim=None, savename=None,
flux=None):
threshold_bins = completness.shape[-1]
print('subplots:', threshold_bins)
fig, axes = plt.subplots(nrows=2, ncols=threshold_bins, sharex=True,
sharey=True)
print('axes shape', axes.shape)
print('completness shape', completness.shape)
for i in range(threshold_bins):
print(i)
axes[0, i].imshow(completness[:, :, i], vmin=0, vmax=1)
im = axes[1, i].imshow(purity[:, :, i], vmin=0, vmax=1)
axes[1, i].set_xlabel("thresh={:.2f}".format(threshold[i]))
if i == (threshold_bins-1):
# print('-----------')
divider = make_axes_locatable(axes[1, i])
cax = divider.append_axes('right', size='5%', pad=0.0)
fig = plt.gcf()
fig.colorbar(im, cax=cax, orientation='vertical')
if nsim is not None:
axes[0, 0].set_title("{} simulations".format(nsim))
if flux is not None:
axes[0, 1].set_title("{}".format(flux))
axes[0, 0].set_ylabel("completness")
axes[1, 0].set_ylabel("purity")
if savename is not None:
plt.savefig(savename)
for i in range(len(plotidx)):
_comp = plotcomp[i]
_pur = plotpur[i]
Plot_CompPur(_comp, _pur, threshold, flux=plotflux[i])
# xx.PlotBin(xx.completness[:, 9, 9, :])
# xx.PlotFixedThreshold(xx.completness[:, 9, 9, :], np.array([3, 5]))
xx.PlotOverview(flux=5*u.mJy)
plt.show(block=True)
sys.exit('Done')
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