khiva 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d46c3d7ed62e5adf119b172084d7a7bf6f6a77d49eae2cb3039cca6ce4a9b9a3
4
+ data.tar.gz: '09b7f85b1c1ddd51f2a78c732b24350d4e43c2471ebccbe5e37f501213a4cf65'
5
+ SHA512:
6
+ metadata.gz: 7e5ef4486b11cd7333fd819381150f854a62b2c744c31014950f1e5e7ca8b17ede6e87e1277751c58211e029edb990a605e7d2f428c8374ec7c1cc63ec8c3c7d
7
+ data.tar.gz: a96c1716abec95ddef7caa6e389e8fa796c4ef03a584c0204508ab37a0096fce5b356040c2d1770e422803285a7a3aa2d7a27e4573e35d0faf89857b588f7100
@@ -0,0 +1,3 @@
1
+ ## 0.1.0 (2020-12-17)
2
+
3
+ - First release
@@ -0,0 +1,362 @@
1
+ Mozilla Public License, version 2.0
2
+
3
+ 1. Definitions
4
+
5
+ 1.1. "Contributor"
6
+
7
+ means each individual or legal entity that creates, contributes to the
8
+ creation of, or owns Covered Software.
9
+
10
+ 1.2. "Contributor Version"
11
+
12
+ means the combination of the Contributions of others (if any) used by a
13
+ Contributor and that particular Contributor's Contribution.
14
+
15
+ 1.3. "Contribution"
16
+
17
+ means Covered Software of a particular Contributor.
18
+
19
+ 1.4. "Covered Software"
20
+
21
+ means Source Code Form to which the initial Contributor has attached the
22
+ notice in Exhibit A, the Executable Form of such Source Code Form, and
23
+ Modifications of such Source Code Form, in each case including portions
24
+ thereof.
25
+
26
+ 1.5. "Incompatible With Secondary Licenses"
27
+ means
28
+
29
+ a. that the initial Contributor has attached the notice described in
30
+ Exhibit B to the Covered Software; or
31
+
32
+ b. that the Covered Software was made available under the terms of
33
+ version 1.1 or earlier of the License, but not also under the terms of
34
+ a Secondary License.
35
+
36
+ 1.6. "Executable Form"
37
+
38
+ means any form of the work other than Source Code Form.
39
+
40
+ 1.7. "Larger Work"
41
+
42
+ means a work that combines Covered Software with other material, in a
43
+ separate file or files, that is not Covered Software.
44
+
45
+ 1.8. "License"
46
+
47
+ means this document.
48
+
49
+ 1.9. "Licensable"
50
+
51
+ means having the right to grant, to the maximum extent possible, whether
52
+ at the time of the initial grant or subsequently, any and all of the
53
+ rights conveyed by this License.
54
+
55
+ 1.10. "Modifications"
56
+
57
+ means any of the following:
58
+
59
+ a. any file in Source Code Form that results from an addition to,
60
+ deletion from, or modification of the contents of Covered Software; or
61
+
62
+ b. any new file in Source Code Form that contains any Covered Software.
63
+
64
+ 1.11. "Patent Claims" of a Contributor
65
+
66
+ means any patent claim(s), including without limitation, method,
67
+ process, and apparatus claims, in any patent Licensable by such
68
+ Contributor that would be infringed, but for the grant of the License,
69
+ by the making, using, selling, offering for sale, having made, import,
70
+ or transfer of either its Contributions or its Contributor Version.
71
+
72
+ 1.12. "Secondary License"
73
+
74
+ means either the GNU General Public License, Version 2.0, the GNU Lesser
75
+ General Public License, Version 2.1, the GNU Affero General Public
76
+ License, Version 3.0, or any later versions of those licenses.
77
+
78
+ 1.13. "Source Code Form"
79
+
80
+ means the form of the work preferred for making modifications.
81
+
82
+ 1.14. "You" (or "Your")
83
+
84
+ means an individual or a legal entity exercising rights under this
85
+ License. For legal entities, "You" includes any entity that controls, is
86
+ controlled by, or is under common control with You. For purposes of this
87
+ definition, "control" means (a) the power, direct or indirect, to cause
88
+ the direction or management of such entity, whether by contract or
89
+ otherwise, or (b) ownership of more than fifty percent (50%) of the
90
+ outstanding shares or beneficial ownership of such entity.
91
+
92
+
93
+ 2. License Grants and Conditions
94
+
95
+ 2.1. Grants
96
+
97
+ Each Contributor hereby grants You a world-wide, royalty-free,
98
+ non-exclusive license:
99
+
100
+ a. under intellectual property rights (other than patent or trademark)
101
+ Licensable by such Contributor to use, reproduce, make available,
102
+ modify, display, perform, distribute, and otherwise exploit its
103
+ Contributions, either on an unmodified basis, with Modifications, or
104
+ as part of a Larger Work; and
105
+
106
+ b. under Patent Claims of such Contributor to make, use, sell, offer for
107
+ sale, have made, import, and otherwise transfer either its
108
+ Contributions or its Contributor Version.
109
+
110
+ 2.2. Effective Date
111
+
112
+ The licenses granted in Section 2.1 with respect to any Contribution
113
+ become effective for each Contribution on the date the Contributor first
114
+ distributes such Contribution.
115
+
116
+ 2.3. Limitations on Grant Scope
117
+
118
+ The licenses granted in this Section 2 are the only rights granted under
119
+ this License. No additional rights or licenses will be implied from the
120
+ distribution or licensing of Covered Software under this License.
121
+ Notwithstanding Section 2.1(b) above, no patent license is granted by a
122
+ Contributor:
123
+
124
+ a. for any code that a Contributor has removed from Covered Software; or
125
+
126
+ b. for infringements caused by: (i) Your and any other third party's
127
+ modifications of Covered Software, or (ii) the combination of its
128
+ Contributions with other software (except as part of its Contributor
129
+ Version); or
130
+
131
+ c. under Patent Claims infringed by Covered Software in the absence of
132
+ its Contributions.
133
+
134
+ This License does not grant any rights in the trademarks, service marks,
135
+ or logos of any Contributor (except as may be necessary to comply with
136
+ the notice requirements in Section 3.4).
137
+
138
+ 2.4. Subsequent Licenses
139
+
140
+ No Contributor makes additional grants as a result of Your choice to
141
+ distribute the Covered Software under a subsequent version of this
142
+ License (see Section 10.2) or under the terms of a Secondary License (if
143
+ permitted under the terms of Section 3.3).
144
+
145
+ 2.5. Representation
146
+
147
+ Each Contributor represents that the Contributor believes its
148
+ Contributions are its original creation(s) or it has sufficient rights to
149
+ grant the rights to its Contributions conveyed by this License.
150
+
151
+ 2.6. Fair Use
152
+
153
+ This License is not intended to limit any rights You have under
154
+ applicable copyright doctrines of fair use, fair dealing, or other
155
+ equivalents.
156
+
157
+ 2.7. Conditions
158
+
159
+ Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
160
+ Section 2.1.
161
+
162
+
163
+ 3. Responsibilities
164
+
165
+ 3.1. Distribution of Source Form
166
+
167
+ All distribution of Covered Software in Source Code Form, including any
168
+ Modifications that You create or to which You contribute, must be under
169
+ the terms of this License. You must inform recipients that the Source
170
+ Code Form of the Covered Software is governed by the terms of this
171
+ License, and how they can obtain a copy of this License. You may not
172
+ attempt to alter or restrict the recipients' rights in the Source Code
173
+ Form.
174
+
175
+ 3.2. Distribution of Executable Form
176
+
177
+ If You distribute Covered Software in Executable Form then:
178
+
179
+ a. such Covered Software must also be made available in Source Code Form,
180
+ as described in Section 3.1, and You must inform recipients of the
181
+ Executable Form how they can obtain a copy of such Source Code Form by
182
+ reasonable means in a timely manner, at a charge no more than the cost
183
+ of distribution to the recipient; and
184
+
185
+ b. You may distribute such Executable Form under the terms of this
186
+ License, or sublicense it under different terms, provided that the
187
+ license for the Executable Form does not attempt to limit or alter the
188
+ recipients' rights in the Source Code Form under this License.
189
+
190
+ 3.3. Distribution of a Larger Work
191
+
192
+ You may create and distribute a Larger Work under terms of Your choice,
193
+ provided that You also comply with the requirements of this License for
194
+ the Covered Software. If the Larger Work is a combination of Covered
195
+ Software with a work governed by one or more Secondary Licenses, and the
196
+ Covered Software is not Incompatible With Secondary Licenses, this
197
+ License permits You to additionally distribute such Covered Software
198
+ under the terms of such Secondary License(s), so that the recipient of
199
+ the Larger Work may, at their option, further distribute the Covered
200
+ Software under the terms of either this License or such Secondary
201
+ License(s).
202
+
203
+ 3.4. Notices
204
+
205
+ You may not remove or alter the substance of any license notices
206
+ (including copyright notices, patent notices, disclaimers of warranty, or
207
+ limitations of liability) contained within the Source Code Form of the
208
+ Covered Software, except that You may alter any license notices to the
209
+ extent required to remedy known factual inaccuracies.
210
+
211
+ 3.5. Application of Additional Terms
212
+
213
+ You may choose to offer, and to charge a fee for, warranty, support,
214
+ indemnity or liability obligations to one or more recipients of Covered
215
+ Software. However, You may do so only on Your own behalf, and not on
216
+ behalf of any Contributor. You must make it absolutely clear that any
217
+ such warranty, support, indemnity, or liability obligation is offered by
218
+ You alone, and You hereby agree to indemnify every Contributor for any
219
+ liability incurred by such Contributor as a result of warranty, support,
220
+ indemnity or liability terms You offer. You may include additional
221
+ disclaimers of warranty and limitations of liability specific to any
222
+ jurisdiction.
223
+
224
+ 4. Inability to Comply Due to Statute or Regulation
225
+
226
+ If it is impossible for You to comply with any of the terms of this License
227
+ with respect to some or all of the Covered Software due to statute,
228
+ judicial order, or regulation then You must: (a) comply with the terms of
229
+ this License to the maximum extent possible; and (b) describe the
230
+ limitations and the code they affect. Such description must be placed in a
231
+ text file included with all distributions of the Covered Software under
232
+ this License. Except to the extent prohibited by statute or regulation,
233
+ such description must be sufficiently detailed for a recipient of ordinary
234
+ skill to be able to understand it.
235
+
236
+ 5. Termination
237
+
238
+ 5.1. The rights granted under this License will terminate automatically if You
239
+ fail to comply with any of its terms. However, if You become compliant,
240
+ then the rights granted under this License from a particular Contributor
241
+ are reinstated (a) provisionally, unless and until such Contributor
242
+ explicitly and finally terminates Your grants, and (b) on an ongoing
243
+ basis, if such Contributor fails to notify You of the non-compliance by
244
+ some reasonable means prior to 60 days after You have come back into
245
+ compliance. Moreover, Your grants from a particular Contributor are
246
+ reinstated on an ongoing basis if such Contributor notifies You of the
247
+ non-compliance by some reasonable means, this is the first time You have
248
+ received notice of non-compliance with this License from such
249
+ Contributor, and You become compliant prior to 30 days after Your receipt
250
+ of the notice.
251
+
252
+ 5.2. If You initiate litigation against any entity by asserting a patent
253
+ infringement claim (excluding declaratory judgment actions,
254
+ counter-claims, and cross-claims) alleging that a Contributor Version
255
+ directly or indirectly infringes any patent, then the rights granted to
256
+ You by any and all Contributors for the Covered Software under Section
257
+ 2.1 of this License shall terminate.
258
+
259
+ 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
260
+ license agreements (excluding distributors and resellers) which have been
261
+ validly granted by You or Your distributors under this License prior to
262
+ termination shall survive termination.
263
+
264
+ 6. Disclaimer of Warranty
265
+
266
+ Covered Software is provided under this License on an "as is" basis,
267
+ without warranty of any kind, either expressed, implied, or statutory,
268
+ including, without limitation, warranties that the Covered Software is free
269
+ of defects, merchantable, fit for a particular purpose or non-infringing.
270
+ The entire risk as to the quality and performance of the Covered Software
271
+ is with You. Should any Covered Software prove defective in any respect,
272
+ You (not any Contributor) assume the cost of any necessary servicing,
273
+ repair, or correction. This disclaimer of warranty constitutes an essential
274
+ part of this License. No use of any Covered Software is authorized under
275
+ this License except under this disclaimer.
276
+
277
+ 7. Limitation of Liability
278
+
279
+ Under no circumstances and under no legal theory, whether tort (including
280
+ negligence), contract, or otherwise, shall any Contributor, or anyone who
281
+ distributes Covered Software as permitted above, be liable to You for any
282
+ direct, indirect, special, incidental, or consequential damages of any
283
+ character including, without limitation, damages for lost profits, loss of
284
+ goodwill, work stoppage, computer failure or malfunction, or any and all
285
+ other commercial damages or losses, even if such party shall have been
286
+ informed of the possibility of such damages. This limitation of liability
287
+ shall not apply to liability for death or personal injury resulting from
288
+ such party's negligence to the extent applicable law prohibits such
289
+ limitation. Some jurisdictions do not allow the exclusion or limitation of
290
+ incidental or consequential damages, so this exclusion and limitation may
291
+ not apply to You.
292
+
293
+ 8. Litigation
294
+
295
+ Any litigation relating to this License may be brought only in the courts
296
+ of a jurisdiction where the defendant maintains its principal place of
297
+ business and such litigation shall be governed by laws of that
298
+ jurisdiction, without reference to its conflict-of-law provisions. Nothing
299
+ in this Section shall prevent a party's ability to bring cross-claims or
300
+ counter-claims.
301
+
302
+ 9. Miscellaneous
303
+
304
+ This License represents the complete agreement concerning the subject
305
+ matter hereof. If any provision of this License is held to be
306
+ unenforceable, such provision shall be reformed only to the extent
307
+ necessary to make it enforceable. Any law or regulation which provides that
308
+ the language of a contract shall be construed against the drafter shall not
309
+ be used to construe this License against a Contributor.
310
+
311
+
312
+ 10. Versions of the License
313
+
314
+ 10.1. New Versions
315
+
316
+ Mozilla Foundation is the license steward. Except as provided in Section
317
+ 10.3, no one other than the license steward has the right to modify or
318
+ publish new versions of this License. Each version will be given a
319
+ distinguishing version number.
320
+
321
+ 10.2. Effect of New Versions
322
+
323
+ You may distribute the Covered Software under the terms of the version
324
+ of the License under which You originally received the Covered Software,
325
+ or under the terms of any subsequent version published by the license
326
+ steward.
327
+
328
+ 10.3. Modified Versions
329
+
330
+ If you create software not governed by this License, and you want to
331
+ create a new license for such software, you may create and use a
332
+ modified version of this License if you rename the license and remove
333
+ any references to the name of the license steward (except to note that
334
+ such modified license differs from this License).
335
+
336
+ 10.4. Distributing Source Code Form that is Incompatible With Secondary
337
+ Licenses If You choose to distribute Source Code Form that is
338
+ Incompatible With Secondary Licenses under the terms of this version of
339
+ the License, the notice described in Exhibit B of this License must be
340
+ attached.
341
+
342
+ Exhibit A - Source Code Form License Notice
343
+
344
+ This Source Code Form is subject to the
345
+ terms of the Mozilla Public License, v.
346
+ 2.0. If a copy of the MPL was not
347
+ distributed with this file, You can
348
+ obtain one at
349
+ http://mozilla.org/MPL/2.0/.
350
+
351
+ If it is not possible or desirable to put the notice in a particular file,
352
+ then You may include the notice in a location (such as a LICENSE file in a
353
+ relevant directory) where a recipient would be likely to look for such a
354
+ notice.
355
+
356
+ You may add additional accurate notices of copyright ownership.
357
+
358
+ Exhibit B - "Incompatible With Secondary Licenses" Notice
359
+
360
+ This Source Code Form is "Incompatible
361
+ With Secondary Licenses", as defined by
362
+ the Mozilla Public License, v. 2.0.
@@ -0,0 +1,202 @@
1
+ # Khiva
2
+
3
+ [Khiva](https://github.com/shapelets/khiva) - high-performance time series algorithms - for Ruby
4
+
5
+ [![Build Status](https://github.com/ankane/khiva-ruby/workflows/build/badge.svg?branch=master)](https://github.com/ankane/khiva-ruby/actions)
6
+
7
+ ## Installation
8
+
9
+ First, [install Khiva](#khiva-installation). For Homebrew use:
10
+
11
+ ```sh
12
+ brew install khiva
13
+ ```
14
+
15
+ Add this line to your application’s Gemfile:
16
+
17
+ ```ruby
18
+ gem 'khiva'
19
+ ```
20
+
21
+ ## Getting Started
22
+
23
+ Calculate a matrix profile
24
+
25
+ ```ruby
26
+ a = Khiva::Array.new([11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11])
27
+ b = Khiva::Array.new([9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9])
28
+ profile, index = Khiva::Matrix.stomp(a, b, 3)
29
+ ```
30
+
31
+ Find discords
32
+
33
+ ```ruby
34
+ discords, discords_indices, subsequence_indices = Khiva::Matrix.find_best_n_discords(profile, index, 2, 2)
35
+ ```
36
+
37
+ ## Arrays
38
+
39
+ Create an array from a Ruby array
40
+
41
+ ```ruby
42
+ Khiva::Array.new([1, 2, 3])
43
+ ```
44
+
45
+ Specify the type
46
+
47
+ ```ruby
48
+ Khiva::Array.new([1, 2, 3], type: :s64)
49
+ ```
50
+
51
+ Get the type and dimensions
52
+
53
+ ```ruby
54
+ a.type
55
+ a.dims
56
+ ```
57
+
58
+ Perform operations on arrays
59
+
60
+ ```ruby
61
+ a + b
62
+ a - b
63
+ a * b
64
+ a / b
65
+ a % b
66
+ a ** b
67
+ ```
68
+
69
+ Compare arrays
70
+
71
+ ```ruby
72
+ a.eq(b)
73
+ a.ne(b)
74
+ a.lt(b)
75
+ a.gt(b)
76
+ a.le(b)
77
+ a.ge(b)
78
+ ```
79
+
80
+ ## Distances
81
+
82
+ Dynamic time warping (DTW) distance
83
+
84
+ ```ruby
85
+ Khiva::Distances.dtw(tss)
86
+ ```
87
+
88
+ Euclidean distance
89
+
90
+ ```ruby
91
+ Khiva::Distances.euclidean(tss)
92
+ ```
93
+
94
+ Hamming distance
95
+
96
+ ```ruby
97
+ Khiva::Distances.hamming(tss)
98
+ ```
99
+
100
+ Manhattan distance
101
+
102
+ ```ruby
103
+ Khiva::Distances.manhattan(tss)
104
+ ```
105
+
106
+ Shape-based distance (SBD)
107
+
108
+ ```ruby
109
+ Khiva::Distances.sbd(tss)
110
+ ```
111
+
112
+ Squared Euclidean distance
113
+
114
+ ```ruby
115
+ Khiva::Distances.squared_euclidean(tss)
116
+ ```
117
+
118
+ ## Normalization
119
+
120
+ Decimal scaling
121
+
122
+ ```ruby
123
+ Khiva::Distances.decimal_scaling_norm(tss)
124
+ Khiva::Distances.decimal_scaling_norm!(tss)
125
+ ```
126
+
127
+ Max min
128
+
129
+ ```ruby
130
+ Khiva::Distances.max_min_norm(tss)
131
+ Khiva::Distances.max_min_norm!(tss)
132
+ ```
133
+
134
+ Mean
135
+
136
+ ```ruby
137
+ Khiva::Distances.mean_norm(tss)
138
+ Khiva::Distances.mean_norm!(tss)
139
+ ```
140
+
141
+ Znorm
142
+
143
+ ```ruby
144
+ Khiva::Distances.znorm(tss)
145
+ Khiva::Distances.znorm!(tss)
146
+ ```
147
+
148
+ ## Statistics
149
+
150
+ Covariance
151
+
152
+ ```ruby
153
+ Khiva::Statistics.covariance(tss, unbiased: false)
154
+ ```
155
+
156
+ ## Khiva Installation
157
+
158
+ ### Linux
159
+
160
+ Run:
161
+
162
+ ```sh
163
+ # todo
164
+ ```
165
+
166
+ ### Mac
167
+
168
+ Run:
169
+
170
+ ```sh
171
+ brew install khiva
172
+ ```
173
+
174
+ ### Windows
175
+
176
+ See [instructions](https://khiva.readthedocs.io/en/latest/gettingStarted.html#windows).
177
+
178
+ ## Credits
179
+
180
+ This library is modeled after the [Khiva-Python API](https://github.com/shapelets/khiva-python).
181
+
182
+ ## History
183
+
184
+ View the [changelog](https://github.com/ankane/khiva-ruby/blob/master/CHANGELOG.md)
185
+
186
+ ## Contributing
187
+
188
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
189
+
190
+ - [Report bugs](https://github.com/ankane/khiva-ruby/issues)
191
+ - Fix bugs and [submit pull requests](https://github.com/ankane/khiva-ruby/pulls)
192
+ - Write, clarify, or fix documentation
193
+ - Suggest or add new features
194
+
195
+ To get started with development:
196
+
197
+ ```sh
198
+ git clone https://github.com/ankane/khiva-ruby.git
199
+ cd khiva-ruby
200
+ bundle install
201
+ bundle exec rake test
202
+ ```
@@ -0,0 +1,37 @@
1
+ # stdlib
2
+ require "fiddle/import"
3
+
4
+ # modules
5
+ require "khiva/array"
6
+ require "khiva/distances"
7
+ require "khiva/library"
8
+ require "khiva/matrix"
9
+ require "khiva/normalization"
10
+ require "khiva/statistics"
11
+ require "khiva/utils"
12
+ require "khiva/version"
13
+
14
+ module Khiva
15
+ class Error < StandardError; end
16
+
17
+ class << self
18
+ attr_accessor :ffi_lib
19
+ end
20
+ self.ffi_lib =
21
+ if Gem.win_platform?
22
+ ["khiva_c.dll"]
23
+ elsif RbConfig::CONFIG["host_os"] =~ /darwin/i
24
+ ["libkhiva_c.dylib"]
25
+ else
26
+ ["libkhiva_c.so"]
27
+ end
28
+
29
+ # friendlier error message
30
+ autoload :FFI, "khiva/ffi"
31
+
32
+ def self.lib_version
33
+ v = Fiddle::Pointer.malloc(20)
34
+ FFI.call(:version, v.ref)
35
+ v.to_s
36
+ end
37
+ end
@@ -0,0 +1,138 @@
1
+ module Khiva
2
+ class Array
3
+ TYPES = [:f32, :c32, :f64, :c64, :b8, :s32, :u32, :u8, :s64, :u64, :s16, :u16]
4
+ # TODO support complex
5
+ TYPE_FORMAT = {
6
+ f32: "f",
7
+ c32: "",
8
+ f64: "d",
9
+ c64: "",
10
+ b8: "c",
11
+ s32: "l",
12
+ u32: "L",
13
+ u8: "C",
14
+ s64: "q",
15
+ u64: "Q",
16
+ s16: "s",
17
+ u16: "S"
18
+ }
19
+
20
+ def initialize(obj, type: nil)
21
+ if obj.is_a?(Fiddle::Pointer)
22
+ @ptr = obj
23
+ else
24
+ # TODO make more performant for Numo
25
+ obj = obj.to_a
26
+ dims = []
27
+ o = obj
28
+ 4.times do
29
+ break unless o.is_a?(::Array)
30
+ dims << o.size
31
+ o = o.first
32
+ end
33
+ dims.reverse!
34
+
35
+ flat_obj = obj.flatten(dims.size)
36
+
37
+ # TODO check each dimension
38
+ expected_size = dims.inject(1, &:*)
39
+ raise Error, "Unexpected size" if flat_obj.size != expected_size
40
+
41
+ # TODO check integer range
42
+ type ||= flat_obj.all? { |v| v.is_a?(Integer) } ? :s64 : :f64
43
+
44
+ data = Fiddle::Pointer[flat_obj.pack("#{TYPE_FORMAT[type]}*")]
45
+ ndims = dims.size
46
+ dims = Fiddle::Pointer[dims.pack("q!*")]
47
+ result = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
48
+
49
+ FFI.call(:create_array, data, ndims, dims, result, TYPES.index(type))
50
+ @ptr = result
51
+ end
52
+
53
+ ObjectSpace.define_finalizer(self, self.class.finalize(@ptr))
54
+ end
55
+
56
+ def display
57
+ FFI.call(:display, @ptr)
58
+ end
59
+
60
+ # TODO add bit operations
61
+ [
62
+ [:+, :khiva_add],
63
+ [:-, :khiva_sub],
64
+ [:*, :khiva_mul],
65
+ [:/, :khiva_div],
66
+ [:%, :khiva_mod],
67
+ [:**, :khiva_pow],
68
+ [:lt, :khiva_lt],
69
+ [:gt, :khiva_gt],
70
+ [:le, :khiva_le],
71
+ [:ge, :khiva_ge],
72
+ [:eq, :khiva_eq],
73
+ [:ne, :khiva_ne]
74
+ ].each do |ruby_method, ffi_method|
75
+ define_method(ruby_method) do |other|
76
+ result = Utils.create_ptr
77
+ FFI.call(ffi_method, @ptr, other, result)
78
+ self.class.new(result)
79
+ end
80
+ end
81
+
82
+ def to_a
83
+ d = dims
84
+ d.pop while d.last == 1 && d.size > 1
85
+
86
+ elements = dims.inject(1, &:*)
87
+ data = Fiddle::Pointer.malloc(elements * element_size)
88
+ FFI.call(:get_data, @ptr, data)
89
+ result = data.to_s(data.size).unpack("#{TYPE_FORMAT[type]}*")
90
+ result.map! { |r| r > 0 ? true : false } if type == :b8
91
+ Utils.reshape(result, d)
92
+ end
93
+
94
+ def dims
95
+ dims = Fiddle::Pointer.malloc(Fiddle::SIZEOF_LONG_LONG * 4)
96
+ FFI.call(:get_dims, @ptr, dims)
97
+ dims.to_s(dims.size).unpack("q!*")
98
+ end
99
+
100
+ def type
101
+ type = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
102
+ FFI.call(:get_type, @ptr, type)
103
+ TYPES[type.to_s(type.size).unpack1("i")]
104
+ end
105
+
106
+ def to_ptr
107
+ @ptr
108
+ end
109
+
110
+ def element_size
111
+ case type
112
+ when :b8, :u8
113
+ 1
114
+ when :s16, :u16
115
+ 2
116
+ when :f32, :c32, :s32, :u32
117
+ 4
118
+ when :f64, :c64, :s64, :u64
119
+ 8
120
+ else
121
+ raise Error, "Unknown type: #{type}"
122
+ end
123
+ end
124
+
125
+ def copy
126
+ result = Utils.create_ptr
127
+ FFI.call(:copy, @ptr, result)
128
+ self.class.new(result)
129
+ end
130
+ alias_method :dup, :copy
131
+ alias_method :clone, :copy
132
+
133
+ def self.finalize(ptr)
134
+ # must use proc instead of stabby lambda
135
+ proc { FFI.call(:delete_array, ptr) }
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,41 @@
1
+ module Khiva
2
+ module Distances
3
+ class << self
4
+ def dtw(tss)
5
+ result = Utils.create_ptr
6
+ FFI.call(:dtw, tss, result)
7
+ Array.new(result)
8
+ end
9
+
10
+ def euclidean(tss)
11
+ result = Utils.create_ptr
12
+ FFI.call(:euclidean, tss, result)
13
+ Array.new(result)
14
+ end
15
+
16
+ def hamming(tss)
17
+ result = Utils.create_ptr
18
+ FFI.call(:hamming, tss, result)
19
+ Array.new(result)
20
+ end
21
+
22
+ def manhattan(tss)
23
+ result = Utils.create_ptr
24
+ FFI.call(:manhattan, tss, result)
25
+ Array.new(result)
26
+ end
27
+
28
+ def sbd(tss)
29
+ result = Utils.create_ptr
30
+ FFI.call(:sbd, tss, result)
31
+ Array.new(result)
32
+ end
33
+
34
+ def squared_euclidean(tss)
35
+ result = Utils.create_ptr
36
+ FFI.call(:squared_euclidean, tss, result)
37
+ Array.new(result)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,101 @@
1
+ module Khiva
2
+ module FFI
3
+ extend Fiddle::Importer
4
+
5
+ libs = Khiva.ffi_lib.dup
6
+ begin
7
+ dlload Fiddle.dlopen(libs.shift)
8
+ rescue Fiddle::DLError => e
9
+ retry if libs.any?
10
+ raise e if ENV["KHIVA_DEBUG"]
11
+ raise LoadError, "Khiva not found"
12
+ end
13
+
14
+ typealias "bool", "char"
15
+
16
+ # array.h
17
+ extern "void create_array(const void *data, unsigned int ndims, const long long *dims, khiva_array *result, int type, int *error_code, char *error_message)"
18
+ extern "void delete_array(khiva_array *array, int *error_code, char *error_message)"
19
+ extern "void display(const khiva_array *array, int *error_code, char *error_message)"
20
+ extern "void get_data(const khiva_array *array, void *data, int *error_code, char *error_message)"
21
+ extern "void get_dims(const khiva_array *array, long long *dims, int *error_code, char *error_message)"
22
+ extern "void get_type(const khiva_array *array, int *t, int *error_code, char *error_message)"
23
+ extern "void join(int dim, const khiva_array *first, const khiva_array *second, khiva_array *result, int *error_code, char *error_message)"
24
+ extern "void khiva_add(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
25
+ extern "void khiva_mul(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
26
+ extern "void khiva_sub(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
27
+ extern "void khiva_div(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
28
+ extern "void khiva_mod(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
29
+ extern "void khiva_pow(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
30
+ extern "void khiva_lt(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
31
+ extern "void khiva_gt(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
32
+ extern "void khiva_le(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
33
+ extern "void khiva_ge(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
34
+ extern "void khiva_eq(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
35
+ extern "void khiva_ne(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
36
+ extern "void khiva_bitand(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
37
+ extern "void khiva_bitor(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
38
+ extern "void khiva_bitxor(const khiva_array *lhs, const khiva_array *rhs, khiva_array *result, int *error_code, char *error_message)"
39
+ extern "void khiva_bitshiftl(const khiva_array *array, int n, khiva_array *result, int *error_code, char *error_message)"
40
+ extern "void khiva_bitshiftr(const khiva_array *array, int n, khiva_array *result, int *error_code, char *error_message)"
41
+ extern "void khiva_not(const khiva_array *array, khiva_array *result, int *error_code, char *error_message)"
42
+ extern "void copy(const khiva_array *array, khiva_array *result, int *error_code, char *error_message)"
43
+
44
+ # library.h
45
+ extern "void backend_info(char **info, int *error_code, char *error_message)"
46
+ extern "void set_backend(const int *backend, int *error_code, char *error_message)"
47
+ extern "void set_device(const int *device, int *error_code, char *error_message)"
48
+ extern "void version(char **v, int *error_code, char *error_message)"
49
+
50
+ # distances.h
51
+ extern "void dtw(const khiva_array *tss, khiva_array *result, int *error_code, char *error_message)"
52
+ extern "void euclidean(const khiva_array *tss, khiva_array *result, int *error_code, char *error_message)"
53
+ extern "void hamming(const khiva_array *tss, khiva_array *result, int *error_code, char *error_message)"
54
+ extern "void manhattan(const khiva_array *tss, khiva_array *result, int *error_code, char *error_message)"
55
+ extern "void sbd(const khiva_array *tss, khiva_array *result, int *error_code, char *error_message)"
56
+ extern "void squared_euclidean(const khiva_array *tss, khiva_array *result, int *error_code, char *error_message)"
57
+
58
+ # matrix.h
59
+ extern "void find_best_n_discords(const khiva_array *profile, const khiva_array *index, long m, long n, khiva_array *discord_distances, khiva_array *discord_indices, khiva_array *subsequence_indices, bool self_join, int *error_code, char *error_message)"
60
+ extern "void stomp(const khiva_array *tssa, const khiva_array *tssb, long m, khiva_array *p, khiva_array *i, int *error_code, char *error_message)"
61
+
62
+ # normalization.h
63
+ extern "void decimal_scaling_norm(const khiva_array *tss, khiva_array *result, int *error_code, char *error_message)"
64
+ extern "void decimal_scaling_norm_in_place(khiva_array *tss, int *error_code, char *error_message)"
65
+ extern "void max_min_norm(const khiva_array *tss, const double *high, const double *low, const double *epsilon, khiva_array *result, int *error_code, char *error_message)"
66
+ extern "void max_min_norm_in_place(khiva_array *tss, const double *high, const double *low, const double *epsilon, int *error_code, char *error_message)"
67
+ extern "void mean_norm(const khiva_array *tss, khiva_array *result, int *error_code, char *error_message)"
68
+ extern "void mean_norm_in_place(khiva_array *tss, int *error_code, char *error_message)"
69
+ extern "void znorm(const khiva_array *tss, const double *epsilon, khiva_array *result, int *error_code, char *error_message)"
70
+ extern "void znorm_in_place(khiva_array *tss, const double *epsilon, int *error_code, char *error_message)"
71
+
72
+ # statistics.h
73
+ extern "void covariance_statistics(const khiva_array *tss, const bool *unbiased, khiva_array *result, int *error_code, char *error_message)"
74
+ extern "void kurtosis_statistics(const khiva_array *tss, khiva_array *result, int *error_code, char *error_message)"
75
+ extern "void ljung_box(const khiva_array *tss, const long *lags, khiva_array *result, int *error_code, char *error_message)"
76
+ extern "void moment_statistics(const khiva_array *tss, const int *k, khiva_array *result, int *error_code, char *error_message)"
77
+ extern "void quantile_statistics(const khiva_array *tss, const khiva_array *q, const float *precision, khiva_array *result, int *error_code, char *error_message)"
78
+ extern "void quantiles_cut_statistics(const khiva_array *tss, const float *quantiles, const float *precision, khiva_array *result, int *error_code, char *error_message)"
79
+ extern "void sample_stdev_statistics(const khiva_array *tss, khiva_array *result, int *error_code, char *error_message)"
80
+ extern "void skewness_statistics(const khiva_array *tss, khiva_array *result, int *error_code, char *error_message)"
81
+
82
+ class << self
83
+ # thread-safe
84
+ def error_code
85
+ Thread.current[:khiva_error_code] ||= Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
86
+ end
87
+
88
+ # thread-safe
89
+ def error_message
90
+ Thread.current[:khiva_error_message] ||= Fiddle::Pointer.malloc(256)
91
+ end
92
+
93
+ def call(method, *args)
94
+ FFI.send(method, *args, error_code, error_message)
95
+ if error_code.to_s(error_code.size).unpack1("i*") != 0
96
+ raise Error, error_message.to_s
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,28 @@
1
+ module Khiva
2
+ module Library
3
+ BACKENDS = {
4
+ default: 0,
5
+ cpu: 1,
6
+ cuda: 2,
7
+ opencl: 4
8
+ }
9
+
10
+ class << self
11
+ def backend_info
12
+ info = Fiddle::Pointer.malloc(1000)
13
+ FFI.call(:backend_info, info.ref)
14
+ info.to_s
15
+ end
16
+
17
+ def set_backend(backend)
18
+ b = BACKENDS[backend]
19
+ raise Error, "Invalid backend: #{backend}" unless b
20
+ FFI.call(:set_backend, Utils.int_ptr(b))
21
+ end
22
+
23
+ def set_device(device)
24
+ FFI.call(:set_device, Utils.int_ptr(device))
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ module Khiva
2
+ module Matrix
3
+ class << self
4
+ def find_best_n_discords(profile, index, m, n, self_join: false)
5
+ discord_distances = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
6
+ discord_indices = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
7
+ subsequence_indices = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
8
+ FFI.call(:find_best_n_discords, profile, index, m, n, discord_distances, discord_indices, subsequence_indices, self_join ? 1 : 0)
9
+ [Array.new(discord_distances), Array.new(discord_indices), Array.new(subsequence_indices)]
10
+ end
11
+
12
+ def stomp(tssa, tssb, m)
13
+ profile = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
14
+ index = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
15
+ FFI.call(:stomp, tssa, tssb, m, profile, index)
16
+ [Array.new(profile), Array.new(index)]
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,49 @@
1
+ module Khiva
2
+ module Normalization
3
+ class << self
4
+ def decimal_scaling_norm(tss)
5
+ result = Utils.create_ptr
6
+ FFI.call(:decimal_scaling_norm, tss, result)
7
+ Array.new(result)
8
+ end
9
+
10
+ def decimal_scaling_norm!(tss)
11
+ FFI.call(:decimal_scaling_norm_in_place, tss)
12
+ tss
13
+ end
14
+
15
+ def max_min_norm(tss, high: 1, low: 0, epsilon: 0.00000001)
16
+ result = Utils.create_ptr
17
+ FFI.call(:max_min_norm, tss, Utils.double_ptr(high), Utils.double_ptr(low), Utils.double_ptr(epsilon), result)
18
+ Array.new(result)
19
+ end
20
+
21
+ def max_min_norm!(tss, high: 1, low: 0, epsilon: 0.00000001)
22
+ FFI.call(:max_min_norm_in_place, tss, Utils.double_ptr(high), Utils.double_ptr(low), Utils.double_ptr(epsilon))
23
+ tss
24
+ end
25
+
26
+ def mean_norm(tss)
27
+ result = Utils.create_ptr
28
+ FFI.call(:mean_norm, tss, result)
29
+ Array.new(result)
30
+ end
31
+
32
+ def mean_norm!(tss)
33
+ FFI.call(:mean_norm_in_place, tss)
34
+ tss
35
+ end
36
+
37
+ def znorm(tss, epsilon: 0.00000001)
38
+ result = Utils.create_ptr
39
+ FFI.call(:znorm, tss, Utils.double_ptr(epsilon), result)
40
+ Array.new(result)
41
+ end
42
+
43
+ def znorm!(tss, epsilon: 0.00000001)
44
+ FFI.call(:znorm_in_place, tss, Utils.double_ptr(epsilon))
45
+ tss
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,11 @@
1
+ module Khiva
2
+ module Statistics
3
+ class << self
4
+ def covariance(tss, unbiased: false)
5
+ result = Utils.create_ptr
6
+ FFI.call(:covariance_statistics, tss, Utils.bool_ptr(unbiased), result)
7
+ Array.new(result)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,30 @@
1
+ module Khiva
2
+ module Utils
3
+ class << self
4
+ def create_ptr
5
+ Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
6
+ end
7
+
8
+ def int_ptr(v)
9
+ Fiddle::Pointer[[v].pack("i*")]
10
+ end
11
+
12
+ def double_ptr(v)
13
+ Fiddle::Pointer[[v].pack("d")]
14
+ end
15
+
16
+ def bool_ptr(v)
17
+ Fiddle::Pointer[[v ? 1 : 0].pack("c")]
18
+ end
19
+
20
+ # TODO make more efficient
21
+ def reshape(arr, dims)
22
+ arr = arr.flatten
23
+ dims[1..-1].reverse.each do |dim|
24
+ arr = arr.each_slice(dim)
25
+ end
26
+ arr.to_a
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Khiva
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: khiva
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: andrew@chartkick.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - CHANGELOG.md
20
+ - LICENSE.txt
21
+ - README.md
22
+ - lib/khiva.rb
23
+ - lib/khiva/array.rb
24
+ - lib/khiva/distances.rb
25
+ - lib/khiva/ffi.rb
26
+ - lib/khiva/library.rb
27
+ - lib/khiva/matrix.rb
28
+ - lib/khiva/normalization.rb
29
+ - lib/khiva/statistics.rb
30
+ - lib/khiva/utils.rb
31
+ - lib/khiva/version.rb
32
+ homepage: https://github.com/ankane/khiva
33
+ licenses:
34
+ - MPL-2.0
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '2.5'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.1.4
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: High-performance time series algorithms for Ruby
55
+ test_files: []