Exemple #1
0
    def _destripe(self, comm, pars, dets, periods, psdinfo):
        """ Destripe the buffered data

        """
        auto_timer = timing.auto_timer(type(self).__name__)
        if self._verbose:
            memreport(comm, "just before calling libmadam.destripe")
        if self._cached:
            # destripe
            outpath = ""
            if "path_output" in self.params:
                outpath = self.params["path_output"]
            outpath = outpath.encode("ascii")
            madam.destripe_with_cache(
                comm,
                self._madam_timestamps,
                self._madam_pixels,
                self._madam_pixweights,
                self._madam_signal,
                outpath,
            )
        else:
            (detweights, npsd, psdstarts, psdfreqs, psdvals) = psdinfo

            # destripe
            madam.destripe(
                comm,
                pars,
                dets,
                detweights,
                self._madam_timestamps,
                self._madam_pixels,
                self._madam_pixweights,
                self._madam_signal,
                periods,
                npsd,
                psdstarts,
                psdfreqs,
                psdvals,
            )

            if self._mcmode:
                self._cached = True
        return
Exemple #2
0
    def _destripe(self, comm, pars, dets, periods, psdinfo):
        """ Destripe the buffered data

        """
        auto_timer = timing.auto_timer(type(self).__name__)
        if self._verbose:
            memreport(comm, "just before calling libmadam.destripe")
        if self._cached:
            # destripe
            outpath = ""
            if "path_output" in self.params:
                outpath = self.params["path_output"]
            outpath = outpath.encode("ascii")
            madam.destripe_with_cache(
                comm,
                self._madam_timestamps,
                self._madam_pixels,
                self._madam_pixweights,
                self._madam_signal,
                outpath,
            )
        else:
            (detweights, npsd, psdstarts, psdfreqs, psdvals) = psdinfo

            # destripe
            madam.destripe(
                comm,
                pars,
                dets,
                detweights,
                self._madam_timestamps,
                self._madam_pixels,
                self._madam_pixweights,
                self._madam_signal,
                periods,
                npsd,
                psdstarts,
                psdfreqs,
                psdvals,
            )

            if self._mcmode:
                self._cached = True
        return
class MadamMCTest(TestCase):

    np.random.seed(9876543)
    comm = MPI.COMM_WORLD
    itask = comm.Get_rank()
    ntask = comm.Get_size()

    try:
        import healpy as hp
    except:
        hp = None
        if itask == 0:
            print(
                "Warning: unable to import healpy. Output maps are not checked."
            )

    if itask == 0:
        print("Running with ", ntask, " MPI tasks")

    print("Calling Madam")

    nside = 8
    npix = 12 * nside**2
    fsample = 32.5

    pars = {}
    pars["base_first"] = 1.0
    pars["nsubchunk"] = 1
    pars["fsample"] = fsample
    pars["nside_map"] = nside
    pars["nside_cross"] = nside
    pars["nside_submap"] = nside
    pars["write_map"] = True
    pars["write_binmap"] = True
    pars["write_matrix"] = True
    pars["write_wcov"] = True
    pars["write_hits"] = True
    pars["write_leakmatrix"] = True
    pars["kfilter"] = True
    pars["file_root"] = "madam_pytest"
    pars["path_output"] = "./mc_maps/"
    pars["mcmode"] = False
    pars["info"] = 0

    if itask == 0:
        shutil.rmtree("mc_maps", ignore_errors=True)
        os.mkdir("mc_maps")
        for fn in [
                "mc_maps/madam_pytest_hmap.fits",
                "mc_maps/madam_pytest_bmap.fits"
        ]:
            if os.path.isfile(fn):
                os.remove(fn)

    dets = ["LFI27M", "LFI27S", "LFI28M", "LFI28S"]

    ndet = len(dets)

    weights = np.ones(ndet, dtype="double")

    nsamp = 1000  # number of time ordered data samples

    nnz = 1  # number or non zero pointing weights, typically 3 for IQU

    timestamps = np.zeros(nsamp, dtype=madam.TIMESTAMP_TYPE)
    timestamps[:] = np.arange(nsamp) + itask * nsamp

    pixels = np.zeros(ndet * nsamp, dtype=madam.PIXEL_TYPE)
    pixels[:] = np.arange(len(pixels)) % npix

    pixweights = np.zeros(ndet * nsamp * nnz, dtype=madam.WEIGHT_TYPE)
    pixweights[:] = 1

    signal = np.zeros(ndet * nsamp, dtype=madam.SIGNAL_TYPE)
    signal[:] = pixels
    signal[:] += np.random.randn(nsamp * ndet)

    signal2 = np.zeros(ndet * nsamp, dtype=madam.SIGNAL_TYPE)
    signal2[:] = pixels % 12
    signal2[:] += np.random.randn(nsamp * ndet)

    nperiod = 4  # number of pointing periods

    periods = np.zeros(nperiod, dtype=np.int64)
    periods[1] = int(nsamp * 0.25)
    periods[2] = int(nsamp * 0.50)
    periods[3] = int(nsamp * 0.75)

    npsd = np.ones(ndet, dtype=np.int64)
    npsdtot = np.sum(npsd)
    psdstarts = np.zeros(npsdtot)
    npsdbin = 10
    psdfreqs = np.arange(npsdbin) * fsample / npsdbin
    npsdval = npsdbin * npsdtot
    psdvals = np.ones(npsdval)

    signal_copy = signal.copy()

    # Reference maps for checking

    hmap = np.zeros(npix, dtype=np.int64)
    bmap = np.zeros(npix, dtype=np.float64)

    for p, s in zip(pixels, signal):
        hmap[p] += 1
        bmap[p] += s

    hmap_tot = np.zeros(npix, dtype=np.int64)
    bmap_tot = np.zeros(npix, dtype=np.float64)

    comm.Reduce(hmap, hmap_tot, op=MPI.SUM, root=0)
    comm.Reduce(bmap, bmap_tot, op=MPI.SUM, root=0)

    # Basic Madam call with signal # 1

    madam.destripe(
        comm,
        pars,
        dets,
        weights,
        timestamps,
        pixels,
        pixweights,
        signal,
        periods,
        npsd,
        psdstarts,
        psdfreqs,
        psdvals,
    )

    pars["mcmode"] = True
    signal[:] = signal2

    # Basic Madam call with signal # 2, this time, cache the parameters, filters e.t.c.

    madam.destripe(
        comm,
        pars,
        dets,
        weights,
        timestamps,
        pixels,
        pixweights,
        signal,
        periods,
        npsd,
        psdstarts,
        psdfreqs,
        psdvals,
    )

    signal[:] = signal_copy

    # MC Madam call again with signal # 1, This should produce the same result as the first call.

    outpath = "./mc_maps/2/"
    if itask == 0:
        if not os.path.isdir(outpath):
            os.mkdir(outpath)
            for fn in [
                    "mc_maps/2/madam_pytest_hmap.fits",
                    "mc_maps/2/madam_pytest_bmap.fits",
            ]:
                if os.path.isfile(fn):
                    os.remove(fn)
    outpath = outpath.encode("ascii")

    madam.destripe_with_cache(comm, timestamps, pixels, pixweights, signal,
                              outpath)

    # Test clearing the caches twice

    madam.clear_caches()

    madam.clear_caches()

    if itask == 0 and hp is not None:
        good = hmap_tot != 0
        bmap_tot[good] /= hmap_tot[good]

        hmap = hmap_tot.astype(np.int32)
        bmap = bmap_tot.astype(np.float32)

        # hp.write_map("hits.fits", hmap, nest=True, overwrite=True)
        # hp.write_map("binned.fits", bmap, nest=True, overwrite=True)

        madam_hmap = hp.read_map("mc_maps/madam_pytest_hmap.fits", nest=True)
        madam_bmap = hp.read_map("mc_maps/madam_pytest_bmap.fits", nest=True)

        npt.assert_allclose(madam_hmap, hmap)
        npt.assert_allclose(madam_bmap, bmap)

    if itask == 0:
        shutil.rmtree("mc_maps")

    print("Done")
Exemple #4
0
class MadamTest(TestCase):

    np.random.seed(123456)
    comm = MPI.COMM_WORLD
    itask = comm.Get_rank()
    ntask = comm.Get_size()

    try:
        import healpy as hp
    except:
        hp = None
        if itask == 0:
            print(
                "Warning: unable to import healpy. Output maps are not checked."
            )

    if itask == 0:
        print("Running with ", ntask, " MPI tasks")

    print("Calling Madam")

    nside = 8
    npix = 12 * nside**2
    fsample = 32.5
    nsamp = 1000  # number of time ordered data samples
    nnz = 1  # number or non zero pointing weights, typically 3 for IQU

    pars = {}
    pars["base_first"] = 1.0
    pars["fsample"] = fsample
    pars["nside_map"] = nside
    pars["nside_cross"] = nside // 2
    pars["nside_submap"] = nside // 4
    pars["write_map"] = True
    pars["write_binmap"] = True
    pars["write_matrix"] = True
    pars["write_wcov"] = True
    pars["write_hits"] = True
    pars["write_leakmatrix"] = True
    pars["kfilter"] = True
    pars["diagfilter"] = 0
    pars["file_root"] = "madam_pytest"
    pars["path_output"] = "./pymaps/"
    pars["iter_max"] = 100
    pars["nsubchunk"] = 2
    pars["allreduce"] = True
    pars["info"] = 0

    # pars[ 'detset' ] = ['LFI27 : LFI27M, LFI27S',
    #                    'LFI28 : LFI28M, LFI28S']
    pars["survey"] = [
        "hm1 : {} - {}".format(0, nsamp / 2),
        #'hm2 : {} - {}'.format(nsamp/2, nsamp),
        #'odd : {} - {}, {} - {}'.format(0, nsamp/4, nsamp/2, 3*nsamp/4),
        #'even : {} - {}, {} - {}'.format(nsamp/4, nsamp/2, 3*nsamp/4, nsamp)
    ]
    pars["bin_subsets"] = True

    if itask == 0:
        shutil.rmtree("pymaps", ignore_errors=True)
        os.mkdir("pymaps")
        for fn in [
                "pymaps/madam_pytest_hmap.fits",
                "pymaps/madam_pytest_bmap.fits"
        ]:
            if os.path.isfile(fn):
                print("Removing old {}".format(fn))
                os.remove(fn)

    dets = ["LFI27M", "LFI27S", "LFI28M", "LFI28S"]

    ndet = len(dets)

    weights = np.ones(ndet, dtype=np.float64)

    timestamps = np.zeros(nsamp, dtype=madam.TIMESTAMP_TYPE)
    timestamps[:] = np.arange(nsamp) + itask * nsamp

    pixels = np.zeros(ndet * nsamp, dtype=madam.PIXEL_TYPE)
    pixels[:] = np.arange(len(pixels)) % npix

    pixweights = np.zeros(ndet * nsamp * nnz, dtype=madam.WEIGHT_TYPE)
    pixweights[:] = 1

    signal = np.zeros(ndet * nsamp, dtype=madam.SIGNAL_TYPE)
    signal[:] = pixels
    signal[:] += np.random.randn(nsamp * ndet)

    nperiod = 4  # number of pointing periods

    periods = np.zeros(nperiod, dtype=np.int64)
    periods[1] = int(nsamp * 0.25)
    periods[2] = int(nsamp * 0.50)
    periods[3] = int(nsamp * 0.75)

    npsd = np.ones(ndet, dtype=np.int64)
    npsdtot = np.sum(npsd)
    psdstarts = np.zeros(npsdtot)
    npsdbin = 10
    psdfreqs = np.arange(npsdbin) * fsample / npsdbin
    npsdval = npsdbin * npsdtot
    psdvals = np.ones(npsdval)

    # Reference maps for checking

    hmap = np.zeros(npix, dtype=np.int64)
    bmap = np.zeros(npix, dtype=np.float64)

    for p, s in zip(pixels, signal):
        hmap[p] += 1
        bmap[p] += s

    hmap_tot = np.zeros(npix, dtype=np.int64)
    bmap_tot = np.zeros(npix, dtype=np.float64)

    comm.Reduce(hmap, hmap_tot, op=MPI.SUM, root=0)
    comm.Reduce(bmap, bmap_tot, op=MPI.SUM, root=0)

    # Ensure we can successfully call Madam twice with different inputs
    for i in range(2):
        madam.destripe(
            comm,
            pars,
            dets,
            weights,
            timestamps,
            pixels,
            pixweights,
            signal,
            periods,
            npsd,
            psdstarts,
            psdfreqs,
            psdvals,
        )
        nside *= 2
        pars["nside_map"] = nside

    if itask == 0 and hp is not None:
        good = hmap_tot != 0
        bmap_tot[good] /= hmap_tot[good]

        hmap = hmap_tot.astype(np.int32)
        bmap = bmap_tot.astype(np.float32)

        # hp.write_map("hits.fits", hmap, nest=True, overwrite=True)
        # hp.write_map("binned.fits", bmap, nest=True, overwrite=True)

        madam_hmap = hp.read_map("pymaps/madam_pytest_hmap.fits", nest=True)
        madam_bmap = hp.read_map("pymaps/madam_pytest_bmap.fits", nest=True)

        npt.assert_allclose(madam_hmap, hmap)
        npt.assert_allclose(madam_bmap, bmap)

        madam_hmap1 = hp.read_map("pymaps/madam_pytest_hmap_sub1of2.fits",
                                  nest=True)
        madam_hmap2 = hp.read_map("pymaps/madam_pytest_hmap_sub2of2.fits",
                                  nest=True)

        npt.assert_allclose(madam_hmap, madam_hmap1 + madam_hmap2)

    if itask == 0:
        shutil.rmtree("pymaps")

    print("Done")