Пример #1
0
def test_roi():
    """ Testing ugali.observation.roi """
    import ugali.observation.roi
    roi = ugali.observation.roi.ROI(CONFIG, LON, LAT)

    # testing roi location
    np.testing.assert_equal(roi.lon, LON)
    np.testing.assert_equal(roi.lat, LAT)

    # testing the roi regions
    np.testing.assert_equal(len(roi.pixels),61327)
    np.testing.assert_equal(len(roi.pixels_interior),3828)
    np.testing.assert_equal(len(roi.pixels_annulus),57499)
    np.testing.assert_equal(len(roi.pixels_target),256)

    # test points
    np.testing.assert_equal(roi.inROI(POINTS[0],POINTS[1]),
                            np.array([True,True,True,True]))
    np.testing.assert_equal(roi.inAnnulus(POINTS[0],POINTS[1]),
                            np.array([False,False,False,True]))
    np.testing.assert_equal(roi.inInterior(POINTS[0],POINTS[1]),
                            np.array([True,True,True,False]))
    np.testing.assert_equal(roi.inTarget(POINTS[0],POINTS[1]),
                            np.array([True,True,False,False]))
    np.testing.assert_equal(roi.indexPixels(POINTS[0],POINTS[1],roi.pixels),
                            np.array([31034,30327,27527,54866]))
Пример #2
0
    def run(self, outfile=None, size=None):
        if size is None: size = self.config['simulate']['size']
        data = self.generate(size)

        dtype = [('kernel', 'S18'), ('ts', '>f4'), ('fit_kernel', 'S18'),
                 ('fit_ts', '>f4'), ('fit_mass', '>f4'),
                 ('fit_mass_err', '>f4'), ('fit_distance', '>f4'),
                 ('fit_distance_err', '>f4')]
        results = np.array(np.nan * np.ones(size), dtype=dtype)
        results = recfuncs.merge_arrays([data, results],
                                        flatten=True,
                                        asrecarray=False,
                                        usemask=False)
        self.results = results

        if outfile: self.write(outfile, results)

        for i, d in enumerate(data):
            params = dict(zip(data.dtype.names, d))
            lon, lat = params['lon'], params['lat']
            distance_modulus = params['distance_modulus']

            logger.info('\n(%i/%i); (lon, lat) = (%.2f, %.2f)' %
                        (i + 1, len(data), lon, lat))
            roi = ugali.analysis.loglike.createROI(self.config, lon, lat)
            mask = ugali.analysis.loglike.createMask(self.config, roi)
            isochrone = ugali.analysis.loglike.createIsochrone(self.config)
            kernel = ugali.analysis.loglike.createKernel(self.config, lon, lat)
            pix = roi.indexTarget(lon, lat)

            simulator = Simulator(self.config, roi)
            #catalog   = simulator.simulate(seed=self.seed, **params)
            catalog = simulator.simulate(**params)
            #print "Catalog annulus contains:",roi.inAnnulus(simulator.catalog.lon,simulator.catalog.lat).sum()
            logger.info("Simulated catalog annulus contains %i stars" %
                        roi.inAnnulus(catalog.lon, catalog.lat).sum())

            if len(catalog.lon) < 1000:
                logger.error(
                    "Simulation contains too few objects; skipping...")
                continue
            """
            like = ugali.analysis.loglike.LogLikelihood(self.config, roi, mask, catalog, isochrone, kernel)
            like.set_params(distance_modulus=params['distance_modulus'])
            like.sync_params()
            results[i]['ts'] = 2*like.fit_richness()[0]
            print 'TS=',results[i]['ts'] 
            
            like2 = ugali.analysis.loglike.LogLikelihood(self.config, roi, mask, simulator.catalog, isochrone, kernel)
            like2.set_params(distance_modulus=params['distance_modulus'])
            like2.sync_params()
            print 'TS=',2*like2.fit_richness()[0]
            """
            #return simulator,like,like2

            # Index of closest distance modulus
            grid = ugali.analysis.scan.GridSearch(self.config, roi, mask,
                                                  catalog, isochrone, kernel)

            self.catalog = catalog
            self.simulator = simulator
            self.grid = grid
            self.loglike = self.grid.loglike

            # ADW: Should allow fit_distance to float in order to model search procedure
            #fit_distance = float(distance_modulus)
            distance_idx = np.fabs(grid.distance_modulus_array -
                                   params['distance_modulus']).argmin()
            fit_distance = grid.distance_modulus_array[distance_idx]
            grid.search(coords=(lon, lat), distance_modulus=fit_distance)

            logger.info(str(self.loglike))

            mle = grid.mle()
            results[i]['kernel'] = simulator.kernel.name
            results[i]['fit_kernel'] = grid.loglike.kernel.name
            results[i][
                'ts'] = 2 * grid.log_likelihood_sparse_array[distance_idx][pix]
            results[i]['fit_ts'] = 2 * np.max(
                grid.log_likelihood_sparse_array[:, pix])
            results[i][
                'fit_mass'] = grid.stellar_mass_conversion * mle['richness']
            results[i]['fit_distance'] = fit_distance  #mle['distance_modulus']

            err = grid.err()
            richness_err = (err['richness'][1] - err['richness'][0]) / 2.
            results[i][
                'fit_mass_err'] = grid.stellar_mass_conversion * richness_err

            distance_modulus_err = (err['distance_modulus'][1] -
                                    err['distance_modulus'][0]) / 2.
            results[i]['fit_distance_err'] = distance_modulus_err

            for d in dtype:
                logger.info('\t%s: %s' % (d[0], results[i][d[0]]))

            if i % self.config['simulate']['save'] == 0 and outfile:
                self.write(outfile, results)

        if outfile: self.write(outfile, results)

        return results
Пример #3
0
    def run(self, outfile=None, size=None):
        if size is None: size = self.config['simulate']['size']
        data = self.generate(size)
        
        dtype=[('kernel','S18'),('ts','>f4'),('fit_kernel','S18'),('fit_ts','>f4'),
               ('fit_mass','>f4'),('fit_mass_err','>f4'),
               ('fit_distance','>f4'),('fit_distance_err','>f4')]
        results = np.array(np.nan*np.ones(size),dtype=dtype)
        results = recfuncs.merge_arrays([data,results],flatten=True,asrecarray=False,usemask=False)
        self.results = results

        if outfile: self.write(outfile,results)

        for i,d in enumerate(data): 
            params = dict(zip(data.dtype.names,d))
            lon,lat = params['lon'],params['lat']
            distance_modulus = params['distance_modulus']

            logger.info('\n(%i/%i); (lon, lat) = (%.2f, %.2f)'%(i+1,len(data),lon,lat))
            roi = ugali.analysis.loglike.createROI(self.config,lon,lat)
            mask = ugali.analysis.loglike.createMask(self.config,roi)
            isochrone = ugali.analysis.loglike.createIsochrone(self.config)
            kernel = ugali.analysis.loglike.createKernel(self.config,lon,lat)
            pix = roi.indexTarget(lon,lat)

            simulator = Simulator(self.config,roi)
            #catalog   = simulator.simulate(seed=self.seed, **params)
            catalog   = simulator.simulate(**params)
            #print "Catalog annulus contains:",roi.inAnnulus(simulator.catalog.lon,simulator.catalog.lat).sum()
            logger.info("Simulated catalog annulus contains %i stars"%roi.inAnnulus(catalog.lon,catalog.lat).sum())

            if len(catalog.lon) < 1000:
                logger.error("Simulation contains too few objects; skipping...")
                continue

            """
            like = ugali.analysis.loglike.LogLikelihood(self.config, roi, mask, catalog, isochrone, kernel)
            like.set_params(distance_modulus=params['distance_modulus'])
            like.sync_params()
            results[i]['ts'] = 2*like.fit_richness()[0]
            print 'TS=',results[i]['ts'] 
            
            like2 = ugali.analysis.loglike.LogLikelihood(self.config, roi, mask, simulator.catalog, isochrone, kernel)
            like2.set_params(distance_modulus=params['distance_modulus'])
            like2.sync_params()
            print 'TS=',2*like2.fit_richness()[0]
            """
            #return simulator,like,like2

            # Index of closest distance modulus
            grid = ugali.analysis.scan.GridSearch(self.config,roi,mask,catalog,isochrone,kernel)

            self.catalog = catalog
            self.simulator = simulator
            self.grid = grid
            self.loglike = self.grid.loglike

            # ADW: Should allow fit_distance to float in order to model search procedure
            #fit_distance = float(distance_modulus)
            distance_idx = np.fabs(grid.distance_modulus_array-params['distance_modulus']).argmin()
            fit_distance = grid.distance_modulus_array[distance_idx]
            grid.search(coords=(lon,lat),distance_modulus=fit_distance)

            logger.info(str(self.loglike))

            mle = grid.mle()
            results[i]['kernel'] = simulator.kernel.name
            results[i]['fit_kernel'] = grid.loglike.kernel.name
            results[i]['ts'] = 2*grid.log_likelihood_sparse_array[distance_idx][pix]
            results[i]['fit_ts'] = 2*np.max(grid.log_likelihood_sparse_array[:,pix])
            results[i]['fit_mass'] = grid.stellar_mass_conversion*mle['richness']
            results[i]['fit_distance'] = fit_distance #mle['distance_modulus']

            err = grid.err()
            richness_err = (err['richness'][1]-err['richness'][0])/2.
            results[i]['fit_mass_err'] = grid.stellar_mass_conversion*richness_err

            distance_modulus_err = (err['distance_modulus'][1]-err['distance_modulus'][0])/2.
            results[i]['fit_distance_err'] = distance_modulus_err

            for d in dtype:
                logger.info('\t%s: %s'%(d[0], results[i][d[0]]))

            if i%self.config['simulate']['save']==0 and outfile: 
                self.write(outfile,results)

        if outfile: self.write(outfile,results)
            
        return results