Example #1
0
    def model(self, redshift, source='li-drory-march', **kwargs): 
        ''' Plot the model (Analytic) SMF at given redshift 
        '''
        if 'label' in kwargs: 
            smf_label = kwargs['label']
            kwargs.pop('label', None)
        else: 
            smf_label = 'Analytic'
    
        if 'line_color' in kwargs: 
            line_color = kwargs['line_color']
            kwargs.pop('line_color', None)
        else: 
            line_color = 'black'

        if 'line_style' in kwargs:
            line_style = kwargs['line_style'] 
            kwargs.pop('line_style', None)
        else: 
            line_style = '--'

        if 'lw' in kwargs: 
            line_width = kwargs['lw'] 
            kwargs.pop('lw', None)
        else:
            line_width = 3
        smf = SMF()
        mass, phi = smf.analytic(redshift, source=source) 
        print phi[np.where((mass > 7.0) & (mass < 7.5))]
        
        self.sub.plot(mass, phi, 
                c=line_color, ls=line_style, lw=line_width,
                label=smf_label
                )
def SubhaloSMF(type, scatter=0.0, source='li-drory-march', 
        nsnap_ancestor=20 ): 
    ''' Test the Subhalos/CentralSubhalos imported from TreePM by 
    comparing their measured SMFs to the analytic SMFs 

    Parameters
    ----------
    type : string 
        String that specifies whether we're interested in CentralSubhalos 
        or all Subhalos.
    scatter : float
        Float that specifies the scatter in the SMHM relation, which 
        affects the SHAM masses
    source : string
        String that specifies which SMF is used for SHAM
    '''
    prettyplot()
    pretty_colors = prettycolors()

    fig = plt.figure(figsize=(10,10))
    sub = fig.add_subplot(111)
    for i_snap in range(1, nsnap_ancestor+1):
        smf = SMF()
        if type == 'all': 
            subh = Subhalos()
        elif type == 'central': 
            subh = CentralSubhalos()
        else: 
            raise ValueError

        subh.Read(i_snap, scatter=scatter, source=source, nsnap_ancestor=nsnap_ancestor)
        
        subh_mass, subh_phi = smf.Obj(subh, dlogm=0.1, LF=False)
        sub.plot(subh_mass, subh_phi, lw=4, c=pretty_colors[i_snap % 19], alpha=0.5) 

        analytic_mass, analytic_phi = smf.analytic(get_z_nsnap(i_snap), source=source) 
        sub.plot(analytic_mass, analytic_phi, lw=4, ls='--', c=pretty_colors[i_snap % 19], 
                label=r"$ z = "+str(round(get_z_nsnap(i_snap),2))+"$") 

    sub.set_yscale('log')
    sub.set_ylim([10**-5, 10**-1])
    sub.set_xlim([6.0, 12.0])
    #sub.legend(loc='upper right')
    if type == 'all': 
        subhalo_str = 'subhalo' 
    elif type == 'central': 
        subhalo_str = 'central_subhalo' 
    else: 
        raise ValueError
    plt.show()
    fig_file = ''.join(['figure/test/'
        'SubhaloSMF', 
        '.', subhalo_str, 
        '.scatter', str(round(scatter,2)), 
        '.ancestor', str(nsnap_ancestor),
        '.', source, 
        '.png']) 
    fig.savefig(fig_file, bbox_inches='tight')
    plt.close()
Example #3
0
def LineageFinalDescendantSMF(nsnap_ancestor,
                              subhalo_prop={
                                  'scatter': 0.0,
                                  'source': 'li-march'
                              },
                              sfr_prop={
                                  'fq': {
                                      'name': 'wetzelsmooth'
                                  },
                                  'sfr': {
                                      'name': 'average'
                                  }
                              }):
    ''' Plot SMF of final descendant. Also mark the composition of the final SMF from Subhalos 
    that gain stellar mass at different snapshots in the middle of the simulation. 
    '''
    prettyplot()
    pretty_colors = prettycolors()

    bloodline = Lineage(nsnap_ancestor=nsnap_ancestor,
                        subhalo_prop=subhalo_prop)
    bloodline.Read([1], sfr_prop=sfr_prop)

    final_desc = bloodline.descendant_snapshot1

    mf = SMF()

    smf_plot = plots.PlotSMF()
    smf_plot.cgpop(final_desc, line_color='k')

    for isnap in range(2, nsnap_ancestor + 1)[::-1]:
        started_here = np.where(final_desc.nsnap_genesis == isnap)
        mass, phi = mf._smf(final_desc.mass[started_here])

        try:
            phi_tot += phi
            smf_plot.sub.fill_between(mass,
                                      phi_tot - phi,
                                      phi_tot,
                                      color=pretty_colors[isnap],
                                      label='Nsnap=' + str(isnap))
        except UnboundLocalError:
            phi_tot = phi
            smf_plot.sub.fill_between(mass,
                                      np.zeros(len(phi)),
                                      phi,
                                      color=pretty_colors[isnap],
                                      label='Nsnap=' + str(isnap))

    smf_plot.set_axes()
    fig_file = ''.join([
        'figure/test/', 'LineageFinalDescendantSMF', '.ancestor',
        str(nsnap_ancestor),
        bloodline._file_spec(subhalo_prop=subhalo_prop, sfr_prop=sfr_prop),
        '.png'
    ])
    smf_plot.save_fig(fig_file)

    return None
Example #4
0
 def SMF(self, **smf_kwargs):
     ''' 
     Calculate the Stellar Mass Function of Cenque Object
     '''
     if self.mass is None:
         raise ValueError
     smf = SMF()
     return smf.Obj(self, **smf_kwargs)
def SubhaloLF(scatter=0.0, source='cool_ages', nsnap_ancestor=20):
    ''' Test the Subhalos/CentralSubhalos imported from TreePM by 
    comparing their measured SMFs to the analytic SMFs 

    Parameters
    ----------
    scatter : float
        Float that specifies the scatter in the SMHM relation, which 
        affects the SHAM masses
    source : string
        String that specifies which SMF is used for SHAM
    '''
    prettyplot()
    pretty_colors = prettycolors()

    fig = plt.figure(figsize=(10, 10))
    sub = fig.add_subplot(111)
    for i_snap in [11, 7, 4, 1]:
        smf = SMF()
        subh = Subhalos()

        subh.Read(i_snap,
                  scatter=scatter,
                  source=source,
                  nsnap_ancestor=nsnap_ancestor)
        subh.mag_r *= -1.
        mass, phi = smf.Obj(subh,
                            dlogm=0.1,
                            m_arr=np.arange(-24.0, -16., 0.1),
                            LF=True)

        sub.plot(mass, phi, lw=2, ls='-', c=pretty_colors[i_snap % 19])

        analytic_mass, analytic_phi = smf.analytic(get_z_nsnap(i_snap),
                                                   source=source)
        sub.plot(analytic_mass,
                 analytic_phi,
                 lw=4,
                 ls='--',
                 c=pretty_colors[i_snap % 19],
                 label=r"$ z = " + str(round(get_z_nsnap(i_snap), 2)) + "$")

    sub.set_yscale('log')
    sub.set_ylim([10**-7, 10**-1])
    sub.set_xlim([-24.5, -17.8])
    sub.legend(loc='upper right')
    fig_file = ''.join([
        'figure/test/'
        'SubhaloLF', '.scatter',
        str(round(scatter, 2)), '.ancestor',
        str(nsnap_ancestor), '.', source, '.png'
    ])
    fig.savefig(fig_file, bbox_inches='tight')
    plt.close()
Example #6
0
def AllSubhalosSMF(scatter=0.2, source='li-march', age_indicator=None): 
    ''' Compare the SMF of the AllSubhalo object to analytic SMF 
    '''
    # import AllSubhalo object
    owl = AllSubhalos(scatter=scatter, source=source)
    owl.Build(age_indicator=age_indicator)

    prettyplot()
    pretty_colors = prettycolors()

    fig = plt.figure(figsize=(10,10))
    sub = fig.add_subplot(111)
    smf = SMF()
    
    # AllSubhalo SMF
    owl.mass = getattr(owl, 'm.star') 
    subh_mass, subh_phi = smf.Obj(owl, dlogm=0.1, LF=False)
    sub.plot(subh_mass, subh_phi, lw=4, c=pretty_colors[3]) 
    subh_mass, subh_phi = smf._smf(owl.mass[np.where(owl.sfr != -999.)], dlogm=0.1)
    sub.plot(subh_mass, subh_phi, lw=1, c='k') 
    sub.vlines(9.7, subh_phi.min(), subh_phi.max(), linestyle='--', color='k')
    
    # Analytic SMF
    analytic_mass, analytic_phi = smf.analytic(0.05, source=source) 
    sub.plot(analytic_mass, analytic_phi, lw=4, ls='--', c='k', label='Analytic')
    
    # y-axis
    sub.set_yscale('log')
    sub.set_ylim([10**-5, 10**-1])
    sub.set_ylabel(r'$\Phi$', fontsize=25)
    # x-axis
    sub.set_xlim([6.0, 12.0])
    sub.set_xlabel(r'$\mathtt{M_*}$', fontsize=25)

    sub.legend(loc='lower left') 
    
    if age_indicator is None: 
        age_str = 'NOagematch'
    else: 
        age_str = 'agematch_'+age_indicator

    fig_file = ''.join(['figure/test/', 
        'AllSubhalosSMF', 
        '.sham', 
        '.', str(scatter),
        '.', source, 
        '.', age_str, 
        '.png']) 

    fig.savefig(fig_file, bbox_inches='tight') 
    plt.close() 
    return None 
def SubhaloLF(scatter=0.0, source='cool_ages', 
        nsnap_ancestor=20 ): 
    ''' Test the Subhalos/CentralSubhalos imported from TreePM by 
    comparing their measured SMFs to the analytic SMFs 

    Parameters
    ----------
    scatter : float
        Float that specifies the scatter in the SMHM relation, which 
        affects the SHAM masses
    source : string
        String that specifies which SMF is used for SHAM
    '''
    prettyplot()
    pretty_colors = prettycolors()

    fig = plt.figure(figsize=(10,10))
    sub = fig.add_subplot(111)
    for i_snap in [11, 7, 4, 1]:
        smf = SMF()
        subh = Subhalos()

        subh.Read(i_snap, scatter=scatter, source=source, nsnap_ancestor=nsnap_ancestor)
        subh.mag_r *= -1.
        mass, phi = smf.Obj(subh, dlogm=0.1, m_arr=np.arange(-24.0, -16., 0.1), LF=True)

        sub.plot(mass, phi, lw=2, ls='-', c=pretty_colors[i_snap % 19])

        analytic_mass, analytic_phi = smf.analytic(get_z_nsnap(i_snap), source=source) 
        sub.plot(analytic_mass, analytic_phi, lw=4, ls='--', c=pretty_colors[i_snap % 19], 
                label=r"$ z = "+str(round(get_z_nsnap(i_snap),2))+"$") 

    sub.set_yscale('log')
    sub.set_ylim([10**-7, 10**-1])
    sub.set_xlim([-24.5, -17.8])
    sub.legend(loc='upper right')
    fig_file = ''.join(['figure/test/'
        'SubhaloLF', 
        '.scatter', str(round(scatter,2)), 
        '.ancestor', str(nsnap_ancestor),
        '.', source, 
        '.png']) 
    fig.savefig(fig_file, bbox_inches='tight')
    plt.close()
def LineageFinalDescendantSMF(nsnap_ancestor, 
        subhalo_prop={'scatter': 0.0, 'source': 'li-march'}, 
        sfr_prop={'fq': {'name': 'wetzelsmooth'}, 'sfr': {'name': 'average'}}): 
    ''' Plot SMF of final descendant. Also mark the composition of the final SMF from Subhalos 
    that gain stellar mass at different snapshots in the middle of the simulation. 
    '''
    prettyplot()
    pretty_colors=prettycolors()

    bloodline = Lineage(nsnap_ancestor=nsnap_ancestor, subhalo_prop=subhalo_prop)
    bloodline.Read([1], sfr_prop=sfr_prop)

    final_desc = bloodline.descendant_snapshot1
    
    mf = SMF()
    
    smf_plot = plots.PlotSMF()
    smf_plot.cgpop(final_desc, line_color='k')
    
    for isnap in range(2, nsnap_ancestor+1)[::-1]:
        started_here = np.where(final_desc.nsnap_genesis == isnap)
        mass, phi = mf._smf(final_desc.mass[started_here])

        try: 
            phi_tot += phi 
            smf_plot.sub.fill_between(mass, phi_tot - phi, phi_tot, color=pretty_colors[isnap], label='Nsnap='+str(isnap))
        except UnboundLocalError: 
            phi_tot = phi 
            smf_plot.sub.fill_between(mass, np.zeros(len(phi)), phi, color=pretty_colors[isnap], label='Nsnap='+str(isnap))

    smf_plot.set_axes() 
    fig_file = ''.join([
        'figure/test/',
        'LineageFinalDescendantSMF', 
        '.ancestor', str(nsnap_ancestor),
        bloodline._file_spec(subhalo_prop=subhalo_prop, sfr_prop=sfr_prop), 
        '.png'])
    smf_plot.save_fig(fig_file)

    return None
Example #9
0
    def plot(self, mass=None, smf=None, **kwargs): 
        ''' Plot Stellar Mass Function  
        '''
        if 'label' in kwargs: 
            smf_label = kwargs['label']
            kwargs.pop('label', None)
        else: 
            smf_label = ''
    
        if 'line_color' in kwargs: 
            if isinstance(kwargs['line_color'], str):
                line_color = kwargs['line_color']
            elif isinstance(kwargs['line_color'], int):
                line_color = self.pretty_colors[kwargs['line_color']]
            kwargs.pop('line_color', None)
        else: 
            line_color = 'black'

        if 'line_style' in kwargs:
            line_style = kwargs['line_style'] 
            kwargs.pop('line_style', None)
        else: 
            line_style = '-'

        if 'lw' in kwargs: 
            line_width = kwargs['lw'] 
            kwargs.pop('lw', None)
        else:
            line_width = 4
        
        if smf is None: 
            mf = SMF()
            masses, phi = mf._smf(mass, **kwargs)
        else: 
            masses, phi = smf

        self.sub.plot(masses, phi, 
                c=line_color, ls=line_style, lw=line_width,
                label=smf_label)
        return mass, phi 
def DescendantSMF_composition(nsnap_descendant, mass_evol=True, nsnap_ancestor=20, abc_step=29, 
    subhalo_prop = {'scatter': 0.2, 'source': 'li-march'}, 
    sfr_prop = {'fq': {'name': 'wetzelsmooth'}, 'sfr': {'name': 'average'}},
    evol_prop = {'sfr': {'dutycycle': {'name': 'notperiodic'}}, 'mass': {'name': 'sham'}}): 
    
    sf_inherit_file = InheritSF_file(nsnap_descendant, nsnap_ancestor=nsnap_ancestor, 
        abc_step=abc_step, subhalo_prop=subhalo_prop, sfr_prop=sfr_prop, evol_prop=evol_prop)

    bloodline = Lineage(nsnap_ancestor=nsnap_ancestor, subhalo_prop=subhalo_prop)
    bloodline.Read([nsnap_descendant], filename=sf_inherit_file)
    
    descendant = getattr(bloodline, 'descendant_snapshot'+str(nsnap_descendant)) 
    smf_plot = descendant.plotSMF()
    
    mf = SMF()

    if mass_evol: 
        # SMF composition based on their initial mass at nsnap_ancestor 
        started_here = np.where(descendant.nsnap_genesis == nsnap_ancestor)
        start_mass = descendant.mass_genesis[started_here]
        
        mass_bins = np.arange(7., 12.5, 0.5)
        mass_bin_low = mass_bins[:-1]
        mass_bin_high = mass_bins[1:]

        for i_m in range(len(mass_bin_low)): 

            mbin = np.where( 
                    (start_mass > mass_bin_low[i_m]) & 
                    (start_mass <= mass_bin_high[i_m])
                    )

            mass, phi = mf._smf(descendant.mass[started_here[0][mbin]])

            try: 
                phi_tot += phi 
                smf_plot.sub.fill_between(mass, phi_tot - phi, phi_tot, 
                        color=pretty_colors[i_m], 
                        label=str(round(mass_bin_low[i_m], 2))+'-'+str(round(mass_bin_high[i_m], 2)))
            except UnboundLocalError: 
                phi_tot = phi 
                smf_plot.sub.fill_between(mass, np.zeros(len(phi)), phi, 
                        color=pretty_colors[i_m], 
                        label=str(round(mass_bin_low[i_m], 2))+'-'+str(round(mass_bin_high[i_m], 2)))
    else:
        for isnap in range(2, nsnap_ancestor+1)[::-1]:
            started_here = np.where(descendant.nsnap_genesis == isnap)
            mass, phi = mf._smf(descendant.mass[started_here])

            try: 
                phi_tot += phi 
                smf_plot.sub.fill_between(mass, phi_tot - phi, phi_tot, color=pretty_colors[isnap], label='Nsnap='+str(isnap))
            except UnboundLocalError: 
                phi_tot = phi 
                smf_plot.sub.fill_between(mass, np.zeros(len(phi)), phi, color=pretty_colors[isnap], label='Nsnap='+str(isnap))
    smf_plot.set_axes()
    mass_evol_str = ''
    if mass_evol: 
        mass_evol_str = '.mass_evol'

    fig_file = ''.join([
        'figure/test/', 
        ''.join((sf_inherit_file.rsplit('/')[-1]).rsplit('.')[:-1]), 
        '.SMF_composition', 
        mass_evol_str, '.png'])
    smf_plot.save_fig(fig_file)
    return None
def OrphanSubhaloSMF(type,
                     nsnap_ancestor=20,
                     scatter=0.0,
                     source='li-drory-march'):
    ''' SMF for orphan central subhalos that do not have ancestors at snapshot 
    nsnap_ancestor.
    '''
    prettyplot()
    pretty_colors = prettycolors()

    fig = plt.figure(figsize=(10, 10))
    sub = fig.add_subplot(111)

    # Central subhalo at nsnap_ancestor
    if type == 'central':
        subh = CentralSubhalos()
    elif type == 'all':
        subh = Subhalos()
    subh.Read(nsnap_ancestor,
              scatter=scatter,
              source=source,
              nsnap_ancestor=nsnap_ancestor)
    ancestor_index = subh.index
    ancestor_mass = subh.mass

    for i_snap in [1, 5, 10, 15]:
        if i_snap >= nsnap_ancestor:
            continue

        smf = SMF()
        if type == 'central':
            subh = CentralSubhalos()
        elif type == 'all':
            subh = Subhalos()
        subh.Read(i_snap,
                  scatter=scatter,
                  source=source,
                  nsnap_ancestor=nsnap_ancestor)

        # total central subhalo SMF
        subh_mass, subh_phi = smf.centralsubhalos(subh)
        sub.plot(subh_mass, subh_phi, lw=4, c=pretty_colors[i_snap], alpha=0.5)

        # SMF of central subhalos who's ancestors are centrals at snapshot 20
        if i_snap == 1:
            label = 'Galaxies that do not have Ancestors'
        else:
            label = None
        orphan = np.invert(
            np.in1d(getattr(subh, 'ancestor' + str(nsnap_ancestor)),
                    ancestor_index))
        orph_mass, orph_phi = smf.smf(subh.mass[orphan])
        sub.plot(orph_mass,
                 orph_phi,
                 lw=2,
                 ls='--',
                 c=pretty_colors[i_snap],
                 label=label)

    anc_mass, anc_phi = smf.smf(ancestor_mass)
    sub.plot(anc_mass, anc_phi, lw=4, c='gray', label='Total')

    sub.set_yscale('log')
    sub.set_ylim([10**-5, 10**-1])
    sub.set_xlim([6.0, 12.0])
    sub.legend(loc='upper right')

    fig_file = ''.join([
        'figure/test/'
        'OrphanSubhaloSMF', '.', type, '_subhalo', '.scatter',
        str(round(scatter, 2)), '.ancestor',
        str(nsnap_ancestor), '.', source, '.png'
    ])
    fig.savefig(fig_file, bbox_inches='tight')
    plt.close()
    return None
def SubhaloSMF(type, scatter=0.0, source='li-drory-march', nsnap_ancestor=20):
    ''' Test the Subhalos/CentralSubhalos imported from TreePM by 
    comparing their measured SMFs to the analytic SMFs 

    Parameters
    ----------
    type : string 
        String that specifies whether we're interested in CentralSubhalos 
        or all Subhalos.
    scatter : float
        Float that specifies the scatter in the SMHM relation, which 
        affects the SHAM masses
    source : string
        String that specifies which SMF is used for SHAM
    '''
    prettyplot()
    pretty_colors = prettycolors()

    fig = plt.figure(figsize=(10, 10))
    sub = fig.add_subplot(111)
    for i_snap in range(1, nsnap_ancestor + 1):
        smf = SMF()
        if type == 'all':
            subh = Subhalos()
        elif type == 'central':
            subh = CentralSubhalos()
        else:
            raise ValueError

        subh.Read(i_snap,
                  scatter=scatter,
                  source=source,
                  nsnap_ancestor=nsnap_ancestor)

        subh_mass, subh_phi = smf.Obj(subh, dlogm=0.1, LF=False)
        sub.plot(subh_mass,
                 subh_phi,
                 lw=4,
                 c=pretty_colors[i_snap % 19],
                 alpha=0.5)

        analytic_mass, analytic_phi = smf.analytic(get_z_nsnap(i_snap),
                                                   source=source)
        sub.plot(analytic_mass,
                 analytic_phi,
                 lw=4,
                 ls='--',
                 c=pretty_colors[i_snap % 19],
                 label=r"$ z = " + str(round(get_z_nsnap(i_snap), 2)) + "$")

    sub.set_yscale('log')
    sub.set_ylim([10**-5, 10**-1])
    sub.set_xlim([6.0, 12.0])
    #sub.legend(loc='upper right')
    if type == 'all':
        subhalo_str = 'subhalo'
    elif type == 'central':
        subhalo_str = 'central_subhalo'
    else:
        raise ValueError
    plt.show()
    fig_file = ''.join([
        'figure/test/'
        'SubhaloSMF', '.', subhalo_str, '.scatter',
        str(round(scatter, 2)), '.ancestor',
        str(nsnap_ancestor), '.', source, '.png'
    ])
    fig.savefig(fig_file, bbox_inches='tight')
    plt.close()
def DescendantSubhaloSMF(type,
                         nsnap_ancestor=20,
                         scatter=0.0,
                         source='li-drory-march',
                         nomass=False):
    ''' SMF for 'descendant' all/central subhalos that have ancestors. If nomass is 
    specified, then we highlight the portion of the SMF from galaxies whose 
    ancestors have M* = 0 at snapshot nsnap_ancestor. 
    '''
    prettyplot()
    pretty_colors = prettycolors()

    fig = plt.figure(figsize=(10, 10))
    sub = fig.add_subplot(111)

    # Central subhalo at nsnap_ancestor
    if type == 'central':
        subh = CentralSubhalos()
    elif type == 'all':
        subh = Subhalos()

    # read in ancestor snapshot
    subh.Read(nsnap_ancestor,
              scatter=scatter,
              source=source,
              nsnap_ancestor=nsnap_ancestor)
    ancestor_index = subh.index
    ancestor_mass = subh.mass

    # galaxy has M* = 0 at nsnap_ancestor
    nomass = np.where(subh.mass == 0.0)
    nomass_anc_index = subh.index[nomass]
    nomass_anc_mass = subh.mass[nomass]
    # galaxy has M* > 0 at nsnap_ancestor
    massive = np.where(subh.mass > 0.0)
    massive_anc_index = subh.index[massive]
    massive_anc_mass = subh.mass[massive]

    for i_snap in [1, 5, 10, 15]:
        if i_snap == 1:
            massive_label = 'Galaxies whose Ancestors have M* > 0'
            nomass_label = 'Galaxies whose Ancestors have M* = 0'
            label = 'Total'
        elif i_snap >= nsnap_ancestor:
            continue
        else:
            massive_label = None
            nomass_label = None
            label = None

        smf = SMF()
        # total central subhalo SMF
        if type == 'central':
            subh = CentralSubhalos()
        elif type == 'all':
            subh = Subhalos()

        subh.Read(i_snap,
                  scatter=scatter,
                  source=source,
                  nsnap_ancestor=nsnap_ancestor)
        subh_mass, subh_phi = smf.centralsubhalos(subh)
        sub.plot(subh_mass,
                 subh_phi,
                 lw=3,
                 c=pretty_colors[i_snap],
                 label=label)

        anc_ind = getattr(subh, 'ancestor' + str(nsnap_ancestor))
        if not nomass:
            # SMF of central subhalos who's ancestors are massive centrals at snapshot 20
            if i_snap == 1:
                has_ancestor, has_descendant = intersection_index(
                    anc_ind, massive_anc_index)
            else:
                has_ancestor, has_d = intersection_index(
                    anc_ind, massive_anc_index)
            mass, phi = smf.smf(subh.mass[has_ancestor])
            sub.plot(mass,
                     phi,
                     lw=2,
                     ls='--',
                     c=pretty_colors[i_snap],
                     label=massive_label)

        if nomass:
            # SMF of central subhalos who's ancestors have M*=0 centrals at snapshot 20
            if i_snap == 1:
                has_ancestor, has_descendant = intersection_index(
                    anc_ind, nomass_anc_index)
            else:
                has_ancestor, has_d = intersection_index(
                    anc_ind, nomass_anc_index)
            mass, phi = smf.smf(subh.mass[has_ancestor])
            sub.plot(mass,
                     phi,
                     lw=2,
                     ls='--',
                     c=pretty_colors[i_snap],
                     label=nomass_label)

        del subh

    anc_mass, anc_phi = smf.smf(ancestor_mass)
    sub.plot(anc_mass, anc_phi, lw=2, c='k', label='Initial Redshift')
    #print 'With descendants at snapshot 1', len(ancestor_mass[has_descendant])
    #anc_massive = np.where(ancestor_mass[has_descendant] > 0.)
    #print 'With mass greater than 0', len(ancestor_mass[has_descendant[anc_massive]])
    #anc_mass, anc_phi = smf.smf(ancestor_mass[has_descendant])
    #sub.plot(anc_mass, anc_phi, ls='--', lw=4, c='k')
    #anc_mass, anc_phi = smf.smf(ancestor_mass[has_descendant[anc_massive]])
    #sub.plot(anc_mass, anc_phi, ls='--', lw=2, c='green')

    sub.set_yscale('log')
    sub.set_ylim([10**-5, 10**-1])
    sub.set_xlim([6.0, 12.0])
    sub.legend(loc='upper right')
    if nomass:
        nomass_str = '.ancestor_Mstar0'
    else:
        nomass_str = ''

    fig_file = ''.join([
        'figure/test/'
        'DescendantSubhaloSMF', '.', type, '_subhalo', '.scatter',
        str(round(scatter, 2)), '.ancestor',
        str(nsnap_ancestor), '.', source, nomass_str, '.png'
    ])

    fig.savefig(fig_file, bbox_inches='tight')
    plt.close()
    return None
def Compare_DescendantSMF_composition(nsnap_descendant, kwarg_list, n_step=29):
    ''' Compare the M* distribution at z_final of galaxies from same initial 
    M* bin for different prescriptions of SF evolution. More specifically
    this is to see how well the integrated SF stellar masses match the SHAM 
    stellar masses. 
    '''
    descendants = []
    for kwarg in kwarg_list:  # import SF inherited Lineages for different set of arguments
        sf_inherit_file = InheritSF_file(nsnap_descendant,
                                         abc_step=n_step,
                                         **kwarg)
        bloodline = Lineage(nsnap_ancestor=kwarg['nsnap_ancestor'],
                            subhalo_prop=kwarg['subhalo_prop'])
        bloodline.Read([nsnap_descendant], filename=sf_inherit_file)

        descendants.append(
            getattr(bloodline, 'descendant_snapshot' + str(nsnap_descendant)))

    mf = SMF()

    mass_bins = np.arange(7., 12.5, 0.5)
    mass_bin_low = mass_bins[:-1]
    mass_bin_high = mass_bins[1:]

    prettyplot()
    pretty_colors = prettycolors()
    lstyles = ['-', '--', '-.']
    for i_m in range(len(mass_bin_low)):
        fig = plt.figure(1)
        sub = fig.add_subplot(111)
        mass_evol_str = ''
        for i_desc, descendant in enumerate(descendants):
            # SMF composition based on their initial mass at nsnap_ancestor
            started_here = np.where(descendant.nsnap_genesis ==
                                    kwarg_list[i_desc]['nsnap_ancestor'])
            start_mass = descendant.mass_genesis[started_here]

            mbin = np.where((start_mass > mass_bin_low[i_m])
                            & (start_mass <= mass_bin_high[i_m]))

            mass, phi = mf._smf(descendant.mass[started_here[0][mbin]])

            sub.plot(mass,
                     phi,
                     c=pretty_colors[i_desc + 1],
                     lw=4,
                     ls=lstyles[i_desc],
                     label=(kwarg_list[i_desc])['evol_prop']['mass']['name'] +
                     ' Masses')
            mass_evol_str += (kwarg_list[i_desc])['evol_prop']['mass']['name']
        sub.set_xlim([7.5, 12.0])
        sub.set_ylim([0.0, 0.012])
        sub.set_ylabel(r'$\Phi$', fontsize=25)
        sub.set_xlabel(r'$\mathtt{log\;M_*}$', fontsize=25)
        sub.legend(loc='upper right')
        sub.set_title(''.join([
            'log M* = ',
            str(round(mass_bin_low[i_m], 2)), '-',
            str(round(mass_bin_high[i_m], 2)), ' at Snapshot ',
            str(kwarg_list[i_desc]['nsnap_ancestor'])
        ]),
                      fontsize=25)

        fig_file = ''.join([
            'figure/test/', 'DescendantSMF_composition', '.massevol_',
            mass_evol_str, '.Mbin',
            str(round(mass_bin_low[i_m], 2)), '_',
            str(round(mass_bin_high[i_m], 2)), '.png'
        ])
        fig.savefig(fig_file, bbox_inches='tight')
        plt.close()
    return None
def OrphanSubhaloSMF(type, nsnap_ancestor=20, scatter=0.0, source='li-drory-march'): 
    ''' SMF for orphan central subhalos that do not have ancestors at snapshot 
    nsnap_ancestor.
    '''
    prettyplot()
    pretty_colors = prettycolors()

    fig = plt.figure(figsize=(10,10))
    sub = fig.add_subplot(111)
    
    # Central subhalo at nsnap_ancestor
    if type == 'central': 
        subh = CentralSubhalos()
    elif type == 'all': 
        subh = Subhalos()
    subh.Read(nsnap_ancestor, scatter=scatter, source=source, nsnap_ancestor=nsnap_ancestor)
    ancestor_index = subh.index
    ancestor_mass = subh.mass

    for i_snap in [1, 5, 10, 15]:
        if i_snap >= nsnap_ancestor: 
            continue 

        smf = SMF()
        if type == 'central': 
            subh = CentralSubhalos()
        elif type == 'all': 
            subh = Subhalos()
        subh.Read(i_snap, scatter=scatter, source=source, nsnap_ancestor=nsnap_ancestor)
    
        # total central subhalo SMF
        subh_mass, subh_phi = smf.centralsubhalos(subh)
        sub.plot(subh_mass, subh_phi, lw=4, c=pretty_colors[i_snap], alpha=0.5) 

        # SMF of central subhalos who's ancestors are centrals at snapshot 20
        if i_snap == 1: 
            label = 'Galaxies that do not have Ancestors'
        else: 
            label = None
        orphan = np.invert(np.in1d(getattr(subh, 'ancestor'+str(nsnap_ancestor)), ancestor_index))
        orph_mass, orph_phi = smf.smf(subh.mass[orphan])
        sub.plot(orph_mass, orph_phi, lw=2, ls='--', c=pretty_colors[i_snap], label=label) 

    anc_mass, anc_phi = smf.smf(ancestor_mass)
    sub.plot(anc_mass, anc_phi,  lw=4, c='gray', label='Total') 

    sub.set_yscale('log')
    sub.set_ylim([10**-5, 10**-1])
    sub.set_xlim([6.0, 12.0])
    sub.legend(loc='upper right')

    fig_file = ''.join([
        'figure/test/'
        'OrphanSubhaloSMF', 
        '.', type, '_subhalo',  
        '.scatter', str(round(scatter, 2)), 
        '.ancestor', str(nsnap_ancestor),
        '.', source,
        '.png'])
    fig.savefig(fig_file, bbox_inches='tight')
    plt.close()
    return None
def SMFevol(): 
    ''' Compare the Marchesini interpolation to others in the literature 
    '''
    #zrange = [0.9, 0.5, 0.05]
    zrange = [0.9]
    source_list = ['li-march', 'li-march-extreme']

    prettyplot() 
    pretty_colors = prettycolors()
    fig = plt.figure(1, figsize=(7, 7))
    sub = fig.add_subplot(111)

    smf = SMF()
    for i_s, source in enumerate(source_list): 
        for iz, z in enumerate(zrange): 
            mass, phi = smf.analytic(z, source=source)  # SMF phi(M*) 
            phi = np.log10(phi)   
            # labels for the SMF sources 
            if source == 'li-march': 
                source_lbl = 'Marchesini+2009 \&\nLi+2009 (Fiducial)'
            elif source == 'li-march-extreme': 
                #source_lbl = 'Li$+$2009 $\&$ \nextreme evolution'
                source_lbl = 'extreme SMF evolution'

            if source == 'li-march':  
                if z == 0.05: 
                    sub.plot(mass, phi, lw=4, color='#b35a00', label=source_lbl)
                    sub.text(10.675, 0.01, "z", color='#b35a00', fontsize=25)
                    sub.text(10.8, 0.01, r"$\mathtt{= 0.05}$", color='#b35a00', fontsize=25)
                elif z == 0.5: 
                    sub.plot(mass, phi, lw=4, color='#ff8100')
                    sub.text(10.8, 0.75*0.01, r"$\mathtt{= 0.5}$", color='#ff8100', fontsize=25)
                elif z == 0.9: 
                    sub.plot(mass, phi, lw=4, color='#ffa74d') 
                    sub.text(10.8, 0.75*0.75*0.01, r"$\mathtt{= 0.9}$", color='#ffa74d', fontsize=25)
            else: 
                if z == 0.5:  
                    sub.plot(mass, phi, lw=2, ls='-.', c='k', label=source_lbl)
                elif z == 0.9:  
                    sub.plot(mass, phi, lw=2, ls='-.', c='k')

    for z in [0.9]:#0.75, 0.9, 1.25]: 
        mass, phi = smf.analytic(z, source='muzzin')  # SMF phi(M*) 
        phi = np.log10(phi)   
        if z == 0.9: 
            phi_low = (1.2/16.25+1.)*phi
            phi_high = (13.91-1.23)/(13.91)*phi
            sub.fill_between(mass, phi_low, phi_high)
        else: 
            sub.plot(mass, phi, lw=6, ls=':', c='k')
            
    #sub.set_ylim([10**-3.75, 10**-1.75])
    sub.set_ylim([-3.75, -1.75])
    sub.set_xlim([8.8, 11.5])
    sub.set_xticks([9.0, 10.0, 11.0])
    sub.minorticks_on()
    #sub.set_yscale('log')
    sub.set_xlabel(r'log($\mathtt{M_*} /\mathtt{M_\odot}$)', fontsize=25) 
    sub.set_ylabel(r'log($\mathtt{\Phi / Mpc^{-3}\;dex^{-1}}$)', fontsize=25) 
    sub.legend(loc='lower left', scatteryoffsets=[0.6])
    
    plt.show() 
    
    #fig_file = ''.join(['figure/', 'test_SMF_evol.png'])
    #fig.savefig(fig_file, bbox_inches='tight', dpi=150) 
    return None
def DescendantQAplot(nsnap_descendants, **sfinh_kwargs):
    ''' The ultimate QAplot t rule them all. 4 panels showing all the properties.
    '''
    sfinherit_file = InheritSF_file(nsnap_descendants, **sfinh_kwargs)
    bloodline = Lineage(nsnap_ancestor=sfinh_kwargs['nsnap_ancestor'],
                        subhalo_prop=sfinh_kwargs['subhalo_prop'])
    if not isinstance(nsnap_descendants, list):
        nsnap_descendants = [nsnap_descendants]
    bloodline.Read(nsnap_descendants, filename=sfinherit_file)

    for nsnap_descendant in nsnap_descendants:
        descendant = getattr(bloodline,
                             'descendant_snapshot' + str(nsnap_descendant))
        descendant.sfr_prop = sfinh_kwargs['sfr_prop']
        started_here = np.where(
            descendant.nsnap_genesis == sfinh_kwargs['nsnap_ancestor'])
        start_mass = descendant.mass_genesis[started_here]

        # Mass bins
        mass_bins = np.arange(7., 12.5, 0.5)
        mass_bin_low = mass_bins[:-1]
        mass_bin_high = mass_bins[1:]

        plt.close()
        prettyplot()
        fig = plt.figure(1, figsize=[25, 6])
        for i_sub in range(1, 5):
            sub_i = fig.add_subplot(1, 4, i_sub)

            if i_sub == 1:  # SMF
                mf = SMF()
                mass, phi = mf.Obj(descendant)

                sub_i.plot(mass,
                           phi,
                           lw=4,
                           c=pretty_colors[descendant.nsnap],
                           label=r'Simulated')

                censub = CentralSubhalos()
                censub.Read(descendant.nsnap,
                            scatter=sfinh_kwargs['subhalo_prop']['scatter'],
                            source=sfinh_kwargs['subhalo_prop']['source'],
                            nsnap_ancestor=sfinh_kwargs['nsnap_ancestor'])

                mass, phi = mf._smf(censub.mass)
                sub_i.plot(mass,
                           phi,
                           c='k',
                           lw=4,
                           ls='--',
                           label='Central Subhalos')

                sub_i.set_ylim([10**-5, 10**-1])
                sub_i.set_xlim([7.5, 12.0])
                plt.xticks([8., 9., 10., 11., 12.])
                sub_i.set_yscale('log')

                # x,y labels
                sub_i.set_xlabel(r'Mass $\mathtt{M_*}$', fontsize=25)
                sub_i.set_ylabel(r'Stellar Mass Function $\mathtt{\Phi}$',
                                 fontsize=25)
                sub_i.legend(loc='upper right', frameon=False)

            elif i_sub == 2:  # SFMS
                # SMF composition based on their initial mass at nsnap_ancestor
                for i_m in range(len(mass_bin_low)):
                    mbin = np.where((start_mass > mass_bin_low[i_m])
                                    & (start_mass <= mass_bin_high[i_m]))

                    sub_i.scatter(descendant.mass[started_here[0][mbin]],
                                  descendant.sfr[started_here[0][mbin]],
                                  color=pretty_colors[i_m],
                                  label=r"$\mathtt{M_{*,i}=}$" +
                                  str(round(mass_bin_low[i_m], 2)) + "-" +
                                  str(round(mass_bin_high[i_m], 2)))

                qfrac = Fq()
                m_arr = np.arange(9.0, 12.5, 0.5)
                sub_i.plot(m_arr,
                           qfrac.SFRcut(
                               m_arr,
                               descendant.zsnap,
                               sfms_prop=(sfinh_kwargs['sfr_prop'])['sfms']),
                           c='k',
                           ls='--',
                           lw=4)

                sub_i.set_xlim([9.0, 12.0])
                sub_i.set_ylim([-5.0, 2.0])
                sub_i.set_xlabel(r'$\mathtt{log\;M_*}$', fontsize=25)
                sub_i.set_ylabel(r'$\mathtt{log\;SFR}$', fontsize=25)

            elif i_sub == 3:  #SMHM
                for i_m in range(len(mass_bin_low)):
                    mbin = np.where((start_mass > mass_bin_low[i_m])
                                    & (start_mass <= mass_bin_high[i_m]))

                    sub_i.scatter(descendant.halo_mass[started_here[0][mbin]],
                                  descendant.mass[started_here[0][mbin]],
                                  color=pretty_colors[i_m],
                                  label=r"$\mathtt{M_{*,i}=}$" +
                                  str(round(mass_bin_low[i_m], 2)) + "-" +
                                  str(round(mass_bin_high[i_m], 2)))

                stellarmass = descendant.mass[started_here]
                halomass = descendant.halo_mass[started_here]
                mbin = np.arange(halomass.min(), halomass.max(), 0.25)
                mlow = mbin[:-1]
                mhigh = mbin[1:]

                muMstar = np.zeros(len(mlow))
                sigMstar = np.zeros(len(mlow))

                for im in range(len(mlow)):
                    mbin = np.where((halomass > mlow[im])
                                    & (halomass <= mhigh[im]))
                    muMstar[im] = np.mean(stellarmass[mbin])
                    sigMstar[im] = np.std(stellarmass[mbin])
                sub_i.errorbar(0.5 * (mlow + mhigh),
                               muMstar,
                               yerr=sigMstar,
                               color='k',
                               lw=3,
                               fmt='o',
                               capthick=2)

                sub_i.set_ylim([9.0, 12.0])
                sub_i.set_xlim([10.0, 15.0])
                sub_i.set_ylabel(r'Stellar Mass $\mathtt{M_*}$', fontsize=25)
                sub_i.set_xlabel(r'Halo Mass $\mathtt{M_{Halo}}$', fontsize=25)

                #sub_i.legend(loc='upper left', frameon=False, scatterpoints=1)
            elif i_sub == 4:  # Fq
                #mass, fq = descendant.Fq()
                sfq = qfrac.Classify(descendant.mass,
                                     descendant.sfr,
                                     descendant.zsnap,
                                     sfms_prop=descendant.sfr_prop['sfms'])
                gc = GroupCat(Mrcut=18, position='central')
                gc.Read()
                gc_sfq = qfrac.Classify(gc.mass,
                                        gc.sfr,
                                        np.mean(gc.z),
                                        sfms_prop=descendant.sfr_prop['sfms'])
                #sub_i.plot(mass, fq, color=pretty_colors[descendant.nsnap], lw=3, ls='--',
                #        label=r'$\mathtt{z =} '+str(descendant.zsnap)+'$')
                M_bin = np.array([9.7, 10.1, 10.5, 10.9, 11.3])
                M_low = M_bin[:-1]
                M_high = M_bin[1:]
                M_mid = 0.5 * (M_low + M_high)

                fq = np.zeros(len(M_low))
                gc_fq = np.zeros(len(M_low))
                for i_m in xrange(len(M_low)):
                    mlim = np.where((descendant.mass > M_low[i_m])
                                    & (descendant.mass <= M_high[i_m]))
                    gc_mlim = np.where((gc.mass > M_low[i_m])
                                       & (gc.mass <= M_high[i_m]))
                    ngal = np.float(len(mlim[0]))
                    gc_ngal = np.float(len(gc_mlim[0]))

                    if ngal != 0:  # no galaxy in mass bin
                        ngal_q = np.float(
                            len(np.where(sfq[mlim] == 'quiescent')[0]))
                        fq[i_m] = ngal_q / ngal
                    if gc_ngal != 0:
                        gc_ngal_q = np.float(
                            len(np.where(gc_sfq[gc_mlim] == 'quiescent')[0]))
                        gc_fq[i_m] = gc_ngal_q / gc_ngal

                sub_i.plot(M_mid,
                           fq,
                           color=pretty_colors[descendant.nsnap],
                           lw=3,
                           label=r'$\mathtt{z =} ' + str(descendant.zsnap) +
                           '$')

                fq_model = qfrac.model(
                    M_bin,
                    descendant.zsnap,
                    lit=sfinh_kwargs['sfr_prop']['fq']['name'])
                sub_i.plot(M_bin,
                           fq_model,
                           color='k',
                           lw=4,
                           ls='--',
                           label=sfinh_kwargs['sfr_prop']['fq']['name'])
                sub_i.scatter(M_mid,
                              gc_fq,
                              color='k',
                              s=100,
                              lw=0,
                              label='Group Catalog')

                sub_i.set_xlim([9.0, 12.0])
                sub_i.set_ylim([0.0, 1.0])

                sub_i.set_xlabel(r'Mass $\mathtt{M_*}$')
                sub_i.set_ylabel(r'Quiescent Fraction $\mathtt{f_Q}$',
                                 fontsize=20)

                sub_i.legend(loc='upper left',
                             frameon=False,
                             scatterpoints=1,
                             markerscale=0.75)

        plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
        fig_name = ''.join([
            'figure/test/', 'QAplot.', '.nsnap',
            str(nsnap_descendant), '.'.join(
                (sfinherit_file.rsplit('/')[-1]).rsplit('.')[:-1]), '.png'
        ])
        fig.savefig(fig_name, bbox_inches='tight')
        plt.close()
    return None
def DescendantQAplot(nsnap_descendants, **sfinh_kwargs): 
    ''' The ultimate QAplot t rule them all. 4 panels showing all the properties.
    '''
    sfinherit_file = InheritSF_file(nsnap_descendants, **sfinh_kwargs)
    bloodline = Lineage(
            nsnap_ancestor=sfinh_kwargs['nsnap_ancestor'], 
            subhalo_prop=sfinh_kwargs['subhalo_prop']
            )
    if not isinstance(nsnap_descendants, list): 
        nsnap_descendants = [nsnap_descendants]
    bloodline.Read(nsnap_descendants, filename=sfinherit_file)
    
    for nsnap_descendant in nsnap_descendants: 
        descendant = getattr(bloodline, 'descendant_snapshot'+str(nsnap_descendant)) 
        descendant.sfr_prop = sfinh_kwargs['sfr_prop']
        started_here = np.where(descendant.nsnap_genesis == sfinh_kwargs['nsnap_ancestor'])
        start_mass = descendant.mass_genesis[started_here]

        # Mass bins
        mass_bins = np.arange(7., 12.5, 0.5)
        mass_bin_low = mass_bins[:-1]
        mass_bin_high = mass_bins[1:]

        plt.close()
        prettyplot()
        fig = plt.figure(1, figsize=[25,6])
        for i_sub in range(1,5): 
            sub_i = fig.add_subplot(1,4,i_sub) 
            
            if i_sub == 1:  # SMF
                mf = SMF()
                mass, phi = mf.Obj(descendant)

                sub_i.plot(mass, phi, lw=4, c=pretty_colors[descendant.nsnap], label=r'Simulated')

                censub = CentralSubhalos()
                censub.Read(descendant.nsnap, 
                        scatter=sfinh_kwargs['subhalo_prop']['scatter'], 
                        source=sfinh_kwargs['subhalo_prop']['source'], 
                        nsnap_ancestor=sfinh_kwargs['nsnap_ancestor'])

                mass, phi = mf._smf(censub.mass)
                sub_i.plot(mass, phi, c='k', lw=4, ls='--', label='Central Subhalos') 

                sub_i.set_ylim([10**-5, 10**-1])
                sub_i.set_xlim([7.5, 12.0])
                plt.xticks([8., 9., 10., 11., 12.])
                sub_i.set_yscale('log')

                # x,y labels
                sub_i.set_xlabel(r'Mass $\mathtt{M_*}$', fontsize=25) 
                sub_i.set_ylabel(r'Stellar Mass Function $\mathtt{\Phi}$', fontsize=25) 
                sub_i.legend(loc='upper right', frameon=False)

            elif i_sub == 2: # SFMS
                # SMF composition based on their initial mass at nsnap_ancestor 
                for i_m in range(len(mass_bin_low)): 
                    mbin = np.where((start_mass > mass_bin_low[i_m]) & (start_mass <= mass_bin_high[i_m]))

                    sub_i.scatter(
                            descendant.mass[started_here[0][mbin]], 
                            descendant.sfr[started_here[0][mbin]], 
                            color=pretty_colors[i_m], 
                            label=r"$\mathtt{M_{*,i}=}$"+str(round(mass_bin_low[i_m],2))+"-"+str(round(mass_bin_high[i_m],2))
                            )
                
                qfrac = Fq()
                m_arr = np.arange(9.0, 12.5, 0.5)
                sub_i.plot(
                        m_arr, 
                        qfrac.SFRcut(m_arr, descendant.zsnap, sfms_prop=(sfinh_kwargs['sfr_prop'])['sfms']), 
                        c='k', ls='--', lw=4)

                sub_i.set_xlim([9.0, 12.0])
                sub_i.set_ylim([-5.0, 2.0])
                sub_i.set_xlabel(r'$\mathtt{log\;M_*}$', fontsize=25)
                sub_i.set_ylabel(r'$\mathtt{log\;SFR}$', fontsize=25)

            elif i_sub == 3: #SMHM
                for i_m in range(len(mass_bin_low)): 
                    mbin = np.where((start_mass > mass_bin_low[i_m]) & (start_mass <= mass_bin_high[i_m]))

                    sub_i.scatter(
                            descendant.halo_mass[started_here[0][mbin]], 
                            descendant.mass[started_here[0][mbin]], 
                            color=pretty_colors[i_m], 
                            label=r"$\mathtt{M_{*,i}=}$"+str(round(mass_bin_low[i_m],2))+"-"+str(round(mass_bin_high[i_m],2))
                            )

                stellarmass = descendant.mass[started_here]
                halomass = descendant.halo_mass[started_here]
                mbin = np.arange(halomass.min(), halomass.max(), 0.25) 
                mlow = mbin[:-1]
                mhigh = mbin[1:]
                
                muMstar = np.zeros(len(mlow))
                sigMstar = np.zeros(len(mlow))

                for im in range(len(mlow)): 
                    mbin = np.where((halomass > mlow[im]) & (halomass <= mhigh[im]))
                    muMstar[im] = np.mean(stellarmass[mbin])
                    sigMstar[im] = np.std(stellarmass[mbin])
                sub_i.errorbar(0.5*(mlow+mhigh), muMstar, yerr=sigMstar, color='k', lw=3, fmt='o', capthick=2)

                sub_i.set_ylim([9.0, 12.0])
                sub_i.set_xlim([10.0, 15.0])
                sub_i.set_ylabel(r'Stellar Mass $\mathtt{M_*}$', fontsize=25) 
                sub_i.set_xlabel(r'Halo Mass $\mathtt{M_{Halo}}$', fontsize=25) 

                #sub_i.legend(loc='upper left', frameon=False, scatterpoints=1)
            elif i_sub == 4: # Fq
                #mass, fq = descendant.Fq()
                sfq = qfrac.Classify(descendant.mass, descendant.sfr, descendant.zsnap, 
                        sfms_prop=descendant.sfr_prop['sfms'])
                gc = GroupCat(Mrcut=18, position='central')
                gc.Read()
                gc_sfq = qfrac.Classify(gc.mass, gc.sfr, np.mean(gc.z), 
                        sfms_prop=descendant.sfr_prop['sfms'])
                #sub_i.plot(mass, fq, color=pretty_colors[descendant.nsnap], lw=3, ls='--', 
                #        label=r'$\mathtt{z =} '+str(descendant.zsnap)+'$')
                M_bin = np.array([9.7, 10.1, 10.5, 10.9, 11.3])
                M_low = M_bin[:-1]
                M_high = M_bin[1:]
                M_mid = 0.5 * (M_low + M_high)

                fq = np.zeros(len(M_low))
                gc_fq = np.zeros(len(M_low))
                for i_m in xrange(len(M_low)):
                    mlim = np.where((descendant.mass > M_low[i_m]) & (descendant.mass <= M_high[i_m]))
                    gc_mlim = np.where((gc.mass > M_low[i_m]) & (gc.mass <= M_high[i_m]))
                    ngal = np.float(len(mlim[0]))
                    gc_ngal = np.float(len(gc_mlim[0]))

                    if ngal != 0:  # no galaxy in mass bin 
                        ngal_q = np.float(len(np.where(sfq[mlim] == 'quiescent')[0]))
                        fq[i_m] = ngal_q/ngal
                    if gc_ngal != 0:
                        gc_ngal_q = np.float(len(np.where(gc_sfq[gc_mlim] == 'quiescent')[0]))
                        gc_fq[i_m] = gc_ngal_q/gc_ngal

                sub_i.plot(M_mid, fq, color=pretty_colors[descendant.nsnap], lw=3, label=r'$\mathtt{z =} '+str(descendant.zsnap)+'$')
                
                fq_model = qfrac.model(M_bin, descendant.zsnap, lit=sfinh_kwargs['sfr_prop']['fq']['name'])
                sub_i.plot(M_bin, fq_model, color='k', lw=4, ls='--', label=sfinh_kwargs['sfr_prop']['fq']['name'])
                sub_i.scatter(M_mid, gc_fq, color='k', s=100, lw=0, label='Group Catalog')

                sub_i.set_xlim([9.0, 12.0])
                sub_i.set_ylim([0.0, 1.0])

                sub_i.set_xlabel(r'Mass $\mathtt{M_*}$') 
                sub_i.set_ylabel(r'Quiescent Fraction $\mathtt{f_Q}$', fontsize=20) 

                sub_i.legend(loc='upper left', frameon=False, scatterpoints=1, markerscale=0.75)

        plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0) 
        fig_name = ''.join([
            'figure/test/', 
            'QAplot.',
            '.nsnap', str(nsnap_descendant),  
            '.'.join((sfinherit_file.rsplit('/')[-1]).rsplit('.')[:-1]), 
            '.png'])
        fig.savefig(fig_name, bbox_inches='tight') 
        plt.close()
    return None 
Example #19
0
def QAplot(descendant, sfinh_kwargs, fig_name=None, **kwargs):
    ''' Given galpop object and the SF Inheritance parametes, 
    plot its SMF, 
    '''
    # Mass bins
    mass_bins = np.arange(7., 12.5, 0.5)
    mass_bin_low = mass_bins[:-1]
    mass_bin_high = mass_bins[1:]

    plt.close()
    prettyplot()
    fig = plt.figure(1, figsize=[20,12])
    for i_sub in range(1,6): 
        sub_i = fig.add_subplot(2,3,i_sub) 
    
        if i_sub == 1:  # SMF
            mf = SMF()
            mass, phi = mf.Obj(descendant)

            sub_i.plot(mass, phi, lw=4, c='r', label=r'Simulated')

            censub = CentralSubhalos()
            censub.Read(descendant.nsnap, 
                    scatter=sfinh_kwargs['subhalo_prop']['scatter'], 
                    source=sfinh_kwargs['subhalo_prop']['source'], 
                    nsnap_ancestor=sfinh_kwargs['nsnap_ancestor'])

            mass, phi = mf._smf(censub.mass)
            sub_i.plot(mass, phi, c='k', lw=4, ls='--', label='Central Subhalos') 

            sub_i.set_ylim([10**-5, 10**-1])
            sub_i.set_xlim([7.5, 12.0])
            plt.xticks([8., 9., 10., 11., 12.])
            sub_i.set_yscale('log')

            # x,y labels
            sub_i.set_ylabel(r'Stellar Mass ($\mathtt{log\; M_*}$)', fontsize=20) 
            sub_i.set_ylabel(r'Stellar Mass Function $\mathtt{\Phi}$', fontsize=20) 
            sub_i.legend(loc='upper right', frameon=False)

        elif i_sub == 2: # SFMS
            # SMF composition based on their initial mass at nsnap_ancestor 

            # random subsample 
            n_tot = len(descendant.mass)
            r_subsample = np.random.choice(n_tot, size=100000)
            sub_i.scatter(descendant.mass[r_subsample], descendant.sfr[r_subsample], color='b', s=0.5) 

            qfrac = Fq()
            m_arr = np.arange(9.0, 12.5, 0.5)
            sub_i.plot(
                    m_arr, 
                    qfrac.SFRcut(m_arr, descendant.zsnap, sfms_prop=(sfinh_kwargs['sfr_prop'])['sfms']), 
                    c='k', ls='--', lw=4)

            sub_i.text(10.5, -4., r'$\mathtt{z='+str(descendant.zsnap)+'}$', fontsize=25)

            sub_i.set_xlim([9.0, 12.0])
            sub_i.set_ylim([-5.0, 2.0])
            sub_i.set_ylabel(r'Stellar Mass ($\mathtt{log\; M_*}$)', fontsize=20) 
            sub_i.set_ylabel(r'$\mathtt{log\;SFR}$', fontsize=20)

        elif i_sub == 3: #SMHM
            #corner.hist2d(descendant.halo_mass, descendant.mass, ax=sub_i, c='b', fill_contours=True, smooth=1.0)
            n_tot = len(descendant.mass)
            r_subsample = np.random.choice(n_tot, size=100000)
            sub_i.scatter(descendant.halo_mass[r_subsample], descendant.mass[r_subsample], color='b', s=0.5)

            stellarmass = descendant.mass
            halomass = descendant.halo_mass
            mbin = np.arange(halomass.min(), halomass.max(), 0.25) 
            mlow = mbin[:-1]
            mhigh = mbin[1:]
            
            muMstar = np.zeros(len(mlow))
            sigMstar = np.zeros(len(mlow))

            for im in range(len(mlow)): 
                mbin = np.where((halomass > mlow[im]) & (halomass <= mhigh[im]))
                muMstar[im] = np.mean(stellarmass[mbin])
                sigMstar[im] = np.std(stellarmass[mbin])
            sub_i.errorbar(0.5*(mlow+mhigh), muMstar, yerr=sigMstar, color='k', lw=3, fmt='o', capthick=2)

            sub_i.set_ylim([9.0, 12.0])
            sub_i.set_xlim([10.0, 15.0])
            sub_i.set_ylabel(r'Stellar Mass ($\mathtt{log\; M_*}$)', fontsize=20) 
            sub_i.set_xlabel(r'Halo Mass ($\mathtt{log\;M_{Halo}}$)', fontsize=20) 

        elif i_sub == 4: # Fq
            sfq = qfrac.Classify(descendant.mass, descendant.sfr, descendant.zsnap, 
                    sfms_prop=descendant.sfr_prop['sfms'])
            gc = GroupCat(Mrcut=18, position='central')
            gc.Read()
            gc_sfq = qfrac.Classify(gc.mass, gc.sfr, np.mean(gc.z), 
                    sfms_prop=descendant.sfr_prop['sfms'])
            #sub_i.plot(mass, fq, color=pretty_colors[descendant.nsnap], lw=3, ls='--', 
            #        label=r'$\mathtt{z =} '+str(descendant.zsnap)+'$')
            M_bin = np.array([9.7, 10.1, 10.5, 10.9, 11.3])
            M_low = M_bin[:-1]
            M_high = M_bin[1:]
            M_mid = 0.5 * (M_low + M_high)

            fq = np.zeros(len(M_low))
            gc_fq = np.zeros(len(M_low))
            for i_m in xrange(len(M_low)):
                mlim = np.where((descendant.mass > M_low[i_m]) & (descendant.mass <= M_high[i_m]))
                gc_mlim = np.where((gc.mass > M_low[i_m]) & (gc.mass <= M_high[i_m]))
                ngal = np.float(len(mlim[0]))
                gc_ngal = np.float(len(gc_mlim[0]))

                if ngal != 0:  # no galaxy in mass bin 
                    ngal_q = np.float(len(np.where(sfq[mlim] == 'quiescent')[0]))
                    fq[i_m] = ngal_q/ngal
                if gc_ngal != 0:
                    gc_ngal_q = np.float(len(np.where(gc_sfq[gc_mlim] == 'quiescent')[0]))
                    gc_fq[i_m] = gc_ngal_q/gc_ngal

            sub_i.plot(M_mid, fq, color='r', lw=3, label=r'Simulated')
            
            # fQ of the Group Catalog 
            sub_i.scatter(M_mid, gc_fq, color='k', s=100, lw=0, label='Group Catalog')
            # fQ of the model (input) fQ
            fq_model = qfrac.model(M_bin, descendant.zsnap, lit=sfinh_kwargs['sfr_prop']['fq']['name'])
            sub_i.plot(M_bin, fq_model, 
                    color='k', lw=4, ls='--', 
                    label=sfinh_kwargs['sfr_prop']['fq']['name'].replace('_', ' ').title() 
                    )

            sub_i.set_xlim([9.0, 12.0])
            sub_i.set_ylim([0.0, 1.0])

            sub_i.set_ylabel(r'Stellar Mass ($\mathtt{log\; M_*}$)', fontsize=20) 
            sub_i.set_ylabel(r'Quiescent Fraction $\mathtt{f_Q}$', fontsize=20) 

            sub_i.legend(loc='upper left', frameon=False, scatterpoints=1, markerscale=0.75)

        elif i_sub == 5: # Tau_Q(M*)
            mass_bin = np.arange(9.0, 12.0, 0.1)

            tau_m = getTauQ(mass_bin, tau_prop=sfinh_kwargs['evol_prop']['tau']) 
            sub_i.plot(10**mass_bin, tau_m, color='r', lw=4, label='Simulated') 

            if 'taus' in kwargs: 
                tau_slopes, tau_offsets = kwargs['taus']

                i_tau_ms = np.zeros((len(mass_bin), len(tau_slopes)))
                for i_tau in range(len(tau_slopes)): 
                    i_tau_prop = sfinh_kwargs['evol_prop']['tau'].copy()
                    i_tau_prop['slope'] = tau_slopes[i_tau]
                    i_tau_prop['yint'] = tau_offsets[i_tau]

                    i_tau_m = getTauQ(mass_bin, tau_prop=i_tau_prop) 
                    #sub_i.plot(10**mass_bin, i_tau_m, color='r', alpha=0.1, lw=2) 
                    i_tau_ms[:,i_tau] = i_tau_m
            
                sig_tau = np.zeros(len(mass_bin))
                for im in range(len(mass_bin)):
                    sig_tau[im] = np.std(i_tau_ms[im,:])
                
                sub_i.errorbar(10**mass_bin, tau_m, yerr=sig_tau, color='r', lw=4) 

            # satellite quenching fraction for comparison 
            tau_sat = getTauQ(mass_bin, tau_prop={'name': 'satellite'}) 

            sub_i.plot(10**mass_bin, tau_sat, color='black', lw=4, ls='--', label=r'Satellite (Wetzel+ 2013)') 

            sub_i.set_xlim([10**9.5, 10**11.75])
            sub_i.set_ylim([0.0, 2.0])
            sub_i.set_xscale('log')

            sub_i.set_xlabel(r'Stellar Mass $[\mathtt{M_\odot}]$',fontsize=20) 
            sub_i.legend(loc='upper right') 

            sub_i.set_ylabel(r'Quenching Timescales $(\tau_\mathtt{Q})$ [Gyr]',fontsize=20) 
        
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0) 
    if fig_name is None: 
        plt.show()
        plt.close()
    else: 
        fig.savefig(fig_name, bbox_inches='tight') 
        plt.close()
    return None 
def Compare_DescendantSMF_composition(nsnap_descendant, kwarg_list, n_step=29):
    ''' Compare the M* distribution at z_final of galaxies from same initial 
    M* bin for different prescriptions of SF evolution. More specifically
    this is to see how well the integrated SF stellar masses match the SHAM 
    stellar masses. 
    '''
    descendants = [] 
    for kwarg in kwarg_list:    # import SF inherited Lineages for different set of arguments
        sf_inherit_file = InheritSF_file(nsnap_descendant, abc_step=n_step, 
                **kwarg)
        bloodline = Lineage(nsnap_ancestor=kwarg['nsnap_ancestor'], subhalo_prop=kwarg['subhalo_prop'])
        bloodline.Read([nsnap_descendant], filename=sf_inherit_file)

        descendants.append(getattr(bloodline, 'descendant_snapshot'+str(nsnap_descendant)))
    
    mf = SMF()
    
    mass_bins = np.arange(7., 12.5, 0.5)
    mass_bin_low = mass_bins[:-1]
    mass_bin_high = mass_bins[1:]

    prettyplot()
    pretty_colors = prettycolors()
    lstyles = ['-', '--', '-.']
    for i_m in range(len(mass_bin_low)): 
        fig = plt.figure(1)
        sub = fig.add_subplot(111)
        mass_evol_str = ''
        for i_desc, descendant in enumerate(descendants): 
            # SMF composition based on their initial mass at nsnap_ancestor 
            started_here = np.where(descendant.nsnap_genesis == kwarg_list[i_desc]['nsnap_ancestor'])
            start_mass = descendant.mass_genesis[started_here]

            mbin = np.where( 
                    (start_mass > mass_bin_low[i_m]) & 
                    (start_mass <= mass_bin_high[i_m])
                    )

            mass, phi = mf._smf(descendant.mass[started_here[0][mbin]])

            sub.plot(
                    mass, 
                    phi, 
                    c=pretty_colors[i_desc+1], 
                    lw=4, 
                    ls=lstyles[i_desc],
                    label=(kwarg_list[i_desc])['evol_prop']['mass']['name']+' Masses'
                    )
            mass_evol_str += (kwarg_list[i_desc])['evol_prop']['mass']['name']
        sub.set_xlim([7.5, 12.0])
        sub.set_ylim([0.0, 0.012])
        sub.set_ylabel(r'$\Phi$', fontsize=25)
        sub.set_xlabel(r'$\mathtt{log\;M_*}$', fontsize=25)
        sub.legend(loc='upper right')
        sub.set_title(''.join([
            'log M* = ', str(round(mass_bin_low[i_m],2)), '-', str(round(mass_bin_high[i_m],2)),
            ' at Snapshot ', str(kwarg_list[i_desc]['nsnap_ancestor'])]), fontsize=25)

        fig_file = ''.join([
            'figure/test/', 
            'DescendantSMF_composition', 
            '.massevol_', 
            mass_evol_str, 
            '.Mbin', str(round(mass_bin_low[i_m],2)), '_', str(round(mass_bin_high[i_m],2)),
            '.png'])
        fig.savefig(fig_file, bbox_inches='tight')
        plt.close()
    return None
def DescendantHMF_composition(nsnap_descendant, nsnap_ancestor=20, n_step=29, 
    subhalo_prop = {'scatter': 0.2, 'source': 'li-march'}, 
    sfr_prop = {'fq': {'name': 'wetzelsmooth'}, 'sfms': {'name': 'linear'}},
    evol_prop = {'sfr': {'dutycycle': {'name': 'notperiodic'}}, 'mass': {'name': 'sham'}}): 
    ''' Halo Mass Function composition at z = z_final by initial halo mass at 
    nsnap_ancestor. 
    '''
    # import SF inherited Lineage
    sf_inherit_file = InheritSF_file(
            nsnap_descendant, 
            nsnap_ancestor=nsnap_ancestor, 
            abc_step=n_step, 
            subhalo_prop=subhalo_prop, 
            sfr_prop=sfr_prop, 
            evol_prop=evol_prop
            )
    bloodline = Lineage(
            nsnap_ancestor=nsnap_ancestor, 
            subhalo_prop=subhalo_prop
            )
    bloodline.Read([nsnap_descendant], filename=sf_inherit_file)
    # descendant
    descendant = getattr(bloodline, 'descendant_snapshot'+str(nsnap_descendant)) 
    smf_plot = PlotSMF()
    mf = SMF()

    # SMF composition based on their initial mass at nsnap_ancestor 
    started_here = np.where(descendant.nsnap_genesis == nsnap_ancestor)
    start_mass = descendant.halomass_genesis[started_here]
    
    mass_bins = np.arange(10., 17.5, 0.5)
    mass_bin_low = mass_bins[:-1]
    mass_bin_high = mass_bins[1:]

    for i_m in range(len(mass_bin_low)): 

        mbin = np.where( 
                (start_mass > mass_bin_low[i_m]) & 
                (start_mass <= mass_bin_high[i_m])
                )

        mass, phi = mf._smf(descendant.halo_mass[started_here[0][mbin]], 
                m_arr= np.arange(10., 17.1, 0.1))

        try: 
            phi_tot += phi 
            smf_plot.sub.fill_between(mass, phi_tot - phi, phi_tot, 
                    color=pretty_colors[i_m], 
                    label=str(round(mass_bin_low[i_m], 2))+'-'+str(round(mass_bin_high[i_m], 2)))
        except UnboundLocalError: 
            phi_tot = phi 
            smf_plot.sub.fill_between(mass, np.zeros(len(phi)), phi, 
                    color=pretty_colors[i_m], 
                    label=str(round(mass_bin_low[i_m], 2))+'-'+str(round(mass_bin_high[i_m], 2)))

    #smf_plot.set_axes()
    smf_plot.sub.set_yscale('log') 
    smf_plot.sub.set_xlim([10.0, 17.0])
    smf_plot.sub.legend(loc='upper right') 
    mass_evol_str = '.mass_evol'

    fig_file = ''.join([
        'figure/test/', 
        ''.join((sf_inherit_file.rsplit('/')[-1]).rsplit('.')[:-1]), 
        '.HMF_composition', 
        mass_evol_str, '.png'])
    smf_plot.save_fig(fig_file)
    return None
def DescendantHMF_composition(nsnap_descendant,
                              nsnap_ancestor=20,
                              n_step=29,
                              subhalo_prop={
                                  'scatter': 0.2,
                                  'source': 'li-march'
                              },
                              sfr_prop={
                                  'fq': {
                                      'name': 'wetzelsmooth'
                                  },
                                  'sfms': {
                                      'name': 'linear'
                                  }
                              },
                              evol_prop={
                                  'sfr': {
                                      'dutycycle': {
                                          'name': 'notperiodic'
                                      }
                                  },
                                  'mass': {
                                      'name': 'sham'
                                  }
                              }):
    ''' Halo Mass Function composition at z = z_final by initial halo mass at 
    nsnap_ancestor. 
    '''
    # import SF inherited Lineage
    sf_inherit_file = InheritSF_file(nsnap_descendant,
                                     nsnap_ancestor=nsnap_ancestor,
                                     abc_step=n_step,
                                     subhalo_prop=subhalo_prop,
                                     sfr_prop=sfr_prop,
                                     evol_prop=evol_prop)
    bloodline = Lineage(nsnap_ancestor=nsnap_ancestor,
                        subhalo_prop=subhalo_prop)
    bloodline.Read([nsnap_descendant], filename=sf_inherit_file)
    # descendant
    descendant = getattr(bloodline,
                         'descendant_snapshot' + str(nsnap_descendant))
    smf_plot = PlotSMF()
    mf = SMF()

    # SMF composition based on their initial mass at nsnap_ancestor
    started_here = np.where(descendant.nsnap_genesis == nsnap_ancestor)
    start_mass = descendant.halomass_genesis[started_here]

    mass_bins = np.arange(10., 17.5, 0.5)
    mass_bin_low = mass_bins[:-1]
    mass_bin_high = mass_bins[1:]

    for i_m in range(len(mass_bin_low)):

        mbin = np.where((start_mass > mass_bin_low[i_m])
                        & (start_mass <= mass_bin_high[i_m]))

        mass, phi = mf._smf(descendant.halo_mass[started_here[0][mbin]],
                            m_arr=np.arange(10., 17.1, 0.1))

        try:
            phi_tot += phi
            smf_plot.sub.fill_between(mass,
                                      phi_tot - phi,
                                      phi_tot,
                                      color=pretty_colors[i_m],
                                      label=str(round(mass_bin_low[i_m], 2)) +
                                      '-' + str(round(mass_bin_high[i_m], 2)))
        except UnboundLocalError:
            phi_tot = phi
            smf_plot.sub.fill_between(mass,
                                      np.zeros(len(phi)),
                                      phi,
                                      color=pretty_colors[i_m],
                                      label=str(round(mass_bin_low[i_m], 2)) +
                                      '-' + str(round(mass_bin_high[i_m], 2)))

    #smf_plot.set_axes()
    smf_plot.sub.set_yscale('log')
    smf_plot.sub.set_xlim([10.0, 17.0])
    smf_plot.sub.legend(loc='upper right')
    mass_evol_str = '.mass_evol'

    fig_file = ''.join([
        'figure/test/', ''.join(
            (sf_inherit_file.rsplit('/')[-1]).rsplit('.')[:-1]),
        '.HMF_composition', mass_evol_str, '.png'
    ])
    smf_plot.save_fig(fig_file)
    return None
Example #23
0
def LineageSMF(nsnap_ancestor,
               descendants=None,
               subhalo_prop={
                   'scatter': 0.0,
                   'source': 'li-march'
               },
               sfr_prop={
                   'fq': {
                       'name': 'wetzelsmooth'
                   },
                   'sfr': {
                       'name': 'average'
                   }
               }):
    ''' Plot the SMF of lineage galaxy population (both ancestor and descendants).
    Compare the lineage SMF to the central subhalo and analytic SMFs for all 
    subhalos. The main agreement, is between the lineage SMF and the central subhalo
    SMF. If nsnap_ancestor is high, then there should be more discrepancy 
    between LIneage SMF and CentralSubhalos SMF because more galaxies are lost. 
    '''
    # read in desendants from the lineage object
    if descendants is None:
        descendants = range(1, nsnap_ancestor)
    bloodline = Lineage(nsnap_ancestor=nsnap_ancestor,
                        subhalo_prop=subhalo_prop)
    bloodline.Read(descendants)

    smf_plot = plots.PlotSMF()
    # plot ancestor against the analytic SMF
    ancestor = getattr(bloodline, 'ancestor')
    #smf_plot.cenque(ancestor)   # SMF of lineage ancestor
    #smf_plot.analytic(
    #        ancestor.zsnap,
    #        source=ancestor.subhalo_prop['source'],
    #        line_style='-', lw=1,
    #        label='All Subhalos')

    #smf = SMF()
    #subh = CentralSubhalos()
    #subh.Read(
    #        nsnap_ancestor,
    #        scatter=ancestor.subhalo_prop['scatter'],
    #        source=ancestor.subhalo_prop['source'])
    #subh_mass, subh_phi = smf.centralsubhalos(subh)
    #smf_plot.sub.plot(subh_mass, subh_phi, lw=4, ls='--', c='gray', label='Central Subhalos')

    for isnap in descendants:
        descendant = getattr(bloodline, 'descendant_snapshot' + str(isnap))
        smf = SMF()
        subh = CentralSubhalos()
        subh.Read(isnap,
                  scatter=descendant.subhalo_prop['scatter'],
                  source=descendant.subhalo_prop['source'])
        subh_mass, subh_phi = smf.Obj(subh)
        smf_plot.sub.plot(subh_mass, subh_phi, lw=4, ls='--', c='gray')
        #print (subh_phi - d_phi)/d_phi

        #smf_plot.analytic(
        #        descendant.zsnap,
        #        source=descendant.subhalo_prop['source'],
        #        line_style='-', lw=1,
        #        label=None)

    smf_plot.set_axes()
    plt.show()
    smf_plot_file = ''.join([
        'figure/test/', 'LineageSMF', '.ancestor',
        str(nsnap_ancestor),
        bloodline._file_spec(subhalo_prop=bloodline.subhalo_prop,
                             sfr_prop=bloodline.sfr_prop), '.png'
    ])
    smf_plot.save_fig(smf_plot_file)
    return None
def DescendantSMF_composition(nsnap_descendant,
                              mass_evol=True,
                              nsnap_ancestor=20,
                              abc_step=29,
                              subhalo_prop={
                                  'scatter': 0.2,
                                  'source': 'li-march'
                              },
                              sfr_prop={
                                  'fq': {
                                      'name': 'wetzelsmooth'
                                  },
                                  'sfr': {
                                      'name': 'average'
                                  }
                              },
                              evol_prop={
                                  'sfr': {
                                      'dutycycle': {
                                          'name': 'notperiodic'
                                      }
                                  },
                                  'mass': {
                                      'name': 'sham'
                                  }
                              }):

    sf_inherit_file = InheritSF_file(nsnap_descendant,
                                     nsnap_ancestor=nsnap_ancestor,
                                     abc_step=abc_step,
                                     subhalo_prop=subhalo_prop,
                                     sfr_prop=sfr_prop,
                                     evol_prop=evol_prop)

    bloodline = Lineage(nsnap_ancestor=nsnap_ancestor,
                        subhalo_prop=subhalo_prop)
    bloodline.Read([nsnap_descendant], filename=sf_inherit_file)

    descendant = getattr(bloodline,
                         'descendant_snapshot' + str(nsnap_descendant))
    smf_plot = descendant.plotSMF()

    mf = SMF()

    if mass_evol:
        # SMF composition based on their initial mass at nsnap_ancestor
        started_here = np.where(descendant.nsnap_genesis == nsnap_ancestor)
        start_mass = descendant.mass_genesis[started_here]

        mass_bins = np.arange(7., 12.5, 0.5)
        mass_bin_low = mass_bins[:-1]
        mass_bin_high = mass_bins[1:]

        for i_m in range(len(mass_bin_low)):

            mbin = np.where((start_mass > mass_bin_low[i_m])
                            & (start_mass <= mass_bin_high[i_m]))

            mass, phi = mf._smf(descendant.mass[started_here[0][mbin]])

            try:
                phi_tot += phi
                smf_plot.sub.fill_between(
                    mass,
                    phi_tot - phi,
                    phi_tot,
                    color=pretty_colors[i_m],
                    label=str(round(mass_bin_low[i_m], 2)) + '-' +
                    str(round(mass_bin_high[i_m], 2)))
            except UnboundLocalError:
                phi_tot = phi
                smf_plot.sub.fill_between(
                    mass,
                    np.zeros(len(phi)),
                    phi,
                    color=pretty_colors[i_m],
                    label=str(round(mass_bin_low[i_m], 2)) + '-' +
                    str(round(mass_bin_high[i_m], 2)))
    else:
        for isnap in range(2, nsnap_ancestor + 1)[::-1]:
            started_here = np.where(descendant.nsnap_genesis == isnap)
            mass, phi = mf._smf(descendant.mass[started_here])

            try:
                phi_tot += phi
                smf_plot.sub.fill_between(mass,
                                          phi_tot - phi,
                                          phi_tot,
                                          color=pretty_colors[isnap],
                                          label='Nsnap=' + str(isnap))
            except UnboundLocalError:
                phi_tot = phi
                smf_plot.sub.fill_between(mass,
                                          np.zeros(len(phi)),
                                          phi,
                                          color=pretty_colors[isnap],
                                          label='Nsnap=' + str(isnap))
    smf_plot.set_axes()
    mass_evol_str = ''
    if mass_evol:
        mass_evol_str = '.mass_evol'

    fig_file = ''.join([
        'figure/test/', ''.join(
            (sf_inherit_file.rsplit('/')[-1]).rsplit('.')[:-1]),
        '.SMF_composition', mass_evol_str, '.png'
    ])
    smf_plot.save_fig(fig_file)
    return None
Example #25
0
def SMFevol():
    ''' Compare the Marchesini interpolation to others in the literature 
    '''
    #zrange = [0.9, 0.5, 0.05]
    zrange = [0.9]
    source_list = ['li-march', 'li-march-extreme']

    prettyplot()
    pretty_colors = prettycolors()
    fig = plt.figure(1, figsize=(7, 7))
    sub = fig.add_subplot(111)

    smf = SMF()
    for i_s, source in enumerate(source_list):
        for iz, z in enumerate(zrange):
            mass, phi = smf.analytic(z, source=source)  # SMF phi(M*)
            phi = np.log10(phi)
            # labels for the SMF sources
            if source == 'li-march':
                source_lbl = 'Marchesini+2009 \&\nLi+2009 (Fiducial)'
            elif source == 'li-march-extreme':
                #source_lbl = 'Li$+$2009 $\&$ \nextreme evolution'
                source_lbl = 'extreme SMF evolution'

            if source == 'li-march':
                if z == 0.05:
                    sub.plot(mass,
                             phi,
                             lw=4,
                             color='#b35a00',
                             label=source_lbl)
                    sub.text(10.675, 0.01, "z", color='#b35a00', fontsize=25)
                    sub.text(10.8,
                             0.01,
                             r"$\mathtt{= 0.05}$",
                             color='#b35a00',
                             fontsize=25)
                elif z == 0.5:
                    sub.plot(mass, phi, lw=4, color='#ff8100')
                    sub.text(10.8,
                             0.75 * 0.01,
                             r"$\mathtt{= 0.5}$",
                             color='#ff8100',
                             fontsize=25)
                elif z == 0.9:
                    sub.plot(mass, phi, lw=4, color='#ffa74d')
                    sub.text(10.8,
                             0.75 * 0.75 * 0.01,
                             r"$\mathtt{= 0.9}$",
                             color='#ffa74d',
                             fontsize=25)
            else:
                if z == 0.5:
                    sub.plot(mass, phi, lw=2, ls='-.', c='k', label=source_lbl)
                elif z == 0.9:
                    sub.plot(mass, phi, lw=2, ls='-.', c='k')

    for z in [0.9]:  #0.75, 0.9, 1.25]:
        mass, phi = smf.analytic(z, source='muzzin')  # SMF phi(M*)
        phi = np.log10(phi)
        if z == 0.9:
            phi_low = (1.2 / 16.25 + 1.) * phi
            phi_high = (13.91 - 1.23) / (13.91) * phi
            sub.fill_between(mass, phi_low, phi_high)
        else:
            sub.plot(mass, phi, lw=6, ls=':', c='k')

    #sub.set_ylim([10**-3.75, 10**-1.75])
    sub.set_ylim([-3.75, -1.75])
    sub.set_xlim([8.8, 11.5])
    sub.set_xticks([9.0, 10.0, 11.0])
    sub.minorticks_on()
    #sub.set_yscale('log')
    sub.set_xlabel(r'log($\mathtt{M_*} /\mathtt{M_\odot}$)', fontsize=25)
    sub.set_ylabel(r'log($\mathtt{\Phi / Mpc^{-3}\;dex^{-1}}$)', fontsize=25)
    sub.legend(loc='lower left', scatteryoffsets=[0.6])

    plt.show()

    #fig_file = ''.join(['figure/', 'test_SMF_evol.png'])
    #fig.savefig(fig_file, bbox_inches='tight', dpi=150)
    return None
def DescendantSubhaloSMF(type, nsnap_ancestor=20, scatter=0.0, source='li-drory-march', 
        nomass=False): 
    ''' SMF for 'descendant' all/central subhalos that have ancestors. If nomass is 
    specified, then we highlight the portion of the SMF from galaxies whose 
    ancestors have M* = 0 at snapshot nsnap_ancestor. 
    '''
    prettyplot()
    pretty_colors = prettycolors()

    fig = plt.figure(figsize=(10,10))
    sub = fig.add_subplot(111)
    
    # Central subhalo at nsnap_ancestor
    if type == 'central': 
        subh = CentralSubhalos()
    elif type == 'all':
        subh = Subhalos()

    # read in ancestor snapshot  
    subh.Read(nsnap_ancestor, scatter=scatter, source=source, nsnap_ancestor=nsnap_ancestor)
    ancestor_index = subh.index
    ancestor_mass = subh.mass
    
    # galaxy has M* = 0 at nsnap_ancestor
    nomass = np.where(subh.mass == 0.0)
    nomass_anc_index = subh.index[nomass]
    nomass_anc_mass = subh.mass[nomass]
    # galaxy has M* > 0 at nsnap_ancestor
    massive = np.where(subh.mass > 0.0)
    massive_anc_index = subh.index[massive]
    massive_anc_mass = subh.mass[massive]

    for i_snap in [1, 5, 10, 15]:
        if i_snap == 1: 
            massive_label = 'Galaxies whose Ancestors have M* > 0'
            nomass_label = 'Galaxies whose Ancestors have M* = 0'
            label = 'Total'
        elif i_snap >= nsnap_ancestor: 
            continue 
        else: 
            massive_label = None 
            nomass_label = None
            label = None

        smf = SMF()
        # total central subhalo SMF
        if type == 'central': 
            subh = CentralSubhalos()
        elif type == 'all': 
            subh = Subhalos()

        subh.Read(i_snap, scatter=scatter, source=source, nsnap_ancestor=nsnap_ancestor)
        subh_mass, subh_phi = smf.centralsubhalos(subh)
        sub.plot(subh_mass, subh_phi, lw=3, c=pretty_colors[i_snap], label=label) 
            
        anc_ind = getattr(subh, 'ancestor'+str(nsnap_ancestor))
        if not nomass: 
            # SMF of central subhalos who's ancestors are massive centrals at snapshot 20
            if i_snap == 1: 
                has_ancestor, has_descendant = intersection_index(anc_ind, massive_anc_index)
            else: 
                has_ancestor, has_d = intersection_index(anc_ind, massive_anc_index)
            mass, phi = smf.smf(subh.mass[has_ancestor])
            sub.plot(mass, phi, lw=2, ls='--', c=pretty_colors[i_snap], label=massive_label) 
        
        if nomass:
            # SMF of central subhalos who's ancestors have M*=0 centrals at snapshot 20
            if i_snap == 1: 
                has_ancestor, has_descendant = intersection_index(anc_ind, nomass_anc_index)
            else: 
                has_ancestor, has_d = intersection_index(anc_ind, nomass_anc_index)
            mass, phi = smf.smf(subh.mass[has_ancestor])
            sub.plot(mass, phi, lw=2, ls='--', c=pretty_colors[i_snap], label=nomass_label) 

        del subh

    anc_mass, anc_phi = smf.smf(ancestor_mass)
    sub.plot(anc_mass, anc_phi, lw=2, c='k', label='Initial Redshift') 
    #print 'With descendants at snapshot 1', len(ancestor_mass[has_descendant])
    #anc_massive = np.where(ancestor_mass[has_descendant] > 0.)
    #print 'With mass greater than 0', len(ancestor_mass[has_descendant[anc_massive]])
    #anc_mass, anc_phi = smf.smf(ancestor_mass[has_descendant])
    #sub.plot(anc_mass, anc_phi, ls='--', lw=4, c='k') 
    #anc_mass, anc_phi = smf.smf(ancestor_mass[has_descendant[anc_massive]])
    #sub.plot(anc_mass, anc_phi, ls='--', lw=2, c='green') 

    sub.set_yscale('log')
    sub.set_ylim([10**-5, 10**-1])
    sub.set_xlim([6.0, 12.0])
    sub.legend(loc='upper right')
    if nomass: 
        nomass_str = '.ancestor_Mstar0'
    else: 
        nomass_str = ''
    
    fig_file = ''.join([
        'figure/test/'
        'DescendantSubhaloSMF', 
        '.', type, '_subhalo',  
        '.scatter', str(round(scatter, 2)), 
        '.ancestor', str(nsnap_ancestor),
        '.', source,
        nomass_str, 
        '.png'])

    fig.savefig(fig_file, bbox_inches='tight')
    plt.close()
    return None