コード例 #1
0
ファイル: field.py プロジェクト: DanielLenz/NaMaster
    def __init__(
        self,
        lx,
        ly,
        mask,
        maps,
        templates=None,
        beam=None,
        purify_e=False,
        purify_b=False,
        tol_pinv=1E-10,
    ):
        self.fl = None

        pure_e = 0
        if purify_e:
            pure_e = 1
        pure_b = 0
        if purify_b:
            pure_b = 1

        if (lx < 0) or (ly < 0):
            raise ValueError(
                "Must supply sensible dimensions for flat-sky field")
        # Flatten arrays and check dimensions
        shape_2D = np.shape(mask)
        nmaps = len(maps)
        self.ny = shape_2D[0]
        self.nx = shape_2D[1]
        if (nmaps != 1) and (nmaps != 2):
            raise ValueError("Must supply 1 or 2 maps per field")

        # Flatten mask
        msk = (mask.astype(np.float64)).flatten()

        # Flatten maps
        mps = []
        for m in maps:
            if np.shape(m) != shape_2D:
                raise ValueError("Mask and maps don't have the same shape")
            mps.append((m.astype(np.float64)).flatten())
        mps = np.array(mps)

        # Flatten templates
        if isinstance(templates, (list, tuple, np.ndarray)):
            tmps = []
            for t in templates:
                tmp = []
                if len(t) != nmaps:
                    raise ValueError(
                        "Maps and templates should have the same number of maps"
                    )
                for m in t:
                    if np.shape(m) != shape_2D:
                        raise ValueError(
                            "Mask and templates don't have the same shape")
                    tmp.append((m.astype(np.float64)).flatten())
                tmps.append(tmp)
            tmps = np.array(tmps)
        else:
            if templates is not None:
                raise ValueError(
                    "Input templates can only be an array or None")

        # Form beam
        if isinstance(beam, (list, tuple, np.ndarray)):
            beam_use = beam
        else:
            if beam is None:
                beam_use = np.array([[-1.], [-1.]])
            else:
                raise ValueError("Input beam can only be an array or None")

        # Generate field
        if isinstance(templates, (list, tuple, np.ndarray)):
            self.fl = lib.field_alloc_new_flat(
                self.nx,
                self.ny,
                lx,
                ly,
                msk,
                mps,
                tmps,
                beam_use,
                pure_e,
                pure_b,
                tol_pinv,
            )
        else:
            self.fl = lib.field_alloc_new_notemp_flat(self.nx, self.ny, lx, ly,
                                                      msk, mps, beam_use,
                                                      pure_e, pure_b)
コード例 #2
0
    def __init__(self,
                 lx,
                 ly,
                 mask,
                 maps,
                 spin=None,
                 templates=None,
                 beam=None,
                 purify_e=False,
                 purify_b=False,
                 tol_pinv=1E-10,
                 masked_on_input=False,
                 lite=False):
        self.fl = None

        pure_e = 0
        if purify_e:
            pure_e = 1
        pure_b = 0
        if purify_b:
            pure_b = 1
        masked_input = 0
        if masked_on_input:
            masked_input = 1

        if (lx < 0) or (ly < 0):
            raise ValueError("Must supply sensible dimensions for "
                             "flat-sky field")
        # Flatten arrays and check dimensions
        shape_2D = np.shape(mask)
        self.ny = shape_2D[0]
        self.nx = shape_2D[1]

        if maps is None:
            mask_only = True
            if spin is None:
                raise ValueError("Please supply field spin")
            lite = True
        else:
            mask_only = False

            nmaps = len(maps)
            if (nmaps != 1) and (nmaps != 2):
                raise ValueError("Must supply 1 or 2 maps per field")

            if spin is None:
                if nmaps == 1:
                    spin = 0
                else:
                    spin = 2
            else:
                if (((spin != 0) and nmaps == 1)
                        or ((spin == 0) and nmaps != 1)):
                    raise ValueError("Spin-zero fields are "
                                     "associated with a single map")

        if (pure_e or pure_b) and spin != 2:
            raise ValueError("Purification only implemented for spin-2 fields")

        # Flatten mask
        msk = (mask.astype(np.float64)).flatten()

        if (not mask_only):
            # Flatten maps
            mps = []
            for m in maps:
                if np.shape(m) != shape_2D:
                    raise ValueError("Mask and maps don't have the same shape")
                mps.append((m.astype(np.float64)).flatten())
            mps = np.array(mps)

        # Flatten templates
        if isinstance(templates, (list, tuple, np.ndarray)):
            tmps = []
            for t in templates:
                tmp = []
                if len(t) != nmaps:
                    raise ValueError("Maps and templates should have the "
                                     "same number of maps")
                for m in t:
                    if np.shape(m) != shape_2D:
                        raise ValueError("Mask and templates don't have "
                                         "the same shape")
                    tmp.append((m.astype(np.float64)).flatten())
                tmps.append(tmp)
            tmps = np.array(tmps)
        else:
            if templates is not None:
                raise ValueError("Input templates can only be an array "
                                 "or None")

        # Form beam
        if isinstance(beam, (list, tuple, np.ndarray)):
            beam_use = beam
        else:
            if beam is None:
                beam_use = np.array([[-1.], [-1.]])
            else:
                raise ValueError("Input beam can only be an array or " "None")

        if mask_only:
            self.fl = lib.field_alloc_empty_flat(self.nx, self.ny, lx, ly,
                                                 spin, msk, beam_use, pure_e,
                                                 pure_b)
        else:
            # Generate field
            if isinstance(templates, (list, tuple, np.ndarray)):
                self.fl = lib.field_alloc_new_flat(self.nx, self.ny, lx, ly,
                                                   spin, msk, mps, tmps,
                                                   beam_use, pure_e, pure_b,
                                                   tol_pinv, masked_input,
                                                   int(lite))
            else:
                self.fl = lib.field_alloc_new_notemp_flat(
                    self.nx, self.ny, lx, ly, spin, msk, mps, beam_use, pure_e,
                    pure_b, masked_input, int(lite))
        self.lite = lite