nysol-view 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/mautocolor.rb ADDED
@@ -0,0 +1,244 @@
1
+ #!/usr/bin/env ruby
2
+ #-*- coding: utf-8 -*-
3
+
4
+ require "rubygems"
5
+ require "nysol/mcmd"
6
+ require "set"
7
+
8
+ # ver="1.0" # 初期リリース 2016/12/12
9
+ $cmd=$0.sub(/.*\//,"")
10
+
11
+ $version=1.0
12
+ $revision="###VERSION###"
13
+
14
+ def help
15
+
16
+ STDERR.puts <<EOF
17
+ ------------------------
18
+ #{$cmd} version #{$version}
19
+ ------------------------
20
+ 概要) 値に応じた色を自動的に割り付ける
21
+
22
+ 書式1) #{$cmd} f= col= [order=alpha|descend|ascend] [transmit=] o=
23
+
24
+ f= : カラー項目名(この値に応じてカラーの値が決まる,1項目のみ指定可)。
25
+ col=category|2色HEXコード(ex. FF0000,0000FF)
26
+ categoryを指定した場合は、f=の値をカテゴリとして扱い、
27
+ アルファベット順にRGBの値を以下の順番で設定していく。
28
+ FF,80,C0,40,E0,60,A0,20,F0,70,B0,30,D0,50,90,10
29
+ また上記各値の中でRGBの組合わせをR,G,B,RG,GB,RBの順に設定する。
30
+ よって、16 x 6=96通りの色が設定される。
31
+ カテゴリの数が96を超えた場合、超えた分は000000(黒)として出力される。
32
+ FF0000,00FF00,0000FF,FFFF00,00FFFF,FF00FF,800000,008000,000080,...
33
+ 2色のHEXコードを指定した場合、f=の値を数値として扱い、
34
+ 指定した2色間のグラデーションを数値の大きさに応じて割り当てる。
35
+ FF0000,0000FFの2色を指定した場合、f=項目の最小値がFF0000で、最大値が0000FFとなる。
36
+ f=項目の値をv,最小値をmin,最大をmaxとすると、
37
+ vに対して割り当てられるR(赤)要素のカラー値は以下の通り計算される。
38
+ floor(r0+(r1-r0)*dist) ただし、dist:(v-min)/(max-min)
39
+ r0: color=で指定した色範囲開始のR要素
40
+ r1: color=で指定した色範囲終了のR要素
41
+ ex) color=FF0000,0000FFと指定していれば、r0=FF,r1=00
42
+ 与えられた値が全て同じ場合は計算不能のため、null値を出力する。
43
+ order=: col=categoryの場合、色の割り付け順序を指定する(デフォルトはalpha)
44
+ alpha: f=で指定した値をalphabet順
45
+ descend: f=で指定した値の件数が多い順
46
+ ascend: f=で指定した値の件数が少ない順
47
+ transmit= : 透過率を指定する。透過率は00からFFまでの値で、00で完全透明、FFで透明度0となる。
48
+ 色コードの後ろに追加される。
49
+ o= : 出力ファイル名
50
+
51
+ -h,--help : ヘルプの表示
52
+
53
+ カテゴリデータをカラー化する例)
54
+ $ cat color.csv
55
+ num,class
56
+ 01,B,10
57
+ 02,A,15
58
+ 03,C,11
59
+ 04,D,29
60
+ 05,B,32
61
+ 06,A,
62
+ 07,C,9
63
+ 08,D,3
64
+ 09,B,11
65
+ 10,E,22
66
+ 11,,21
67
+ 12,C,35
68
+ $ mautocolor.rb f=class color=category a=color i=color.csv o=output1.csv↩
69
+ $ cat output1.csv
70
+ num,class1,value,color
71
+ 01,B,10,00FF00
72
+ 02,A,15,FF0000
73
+ 03,C,11,0000FF
74
+ 04,D,29,FFFF00
75
+ 05,B,32,00FF00
76
+ 06,A,,FF0000
77
+ 07,C,9,0000FF
78
+ 08,D,3,FFFF00
79
+ 09,B,11,00FF00
80
+ 10,E,22,00FFFF
81
+ 11,,21,
82
+ 12,C,35,0000FF
83
+
84
+ 数値データをカラー化する例)
85
+ $ mautocolor.rb f=value color=FF0000,0000FF a=color i=color.csv o=output2.csv↩
86
+ $ cat output2.csv
87
+ num,class,value,color
88
+ 01,B,10,c70037
89
+ 02,A,15,9f005f
90
+ 03,C,11,bf003f
91
+ 04,D,29,2f00cf
92
+ 05,B,32,1700e7
93
+ 06,A,,
94
+ 07,C,9,cf002f
95
+ 08,D,3,ff0000
96
+ 09,B,11,bf003f
97
+ 10,E,22,670097
98
+ 11,,21,6f008f
99
+ 12,C,35,0000ff
100
+ EOF
101
+ exit
102
+ end
103
+ def ver()
104
+ $revision ="0" if $revision =~ /VERSION/
105
+ STDERR.puts "version #{$version} revision #{$revision}"
106
+ exit
107
+ end
108
+
109
+ class Color
110
+ def initialize(nc,ni,col,order)
111
+ @nc=nc
112
+ @ni=ni
113
+ @col=col
114
+ if @nc and @ni
115
+ if col=="category"
116
+ @type="category"
117
+ # preparing a color pallet
118
+ pallet=[]
119
+ val=["FF","80","C0","40","E0","60","A0","20","F0","70","B0","30","D0","50","90","10"]
120
+ val.each{|v|
121
+ pallet << "#{v}0000"
122
+ pallet << "00#{v}00"
123
+ pallet << "0000#{v}"
124
+ pallet << "#{v}#{v}00"
125
+ pallet << "00#{v}#{v}"
126
+ pallet << "#{v}00#{v}"
127
+ }
128
+ # read color field data and make a mapping table(data to pallet)
129
+ temp=MCMD::Mtemp.new
130
+ xxcTable=temp.file
131
+ f=""
132
+ f << "mcut f=#{nc}:ckey i=#{ni} |"
133
+ f << "mdelnull f=ckey |"
134
+ f << "mcount k=ckey a=freq |"
135
+ if order=="descend"
136
+ f << "mbest s=freq%nr,ckey from=0 size=96 o=#{xxcTable}"
137
+ elsif order=="ascend"
138
+ f << "mbest s=freq%n,ckey from=0 size=96 o=#{xxcTable}"
139
+ else
140
+ f << "mbest s=ckey from=0 size=96 o=#{xxcTable}"
141
+ end
142
+ system(f)
143
+ @cTable={}
144
+ iCSV=MCMD::Mcsvin.new("i=#{xxcTable}")
145
+ i=0
146
+ iCSV.each{|flds|
147
+ cKey=flds["ckey"]
148
+ @cTable[cKey]=pallet[i]
149
+ i+=1
150
+ }
151
+ else
152
+ @type="numeric"
153
+ ary=col.split(",")
154
+ if ary.size!=2 or ary[0].size!=6 or ary[1].size!=6
155
+ raise "col= takes two 6-digites HEX codes like FF0000,00FF00"
156
+ end
157
+ @r0=ary[0][0..1].hex
158
+ @g0=ary[0][2..3].hex
159
+ @b0=ary[0][4..5].hex
160
+ @r1=ary[1][0..1].hex
161
+ @g1=ary[1][2..3].hex
162
+ @b1=ary[1][4..5].hex
163
+
164
+ temp=MCMD::Mtemp.new
165
+ xxcTable=temp.file
166
+ f=""
167
+ f << "mcut f=#{nc}:ckey i=#{ni} |"
168
+ f << "mdelnull f=ckey |"
169
+ f << "msummary f=ckey c=min,max o=#{xxcTable}"
170
+ system(f)
171
+
172
+ # fld,min,max
173
+ # ckey,1,14
174
+ tbl=MCMD::Mtable.new("i=#{xxcTable}")
175
+ @min=tbl.cell(1)
176
+ @max=tbl.cell(2)
177
+ @min=@min.to_f if @min
178
+ @max=@max.to_f if @max
179
+ @range=@max-@min if @min and @max
180
+ end
181
+ end
182
+ end
183
+
184
+ def getRGB(val)
185
+ return nil if val==nil or val==""
186
+ if @type=="category"
187
+ rgb=@cTable[val]
188
+ rgb="000000" unless rgb
189
+ return rgb
190
+ else
191
+ if @range==0 or @min==nil or @max==nil
192
+ rgb=nil
193
+ else
194
+ val=val.to_f
195
+ distance=(val-@min)/@range
196
+ r=(@r0+(@r1-@r0)*distance).to_i.to_s(16)
197
+ g=(@g0+(@g1-@g0)*distance).to_i.to_s(16)
198
+ b=(@b0+(@b1-@b0)*distance).to_i.to_s(16)
199
+ rgb=sprintf("%2s%2s%2s",r,g,b).gsub(" ","0")
200
+ end
201
+ return rgb
202
+ end
203
+ end
204
+ end
205
+
206
+ #############
207
+ # entry point
208
+ help() if ARGV.size <= 0 or ARGV[0]=="--help"
209
+ ver() if ARGV[0]=="--version"
210
+
211
+ # ===================================================================
212
+ # パラメータ処理
213
+ args=MCMD::Margs.new(ARGV,"f=,color=,order=,transmit=,a=,i=,o=","f=,i=,a=,color=")
214
+
215
+ # mcmdのメッセージは警告とエラーのみ
216
+ ENV["KG_VerboseLevel"]="2" unless args.bool("-mcmdenv")
217
+
218
+ iFile = args. file("i=","r")
219
+ fld = args.field("f=", iFile)["names"][0]
220
+ color = args.str("color=","category")
221
+ aFld = args.str("a=")
222
+ order = args.str("order=","alpha")
223
+ transmit = args.str("transmit=")
224
+ oFile = args.file("o=","w")
225
+
226
+ color=Color.new(fld,iFile,color,order)
227
+ iCSV=MCMD::Mcsvin.new("i=#{iFile} -array")
228
+
229
+ oFlds=[]
230
+ oFlds << iCSV.names
231
+ oFlds << aFld
232
+ oCSV=MCMD::Mcsvout.new("f=#{oFlds.join(',')} o=#{oFile}")
233
+ fldNum=iCSV.names.index(fld)
234
+ iCSV.each{|flds|
235
+ colVal=flds[fldNum]
236
+ colorStr=color.getRGB(colVal)
237
+ colorStr="#{colorStr}#{transmit}" if colorStr and transmit
238
+ flds << colorStr
239
+ oCSV.write(flds)
240
+ }
241
+
242
+ # 終了メッセージ
243
+ MCMD::endLog(args.cmdline)
244
+