daru 0.1.4.1 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.travis.yml +3 -0
- data/CONTRIBUTING.md +27 -3
- data/Guardfile +7 -0
- data/History.md +39 -1
- data/README.md +1 -1
- data/daru.gemspec +9 -2
- data/lib/daru.rb +4 -1
- data/lib/daru/accessors/gsl_wrapper.rb +93 -91
- data/lib/daru/accessors/nmatrix_wrapper.rb +109 -107
- data/lib/daru/category.rb +22 -15
- data/lib/daru/core/group_by.rb +13 -2
- data/lib/daru/core/merge.rb +37 -31
- data/lib/daru/core/query.rb +10 -2
- data/lib/daru/dataframe.rb +95 -34
- data/lib/daru/date_time/index.rb +15 -16
- data/lib/daru/date_time/offsets.rb +14 -11
- data/lib/daru/formatters/table.rb +2 -2
- data/lib/daru/index/categorical_index.rb +201 -0
- data/lib/daru/index/index.rb +289 -0
- data/lib/daru/index/multi_index.rb +266 -0
- data/lib/daru/maths/statistics/vector.rb +13 -9
- data/lib/daru/monkeys.rb +0 -7
- data/lib/daru/plotting/gruff/category.rb +1 -0
- data/lib/daru/plotting/gruff/dataframe.rb +3 -3
- data/lib/daru/plotting/nyaplot/dataframe.rb +1 -1
- data/lib/daru/vector.rb +36 -21
- data/lib/daru/version.rb +1 -1
- data/spec/accessors/array_wrapper_spec.rb +3 -0
- data/spec/accessors/{wrappers_spec.rb → gsl_wrapper_spec.rb} +0 -35
- data/spec/accessors/nmatrix_wrapper_spec.rb +32 -0
- data/spec/{categorical_spec.rb → category_spec.rb} +3 -0
- data/spec/core/group_by_spec.rb +17 -1
- data/spec/core/merge_spec.rb +38 -1
- data/spec/core/query_spec.rb +5 -0
- data/spec/dataframe_spec.rb +230 -57
- data/spec/date_time/offsets_spec.rb +84 -3
- data/spec/formatters/table_formatter_spec.rb +9 -0
- data/spec/index/categorical_index_spec.rb +2 -0
- data/spec/index/index_spec.rb +17 -2
- data/spec/{math → maths}/arithmetic/dataframe_spec.rb +0 -0
- data/spec/{math → maths}/arithmetic/vector_spec.rb +0 -0
- data/spec/{math → maths}/statistics/dataframe_spec.rb +1 -1
- data/spec/{math → maths}/statistics/vector_spec.rb +7 -12
- data/spec/plotting/gruff/category_spec.rb +44 -0
- data/spec/plotting/gruff/dataframe_spec.rb +84 -0
- data/spec/plotting/gruff/vector_spec.rb +70 -0
- data/spec/plotting/nyaplot/category_spec.rb +51 -0
- data/spec/plotting/{dataframe_spec.rb → nyaplot/dataframe_spec.rb} +0 -83
- data/spec/plotting/nyaplot/vector_spec.rb +66 -0
- data/spec/spec_helper.rb +3 -2
- data/spec/vector_spec.rb +68 -1
- metadata +53 -24
- data/lib/daru/index.rb +0 -761
- data/spec/plotting/vector_spec.rb +0 -230
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b584977ade16d5cfbd3bdd9da7202bbc2adae894
|
4
|
+
data.tar.gz: 3de8ef80fc55c91c0f504543fda1f23b40b4db6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f498f5caa4dbc14c2ca884c45492e474442d3f067ecf673a0235f01ec1781bb96c6f6e764006dcbf349a68ea011ccd977d69fdeb324a202ffa4fc5351a3a7b9
|
7
|
+
data.tar.gz: d209e60b566e0e900d9c62fca6a1fb9023cd09cd81194e9b331c59682f557d8917ca347978bd4ed9b614c8d1ba441126211f3dc15a691752733f5dca0200be5d
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
@@ -6,6 +6,7 @@ rvm:
|
|
6
6
|
- '2.1'
|
7
7
|
- '2.2'
|
8
8
|
- '2.3.0'
|
9
|
+
- '2.4.0'
|
9
10
|
|
10
11
|
matrix:
|
11
12
|
fast_finish:
|
@@ -17,9 +18,11 @@ script:
|
|
17
18
|
|
18
19
|
install:
|
19
20
|
- gem install bundler
|
21
|
+
- gem install rainbow -v '2.2.1'
|
20
22
|
- bundle install
|
21
23
|
|
22
24
|
before_install:
|
23
25
|
- sudo apt-get update -qq
|
24
26
|
- sudo apt-get install -y libgsl0-dev r-base r-base-dev
|
25
27
|
- sudo Rscript -e "install.packages(c('Rserve','irr'),,'http://cran.us.r-project.org')"
|
28
|
+
- sudo apt-get install libmagickwand-dev imagemagick
|
data/CONTRIBUTING.md
CHANGED
@@ -6,9 +6,10 @@ Either nmatrix or rb-gsl are NOT NECESSARY for using daru. They are just require
|
|
6
6
|
|
7
7
|
To install dependencies, execute the following commands:
|
8
8
|
|
9
|
-
`sudo apt-get update -qq`
|
10
|
-
`sudo apt-get install -y libgsl0-dev r-base r-base-dev`
|
11
|
-
`sudo Rscript -e "install.packages(c('Rserve','irr'),,'http://cran.us.r-project.org')"`
|
9
|
+
* `sudo apt-get update -qq`
|
10
|
+
* `sudo apt-get install -y libgsl0-dev r-base r-base-dev`
|
11
|
+
* `sudo Rscript -e "install.packages(c('Rserve','irr'),,'http://cran.us.r-project.org')"`
|
12
|
+
* `sudo apt-get install libmagickwand-dev imagemagick`
|
12
13
|
|
13
14
|
|
14
15
|
Then install remaining dependencies:
|
@@ -24,6 +25,29 @@ If you have problems installing nmatrix, please consult the [nmatrix installatio
|
|
24
25
|
While preparing your pull requests, don't forget to check your code with Rubocop:
|
25
26
|
|
26
27
|
`bundle exec rubocop`
|
28
|
+
|
29
|
+
[Optional] Install all Ruby versions which Daru currently supports with `rake spec setup`.
|
30
|
+
|
31
|
+
## Basic Development Flow
|
32
|
+
|
33
|
+
1. Create a new branch with `git checkout -b <branch_name>`.
|
34
|
+
2. Make your changes.
|
35
|
+
3. Try out these changes with `rake pry`.
|
36
|
+
4. Run the test suite with `rake spec`. (Alternatively you can use `guard` as described [here](https://github.com/SciRuby/daru/blob/master/CONTRIBUTING.md#testing). Also run Rubocop coding style guidelines with `rake cop`.
|
37
|
+
5. Commit the changes with `git commit -am "briefly describe what you did"` and submit pull request.
|
38
|
+
|
39
|
+
[Optional] You can run rspec for all Ruby versions at once with `rake spec run all`. But remember to first have all Ruby versions installed with `ruby spec setup`.
|
40
|
+
|
41
|
+
|
42
|
+
## Testing
|
43
|
+
|
44
|
+
Daru has automatic testing with Guard. Just execute the following code before you start editting a file and any change you make will trigger the appropriate tests-
|
45
|
+
|
46
|
+
```
|
47
|
+
guard
|
48
|
+
```
|
49
|
+
|
50
|
+
**NOTE**: Please make sure that you place test for your file at the same level and with same itermediatary directories. For example if code file lies in `lib/xyz/abc.rb` then its corresponding test should lie in `spec/xyz/abc_spec.rb`. This is to ensure correct working of Guard.
|
27
51
|
|
28
52
|
## Daru internals
|
29
53
|
|
data/Guardfile
ADDED
data/History.md
CHANGED
@@ -1,3 +1,41 @@
|
|
1
|
+
# 0.1.5 (30 January 2017)
|
2
|
+
* Major Enhancements
|
3
|
+
- Add Daru::Vector#group_by (@lokeshh).
|
4
|
+
- Add rspec-guard to run tests automatically (@lokeshh).
|
5
|
+
- Remove Daru::DataFrame implicit Hash method since Dataframes are not implicit hashes and having an implicit converter can introduce unwanted side effects. (@gnilrets)
|
6
|
+
- Add `Daru::DataFrame#union`. (Tim)
|
7
|
+
|
8
|
+
* Minor Enhancements
|
9
|
+
- Added a join indicator. (@gnilrets)
|
10
|
+
- Support an enumerable value as an index of a vector. (Yuichiro Kaneko)
|
11
|
+
- Add test case for `NegativeDateOffset`. (Yuichiro Kaneko)
|
12
|
+
- Add test case for `#on_offset?`. (Yuichiro Kaneko)
|
13
|
+
- `NegativeDateOffset#-` returns `DateOffset`. (Yuichiro Kaneko)
|
14
|
+
- Make `Vector#resort_index` private because its only use was for internal usage in `Vector#sort`. (Yuichiro Kaneko)
|
15
|
+
- Add `DataFrame#order=` method to reorder vectors in a dataframe. (@lokeshh)
|
16
|
+
- Use `Integer` instead of `Fixnum` throughout the gem. (Yuichiro Kaneko)
|
17
|
+
- Improve error message of `Daru::Vector#index=`. (@lokeshh)
|
18
|
+
- Deprecate `freqs` and make `frequencies` return a `Daru::Vector`. (@lokeshh)
|
19
|
+
- `DataFrame#access_row` with integer index. (Yusuke Sangenya)
|
20
|
+
- Add method alias for comparison operator. (Yusuke Sangenya)
|
21
|
+
- Update Nokogiri version. (Yusuke Sangenya)
|
22
|
+
- Return `Daru::Vector` for multiple modal values for `Daru::Vector#mode`. (baarkerlounger)
|
23
|
+
|
24
|
+
* Fixes
|
25
|
+
- Fix many to one joins. The prior version was shifting values in the left dataframe before checking whether values in the right dataframe should be shifted. They both need to be checked at the same time before shifting either. (@gnilrets)
|
26
|
+
- Support formatting empty dataframes. They were returning an error before. (@gnilrets)
|
27
|
+
- method_missing in Daru::DataFrame would not detect the correct vector if it was a String. Fixed that. (@lokeshh)
|
28
|
+
- Fix docs of contrast_code to specify that the default value is false. (@v0dro)
|
29
|
+
- Fix occurence of SystemStackError due to faulty arguement passing to Array#values_at. (@v0dro)
|
30
|
+
- Fix `DataFrame#pivot_table` regression that raised an ArgumentError if the `:index` option was not specified. (@zverok)
|
31
|
+
- Fix `DateFrame.rows` to accept empty argument. (@zverok)
|
32
|
+
- Fix bug with false values on dataframe create. DataFrame from an Array of hashes wasn't being created properly when some of the values were `false`. (@gnilrets)
|
33
|
+
- Fix `Vector#reorder!` method. (Yusuke Sangenya)
|
34
|
+
- Fix `DataFrame#group_by` for numeric indexes. (@zverok)
|
35
|
+
- Make `DataFrame#index=` accept only `Daru::Index`. (Yusuke Sangenya)
|
36
|
+
- `DataFrame#vectors=` now changes the name of vectors contained in the internal `@data` variable. (Yusuke Sangenya)
|
37
|
+
|
38
|
+
|
1
39
|
# 0.1.4.1 (20 August 2016)
|
2
40
|
* Fixes
|
3
41
|
- Turns out that removing the dependencies did not load a few libraries from the Ruby standard library when daru is deployed on a fresh system. This release fixes that by adding extra require calls.
|
@@ -235,7 +273,7 @@
|
|
235
273
|
* Added statistics methods #mean, #std, #max, #min, #count, #product, #sum to DataFrame.
|
236
274
|
* Added #describe to DataFrame for producing multiple statistics data of numerical vectors in one shot.
|
237
275
|
* Monkey patched Ruby Matrix to include #elementwise_division.
|
238
|
-
* Added #covariance to calculate the covariance between numbers of a DataFrame and #correlation to calculate correlation.
|
276
|
+
* Added #covariance to calculate the covariance between numbers of a DataFrame and #correlation to calculate correlation.
|
239
277
|
* Enumerators return Enumerator objects if there is no block.
|
240
278
|
|
241
279
|
# 0.0.4
|
data/README.md
CHANGED
@@ -36,7 +36,7 @@ daru makes it easy and intuitive to process data predominantly through 2 data st
|
|
36
36
|
* [Detailed Usage of Daru::DataFrame](http://nbviewer.ipython.org/github/SciRuby/sciruby-notebooks/blob/master/Data%20Analysis/Usage%20of%20DataFrame.ipynb)
|
37
37
|
* [Searching and combining data in daru](http://nbviewer.ipython.org/github/SciRuby/sciruby-notebooks/blob/master/Data%20Analysis/Searching%20and%20Combining%20Data.ipynb)
|
38
38
|
* [Grouping, Splitting and Pivoting Data](http://nbviewer.ipython.org/github/SciRuby/sciruby-notebooks/blob/master/Data%20Analysis/Grouping%2C%20Splitting%20and%20Pivoting.ipynb)
|
39
|
-
* [Usage of
|
39
|
+
* [Usage of Categorical Data](http://nbviewer.jupyter.org/github/SciRuby/sciruby-notebooks/blob/master/Data%20Analysis/Categorical%20Data/Categorical%20Data.ipynb)
|
40
40
|
|
41
41
|
#### Visualization
|
42
42
|
* [Visualizing Data With Daru::DataFrame](http://nbviewer.ipython.org/github/SciRuby/sciruby-notebooks/blob/master/Visualization/Visualizing%20data%20with%20daru%20DataFrame.ipynb)
|
data/daru.gemspec
CHANGED
@@ -65,7 +65,7 @@ EOF
|
|
65
65
|
spec.add_development_dependency 'nyaplot', '~> 0.1.5'
|
66
66
|
spec.add_development_dependency 'nmatrix', '~> 0.2.1'
|
67
67
|
spec.add_development_dependency 'distribution', '~> 0.7'
|
68
|
-
spec.add_development_dependency '
|
68
|
+
spec.add_development_dependency 'gsl', '~>2.1.0.2'
|
69
69
|
spec.add_development_dependency 'dbd-sqlite3'
|
70
70
|
spec.add_development_dependency 'dbi'
|
71
71
|
spec.add_development_dependency 'activerecord', '~> 4.0'
|
@@ -73,6 +73,13 @@ EOF
|
|
73
73
|
spec.add_development_dependency 'rubocop', '>= 0.40.0'
|
74
74
|
spec.add_development_dependency 'ruby-prof'
|
75
75
|
spec.add_development_dependency 'simplecov'
|
76
|
-
spec.add_development_dependency 'nokogiri'
|
77
76
|
spec.add_development_dependency 'gruff'
|
77
|
+
if RUBY_VERSION < '2.1.0'
|
78
|
+
spec.add_development_dependency 'nokogiri', '<= 1.6.8.1'
|
79
|
+
else
|
80
|
+
spec.add_development_dependency 'nokogiri'
|
81
|
+
end
|
82
|
+
if RUBY_VERSION >= '2.2.5'
|
83
|
+
spec.add_development_dependency 'guard-rspec'
|
84
|
+
end
|
78
85
|
end
|
data/lib/daru.rb
CHANGED
@@ -98,8 +98,11 @@ require 'date'
|
|
98
98
|
|
99
99
|
require 'daru/version.rb'
|
100
100
|
|
101
|
+
require 'daru/index/index.rb'
|
102
|
+
require 'daru/index/multi_index.rb'
|
103
|
+
require 'daru/index/categorical_index.rb'
|
104
|
+
|
101
105
|
require 'daru/helpers/array.rb'
|
102
|
-
require 'daru/index.rb'
|
103
106
|
require 'daru/vector.rb'
|
104
107
|
require 'daru/dataframe.rb'
|
105
108
|
require 'daru/monkeys.rb'
|
@@ -1,122 +1,124 @@
|
|
1
|
-
|
2
|
-
module
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
if Daru.has_gsl?
|
2
|
+
module Daru
|
3
|
+
module Accessors
|
4
|
+
module GSLStatistics
|
5
|
+
def vector_standardized_compute(m,sd)
|
6
|
+
Daru::Vector.new @data.collect { |x| (x.to_f - m).quo(sd) }, dtype: :gsl,
|
7
|
+
index: @context.index, name: @context.name
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
def vector_centered_compute(m)
|
11
|
+
Daru::Vector.new @data.collect { |x| (x.to_f - m) }, dtype: :gsl,
|
12
|
+
index: @context.index, name: @context.name
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def sample_with_replacement(sample=1)
|
16
|
+
r = GSL::Rng.alloc(GSL::Rng::MT19937,rand(10_000))
|
17
|
+
Daru::Vector.new(r.sample(@data, sample).to_a, dtype: :gsl,
|
18
|
+
index: @context.index, name: @context.name)
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
def sample_without_replacement(sample=1)
|
22
|
+
r = GSL::Rng.alloc(GSL::Rng::MT19937,rand(10_000))
|
23
|
+
r.choose(@data, sample).to_a
|
24
|
+
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
def median
|
27
|
+
GSL::Stats.median_from_sorted_data(@data.sort)
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
def variance_sample(m)
|
31
|
+
@data.variance(m)
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
34
|
+
def standard_deviation_sample(m)
|
35
|
+
@data.sd(m)
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
38
|
+
def variance_population(m)
|
39
|
+
@data.variance_with_fixed_mean(m)
|
40
|
+
end
|
40
41
|
|
41
|
-
|
42
|
-
|
43
|
-
|
42
|
+
def standard_deviation_population m
|
43
|
+
@data.sd_with_fixed_mean(m)
|
44
|
+
end
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
|
46
|
+
def skew
|
47
|
+
@data.skew
|
48
|
+
end
|
48
49
|
|
49
|
-
|
50
|
-
|
50
|
+
def kurtosis
|
51
|
+
@data.kurtosis
|
52
|
+
end
|
51
53
|
end
|
52
|
-
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
class GSLWrapper
|
56
|
+
include Enumerable
|
57
|
+
extend Forwardable
|
58
|
+
include Daru::Accessors::GSLStatistics
|
58
59
|
|
59
|
-
|
60
|
+
def_delegators :@data, :[], :size, :to_a, :each
|
60
61
|
|
61
|
-
|
62
|
+
attr_reader :data
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
|
64
|
+
def compact
|
65
|
+
::GSL::Vector.alloc(@data.to_a - [Float::NAN])
|
66
|
+
end
|
66
67
|
|
67
|
-
|
68
|
-
|
69
|
-
|
68
|
+
[:mean, :min, :max, :prod, :sum].each do |method|
|
69
|
+
define_method(method) do
|
70
|
+
compact.send(method.to_sym) rescue nil
|
71
|
+
end
|
70
72
|
end
|
71
|
-
end
|
72
73
|
|
73
|
-
|
74
|
+
alias :product :prod
|
74
75
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
76
|
+
def each(&block)
|
77
|
+
@data.each(&block)
|
78
|
+
self
|
79
|
+
end
|
79
80
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
81
|
+
def map!(&block)
|
82
|
+
@data.map!(&block)
|
83
|
+
self
|
84
|
+
end
|
84
85
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
86
|
+
def initialize data, context
|
87
|
+
@data = ::GSL::Vector.alloc(data)
|
88
|
+
@context = context
|
89
|
+
end
|
89
90
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
91
|
+
def []= index, element
|
92
|
+
if index == size
|
93
|
+
push element
|
94
|
+
else
|
95
|
+
@data[index] = element
|
96
|
+
end
|
95
97
|
end
|
96
|
-
end
|
97
98
|
|
98
|
-
|
99
|
-
|
100
|
-
|
99
|
+
def delete_at index
|
100
|
+
@data.delete_at index
|
101
|
+
end
|
101
102
|
|
102
|
-
|
103
|
-
|
104
|
-
|
103
|
+
def index key
|
104
|
+
@data.to_a.index key
|
105
|
+
end
|
105
106
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
107
|
+
def push value
|
108
|
+
@data = @data.concat value
|
109
|
+
self
|
110
|
+
end
|
111
|
+
alias :<< :push
|
112
|
+
alias :concat :push
|
112
113
|
|
113
|
-
|
114
|
-
|
115
|
-
|
114
|
+
def dup
|
115
|
+
GSLWrapper.new(@data.to_a, @context)
|
116
|
+
end
|
116
117
|
|
117
|
-
|
118
|
-
|
118
|
+
def == other
|
119
|
+
@data == other.data
|
120
|
+
end
|
119
121
|
end
|
120
122
|
end
|
121
123
|
end
|
122
|
-
end
|
124
|
+
end
|
@@ -1,110 +1,112 @@
|
|
1
|
-
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
if Daru.has_nmatrix?
|
2
|
+
module Daru
|
3
|
+
module Accessors
|
4
|
+
# Internal class for wrapping NMatrix
|
5
|
+
class NMatrixWrapper
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
def each(&block)
|
9
|
+
@data[0...@size].each(&block)
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
def map!(&block)
|
14
|
+
@data = NMatrix.new [@size*2], map(&block).to_a, dtype: nm_dtype
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
# :nocov:
|
19
|
+
# FIXME: not sure, why this kind of wrapper have such a pure coverage
|
20
|
+
def inject(*args, &block)
|
21
|
+
@data[0...@size].inject(*args, &block)
|
22
|
+
end
|
23
|
+
# :nocov:
|
24
|
+
|
25
|
+
attr_reader :size, :data, :nm_dtype
|
26
|
+
|
27
|
+
def initialize vector, context, nm_dtype=:int32
|
28
|
+
@size = vector.size
|
29
|
+
@data = NMatrix.new [@size*2], vector.to_a, dtype: nm_dtype
|
30
|
+
@context = context
|
31
|
+
@nm_dtype = @data.dtype
|
32
|
+
# init with twice the storage for reducing the need to resize
|
33
|
+
end
|
34
|
+
|
35
|
+
def [] *index
|
36
|
+
return @data[*index] if index[0] < @size
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
|
40
|
+
def []= index, value
|
41
|
+
raise ArgumentError, "Index #{index} does not exist" if
|
42
|
+
index > @size && index < @data.size
|
43
|
+
resize if index >= @data.size
|
44
|
+
@size += 1 if index == @size
|
45
|
+
|
46
|
+
@data = @data.cast(dtype: :object) if value.nil?
|
47
|
+
@data[index] = value
|
48
|
+
end
|
49
|
+
|
50
|
+
# :nocov:
|
51
|
+
def == other
|
52
|
+
@data[0...@size] == other[0...@size] and @size == other.size
|
53
|
+
end
|
54
|
+
# :nocov:
|
55
|
+
|
56
|
+
def delete_at index
|
57
|
+
arry = @data.to_a
|
58
|
+
arry.delete_at index
|
59
|
+
@data = NMatrix.new [(2*@size-1)], arry, dtype: @nm_dtype
|
60
|
+
@size -= 1
|
61
|
+
end
|
62
|
+
|
63
|
+
def index key
|
64
|
+
@data.to_a.index key
|
65
|
+
end
|
66
|
+
|
67
|
+
# :nocov:
|
68
|
+
def << element
|
69
|
+
resize if @size >= @data.size
|
70
|
+
self[@size] = element
|
71
|
+
end
|
72
|
+
# :nocov:
|
73
|
+
|
74
|
+
def to_a
|
75
|
+
@data[0...@size].to_a
|
76
|
+
end
|
77
|
+
|
78
|
+
def dup
|
79
|
+
NMatrixWrapper.new @data[0...@size].to_a, @context, @nm_dtype
|
80
|
+
end
|
81
|
+
|
82
|
+
def resize size=@size*2
|
83
|
+
raise ArgumentError, 'Size must be greater than current size' if size < @size
|
84
|
+
|
85
|
+
@data = NMatrix.new [size], @data.to_a, dtype: @nm_dtype
|
86
|
+
end
|
87
|
+
|
88
|
+
# :nocov:
|
89
|
+
def mean
|
90
|
+
@data[0...@size].mean.first
|
91
|
+
end
|
92
|
+
|
93
|
+
def product
|
94
|
+
@data[0...@size].inject(1) { |m,e| m*e }
|
95
|
+
end
|
96
|
+
|
97
|
+
def sum
|
98
|
+
@data[0...@size].inject(:+)
|
99
|
+
end
|
100
|
+
|
101
|
+
def max
|
102
|
+
@data[0...@size].max
|
103
|
+
end
|
104
|
+
|
105
|
+
def min
|
106
|
+
@data[0...@size].min
|
107
|
+
end
|
108
|
+
# :nocov:
|
10
109
|
end
|
11
|
-
|
12
|
-
def map!(&block)
|
13
|
-
@data = NMatrix.new [@size*2], map(&block).to_a, dtype: nm_dtype
|
14
|
-
self
|
15
|
-
end
|
16
|
-
|
17
|
-
# :nocov:
|
18
|
-
# FIXME: not sure, why this kind of wrapper have such a pure coverage
|
19
|
-
def inject(*args, &block)
|
20
|
-
@data[0...@size].inject(*args, &block)
|
21
|
-
end
|
22
|
-
# :nocov:
|
23
|
-
|
24
|
-
attr_reader :size, :data, :nm_dtype
|
25
|
-
|
26
|
-
def initialize vector, context, nm_dtype=:int32
|
27
|
-
@size = vector.size
|
28
|
-
@data = NMatrix.new [@size*2], vector.to_a, dtype: nm_dtype
|
29
|
-
@context = context
|
30
|
-
@nm_dtype = @data.dtype
|
31
|
-
# init with twice the storage for reducing the need to resize
|
32
|
-
end
|
33
|
-
|
34
|
-
def [] *index
|
35
|
-
return @data[*index] if index[0] < @size
|
36
|
-
nil
|
37
|
-
end
|
38
|
-
|
39
|
-
def []= index, value
|
40
|
-
raise ArgumentError, "Index #{index} does not exist" if
|
41
|
-
index > @size && index < @data.size
|
42
|
-
resize if index >= @data.size
|
43
|
-
@size += 1 if index == @size
|
44
|
-
|
45
|
-
@data = @data.cast(dtype: :object) if value.nil?
|
46
|
-
@data[index] = value
|
47
|
-
end
|
48
|
-
|
49
|
-
# :nocov:
|
50
|
-
def == other
|
51
|
-
@data[0...@size] == other[0...@size] and @size == other.size
|
52
|
-
end
|
53
|
-
# :nocov:
|
54
|
-
|
55
|
-
def delete_at index
|
56
|
-
arry = @data.to_a
|
57
|
-
arry.delete_at index
|
58
|
-
@data = NMatrix.new [(2*@size-1)], arry, dtype: @nm_dtype
|
59
|
-
@size -= 1
|
60
|
-
end
|
61
|
-
|
62
|
-
def index key
|
63
|
-
@data.to_a.index key
|
64
|
-
end
|
65
|
-
|
66
|
-
# :nocov:
|
67
|
-
def << element
|
68
|
-
resize if @size >= @data.size
|
69
|
-
self[@size] = element
|
70
|
-
end
|
71
|
-
# :nocov:
|
72
|
-
|
73
|
-
def to_a
|
74
|
-
@data[0...@size].to_a
|
75
|
-
end
|
76
|
-
|
77
|
-
def dup
|
78
|
-
NMatrixWrapper.new @data[0...@size].to_a, @context, @nm_dtype
|
79
|
-
end
|
80
|
-
|
81
|
-
def resize size=@size*2
|
82
|
-
raise ArgumentError, 'Size must be greater than current size' if size < @size
|
83
|
-
|
84
|
-
@data = NMatrix.new [size], @data.to_a, dtype: @nm_dtype
|
85
|
-
end
|
86
|
-
|
87
|
-
# :nocov:
|
88
|
-
def mean
|
89
|
-
@data[0...@size].mean.first
|
90
|
-
end
|
91
|
-
|
92
|
-
def product
|
93
|
-
@data[0...@size].inject(1) { |m,e| m*e }
|
94
|
-
end
|
95
|
-
|
96
|
-
def sum
|
97
|
-
@data[0...@size].inject(:+)
|
98
|
-
end
|
99
|
-
|
100
|
-
def max
|
101
|
-
@data[0...@size].max
|
102
|
-
end
|
103
|
-
|
104
|
-
def min
|
105
|
-
@data[0...@size].min
|
106
|
-
end
|
107
|
-
# :nocov:
|
108
110
|
end
|
109
111
|
end
|
110
|
-
end
|
112
|
+
end
|