def main():

    file_name, interactive = lib_args.get_args()
    header, pixels = lib_fits.read_first_image(file_name)
    background, dispersion, _ = lib_background.compute_background(pixels)

    # search for clusters
    clustering = RecursiveClustering()
    clusters = clustering(pixels, background, dispersion)
    max_cluster = clusters[0]
    wcs = lib_wcs.get_wcs(header)
    pxy = lib_wcs.PixelXY(max_cluster.column, max_cluster.row)
    radec = lib_wcs.xy_to_radec(wcs, pxy)
    cobjects, _, _ = lib_stars.get_celestial_objects(radec)

    # console output
    print(
        'number of clusters: {:2d}, greatest integral: {:7d}, x: {:4.1f}, y: {:4.1f}'
        .format(len(clusters), max_cluster.integral, max_cluster.column,
                max_cluster.row))
    for cobj in cobjects.keys():
        print('celestial object: {}'.format(cobj))

    # graphic output
    if interactive:
        _, axis = plt.subplots()
        axis.imshow(lib_cluster.add_crosses(pixels, clusters))
        plt.show()

    return 0
def main():

    file_name, interactive = lib_args.get_args()
    header, pixels = lib_fits.read_first_image(file_name)
    background, dispersion, max_x = lib_background.compute_background(pixels)
    clustering = lib_cluster.Clustering()
    clusters = clustering(pixels, background, dispersion)

    # console output
    if not interactive:

        print('{} clusters'.format(len(clusters)))

    else:
        # graphic output

        fig, axis = plt.subplots()
        imgplot = axis.imshow(pixels)

        axcolor = 'lightgoldenrodyellow'
        ax_thresh = plt.axes([0.25, 0.92, 0.65, 0.03], axisbg=axcolor)

        threshold = 6.0
        slider = widgets.Slider(ax_thresh, 'Threshold', 0.0, 5*threshold, valinit=threshold)

        update_slider = UpdateSlider(pixels, background, dispersion, imgplot, fig)
        slider.on_changed(update_slider)
        update_slider(threshold)

        plt.show()

    return 0
示例#3
0
def main():

    file_name, interactive = lib_args.get_args()
    header, pixels = lib_fits.read_first_image(file_name)
    background, dispersion, _ = lib_background.compute_background(pixels)

    # search for clusters
    time0 = time.time()
    clustering = ParallelClustering()
    clusters = clustering(pixels, background, dispersion)
    time1 = time.time()
    max_cluster = clusters[0]

    # console output
    print(
        'number of clusters: {:2d}, greatest integral: {:7d}, x: {:4.1f}, y: {:4.1f}'
        .format(len(clusters), max_cluster.integral, max_cluster.column,
                max_cluster.row))
    print('clustering execution time: {:.3f} seconds'.format(time1 - time0))

    # graphic output
    if interactive:
        _, axis = plt.subplots()
        axis.imshow(lib_cluster.add_crosses(pixels, clusters))
        plt.show()

    return 0
示例#4
0
def main():

    file_name, interactive = lib_args.get_args()
    header, pixels = lib_fits.read_first_image(file_name)
    background, dispersion, max_x = lib_background.compute_background(pixels)

    # console output
    print('background: {:d}, dispersion: {:d}'.format(int(background),int(dispersion)))

    # graphic output
    if interactive:

        fig, axis = plt.subplots()
        imgplot = axis.imshow(pixels)

        ax_thresh = plt.axes([0.25, 0.92, 0.65, 0.03])
        s_thresh = widgets.Slider(ax_thresh, 'Threshold', 0.0, max_x, valinit=background)

        update_slider = UpdateSlider(pixels,imgplot,fig)
        s_thresh.on_changed(update_slider)
        update_slider(background)

        plt.show()

    return 0
示例#5
0
def main():

    file_name, interactive = lib_args.get_args()
    header, pixels = lib_fits.read_first_image(file_name)
    background, dispersion, _ = lib_background.compute_background(pixels)
    clustering = lib_cluster.Clustering()
    clusters = clustering(pixels, background, dispersion)
    max_cluster = clusters[0]

    # coordinates ra dec
    wcs = lib_wcs.get_wcs(header)

    for i, c in enumerate(clusters):
        show_cluster(wcs, i, c)

    # graphic output
    if interactive:
        import matplotlib.pyplot as plt
        import lib_graphics

        fig, axis = plt.subplots()
        axis.imshow(pixels, interpolation='none')
        fig.canvas.mpl_connect('motion_notify_event',
            lib_graphics.ShowClusterProperties(fig,clusters,ShowCelestialObjects(wcs)))
        plt.show()

    return 0
示例#6
0
def main():

    # analyse command line arguments

    file_name, interactive = lib_args.get_args()

    data = None
    data, header = lib_fits.read_first_image(file_name)
    data = data[400:600, 300:500]
    done = np.zeros(data.shape)

    background, dispersion, mx, hist_sum = lib_background.compute_background(
        data, False)
    threshold = 2 * background + 6. * dispersion
    #threshold=11000
    cluster_pix_list = []
    for i in range(data.shape[0]):
        for j in range(data.shape[1]):
            if not done[i][j]:
                done[i][j] = 1
                if data[i][j] > threshold:
                    cluster_pixels = lib_pixels_set.PixelsSet()
                    cluster_pixels.add(i, j, data[i][j])
                    recursive_search(i, j, data, done, cluster_pixels,
                                     threshold)
                    if cluster_pixels.get_len() > 1:
                        cluster_pix_list.append(cluster_pixels)

    print([str(cluster) for cluster in cluster_pix_list])

    if interactive:
        fig, main_axes = plt.subplots()
        main_axes.imshow(data)
        for cluster in cluster_pix_list:
            peak = cluster.get_peak()
            plt.scatter([peak[1]], [peak[0]])

        fig, main_axes = plt.subplots()
        main_axes.imshow(data)
        for cluster in cluster_pix_list:
            for pixel in cluster.pixels:
                plt.plot(pixel[1], pixel[0], 'rs', alpha=0.05)

        fig, main_axes = plt.subplots()
        main_axes.imshow(data)
        matrix = np.zeros(data.shape)
        for cluster in cluster_pix_list:
            for pixel in cluster.pixels:
                matrix[pixel[0], pixel[1]] = 1
        plt.contour(matrix)

        plt.show()

    return 0
示例#7
0
def find_clusters(pixels, interactive):
    pattern = lib_cluster.build_pattern(interactive)
    background, dispersion, mx, hist_sum = lib_background.compute_background(
        pixels, interactive)
    threshold = background + 6. * dispersion
    extended_image = lib_cluster.extend(pixels, interactive, 0, 4)
    convolved_image = lib_cluster.convolve(extended_image, pattern)
    max_mask = lib_cluster.has_peak(convolved_image, interactive, threshold)
    peak_list = lib_cluster.get_peaks_from_mask(max_mask)
    clusters = lib_cluster.get_clusters(peak_list, pixels, threshold)
    sorted_clusters = sorted(clusters, key=lambda cluster: -cluster.int_lumi)
    return sorted_clusters
示例#8
0
def main():

    # analyse command line arguments
    file_name, interactive = lib_args.get_args()
    logging.info('----------------')
    logging.info('name of file: {}'.format(file_name))

    # read fits file
    header, pixels = lib_fits.read_first_image(file_name)
    logging.info('cd1_1: {CD1_1:.10f}'.format(**header))
    logging.info('cd1_2: {CD1_2:.10f}'.format(**header))
    logging.info('cd2_1: {CD2_1:.10f}'.format(**header))
    logging.info('cd2_2: {CD2_2:.10f}'.format(**header))

    # compute background
    background, dispersion, _ = lib_background.compute_background(pixels)
    logging.info('background: {:d}'.format(int(background)))
    logging.info('dispersion: {:d}'.format(int(dispersion)))

    # clustering
    clustering = lib_cluster.Clustering()
    clusters = clustering(pixels, background, dispersion)
    for icl, cl in enumerate(clusters):

        logging.info('----------------')
        logging.info('cluster {:d}: {}'.format(icl, cl))

        # radec coordinates of the greatest cluster
        wcs = lib_wcs.get_wcs(header)
        pxy = lib_wcs.PixelXY(cl.column, cl.row)
        radec = lib_wcs.xy_to_radec(wcs, pxy)
        logging.info('right ascension: {:.3f}'.format(radec.ra))
        logging.info('declination: {:.3f}'.format(radec.dec))

        # celestial objects for the biggest cluster
        cobjects, _, _ = get_celestial_objects(wcs, cl)
        for icobj, cobj in enumerate(cobjects.keys()):
            logging.info('celestial object {}: {}'.format(icobj, cobj))

    # graphic output
    if interactive:
        fig, axis = plt.subplots()
        axis.imshow(pixels, interpolation='none')
        fig.canvas.mpl_connect(
            'motion_notify_event',
            lib_graphics.ShowClusterProperties(fig, clusters,
                                               ShowCelestialObjects(wcs)))
        plt.show()

    logging.info('----------------')
    return 0
示例#9
0
def main():

    file_name, interactive = lib_args.get_args()
    header, pixels = lib_fits.read_first_image(file_name)
    background, dispersion, _ = lib_background.compute_background(pixels)
    clustering = lib_cluster.Clustering()
    clusters = clustering(pixels, background, dispersion)
    max_cluster = clusters[0]

    # coordinates ra dec
    wcs = lib_wcs.get_wcs(header)

    for i, c in enumerate(clusters):
        show_cluster(wcs, i, c)
        break

    return 0
def main():
    '''
    Main function of the program
    '''

    global reg

    # process command-line options
    file_name, interactive = lib_args.get_args()
    header, pixels = lib_fits.read_first_image(file_name)

    logging.debug('cd1_1: %s, cd1_2: %s, cd2_1: %s, cd2_2: %s',
                  header['CD1_1'], header['CD1_2'], header['CD2_1'],
                  header['CD2_2'])
    logging.debug('height: %s, width: %s', pixels.shape[0], pixels.shape[1])

    # compute background
    background, dispersion, _ = lib_background.compute_background(pixels)
    logging.debug('background: %s, dispersion: %s', int(background),
                  int(dispersion))

    print('---------------------')
    # search for clusters in a sub-region of the image
    threshold = 6.0

    # graphic output
    if interactive:
        image = pixels

        reg = lib_cluster_thr.RegionThr(image,
                                        background + threshold * dispersion)
        reg.run_threaded()

        max_integral = reg.clusters[0].integral
        logging.info(
            'number of clusters: %2d, greatest integral: %7d, centroid x: %4.1f, centroid y: %4.1f',
            len(reg.clusters), max_integral, reg.clusters[0].centroid[1],
            reg.clusters[0].centroid[0])

        plt.show()

    return 0
示例#11
0
def check(pixels):

    import lib_background
    import lib_cluster

    print("-----------------------------------")
    background, dispersion, _ = lib_background.compute_background(pixels)
    print('background: %s, dispersion: %s' % (int(background), int(dispersion)))

    # search for clusters in a sub-region of the image
    threshold = 6.0
    reg = lib_cluster.Region(pixels, background + threshold*dispersion)
    reg.run_convolution()
    max_integral = reg.clusters[0]['integral']

    print('nb clusters: %s, greatest integral: %s' % (len(reg.clusters), max_integral))

    for nc, ic in enumerate(reg.clusters):
        print('cluster {}: {}'.format(nc, ic))
    print("-----------------------------------")
示例#12
0
            label = ' '.join(tokens)
            self.text = plt.text(x, y, label, fontsize=14, color='white')

        self.fig.canvas.draw()


# =====
# Unit test
# =====

if __name__ == '__main__':

    import sys, lib_fits, lib_background, lib_wcs

    filename = '../../data/fits/common.fits'
    header, pixels = lib_fits.read_first_image(filename)
    background, dispersion, _ = lib_background.compute_background(pixels)
    clusters = lib_cluster.convolution_clustering(pixels, background,
                                                  dispersion)
    wcs = lib_wcs.get_wcs(header)

    fig, axis = plt.subplots()
    axis.imshow(pixels, interpolation='none')
    fig.canvas.mpl_connect(
        'motion_notify_event',
        ShowClusterProperties(fig, clusters,
                              lambda cl: ["{}".format(cl.integral)]))
    plt.show()

    sys.exit(0)
示例#13
0
def main():

    # analyse command line arguments
    file_name, interactive = lib_args.get_args()

    # importing image
    pixels = None
    pixels, header = lib_fits.read_first_image(file_name)

    pattern = lib_cluster.build_pattern(interactive)

    background, dispersion, mx, hist_sum = lib_background.compute_background(
        pixels, interactive)
    threshold = background + 6. * dispersion

    extended_image = lib_cluster.extend(pixels, interactive, 0, 4)
    convolved_image = lib_cluster.convolve(extended_image, pattern)
    extended_convolved_image = lib_cluster.extend(convolved_image, interactive,
                                                  0, 1)

    max_mask = lib_cluster.has_peak(convolved_image, interactive, threshold)
    peak_list = lib_cluster.get_peaks_from_mask(max_mask)
    clusters = lib_cluster.get_clusters(peak_list, pixels, threshold)
    sorted_clusters = sorted(clusters, key=lambda cluster: -cluster.int_lumi)

    print('RESULT: pattern_sum = {:5.0f}'.format(pattern.sum().sum()))

    print('RESULT: extended_image_width = {:2d}'.format(
        extended_image.shape[0]))
    print('RESULT: extended_image_height = {:2d}'.format(
        extended_image.shape[1]))
    print('RESULT: extended_image_sum = {:5.0f}'.format(
        extended_image.sum().sum()))

    print('RESULT: convolution_image_width = {:2d}'.format(
        convolved_image.shape[0]))
    print('RESULT: convolution_image_height = {:2d}'.format(
        convolved_image.shape[1]))
    print('RESULT: convolution_image_sum = {:5.0f}'.format(
        convolved_image.sum().sum()))

    print('RESULT: extended_convolution_image_width = {:2d}'.format(
        extended_convolved_image.shape[0]))
    print('RESULT: extended_convolution_image_height = {:2d}'.format(
        extended_convolved_image.shape[1]))
    print('RESULT: extended_convolution_image_sum = {:5.0f}'.format(
        extended_convolved_image.sum().sum()))

    print('RESULT: peaks_number={:2d}'.format(max_mask.sum().sum()))

    print('RESULT: clusters_number={:2d}'.format(len(sorted_clusters)))
    print('RESULT: cluster_max_top={:5d}'.format(
        int(sorted_clusters[0].peak_lumi)))

    print('RESULT: cluster_max_integral={:5d}'.format(
        int(sorted_clusters[0].int_lumi)))
    print('RESULT: cluster_max_column={:5d}'.format(int(sorted_clusters[0].y)))
    print('RESULT: cluster_max_row={:5d}'.format(int(sorted_clusters[0].x)))

    # graphic output
    if interactive:
        fig, main_axes = plt.subplots()
        main_axes.imshow(convolved_image)
        #plt.show()

    # end
    return 0
示例#14
0
def main():

    file_name, interactive = lib_args.get_args()
    #print(file_name)
    header, pixels = lib_fits.read_first_image(file_name)
    background, dispersion, _ = lib_background.compute_background(pixels)

    # search for clusters
    clustering = lib_cluster.Clustering()
    #   clusters = clustering(pixels, background, dispersion)

    pattern = clustering.step_build_pattern()

    print('RESULT: pattern_sum = {:5.0f}'.format(np.sum(pattern)))

    ext_image = clustering.step_extend_image(pixels)

    print('RESULT: extended_image_width = {:2d}'.format(ext_image.shape[1]))
    print('RESULT: extended_image_height = {:2d}'.format(ext_image.shape[0]))
    print('RESULT: extended_image_sum = {:5.0f}'.format(np.sum(ext_image)))

    cp_image = clustering.step_build_convolution_image(ext_image)

    print('RESULT: convolution_image_width = {:2d}'.format(cp_image.shape[1]))
    print('RESULT: convolution_image_height = {:2d}'.format(cp_image.shape[0]))
    print('RESULT: convolution_image_sum = {:5.0f}'.format(np.sum(cp_image)))

    ext_cp_image = clustering.step_extend_convolution_image(cp_image)

    print('RESULT: extended_convolution_image_width = {:2d}'.format(ext_cp_image.shape[1]))
    print('RESULT: extended_convolution_image_height = {:2d}'.format(ext_cp_image.shape[0]))
    print('RESULT: extended_convolution_image_sum = {:5.0f}'.format(np.sum(ext_cp_image)))

    peaks = clustering.step_detect_peaks(pixels, cp_image, ext_cp_image, background, dispersion)

    print('RESULT: peaks_number = {:2d}'.format(len(peaks)))

    #for npeak, peak in enumerate(peaks):
    #    print('peak[{}]: {}'.format(npeak, peak))
    clusters = clustering.step_build_clusters(pixels, peaks, background, dispersion)

    print('RESULT: clusters_number = {:2d}'.format(len(clusters)))

    clusters, max_top = clustering.step_sort_clusters(clusters)

    print('RESULT: cluster_max_top = {:5d}'.format(max_top))

    max_cluster = clusters[0]

    # console output
    print('RESULT: cluster_max_integral = {:5d}'.format(max_cluster.integral))
    print('RESULT: cluster_max_column = {:5d}'.format(max_cluster.column))
    print('RESULT: cluster_max_row = {:5d}'.format(max_cluster.row))

    # graphic output
    if interactive:
        import matplotlib.pyplot as plt

        _, axes = plt.subplots(2)
        _ = axes[0].imshow(clustering._build_pattern())
        _ = axes[1].imshow(lib_cluster.add_crosses(pixels,clusters))
        plt.show()

    return 0
示例#15
0
def main():
    '''
    Main function of the program
    '''

    global reg
    global strategy
    global in_cluster
    global in_scanning

    # process command-line options
    file_name, interactive = lib_args.get_args()
    header, pixels = lib_fits.read_first_image(file_name)

    logging.debug('cd1_1: %s, cd1_2: %s, cd2_1: %s, cd2_2: %s',
                  header['CD1_1'], header['CD1_2'], header['CD2_1'],
                  header['CD2_2'])
    logging.debug('height: %s, width: %s', pixels.shape[0], pixels.shape[1])

    # compute background
    background, dispersion, _ = lib_background.compute_background(pixels)
    logging.debug('background: %s, dispersion: %s', int(background),
                  int(dispersion))

    print('---------------------')
    # search for clusters in a sub-region of the image
    threshold = 6.0

    # graphic output
    if interactive:

        # cluster central
        image = pixels[45:70, 40:65]

        reg = lib_cluster.Region(image, background + threshold * dispersion)

        strategy = '4centers'
        in_cluster = 1.0
        in_scanning = 0.1

        def select_strategy(value):
            global strategy
            print(('select strategy=', value))
            strategy = value

        def select_in_cluster(value):
            global in_cluster
            in_cluster = value

        def select_in_scanning(value):
            global in_scanning
            in_scanning = value

        def start_animate(value):
            print(('animate with strategy=', strategy))
            reg.animate(in_cluster=in_cluster,
                        in_scanning=in_scanning,
                        strategy=strategy)

        strategies = ['random', 'all', 'center', '4centers']
        axis_strategy = plt.axes([0.2, 0.8, 0.15, 0.15])
        axis_strategy.set_title('Scanning strategy')
        strategy_widget = RadioButtons(axis_strategy, strategies)
        strategy_widget.set_active(strategies.index(strategy))
        strategy_widget.on_clicked(select_strategy)

        axis_in_cluster = plt.axes([0.2, 0.7, 0.65, 0.03])
        in_cluster_widget = Slider(axis_in_cluster,
                                   'wait in clusters',
                                   0.0,
                                   5.0,
                                   valinit=in_cluster)
        in_cluster_widget.on_changed(select_in_cluster)

        axis_in_scanning = plt.axes([0.2, 0.6, 0.65, 0.03])
        in_scanning_widget = Slider(axis_in_scanning,
                                    'wait in scanning',
                                    0.0,
                                    1.0,
                                    valinit=in_scanning)
        in_scanning_widget.on_changed(select_in_scanning)

        axis_animate = plt.axes([0.2, 0.5, 0.1, 0.03])
        in_animate = Button(axis_animate, 'Animate')
        in_animate.on_clicked(start_animate)

        plt.show()

    return 0