Beispiel #1
0
    def get_1d_halo_matter(self, kind='small_r', average='box'):
        """
        Return the 1D real-space halo-matter correlation functions, 
        with errors estimated from the scatter in the 10 realizations
        """
        valid_kinds = ['small_r', 'fft']
        if kind not in valid_kinds:
            raise ValueError("valid choices for `kind` are: %s" %str(valid_kinds))
            
        name = '_1d_halo_matter_%s' %kind
        if average is not None:
            name += '_'+average

        try:
            return getattr(self, name)
        except AttributeError:

            basename = 'corr_1d_hm{mass}_runPB_PB{box}_{a}_%s.dat' %kind
            d = os.path.join(self.root, 'halo-matter/config/real')

            loader = io.load_correlation
            coords = [self.a, self.mass, self.box]; dims = ['a', 'mass', 'box']
            corr = SpectraSet.from_files(loader, d, basename, coords, dims, args=('1d',))
        
            # reindex and add the errors
            corr = self.reindex(corr, 'r_cen', self.dr, weights='N')
        
            # compute errors
            errs = {}
            for key in corr.ndindex(dims=['a', 'mass']):
                xi = corr.sel(**key)
                
                data = []
                for x in xi:
                    x = x.get()
                    data.append(x['corr'])
                
                subkey = (key['a'], key['mass'])
                errs[subkey] = np.diag(np.cov(np.asarray(data).T))**0.5
                if average is not None:
                    errs[subkey] /= (len(self.box))**0.5
            
            # average?
            if average is not None:
                corr = corr.average(axis=average)
        
            # add the errors
            for key in corr.ndindex():
                xi = corr.loc[key].get()
                xi['error'] = errs[(key['a'], key['mass'])]

            setattr(self, name, corr)
            return corr
    def get_subbox_poles(self, which, average=False, subtract_shot_noise=True):
        """
        Return the cutsky galaxy multipoles in redshift space, measured
        for the subboxes
        """ 
        name = '_my_poles_subboxes_%s_Mpch' %(which)
        if average: name += '_mean'
        
        try:
            return getattr(self, name)         
        except AttributeError:
        
            # form the filename and load the data
            d = self.root
            basename = 'poles_my_cutskyN1_subbox{box:d}_%s_Mpch_unscaled_no_fkp_dk005.dat' %which

            # read in the data
            loader = io.load_power
            mapcols = {'power_0.real':'mono', 'power_2.real':'quad', 'power_4.real':'hexadec'}
            usecols = ['k', 'mono', 'quad', 'hexadec', 'modes']
            kwargs = {'usecols':usecols, 'mapcols':mapcols}
            
            if which == '300':
                boxes = list(range(94))
            else:
                boxes = list(range(24))
            poles = SpectraSet.from_files(loader, d, basename, [boxes], ['box'], args=('1d',), kwargs=kwargs, ignore_missing=True)

            # remove null
            valid_boxes = []
            for key, p in poles.nditer():
                if not p.isnull():
                    valid_boxes.append(key['box'])
            poles = poles.sel(box=valid_boxes)
        
            # reindex
            poles = self.reindex(poles, 'k_cen', self.dk, weights='modes')
            
            # unstack the poles
            ells = [('mono',0), ('quad', 2), ('hexadec', 4)]
            poles = tools.unstack_multipoles(poles, ells, 'power')
            
            # compute errors
            errs = {}
            for ell in poles['ell'].values:
                data = []
                for box in poles['box'].values:
                    p = poles.sel(box=box, ell=ell).get()
                    data.append(p['power'])

                errs[ell] = np.diag(np.cov(np.asarray(data).T))**0.5
                if average:
                    errs[ell] /= (len(boxes))**0.5
            
            if subtract_shot_noise:
                for key in poles.ndindex():
                    if key['ell'] == 0:
                        p = poles.loc[key].get()
                        p['power'] = p['power'] - p.attrs['shot_noise']
            
            # average?
            if average:
                poles = poles.average(axis='box')
                
            # add the errors
            for key in poles.ndindex():
                p = poles.loc[key].get()
                p['error'] = errs[key['ell']]
        
            setattr(self, name, poles)
            return poles
    def get_my_poles(self, space='redshift', scaled=False, average=False, tag="", 
                        subtract_shot_noise=True):
        """
        Return the cutsky galaxy multipoles in redshift space, measured
        for my 84 box realizations
        """ 
        if space not in ['real', 'redshift']:
            raise ValueError("`space` should be 'real' or 'redshift'")
        
        scaled_tag = 'scaled' if scaled else 'unscaled'
        name = '_my_poles_%s_%s' %(space, scaled_tag)
        if average: name += '_mean'
    
        if tag: 
            tag = '_'+tag
            name += tag
        
        try:
            return getattr(self, name)         
        except AttributeError:
        
            # form the filename and load the data
            d = os.path.join(self.root, 'nbodykit/poles')
            if space == 'redshift':
                basename = 'poles_my_cutskyN{box:d}_%s_no_fkp_dk005%s.dat' %(scaled_tag, tag)
            else:
                basename = 'poles_my_cutskyN{box:d}_real_%s_no_fkp_dk005%s.dat' %(scaled_tag, tag)

            # read in the data
            loader = io.load_power
            mapcols = {'power_0.real':'mono', 'power_2.real':'quad', 'power_4.real':'hexadec'}
            usecols = ['k', 'mono', 'quad', 'hexadec', 'modes']
            kwargs = {'usecols':usecols, 'mapcols':mapcols}
            poles = SpectraSet.from_files(loader, d, basename, [self.boxes], ['box'], args=('1d',), kwargs=kwargs, ignore_missing=True)

            # remove null
            valid_boxes = []
            for key, p in poles.nditer():
                if not p.isnull():
                    valid_boxes.append(key['box'])
            poles = poles.sel(box=valid_boxes)
        
            # reindex
            poles = self.reindex(poles, 'k_cen', self.dk, weights='modes')
            
            # unstack the poles
            ells = [('mono',0), ('quad', 2), ('hexadec', 4)]
            poles = tools.unstack_multipoles(poles, ells, 'power')
            
            # compute errors
            errs = {}
            for ell in poles['ell'].values:
                data = []
                for box in poles['box'].values:
                    p = poles.sel(box=box, ell=ell).get()
                    data.append(p['power'])

                errs[ell] = np.diag(np.cov(np.asarray(data).T))**0.5
                if average:
                    errs[ell] /= (len(self.boxes))**0.5
            
            if subtract_shot_noise:
                for key in poles.ndindex():
                    if key['ell'] == 0:
                        p = poles.loc[key].get()
                        p['power'] = p['power'] - p.attrs['shot_noise']
            
            # average?
            if average:
                poles = poles.average(axis='box')
                
            # add the errors
            for key in poles.ndindex():
                p = poles.loc[key].get()
                p['error'] = errs[key['ell']]
        
            setattr(self, name, poles)
            return poles
Beispiel #4
0
 def _errors_from_realizations(self, a, mass, moments, sel_mu=None):
     """
     Internal function to compute the momentum moment errors as the 
     diagonal elements of the covariance matrix, which is constructed using 
     the 30 different realizations of each spectrum 
     (3 lines-of-sight + 10 realizations)
     
     Parameters
     ----------
     a : str
         the string specifying the scale factor
     mass : int
         the integer specifying the mass bin
     moments : list of tuples
         list of tuples specifying the velocity moments to load
     """
     from glob import glob
     from collections import defaultdict
 
     if isinstance(sel_mu, int): sel_mu = [sel_mu]
     if sel_mu is None: sel_mu = self.mu
     Nmu = len(self.mu)
     
     # the file pattern
     d = "/Volumes/Frodo/RSDResearch/RunPB/Results/halo/real/momentum/poles"
     data = defaultdict(list)
     
     for i, (ell, ell_prime) in enumerate(moments):
         
         args = (ell, ell_prime, mass, a)
         basename = 'poles_P%d%d_hh%d_runPB_*_%s_*los.dat' %args
         pattern = os.path.join(d, basename)
     
         # the matching files
         files = glob(pattern)
         kw = {'sum_only':['modes'], 'force_index_match':True}
     
         for j, f in enumerate(files):
         
             loaded = io.load_momentum(f, ell, ell_prime, **kw)
             if self.dk is not None:
                 for ii, l in enumerate(loaded):
                     if isinstance(l, DataSet):
                         loaded[ii] = l.reindex('k_cen', self.dk, weights='modes')
         
             for k, mu in enumerate(self.mu):
                 if mu not in sel_mu: continue
                 if isinstance(loaded[k], DataSet):
                     if i == 0:
                         data[mu].append(loaded[k]['power'])
                     else:
                         data[mu][j] += loaded[k]['power']
             
     toret = [None]*Nmu
     for i, mu in enumerate(self.mu):
         
         if len(data[mu]):
             cov = np.cov(np.nan_to_num(data[mu]), rowvar=False)
             errs = cov.diagonal()**0.5
     
             # divide by root N if mean
             if 'mean' in self.tag:
                 errs /= len(files)**0.5
             toret[i] = errs
     
     return toret