daru_lite 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/History.md +30 -0
- data/README.md +1 -1
- data/lib/daru_lite/data_frame/convertible.rb +0 -33
- data/lib/daru_lite/data_frame/fetchable.rb +1 -1
- data/lib/daru_lite/data_frame/i_o_able.rb +0 -102
- data/lib/daru_lite/data_frame/indexable.rb +24 -0
- data/lib/daru_lite/data_frame/relabelable.rb +125 -0
- data/lib/daru_lite/data_frame/sortable.rb +27 -1
- data/lib/daru_lite/dataframe.rb +3 -4
- data/lib/daru_lite/index/categorical_index.rb +8 -0
- data/lib/daru_lite/index/index.rb +0 -1
- data/lib/daru_lite/index/multi_index.rb +8 -5
- data/lib/daru_lite/io/io.rb +0 -195
- data/lib/daru_lite/maths/arithmetic/vector.rb +31 -0
- data/lib/daru_lite/maths/statistics/vector/descriptive.rb +134 -0
- data/lib/daru_lite/maths/statistics/vector/dispersion.rb +120 -0
- data/lib/daru_lite/maths/statistics/vector/extrema.rb +217 -0
- data/lib/daru_lite/maths/statistics/vector/normalization.rb +86 -0
- data/lib/daru_lite/maths/statistics/vector/percentile.rb +64 -0
- data/lib/daru_lite/maths/statistics/vector/sampling.rb +56 -0
- data/lib/daru_lite/maths/statistics/vector/time_series.rb +350 -0
- data/lib/daru_lite/maths/statistics/vector.rb +16 -994
- data/lib/daru_lite/vector/convertible.rb +0 -32
- data/lib/daru_lite/vector.rb +0 -9
- data/lib/daru_lite/version.rb +1 -1
- data/lib/daru_lite.rb +0 -30
- metadata +9 -75
- data/lib/daru_lite/date_time/index.rb +0 -570
- data/lib/daru_lite/date_time/offsets.rb +0 -397
- data/lib/daru_lite/extensions/which_dsl.rb +0 -53
- data/lib/daru_lite/io/sql_data_source.rb +0 -80
- data/lib/daru_lite/iruby/helpers.rb +0 -38
- data/lib/daru_lite/iruby/templates/dataframe.html.erb +0 -5
- data/lib/daru_lite/iruby/templates/dataframe_mi.html.erb +0 -5
- data/lib/daru_lite/iruby/templates/dataframe_mi_tbody.html.erb +0 -35
- data/lib/daru_lite/iruby/templates/dataframe_mi_thead.html.erb +0 -21
- data/lib/daru_lite/iruby/templates/dataframe_tbody.html.erb +0 -28
- data/lib/daru_lite/iruby/templates/dataframe_thead.html.erb +0 -21
- data/lib/daru_lite/iruby/templates/multi_index.html.erb +0 -12
- data/lib/daru_lite/iruby/templates/vector.html.erb +0 -5
- data/lib/daru_lite/iruby/templates/vector_mi.html.erb +0 -5
- data/lib/daru_lite/iruby/templates/vector_mi_tbody.html.erb +0 -26
- data/lib/daru_lite/iruby/templates/vector_mi_thead.html.erb +0 -8
- data/lib/daru_lite/iruby/templates/vector_tbody.html.erb +0 -17
- data/lib/daru_lite/iruby/templates/vector_thead.html.erb +0 -8
|
@@ -1,397 +0,0 @@
|
|
|
1
|
-
module DaruLite
|
|
2
|
-
# Generic class for generating date offsets.
|
|
3
|
-
class DateOffset
|
|
4
|
-
# A DaruLite::DateOffset object is created by a passing certain options
|
|
5
|
-
# to the constructor, which determine the kind of offset the object
|
|
6
|
-
# will support.
|
|
7
|
-
#
|
|
8
|
-
# You can pass one of the following options followed by their number
|
|
9
|
-
# to the DateOffset constructor:
|
|
10
|
-
#
|
|
11
|
-
# * :secs - Create a seconds offset
|
|
12
|
-
# * :mins - Create a minutes offset
|
|
13
|
-
# * :hours - Create an hours offset
|
|
14
|
-
# * :days - Create a days offset
|
|
15
|
-
# * :weeks - Create a weeks offset
|
|
16
|
-
# * :months - Create a months offset
|
|
17
|
-
# * :years - Create a years offset
|
|
18
|
-
#
|
|
19
|
-
# Additionaly, passing the `:n` option will apply the offset that many times.
|
|
20
|
-
#
|
|
21
|
-
# @example Usage of DateOffset
|
|
22
|
-
# # Create an offset of 3 weeks.
|
|
23
|
-
# offset = DaruLite::DateOffset.new(weeks: 3)
|
|
24
|
-
# offset + DateTime.new(2012,5,3)
|
|
25
|
-
# #=> #<DateTime: 2012-05-24T00:00:00+00:00 ((2456072j,0s,0n),+0s,2299161j)>
|
|
26
|
-
#
|
|
27
|
-
# # Create an offset of 5 hours
|
|
28
|
-
# offset = DaruLite::DateOffset.new(hours: 5)
|
|
29
|
-
# offset + DateTime.new(2015,3,3,23,5,1)
|
|
30
|
-
# #=> #<DateTime: 2015-03-04T04:05:01+00:00 ((2457086j,14701s,0n),+0s,2299161j)>
|
|
31
|
-
#
|
|
32
|
-
# # Create an offset of 2 minutes, applied 5 times
|
|
33
|
-
# offset = DaruLite::DateOffset.new(mins: 2, n: 5)
|
|
34
|
-
# offset + DateTime.new(2011,5,3,3,5)
|
|
35
|
-
# #=> #<DateTime: 2011-05-03T03:15:00+00:00 ((2455685j,11700s,0n),+0s,2299161j)>
|
|
36
|
-
def initialize(opts = {})
|
|
37
|
-
n = opts[:n] || 1
|
|
38
|
-
Offsets::LIST.each do |key, klass|
|
|
39
|
-
if opts.key?(key)
|
|
40
|
-
@offset = klass.new(n * opts[key])
|
|
41
|
-
break
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
@offset = Offsets::Day.new(7 * n * opts[:weeks]) if opts[:weeks]
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
# Offset a DateTime forward.
|
|
49
|
-
#
|
|
50
|
-
# @param other [DateTime] A DateTime object which is to offset.
|
|
51
|
-
def +(other)
|
|
52
|
-
@offset + other
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
# Offset a DateTime backward.
|
|
56
|
-
#
|
|
57
|
-
# @param other [DateTime] A DateTime object which is to offset.
|
|
58
|
-
def -(other)
|
|
59
|
-
@offset - other
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def -@
|
|
63
|
-
NegativeDateOffset.new(self)
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
class NegativeDateOffset
|
|
68
|
-
def initialize(offset)
|
|
69
|
-
@offset = offset
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def +(other)
|
|
73
|
-
@offset - other
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
def -(other)
|
|
77
|
-
@offset + other
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
def -@
|
|
81
|
-
@offset
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
module Offsets
|
|
86
|
-
class DateOffsetType < DateOffset
|
|
87
|
-
# @!method initialize(n)
|
|
88
|
-
# Initialize one of the subclasses of DateOffsetType with the number of the times
|
|
89
|
-
# the offset should be applied, which is the supplied as the argument.
|
|
90
|
-
#
|
|
91
|
-
# @param n [Integer] The number of times an offset should be applied.
|
|
92
|
-
def initialize(n = 1)
|
|
93
|
-
@n = n
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
def freq_string
|
|
97
|
-
(@n == 1 ? '' : @n.to_s) + self.class::FREQ
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
# Private superclass for Offsets with equal inter-frequencies.
|
|
102
|
-
# @abstract
|
|
103
|
-
# @private
|
|
104
|
-
class Tick < DateOffsetType
|
|
105
|
-
def +(other)
|
|
106
|
-
other + (@n * multiplier)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def -(other)
|
|
110
|
-
other - (@n * multiplier)
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
def ==(other)
|
|
114
|
-
other.is_a?(Tick) && period == other.period
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
def period
|
|
118
|
-
@n * multiplier
|
|
119
|
-
end
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
# Create a seconds offset
|
|
123
|
-
#
|
|
124
|
-
# @param n [Integer] The number of times an offset should be applied.
|
|
125
|
-
# @example Create a Seconds offset
|
|
126
|
-
# offset = DaruLite::Offsets::Second.new(5)
|
|
127
|
-
# offset + DateTime.new(2012,5,1,4,3)
|
|
128
|
-
# #=> #<DateTime: 2012-05-01T04:03:05+00:00 ((2456049j,14585s,0n),+0s,2299161j)>
|
|
129
|
-
class Second < Tick
|
|
130
|
-
FREQ = 'S'.freeze
|
|
131
|
-
|
|
132
|
-
def multiplier
|
|
133
|
-
1.to_r / 24 / 60 / 60
|
|
134
|
-
end
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
# Create a minutes offset
|
|
138
|
-
#
|
|
139
|
-
# @param n [Integer] The number of times an offset should be applied.
|
|
140
|
-
# @example Create a Minutes offset
|
|
141
|
-
# offset = DaruLite::Offsets::Minute.new(8)
|
|
142
|
-
# offset + DateTime.new(2012,5,1,4,3)
|
|
143
|
-
# #=> #<DateTime: 2012-05-01T04:11:00+00:00 ((2456049j,15060s,0n),+0s,2299161j)>
|
|
144
|
-
class Minute < Tick
|
|
145
|
-
FREQ = 'M'.freeze
|
|
146
|
-
|
|
147
|
-
def multiplier
|
|
148
|
-
1.to_r / 24 / 60
|
|
149
|
-
end
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
# Create an hours offset
|
|
153
|
-
#
|
|
154
|
-
# @param n [Integer] The number of times an offset should be applied.
|
|
155
|
-
# @example Create a Hour offset
|
|
156
|
-
# offset = DaruLite::Offsets::Hour.new(8)
|
|
157
|
-
# offset + DateTime.new(2012,5,1,4,3)
|
|
158
|
-
# #=> #<DateTime: 2012-05-01T12:03:00+00:00 ((2456049j,43380s,0n),+0s,2299161j)>
|
|
159
|
-
class Hour < Tick
|
|
160
|
-
FREQ = 'H'.freeze
|
|
161
|
-
|
|
162
|
-
def multiplier
|
|
163
|
-
1.to_r / 24
|
|
164
|
-
end
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
# Create an days offset
|
|
168
|
-
#
|
|
169
|
-
# @param n [Integer] The number of times an offset should be applied.
|
|
170
|
-
# @example Create a Day offset
|
|
171
|
-
# offset = DaruLite::Offsets::Day.new(2)
|
|
172
|
-
# offset + DateTime.new(2012,5,1,4,3)
|
|
173
|
-
# #=> #<DateTime: 2012-05-03T04:03:00+00:00 ((2456051j,14580s,0n),+0s,2299161j)>
|
|
174
|
-
class Day < Tick
|
|
175
|
-
FREQ = 'D'.freeze
|
|
176
|
-
|
|
177
|
-
def multiplier
|
|
178
|
-
1
|
|
179
|
-
end
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
# Create an months offset
|
|
183
|
-
#
|
|
184
|
-
# @param n [Integer] The number of times an offset should be applied.
|
|
185
|
-
# @example Create a Month offset
|
|
186
|
-
# offset = DaruLite::Offsets::Month.new(5)
|
|
187
|
-
# offset + DateTime.new(2012,5,1,4,3)
|
|
188
|
-
# #=> #<DateTime: 2012-10-01T04:03:00+00:00 ((2456202j,14580s,0n),+0s,2299161j)>
|
|
189
|
-
class Month < Tick
|
|
190
|
-
FREQ = 'MONTH'.freeze
|
|
191
|
-
|
|
192
|
-
def +(other)
|
|
193
|
-
other >> @n
|
|
194
|
-
end
|
|
195
|
-
|
|
196
|
-
def -(other)
|
|
197
|
-
other << @n
|
|
198
|
-
end
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
# Create a years offset
|
|
202
|
-
#
|
|
203
|
-
# @param n [Integer] The number of times an offset should be applied.
|
|
204
|
-
# @example Create a Year offset
|
|
205
|
-
# offset = DaruLite::Offsets::Year.new(2)
|
|
206
|
-
# offset + DateTime.new(2012,5,1,4,3)
|
|
207
|
-
# #=> #<DateTime: 2014-05-01T04:03:00+00:00 ((2456779j,14580s,0n),+0s,2299161j)>
|
|
208
|
-
class Year < Tick
|
|
209
|
-
FREQ = 'YEAR'.freeze
|
|
210
|
-
|
|
211
|
-
def +(other)
|
|
212
|
-
other >> (@n * 12)
|
|
213
|
-
end
|
|
214
|
-
|
|
215
|
-
def -(other)
|
|
216
|
-
other << (@n * 12)
|
|
217
|
-
end
|
|
218
|
-
end
|
|
219
|
-
|
|
220
|
-
class Week < DateOffset
|
|
221
|
-
def initialize(*args)
|
|
222
|
-
@n = args[0].is_a?(Hash) ? 1 : args[0]
|
|
223
|
-
opts = args[-1]
|
|
224
|
-
@weekday = opts[:weekday] || 0
|
|
225
|
-
end
|
|
226
|
-
|
|
227
|
-
def +(other)
|
|
228
|
-
wday = other.wday
|
|
229
|
-
distance = (@weekday - wday).abs
|
|
230
|
-
if @weekday > wday
|
|
231
|
-
other + distance + (7 * (@n - 1))
|
|
232
|
-
else
|
|
233
|
-
other + (7 - distance) + (7 * (@n - 1))
|
|
234
|
-
end
|
|
235
|
-
end
|
|
236
|
-
|
|
237
|
-
def -(other)
|
|
238
|
-
wday = other.wday
|
|
239
|
-
distance = (@weekday - wday).abs
|
|
240
|
-
if @weekday >= wday
|
|
241
|
-
other - ((7 - distance) + (7 * (@n - 1)))
|
|
242
|
-
else
|
|
243
|
-
other - (distance + (7 * (@n - 1)))
|
|
244
|
-
end
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
def on_offset?(date_time)
|
|
248
|
-
date_time.wday == @weekday
|
|
249
|
-
end
|
|
250
|
-
|
|
251
|
-
def freq_string
|
|
252
|
-
"#{@n.to_s unless @n == 1}W-#{DaruLite::DAYS_OF_WEEK.key(@weekday)}"
|
|
253
|
-
end
|
|
254
|
-
end
|
|
255
|
-
|
|
256
|
-
# Create a month begin offset
|
|
257
|
-
#
|
|
258
|
-
# @param n [Integer] The number of times an offset should be applied.
|
|
259
|
-
# @example Create a MonthBegin offset
|
|
260
|
-
# offset = DaruLite::Offsets::MonthBegin.new(2)
|
|
261
|
-
# offset + DateTime.new(2012,5,5)
|
|
262
|
-
# #=> #<DateTime: 2012-07-01T00:00:00+00:00 ((2456110j,0s,0n),+0s,2299161j)>
|
|
263
|
-
class MonthBegin < DateOffsetType
|
|
264
|
-
FREQ = 'MB'.freeze
|
|
265
|
-
|
|
266
|
-
def +(other)
|
|
267
|
-
@n.times do
|
|
268
|
-
days_in_month = DaruLite::MONTH_DAYS[other.month]
|
|
269
|
-
days_in_month += 1 if other.leap? && other.month == 2
|
|
270
|
-
other += (days_in_month - other.day + 1)
|
|
271
|
-
end
|
|
272
|
-
|
|
273
|
-
other
|
|
274
|
-
end
|
|
275
|
-
|
|
276
|
-
def -(other)
|
|
277
|
-
@n.times do
|
|
278
|
-
other <<= 1 if on_offset?(other)
|
|
279
|
-
other = DateTime.new(other.year, other.month, 1,
|
|
280
|
-
other.hour, other.min, other.sec)
|
|
281
|
-
end
|
|
282
|
-
|
|
283
|
-
other
|
|
284
|
-
end
|
|
285
|
-
|
|
286
|
-
def on_offset?(date_time)
|
|
287
|
-
date_time.day == 1
|
|
288
|
-
end
|
|
289
|
-
end
|
|
290
|
-
|
|
291
|
-
# Create a month end offset
|
|
292
|
-
#
|
|
293
|
-
# @param n [Integer] The number of times an offset should be applied.
|
|
294
|
-
# @example Create a MonthEnd offset
|
|
295
|
-
# offset = DaruLite::Offsets::MonthEnd.new
|
|
296
|
-
# offset + DateTime.new(2012,5,5)
|
|
297
|
-
# #=> #<DateTime: 2012-05-31T00:00:00+00:00 ((2456079j,0s,0n),+0s,2299161j)>
|
|
298
|
-
class MonthEnd < DateOffsetType
|
|
299
|
-
FREQ = 'ME'.freeze
|
|
300
|
-
|
|
301
|
-
def +(other)
|
|
302
|
-
@n.times do
|
|
303
|
-
other >>= 1 if on_offset?(other)
|
|
304
|
-
days_in_month = DaruLite::MONTH_DAYS[other.month]
|
|
305
|
-
days_in_month += 1 if other.leap? && other.month == 2
|
|
306
|
-
|
|
307
|
-
other += (days_in_month - other.day)
|
|
308
|
-
end
|
|
309
|
-
|
|
310
|
-
other
|
|
311
|
-
end
|
|
312
|
-
|
|
313
|
-
def -(other)
|
|
314
|
-
@n.times do
|
|
315
|
-
other <<= 1
|
|
316
|
-
days_in_month = DaruLite::MONTH_DAYS[other.month]
|
|
317
|
-
days_in_month += 1 if other.leap? && other.month == 2
|
|
318
|
-
|
|
319
|
-
other += (days_in_month - other.day)
|
|
320
|
-
end
|
|
321
|
-
|
|
322
|
-
other
|
|
323
|
-
end
|
|
324
|
-
|
|
325
|
-
def on_offset?(date_time)
|
|
326
|
-
(date_time + 1).day == 1
|
|
327
|
-
end
|
|
328
|
-
end
|
|
329
|
-
|
|
330
|
-
# Create a year begin offset
|
|
331
|
-
#
|
|
332
|
-
# @param n [Integer] The number of times an offset should be applied.
|
|
333
|
-
# @example Create a YearBegin offset
|
|
334
|
-
# offset = DaruLite::Offsets::YearBegin.new(3)
|
|
335
|
-
# offset + DateTime.new(2012,5,5)
|
|
336
|
-
# #=> #<DateTime: 2015-01-01T00:00:00+00:00 ((2457024j,0s,0n),+0s,2299161j)>
|
|
337
|
-
class YearBegin < DateOffsetType
|
|
338
|
-
FREQ = 'YB'.freeze
|
|
339
|
-
|
|
340
|
-
def +(other)
|
|
341
|
-
DateTime.new(other.year + @n, 1, 1,
|
|
342
|
-
other.hour, other.min, other.sec)
|
|
343
|
-
end
|
|
344
|
-
|
|
345
|
-
def -(other)
|
|
346
|
-
if on_offset?(other)
|
|
347
|
-
DateTime.new(other.year - @n, 1, 1,
|
|
348
|
-
other.hour, other.min, other.sec)
|
|
349
|
-
else
|
|
350
|
-
DateTime.new(other.year - (@n - 1), 1, 1)
|
|
351
|
-
end
|
|
352
|
-
end
|
|
353
|
-
|
|
354
|
-
def on_offset?(date_time)
|
|
355
|
-
date_time.month == 1 && date_time.day == 1
|
|
356
|
-
end
|
|
357
|
-
end
|
|
358
|
-
|
|
359
|
-
# Create a year end offset
|
|
360
|
-
#
|
|
361
|
-
# @param n [Integer] The number of times an offset should be applied.
|
|
362
|
-
# @example Create a YearEnd offset
|
|
363
|
-
# offset = DaruLite::Offsets::YearEnd.new
|
|
364
|
-
# offset + DateTime.new(2012,5,5)
|
|
365
|
-
# #=> #<DateTime: 2012-12-31T00:00:00+00:00 ((2456293j,0s,0n),+0s,2299161j)>
|
|
366
|
-
class YearEnd < DateOffsetType
|
|
367
|
-
FREQ = 'YE'.freeze
|
|
368
|
-
|
|
369
|
-
def +(other)
|
|
370
|
-
if on_offset?(other)
|
|
371
|
-
DateTime.new(other.year + @n, 12, 31,
|
|
372
|
-
other.hour, other.min, other.sec)
|
|
373
|
-
else
|
|
374
|
-
DateTime.new(other.year + (@n - 1), 12, 31,
|
|
375
|
-
other.hour, other.min, other.sec)
|
|
376
|
-
end
|
|
377
|
-
end
|
|
378
|
-
|
|
379
|
-
def -(other)
|
|
380
|
-
DateTime.new(other.year - 1, 12, 31)
|
|
381
|
-
end
|
|
382
|
-
|
|
383
|
-
def on_offset?(date_time)
|
|
384
|
-
date_time.month == 12 && date_time.day == 31
|
|
385
|
-
end
|
|
386
|
-
end
|
|
387
|
-
|
|
388
|
-
LIST = {
|
|
389
|
-
secs: Second,
|
|
390
|
-
mins: Minute,
|
|
391
|
-
hours: Hour,
|
|
392
|
-
days: Day,
|
|
393
|
-
months: Month,
|
|
394
|
-
years: Year
|
|
395
|
-
}.freeze
|
|
396
|
-
end
|
|
397
|
-
end
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
# Support for a simple query DSL for accessing where(), inspired by gem "squeel"
|
|
2
|
-
|
|
3
|
-
module DaruLite
|
|
4
|
-
class DataFrame
|
|
5
|
-
# a simple query DSL for accessing where(), inspired by gem "squeel"
|
|
6
|
-
# e.g.:
|
|
7
|
-
# df.which{ `FamilySize` == `FamilySize`.max }
|
|
8
|
-
# equals
|
|
9
|
-
# df.where( df['FamilySize'].eq( df['FamilySize'].max ) )
|
|
10
|
-
#
|
|
11
|
-
# e.g.:
|
|
12
|
-
# df.which{ (`NameTitle` == 'Dr') & (`Sex` == 'female') }
|
|
13
|
-
# equals
|
|
14
|
-
# df.where( df['NameTitle'].eq('Dr') & df['Sex'].eq('female') )
|
|
15
|
-
def which(&)
|
|
16
|
-
WhichQuery.new(self, &).exec
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
class WhichQuery
|
|
21
|
-
def initialize(df, &condition)
|
|
22
|
-
@df = df
|
|
23
|
-
@condition = condition
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
# executes a block of DSL code
|
|
27
|
-
def exec
|
|
28
|
-
query = instance_eval(&@condition)
|
|
29
|
-
@df.where(query)
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def `(vector_name)
|
|
33
|
-
vector_name = vector_name.to_sym if !@df.has_vector?(vector_name) && @df.has_vector?(vector_name.to_sym)
|
|
34
|
-
VectorWrapper.new(@df[vector_name])
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
class VectorWrapper < SimpleDelegator
|
|
38
|
-
{
|
|
39
|
-
:== => :eq,
|
|
40
|
-
:!= => :not_eq,
|
|
41
|
-
:< => :lt,
|
|
42
|
-
:<= => :lteq,
|
|
43
|
-
:> => :mt,
|
|
44
|
-
:>= => :mteq,
|
|
45
|
-
:=~ => :in
|
|
46
|
-
}.each do |opt, method|
|
|
47
|
-
define_method opt do |*args|
|
|
48
|
-
send(method, *args)
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
end
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
module DaruLite
|
|
2
|
-
module IO
|
|
3
|
-
class SqlDataSource
|
|
4
|
-
# @private
|
|
5
|
-
class Adapter
|
|
6
|
-
def initialize(conn, query)
|
|
7
|
-
@conn = conn
|
|
8
|
-
@query = query
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
def result_hash
|
|
12
|
-
column_names
|
|
13
|
-
.map(&:to_sym)
|
|
14
|
-
.zip(rows.transpose)
|
|
15
|
-
.to_h
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
# Private adapter class for connections of ActiveRecord
|
|
20
|
-
# @private
|
|
21
|
-
class ActiveRecordConnectionAdapter < Adapter
|
|
22
|
-
private
|
|
23
|
-
|
|
24
|
-
def column_names
|
|
25
|
-
result.columns
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def rows
|
|
29
|
-
result.cast_values
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def result
|
|
33
|
-
@result ||= @conn.exec_query(@query)
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
private_constant :ActiveRecordConnectionAdapter
|
|
38
|
-
|
|
39
|
-
def self.make_dataframe(db, query)
|
|
40
|
-
new(db, query).make_dataframe
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def initialize(db, query)
|
|
44
|
-
@adapter = init_adapter(db, query)
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def make_dataframe
|
|
48
|
-
DaruLite::DataFrame.new(@adapter.result_hash).tap(&:update)
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
private
|
|
52
|
-
|
|
53
|
-
def init_adapter(db, query)
|
|
54
|
-
query = String.try_convert(query) or
|
|
55
|
-
raise ArgumentError, "Query must be a string, #{query.class} received"
|
|
56
|
-
|
|
57
|
-
db = attempt_sqlite3_connection(db) if db.is_a?(String) && Pathname(db).exist?
|
|
58
|
-
|
|
59
|
-
case db
|
|
60
|
-
when ActiveRecord::ConnectionAdapters::AbstractAdapter
|
|
61
|
-
ActiveRecordConnectionAdapter.new(db, query)
|
|
62
|
-
else
|
|
63
|
-
raise ArgumentError, "Unknown database adapter type #{db.class}"
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
def attempt_sqlite3_connection(db)
|
|
68
|
-
ActiveRecord::Base.establish_connection(
|
|
69
|
-
adapter: 'sqlite3',
|
|
70
|
-
database: db
|
|
71
|
-
)
|
|
72
|
-
ActiveRecord::Base.connection.tap(&:verify!)
|
|
73
|
-
rescue ActiveRecord::StatementInvalid
|
|
74
|
-
raise ArgumentError, "Expected #{db} to point to a SQLite3 database"
|
|
75
|
-
rescue NameError
|
|
76
|
-
raise NameError, "In order to establish a connection to #{db}, please require 'ActiveRecord'"
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
end
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
module DaruLite
|
|
2
|
-
# @private
|
|
3
|
-
module IRuby
|
|
4
|
-
module Helpers
|
|
5
|
-
module_function
|
|
6
|
-
|
|
7
|
-
def tuples_with_rowspans(index)
|
|
8
|
-
index.sparse_tuples.transpose
|
|
9
|
-
.map { |r| nils_counted(r) }
|
|
10
|
-
.transpose.map(&:compact)
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def tuples_with_colspans(index)
|
|
14
|
-
index.sparse_tuples.transpose
|
|
15
|
-
.map { |r| nils_counted(r) }
|
|
16
|
-
.map(&:compact)
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
# It is complicated, but the only algo I could think of.
|
|
20
|
-
# It does [:a, nil, nil, :b, nil, :c] # =>
|
|
21
|
-
# [[:a,3], nil, nil, [:b,2], nil, :c]
|
|
22
|
-
# Needed by tuples_with_colspans/rowspans, which we need for pretty HTML
|
|
23
|
-
def nils_counted(array)
|
|
24
|
-
grouped = [[array.first]]
|
|
25
|
-
array[1..].each do |val|
|
|
26
|
-
if val
|
|
27
|
-
grouped << [val]
|
|
28
|
-
else
|
|
29
|
-
grouped.last << val
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
grouped.flat_map do |items|
|
|
33
|
-
[[items.first, items.count], *Array.new(items.count - 1)]
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
<tbody>
|
|
2
|
-
<% DaruLite::IRuby::Helpers.tuples_with_rowspans(@index).first(threshold).zip(@index.to_a).each do |tuple, index| %>
|
|
3
|
-
<tr>
|
|
4
|
-
<% tuple.each do |idx, span| %>
|
|
5
|
-
<th rowspan="<%= span %>"><%= idx %></th>
|
|
6
|
-
<% end %>
|
|
7
|
-
<% row[index].each do |element| %>
|
|
8
|
-
<td><%= element.to_s %></td>
|
|
9
|
-
<% end %>
|
|
10
|
-
</tr>
|
|
11
|
-
<% end %>
|
|
12
|
-
|
|
13
|
-
<% if nrows > threshold %>
|
|
14
|
-
<tr>
|
|
15
|
-
<% index.width.times do %>
|
|
16
|
-
<th>...</th>
|
|
17
|
-
<% end %>
|
|
18
|
-
<% @vectors.size.times do %>
|
|
19
|
-
<td>...</td>
|
|
20
|
-
<% end %>
|
|
21
|
-
</tr>
|
|
22
|
-
|
|
23
|
-
<% last_index = @index.to_a.last
|
|
24
|
-
last_row = row[last_index] %>
|
|
25
|
-
|
|
26
|
-
<tr>
|
|
27
|
-
<% last_index.each do |idx| %>
|
|
28
|
-
<th><%= idx %></td>
|
|
29
|
-
<% end %>
|
|
30
|
-
<% last_row.each do |element| %>
|
|
31
|
-
<td><%= element.to_s %></td>
|
|
32
|
-
<% end %>
|
|
33
|
-
</tr>
|
|
34
|
-
<% end %>
|
|
35
|
-
</tbody>
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
<thead>
|
|
2
|
-
<% if @vectors.is_a? MultiIndex %>
|
|
3
|
-
<% DaruLite::IRuby::Helpers.tuples_with_colspans(@vectors).each_with_index do |tuple, idx| %>
|
|
4
|
-
<tr>
|
|
5
|
-
<% if idx.zero? %>
|
|
6
|
-
<th colspan="<%= index.width %>" rowspan="<%= @vectors.width %>"></th>
|
|
7
|
-
<% end %>
|
|
8
|
-
<% tuple.each do |idx, span| %>
|
|
9
|
-
<th colspan="<%= span %>"><%= idx %></th>
|
|
10
|
-
<% end %>
|
|
11
|
-
</tr>
|
|
12
|
-
<% end %>
|
|
13
|
-
<% else %>
|
|
14
|
-
<tr>
|
|
15
|
-
<th colspan="<%= index.width %>"></th>
|
|
16
|
-
<% @vectors.each do |vector| %>
|
|
17
|
-
<th><%=vector%></th>
|
|
18
|
-
<% end %>
|
|
19
|
-
</tr>
|
|
20
|
-
<% end %>
|
|
21
|
-
</thead>
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
<tbody>
|
|
2
|
-
<% @index.first(threshold).each_with_index do |index, pos| %>
|
|
3
|
-
<tr>
|
|
4
|
-
<td><%= index %></td>
|
|
5
|
-
<% row.at(pos).each do |element| %>
|
|
6
|
-
<td><%= element.to_s %></td>
|
|
7
|
-
<% end %>
|
|
8
|
-
</tr>
|
|
9
|
-
<% end %>
|
|
10
|
-
|
|
11
|
-
<% if nrows > threshold %>
|
|
12
|
-
<tr>
|
|
13
|
-
<% (@vectors.size + 1).times do %>
|
|
14
|
-
<td>...</td>
|
|
15
|
-
<% end %>
|
|
16
|
-
</tr>
|
|
17
|
-
|
|
18
|
-
<% last_index = @index.to_a.last
|
|
19
|
-
last_row = row_at @index.size-1 %>
|
|
20
|
-
|
|
21
|
-
<tr>
|
|
22
|
-
<td><%= last_index %></td>
|
|
23
|
-
<% last_row.each do |element| %>
|
|
24
|
-
<td><%= element.to_s %></td>
|
|
25
|
-
<% end %>
|
|
26
|
-
</tr>
|
|
27
|
-
<% end %>
|
|
28
|
-
</tbody>
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
<thead>
|
|
2
|
-
<% if @vectors.is_a? MultiIndex %>
|
|
3
|
-
<% DaruLite::IRuby::Helpers.tuples_with_colspans(@vectors).each_with_index do |tuple, idx| %>
|
|
4
|
-
<tr>
|
|
5
|
-
<% if idx.zero? %>
|
|
6
|
-
<th rowspan="<%= @vectors.width %>"></th>
|
|
7
|
-
<% end %>
|
|
8
|
-
<% tuple.each do |idx, span| %>
|
|
9
|
-
<th colspan="<%= span %>"><%= idx %></th>
|
|
10
|
-
<% end %>
|
|
11
|
-
</tr>
|
|
12
|
-
<% end %>
|
|
13
|
-
<% else %>
|
|
14
|
-
<tr>
|
|
15
|
-
<th></th>
|
|
16
|
-
<% @vectors.each do |vector| %>
|
|
17
|
-
<th><%=vector%></th>
|
|
18
|
-
<% end %>
|
|
19
|
-
</tr>
|
|
20
|
-
<% end %>
|
|
21
|
-
</thead>
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
<table>
|
|
2
|
-
<tr>
|
|
3
|
-
<th colspan="<%= width %>">DaruLite::MultiIndex(<%= size %>x<%= width %>)</th>
|
|
4
|
-
</tr>
|
|
5
|
-
<% DaruLite::IRuby::Helpers.tuples_with_rowspans(self).each do |row| %>
|
|
6
|
-
<tr>
|
|
7
|
-
<% row.each do |val, span| %>
|
|
8
|
-
<th rowspan="<%= span %>"><%= val %></th>
|
|
9
|
-
<% end %>
|
|
10
|
-
</tr>
|
|
11
|
-
<% end %>
|
|
12
|
-
</table>
|