forked from astropy/astropy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_init_table.py
More file actions
276 lines (217 loc) · 9.87 KB
/
Copy pathtest_init_table.py
File metadata and controls
276 lines (217 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from __future__ import print_function # For print debugging with python 2 or 3
import pytest
import numpy as np
from .. import Table, Column
from astropy.utils import OrderedDict
class BaseInitFrom():
def test_basic_init(self):
t = Table(self.data, names=('a', 'b', 'c'))
assert t.colnames == ['a', 'b', 'c']
assert np.all(t['a'] == np.array([1, 3]))
assert np.all(t['b'] == np.array([2, 4]))
assert np.all(t['c'] == np.array([3, 5]))
assert all(t[name].name == name for name in t.colnames)
def test_set_dtypes(self):
t = Table(self.data, names=('a', 'b', 'c'), dtypes=('i4', 'f4', 'f8'))
assert t.colnames == ['a', 'b', 'c']
assert np.all(t['a'] == np.array([1, 3], dtype='i4'))
assert np.all(t['b'] == np.array([2, 4], dtype='f4'))
assert np.all(t['c'] == np.array([3, 5], dtype='f8'))
assert t['a'].dtype.type == np.int32
assert t['b'].dtype.type == np.float32
assert t['c'].dtype.type == np.float64
assert all(t[name].name == name for name in t.colnames)
def test_names_dtypes_mismatch(self):
with pytest.raises(ValueError):
Table(self.data, names=('a',), dtypes=('i4', 'f4', 'i4'))
def test_names_cols_mismatch(self):
with pytest.raises(ValueError):
Table(self.data, names=('a',), dtypes=('i4'))
class BaseInitFromListLike(BaseInitFrom):
def test_names_cols_mismatch(self):
with pytest.raises(ValueError):
Table(self.data, names=['a'], dtypes=[int])
def test_names_copy_false(self):
with pytest.raises(ValueError):
Table(self.data, names=['a'], dtypes=[int], copy=False)
class BaseInitFromDictLike(BaseInitFrom):
pass
class TestInitFromNdarrayHomo(BaseInitFromListLike):
def setup_method(self, method):
self.data = np.array([(1, 2, 3),
(3, 4, 5)],
dtype='i4')
def test_default_names(self):
t = Table(self.data)
assert t.colnames == ['col0', 'col1', 'col2']
def test_ndarray_ref(self):
"""Init with ndarray and copy=False and show that ValueError is raised
to input ndarray"""
t = Table(self.data, copy=False)
t['col1'][1] = 0
assert t._data['col1'][1] == 0
assert t['col1'][1] == 0
assert self.data[1][1] == 0
# NOTE: assert np.all(t._data == self.data) fails because when
# homogenous array is viewcast to structured then the == is False
def test_partial_names_dtypes(self):
t = Table(self.data, names=['a', None, 'c'], dtypes=[None, None, 'f8'])
assert t.colnames == ['a', 'col1', 'c']
assert t['a'].dtype.type == np.int32
assert t['col1'].dtype.type == np.int32
assert t['c'].dtype.type == np.float64
assert all(t[name].name == name for name in t.colnames)
def test_partial_names_ref(self):
t = Table(self.data, names=['a', None, 'c'])
assert t.colnames == ['a', 'col1', 'c']
assert t['a'].dtype.type == np.int32
assert t['col1'].dtype.type == np.int32
assert t['c'].dtype.type == np.int32
assert all(t[name].name == name for name in t.colnames)
class TestInitFromListOfLists(BaseInitFromListLike):
def setup_method(self, method):
self.data = [(np.int32(1), np.int32(3)),
Column('col1', [2, 4], dtype=np.int32),
np.array([3, 5], dtype=np.int32)]
def test_default_names(self):
t = Table(self.data)
assert t.colnames == ['col0', 'col1', 'col2']
assert all(t[name].name == name for name in t.colnames)
def test_partial_names_dtypes(self):
t = Table(self.data, names=['b', None, 'c'],
dtypes=['f4', None, 'f8'])
assert t.colnames == ['b', 'col1', 'c']
assert t['b'].dtype.type == np.float32
assert t['col1'].dtype.type == np.int32
assert t['c'].dtype.type == np.float64
assert all(t[name].name == name for name in t.colnames)
def test_bad_data(self):
with pytest.raises(ValueError):
Table([[1, 2],
[3, 4, 5]])
class TestInitFromColsList(BaseInitFromListLike):
def setup_method(self, method):
self.data = [Column('x', [1, 3], dtype=np.int32),
np.array([2, 4], dtype=np.int32),
np.array([3, 5], dtype='i8')]
def test_default_names(self):
t = Table(self.data)
assert t.colnames == ['x', 'col1', 'col2']
assert all(t[name].name == name for name in t.colnames)
def test_partial_names_dtypes(self):
t = Table(self.data, names=['b', None, 'c'], dtypes=['f4', None, 'f8'])
assert t.colnames == ['b', 'col1', 'c']
assert t['b'].dtype.type == np.float32
assert t['col1'].dtype.type == np.int32
assert t['c'].dtype.type == np.float64
assert all(t[name].name == name for name in t.colnames)
def test_ref(self):
with pytest.raises(ValueError):
Table(self.data, copy=False)
class TestInitFromNdarrayStruct(BaseInitFromDictLike):
def setup_method(self, method):
self.data = np.array([(1, 2, 3),
(3, 4, 5)],
dtype=[('x', 'i8'), ('y', 'i4'), ('z', 'i8')])
def test_ndarray_ref(self):
"""Init with ndarray and copy=False and show that table uses reference
to input ndarray"""
t = Table(self.data, copy=False)
assert np.all(t._data == self.data)
t['x'][1] = 0
assert t._data['x'][1] == 0
assert self.data['x'][1] == 0
assert np.all(t._data == self.data)
assert all(t[name].name == name for name in t.colnames)
def test_partial_names_dtypes(self):
t = Table(self.data, names=['e', None, 'd'], dtypes=['f4', None, 'f8'])
assert t.colnames == ['e', 'y', 'd']
assert t['e'].dtype.type == np.float32
assert t['y'].dtype.type == np.int32
assert t['d'].dtype.type == np.float64
assert all(t[name].name == name for name in t.colnames)
def test_partial_names_ref(self):
t = Table(self.data, names=['e', None, 'd'], copy=False)
assert t.colnames == ['e', 'y', 'd']
assert t['e'].dtype.type == np.int64
assert t['y'].dtype.type == np.int32
assert t['d'].dtype.type == np.int64
assert all(t[name].name == name for name in t.colnames)
class TestInitFromDict(BaseInitFromDictLike):
def setup_method(self, method):
self.data = dict([('a', Column('x', [1, 3])),
('b', [2, 4]),
('c', np.array([3, 5], dtype='i8'))])
class TestInitFromOrderedDict(BaseInitFromDictLike):
def setup_method(self, method):
self.data = OrderedDict([('a', Column('x', [1, 3])),
('b', [2, 4]),
('c', np.array([3, 5], dtype='i8'))])
def test_col_order(self):
t = Table(self.data)
assert t.colnames == ['a', 'b', 'c']
class TestInitFromTable(BaseInitFromDictLike):
def setup_method(self, method):
arr = np.array([(1, 2, 3),
(3, 4, 5)],
dtype=[('x', 'i8'), ('y', 'i8'), ('z', 'f8')])
self.data = Table(arr, meta={'comments': ['comment1', 'comment2']})
def test_data_meta_copy(self):
t = Table(self.data)
assert t.meta['comments'][0] == 'comment1'
t['x'][1] = 8
t.meta['comments'][1] = 'new comment2'
assert self.data.meta['comments'][1] == 'comment2'
assert np.all(t['x'] == np.array([1, 8]))
assert np.all(self.data['x'] == np.array([1, 3]))
assert t['z'].name == 'z'
assert all(t[name].name == name for name in t.colnames)
def test_table_ref(self):
t = Table(self.data, copy=False)
assert np.all(t._data == self.data._data)
t['x'][1] = 0
assert t._data['x'][1] == 0
assert self.data._data['x'][1] == 0
assert np.all(t._data == self.data._data)
assert all(t[name].name == name for name in t.colnames)
def test_partial_names_dtypes(self):
t = Table(self.data, names=['e', None, 'd'], dtypes=['f4', None, 'i8'])
assert t.colnames == ['e', 'y', 'd']
assert t['e'].dtype.type == np.float32
assert t['y'].dtype.type == np.int64
assert t['d'].dtype.type == np.int64
assert all(t[name].name == name for name in t.colnames)
def test_partial_names_ref(self):
t = Table(self.data, names=['e', None, 'd'], copy=False)
assert t.colnames == ['e', 'y', 'd']
assert t['e'].dtype.type == np.int64
assert t['y'].dtype.type == np.int64
assert t['d'].dtype.type == np.float64
assert all(t[name].name == name for name in t.colnames)
def test_init_from_columns(self):
t = Table(self.data)
t2 = Table(t.columns['z', 'x', 'y'])
assert t2.colnames == ['z', 'x', 'y']
assert t2._data.dtype.names == ('z', 'x', 'y')
def test_init_from_columns_slice(self):
t = Table(self.data)
t2 = Table(t.columns[0:2])
assert t2.colnames == ['x', 'y']
assert t2._data.dtype.names == ('x', 'y')
def test_init_from_columns_mix(self):
t = Table(self.data)
t2 = Table([t.columns[0], t.columns['z']])
assert t2.colnames == ['x', 'z']
assert t2._data.dtype.names == ('x', 'z')
class TestInitFromNone():
# Note table_table.TestEmptyData tests initializing a completely empty
# table and adding data.
def test_data_none_with_cols(self):
t = Table(names=('a', 'b'))
assert len(t['a']) == 0
assert len(t['b']) == 0
assert t.colnames == ['a', 'b']
t = Table(names=('a', 'b'), dtypes=('f4', 'i4'))
assert t['a'].dtype.type == np.float32
assert t['b'].dtype.type == np.int32
assert t.colnames == ['a', 'b']