energy_market 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,405 @@
1
+ require 'test/unit'
2
+ require 'energy_market'
3
+
4
+ class TestOne < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @array = [3, 6, 7, -34, 5.5, 0, 8]
8
+ @vector = EnergyMarket::Vector.new("2013-01-01", "Rome").data(@array)
9
+ end
10
+
11
+ def test_set_all_to
12
+ v = 1.2
13
+ @vector.set_all_to(v)
14
+ assert_equal(Array.new(@array.size, v), @vector.v)
15
+ end
16
+
17
+ def test_start_time
18
+ assert_equal(Time.parse("2013-01-01 00:00:00"), @vector.start_time)
19
+
20
+ # test time floor
21
+ zone = "Rome" # +1
22
+ vector2 = EnergyMarket::Vector.new("2013-01-01 00:23:45", zone).data(@array)
23
+ assert_equal(@vector.start_time, vector2.start_time)
24
+
25
+ # another timezone!
26
+ zone = "London" # 0
27
+ vector3 = EnergyMarket::Vector.new("2013-01-01 00:23:45", zone).data(@array)
28
+ assert_not_equal(Time.parse("2013-01-01 00:00:00"), vector3.start_time)
29
+ assert_not_equal(@vector.start_time, vector3.start_time)
30
+
31
+ # another timezone!
32
+ zone = "Helsinki" # +2
33
+ vector4 = EnergyMarket::Vector.new("2013-01-01 01:23:45", zone).data(@array)
34
+ assert_equal(Time.parse("2013-01-01 00:00:00"), vector4.start_time)
35
+ assert_equal(@vector.start_time, vector4.start_time)
36
+
37
+ # zone = "London"
38
+ # vector3 = EnergyMarket::Vector.new("2013-05-03 16:23:45", zone, '1day').data(@array)
39
+ # assert_not_equal(Time.parse("2013-05-03 00:00:00"), vector3.start_time)
40
+
41
+ # vector3 = EnergyMarket::Vector.new("2013-05-03 16:23:45", zone, '1month').data(@array)
42
+ # assert_not_equal(Time.parse("2013-05-01 00:00:00"), vector3.start_time)
43
+
44
+ # vector3 = EnergyMarket::Vector.new("2013-05-03 16:23:45", zone, '1year').data(@array)
45
+ # assert_not_equal(Time.parse("2013-01-01 00:00:00"), vector3.start_time)
46
+
47
+ # vector3 = EnergyMarket::Vector.new("2013-05-03 16:23:45", zone, '15minutes').data(@array)
48
+ # assert_not_equal(Time.parse("2013-05-03 16:15:00"), vector3.start_time)
49
+
50
+ assert_nothing_raised do
51
+ EnergyMarket::Vector.new("13")
52
+ EnergyMarket::Vector.new("2013")
53
+ EnergyMarket::Vector.new("2013-01")
54
+ end
55
+ assert_raise ArgumentError do
56
+ EnergyMarket::Vector.new("201301")
57
+ end
58
+ end
59
+
60
+ def test_end_time
61
+ assert_equal(Time.parse("2013-01-01 00:00:00")+(@array.size-1).hours, @vector.end_time)
62
+ assert_equal(Time.parse("2014-01-01 00:00:00")-1.hour, @vector.until_the_end_of_the_year.end_time)
63
+ end
64
+
65
+ def test_size
66
+ assert_equal(@array.size, @vector.size)
67
+ assert_equal(8760, @vector.until_the_end_of_the_year.size)
68
+ end
69
+
70
+ def test_month_data
71
+ size = 12
72
+ start = 23
73
+ array = (start...(start+size)).to_a
74
+ vector_1 = EnergyMarket::Vector.new("2013-01-01", "Rome").data(array, 'month')
75
+ time = vector_1.start_time
76
+ vector_1.v.each do |value|
77
+ assert_equal(array[time.month-vector_1.start_time.month], value)
78
+ time += 1.hour
79
+ end
80
+ end
81
+
82
+ def test_day_data
83
+ size = 365
84
+ start = 17
85
+ array = (start...(start+size)).to_a
86
+ vector_1 = EnergyMarket::Vector.new("2013-01-01", "Rome").data(array, 'day')
87
+ time = vector_1.start_time
88
+ vector_1.v.each do |value|
89
+ assert_equal(array[time.yday-vector_1.start_time.yday], value)
90
+ time += 1.hour
91
+ end
92
+ end
93
+
94
+ def test_year_data
95
+ size = 1
96
+ start = 41
97
+ array = (start...(start+size)).to_a
98
+ vector_1 = EnergyMarket::Vector.new("2013-01-01", "Rome").data(array, 'year')
99
+ time = vector_1.start_time
100
+ vector_1.v.each do |value|
101
+ assert_equal(array[0], value)
102
+ end
103
+
104
+ vector_2 = EnergyMarket::Vector.new("2013-01-01", "Rome").data(5.3, 'year')
105
+ time = vector_2.start_time
106
+ vector_2.v.each do |value|
107
+ assert_equal(5.3, value)
108
+ end
109
+ end
110
+
111
+ def test_elements_sum
112
+ # all elements
113
+ elements_sum = @array.inject(0.0){|total, n| total + n }
114
+ assert_equal(elements_sum, @vector.sum)
115
+ assert_equal(elements_sum, @vector.sum(:values => :all))
116
+ assert_equal(elements_sum, @vector.sum(:values => :not_zero))
117
+
118
+ # i>0 elements
119
+ elements_sum = @array.inject(0.0){|total, n| total + (n>0 ? n : 0.0) }
120
+ assert_equal(elements_sum, @vector.sum(:values => :positive))
121
+
122
+ # i>=0 elements
123
+ elements_sum = @array.inject(0.0){|total, n| total + (n>=0 ? n : 0.0) }
124
+ assert_equal(elements_sum, @vector.sum(:values => :not_negative))
125
+
126
+ # i<0 elements
127
+ elements_sum = @array.inject(0.0){|total, n| total + (n<0 ? n : 0.0) }
128
+ assert_equal(elements_sum, @vector.sum(:values => :negative))
129
+
130
+ # i<=0 elements
131
+ elements_sum = @array.inject(0.0){|total, n| total + (n<=0 ? n : 0.0) }
132
+ assert_equal(elements_sum, @vector.sum(:values => :not_positive))
133
+
134
+ # i==0 elements
135
+ assert_equal(0.0, @vector.sum(:values => :zero))
136
+ end
137
+
138
+
139
+ def test_elements_count
140
+ # all elements
141
+ elements_count = @array.inject(0.0){|total, n| total + 1 }
142
+ assert_equal(elements_count, @vector.count)
143
+ assert_equal(elements_count, @vector.count(:values => :all))
144
+
145
+ # i>0 elements
146
+ elements_count = @array.inject(0){|total, n| total + (n>0.0 ? 1 : 0) }
147
+ assert_equal(elements_count, @vector.count(:values => :positive))
148
+
149
+ # i>=0 elements
150
+ elements_count = @array.inject(0){|total, n| total + (n>=0.0 ? 1 : 0) }
151
+ assert_equal(elements_count, @vector.count(:values => :not_negative))
152
+
153
+ # i<0 elements
154
+ elements_count = @array.inject(0){|total, n| total + (n<0.0 ? 1 : 0) }
155
+ assert_equal(elements_count, @vector.count(:values => :negative))
156
+
157
+ # i<=0 elements
158
+ elements_count = @array.inject(0){|total, n| total + (n<=0.0 ? 1 : 0) }
159
+ assert_equal(elements_count, @vector.count(:values => :not_positive))
160
+
161
+ # i==0 elements
162
+ elements_count = @array.inject(0){|total, n| total + (n==0.0 ? 1 : 0) }
163
+ assert_equal(elements_count, @vector.count(:values => :zero))
164
+
165
+ # i!=0 elements
166
+ elements_count = @array.inject(0){|total, n| total + (n!=0.0 ? 1 : 0) }
167
+ assert_equal(elements_count, @vector.count(:values => :not_zero))
168
+
169
+ end
170
+
171
+ def test_elements_mean
172
+ elements_count = @array.inject(0.0){|total, n| total + 1 }
173
+ elements_sum = @array.inject(0.0){|total, n| total + n }
174
+ assert_equal(elements_sum/elements_count, @vector.mean)
175
+ assert_equal(elements_sum/elements_count, @vector.mean(:values => :all))
176
+
177
+ elements_sum = @array.inject(0.0){|total, n| total + (n>0 ? n : 0.0) }
178
+ elements_count = @array.inject(0){|total, n| total + (n>0.0 ? 1 : 0) }
179
+ assert_equal(elements_sum/elements_count, @vector.mean(:values => :positive))
180
+
181
+ elements_sum = @array.inject(0.0){|total, n| total + (n>=0 ? n : 0.0) }
182
+ elements_count = @array.inject(0){|total, n| total + (n>=0.0 ? 1 : 0) }
183
+ assert_equal(elements_sum/elements_count, @vector.mean(:values => :not_negative))
184
+
185
+ elements_sum = @array.inject(0.0){|total, n| total + (n<0 ? n : 0.0) }
186
+ elements_count = @array.inject(0){|total, n| total + (n<0.0 ? 1 : 0) }
187
+ assert_equal(elements_sum/elements_count, @vector.mean(:values => :negative))
188
+
189
+ elements_sum = @array.inject(0.0){|total, n| total + (n<=0 ? n : 0.0) }
190
+ elements_count = @array.inject(0){|total, n| total + (n<=0.0 ? 1 : 0) }
191
+ assert_equal(elements_sum/elements_count, @vector.mean(:values => :not_positive))
192
+
193
+ assert_equal(0.0, @vector.mean(:values => :zero))
194
+
195
+ elements_sum = @array.inject(0.0){|total, n| total + (n!=0 ? n : 0.0) }
196
+ elements_count = @array.inject(0){|total, n| total + (n!=0.0 ? 1 : 0) }
197
+ assert_equal(elements_sum/elements_count, @vector.mean(:values => :not_zero))
198
+
199
+
200
+ array = [2.0, 3.4, 5.8, 3.1, 0, 0.0]
201
+ vector = EnergyMarket::Vector.new("2013-01-01", "Rome").data(array)
202
+ assert(vector.mean(:values => :negative).nan?)
203
+ # assert_raise ZeroDivisionError do
204
+ # vector.mean(:values => :negative)
205
+ # end
206
+
207
+ end
208
+
209
+ def test_minimum_and_maximum_values
210
+ assert_equal(8, @vector.maximum_value)
211
+ assert_equal(-34, @vector.minimum_value)
212
+ end
213
+
214
+ def test_size
215
+ assert_equal(@array.size, @vector.size)
216
+ assert_equal(@array.size, @vector.count)
217
+ end
218
+
219
+
220
+ def test_round
221
+ array = [2.345643, 3.345643, 5.845643, 3.145643, 0, 0.045643]
222
+ vector = EnergyMarket::Vector.new("2013-01-01", "Rome").data(array)
223
+ assert_equal(array.collect{|e| e.round(2)}, vector.round!(2))
224
+ assert_equal(array.collect{|e| e.round(3)}, vector.round!)
225
+ end
226
+
227
+
228
+ def test_xy_array
229
+ array = [1, 0, 3, -1, -3, 0.5, 0.0]
230
+ vector = EnergyMarket::Vector.new("2013").data(array)
231
+ dt, vv = vector.xy_array
232
+
233
+ aa = []
234
+ array.size.times do |i|
235
+ aa[i] = vector.start_time + i.hours
236
+ end
237
+ assert_equal(aa, dt)
238
+ assert_equal(array, vv)
239
+
240
+
241
+ arr = array.map{|e| e>0.0 ? e : nil}
242
+ dt, vv = vector.xy_array(:values => :positive)
243
+ aa = []
244
+ array.size.times do |i|
245
+ if arr[i].nil?
246
+ aa[i] = nil
247
+ else
248
+ aa[i] = vector.start_time + i.hours
249
+ end
250
+
251
+ end
252
+ assert_equal(aa.compact, dt)
253
+ assert_equal(arr.compact, vv)
254
+
255
+ end
256
+
257
+
258
+ def test_is_aligned_with
259
+ @array = [3, 6, 7, -34, 5.5, 0, 8]
260
+ assert(@vector.aligned_with?(EnergyMarket::Vector.new("2013-01-01", "Rome").data(@array)))
261
+ assert(!@vector.aligned_with?(EnergyMarket::Vector.new("2013-01-01", "London").data(@array)))
262
+ assert(!@vector.aligned_with?(EnergyMarket::Vector.new("2013-01-01", "Rome").data(@array[0...-1])))
263
+ assert(!@vector.aligned_with?(EnergyMarket::Vector.new("2013-01-01 03:00:00", "Rome").data(@array)))
264
+ assert(!@vector.aligned_with?(EnergyMarket::Vector.new("2013-01-01 02:00:00", "Rome").data(@array[0...-2])))
265
+ end
266
+
267
+ def test_align_with
268
+ # Case 1: v1: |----------|
269
+ # v2: |----------|
270
+ arr_1 = (0...10).to_a
271
+ vec_1 = EnergyMarket::Vector.new("2013-01-01 03:00:00").data(arr_1)
272
+ ost = vec_1.start_time.clone
273
+ arr_2 = (0...10).to_a
274
+ vec_2 = EnergyMarket::Vector.new("2013-01-01 01:00:00").data(arr_2)
275
+ vec_1.align_with(vec_2)
276
+ assert_equal(ost, vec_1.start_time)
277
+ assert_equal(8, vec_1.size)
278
+
279
+
280
+ # Case 2: v1: |----------|
281
+ # v2: |----------|
282
+ arr_1 = (0...10).to_a
283
+ vec_1 = EnergyMarket::Vector.new("2013-01-01 01:00:00").data(arr_1)
284
+ arr_2 = (0...10).to_a
285
+ vec_2 = EnergyMarket::Vector.new("2013-01-01 03:00:00").data(arr_2)
286
+ vec_1.align_with(vec_2)
287
+ assert_equal(vec_2.start_time, vec_1.start_time)
288
+ assert_equal(8, vec_1.size)
289
+
290
+ # Case 3: v1: |----------------|
291
+ # v2: |----------|
292
+ arr_1 = (0...20).to_a
293
+ vec_1 = EnergyMarket::Vector.new("2013-01-01 01:00:00").data(arr_1)
294
+ arr_2 = (0...10).to_a
295
+ vec_2 = EnergyMarket::Vector.new("2013-01-01 03:00:00").data(arr_2)
296
+ vec_1.align_with(vec_2)
297
+ assert_equal(vec_2.start_time, vec_1.start_time)
298
+ assert_equal(10, vec_1.size)
299
+
300
+ # Case 4: v1: |----------|
301
+ # v2: |-----------------|
302
+ arr_1 = (0...10).to_a
303
+ vec_1 = EnergyMarket::Vector.new("2013-01-01 03:00:00").data(arr_1)
304
+ ost = vec_1.start_time.clone
305
+ arr_2 = (0...20).to_a
306
+ vec_2 = EnergyMarket::Vector.new("2013-01-01 01:00:00").data(arr_2)
307
+ vec_1.align_with(vec_2)
308
+ assert_equal(ost, vec_1.start_time)
309
+ assert_equal(10, vec_1.size)
310
+
311
+ end
312
+
313
+ def test_binary_artmetical_operations
314
+ # sum
315
+ arr_1 = (0...10).to_a
316
+ vec_1 = EnergyMarket::Vector.new("2013-01-01 01:00:00").data(arr_1)
317
+ arr_2 = (0...10).to_a
318
+ vec_2 = EnergyMarket::Vector.new("2013-01-01 03:00:00").data(arr_2)
319
+ vec_3 = vec_1+vec_2
320
+ assert_equal([2,4,6,8,10,12,14,16], vec_3.v)
321
+ vec_3 = vec_3+2.0
322
+ assert_equal([4,6,8,10,12,14,16,18], vec_3.v)
323
+
324
+
325
+ # subtraction
326
+ arr_1 = (0...10).to_a
327
+ vec_1 = EnergyMarket::Vector.new("2013-01-01 01:00:00").data(arr_1)
328
+ arr_2 = (0...10).to_a
329
+ vec_2 = EnergyMarket::Vector.new("2013-01-01 03:00:00").data(arr_2)
330
+ vec_3 = vec_1-vec_2
331
+ assert_equal([2,2,2,2,2,2,2,2], vec_3.v)
332
+ vec_3 = vec_3-12.0
333
+ assert_equal([-10,-10,-10,-10,-10,-10,-10,-10], vec_3.v)
334
+
335
+ # multiplication
336
+ arr_1 = (0...10).to_a
337
+ vec_1 = EnergyMarket::Vector.new("2013-01-01 01:00:00").data(arr_1)
338
+ arr_2 = (0...10).to_a
339
+ vec_2 = EnergyMarket::Vector.new("2013-01-01 03:00:00").data(arr_2)
340
+ vec_3 = vec_1*vec_2
341
+ # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
342
+ # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
343
+ aa = [0, 3, 8, 15, 24, 35, 48, 63]
344
+ assert_equal(aa, vec_3.v)
345
+ k = 2.34
346
+ vec_3 = vec_3 * k
347
+ aa.collect!{|e| e*k}
348
+ assert_equal(aa, vec_3.v)
349
+
350
+ # ratio
351
+ arr_1 = (0...10).to_a
352
+ vec_1 = EnergyMarket::Vector.new("2013-01-01 01:00:00").data(arr_1)
353
+ arr_2 = (0...10).to_a
354
+ vec_2 = EnergyMarket::Vector.new("2013-01-01 03:00:00").data(arr_2)
355
+ vec_3 = vec_1/vec_2
356
+ aa = [1.0/0, 3.0, 2.0, 5.0/3, 1.5, 1.4, 4.0/3, 9.0/7]
357
+ assert_equal(aa, vec_3.v)
358
+ vec_3 = vec_3 / k
359
+ aa.collect!{|e| e/k}
360
+ assert_equal(aa, vec_3.v)
361
+
362
+ end
363
+
364
+
365
+ def test_max_and_min
366
+ arr_1 = [3.1, 2, 0, 8.6, -1.1, 0.8, -7.7, -2.3, 1, -3]
367
+ vec_1 = EnergyMarket::Vector.new("2013-01-01 01:00:00").data(arr_1)
368
+ arr_2 = [3.1, 2, 0, 8.6, -1.1, 0.8, -7.7, -2.3, 1, -3]
369
+ vec_2 = EnergyMarket::Vector.new("2013-01-01 03:00:00").data(arr_2)
370
+ # [3.1, 2, 0, 8.6, -1.1, 0.8, -7.7, -2.3, 1, -3]
371
+ # [3.1, 2, 0, 8.6, -1.1, 0.8, -7.7, -2.3, 1, -3]
372
+ vec_3 = vec_1.max(vec_2)
373
+ assert_equal([3.1, 8.6, 0, 8.6, -1.1, 0.8, 1, -2.3], vec_3.v)
374
+
375
+ vec_3 = vec_1.max
376
+ assert_equal([3.1, 2, 0, 8.6, 0.0, 0.8, 0.0, 0.0, 1, 0.0], vec_3.v)
377
+
378
+ vec_3 = vec_1.max(1.1)
379
+ assert_equal([3.1, 2, 1.1, 8.6, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1], vec_3.v)
380
+
381
+
382
+ vec_3 = vec_1.min(vec_2)
383
+ assert_equal([0, 2, -1.1, 0.8, -7.7, -2.3, -7.7, -3], vec_3.v)
384
+
385
+ vec_3 = vec_1.min
386
+ assert_equal([0.0, 0.0, 0, 0.0, -1.1, 0.0, -7.7, -2.3, 0.0, -3], vec_3.v)
387
+
388
+ vec_3 = vec_1.min(1.1)
389
+ assert_equal([1.1, 1.1, 0, 1.1, -1.1, 0.8, -7.7, -2.3, 1, -3], vec_3.v)
390
+ end
391
+
392
+
393
+
394
+ # def test_sum
395
+ # @array = [3, 6, 7, -34, 5.5, 0, 8]
396
+ # array = [3.1, 2, 0, 8.6, -1.1, 0.8, -7.7]
397
+ # sum_array = []
398
+ # array.size.times do |i|
399
+ # sum_array[i] = @array[i] + array[i]
400
+ # end
401
+ # vector = EnergyMarket::Vector.new("2013-01-01", "Rome").data(array).sum
402
+ # assert_equal(sum_array, vector.v)
403
+ # end
404
+ end
405
+