Skip to content

Commit

Permalink
Fix tests so no warnings are generated in Column() calls
Browse files Browse the repository at this point in the history
This is only complete for table and io.ascii.  All of these tests
run without generating warnings.  This was done to be sure that
all code in astropy.table and astropy.io.ascii is fully compliant
with the new Column args checking, i.e. it will never happen that
a call to astropy.Table can generate a warning to the user.
  • Loading branch information
taldcroft committed Feb 7, 2013
1 parent 716dde9 commit 69ed301
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 91 deletions.
20 changes: 10 additions & 10 deletions astropy/io/ascii/tests/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def test_read_generic(filename):

def test_write_generic(tmpdir):
t = Table()
t.add_column(Column('a', [1,2,3]))
t.add_column(Column('b', ['a', 'b', 'c']))
t.add_column(Column(name='a', data=[1,2,3]))
t.add_column(Column(name='b', data=['a', 'b', 'c']))
t.write(str(tmpdir.join("test")), format='ascii')


Expand Down Expand Up @@ -47,16 +47,16 @@ def test_read_latex_noformat():

def test_write_latex(tmpdir):
t = Table()
t.add_column(Column('a', [1,2,3]))
t.add_column(Column('b', ['a', 'b', 'c']))
t.add_column(Column(name='a', data=[1,2,3]))
t.add_column(Column(name='b', data=['a', 'b', 'c']))
path = str(tmpdir.join("data.tex"))
t.write(path, format='latex')


def test_write_latex_noformat(tmpdir):
t = Table()
t.add_column(Column('a', [1,2,3]))
t.add_column(Column('b', ['a', 'b', 'c']))
t.add_column(Column(name='a', data=[1,2,3]))
t.add_column(Column(name='b', data=['a', 'b', 'c']))
path = str(tmpdir.join("data.tex"))
t.write(path)

Expand All @@ -71,15 +71,15 @@ def test_read_rdb_noformat():

def test_write_rdb(tmpdir):
t = Table()
t.add_column(Column('a', [1,2,3]))
t.add_column(Column('b', ['a', 'b', 'c']))
t.add_column(Column(name='a', data=[1,2,3]))
t.add_column(Column(name='b', data=['a', 'b', 'c']))
path = str(tmpdir.join("data.rdb"))
t.write(path, format='rdb')


def test_write_rdb_noformat(tmpdir):
t = Table()
t.add_column(Column('a', [1,2,3]))
t.add_column(Column('b', ['a', 'b', 'c']))
t.add_column(Column(name='a', data=[1,2,3]))
t.add_column(Column(name='b', data=['a', 'b', 'c']))
path = str(tmpdir.join("data.rdb"))
t.write(path)
20 changes: 10 additions & 10 deletions astropy/table/tests/test_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def Column(request):
class TestColumn():

def test_1(self, Column):
Column('a')
Column(name='a')

def test_subclass(self, Column):
c = Column('a')
c = Column(name='a')
assert isinstance(c, np.ndarray)
c2 = c * 2
assert isinstance(c2, Column)
Expand All @@ -30,7 +30,7 @@ def test_numpy_ops(self, Column):
"""Show that basic numpy operations with Column behave sensibly"""

arr = np.array([1, 2, 3])
c = Column('a', arr)
c = Column(name='a', data=arr)
eq = c == arr
assert np.all(eq)
assert len(eq) == 3
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_format(self, Column):
table.pprint.MAX_LINES.set(MAX_LINES_val)

def test_convert_numpy_array(self, Column):
d = Column('a', [1, 2, 3], dtype='i8')
d = Column(name='a', data=[1, 2, 3], dtype='i8')

np_data = np.array(d)
assert np.all(np_data == d)
Expand All @@ -74,7 +74,7 @@ def test_convert_numpy_array(self, Column):
assert np.all(np_data == d)

def test_convert_units(self, Column):
d = Column('a', [1, 2, 3], dtype="f8", units="m")
d = Column(name='a', data=[1, 2, 3], dtype="f8", units="m")
d.convert_units_to("km")
assert np.all(d.data == [0.001, 0.002, 0.003])

Expand All @@ -83,7 +83,7 @@ def test_array_wrap(self):
output that has a different shape into an ndarray view. Without this a
method call like c.mean() returns a Column array object with length=1."""
# Mean and sum for a 1-d float column
c = table.Column('a', [1., 2., 3.])
c = table.Column(name='a', data=[1., 2., 3.])
assert np.allclose(c.mean(), 2.0)
assert isinstance(c.mean(), (np.floating, float))
assert np.allclose(c.sum(), 6.)
Expand All @@ -93,13 +93,13 @@ def test_array_wrap(self):
assert isinstance(np.cos(c), table.Column)

# Sum for a 1-d int column
c = table.Column('a', [1, 2, 3])
c = table.Column(name='a', data=[1, 2, 3])
assert np.allclose(c.sum(), 6)
assert isinstance(c.sum(), (np.integer, int))

# Sum for a 2-d int column
c = table.Column('a', [[1, 2, 3],
[4, 5, 6]])
c = table.Column(name='a', data=[[1, 2, 3],
[4, 5, 6]])
assert c.sum() == 21
assert isinstance(c.sum(), (np.integer, int))
assert np.all(c.sum(axis=0) == [5, 7, 9])
Expand All @@ -108,7 +108,7 @@ def test_array_wrap(self):

if not numpy_lt_1p5:
# Sum and mean for a 1-d masked column
c = table.MaskedColumn('a', [1., 2., 3.], mask=[0, 0, 1])
c = table.MaskedColumn(name='a', data=[1., 2., 3.], mask=[0, 0, 1])
assert np.allclose(c.mean(), 1.5)
assert isinstance(c.mean(), (np.floating, float))
assert np.allclose(c.sum(), 3.)
Expand Down
8 changes: 4 additions & 4 deletions astropy/table/tests/test_init_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class TestInitFromListOfLists(BaseInitFromListLike):

def setup_method(self, method):
self.data = [(np.int32(1), np.int32(3)),
Column('col1', [2, 4], dtype=np.int32),
Column(name='col1', data=[2, 4], dtype=np.int32),
np.array([3, 5], dtype=np.int32)]

def test_default_names(self):
Expand All @@ -150,7 +150,7 @@ def test_bad_data(self):
class TestInitFromColsList(BaseInitFromListLike):

def setup_method(self, method):
self.data = [Column('x', [1, 3], dtype=np.int32),
self.data = [Column(name='x', data=[1, 3], dtype=np.int32),
np.array([2, 4], dtype=np.int32),
np.array([3, 5], dtype='i8')]

Expand Down Expand Up @@ -212,7 +212,7 @@ def test_partial_names_ref(self):
class TestInitFromDict(BaseInitFromDictLike):

def setup_method(self, method):
self.data = dict([('a', Column('x', [1, 3])),
self.data = dict([('a', Column(name='x', data=[1, 3])),
('b', [2, 4]),
('c', np.array([3, 5], dtype='i8'))])

Expand All @@ -221,7 +221,7 @@ def setup_method(self, method):
class TestInitFromOrderedDict(BaseInitFromDictLike):

def setup_method(self, method):
self.data = OrderedDict([('a', Column('x', [1, 3])),
self.data = OrderedDict([('a', Column(name='x', data=[1, 3])),
('b', [2, 4]),
('c', np.array([3, 5], dtype='i8'))])

Expand Down
6 changes: 3 additions & 3 deletions astropy/table/tests/test_item_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def set_global_Table_DATA(request):

Table = MaskedTable if request.param else table.Table
Column = table.MaskedColumn if request.param else table.Column
COLS = [Column('a', [1, 2, 3], description='da',
COLS = [Column(name='a', data=[1, 2, 3], description='da',
format='fa', meta={'ma': 1}, units='ua'),
Column('b', [4, 5, 6], description='db',
Column(name='b', data=[4, 5, 6], description='db',
format='fb', meta={'mb': 1}, units='ub'),
Column('c', [7, 8, 9], description='dc',
Column(name='c', data=[7, 8, 9], description='dc',
format='fc', meta={'mc': 1}, units='ub')]
DATA = Table(COLS)

Expand Down
66 changes: 33 additions & 33 deletions astropy/table/tests/test_masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

class SetupData(object):
def setup_method(self, method):
self.a = MaskedColumn('a', [1, 2, 3], fill_value=1)
self.b = MaskedColumn('b', [4, 5, 6], mask=True)
self.c = MaskedColumn('c', [7, 8, 9], mask=False)
self.a = MaskedColumn(name='a', data=[1, 2, 3], fill_value=1)
self.b = MaskedColumn(name='b', data=[4, 5, 6], mask=True)
self.c = MaskedColumn(name='c', data=[7, 8, 9], mask=False)
self.d_mask = np.array([False, True, False])
self.d = MaskedColumn('d', [7, 8, 7], mask=self.d_mask)
self.d = MaskedColumn(name='d', data=[7, 8, 7], mask=self.d_mask)
self.t = Table([self.a, self.b], masked=True)
self.ca = Column('ca', [1, 2, 3])
self.ca = Column(name='ca', data=[1, 2, 3])


@pytest.mark.xfail('numpy_lt_1p5')
Expand All @@ -34,9 +34,9 @@ class TestFilled(object):
def setup_method(self, method):
mask = [True, False, False]
self.meta = {'a': 1, 'b': [2, 3]}
a = self.a = MaskedColumn('a', [1, 2, 3], fill_value=10, mask=mask, meta={'a': 1})
b = self.b = MaskedColumn('b', [4.0, 5.0, 6.0], fill_value=10.0, mask=mask)
c = self.c = MaskedColumn('c', ['7', '8', '9'], fill_value='1', mask=mask)
a = self.a = MaskedColumn(name='a', data=[1, 2, 3], fill_value=10, mask=mask, meta={'a': 1})
b = self.b = MaskedColumn(name='b', data=[4.0, 5.0, 6.0], fill_value=10.0, mask=mask)
c = self.c = MaskedColumn(name='c', data=['7', '8', '9'], fill_value='1', mask=mask)

def test_filled_column(self):
f = self.a.filled()
Expand Down Expand Up @@ -103,7 +103,7 @@ class TestFillValue(SetupData):
def test_init_set_fill_value(self):
"""Check that setting fill_value in the MaskedColumn init works"""
assert self.a.fill_value == 1
c = MaskedColumn('c', ['xxxx', 'yyyy'], fill_value='none')
c = MaskedColumn(name='c', data=['xxxx', 'yyyy'], fill_value='none')
assert c.fill_value == 'none'

def test_set_get_fill_value_for_bare_column(self):
Expand All @@ -113,7 +113,7 @@ def test_set_get_fill_value_for_bare_column(self):
assert np.all(self.d.filled() == [7, -999, 7])

def test_set_get_fill_value_for_str_column(self):
c = MaskedColumn('c', ['xxxx', 'yyyy'], mask=[True, False])
c = MaskedColumn(name='c', data=['xxxx', 'yyyy'], mask=[True, False])
# assert np.all(c.filled() == ['N/A', 'yyyy'])
c.fill_value = 'ABCDEF'
assert c.fill_value == 'ABCD' # string truncated to dtype length
Expand Down Expand Up @@ -156,19 +156,19 @@ def test_set_mask_and_not_ref(self):
def test_set_mask_from_list(self):
"""Set mask from a list"""
mask_list = [False, True, False]
a = MaskedColumn('a', [1, 2, 3], mask=mask_list)
a = MaskedColumn(name='a', data=[1, 2, 3], mask=mask_list)
assert np.all(a.mask == mask_list)

def test_override_existing_mask(self):
"""Override existing mask values"""
mask_list = [False, True, False]
b = MaskedColumn('b', self.b, mask=mask_list)
b = MaskedColumn(name='b', data=self.b, mask=mask_list)
assert np.all(b.mask == mask_list)

def test_incomplete_mask_spec(self):
"""Incomplete mask specification (mask values cycle through available)"""
mask_list = [False, True]
b = MaskedColumn('b', length=4, mask=mask_list)
b = MaskedColumn(name='b', length=4, mask=mask_list)
assert np.all(b.mask == mask_list + mask_list)


Expand Down Expand Up @@ -204,9 +204,9 @@ class TestAddColumn(object):
def test_add_masked_column_to_masked_table(self):
t = Table(masked=True)
assert t.masked
t.add_column(MaskedColumn('a', [1,2,3], mask=[0,1,0]))
t.add_column(MaskedColumn(name='a', data=[1,2,3], mask=[0,1,0]))
assert t.masked
t.add_column(MaskedColumn('b', [4,5,6], mask=[1,0,1]))
t.add_column(MaskedColumn(name='b', data=[4,5,6], mask=[1,0,1]))
assert t.masked
assert np.all(t['a'] == np.array([1,2,3]))
assert np.all(t['a'].mask == np.array([0,1,0], bool))
Expand All @@ -216,9 +216,9 @@ def test_add_masked_column_to_masked_table(self):
def test_add_masked_column_to_non_masked_table(self):
t = Table(masked=False)
assert not t.masked
t.add_column(Column('a', [1,2,3]))
t.add_column(Column(name='a', data=[1,2,3]))
assert not t.masked
t.add_column(MaskedColumn('b', [4,5,6], mask=[1,0,1]))
t.add_column(MaskedColumn(name='b', data=[4,5,6], mask=[1,0,1]))
assert t.masked
assert np.all(t['a'] == np.array([1,2,3]))
assert np.all(t['a'].mask == np.array([0,0,0], bool))
Expand All @@ -228,9 +228,9 @@ def test_add_masked_column_to_non_masked_table(self):
def test_add_non_masked_column_to_masked_table(self):
t = Table(masked=True)
assert t.masked
t.add_column(Column('a', [1,2,3]))
t.add_column(Column(name='a', data=[1,2,3]))
assert t.masked
t.add_column(MaskedColumn('b', [4,5,6], mask=[1,0,1]))
t.add_column(MaskedColumn(name='b', data=[4,5,6], mask=[1,0,1]))
assert t.masked
assert np.all(t['a'] == np.array([1,2,3]))
assert np.all(t['a'].mask == np.array([0,0,0], bool))
Expand All @@ -243,8 +243,8 @@ class TestAddRow(object):

def test_add_masked_row_to_masked_table_iterable(self):
t = Table(masked=True)
t.add_column(MaskedColumn('a', [1], mask=[0]))
t.add_column(MaskedColumn('b', [4], mask=[1]))
t.add_column(MaskedColumn(name='a', data=[1], mask=[0]))
t.add_column(MaskedColumn(name='b', data=[4], mask=[1]))
t.add_row([2,5], mask=[1,0])
t.add_row([3,6], mask=[0,1])
assert t.masked
Expand All @@ -255,8 +255,8 @@ def test_add_masked_row_to_masked_table_iterable(self):

def test_add_masked_row_to_masked_table_mapping1(self):
t = Table(masked=True)
t.add_column(MaskedColumn('a', [1], mask=[0]))
t.add_column(MaskedColumn('b', [4], mask=[1]))
t.add_column(MaskedColumn(name='a', data=[1], mask=[0]))
t.add_column(MaskedColumn(name='b', data=[4], mask=[1]))
t.add_row({'b':5, 'a':2}, mask={'a':1, 'b':0})
t.add_row({'a':3, 'b':6}, mask={'b':1, 'a':0})
assert t.masked
Expand All @@ -269,8 +269,8 @@ def test_add_masked_row_to_masked_table_mapping2(self):
# When adding values to a masked table, if the mask is specified as a
# dict, then values not specified will have mask values set to True
t = Table(masked=True)
t.add_column(MaskedColumn('a', [1], mask=[0]))
t.add_column(MaskedColumn('b', [4], mask=[1]))
t.add_column(MaskedColumn(name='a', data=[1], mask=[0]))
t.add_column(MaskedColumn(name='b', data=[4], mask=[1]))
t.add_row({'b':5}, mask={'b':0})
t.add_row({'a':3}, mask={'a':0})
assert t.masked
Expand All @@ -284,8 +284,8 @@ def test_add_masked_row_to_masked_table_mapping3(self):
# add_row, then the mask should be set to False if values are present
# and True if not.
t = Table(masked=True)
t.add_column(MaskedColumn('a', [1], mask=[0]))
t.add_column(MaskedColumn('b', [4], mask=[1]))
t.add_column(MaskedColumn(name='a', data=[1], mask=[0]))
t.add_column(MaskedColumn(name='b', data=[4], mask=[1]))
t.add_row({'b':5})
t.add_row({'a':3})
assert t.masked
Expand All @@ -298,16 +298,16 @@ def test_add_masked_row_to_masked_table_mapping4(self):
# When adding values to a masked table, if the mask is specified as a
# dict, then keys in values should match keys in mask
t = Table(masked=True)
t.add_column(MaskedColumn('a', [1], mask=[0]))
t.add_column(MaskedColumn('b', [4], mask=[1]))
t.add_column(MaskedColumn(name='a', data=[1], mask=[0]))
t.add_column(MaskedColumn(name='b', data=[4], mask=[1]))
with pytest.raises(ValueError) as exc:
t.add_row({'b':5}, mask={'a':True})
assert exc.value.args[0] == 'keys in mask should match keys in vals'

def test_add_masked_row_to_masked_table_mismatch(self):
t = Table(masked=True)
t.add_column(MaskedColumn('a', [1], mask=[0]))
t.add_column(MaskedColumn('b', [4], mask=[1]))
t.add_column(MaskedColumn(name='a', data=[1], mask=[0]))
t.add_column(MaskedColumn(name='b', data=[4], mask=[1]))
with pytest.raises(TypeError) as exc:
t.add_row([2,5], mask={'a':1, 'b':0})
assert exc.value.args[0] == "Mismatch between type of vals and mask"
Expand All @@ -317,8 +317,8 @@ def test_add_masked_row_to_masked_table_mismatch(self):

def test_add_masked_row_to_non_masked_table_iterable(self):
t = Table(masked=False)
t.add_column(Column('a', [1]))
t.add_column(Column('b', [4]))
t.add_column(Column(name='a', data=[1]))
t.add_column(Column(name='b', data=[4]))
assert not t.masked
t.add_row([2,5])
assert not t.masked
Expand Down
4 changes: 2 additions & 2 deletions astropy/table/tests/test_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def t(self):
if Column is None:
return None
if not hasattr(self, '_t'):
a = Column('a', [1, 2, 3], dtype='i8')
b = Column('b', [4, 5, 6], dtype='i8')
a = Column(name='a', data=[1, 2, 3], dtype='i8')
b = Column(name='b', data=[4, 5, 6], dtype='i8')
self._t = Table([a, b])
return self._t

Expand Down
Loading

0 comments on commit 69ed301

Please sign in to comment.