Source code for asterism.analysis_tasks.source_detection.deblending.glw_deblending

"""

"""
from __future__ import division, absolute_import, print_function
__author__ = 'andrea tramacere'


import  numpy as np
from skimage.morphology import watershed

from .deblending import  DeblendedProducts
from ....core.clustering.connectivity_based.connected_components import ConnectedComponents
from ....pipeline_manager.analysis_tasks import AnalysisTask
from ....core.image_processing.features import BlobDetectionGaussLaplace,PeakLocalMaxima

[docs]def test_plot(parent_cluster_image,local_maxima,deblended_cluster_list): from asterism.plotting.plot_tools import plot_contour, show_image import pylab as plt sources_cluster_list = [] for ID1, cl in enumerate(deblended_cluster_list): sources_cluster_list.append(cl.SourceCluster_factory(ID=ID1)) im, ax = show_image(parent_cluster_image) for lm in local_maxima: ax.plot(lm[1],lm[0],'o',c='white',ms=5) for cl in sources_cluster_list: if cl.contour_shape is not None: plot_contour(ax, cl.contour_shape[:, 0] , cl.contour_shape[:, 1] , 'w') ax.annotate('C.ID=%d' % (cl.ID), xy=(cl.x_c, cl.y_c), color='w') plt.show()
[docs]def do_cluster_glw_segmentation(parent_cluster,h_frac,min_size,h_min=None,gl_th_rel=0.1,plot=False,verbose=False): """ This function implements the cluster image segmentation based * GaussLaplace local maxima (:class:`~asterism.core.image_processing.features.BlobDetectionGaussLaplace`) * watershed segmentation on the corresponding local maxima (:func:`~skimage.morphology.watershed.watershed`) Parameters ---------- parent_cluster :n min_size : h_frac : h_min : th_rel : plot : Returns ------- deblended_cluster_list off_x off_y """ children_clusters_list=[] off_x=0 off_y=0 if parent_cluster.cl_len>min_size: #cluster image bkg=parent_cluster.flux.min() parent_cluster_image,off_x,off_y,selected=parent_cluster.get_cluster_image_array(bkg=bkg,border=2) h1=parent_cluster.r_max*h_frac if h_min is not None: h1=max(h_min,h1) h2=1.5*h1 if verbose is True: print("|connected segmentation for cl", parent_cluster.ID, " size", parent_cluster.cl_len) print("|GaussLaplace h1=%f ,h2=%f|"%(h1,h2)) blob_log_det=BlobDetectionGaussLaplace(min_sigma=h1, max_sigma=h2, threshold_rel=gl_th_rel, overlap=0.5) local_maxima,local_maxima_flux=blob_log_det.apply(parent_cluster_image,coords_in_xy=False,verbose=verbose) #peak_det=PeakLocalMaxima(threshold_abs=None,threshold_rel=0.05) #local_maxima, local_maxima_flux = peak_det.apply(parent_cluster_image, coords_in_xy=False,verbose=verbose) if local_maxima_flux is not None: if local_maxima_flux.size>1: markers = np.zeros(parent_cluster_image.shape) m=1 for lm in local_maxima: markers[lm[0],lm[1]]=m m+=1 labels = watershed(-parent_cluster_image, markers, mask=~selected) if labels.max()>0: conn = ConnectedComponents(1) conn.run(parent_cluster_image, -1,seg_map=labels, seg_map_bkg=0) children_clusters_list=conn.clusters_list if plot is True: test_plot(parent_cluster_image,local_maxima,children_clusters_list) return children_clusters_list,off_x,off_y
[docs]def do_guass_laplace_watershed_deblending(clusters_list, h_frac=0.1, h_min=None, gl_th_rel=0.1, min_size=9, plot=False, verbose=False): """ This function implements the top level algorithm for the guass_laplace_watershed_deblending: * each parent cluster in the `cluster_list` is partitioned by the :func:`do_cluster_glw_segmentation` function. * the parent cluster with his children clusters are used to build :class:`DeblendedProducts` object * a list of class:`DeblendedProducts` objects is returned Parameters ---------- clusters_list h_frac h_min th_rel min_size plot verbose Returns ------- deblended_prod_list : list of :class:`DeblendedProducts` objects """ deblended_prod_list=[] for parent_cluster in clusters_list: _cluster_list,offset_x,offset_y=do_cluster_glw_segmentation(parent_cluster, h_frac, min_size, h_min=h_min, gl_th_rel=gl_th_rel, plot=plot, verbose=verbose) deblended_prod_list.append(DeblendedProducts(parent=parent_cluster, children_list=_cluster_list, offset_x=offset_x, offset_y=offset_y)) return deblended_prod_list
[docs]class DoGLWDeblendingTask(AnalysisTask): def __init__(self, name='guass_laplace_watershed_deblending', func=do_guass_laplace_watershed_deblending, parser=None, process=None): super(DoGLWDeblendingTask,self).__init__(name=name,func=func,parser=parser,process=process) self.parameters_list.add_par('-h_frac', type=np.float ,help='sets the width of the kernel as h=h_frac*sqrt(r_max^2+r_cluster^2)',default=0.1 ) self.parameters_list.add_par('-h_min', type=np.float, help='kernel width obtained by h_frac can not be lower than h_min', default=1.0) self.parameters_list.add_par('-gl_th_rel', type=np.float, help='relative threshold for the GaussLaplace local maxima detection', default=0.1) self.parameters_list.add_par('-min_size', type=np.int, help='sets the minimum size in pixels to perform a deblending', default=9) self.parameters_list.add_par('-verbose',help='set verbosity on',action='store_true') self.parameters_list.add_par('-plot',help='plot ',action='store_true') self.func=func