Esempio n. 1
0
    def make_wtvsparamval(self, allparams=True, paramlist=None):
        '''
        template usage vs parameter value
        '''
        if allparams:
            paramlist = set(self.test_metadata.colnames) & set(pca.metadata.colnames)
            paramlist = [n for n in paramlist
                         if self.test_metadata[n].shape == (len(self.test_metadata), )]
            paramlist = sorted(paramlist)

        gs, fig = figures_tools.gen_gridspec_fig(
            len(paramlist), border=(1., 1., 0.5, 0.5), space=(0.7, 0.4),
            spsize=(2.5, 1.25))
        subplot_inds = iterprod(range(gs._nrows), range(gs._ncols))

        fig_axes = {n: fig.add_subplot(gs[ii, jj])
                    for (ii, jj), n in zip(subplot_inds, paramlist)}

        for n, ax in fig_axes.items():
            ax.hist(self.trn_metadata[n].data, weights=self.trn_usage_cts,
                       histtype='step', density=True)
            ax.set_yscale('log')
            ax.set_ylabel(r'density', size='x-small')
            ax.tick_params(labelsize='x-small')
            ax.set_xlabel(n.replace('_', '\_'))

        figures_tools.savefig(fig, fname='wtsvsparams.png', fdir=self.workdir)
Esempio n. 2
0
    def make_wtvsparamval(self, allparams=True, paramlist=None):
        '''
        template usage vs parameter value
        '''
        if allparams:
            paramlist = set(self.test_metadata.colnames) & set(
                pca.metadata.colnames)
            paramlist = [
                n for n in paramlist
                if self.test_metadata[n].shape == (len(self.test_metadata), )
            ]
            paramlist = sorted(paramlist)

        gs, fig = figures_tools.gen_gridspec_fig(len(paramlist),
                                                 border=(1., 1., 0.5, 0.5),
                                                 space=(0.7, 0.4),
                                                 spsize=(2.5, 1.25))
        subplot_inds = iterprod(range(gs._nrows), range(gs._ncols))

        fig_axes = {
            n: fig.add_subplot(gs[ii, jj])
            for (ii, jj), n in zip(subplot_inds, paramlist)
        }

        for n, ax in fig_axes.items():
            ax.hist(self.trn_metadata[n].data,
                    weights=self.trn_usage_cts,
                    histtype='step',
                    density=True)
            ax.set_yscale('log')
            ax.set_ylabel(r'density', size='x-small')
            ax.tick_params(labelsize='x-small')
            ax.set_xlabel(n.replace('_', '\_'))

        figures_tools.savefig(fig, fname='wtsvsparams.png', fdir=self.workdir)
Esempio n. 3
0
def make_cosmic_sfr(CSP_dir):
    SFHs_fnames = glob.glob(os.path.join(CSP_dir, 'SFHs_*.fits'))
    nsubpersfh = fits.getval(SFHs_fnames[0], ext=0, keyword='NSUBPER')
    SFHs = np.row_stack(
        [fits.getdata(fn_, 'allsfhs') / fits.getdata(fn_, 'mformed')[::nsubpersfh, None]
         for fn_ in SFHs_fnames])
    ts = fits.getdata(SFHs_fnames[0], 'allts')
    zs, zmasks = zip(*[masked_z_at_value(WMAP9.age, t_ * u.Gyr) for t_ in ts])
    zs = np.ma.array(zs, mask=zmasks)

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    SFRDmean = SFHs.mean(axis=0) / WMAP9.scale_factor(zs)**3.

    ax.plot(1. / WMAP9.scale_factor(zs), SFRDmean / SFRDmean.max())
    ax.set_xlim([1., 1. / WMAP9.scale_factor(10.)])
    ax.set_yscale('log')
    ax.set_ylim([.009, 1.05])
    ax.set_xlabel(r'$\frac{1}{a}$', size='x-small')
    ax.set_ylabel(r'$\log{\psi}$', size='x-small')
    ax.tick_params(labelsize='x-small', which='both')

    ax_ = ax.twiny()
    ax_.set_xlim(ax.get_xlim())
    zticks = np.linspace(0., 10., 11)
    inv_sf_ticks = 1. / WMAP9.scale_factor(zticks)
    ax_.set_xticks(inv_sf_ticks, minor=False)
    ax_.set_xticklabels(zticks)
    ax_.tick_params(labelsize='x-small')
    ax_.set_xlabel(r'$z$', size='x-small')

    fig.suptitle('``Cosmic" SFR', size='small')

    savefig(fig, 'CosmicSFR.png', CSP_dir, close=True)
Esempio n. 4
0
    def make_weightfig(self,
                       xqty,
                       yqty,
                       xbds=[None, None],
                       ybds=[None, None],
                       logx=False,
                       logy=False,
                       xbins=20,
                       ybins=20,
                       overplot_ngroups=0):
        xdata = self.trn_metadata[xqty].data
        ydata = self.trn_metadata[yqty].data

        if logx:
            xdata = np.log10(xdata)
        if logy:
            ydata = np.log10(ydata)

        xbds = figures_tools.decide_lims_pctls(xdata, bds=xbds)
        ybds = figures_tools.decide_lims_pctls(ydata, bds=ybds)
        xbinedges = np.linspace(*xbds, xbins + 1)
        ybinedges = np.linspace(*ybds, ybins + 1)
        xbinctrs = 0.5 * (xbinedges[:-1] + xbinedges[1:])
        ybinctrs = 0.5 * (ybinedges[:-1] + ybinedges[1:])

        rawhist, *_ = np.histogram2d(xdata, ydata, bins=[xbinedges, ybinedges])
        wthist, *_ = np.histogram2d(xdata,
                                    ydata,
                                    bins=[xbinedges, ybinedges],
                                    weights=self.trn_usage_cts)
        Xgrid, Ygrid = np.meshgrid(xbinctrs, ybinctrs, indexing='ij')

        fig, ax = plt.subplots(1, 1)
        raw_cs = ax.contour(Xgrid,
                            Ygrid,
                            rawhist,
                            colors='C0',
                            linewidths=0.25)
        wtd_cs = ax.contour(Xgrid, Ygrid, wthist, colors='C1', linewidths=0.25)

        raw_cs.collections[-1].set_label('all')
        wtd_cs.collections[-1].set_label('wtd')

        if overplot_ngroups > 0:
            self._overplot_models(overplot_ngroups, ax, xdata, ydata)

        if (xqty[0] == 'C') and (yqty[0] == 'C'):
            self._add_color_arrow(c1=xqty, c2=yqty, ax=ax)

        ax.legend()

        ax.set_xlabel(xqty.replace('_', '\_'), size='x-small')
        ax.set_ylabel(yqty.replace('_', '\_'), size='x-small')

        fname = '_'.join(('wtfig', xqty, yqty)) + '.png'
        figures_tools.savefig(fig, fname=fname, fdir=self.workdir)
Esempio n. 5
0
    def make_weighthist(self, **kwargs):
        '''
        histogram of total template usage
        '''
        fig, ax = plt.subplots(1, 1)
        histkws = dict(histtype='step', normed=True)
        histkws.update(**kwargs)
        bins = np.unique(
            np.logspace(0, np.log10(len(self.trn_usage_cts) + 1),
                        21, dtype=int)) - .5
        ax.hist(self.trn_usage_cts + 1, bins=bins, **histkws)

        ax.set_xlabel(r'$N + 1$', size='x-small')
        ax.set_xscale('log')
        ax.set_yscale('log')

        fname = 'wthist.png'
        figures_tools.savefig(fig, fname=fname, fdir=self.workdir)
Esempio n. 6
0
    def make_weighthist(self, **kwargs):
        '''
        histogram of total template usage
        '''
        fig, ax = plt.subplots(1, 1)
        histkws = dict(histtype='step', normed=True)
        histkws.update(**kwargs)
        bins = np.unique(
            np.logspace(
                0, np.log10(len(self.trn_usage_cts) + 1), 21, dtype=int)) - .5
        ax.hist(self.trn_usage_cts + 1, bins=bins, **histkws)

        ax.set_xlabel(r'$N + 1$', size='x-small')
        ax.set_xscale('log')
        ax.set_yscale('log')

        fname = 'wthist.png'
        figures_tools.savefig(fig, fname=fname, fdir=self.workdir)
Esempio n. 7
0
    def make_weightfig(self, xqty, yqty, xbds=[None, None], ybds=[None, None],
                       logx=False, logy=False, xbins=20, ybins=20, overplot_ngroups=0):
        xdata = self.trn_metadata[xqty].data
        ydata = self.trn_metadata[yqty].data

        if logx:
            xdata = np.log10(xdata)
        if logy:
            ydata = np.log10(ydata)

        xbds = figures_tools.decide_lims_pctls(xdata, bds=xbds)
        ybds = figures_tools.decide_lims_pctls(ydata, bds=ybds)
        xbinedges = np.linspace(*xbds, xbins + 1)
        ybinedges = np.linspace(*ybds, ybins + 1)
        xbinctrs = 0.5 * (xbinedges[:-1] + xbinedges[1:])
        ybinctrs = 0.5 * (ybinedges[:-1] + ybinedges[1:])

        rawhist, *_ = np.histogram2d(xdata, ydata, bins=[xbinedges, ybinedges])
        wthist, *_ = np.histogram2d(xdata, ydata, bins=[xbinedges, ybinedges],
                                    weights=self.trn_usage_cts)
        Xgrid, Ygrid = np.meshgrid(xbinctrs, ybinctrs, indexing='ij')

        fig, ax = plt.subplots(1, 1)
        raw_cs = ax.contour(
            Xgrid, Ygrid, rawhist, colors='C0', linewidths=0.25)
        wtd_cs = ax.contour(
            Xgrid, Ygrid, wthist, colors='C1', linewidths=0.25)

        raw_cs.collections[-1].set_label('all')
        wtd_cs.collections[-1].set_label('wtd')

        if overplot_ngroups > 0:
            self._overplot_models(overplot_ngroups, ax, xdata, ydata)

        if (xqty[0] == 'C') and (yqty[0] == 'C'):
            self._add_color_arrow(c1=xqty, c2=yqty, ax=ax)

        ax.legend()

        ax.set_xlabel(xqty.replace('_', '\_'), size='x-small')
        ax.set_ylabel(yqty.replace('_', '\_'), size='x-small')

        fname = '_'.join(('wtfig', xqty, yqty)) + '.png'
        figures_tools.savefig(fig, fname=fname, fdir=self.workdir)
Esempio n. 8
0
def make_cosmic_sfr(CSP_dir):
    SFHs_fnames = glob.glob(os.path.join(CSP_dir, 'SFHs_*.fits'))
    nsubpersfh = fits.getval(SFHs_fnames[0], ext=0, keyword='NSUBPER')
    SFHs = np.row_stack([
        fits.getdata(fn_, 'allsfhs') /
        fits.getdata(fn_, 'mformed')[::nsubpersfh, None] for fn_ in SFHs_fnames
    ])
    ts = fits.getdata(SFHs_fnames[0], 'allts')
    zs, zmasks = zip(*[masked_z_at_value(WMAP9.age, t_ * u.Gyr) for t_ in ts])
    zs = np.ma.array(zs, mask=zmasks)

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    SFRDmean = SFHs.mean(axis=0) / WMAP9.scale_factor(zs)**3.

    ax.plot(1. / WMAP9.scale_factor(zs), SFRDmean / SFRDmean.max())
    ax.set_xlim([1., 1. / WMAP9.scale_factor(10.)])
    ax.set_yscale('log')
    ax.set_ylim([.009, 1.05])
    ax.set_xlabel(r'$\frac{1}{a}$', size='x-small')
    ax.set_ylabel(r'$\log{\psi}$', size='x-small')
    ax.tick_params(labelsize='x-small', which='both')

    ax_ = ax.twiny()
    ax_.set_xlim(ax.get_xlim())
    zticks = np.linspace(0., 10., 11)
    inv_sf_ticks = 1. / WMAP9.scale_factor(zticks)
    ax_.set_xticks(inv_sf_ticks, minor=False)
    ax_.set_xticklabels(zticks)
    ax_.tick_params(labelsize='x-small')
    ax_.set_xlabel(r'$z$', size='x-small')

    fig.suptitle('``Cosmic" SFR', size='small')

    savefig(fig, 'CosmicSFR.png', CSP_dir, close=True)
Esempio n. 9
0
def make_color_compare(CSP_dir,
                       mpl_v,
                       n_obs=10,
                       model_zcolors=False,
                       randomseed=None,
                       snr_min=5.):
    '''
    make (g-r) vs (r-i) plot
    '''

    if randomseed is not None:
        np.random.seed(randomseed)
    drpall = m.load_drpall(mpl_v, index='plateifu')
    drpall = drpall[(drpall['mngtarg2'] == 0) * (drpall['nsa_z'] != -9999)]

    plateifus = np.random.choice(drpall['plateifu'], replace=False, size=n_obs)
    # cols are g, r, and i fluxes in order
    obs_fluxes = [
        m.get_gal_bpfluxes(*plateifu.split('-'), mpl_v, ['g', 'r', 'i'],
                           snr_min) for plateifu in plateifus
    ]
    obs_ext = np.concatenate([
        np.repeat(drpall.loc[plateifu]['nsa_extinction'][None, 3:6],
                  f.shape[0],
                  axis=0) for f, plateifu in zip(obs_fluxes, plateifus)
    ])
    obs_fluxes = np.row_stack(obs_fluxes)

    integrated_g = drpall['nsa_elpetro_absmag'][:, 3] - \
                       drpall['nsa_extinction'][:, 3]
    integrated_r = drpall['nsa_elpetro_absmag'][:, 4] - \
                       drpall['nsa_extinction'][:, 4]
    integrated_i = drpall['nsa_elpetro_absmag'][:, 5] - \
                       drpall['nsa_extinction'][:, 5]
    integrated_gr = integrated_g - integrated_r
    integrated_ri = integrated_r - integrated_i

    obs_gr = -2.5 * np.log10(obs_fluxes[:, 0] / obs_fluxes[:, 1]) - \
        (obs_ext[:, 0] - obs_ext[:, 1])
    obs_ri = -2.5 * np.log10(obs_fluxes[:, 1] / obs_fluxes[:, 2]) - \
        (obs_ext[:, 1] - obs_ext[:, 2])

    models_fnames = glob.glob(os.path.join(CSP_dir, 'CSPs_*.fits'))

    if model_zcolors:
        color1 = 'Cgr_z015'
        color2 = 'Cri_z015'
        color1TeX = r'$^{.15}C_{gr}$'
        color2TeX = r'$^{.15}C_{ri}$'
    else:
        color1 = 'Cgr'
        color2 = 'Cri'
        color1TeX = r'$C_{gr}$'
        color2TeX = r'$C_{ri}$'

    models_colors = t.vstack(
        [t.Table.read(fn_)[color1, color2] for fn_ in models_fnames])

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection='scatter_density')
    ax.scatter([-100], [-100], c='g', s=1, marker='s', label='models')
    ax.scatter([-100], [-100], c='b', s=1, marker='s', label='spax')
    ax.scatter([-100], [-100], c='r', s=1, marker='s', label='integrated')
    ax.scatter_density(obs_gr, obs_ri, color='b')
    ax.scatter_density(models_colors[color1], models_colors[color2], color='g')
    #ax.scatter_density(integrated_gr, integrated_ri, color='r')
    ax.set_xlim([-.2, 1.4])
    ax.set_ylim([-.1, 0.6])
    ax.set_xlabel(color1TeX, size='small')
    ax.set_ylabel(color2TeX, size='small')
    ax.legend(loc='best')
    fig.suptitle(CSP_dir.replace('_', '-'), size='x-small')
    fig.tight_layout()
    ax.tick_params(labelsize='x-small')

    basename = 'Cgr_Cri'
    if model_zcolors:
        basename = '_'.join((basename, 'z015'))

    savefig(fig, ''.join((basename, '.png')), CSP_dir, close=True)
Esempio n. 10
0
 def savefig(self):
     savefig(self.fig, fname='snr_vs_goodpdf.png', fdir='.')
Esempio n. 11
0
def make_color_compare(CSP_dir, mpl_v, n_obs=10, model_zcolors=False,
                       randomseed=None, snr_min=5.):
    '''
    make (g-r) vs (r-i) plot
    '''

    if randomseed is not None:
        np.random.seed(randomseed)
    drpall = m.load_drpall(mpl_v, index='plateifu')
    drpall = drpall[(drpall['mngtarg2'] == 0) * (drpall['nsa_z'] != -9999)]

    plateifus = np.random.choice(drpall['plateifu'], replace=False, size=n_obs)
    # cols are g, r, and i fluxes in order
    obs_fluxes = [m.get_gal_bpfluxes(*plateifu.split('-'), mpl_v,
                                     ['g', 'r', 'i'], snr_min)
                  for plateifu in plateifus]
    obs_ext = np.concatenate(
        [np.repeat(drpall.loc[plateifu]['nsa_extinction'][None, 3:6],
                   f.shape[0], axis=0)
         for f, plateifu in zip(obs_fluxes, plateifus)])
    obs_fluxes = np.row_stack(obs_fluxes)

    integrated_g = drpall['nsa_elpetro_absmag'][:, 3] - \
                       drpall['nsa_extinction'][:, 3]
    integrated_r = drpall['nsa_elpetro_absmag'][:, 4] - \
                       drpall['nsa_extinction'][:, 4]
    integrated_i = drpall['nsa_elpetro_absmag'][:, 5] - \
                       drpall['nsa_extinction'][:, 5]
    integrated_gr = integrated_g - integrated_r
    integrated_ri = integrated_r - integrated_i

    obs_gr = -2.5 * np.log10(obs_fluxes[:, 0] / obs_fluxes[:, 1]) - \
        (obs_ext[:, 0] - obs_ext[:, 1])
    obs_ri = -2.5 * np.log10(obs_fluxes[:, 1] / obs_fluxes[:, 2]) - \
        (obs_ext[:, 1] - obs_ext[:, 2])

    models_fnames = glob.glob(os.path.join(CSP_dir, 'CSPs_*.fits'))

    if model_zcolors:
        color1 = 'Cgr_z015'
        color2 = 'Cri_z015'
        color1TeX = r'$^{.15}C_{gr}$'
        color2TeX = r'$^{.15}C_{ri}$'
    else:
        color1 = 'Cgr'
        color2 = 'Cri'
        color1TeX = r'$C_{gr}$'
        color2TeX = r'$C_{ri}$'

    models_colors = t.vstack([t.Table.read(fn_)[color1, color2]
                              for fn_ in models_fnames])

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection='scatter_density')
    ax.scatter([-100], [-100], c='g', s=1, marker='s', label='models')
    ax.scatter([-100], [-100], c='b', s=1, marker='s', label='spax')
    ax.scatter([-100], [-100], c='r', s=1, marker='s', label='integrated')
    ax.scatter_density(obs_gr, obs_ri, color='b')
    ax.scatter_density(models_colors[color1], models_colors[color2], color='g')
    #ax.scatter_density(integrated_gr, integrated_ri, color='r')
    ax.set_xlim([-.2, 1.4])
    ax.set_ylim([-.1, 0.6])
    ax.set_xlabel(color1TeX, size='small')
    ax.set_ylabel(color2TeX, size='small')
    ax.legend(loc='best')
    fig.suptitle(CSP_dir.replace('_', '-'), size='x-small')
    fig.tight_layout()
    ax.tick_params(labelsize='x-small')

    basename = 'Cgr_Cri'
    if model_zcolors:
        basename = '_'.join((basename, 'z015'))

    savefig(fig, ''.join((basename, '.png')), CSP_dir, close=True)