Example #1
0
def test_rotation_handedness():
    result = np.empty(3)

    lbs.rotate_vector(result, *lbs.quat_rotation_x(np.pi / 2), y)
    assert np.allclose(result, z)
    lbs.rotate_vector(result, *lbs.quat_rotation_x(np.pi / 2), z)
    assert np.allclose(result, -y)
    lbs.rotate_vector(result, *lbs.quat_rotation_y(np.pi / 2), x)
    assert np.allclose(result, -z)
    lbs.rotate_vector(result, *lbs.quat_rotation_y(np.pi / 2), z)
    assert np.allclose(result, x)
    lbs.rotate_vector(result, *lbs.quat_rotation_z(np.pi / 2), x)
    assert np.allclose(result, y)
    lbs.rotate_vector(result, *lbs.quat_rotation_z(np.pi / 2), y)
    assert np.allclose(result, -x)
Example #2
0
def test_spin_to_ecliptic():
    result = np.empty(4)

    lbs.spin_to_ecliptic(
        result=result,
        sun_earth_angle_rad=0.0,
        spin_sun_angle_rad=0.0,
        precession_rate_hz=0.0,
        spin_rate_hz=0.0,
        time_s=0.0,
    )
    assert np.allclose(result, np.array(lbs.quat_rotation_y(np.pi / 2)))

    for sun_earth_angle_rad in (0.1, np.pi / 2, -0.1):
        lbs.spin_to_ecliptic(
            result=result,
            sun_earth_angle_rad=sun_earth_angle_rad,
            spin_sun_angle_rad=0.0,
            precession_rate_hz=0.0,
            spin_rate_hz=0.0,
            time_s=0.0,
        )
        expected = np.array(lbs.quat_rotation_y(np.pi / 2))
        lbs.quat_left_multiply(expected,
                               *lbs.quat_rotation_z(sun_earth_angle_rad))
        assert np.allclose(result, expected)
Example #3
0
def test_quick_rotations():
    vec = np.empty(3)

    quat = np.array(lbs.quat_rotation_z(np.pi / 2))
    lbs.quat_right_multiply(quat, *lbs.quat_rotation_y(np.pi / 2))
    lbs.rotate_z_vector(vec, *quat)
    assert np.allclose(vec, y)

    quat = np.array(lbs.quat_rotation_x(np.pi / 2))
    lbs.quat_right_multiply(quat, *lbs.quat_rotation_y(np.pi / 2))
    lbs.rotate_x_vector(vec, *quat)
    assert np.allclose(vec, y)

    quat = np.array(lbs.quat_rotation_y(np.pi / 2))
    lbs.quat_right_multiply(quat, *lbs.quat_rotation_x(np.pi / 2))
    lbs.rotate_y_vector(vec, *quat)
    assert np.allclose(vec, x)
Example #4
0
def test_collective_quick_rotations():
    vec = np.empty((1, 3))

    quat = np.array(lbs.quat_rotation_z(np.pi / 2))
    lbs.quat_right_multiply(quat, *lbs.quat_rotation_y(np.pi / 2))
    lbs.all_rotate_z_vectors(vec, quat.reshape(1, 4))
    assert np.allclose(vec, y)

    quat = np.array(lbs.quat_rotation_x(np.pi / 2))
    lbs.quat_right_multiply(quat, *lbs.quat_rotation_y(np.pi / 2))
    lbs.all_rotate_x_vectors(vec, quat.reshape(1, 4))
    assert np.allclose(vec, y)

    quat = np.array(lbs.quat_rotation_y(np.pi / 2))
    lbs.quat_right_multiply(quat, *lbs.quat_rotation_x(np.pi / 2))
    lbs.all_rotate_y_vectors(vec, quat.reshape(1, 4))
    assert np.allclose(vec, x)
Example #5
0
        spin_sun_angle_rad=np.deg2rad(30),  # CORE-specific parameter
        spin_rate_hz=0.5 / 60,  # Ditto
        # We use astropy to convert the period (4 days) in
        # seconds
        precession_rate_hz=1.0 / (4 * u.day).to("s").value,
    ))
instr = lbs.InstrumentInfo(name="core",
                           spin_boresight_angle_rad=np.deg2rad(65))

# We create two detectors, whose polarization angles are separated by π/2
sim.create_observations(
    detectors=[
        lbs.DetectorInfo(name="0A", sampling_rate_hz=10),
        lbs.DetectorInfo(name="0B",
                         sampling_rate_hz=10,
                         quat=lbs.quat_rotation_z(np.pi / 2)),
    ],
    dtype_tod=np.float64,
    n_blocks_time=lbs.MPI_COMM_WORLD.size,
    split_list_over_processes=False,
)

# Generate some white noise
rs = RandomState(MT19937(SeedSequence(123456789)))
for curobs in sim.observations:
    curobs.tod *= 0.0
    curobs.tod += rs.randn(*curobs.tod.shape)

params = lbs.DestriperParameters(nside=16,
                                 return_hit_map=True,
                                 return_binned_map=True,
Example #6
0
def test_destriper(tmp_path):
    sim = lbs.Simulation(base_path=tmp_path / "destriper_output",
                         start_time=0,
                         duration_s=86400.0)

    sim.generate_spin2ecl_quaternions(
        scanning_strategy=lbs.SpinningScanningStrategy(
            spin_sun_angle_rad=np.deg2rad(30),  # CORE-specific parameter
            spin_rate_hz=0.5 / 60,  # Ditto
            # We use astropy to convert the period (4 days) in
            # seconds
            precession_rate_hz=1.0 / (4 * u.day).to("s").value,
        ))
    instr = lbs.InstrumentInfo(name="core",
                               spin_boresight_angle_rad=np.deg2rad(65))
    sim.create_observations(
        detectors=[
            lbs.DetectorInfo(name="0A", sampling_rate_hz=10),
            lbs.DetectorInfo(name="0B",
                             sampling_rate_hz=10,
                             quat=lbs.quat_rotation_z(np.pi / 2)),
        ],
        # num_of_obs_per_detector=lbs.MPI_COMM_WORLD.size,
        dtype_tod=np.float64,
        n_blocks_time=lbs.MPI_COMM_WORLD.size,
        split_list_over_processes=False,
    )

    # Generate some white noise
    rs = RandomState(MT19937(SeedSequence(123456789)))
    for curobs in sim.observations:
        curobs.tod *= 0.0
        curobs.tod += rs.randn(*curobs.tod.shape)

    params = lbs.DestriperParameters(
        nside=16,
        nnz=3,
        baseline_length=100,
        iter_max=10,
        return_hit_map=True,
        return_binned_map=True,
        return_destriped_map=True,
        return_npp=True,
        return_invnpp=True,
        return_rcond=True,
    )

    results = lbs.destripe(sim, instr, params=params)

    ref_map_path = Path(__file__).parent / "destriper_reference"

    hit_map_filename = ref_map_path / "destriper_hit_map.fits.gz"
    # healpy.write_map(hit_map_filename, results.hit_map, dtype="int32", overwrite=True)
    np.testing.assert_allclose(
        results.hit_map,
        healpy.read_map(hit_map_filename, field=None, dtype=np.int32))

    binned_map_filename = ref_map_path / "destriper_binned_map.fits.gz"
    # healpy.write_map(
    #     binned_map_filename,
    #     results.binned_map,
    #     dtype=list((np.float32 for i in range(3))),
    #     overwrite=True,
    # )
    ref_binned = healpy.read_map(binned_map_filename,
                                 field=None,
                                 dtype=list((np.float32 for i in range(3))))
    assert results.binned_map.shape == ref_binned.shape
    np.testing.assert_allclose(results.binned_map,
                               ref_binned,
                               rtol=1e-2,
                               atol=1e-3)

    destriped_map_filename = ref_map_path / "destriper_destriped_map.fits.gz"
    # healpy.write_map(
    #     destriped_map_filename,
    #     results.destriped_map,
    #     dtype=list((np.float32 for i in range(3))),
    #     overwrite=True,
    # )
    ref_destriped = healpy.read_map(destriped_map_filename,
                                    field=None,
                                    dtype=list((np.float32 for i in range(3))))
    assert results.destriped_map.shape == ref_destriped.shape
    np.testing.assert_allclose(results.destriped_map,
                               ref_destriped,
                               rtol=1e-2,
                               atol=1e-3)

    npp_filename = ref_map_path / "destriper_npp.fits.gz"
    # healpy.write_map(
    #     npp_filename,
    #     results.npp,
    #     dtype=list((np.float32 for i in range(6))),
    #     overwrite=True,
    # )
    ref_npp = healpy.read_map(npp_filename,
                              field=None,
                              dtype=list((np.float32 for i in range(6))))
    assert results.npp.shape == ref_npp.shape
    np.testing.assert_allclose(results.npp, ref_npp, rtol=1e-2, atol=1e-3)

    invnpp_filename = ref_map_path / "destriper_invnpp.fits.gz"
    # healpy.write_map(
    #     invnpp_filename,
    #     results.invnpp,
    #     dtype=list((np.float32 for i in range(6))),
    #     overwrite=True,
    # )
    ref_invnpp = healpy.read_map(invnpp_filename,
                                 field=None,
                                 dtype=list((np.float32 for i in range(6))))
    assert results.invnpp.shape == ref_invnpp.shape
    np.testing.assert_allclose(results.invnpp,
                               ref_invnpp,
                               rtol=1e-2,
                               atol=1e-3)

    rcond_filename = ref_map_path / "destriper_rcond.fits.gz"
    # healpy.write_map(
    #     rcond_filename,
    #     results.rcond,
    #     dtype=np.float32,
    #     overwrite=True,
    # )
    assert np.allclose(
        results.rcond,
        healpy.read_map(rcond_filename, field=None, dtype=np.float32))
Example #7
0
def test_quat_multiply_and_rotations():
    # Simple composition of rotations
    quat = np.array(lbs.quat_rotation_x(np.pi / 3))
    lbs.quat_right_multiply(quat, *lbs.quat_rotation_x(np.pi / 3))
    assert np.allclose(quat, np.array(lbs.quat_rotation_x(2 * np.pi / 3)))

    quat = np.array(lbs.quat_rotation_y(np.pi / 3))
    lbs.quat_right_multiply(quat, *lbs.quat_rotation_y(np.pi / 3))
    assert np.allclose(quat, np.array(lbs.quat_rotation_y(2 * np.pi / 3)))

    quat = np.array(lbs.quat_rotation_z(np.pi / 3))
    lbs.quat_right_multiply(quat, *lbs.quat_rotation_z(np.pi / 3))
    assert np.allclose(quat, np.array(lbs.quat_rotation_z(2 * np.pi / 3)))

    quat = np.array(lbs.quat_rotation_x(np.pi / 3))
    lbs.quat_left_multiply(quat, *lbs.quat_rotation_x(np.pi / 3))
    assert np.allclose(quat, np.array(lbs.quat_rotation_x(2 * np.pi / 3)))

    quat = np.array(lbs.quat_rotation_y(np.pi / 3))
    lbs.quat_left_multiply(quat, *lbs.quat_rotation_y(np.pi / 3))
    assert np.allclose(quat, np.array(lbs.quat_rotation_y(2 * np.pi / 3)))

    quat = np.array(lbs.quat_rotation_z(np.pi / 3))
    lbs.quat_left_multiply(quat, *lbs.quat_rotation_z(np.pi / 3))
    assert np.allclose(quat, np.array(lbs.quat_rotation_z(2 * np.pi / 3)))

    # Now we test more complex compositions

    vec = np.empty(3)

    # Right multiplication
    quat = np.array(lbs.quat_rotation_y(np.pi / 2))
    lbs.quat_right_multiply(quat, *lbs.quat_rotation_x(np.pi / 2))
    lbs.rotate_vector(vec, *quat, y)
    assert np.allclose(vec, x)

    quat = np.array(lbs.quat_rotation_z(np.pi / 2))
    lbs.quat_right_multiply(quat, *lbs.quat_rotation_y(np.pi / 2))
    lbs.rotate_vector(vec, *quat, z)
    assert np.allclose(vec, y)

    quat = np.array(lbs.quat_rotation_x(np.pi / 2))
    lbs.quat_right_multiply(quat, *lbs.quat_rotation_y(np.pi / 2))
    lbs.rotate_vector(vec, *quat, x)
    assert np.allclose(vec, y)

    # Left multiplication
    quat = np.array(lbs.quat_rotation_y(np.pi / 2))
    lbs.quat_left_multiply(quat, *lbs.quat_rotation_z(np.pi / 2))
    lbs.rotate_vector(vec, *quat, z)
    assert np.allclose(vec, y)

    quat = np.array(lbs.quat_rotation_z(np.pi / 2))
    lbs.quat_left_multiply(quat, *lbs.quat_rotation_y(np.pi / 2))
    lbs.rotate_vector(vec, *quat, y)
    assert np.allclose(vec, z)

    quat = np.array(lbs.quat_rotation_z(np.pi / 2))
    lbs.quat_left_multiply(quat, *lbs.quat_rotation_x(np.pi / 2))
    lbs.rotate_vector(vec, *quat, x)
    assert np.allclose(vec, z)