Пример #1
0
def test_invalid_slices():
    with pytest.raises(IndexError):
        SlicedLowLevelWCS(WCS_SPECTRAL_CUBE, [None, None, [False, False, False]])

    with pytest.raises(IndexError):
        SlicedLowLevelWCS(WCS_SPECTRAL_CUBE, [None, None, slice(None, None, 2)])

    with pytest.raises(IndexError):
        SlicedLowLevelWCS(WCS_SPECTRAL_CUBE, [None, None, 1000.100])
Пример #2
0
def test_nested_slicing():
    # Make sure that if we call slicing several times, the result is the same
    # as calling the slicing once with the final slice settings.
    wcs = WCS_SPECTRAL_CUBE
    sub1 = SlicedLowLevelWCS(
                SlicedLowLevelWCS(
                    SlicedLowLevelWCS(wcs, [slice(None), slice(1, 10), slice(None)]),
                    [3, slice(2, None)]),
                [slice(None), slice(2, 8)])
    sub2 = wcs[3, 3:10, 2:8]
    assert_allclose(sub1.pixel_to_world_values(3, 5),
                    sub2.pixel_to_world_values(3, 5))
    assert not isinstance(sub1._wcs, SlicedLowLevelWCS)
Пример #3
0
def test_dropped_dimensions_4d(cube_4d_fitswcs):

    sub = SlicedLowLevelWCS(cube_4d_fitswcs, np.s_[:, 12, 5, 5])

    dwd = sub.dropped_world_dimensions
    wao_classes = dwd.pop("world_axis_object_classes")
    wao_components = dwd.pop("world_axis_object_components")

    validate_info_dict(
        dwd, {
            "value": [4.e+00, -2.e+00, 1.e+10],
            "world_axis_physical_types":
            ["pos.eq.ra", "pos.eq.dec", "em.freq"],
            "world_axis_names":
            ['Right Ascension', 'Declination', 'Frequency'],
            "world_axis_units": ["deg", "deg", "Hz"],
            "serialized_classes": False,
        })

    assert wao_classes['celestial'][0] is SkyCoord
    assert wao_classes['celestial'][1] == ()
    assert isinstance(wao_classes['celestial'][2]['frame'], ICRS)
    assert wao_classes['celestial'][2]['unit'] is u.deg
    assert wao_classes['spectral'][0:3] == (u.Quantity, (), {})

    assert wao_components[0] == ('celestial', 0, 'spherical.lon.degree')
    assert wao_components[1] == ('celestial', 1, 'spherical.lat.degree')
    assert wao_components[2][0:2] == ('spectral', 0)

    sub = SlicedLowLevelWCS(cube_4d_fitswcs, np.s_[12, 12])

    dwd = sub.dropped_world_dimensions
    wao_classes = dwd.pop("world_axis_object_classes")
    wao_components = dwd.pop("world_axis_object_components")
    validate_info_dict(
        dwd, {
            "value": [1.e+10, 5.e+00],
            "world_axis_physical_types": ["em.freq", "time"],
            "world_axis_names": ["Frequency", "Time"],
            "world_axis_units": ["Hz", "s"],
            "serialized_classes": False,
        })
    assert wao_components[0][0:2] == ('spectral', 0)
    assert wao_components[1][0] == 'time'
    assert wao_components[1][1] == 0

    assert wao_classes['spectral'][0:3] == (u.Quantity, (), {})
    assert wao_classes['time'][0:3] == (Time, (), {})
Пример #4
0
def test_ellipsis_none_types():

    wcs = SlicedLowLevelWCS(WCS_SPECTRAL_CUBE_NONE_TYPES, Ellipsis)

    assert wcs.pixel_n_dim == 3
    assert wcs.world_n_dim == 3
    assert wcs.array_shape == (30, 20, 10)
    assert wcs.pixel_shape == (10, 20, 30)
    assert wcs.world_axis_physical_types == ['pos.galactic.lat', None, 'pos.galactic.lon']
    assert wcs.world_axis_units == ['deg', 'Hz', 'deg']

    assert_equal(wcs.axis_correlation_matrix, [[True, False, True],
                                               [False, True, False], [True, False, True]])

    assert wcs.world_axis_object_components == [('celestial', 1, 'spherical.lat.degree'),
                                                  ('world', 0, 'value'),
                                                  ('celestial', 0, 'spherical.lon.degree')]

    assert wcs.world_axis_object_classes['celestial'][0] is SkyCoord
    assert wcs.world_axis_object_classes['celestial'][1] == ()
    assert isinstance(wcs.world_axis_object_classes['celestial'][2]['frame'], Galactic)
    assert wcs.world_axis_object_classes['celestial'][2]['unit'] is u.deg

    assert_allclose(wcs.pixel_to_world_values(29, 39, 44), (10, 20, 25))
    assert_allclose(wcs.array_index_to_world_values(44, 39, 29), (10, 20, 25))

    assert_allclose(wcs.world_to_pixel_values(10, 20, 25), (29., 39., 44.))
    assert_equal(wcs.world_to_array_index_values(10, 20, 25), (44, 39, 29))

    assert_equal(wcs.pixel_bounds, [(-1, 11), (-2, 18), (5, 15)])

    assert str(wcs) == EXPECTED_ELLIPSIS_REPR_NONE_TYPES.strip()
    assert EXPECTED_ELLIPSIS_REPR_NONE_TYPES.strip() in repr(wcs)
Пример #5
0
def test_no_array_shape():

    wcs = SlicedLowLevelWCS(WCS_NO_SHAPE_CUBE, Ellipsis)

    assert wcs.pixel_n_dim == 3
    assert wcs.world_n_dim == 3
    assert wcs.array_shape is None
    assert wcs.pixel_shape is None
    assert wcs.world_axis_physical_types == ['pos.galactic.lat', 'em.freq', 'pos.galactic.lon']
    assert wcs.world_axis_units == ['deg', 'Hz', 'deg']

    assert_equal(wcs.axis_correlation_matrix, [[True, False, True], [False, True, False], [True, False, True]])

    assert len(wcs.world_axis_object_components) == 3
    assert wcs.world_axis_object_components[0] == ('celestial', 1, 'spherical.lat.degree')
    assert wcs.world_axis_object_components[1][:2] == ('spectral', 0)
    assert wcs.world_axis_object_components[2] == ('celestial', 0, 'spherical.lon.degree')

    assert wcs.world_axis_object_classes['celestial'][0] is SkyCoord
    assert wcs.world_axis_object_classes['celestial'][1] == ()
    assert isinstance(wcs.world_axis_object_classes['celestial'][2]['frame'], Galactic)
    assert wcs.world_axis_object_classes['celestial'][2]['unit'] is u.deg

    assert wcs.world_axis_object_classes['spectral'][0] is Quantity
    assert wcs.world_axis_object_classes['spectral'][1] == ()
    assert wcs.world_axis_object_classes['spectral'][2] == {}

    assert_allclose(wcs.pixel_to_world_values(29, 39, 44), (10, 20, 25))
    assert_allclose(wcs.array_index_to_world_values(44, 39, 29), (10, 20, 25))

    assert_allclose(wcs.world_to_pixel_values(10, 20, 25), (29., 39., 44.))
    assert_equal(wcs.world_to_array_index_values(10, 20, 25), (44, 39, 29))

    assert str(wcs) == EXPECTED_NO_SHAPE_REPR.strip()
    assert EXPECTED_NO_SHAPE_REPR.strip() in repr(wcs)
Пример #6
0
def test_spectral_slice():

    wcs = SlicedLowLevelWCS(WCS_SPECTRAL_CUBE, [slice(None), 10])

    assert wcs.pixel_n_dim == 2
    assert wcs.world_n_dim == 2
    assert wcs.array_shape == (30, 10)
    assert wcs.pixel_shape == (10, 30)
    assert wcs.world_axis_physical_types == ['pos.galactic.lat', 'pos.galactic.lon']
    assert wcs.world_axis_units == ['deg', 'deg']
    assert wcs.pixel_axis_names == ['', '']
    assert wcs.world_axis_names == ['Latitude', 'Longitude']

    assert_equal(wcs.axis_correlation_matrix, [[True, True], [True, True]])

    assert wcs.world_axis_object_components == [('celestial', 1, 'spherical.lat.degree'),
                                                ('celestial', 0, 'spherical.lon.degree')]

    assert wcs.world_axis_object_classes['celestial'][0] is SkyCoord
    assert wcs.world_axis_object_classes['celestial'][1] == ()
    assert isinstance(wcs.world_axis_object_classes['celestial'][2]['frame'], Galactic)
    assert wcs.world_axis_object_classes['celestial'][2]['unit'] is u.deg

    assert_allclose(wcs.pixel_to_world_values(29, 44), (10, 25))
    assert_allclose(wcs.array_index_to_world_values(44, 29), (10, 25))

    assert_allclose(wcs.world_to_pixel_values(10, 25), (29., 44.))
    assert_equal(wcs.world_to_array_index_values(10, 25), (44, 29))

    assert_equal(wcs.pixel_bounds, [(-1, 11), (5, 15)])

    assert str(wcs) == EXPECTED_SPECTRAL_SLICE_REPR.strip()
    assert EXPECTED_SPECTRAL_SLICE_REPR.strip() in repr(wcs)
Пример #7
0
def test_pixel_to_world_values_different_int_types():
    int_sliced = SlicedLowLevelWCS(WCS_SPECTRAL_CUBE, np.s_[:, 0, :])
    np64_sliced = SlicedLowLevelWCS(WCS_SPECTRAL_CUBE, np.s_[:,
                                                             np.int64(0), :])
    pixel_arrays = ([0, 1], [0, 1])
    for int_coord, np64_coord in zip(
            int_sliced.pixel_to_world_values(*pixel_arrays),
            np64_sliced.pixel_to_world_values(*pixel_arrays)):
        assert all(int_coord == np64_coord)
Пример #8
0
def test_celestial_range_rot():

    wcs = SlicedLowLevelWCS(WCS_SPECTRAL_CUBE_ROT, [Ellipsis, slice(5, 10)])

    assert wcs.pixel_n_dim == 3
    assert wcs.world_n_dim == 3
    assert wcs.array_shape == (30, 20, 5)
    assert wcs.pixel_shape == (5, 20, 30)
    assert wcs.world_axis_physical_types == [
        'pos.galactic.lat', 'em.freq', 'pos.galactic.lon'
    ]
    assert wcs.world_axis_units == ['deg', 'Hz', 'deg']
    assert wcs.pixel_axis_names == ['', '', '']
    assert wcs.world_axis_names == ['Latitude', 'Frequency', 'Longitude']

    assert_equal(
        wcs.axis_correlation_matrix,
        [[True, False, True], [False, True, False], [True, False, True]])

    assert len(wcs.world_axis_object_components) == 3
    assert wcs.world_axis_object_components[0] == ('celestial', 1,
                                                   'spherical.lat.degree')
    assert wcs.world_axis_object_components[1][:2] == ('spectral', 0)
    assert wcs.world_axis_object_components[2] == ('celestial', 0,
                                                   'spherical.lon.degree')

    assert wcs.world_axis_object_classes['celestial'][0] is SkyCoord
    assert wcs.world_axis_object_classes['celestial'][1] == ()
    assert isinstance(wcs.world_axis_object_classes['celestial'][2]['frame'],
                      Galactic)
    assert wcs.world_axis_object_classes['celestial'][2]['unit'] is u.deg

    assert wcs.world_axis_object_classes['spectral'][0] is Quantity
    assert wcs.world_axis_object_classes['spectral'][1] == ()
    assert wcs.world_axis_object_classes['spectral'][2] == {}

    assert_allclose(wcs.pixel_to_world_values(14, 29, 34), (1, 15, 24))
    assert_allclose(wcs.array_index_to_world_values(34, 29, 14), (1, 15, 24))

    assert_allclose(wcs.world_to_pixel_values(1, 15, 24), (14., 29., 34.))
    assert_equal(wcs.world_to_array_index_values(1, 15, 24), (34, 29, 14))

    assert_equal(wcs.pixel_bounds, [(-6, 6), (-2, 18), (5, 15)])

    assert str(wcs) == EXPECTED_CELESTIAL_RANGE_ROT_REPR.strip()
    assert EXPECTED_CELESTIAL_RANGE_ROT_REPR.strip() in repr(wcs)
Пример #9
0
def test_dropped_dimensions():
    wcs = WCS_SPECTRAL_CUBE

    sub = SlicedLowLevelWCS(wcs, np.s_[:, :, :])

    assert sub.dropped_world_dimensions == {}

    sub = SlicedLowLevelWCS(wcs, np.s_[:, 2:5, :])

    assert sub.dropped_world_dimensions == {}

    sub = SlicedLowLevelWCS(wcs, np.s_[:, 0])

    waocomp = sub.dropped_world_dimensions.pop("world_axis_object_components")
    assert len(
        waocomp) == 1 and waocomp[0][0] == "spectral" and waocomp[0][1] == 0
    waocls = sub.dropped_world_dimensions.pop("world_axis_object_classes")
    assert len(waocls) == 1 and "spectral" in waocls and waocls["spectral"][
        0] == u.Quantity
    validate_info_dict(
        sub.dropped_world_dimensions, {
            "value": [0.5],
            "world_axis_physical_types": ["em.freq"],
            "world_axis_names": ["Frequency"],
            "world_axis_units": ["Hz"],
            "serialized_classes": False,
        })

    sub = SlicedLowLevelWCS(wcs, np.s_[:, 0, 0])

    waocomp = sub.dropped_world_dimensions.pop("world_axis_object_components")
    assert len(
        waocomp) == 1 and waocomp[0][0] == "spectral" and waocomp[0][1] == 0
    waocls = sub.dropped_world_dimensions.pop("world_axis_object_classes")
    assert len(waocls) == 1 and "spectral" in waocls and waocls["spectral"][
        0] == u.Quantity
    validate_info_dict(
        sub.dropped_world_dimensions, {
            "value": [0.5],
            "world_axis_physical_types": ["em.freq"],
            "world_axis_names": ["Frequency"],
            "world_axis_units": ["Hz"],
            "serialized_classes": False,
        })

    sub = SlicedLowLevelWCS(wcs, np.s_[0, :, 0])

    dwd = sub.dropped_world_dimensions
    wao_classes = dwd.pop("world_axis_object_classes")
    validate_info_dict(
        dwd, {
            "value": [12.86995801, 20.49217541],
            "world_axis_physical_types":
            ["pos.galactic.lat", "pos.galactic.lon"],
            "world_axis_names": ["Latitude", "Longitude"],
            "world_axis_units": ["deg", "deg"],
            "serialized_classes":
            False,
            "world_axis_object_components": [
                ('celestial', 1, 'spherical.lat.degree'),
                ('celestial', 0, 'spherical.lon.degree')
            ],
        })

    assert wao_classes['celestial'][0] is SkyCoord
    assert wao_classes['celestial'][1] == ()
    assert isinstance(wao_classes['celestial'][2]['frame'], Galactic)
    assert wao_classes['celestial'][2]['unit'] is u.deg

    sub = SlicedLowLevelWCS(wcs, np.s_[5, :5, 12])

    dwd = sub.dropped_world_dimensions
    wao_classes = dwd.pop("world_axis_object_classes")
    validate_info_dict(
        dwd, {
            "value": [11.67648267, 21.01921192],
            "world_axis_physical_types":
            ["pos.galactic.lat", "pos.galactic.lon"],
            "world_axis_names": ["Latitude", "Longitude"],
            "world_axis_units": ["deg", "deg"],
            "serialized_classes":
            False,
            "world_axis_object_components": [
                ('celestial', 1, 'spherical.lat.degree'),
                ('celestial', 0, 'spherical.lon.degree')
            ],
        })

    assert wao_classes['celestial'][0] is SkyCoord
    assert wao_classes['celestial'][1] == ()
    assert isinstance(wao_classes['celestial'][2]['frame'], Galactic)
    assert wao_classes['celestial'][2]['unit'] is u.deg
Пример #10
0
def test_1d_sliced_low_level(time_1d_wcs):
    sll = SlicedLowLevelWCS(time_1d_wcs, np.s_[10:20])
    world = sll.pixel_to_world_values([1, 2])
    assert isinstance(world, np.ndarray)
    assert np.allclose(world, [27, 29])
Пример #11
0
def test_world_to_pixel_broadcasting():
    wcs = SlicedLowLevelWCS(WCS_SPECTRAL_CUBE, Ellipsis)

    assert_allclose(wcs.world_to_pixel_values((10, 10), 20, 25),
                    ((29., 29.), (39., 39.), (44., 44.)))
Пример #12
0
def test_pixel_to_world_broadcasting():
    wcs = SlicedLowLevelWCS(WCS_SPECTRAL_CUBE, Ellipsis)

    assert_allclose(wcs.pixel_to_world_values((29, 29), 39, 44),
                    ((10, 10), (20, 20), (25, 25)))