runarray 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -0
- data/{README → README.rdoc} +3 -5
- data/Rakefile +38 -142
- data/VERSION +1 -0
- data/lib/.vecutils.rb.swp +0 -0
- data/lib/runarray/narray.rb +128 -13
- data/spec/runarray/narray_spec.rb +290 -298
- data/spec/spec_helper.rb +16 -0
- metadata +90 -50
data/CHANGELOG
ADDED
data/{README → README.rdoc}
RENAMED
@@ -1,7 +1,7 @@
|
|
1
|
-
=
|
1
|
+
= runarray
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
runarray - Attempting to provide a pure ruby implementation of major NArray
|
4
|
+
functionality.
|
5
5
|
|
6
6
|
Pronounced like Scooby-Doo saying 'Run away!'.
|
7
7
|
|
@@ -10,7 +10,6 @@ implemented.
|
|
10
10
|
|
11
11
|
== Example
|
12
12
|
|
13
|
-
|
14
13
|
If you require the auto file then NArray is used if it is available. If not, the Runarray module is included and one can seamlessly use NArray objects.
|
15
14
|
|
16
15
|
require 'runarray/auto'
|
@@ -18,7 +17,6 @@ If you require the auto file then NArray is used if it is available. If not, th
|
|
18
17
|
na = NArray.float(4) # -> an NArray object if narray is available
|
19
18
|
# -> else an Runarray::NArray object
|
20
19
|
|
21
|
-
|
22
20
|
=== Using Pure Ruby
|
23
21
|
|
24
22
|
require 'runarray/narray'
|
data/Rakefile
CHANGED
@@ -1,149 +1,45 @@
|
|
1
|
-
|
2
|
-
require 'rubygems'
|
3
|
-
require 'rake/rdoctask'
|
4
|
-
require 'rake/gempackagetask'
|
5
|
-
require 'rake/testtask'
|
6
|
-
require 'rake/clean'
|
7
|
-
require 'fileutils'
|
8
|
-
|
9
|
-
###############################################
|
10
|
-
# GLOBAL
|
11
|
-
###############################################
|
12
|
-
|
13
|
-
FL = FileList
|
14
|
-
NAME = "runarray"
|
15
|
-
FU = FileUtils
|
16
|
-
|
17
|
-
readme = "README"
|
18
|
-
|
19
|
-
rdoc_dir = 'rdoc'
|
20
|
-
rdoc_extra_includes = [readme, "LICENSE"]
|
21
|
-
rdoc_options = ['--main', readme, '--title', NAME, '--line-numbers', '--inline-source']
|
22
|
-
|
23
|
-
lib_files = FL["lib/**/*.rb"]
|
24
|
-
dist_files = lib_files + FL[readme, "LICENSE", "Rakefile", "{specs}/**/*"]
|
25
|
-
changelog = 'CHANGELOG'
|
26
|
-
|
27
|
-
###############################################
|
28
|
-
# ENVIRONMENT
|
29
|
-
###############################################
|
30
|
-
ENV["OS"] == "Windows_NT" ? WIN32 = true : WIN32 = false
|
31
|
-
$gemcmd = "gem"
|
32
|
-
if WIN32
|
33
|
-
unless ENV["TERM"] == "cygwin"
|
34
|
-
$gemcmd << ".cmd"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
|
39
|
-
###############################################
|
40
|
-
# DOC
|
41
|
-
###############################################
|
42
|
-
Rake::RDocTask.new do |rd|
|
43
|
-
rd.rdoc_dir = rdoc_dir
|
44
|
-
rd.main = readme
|
45
|
-
rd.rdoc_files.include( rdoc_extra_includes )
|
46
|
-
rd.rdoc_files.include( lib_files.uniq )
|
47
|
-
rd.options.push( *rdoc_options )
|
48
|
-
end
|
49
|
-
|
50
|
-
desc "create and upload docs to server"
|
51
|
-
task :upload_docs => [:rdoc] do
|
52
|
-
sh "scp -r #{rdoc_dir}/* jtprince@rubyforge.org:/var/www/gforge-projects/axml/"
|
53
|
-
end
|
54
|
-
|
1
|
+
# encoding: utf-8
|
55
2
|
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rake'
|
56
5
|
|
57
|
-
|
58
|
-
|
59
|
-
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
9
|
+
gem.name = "runarray"
|
10
|
+
gem.homepage = "http://github.com/jtprince/runarray"
|
11
|
+
gem.license = "MIT"
|
12
|
+
gem.summary = %Q{a pure ruby implementation of a numeric array interface}
|
13
|
+
gem.description = %Q{a pure ruby implementation of a numeric array interface.}
|
14
|
+
gem.email = "jtprince@gmail.com"
|
15
|
+
gem.authors = ["John T. Prince"]
|
16
|
+
gem.add_development_dependency "rspec", "~> 2.8.0"
|
17
|
+
gem.add_development_dependency "rdoc", "~> 3.12"
|
18
|
+
#gem.add_development_dependency "bundler", "~> 1.0.0"
|
19
|
+
gem.add_development_dependency "jeweler", "~> 1.8.4"
|
20
|
+
#gem.add_development_dependency "rcov", ">= 0"
|
21
|
+
end
|
22
|
+
Jeweler::RubygemsDotOrgTasks.new
|
23
|
+
|
24
|
+
require 'rspec/core'
|
25
|
+
require 'rspec/core/rake_task'
|
26
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
27
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
28
|
+
end
|
29
|
+
|
30
|
+
#RSpec::Core::RakeTask.new(:rcov) do |spec|
|
31
|
+
# spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
# spec.rcov = true
|
33
|
+
#end
|
60
34
|
|
61
|
-
desc 'Default: Run specs.'
|
62
35
|
task :default => :spec
|
63
36
|
|
64
|
-
|
65
|
-
Rake::
|
66
|
-
|
67
|
-
t.warning = true
|
68
|
-
ENV['RUBYOPT'] = 'rubygems'
|
69
|
-
ENV['TEST'] = ENV['SPEC'] if ENV['SPEC']
|
70
|
-
t.libs = ['lib']
|
71
|
-
t.test_files = Dir.glob( File.join('spec', ENV['pattern'] || '**/*_spec.rb') )
|
72
|
-
t.options = "-v"
|
73
|
-
end
|
74
|
-
|
75
|
-
###############################################
|
76
|
-
# PACKAGE / INSTALL / UNINSTALL
|
77
|
-
###############################################
|
78
|
-
|
79
|
-
# looks for a header, collects the paragraph after the space
|
80
|
-
def get_section(header, file)
|
81
|
-
get_space = false
|
82
|
-
found_space = false
|
83
|
-
string = ''
|
84
|
-
IO.foreach(file) do |line|
|
85
|
-
if found_space
|
86
|
-
if line =~ /[^\s]/
|
87
|
-
string << line
|
88
|
-
else
|
89
|
-
break
|
90
|
-
end
|
91
|
-
elsif get_space
|
92
|
-
if line !~ /[^\s]/
|
93
|
-
found_space = true
|
94
|
-
get_space = false
|
95
|
-
end
|
96
|
-
elsif line =~ /^#{header}/
|
97
|
-
get_space = true
|
98
|
-
end
|
99
|
-
end
|
100
|
-
string.gsub!("\n", ' ')
|
101
|
-
end
|
102
|
-
|
103
|
-
tm = Time.now
|
104
|
-
gemspec = Gem::Specification.new do |t|
|
105
|
-
description = "pure ruby implementation of narray"
|
106
|
-
summary = "pure ruby implementation of narray"
|
107
|
-
t.platform = Gem::Platform::RUBY
|
108
|
-
t.name = NAME
|
109
|
-
t.version = IO.readlines(changelog).grep(/##.*version/).pop.split(/\s+/).last.chomp
|
110
|
-
t.homepage = 'http://rubyforge.org/projects/mspire'
|
111
|
-
t.rubyforge_project = 'mspire'
|
112
|
-
t.summary = summary
|
113
|
-
t.date = "#{tm.year}-#{tm.month}-#{tm.day}"
|
114
|
-
t.email = "jtprince@gmail.com"
|
115
|
-
t.description = description
|
116
|
-
t.has_rdoc = true
|
117
|
-
t.authors = ["John Prince"]
|
118
|
-
t.files = dist_files
|
119
|
-
t.rdoc_options = rdoc_options
|
120
|
-
t.extra_rdoc_files = rdoc_extra_includes
|
121
|
-
t.executables = FL["bin/*"].map {|file| File.basename(file) }
|
122
|
-
t.test_files = FL["spec/**/*_spec.rb"]
|
123
|
-
end
|
124
|
-
|
125
|
-
desc "Create packages."
|
126
|
-
Rake::GemPackageTask.new(gemspec) do |pkg|
|
127
|
-
#pkg.need_zip = true
|
128
|
-
#pkg.need_tar = true
|
129
|
-
end
|
37
|
+
require 'rdoc/task'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
130
40
|
|
131
|
-
|
132
|
-
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "runarray #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
133
45
|
end
|
134
|
-
|
135
|
-
task :install => [:reinstall]
|
136
|
-
|
137
|
-
desc "uninstalls the package, packages a fresh one, and installs"
|
138
|
-
task :reinstall => [:remove_pkg, :clean, :package] do
|
139
|
-
reply = `#{$gemcmd} list -l #{NAME}`
|
140
|
-
if reply.include?(NAME + " (")
|
141
|
-
%x( #{$gemcmd} uninstall -a -x #{NAME} )
|
142
|
-
end
|
143
|
-
FileUtils.cd("pkg") do
|
144
|
-
cmd = "#{$gemcmd} install #{NAME}*.gem"
|
145
|
-
puts "EXECUTING: #{cmd}"
|
146
|
-
system cmd
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.2
|
Binary file
|
data/lib/runarray/narray.rb
CHANGED
@@ -17,8 +17,8 @@ module Runarray
|
|
17
17
|
def build(typecode, *dims)
|
18
18
|
zero =
|
19
19
|
case typecode
|
20
|
-
when 'float'
|
21
|
-
when 'int'
|
20
|
+
when 'float' then 0.0
|
21
|
+
when 'int' then 0
|
22
22
|
end
|
23
23
|
raise NotImplementedError, "dims <= 1 right now" if dims.size > 2
|
24
24
|
case dims.size
|
@@ -336,9 +336,9 @@ module Runarray
|
|
336
336
|
case behavior
|
337
337
|
when "sum"
|
338
338
|
xvec_scaled.each_with_index do |ind,i|
|
339
|
-
|
340
|
-
|
341
|
-
|
339
|
+
val = yvec[i]
|
340
|
+
arr[ind] = nobl[ind] + val
|
341
|
+
nobl[ind] += val
|
342
342
|
end
|
343
343
|
when "high" ## FASTEST BEHAVIOR
|
344
344
|
xvec_scaled.each_with_index do |ind,i|
|
@@ -362,8 +362,8 @@ module Runarray
|
|
362
362
|
end
|
363
363
|
when "maxb"
|
364
364
|
xvec_scaled.each_with_index do |ind,i|
|
365
|
-
|
366
|
-
|
365
|
+
val = yvec[i]
|
366
|
+
if val > arr[ind]; arr[ind] = val end
|
367
367
|
end
|
368
368
|
else
|
369
369
|
warn "Not a valid behavior: #{behavior}, in one_dim\n"
|
@@ -740,6 +740,19 @@ module Runarray
|
|
740
740
|
end
|
741
741
|
|
742
742
|
|
743
|
+
# these are given in inclusive terms output is the array used for placing
|
744
|
+
# the data in (expects same size). If nil it will be in-place.
|
745
|
+
def clip(min, max, output=nil)
|
746
|
+
output ||= self
|
747
|
+
self.each_with_index do |v,i|
|
748
|
+
n = v
|
749
|
+
n = min if v < min
|
750
|
+
n = max if v > max
|
751
|
+
output[i] = n
|
752
|
+
end
|
753
|
+
output
|
754
|
+
end
|
755
|
+
|
743
756
|
# Returns (min, max)
|
744
757
|
def min_max
|
745
758
|
mn = self.first
|
@@ -751,6 +764,108 @@ module Runarray
|
|
751
764
|
return mn, mx
|
752
765
|
end
|
753
766
|
|
767
|
+
# originally taken from a pastebin posting (2009-08-26) which is
|
768
|
+
# considered public domain. (http://en.pastebin.ca/1255734)
|
769
|
+
# self is considered the x values. Returns y values at the x values given
|
770
|
+
def lowess(y, f=2.0/3.0, iter=3)
|
771
|
+
x = self
|
772
|
+
n = x.size
|
773
|
+
r = (f*n).ceil.to_i
|
774
|
+
# h = [numpy.sort(numpy.abs(x-x[i]))[r] for i in range(n)]
|
775
|
+
(0...n).each { |i| (x-x[i]).abs.sort[r] }
|
776
|
+
raise NotImplementedError, "not finished!"
|
777
|
+
|
778
|
+
#w = numpy.clip(numpy.abs(([x]-numpy.transpose([x]))/h),0.0,1.0)
|
779
|
+
#w = 1-w*w*w
|
780
|
+
#w = w*w*w
|
781
|
+
#yest = numpy.zeros(n)
|
782
|
+
#delta = numpy.ones(n)
|
783
|
+
#for iteration in range(iter):
|
784
|
+
# for i in range(n):
|
785
|
+
# weights = delta * w[:,i]
|
786
|
+
# theta = weights*x
|
787
|
+
# b_top = sum(weights*y)
|
788
|
+
# b_bot = sum(theta*y)
|
789
|
+
# a = sum(weights)
|
790
|
+
# b = sum(theta)
|
791
|
+
# d = sum(theta*x)
|
792
|
+
# yest[i] = (d*b_top-b*b_bot+(a*b_bot-b*b_top)*x[i])/(a*d-b**2)
|
793
|
+
# residuals = y-yest
|
794
|
+
# s = numpy.median(abs(residuals))
|
795
|
+
# delta = numpy.clip(residuals/(6*s),-1,1)
|
796
|
+
# delta = 1-delta*delta
|
797
|
+
# delta = delta*delta
|
798
|
+
# return yest
|
799
|
+
|
800
|
+
end
|
801
|
+
|
802
|
+
|
803
|
+
#"""
|
804
|
+
#This module implements the Lowess function for nonparametric regression.
|
805
|
+
|
806
|
+
#Functions:
|
807
|
+
#lowess Fit a smooth nonparametric regression curve to a scatterplot.
|
808
|
+
|
809
|
+
#For more information, see
|
810
|
+
|
811
|
+
#William S. Cleveland: "Robust locally weighted regression and smoothing
|
812
|
+
#scatterplots", Journal of the American Statistical Association, December 1979,
|
813
|
+
#volume 74, number 368, pp. 829-836.
|
814
|
+
|
815
|
+
#William S. Cleveland and Susan J. Devlin: "Locally weighted regression: An
|
816
|
+
#approach to regression analysis by local fitting", Journal of the American
|
817
|
+
#Statistical Association, September 1988, volume 83, number 403, pp. 596-610.
|
818
|
+
#"""
|
819
|
+
|
820
|
+
#import numpy
|
821
|
+
#try:
|
822
|
+
#from Bio.Cluster import median
|
823
|
+
## The function median in Bio.Cluster is faster than the function median
|
824
|
+
## in NumPy, as it does not require a full sort.
|
825
|
+
#except ImportError, x:
|
826
|
+
## Use the median function in NumPy if Bio.Cluster is not available
|
827
|
+
#from numpy import median
|
828
|
+
|
829
|
+
#def lowess(x, y, f=2./3., iter=3):
|
830
|
+
#"""lowess(x, y, f=2./3., iter=3) -> yest
|
831
|
+
|
832
|
+
#Lowess smoother: Robust locally weighted regression.
|
833
|
+
#The lowess function fits a nonparametric regression curve to a scatterplot.
|
834
|
+
#The arrays x and y contain an equal number of elements; each pair
|
835
|
+
#(x[i], y[i]) defines a data point in the scatterplot. The function returns
|
836
|
+
#the estimated (smooth) values of y.
|
837
|
+
|
838
|
+
#The smoothing span is given by f. A larger value for f will result in a
|
839
|
+
#smoother curve. The number of robustifying iterations is given by iter. The
|
840
|
+
#function will run faster with a smaller number of iterations."""
|
841
|
+
#n = len(x)
|
842
|
+
#r = int(numpy.ceil(f*n))
|
843
|
+
#h = [numpy.sort(numpy.abs(x-x[i]))[r] for i in range(n)]
|
844
|
+
#w = numpy.clip(numpy.abs(([x]-numpy.transpose([x]))/h),0.0,1.0)
|
845
|
+
#w = 1-w*w*w
|
846
|
+
#w = w*w*w
|
847
|
+
#yest = numpy.zeros(n)
|
848
|
+
#delta = numpy.ones(n)
|
849
|
+
#for iteration in range(iter):
|
850
|
+
#for i in range(n):
|
851
|
+
#weights = delta * w[:,i]
|
852
|
+
#theta = weights*x
|
853
|
+
#b_top = sum(weights*y)
|
854
|
+
#b_bot = sum(theta*y)
|
855
|
+
#a = sum(weights)
|
856
|
+
#b = sum(theta)
|
857
|
+
#d = sum(theta*x)
|
858
|
+
#yest[i] = (d*b_top-b*b_bot+(a*b_bot-b*b_top)*x[i])/(a*d-b**2)
|
859
|
+
#residuals = y-yest
|
860
|
+
#s = numpy.median(abs(residuals))
|
861
|
+
#delta = numpy.clip(residuals/(6*s),-1,1)
|
862
|
+
#delta = 1-delta*delta
|
863
|
+
#delta = delta*delta
|
864
|
+
#return yest
|
865
|
+
|
866
|
+
|
867
|
+
alias_method :loess, :lowess
|
868
|
+
|
754
869
|
=begin
|
755
870
|
# complete rewrite of the
|
756
871
|
# returns empty derivs for size == 0
|
@@ -988,13 +1103,13 @@ module Runarray
|
|
988
1103
|
#end
|
989
1104
|
|
990
1105
|
#class VecI < Vec
|
991
|
-
|
992
|
-
|
993
|
-
|
1106
|
+
#tmp = $VERBOSE ; $VERBOSE = nil
|
1107
|
+
#@@zero = 0
|
1108
|
+
#$VERBOSE = tmp
|
994
1109
|
|
995
|
-
|
996
|
-
|
997
|
-
|
1110
|
+
#def to_rep(val)
|
1111
|
+
#val.to_i
|
1112
|
+
#end
|
998
1113
|
#end
|
999
1114
|
|
1000
1115
|
|
@@ -1,291 +1,281 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
require 'narray'
|
4
4
|
require 'runarray/narray'
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
shared_examples 'initializer' do
|
7
|
+
context 'initializes' do
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
other = args.first
|
12
|
-
self.each_with_index do |v,i|
|
13
|
-
v.old_must_equal other[i]
|
14
|
-
end
|
15
|
-
else
|
16
|
-
self.old_must_equal(*args)
|
9
|
+
before do
|
10
|
+
@klass = nil # you define the class
|
17
11
|
end
|
18
|
-
end
|
19
12
|
|
13
|
+
it 'initializes with different types' do
|
14
|
+
@klass.float(3).enums [0.0, 0.0, 0.0]
|
15
|
+
@klass.object(3).enums [nil, nil, nil]
|
16
|
+
#@klass.byte(3).enums [ ]
|
17
|
+
end
|
18
|
+
end
|
20
19
|
end
|
21
20
|
|
22
|
-
|
21
|
+
shared_examples 'a runarray' do
|
22
|
+
context 'a runarray object' do
|
23
|
+
include Runarray
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
25
|
+
def initialize(*args)
|
26
|
+
super(*args)
|
27
|
+
@klass = NArray
|
28
|
+
end
|
29
29
|
|
30
|
-
|
30
|
+
it 'can be created with no arguments' do
|
31
|
+
@klass = NArray
|
32
|
+
obj1 = @klass.new
|
33
|
+
obj1.size.should == 0
|
34
|
+
obj1.class.should == @klass
|
35
|
+
end
|
31
36
|
|
32
|
-
|
33
|
-
|
37
|
+
it 'can be created given a desired size' do
|
38
|
+
size = 10
|
39
|
+
obj2 = @klass.new(size)
|
40
|
+
obj2.size.should == size
|
41
|
+
obj2.class.should == @klass
|
42
|
+
end
|
34
43
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
44
|
+
it 'can be created given an array' do
|
45
|
+
obj3 = @klass.new([1.0,2.0,3.0])
|
46
|
+
obj3.size.should == 3
|
47
|
+
obj3.class.should == @klass
|
48
|
+
end
|
39
49
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
50
|
+
it 'will cast with prep and []' do
|
51
|
+
obj4 = @klass.new([0.0,2.0,5.0])
|
52
|
+
obj5 = @klass.prep([0,2,5]) # should be converted into floats
|
53
|
+
obj6 = @klass[0,2,5] # should be converted into floats
|
54
|
+
obj4.each_index do |i|
|
55
|
+
obj5[i].class.should == obj4[i].class # "prep converts to floats"
|
56
|
+
obj6[i].class.should == obj4[i].class # "Class[] -> prep"
|
57
|
+
end
|
58
|
+
end
|
46
59
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
60
|
+
it 'will NOT cast values with new' do
|
61
|
+
obj4 = @klass.new([0.0,2.0,5.0])
|
62
|
+
obj5 = @klass.prep([0,2,5]) # should be converted into floats
|
63
|
+
obj6 = @klass[0,2,5] # should be converted into floats
|
64
|
+
obj7 = @klass.new([0,2,5])
|
65
|
+
obj5.should == obj4
|
66
|
+
obj6.should == obj4
|
67
|
+
obj4.each_index do |i|
|
68
|
+
obj7[i].class.wont_equal obj4[i].class # "w/o prep class stays int"
|
69
|
+
end
|
70
|
+
end
|
53
71
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
end
|
72
|
+
it 'will call itself equal to another object if all vals equal' do
|
73
|
+
obj4 = @klass.new([0.0,2.0,5.0])
|
74
|
+
obj4.should == [0.0,2.0,5.0] # "arrays and objs may be equal"
|
75
|
+
obj4.should == [0,2,5] # even if the types arr different, but equal val"
|
59
76
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
obj4.each_index do |i|
|
65
|
-
obj5[i].class.must_equal obj4[i].class # "prep converts to floats"
|
66
|
-
obj6[i].class.must_equal obj4[i].class # "Class[] -> prep"
|
77
|
+
obj8 = @klass[0,2,5]
|
78
|
+
obj9 = @klass.new(obj8)
|
79
|
+
obj9.should == obj8 # "new from #{@klass} object"
|
80
|
+
obj9.class.should == obj8.class
|
67
81
|
end
|
68
|
-
end
|
69
82
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
obj7[i].class.wont_equal obj4[i].class # "w/o prep class stays int"
|
83
|
+
it 'can do division' do
|
84
|
+
x = NArray[8,4,2]
|
85
|
+
y = NArray[4,2,1]
|
86
|
+
(x / 2).should == NArray[4,2,1]
|
87
|
+
vec_by_vec = NArray[2,2,2]
|
88
|
+
(x / y).should == vec_by_vec
|
89
|
+
x /= y
|
90
|
+
x.should == vec_by_vec
|
79
91
|
end
|
80
|
-
end
|
81
92
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
end
|
93
|
+
it 'can do addition' do
|
94
|
+
x = NArray[8,4,2]
|
95
|
+
y = NArray[4,2,1]
|
96
|
+
vec_by_vec = NArray[12,6,3]
|
97
|
+
(x + y).should == vec_by_vec
|
98
|
+
(x + 2).should == NArray[10,6,4]
|
99
|
+
x += y
|
100
|
+
x.should == vec_by_vec
|
101
|
+
end
|
92
102
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
103
|
+
it 'can do multiplication' do
|
104
|
+
x = NArray[8,4,2]
|
105
|
+
y = NArray[4,2,1]
|
106
|
+
vec_by_vec = NArray[32,8,2]
|
107
|
+
(x * y).should == vec_by_vec
|
108
|
+
(x * 2).should == NArray[16,8,4]
|
109
|
+
x *= y
|
110
|
+
x.should == vec_by_vec
|
111
|
+
end
|
102
112
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
it 'can do multiplication' do
|
114
|
-
x = NArray[8,4,2]
|
115
|
-
y = NArray[4,2,1]
|
116
|
-
vec_by_vec = NArray[32,8,2]
|
117
|
-
(x * y).must_equal vec_by_vec
|
118
|
-
(x * 2).must_equal NArray[16,8,4]
|
119
|
-
x *= y
|
120
|
-
x.must_equal vec_by_vec
|
121
|
-
end
|
113
|
+
it 'can do subtraction' do
|
114
|
+
x = NArray[8,4,2]
|
115
|
+
y = NArray[4,2,1]
|
116
|
+
vec_by_vec = NArray[4,2,1]
|
117
|
+
(x - y).should == vec_by_vec
|
118
|
+
(x - 2).should == NArray[6,2,0]
|
119
|
+
x -= y
|
120
|
+
x.should == vec_by_vec
|
121
|
+
end
|
122
122
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
(x - y).must_equal vec_by_vec
|
128
|
-
(x - 2).must_equal NArray[6,2,0]
|
129
|
-
x -= y
|
130
|
-
x.must_equal vec_by_vec
|
131
|
-
end
|
123
|
+
it 'can calculate orders' do
|
124
|
+
x = NArray[0.05, 0.5, 0.0009, 0.05, 0.5]
|
125
|
+
x.order.should == [2,0,3,1,4]
|
126
|
+
end
|
132
127
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
128
|
+
def _inc_x(x,y,xexp, exp,mz_start,mz_end,inc,bl,type)
|
129
|
+
(xvec, answ) = x.inc_x(y,mz_start,mz_end,inc,bl,type)
|
130
|
+
xvec == xexp and answ.class == x.class and answ == exp
|
131
|
+
end
|
137
132
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
133
|
+
it 'works for _inc_x (private)' do
|
134
|
+
|
135
|
+
bl = 33
|
136
|
+
x = NArray.new([300, 301, 302, 304, 304, 304.2, 305, 306, 307.6])
|
137
|
+
y = NArray.new([10, 20, 30, 50 , 55, 54, 70, 80, 90])
|
138
|
+
xexp = (300..310).to_a
|
139
|
+
exp = [10, 20, 30, bl, 159, 70, 80, bl, 90, bl, bl]
|
140
|
+
_inc_x(x,y,xexp,exp,300,310,1,bl,"sum").should == true
|
141
|
+
|
142
|
+
bl = 33
|
143
|
+
x = NArray.new([300, 301, 302, 304, 304, 304.2, 305, 306, 307.6])
|
144
|
+
y = NArray.new([10, 20, 30, 50 , 55, 54, 70, 80, 90])
|
145
|
+
xexp = (300..310).to_a
|
146
|
+
exp = [10, 20, 30, bl, 55, 70, 80, bl, 90, bl, bl]
|
147
|
+
_inc_x(x,y,xexp,exp,300,310, 1, bl, "max" ).should == true
|
148
|
+
|
149
|
+
bl = 15
|
150
|
+
x = NArray.new([300, 301, 302, 304, 304, 304.2, 305, 306, 307.6])
|
151
|
+
y = NArray.new([10, 20, 30, 50 , 55, 54, 70, 80, 90])
|
152
|
+
xexp = (300..310).to_a
|
153
|
+
exp = [bl, 20, 30, bl, 55, 70, 80, bl, 90, bl, bl]
|
154
|
+
_inc_x(x,y,xexp,exp, 300, 310, 1, bl, "maxb" ).should == true
|
155
|
+
|
156
|
+
bl = 33
|
157
|
+
x = NArray.new([300, 301, 302, 304, 304, 304.2, 305, 306, 307.6])
|
158
|
+
y = NArray.new([10, 20, 30, 50 , 55, 54, 70, 80, 90])
|
159
|
+
xexp = (300..310).to_a
|
160
|
+
exp = [10, 20, 30, bl, 54, 70, 80, bl, 90, bl, bl]
|
161
|
+
_inc_x(x,y,xexp,exp, 300, 310, 1, bl, "high" ).should == true
|
162
|
+
|
163
|
+
bl = 33
|
164
|
+
x = NArray.new([300, 301, 302, 304, 304, 304.2, 305, 306, 307.6])
|
165
|
+
y = NArray.new([10, 20, 30, 50 , 62, 68, 70, 80, 90])
|
166
|
+
xexp = (300..310).to_a
|
167
|
+
exp = [10, 20, 30, bl, 60, 70, 80, bl, 90, bl, bl]
|
168
|
+
_inc_x(x,y,xexp,exp, 300, 310, 1, bl, "avg" ).should == true
|
142
169
|
|
143
|
-
|
144
|
-
|
145
|
-
bl = 33
|
146
|
-
x = NArray.new([300, 301, 302, 304, 304, 304.2, 305, 306, 307.6])
|
147
|
-
y = NArray.new([10, 20, 30, 50 , 55, 54, 70, 80, 90])
|
148
|
-
xexp = (300..310).to_a
|
149
|
-
exp = [10, 20, 30, bl, 159, 70, 80, bl, 90, bl, bl]
|
150
|
-
_inc_x(x,y,xexp,exp,300,310,1,bl,"sum").must_equal true
|
151
|
-
|
152
|
-
bl = 33
|
153
|
-
x = NArray.new([300, 301, 302, 304, 304, 304.2, 305, 306, 307.6])
|
154
|
-
y = NArray.new([10, 20, 30, 50 , 55, 54, 70, 80, 90])
|
155
|
-
xexp = (300..310).to_a
|
156
|
-
exp = [10, 20, 30, bl, 55, 70, 80, bl, 90, bl, bl]
|
157
|
-
_inc_x(x,y,xexp,exp,300,310, 1, bl, "max" ).must_equal true
|
158
|
-
|
159
|
-
bl = 15
|
160
|
-
x = NArray.new([300, 301, 302, 304, 304, 304.2, 305, 306, 307.6])
|
161
|
-
y = NArray.new([10, 20, 30, 50 , 55, 54, 70, 80, 90])
|
162
|
-
xexp = (300..310).to_a
|
163
|
-
exp = [bl, 20, 30, bl, 55, 70, 80, bl, 90, bl, bl]
|
164
|
-
_inc_x(x,y,xexp,exp, 300, 310, 1, bl, "maxb" ).must_equal true
|
165
|
-
|
166
|
-
bl = 33
|
167
|
-
x = NArray.new([300, 301, 302, 304, 304, 304.2, 305, 306, 307.6])
|
168
|
-
y = NArray.new([10, 20, 30, 50 , 55, 54, 70, 80, 90])
|
169
|
-
xexp = (300..310).to_a
|
170
|
-
exp = [10, 20, 30, bl, 54, 70, 80, bl, 90, bl, bl]
|
171
|
-
_inc_x(x,y,xexp,exp, 300, 310, 1, bl, "high" ).must_equal true
|
172
|
-
|
173
|
-
bl = 33
|
174
|
-
x = NArray.new([300, 301, 302, 304, 304, 304.2, 305, 306, 307.6])
|
175
|
-
y = NArray.new([10, 20, 30, 50 , 62, 68, 70, 80, 90])
|
176
|
-
xexp = (300..310).to_a
|
177
|
-
exp = [10, 20, 30, bl, 60, 70, 80, bl, 90, bl, bl]
|
178
|
-
_inc_x(x,y,xexp,exp, 300, 310, 1, bl, "avg" ).must_equal true
|
170
|
+
end
|
179
171
|
|
180
|
-
|
172
|
+
it 'uses index notation' do
|
173
|
+
obj1 = NArray.new(10)
|
174
|
+
obj1[0].nil?.should == true
|
175
|
+
obj1[0] = 1
|
176
|
+
obj1[0].should == 1
|
177
|
+
end
|
181
178
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
end
|
179
|
+
it 'can calculate Pearsons R' do
|
180
|
+
x = NArray.new([0,1,2,3,4,5,6,7,8,9,10])
|
181
|
+
y = NArray.new([3,4,5,6,9,6,5,4,3,4,5])
|
182
|
+
x.pearsons_r(y).should be_within(1e-12).of(0.0709326902131908)
|
183
|
+
end
|
188
184
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
185
|
+
it 'can calculate rsq slope and intercept' do
|
186
|
+
obj1 = NArray[0,2,5,5]
|
187
|
+
obj2 = NArray[1,3,4,7]
|
188
|
+
rsq, slope, inter = obj1.rsq_slope_intercept(obj2)
|
189
|
+
rsq.should be_within(1e-6).of(0.758519)
|
190
|
+
slope.should be_within(1e-6).of(0.888888888)
|
191
|
+
inter.should be_within(1e-6).of(1.083333333)
|
192
|
+
|
193
|
+
obj3 = NArray[1,3]
|
194
|
+
obj4 = NArray[2,4]
|
195
|
+
rsq, slope, inter = obj3.rsq_slope_intercept(obj4)
|
196
|
+
[rsq, slope, inter].each {|v| v.should == 1.0 }
|
197
|
+
end
|
194
198
|
|
195
|
-
it 'can calculate rsq slope and intercept' do
|
196
|
-
obj1 = NArray[0,2,5,5]
|
197
|
-
obj2 = NArray[1,3,4,7]
|
198
|
-
rsq, slope, inter = obj1.rsq_slope_intercept(obj2)
|
199
|
-
rsq.must_be_close_to(0.758519, 0.000001)
|
200
|
-
slope.must_be_close_to(0.888888888, 0.000001)
|
201
|
-
inter.must_be_close_to(1.083333333, 0.000001)
|
202
|
-
|
203
|
-
obj3 = NArray[1,3]
|
204
|
-
obj4 = NArray[2,4]
|
205
|
-
rsq, slope, inter = obj3.rsq_slope_intercept(obj4)
|
206
|
-
[rsq, slope, inter].each {|v| v.must_equal 1.0 }
|
207
|
-
end
|
208
199
|
|
200
|
+
xit 'can find residuals from least squares' do
|
201
|
+
obj1 = NArray[0,2,5,5]
|
202
|
+
obj2 = NArray[1,3,4,7]
|
203
|
+
out = obj1.residuals_from_least_squares(obj2)
|
204
|
+
out.size.should == 4
|
205
|
+
# frozen
|
206
|
+
[-0.141112436470222, 0.235187394117037, -2.58706133528741, 2.49298637764059].zip(out) do |exp, ans|
|
207
|
+
ans.should be_within(0.0000000001).of(exp)
|
208
|
+
end
|
209
|
+
end
|
209
210
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
# frozen
|
216
|
-
[-0.141112436470222, 0.235187394117037, -2.58706133528741, 2.49298637764059].zip(out) do |exp, ans|
|
217
|
-
ans.must_be_close_to(exp, 0.0000000001)
|
211
|
+
it 'can find sample stats (mean and stdev)' do
|
212
|
+
obj1 = NArray[0,2,5,5,6,7]
|
213
|
+
mean, std_dev = obj1.sample_stats
|
214
|
+
mean.should be_within(0.00001).of(4.166666)
|
215
|
+
std_dev.should be_within(0.00001).of(2.639444)
|
218
216
|
end
|
219
|
-
end
|
220
217
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
std_dev.must_be_close_to(2.639444, 0.00001)
|
226
|
-
end
|
218
|
+
xit 'can find outliers by index' do
|
219
|
+
NArray[0,1,1,2,1,2,10,1,0,0,1].outliers(2).should == [6]
|
220
|
+
NArray[0,-10,1,2,1,2,10,1,0,0,1].outliers(2).should == [1,6]
|
221
|
+
end
|
227
222
|
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
223
|
+
xit 'can find outliers iteratively' do
|
224
|
+
NArray[-1,-1,0,0,0,0,1,1,15,100].outliers(2).should == [9]
|
225
|
+
NArray[-1,-1,0,0,0,0,1,1,15,100].outliers_iteratively(2).should == [8,9]
|
226
|
+
end
|
232
227
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
228
|
+
xit 'can delete outliers (for least squares residuals)' do
|
229
|
+
# Consistency/sanity checks right now (not accuracy)
|
230
|
+
x = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,10,0 ,1,2,3,4,5,6,7,8,9]
|
231
|
+
y = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0 ,10,1,2,3,4,5,6,7,8,9]
|
232
|
+
|
233
|
+
nx1, ny1 = x.delete_outliers(3.2, y)
|
234
|
+
expx1 = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,10,1,2,3,4,5,6,7,8,9]
|
235
|
+
expy1 = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0 ,1,2,3,4,5,6,7,8,9]
|
236
|
+
nx1.should == expx1
|
237
|
+
ny1.should == expy1
|
238
|
+
|
239
|
+
nx2, ny2 = x.delete_outliers(2.8, y)
|
240
|
+
expx2 = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8,9]
|
241
|
+
expy2 = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8,9]
|
242
|
+
nx2.should == expx2
|
243
|
+
ny2.should == expy2
|
244
|
+
|
245
|
+
#res = nx1.residuals_from_least_squares(ny1)
|
246
|
+
#mean, std = res.sample_stats
|
247
|
+
#puts res/std
|
248
|
+
|
249
|
+
nx, ny = x.delete_outliers_iteratively(3.2, y)
|
250
|
+
nx.should == expx2
|
251
|
+
ny.should == expy2
|
252
|
+
end
|
237
253
|
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
expx1 = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,10,1,2,3,4,5,6,7,8,9]
|
245
|
-
expy1 = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0 ,1,2,3,4,5,6,7,8,9]
|
246
|
-
nx1.must_equal expx1
|
247
|
-
ny1.must_equal expy1
|
248
|
-
|
249
|
-
nx2, ny2 = x.delete_outliers(2.8, y)
|
250
|
-
expx2 = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8,9]
|
251
|
-
expy2 = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8,9]
|
252
|
-
nx2.must_equal expx2
|
253
|
-
ny2.must_equal expy2
|
254
|
-
|
255
|
-
#res = nx1.residuals_from_least_squares(ny1)
|
256
|
-
#mean, std = res.sample_stats
|
257
|
-
#puts res/std
|
258
|
-
|
259
|
-
nx, ny = x.delete_outliers_iteratively(3.2, y)
|
260
|
-
nx.must_equal expx2
|
261
|
-
ny.must_equal expy2
|
262
|
-
end
|
254
|
+
xit 'finds outliers (for least squares residuals)' do
|
255
|
+
# Consistency/sanity checks right now (not accuracy)
|
256
|
+
x = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,10,0 ,1,2,3,7,5,6,7,8,9]
|
257
|
+
y = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0 ,10,1,2,3,4,5,6,7,8,9]
|
258
|
+
xdup = x
|
259
|
+
ydup = y
|
263
260
|
|
264
|
-
|
265
|
-
|
266
|
-
x = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,10,0 ,1,2,3,7,5,6,7,8,9]
|
267
|
-
y = NArray[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0 ,10,1,2,3,4,5,6,7,8,9]
|
268
|
-
xdup = x
|
269
|
-
ydup = y
|
261
|
+
ind = x.outliers(0.5, y)
|
262
|
+
ind.should == [10,11,15]
|
270
263
|
|
271
|
-
|
272
|
-
|
264
|
+
ind = x.outliers(0.7, y)
|
265
|
+
ind.should == [10,11]
|
273
266
|
|
274
|
-
|
275
|
-
|
267
|
+
ind2 = x.outliers_iteratively(0.7, y)
|
268
|
+
ind2.should == [10,11,15]
|
269
|
+
x.should == xdup # "method didn't change vector"
|
270
|
+
y.should == ydup # "method didn't change vector"
|
271
|
+
end
|
276
272
|
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
y.must_equal ydup # "method didn't change vector"
|
281
|
-
end
|
282
|
-
|
283
|
-
it 'finds _correct_indices (private)' do
|
284
|
-
NArray.new._correct_indices([[5],[0,3],[3],[1,4]]).must_equal [0,2,3,5,6,8]
|
285
|
-
end
|
273
|
+
it 'finds _correct_indices (private)' do
|
274
|
+
NArray.new._correct_indices([[5],[0,3],[3],[1,4]]).should == [0,2,3,5,6,8]
|
275
|
+
end
|
286
276
|
|
287
|
-
|
288
|
-
|
277
|
+
# TODO: fix this spec
|
278
|
+
# something is wrong in this guy
|
289
279
|
=begin
|
290
280
|
it 'can noisify its values' do
|
291
281
|
[[100,10,1],[-100,-10,-1]].each do |arr|
|
@@ -298,70 +288,72 @@ class RunarrayNArrayUnitSpec < MiniTest::Spec
|
|
298
288
|
x = NArray.prep([0,2,3,5,6,8])
|
299
289
|
x.noisify!(fraction)
|
300
290
|
xdup.zip(x) do |arr|
|
301
|
-
|
291
|
+
|
292
|
+
arr[1].should be_within((fraction*arr[0]).abs).of( arr[0] )
|
302
293
|
end
|
303
294
|
end
|
304
295
|
end
|
305
296
|
end
|
306
297
|
=end
|
307
|
-
|
308
|
-
it 'can duplicate itself' do
|
309
|
-
x = NArray[100,10,1]
|
310
|
-
d = x.dup
|
311
|
-
d.must_equal x
|
312
|
-
d.class.must_equal x.class
|
313
|
-
x[0] = 10.0
|
314
|
-
d.wont_equal x
|
315
|
-
end
|
316
298
|
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
299
|
+
it 'can duplicate itself' do
|
300
|
+
x = NArray[100,10,1]
|
301
|
+
d = x.dup
|
302
|
+
d.should == x
|
303
|
+
d.class.should == x.class
|
304
|
+
x[0] = 10.0
|
305
|
+
d.wont_equal x
|
306
|
+
end
|
322
307
|
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
308
|
+
it 'can do a moving average' do
|
309
|
+
obj = NArray[0,1,2,3,4,5,6].moving_avg
|
310
|
+
obj.should == [0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 5.5]
|
311
|
+
obj = NArray[0,1,2,3,10,5,6].moving_avg
|
312
|
+
obj.should == [0.5, 1.0, 2.0, 5.0, 6.0, 7.0, 5.5]
|
327
313
|
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
derivs = x.chim(y)
|
332
|
-
derivs.must_equal [0]
|
333
|
-
|
334
|
-
x = NArray[1,2]
|
335
|
-
y = NArray[2,4]
|
336
|
-
derivs = x.chim(y)
|
337
|
-
derivs.must_equal [2.0, 2.0]
|
338
|
-
|
339
|
-
x = NArray[0,1,2,3,4,5,6,7,8,9]
|
340
|
-
y = NArray[0,10,12,4,5,2,7,9,10,4]
|
341
|
-
derivs = x.chim(y)
|
342
|
-
[14, 3.3333333, 0, 0, 0, 0, 2.8571429, 1.3333333, 0, -9.5].zip(derivs) do |exp, act|
|
343
|
-
act.must_be_close_to(exp, 0.0001)
|
314
|
+
obj = NArray[0,1,2,3,4,5,6].moving_avg(4,4).should == [2.0, 2.5, 3.0, 3.0, 3.0, 3.5, 4.0]
|
315
|
+
obj = NArray[0,1,2,3,4,5,6].moving_avg(0,6).should == [3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0]
|
316
|
+
obj = NArray[0,1,2,3,4,5,6].moving_avg(6,0).should == [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
|
344
317
|
end
|
345
|
-
|
346
|
-
end
|
347
318
|
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
319
|
+
it 'can find 3-point derivatives (with chim)' do
|
320
|
+
x = NArray[1]
|
321
|
+
y = NArray[2]
|
322
|
+
derivs = x.chim(y)
|
323
|
+
derivs.should == [0]
|
324
|
+
|
325
|
+
x = NArray[1,2]
|
326
|
+
y = NArray[2,4]
|
327
|
+
derivs = x.chim(y)
|
328
|
+
derivs.should == [2.0, 2.0]
|
329
|
+
|
330
|
+
x = NArray[0,1,2,3,4,5,6,7,8,9]
|
331
|
+
y = NArray[0,10,12,4,5,2,7,9,10,4]
|
332
|
+
derivs = x.chim(y)
|
333
|
+
[14, 3.3333333, 0, 0, 0, 0, 2.8571429, 1.3333333, 0, -9.5].zip(derivs) do |exp, act|
|
334
|
+
act.should be_within(0.0001).of(exp)
|
335
|
+
end
|
336
|
+
|
363
337
|
end
|
364
|
-
end
|
365
338
|
|
339
|
+
it 'can do custom transformations (moving avg, stdev, etc)' do
|
340
|
+
vec = NArray[0,1,2,3,3,4,2,1]
|
341
|
+
# a three point moving average:
|
342
|
+
answ = vec.transform(1, 1) {|x| x.avg }
|
343
|
+
exp = [0.5, 1, 2, 8.0/3, 10.0/3, 3, 7.0/3, 3.0/2]
|
344
|
+
answ.should == exp
|
345
|
+
|
346
|
+
# 5 point stdeviation transformation
|
347
|
+
pre = 2
|
348
|
+
post = 2
|
349
|
+
# transform with the standard deviation
|
350
|
+
answ = vec.transform(pre, post) {|x| x.sample_stats[1] }
|
351
|
+
exp = [1.0, 1.29099444873581, 1.30384048104053, 1.14017542509914, 0.836660026534075, 1.14017542509914, 1.29099444873581, 1.52752523165195]
|
352
|
+
answ.zip(exp) do |ans, ex|
|
353
|
+
ans.should be_within(0.000000001).of(ex)
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
end
|
366
358
|
|
367
359
|
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rspec'
|
4
|
+
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
#Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.color_enabled = true
|
13
|
+
config.tty = true
|
14
|
+
config.formatter = :documentation # :progress, :html, :textmate
|
15
|
+
#config.formatter = :progress # :progress, :html, :textmate
|
16
|
+
end
|
metadata
CHANGED
@@ -1,66 +1,106 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: runarray
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
|
-
authors:
|
7
|
-
- John Prince
|
7
|
+
authors:
|
8
|
+
- John T. Prince
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
date: 2013-02-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.8.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.8.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rdoc
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.12'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.12'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jeweler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.8.4
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.8.4
|
62
|
+
description: a pure ruby implementation of a numeric array interface.
|
17
63
|
email: jtprince@gmail.com
|
18
64
|
executables: []
|
19
|
-
|
20
65
|
extensions: []
|
21
|
-
|
22
|
-
extra_rdoc_files:
|
23
|
-
- README
|
66
|
+
extra_rdoc_files:
|
24
67
|
- LICENSE
|
25
|
-
|
26
|
-
|
27
|
-
-
|
28
|
-
- lib/runarray.rb
|
29
|
-
- README
|
68
|
+
- README.rdoc
|
69
|
+
files:
|
70
|
+
- CHANGELOG
|
30
71
|
- LICENSE
|
72
|
+
- README.rdoc
|
31
73
|
- Rakefile
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
74
|
+
- VERSION
|
75
|
+
- lib/.vecutils.rb.swp
|
76
|
+
- lib/runarray.rb
|
77
|
+
- lib/runarray/auto.rb
|
78
|
+
- lib/runarray/narray.rb
|
79
|
+
- spec/runarray/narray_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
homepage: http://github.com/jtprince/runarray
|
82
|
+
licenses:
|
83
|
+
- MIT
|
36
84
|
post_install_message:
|
37
|
-
rdoc_options:
|
38
|
-
|
39
|
-
- README
|
40
|
-
- --title
|
41
|
-
- runarray
|
42
|
-
- --line-numbers
|
43
|
-
- --inline-source
|
44
|
-
require_paths:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
45
87
|
- lib
|
46
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
58
100
|
requirements: []
|
59
|
-
|
60
|
-
|
61
|
-
rubygems_version: 1.3.2
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.8.23
|
62
103
|
signing_key:
|
63
104
|
specification_version: 3
|
64
|
-
summary: pure ruby implementation of
|
65
|
-
test_files:
|
66
|
-
- spec/runarray/narray_spec.rb
|
105
|
+
summary: a pure ruby implementation of a numeric array interface
|
106
|
+
test_files: []
|