Exemplo n.º 1
0
    def get_poles(self, scaled=True):
        """
        Return a dictionary holding the configuration space galaxy spectrum
        multipoles in redshift space, as measured by nbodykit
        """
        tag = '_scaled' if scaled else '_unscaled'
        name = '_poles' + tag
        try:
            return getattr(self, name)
        except AttributeError:

            basename = 'poles_challenge_box{box}%s.dat' %tag
            d = os.path.join(self.root, 'corr_poles')
                        
            loader = io.load_correlation
            kwargs = {'sum_only':['RR', 'N'], 'force_index_match':True}
            poles = SpectraSet.from_files(loader, d, basename, [self.boxes], ['box'], args=('1d',), kwargs=kwargs)
            
            # reindex and add the errors
            poles = self.reindex(poles, 'r_cen', self.dr, weights='N')

            # now convert
            ells = [('corr_0',0), ('corr_2', 2), ('corr_4', 4)]
            poles = tools.unstack_multipoles(poles, ells, 'corr')
            setattr(self, name, poles)
            return poles
Exemplo n.º 2
0
    def get_shifted_poles(self, name, tag="", average=None, subtract_shot_noise=True):
        """
        Return the N-series multipoles in redshift space, which haven been shifted in
        space (and computed with Bianchi algorithm)
        """
        if name not in ["boss_like", "plane_parallel"]:
            raise ValueError("please choose a name from 'boss_like' or 'plane_parallel'")

        tag_ = "_" + tag if tag else ""
        if tag:
            name += tag_

        if average is not None:
            if isinstance(average, str):
                average = [average]
        else:
            average = []

        name_ = name
        if len(average):
            name_ += "_" + "_".join(average)

        try:
            return getattr(self, "shifted_poles_%s" % name_)
        except AttributeError:

            # load the data from file
            basename = "poles_challenge_boxN{box}_shifted_unscaled_shift{shift}_%s.dat" % name
            coords = [self.boxes, [0, 1, 2]]
            d = os.path.join(self.root, "poles")

            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, coords, ["box", "shift"], args=("1d",), kwargs=kwargs)

            # reindex and add the errors
            poles = self.reindex(poles, "k_cen", self.dk, weights="modes")

            # now convert
            ells = [("mono", 0), ("quad", 2), ("hexadec", 4)]
            poles = tools.unstack_multipoles(poles, ells, "power")

            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 len(average):
                poles = poles.average(axis=average, weights="modes")

            setattr(self, name, poles)
            return poles
Exemplo n.º 3
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
Exemplo n.º 4
0
 def get_poles(self, average=None):
     """
     Return the cutsky galaxy multipoles in redshift space
     """
     if average is not None:
         if isinstance(average, str):
             average = [average]
     else:
         average = []
     
     name = '_poles'
     if len(average):
         name += '_' + '_'.join(average)
         
     try:
         return getattr(self, name)
     except AttributeError:
     
         # form the filename and load the data
         basename = 'bianchips_qpmdr12_TSC_{box:04d}.dat'
         
         # read in the data
         loader = io.read_cutsky_power_poles
         kwargs = {'skiprows' : 31, 'sum_only':['modes'], 'force_index_match':True}
         poles = SpectraSet.from_files(loader, self.root, basename, [self.boxes], ['box'], kwargs=kwargs)
     
         # 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')
     
         if len(average):
             poles = poles.average(axis=average, weights='modes')
     
         setattr(self, name, poles)
         return poles
Exemplo n.º 5
0
    def get_florian_poles(self, scaled=True, average=False):
        """
        Return the cutsky galaxy multipoles in redshift space, as
        measured by Florian's code
        """
        scaled_tag = 'scaled' if scaled else 'unscaled'
        name = '_poles_florian_' + scaled_tag
        if average: name += '_mean'
        
        try:
            return getattr(self, name)         
        except AttributeError:
        
            # form the filename and load the data
            d = os.path.join(self.root, 'data', scaled_tag)
            if scaled:
                basename = 'bianchips_cutsky_TSC_0.7_{box:d}.dat'
            else:
                basename = 'bianchips_Ncutsky_{box:02d}.dat'

            # read in the data
            loader = io.read_cutsky_power_poles
            kwargs = {'skiprows' : 31, 'sum_only':['modes'], 'force_index_match':True}
            poles = SpectraSet.from_files(loader, d, basename, [self.boxes], ['box'], kwargs=kwargs)
        
            # 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')
            
            if average:
                poles = poles.average(axis='box', weights='modes')
        
            setattr(self, name, poles)
            return poles
Exemplo n.º 6
0
    def get_poles(self, spacing="dk005", scaled=False, Nmu=100):
        """
        Return the total galaxy spectrum multipoles in redshift space
        """
        _spacing = spacing
        tag = "unscaled" if not scaled else "scaled"
        if spacing:
            spacing = "_" + spacing
        name = "_poles%s_%s" % (spacing, tag)
        try:
            return getattr(self, name)
        except AttributeError:

            # load the data from file
            basename = "poles_challenge_box{box}_%s%s_Nmu%d.dat" % (tag, spacing, Nmu)
            coords = [self.boxes]
            d = os.path.join(self.root, "poles")

            loader = io.load_power
            columns = ["k", "mono", "quad", "hexadec", "modes"]
            kwargs = {"sum_only": ["modes"], "force_index_match": True, "columns": columns}
            poles = SpectraSet.from_files(loader, d, basename, coords, ["box"], args=("1d",), kwargs=kwargs)

            # reindex and add the errors
            poles = self.reindex(poles, "k_cen", self.dk, weights="modes")

            # now convert
            ells = [("mono", 0), ("quad", 2), ("hexadec", 4)]
            toret = tools.unstack_multipoles(poles, ells, "power")

            # add the errors
            pkmu = self.get_Pgal(scaled=scaled, spacing=_spacing, Nmu=Nmu)
            toret.add_power_pole_errors(pkmu)

            setattr(self, name, toret)
            return toret
Exemplo n.º 7
0
    def get_Pgal(self, spacing="dk005", scaled=False, Nmu=5):
        """
        Return the total galaxy spectrum in redshift space
        
        Parameters
        ----------
        spacing : str, optional
            the tag used to identify results with different k spacing, i.e. `dk005`
        scaled : bool, optional
            return the results that have/haven't been scaled by AP factors
        Nmu : int, optional
            the number of mu bins
        """
        tag = "unscaled" if not scaled else "scaled"
        if spacing:
            spacing = "_" + spacing
        name = "_Pgal%s_%s_Nmu%d" % (spacing, tag, Nmu)
        try:
            return getattr(self, name)
        except AttributeError:

            # load the data from file
            basename = "pkmu_challenge_box{box}_%s%s_Nmu%d.dat" % (tag, spacing, Nmu)
            coords = [self.boxes]
            d = os.path.join(self.root, "power")

            loader = io.load_power
            kwargs = {"sum_only": ["modes"], "force_index_match": True}
            Pgal = SpectraSet.from_files(loader, d, basename, coords, ["box"], args=("2d",), kwargs=kwargs)

            # reindex and add the errors
            Pgal = self.reindex(Pgal, "k_cen", self.dk, weights="modes")
            Pgal.add_power_errors()

            setattr(self, name, Pgal)
            return Pgal
Exemplo n.º 8
0
    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
Exemplo n.º 9
0
    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
Exemplo n.º 10
0
    def get_poles(
        self,
        space="redshift",
        subtract_shot_noise=True,
        spacing="dk005",
        Nmu=100,
        scaled=False,
        average=None,
        tag="",
        include_tetrahex=False,
    ):
        """
        Return the N-series multipoles in redshift space
        """
        if space not in ["redshift", "real"]:
            raise ValueError("`space` must be real' or 'redshift'")

        if average is not None:
            if isinstance(average, str):
                average = [average]
        else:
            average = []

        # determine the tag
        scaled_tag = "scaled" if scaled else "unscaled"
        _spacing = spacing
        if spacing:
            spacing = "_" + spacing
        name = "_nseries_poles_%s_%s%s" % (space, scaled_tag, spacing)
        if len(average):
            name += "_" + "_".join(average)

        tag_ = "_" + tag if tag else ""
        if tag:
            name += tag_

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

            # load the data from file
            if space == "redshift":
                basename = "poles_challenge_boxN{box}_%s%s_Nmu%d_{los}los%s.dat" % (scaled_tag, spacing, Nmu, tag_)
                coords = [self.los, self.boxes]
                dims = ["los", "box"]
            else:
                basename = "poles_challenge_boxN{box}_real_%s%s_Nmu100%s.dat" % (scaled_tag, spacing, tag_)
                coords = [self.boxes]
                dims = ["box"]
            d = os.path.join(self.root, "poles")

            loader = io.load_power
            mapcols = {"power_0.real": "mono", "power_2.real": "quad", "power_4.real": "hexadec"}
            usecols = ["k", "mono", "quad", "hexadec", "modes"]
            if include_tetrahex:
                mapcols["power_6.real"] = "tetrahex"
                usecols.append("tetrahex")
            kwargs = {"usecols": usecols, "mapcols": mapcols}
            poles = SpectraSet.from_files(loader, d, basename, coords, dims, args=("1d",), kwargs=kwargs)

            # reindex and add the errors
            poles = self.reindex(poles, "k_cen", self.dk, weights="modes")

            # now convert
            ells = [("mono", 0), ("quad", 2), ("hexadec", 4)]
            if include_tetrahex:
                ells.append(("tetrahex", 6))
            poles = tools.unstack_multipoles(poles, ells, "power")

            if subtract_shot_noise:
                for key in poles.ndindex():
                    if key["ell"] == 0:
                        p = poles.loc[key].get()
                        p["power"] = p["power"] - p.attrs["volume"] / p.attrs["N1"]

            # average?
            if len(average):
                poles = poles.average(axis=average, weights="modes")

            # add the errors
            pkmu = self.get_Pgal(space=space, spacing=_spacing, Nmu=Nmu, average=average, scaled=scaled, tag=tag)
            poles.add_power_pole_errors(pkmu)

            setattr(self, name, poles)
            return poles
Exemplo n.º 11
0
    def get_Pgal(
        self, space="redshift", spacing="dk005", subtract_shot_noise=False, Nmu=100, scaled=False, average=None, tag=""
    ):
        """
        Return the total galaxy spectrum in redshift space
        """
        if space not in ["redshift", "real"]:
            raise ValueError("`space` must be real' or 'redshift'")

        if average is not None:
            if isinstance(average, str):
                average = [average]
        else:
            average = []

        # determine the tag
        scaled_tag = "scaled" if scaled else "unscaled"
        _spacing = spacing
        if spacing:
            spacing = "_" + spacing
        name = "_Pgal%s_%s_Nmu%d_%s" % (spacing, space, Nmu, scaled_tag)
        if len(average):
            name += "_" + "_".join(average)

        if tag:
            tag = "_" + tag
            name += tag

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

            # load the data from file
            if space == "redshift":
                basename = "pkmu_challenge_boxN{box}_%s%s_Nmu%d_{los}los%s.dat" % (scaled_tag, spacing, Nmu, tag)
                coords = [self.los, self.boxes]
                dims = ["los", "box"]
            else:
                basename = "pkmu_challenge_boxN{box}_real_%s%s_Nmu100%s.dat" % (scaled_tag, spacing, tag)
                coords = [self.boxes]
                dims = ["box"]
            d = os.path.join(self.root, "power")

            loader = io.load_power
            kwargs = {"sum_only": ["modes"], "force_index_match": True}
            Pgal = SpectraSet.from_files(loader, d, basename, coords, dims, args=("2d",), kwargs=kwargs)

            # take the real part
            for key in Pgal.ndindex():
                pkmu = (Pgal.loc[key]).get()
                pkmu["power"] = pkmu["power"].real

            if subtract_shot_noise:
                for key in Pgal.ndindex():
                    p = Pgal.loc[key].get()
                    p["power"] = p["power"] - p.attrs["volume"] / p.attrs["N1"]

            if len(average):
                Pgal = Pgal.average(axis=average, weights="modes")

            if isinstance(Pgal, SpectraSet):
                # reindex and add the errors
                Pgal = self.reindex(Pgal, "k_cen", self.dk, weights="modes")
                Pgal.add_power_errors()
            else:

                # reindex
                if self.dk is not None:
                    Pgal = Pgal.reindex("k_cen", self.dk, weights="modes", force=True)

                # add errors
                Pgal["error"] = (2.0 / Pgal["modes"]) ** 0.5 * Pgal["power"]

            setattr(self, name, Pgal)
            return Pgal
Exemplo n.º 12
0
    def _get_moment(self, moments, los="", save_errors=False, name=None, sel_mu=None, ignore_cache=False):
        """
        Internal function to return any (ell, ell_prime) moment
        
        Parameters
        ----------
        moments : list of tuples
            list of tuples specifying the velocity moments to load
        los : str, {'x', 'y', 'z'} optional
            the string specifying the line-of-sight
        save_errors : bool, optional
            whether to pickle the errors for future loading
        name : str, optional
            use this string as the attribute name
        sel_mu : int, list of int, optional
            select specific only mu values
        ignore_cache : bool, optional
            if `True`, ignore any saved errors
        """
        if name is None:
            name = "_plus_".join("P%d%d" %(ell, ell_prime) for (ell, ell_prime) in moments)
        name_ = "_" + name
        if los: name_ += '_%slos' %los
        try:
            return getattr(self, name_)
        except AttributeError:

            coords = [self.a, self.mass]
            dims = ['a', 'mass']
            add_errors = True
            
            # load
            loader = io.load_momentum
            
            # loop over all moments
            toret = []
            for (ell, ell_prime) in moments:
                
                columns = None
                if ell == 0 and ell_prime == 0:
                    d = os.path.join(self.root, 'halo/real/density/power') 
                    tag = self.tag if 'mean' not in self.tag else '10mean'
                    basename = 'pk_hh{mass}_runPB_%s_{a}.dat' %tag
                    columns = ['k', 'power', 'modes']
                else:
                    d = os.path.join(self.root, 'halo/real/momentum/poles')
                    if los:
                        args = (ell, ell_prime, self.tag, los)
                        basename = 'poles_P%d%d_hh{mass}_runPB_%s_{a}_%slos.dat' %args
                    else:
                        args = (ell, ell_prime, self.tag)
                        basename = 'poles_P%d%d_hh{mass}_runPB_%s_{a}.dat' %args
                
                kwargs = {'sum_only':['modes'], 'force_index_match':True}
                if columns is not None: kwargs['columns'] = columns
                poles = SpectraSet.from_files(loader, d, basename, coords, dims, args=(ell,ell_prime,), kwargs=kwargs)
            
                # add the mu dimension
                data = np.asarray(poles.values.tolist())
                poles = SpectraSet(data, coords=coords+[self.mu], dims=dims+['mu'])
                
                # reindex 
                poles = self.reindex(poles, 'k_cen', self.dk, weights='modes')
                                    
                # choose specific mu
                if sel_mu is not None:
                    poles = poles.sel(mu=sel_mu)
                
                # add errors
                if ell == 0 and ell_prime == 0:
                    poles.add_power_errors()
                    add_errors = False
                    
                toret.append(poles)
            
            if len(moments) != 0:
                for i in range(1, len(moments)):
                    for key, P in toret[i].nditer():
                        
                        subkey = {k:key[k] for k in key if k in toret[0].dims}
                        P0 = toret[0].sel(**subkey)
                        if P0.isnull():
                            continue
                        else:
                            P0 = P0.get()
                        P0['power'] += P.get()['power']
            toret = toret[0]
            
            if add_errors:
                kw = {'sel_mu':sel_mu, 'save_errors':save_errors, 'name':name, 'ignore_cache':ignore_cache}
                self._add_errors(toret, moments, **kw)
                                
            setattr(self, name_, toret)
            return toret