util_test.py 12.8 KB
Newer Older
Christopher Shallue's avatar
Christopher Shallue committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Copyright 2018 The TensorFlow Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for util.py."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from absl.testing import absltest
import numpy as np

24
from light_curve import periodic_event
Christopher Shallue's avatar
Christopher Shallue committed
25

26
from light_curve import util
Christopher Shallue's avatar
Christopher Shallue committed
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


class LightCurveUtilTest(absltest.TestCase):

  def testPhaseFoldTime(self):
    time = np.arange(0, 2, 0.1)

    # Simple.
    tfold = util.phase_fold_time(time, period=1, t0=0.45)
    expected = [
        -0.45, -0.35, -0.25, -0.15, -0.05, 0.05, 0.15, 0.25, 0.35, 0.45, -0.45,
        -0.35, -0.25, -0.15, -0.05, 0.05, 0.15, 0.25, 0.35, 0.45
    ]
    self.assertSequenceAlmostEqual(expected, tfold)

    # Large t0.
    tfold = util.phase_fold_time(time, period=1, t0=1.25)
    expected = [
        -0.25, -0.15, -0.05, 0.05, 0.15, 0.25, 0.35, 0.45, -0.45, -0.35, -0.25,
        -0.15, -0.05, 0.05, 0.15, 0.25, 0.35, 0.45, -0.45, -0.35
    ]
    self.assertSequenceAlmostEqual(expected, tfold)

    # Negative t0.
    tfold = util.phase_fold_time(time, period=1, t0=-1.65)
    expected = [
        -0.35, -0.25, -0.15, -0.05, 0.05, 0.15, 0.25, 0.35, 0.45, -0.45, -0.35,
        -0.25, -0.15, -0.05, 0.05, 0.15, 0.25, 0.35, 0.45, -0.45
    ]
    self.assertSequenceAlmostEqual(expected, tfold)

    # Negative time.
    time = np.arange(-3, -1, 0.1)
    tfold = util.phase_fold_time(time, period=1, t0=0.55)
    expected = [
        0.45, -0.45, -0.35, -0.25, -0.15, -0.05, 0.05, 0.15, 0.25, 0.35, 0.45,
        -0.45, -0.35, -0.25, -0.15, -0.05, 0.05, 0.15, 0.25, 0.35
    ]
    self.assertSequenceAlmostEqual(expected, tfold)

  def testSplit(self):
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    # Single segment.
    all_time = np.concatenate([np.arange(0, 1, 0.1), np.arange(1.5, 2, 0.1)])
    all_flux = np.ones(15)

    # Gap width 0.5.
    split_time, split_flux = util.split(all_time, all_flux, gap_width=0.5)
    self.assertLen(split_time, 2)
    self.assertLen(split_flux, 2)
    self.assertSequenceAlmostEqual(np.arange(0, 1, 0.1), split_time[0])
    self.assertSequenceAlmostEqual(np.ones(10), split_flux[0])
    self.assertSequenceAlmostEqual(np.arange(1.5, 2, 0.1), split_time[1])
    self.assertSequenceAlmostEqual(np.ones(5), split_flux[1])

    # Multi segment.
Christopher Shallue's avatar
Christopher Shallue committed
82
83
84
85
86
    all_time = [
        np.concatenate([
            np.arange(0, 1, 0.1),
            np.arange(1.5, 2, 0.1),
            np.arange(3, 4, 0.1)
87
88
        ]),
        np.arange(4, 5, 0.1)
Christopher Shallue's avatar
Christopher Shallue committed
89
    ]
90
91
    all_flux = [np.ones(25), np.ones(10)]

92
93
94
    self.assertLen(all_time, 2)
    self.assertLen(all_time[0], 25)
    self.assertLen(all_time[1], 10)
95

96
97
98
    self.assertLen(all_flux, 2)
    self.assertLen(all_flux[0], 25)
    self.assertLen(all_flux[1], 10)
Christopher Shallue's avatar
Christopher Shallue committed
99
100
101

    # Gap width 0.5.
    split_time, split_flux = util.split(all_time, all_flux, gap_width=0.5)
102
103
104
105
106
107
108
109
110
111
    self.assertLen(split_time, 4)
    self.assertLen(split_flux, 4)
    self.assertSequenceAlmostEqual(np.arange(0, 1, 0.1), split_time[0])
    self.assertSequenceAlmostEqual(np.ones(10), split_flux[0])
    self.assertSequenceAlmostEqual(np.arange(1.5, 2, 0.1), split_time[1])
    self.assertSequenceAlmostEqual(np.ones(5), split_flux[1])
    self.assertSequenceAlmostEqual(np.arange(3, 4, 0.1), split_time[2])
    self.assertSequenceAlmostEqual(np.ones(10), split_flux[2])
    self.assertSequenceAlmostEqual(np.arange(4, 5, 0.1), split_time[3])
    self.assertSequenceAlmostEqual(np.ones(10), split_flux[3])
Christopher Shallue's avatar
Christopher Shallue committed
112
113
114

    # Gap width 1.0.
    split_time, split_flux = util.split(all_time, all_flux, gap_width=1)
115
116
    self.assertLen(split_time, 3)
    self.assertLen(split_flux, 3)
Christopher Shallue's avatar
Christopher Shallue committed
117
118
119
    self.assertSequenceAlmostEqual([
        0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.5, 1.6, 1.7, 1.8, 1.9
    ], split_time[0])
120
121
122
123
124
    self.assertSequenceAlmostEqual(np.ones(15), split_flux[0])
    self.assertSequenceAlmostEqual(np.arange(3, 4, 0.1), split_time[1])
    self.assertSequenceAlmostEqual(np.ones(10), split_flux[1])
    self.assertSequenceAlmostEqual(np.arange(4, 5, 0.1), split_time[2])
    self.assertSequenceAlmostEqual(np.ones(10), split_flux[2])
Christopher Shallue's avatar
Christopher Shallue committed
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

  def testRemoveEvents(self):
    time = np.arange(20, dtype=np.float)
    flux = 10 * time

    # One event.
    events = [periodic_event.Event(period=4, duration=1.5, t0=3.5)]
    output_time, output_flux = util.remove_events(time, flux, events)
    self.assertSequenceAlmostEqual([1, 2, 5, 6, 9, 10, 13, 14, 17, 18],
                                   output_time)
    self.assertSequenceAlmostEqual(
        [10, 20, 50, 60, 90, 100, 130, 140, 170, 180], output_flux)

    # Two events.
    events.append(periodic_event.Event(period=7, duration=1.5, t0=6.5))
    output_time, output_flux = util.remove_events(time, flux, events)
    self.assertSequenceAlmostEqual([1, 2, 5, 9, 10, 17, 18], output_time)
    self.assertSequenceAlmostEqual([10, 20, 50, 90, 100, 170, 180], output_flux)

    # Multi segment light curve.
    time = [np.arange(10, dtype=np.float), np.arange(10, 20, dtype=np.float)]
    flux = [10 * t for t in time]
    output_time, output_flux = util.remove_events(time, flux, events)
    self.assertLen(output_time, 2)
    self.assertLen(output_flux, 2)
    self.assertSequenceAlmostEqual([1, 2, 5, 9], output_time[0])
    self.assertSequenceAlmostEqual([10, 20, 50, 90], output_flux[0])
    self.assertSequenceAlmostEqual([10, 17, 18], output_time[1])
    self.assertSequenceAlmostEqual([100, 170, 180], output_flux[1])

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    # One segment totally removed with include_empty_segments = True.
    time = [np.arange(5, dtype=np.float), np.arange(10, 20, dtype=np.float)]
    flux = [10 * t for t in time]
    events = [periodic_event.Event(period=10, duration=2, t0=2.5)]
    output_time, output_flux = util.remove_events(
        time, flux, events, width_factor=3, include_empty_segments=True)
    self.assertLen(output_time, 2)
    self.assertLen(output_flux, 2)
    self.assertSequenceEqual([], output_time[0])
    self.assertSequenceEqual([], output_flux[0])
    self.assertSequenceAlmostEqual([16, 17, 18, 19], output_time[1])
    self.assertSequenceAlmostEqual([160, 170, 180, 190], output_flux[1])

    # One segment totally removed with include_empty_segments = False.
    time = [np.arange(5, dtype=np.float), np.arange(10, 20, dtype=np.float)]
    flux = [10 * t for t in time]
    events = [periodic_event.Event(period=10, duration=2, t0=2.5)]
    output_time, output_flux = util.remove_events(
        time, flux, events, width_factor=3, include_empty_segments=False)
    self.assertLen(output_time, 1)
    self.assertLen(output_flux, 1)
    self.assertSequenceAlmostEqual([16, 17, 18, 19], output_time[0])
    self.assertSequenceAlmostEqual([160, 170, 180, 190], output_flux[0])

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
  def testInterpolateMissingTime(self):
    # Fewer than 2 finite values.
    with self.assertRaises(ValueError):
      util.interpolate_missing_time(np.array([]))
    with self.assertRaises(ValueError):
      util.interpolate_missing_time(np.array([5.0]))
    with self.assertRaises(ValueError):
      util.interpolate_missing_time(np.array([5.0, np.nan]))
    with self.assertRaises(ValueError):
      util.interpolate_missing_time(np.array([np.nan, np.nan, np.nan]))

    # Small time arrays.
    self.assertSequenceAlmostEqual([0.5, 0.6],
                                   util.interpolate_missing_time(
                                       np.array([0.5, 0.6])))
    self.assertSequenceAlmostEqual([0.5, 0.6, 0.7],
                                   util.interpolate_missing_time(
                                       np.array([0.5, np.nan, 0.7])))

    # Time array of length 20 with some values NaN.
    time = np.array([
        np.nan, 0.5, 1.0, 1.5, 2.0, 2.5, np.nan, 3.5, 4.0, 4.5, 5.0, np.nan,
        np.nan, np.nan, np.nan, 7.5, 8.0, 8.5, np.nan, np.nan
    ])
    interp_time = util.interpolate_missing_time(time)
    self.assertSequenceAlmostEqual([
        0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5,
        7.0, 7.5, 8.0, 8.5, 9.0, 9.5
    ], interp_time)

    # Fill with 0.0 for missing values at the beginning and end.
    interp_time = util.interpolate_missing_time(time, fill_value=0.0)
    self.assertSequenceAlmostEqual([
        0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5,
        7.0, 7.5, 8.0, 8.5, 0.0, 0.0
    ], interp_time)

    # Interpolate with cadences.
    cadences = np.array([
        100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
        114, 115, 116, 117, 118, 119
    ])
    interp_time = util.interpolate_missing_time(time, cadences)
    self.assertSequenceAlmostEqual([
        0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5,
        7.0, 7.5, 8.0, 8.5, 9.0, 9.5
    ], interp_time)

    # Interpolate with missing cadences.
    time = np.array([0.6, 0.7, np.nan, np.nan, np.nan, 1.3, 1.4, 1.5])
    cadences = np.array([106, 107, 108, 109, 110, 113, 114, 115])
    interp_time = util.interpolate_missing_time(time, cadences)
    self.assertSequenceAlmostEqual([0.6, 0.7, 0.8, 0.9, 1.0, 1.3, 1.4, 1.5],
                                   interp_time)

Christopher Shallue's avatar
Christopher Shallue committed
234
235
236
237
  def testInterpolateMaskedSpline(self):
    all_time = [
        np.arange(0, 10, dtype=np.float),
        np.arange(10, 20, dtype=np.float),
238
        np.arange(20, 30, dtype=np.float),
Christopher Shallue's avatar
Christopher Shallue committed
239
240
241
242
    ]
    all_masked_time = [
        np.array([0, 1, 2, 3, 8, 9], dtype=np.float),  # No 4, 5, 6, 7
        np.array([10, 11, 12, 13, 14, 15, 16], dtype=np.float),  # No 17, 18, 19
243
        np.array([], dtype=np.float)
Christopher Shallue's avatar
Christopher Shallue committed
244
245
246
247
248
    ]
    all_masked_spline = [2 * t + 100 for t in all_masked_time]

    interp_spline = util.interpolate_masked_spline(all_time, all_masked_time,
                                                   all_masked_spline)
249
    self.assertLen(interp_spline, 3)
Christopher Shallue's avatar
Christopher Shallue committed
250
251
252
253
    self.assertSequenceAlmostEqual(
        [100, 102, 104, 106, 108, 110, 112, 114, 116, 118], interp_spline[0])
    self.assertSequenceAlmostEqual(
        [120, 122, 124, 126, 128, 130, 132, 132, 132, 132], interp_spline[1])
254
    self.assertTrue(np.all(np.isnan(interp_spline[2])))
Christopher Shallue's avatar
Christopher Shallue committed
255

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  def testReshardArrays(self):
    xs = [
        np.array([1, 2, 3]),
        np.array([4]),
        np.array([5, 6, 7, 8, 9]),
        np.array([]),
    ]
    ys = [
        np.array([]),
        np.array([10, 20]),
        np.array([30, 40, 50, 60]),
        np.array([70]),
        np.array([80, 90]),
    ]
    reshard_xs = util.reshard_arrays(xs, ys)
271
    self.assertLen(reshard_xs, 5)
272
273
274
275
276
277
278
279
280
281
282
    np.testing.assert_array_equal([], reshard_xs[0])
    np.testing.assert_array_equal([1, 2], reshard_xs[1])
    np.testing.assert_array_equal([3, 4, 5, 6], reshard_xs[2])
    np.testing.assert_array_equal([7], reshard_xs[3])
    np.testing.assert_array_equal([8, 9], reshard_xs[4])

    with self.assertRaisesRegexp(ValueError,
                                 "xs and ys do not have the same total length"):
      util.reshard_arrays(xs, [np.array([10, 20, 30]), np.array([40, 50])])

  def testUniformCadenceLightCurve(self):
283
284
285
    input_cadence_no = np.array([13, 4, 5, 6, 8, 9, 11, 12])
    input_time = np.array([130, 40, 50, 60, 80, 90, 110, 120])
    input_flux = np.array([1300, 400, 500, 600, 800, np.nan, 1100, 1200])
286
    cadence_no, time, flux, mask = util.uniform_cadence_light_curve(
287
        input_cadence_no, input_time, input_flux)
288
289
290
291
292
293
294
295
296
    np.testing.assert_array_equal([4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
                                  cadence_no)
    np.testing.assert_array_equal([40, 50, 60, 0, 80, 0, 0, 110, 120, 130],
                                  time)
    np.testing.assert_array_equal(
        [400, 500, 600, 0, 800, 0, 0, 1100, 1200, 1300], flux)
    np.testing.assert_array_equal([1, 1, 1, 0, 1, 0, 0, 1, 1, 1], mask)

    # Add duplicate cadence number.
297
298
299
    input_cadence_no = np.concatenate([input_cadence_no, np.array([13, 14])])
    input_time = np.concatenate([input_time, np.array([130, 140])])
    input_flux = np.concatenate([input_flux, np.array([1300, 1400])])
300
    with self.assertRaisesRegexp(ValueError, "Duplicate cadence number"):
301
      util.uniform_cadence_light_curve(input_cadence_no, input_time, input_flux)
302

Christopher Shallue's avatar
Christopher Shallue committed
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  def testCountTransitPoints(self):
    time = np.concatenate([
        np.arange(0, 10, 0.1, dtype=np.float),
        np.arange(15, 30, 0.1, dtype=np.float),
        np.arange(50, 100, 0.1, dtype=np.float)
    ])
    event = periodic_event.Event(period=10, duration=5, t0=9.95)

    points_in_transit = util.count_transit_points(time, event)
    np.testing.assert_array_equal([25, 50, 25, 0, 25, 50, 50, 50, 50],
                                  points_in_transit)


if __name__ == "__main__":
  absltest.main()