~ubuntu-branches/ubuntu/precise/python-chaco/precise

« back to all changes in this revision

Viewing changes to enthought/chaco/scales/time_scale_test_case.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2008-12-29 02:34:05 UTC
  • Revision ID: james.westby@ubuntu.com-20081229023405-x7i4kp9mdxzmdnvu
Tags: upstream-3.0.1
ImportĀ upstreamĀ versionĀ 3.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from itertools import starmap
 
3
from datetime import datetime as DT
 
4
 
 
5
from scales import ScaleSystem
 
6
from time_scale import dt_to_sec, trange, TimeScale, HMSScales
 
7
from formatters import TimeFormatter
 
8
 
 
9
from scales_test_case import TicksTestCase
 
10
 
 
11
 
 
12
def DTS(*args, **kw):
 
13
    """ Returns a unix-timestamp-like time """
 
14
    return dt_to_sec(DT(*args, **kw))
 
15
 
 
16
def sec_from_hms(start, *times):
 
17
    """ Returns a list of times based on adding each offset tuple in times
 
18
    to the start time (which should be in seconds).  Offset tuples can be
 
19
    in any of the forms: (hours), (hours,minutes), or (hours,minutes,seconds).
 
20
    """
 
21
    ret = []
 
22
    for t in times:
 
23
        cur = 0
 
24
        if len(t) > 0:
 
25
            cur += t[0] * 3600
 
26
        if len(t) > 1:
 
27
            cur += t[1] * 60
 
28
        if len(t) > 2:
 
29
            cur += t[2]
 
30
        ret.append(start+cur)
 
31
    return ret
 
32
 
 
33
 
 
34
class TRangeTestCase(TicksTestCase):
 
35
 
 
36
    def test_null_ranges(self):
 
37
        ranges = (
 
38
            ((2005,3,15,10,23,15), (2005,3,15,10,23,45), {"minutes":1}),
 
39
            ((2005,3,15,10,23), (2005,3,15,10,47), {"hours":1}),
 
40
            ((2005,3,15,5,23), (2005,3,15,18,43), {"days":1}),
 
41
            ((2005,3,15,10,30), (2005,12,25,18,30), {"years":1})
 
42
            )
 
43
        for start, end, kw in ranges:
 
44
            self.assert_empty(trange(DTS(*start), DTS(*end), **kw))
 
45
        return
 
46
 
 
47
    def test_microseconds(self):
 
48
        base = DTS(2005, 3, 15, 10, 45, 10)
 
49
        start = base + 0.0000027
 
50
        end = base + 0.0000088
 
51
        ticks = trange(start, end, microseconds=1)
 
52
        desired = [base+i for i in (3e-6, 4e-6, 5e-6, 6e-6, 7e-6, 8e-6)]
 
53
        self.check_ticks(ticks, desired)
 
54
 
 
55
    def test_milliseconds(self):
 
56
        base = DTS(2005, 3, 15, 10, 45, 10)
 
57
        start = base + 0.0028
 
58
        end = base + 0.0075
 
59
        ticks = trange(start, end, milliseconds=1)
 
60
        desired = [base + i for i in (0.003, 0.004, 0.005, 0.006, 0.007)]
 
61
        self.check_ticks(ticks, desired)
 
62
        ticks = trange(start, end, milliseconds=2)
 
63
        self.check_ticks(ticks, (base+0.004, base+0.006))
 
64
 
 
65
    def test_daily(self):
 
66
        base = DTS(2005, 1, 1)
 
67
        secs_per_day = 24*3600
 
68
        ticks = trange(base, base + secs_per_day*5, days=1)
 
69
        self.check_ticks(ticks, [base+i*secs_per_day for i in range(5)])
 
70
 
 
71
    def test_daily_leap(self):
 
72
        start = DTS(2004, 2, 27)
 
73
        end = DTS(2004, 3, 2)
 
74
        ticks = trange(start, end, days=1)
 
75
        desired = [start + i*24*3600 for i in range(5)]
 
76
        self.check_ticks(ticks, desired)
 
77
 
 
78
    def test_hourly(self):
 
79
        # test between Feb 29,2004 10:15pm and Mar 1st 3:15am
 
80
        ticks = trange(DTS(2004,2,29,22,15), DTS(2004,3,1,3,15), hours=1)
 
81
        start = DTS(2004,2,29,23)
 
82
        desired = [start + i*3600 for i in range(5)]
 
83
        self.check_ticks(ticks, desired)
 
84
 
 
85
    def test_multiday_increment(self):
 
86
        start = DTS(2005, 1, 1)
 
87
        ticks = trange(start, start + 9*24*3600, days=3)
 
88
        self.check_ticks(ticks, [start+i*3*24*3600 for i in range(3)])
 
89
 
 
90
 
 
91
 
 
92
class TimeScaleTestCase(TicksTestCase):
 
93
    """ This exercises a single TimeScale set at various resolutions """
 
94
 
 
95
    def test_hourly(self):
 
96
        ts = TimeScale(hours=1)
 
97
        start = DTS(2005, 3, 15, 10, 30)
 
98
        end = DTS(2005, 3, 15, 16, 59)
 
99
        desired_start = DTS(2005, 3, 15)
 
100
        desired = [desired_start + i*3600 for i in (11, 12, 13, 14, 15, 16)]
 
101
        self.check_ticks(ts.ticks(start, end), desired)
 
102
 
 
103
    def test_minutes(self):
 
104
        ts = TimeScale(minutes=15)
 
105
        start = DTS(2005, 3, 15, 10, 20)
 
106
        end = DTS(2005, 3, 15, 11, 55)
 
107
        dstart = DTS(2005, 3, 15)
 
108
        desired = ((10,30), (10,45), (11,00), (11,15), (11,30), (11,45))
 
109
        self.check_ticks(ts.ticks(start, end),
 
110
                                sec_from_hms(dstart, *desired))
 
111
 
 
112
    def test_day_of_month(self):
 
113
        ts = TimeScale(day_of_month=(1,8,15,22))
 
114
        start = DTS(2005,3,12)
 
115
        end = DTS(2005,5,3)
 
116
        desired = starmap(DTS, ((2005,3,15), (2005,3,22), (2005,4,1), (2005,4,8),
 
117
                                (2005,4,15), (2005,4,22), (2005,5,1)))
 
118
        self.check_ticks(ts.ticks(start,end), desired)
 
119
 
 
120
        # test adjacent months
 
121
        start = DTS(2005, 3, 12)
 
122
        end = DTS(2005, 4, 10)
 
123
        desired = starmap(DTS, ((2005,3,15), (2005,3,22), (2005,4,1), (2005,4,8)))
 
124
        self.check_ticks(ts.ticks(start,end), desired)
 
125
 
 
126
 
 
127
    def test_month_of_year(self):
 
128
        ts = TimeScale(month_of_year=(1,4,8))
 
129
        start = DTS(2005,1,1)
 
130
        end = DTS(2006,5,1)
 
131
        desired = starmap(DTS, ((2005,1,1), (2005,4,1), (2005,8,1), (2006,1,1), (2006,4,1)))
 
132
        self.check_ticks(ts.ticks(start,end), desired)
 
133
 
 
134
    def test_microsecond(self):
 
135
        ts = TimeScale(microseconds=1)
 
136
        base = DTS(1975, 3, 15, 10, 45, 10)
 
137
        start = base + 2.8e-6
 
138
        end = start + 9.2e-6
 
139
        desired = [base+i for i in (3e-6, 4e-6, 5e-6, 6e-6, 7e-6, 8e-6, 9e-6)]
 
140
        self.check_ticks(ts.ticks(start, end), desired)
 
141
        
 
142
 
 
143
 
 
144
class CalendarScaleSystemTestCase(TicksTestCase):
 
145
    """ This exercises the ability of multiple TimeScale objects to play well
 
146
    within a single ScaleSystem.
 
147
    """
 
148
    
 
149
    def test_hourly_scales(self):
 
150
        scales = [TimeScale(seconds=dt) for dt in (1, 5, 15, 30)] + \
 
151
                 [TimeScale(minutes=dt) for dt in (1, 5, 15, 30)] + \
 
152
                 [TimeScale(hours=dt) for dt in (1, 2, 3, 4, 6, 12)]
 
153
 
 
154
    def test_yearly_scales(self):
 
155
        ticker = ScaleSystem(TimeScale(month_of_year=1), default_scale=None)
 
156
        ticks = ticker.ticks(DTS(2000,1,1), DTS(2007,1,1), 10)
 
157
        desired = starmap(DTS, ((2000,1,1), (2001,1,1), (2002,1,1), (2003,1,1),
 
158
                                (2004,1,1), (2005,1,1), (2006,1,1), (2007,1,1)))
 
159
        self.check_ticks(ticks, desired)
 
160
 
 
161
 
 
162
class TimeFormatterTestCase(TicksTestCase):
 
163
 
 
164
    def test_widths(self):
 
165
        fmt = TimeFormatter()
 
166
        scale = TimeScale(minutes = 5)
 
167
        test_intervals = ([(2005,3,15,10,30), (2005,3,15,10,50), 50],
 
168
                          )
 
169
        print
 
170
        for start, end, width in test_intervals:
 
171
            est_width = scale.label_width(DTS(*start), DTS(*end), char_width=width)
 
172
            print start, end,
 
173
            print " avail:", width, "est:", est_width[1], "numlabels:", est_width[0]
 
174
        return
 
175
 
 
176
    def test_labels(self):
 
177
        fmt = TimeFormatter()
 
178
        scale = ScaleSystem(*HMSScales)
 
179
 
 
180
        test_intervals = ([(2005,3,15,10,30), (2005,3,15,10,50), 150],
 
181
                          )
 
182
        print
 
183
        for start, end, width in test_intervals:
 
184
            labels = scale.labels(DTS(*start), DTS(*end), char_width=width)
 
185
            print start, end, " avail:", width,
 
186
            print " used:", sum([len(x[1]) for x in labels]),
 
187
            print labels
 
188
        return
 
189
 
 
190
 
 
191
if __name__ == "__main__":
 
192
    import nose
 
193
    nose.run()