nysol-take 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +7 -0
  2. data/bin/mbiclique.rb +317 -0
  3. data/bin/mbipolish.rb +362 -0
  4. data/bin/mccomp.rb +235 -0
  5. data/bin/mclique.rb +295 -0
  6. data/bin/mclique2g.rb +105 -0
  7. data/bin/mcliqueInfo.rb +203 -0
  8. data/bin/mfriends.rb +202 -0
  9. data/bin/mgdiff.rb +252 -0
  10. data/bin/mhifriend.rb +456 -0
  11. data/bin/mhipolish.rb +465 -0
  12. data/bin/mitemset.rb +168 -0
  13. data/bin/mpal.rb +410 -0
  14. data/bin/mpolishing.rb +399 -0
  15. data/bin/msequence.rb +165 -0
  16. data/bin/mtra2g.rb +476 -0
  17. data/bin/mtra2gc.rb +360 -0
  18. data/ext/grhfilrun/extconf.rb +12 -0
  19. data/ext/grhfilrun/grhfilrun.c +85 -0
  20. data/ext/grhfilrun/src/_sspc.c +358 -0
  21. data/ext/grhfilrun/src/aheap.c +545 -0
  22. data/ext/grhfilrun/src/aheap.h +251 -0
  23. data/ext/grhfilrun/src/base.c +92 -0
  24. data/ext/grhfilrun/src/base.h +59 -0
  25. data/ext/grhfilrun/src/fstar.c +497 -0
  26. data/ext/grhfilrun/src/fstar.h +80 -0
  27. data/ext/grhfilrun/src/grhfil.c +214 -0
  28. data/ext/grhfilrun/src/itemset.c +713 -0
  29. data/ext/grhfilrun/src/itemset.h +170 -0
  30. data/ext/grhfilrun/src/problem.c +415 -0
  31. data/ext/grhfilrun/src/problem.h +179 -0
  32. data/ext/grhfilrun/src/queue.c +533 -0
  33. data/ext/grhfilrun/src/queue.h +182 -0
  34. data/ext/grhfilrun/src/sample.c +19 -0
  35. data/ext/grhfilrun/src/sspc.c +597 -0
  36. data/ext/grhfilrun/src/sspc2.c +491 -0
  37. data/ext/grhfilrun/src/stdlib2.c +1482 -0
  38. data/ext/grhfilrun/src/stdlib2.h +892 -0
  39. data/ext/grhfilrun/src/trsact.c +817 -0
  40. data/ext/grhfilrun/src/trsact.h +160 -0
  41. data/ext/grhfilrun/src/vec.c +745 -0
  42. data/ext/grhfilrun/src/vec.h +172 -0
  43. data/ext/lcmrun/extconf.rb +20 -0
  44. data/ext/lcmrun/lcmrun.cpp +99 -0
  45. data/ext/lcmrun/src/aheap.c +216 -0
  46. data/ext/lcmrun/src/aheap.h +111 -0
  47. data/ext/lcmrun/src/base.c +92 -0
  48. data/ext/lcmrun/src/base.h +59 -0
  49. data/ext/lcmrun/src/itemset.c +496 -0
  50. data/ext/lcmrun/src/itemset.h +157 -0
  51. data/ext/lcmrun/src/lcm.c +427 -0
  52. data/ext/lcmrun/src/problem.c +349 -0
  53. data/ext/lcmrun/src/problem.h +177 -0
  54. data/ext/lcmrun/src/queue.c +528 -0
  55. data/ext/lcmrun/src/queue.h +176 -0
  56. data/ext/lcmrun/src/sgraph.c +359 -0
  57. data/ext/lcmrun/src/sgraph.h +173 -0
  58. data/ext/lcmrun/src/stdlib2.c +1282 -0
  59. data/ext/lcmrun/src/stdlib2.h +823 -0
  60. data/ext/lcmrun/src/trsact.c +747 -0
  61. data/ext/lcmrun/src/trsact.h +159 -0
  62. data/ext/lcmrun/src/vec.c +731 -0
  63. data/ext/lcmrun/src/vec.h +171 -0
  64. data/ext/lcmseq0run/extconf.rb +20 -0
  65. data/ext/lcmseq0run/lcmseq0run.cpp +59 -0
  66. data/ext/lcmseq0run/src/aheap.c +216 -0
  67. data/ext/lcmseq0run/src/aheap.h +111 -0
  68. data/ext/lcmseq0run/src/base.c +92 -0
  69. data/ext/lcmseq0run/src/base.h +59 -0
  70. data/ext/lcmseq0run/src/itemset.c +518 -0
  71. data/ext/lcmseq0run/src/itemset.h +157 -0
  72. data/ext/lcmseq0run/src/itemset_zero.c +522 -0
  73. data/ext/lcmseq0run/src/lcm_seq.c +446 -0
  74. data/ext/lcmseq0run/src/lcm_seq_zero.c +446 -0
  75. data/ext/lcmseq0run/src/problem.c +439 -0
  76. data/ext/lcmseq0run/src/problem.h +179 -0
  77. data/ext/lcmseq0run/src/problem_zero.c +439 -0
  78. data/ext/lcmseq0run/src/queue.c +533 -0
  79. data/ext/lcmseq0run/src/queue.h +182 -0
  80. data/ext/lcmseq0run/src/stdlib2.c +1350 -0
  81. data/ext/lcmseq0run/src/stdlib2.h +864 -0
  82. data/ext/lcmseq0run/src/trsact.c +747 -0
  83. data/ext/lcmseq0run/src/trsact.h +159 -0
  84. data/ext/lcmseq0run/src/vec.c +779 -0
  85. data/ext/lcmseq0run/src/vec.h +172 -0
  86. data/ext/lcmseqrun/extconf.rb +20 -0
  87. data/ext/lcmseqrun/lcmseqrun.cpp +101 -0
  88. data/ext/lcmseqrun/src/aheap.c +216 -0
  89. data/ext/lcmseqrun/src/aheap.h +111 -0
  90. data/ext/lcmseqrun/src/base.c +92 -0
  91. data/ext/lcmseqrun/src/base.h +59 -0
  92. data/ext/lcmseqrun/src/itemset.c +518 -0
  93. data/ext/lcmseqrun/src/itemset.h +157 -0
  94. data/ext/lcmseqrun/src/itemset_zero.c +522 -0
  95. data/ext/lcmseqrun/src/lcm_seq.c +447 -0
  96. data/ext/lcmseqrun/src/lcm_seq_zero.c +446 -0
  97. data/ext/lcmseqrun/src/problem.c +439 -0
  98. data/ext/lcmseqrun/src/problem.h +179 -0
  99. data/ext/lcmseqrun/src/problem_zero.c +439 -0
  100. data/ext/lcmseqrun/src/queue.c +533 -0
  101. data/ext/lcmseqrun/src/queue.h +182 -0
  102. data/ext/lcmseqrun/src/stdlib2.c +1350 -0
  103. data/ext/lcmseqrun/src/stdlib2.h +864 -0
  104. data/ext/lcmseqrun/src/trsact.c +747 -0
  105. data/ext/lcmseqrun/src/trsact.h +159 -0
  106. data/ext/lcmseqrun/src/vec.c +779 -0
  107. data/ext/lcmseqrun/src/vec.h +172 -0
  108. data/ext/lcmtransrun/extconf.rb +18 -0
  109. data/ext/lcmtransrun/lcmtransrun.cpp +264 -0
  110. data/ext/macerun/extconf.rb +20 -0
  111. data/ext/macerun/macerun.cpp +57 -0
  112. data/ext/macerun/src/aheap.c +217 -0
  113. data/ext/macerun/src/aheap.h +112 -0
  114. data/ext/macerun/src/itemset.c +491 -0
  115. data/ext/macerun/src/itemset.h +158 -0
  116. data/ext/macerun/src/mace.c +503 -0
  117. data/ext/macerun/src/problem.c +346 -0
  118. data/ext/macerun/src/problem.h +174 -0
  119. data/ext/macerun/src/queue.c +529 -0
  120. data/ext/macerun/src/queue.h +177 -0
  121. data/ext/macerun/src/sgraph.c +360 -0
  122. data/ext/macerun/src/sgraph.h +174 -0
  123. data/ext/macerun/src/stdlib2.c +993 -0
  124. data/ext/macerun/src/stdlib2.h +811 -0
  125. data/ext/macerun/src/vec.c +634 -0
  126. data/ext/macerun/src/vec.h +170 -0
  127. data/ext/sspcrun/extconf.rb +20 -0
  128. data/ext/sspcrun/src/_sspc.c +358 -0
  129. data/ext/sspcrun/src/aheap.c +545 -0
  130. data/ext/sspcrun/src/aheap.h +251 -0
  131. data/ext/sspcrun/src/base.c +92 -0
  132. data/ext/sspcrun/src/base.h +59 -0
  133. data/ext/sspcrun/src/fstar.c +496 -0
  134. data/ext/sspcrun/src/fstar.h +80 -0
  135. data/ext/sspcrun/src/grhfil.c +213 -0
  136. data/ext/sspcrun/src/itemset.c +713 -0
  137. data/ext/sspcrun/src/itemset.h +170 -0
  138. data/ext/sspcrun/src/problem.c +415 -0
  139. data/ext/sspcrun/src/problem.h +179 -0
  140. data/ext/sspcrun/src/queue.c +533 -0
  141. data/ext/sspcrun/src/queue.h +182 -0
  142. data/ext/sspcrun/src/sample.c +19 -0
  143. data/ext/sspcrun/src/sspc.c +598 -0
  144. data/ext/sspcrun/src/sspc2.c +491 -0
  145. data/ext/sspcrun/src/stdlib2.c +1482 -0
  146. data/ext/sspcrun/src/stdlib2.h +892 -0
  147. data/ext/sspcrun/src/trsact.c +817 -0
  148. data/ext/sspcrun/src/trsact.h +160 -0
  149. data/ext/sspcrun/src/vec.c +745 -0
  150. data/ext/sspcrun/src/vec.h +172 -0
  151. data/ext/sspcrun/sspcrun.cpp +54 -0
  152. data/lib/nysol/enumLcmEp.rb +338 -0
  153. data/lib/nysol/enumLcmEsp.rb +284 -0
  154. data/lib/nysol/enumLcmIs.rb +275 -0
  155. data/lib/nysol/enumLcmSeq.rb +143 -0
  156. data/lib/nysol/items.rb +201 -0
  157. data/lib/nysol/seqDB.rb +256 -0
  158. data/lib/nysol/take.rb +39 -0
  159. data/lib/nysol/taxonomy.rb +113 -0
  160. data/lib/nysol/traDB.rb +257 -0
  161. metadata +239 -0
data/bin/mpolishing.rb ADDED
@@ -0,0 +1,399 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require "rubygems"
5
+ require "nysol/mcmd"
6
+ require "nysol/take"
7
+ require "set"
8
+
9
+ # ver="1.0" # 初期リリース 2014/2/20
10
+ # ver="1.1" # 節点データ対応、途中経過出力 2014/8/1
11
+ # ver="1.2" # 出力nodeファイルの項目名をnf=の値にする 2016/11/11
12
+ $cmd=$0.sub(/.*\//,"")
13
+ $version="1.2"
14
+
15
+ def help
16
+ STDERR.puts <<EOF
17
+ ----------------------------
18
+ #{$cmd} version #{$version}
19
+ ----------------------------
20
+ 概要) グラフデータの研磨をおこなう。
21
+ 内容) 一般グラフデータを入力として、密度の高い部分グラフにあって枝が張られていないノードペアに枝を張る。
22
+ 逆に、密度の低い部分グラフにあって枝が張られているノードペアの枝を刈る。
23
+ 新たに張られる枝や刈られる枝の程度は、sim=とth=で与えた値によって変わる。
24
+ 詳細は以下に示したアルゴリズムを参照。
25
+ 書式) #{$cmd} ei= [ef=] [ni=] [nf=] eo= [no=] [sim=i|I|s|S|T|R|P|C] th= [sup=] [-indirect] [iter=] [log=] [T=] [--help]
26
+
27
+ ファイル名指定
28
+ ei= : 枝データファイル
29
+ ef= : 枝データ上の2つの節点項目名(省略時は"node1,node2")
30
+ ni= : 節点データファイル
31
+ nf= : 節点データ上の節点項目名(省略時は"node")
32
+ eo= : データ研磨後の枝データファイル
33
+ no= : データ研磨後の節点データファイル
34
+ sim= : 節点a,bと接続された枝集合を、それぞれA,Bとすると、節点a,bに枝を張るために用いる類似度。
35
+ 省略時はRが設定される。
36
+ i: inclusion
37
+ I: both-inclusion
38
+ S: |A∩B|/max(|A|,|B|)
39
+ s: |A∩B|/min(|A|,|B|)
40
+ T (intersection): find pairs having common [threshld] items
41
+ R (resemblance): find pairs s.t. |A\capB|/|A\cupB| >= [threshld]
42
+ P (PMI): find pairs s.t. log (|A\capB|*|all| / (|A|*|B|)) >= [threshld]
43
+ C (cosine distance): find pairs s.t. inner product of their normalized vectors >= [threshld]
44
+ th= : sim=で指定された類似度について、ここで指定された値以上の節点間に枝を張る。
45
+ sup= : 類似度計算において、|A∩B|>=supの条件を加える。省略すればsup=0。
46
+ -indirect: 上記類似度計算における隣接節点集合から直接の関係を除外する。
47
+ すなわち、A=A-b, B=B-a として類似度を計算する。
48
+ iter= : データ研磨の最大繰り返し数(デフォルト=30)
49
+ log= : パラメータの設定値や収束回数等をkey-value形式のCSVで保存するファイル名
50
+
51
+ その他
52
+ T= : ワークディレクトリ(default:/tmp)
53
+ O= : デバッグモード時、データ研磨過程のグラフを保存するディレクトリ名
54
+ --help : ヘルプの表示
55
+
56
+ 備考)
57
+ 内部で起動しているコマンドsspcは0から始まる整数で指定された節点名を前提として処理する。
58
+ 一方で本コマンドは、任意の文字列で節点名を表したデータを処理できる。
59
+ それは、sspcを実行する前に、それら文字列と整数との対応表を前処理で作成しているからである。
60
+
61
+ アルゴリズム)
62
+ # 本コマンドでデフォルトで設定されている類似度関数。
63
+ function sim(E,a,b,th,minSup)
64
+ A=E上での節点aの隣接節点集合
65
+ B=E上での節点bの隣接節点集合
66
+ if |A ∩ B|>=minSup and |A ∩ B|/|A ∪ B|>=th
67
+ return TRUE
68
+ else
69
+ return FALSE
70
+ end
71
+ end
72
+
73
+ function conv(E,sim,th,minSup)
74
+ foreach(e∈E)
75
+ a,b=eを構成する2つの節点
76
+ if sim(E,a,b,th,minSup)
77
+ E' = E' ∪ edge(a,b)
78
+ end
79
+ end
80
+ return E'
81
+ end
82
+
83
+ polishing(E,sim,th,minSup,iter)
84
+ E:=グラフを構成する枝集合
85
+ iter:=最大繰り返し回数
86
+ sim:=類似度関数
87
+ th:=最小類似度
88
+ c=0
89
+ while(c<iter)
90
+ E'=conv(E,sim,th,minSup)
91
+ if E==E'
92
+ break
93
+ end
94
+ E=E'
95
+ c=c+1
96
+ end
97
+ return E
98
+ end
99
+
100
+ 例) 枝データからのみグラフ研磨を実行する例
101
+ $ cat edge.csv
102
+ n1,n2
103
+ a,b
104
+ a,d
105
+ b,c
106
+ b,d
107
+ c,d
108
+ d,e
109
+ $ #{$cmd} ei=edge.csv ef=n1,n2 th=0.5 eo=output.csv
110
+ #MSG# converting graph files into a pair of numbered nodes ...; 2013/10/10 14:48:03
111
+ #MSG# polishing iteration #0; 2013/10/10 14:48:04
112
+ #MSG# converting the numbered nodes into original name ...; 2013/10/10 14:48:04
113
+ #END# #{$cmd} ei=edge.csv ef=n1,n2 th=0.1 eo=output.csv; 2013/10/10 14:48:04
114
+ $ cat output.csv
115
+ n1,n2
116
+ a,b
117
+ a,c
118
+ a,d
119
+ b,c
120
+ b,d
121
+ c,d
122
+
123
+ # Copyright(c) NYSOL 2012- All Rights Reserved.
124
+ EOF
125
+ exit
126
+ end
127
+
128
+ def ver()
129
+ STDERR.puts "version #{$version}"
130
+ exit
131
+ end
132
+
133
+ help() if ARGV.size <= 0 or ARGV[0]=="--help"
134
+ ver() if ARGV[0]=="--version"
135
+
136
+ args=MCMD::Margs.new(ARGV,"ni=,nf=,ei=,ef=,-indirect,eo=,no=,th=,sim=,sup=,iter=,log=,O=","ei=,ef=,th=")
137
+
138
+ # コマンド実行可能確認
139
+ #CMD_sspc="sspc_20161209"
140
+ #CMD_grhfil="grhfil_20150920"
141
+ #exit(1) unless(MCMD::chkCmdExe(CMD_sspc , "executable"))
142
+ #exit(1) unless(MCMD::chkCmdExe(CMD_grhfil, "executable"))
143
+
144
+ # mcmdのメッセージは警告とエラーのみ
145
+ ENV["KG_VerboseLevel"]="2" unless args.bool("-mcmdenv")
146
+
147
+ #ワークファイルパス
148
+ if args.str("T=")!=nil then
149
+ ENV["KG_TmpPath"] = args.str("T=").sub(/\/$/,"")
150
+ end
151
+
152
+ indirect=args.bool("-indirect")
153
+
154
+ ei = args. file("ei=","r") # edge file name
155
+ ni = args. file("ni=","r") # node file name
156
+
157
+ # ---- edge field names (two nodes) on ei=
158
+ ef1,ef2 = args.field("ef=", ei, "node1,node2",2,2)["names"]
159
+
160
+ # ---- node field name on ni=
161
+ nf = args.field("nf=", ni, "node",1,1)
162
+ nf = nf["names"][0] if nf
163
+
164
+ measure = args.str("sim=","R") # similarity measure
165
+ minSupp = args.int("sup=",0) # mimam support
166
+ iterMax = args.int("iter=",30,1) # upper bound of iterations
167
+ th = args.float("th=") # threashold for similarity measure
168
+
169
+ eo = args.file("eo=", "w")
170
+ no = args.file("no=", "w")
171
+ logFile = args.file("log=", "w")
172
+ outDir = args.str("O=") # 過程出力
173
+ MCMD::mkDir(outDir) if outDir
174
+
175
+ # node数とedge数をカウント
176
+ def calGsize(file)
177
+ nodes=Set.new
178
+ edgeSize=0
179
+ File.open(file,"r"){|fpr|
180
+ while line = fpr.gets
181
+ n1,n2 = line.split(" ")
182
+ nodes << n1
183
+ nodes << n2
184
+ edgeSize+=1
185
+ end
186
+ }
187
+ return nodes.size,edgeSize
188
+ end
189
+
190
+ # graphの各種特徴量を計算する
191
+ # orgNsizeが与えられなければ、node数は枝ファイル(file)から計算する。
192
+ # 0,1
193
+ # 0,2
194
+ # 0,3
195
+ # 1,2
196
+ # 1,3
197
+ def features(file,orgNsize=nil)
198
+ nodes=Set.new
199
+ graph=Hash.new
200
+ edgeSize=0
201
+ File.open(file,"r"){|fpr|
202
+ while line = fpr.gets
203
+ n1,n2 = line.split(" ")
204
+ if n1>n2 then
205
+ nt=n1; n1=n2; n2=nt
206
+ end
207
+ s=graph[n1]
208
+ s << n2 if s and not s.include?(n2)
209
+ nodes << n1
210
+ nodes << n2
211
+ edgeSize+=1
212
+ end
213
+ }
214
+
215
+ # 密度
216
+ dens=nil
217
+ nSize=nodes.size.to_f
218
+ dens=edgeSize.to_f/(nSize*(nSize-1.0)/2.0) if nSize>1.0
219
+
220
+ # clustering coefficient
221
+ graph.each{|s|
222
+ size=s.size
223
+ }
224
+
225
+ nodeSize,edgeSize=calGsize(file)
226
+
227
+ nSize=nodeSize.to_f
228
+ nSize=orgNsize.to_f if orgNsize
229
+
230
+ dens=nil
231
+ dens=edgeSize.to_f/(nSize*(nSize-1)/2.0) if nSize>1
232
+
233
+ return nSize,edgeSize,dens
234
+ end
235
+
236
+ def same?(file1,file2)
237
+ xx=MCMD::Mtemp.new.file
238
+
239
+ return false if File.size(file1)!=File.size(file2)
240
+ system "diff -q #{file1} #{file2} > #{xx}"
241
+ return false if File.size(xx)!=0
242
+ return true
243
+ end
244
+
245
+ def g2pair(ni,nf,ei,ef1,ef2,ipair,mapFile)
246
+ MCMD::msgLog("converting graph files into a pair of numbered nodes ...")
247
+ wf=MCMD::Mtemp.new
248
+ wf1=wf.file
249
+ wf2=wf.file
250
+ wf3=wf.file
251
+
252
+ system "mcut f=#{ef1}:node i=#{ei} o=#{wf1}"
253
+ system "mcut f=#{ef2}:node i=#{ei} o=#{wf2}"
254
+ system "mcut f=#{nf}:node i=#{ni} o=#{wf3}" if ni
255
+
256
+ f=""
257
+ if ni
258
+ f << "mcat i=#{wf1},#{wf2},#{wf3} f=node |"
259
+ else
260
+ f << "mcat i=#{wf1},#{wf2} f=node |"
261
+ end
262
+ f << "msortf f=node |"
263
+ f << "muniq k=node |"
264
+ f << "mnumber -q a=num o=#{mapFile}"
265
+ system(f)
266
+
267
+ f=""
268
+ f << "mcut f=#{ef1},#{ef2} i=#{ei} |"
269
+ f << "msortf f=#{ef1} |"
270
+ f << "mjoin k=#{ef1} K=node m=#{mapFile} f=num:num1 |"
271
+ f << "msortf f=#{ef2} |"
272
+ f << "mjoin k=#{ef2} K=node m=#{mapFile} f=num:num2 |"
273
+ f << "mcut f=num1,num2 |"
274
+ f << "mfsort f=num1,num2 |"
275
+ f << "msortf f=num1%n,num2%n -nfno |"
276
+ f << "tr ',' ' ' >#{ipair}"
277
+ system(f)
278
+ end
279
+
280
+ # ============
281
+ # entry point
282
+ t=Time.now
283
+
284
+ # 入力ファイルをノード番号ペアデータ(input)に変換する。
285
+ # csvで指定された場合は、番号-アイテムmapデータも作成(xxmap)。
286
+ wf=MCMD::Mtemp.new
287
+ xxinp=wf.file
288
+ xxmap=wf.file
289
+ xxmaprev=wf.file
290
+ input=ei
291
+
292
+ g2pair(ni,nf,ei,ef1,ef2,xxinp,xxmap)
293
+ input=xxinp
294
+ system "msortf f=num i=#{xxmap} o=#{xxmaprev}"
295
+
296
+ xxpair = wf.file # sscpの出力(pair形式)
297
+ xxtra = wf.file # sscpの入力(tra形式)
298
+ xxprev = wf.file # 前回のxxtra
299
+ system "cp #{input} #{xxpair}"
300
+
301
+ nSizes=[]
302
+ eSizes=[]
303
+ denses=[]
304
+
305
+ iter=0
306
+ while true
307
+ # グラフ特徴量の計算
308
+ if logFile
309
+ nSize,eSize,dens=features(xxpair)
310
+ nSizes << nSize
311
+ eSizes << eSize
312
+ denses << dens
313
+ end
314
+
315
+ # node pairをsspc入力形式に変換
316
+ if indirect then
317
+ #system "#{CMD_grhfil} ue #{xxpair} #{xxtra}"
318
+ TAKE::run_grhfil("ue #{xxpair} #{xxtra}")
319
+
320
+ else
321
+ #system "#{CMD_grhfil} ue0 #{xxpair} #{xxtra}"
322
+ TAKE::run_grhfil("ue0 #{xxpair} #{xxtra}")
323
+ end
324
+
325
+
326
+ # outDirが指定されていれば、途中経過グラフを出力
327
+ if outDir then
328
+ f=""
329
+ f << "tr ' ' ',' <#{xxpair} |"
330
+ f << "mcut f=0:num1,1:num2 -nfni |"
331
+ f << "msortf f=num1 |"
332
+ f << "mjoin k=num1 K=num m=#{xxmaprev} f=node:#{ef1} |"
333
+ f << "msortf f=num2 |"
334
+ f << "mjoin k=num2 K=num m=#{xxmaprev} f=node:#{ef2} |"
335
+ f << "mcut f=#{ef1},#{ef2} |"
336
+ f << "mfsort f=#{ef1},#{ef2} |"
337
+ f << "msortf f=#{ef1},#{ef2} o=#{outDir}/pair_#{iter}.csv"
338
+ system(f)
339
+ end
340
+
341
+ # 終了判定
342
+ break if iter>=iterMax
343
+ break if iter!=0 and same?(xxtra,xxprev)
344
+
345
+ MCMD::msgLog("polishing iteration ##{iter} (tra size=#{File.size(xxtra)}")
346
+ system "cp #{xxtra} #{xxprev}"
347
+ puts "sspc #{measure} -l #{minSupp} #{xxtra} #{th} #{xxpair}"
348
+ #system "#{CMD_sspc} #{measure} -l #{minSupp} #{xxtra} #{th} #{xxpair}"
349
+ TAKE::run_sspc("#{measure} -l #{minSupp} #{xxtra} #{th} #{xxpair}")
350
+
351
+
352
+ iter+=1
353
+ end
354
+
355
+ # 上記iterationで収束したマイクロクラスタグラフを元の節点文字列に直して出力する
356
+ MCMD::msgLog("converting the numbered nodes into original name ...")
357
+ f=""
358
+ f << "tr ' ' ',' <#{xxpair} |"
359
+ f << "mcut f=0:num1,1:num2 -nfni |"
360
+ f << "msortf f=num1 |"
361
+ f << "mjoin k=num1 K=num m=#{xxmaprev} f=node:#{ef1} |"
362
+ f << "msortf f=num2 |"
363
+ f << "mjoin k=num2 K=num m=#{xxmaprev} f=node:#{ef2} |"
364
+ f << "mcut f=#{ef1},#{ef2} |"
365
+ f << "mfsort f=#{ef1},#{ef2} |"
366
+ f << "msortf f=#{ef1},#{ef2} o=#{eo}"
367
+ system(f)
368
+
369
+ if no then
370
+ f=""
371
+ if nf
372
+ f << "mcut f=node:#{nf} i=#{xxmap} o=#{no}"
373
+ else
374
+ f << "mcut f=node i=#{xxmap} o=#{no}"
375
+ end
376
+ system(f)
377
+ end
378
+
379
+ procTime=Time.now-t
380
+
381
+ # ログファイル出力
382
+ if logFile
383
+ kv=args.getKeyValue()
384
+ kv << ["iter",iter]
385
+ kv << ["time",procTime]
386
+ (0...nSizes.size).each{|i|
387
+ kv << ["nSize#{i}",nSizes[i]]
388
+ kv << ["eSize#{i}",eSizes[i]]
389
+ kv << ["dens#{i}" ,denses[i]]
390
+ }
391
+ MCMD::Mcsvout.new("o=#{logFile} f=key,value"){|csv|
392
+ kv.each{|line|
393
+ csv.write(line)
394
+ }
395
+ }
396
+ end
397
+
398
+ # 終了メッセージ
399
+ MCMD::endLog(args.cmdline)
data/bin/msequence.rb ADDED
@@ -0,0 +1,165 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require "fileutils"
5
+ require "nysol/seqDB"
6
+ require "nysol/taxonomy"
7
+ require "nysol/enumLcmEsp"
8
+ require "nysol/enumLcmSeq"
9
+
10
+
11
+ $cmd=$0.sub(/.*\//,"")
12
+ $version="1.0"
13
+
14
+ def help
15
+ STDERR.puts <<EOF
16
+ ------------------------
17
+ #{$cmd} version #{$version}
18
+ ------------------------
19
+ 概要) LCM_seqにより多頻度系列パターンを列挙する
20
+ 特徴) 1)
21
+ 書式) #{$cmd} i= [x=] [O=] [tid=] [item=] [time=] [taxo=] [class=] [s=] [S=] [sx=] [Sx=] [l=] [u=] [top=] [p=] [g=] [w=] [Z=] [T=] [--help]
22
+
23
+ 例1) #{$cmd} i=weblog.csv c=customer.csv tid=traID item=page time=アクセス時刻 class=購買
24
+ 例2) #{$cmd} i=weblog.csv tid=traID item=page time=アクセス時刻
25
+
26
+ ファイル名指定
27
+ i= : アイテム集合データベースファイル名【必須】
28
+ c= : クラスファイル名【オブション】
29
+ x= : taxonomyファイル名【オブション】*1
30
+ O= : 出力ディレクトリ名【オプション:default:./take_現在日付時刻】
31
+
32
+ 項目名指定
33
+ tid= : トランザクションID項目名(i=,c=上の項目名)【オプション:default="tid"】
34
+ time= : 時間項目名(i=上の項目名)【オプション:default="time"】
35
+ item= : アイテム項目名(i=,t=上の項目名)【オプション:default="item"】
36
+ class= : クラス項目名(c=上の項目名)【オプション:default="class"】
37
+ taxo= : 分類項目名を指定する【条件付き必須:x=】
38
+
39
+ 列挙パラメータ
40
+ s= : 最小支持度(support)【オプション:default:0.05, 0以上1以下の実数】
41
+ S= : 最小支持件数【オプション】
42
+ sx= : 最大支持度(support)【オプション:default:1.0, 0以上1以下の実数】
43
+ Sx= : 最大支持件数【オプション】
44
+ l= : パターンサイズの下限(1以上20以下の整数)【オプション:default:制限なし】
45
+ u= : パターンサイズの上限(1以上20以下の整数)【オプション:default:制限なし】
46
+ p= : 最小事後確率【オプション:default:0.5】
47
+ g= : 最小増加率【オプション】
48
+ gap= : パターンのギャップ長の上限(0以上の整数)【オプション:0で制限無し,default:0】
49
+ win= : パターンのウィンドウサイズの上限(0以上の整数)【オプション:0で制限無し,default:0】
50
+ -padding : 時刻を整数とみなし、連続でない時刻に特殊なアイテムがあることを想定する。
51
+ : gap=やwin=の指定に影響する。
52
+ top= : 列挙するパターン数の上限【オプション:default:制限なし】*2
53
+
54
+ その他
55
+ T= : ワークディレクトリ(default:/tmp)
56
+ --help : ヘルプの表示
57
+
58
+ 注釈
59
+ *1 x=が指定されたとき、item項目の値を対応するtaxonomyに変換して実行する。例えば、アイテムa,bのtaxonomyをX、c,dのtaxonomyをYとすると、
60
+ シーケンス"aeadd"は"XeXYY"に変換される。
61
+ *2 top=が指定された時の動作: 例えばtop=10と指定すると、支持度が10番目高いパターンの支持度を最小支持度として頻出パターンを列挙する。よって、同じ支持度のパターンが複数個ある場合は10個以上のパターンが列挙されるかもしれない。
62
+
63
+ # より詳しい情報源 http://www.nysol.jp
64
+ # LCM_seqの詳しい情報源 http://research.nii.ac.jp/~uno/codes-j.htm
65
+ # Copyright(c) NYSOL 2012- All Rights Reserved.
66
+ EOF
67
+ exit
68
+ end
69
+
70
+ def ver()
71
+ STDERR.puts "version #{$version}"
72
+ exit
73
+ end
74
+
75
+ help() if ARGV.size <= 0 or ARGV[0]=="--help"
76
+ ver() if ARGV[0]=="--version"
77
+ args=MCMD::Margs.new(ARGV,"i=,c=,x=,O=,tid=,time=,item=,class=,taxo=,s=,S=,sx=,Sx=,g=,p=,-uniform,l=,u=,top=,gap=,win=,-padding,T=,-mcmdenv")
78
+
79
+ # lcm_seqコマンド実行可能確認
80
+ #exit(1) unless(MCMD::chkCmdExe(TAKE::LcmSeq::CMD , "executable"))
81
+ #exit(1) unless(MCMD::chkCmdExe(TAKE::LcmSeq::CMD_ZERO , "executable"))
82
+ #exit(1) unless(MCMD::chkCmdExe(TAKE::LcmSeq::CMD_TRANS, "-v", "lcm_trans 1.0"))
83
+
84
+ # mcmdのメッセージは警告とエラーのみ
85
+ ENV["KG_VerboseLevel"]="2" unless args.bool("-mcmdenv")
86
+
87
+ #ワークファイルパス
88
+ if args.str("T=")!=nil then
89
+ ENV["KG_TmpPath"] = args.str("T=").sub(/\/$/,"")
90
+ end
91
+
92
+ iFile = args.file("i=","r")
93
+ xFile = args.file("x=","r")
94
+
95
+ t=Time.now
96
+ outPath = args.file("O=", "w", "./take_#{sprintf('%d%02d%02d%02d%02d%02d',t.year,t.month,t.day,t.hour,t.min,t.sec)}")
97
+
98
+ idFN = args.field("tid=", iFile, "tid" )
99
+ timeFN = args.field("time=", iFile, "time" )
100
+ itemFN = args.field("item=", iFile, "item" )
101
+ clsFN = args.field("class=", iFile, nil )
102
+ taxoFN = args.field("taxo=", xFile, "taxo" )
103
+ idFN = idFN["names"].join(",") if idFN
104
+ timeFN = timeFN["names"].join(",") if timeFN
105
+ itemFN = itemFN["names"].join(",") if itemFN
106
+ clsFN = clsFN["names"].join(",") if clsFN
107
+ taxoFN = taxoFN["names"].join(",") if taxoFN
108
+
109
+ eArgs=Hash.new
110
+ eArgs["minSup" ] = args.float("s=" ,0.05 ,0,1 ) # 最小サポート
111
+ eArgs["minCnt" ] = args. int("S=" ,nil ,1,nil ) # 最小サポート件数
112
+ eArgs["maxSup" ] = args.float("sx=" ,nil ,0,1 ) # 最小サポート
113
+ eArgs["maxCnt" ] = args. int("Sx=" ,nil ,1,nil ) # 最小サポート件数
114
+ eArgs["minProb"] = args.float("p=" ,0.5 ,0.5,1 ) # 最小事後確率
115
+ eArgs["minGR" ] = args.float("g=" ,nil ,1.0,nil) # 最小GR
116
+ eArgs["uniform"] = args. bool("-uniform") # クラス事前確率を一様と考えるかどうか
117
+ eArgs["minLen" ] = args. int("l=" ,nil ,1,nil )
118
+ eArgs["maxLen" ] = args. int("u=" ,nil ,1,nil )
119
+ eArgs["gap" ] = args. int("gap=" ,nil ,0,nil ) # gap長上限限
120
+ eArgs["win" ] = args. int("win=" ,nil ,0,nil ) # win size上限限
121
+ eArgs["padding"] = args. bool("-padding") # 0item ommit
122
+ eArgs["top" ] = args. int("top=" ,nil,0)
123
+
124
+ if eArgs["minLen"] and eArgs["maxLen"]
125
+ if eArgs["minLen"] > eArgs["maxLen"] then
126
+ raise "u= must be greater than or equal to l="
127
+ end
128
+ end
129
+
130
+ if eArgs["gap"] and eArgs["win"]
131
+ if eArgs["gap"] > eArgs["win"] then
132
+ raise "win= must be greater than or equal to gap="
133
+ end
134
+ end
135
+
136
+ # seq型DBの読み込み
137
+ db=TAKE::SeqDB.new(iFile,idFN,timeFN,itemFN,eArgs["padding"],clsFN)
138
+ #db.show
139
+
140
+ # taxonomyのセット
141
+ taxo=nil
142
+ if xFile!=nil then
143
+ taxo=TAKE::Taxonomy.new(xFile,itemFN,taxoFN)
144
+ db.repTaxo(taxo) # seqはtaxonomyの置換のみ
145
+ end
146
+
147
+ # パターン列挙
148
+ lcm=nil
149
+ if clsFN then
150
+ lcm=TAKE::LcmEsp.new(db);
151
+ lcm.enumerate(eArgs)
152
+ else
153
+ lcm=TAKE::LcmSeq.new(db);
154
+ lcm.enumerate(eArgs)
155
+ end
156
+
157
+ # 出力
158
+ system("mkdir -p #{outPath}")
159
+ lcm.output(outPath)
160
+
161
+ MCMD::msgLog("The final results are in the directory `#{outPath}'")
162
+
163
+ # 終了メッセージ
164
+ MCMD::endLog(args.cmdline)
165
+