def test_fluxbinner(spectra):
    wngrid = np.linspace(400, 20000, 100)
    fb = FluxBinner(wngrid=wngrid)
    wn, sp, _, _ = fb.bindown(*spectra)

    assert(wngrid.shape[0] == wn.shape[0])

    assert np.mean(sp) == pytest.approx(np.mean(spectra[1]), rel=0.1)
Exemple #2
0
    def test_wngrid_clip(self):
        from taurex.util.util import clip_native_to_wngrid
        from taurex.binning import FluxBinner

        total_values = 1000
        wngrid = np.linspace(100, 10000, total_values)

        values = np.random.rand(total_values)

        test_grid = wngrid[(wngrid > 4000) & (wngrid < 8000)]

        fb = FluxBinner(wngrid=test_grid)

        true = fb.bindown(wngrid, values)

        clipped = clip_native_to_wngrid(wngrid, test_grid)
        interp_values = np.interp(clipped, wngrid, values)
        clipped_flux = fb.bindown(clipped, interp_values)

        np.testing.assert_array_equal(true[1], clipped_flux[1])
    def _generic_plot(self,
                      wlgrid,
                      native_grid,
                      spectra,
                      resolution,
                      color=None,
                      error=False,
                      alpha=1.0,
                      label=None):

        binned_error = None
        if resolution is not None:
            from taurex.binning import FluxBinner
            from taurex.util.util import create_grid_res, wnwidth_to_wlwidth
            _grid = create_grid_res(resolution,
                                    wlgrid.min() * 0.9,
                                    wlgrid.max() * 1.1)
            bin_wlgrid = _grid[:, 0]

            bin_wngrid = 10000 / _grid[:, 0]

            bin_sort = bin_wngrid.argsort()

            bin_wlgrid = bin_wlgrid[bin_sort]
            bin_wngrid = bin_wngrid[bin_sort]

            bin_wnwidth = wnwidth_to_wlwidth(bin_wlgrid, _grid[bin_sort, 1])
            wlgrid = _grid[bin_sort, 0]
            binner = FluxBinner(bin_wngrid, bin_wnwidth)
            native_spectra = spectra['native_spectrum'][...]
            binned_spectrum = binner.bindown(native_grid, native_spectra)[1]
            try:
                native_error = spectra['native_std']
            except KeyError:
                native_error = None
            if native_error is not None:
                binned_error = binner.bindown(native_grid, native_error)[1]

        else:
            try:
                binned_spectrum = spectra['binned_spectrum'][...]
            except KeyError:
                try:
                    binned_spectrum = spectra['bin_spectrum'][...]
                except KeyError:
                    binned_spectrum = spectra['native_spectrum'][...]
            try:
                binned_error = spectra['binned_std'][...]
            except KeyError:
                binned_error = None
        plt.plot(wlgrid, binned_spectrum, label=label, alpha=alpha)
        if binned_error is not None:
            plt.fill_between(wlgrid,
                             binned_spectrum - binned_error,
                             binned_spectrum + binned_error,
                             alpha=0.5,
                             zorder=-2,
                             color=color,
                             edgecolor='none')

            # 2 sigma
            plt.fill_between(wlgrid,
                             binned_spectrum - 2 * binned_error,
                             binned_spectrum + 2 * binned_error,
                             alpha=0.2,
                             zorder=-3,
                             color=color,
                             edgecolor='none')