def testWcsConversionConsistency(self): """ Check that extracting a source from FITS and CASA versions of the same dataset gives the same results (especially, RA and Dec). """ fits_image = accessors.sourcefinder_image_from_accessor( accessors.FitsImage(os.path.join(DATAPATH, 'sourcefinder/GRB130828A/SWIFT_554620-130504.fits'))) # Abuse the KAT7 CasaImage class here, since we just want to access # the pixel data and the WCS: casa_image = accessors.sourcefinder_image_from_accessor( accessors.kat7casaimage.Kat7CasaImage( os.path.join(DATAPATH, 'sourcefinder/GRB130828A/SWIFT_554620-130504.image'))) ew_sys_err, ns_sys_err = 0.0, 0.0 fits_results = fits_image.extract(det=5, anl=3) fits_results = [result.serialize(ew_sys_err, ns_sys_err) for result in fits_results] casa_results = casa_image.extract(det=5, anl=3) casa_results = [result.serialize(ew_sys_err, ns_sys_err) for result in casa_results] self.assertEqual(len(fits_results), 1) self.assertEqual(len(casa_results), 1) fits_src = fits_results[0] casa_src = casa_results[0] self.assertEqual(len(fits_src),len(casa_src)) for idx, _ in enumerate(fits_src): self.assertAlmostEqual(fits_src[idx], casa_src[idx], places=5)
def testFlatImage(self): sfimage = accessors.sourcefinder_image_from_accessor( SyntheticImage(data=np.zeros((512,512)))) self.assertTrue(np.ma.max(sfimage.data) == np.ma.min(sfimage.data), msg = "Data should be flat") with self.assertRaises(RuntimeError): sfimage.extract(det=5,anl=3)
def get_detection_labels(filename, det, anl, beam, configuration, plane=0): print "Detecting islands in %s" % (filename,) print "Thresholding with det = %f sigma, analysis = %f sigma" % (det, anl) ff = open_accessor(filename, beam=beam, plane=plane) imagedata = sourcefinder_image_from_accessor(ff, **configuration) labels, labelled_data = imagedata.label_islands(det * imagedata.rmsmap, anl * imagedata.rmsmap) return labels, labelled_data
def forced_fits(image_path, positions, parset): """ Perform forced source measurements on an image based on a list of positions. :param image_path: path to image for measurements. :param positions: list of (ra, dec) pairs for measurement. :param parset: configuration parameterset as a dictionary. """ logger.info("Forced fitting in image: %s" % (image_path)) fitsimage = tkp.accessors.open(image_path) data_image = sourcefinder_image_from_accessor(fitsimage, margin=parset['margin'], radius=parset['radius'], detection_threshold=parset['detection_threshold'], analysis_threshold=parset['analysis_threshold'], ra_sys_err=parset['ra_sys_err'], dec_sys_err=parset['dec_sys_err']) if len(positions): boxsize = parset['box_in_beampix'] * max(data_image.beam[0], data_image.beam[1]) forced_fits = data_image.fit_fixed_positions(positions, boxsize) return [forced_fit.serialize() for forced_fit in forced_fits] else: return []
def extract_sources(image_path, parset): """ Extract sources from an image. :param image_path: path to file from which to extract sources. :param parset: dictionary containing at least the detection and analysis threshold and the association radius, the last one a multiplication factor of the de Ruiter radius. :returns: list of source measurements. """ logger.info("Extracting image: %s" % image_path) accessor = tkp.accessors.open(image_path) logger.debug("Detecting sources in image %s at detection threshold %s", image_path, parset['detection_threshold']) data_image = sourcefinder_image_from_accessor(accessor, margin=parset['margin'], radius=parset['radius'], detection_threshold=parset['detection_threshold'], analysis_threshold=parset['analysis_threshold'], ra_sys_err=parset['ra_sys_err'], dec_sys_err=parset['dec_sys_err'], force_beam=parset['force_beam']) logger.debug("Employing margin: %s extraction radius: %s deblend: %s deblend_nthresh: %s", parset['margin'], parset['radius'], parset['deblend'], parset['deblend_nthresh'] ) results = data_image.extract() # "blind" extraction of sources logger.info("Detected %d sources in image %s" % (len(results), image_path)) return [r.serialize() for r in results]
def testSingleSourceExtraction(self): """ Single source extaction From visual inspection we only expect a single source in the image, at around 5 or 6 sigma detection level.""" known_result = ( 136.89603241069054, 14.022184792492785, #RA, DEC 0.0005341819139061954, 0.0013428186757078464, #Err, Err 0.0007226590529214518, 0.00010918184742211533, #Peak flux, err 0.0006067963179204716, 0.00017037685531724465, #Integrated flux, err 6.192259965962862, 25.516190123153514, #Significance level, Beam semimajor-axis width (arcsec) 10.718798843620489, 178.62899212789304, #Beam semiminor-axis width (arcsec), Beam parallactic angle 0.0, 0.0 #ra_sys_err, dec_sys_err ) self.image = accessors.sourcefinder_image_from_accessor( accessors.FitsImage(os.path.join(DATAPATH, 'GRB120422A/GRB120422A-120429.fits'))) results = self.image.extract(det=5, anl=3) results = [result.serialize() for result in results] self.assertEqual(len(results), 1) r = results[0] self.assertEqual(len(r), len(known_result)) for i in range(len(r)): self.assertAlmostEqual(r[i], known_result[i], places=5)
def testMaskedBackgroundBlind(self): self.image = accessors.sourcefinder_image_from_accessor( accessors.open(os.path.join(DATAPATH, "L41391_0.img.restored.corr.fits")), radius=1.0, ) result = self.image.extract() self.assertFalse(result)
def forced_fits(image_path, positions, extraction_params): """ Perform forced source measurements on an image based on a list of positions. :param image_path: path to image for measurements. :param positions: list of (ra, dec) pairs for measurement. :param extraction_params: source extraction parameters, as a dictionary. """ logger.info("Forced fitting in image: %s" % (image_path)) fitsimage = tkp.accessors.open(image_path) data_image = sourcefinder_image_from_accessor(fitsimage, margin=extraction_params['margin'], radius=extraction_params['extraction_radius_pix'], back_size_x=extraction_params['back_size_x'], back_size_y=extraction_params['back_size_y']) if len(positions): boxsize = extraction_params['box_in_beampix'] * max(data_image.beam[0], data_image.beam[1]) forced_fits = data_image.fit_fixed_positions(positions, boxsize) return [ forced_fit.serialize( extraction_params['ew_sys_err'], extraction_params['ns_sys_err'] ) for forced_fit in forced_fits ] else: return []
def testFlatImage(self): sfimage = accessors.sourcefinder_image_from_accessor( SyntheticImage(data=np.zeros((512, 512)))) self.assertTrue(np.ma.max(sfimage.data) == np.ma.min(sfimage.data), msg="Data should be flat") with self.assertRaises(RuntimeError): sfimage.extract(det=5, anl=3)
def testMaskedBackgroundForcedFit(self): """ Background at forced fit is masked """ self.image = accessors.sourcefinder_image_from_accessor( accessors.open(fits_file), radius=1.0) result = self.image.fit_to_point(256, 256, 10, 0, None) self.assertFalse(result)
def get_detection_labels(filename, det, anl, beam, configuration, plane=0): print "Detecting islands in %s" % (filename, ) print "Thresholding with det = %f sigma, analysis = %f sigma" % (det, anl) ff = open_accessor(filename, beam=beam, plane=plane) imagedata = sourcefinder_image_from_accessor(ff, **configuration) labels, labelled_data = imagedata.label_islands(det * imagedata.rmsmap, anl * imagedata.rmsmap) return labels, labelled_data
def test_casaimage(self): results = self.accessor.extract_metadata() sfimage = accessors.sourcefinder_image_from_accessor(self.accessor) known_bmaj, known_bmin, known_bpa = 3.33, 3.33, 0 bmaj, bmin, bpa = self.accessor.beam self.assertAlmostEqual(known_bmaj, bmaj, 2) self.assertAlmostEqual(known_bmin, bmin, 2) self.assertAlmostEqual(known_bpa, bpa, 2)
def setUpClass(cls): cls.temp_dir = tempfile.mkdtemp() cls.start_dir = os.getcwd() os.chdir(cls.temp_dir) cls.filename = os.path.join(cls.temp_dir, 'playground.fits') shutil.copy(orig_fits_file, cls.filename) cls.fits = FitsImage(cls.filename, beam=(.5, .5, .5)) cls.imagedata = sourcefinder_image_from_accessor(cls.fits) cls.sourcelist = cls.imagedata.extract()
def test_casaimage(self): results = self.accessor.extract_metadata() sfimage = accessors.sourcefinder_image_from_accessor(self.accessor) known_bmaj, known_bmin, known_bpa = 4.4586, 4.458, 0 bmaj, bmin, bpa = self.accessor.beam self.assertAlmostEqual(known_bmaj, bmaj, 2) self.assertAlmostEqual(known_bmin, bmin, 2) self.assertAlmostEqual(known_bpa, bpa, 2)
def testWholeSourceMasked(self): """ Source in masked region """ self.image = accessors.sourcefinder_image_from_accessor( accessors.FitsImage(GRB120422A)) self.image.data[250:280, 250:280] = np.ma.masked results = self.image.extract(det=5, anl=3) self.assertFalse(results)
def test_casaimage(self): self.assertTrue(isinstance(self.accessor, LofarAccessor)) results = self.accessor.extract_metadata() sfimage = accessors.sourcefinder_image_from_accessor(self.accessor) known_bmaj, known_bmin, known_bpa = 2.64, 1.85, 1.11 bmaj, bmin, bpa = self.accessor.beam self.assertAlmostEqual(known_bmaj, bmaj, 2) self.assertAlmostEqual(known_bmin, bmin, 2) self.assertAlmostEqual(known_bpa, bpa, 2)
def test_casaimage(self): self.assertEqual(self.accessor.telescope, "LOFAR") results = self.accessor.extract_metadata() sfimage = accessors.sourcefinder_image_from_accessor(self.accessor) known_bmaj, known_bmin, known_bpa = 2.64, 1.85, 1.11 bmaj, bmin, bpa = self.accessor.beam self.assertAlmostEqual(known_bmaj, bmaj, 2) self.assertAlmostEqual(known_bmin, bmin, 2) self.assertAlmostEqual(known_bpa, bpa, 2)
def test_casaimage(self): self.assertEqual(self.accessor.telescope, 'KAT-7') results = self.accessor.extract_metadata() sfimage = accessors.sourcefinder_image_from_accessor(self.accessor) known_bmaj, known_bmin, known_bpa = 3.33, 3.33, 0 bmaj, bmin, bpa = self.accessor.beam self.assertAlmostEqual(known_bmaj, bmaj, 2) self.assertAlmostEqual(known_bmin, bmin, 2) self.assertAlmostEqual(known_bpa, bpa, 2)
def testMaskedBackgroundForcedFit(self): """ Background at forced fit is masked """ self.image = accessors.sourcefinder_image_from_accessor( accessors.open(os.path.join(DATAPATH, "L41391_0.img.restored.corr.fits")), radius=1.0, ) result = self.image.fit_to_point(256, 256, 10, 0, None) self.assertFalse(result)
def testFreqinfo(self): database = Database() dataset = DataSet(data={"description": "dataset"}, database=database) # image without frequency information image = FitsImage(os.path.join(DATAPATH, "VLSS.fits")) # The database requires frequency information # self.assertRaises(ValueError, accessors.dbimage_from_accessor, dataset, image) # But the sourcefinder does not need frequency information self.assertListEqual(list(accessors.sourcefinder_image_from_accessor(image).data.shape), [2048, 2048]) tkp.db.rollback()
def testWholeSourceMasked(self): """ Part of source masked Tip of major axis is around 267, 264 """ self.image = accessors.sourcefinder_image_from_accessor( accessors.FitsImage(GRB120422A)) self.image.data[266:269, 263:266] = np.ma.masked results = self.image.extract(det=5, anl=3) self.assertFalse(results)
def test_casaimage(self): results = self.accessor.extract_metadata() sfimage = accessors.sourcefinder_image_from_accessor(self.accessor) known_bmaj, known_bmin, known_bpa = (4.002118682861328, 2.4657058715820312, 0.3598241556754317) bmaj, bmin, bpa = self.accessor.beam self.assertAlmostEqual(known_bmaj, bmaj, 2) self.assertAlmostEqual(known_bmin, bmin, 2) self.assertAlmostEqual(known_bpa, bpa, 2)
def testForceSourceShape(self): """ Force source shape to beam This image contains a single source (with parameters as listed under testSingleSourceExtraction(), above). Here we force the lengths of the major/minor axes to be held constant when fitting. """ self.image = accessors.sourcefinder_image_from_accessor( accessors.FitsImage(GRB120422A)) results = self.image.extract(det=5, anl=3, force_beam=True) self.assertEqual(results[0].smaj.value, self.image.beam[0]) self.assertEqual(results[0].smin.value, self.image.beam[1])
def testNoLabelledIslandsCase(self): """ If an image is in fact very boring and flat/empty, then we may not even locate any labelled islands, if the analysis threshold is set high enough. (We reproduce this test case, even though GRB120422A-120429 has a source in the image, just by setting the thresholds very high - this avoids requiring additional data). """ self.image = accessors.sourcefinder_image_from_accessor( accessors.FitsImage(GRB120422A)) results = self.image.extract(det=5e10, anl=5e10) results = [result.serialize() for result in results] self.assertEqual(len(results), 0)
def setUpClass(cls): cls.temp_dir = tempfile.mkdtemp() cls.start_dir = os.getcwd() os.chdir(cls.temp_dir) cls.filename = os.path.join(cls.temp_dir, 'playground.fits') shutil.copy(orig_fits_file, cls.filename) cls.fits = FitsImage(cls.filename) cls.imagedata = sourcefinder_image_from_accessor(cls.fits) cls.sourcelist = cls.imagedata.extract( det=10, anl=3, deblend_nthresh=options['deblend_thresholds'], force_beam=options['force_beam'] ) assert(len(cls.sourcelist) > 0)
def setUpClass(cls): cls.temp_dir = tempfile.mkdtemp() cls.start_dir = os.getcwd() os.chdir(cls.temp_dir) cls.filename = os.path.join(cls.temp_dir, 'playground.fits') shutil.copy(orig_fits_file, cls.filename) cls.fits = FitsImage(cls.filename) cls.imagedata = sourcefinder_image_from_accessor(cls.fits) cls.sourcelist = cls.imagedata.extract( det=10, anl=3, deblend_nthresh=options['deblend_thresholds'], force_beam=options['force_beam']) assert (len(cls.sourcelist) > 0)
def testWcsConversionConsistency(self): """ Check that extracting a source from FITS and CASA versions of the same dataset gives the same results (especially, RA and Dec). """ fits_image = accessors.sourcefinder_image_from_accessor( accessors.FitsImage( os.path.join( DATAPATH, 'sourcefinder/GRB130828A/SWIFT_554620-130504.fits'))) # Abuse the KAT7 CasaImage class here, since we just want to access # the pixel data and the WCS: casa_image = accessors.sourcefinder_image_from_accessor( accessors.kat7casaimage.Kat7CasaImage( os.path.join( DATAPATH, 'sourcefinder/GRB130828A/SWIFT_554620-130504.image'))) ew_sys_err, ns_sys_err = 0.0, 0.0 fits_results = fits_image.extract(det=5, anl=3) fits_results = [ result.serialize(ew_sys_err, ns_sys_err) for result in fits_results ] casa_results = casa_image.extract(det=5, anl=3) casa_results = [ result.serialize(ew_sys_err, ns_sys_err) for result in casa_results ] self.assertEqual(len(fits_results), 1) self.assertEqual(len(casa_results), 1) fits_src = fits_results[0] casa_src = casa_results[0] self.assertEqual(len(fits_src), len(casa_src)) for idx, _ in enumerate(fits_src): self.assertAlmostEqual(fits_src[idx], casa_src[idx], places=5)
def perform_forced_fits(fit_posns, fit_ids, accessor, extraction_params): """ Perform forced source measurements on an image based on a list of positions. Args: fit_posns (tuple): List of (RA, Dec) tuples: Positions to be fit. fit_ids: List of identifiers for each requested fit position. image_path (str): path to image for measurements. extraction_params (dict): source extraction parameters, as a dictionary. Returns: tuple: A matched pair of lists (serialized_fits, ids), corresponding to successfully fitted positions. NB returned lists may be shorter than input lists if some fits are unsuccessful. """ logger.debug("Forced fitting in image: %s" % (accessor.url)) if not len(fit_ids): logging.debug("nothing to force fit") return [], [] margin = extraction_params['margin'] radius = extraction_params['extraction_radius_pix'] back_size_x = extraction_params['back_size_x'] back_size_y = extraction_params['back_size_y'] data_image = sourcefinder_image_from_accessor(accessor, margin=margin, radius=radius, back_size_x=back_size_x, back_size_y=back_size_y) box_in_beampix = extraction_params['box_in_beampix'] boxsize = box_in_beampix * max(data_image.beam[0], data_image.beam[1]) fits = data_image.fit_fixed_positions(fit_posns, boxsize, ids=fit_ids) successful_fits, successful_ids = fits if successful_fits: serialized = [ f.serialize(extraction_params['ew_sys_err'], extraction_params['ns_sys_err']) for f in successful_fits ] return serialized, successful_ids else: return [], []
def extract_sources(image_path, extraction_params): """ Extract sources from an image. args: image_path: path to file from which to extract sources. extraction_params: dictionary containing at least the detection and analysis threshold and the association radius, the last one a multiplication factor of the de Ruiter radius. returns: list of ExtractionResults named tuples containing source measurements, min RMS value and max RMS value """ logger.info("Extracting image: %s" % image_path) accessor = tkp.accessors.open(image_path) logger.debug("Detecting sources in image %s at detection threshold %s", image_path, extraction_params['detection_threshold']) data_image = sourcefinder_image_from_accessor(accessor, margin=extraction_params['margin'], radius=extraction_params['extraction_radius_pix'], back_size_x=extraction_params['back_size_x'], back_size_y=extraction_params['back_size_y']) logger.debug("Employing margin: %s extraction radius: %s deblend_nthresh: %s", extraction_params['margin'], extraction_params['extraction_radius_pix'], extraction_params['deblend_nthresh'] ) # "blind" extraction of sources results = data_image.extract( det=extraction_params['detection_threshold'], anl=extraction_params['analysis_threshold'], deblend_nthresh=extraction_params['deblend_nthresh'], force_beam=extraction_params['force_beam'] ) logger.info("Detected %d sources in image %s" % (len(results), image_path)) ew_sys_err = extraction_params['ew_sys_err'] ns_sys_err = extraction_params['ns_sys_err'] serialized = [r.serialize(ew_sys_err, ns_sys_err) for r in results] return ExtractionResults(sources=serialized, rms_min=float(data_image.rmsmap.min()), rms_max=float(data_image.rmsmap.max()) )
def perform_forced_fits(fit_posns, fit_ids, accessor, extraction_params): """ Perform forced source measurements on an image based on a list of positions. Args: fit_posns (list): List of (RA, Dec) tuples: Positions to be fit. fit_ids: List of identifiers for each requested fit position. image_path (str): path to image for measurements. extraction_params (dict): source extraction parameters, as a dictionary. Returns: tuple: A matched pair of lists (serialized_fits, ids), corresponding to successfully fitted positions. NB returned lists may be shorter than input lists if some fits are unsuccessful. """ logger.debug("Forced fitting in image: %s" % (accessor.url)) if not len(fit_ids): logging.debug("nothing to force fit") return [], [] margin = extraction_params['margin'] radius = extraction_params['extraction_radius_pix'] back_size_x = extraction_params['back_size_x'] back_size_y = extraction_params['back_size_y'] data_image = sourcefinder_image_from_accessor(accessor, margin=margin, radius=radius, back_size_x=back_size_x, back_size_y=back_size_y) box_in_beampix = extraction_params['box_in_beampix'] boxsize = box_in_beampix * max(data_image.beam[0], data_image.beam[1]) fits = data_image.fit_fixed_positions( fit_posns, boxsize, ids=fit_ids) successful_fits, successful_ids = fits if successful_fits: serialized = [ f.serialize( extraction_params['ew_sys_err'], extraction_params['ns_sys_err']) for f in successful_fits] return serialized, successful_ids else: return [], []
def setUp(self): """ NB the required image has been committed to the tkp/data subversion repository. (See tkp/data/unittests/tkp_lib for a full copy of all the unittest data). Source positions / background positions were simply picked out by eye in DS9 """ self.image = accessors.sourcefinder_image_from_accessor( accessors.open(os.path.join(DATAPATH, 'NCP_sample_image_1.fits')) ) self.assertListEqual(list(self.image.data.shape),[1024,1024]) self.boxsize = BOX_IN_BEAMPIX*max(self.image.beam[0], self.image.beam[1]) self.bright_src_posn = (215.83993,86.307504) #RA, DEC self.background_posn = (186.33731,82.70002) #RA, DEC ##NB These are simply plucked from a previous run, # so they merely ensure *consistent*, rather than *correct*, results. self.known_fit_results = [215.84 , 86.31 , 9.88] #RA, DEC, PEAK
def setUp(self): """ NB the required image has been committed to the tkp/data subversion repository. (See tkp/data/unittests/tkp_lib for a full copy of all the unittest data). Source positions / background positions were simply picked out by eye in DS9 """ self.image = accessors.sourcefinder_image_from_accessor( accessors.open(os.path.join(DATAPATH, 'sourcefinder/NCP_sample_image_1.fits')) ) self.assertListEqual(list(self.image.data.shape),[1024,1024]) self.boxsize = BOX_IN_BEAMPIX*max(self.image.beam[0], self.image.beam[1]) self.bright_src_posn = (35.76726,86.305771) #RA, DEC self.background_posn = (6.33731,82.70002) #RA, DEC # #NB Peak of forced gaussian fit is simply plucked from a previous run; # so merely ensures *consistent*, rather than *correct*, results. self.known_fit_results = (self.bright_src_posn[0], # RA, self.bright_src_posn[1], # Dec 13.457697411730384) # Peak
def testSingleSourceExtraction(self): """ Single source extaction From visual inspection we only expect a single source in the image, at around 5 or 6 sigma detection level.""" ew_sys_err, ns_sys_err = 0.0, 0.0 known_result = ( 136.89603241069054, 14.022184792492785, # RA, DEC 0.0005341819139061954, 0.0013428186757078464, # Err, Err 0.0007226590529214518, 0.00010918184742211533, # Peak flux, err 0.0006067963179204716, 0.00017037685531724465, # Integrated flux, err 6.192259965962862, 25.516190123153514, # Significance level, Beam semimajor-axis width (arcsec) 10.718798843620489, 178.62899212789304, # Beam semiminor-axis width (arcsec), Beam parallactic angle ew_sys_err, ns_sys_err, 5.181697175052841, # error_radius 1, # fit_type 0.59184643302, #chisq 0.67199741142, #reduced chisq ) self.image = accessors.sourcefinder_image_from_accessor( accessors.FitsImage(GRB120422A)) results = self.image.extract(det=5, anl=3) results = [ result.serialize(ew_sys_err, ns_sys_err) for result in results ] self.assertEqual(len(results), 1) r = results[0] self.assertEqual(len(r), len(known_result)) for i in range(len(r)): self.assertAlmostEqual(r[i], known_result[i], places=5)
def extract_sources(image_path, extraction_params): """ Extract sources from an image. :param image_path: path to file from which to extract sources. :param extraction_params: dictionary containing at least the detection and analysis threshold and the association radius, the last one a multiplication factor of the de Ruiter radius. :returns: list of source measurements. """ logger.info("Extracting image: %s" % image_path) accessor = tkp.accessors.open(image_path) logger.debug("Detecting sources in image %s at detection threshold %s", image_path, extraction_params['detection_threshold']) data_image = sourcefinder_image_from_accessor(accessor, margin=extraction_params['margin'], radius=extraction_params['extraction_radius_pix'], back_size_x=extraction_params['back_size_x'], back_size_y=extraction_params['back_size_y']) logger.debug("Employing margin: %s extraction radius: %s deblend_nthresh: %s", extraction_params['margin'], extraction_params['extraction_radius_pix'], extraction_params['deblend_nthresh'] ) # "blind" extraction of sources results = data_image.extract( det=extraction_params['detection_threshold'], anl=extraction_params['analysis_threshold'], deblend_nthresh=extraction_params['deblend_nthresh'], force_beam=extraction_params['force_beam'] ) logger.info("Detected %d sources in image %s" % (len(results), image_path)) return [ r.serialize( extraction_params['ew_sys_err'], extraction_params['ns_sys_err'] ) for r in results ]
def setUp(self): """ NB the required image has been committed to the tkp/data subversion repository. (See tkp/data/unittests/tkp_lib for a full copy of all the unittest data). Source positions / background positions were simply picked out by eye in DS9 """ self.image = accessors.sourcefinder_image_from_accessor( accessors.open( os.path.join(DATAPATH, 'sourcefinder/NCP_sample_image_1.fits'))) self.assertListEqual(list(self.image.data.shape), [1024, 1024]) self.boxsize = BOX_IN_BEAMPIX * max(self.image.beam[0], self.image.beam[1]) self.bright_src_posn = (35.76726, 86.305771) #RA, DEC self.background_posn = (6.33731, 82.70002) #RA, DEC # #NB Peak of forced gaussian fit is simply plucked from a previous run; # so merely ensures *consistent*, rather than *correct*, results. self.known_fit_results = ( self.bright_src_posn[0], # RA, self.bright_src_posn[1], # Dec 13.457697411730384) # Peak
def testSFImageFromFITS(self): image = FitsImage(os.path.join(DATAPATH, 'sourcefinder/L15_12h_const/observed-all.fits'), beam=(54./3600, 54./3600, 0.)) sfimage = accessors.sourcefinder_image_from_accessor(image)
def testSFImageFromFITS(self): fits_file = os.path.join( DATAPATH, 'sourcefinder/L15_12h_const/observed-all.fits') image = FitsImage(fits_file, beam=(54. / 3600, 54. / 3600, 0.)) sfimage = accessors.sourcefinder_image_from_accessor(image)
def testmaps_array_type(self): self.image = accessors.sourcefinder_image_from_accessor( accessors.FitsImage(GRB120422A), margin=10) self.assertIsInstance(self.image.rmsmap, np.ma.MaskedArray) self.assertIsInstance(self.image.backmap, np.ma.MaskedArray)
def testMaskedBackgroundBlind(self): self.image = accessors.sourcefinder_image_from_accessor( accessors.open(fits_file), radius=1.0) result = self.image.extract(det=10.0, anl=3.0) self.assertFalse(result)
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()