narray_miss 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0eae189cb0630885345fb435009238c765297353
4
- data.tar.gz: 6439d44068f017bab6f17b7853abcc337d614154
3
+ metadata.gz: ad81e7b19996cde6cb259232a5d63498de4264ea
4
+ data.tar.gz: 6a6fcb1482ed57dd4305a90468a78062beb897ac
5
5
  SHA512:
6
- metadata.gz: a1679506a1a943f754aeff6bc7a9a3f93373d67bca0f76d5c6fbbf6ff4add65c5c84d798cc42fe61975d80a8c877f3a98425e90100d06366dbcdd24fa3861135
7
- data.tar.gz: 4c85b99c69bf120b1e2d21c0de7953dfebe3eb2328cdd1692bdf524ef93a000e4e06c79816cd1f192c6be042f4046e953e6daeff1ef96c05c9b04bdd71bbd769
6
+ metadata.gz: a05718777eee5284f4ce9c202acaf0994976fc50ed6f8f7c6be95325fd9da2dc0beca71c0cd9de4744eb2c0c021a988fa31463133ac4674383f9fe3c2c314c4b
7
+ data.tar.gz: 0f350a12bff835850166279c8daff99cb3019c6da5ec96bcdf3e7d2c70e0de27f9ccbbd81c4e06623d0e167a3016765bd06ffa63693df7d3a220d7d57378b581
@@ -34,18 +34,26 @@ To use NArrayMiss class, you need invoking "require 'narray_miss.rb'" in your sc
34
34
 
35
35
  =end
36
36
 
37
- begin
38
- require 'numru/narray'
37
+ if defined?(NumRu::NArray)
39
38
  NARRAY = NumRu::NArray
40
- rescue LoadError
41
- err = $!
39
+ elsif defined?(NArray)
40
+ NARRAY = NArray
41
+ else
42
+
42
43
  begin
43
44
  require 'narray'
44
45
  NARRAY = NArray
45
46
  rescue LoadError
46
- STDERR.puts "You should install numru-narray or narray"
47
- raise err
47
+ err = $!
48
+ begin
49
+ require 'numru/narray'
50
+ NARRAY = NumRu::NArray
51
+ rescue LoadError
52
+ STDERR.puts "You should install numru-narray or narray"
53
+ raise err
54
+ end
48
55
  end
56
+
49
57
  end
50
58
 
51
59
 
@@ -185,7 +193,7 @@ go back to ((<Index>))
185
193
  array = arg[0]
186
194
  if Numeric===array then array = NARRAY[array] end
187
195
  if Array===array then array = NARRAY.to_na(array) end
188
- if !array.is_a?(NARRAY) then
196
+ unless array.is_a?(NARRAY) || /NArray/=~ array.class.to_s then
189
197
  raise("argument must be Numeric, NArray or Array")
190
198
  end
191
199
 
@@ -193,29 +201,26 @@ go back to ((<Index>))
193
201
  mask = arg[1]
194
202
  if Numeric===mask then mask = array.ne(mask) end
195
203
  if Array===mask then
196
- mask = NARRAY.to_na(mask).ne(0)
204
+ mask = array.class.to_na(mask).ne(0)
197
205
  end
198
206
  if mask.class == FalseClass then
199
- mask = NARRAY.byte(*array.shape)
207
+ mask = array.class.byte(*array.shape)
200
208
  end
201
209
  if mask.class == TrueClass then
202
- mask = NARRAY.byte(*array.shape).fill(1)
210
+ mask = array.class.byte(*array.shape).fill(1)
203
211
  end
204
- if !(NARRAY===mask && mask.typecode==BYTE) then
212
+ if !(array.class===mask && mask.typecode==BYTE) then
205
213
  raise("mask must be Numeric, Array, true, false or NArray(byte)")
206
214
  end
207
215
  if mask.length!=array.length
208
216
  raise "mask.length must be same as array.length"
209
217
  end
210
218
  else
211
- mask = NARRAY.byte(*array.shape).fill(1)
219
+ mask = array.class.byte(*array.shape).fill(1)
212
220
  end
213
221
  __new__(array,mask)
214
222
  end
215
223
  def self.to_nam(*arg)
216
- if !(Numeric===arg[0]) && !(Array===arg[0]) && !arg[0].is_a?(NARRAY)
217
- raise "first argument must be Numeric, NArray or Array"
218
- end
219
224
  arg[0] = arg[0].dup if !(Numeric===arg[0])
220
225
  if arg.length==2 && !(Numeric===arg[1]) && arg[1].class!=TrueClass && arg[1].class!=FalseClass then
221
226
  arg[1] = arg[1].dup
@@ -617,7 +622,7 @@ go back to ((<Index>))
617
622
  def mean(*dims)
618
623
  min_count = NArrayMiss.check_options(dims, 1)
619
624
  # 整数型の場合は浮動小数型へ変換
620
- ary0 = self.integer? ? self.to_type(NARRAY::DFLOAT) : self
625
+ ary0 = self.integer? ? self.to_type(@array.class.const_get(:DFLOAT)) : self
621
626
  NArrayMiss.reduction(@mask, rank, min_count, dims, true, typecode) do |count_sum, count_accum|
622
627
  ary0.sum(*dims)/count_sum
623
628
  end
@@ -625,7 +630,7 @@ go back to ((<Index>))
625
630
  def stddev(*dims)
626
631
  min_count = NArrayMiss.check_options(dims, 2)
627
632
  # 整数型の場合は浮動小数型へ変換
628
- ary0 = self.integer? ? self.to_type(NARRAY::DFLOAT) : self
633
+ ary0 = self.integer? ? self.to_type(@array.class.const_get(:DFLOAT)) : self
629
634
  NArrayMiss.reduction(@mask, rank, min_count, dims, true, typecode) do |count_sum, count_accum|
630
635
  ary0 = ary0 - ary0.accum(*dims)/count_accum
631
636
  ary0 = ary0.abs if ary0.complex?
@@ -636,7 +641,7 @@ go back to ((<Index>))
636
641
  def rms(*dims)
637
642
  min_count = NArrayMiss.check_options(dims, 1)
638
643
  # 整数型の場合は浮動小数型へ変換
639
- ary0 = self.integer? ? self.to_type(NARRAY::DFLOAT) : self
644
+ ary0 = self.integer? ? self.to_type(@array.class.const_get(:DFLOAT)) : self
640
645
  NArrayMiss.reduction(@mask, rank, min_count, dims, true, typecode) do |count_sum, count_accum|
641
646
  ary0 = ary0.abs if ary0.complex?
642
647
  ary0 = (ary0**2).sum(*dims) / count_sum
@@ -646,7 +651,7 @@ go back to ((<Index>))
646
651
  def rmsdev(*dims)
647
652
  min_count = NArrayMiss.check_options(dims, 1)
648
653
  # 整数型の場合は浮動小数型へ変換
649
- ary0 = self.integer? ? self.to_type(NARRAY::DFLOAT) : self
654
+ ary0 = self.integer? ? self.to_type(@array.class.const_get(:DFLOAT)) : self
650
655
  NArrayMiss.reduction(@mask, rank, min_count, dims, true, typecode) do |count_sum, count_accum|
651
656
  ary0 = ary0 - ary0.accum(*dims)/count_accum
652
657
  ary0 = ary0.abs if ary0.complex?
@@ -659,17 +664,17 @@ go back to ((<Index>))
659
664
  if arg.length==0 then
660
665
  return @array[@mask].median
661
666
  else
662
- nshape = NARRAY.to_na(@array.shape)
667
+ nshape = @array.class.to_na(@array.shape)
663
668
  nshape[arg]=1
664
669
  nslice = nshape[nshape.ne(1).where]
665
- index = NARRAY.object(@mask.rank)
670
+ index = @array.class.object(@mask.rank)
666
671
  index[nshape.eq(1).where] = true
667
672
  obj = NArrayMiss.new(@array.typecode,*nslice.to_a)
668
673
  total = 1
669
674
  nslice.each{|n| total *= n}
670
675
  for i in 0...total
671
676
  index[nshape.ne(1).where] = pos(i,nslice)
672
- mask = NARRAY.byte(*@array.shape).fill(0)
677
+ mask = @array.class.byte(*@array.shape).fill(0)
673
678
  mask[*index] = 1
674
679
  mask = @mask&mask
675
680
  if mask.count_true != 0 then
@@ -697,17 +702,17 @@ go back to ((<Index>))
697
702
  obj[@mask] = @array[@mask].#{operator}
698
703
  return obj
699
704
  else
700
- nshape = NARRAY.to_na(@array.shape)
705
+ nshape = @array.class.to_na(@array.shape)
701
706
  nshape[arg]=1
702
707
  nslice = nshape[nshape.ne(1).where]
703
- index = NARRAY.object(@mask.rank)
708
+ index = @array.class.object(@mask.rank)
704
709
  index[nshape.eq(1).where] = true
705
710
  obj = NArrayMiss.new(@array.typecode,*@array.shape)
706
711
  total = 1
707
712
  nslice.each{|n| total *= n}
708
713
  for i in 0...total
709
714
  index[nshape.ne(1).where] = pos(i,nslice)
710
- mask = NARRAY.byte(*@array.shape).fill(0)
715
+ mask = @array.class.byte(*@array.shape).fill(0)
711
716
  mask[*index] = 1
712
717
  mask = @mask&mask
713
718
  if mask.count_true != 0 then
@@ -1077,14 +1082,14 @@ go back to ((<Index>))
1077
1082
  end
1078
1083
  def set_mask(mask)
1079
1084
  if mask.class == Array then
1080
- tmp = NARRAY.byte(*@mask.shape)
1085
+ tmp = @array.class.byte(*@mask.shape)
1081
1086
  tmp[true] = mask
1082
1087
  mask = tmp
1083
1088
  end
1084
1089
  if mask.class == NArrayMiss then
1085
1090
  mask = mask.to_na(0)
1086
1091
  end
1087
- if mask.class == NARRAY then
1092
+ if mask.class == @array.class then
1088
1093
  if mask.typecode != BYTE then
1089
1094
  raise("mask must be NArrayMiss.byte, NArray.byte or Array")
1090
1095
  end
@@ -1160,7 +1165,7 @@ go back to ((<Index>))
1160
1165
  if arg.length==0 then
1161
1166
  return @mask.count_false
1162
1167
  else
1163
- return NARRAY.int(*@mask.shape).fill(1).sum(*arg)-
1168
+ return @array.class.int(*@mask.shape).fill(1).sum(*arg)-
1164
1169
  @mask.to_type(MINT).sum(*arg)
1165
1170
  end
1166
1171
  end
@@ -1201,8 +1206,8 @@ go back to ((<Index>))
1201
1206
 
1202
1207
  def coerce(x)
1203
1208
  if Numeric===x then
1204
- return [NArrayMiss.new(NARRAY[x].typecode,*self.shape).fill(x),self]
1205
- elsif x.class==Array || x.class==NARRAY then
1209
+ return [NArrayMiss.new(@array.class[x].typecode,*self.shape).fill(x),self]
1210
+ elsif x.class==Array || x.class==@array.class then
1206
1211
  return [NArrayMiss.to_nam(x), self]
1207
1212
  else
1208
1213
  raise("donnot know how to cange #{x.class} to NArrayMiss")
@@ -1217,7 +1222,7 @@ go back to ((<Index>))
1217
1222
  max_col = 80
1218
1223
  sep = ", "
1219
1224
  const = Hash.new
1220
- NARRAY.constants.each{|c| const[NARRAY.const_get(c)] = c}
1225
+ @array.class.constants.each{|c| const[@array.class.const_get(c)] = c}
1221
1226
  str_ret = "NArrayMiss."+const[typecode].to_s.downcase+"("+shape.join(",")+"):"
1222
1227
  if rank == 0 then
1223
1228
  str_ret << " []"
@@ -1291,7 +1296,7 @@ go back to ((<Index>))
1291
1296
  private
1292
1297
  def pos(n,shape)
1293
1298
  rank = shape.length
1294
- result = NARRAY.int(rank)
1299
+ result = @array.class.int(rank)
1295
1300
  m=n
1296
1301
  for i in 0..rank-2
1297
1302
  j = rank-1-i
@@ -1310,11 +1315,11 @@ go back to ((<Index>))
1310
1315
  term1 = @array
1311
1316
  term2 = arg
1312
1317
  mask = @mask
1313
- when Array, NARRAY
1318
+ when Array, @array.class
1314
1319
  term1 = @array.dup
1315
1320
  term1[@mask.not] = dummy # 欠損部分に dummy を代入
1316
- term2 = arg.kind_of?(NARRAY) ? arg : NARRAY.to_na(arg) # Array -> NArray
1317
- mask = NARRAY.byte(*term2.shape).fill(1) # 2項目は欠損無し
1321
+ term2 = arg.kind_of?(@array.class) ? arg : @array.class.to_na(arg) # Array -> NArray
1322
+ mask = @array.class.byte(*term2.shape).fill(1) # 2項目は欠損無し
1318
1323
  mask = @mask & mask
1319
1324
  when NArrayMiss
1320
1325
  term1 = @array.dup
@@ -1337,12 +1342,13 @@ go back to ((<Index>))
1337
1342
  def self.reduction(mask, rank, min_count, dims, flag, typecode)
1338
1343
  # flag: リダクションを行う次元方向の有効な値の個数で、割り算を行うかどうかのフラグ
1339
1344
  count_sum = mask.to_type(MINT).sum(*dims)
1345
+ klass = mask.class
1340
1346
  # 返り値が配列か、スカラーかによって分岐
1341
- if count_sum.kind_of?(NARRAY)
1347
+ if count_sum.kind_of?(klass)
1342
1348
  mask = count_sum.ge(min_count)
1343
1349
  # すべての要素が欠損値にならないかチェック
1344
1350
  if mask.any?
1345
- count_accum = NARRAY.ref(count_sum)
1351
+ count_accum = klass.ref(count_sum)
1346
1352
  dims.collect{|d|d<0 ? d+rank : d}.sort.each do |d|
1347
1353
  count_accum.newdim!(d)
1348
1354
  end
@@ -1355,13 +1361,13 @@ go back to ((<Index>))
1355
1361
  ary = NArrayMiss.to_nam_no_dup(ary, mask) unless flag
1356
1362
  else
1357
1363
  # すべての要素が欠損値の NArrayMiss を返す
1358
- na = NARRAY.new(typecode, *mask.shape)
1364
+ na = klass.new(typecode, *mask.shape)
1359
1365
  ary = NArrayMiss.to_nam_no_dup(na, false)
1360
1366
  end
1361
1367
  else
1362
1368
  # 有効な要素数があるかチェック
1363
1369
  if count_sum >= min_count
1364
- count_accum = NARRAY.int(*([1]*mask.rank)).fill!(count_sum)
1370
+ count_accum = klass.int(*([1]*mask.rank)).fill!(count_sum)
1365
1371
  ary = yield(count_sum, count_accum)
1366
1372
  else
1367
1373
  # 有効な要素数が足りない場合は nil を返す
@@ -1,3 +1,3 @@
1
1
  class NArrayMiss
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.1"
3
3
  end
@@ -31,5 +31,6 @@ Gem::Specification.new do |s|
31
31
 
32
32
  # specify any dependencies here; for example:
33
33
  #s.add_development_dependency "rspec"
34
- s.add_runtime_dependency "numru-narray"
34
+ s.add_runtime_dependency "narray"
35
+ #s.add_runtime_dependency "numru-narray"
35
36
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: narray_miss
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seiya Nishizawa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-15 00:00:00.000000000 Z
11
+ date: 2016-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: numru-narray
14
+ name: narray
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="