Module: Gnutemplate

Defined in:
lib/gnutemplate/version.rb,
lib/gnutemplate.rb

Defined Under Namespace

Classes: Error, SubPlot

Constant Summary collapse

VERSION =
"0.2.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.draw(subplot, file: nil, campus_size: nil, multicolumn: 1) ⇒ Object

of def draw



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/gnutemplate.rb', line 346

def draw(subplot, file: nil, campus_size: nil, multicolumn: 1) # for any plot function
	
	subplot = [subplot] if !subplot.is_a?(Array)
	
	Numo.gnuplot do

if file
	if campus_size
		set termnal: "png", size: campus_size
	else
		set terminal: "png"
	end
	set output: file
end

multiline = (subplot.length.to_f / multicolumn).ceil
set :multiplot, layout: "#{multiline},#{multicolumn}"

subplot.each do |sp|
	
	# こういうのがパターンマッチでできるの?
	sp.settings.each do |k, v|
		if v.kind_of?(Hash)
		  sargs = []
		  skwargs = {}
		  
		  v.map do |vk, vv|
          if vv.nil?
            sargs.push vk
          else
            skwargs[vk] = vv
          end
		  end
		  
		  set k, *sargs, **skwargs

		elsif v.kind_of?(Array)

		  v.each do |vi|
          if vi.kind_of?(Hash)
            sargs = []
            skwargs = {}
            vi.map do |vk, vv|
              if vv.nil?
                sargs.push vk
              else
                skwargs[vk] = vv
              end
            end
            set k, *sargs, **skwargs
          else
            set k, vi
          end
		  end
		  
		else

		  if v.nil?
			  set k
		  else
			  set k, v
		  end

		end
	end
	
	plot *(sp.args)

end # of subplot.each

unset :multiplot

  end # of Numo.gnuplot
end

.draw_box(rows, titlenames, xrange: nil, xtics: nil, title: nil, file: nil, alpha: 60, engine: :note) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/gnutemplate.rb', line 120

def draw_box(rows, titlenames, xrange: nil, xtics: nil, title: nil, file: nil, alpha: 60, engine: :note)
  
  # この色をローテーションで使えるようにすれば、何都市でも描けるね、とりあえず
  alpha_hex = (alpha * 256 / 100).to_s(16).upcase
  colors = ["##{alpha_hex}CC0000", "##{alpha_hex}00CC00", "##{alpha_hex}0000CC", "##{alpha_hex}AAAA00"]
  
  # 列データをmin, maxごとに分離する
  rows_min = (0...(rows.length)).to_a.map {|i| rows[i] if i % 2 == 0}.compact
  rows_max = (0...(rows.length)).to_a.map {|i| rows[i] if i % 2 == 1}.compact

  settings = {}
  args = []

  if !xtics.nil?
    set[:xtics] = [xtics]
    set[:xtics].push "rotate by 90"
  end

  xrange ||= (rows_min.map(&:to_a).flatten.compact.min)..(rows_max.map(&:to_a).flatten.compact.max)
  
  set[:xrange] = xrange
  set[:grid] = nil
  set[:style] = :fill_solid
  set[:title] = title if title

  ### キャンドルスティックを応用する
  # X軸, 太い所の最大値, 最大値, 最小値, 太い所の最小値
  # 今回は細い箇所は不要
  # X軸はこの場合配列にしなきゃだめ(to_a) これは癖をつけたほうが安全

  args = rows_min.zip(rows_max, titlenames).each.with_index.inject([]) do |result, ((mi, ma, t), i)|
    result += [xrange.to_a, mi.to_a, mi.to_a, ma.to_a, ma.to_a, {with: "candlesticks", fc_rgb: colors[i % 4], title: t} ]
  end

  plot *args

end

.draw_histogram(data, labels: nil, pileup: true, xmin: nil, xmax: nil, ymin: 0, ymax: nil, bins: 10, plotsize: nil, rotate_xtics: 45, fill: true, alpha: 33, background: nil, file: nil) ⇒ Object

Of def function



254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/gnutemplate.rb', line 254

def draw_histogram(data, labels: nil, pileup: true,
  xmin: nil, xmax: nil, ymin: 0, ymax: nil, bins: 10,
  plotsize: nil, rotate_xtics: 45,
  fill: true, alpha: 33, background: nil,
  file: nil)

  as = Gnutemplate.histogram(data, labels: labels, pileup: pileup,
  xmin: xmin, xmax: xmax, ymin: ymin, ymax: ymax, bins: bins,
  plotsize: plotsize, rotate_xtics: rotate_xtics,
  fill: fill, alpha: alpha, background: background)
	
  draw as, file: file

end

.draw_line(data) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/gnutemplate.rb', line 62

def draw_line(data)
  Numo.gnuplot do
    set xtics: xtics
    set yrange: 0..50
    set :grid
    plot xs, data[0],{w: :linespoints, lc_rgb: "#1199dd", pointtype: 7, pointsize: 0.7, title: 0}, 
        xs, data[1], {w: :linespoints, lc_rgb: "#99dd11", pointtype: 7, pointsize: 0.7, title: 1}
  end
end

.histogram(data, labels: nil, pileup: true, xmin: nil, xmax: nil, ymin: 0, ymax: nil, bins: 10, plotsize: nil, rotate_xtics: 45, fill: true, alpha: 33, background: nil, file: nil) ⇒ Object

ここからsettings, argsを返せるようにする set terminal部の扱いは考えなければならない(多分、_drow, _noteにif…set…end だけ残せばいい)



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/gnutemplate.rb', line 160

def histogram(data, labels: nil, pileup: true,
  xmin: nil, xmax: nil, ymin: 0, ymax: nil, bins: 10,
  plotsize: nil, rotate_xtics: 45,
  fill: true, alpha: 33, background: nil,
  file: nil)

  data = [data] if data[0].kind_of?(Numeric) || data[0].nil?

  alpha_hex = (alpha * 256 / 100).to_s(16).upcase
  colors = ["##{alpha_hex}CC0000", "##{alpha_hex}00CC00", "##{alpha_hex}0000CC", "##{alpha_hex}888800"]

  xmax ||= data.map(&:to_a).flatten.compact.max
  xmin ||= data.map(&:to_a).flatten.compact.min
  freqs = data.map {|d| d.to_a.compact.histogram(bins, min: xmin, max: xmax) }
  ymax ||= freqs.map{ _1[1] }.flatten.max * 1.1

  if pileup

    xticinterval = (xmax-xmin).to_f / bins

    settings = {}
    settings[:size] =  "#{plotsize[0]},#{plotsize[1]}" if plotsize
    settings[:style] = "fill solid" if fill

    settings[:xtics] = ["#{xmin-xticinterval}, #{xticinterval}, #{xmax+xticinterval}"]
    settings[:xtics].push("rotate by #{rotate_xtics}") if rotate_xtics

    settings[:xrange] = (xmin-xticinterval)..(xmax+xticinterval)
    settings[:yrange] = ymin..ymax

    args = if background
      ["[#{xmin}:#{xmax}] #{ymax} with filledc above y=#{ymin} fc \"##{background}\" notitle", {}]
    else
      []
    end

    freqs.each_with_index do |f, i|
      args.push f[0], f[1]

      if labels
        args.push({:with => :boxes, :title => labels[i], :fillcolor => "rgb \"#{colors[i % 4]}\""})
      else
        args.push({:with => :boxes, :fillcolor => "rgb \"#{colors[i % 4]}\""})
      end
    end

    return SubPlot.new(args, settings)

  else

    # set title:"Temperature"
    settings = {}
    settings[:auto] = "x"
    settings[:style] = [{data: nil, histogram: nil},   # set :style, :data, :histogram を表現
                        {histogram: nil, cluster: nil, gap: 1},  # set :histogram, :cluster, gap: 1 を表現
                        {fill: nil, solid: 1.0 - (alpha/100.0),  border: -1}]
    settings[:boxwidth] = 0.9
    settings[:xtic] = {rotate: nil, by: rotate_xtics, scale: 0}

    xticinterval = (xmax-xmin).to_f / bins
    settings[:xrange] = 0..((xmax-xmin) / xticinterval).to_i

    xtics = freqs[0][0]
    .each.with_index
    .inject("(") { |result, (x, i)| result += "'#{x-xticinterval/2}-#{x+xticinterval/2}' #{i}," }
    .gsub(/,$/, ")")
    settings[:xtics] = xtics

    labels ||= (0...(freqs.length)).map(&:to_s)

    args = freqs.zip(labels).each_with_index.map do |(f, l), i|
      [*f, using: 2, w: :histogram, t: labels[i], fillcolor: "rgb \"#{colors[i % 4]}\""]
    end

    return SubPlot.new(args, settings)

  end # Of if pileup..else
end

.note(subplot, file: nil, campus_size: nil, multicolumn: 1) ⇒ Object

for any plot function



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/gnutemplate.rb', line 270

def note(subplot, file: nil, campus_size: nil, multicolumn: 1) # for any plot function
	
	subplot = [subplot] if !subplot.is_a?(Array)
	
	Numo.noteplot do

if file
	if campus_size
		set termnal: "png", size: campus_size
	else
		set terminal: "png"
	end
	set output: file
end

# マルチプロット調整
multiline = (subplot.length.to_f / multicolumn).ceil
set :multiplot, layout: "#{multiline},#{multicolumn}"

# subplot一つ一つを描画
subplot.each do |sp|
	
	# こういうのがパターンマッチでできるの?
	sp.settings.each do |k, v|
		if v.kind_of?(Hash)
			sargs = []
			skwargs = {}

			v.map do |vk, vv|
				if vv.nil?
				  sargs.push vk
				else
				  skwargs[vk] = vv
				end
			end
		  
			set k, *sargs, **skwargs

		elsif v.kind_of?(Array)

			v.each do |vi|
				if vi.kind_of?(Hash)
				  sargs = []
				  skwargs = {}
				  vi.map do |vk, vv|
              if vv.nil?
                sargs.push vk
              else
                skwargs[vk] = vv
              end
				  end
				  set k, *sargs, **skwargs
				else
				  set k, vi
				end
			end
		  
		else

		  if v.nil?
			  set k
		  else
			  set k, v
		  end

		end
	end

	plot *(sp.args)
end # of subplot.each

unset :multiplot
 
	end # of Numo.noteplot
end

.note_box(rows, titlenames, xrange: nil, xtics: nil, title: nil, file: nil, alpha: 60, engine: :note) ⇒ Object

一つのデータで min, max 2列必要とするので rows = [minデータ1, maxデータ1, minデータ2, maxデータ2, …] という形式で入れてやる xrangeはx軸の範囲(数値..数値の範囲で指定)で、xticsは実際に目盛に表示するラベル(Stringなどの配列)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/gnutemplate.rb', line 80

def note_box(rows, titlenames, xrange: nil, xtics: nil, title: nil, file: nil, alpha: 60, engine: :note)
  
  # この色をローテーションで使えるようにすれば、何都市でも描けるね、とりあえず
  alpha_hex = (alpha * 256 / 100).to_s(16).upcase
  colors = ["##{alpha_hex}CC0000", "##{alpha_hex}00CC00", "##{alpha_hex}0000CC", "##{alpha_hex}AAAA00"]
  
  # 列データをmin, maxごとに分離する
  rows_min = (0...(rows.length)).to_a.map {|i| rows[i] if i % 2 == 0}.compact
  rows_max = (0...(rows.length)).to_a.map {|i| rows[i] if i % 2 == 1}.compact
  
  Numo.noteplot do
    if !file.nil?
      set terminal: "gif"
      set output: file
    end

    if !xtics.nil?
      set xtics: xtics 
      set :xtics, "rotate by 90"
    end

    xrange ||= (rows_min.map(&:to_a).flatten.compact.min)..(rows_max.map(&:to_a).flatten.compact.max)
    set xrange: xrange
    set :grid
    set style: :fill_solid
    set title: title if title

    ### キャンドルスティックを応用する
    # X軸, 太い所の最大値, 最大値, 最小値, 太い所の最小値
    # 今回は細い箇所は不要
    # X軸はこの場合配列にしなきゃだめ(to_a) これは癖をつけたほうが安全

    args = rows_min.zip(rows_max, titlenames).each.with_index.inject([]) do |result, ((mi, ma, t), i)|
      result += [xrange.to_a, mi.to_a, mi.to_a, ma.to_a, ma.to_a, {with: "candlesticks", fc_rgb: colors[i % 4], title: t} ]
    end

    plot *args
  end
end

.note_histogram(data, labels: nil, pileup: true, xmin: nil, xmax: nil, ymin: 0, ymax: nil, bins: 10, plotsize: nil, rotate_xtics: 45, fill: true, alpha: 33, background: nil, file: nil) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/gnutemplate.rb', line 239

def note_histogram(data, labels: nil, pileup: true,
  xmin: nil, xmax: nil, ymin: 0, ymax: nil, bins: 10,
  plotsize: nil, rotate_xtics: 45,
  fill: true, alpha: 33, background: nil,
  file: nil)

  as = Gnutemplate.histogram(data, labels: labels, pileup: pileup,
  xmin: xmin, xmax: xmax, ymin: ymin, ymax: ymax, bins: bins,
  plotsize: plotsize, rotate_xtics: rotate_xtics,
  fill: fill, alpha: alpha, background: background)
	
  note as, file: file
  
end

.note_line(data) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/gnutemplate.rb', line 52

def note_line(data)
  Numo.noteplot do
    set xtics: xtics
    set yrange: 0..50
    set :grid
    plot xs, data[0],{w: :linespoints, lc_rgb: "#1199dd", pointtype: 7, pointsize: 0.7, title: 0}, 
        xs, data[1], {w: :linespoints, lc_rgb: "#99dd11", pointtype: 7, pointsize: 0.7, title: 1}
  end
end

Instance Method Details

#box(rows, titlenames, xrange: nil, xtics: nil, title: nil, file: nil, alpha: 60) ⇒ Object



72
73
74
# File 'lib/gnutemplate.rb', line 72

def box(rows, titlenames, xrange: nil, xtics: nil, title: nil, file: nil, alpha: 60)

end