Class: GraphKit::DataKit

Inherits:
KitHash show all
Includes:
Log
Defined in:
lib/graphkit.rb,
lib/graphkit/mm.rb,
lib/graphkit/csv.rb,
lib/graphkit/gnuplot.rb,
lib/graphkit/vtk_legacy_ruby.rb

Defined Under Namespace

Classes: MathematicaOptions, TensorArray

Constant Summary collapse

AXES =
GraphKit::AXES
ALLOWED_RANKS =

ALLOWED_RANKS = [[1], [1,1], [1,1,1], [1,1,2], [1,1,1,1], [1,1,2,2], [1,1,1,3]]

[[1], [1,1], [1,1,1], [1,1,2], [2,2,2], [2,2,2,2], [1,1,1,1], [1,1,2,2], [1,1,1,3], [3,3,3,3]]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from KitHash

from_hash, #inspect

Methods included from Kit

#check, #method_missing

Methods inherited from Hash

#modify

Constructor Details

#initialize(options = {}) ⇒ DataKit

Returns a new instance of DataKit.



584
585
586
587
588
# File 'lib/graphkit.rb', line 584

def initialize(options = {})
	super()
	self[:axes] = {}
	absorb options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Kit

Class Method Details

.autocreate(hash) ⇒ Object



590
591
592
# File 'lib/graphkit.rb', line 590

def self.autocreate(hash)
	new.autocreate(hash)
end

Instance Method Details

#allowed_ranksObject



664
665
666
# File 'lib/graphkit.rb', line 664

def allowed_ranks 
	ALLOWED_RANKS
end

#autocreate(hash) ⇒ Object



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/graphkit.rb', line 594

def autocreate(hash)
	logf :autocreate
	hash.each do |key, value|
# 			puts value.inspect
		if AXES.include? key
			self[:axes][key] = AxisKit.autocreate(value)
		else 
			raise ArgumentError.new("bad key value pair in autocreate: #{key.inspect}, #{value.inspect}")
		end
# 			puts self[key].inspect
	end
# 		pp self
	first = true
	self.title = AXES.map{|axis| axes[axis] ? axes[axis].title : nil}.compact.reverse.inject("") do |str, name|
		str + name + (first ? (first = false; ' vs ') : ', ')
	end
	self.title = self.title.sub(/, $/, '').sub(/ vs $/, '')
	check_integrity
	self
end

#axes_arrayObject



581
582
583
# File 'lib/graphkit.rb', line 581

def axes_array
	self.axes.values_at(*AXES).compact
end

#check_integrityObject

Raises:



615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/graphkit.rb', line 615

def check_integrity
	logf :check_integrity
	check(['title.class', [String, NilClass]], ['with.class', [String, NilClass]],  ['axes.class', Hash])
	axes.keys.each do |key|
			check(["key #{key} from a datakit.axes", key, AXES])
		check(["self.axes[#{key.inspect}].class", AxisKit])
		self.axes[key].check_integrity
	end
# 		axes.values.map{|axiskit| axiskit.data.to_a.size}.each_with_index do |size, index|
# 			raise IntegrityError.new("Axis data sets in this datakit have different sizes than the function #{size}, #{f.shape[0]}") unless size == new_size
# 			size
# 		end
# 		puts 'checking f.class', f.class
# 		check(['f.class', CodeRunner::FunctionKit])
	
# 		shape = f.shape
	log 'checking ranks'
	rnks = ranks
	log rnks
	raise IntegrityError.new("The combination of ranks of your data cannot be plotted. Your data has a set of axes with ranks #{rnks.inspect}. (NB, rank 1 corresponds to a vector, rank 2 to a matrix and 3 to a third rank tensor). The only possible sets of types are #{allowed_ranks.inspect}") unless allowed_ranks.include? rnks
	passed = true
	case rnks
	when [1], [1,1], [1,1,1], [1,1,1,1]
		axes.values.map{|axiskit| axiskit.shape}.inject do |old, new|
# 				puts old, new
			passed = false unless new == old
			old
		end
	when [1,1,2], [1,1,2,2]
# 			passed = false unless axes[:x].shape == axes[:y].shape
		passed = false unless axes[:z].shape == [axes[:x].shape[0], axes[:y].shape[0]]
		passed = false unless axes[:z].shape == axes[:f].shape if axes[:f]
	when [1,1,1,3]
		#axes.values_at(:x, :y, :z).map{|axiskit| axiskit.shape}.inject do |old, new|
			#passed = false unless new == old
			#old
		#end
		passed = false unless axes[:f].shape == [axes[:x].shape[0], axes[:y].shape[0], axes[:z].shape[0]]
	end
	raise IntegrityError.new(%[The dimensions of this data do not match: \n#{axes.inject(""){|str, (axis, axiskit)| str + "#{axis}: #{axiskit.shape}\n"}}\nranks: #{rnks}]) unless passed 
# 		log 'finished checking ranks'
	logfc :check_integrity
# 		raise IntegrityError.new("function data must be a vector, or have the correct dimensions (or shape) for the axes: function dimensions: #{shape}; axes dimesions: #{axes_shape}") unless shape.size == 1 or axes_shape == shape
	return true
end

#convert(&block) ⇒ Object



555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/graphkit.rb', line 555

def convert(&block)

		xdat = self.axes[:x].data.to_gslv
		ydat = self.axes[:y].data.to_gslv
		xnew = GSL::Vector.alloc(xdat.size)
		ynew = GSL::Vector.alloc(xdat.size)

		for i in 0...xdat.size
			xnew[i], ynew[i] = yield(xdat[i], ydat[i])
		end
		self.axes[:x].data=xnew
		self.axes[:y].data=ynew
		#p 'dk', self
		self
	
end

#dupObject



751
752
753
754
755
756
757
758
# File 'lib/graphkit.rb', line 751

def dup
# 		puts 'Datakit.dup'
	new = self.class.new(self)
	new.axes.each do |axis, value|
		new.axes[axis] = value.dup
	end
	new
end

#exclude_outliersObject



779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
# File 'lib/graphkit.rb', line 779

def exclude_outliers
	raise "Can only get rid of outliers for 1D or 2D data" if axes.size > 2
# 		self.outliers = []
	if axes.size == 1
		data = axes[:x].data
		i = 0
		loop do 
			break if i > data.size - 2
			should_be = (data[i+1] + data[i-1]) / 2.0
			deviation = (should_be - data[i]).abs / data[i].abs
			if deviation > outlier_tolerance
				data.delete_at(i)
				i-=1
			end
			i+=1
		end
	else
		x_data = axes[:x].data
		data = axes[:y].data
		i = 0
		loop do 
			jump = 1
			loop do 
				break if i > data.size - 1 - jump
				break unless x_data[i+jump] == x_data[i-jump]
				jump += 1
			end
			break if i > data.size - 1 - jump
			should_be = data[i-jump] + (data[i+jump] - data[i-jump]) / (x_data[i+jump] - x_data[i-jump]) * (x_data[i] - x_data[i-jump]) #ie y1 + gradient * delta x
			deviation = (should_be - data[i]).abs / data[i].abs
			if deviation > outlier_tolerance
				data.delete_at(i)
				x_data.delete_at(i)
				i-=1
			end
			i+=1
		end
	end
# 		p self.outliers
end

#extend_using(other) ⇒ Object



696
697
698
699
700
701
# File 'lib/graphkit.rb', line 696

def extend_using(other)
	raise "A dataset can only be extended using another dataset with the same ranks: the ranks of this dataset are #{ranks} and the ranks of the other dataset are #{other.ranks}" unless ranks == other.ranks
	axes.each do |key, axiskit|
		axiskit.extend_using(other.axes[key])
	end
end

#gnuplot(io) ⇒ Object



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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/graphkit/gnuplot.rb', line 378

def gnuplot(io)
	axs = self.axes.values_at(*AXES).compact
	#ep 'axs', axs
	dl = data_length = axs[-1].shape.product
	dat = axs.map{|ax| ax.data}
	sh = shapes
	cml_sh = sh.map do |sh|
		cml = 1
		sh.reverse.map{|dim| cml *= dim; cml}.reverse
	end
	dat = dat.map do |d|
	 d.kind_of?(Array) ? TensorArray.new(d) : d
	end
			
	if self.errors
		raise "Errors can only be plotted for 1D or 2D data" unless ranks == [1] or ranks == [1,1]
		edat = self.errors.values_at(:x, :xmin, :xmax, :y, :ymin, :ymax).compact
		#ep 'edat', edat
	end
	case ranks
	when [1], [1,1], [1,1,1], [1,1,1,1]
		dl.times do |n| 
			dat.each{|d| io << d[n] << " "}
			io << " " << edat.map{|e| e[n].to_s}.join(" ") if self.errors
			io << "\n"
		end
	when [1,1,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[2][i,j]
				d = [dat[0][i], dat[1][j], dat[2][i,j]]
				d.each{|dt| io << dt << " "}
				io << "\n"
			end
			io << "\n" unless sh[-1][1] == 1
		end
	when [2,2,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[2][i,j]
				d = [dat[0][i,j], dat[1][i,j], dat[2][i,j]]
				d.each{|dt| io << dt << " "}
				io << "\n"
			end
			io << "\n" unless sh[-1][1] == 1
		end
	when [1,1,2,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[3][i,j]
				d = [dat[0][i], dat[1][j], dat[2][i,j], dat[3][i,j]]
				d.each{|dt| io << dt << " "}
				io << "\n"
			end
			io << "\n" unless sh[-1][1] == 1
		end
	when [1,1,1,3]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				sh[-1][2].times do |k|
					next unless dat[3][i,j,k]

					d = [dat[0][i], dat[1][j], dat[2][k], dat[3][i,j,k]]
					d.each{|dt| io << dt << " "}
					io << "\n"
				end
				io << "\n" unless sh[-1][2] == 1
			end
			io << "\n" unless sh[-1][1] == 1
		end
	when [2,2,2,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[3][i,j]
				d = [dat[0][i,j], dat[1][i,j], dat[2][i,j], dat[3][i,j]]
				d.each{|dt| io << dt << " "}
				io << "\n"
			end
			io << "\n" unless sh[-1][1] == 1
		end
	when [3,3,3,3]
					#pp dat
					#pp dat
					#pp sh
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				sh[-1][2].times do |k|
					next unless dat[3][i,j,k]
					#p [i,j,k]

					#d = [dat[0][i,j,k], dat[1][i,j,k], dat[2][i,j,k], dat[3][i,j,k]]
					io << "#{dat[0][i,j,k]} #{dat[1][i,j,k]} #{dat[2][i,j,k]} #{dat[3][i,j,k]} \n"
					#d.each{|dt| io << dt << " "}
					#io << "\n"
				end
				io << "\n" unless sh[-1][2] == 1
			end
			io << "\n" unless sh[-1][1] == 1
		end
	end

end

#gnuplot_plot_optionsObject Also known as: gp



363
364
365
# File 'lib/graphkit/gnuplot.rb', line 363

def gnuplot_plot_options
	self[:gnuplot_plot_options] ||= GnuplotPlotOptions.new
end

#mtmObject



31
32
33
# File 'lib/graphkit/mm.rb', line 31

def mtm
	self[:mtm] ||= MathematicaOptions.new
end

#plot_area_sizeObject



760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
# File 'lib/graphkit.rb', line 760

def plot_area_size
	ans = []
# 		p data
	(axes.values_at(*AXES).compact).each do |axiskit|
		if range = axiskit.range
			ans.push range
			next
		else
# 				p 'hello'
# 				p data[0].axes[key]
			axdata = axiskit.data #(key == :f) ? data[0].f.data : (next unless data[0].axes[key]; data[0].axes[key].data)
			next unless axdata
			#p 'axdatamin', axdata.min
			ans.push [axdata.min, axdata.max]
		end
	end
	ans
end

#rank_c_switchObject



682
683
684
685
686
687
688
# File 'lib/graphkit.rb', line 682

def rank_c_switch
	#i = -1
	#puts ALLOWED_RANKS.map{|r| i+=1;"#{i} --> #{r}"}
	 switch = ALLOWED_RANKS.index(ranks)
	switch
	
end

#ranksObject



689
690
691
692
693
694
# File 'lib/graphkit.rb', line 689

def ranks
	logf :ranks
	ans = shapes.map{|shape| shape.size}
	logfc :ranks
	return ans
end

#shapesObject

end



675
676
677
678
679
680
# File 'lib/graphkit.rb', line 675

def shapes
	logf :shapes
	ans = axes.values_at(*AXES).compact.inject([]){|arr, axis| arr.push axis.shape}
	logfc :shapes
	return ans
end

#to_csv(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/graphkit/csv.rb', line 37

def to_csv(options={})
	io = options[:io] || StringIO.new
	axs = self.axes.values_at(*AXES).compact
	#ep 'axs', axs
	dl = data_length = axs[-1].shape.product
	dat = axs.map{|ax| ax.data}
	sh = shapes
	cml_sh = sh.map do |sh|
		cml = 1
		sh.reverse.map{|dim| cml *= dim; cml}.reverse
	end
	dat = dat.map do |d|
	 d.kind_of?(Array) ? TensorArray.new(d) : d
	end
			
	if self.errors
		raise "Errors can only be plotted for 1D or 2D data" unless ranks == [1] or ranks == [1,1]
		edat = self.errors.values_at(:x, :xmin, :xmax, :y, :ymin, :ymax).compact
		#ep 'edat', edat
	end
	case ranks
	when [1], [1,1], [1,1,1], [1,1,1,1]
		dl.times do |n| 
			dat.each{|d| io << d[n] << " "}
			io << " " << edat.map{|e| e[n].to_s}.join(", ") if self.errors
			io << "\n"
		end
	when [1,1,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[2][i,j]
				d = [dat[0][i], dat[1][j], dat[2][i,j]]
				io << d[0] << ", " << d[1] << ", " << d[2] << "\n"
			end
			io << "\n" unless sh[-1][1] == 1
		end
	when [2,2,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[2][i,j]
				d = [dat[0][i,j], dat[1][i,j], dat[2][i,j]]
				io << d[0] << ", " << d[1] << ", " << d[2] << "\n"
			end
			io << "\n" unless sh[-1][1] == 1
		end
	when [1,1,2,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[3][i,j]
				d = [dat[0][i], dat[1][j], dat[2][i,j], dat[3][i,j]]
				io << d[0] << ", " << d[1] << ", " << d[2] << ", " << d[3] << "\n"
			end
			io << "\n" unless sh[-1][1] == 1
		end
	when [1,1,1,3]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				sh[-1][2].times do |k|
					next unless dat[3][i,j,k]

					d = [dat[0][i], dat[1][j], dat[2][k], dat[3][i,j,k]]
					io << d[0] << ", " << d[1] << ", " << d[2] << ", " << d[3] << "\n"
				end
				io << "\n" unless sh[-1][2] == 1
			end
			io << "\n" unless sh[-1][1] == 1
		end
	when [2,2,2,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[3][i,j]
				d = [dat[0][i,j], dat[1][i,j], dat[2][i,j], dat[3][i,j]]
				d.each{|dt| io << dt << " "}
				io << "\n"
			end
			io << "\n" unless sh[-1][1] == 1
		end
	when [3,3,3,3]
					#pp dat
					#pp dat
					#pp sh
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				sh[-1][2].times do |k|
					next unless dat[3][i,j,k]
					#p [i,j,k]

					#d = [dat[0][i,j,k], dat[1][i,j,k], dat[2][i,j,k], dat[3][i,j,k]]
					io << "#{dat[0][i,j,k]},#{dat[1][i,j,k]},#{dat[2][i,j,k]},#{dat[3][i,j,k]}\n"
					#d.each{|dt| io << dt << " "}
					#io << "\n"
				end
				io << "\n" unless sh[-1][2] == 1
			end
			io << "\n" unless sh[-1][1] == 1
		end
	end

return stringio.string unless options[:io]
end

#to_mathematicaObject



34
35
36
37
38
39
40
41
# File 'lib/graphkit/mm.rb', line 34

def to_mathematica
	case ranks
	when [1], [1,1], [1, 1, 1]
		"{#{(axes.values.map{|ax| ax.data.to_a}.transpose.map do |datapoint|
				"{#{datapoint.map{|coord| coord.to_s}.join(',')}}"
		end).join(',')}}"
	end
end

#vtk_legacy_cell_types(options = {}) ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/graphkit/vtk_legacy_ruby.rb', line 422

def vtk_legacy_cell_types(options={})
	pointcount = options[:pointcount]
	io = options[:io] || StringIO.new
	axs = self.axes.values_at(*AXES).compact
	#ep 'axs', axs
	dl = data_length = axs[-1].shape.product
	dat = axs.map{|ax| ax.data}
	sh = shapes
	cml_sh = sh.map do |sh|
		cml = 1
		sh.reverse.map{|dim| cml *= dim; cml}.reverse
	end
	dat = dat.map do |d|
	 d.kind_of?(Array) ? TensorArray.new(d) : d
	end
			
	if self.errors
		raise "Errors can only be plotted for 1D or 2D data" unless ranks == [1] or ranks == [1,1]
		edat = self.errors.values_at(:x, :xmin, :xmax, :y, :ymin, :ymax).compact
		#ep 'edat', edat
	end
	case ranks
	when [1], [1,1], [1,1,1], [1,1,1,1]
		(dl-1).times do |n| 
			io << "3" # Lines
			io << "\n"
		end
	#when [1,1,2]
		#sh[-1][0].times do |i|
			#sh[-1][1].times do |j|
				#next unless dat[2][i,j]
				#d = [dat[0][i], dat[1][j], dat[2][i,j]]
				#io << d[0] << ", " << d[1] << ", " << d[2] << "\n"
			#end
			#io << "\n" unless sh[-1][1] == 1
		#end
	#when [2,2,2]
		#sh[-1][0].times do |i|
			#sh[-1][1].times do |j|
				#next unless dat[2][i,j]
				#d = [dat[0][i,j], dat[1][i,j], dat[2][i,j]]
				#io << d[0] << ", " << d[1] << ", " << d[2] << "\n"
			#end
			#io << "\n" unless sh[-1][1] == 1
		#end
	#when [1,1,2,2]
		#sh[-1][0].times do |i|
			#sh[-1][1].times do |j|
				#next unless dat[3][i,j]
				#d = [dat[0][i], dat[1][j], dat[2][i,j], dat[3][i,j]]
				#io << d[0] << ", " << d[1] << ", " << d[2] << ", " << d[3] << "\n"
			#end
			#io << "\n" unless sh[-1][1] == 1
		#end
	#when [1,1,1,3]
		#sh[-1][0].times do |i|
			#sh[-1][1].times do |j|
				#sh[-1][2].times do |k|
					#next unless dat[3][i,j,k]

					#d = [dat[0][i], dat[1][j], dat[2][k], dat[3][i,j,k]]
					#io << d[0] << ", " << d[1] << ", " << d[2] << ", " << d[3] << "\n"
				#end
				#io << "\n" unless sh[-1][2] == 1
			#end
			#io << "\n" unless sh[-1][1] == 1
		#end
	when [1,1,2], [2,2,2], [1,1,2,2], [2,2,2,2]
		ni, nj = sh[-1]
		type = case [ni > 1 ? 1 : nil, nj > 1 ? 1 : nil].compact.size
					 when 2
						 7 # Polygons see www.vtk.org/VTK/img/file-formats.pdf
					 when 1
						 3 # Lines
					 when 0
						 1 # Verteces
					 end
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[3][i,j]
					next unless (ni==1 or (i+1) < ni) and (nj==1 or (j+1) < nj)
					io << type << "\n"
			end
			#io << "\n" unless sh[-1][1] == 1
		end
	when [1,1,1,3],[3,3,3,3]
					#pp dat
					#pp dat
					#pp sh
		ni, nj, nk = sh[-1]
		type = case [ni > 1 ? 1 : nil, nj > 1 ? 1 : nil, nk > 1 ? 1 : nil].compact.size
					 when 3
						 11 # 3D cells, see www.vtk.org/VTK/img/file-formats.pdf
					 when 2
						 7 # Polygons
					 when 1
						 3 # Lines
					 when 0
						 1 # Verteces
					 end

		#ep ni, nj, nk; gets
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				sh[-1][2].times do |k|
					next unless dat[3][i,j,k]
					#p [i,j,k]

					#d = [dat[0][i,j,k], dat[1][i,j,k], dat[2][i,j,k], dat[3][i,j,k]]
					#io << "#{dat[0][i,j,k]} #{dat[1][i,j,k]} #{dat[2][i,j,k]} #{dat[3][i,j,k]}\n"
					
					next unless (ni==1 or (i+1) < ni) and (nj==1 or (j+1) < nj) and (nk==1 or (k+1) < nk)
					io << type << "\n"
					 	
					#d.each{|dt| io << dt << " "}
					#io << "\n"
				end
				#io << "\n" unless sh[-1][2] == 1
			end
			#io << "\n" unless sh[-1][1] == 1
		end
	end

return io.string unless options[:io]
end

#vtk_legacy_cells(options = {}) ⇒ Object



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
345
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
420
421
# File 'lib/graphkit/vtk_legacy_ruby.rb', line 295

def vtk_legacy_cells(options={})
	pointcount = options[:pointcount]
	io = options[:io] || StringIO.new
	axs = self.axes.values_at(*AXES).compact
	#ep 'axs', axs
	dl = data_length = axs[-1].shape.product
	dat = axs.map{|ax| ax.data}
	sh = shapes
	cml_sh = sh.map do |sh|
		cml = 1
		sh.reverse.map{|dim| cml *= dim; cml}.reverse
	end
	dat = dat.map do |d|
	 d.kind_of?(Array) ? TensorArray.new(d) : d
	end
			
	if self.errors
		raise "Errors can only be plotted for 1D or 2D data" unless ranks == [1] or ranks == [1,1]
		edat = self.errors.values_at(:x, :xmin, :xmax, :y, :ymin, :ymax).compact
		#ep 'edat', edat
	end
	case ranks
	when [1], [1,1], [1,1,1], [1,1,1,1]
		(dl-1).times do |n| 
			#dat.each{|d| io << d[n] << " "}
			#io << " " << edat.map{|e| e[n].to_s}.join(" ") if self.errors
			next unless dat[-1][n]
			io << "2 #{n + pointcount} #{n + pointcount + 1}" 
			io << "\n"
		end
	#when [1,1,2]
		#sh[-1][0].times do |i|
			#sh[-1][1].times do |j|
				#next unless dat[2][i,j]
				#d = [dat[0][i], dat[1][j], dat[2][i,j]]
				#io << d[0] << ", " << d[1] << ", " << d[2] << "\n"
			#end
			#io << "\n" unless sh[-1][1] == 1
		#end
	#when [2,2,2]
		#sh[-1][0].times do |i|
			#sh[-1][1].times do |j|
				#next unless dat[2][i,j]
				#d = [dat[0][i,j], dat[1][i,j], dat[2][i,j]]
				#io << d[0] << ", " << d[1] << ", " << d[2] << "\n"
			#end
			#io << "\n" unless sh[-1][1] == 1
		#end
	#when [1,1,2,2]
		#sh[-1][0].times do |i|
			#sh[-1][1].times do |j|
				#next unless dat[3][i,j]
				#d = [dat[0][i], dat[1][j], dat[2][i,j], dat[3][i,j]]
				#io << d[0] << ", " << d[1] << ", " << d[2] << ", " << d[3] << "\n"
			#end
			#io << "\n" unless sh[-1][1] == 1
		#end
	#when [1,1,1,3]
		#ni, nj, nk = sh[-1]
		#sh[-1][0].times do |i|
			#sh[-1][1].times do |j|
				#sh[-1][2].times do |k|
					#next unless dat[3][i,j,k]

					#d = [dat[0][i], dat[1][j], dat[2][k], dat[3][i,j,k]]
					#io << d[0] << ", " << d[1] << ", " << d[2] << ", " << d[3] << "\n"
				#end
				#io << "\n" unless sh[-1][2] == 1
			#end
			#io << "\n" unless sh[-1][1] == 1
		#end
	when [1,1,2], [2,2,2], [1,1,2,2], [2,2,2,2]
		ni, nj = sh[-1]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
					next unless (ni==1 or (i+1) < ni) and (nj==1 or (j+1) < nj)
					cell = [
						i*(nj) + j , 
						nj > 1 ? i*(nj) + (j+1) : nil, 
						ni > 1 ? (i+1)*(nj) + j : nil, 
						ni > 1 && nj > 1 ? (i+1)*(nj) + (j+1) : nil, 
					].compact.map{|pnt| pnt+pointcount}
					cell = [cell[0],cell[1],cell[3],cell[2]] if cell.size == 4
					io << cell.size << " " << cell.join(" ") << "\n"
			end
			#io << "\n" unless sh[-1][1] == 1
		end
	when [1,1,1,3],[3,3,3,3]
					#pp dat
					#pp dat
					#pp sh
		ni, nj, nk = sh[-1]
		#ep ni, nj, nk; gets
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				sh[-1][2].times do |k|
					next unless dat[3][i,j,k]
					#p [i,j,k]

					#d = [dat[0][i,j,k], dat[1][i,j,k], dat[2][i,j,k], dat[3][i,j,k]]
					#io << "#{dat[0][i,j,k]} #{dat[1][i,j,k]} #{dat[2][i,j,k]} #{dat[3][i,j,k]}\n"
					
					next unless (ni==1 or (i+1) < ni) and (nj==1 or (j+1) < nj) and (nk==1 or (k+1) < nk)
					cell = [
						i*(nj*nk) + j*nk + k, 
						nk > 1 ? i*(nj*nk) + j*nk + (k+1) : nil, 
						nj > 1 ? i*(nj*nk) + (j+1)*nk + (k) : nil, 
						nj > 1  && nk > 1 ? i*(nj*nk) + (j+1)*nk + (k+1) : nil, 
						ni > 1 ? (i+1)*(nj*nk) + j*nk + k : nil, 
						ni > 1 && nk > 1 ? (i+1)*(nj*nk) + j*nk + (k+1) : nil, 
						ni > 1 && nj > 1 ? (i+1)*(nj*nk) + (j+1)*nk + (k) : nil, 
						ni > 1 && nj > 1  && nk > 1 ? (i+1)*(nj*nk) + (j+1)*nk + (k+1) : nil, 
					].compact.map{|pnt| pnt+pointcount}
					cell = [cell[0],cell[1],cell[3],cell[2]] if cell.size == 4
					io << cell.size << " " << cell.join(" ") << "\n"
					 	
					#d.each{|dt| io << dt << " "}
					#io << "\n"
				end
				#io << "\n" unless sh[-1][2] == 1
			end
			#io << "\n" unless sh[-1][1] == 1
		end
	end

return io.string unless options[:io]
end

#vtk_legacy_point_data(options = {}) ⇒ Object



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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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
# File 'lib/graphkit/vtk_legacy_ruby.rb', line 196

def vtk_legacy_point_data(options={})
	io = options[:io] || StringIO.new
	axs = self.axes.values_at(*AXES).compact
	#ep 'axs', axs
	dl = data_length = axs[-1].shape.product
	dat = axs.map{|ax| ax.data}
	sh = shapes
	cml_sh = sh.map do |sh|
		cml = 1
		sh.reverse.map{|dim| cml *= dim; cml}.reverse
	end
	dat = dat.map do |d|
	 d.kind_of?(Array) ? TensorArray.new(d) : d
	end
			
	if self.errors
		raise "Errors can only be plotted for 1D or 2D data" unless ranks == [1] or ranks == [1,1]
		edat = self.errors.values_at(:x, :xmin, :xmax, :y, :ymin, :ymax).compact
		#ep 'edat', edat
	end
	case ranks
	when [1], [1,1], [1,1,1], [1,1,1,1]
		dl.times do |n| 
			next unless dat[-1][n]
			#dat.each{|d| io << d[n] << " "}
			#io << " " << edat.map{|e| e[n].to_s}.join(" ") if self.errors
			io << "#{dat[3] ? dat[3][n] : 0}\n"
		end
	#when [1,1,2]
		#sh[-1][0].times do |i|
			#sh[-1][1].times do |j|
				#next unless dat[2][i,j]
				#d = [dat[0][i], dat[1][j], dat[2][i,j]]
				#io << d[0] << ", " << d[1] << ", " << d[2] << "\n"
			#end
			##io << "\n" unless sh[-1][1] == 1
		#end
	when [1,1,2], [2,2,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[2][i,j]
				#d = [dat[0][i,j], dat[1][i,j], dat[2][i,j]]
				#io << d[0] << ", " << d[1] << ", " << d[2] << "\n"
				io << "0\n"
			end
			#io << "\n" unless sh[-1][1] == 1
		end
	when [1,1,2,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[3][i,j]
				#d = [dat[0][i], dat[1][j], dat[2][i,j], dat[3][i,j]]
				io <<  dat[3][i,j] << "\n"
			end
			#io << "\n" unless sh[-1][1] == 1
		end
	when [1,1,1,3]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				sh[-1][2].times do |k|
					next unless dat[3][i,j,k]

					#d = [dat[0][i], dat[1][j], dat[2][k], dat[3][i,j,k]]
					io <<  dat[3][i,j,k] << "\n"
				end
				#io << "\n" unless sh[-1][2] == 1
			end
			#io << "\n" unless sh[-1][1] == 1
		end
	when [2,2,2,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[3][i,j]
					io << "#{dat[3][i,j]}\n"
			end
		end
	when [3,3,3,3]
					#pp dat
					#pp dat
					#pp sh
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				sh[-1][2].times do |k|
					next unless dat[3][i,j,k]
					#p [i,j,k]

					#d = [dat[0][i,j,k], dat[1][i,j,k], dat[2][i,j,k], dat[3][i,j,k]]
					io << "#{dat[3][i,j,k]}\n"
					#d.each{|dt| io << dt << " "}
					#io << "\n"
				end
				#io << "\n" unless sh[-1][2] == 1
			end
			#io << "\n" unless sh[-1][1] == 1
		end
	end

return io.string unless options[:io]
end

#vtk_legacy_points(options = {}) ⇒ Object



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
119
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
157
158
159
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
# File 'lib/graphkit/vtk_legacy_ruby.rb', line 87

def vtk_legacy_points(options={})
	io = options[:io] || StringIO.new
	axs = self.axes.values_at(*AXES).compact
	#ep 'axs', axs
	dl = data_length = axs[-1].shape.product
	dat = axs.map{|ax| ax.data}
	sh = shapes
	cml_sh = sh.map do |sh|
		cml = 1
		sh.reverse.map{|dim| cml *= dim; cml}.reverse
	end
	dat = dat.map do |d|
	 d.kind_of?(Array) ? TensorArray.new(d) : d
	end
			
	if self.errors
		raise "Errors can only be plotted for 1D or 2D data" unless ranks == [1] or ranks == [1,1]
		edat = self.errors.values_at(:x, :xmin, :xmax, :y, :ymin, :ymax).compact
		#ep 'edat', edat
	end
	case ranks
	when [1]
		dl.times do |n| 
			next unless dat[-1][n]
			dat.times.each{|idx| io << "0 " << idx << " #{dat[0][n]}"}
			io << "\n"
		end
	when [1,1]
		dl.times do |n| 
			next unless dat[-1][n]
			io << "0 " << dat[0][n] << ' ' << dat[1][n]
			#io << " " << edat.map{|e| e[n].to_s}.join(" ") if self.errors
			io << "\n"
		end
	when [1,1,1],[1,1,1,1]
		dl.times do |n| 
			next unless dat[-1][n]
			io << dat[0][n] << ' ' << dat[1][n] << ' ' << dat[2][n]
			io << "\n"
		end
	when [1,1,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[2][i,j]
				d = [dat[0][i], dat[1][j], dat[2][i,j]]
				io << d[0] << " " << d[1] << " " << d[2] << "\n"
			end
			#io << "\n" unless sh[-1][1] == 1
		end
	when [2,2,2], [2,2,2,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[2][i,j]
				d = [dat[0][i,j], dat[1][i,j], dat[2][i,j]]
				io << d[0] << " " << d[1] << " " << d[2] << "\n"
			end
			#io << "\n" unless sh[-1][1] == 1
		end
	when [1,1,2,2]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				next unless dat[3][i,j]
				d = [dat[0][i], dat[1][j], dat[2][i,j], dat[3][i,j]]
				io << d[0] << " " << d[1] << " " << d[2] << "\n"
			end
			#io << "\n" unless sh[-1][1] == 1
		end
	when [1,1,1,3]
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				sh[-1][2].times do |k|
					next unless dat[3][i,j,k]

					d = [dat[0][i], dat[1][j], dat[2][k], dat[3][i,j,k]]
					io << d[0] << " " << d[1] << " " << d[2] << "\n"
				end
			end
		end
	#when [2,2,2,2]
		#sh[-1][0].times do |i|
			#sh[-1][1].times do |j|
				#next unless dat[3][i,j]
				##d = [dat[0][i,j], dat[1][i,j], dat[2][i,j], dat[3][i,j]]
					#io << "#{dat[0][i,j]} #{dat[1][i,j]} #{dat[2][i,j]}\n"
				##d.each{|dt| io << dt << " "}
				##io << "\n"
			#end
		#end
	when [3,3,3,3]
					#pp dat
					#pp dat
					#pp sh
		sh[-1][0].times do |i|
			sh[-1][1].times do |j|
				sh[-1][2].times do |k|
					next unless dat[3][i,j,k]
					#p [i,j,k]

					#d = [dat[0][i,j,k], dat[1][i,j,k], dat[2][i,j,k], dat[3][i,j,k]]
					io << "#{dat[0][i,j,k]} #{dat[1][i,j,k]} #{dat[2][i,j,k]}\n"
					#d.each{|dt| io << dt << " "}
					#io << "\n"
				end
			end
		end
	end

return io.string unless options[:io]
end

#vtk_legacy_sizeObject



83
84
85
86
# File 'lib/graphkit/vtk_legacy_ruby.rb', line 83

def vtk_legacy_size
	axs = self.axes.values_at(*AXES).compact
	return axs[-1].shape.product
end