Esempio n. 1
0
    def testPositions(self):
        # The pixel position x, y of the source should be the same as the
        # position of the maximum in the generated result map.
        x_pos = 40
        y_pos = 60

        empty_data = numpy.zeros((100, 100))
        SrcMeasurement = namedtuple("SrcMeasurement",
            ['peak', 'x', 'y', 'smaj', 'smin', 'theta']
        )
        src_measurement = SrcMeasurement(
            peak=Uncertain(10), x=Uncertain(x_pos), y=Uncertain(y_pos),
            smaj=Uncertain(10), smin=Uncertain(10), theta=Uncertain(0)
        )
        gaussian_map, residual_map = generate_result_maps(empty_data, [src_measurement])
        gaussian_max_x = numpy.where(gaussian_map==gaussian_map.max())[0][0]
        gaussian_max_y = numpy.where(gaussian_map==gaussian_map.max())[1][0]

        self.assertEqual(gaussian_max_x, x_pos)
        self.assertEqual(gaussian_max_y, y_pos)
Esempio n. 2
0
    def testPositions(self):
        # The pixel position x, y of the source should be the same as the
        # position of the maximum in the generated result map.
        x_pos = 40
        y_pos = 60

        empty_data = numpy.zeros((100, 100))
        SrcMeasurement = namedtuple(
            "SrcMeasurement", ['peak', 'x', 'y', 'smaj', 'smin', 'theta'])
        src_measurement = SrcMeasurement(peak=Uncertain(10),
                                         x=Uncertain(x_pos),
                                         y=Uncertain(y_pos),
                                         smaj=Uncertain(10),
                                         smin=Uncertain(10),
                                         theta=Uncertain(0))
        gaussian_map, residual_map = generate_result_maps(
            empty_data, [src_measurement])
        gaussian_max_x = numpy.where(gaussian_map == gaussian_map.max())[0][0]
        gaussian_max_y = numpy.where(gaussian_map == gaussian_map.max())[1][0]

        self.assertEqual(gaussian_max_x, x_pos)
        self.assertEqual(gaussian_max_y, y_pos)
Esempio n. 3
0
def run_sourcefinder(files, options):
    """
    Iterate over the list of files, running a sourcefinding step on each in
    turn. If specified, a DS9-compatible region file and/or a FITS file
    showing the residuals after Gaussian fitting are dumped for each file.
    A string containing a human readable list of sources is returned.
    """
    output = StringIO()

    beam = get_beam(options.bmaj, options.bmin, options.bpa)
    configuration = get_sourcefinder_configuration(options)

    if options.mode == "detimage":
        labels, labelled_data = get_detection_labels(options.detection_image,
                                                     options.detection,
                                                     options.analysis, beam,
                                                     configuration)
    else:
        labels, labelled_data = [], None

    for counter, filename in enumerate(files):
        print "Processing %s (file %d of %d)." % (filename, counter + 1,
                                                  len(files))
        imagename = os.path.splitext(os.path.basename(filename))[0]
        ff = open_accessor(filename, beam=beam, plane=0)
        imagedata = sourcefinder_image_from_accessor(ff, **configuration)

        if options.mode == "fixed":
            sr = imagedata.fit_fixed_positions(
                options.fixed_coords, options.ffbox * max(imagedata.beam[0:2]))

        else:
            if options.mode == "fdr":
                print "Using False Detection Rate algorithm with alpha = %f" % (
                    options.alpha, )
                sr = imagedata.fd_extract(
                    alpha=options.alpha,
                    deblend_nthresh=options.deblend_thresholds,
                    force_beam=options.force_beam)
            else:
                if labelled_data is None:
                    print "Thresholding with det = %f sigma, analysis = %f sigma" % (
                        options.detection, options.analysis)

                sr = imagedata.extract(
                    det=options.detection,
                    anl=options.analysis,
                    labelled_data=labelled_data,
                    labels=labels,
                    deblend_nthresh=options.deblend_thresholds,
                    force_beam=options.force_beam)

        if options.regions:
            regionfile = imagename + ".reg"
            regionfile = open(regionfile, 'w')
            regionfile.write(regions(sr))
            regionfile.close()
        if options.residuals or options.islands:
            gaussian_map, residual_map = generate_result_maps(
                imagedata.data, sr)
        if options.residuals:
            residualfile = imagename + ".residuals.fits"
            writefits(residualfile, residual_map, pyfits.getheader(filename))
        if options.islands:
            islandfile = imagename + ".islands.fits"
            writefits(islandfile, gaussian_map, pyfits.getheader(filename))
        if options.rmsmap:
            rmsfile = imagename + ".rms.fits"
            writefits(rmsfile, numpy.array(imagedata.rmsmap),
                      pyfits.getheader(filename))
        if options.sigmap:
            sigfile = imagename + ".sig.fits"
            writefits(sigfile,
                      numpy.array(imagedata.data_bgsubbed / imagedata.rmsmap),
                      pyfits.getheader(filename))
        if options.skymodel:
            with open(imagename + ".skymodel", 'w') as skymodelfile:
                if ff.freq_eff:
                    skymodelfile.write(skymodel(sr, ff.freq_eff))
                else:
                    print "WARNING: Using default reference frequency for %s" % (
                        skymodelfile.name, )
                    skymodelfile.write(skymodel(sr))
        if options.csv:
            with open(imagename + ".csv", 'w') as csvfile:
                csvfile.write(csv(sr))
        print >> output, summary(filename, sr),
    return output.getvalue()
Esempio n. 4
0
File: pyse.py Progetto: hughbg/tkp
def run_sourcefinder(files, options):
    """
    Iterate over the list of files, running a sourcefinding step on each in
    turn. If specified, a DS9-compatible region file and/or a FITS file
    showing the residuals after Gaussian fitting are dumped for each file.
    A string containing a human readable list of sources is returned.
    """
    output = StringIO()

    beam = get_beam(options.bmaj, options.bmin, options.bpa)
    configuration = get_sourcefinder_configuration(options)

    if options.mode == "detimage":
        labels, labelled_data = get_detection_labels(
            options.detection_image, options.detection, options.analysis, beam, configuration
        )
    else:
        labels, labelled_data = [], None

    for counter, filename in enumerate(files):
        print "Processing %s (file %d of %d)." % (filename, counter + 1, len(files))
        imagename = os.path.splitext(os.path.basename(filename))[0]
        ff = open_accessor(filename, beam=beam, plane=0)
        imagedata = sourcefinder_image_from_accessor(ff, **configuration)

        if options.mode == "fixed":
            sr = imagedata.fit_fixed_positions(options.fixed_coords, options.ffbox * max(imagedata.beam[0:2]))

        else:
            if options.mode == "fdr":
                print "Using False Detection Rate algorithm with alpha = %f" % (options.alpha,)
                sr = imagedata.fd_extract(
                    alpha=options.alpha, deblend_nthresh=options.deblend_thresholds, force_beam=options.force_beam
                )
            else:
                if labelled_data is None:
                    print "Thresholding with det = %f sigma, analysis = %f sigma" % (
                        options.detection,
                        options.analysis,
                    )

                sr = imagedata.extract(
                    det=options.detection,
                    anl=options.analysis,
                    labelled_data=labelled_data,
                    labels=labels,
                    deblend_nthresh=options.deblend_thresholds,
                    force_beam=options.force_beam,
                )

        if options.regions:
            regionfile = imagename + ".reg"
            regionfile = open(regionfile, "w")
            regionfile.write(regions(sr))
            regionfile.close()
        if options.residuals or options.islands:
            gaussian_map, residual_map = generate_result_maps(imagedata.data, sr)
        if options.residuals:
            residualfile = imagename + ".residuals.fits"
            writefits(residualfile, residual_map, pyfits.getheader(filename))
        if options.islands:
            islandfile = imagename + ".islands.fits"
            writefits(islandfile, gaussian_map, pyfits.getheader(filename))
        if options.rmsmap:
            rmsfile = imagename + ".rms.fits"
            writefits(rmsfile, numpy.array(imagedata.rmsmap), pyfits.getheader(filename))
        if options.sigmap:
            sigfile = imagename + ".sig.fits"
            writefits(sigfile, numpy.array(imagedata.data_bgsubbed / imagedata.rmsmap), pyfits.getheader(filename))
        if options.skymodel:
            with open(imagename + ".skymodel", "w") as skymodelfile:
                if ff.freq_eff:
                    skymodelfile.write(skymodel(sr, ff.freq_eff))
                else:
                    print "WARNING: Using default reference frequency for %s" % (skymodelfile.name,)
                    skymodelfile.write(skymodel(sr))
        if options.csv:
            with open(imagename + ".csv", "w") as csvfile:
                csvfile.write(csv(sr))
        print >> output, summary(filename, sr),
    return output.getvalue()
Esempio n. 5
0
def run_sourcefinder(files, options):
    """
    Iterate over the list of files, running a sourcefinding step on each in
    turn. If specified, a DS9-compatible region file and/or a FITS file
    showing the residuals after Gaussian fitting are dumped for each file.
    A string containing a human readable list of sources is returned.
    """
    output = StringIO()
    configuration = {
        "back_sizex": options.grid,
        "back_sizey": options.grid,
        "margin": options.margin,
        "radius": options.radius,
        "deblend": bool(options.deblend),
        "deblend_nthresh": options.deblend_thresholds,
        "force_beam": options.force_beam
    }
    if options.residuals or options.islands:
        configuration['residuals'] = True
    if options.detection_image:
        if options.fdr:
            print "WARNING: --detection-image not supported with --fdr; ignored"
        else:
            print "Detecting islands in %s" % (options.detection_image)
            print "Thresholding with det = %f sigma, analysis = %f sigma" % (options.detection, options.analysis)
            if (
                isinstance(options.bmaj, float)
                and isinstance(options.bmin, float)
                and isinstance(options.bpa, float)
            ):
                ff = FitsImage(options.detection_image, beam=(options.bmaj, options.bmin, options.bpa), plane=0)
            else:
                ff = FitsImage(options.detection_image, plane=0)
            imagedata = sourcefinder_image_from_accessor(ff, **configuration)
            labels, labelled_data = imagedata.label_islands(
                options.detection * imagedata.rmsmap, options.analysis * imagedata.rmsmap
            )
    else:
        labels, labelled_data = [], None
    for counter, filename in enumerate(files):
        print "Processing %s (file %d of %d)." % (filename, counter+1, len(files))
        if (
            isinstance(options.bmaj, float)
            and isinstance(options.bmin, float)
            and isinstance(options.bpa, float)
        ):
            ff = FitsImage(filename, beam=(options.bmaj, options.bmin, options.bpa), plane=0)
        else:
            ff = FitsImage(filename, plane=0)
        imagedata = sourcefinder_image_from_accessor(ff, **configuration)
        if options.fdr:
            print "Using False Detection Rate algorithm with alpha = %f" % (options.alpha,)
            sr = imagedata.fd_extract(options.alpha)
        else:
            if labelled_data is None:
                print "Thresholding with det = %f sigma, analysis = %f sigma" % (options.detection, options.analysis)
            sr = imagedata.extract(options.detection, options.analysis, labelled_data=labelled_data, labels=labels)
        if options.regions:
            regionfile = os.path.splitext(os.path.basename(filename))[0] + ".reg"
            regionfile = open(regionfile, 'w')
            regionfile.write(regions(sr))
            regionfile.close()
        if options.residuals or options.islands:
            gaussian_map, residual_map = generate_result_maps(imagedata.data, sr)
        if options.residuals:
            residualfile = os.path.splitext(os.path.basename(filename))[0] + ".residuals.fits"
            writefits(residualfile, residual_map, pyfits.getheader(filename))
        if options.islands:
            islandfile = os.path.splitext(os.path.basename(filename))[0] + ".islands.fits"
            writefits(islandfile, gaussian_map, pyfits.getheader(filename))
        if options.rmsmap:
            rmsfile = os.path.splitext(os.path.basename(filename))[0] + ".rms.fits"
            writefits(rmsfile, numpy.array(imagedata.rmsmap), pyfits.getheader(filename))
        if options.sigmap:
            sigfile = os.path.splitext(os.path.basename(filename))[0] + ".sig.fits"
            writefits(sigfile, numpy.array(imagedata.data_bgsubbed/imagedata.rmsmap), pyfits.getheader(filename))
        if options.skymodel:
            skymodelfile = os.path.splitext(os.path.basename(filename))[0] + ".skymodel"
            skymodelfile = open(skymodelfile, 'w')
            if ff.freq_eff:
                skymodelfile.write(skymodel(sr, ff.freq_eff))
            else:
                print "WARNING: Using default reference frequency for %s" % (skymodelfile.name,)
                skymodelfile.write(skymodel(sr))
            skymodelfile.close()
        if options.csv:
            csvfile = os.path.splitext(os.path.basename(filename))[0] + ".csv"
            csvfile = open(csvfile, 'w')
            csvfile.write(csv(sr))
            csvfile.close()
        print >>output, summary(filename, sr),
    return output.getvalue()