carray 1.1.7 → 1.1.8

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: acb090c276425cdbf6c4ec2f3a4788be1b41cb56
4
- data.tar.gz: 8b30432918e5dd2bd072b5918b26b020680e972b
3
+ metadata.gz: e750df14d398beba90298203a253b80bf9f3f8a1
4
+ data.tar.gz: 372405f4be4af7621011dc3e725aad673903d618
5
5
  SHA512:
6
- metadata.gz: c89864281f670d3d541af28e841ff5aa2300843c274d62cdb1aeb50cbe55c1a37128f2fde5b83809e3d60d50bc43b2a9041c18dfa0c6a603874e652231411001
7
- data.tar.gz: dfcc259e3337e102a6e798829b2f2438f82556a16fb2bbda4275b1acc28a50f04b9b94851775efdc1927e3a73f6aba41b9578abd51cbcd5870fc728febc3c188
6
+ metadata.gz: 2d3eaf9c183473c1ae7a65120ee54d5f32b369229b29282936b5bcbb32b482a39e7da011b8b51479ef8f9dd4f941c58571455759065bce6eb81889b061bc11c4
7
+ data.tar.gz: 5283d53bae1c583c57fbe5459505a10f4d6bc5b45fc7647863c2ca4927f18ed205a70e4ade244b39f1ac3f5f403fbc83896dfd5fedf9d9444ec9ef51db70493e
@@ -31,12 +31,17 @@ Gem::Specification::new do |s|
31
31
  Dir["ext/*/extconf.rb"].select{|f| File.exist?(f) }
32
32
  s.has_rdoc = true
33
33
  s.rdoc_options = [
34
- "--main=rdoc_main.rb",
34
+ "--main rdoc_main.rb",
35
35
  "rdoc_main.rb",
36
36
  "rdoc_ext.rb",
37
37
  "rdoc_math.rb",
38
38
  "rdoc_stat.rb",
39
- Dir.glob("lib/carray/**/*.rb")
39
+ Dir.glob("lib/carray/**/*.rb"),
40
40
  ].flatten
41
41
  s.required_ruby_version = ">= 1.8.1"
42
+ s.add_runtime_dependency 'narray', '~> 0.6.1.1'
43
+ s.add_runtime_dependency 'narray_miss', '~> 1.3'
44
+ s.add_runtime_dependency 'axlsx', '~> 2.0'
45
+ s.add_runtime_dependency 'spreadsheet', '~> 1.1'
46
+ s.add_runtime_dependency 'sqlite3', '~> 1.3'
42
47
  end
data/carray.h CHANGED
@@ -681,6 +681,7 @@ enum {
681
681
  CA_REG_NONE,
682
682
  CA_REG_ALL,
683
683
  CA_REG_ADDRESS,
684
+ CA_REG_FLATTEN,
684
685
  CA_REG_ADDRESS_COMPLEX,
685
686
  CA_REG_POINT,
686
687
  CA_REG_BLOCK,
@@ -348,7 +348,7 @@ rb_ary_flatten_for_elements (VALUE ary, int32_t elements, void *ap)
348
348
  rb_raise(rb_eRuntimeError, "invalid shape array for conversion to carray");
349
349
  }
350
350
  else {
351
- VALUE out = rb_ary_new2(elements);
351
+ VALUE out = rb_ary_new2(0);
352
352
  int len = 0;
353
353
  ary_flatten_upto_level(ary, max_level, 0, out, &len);
354
354
  if ( len != elements ) {
@@ -374,7 +374,7 @@ rb_ary_flatten_for_elements (VALUE ary, int32_t elements, void *ap)
374
374
  }
375
375
 
376
376
  if ( same_shape ) {
377
- VALUE out = rb_ary_new2(elements);
377
+ VALUE out = rb_ary_new2(0);
378
378
  int len = 0;
379
379
  ary_flatten_upto_level(ary, ca->rank-1, 0, out, &len);
380
380
 
@@ -396,7 +396,7 @@ rb_ary_flatten_for_elements (VALUE ary, int32_t elements, void *ap)
396
396
  }
397
397
 
398
398
  if ( level >= 0 ) {
399
- VALUE out = rb_ary_new2(elements);
399
+ VALUE out = rb_ary_new2(0);
400
400
  int len = 0;
401
401
  ary_flatten_upto_level(ary, level, 0, out, &len);
402
402
  if ( len != elements ) {
@@ -512,6 +512,10 @@ rb_ca_scan_index (int ca_rank, int32_t *ca_dim, int32_t ca_elements,
512
512
  info->index[0].scalar = addr;
513
513
  return;
514
514
  }
515
+ else if ( arg == Qnil ) {
516
+ info->type = CA_REG_FLATTEN;
517
+ return;
518
+ }
515
519
  else { /* ca[i..j] -> CA_REG_ADDRESS_COMPLEX */
516
520
  info->type = CA_REG_ADDRESS_COMPLEX;
517
521
  return;
@@ -1227,6 +1231,9 @@ rb_ca_fetch_method (int argc, VALUE *argv, VALUE self)
1227
1231
  case CA_REG_ADDRESS:
1228
1232
  obj = rb_ca_ref_address(self, &info);
1229
1233
  break;
1234
+ case CA_REG_FLATTEN:
1235
+ obj = rb_ca_refer_new_flatten(self);
1236
+ break;
1230
1237
  case CA_REG_POINT:
1231
1238
  obj = rb_ca_ref_point(self, &info);
1232
1239
  break;
@@ -1311,6 +1318,10 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1311
1318
  case CA_REG_ADDRESS:
1312
1319
  obj = rb_ca_store_address(self, &info, rval);
1313
1320
  break;
1321
+ case CA_REG_FLATTEN:
1322
+ self = rb_ca_refer_new_flatten(self);
1323
+ obj = rb_ca_store_all(self, rval);
1324
+ break;
1314
1325
  case CA_REG_POINT:
1315
1326
  obj = rb_ca_store_point(self, &info, rval);
1316
1327
  break;
@@ -1584,6 +1595,7 @@ rb_ca_normalize_index (VALUE self, VALUE ridx)
1584
1595
  }
1585
1596
  return rindex;
1586
1597
  case CA_REG_ADDRESS_COMPLEX:
1598
+ case CA_REG_FLATTEN:
1587
1599
  self = rb_ca_refer_new_flatten(self);
1588
1600
  return rb_ca_normalize_index(self, ridx);
1589
1601
  default:
@@ -1779,6 +1791,7 @@ Init_carray_access ()
1779
1791
  rb_define_const(rb_cObject, "CA_REG_NONE", INT2NUM(CA_REG_NONE));
1780
1792
  rb_define_const(rb_cObject, "CA_REG_ALL", INT2NUM(CA_REG_ALL));
1781
1793
  rb_define_const(rb_cObject, "CA_REG_ADDRESS", INT2NUM(CA_REG_ADDRESS));
1794
+ rb_define_const(rb_cObject, "CA_REG_FLATTEN", INT2NUM(CA_REG_FLATTEN));
1782
1795
  rb_define_const(rb_cObject, "CA_REG_ADDRESS_COMPLEX",
1783
1796
  INT2NUM(CA_REG_ADDRESS_COMPLEX));
1784
1797
  rb_define_const(rb_cObject, "CA_REG_POINT", INT2NUM(CA_REG_POINT));
@@ -0,0 +1,76 @@
1
+ require "carray"
2
+
3
+ def guess_shape (ary)
4
+ if ary.class == Array
5
+ info = {
6
+ :rank => 0,
7
+ :dim => [],
8
+ :stop_rank => 0,
9
+ }
10
+ (1..5).each do |rank|
11
+ info[:rank] = rank
12
+ guess_shape_rank(ary, info, 1)
13
+ if info[:stop_rank] != 0
14
+ return info[:dim]
15
+ end
16
+ end
17
+ raise "too deep array"
18
+ else
19
+ return [] ### rank = 0
20
+ end
21
+ end
22
+
23
+ # ok 0
24
+ # scalar => 1
25
+ # array invalid length => 2
26
+
27
+ def guess_shape_rank (ary, info, level)
28
+ len = info[:dim][level-1]
29
+ printf "len->#{len}\n"
30
+ if len
31
+ if len != ary.length
32
+ info[:stop_rank] = level-1
33
+ return 2
34
+ end
35
+ else
36
+ len = ary.length
37
+ printf "len: set #{len}\n"
38
+ info[:dim][level-1] = len
39
+ end
40
+ if level == info[:rank]
41
+ return 0
42
+ end
43
+ if level < info[:rank]
44
+ p ary
45
+ ary.each do |e|
46
+ if e.class == Array
47
+ retval = guess_shape_rank(e, info, level+1)
48
+ case retval
49
+ when 1, 2
50
+ return retval
51
+ when 0
52
+ end
53
+ else
54
+ info[:stop_rank] = level
55
+ info[:dim] = info[:dim][0..level-1]
56
+ return 1
57
+ end
58
+ end
59
+ return 0
60
+ end
61
+ return 3
62
+ end
63
+
64
+
65
+ ary = [
66
+ [[[[3],3],1]],
67
+ [[2,2]],
68
+ [[3,3,]]
69
+ ]
70
+ #ca = CArray.object(4,3,2,1).seq
71
+ #ca.elem_store([0,0,0,0],[1,2])
72
+ #ary = ca.to_a
73
+ p ary
74
+ p CArray.guess_array_shape(ary)
75
+ p guess_shape(ary)
76
+ p ary.to_ca
@@ -0,0 +1,10 @@
1
+ require "carray"
2
+
3
+ data = <<HERE_END
4
+ time,temp,humidity
5
+
6
+
7
+ HERE_END
8
+
9
+ ca = CArray.int(3,3).seq!
10
+
@@ -0,0 +1,13 @@
1
+ require "carray"
2
+
3
+ a = CArray.double(10).seq(0.1,0.1)
4
+ b = CA_DOUBLE([0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])
5
+ a1 = (a/0.1)
6
+ b1 = (b/0.1)
7
+
8
+ p a
9
+ p b
10
+ p a1
11
+ p b1
12
+ p a1.int
13
+ p b1.int
@@ -0,0 +1,3 @@
1
+ class CArray
2
+ autoload_method "zimg", "carray/graphics/zimg"
3
+ end
@@ -61,7 +61,12 @@ module CA
61
61
  begin
62
62
  Gem::Specification.each do |spec|
63
63
  if spec.name =~ /carray/
64
- autoload_dirs.push(spec.require_paths.first)
64
+ spec.require_paths.each do |path|
65
+ if path !~ /^\//
66
+ path = File.join(spec.full_gem_path, path)
67
+ end
68
+ autoload_dirs.push(path)
69
+ end
65
70
  end
66
71
  end
67
72
  rescue Gem::LoadError
@@ -318,6 +318,18 @@ class CArray
318
318
  return reshape(elements).to_ca
319
319
  end
320
320
 
321
+ # pulled
322
+
323
+ def pulled (*args)
324
+ idx = args.map{|s| s.nil? ? :% : s}
325
+ return self[*idx]
326
+ end
327
+
328
+ def pull (*args)
329
+ idx = args.map{|s| s.nil? ? :% : s}
330
+ return self[*idx].to_ca
331
+ end
332
+
321
333
  # reversed
322
334
 
323
335
  def reversed
@@ -599,7 +611,7 @@ class CArray
599
611
 
600
612
  #
601
613
  # CArray.span(data_type, range[, step])
602
- # CArray.span(range[, step]) -> data_type guessed by range.first typ
614
+ # CArray.span(range[, step]) -> data_type guessed by range.first type
603
615
  #
604
616
 
605
617
  def self.span (*argv)
@@ -608,7 +620,7 @@ class CArray
608
620
  else
609
621
  type, = *CArray.guess_type_and_bytes(argv.shift, nil)
610
622
  end
611
- range, step = argv[0], argv[1] || 1
623
+ range, step = argv[0], argv[1]
612
624
  start, stop = range.begin, range.end
613
625
  if step == 0
614
626
  raise "step should not be 0"
@@ -624,11 +636,9 @@ class CArray
624
636
  end
625
637
  end
626
638
  if type == CA_OBJECT and not step
627
- return CA_OBJECT(range.map)
639
+ return CA_OBJECT(range.to_a)
628
640
  else
629
- # start = start.to_s.to_r
630
- # stop = stop.to_s.to_r
631
- # step = step.to_s.to_r
641
+ step ||= 1
632
642
  if range.exclude_end?
633
643
  n = ((stop - start).abs/step).floor
634
644
  else
@@ -126,13 +126,11 @@ class CArray::Inspector # :nodoc:
126
126
  lambda{|x| "%i" % x }
127
127
  when CA_UINT8, CA_UINT16, CA_UINT32, CA_UINT64
128
128
  lambda{|x| "%u" % x }
129
- when CA_FLOAT32
130
- lambda{|x| "%.5g" % x }
131
- when CA_FLOAT64, CA_FLOAT128
132
- lambda{|x| "%.8g" % x }
129
+ when CA_FLOAT32, CA_FLOAT64, CA_FLOAT128
130
+ lambda{|x| x.inspect }
133
131
  when CA_CMPLX64, CA_CMPLX128, CA_CMPLX256
134
- lambda{|x| format("%.5g%s%.5gi",
135
- x.real, (x.imag >= 0) ? "+" : "-", x.imag.abs) }
132
+ lambda{|x| format("%s%s%si",
133
+ x.real.inspect, (x.imag >= 0) ? "+" : "-", x.imag.abs.inspect) }
136
134
  when CA_FIXLEN
137
135
  if @carray.data_class
138
136
  if @carray.bytes <= 6
@@ -303,7 +303,7 @@ class CA::Struct::Builder # :nodoc:
303
303
  else
304
304
  data_type, @bytes = CArray.guess_type_and_bytes(@type, @opt[:bytes])
305
305
  if data_type == CA_OBJECT
306
- raise("CA_OBJECT type can't be a member of struct or union")
306
+ raise RuntimeError, "CA_OBJECT type can't be a member of struct or union"
307
307
  end
308
308
  @byte_length = @bytes
309
309
  end
@@ -0,0 +1,296 @@
1
+ # ----------------------------------------------------------------------------
2
+ #
3
+ # carray/graphics/zimg.rb
4
+ #
5
+ # This file is part of Ruby/CArray extension library.
6
+ # You can redistribute it and/or modify it under the terms of
7
+ # the Ruby Licence.
8
+ #
9
+ # Copyright (C) 2005 Hiroki Motoyoshi
10
+ #
11
+ # ----------------------------------------------------------------------------
12
+
13
+
14
+ =begin
15
+
16
+ option
17
+
18
+ :debug => (true|false)
19
+ :output => filename
20
+ :complex => (abs|length|phase|real|imaginary)
21
+ :colormap => (red|blue|gray) | "filename" | [r,g,b]
22
+ :invert => (true|false)
23
+ :crange => [min,max]
24
+ :nda_color => color
25
+ :scale => [x[,y]]
26
+ :align => [h,[,v,bcol]]
27
+ :quality => quality
28
+ :interlace => (true|false)
29
+ :colorbox => (true|false)
30
+ :cbox_tics => n
31
+ :cbox_label => string
32
+ :cbox_format => string
33
+ :display => (true|false) | "program"
34
+ :legend => string
35
+ :label => [[x1,y1,s1], [x2,y2,s2], ...]
36
+ :vlabel => [[x1,y1,s1], [x2,y2,s2], ...]
37
+ :textfont => n
38
+ :textcolor => "xxx"|"xxxxxx"
39
+ :smooth => threshold
40
+ :contors => [levels,"log",bg,fg] ### Not Implemented
41
+
42
+ =end
43
+
44
+ class CArray
45
+
46
+ @@zimg_count = 0
47
+
48
+ def zimg (option={}) # :nodoc:
49
+
50
+ tempfile = "%zimg_#{$$}_#{@@zimg_count}.dat"
51
+ @@zimg_count += 1
52
+
53
+ if self.rank != 2
54
+ raise "zimg accept only 2-D data"
55
+ end
56
+
57
+ option.each do |k, v|
58
+ if k.kind_of?(String)
59
+ option[k.intern] = v
60
+ end
61
+ end
62
+
63
+ if option[:output]
64
+ output = option[:output]
65
+ case output
66
+ when /.ppm$/
67
+ outfmt = "--ppm"
68
+ when /.pgm$/
69
+ outfmt = "--pgm"
70
+ when /.jpg$/i, /.jpeg$/i
71
+ if option[:quality]
72
+ outfmt = "--jpeg=" + option[:quality].to_s
73
+ else
74
+ outfmt = "--jpeg"
75
+ end
76
+ else
77
+ outfmt = ""
78
+ end
79
+ else
80
+ output = "-"
81
+ outfmt = ""
82
+ end
83
+
84
+ if option[:interlace]
85
+ interlace = "--interlace"
86
+ else
87
+ interlace = ""
88
+ end
89
+
90
+ case self.data_type
91
+ when CA_UINT8
92
+ type_spec = "--unsigned-char"
93
+ when CA_INT8, CA_BOOLEAN
94
+ type_spec = "--char"
95
+ when CA_UINT16
96
+ type_spec = "--unsigned-short"
97
+ when CA_INT16
98
+ type_spec = "--short"
99
+ when CA_UINT32
100
+ type_spec = "--unsigned-int"
101
+ when CA_INT32
102
+ type_spec = "--int"
103
+ when CA_FLOAT32
104
+ type_spec = "--float"
105
+ when CA_FLOAT64
106
+ type_spec = "--double"
107
+ when CA_CMPLX64
108
+ type_spec = "--complex-float=" + ( option[:complex] || "abs" )
109
+ when CA_CMPLX128
110
+ type_spec = "--complex-double=" + ( option[:complex] || "abs" )
111
+ else
112
+ raise "can't create image for invalid numerical data"
113
+ end
114
+
115
+ case option[:colormap]
116
+ when "red", "blue", "gray"
117
+ colormap = "--" + option[:colormap]
118
+ when String
119
+ if File.exist?(option[:colormap])
120
+ colormap = "--colormap=" + option[:colormap]
121
+ else
122
+ raise "can't find colomap file"
123
+ end
124
+ when Array
125
+ colormap = "--colormap=" + option[:colormap].join(",")
126
+ else
127
+ colormap = ""
128
+ end
129
+
130
+ if option[:crange]
131
+ crange = "--crange=" + option[:crange].join(',')
132
+ else
133
+ crange = ''
134
+ end
135
+
136
+ if option[:nda_color]
137
+ if option[:fill_value]
138
+ nda = "--nda=" + [option[:fill_value], option[:nda_color]].join(',')
139
+ else
140
+ nda = "--nda=" + ["Infinity", option[:nda_color]].join(',')
141
+ end
142
+ else
143
+ nda = ""
144
+ end
145
+
146
+ if option[:scale]
147
+ scale = "--scale=" + option[:scale].join(',')
148
+ else
149
+ scale = ''
150
+ end
151
+
152
+ if option[:align]
153
+ if option[:align].last.kind_of?(String)
154
+ align = "--align=" + option[:align][0..-2].join('x')
155
+ "," + option[:align].last
156
+ else
157
+ align = "--align=" + option[:align].join('x')
158
+ end
159
+ else
160
+ align = ''
161
+ end
162
+
163
+ if option[:colorbox]
164
+ if option[:cbox_tics]
165
+ colorbox = "--colorbox=" + option[:cbox_tics].to_s
166
+ else
167
+ colorbox = "--colorbox"
168
+ end
169
+ else
170
+ colorbox = ""
171
+ end
172
+
173
+ if option[:cbox_format]
174
+ cbox_format = option[:cbox_format]
175
+ else
176
+ cbox_format = ""
177
+ end
178
+
179
+ if option[:cbox_label]
180
+ cbox_label = option[:cbox_label]
181
+ else
182
+ cbox_label = ""
183
+ end
184
+
185
+ geom = lambda{|x,y|
186
+ [ (x > 0 ? "+" : '') + x.to_s, (y > 0 ? '+' :'') + y.to_s].join
187
+ }
188
+
189
+ label = ""
190
+ if option[:label]
191
+ option[:label].each do |x, y, s|
192
+ label << "--label=" + [geom[x, y],s].join(',') + " "
193
+ end
194
+ end
195
+
196
+ vlabel = ""
197
+ if option[:vlabel]
198
+ option[:vlabel].each do |x, y, s|
199
+ vlabel << "--vlabel=" + [geom[x, y],s].join(',') + " "
200
+ end
201
+ end
202
+
203
+ if option[:textfont]
204
+ textfont = "--font=" + option[:textfont].to_s
205
+ else
206
+ textfont = ""
207
+ end
208
+
209
+ if option[:textcolor]
210
+ textcolor = "--textcolor=" + option[:textcolor].to_s
211
+ else
212
+ textcolor = ""
213
+ end
214
+
215
+ case option[:smooth]
216
+ when Numeric
217
+ smooth = "--smooth=" + option[:smooth].to_s
218
+ else
219
+ if option[:smooth]
220
+ smooth = "--smooth"
221
+ else
222
+ smooth = ""
223
+ end
224
+ end
225
+
226
+ command = [
227
+ "zimg",
228
+ "--output=#{output}",
229
+ outfmt,
230
+ interlace,
231
+ type_spec,
232
+ "--size=" + self.dim.reverse.join(","),
233
+ scale,
234
+ align,
235
+ colormap,
236
+ option[:invert] ? "--invert" : "",
237
+ crange,
238
+ nda,
239
+ colorbox,
240
+ cbox_label,
241
+ cbox_format,
242
+ label,
243
+ vlabel,
244
+ textfont,
245
+ textcolor,
246
+ smooth,
247
+ tempfile
248
+ ].join(" ")
249
+
250
+ if option[:nda_color] and self.has_mask?
251
+ if option[:fill_value]
252
+ out = self.unmask(option[:fill_value])
253
+ else
254
+ out = self.unmask(nan)
255
+ end
256
+ else
257
+ out = self
258
+ end
259
+
260
+ open(tempfile, "w"){ |io|
261
+ out.dump_binary(io)
262
+ }
263
+
264
+ if option[:debug]
265
+ puts command
266
+ end
267
+
268
+ if option[:output]
269
+ system(command)
270
+ case option[:display]
271
+ when String
272
+ system([option[:display], output].join(" "))
273
+ else
274
+ if option[:display]
275
+ system(["display", output].join(" "))
276
+ end
277
+ end
278
+ else
279
+ case option[:display]
280
+ when String
281
+ system(command + ["|", option[:display], "-"].join(" "))
282
+ else
283
+ if option[:display]
284
+ system(command + ["|", "display -"].join(" "))
285
+ end
286
+ end
287
+ end
288
+
289
+ ensure
290
+ if File.exist?(tempfile)
291
+ File.unlink(tempfile)
292
+ end
293
+ end
294
+
295
+ end
296
+