Esempio n. 1
0
def test_gentle_asarray_array_input():
    """
    Test gentle_asarray with various forms of np.ndarray input,
    which may need to be cast to a different data type.
    """
    # Array that already bears the correct dtype:
    inp = np.array([1, 2, 3, 4], dtype=np.int16)
    result = util.gentle_asarray(inp, dtype=np.int16)
    assert result.dtype == np.int16
    assert_array_equal(result, inp)

    # This will require casting, since int8 != int16:
    result = util.gentle_asarray(inp, dtype=np.int8)
    assert result.dtype == np.int8
    assert_array_equal(result, inp)

    # Test broadcasting the same 1D array to a recarray with
    # two columns.  The input array should be copied into
    # each column and cast as necessary:
    dtype = np.dtype([("col1", np.int16), ("col2", np.float32)])
    result = util.gentle_asarray(inp, dtype=dtype)
    assert result.dtype == dtype
    assert result["col1"].dtype == np.int16
    assert_array_equal(result["col1"], np.array([1, 2, 3, 4], dtype=np.int16))
    assert result["col2"].dtype == np.float32
    assert_array_equal(result["col1"], np.array([1, 2, 3, 4],
                                                dtype=np.float32))
Esempio n. 2
0
def test_gentle_asarray_invalid_conversion():
    """
    Test gentle_asarray with input that cannot be cast
    to an array.
    """
    with pytest.raises(ValueError):
        util.gentle_asarray(object(), dtype=np.float32)
Esempio n. 3
0
def test_gentle_asarray_mismatched_field_names():
    """
    Test gentle_asarray with input field names that don't
    match the requested output field names.
    """
    in_dtype = np.dtype([("col1", np.int16), ("col2", np.float32)])
    inp = np.array([1, 2, 3, 4], dtype=in_dtype)
    out_dtype = np.dtype([("col1", np.int16), ("foo", np.float32)])
    with pytest.raises(ValueError):
        util.gentle_asarray(inp, dtype=out_dtype)
Esempio n. 4
0
def test_gentle_asarray_scalar_input():
    """
    Test gentle_asarray with scalar input.
    """
    result = util.gentle_asarray(3.14159, dtype=np.float32)
    assert result.dtype == np.float32
    assert_array_equal(result, np.array([3.14159], dtype=np.float32))
def test_gentle_asarray():
    x = np.array([('abc', 1.0)], dtype=[('FOO', 'S3'), ('BAR', '>f8')])

    new_dtype = [('foo', '|S3'), ('bar', '<f8')]

    y = util.gentle_asarray(x, new_dtype)

    assert y['BAR'][0] == 1.0
Esempio n. 6
0
def test_gentle_asarray_fits_rec_input():
    """
    Test gentle_asarray with FITS_rec array input.
    """
    # 'e' is the FITS data type code for single-precision float:
    cols = [fits.Column("col1", format="e", array=np.array([1, 2, 3, 4]))]
    inp = fits.FITS_rec.from_columns(cols)
    out_dtype = np.dtype([("col1", np.float32)])
    result = util.gentle_asarray(inp, dtype=out_dtype)
    assert result.dtype == out_dtype
    assert_array_equal(result, inp)
Esempio n. 7
0
def test_gentle_asarray_field_name_case():
    """
    Test gentle_assary with input and output field names that
    only differ by case.
    """
    in_dtype = np.dtype([("col1", np.int16), ("col2", np.float32)])
    inp = np.array([1, 2, 3, 4], dtype=in_dtype)
    out_dtype = np.dtype([("COL1", np.int16), ("COL2", np.float32)])
    result = util.gentle_asarray(inp, dtype=out_dtype)
    assert result.dtype == out_dtype
    assert_array_equal(result, inp)
Esempio n. 8
0
def test_gentle_asarray_nested_array():
    """
    Test gentle_asarray with nested arrays in one of the record
    fields.
    """
    # '2f' is a numpy data type code for an array of two 32-bit floats:
    in_dtype = np.dtype([("col1", np.dtype("2f")), ("col2", np.int16)])
    inp = np.array([1, 2, 3, 4], dtype=in_dtype)
    out_dtype = np.dtype([("col1", np.dtype("2f")), ("col2", np.int8)])
    result = util.gentle_asarray(inp, dtype=out_dtype)
    assert result.dtype == out_dtype
    assert_array_equal(result["col2"], inp["col2"])
Esempio n. 9
0
def test_gentle_asarray_recarray_input():
    """
    Test gentle_asarray input that is already an np.recarray.
    """
    # This is just the simple case where the dtype already matches
    # the desired output, but it follows a different code path
    # from array input:
    dtype = np.dtype([("col1", np.int16), ("col2", np.float32)])
    inp = np.array([1, 2, 3, 4], dtype=dtype)
    result = util.gentle_asarray(inp, dtype=dtype)
    assert result.dtype == dtype
    assert_array_equal(result, inp)
Esempio n. 10
0
def test_gentle_asarray_fits_rec_pseudo_unsigned():
    """
    Test gentle_asarray handling of a FITS_rec with a pseudo unsigned
    integer column, which is a special case due to a bug in astropy.
    """
    # 'j' is the FITS data type code for 32-bit integer.  A column
    # is only considered "pseudo unsigned" for certain values of
    # bezero, one of which is 2**31.
    cols = [fits.Column("col1", format="j", array=np.array([1, 2, 3, 4], np.uint32), bzero=2**31)]
    inp = fits.FITS_rec.from_columns(cols)
    out_dtype = np.dtype([("col1", np.uint32)])
    result = util.gentle_asarray(inp, dtype=out_dtype)
    # If not handled, the astropy bug would result in a signed
    # int column dtype:
    assert result["col1"].dtype == np.uint32