Beispiel #1
0
def test_to_ccddata_invalid():

    data = Data(label='not-an-image')
    data.add_component(Component(np.array([3.4, 2.3, -1.1, 0.3]), units='Jy'),
                       'x')

    with pytest.raises(ValueError) as exc:
        data.get_object(CCDData, attribute=data.id['x'])
    assert exc.value.args[
        0] == 'Only 2-dimensional datasets can be converted to CCDData'

    class FakeCoordinates(Coordinates):
        def pixel_to_world_values(self, *pixel):
            raise NotImplementedError()

        def world_to_pixel_values(self, *pixel):
            raise NotImplementedError()

    coords = FakeCoordinates(n_dim=2)
    coords.low_level_wcs = coords

    data = Data(label='image-with-custom-coords', coords=coords)
    data.add_component(Component(np.array([[3, 4], [4, 5]]), units='Jy'), 'x')

    with pytest.raises(TypeError) as exc:
        data.get_object(CCDData, attribute=data.id['x'])
    assert exc.value.args[
        0] == 'data.coords should be an instance of Coordinates or WCS'
def test_to_spectral_cube_missing_wcs():

    data = Data(label='not-a-spectral-cube')
    values = np.random.random((4, 5, 3))
    data.add_component(Component(values, units='Jy'), 'x')

    with pytest.raises(TypeError) as exc:
        data.get_object(SpectralCube, attribute=data.id['x'])
    assert exc.value.args[0] == ('data.coords should be an instance of BaseLowLevelWCS.')
def test_to_spectral_cube_invalid_ndim():

    data = Data(label='not-a-spectral-cube')
    data.add_component(Component(np.array([3.4, 2.3, -1.1, 0.3]), units='Jy'), 'x')

    with pytest.raises(ValueError) as exc:
        data.get_object(SpectralCube, attribute=data.id['x'])
    assert exc.value.args[0] == ('Data object should have 3 or 4 dimensions in order '
                                 'to be converted to a SpectralCube object.')
def test_to_spectrum1d_invalid():

    data = Data(label='not-a-spectrum')
    data.add_component(Component(np.array([3.4, 2.3, -1.1, 0.3]), units='Jy'), 'x')

    with pytest.raises(TypeError) as exc:
        data.get_object(Spectrum1D, attribute=data.id['x'])
    assert exc.value.args[0] == ('data.coords should be an instance of WCS '
                                 'or SpectralCoordinates')
Beispiel #5
0
def test_to_ccddata(with_wcs):

    if with_wcs:
        coords = WCS_CELESTIAL
    else:
        coords = None

    data = Data(label='image', coords=coords)
    data.add_component(
        Component(np.array([[3.4, 2.3], [-1.1, 0.3]]), units='Jy'), 'x')

    image = data.get_object(CCDData, attribute=data.id['x'])

    assert image.wcs is (WCS_CELESTIAL if with_wcs else None)
    assert_allclose(image.data, [[3.4, 2.3], [-1.1, 0.3]])
    assert image.unit is u.Jy

    data.add_subset(data.id['x'] > 1, label='bright')

    image_subset = data.get_subset_object(cls=CCDData,
                                          subset_id=0,
                                          attribute=data.id['x'])

    assert image_subset.wcs is (WCS_CELESTIAL if with_wcs else None)
    assert_allclose(image_subset.data, [[3.4, 2.3], [-1.1, 0.3]])
    assert image_subset.unit is u.Jy
    assert_equal(image_subset.mask, [[0, 0], [1, 1]])
def test_to_spectrum1d():

    # Set up simple spectral WCS
    wcs = WCS(naxis=1)
    wcs.wcs.ctype = ['VELO-LSR']
    wcs.wcs.set()

    coords = WCSCoordinates(wcs=wcs)

    data = Data(label='spectrum', coords=coords)
    data.add_component(Component(np.array([3.4, 2.3, -1.1, 0.3]), units='Jy'),
                       'x')

    spec = data.get_object(Spectrum1D, attribute=data.id['x'])

    assert_quantity_allclose(spec.spectral_axis, [1, 2, 3, 4] * u.m / u.s)
    assert_quantity_allclose(spec.flux, [3.4, 2.3, -1.1, 0.3] * u.Jy)

    data.add_subset(data.id['x'] > 1, label='bright')

    spec_subset = data.get_subset_object(cls=Spectrum1D,
                                         subset_id=0,
                                         attribute=data.id['x'])

    assert_quantity_allclose(spec_subset.spectral_axis,
                             [1, 2, 3, 4] * u.m / u.s)
    assert_quantity_allclose(spec_subset.flux,
                             [3.4, 2.3, np.nan, np.nan] * u.Jy)
    assert_equal(spec_subset.mask, [1, 1, 0, 0])
Beispiel #7
0
def test_to_ccddata_unitless():

    data = Data(label='image', coords=WCS_CELESTIAL)
    data.add_component(Component(np.array([[3.4, 2.3], [-1.1, 0.3]])), 'x')

    image = data.get_object(CCDData, attribute=data.id['x'])

    assert_allclose(image.data, [[3.4, 2.3], [-1.1, 0.3]])
    assert image.unit is u.one
Beispiel #8
0
def test_to_spectral_cube_unitless(spectral_cube_wcs):

    data = Data(label='spectral_cube', coords=spectral_cube_wcs)
    values = np.random.random((4, 5, 3))
    data.add_component(Component(values), 'x')

    spec = data.get_object(SpectralCube, attribute=data.id['x'])

    assert_quantity_allclose(spec.spectral_axis, [1, 2, 3, 4] * u.m / u.s)
    assert_quantity_allclose(spec.filled_data[...], values * u.one)
Beispiel #9
0
def test_translator_from_data():

    # Case where the initial data object wasn't originally created from a
    # DataFrame.

    data = Data()
    data['a'] = [3, 5, 6, 7]
    data['b'] = [1.5, 2.2, 1.3, 3.3]
    data['c'] = ['r', 'd', 'w', 'q']

    with pytest.raises(ValueError) as exc:
        df = data.get_object()
    assert exc.value.args[0] == ('Specify the object class to use with cls= - supported '
                                 'classes are:\n\n* pandas.core.frame.DataFrame')

    df = data.get_object(cls=DataFrame)
    assert_equal(list(df.columns), ['a', 'b', 'c'])
    assert_equal(df['a'].values, [3, 5, 6, 7])
    assert_equal(df['b'].values, [1.5, 2.2, 1.3, 3.3])
    assert_equal(df['c'].values, ['r', 'd', 'w', 'q'])
Beispiel #10
0
def test_to_spectral_cube_invalid_wcs():

    wcs = WCS(naxis=3)

    data = Data(label='not-a-spectral-cube', coords=wcs)
    values = np.random.random((4, 5, 3))
    data.add_component(Component(values, units='Jy'), 'x')

    with pytest.raises(ValueError) as exc:
        data.get_object(SpectralCube, attribute=data.id['x'])
    assert exc.value.args[0] == ('No celestial axes found in WCS')

    wcs.wcs.ctype = ['RA---TAN', 'DEC--TAN', '']

    data = Data(label='not-a-spectral-cube', coords=wcs)
    values = np.random.random((4, 5, 3))
    data.add_component(Component(values, units='Jy'), 'x')

    with pytest.raises(ValueError) as exc:
        data.get_object(SpectralCube, attribute=data.id['x'])
    assert exc.value.args[0] == ('No spectral axes found in WCS')
def test_to_spectrum1d_with_spectral_coordinates():

    coords = SpectralCoordinates([1, 4, 10] * u.micron)

    data = Data(label='spectrum1d', coords=coords)
    data.add_component(Component(np.array([3, 4, 5]), units='Jy'), 'x')

    assert_allclose(data.coords.pixel2world([0, 0.5, 1, 1.5, 2]),
                    [[1, 2.5, 4, 7, 10]])

    spec = data.get_object(Spectrum1D, attribute=data.id['x'])
    assert_quantity_allclose(spec.spectral_axis, [1, 4, 10] * u.micron)
    assert_quantity_allclose(spec.flux, [3, 4, 5] * u.Jy)
def test_to_ccddata_default_attribute():

    data = Data(label='image', coords=WCS_CELESTIAL)

    with pytest.raises(ValueError) as exc:
        data.get_object(CCDData)
    assert exc.value.args[0] == 'Data object has no attributes.'

    data.add_component(Component(np.array([[3, 4], [5, 6]]), units='Jy'), 'x')

    image = data.get_object(CCDData)
    assert_allclose(image.data, [[3, 4], [5, 6]])
    assert image.unit is u.Jy

    data.add_component(Component(np.array([[3, 4], [5, 6]]), units='Jy'), 'y')

    with pytest.raises(ValueError) as exc:
        data.get_object(CCDData)
    assert exc.value.args[0] == ('Data object has more than one attribute, so '
                                 'you will need to specify which one to use as '
                                 'the flux for the spectrum using the attribute= '
                                 'keyword argument.')
def test_to_spectral_cube_default_attribute(spectral_cube_wcs):

    data = Data(label='spectral_cube', coords=spectral_cube_wcs)
    values = np.random.random((4, 5, 3))

    with pytest.raises(ValueError) as exc:
        data.get_object(SpectralCube)
    assert exc.value.args[0] == 'Data object has no attributes.'

    data.add_component(Component(values, units='Jy'), 'x')

    spec = data.get_object(SpectralCube)
    assert_quantity_allclose(spec.filled_data[...], values * u.Jy)

    data.add_component(Component(values, units='Jy'), 'y')

    with pytest.raises(ValueError) as exc:
        data.get_object(SpectralCube)
    assert exc.value.args[0] == ('Data object has more than one attribute, so '
                                 'you will need to specify which one to use as '
                                 'the flux for the spectral cube using the attribute= '
                                 'keyword argument.')
def test_to_spectrum1d_from_3d_cube():

    # Set up simple spectral WCS
    wcs = WCS(naxis=3)
    wcs.wcs.ctype = ['RA---TAN', 'DEC--TAN', 'VELO-LSR']
    wcs.wcs.set()

    data = Data(label='spectral-cube', coords=wcs)
    data.add_component(Component(np.ones((3, 4, 5)), units='Jy'), 'x')

    spec = data.get_object(Spectrum1D, attribute=data.id['x'], statistic='sum')

    assert_quantity_allclose(spec.spectral_axis, [1, 2, 3] * u.m / u.s)
    assert_quantity_allclose(spec.flux, [20, 20, 20] * u.Jy)
def test_to_spectrum1d_unitless():

    # Set up simple spectral WCS
    wcs = WCS(naxis=1)
    wcs.wcs.ctype = ['VELO-LSR']
    wcs.wcs.set()

    data = Data(label='spectrum', coords=wcs)
    data.add_component(Component(np.array([3.4, 2.3, -1.1, 0.3])), 'x')

    spec = data.get_object(Spectrum1D, attribute=data.id['x'])

    assert_quantity_allclose(spec.spectral_axis, [1, 2, 3, 4] * u.m / u.s)
    assert_quantity_allclose(spec.flux, [3.4, 2.3, -1.1, 0.3] * u.one)
def test_to_spectrum1d_default_attribute():

    coords = SpectralCoordinates([1, 4, 10] * u.micron)

    data = Data(label='spectrum1d', coords=coords)

    with pytest.raises(ValueError) as exc:
        data.get_object(Spectrum1D)
    assert exc.value.args[0] == 'Data object has no attributes.'

    data.add_component(Component(np.array([3, 4, 5]), units='Jy'), 'x')

    spec = data.get_object(Spectrum1D)
    assert_quantity_allclose(spec.flux, [3, 4, 5] * u.Jy)

    data.add_component(Component(np.array([3, 4, 5]), units='Jy'), 'y')

    with pytest.raises(ValueError) as exc:
        data.get_object(Spectrum1D)
    assert exc.value.args[0] == ('Data object has more than one attribute, so '
                                 'you will need to specify which one to use as '
                                 'the flux for the spectrum using the attribute= '
                                 'keyword argument.')
Beispiel #17
0
def test_translator_from_data_with_derived():

    # Case where we convert a subset to a DataFrame

    data = Data()
    data['a'] = [3, 5, 6, 7]
    data['b'] = data.id['a'] + 1

    dc = DataCollection([data])
    dc.new_subset_group(label='test subset', subset_state=data.id['b'] > 2)

    df = data.get_object(cls=DataFrame)
    assert_equal(list(df.columns), ['a', 'b'])
    assert_equal(df['a'].values, [3, 5, 6, 7])
    assert_equal(df['b'].values, [4, 6, 7, 8])
def test_to_spectral_cube(spectral_cube_wcs):

    data = Data(label='spectral_cube', coords=spectral_cube_wcs)
    values = np.random.random((4, 5, 3))
    data.add_component(Component(values, units='Jy'), 'x')

    spec = data.get_object(SpectralCube, attribute=data.id['x'])

    assert_quantity_allclose(spec.spectral_axis, [1, 2, 3, 4] * u.m / u.s)
    assert_quantity_allclose(spec.filled_data[...], values * u.Jy)

    data.add_subset(data.id['x'] > 0.5, label='bright')

    spec_subset = data.get_subset_object(cls=SpectralCube, subset_id=0,
                                         attribute=data.id['x'])

    assert_quantity_allclose(spec_subset.spectral_axis, [1, 2, 3, 4] * u.m / u.s)
    expected = values.copy()
    expected[expected <= 0.5] = np.nan
    assert_quantity_allclose(spec_subset.filled_data[...], expected * u.Jy)
    assert_equal(spec_subset.mask.include(), values > 0.5)