Example #1
0
    def lum_func(self, Np, method="vmax", min_acceptable=1, eps=0.01,
                 max_iter=10, new=True):
        """
        Calculates the luminosity function of galaxies in Np bins.
        
        Can use either the EEP method or the Vmax method currently.
        
        INPUT PARAMETERS
        Np                 -- The number of bins to use
        method ["vmax"]    -- A string indicating which method to use (EEP or vmax)
        min_acceptable [1] -- Minimum acceptable galaxies in a bin
        eps [0.01]         -- for EEP, target convergence
        max_iter [10]      -- for EEP, maximum iterations
        new [True]         -- whether to force re-calculation even if previous results are available
        
        OUTPUT
        M   -- Array of absolute magnitude bin centres
        phi -- Array of luminosity function values
        """

        #We first look to find a pre-calculated function if wanted
        if not new:
            try:
                lfunc = np.genfromtxt(self._dirs["LuminosityFunction"] + method + ".dat")
                M = lfunc[:, 0]
                phi = lfunc[:, 1]
                return M, phi
            except IOError:
                pass

        if method == "EEP":
            phi, iteration = lf.EEP(absmag=self.survey_data['absmag'],
                                    apmag=self.survey_data['mag_r'],
                                    Np=Np,
                                    min_acceptable=min_acceptable,
                                    eps=eps,
                                    max_iter=max_iter,
                                    verbose=self.verbose)

            #Plot all iterations
            phi.plot(logy=True, marker='o')
            plt.show()
            plt.savefig(self._dirs["LuminosityFunction"] + "allEEP." + self.plottype)

            M = np.array(phi.index)
            phi = np.array(phi[iteration])

            #Plot the final answer
            plt.clf()
            plt.plot(M, phi)
            plt.xlabel("Absolute Magnitude")
            plt.ylabel("Unnormalised Luminosity Function")
            plt.yscale('log')
            plt.savefig(self._dirs["LuminosityFunction"] + "finalEEP." + self.plottype)


        elif method == "vmax":
            phi, M = lf.vmax_method(absmag=self.survey_data['absmag'],
                                 appmag=self.survey_data['mag_r'],
                                 c_dist=self.survey_data["c_dist"],
                                 redshift=self.survey_data['redshift'],
                                 bins=Np)

            #Plot it up
            plt.clf()
            plt.plot(M, phi)
            plt.yscale('log')
            plt.savefig(self._dirs["LuminosityFunction"] + "vmax." + self.plottype)

        #Save results to file
        output = np.vstack((M, phi)).T
        np.savetxt(self._dirs["LuminosityFunction"] + method + ".dat", output)

        return M, phi
Example #2
0
    oldpara[i][2] = -1.35
    if z[it[i]] < 1.3:
        oldpara[i][1] = 5.1e41 * np.power((1 + z[it[i]]), 3.1)
    if z[it[i]] >= 1.3:
        oldpara[i][1] = 6.8e42
    oldLF[i] = LF.LuminosityFunction(oldpara[i], lumHa[i])
grid = np.array([[0.0 for i in range(interpolatingSize)] for i in range(2)])
mpgrid = mpmath.arange(
    oldLF[oldLF < -5].max(), oldLF.max(), (oldLF.max() - oldLF[oldLF < -5].max()) / interpolatingSize
)
grid[1] = np.ogrid[0 : 6 : interpolatingSize * 1j]
newLF = np.array([[0.0 for i in range(interpolatingSize)] for i in range(interpolatingSize)])
for i in range(interpolatingSize):
    for j in range(interpolatingSize):
        newLF[i][j] = optimize.brentq(
            LF.rootfinding, 1e30, 1e50, args=(LF.luminosityParameterHa(grid[1][i]), mpgrid[j])
        )
z = (z + 1) * (5007.0 / 6563.0) - 1
newLum = interpolate.interpn([grid[1], mpgrid], newLF, np.array([z[it], oldLF]).T, bounds_error=False, fill_value=0.0)
for i in np.where(oldLF < -5)[0]:
    newLum[i] = optimize.brentq(LF.rootfinding, 1e30, 1e50, args=(LF.luminosityParameterHa(z[it][i]), oldLF[i]))
z = (z + 1) * (6563.0 / 5007.0) - 1

Ha[it] = newLum / (4 * np.pi * (dl ** 2))

print "Calibrating OIIIb line"
newLumO3b = [0.0 for x in range((it.shape[0]))]
grid = np.array([[0.0 for i in range(interpolatingSize)] for i in range(2)])
mpgrid = mpmath.linspace(oldLF[oldLF < -2.7].max(), oldLF.max(), interpolatingSize)
grid[1] = np.ogrid[0 : 6 : interpolatingSize * 1j]
newLF = np.array([[0.0 for i in range(interpolatingSize)] for i in range(interpolatingSize)])
Example #3
0
    def __init__(self,
                 filename,  #Filename of survey to be read in (absolute path)
                 radians=True,  #Whether the data is in radians to begin with.
                 cosmology=fidcosmo,  #A dictionary of cosmological parameters. Default is cosmolopy's default.
                 sample_name=None,  #Which sample to use (can always be 'all', otherwise will make one)
                 sample_cuts=None,  #Cuts to apply to the sample
                 clobber=None,  #Whether to automatically overwrite previous project folders
                 verbose=True):
        '''
        Imports the survey and creates a project directory and internal structure. 
        
        Instantiating the class requires a filename or directory in which is kept (solely) the survey information.
        This step is designed to be as seamless as possible. To this end, note the following:
        
        The default expected input is one ascii file with columns as data fields.
        
        If the survey information is in a completely different format, then the best thing to to do is reformat it 
        yourself and try again. And email me the problem and I'll try to update the class to deal with it.
        
        Once the information is read in, a project directory is automatically created. This serves to speed up future
        calculations and simplify the layout of results from your survey. The project folder is named
        <survey_filename>_project/ and included is an empty file "_PyLSSproject_" which serves to tell this class that
        the folder is in fact formatted in the default way. Within this folder the directory structure looks like:
        
        <my_survey>.[dat,csv, etc.]
        <my_survey>_project/
            _PyLSSproject_
            <my_survey>.h5   #Correctly formatted (HDF5) survey
            All/
                _PyLSSsample_
                <plot_varieties>/
                    plot.pdf
                    another_plot.pdf
                    ...
                ...
            <other_sample_name>/
                _PyLSSsample_
                <plot_varieties>/
                    plot.pdf
                    another_plot.pdf
                    ...
                ...
            
        A note on cosmologies: changing the fiducial cosmology will potentially change many of the calculations
        for a given survey (especially those relying on distances). This is reflected in the "Sample" directory
        level. That is, two identical samples of the main survey will be called different samples if their
        cosmology is specified differently.
        
        INPUT:
            radians [True] bool
            #Whether the data is in radians to begin with.

            cosmology [fidcosmo] dict  
            #A dictionary of cosmological parameters. Default is cosmolopy's default.

            sample_name [None] string
            #Which sample to use (can always be 'all', otherwise will make one)
                 
            sample_cuts [None] dict
            #Cuts to apply to the sample, takes the form
            sample_cuts = {"redshift":[min_z,max_z],"ra":[min_z,max_z]...}
   
            clobber [None] bool
            # Whether to overwrite project directory if appropriate
                
        '''
        #TODO: doesn't work if you choose NOT to overwrite if inputting .csv (line 640 survey_data doesn't exist!)

        #Save the cosmology (default fiducial cosmology of cosmolopy).
        self.cosmology = cosmology
        self.sample_name = sample_name  #Which sample to use (can always be 'all', otherwise will make one)
        self.sample_cuts = sample_cuts  #Cuts to apply to the sample
        self._clobber = clobber  #Whether to automatically overwrite previous project folders
        self.verbose = verbose
        self.plottype = 'png'

        #The columns wanted from the original file (some of these are not needed after read-in though)
        self._raw_columns = ["redshift", "ra", "dec", "mag_r", "mag_g", "id", 'extinction_r', "extinction_g"]

        #Determine whether the given filename is a project file or not - also check whether there is a project file for the survey.
        #filename is returned modified if a project file was found for the input filename (which wasn't a project file)
        is_project_file, self._project_dir, filename = self._determine_if_project(filename)


        #If it is NOT a project file, we need to import the ascii file first and make a project file
        if not is_project_file:
            # Import given file and do data reduction
            self._initial_file_import(filename, radians)
            # Create the project directory and write the project file to it.
            self._create_project_dir(filename)
            os.chdir(self._project_dir)
            self._write_project_file(filename)
            # Re-set the filename to the created project file
            filename = os.path.join(self._project_dir, os.path.split(filename)[1].split('.')[0] + '.h5')


        # Now we definitely have a HDF5 project file so we carry on.
        # Define the sample: get the sample name, the cuts and turn these into a query object
        self.sample_name, self.sample_cuts, query = self._define_sample(filename)
        print query
        #Create the sample directory (if it doesn't already exist)
        self._create_sample_dir(self.sample_name)
        #Save the sample directory path to the instance
        self._sample_dir = os.path.join(self._project_dir, self.sample_name)
        #Create results directories inside the sample
        self._make_result_dirs()

        #Write the sample cuts and cosmo to file for next time
        self._write_sample_cuts_cosmo(filename, self.sample_name, self.sample_cuts)

        #Import the project file
        self._project_file_import(filename, query)

        #Apply sample cuts
        self._cut_data(self.sample_cuts)

        # Now the base data is all in. We need to calculate extra properties.

        #Calculate the comoving distance to each particle.
        self.survey_data['c_dist'] = cd.comoving_distance(self.survey_data["redshift"], **self.cosmology)
        lum_dist = lf.lum_dist(self.survey_data['redshift'], self.survey_data['c_dist'])


        #Find the g-r colour and K-Correction to find Abs_Mag and luminosity.
        gr = self.survey_data["mag_g"] - self.survey_data["mag_r"] - self.survey_data["extinction_g"] - self.survey_data["extinction_r"]
        self.survey_data['kcorr'] = lf.k_correct(self.survey_data['redshift'], gr)

        self.survey_data["absmag"] = lf.absolute_mag(self.survey_data['mag_r'], lum_dist, self.survey_data['kcorr'], self.survey_data["extinction_r"])
        self.survey_data['lum'] = lf.absmag_to_lum(self.survey_data['absmag'])

        #Delete stupid entries
        self.clean()

        # Define some overall parameters of the sample.
        self.N = self.survey_data.shape[0]
        self.survey_name = os.path.split(self._project_dir)[1].replace('_Project', '')


        self._define_z_from_dist()