Skip to content

Commit 13d7400

Browse files
committed
Store the array in a single Numpy masked array. Provide a "from_table" convenience function at the top level.
1 parent 54902a8 commit 13d7400

7 files changed

Lines changed: 122 additions & 93 deletions

File tree

‎CHANGES.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ Bug Fixes
9292

9393
- ``astropy.io.votable``
9494

95+
- The `Table` class now maintains a single array object which is a
96+
Numpy masked array. For variable-length columns, the object that
97+
is stored there is also a Numpy masked array.
98+
9599
- Changed the ``pedantic`` configuration option to be ``False`` by default
96100
due to the vast proliferation of non-compliant VO Tables. [#296]
97101

‎astropy/io/votable/__init__.py‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
Observatory (VO) initiative, particularly the VOTable XML format.
55
"""
66

7-
from .table import parse, parse_single_table, validate
7+
from .table import parse, parse_single_table, validate, from_table
88
from .exceptions import (VOWarning, VOTableChangeWarning,
99
VOTableSpecWarning, UnimplementedWarning, IOWarning,
1010
VOTableSpecError)
1111

12-
__all__ = [
13-
'parse', 'parse_single_table', 'validate', 'VOWarning',
14-
'VOTableChangeWarning', 'VOTableSpecWarning',
15-
'UnimplementedWarning', 'IOWarning', 'VOTableSpecError'
16-
]
12+
__all__ = [ 'parse', 'parse_single_table', 'validate', 'from_table',
13+
'VOWarning', 'VOTableChangeWarning', 'VOTableSpecWarning',
14+
'UnimplementedWarning', 'IOWarning', 'VOTableSpecError' ]

‎astropy/io/votable/table.py‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from ...config import ConfigurationItem
1818

1919

20-
__all__ = ['parse', 'parse_single_table', 'writeto', 'validate']
20+
__all__ = ['parse', 'parse_single_table', 'from_table', 'writeto', 'validate']
2121

2222

2323
PEDANTIC = ConfigurationItem(
@@ -131,7 +131,7 @@ def parse_single_table(source, **kwargs):
131131

132132
def writeto(table, file):
133133
"""
134-
Writes a table to a VOTABLE_ xml file.
134+
Writes a `astropy.io.vo.VOTableFile` to a VOTABLE_ xml file.
135135
136136
Parameters
137137
----------
@@ -256,3 +256,20 @@ def validate(filename, output=sys.stdout, xmllint=False):
256256
if return_as_str:
257257
return output.getvalue()
258258
return len(lines) == 0 and success == 0
259+
260+
261+
def from_table(table):
262+
"""
263+
Given an `astropy.table.Table` object, return a
264+
`~astropy.io.votable.tree.VOTableFile` file structure containing
265+
just that single table.
266+
267+
Parameters
268+
----------
269+
table : `astropy.table.Table` instance
270+
271+
Returns
272+
-------
273+
votable : `astropy.io.votable.tree.VOTableFile` instance
274+
"""
275+
return tree.VOTableFile.from_table(table)

‎astropy/io/votable/tests/vo_test.py‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def setup_class(self):
186186
get_data_filename('data/regression.xml'),
187187
pedantic=False).get_first_table()
188188
self.array = self.table.array
189-
self.mask = self.table.mask
189+
self.mask = self.table.array.mask
190190

191191
def test_implicit_id(self):
192192
assert_array_equal(self.array['string_test_2'],
@@ -200,7 +200,7 @@ def setup_class(self):
200200
pedantic=False)
201201
self.table = self.votable.get_first_table()
202202
self.array = self.table.array
203-
self.mask = self.table.mask
203+
self.mask = self.table.array.mask
204204

205205
def test_fieldref(self):
206206
fieldref = self.table.groups[1].entries[0]
@@ -227,7 +227,7 @@ def test_iter_groups(self):
227227

228228
def test_ref_table(self):
229229
tables = list(self.votable.iter_tables())
230-
for x, y in zip(tables[0].array[0], tables[1].array[0]):
230+
for x, y in zip(tables[0].array.data[0], tables[1].array.data[0]):
231231
assert_array_equal(x, y)
232232

233233
def test_iter_coosys(self):
@@ -240,7 +240,7 @@ def test_select_columns_by_index():
240240
get_data_filename('data/regression.xml'),
241241
pedantic=False, columns=columns).get_first_table()
242242
array = table.array
243-
mask = table.mask
243+
mask = table.array.mask
244244
assert array['string_test'][0] == b"String & test"
245245
columns = ['string_test', 'unsignedByte', 'bitarray']
246246
for c in columns:
@@ -254,7 +254,7 @@ def test_select_columns_by_name():
254254
get_data_filename('data/regression.xml'),
255255
pedantic=False, columns=columns).get_first_table()
256256
array = table.array
257-
mask = table.mask
257+
mask = table.array.mask
258258
assert array['string_test'][0] == b"String & test"
259259
for c in columns:
260260
assert not np.all(mask[c])
@@ -267,7 +267,7 @@ def setup_class(self):
267267
get_data_filename('data/regression.xml'),
268268
pedantic=False).get_first_table()
269269
self.array = self.table.array
270-
self.mask = self.table.mask
270+
self.mask = self.table.array.mask
271271

272272
def test_string_test(self):
273273
assert issubclass(self.array['string_test'].dtype.type,
@@ -363,8 +363,8 @@ def test_array(self):
363363
for a0, b0 in zip(a, b):
364364
assert issubclass(a0.dtype.type, np.int64)
365365
assert_array_equal(a0, b0)
366-
assert self.array['array'][3].mask[0][0]
367-
assert self.array['array'][4].mask[0][1]
366+
assert self.array.data['array'][3].mask[0][0]
367+
assert self.array.data['array'][4].mask[0][1]
368368

369369
def test_bit(self):
370370
assert issubclass(self.array['bit'].dtype.type,
@@ -553,7 +553,7 @@ def test_double_array(self):
553553
assert len(self.array['doublearray'][0]) == 0
554554
assert_array_equal(self.array['doublearray'][1],
555555
[0, 1, np.inf, -np.inf, np.nan, 0, -1])
556-
assert_array_equal(self.array['doublearray'][1].mask,
556+
assert_array_equal(self.array.data['doublearray'][1].mask,
557557
[False, False, False, False, False, False, True])
558558

559559
def test_bit_array2(self):
@@ -577,7 +577,7 @@ def setup_class(self):
577577
self.table = parse(join(TMP_DIR, "test_through_tabledata.xml"),
578578
pedantic=False).get_first_table()
579579
self.array = self.table.array
580-
self.mask = self.table.mask
580+
self.mask = self.table.array.mask
581581

582582
def test_schema(self):
583583
assert_validate_schema(join(TMP_DIR, "test_through_tabledata.xml"))
@@ -593,7 +593,7 @@ def setup_class(self):
593593
self.table = parse(join(TMP_DIR, "test_through_binary.xml"),
594594
pedantic=False).get_first_table()
595595
self.array = self.table.array
596-
self.mask = self.table.mask
596+
self.mask = self.table.array.mask
597597

598598
# Masked values in bit fields don't roundtrip through the binary
599599
# representation -- that's not a bug, just a limitation, so
@@ -689,10 +689,10 @@ def test_build_from_scratch():
689689

690690
table = votable.get_first_table()
691691
assert_array_equal(
692-
table.mask, np.array([(False, [[False, False], [False, False]]),
693-
(False, [[False, False], [False, False]])],
694-
dtype=[('filename', '?'),
695-
('matrix', '?', (2, 2))]))
692+
table.array.mask, np.array([(False, [[False, False], [False, False]]),
693+
(False, [[False, False], [False, False]])],
694+
dtype=[('filename', '?'),
695+
('matrix', '?', (2, 2))]))
696696

697697

698698
def test_validate():

0 commit comments

Comments
 (0)