~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to chaco/scales/tests/test_time_scale.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
 
3
 
import time
4
 
 
5
 
import numpy as np
6
 
 
7
 
from chaco.scales.time_scale import tfrac, trange, \
8
 
        TimeScale, CalendarScaleSystem
9
 
 
10
 
#----------------------------------------------------------------
11
 
# tfrac tests
12
 
#----------------------------------------------------------------
13
 
 
14
 
def test_tfrac_days_01():
15
 
    # Not sure this test is useful. --WW
16
 
    t = time.mktime(time.gmtime(0))
17
 
    (base, frac) = tfrac(t, days=1)
18
 
    assert base == t
19
 
    assert frac == 0.0
20
 
 
21
 
def test_tfrac_days_02():
22
 
    # Not sure this test is useful. --WW
23
 
    t = time.mktime(time.gmtime(0)) + 3*24.0*3600 + 1000.0
24
 
    (base, frac) = tfrac(t, days=1)
25
 
    assert base == t - 1000.0
26
 
    assert frac == 1000.0
27
 
 
28
 
def test_tfrac_hours_01():
29
 
    t = 3601
30
 
    (base, frac) = tfrac(t, hours=1)
31
 
    assert base == 3600
32
 
    assert frac == 1
33
 
 
34
 
def test_tfrac_hours_02():
35
 
    t = 3601
36
 
    (base, frac) = tfrac(t, hours=2)
37
 
    assert base == 0
38
 
    assert frac == 3601
39
 
 
40
 
def test_tfrac_hours_03():
41
 
    t = 3600 * 5.5
42
 
    (base, frac) = tfrac(t, hours=2)
43
 
    assert base == 3600 * 4
44
 
    assert frac == 3600 * 1.5
45
 
 
46
 
def test_tfrac_hours_04():
47
 
    t = 3600 * 5.5
48
 
    (base, frac) = tfrac(t, hours=3)
49
 
    assert base == 3600 * 3.0
50
 
    assert frac == 3600 * 2.5
51
 
 
52
 
def test_tfrac_hours_05():
53
 
    t = 3600 * 15.5
54
 
    (base, frac) = tfrac(t, hours=6)
55
 
    assert base == 3600 * 12.0
56
 
    assert frac == 3600 *  3.5
57
 
 
58
 
def test_tfrac_minutes_01():
59
 
    t = 3601
60
 
    (base, frac) = tfrac(t, minutes=1)
61
 
    assert base == 3600
62
 
    assert frac == 1
63
 
 
64
 
def test_tfrac_minutes_02():
65
 
    t = 123.5
66
 
    (base, frac) = tfrac(t, minutes=1)
67
 
    assert base == 120
68
 
    assert frac == 3.5
69
 
 
70
 
def test_tfrac_seconds_01():
71
 
    t = 3601
72
 
    (base, frac) = tfrac(t, seconds=1)
73
 
    assert base == 3601
74
 
    assert frac == 0
75
 
 
76
 
def test_tfrac_seconds_02():
77
 
    t = 1.75
78
 
    (base, frac) = tfrac(t, seconds=1)
79
 
    assert base == 1
80
 
    assert frac == 0.75
81
 
 
82
 
def test_tfrac_milliseconds_01():
83
 
    t = 123.5
84
 
    (base, frac) = tfrac(t, milliseconds=1)
85
 
    assert base == 123.5
86
 
    assert frac == 0.0
87
 
 
88
 
def test_tfrac_milliseconds_02():
89
 
    t = 10.0625
90
 
    (base, frac) = tfrac(t, milliseconds=1)
91
 
    assert base == 10.062
92
 
    assert frac ==  0.0005
93
 
 
94
 
def test_tfrac_milliseconds_03():
95
 
    t = 10.0625
96
 
    (base, frac) = tfrac(t, milliseconds=10)
97
 
    assert base == 10.06
98
 
    assert frac ==  0.0025
99
 
 
100
 
def test_tfrac_milliseconds_04():
101
 
    t = 1.0078121
102
 
    # Note that the last digit is lost due to rounding to microsecond scale.
103
 
    (base, frac) = tfrac(t, milliseconds=1)
104
 
    print base, frac
105
 
    assert base == 1.007
106
 
    assert frac == 0.000812
107
 
 
108
 
def test_tfrac_milliseconds_05():
109
 
    t = 1.0078056
110
 
    # Note that the last digit is lost due to rounding to microsecond scale.
111
 
    (base, frac) = tfrac(t, milliseconds=1)
112
 
    print base, frac
113
 
    assert base == 1.007
114
 
    assert frac == 0.000806
115
 
 
116
 
 
117
 
#----------------------------------------------------------------
118
 
# trange tests
119
 
#----------------------------------------------------------------
120
 
 
121
 
def test_trange_hours_01():
122
 
    r = trange(0, 1, hours=1)
123
 
    assert r == []
124
 
 
125
 
def test_trange_hours_02():
126
 
    r = trange(-1, 1, hours=1)
127
 
    assert r == [0.0]
128
 
 
129
 
def test_trange_hours_03():
130
 
    r = trange(0, 3600, hours=1)
131
 
    assert r == [0.0, 3600.0]
132
 
 
133
 
def test_trange_hours_04():
134
 
    r = trange(-3600, 3600, hours=1)
135
 
    assert r == [-3600.0, 0.0, 3600.0]
136
 
 
137
 
def test_trange_hours_05():
138
 
    r = trange(-10, 3610, hours=1)
139
 
    assert r == [0.0, 3600.0]
140
 
 
141
 
def test_trange_hours_06():
142
 
    r = trange(-10, 7210, hours=1)
143
 
    assert r == [0.0, 3600.0, 7200.0]
144
 
 
145
 
def test_trange_hours_07():
146
 
    r = trange(-10, 7210, hours=2)
147
 
    assert r == [0.0, 7200.0]
148
 
 
149
 
def test_trange_seconds_01():
150
 
    r = trange(0, 1, seconds=1)
151
 
    assert r == [0.0, 1.0]
152
 
 
153
 
def test_trange_seconds_02():
154
 
    r = trange(0, 10, seconds=1)
155
 
    assert r == range(11)
156
 
 
157
 
def test_trange_seconds_03():
158
 
    r = trange(0, 1.5, seconds=1)
159
 
    assert r == [0.0, 1.0]
160
 
 
161
 
def test_trange_milliseconds_01():
162
 
    r = trange(0, 0.1, milliseconds=1)
163
 
    assert np.allclose(np.array(r), np.linspace(0.0, 0.1, 101)).all()
164
 
 
165
 
def test_trange_milliseconds_02():
166
 
    r = trange(-0.002, 0.001, milliseconds=1)
167
 
    assert np.allclose(np.array(r), np.linspace(-0.002, 0.001, 4))
168
 
 
169
 
 
170
 
#----------------------------------------------------------------
171
 
# TimeScale tests
172
 
#----------------------------------------------------------------
173
 
 
174
 
# Could use more tests here... --WW
175
 
 
176
 
def test_time_scale_seconds_01():
177
 
    ts = TimeScale(seconds=1)
178
 
    ticks = ts.ticks(0, 10)
179
 
    assert (np.array(ticks) == np.linspace(0.0, 10.0, 11)).all()
180
 
 
181
 
def test_time_scale_seconds_02():
182
 
    ts = TimeScale(seconds=2)
183
 
    ticks = ts.ticks(0, 10)
184
 
    assert (np.array(ticks) == np.linspace(0.0, 10.0, 6)).all()
185
 
 
186
 
def test_time_scale_milliseconds_01():
187
 
    ts = TimeScale(milliseconds=1)
188
 
    ticks = ts.ticks(0, 0.1)
189
 
    assert len(ticks) == 11
190
 
    assert (np.array(ticks) == np.linspace(0.0, 0.1, 11)).all()
191
 
 
192
 
#----------------------------------------------------------------
193
 
# CalendarScaleSystem tests
194
 
#----------------------------------------------------------------
195
 
 
196
 
def test_calendar_scale_system_01():
197
 
    css = CalendarScaleSystem()
198
 
    ticks = css.ticks(0,10)
199
 
    assert len(ticks) == 11
200
 
    assert (np.array(ticks) == np.linspace(0,10,11)).all()
201
 
 
202
 
 
203
 
# TODO: Add more tests of the ticks() and labels() methods of
204
 
# the CalendarScaleSystem.
205
 
#
206
 
# Determine why the format switches from '##s' to ':##'
207
 
# as in the following, and create appropriate tests:
208
 
#
209
 
# In [145]: css.labels(71010,71021, numlabels=8, char_width=130)
210
 
# Out[145]:
211
 
# [(71010.0, '30s'),
212
 
#  (71011.0, '31s'),
213
 
#  (71012.0, '32s'),
214
 
#  (71013.0, '33s'),
215
 
#  (71014.0, '34s'),
216
 
#  (71015.0, '35s'),
217
 
#  (71016.0, '36s'),
218
 
#  (71017.0, '37s'),
219
 
#  (71018.0, '38s'),
220
 
#  (71019.0, '39s'),
221
 
#  (71020.0, '40s'),
222
 
#  (71021.0, '41s')]
223
 
#
224
 
# In [146]: css.labels(71010,71022, numlabels=8, char_width=130)
225
 
# Out[146]:
226
 
# [(71010.0, ':30'),
227
 
#  (71011.0, ':31'),
228
 
#  (71012.0, ':32'),
229
 
#  (71013.0, ':33'),
230
 
#  (71014.0, ':34'),
231
 
#  (71015.0, ':35'),
232
 
#  (71016.0, ':36'),
233
 
#  (71017.0, ':37'),
234
 
#  (71018.0, ':38'),
235
 
#  (71019.0, ':39'),
236
 
#  (71020.0, ':40'),
237
 
#  (71021.0, ':41'),
238
 
#  (71022.0, ':42')]
239
 
#
240
 
# In [147]: