Esempio n. 1
0
 def test_400_child(self):
     dets = ['det0', 'det1', 'det2']
     n, ofs = 1000, 0
     aman = core.AxisManager(core.LabelAxis('dets', dets),
                             core.OffsetAxis('samps', n, ofs))
     child = core.AxisManager(core.LabelAxis('dets', dets + ['det3']),
                              core.OffsetAxis('samps', n, ofs - n // 2))
     aman.wrap('child', child)
     self.assertEqual(aman.shape, (3, n // 2))
     self.assertEqual(aman._axes['samps'].offset, ofs)
Esempio n. 2
0
 def test_900_everything(self):
     tod = core.AxisManager(core.LabelAxis('dets', list('abcdef')),
                            core.OffsetAxis('samps', 1000))
     cal = core.AxisManager(core.LabelAxis('dets', list('feghij')))
     cuts = core.AxisManager(core.OffsetAxis('samps', 800, 100))
     tod.wrap(
         'data',
         np.ones(tod.shape, 'float32'),
     )
     cal.wrap('cal', np.linspace(.9, 1.2, 6), [(0, 'dets')])
     cuts.wrap('cuts', np.ones(cuts.shape, 'int32'), [(0, 'samps')])
     tod.merge(cal, cuts)
     self.assertEqual(tod.shape, (2, 800))
Esempio n. 3
0
    def test_120_label(self):
        dets = ['det0', 'det1', 'det2']
        a1 = np.zeros(len(dets))
        a1[1] = 1.
        aman = core.AxisManager(core.LabelAxis('dets', dets))
        aman.wrap('a1', a1, [(0, 'dets')])
        aman.restrict('dets', ['det1'])
        self.assertNotEqual(aman.a1[0], 0.)

        # Don't let people use non string labels
        dets_int = [0, 1, 2]
        with self.assertRaises(TypeError):
            aman = core.AxisManager(core.LabelAxis('dets', dets_int))
Esempio n. 4
0
 def test_410_merge(self):
     dets = ['det0', 'det1', 'det2']
     n, ofs = 1000, 0
     aman = core.AxisManager(core.LabelAxis('dets', dets),
                             core.OffsetAxis('samps', n, ofs))
     coparent = core.AxisManager(
         core.LabelAxis('dets', dets + ['det3']),
         core.OffsetAxis('samps', n, ofs - n//2))\
         .wrap('x', np.arange(n), [(0, 'samps')])
     aman.merge(coparent)
     self.assertEqual(aman.shape, (3, n // 2))
     self.assertEqual(aman._axes['samps'].offset, ofs)
     self.assertEqual(aman.x[0], n // 2)
Esempio n. 5
0
    def test_500_io(self):
        # Test save/load HDF5
        dets = ['det0', 'det1', 'det2']
        n, ofs = 1000, 0
        aman = core.AxisManager(core.LabelAxis('dets', dets),
                                core.OffsetAxis('samps', n, ofs),
                                core.IndexAxis('indexaxis', 12))
        # Make sure this has axes, scalars, a string array ...
        aman.wrap_new('test1', ('dets', 'samps'), dtype='float32')
        aman.wrap_new('flag1',
                      shape=('dets', 'samps'),
                      cls=so3g.proj.RangesMatrix.zeros)
        aman.wrap('scalar', 8)
        aman.wrap('test_str', np.array(['a', 'b', 'cd']))
        aman.wrap('flags', core.FlagManager.for_tod(aman, 'dets', 'samps'))

        aman.wrap('a', np.int32(12))
        aman.wrap('b', np.float32(12.))
        aman.wrap('c', np.str_('twelve'))
        aman.wrap('d', np.bool_(False))

        with tempfile.TemporaryDirectory() as tempdir:
            filename = os.path.join(tempdir, 'test.h5')
            aman.save(filename, 'my_axisman')
            aman2 = aman.load(filename, 'my_axisman')
            shutil.copy(filename, 'debug.h5')
        # This is not a very satisfying comparison ... support for ==
        # should be required for all AxisManager members!
        for k in aman._fields.keys():
            self.assertEqual(aman[k].__class__, aman2[k].__class__)
            if hasattr(aman[k], 'shape'):
                self.assertEqual(aman[k].shape, aman2[k].shape)
            else:
                self.assertEqual(aman[k], aman2[k])  # scalar
Esempio n. 6
0
    def test_160_scalars(self):
        aman = core.AxisManager(core.LabelAxis('dets', ['a', 'b']),
                                core.OffsetAxis('samps', 100))

        # Accept trivially promoted scalars
        aman.wrap('x', 12)
        aman.wrap('z', 'hello')

        # Check that numpy int/float types are unpacked.
        aman.wrap('a', np.int32(12))
        aman.wrap('b', np.float32(12.))
        aman.wrap('c', np.str_('twelve'))
        self.assertNotIsInstance(aman['a'], np.integer)
        self.assertNotIsInstance(aman['b'], np.floating)
        self.assertNotIsInstance(aman['c'], np.str_)

        # Don't let people wrap the same scalar twice
        with self.assertRaises(ValueError):
            aman.wrap('x', 13)

        # Don't just let people wrap any old thing.
        with self.assertRaises(AttributeError):
            aman.wrap('a_dict', {'a': 123})
        with self.assertRaises(ValueError):
            aman.wrap('square_root', 1j)
Esempio n. 7
0
def load_act_cuts(filename, pa=None):
    """Read ACT cuts file and return an AxisManager containing the flags.

    The pa is needed to define the dets axis properly, but if passed
    as None then the code will try to determine it by searching for a
    standard ACT TOD basename in filename.
n
    """
    if pa is None:
        _, _, _, ar = extract_basename(filename)
        pa = 'pa' + ar[-1]

    cuts_in = moby2.tod.TODCuts.from_actpol_cuts_file(filename)
    det_names = ['%s_%04i' % (pa, u) for u in cuts_in.det_uid]

    flags = []
    for d, c in zip(det_names, cuts_in.cuts):
        i = so3g.proj.Ranges.from_array(c + cuts_in.sample_offset, cuts_in.nsamps)
        flags.append(i)
    flags = so3g.proj.RangesMatrix(flags)

    aman = core.AxisManager(
        core.LabelAxis('dets', det_names),
        core.OffsetAxis('samps', cuts_in.nsamps, cuts_in.sample_offset),
    )

    aman.wrap('flags', flags, [(0, 'dets'), (1, 'samps')])
    return aman
Esempio n. 8
0
    def test_10_kernels(self):
        """Test creation of sensible WCS kernels for basic cases."""
        ra0, dec0 = CRVAL
        res = 0.01 * DEG

        # Test zenithal -- (ra0, dec0) is the reference point.
        for proj in ['TAN', 'ZEA']:
            wcsk = coords.get_wcs_kernel(proj, ra0, dec0, res)
            msg = f'Check crpix for {proj}'
            self.assertAlmostEqual(wcsk.wcs.crpix[0], 1, delta=TOL_RAD, msg=msg)
            self.assertAlmostEqual(wcsk.wcs.crpix[1], 1, delta=TOL_RAD, msg=msg)

        # Test cylindrical -- pixell puts the crval[1] on the equator
        # and dec0 is used for the conformal latitude.
        for proj in ['CAR', 'CEA']:
            wcsk = coords.get_wcs_kernel(proj, ra0, dec0, res)
            msg = f'Check crpix for {proj}'
            self.assertAlmostEqual(wcsk.wcs.crpix[0], 1, delta=TOL_RAD, msg=msg)
            self.assertNotAlmostEqual(wcsk.wcs.crpix[1], 1, delta=TOL_RAD, msg=msg)

        # This is going to break.
        fp = FP(xi =[0., -0.01*DEG],
                eta=[0., -0.01*DEG])
        sight = get_sightline()
        tod = core.AxisManager(core.LabelAxis('dets', ['a']))
        fp = coords.get_footprint(tod, wcs_kernel=wcsk, focal_plane=fp, sight=sight)
Esempio n. 9
0
 def test_120_label(self):
     dets = ['det0', 'det1', 'det2']
     a1 = np.zeros(len(dets))
     a1[1] = 1.
     aman = core.AxisManager(core.LabelAxis('dets', dets))
     aman.wrap('a1', a1, [(0, 'dets')])
     aman.restrict('dets', ['det1'])
     self.assertNotEqual(aman.a1[0], 0.)
Esempio n. 10
0
 def test_401_restrict(self):
     # Test AxisManager.restrict when it has AxisManager members.
     dets = ['det0', 'det1', 'det2']
     n, ofs = 1000, 0
     for in_place in [True, False]:
         aman = core.AxisManager(core.LabelAxis('dets', dets),
                                 core.OffsetAxis('samps', n, ofs))
         child = core.AxisManager(aman.dets, aman.samps)
         child2 = core.AxisManager(
             core.LabelAxis('not_dets', ['x', 'y', 'z']))
         aman.wrap('child', child)
         aman.wrap('rebel_child', child2)
         aout = aman.restrict('dets', ['det1'], in_place=in_place)
         msg = f'Note restrict was with in_place={in_place}'
         self.assertTrue(aout is aman or not in_place, msg=msg)
         self.assertEqual(aout['child'].shape, (1, n), msg=msg)
         self.assertIn('rebel_child', aout, msg=msg)
         self.assertEqual(aout['rebel_child'].shape, (3, ), msg=msg)
Esempio n. 11
0
 def test_200_multid(self):
     dets = ['det0', 'det1', 'det2']
     a1 = np.zeros((len(dets), len(dets)))
     a1[2, 2] = 1.
     aman = core.AxisManager(core.LabelAxis('dets', dets))
     aman.wrap('a1', a1, [(0, 'dets'), (1, 'dets')])
     aman.restrict('dets', ['det1', 'det2'])
     self.assertEqual(aman.a1.shape, (2, 2))
     self.assertNotEqual(aman.a1[1, 1], 0.)
Esempio n. 12
0
    def test_140_restrict_axes(self):
        dets = ['det0', 'det1', 'det2']
        a1 = np.zeros((len(dets), 100))
        a1[1, 10] = 1.
        aman = core.AxisManager(core.LabelAxis('dets', dets),
                                core.OffsetAxis('samps', a1.shape[1]))
        aman.wrap('a1', a1, [(0, 'dets'), (1, 'samps')])

        r_axes = {
            'dets': core.LabelAxis('dets', dets[1:2]),
            'samps': core.OffsetAxis('samps', 20, 10)
        }
        # Not-in-place...
        rman = aman.restrict_axes(r_axes, in_place=False)
        self.assertCountEqual(aman.a1.shape, (3, 100))
        self.assertCountEqual(rman.a1.shape, (1, 20))
        self.assertNotEqual(aman.a1[1, 10], 0.)
        self.assertNotEqual(rman.a1[0, 0], 0.)
        # In-place.
        aman.restrict_axes(r_axes, in_place=True)
        self.assertCountEqual(aman.a1.shape, (1, 20))
        self.assertNotEqual(aman.a1[0, 0], 0.)
Esempio n. 13
0
 def test_300_restrict(self):
     dets = ['det0', 'det1', 'det2']
     n, ofs = 1000, 5000
     aman = core.AxisManager(core.LabelAxis('dets', dets),
                             core.OffsetAxis('samps', n, ofs))
     # Super-correlation matrix.
     a1 = np.zeros((len(dets), len(dets), n, n))
     a1[1, 1, 20, 21] = 1.
     aman.wrap('a1', a1, [(0, 'dets'), (1, 'dets'), (2, 'samps'),
                          (3, 'samps')])
     aman.restrict('dets', ['det1']).restrict('samps', (20 + ofs, 30 + ofs))
     self.assertEqual(aman.shape, (1, 10))
     self.assertEqual(aman.a1.shape, (1, 1, 10, 10))
     self.assertNotEqual(aman.a1[0, 0, 0, 1], 0.)
Esempio n. 14
0
 def test_150_wrap_new(self):
     dets = ['det0', 'det1', 'det2']
     a1 = np.zeros((len(dets), 100))
     a1[1, 10] = 1.
     aman = core.AxisManager(core.LabelAxis('dets', dets),
                             core.OffsetAxis('samps', a1.shape[1]))
     x = aman.wrap_new('x', shape=('dets', 'samps'))
     y = aman.wrap_new('y', shape=('dets', 'samps'), dtype='float32')
     self.assertEqual(aman.x.shape, aman.y.shape)
     if hasattr(so3g.proj.RangesMatrix, 'zeros'):
         # Jan 2021 -- some so3g might not have this method yet...
         f = aman.wrap_new('f',
                           shape=('dets', 'samps'),
                           cls=so3g.proj.RangesMatrix.zeros)
         self.assertEqual(aman.x.shape, aman.f.shape)
Esempio n. 15
0
def get_tod(sig_type='trendy'):
    tod = core.AxisManager(core.LabelAxis('dets', ['a', 'b', 'c']),
                           core.IndexAxis('samps', 1000))
    tod.wrap_new('signal', ('dets', 'samps'), dtype='float32')
    tod.wrap_new('timestamps', ('samps', ))[:] = (np.arange(tod.samps.count) /
                                                  SAMPLE_FREQ_HZ)
    if sig_type == 'zero':
        pass
    elif sig_type == 'trendy':
        x = np.linspace(0, 1., tod.samps.count)
        tod.signal[:] = [(i + 1) + (i + 1)**2 * x
                         for i in range(tod.dets.count)]
    elif sig_type == 'white':
        tod.signal = np.random.normal(size=tod.shape)
    elif sig_type == 'red':
        tod.signal = np.random.normal(size=tod.shape)
        tod.signal[:] = np.cumsum(tod.signal, axis=1)
    else:
        raise RuntimeError(f'sig_type={sig_type}?')
    return tod
Esempio n. 16
0
def load_act_cal(filename, pa=None):
    """Read ACT relcal file and return an AxisManager containing the
    info.

    The pa is needed to define the dets axis properly, but if passed
    as None then the code will try to determine it by searching for a
    standard ACT TOD basename in filename.

    """
    if pa is None:
        _, _, _, ar = extract_basename(filename)
        pa = 'pa' + ar[-1]

    cal_in = moby2.Calibration.from_dict(filename)
    det_names = ['%s_%04i' % (pa, u) for u in cal_in.det_uid]

    aman = core.AxisManager(
        core.LabelAxis('dets', det_names),
    )

    aman.wrap('cal', cal_in.cal, [(0, 'dets')])
    return aman
Esempio n. 17
0
def load_act_timeconst(filename, pa=None):
    """Read ACT timeconstants file and return an AxisManager containing
    the info.

    The pa is needed to define the dets axis properly, but if passed
    as None then the code will try to determine it by searching for a
    standard ACT TOD basename in filename.

    """
    if pa is None:
        _, _, _, ar = extract_basename(filename)
        pa = 'pa' + ar[-1]

    db = moby2.util.StructDB.from_column_file(
        filename, [('det_uid', 0), ('tau_s', 1)])
    det_names = ['%s_%04i' % (pa, u) for u in db['det_uid']]

    aman = core.AxisManager(
        core.LabelAxis('dets', det_names),
    )

    aman.wrap('timeconst', db['tau_s'], [(0, 'dets')])
    return aman
Esempio n. 18
0
def load_detoffsets_file(position, polarization, pa=None):
    """Read ACT detector offsets + polarization angles files and return an
    AxisManager.

    """
    def guess_pa(text):
        # Find 'pa?' or 'ar?' in text (perhaps a filename) and return it.
        m = re.search('pa[123456789]', text)
        if m is not None:
            return m.group(0)
        m = re.search('ar[123456789]', text)
        if m is not None:
            return 'pa' + m.group(0)[-1]
        raise ValueError('Could not determine "pa" based on filename; '
                         'pass it in yourself.')

    if pa is None:
        pa = guess_pa(position)

    # Load position data...
    fp = moby2.scripting.products.get_detector_offsets(
        {'format': 'ascii',
         'columns': [('det_uid', 0), ('x', 2), ('y', 4), ('mask', 1)],
         'match_bad': 0,
         'filename': position})
    fp = fp.subset(fp.mask)

    det_names = ['%s_%04i' % (pa, u) for u in fp.det_uid]
    aman = core.AxisManager(core.LabelAxis('dets', det_names))
    aman.wrap('act_x', fp.x, [(0, 'dets')])
    aman.wrap('act_y', fp.y, [(0, 'dets')])
    aman.fp = fp

    if polarization is None:
        aman.wrap('act_pol', np.zeros(len(fp.x)), [(0, 'dets')])
    else:
        cols = moby2.util.ascii.read_columns(polarization)
        mask = cols[1] >= 0

        det_names = ['%s_%04i' % (pa, u) for u in cols[0][mask]]
        pol_ang = cols[1][mask]
        amanp = core.AxisManager(core.LabelAxis('dets', det_names))
        amanp.wrap('act_pol', pol_ang, [(0, 'dets')])
        aman.merge(amanp)

    # The coordinate convention for ACT and SO is ever-so-slightly
    # different.  Here's the libactpol construction:
    #
    #    Quaternion_r3(q, -pol_angle);
    #    Quaternion_r2_mul(focalplane_x, q);
    #    Quaternion_r1_mul(-focalplane_y, q);
    #
    # Note focalplane_{x,y} correspond to moby2.FocalPlane.{y,x}.
    # Also pol_angle is FocalPlane.phi - pi/2.
    #
    # The SO (xi, eta) will differ from (act_x, act_y) by O(x^3 + y^4).
    DEG = np.pi/180
    q = (so3g.proj.quat.euler(0,  aman['act_x']) *
         so3g.proj.quat.euler(1, -aman['act_y']) *
         so3g.proj.quat.euler(2, np.pi/2 - aman['act_pol']*DEG))

    xieta = so3g.proj.quat.decompose_xieta(q)
    for i, k in enumerate(['xi', 'eta', 'gamma']):
        aman.wrap(k, xieta[i], [(0, 'dets')])

    return aman
Esempio n. 19
0
 def test_100_inheritance(self):
     tod = core.AxisManager(core.LabelAxis('dets', list('abcdef')),
                            core.OffsetAxis('samps', 1000))
     flags = core.FlagManager.for_tod(tod, 'dets', 'samps')
     tod.wrap('flags', flags)
     self.assertTrue(type(tod.flags) == core.FlagManager)