Class: DaruLite::Index
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/daru_lite/index/index.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(index, opts = {}) ⇒ Index
Returns a new instance of Index.
66
67
68
69
70
71
72
|
# File 'lib/daru_lite/index/index.rb', line 66
def initialize(index, opts = {})
index = guess_index index
@relation_hash = index.each_with_index.to_h.freeze
@keys = @relation_hash.keys
@size = @relation_hash.size
@name = opts[:name]
end
|
Instance Attribute Details
Returns the value of attribute name.
47
48
49
|
# File 'lib/daru_lite/index/index.rb', line 47
def name
@name
end
|
#relation_hash ⇒ Object
Returns the value of attribute relation_hash.
46
47
48
|
# File 'lib/daru_lite/index/index.rb', line 46
def relation_hash
@relation_hash
end
|
Returns the value of attribute size.
46
47
48
|
# File 'lib/daru_lite/index/index.rb', line 46
def size
@size
end
|
Class Method Details
12
|
# File 'lib/daru_lite/index/index.rb', line 12
alias __new__ new
|
._load(data) ⇒ Object
253
254
255
256
257
|
# File 'lib/daru_lite/index/index.rb', line 253
def self._load(data)
h = Marshal.load data
DaruLite::Index.new(h[:relation_hash].keys)
end
|
.coerce(maybe_index) ⇒ Object
35
36
37
|
# File 'lib/daru_lite/index/index.rb', line 35
def self.coerce(maybe_index)
maybe_index.is_a?(Index) ? maybe_index : DaruLite::Index.new(maybe_index)
end
|
.inherited(subclass) ⇒ Object
14
15
16
17
18
|
# File 'lib/daru_lite/index/index.rb', line 14
def inherited(subclass)
class << subclass
alias_method :new, :__new__
end
end
|
.new(*args, &block) ⇒ Object
We over-ride the .new method so that any sort of Index can be generated from DaruLite::Index based on the types of arguments supplied.
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/daru_lite/index/index.rb', line 23
def self.new(*args, &block)
source = args.first
MultiIndex.try_from_tuples(source) ||
DateTimeIndex.try_create(source) ||
allocate.tap { |i| i.send :initialize, *args, &block }
end
|
Instance Method Details
Produce a new index from the set intersection of two indexes
198
199
200
|
# File 'lib/daru_lite/index/index.rb', line 198
def &(other)
Index.new(to_a & other.to_a)
end
|
#==(other) ⇒ Object
74
75
76
77
78
79
|
# File 'lib/daru_lite/index/index.rb', line 74
def ==(other)
return false if self.class != other.class || other.size != @size
@keys == other.to_a &&
@relation_hash.values == other.relation_hash.values
end
|
#[](key, *rest) ⇒ Object
81
82
83
84
85
86
87
88
89
|
# File 'lib/daru_lite/index/index.rb', line 81
def [](key, *rest)
if key.is_a?(Range)
by_range key
elsif !rest.empty?
by_multi_key key, *rest
else
by_single_key key
end
end
|
249
250
251
|
# File 'lib/daru_lite/index/index.rb', line 249
def _dump(*)
Marshal.dump(relation_hash: @relation_hash)
end
|
#add(*indexes) ⇒ Object
245
246
247
|
# File 'lib/daru_lite/index/index.rb', line 245
def add(*indexes)
DaruLite::Index.new(to_a + indexes)
end
|
#at(*positions) ⇒ object
Takes positional values and returns subset of the self
capturing the indexes at mentioned positions
144
145
146
147
148
149
150
151
152
|
# File 'lib/daru_lite/index/index.rb', line 144
def at(*positions)
positions = preprocess_positions(*positions)
validate_positions(*positions)
if positions.is_a? Integer
key(positions)
else
self.class.new(positions.map { |v| key(v) })
end
end
|
Provide an Index for sub vector produced
263
264
265
|
# File 'lib/daru_lite/index/index.rb', line 263
def conform(*)
self
end
|
241
242
243
|
# File 'lib/daru_lite/index/index.rb', line 241
def dup
DaruLite::Index.new @keys, name: @name
end
|
#each(&block) ⇒ Object
39
40
41
42
43
44
|
# File 'lib/daru_lite/index/index.rb', line 39
def each(&block)
return to_enum(:each) unless block
@relation_hash.each_key(&block)
self
end
|
#empty? ⇒ Boolean
237
238
239
|
# File 'lib/daru_lite/index/index.rb', line 237
def empty?
@size.zero?
end
|
#include?(index) ⇒ Boolean
212
213
214
|
# File 'lib/daru_lite/index/index.rb', line 212
def include?(index)
@relation_hash.key? index
end
|
#inspect(threshold = 20) ⇒ Object
154
155
156
157
158
159
160
161
|
# File 'lib/daru_lite/index/index.rb', line 154
def inspect(threshold = 20)
name_part = @name ? "#{@name} " : ''
if size <= threshold
"#<#{self.class}(#{size}): #{name_part}{#{to_a.join(', ')}}>"
else
"#<#{self.class}(#{size}): #{name_part}{#{to_a.first(threshold).join(', ')} ... #{to_a.last}}>"
end
end
|
Note:
Do not use it to check for Float::NAN as Float::NAN == Float::NAN is false
Return vector of booleans with value at ith position is either true or false depending upon whether index value at position i is equal to any of the values passed in the argument or not
232
233
234
235
|
# File 'lib/daru_lite/index/index.rb', line 232
def is_values(*indexes)
bool_array = @keys.map { |r| indexes.include?(r) }
DaruLite::Vector.new(bool_array)
end
|
#key(value) ⇒ Object
206
207
208
209
210
|
# File 'lib/daru_lite/index/index.rb', line 206
def key(value)
return nil unless value.is_a?(Numeric)
@keys[value]
end
|
#pos(*indexes) ⇒ Object
Note:
If the arugent is both a valid index and a valid position, it will treated as valid index
Returns positions given indexes or positions
111
112
113
114
115
116
117
118
119
|
# File 'lib/daru_lite/index/index.rb', line 111
def pos(*indexes)
indexes = preprocess_range(indexes.first) if indexes.first.is_a? Range
if indexes.size == 1
numeric_pos indexes.first
else
indexes.map { |index| numeric_pos index }
end
end
|
#reorder(new_order) ⇒ Object
267
268
269
270
|
# File 'lib/daru_lite/index/index.rb', line 267
def reorder(new_order)
from = to_a
self.class.new(new_order.map { |i| from[i] })
end
|
#slice(*args) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/daru_lite/index/index.rb', line 163
def slice(*args)
start = args[0]
en = args[1]
start_idx = @relation_hash[start]
en_idx = @relation_hash[en]
if start_idx.nil?
nil
elsif en_idx.nil?
Array(start_idx..size - 1)
else
Array(start_idx..en_idx)
end
end
|
#sort(opts = {}) ⇒ Index
Sorts a ‘Index`, according to its values. Defaults to ascending order sorting.
286
287
288
289
290
291
292
293
|
# File 'lib/daru_lite/index/index.rb', line 286
def sort(opts = {})
opts = { ascending: true }.merge(opts)
new_index = @keys.sort
new_index = new_index.reverse unless opts[:ascending]
self.class.new(new_index)
end
|
#subset(*indexes) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/daru_lite/index/index.rb', line 121
def subset(*indexes)
if indexes.first.is_a? Range
start = indexes.first.begin
en = indexes.first.end
subset_slice start, en
elsif include? indexes.first
DaruLite::Index.new indexes
else
DaruLite::Index.new(indexes.map { |k| key k })
end
end
|
#subset_slice(*args) ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/daru_lite/index/index.rb', line 179
def subset_slice(*args)
start = args[0]
en = args[1]
if start.is_a?(Integer) && en.is_a?(Integer)
Index.new @keys[start..en]
else
start_idx = @relation_hash[start]
en_idx = @relation_hash[en]
Index.new @keys[start_idx..en_idx]
end
end
|
202
203
204
|
# File 'lib/daru_lite/index/index.rb', line 202
def to_a
@keys
end
|
295
296
297
|
# File 'lib/daru_lite/index/index.rb', line 295
def to_df
DaruLite::DataFrame.new(name => to_a)
end
|
#valid?(*indexes) ⇒ true, false
Returns true if all arguments are either a valid category or position
99
100
101
|
# File 'lib/daru_lite/index/index.rb', line 99
def valid?(*indexes)
indexes.all? { |i| to_a.include?(i) || (i.is_a?(Numeric) && i < size) }
end
|
Produce new index from the set union of two indexes.
193
194
195
|
# File 'lib/daru_lite/index/index.rb', line 193
def |(other)
Index.new(to_a | other.to_a)
end
|