Beispiel #1
0
	def g2pair(self,ni,nf,ei,ef1,ef2,ew,numFile,mapFile,weightFile):

		inobj = []
		inobj.append(nm.mcut(f="%s:node"%(ef1),i=ei ).msetstr(a="flag",v=0))
		inobj.append(nm.mcut(f="%s:node"%(ef2),i=ei ).msetstr(a="flag",v=0))
		if nf :
			inobj.append(nm.mcut(f="%s:node"%(nf),i=ni ).msetstr(a="flag",v=1))

		f   = nm.mbest(i=inobj,k="node",s="flag",fr=0,size=1 )
		# isolated nodes are set to the end of position in mapping file.
		# S= must start from 0 (but inside R vertex number will be added one)
		f <<= nm.mnumber(s="flag,node",a="num",S=0,o=mapFile)
		f.run()

		f = None 
		f <<= nm.mcut(f=[ef1,ef2] , i=ei)
		f <<= nm.mjoin( k=ef1 , K="node" , m=mapFile ,f="num:num1")
		f <<= nm.mjoin( k=ef2 , K="node" , m=mapFile ,f="num:num2")
		f <<= nm.mcut(f="num1,num2")
		f <<= nm.mfsort(f="num1,num2")
		f <<= nm.msortf(f="num1%n,num2%n",nfno=True)
		f <<= nm.cmd("tr ',' ' ' " )
		f <<= nm.mwrite(o=numFile)
		f.run()

		nodeSize=mrecount(i=mapFile)

		if ew:
			nm.mcut(f=ew,i=ei,o=weightFile).run()
		else:
			ew="weight"
			nm.msetstr(v=1,a=ew,i=ei).mcut(f=ew,o=weightFile).run()

		return nodeSize
Beispiel #2
0
	def g2pair(self,ni,nf,ei,ef1,ef2,ew,numFile,mapFile,weightFile):
		#MCMD::msgLog("converting graph files into a pair of numbered nodes ...")
		#wf=MCMD::Mtemp.new
		#wf1=wf.file
		#wf2=wf.file
		#wf3=wf.file
		
		allinObj =[]		
		
		
		wf1 = nm.mcut(f="%s:node"%(ef1),i=ei ).msetstr(v=0,a="flag")
		wf2 = nm.mcut(f="%s:node"%(ef2),i=ei ).msetstr(v=0,a="flag")


		f = None
		if nf:
			f <<= nm.mcut(i=[wf1,wf2,nm.mcut(f=nf+":node",i=ni).msetstr(v=1,a="flag")],f="node,flag")
			f <<= nm.mbest(k="node" , s="flag" , fr=0 ,size=1)
		else:
			f <<= nm.mcut(i=[wf1,wf2],f="node,flag")
			f <<= nm.muniq(k="node")
	
		f <<= nm.mnumber(s="flag,node",a="num",S=0,o=mapFile)

		f.run()
						
		f = None
		f <<= nm.mcut(f=[ef1,ef2],i=ei)
		f <<= nm.mjoin(k=ef1 , K="node",m=mapFile , f="num:num1")
		f <<= nm.mjoin(k=ef2 , K="node",m=mapFile , f="num:num2")
		f <<= nm.mcut(f="num1,num2")
		f <<= nm.mfsort(f="num1,num2")
		f <<= nm.msortf(f="num1%n,num2%n",nfno=True) 
		f <<= nm.cmd("tr ',' ' ' ")
		f <<= nm.mwrite(o=numFile)
		f.run()


		if ew :
				nm.mcut(f=ew,i=ei,o=weightFile).run()
		else:
			ew="weight"
			nm.msetstr(v=1 , a=ew ,i=ei).mcut(f=ew,o=weightFile).run()

		nodeSize=mrecount(i=mapFile)
		return nodeSize
Beispiel #3
0
    def g2pair(self, ni, nf, ei, ef1, ef2, ipair, mapFile):

        dlist = [
            nm.mcut(f=ef1 + ":node", i=ei),
            nm.mcut(f=ef2 + ":node", i=ei)
        ]

        if (ni):
            dlist.append(nm.mcut(f=nf + ":node", i=ni))

        fmap = None
        fmap <<= nm.mcut(i=dlist, f="node")
        fmap <<= nm.muniq(k="node")
        fmap <<= nm.mnumber(q=True, a="num", o=mapFile)

        f = nm.mcut(f=[ef1, ef2], i=ei)
        f <<= nm.mjoin(k=ef1, K="node", m=fmap, f="num:num1")
        f <<= nm.mjoin(k=ef2, K="node", m=fmap, f="num:num2")
        f <<= nm.mcut(f="num1,num2")
        f <<= nm.mfsort(f="num1,num2")
        f <<= nm.msortf(f="num1%n,num2%n", nfno=True, o=ipair)
        f.run()
Beispiel #4
0
	def run(self):

		from datetime import datetime	
		t = datetime.now()

		temp=nu.Mtemp()
		xxsspcin=temp.file()
		xxmap=temp.file()

		# traファイルの変換とマップファイルの作成
		if self.num :
			total = self.convN(self.iFile,self.idFN,self.itemFN,xxsspcin,xxmap)
		else:
			total = self.conv(self.iFile,self.idFN,self.itemFN,xxsspcin,xxmap)

		# system "head xxsspcin"
		# 3 5 0 2
		# 4 1 2
		# 0 2 3 1
		# 1 0 2
		# 3 4 0 1
		# system "head xxmap"
		# ##item,##freq%0nr,##num
		# b,4,0
		# d,4,1
		# f,4,2
		minSupp = int(total*self.minSupPrb)	if self.minSupPrb else self.minSupCnt
			

		# sspc用simの文字列
		if self.sim :
			if self.sim=="J":
				sspcSim="R"
			elif self.sim=="P":
				sspcSim="P"
			elif self.sim=="C":
				sspcSim="i"
		# sim=省略時はRでth=0とする(sim制約なし)
		else:
			sspcSim="R"
			self.th=0

		############ 列挙本体 ############
		xxsspcout=temp.file()
		tpstr =  sspcSim+"ft_" if self.msgoff else sspcSim+"ft"
		extTake.sspc(type=tpstr,TT=minSupp,i=xxsspcin,th=self.th,o=xxsspcout)

		##################################

		xxtmmp=temp.file()
		
		f =   nm.mread(i=xxsspcout) 
		f <<= nm.cmd("tr ' ()' ','") 
		f <<= nm.mcut(f="1:i1,2:i2,0:frequency,4:sim",nfni=True)

		if self.num :

			f <<= nm.mfldname(f="i1:node1,i2:node2")
			if self.sim!="C":
				f <<= nm.mfsort(f="node1,node2")
			
			f <<= nm.mjoin(k="node1",K="##item",m=xxmap,f="##freq:frequency1")
			f <<= nm.mjoin(k="node2",K="##item",m=xxmap,f="##freq:frequency2") 
			
		else:

			f <<= nm.mjoin(k="i1",K="##num",m=xxmap,f="##item:node1,##freq:frequency1")
			f <<= nm.mjoin(k="i2",K="##num",m=xxmap,f="##item:node2,##freq:frequency2") 

			if self.sim!="C":

				f <<= nm.mcut(f="i1,i2,frequency,sim,node1,node2,frequency1,frequency2,node1:node1x,node2:node2x")
				f <<= nm.mfsort(f="node1x,node2x")
				f <<= nm.mcal(c='if($s{node1}==$s{node1x},$s{frequency1},$s{frequency2})',a="freq1")
				f <<= nm.mcal(c='if($s{node2}==$s{node2x},$s{frequency2},$s{frequency1})',a="freq2")
				f <<= nm.mcut(f="i1,i2,frequency,sim,node1x:node1,node2x:node2,freq1:frequency1,freq2:frequency2")

		f <<= nm.msetstr(v=total,a="total")
		f <<= nm.mcal(c='${frequency}/${frequency1}',a="confidence")
		f <<= nm.mcal(c='${frequency}/${total}',a="support")
		f <<= nm.mcal(c='${frequency}/(${frequency1}+${frequency2}-${frequency})',a="jaccard")
		f <<= nm.mcal(c='(${frequency}*${total})/((${frequency1}*${frequency2}))',a="lift")
		f <<= nm.mcal(c='(ln(${frequency})+ln(${total})-ln(${frequency1})-ln(${frequency2}))/(ln(${total})-ln(${frequency}))',a="PMI")
		f <<= nm.mcut(f="node1,node2,frequency,frequency1,frequency2,total,support,confidence,lift,jaccard,PMI")
		f <<= nm.msortf(f="node1,node2",o=self.oeFile)
		f.run()

		if self.onFile:
			f4 =   nm.mcut(f=self.itemFN+":node",i=self.iFile)
			f4 <<= nm.mcount(k="node",a="frequency")
			if self.node_support :
				minstr = "[%s,]"%(minSupp)
				f4 <<= nm.mselnum(f="frequency",c=minstr)

			f4 <<= nm.msetstr(v=total,a="total")
			f4 <<= nm.mcal(c='${frequency}/${total}',a="support")
			f4 <<= nm.mcut(f="node,support,frequency,total",o=self.onFile)
			f4.run()

		procTime=datetime.now()-t

		# ログファイル出力
		if self.logFile :
			kv=[["key","value"]]
			for k,v in self.args.items():
				kv.append([k,str(v)])
			kv.append(["time",str(procTime)])
			nm.writecsv(i=kv,o=self.logFile).run()
Beispiel #5
0
tra <<= nm.mcut(f="id,date2:date,ret")

# frequency of one item
freq = None
freq <<= nm.mcut(f="id", i=tra)
freq <<= nm.mcount(k="id", a="freq")
freq <<= nm.mselnum(f="freq", c="[5,]")

# total number of transactions
total = None
total <<= nm.mcut(f="date", i=tra)
total <<= nm.muniq(k="date")
total <<= nm.mcount(a="total")

# frequency of cooccurence of id, and calculate lift values
itemCoFreq = None
itemCoFreq <<= nm.mcut(f="date,id", i=tra)
itemCoFreq <<= nm.mcommon(k="id", m=freq)
itemCoFreq <<= nm.mcombi(k="date", n=2, f="id", a="id1,id2")
itemCoFreq <<= nm.mcut(f="id1,id2")
itemCoFreq <<= nm.mfsort(f="id1,id2")
itemCoFreq <<= nm.mcount(k="id1,id2", a="coFreq")
itemCoFreq <<= nm.mjoin(k="id1", m=freq, K="id", f="freq:freq1")
itemCoFreq <<= nm.mjoin(k="id2", m=freq, K="id", f="freq:freq2")
itemCoFreq <<= nm.mproduct(m=total, f="total")
itemCoFreq <<= nm.mcal(c="(${coFreq}*${total})/(${freq1}*${freq2})", a="lift")
itemCoFreq <<= nm.msel(c="${lift}>10 && ${coFreq}>6")
r = itemCoFreq.run(msg=debug)
print(r)
print(len(r))
Beispiel #6
0
    def run(self, **kw_args):

        os.environ['KG_ScpVerboseLevel'] = "2"
        if "msg" in kw_args:
            if kw_args["msg"] == "on":
                os.environ['KG_ScpVerboseLevel'] = "4"

        ln = "#{@pt}line"

        # make the line number
        ln = "{}line".format(self.pt)

        xxmap = self.workf.file()
        sdata = self.workf.file()

        # convert the data for sketchport
        # mkdata
        xx1 = nm.mnumber(S=0, a=ln, q=True, i=self.iFile)
        if self.wfH:
            xx2 = nm.mcut(f=self.wfH + self.tidH + self.elem, i=xx1)
        else:
            self.wfH = ["{}wf".format(self.pt)]
            xx2 = nm.msetstr(v=0, a=self.wfH, i=xx1)
            xx2 <<= nm.mcut(f=self.wfH + self.tidH + self.elem)

        fmap = nm.mcut(f=[ln] + self.tidH, i=xx1, o=xxmap)
        xx2 <<= nm.mcut(f=self.wfH + self.elem, nfno=True)
        xx2 <<= nm.cmd("tr ',' ' '")
        xx2 <<= nm.mwrite(o=sdata)
        nm.runs([fmap, xx2])

        # do sort
        outf = self.workf.file()
        para = {}
        if self.dist == "C":
            para["cosdist"] = self.th
        elif self.dist == "H":
            para["hamdist"] = self.th

        if not self.uc:
            para["centering"] = True

        para["auto"] = True
        para["windowsize"] = self.ws
        para["seed"] = self.seed
        para["missingratio"] = self.mr
        para["i"] = sdata
        para["o"] = outf
        status = extMining.sketchsort(para)
        if status:
            raise Exception("#ERROR# checking sketchsort messages")
        tmp = []
        for val in self.tidH:
            tmp.append("{}:{}2".format(val, val))
        tid2 = ",".join(tmp)

        f = nm.mread(i=outf)
        f <<= nm.cmd("tr ' ' ',' ")
        f <<= nm.mcut(nfni=True, f="0:eline1,1:eline2,2:distance")
        f <<= nm.mfsort(f="eline*")
        # 行番号に対応するtidを取得
        f <<= nm.mjoin(k="eline1",
                       K="{}line".format(self.pt),
                       f=self.tidH,
                       m=xxmap)
        f <<= nm.mjoin(k="eline2", K="{}line".format(self.pt), f=tid2, m=xxmap)
        f <<= nm.msortf(f="eline1%n,eline2%n")
        f <<= nm.mcut(r=True, f="eline1,eline2")
        f <<= nm.msortf(f=self.tidH)
        f <<= nm.mfldname(q=True, o=self.oFile)
        f.run()
        nu.mmsg.endLog(self.__cmdline())
Beispiel #7
0
    def run(self):
        temp = mtemp.Mtemp()

        ### mtra2gc
        xxsimgN = temp.file()
        xxsimgE = temp.file()
        xxsimgE0 = temp.file()

        param = {}
        param["i"] = self.iFile
        if self.idFN:
            param["tid"] = self.idFN
        if self.itemFN:
            param["item"] = self.itemFN
        if self.sp1:
            param["s"] = self.sp1
        if self.sp2:
            param["S"] = self.sp2

        #####################
        # 異なる向きのconfidenceを列挙するためにsim=C th=0として双方向列挙しておく
        # 出力データは倍になるが、mfriendsで-directedとすることで元が取れている
        param["sim"] = "C"
        param["th"] = "0"

        param["node_support"] = True
        if self.numtp:
            param["num"] = True
        param["no"] = xxsimgN
        param["eo"] = xxsimgE0

        nt.mtra2gc(**param).run()

        f = nm.readcsv(xxsimgE0)
        for i in range(self.filterSize):
            f <<= nm.mselnum(f=self.filter[i],
                             c="[%s,%s]" % (self.lb[i], self.ub[i]))
        f <<= nm.writecsv(xxsimgE)
        f.run()

        ### mfrirends
        xxfriends = temp.file()
        xxfriendE = temp.file()
        xxw = temp.file()
        xxf = temp.file()
        xxff = temp.file()
        xxor = temp.file()

        if not os.path.isdir(xxfriends):
            os.makedirs(xxfriends)
        col = [["FF000080", "FF888880"], ["0000FF80", "8888FF80"],
               ["00FF0080", "88FF8880"]]

        for i in range(len(self.sim)):
            paramf = {}
            paramf["ei"] = xxsimgE
            paramf["ni"] = xxsimgN
            paramf["ef"] = "node1,node2"
            paramf["nf"] = "node"
            paramf["eo"] = xxfriendE
            paramf["no"] = xxfriends + "/n_" + str(i)
            paramf["sim"] = self.sim[i]
            paramf["dir"] = self.dir[i]
            paramf["rank"] = self.rank[i]
            paramf["directed"] = True

            nt.mfriends(**paramf).run()

            frec2 = nm.mfsort(f="node1,node2", i=xxfriendE)
            frec2 <<= nm.msummary(k="node1,node2",
                                  f=self.sim[i],
                                  c="count,mean")
            frec2 <<= nm.mselstr(f="count", v=2)
            # node1%0,node2%1,fld,count,mean
            # a,b,support,2,0.1818181818
            # a,d,support,2,0.1818181818

            f = nm.mjoin(k="node1,node2",
                         K="node1,node2",
                         m=frec2,
                         f="mean:s1",
                         n=True,
                         i=xxfriendE)
            f <<= nm.mjoin(k="node2,node1",
                           K="node1,node2",
                           m=frec2,
                           f="mean:s2",
                           n=True)
            # 1) xxrecs2でsimをjoinできない(s1,s2共にnull)ということは、それは片方向枝なので"F"をつける
            # 2) 双方向枝a->b,b->aのうちa->bのみ(s1がnullでない)に"W"の印をつける。
            # 3) それ以外の枝は"D"として削除
            f <<= nm.mcal(
                c='if(isnull($s{s1}),if(isnull($s{s2}),\"F\",\"D\"),\"W\")',
                a="dir")
            f <<= nm.mselstr(f="dir", v="D", r=True)
            f <<= nm.mcal(c='if($s{dir}==\"W\",$s{s1},$s{%s})' % (self.sim[i]),
                          a="sim")
            f <<= nm.mchgstr(f="dir:color",
                             c='W:%s,F:%s' % (col[i][0], col[i][1]),
                             A=True)
            f <<= nm.msetstr(v=[self.sim[i], str(i)], a="simType,simPriority")
            f <<= nm.mcut(f="simType,simPriority,node1,node2,sim,dir,color",
                          o=xxfriends + "/e_" + str(i))
            f.run()
            # node1%1,node2%0,simType,sim,dir,color
            # b,a,jaccard,0.3333333333,F,8888FF
            # j,c,jaccard,0.3333333333,F,8888FF
            # b,d,jaccard,0.3333333333,F,8888FF
            # a,e,jaccard,0.5,W,0000FF
            # d,e,jaccard,0.5,W,0000FF

        # rule fileの出力
        if self.orFile:
            mmm = nm.mcat(i=xxfriends + "/e_*").muniq(k="node1,node2")
            nm.mcommon(k="node1,node2", i=xxsimgE, m=mmm, o=self.orFile).run()

        # マルチ枝の単一化(W優先,パラメータ位置優先)
        if self.prune:
            """
			# 双方向と片方向に分割
			nm.mcat(i=xxfriends+"/e_*").mselstr(f="dir",v="W",o=xxw,u=xxf).run()
			# 片方向のみの枝を選択
			f =   nm.mcommon(k="node1,node2",K="node1,node2",r=True,m=xxw,i=xxf)
			f <<= nm.mcommon(k="node1,node2",K="node2,node1",r=True,m=xxw,o=xxff)
			f.run()
			f = nm.mcat(i=xxw+","+xxff).mbest(k="node1,node2",s="dir%r,simPriority%n",o=self.oeFile).run()
			"""
            #これだめ
            fo = nm.mcat(i=xxfriends + "/e_*").mselstr(f="dir", v="W")
            fu = fo.direction("u")  # これは再考
            fu <<= nm.mcommon(k="node1,node2", K="node1,node2", r=True, m=fo)
            fu <<= nm.mcommon(k="node1,node2", K="node2,node1", r=True, m=fo)
            #f  =   nm.m2cat()
            f = nm.mbest(i=[fo, fu],
                         k="node1,node2",
                         s="dir%r,simPriority%n",
                         o=self.oeFile)

            f.run()

        else:
            nm.mcat(i=xxfriends + "/e_*", o=self.oeFile).run()

        nm.mcat(i=xxfriends + "/n_0", o=self.onFile).run()
Beispiel #8
0
def mnetpie(ei,
            ni,
            ef,
            nf,
            o,
            nodeSizeFld=None,
            nodeTipsFld=None,
            nodeColorFld=None,
            edgeWidthFld=None,
            edgeColorFld=None,
            pieDataFld=None,
            pieTipsFld=None,
            picFld=None,
            undirect=False,
            offline=False):

    #ei:edge file
    #ef:egfile
    if type(ef) is str:
        ef = ef.split(',')
    if len(ef) != 2:
        raise Exception("ef= takes just two field names")

    if not ((pieDataFld == None and pieTipsFld == None) or
            (pieDataFld != None and pieTipsFld != None)):
        raise Exception(
            "pieDataFld= pieTipsFld= are necessary at the same time")

    if picFld != None and pieDataFld != None:
        raise Exception(
            "picFld= cannot be specified with pieDataFld= pieTipsFld=")

    if nodeColorFld != None:
        if picFld != None or pieDataFld != None or pieTipsFld != None:
            raise Exception(
                "nodeColorFld= cannot be specified with pieDataFld= pieTipsFld= picFld="
            )

    if pieDataFld != None and pieTipsFld != None:
        caseNo = 1
    elif picFld != None:
        caseNo = 2
    else:
        caseNo = 0

    tempW = mtemp.Mtemp()

    xxnode = tempW.file()

    nodefld = []
    nodedmy1 = []
    nodedmy2 = []

    nodefld.append("%s:node" % (nf))
    if nodeSizeFld != None:
        nodefld.append("%s:nodesize" % (nodeSizeFld))
    else:
        nodedmy1.append("nodesize")
        nodedmy2.append("50")

    if nodeTipsFld != None:
        nodefld.append("%s:nodeT" % (nodeTipFld))
    else:
        nodedmy1.append("nodeT")
        nodedmy2.append("")

    if nodeColorFld != None:
        nodefld.append("%s:nodeClr" % (nodeColorFld))
    else:
        nodedmy1.append("nodeClr")
        nodedmy2.append("skyblue")

    if caseNo == 1:
        nodefld.append("%s:pieD" % (pieDataFld))
        nodefld.append("%s:pieT" % (pieTipsFld))
    elif caseNo == 2:
        nodefld.append("%s:pic" % (picFld))
    else:
        nodedmy1.append("pic")
        nodedmy2.append("")

    f1 = None
    f1 <<= nm.mcut(i=ni, f=nodefld)
    if len(nodedmy1) != 0:
        f1 <<= nm.msetstr(a=nodedmy1, v=nodedmy2)

    if caseNo == 1:
        f1 <<= nm.mshare(k="node", f="pieD:pieDS")
        f1 <<= nm.mnumber(k="node", a="nodeid", B=True)

        f2 = nm.muniq(k="pieT", i=f1)
        f2 <<= nm.mnumber(q=True, a="pieTno")
        f2 <<= nm.mjoin(k="pieT", f="pieTno", i=f1).iredirect("m")
        f2 <<= nm.msortf(f="nodeid%n,pieTno%n", o=xxnode)
    else:
        f2 = nm.mnumber(a="nodeid%n", q=True, i=f1, o=xxnode)

    f2.run()

    xxedge = tempW.file()
    # MAKE EDGE DATA
    edgefld = []
    edgedmy1 = []
    edgedmy2 = []
    edgefld.append("%s:edgeS" % (ef[0]))
    edgefld.append("%s:edgeE" % (ef[1]))

    if edgeWidthFld != None:
        edgefld.append("%s:edgesize" % (edgeWidthFld))
    else:
        edgedmy1.append("edgesize")
        edgedmy2.append("1")

    if edgeColorFld != None:
        edgefld.append("%s:edgecolor" % (edgeColorFld))
    else:
        edgedmy1.append("edgecolor")
        edgedmy2.append("black")

    f3 = None
    f3 <<= nm.mcut(i=ei, f=edgefld)
    if len(edgedmy1) != 0:
        f3 <<= nm.msetstr(a=edgedmy1, v=edgedmy2)

    f3 <<= nm.mnumber(a="preNo", q=True)
    f3 <<= nm.mbest(k="edgeS,edgeE", s="preNo%nr")
    f3 <<= nm.mnumber(s="preNo%n", a="edgeID")
    f3 <<= nm.mjoin(k="edgeS", K="node", f="nodeid:edgeSid", m=xxnode)
    f3 <<= nm.mjoin(k="edgeE", K="node", f="nodeid:edgeEid", m=xxnode)

    #双方向チェック一応
    f4 = None
    f4 <<= nm.mfsort(i=f3, f="edgeS,edgeE")
    f4 <<= nm.mcount(k="edgeS,edgeE", a="edgecnt")
    f4 <<= nm.mselnum(c="[2,]", f="edgecnt")
    f4 <<= nm.msetstr(a="biflg", v=1)
    f4 <<= nm.mjoin(k="edgeID", f="biflg", n=True, i=f3).iredirect("m")
    f4 <<= nm.msortf(f="edgeID%n", o=xxedge)
    f4.run()

    gdata = "{\"nodes\":["
    if caseNo == 1:
        nodedatastk = []
        nodedatas = ""
        for val, top, bot in nm.readcsv(xxnode).getline(k="nodeid",
                                                        otype='dict',
                                                        q=True):
            name = val["node"]
            r = val["nodesize"]
            title = val["nodeT"]
            if top:
                nodedatas = "{\"name\":\"%s\",\"title\":\"%s\",\"r\":%s,\"node\":[" % (
                    name, title, r)

            pieTno = val["pieTno"]
            pieT = val["pieT"]
            pieDS = val["pieDS"]
            nodedatas += "{\"group\":%s,\"color\":%s,\"value\":%s,\"title\":\"%s\"}" % (
                pieTno, pieDS, pieDS, pieT)

            if bot:
                nodedatas += "]}"
                nodedatastk.append(nodedatas)
                nodedatas = ""
            else:
                nodedatas += ","

        gdata += ",".join(nodedatastk)

    else:
        nodedatastk = []
        for val in nm.readcsv(xxnode).getline(otype='dict'):
            name = val["node"]
            r = val["nodesize"]
            title = val["nodeT"]
            pic = val["pic"]
            nclr = val["nodeClr"]
            nodedatas = "{\"name\":\"%s\",\"title\":\"%s\",\"pic\":\"%s\",\"color\":\"%s\",\"r\":%s}" % (
                name, title, pic, nclr, r)
            nodedatastk.append(nodedatas)

        gdata += ",".join(nodedatastk)

    gdata += "],\"links\": ["

    edgedatastk = []
    for val in nm.readcsv(xxedge).getline(otype='dict'):
        es = val["edgeSid"]
        et = val["edgeEid"]
        esize = val["edgesize"]
        ecolor = val["edgecolor"]
        edgedatas = "{\"source\":%s,\"target\":%s,\"length\":500,\"ewidth\":%s,\"color\":\"%s\"}" % (
            es, et, esize, ecolor)
        edgedatastk.append(edgedatas)

    gdata += ','.join(edgedatastk)

    gdata += "]}"

    direct = ".attr('marker-end','url(#arrowhead)')"
    if undirect:
        direct = ""

    nodeTemplate = '''
    node
			.append("circle")
			.attr("r",function(d){return d.r/4;})
			.attr("fill", function(d){return d.color;})
			.append("title")
			.text(function(d){return d.title;})
	'''
    nodemakeTemplate = '''
	for(var i=0 ; i< graph.nodes.length;i++){
		graph.nodes[i].id = i
	}
	'''

    if pieDataFld != None:
        nodeTemplate = ''' 
    node.selectAll("path")
        .data( function(d, i){
          return pie(d.node);
				})
        .enter()
        .append("svg:path")
        .attr("d", arc)
        .attr("fill", function(d, i) {
					return color(d.data.group);
				})
				.append("title")
				.text(function(d){{return d.data.title;}})

        node.append("circle")
				.attr("r",function(d){{return d.r/4;}})
				.attr({
					'fill': 'white'
				})
				.append("title")
				.text(function(d){{return d.title;}});
		'''
        nodemakeTemplate = '''
			for(var i=0 ; i< graph.nodes.length;i++){
			var r = graph.nodes[i].r
			for(var j=0 ; j< graph.nodes[i].node.length;j++){
				graph.nodes[i].node[j]['r'] = r
			}
			graph.nodes[i].id = i
		}
		'''
    elif picFld != None:
        nodeTemplate = '''
    node
			.append("image")
			.attr("height",function(d){return d.r;})
			.attr("width",function(d){return d.r;})
			.attr("x",function(d){return -1 * d.r/2; })
			.attr("y",function(d){return -1 * d.r/2; })
			.attr("xlink:href",function(d){return d.pic; })
			.append("title")
			.text(function(d){return d.title;})
		'''

    d3js_str = "<script type='text/javascript' src='http://d3js.org/d3.v3.min.js'></script>"

    if offline:
        d3js_str = "<script>%s<script>" % (vjs.ViewJs.d3jsMin())

    outTemplate = '''
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	{d3js_str}
  <style></style>
</head>
<body>
<script type="text/javascript">
	var graph = {gdata} ;

  var width = 4000,
      height = 3000;

	var color = d3.scale.category10();
    
	{nodemakeTemplate};

	for(var i=0 ; i< graph.links.length;i++){{
		graph.links[i].id = i
	}}

	var pie = d3.layout.pie()
        .sort(null)
        .value(function(d) {{ return d.value; }});

	var arc = d3.svg.arc()
       	.outerRadius( function(d){{ return d.data.r ; }})
        .innerRadius( function(d){{ return d.data.r/2 ; }} );
		
	var svg = d3.select("body").append("svg")
		.attr("width", width)
		.attr("height", height);

	d3.select("svg").append('defs').append('marker')
		.attr({{'id':'arrowhead',
						'viewBox':'-0 -5 10 10',
						'refX':30,
						'refY':0,
						'orient':'auto-start-reverse',
						'markerWidth':5,
						'markerHeight':5,
						'xoverflow':'visible'}})
		.append('path')
		.attr('d', 'M 0,-5 L 10 ,0 L 0,5')
		.attr('fill', '#999')
		.style('stroke','none');
            
	var g = svg.append("g");
	var node = g.selectAll(".node");
	var link = g.selectAll(".link");
	nodes = graph.nodes
  links = graph.links

	var force = 
		d3.layout.force()
			.linkDistance(200)
			.linkStrength(3.5)
      .charge(-3500)
			.gravity(0.1)
			.friction(0.95)
      .size([width, height])
			.on("tick", function() {{
				link
					.attr("x1", function(d) {{ return d.source.x; }})
					.attr("y1", function(d) {{ return d.source.y; }})
					.attr("x2", function(d) {{ return d.target.x; }})
					.attr("y2", function(d) {{ return d.target.y; }});

				node
					.attr("x", function(d) {{ return d.x; }})
					.attr("y", function(d) {{ return d.y; }})
					.attr("transform", function(d) {{ return "translate(" + d.x + "," + d.y + ")"}});	
	    }});


		node = node.data(nodes, function( d ) {{ return d.id; }} );
		link = link.data(links, function( d ) {{ return d.id; }} );


    link
      .enter()
      .append("line")
      .attr("class", "link")
			.style("stroke", function( d ) {{ return d.color; }} )
			.style("stroke-width", function( d ) {{ return d.ewidth; }})
			{direct}


    node
    	.enter()
			.append("g")
      .attr("class", "node")
			.style({{}})
			.call(force.drag)
			.on("contextmenu", function(nd) {{
					d3.event.preventDefault();
					force.stop()
				 	nodes.splice( nd.index, 1 );
					links = links.filter(function(nl) {{
						return nl.source.index != nd.index && nl.target.index != nd.index;					
					}});
					node = node.data(nodes, function( d ) {{ return d.id; }} );
					node.exit().remove();
					link = link.data( links, function( d ) {{ return d.id; }} );
					link.exit().remove();
			    force.nodes(nodes)
      	   .links(links)
        	 .start();
				}});  
	
		{nodeTemplate}


    node
      .append("text")
      .attr("text-anchor", "middle")
			.style("stroke", "black")
      .text(function(d) {{ return d.name; }});

    force.nodes(nodes)
         .links(links)
         .start();


</script>
</body>
</html>
	'''.format(d3js_str=d3js_str,
            gdata=gdata,
            nodemakeTemplate=nodemakeTemplate,
            direct=direct,
            nodeTemplate=nodeTemplate)

    html = sys.stdout
    if not o == None:
        html = open(o, "w")

    html.write(outTemplate)

    if not o == None:
        html.close()
Beispiel #9
0
    def run(self):
        wf = mtemp.Mtemp()
        xxpal = wf.file()
        xxa = wf.file()
        xxb = wf.file()
        xxc = wf.file()
        xxd = wf.file()
        xxout = wf.file()

        # ============
        # n1,n2,sim
        # a,b,0.40
        # a,c,0.31
        # a,d,0.22
        # b,c,0.20
        # b,d,0.24
        # b,e,0.14
        # c,d,0.30
        # d,e,0.09
        xpal = None
        if self.directed:
            # 任意の枝a->bのaについて上位rankを選択
            xpal <<= nm.mnumber(k=self.ef1,
                                s=self.sim + "%nr",
                                e="skip",
                                S=1,
                                a="##rank",
                                i=self.ei)
            xpal <<= nm.mselnum(f="##rank", c="[," + str(self.rank) + "]")
        else:
            xxa = nm.mfsort(f=[self.ef1, self.ef2], i=self.ei)
            xxb = nm.mfsort(f=[self.ef2, self.ef1], i=self.ei)
            xpal <<= nm.muniq(k=[self.ef1, self.ef2], i=[xxa, xxb])
            xpal <<= nm.mnumber(k=self.ef1,
                                s=self.sim + "%nr",
                                e="skip",
                                S=1,
                                a="##rank")
            xpal <<= nm.mselnum(f="##rank", c="[," + str(self.rank) + "]")

        # 両方向+片方向
        xpal1 = None
        if self.dir == "x":
            xpal1 <<= nm.mcut(f=[self.ef1, self.ef2, self.sim], i=xpal)
        # 両方向
        elif self.dir == "b":
            selpara = "$s{%s}==$s{##ef2}" % (self.ef1)
            # 得られた上位rankグラフからa->b->cを作成し、a==cであれば相思相愛ということ
            xpal1 <<= nm.mnjoin(k=self.ef2,
                                K=self.ef1,
                                m=xpal,
                                f=self.ef2 + ":##ef2," + self.sim + ":sim2",
                                i=xpal)
            xpal1 <<= nm.msel(c=selpara)
            xpal1 <<= nm.mcut(f=[self.ef1, self.ef2, self.sim])
        else:
            selpara = "$s{%s}==$s{##ef2}" % (self.ef1)
            xxc = None
            xxc <<= nm.mnjoin(k=self.ef2,
                              K=self.ef1,
                              m=xpal,
                              f=self.ef2 + ":##ef2," + self.sim + ":sim2",
                              i=xpal)
            xxc <<= nm.msel(c=selpara)
            xxc <<= nm.mcut(f=[self.ef1, self.ef2])
            xpal1 <<= nm.mcut(f=[self.ef1, self.ef2, self.sim], i=xpal)
            xpal1 <<= nm.mcommon(k=self.ef1 + "," + self.ef2, m=xxc, r=True)

        runpal = None
        kpara = "%s,%s" % (self.ef1, self.ef2)
        if self.udout:
            runpal <<= nm.mfsort(f=kpara, i=xpal1)
            runpal <<= nm.mavg(k=kpara, f=self.sim)
            runpal <<= nm.msortf(f=kpara, o=self.eo)
        else:
            runpal <<= nm.msortf(f=kpara, i=xpal1, o=self.eo)

        runpal.run()

        if self.ni and self.no:
            shutil.copyfile(self.ni, self.no)