Skip to content

Commit e7c1807

Browse files
committed
Improve checking of presence of required columns in time series
1 parent 23da5f3 commit e7c1807

6 files changed

Lines changed: 173 additions & 55 deletions

File tree

‎astropy/timeseries/binned.py‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
from astropy.units import Quantity
1111
from astropy.utils.misc import InheritDocstrings
1212

13-
from astropy.timeseries.core import BaseTimeSeries
13+
from astropy.timeseries.core import BaseTimeSeries, autocheck_required_columns
1414

1515
__all__ = ['BinnedTimeSeries']
1616

1717

18+
@autocheck_required_columns
1819
class BinnedTimeSeries(BaseTimeSeries, metaclass=InheritDocstrings):
1920
"""
2021
A class to represent binned time series data in tabular form.
@@ -67,20 +68,20 @@ class BinnedTimeSeries(BaseTimeSeries, metaclass=InheritDocstrings):
6768
**kwargs : dict, optional
6869
Additional keyword arguments are passed to `~astropy.table.QTable`.
6970
"""
70-
_require_time_column = False
71+
72+
_required_columns = ['time_bin_start', 'time_bin_size']
7173

7274
def __init__(self, data=None, *, time_bin_start=None, time_bin_end=None,
7375
time_bin_size=None, n_bins=None, **kwargs):
7476

7577
super().__init__(data=data, **kwargs)
7678

77-
# FIXME: this is because for some operations, an empty time series needs
78-
# to be created, then columns added one by one. We should check that
79-
# when columns are added manually, time is added first and is of the
80-
# right type.
79+
# For some operations, an empty time series needs to be created, then
80+
# columns added one by one. We should check that when columns are added
81+
# manually, time is added first and is of the right type.
8182
if (data is None and time_bin_start is None and time_bin_end is None and
8283
time_bin_size is None and n_bins is None):
83-
self._required_columns = ['time_bin_start', 'time_bin_size']
84+
self._required_columns_relax = True
8485
return
8586

8687
# First if time_bin_start and time_bin_end have been given in the table data, we
@@ -90,15 +91,13 @@ def __init__(self, data=None, *, time_bin_start=None, time_bin_end=None,
9091
if 'time_bin_start' in self.colnames:
9192
if time_bin_start is None:
9293
time_bin_start = self.columns['time_bin_start']
93-
self.remove_column('time_bin_start')
9494
else:
9595
raise TypeError("'time_bin_start' has been given both in the table "
9696
"and as a keyword argument")
9797

9898
if 'time_bin_size' in self.colnames:
9999
if time_bin_size is None:
100100
time_bin_size = self.columns['time_bin_size']
101-
self.remove_column('time_bin_size')
102101
else:
103102
raise TypeError("'time_bin_size' has been given both in the table "
104103
"and as a keyword argument")
@@ -165,13 +164,20 @@ def __init__(self, data=None, *, time_bin_start=None, time_bin_end=None,
165164
time_bin_end = times
166165
time_bin_size = (time_bin_end - time_bin_start).sec * u.s
167166

168-
self.add_column(time_bin_start, index=0, name='time_bin_start')
169-
self.add_index('time_bin_start')
170-
171167
if time_bin_size.isscalar:
172168
time_bin_size = np.repeat(time_bin_size, len(self))
173169

174-
self.add_column(time_bin_size, index=1, name='time_bin_size')
170+
with self._delay_required_column_checks():
171+
172+
if 'time_bin_start' in self.colnames:
173+
self.remove_column('time_bin_start')
174+
175+
if 'time_bin_size' in self.colnames:
176+
self.remove_column('time_bin_size')
177+
178+
self.add_column(time_bin_start, index=0, name='time_bin_start')
179+
self.add_index('time_bin_start')
180+
self.add_column(time_bin_size, index=1, name='time_bin_size')
175181

176182
@property
177183
def time_bin_start(self):

‎astropy/timeseries/core.py‎

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,92 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3+
from types import FunctionType
4+
from contextlib import contextmanager
5+
36
from astropy.table import QTable
7+
from astropy.utils import wraps
8+
9+
__all__ = ['BaseTimeSeries', 'autocheck_required_columns']
10+
11+
COLUMN_RELATED_METHODS = ['add_column',
12+
'add_columns',
13+
'keep_columns',
14+
'remove_column',
15+
'remove_columns',
16+
'rename_column']
17+
18+
19+
def autocheck_required_columns(cls):
20+
"""
21+
This is a decorator that ensures that the table contains specific
22+
methods indicated by the _required_columns attribute. The aim is to
23+
decorate all methods that might affect the columns in the table and check
24+
for consistency after the methods have been run.
25+
"""
26+
27+
def decorator_method(method):
28+
29+
@wraps(method)
30+
def wrapper(self, *args, **kwargs):
31+
result = method(self, *args, **kwargs)
32+
self._check_required_columns()
33+
return result
34+
35+
return wrapper
436

5-
__all__ = ['BaseTimeSeries']
37+
for name in COLUMN_RELATED_METHODS:
38+
if (not hasattr(cls, name) or
39+
not isinstance(getattr(cls, name), FunctionType)):
40+
raise ValueError("{0} is not a valid method".format(name))
41+
setattr(cls, name, decorator_method(getattr(cls, name)))
42+
43+
return cls
644

745

846
class BaseTimeSeries(QTable):
947

1048
_required_columns = None
49+
_required_columns_enabled = True
1150

12-
def add_columns(self, cols, indexes=None, names=None, **kwargs):
51+
# If _required_column_relax is True, we don't require the columns to be
52+
# present but we do require them to be the correct ones IF present. Note
53+
# that this is a temporary state - as soon as the required columns
54+
# are all present, we toggle this to False
55+
_required_columns_relax = False
1356

14-
if names is None:
15-
new_names = [col.info.name for col in cols]
16-
else:
17-
new_names = names
57+
def _check_required_columns(self):
1858

19-
existing_and_new = set(new_names) | set(self.colnames)
59+
if not self._required_columns_enabled:
60+
return
2061

2162
if self._required_columns is not None:
2263

23-
if len(set(self._required_columns) & existing_and_new) == len(self._required_columns):
24-
# Existing and new columns include all required columns, so we are
25-
# good to go
26-
self._required_columns = None
27-
elif len(existing_and_new - set(self._required_columns)) == 0:
28-
# All existing and new columns are a strict subset of required
29-
# columns - it doesn't mean that all required columns are present,
30-
# but no non-required columns are. So we are good to go.
31-
pass
64+
if self._required_columns_relax:
65+
required_columns = self._required_columns[:len(self.colnames)]
3266
else:
33-
# There are columns from the required ones missing but some non-
34-
# required columns are being added
35-
for colname in self._required_columns:
36-
if colname not in existing_and_new:
37-
raise ValueError("{0} requires a column called '{1}' to be set "
38-
"before data can be added"
39-
.format(self.__class__.__name__, colname))
40-
41-
return super().add_columns(cols, indexes=indexes, names=names, **kwargs)
67+
required_columns = self._required_columns
68+
69+
plural = 's' if len(required_columns) > 1 else ''
70+
71+
if not self._required_columns_relax and len(self.colnames) == 0:
72+
73+
raise ValueError("{0} object is invalid - expected '{1}' "
74+
"as the first column{2} but time series has no columns"
75+
.format(self.__class__.__name__, required_columns[0], plural))
76+
77+
elif self.colnames[:len(required_columns)] != required_columns:
78+
79+
raise ValueError("{0} object is invalid - expected '{1}' "
80+
"as the first column{2} but found '{3}'"
81+
.format(self.__class__.__name__, required_columns[0], plural, self.colnames[0]))
82+
83+
if (self._required_columns_relax
84+
and self._required_columns == self.colnames[:len(self._required_columns)]):
85+
self._required_columns_relax = False
86+
87+
@contextmanager
88+
def _delay_required_column_checks(self):
89+
self._required_columns_enabled = False
90+
yield
91+
self._required_columns_enabled = True
92+
self._check_required_columns()

‎astropy/timeseries/sampled.py‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
from astropy import units as u
1010
from astropy.units import Quantity
1111

12-
from astropy.timeseries.core import BaseTimeSeries
12+
from astropy.timeseries.core import BaseTimeSeries, autocheck_required_columns
1313

1414
__all__ = ['TimeSeries']
1515

1616

17+
@autocheck_required_columns
1718
class TimeSeries(BaseTimeSeries):
1819
"""
1920
A class to represent time series data in tabular form.
@@ -55,19 +56,18 @@ class TimeSeries(BaseTimeSeries):
5556
Additional keyword arguments are passed to `~astropy.table.QTable`.
5657
"""
5758

58-
_require_time_column = False
59+
_required_columns = ['time']
5960

6061
def __init__(self, data=None, *, time=None, time_start=None,
6162
time_delta=None, n_samples=None, **kwargs):
6263

6364
super().__init__(data=data, **kwargs)
6465

65-
# FIXME: this is because for some operations, an empty time series needs
66-
# to be created, then columns added one by one. We should check that
67-
# when columns are added manually, time is added first and is of the
68-
# right type.
69-
if data is None and time is None and time_delta is None:
70-
self._required_columns = ['time']
66+
# For some operations, an empty time series needs to be created, then
67+
# columns added one by one. We should check that when columns are added
68+
# manually, time is added first and is of the right type.
69+
if data is None and time is None and time_start is None and time_delta is None:
70+
self._required_columns_relax = True
7171
return
7272

7373
# First if time has been given in the table data, we should extract it
@@ -84,7 +84,6 @@ def __init__(self, data=None, *, time=None, time_start=None,
8484
if 'time' in self.colnames:
8585
if time is None:
8686
time = self.columns['time']
87-
self.remove_column('time')
8887
else:
8988
raise TypeError("'time' has been given both in the table and as a keyword argument")
9089

@@ -130,7 +129,10 @@ def __init__(self, data=None, *, time=None, time_start=None,
130129
raise TypeError("'time_delta' should not be specified since "
131130
"'time' is an array")
132131

133-
self.add_column(time, index=0, name='time')
132+
with self._delay_required_column_checks():
133+
if 'time' in self.colnames:
134+
self.remove_column('time')
135+
self.add_column(time, index=0, name='time')
134136

135137
@property
136138
def time(self):
@@ -155,7 +157,6 @@ def fold(self, period=None, midpoint_epoch=None):
155157
"""
156158

157159
folded = self.copy()
158-
folded.remove_column('time')
159160

160161
if midpoint_epoch is None:
161162
midpoint_epoch = self.time[0]
@@ -167,7 +168,9 @@ def fold(self, period=None, midpoint_epoch=None):
167168

168169
folded_time = TimeDelta(relative_time_sec * u.s)
169170

170-
folded.add_column(folded_time, name='time', index=0)
171+
with folded._delay_required_column_checks():
172+
folded.remove_column('time')
173+
folded.add_column(folded_time, name='time', index=0)
171174

172175
return folded
173176

‎astropy/timeseries/tests/test_binned.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def test_empty_initialization_invalid():
2525
ts = BinnedTimeSeries()
2626
with pytest.raises(ValueError) as exc:
2727
ts['flux'] = [1, 2, 3]
28-
assert exc.value.args[0] == ("BinnedTimeSeries requires a column called "
29-
"'time_bin_start' to be set before data can be added")
28+
assert exc.value.args[0] == ("BinnedTimeSeries object is invalid - expected "
29+
"'time_bin_start' as the first column but found 'flux'")
3030

3131

3232
def test_initialization_time_bin_invalid():

‎astropy/timeseries/tests/test_common.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3+
import pytest
4+
35
from numpy.testing import assert_equal
46

57
from astropy import units as u
@@ -42,6 +44,15 @@ def test_add_column(self):
4244
def test_add_row(self):
4345
self.series.add_row(self._row)
4446

47+
def test_required_after_stacking(self):
48+
# When stacking, we have to temporarily relax the checking of the
49+
# columns in the time series, but we need to make sure that the
50+
# checking works again afterwards
51+
ts = vstack([self.series, self.series])
52+
with pytest.raises(ValueError) as exc:
53+
ts.remove_columns(ts.colnames)
54+
assert 'TimeSeries object is invalid' in exc.value.args[0]
55+
4556

4657
class TestTimeSeries(CommonTimeSeriesTests):
4758

‎astropy/timeseries/tests/test_sampled.py‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from numpy.testing import assert_equal, assert_allclose
88

9-
from astropy.table import Table
9+
from astropy.table import Table, Column
1010
from astropy.time import Time, TimeDelta
1111
from astropy import units as u
1212
from astropy.utils.data import get_pkg_data_filename
@@ -31,8 +31,8 @@ def test_empty_initialization_invalid():
3131
ts = TimeSeries()
3232
with pytest.raises(ValueError) as exc:
3333
ts['flux'] = [1, 2, 3]
34-
assert exc.value.args[0] == ("TimeSeries requires a column called "
35-
"'time' to be set before data can be added")
34+
assert exc.value.args[0] == ("TimeSeries object is invalid - expected "
35+
"'time' as the first column but found 'flux'")
3636

3737

3838
def test_initialize_only_time():
@@ -230,3 +230,50 @@ def test_tess_astropy():
230230
assert timeseries["sap_flux"].unit.to_string() == 'electron / s'
231231
assert len(timeseries) == 19261
232232
assert len(timeseries.columns) == 20
233+
234+
235+
def test_required_columns():
236+
237+
# Test the machinery that makes sure that the required columns are present
238+
239+
ts = TimeSeries(time=INPUT_TIME,
240+
data=[[10, 2, 3], [4, 5, 6]],
241+
names=['a', 'b'])
242+
243+
# In the examples below, the operation (e.g. remove_column) is actually
244+
# carried out before the checks are made, so we need to use copy() so that
245+
# we don't change the main version of the time series.
246+
247+
# Make sure copy works fine
248+
ts.copy()
249+
250+
with pytest.raises(ValueError) as exc:
251+
ts.copy().add_column(Column([3, 4, 5], name='c'), index=0)
252+
assert exc.value.args[0] == ("TimeSeries object is invalid - expected "
253+
"'time' as the first column but found 'c'")
254+
255+
with pytest.raises(ValueError) as exc:
256+
ts.copy().add_columns([Column([3, 4, 5], name='d'),
257+
Column([3, 4, 5], name='e')], indexes=[0, 1])
258+
assert exc.value.args[0] == ("TimeSeries object is invalid - expected "
259+
"'time' as the first column but found 'd'")
260+
261+
with pytest.raises(ValueError) as exc:
262+
ts.copy().keep_columns(['a', 'b'])
263+
assert exc.value.args[0] == ("TimeSeries object is invalid - expected "
264+
"'time' as the first column but found 'a'")
265+
266+
with pytest.raises(ValueError) as exc:
267+
ts.copy().remove_column('time')
268+
assert exc.value.args[0] == ("TimeSeries object is invalid - expected "
269+
"'time' as the first column but found 'a'")
270+
271+
with pytest.raises(ValueError) as exc:
272+
ts.copy().remove_columns(['time', 'a'])
273+
assert exc.value.args[0] == ("TimeSeries object is invalid - expected "
274+
"'time' as the first column but found 'b'")
275+
276+
with pytest.raises(ValueError) as exc:
277+
ts.copy().rename_column('time', 'banana')
278+
assert exc.value.args[0] == ("TimeSeries object is invalid - expected "
279+
"'time' as the first column but found 'banana'")

0 commit comments

Comments
 (0)