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
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
示例#3
0
def main():

    # analyse command line arguments

    file_name, interactive = lib_args.get_args()

    pixels = None
    pixels, header = lib_fits.read_first_image(file_name)

    signature_fmt_1 = 'RESULT: cd1_1 = {:.10f}'.format(header["CD1_1"])
    signature_fmt_2 = 'RESULT: cd1_2 = {:.10f}'.format(header["CD1_2"])
    signature_fmt_3 = 'RESULT: cd2_1 = {:.10f}'.format(header["CD2_1"])
    signature_fmt_4 = 'RESULT: cd2_2 = {:.10f}'.format(header["CD2_2"])

    print(signature_fmt_1)
    print(signature_fmt_2)
    print(signature_fmt_3)
    print(signature_fmt_4)


    if interactive:
        fig, main_axes = plt.subplots()
        main_axes.imshow(pixels)
        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, _ = 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
示例#5
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
示例#6
0
def main():

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

    pixels = None
    pixels, header = lib_fits.read_first_image(file_name)

    background, dispersion, mx, hist_sum = lib_background.compute_background(
        pixels, interactive)

    test_gaussian = lib_background.modelling_function(
        np.arange(-10.0, 10.0, 1.0), 1.0, 1.0, 1.0).sum()

    print('RESULT: test_gaussian = {:3f}'.format(test_gaussian))
    signature_hist = 'RESULT: histogram = {:5d}'.format(hist_sum)
    print(signature_hist)
    print('RESULT: background = {:d}'.format(int(background)))
    print('RESULT: dispersion = {:d}'.format(int(dispersion)))

    # graphic output
    if interactive:
        # ...
        pass

    # end
    return 0
示例#7
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
示例#8
0
def main():
    """ Exercise 4: Clusters """

    lib_fits.init()
    print(lib_fits.file_name)
    header, pixels = lib_fits.read_first_image(lib_fits.file_name)

    thres = lib_background.threshold(pixels)

    peaks = lib_conv.complete_peaks_search(pixels)
    lums = lib_cluster.peak_lum(pixels, peaks)

    clusters = []
    for i in range(len(peaks)):
        clust = lib_cluster.build_cluster(pixels, peaks[i], thres)
        if clust == None:
            continue
        else:
            clusters.append(clust)

    signature_fmt_1 = 'RESULT: clusters_number={:d}'.format(len(clusters))
    signature_fmt_2 = 'RESULT: cluster_max_top={:d}'.format(max(lums))

    print(signature_fmt_1)
    print(signature_fmt_2)

    sort_clus = lib_cluster.sort_clusters(clusters)
    bcfe = sort_clus[0]

    fig, main_axes = plt.subplots()
    main_axes.imshow(pixels)

    # Draws clusters arround peaks.
    for i in range(len(clusters)):
        xleft = clusters[i].coord[0] - clusters[i].ext
        xright = clusters[i].coord[0] + clusters[i].ext
        ybottom = clusters[i].coord[1] - clusters[i].ext
        ytop = clusters[i].coord[1] + clusters[i].ext
        squarex = [xleft, xright, xright, xleft, xleft]
        squarey = [ybottom, ybottom, ytop, ytop, ybottom]
        plt.plot(squarey, squarex, 'r--')

    plt.show()

    signature_fmt_3 = 'RESULT: cluster_max_integral={:d}'.format(bcfe.lum)
    signature_fmt_4 = 'RESULT: cluster_max_column={:d}'.format(bcfe.coord[1])
    signature_fmt_5 = 'RESULT: cluster_max_row={:d}'.format(bcfe.coord[0])
    signature_fmt_6 = 'RESULT: cluster_max_extension={:d}'.format(bcfe.ext)

    print(signature_fmt_3)
    print(signature_fmt_4)
    print(signature_fmt_5)
    print(signature_fmt_6)

    return 0
示例#9
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
示例#10
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
示例#11
0
def main():
    """ Exercise 6: Display stars """

    lib_fits.init()
    print(lib_fits.file_name)
    global pixels
    global header
    header, pixels = lib_fits.read_first_image(lib_fits.file_name)

    clusters = lib_cluster.find_clusters(pixels)

    for i in range(6):
        rad = lib_stars.cluster_radec(clusters, i, header, pixels)
        lib_stars.celestial_objects(rad, i)
    return 0
示例#12
0
    def __init__(self):

        self.image_file_name = get_fits_file(1)
        self.header_image, self.pixels_image = lib_fits.read_first_image(
            self.image_file_name)

        self.fig, self.main_ax = plt.subplots()

        self.y1_max, self.x1_max = self.pixels_image.shape
        self.border = 5
        self.font_size = 14

        self.pixels_image = np.zeros_like(self.pixels_image)

        self.imgplot = self.main_ax.imshow(self.pixels_image)
        self.update = None
示例#13
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
示例#14
0
def main():
    fig, ax = plt.subplots()
    plt.subplots_adjust(left=0.4)
    pixels, header = lib_fits.read_first_image("../data/common.fits")
    ax.imshow(pixels)
    my_widget_area = plt.axes([0.05, 0.4, 0.25, 0.3],
                              facecolor='lightgoldenrodyellow')

    folder = list(os.walk("../data"))[0]
    dirpath, dirnames, filenames = folder
    radio = RadioButtons(my_widget_area, tuple(filenames))

    def my_action(label):

        pixels, header = lib_fits.read_first_image("../data/" + label)
        ax.imshow(pixels)

    radio.on_clicked(my_action)
    plt.show()
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
示例#16
0
def main():

    file_name, interactive = lib_args.get_args()
    header, pixels = lib_fits.read_first_image(file_name)

    # console output
    print('RESULT: cd1_1 = {CD1_1:.10f}'.format(**header))
    print('RESULT: cd1_2 = {CD1_2:.10f}'.format(**header))
    print('RESULT: cd2_1 = {CD2_1:.10f}'.format(**header))
    print('RESULT: cd2_2 = {CD2_2:.10f}'.format(**header))

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

        _, axis = plt.subplots()
        plt.text(0, -10, file_name, fontsize=14, color='white')
        axis.imshow(pixels)
        plt.show()

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

    file_name, interactive = lib_args.get_args()

    # importing image
    pixels = None
    pixels, header = lib_fits.read_first_image(file_name)
    my_wcs = lib_wcs.get_wcs(header)
    clusters = ex3_clusters.find_clusters(pixels, False)

    for j in range(len(clusters)):
        peak_pixel = lib_wcs.PixelXY(clusters[j].y, clusters[j].x)
        cel_coord = lib_wcs.xy_to_radec(my_wcs, peak_pixel)
        acc_radius = 0.001
        celestial_objects, out, req = lib_stars.get_celestial_objects(
            cel_coord, acc_radius)

        signature_fmt_1 = 'RESULT: right_ascension_{:d} = {:.3f}'.format(
            j, cel_coord[0])
        signature_fmt_2 = 'RESULT: declination_{:d} = {:.3f}'.format(
            j, cel_coord[1])
        print(signature_fmt_1)
        print(signature_fmt_2)
        for i, key in enumerate(celestial_objects.keys()):
            signature_fmt_3 = 'RESULT: celestial_object_{:d}_{:d} = {}'.format(
                j, i, key)
            print(signature_fmt_3)

    # graphic output
    if interactive:
        fig, main_axes = plt.subplots()
        handler = Handler(fig, main_axes, my_wcs)
        main_axes.imshow(pixels)
        #fig.canvas.mpl_connect('motion_notify_event', handler.move)
        fig.canvas.mpl_connect('button_press_event', handler.on_click)

        plt.show()

    # end
    return 0
示例#18
0
def main():
    """ Exercise 5: Stars """

    lib_fits.init()
    print(lib_fits.file_name)
    global pixels
    global header
    header, pixels = lib_fits.read_first_image(lib_fits.file_name)

    clusters = lib_cluster.find_clusters(pixels)

    clusters_radec = []
    for i in range(len(clusters)):
        clusters_radec.append(
            lib_stars.get_radec(clusters[i].coord[0], clusters[i].coord[1],
                                header, pixels))

    signature_fmt_1 = 'RESULT: right_ascension = {:.3f}'.format(
        clusters_radec[0][0])
    signature_fmt_2 = 'RESULT: declination = {:.3f}'.format(
        clusters_radec[0][1])

    print(signature_fmt_1)
    print(signature_fmt_2)

    celestial_objects = stars.get_celestial_objects(clusters_radec[0])
    #print(celestial_objects)

    for i in range(len(celestial_objects[0])):
        signature_fmt_3 = 'RESULT: celestial_object_{:02d} = {:s}'.format(
            i,
            list(celestial_objects[0].keys())[i])
        signature_fmt_4 = 'RESULT: dist_{:02d} = {:5.1f}'.format(
            i,
            list(celestial_objects[0].values())[i])
        print(signature_fmt_3)
        print(signature_fmt_4)

    return 0
示例#19
0
def main():
    """ Exercise 1: Read Image """
    #taking file name

    lib_fits.init()
    print(lib_fits.file_name)
    header, pixels = lib_fits.read_first_image(lib_fits.file_name)

    # show figure
    if lib_fits.interactive:
        fig, main_axes = plt.subplots()
        main_axes.imshow(pixels)
        plt.show()

    #signature

    signature_fmt_1 = 'RESULT: CRPIX1 = {:.0f}'.format(header.get("CRPIX1"))
    signature_fmt_2 = 'RESULT: CRPIX2 = {:.0f}'.format(header.get("CRPIX2"))
    print(signature_fmt_1)
    print(signature_fmt_2)

    return 0
示例#20
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)
    my_wcs = lib_wcs.get_wcs(header)

    sorted_clusters = ex3_clusters.find_clusters(pixels, False)

    peak_pixel = lib_wcs.PixelXY(sorted_clusters[0].y, sorted_clusters[0].x)
    cel_coord = lib_wcs.xy_to_radec(my_wcs, peak_pixel)
    acc_radius = 0.001
    celestial_objects, out, req = lib_stars.get_celestial_objects(
        cel_coord, acc_radius)

    signature_fmt_1 = 'RESULT: right_ascension = {:.3f}'.format(cel_coord[0])
    signature_fmt_2 = 'RESULT: declination = {:.3f}'.format(cel_coord[1])
    print(signature_fmt_1)
    print(signature_fmt_2)
    i = 0
    for key in celestial_objects.keys():
        signature_fmt_3 = 'RESULT: celestial_object_{:d} = {}'.format(i, key)
        print(signature_fmt_3)
        i += 1

    # graphic output
    if interactive:
        # ...
        pass

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

    file_name, interactive = lib_args.get_args()
    header, pixels = lib_fits.read_first_image(file_name)

    z = lib_model.gaussian_model(np.arange(-10.0, 10.0, 1.0), 1.0, 1.0, 1.0)
    print('RESULT: test_gaussian = {:3f}'.format(np.sum(z)))

    y, x = lib_background.build_pixel_histogram(pixels, 200)
    print('RESULT: histogram = {:5d}'.format(np.sum(y)))

    background, dispersion, mx, y, x = lib_background.compute_background_from_histogram((y, x))

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

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

        _, axis = plt.subplots()
        # axis.imshow(pixels)

        z = lib_model.gaussian_model(x, 1.0, background/mx, dispersion/mx)

        plt.plot(x, y, 'b+:', label='data')
        plt.plot(x, z, 'r.:', label='fit')
        plt.legend()
        plt.title('Flux distribution')
        plt.xlabel('Amplitude')
        plt.ylabel('Frequence')

        plt.show()

    return 0
示例#22
0
    def __call__(self, filepath):

        print(filepath)
        header, pixels = lib_fits.read_first_image(filepath)
        imgplot = self.axis.imshow(pixels)
        self.fig.canvas.draw_idle()
示例#23
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
示例#24
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
示例#25
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
示例#26
0
    def my_action(label):

        pixels, header = lib_fits.read_first_image("../data/" + label)
        ax.imshow(pixels)
示例#27
0
def main():
    """ Exercise 3: Peaks """


    lib_fits.init()
    print(lib_fits.file_name)
    header, pixels = lib_fits.read_first_image(lib_fits.file_name)

    # Build Gaussian pattern
    pattern = lib_conv.pattern(9)
    plt.imshow(pattern)
    #plt.show()

    sum_pat = np.sum(pattern)
    pattern = pattern/sum_pat
    sum_pat_norm = np.sum(pattern)
    max_pat_norm = np.max(pattern)


    signature_fmt_1 = 'RESULT: pattern_sum={:.5f}'.format(sum_pat_norm)
    signature_fmt_2 = 'RESULT: pattern_max={:.5f}'.format(max_pat_norm)
    print(signature_fmt_1)
    print(signature_fmt_2)

    # Extend the image
    extended = lib_conv.extend(pixels, 4)
    plt.imshow(extended)
    plt.show()

    height = len(extended)
    width = len(extended[0])
    sum = np.sum(extended)

    signature_fmt_3 = 'RESULT: extended_image_width={:d}'.format(width)
    signature_fmt_4 = 'RESULT: extended_image_height={:d}'.format(height)
    signature_fmt_5 = 'RESULT: extended_image_sum={:.0f}'.format(sum)

    print(signature_fmt_3)
    print(signature_fmt_4)
    print(signature_fmt_5)


    # Convoluted

    conv = lib_conv.scan(extended, 4, pattern)

    plt.imshow(conv)
    plt.show()

    conv_height = len(conv)
    conv_width = len(conv[0])
    conv_sum = np.sum(conv)

    signature_fmt_6 = 'RESULT: convoluted_image_width={:d}'.format(conv_width)
    signature_fmt_7 = 'RESULT: convoluted_image_height={:d}'.format(conv_height)
    signature_fmt_8 = 'RESULT: convoluted_image_sum={:.0f}'.format(conv_sum)

    print(signature_fmt_6)
    print(signature_fmt_7)
    print(signature_fmt_8)

    # Extend the convolution image

    conv_ext = lib_conv.extend(conv, 1)

    conv_ext_height = len(conv_ext)
    conv_ext_width = len(conv_ext[0])
    conv_ext_sum = np.sum(conv_ext)

    signature_fmt_9 = 'RESULT: extended_convoluted_image_width={:d}'.format(conv_ext_width)
    signature_fmt_10 = 'RESULT: extended_convoluted_image_height={:d}'.format(conv_ext_height)
    signature_fmt_11 = 'RESULT: extended_convoluted_image_sum={:.0f}'.format(conv_ext_sum)

    print(signature_fmt_9)
    print(signature_fmt_10)
    print(signature_fmt_11)

    # Identify the Peaks


    threshold = lib_background.threshold(pixels)
    print(threshold)
    peaks = lib_conv.peaks(conv_ext, 1, threshold)

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

    print(signature_fmt_12)

    # end
    return 0
示例#28
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)