コード例 #1
0
ファイル: mpie.py プロジェクト: nysol/viewpy
def __makeData(iFile, legendKey, barValue, keys):

    rtn = dataForGraphDsp()

    kvstr = []

    for flds, top, bot in nm.readcsv(iFile).getline(k=keys,
                                                    otype='dict',
                                                    q=True):

        if top == True:
            keyhead = []
            keyfld = []
            for key in keys:
                keyhead.append("\"%s\":\"%s\"" % (key, flds[key]))
                keyfld.append(flds[key])

            if len(keyhead) > 0:
                kvstr.append(",".join(keyhead))

            rtn.addAxis(keyfld)

        rtn.calMaxLeg(flds[legendKey])
        rtn.addKeyLabel(flds[legendKey])
        rtn.checkValue(flds[barValue])

        kvstr.append("\"_%s\":\"%s\"" % (flds[legendKey], flds[barValue]))

        if bot == True:

            rtn.addData("{" + ",".join(kvstr) + "}")
            kvstr = []

    return rtn
コード例 #2
0
def __dotEdge(iFile, oPath):
    '''
	# key,nam1,nam2%0,keyNum,num1,num2,ev,evv
	# #2_1,#1_1,#1_2,4,1,2,0.2727272727,20
	# #1_1,a,b,1,5,6,0.1818181818,0
	'''

    block = ""
    for flds, top, bot in nm.readcsv(iFile).getline(k="keyNum", otype="dict"):

        num1 = flds["num1"]
        num2 = flds["num2"]
        el = flds["el"]
        ev = flds["ev"]
        ec = flds["ec"]
        ed = flds["ed"]
        leaf1 = flds["leaf1"]
        leaf2 = flds["leaf2"]

        if leaf1 == None or leaf1 == "":
            prefix1 = "cluster"
        else:
            prefix1 = "n"

        if leaf2 == None or leaf2 == "":
            prefix2 = "cluster"
        else:
            prefix2 = "n"

        e1Str = "%s_%s" % (prefix1, num1)
        e2Str = "%s_%s" % (prefix2, num2)

        attrStr = ""
        if el:
            attrStr += 'label="%s" ' % (el)
        if ev:
            attrStr += 'style="setlinewidth(%s)" ' % (ev)
        if ec:
            attrStr += 'color="#%s" ' % (ec)

        if ed:
            if ed == "F":
                attrStr += "dir=forward "
            elif ed == "B":
                attrStr += "dir=back "
            elif ed == "W":
                attrStr += "dir=both "
            elif ed == "N":
                attrStr += "dir=none "

        block += "%s -> %s [%s]\n" % (e1Str, e2Str, attrStr)
        if bot:
            with open("%s/c_%s" % (oPath, flds["keyNum"]), "w") as fpw:
                fpw.write(block)
            block = ""
コード例 #3
0
def __dotTree(iFile, depth, header, footer, oFile):

    with open(oFile, "w") as fpw:
        fpw.write(header)
        fpw.write("##0\n")  # 孤立node(keyがnullのnode)
        oldFlds = None
        stack = []  # "subgraph {"に対応する終了括弧"}"のスタック
        lastDepth = 0  # 前行で出力されたsubgraphの深さ

        for newFlds in nm.readcsv(iFile):

            if newFlds[0] == "0":
                continue

            kbd = __keyBreakDepth(newFlds, oldFlds)  # 前行に比べてどの位置でkeybreakがあったか

            for i in range(0, lastDepth - kbd):
                fpw.write("%s\n" % (stack.pop()))

            for i in range(kbd, depth):

                if newFlds[i] == "":  # nullはその深さにsubgraphなしということ
                    break

                indent = '  ' * (i + 1)  # インデント

                fpw.write("%ssubgraph cluster_%s {\n" % (indent, newFlds[i]))
                fpw.write("##%s\n" % (newFlds[i]))
                stack.append("%s}" % (indent))  # 対応する終了括弧をスタックしておく
                lastDepth = i + 1  # 出力した最深位置の更新

            oldFlds = newFlds

        # 深さが戻った分終了括弧"}"を出力
        for i in range(lastDepth):
            fpw.write("%s\n" % (stack.pop()))

        fpw.write("%s\n" % (footer))
コード例 #4
0
def mautocolor(iFile,
               oFile,
               fld,
               aFld,
               color="category",
               order="alpha",
               transmit=None):

    color = Color(fld, iFile, color, order)
    head = nu.mheader(i=iFile)
    head.append(aFld)
    with _nu.mcsvout(o=oFile, f=head) as oCSV:

        for flds in nm.readcsv(iFile).getline(otype="dict"):
            colorStr = color.getRGB(flds[fld])
            if colorStr != "" and transmit:
                flds[aFld] = "%s%s" % (colorStr, transmit)
            else:
                flds[aFld] = colorStr
            dt = []
            for hname in head:
                dt.append(flds[hname])
            oCSV.write(dt)
コード例 #5
0
ファイル: msankey.py プロジェクト: nysol/viewpy
def msankey(i, o, v, f, title="", h=500, w=960, nl=False, T=None):

    # f= 2 fld
    if type(f) is str:
        ff = f.split(',')
    elif type(f) is list:
        ff = f
    else:
        raise TypeError("f= unsupport " + str(type(k)))

    if len(ff) < 2:
        raise TypeError("f= takes just two field names")

    if T != None:
        import re
        os.environ["KG_TmpPath"] = re.sub(r'/$', "", T)

    if h == None:
        h = 500

    if w == None:
        w = 960

    if title == None:
        title = ""

    tempW = mtemp.Mtemp()
    nodef = tempW.file()
    edgef = tempW.file()

    ef1 = ff[0]
    ef2 = ff[1]
    ev = v

    iFile = i
    oFile = o

    f0 = nm.mcut(i=iFile, f="%s:nodes" % (ef1))
    f1 = nm.mcut(i=iFile, f="%s:nodes" % (ef2))
    f2 = None
    f2 <<= nm.muniq(i=[f0, f1], k="nodes")
    f2 <<= nm.mnumber(s="nodes", a="num", o=nodef)
    f2.run()

    f3 = None
    f3 <<= nm.mcut(f="%s:nodes1,%s:nodes2,%s" % (ef1, ef2, ev), i=iFile)
    f3 <<= nm.mjoin(k="nodes1", K="nodes", m=nodef, f="num:num1")
    f3 <<= nm.mjoin(k="nodes2", K="nodes", m=nodef, f="num:num2")
    f3 <<= nm.mcut(f="num1,num2,%s" % (ev))
    f3 <<= nm.msortf(f="num1%n,num2%n", o=edgef)
    f3.run()

    wk = []
    nodeL = []

    for flds in nm.readcsv(nodef).getline(otype='dict'):
        nodeL.append({"name": flds['nodes']})

    nodes = json.JSONEncoder().encode(nodeL)

    linkL = []
    for flds in nm.readcsv(edgef).getline(otype='dict',
                                          dtype={
                                              "num1": "int",
                                              "num2": "int",
                                              ev: "int"
                                          }):
        linkL.append({
            "source": flds["num1"],
            "target": flds["num2"],
            "value": flds[ev]
        })

    links = json.JSONEncoder().encode(linkL)

    nolabel = ""
    if nl:
        nolabel = "font-size: 0px;"

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

    outTemplate = '''
<!DOCTYPE html>
<html class="ocks-org do-not-copy">
<meta charset="utf-8">
<title>{title}</title>
<style>
body {{
    font: 10px sans-serif;
}}
svg {{
    padding: 10px 0 0 10px;
}}
.arc {{
    stroke: #fff;
}}
#tooltip {{
position: absolute;
width: 150px;
height: auto;
padding: 10px;
background-color: white;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
-webkit-box-shadow: 4px 4px 10px rgba(0,0,0,0.4);
-moz-box-shadow: 4px 4px 10px rgba(0,0,0,0.4);
box-shadow: 4px 4px 10px rgba(0,0,0,0.4);
pointer-events: none;
}}
#tooltip.hidden {{
display: none;
}}
#tooltip p {{
margin: 0;
font-family: sans-serif;
font-size: 10px;
line-height: 14px;
}}
#chart {{
height: 500px;
}}
.node rect {{
    cursor: move;
    fill-opacity: .9;
    shape-rendering: crispEdges;
}}
.node text {{
    pointer-events: none;
    text-shadow: 0 1px 0 #fff;
    {nolabel}
}}
.link {{
    fill: none;
    stroke: #000;
    stroke-opacity: .2;
}}
.link:hover {{
    stroke-opacity: .5;
}}
</style>
<body>
<h1>{title}</h1>
<p id="chart">
<script>
	{d3js_str}
	d3.sankey = function() {{
		var sankey = {{}},
		nodeWidth = 24,
		nodePadding = 8,
		size = [1, 1],
		nodes = [],
		links = [];

		sankey.nodeWidth = function(_) {{
			if (!arguments.length) return nodeWidth;
			nodeWidth = +_;
			return sankey;
		}};

		sankey.nodePadding = function(_) {{
			if (!arguments.length) return nodePadding;
			nodePadding = +_;
			return sankey;
		}};

		sankey.nodes = function(_) {{
			if (!arguments.length) return nodes;
			nodes = _;
			return sankey;
		}};
		sankey.links = function(_) {{
			if (!arguments.length) return links;
			links = _;
			return sankey;
		}};
		sankey.size = function(_) {{
			if (!arguments.length) return size;
			size = _;
			return sankey;
		}};
	
		sankey.layout = function(iterations){{
			computeNodeLinks();
			computeNodeValues();
			computeNodeBreadths();
			computeNodeDepths(iterations);
			computeLinkDepths();
			return sankey;
		}};

		sankey.relayout = function() {{
			computeLinkDepths();
			return sankey;
		}};

		sankey.link = function() {{
			var curvature = .5;
			function link(d) {{
				var x0 = d.source.x + d.source.dx,
				x1 = d.target.x,
				xi = d3.interpolateNumber(x0, x1),
				x2 = xi(curvature),
				x3 = xi(1 - curvature),
				y0 = d.source.y + d.sy + d.dy / 2,
				y1 = d.target.y + d.ty + d.dy / 2;
				return "M" + x0 + "," + y0
				+ "C" + x2 + "," + y0
				+ " " + x3 + "," + y1
				+ " " + x1 + "," + y1;
			}}
			link.curvature = function(_) {{
				if (!arguments.length) return curvature;
				curvature = +_;
				return link;
			}};
			return link;
		}};

		// Populate the sourceLinks and targetLinks for each node.
		// Also, if the source and target are not objects, assume they are indices.
		function computeNodeLinks() {{
			nodes.forEach(function(node) {{
				node.sourceLinks = [];
				node.targetLinks = [];
			}});

			links.forEach(function(link) {{
				var source = link.source,
				target = link.target;
				if (typeof source === "number") source = link.source = nodes[link.source];
				if (typeof target === "number") target = link.target = nodes[link.target];
				source.sourceLinks.push(link);
				target.targetLinks.push(link);
			}});
		}}

		// Compute the value (size) of each node by summing the associated links.
		function computeNodeValues() {{
			nodes.forEach( function(node) {{
				node.value = Math.max(d3.sum(node.sourceLinks, value),d3.sum(node.targetLinks, value));
			}} );
 		}}
 		
		// Iteratively assign the breadth (x-position) for each node.
		// Nodes are assigned the maximum breadth of incoming neighbors plus one;
		// nodes with no incoming links are assigned breadth zero, while
		// nodes with no outgoing links are assigned the maximum breadth.
		function computeNodeBreadths() {{
			var remainingNodes = nodes,
					nextNodes,
					x = 0;
			while (remainingNodes.length) {{
				nextNodes = [];
				remainingNodes.forEach(function(node) {{
					node.x = x;
					node.dx = nodeWidth;
					node.sourceLinks.forEach(function(link) {{
						nextNodes.push(link.target);
					}});
				}});
				remainingNodes = nextNodes;
				++x;
			}}
			//
			moveSinksRight(x);
			scaleNodeBreadths((width - nodeWidth) / (x - 1));
		}}
		
		function moveSourcesRight() {{
			nodes.forEach(function(node) {{
				if (!node.targetLinks.length) {{
					node.x = d3.min(node.sourceLinks, function(d) {{ return d.target.x; }} ) - 1;
				}}
			}});
		}}
	
		function moveSinksRight(x) {{
			nodes.forEach(function(node) {{
				if (!node.sourceLinks.length) {{
					node.x = x - 1;
				}}
			}});
		}}

		function scaleNodeBreadths(kx) {{
			nodes.forEach(function(node) {{
				node.x *= kx;
			}});
		}}



		function computeNodeDepths(iterations) {{
			var nodesByBreadth = d3.nest()
													.key(function(d) {{ return d.x; }})
													.sortKeys(d3.ascending)
													.entries(nodes)
													.map(function(d) {{ return d.values; }});
                                                                
			//
			initializeNodeDepth();
			resolveCollisions();

			for (var alpha = 1; iterations > 0; --iterations){{
				relaxRightToLeft(alpha *= .99);
				resolveCollisions();
				relaxLeftToRight(alpha);
				resolveCollisions();
			}}
                                                                
			function initializeNodeDepth() {{
				var ky = d3.min(nodesByBreadth, function(nodes) {{
					return (size[1] - (nodes.length - 1) * nodePadding) / d3.sum(nodes, value);
				}});
				nodesByBreadth.forEach(function(nodes) {{
					nodes.forEach(function(node, i) {{
						node.y = i;
						node.dy = node.value * ky;
					}});
				}});
				links.forEach(function(link) {{
					link.dy = link.value * ky;
				}});
			}}
		
			function relaxLeftToRight(alpha) {{
				nodesByBreadth.forEach(function(nodes, breadth) {{
					nodes.forEach(function(node) {{
						if (node.targetLinks.length) {{
							var y = d3.sum(node.targetLinks, weightedSource) / d3.sum(node.targetLinks, value);
							node.y += (y - center(node)) * alpha;
						}}
					}});
				}});
			
				function weightedSource(link) {{
					return center(link.source) * link.value;
				}}
			}}
		
			function relaxRightToLeft(alpha) {{
				nodesByBreadth.slice().reverse().forEach(function(nodes){{
					nodes.forEach(function(node) {{
						if (node.sourceLinks.length) {{
							var y = d3.sum(node.sourceLinks, weightedTarget) / d3.sum(node.sourceLinks, value);
							node.y += (y - center(node)) * alpha;
						}}
					}});
				}});

				function weightedTarget(link) {{
					return center(link.target) * link.value;
				}}
			}}
		
			function resolveCollisions() {{
				
				nodesByBreadth.forEach(function(nodes) {{
					var node, dy, y0 = 0,
						n = nodes.length, i;
					// Push any overlapping nodes down.
					nodes.sort(ascendingDepth);
					for (i = 0; i < n; ++i) {{
						node = nodes[i];
						dy = y0 - node.y;
						if (dy > 0) node.y += dy;
						y0 = node.y + node.dy + nodePadding;
					}}
					// If the bottommost node goes outside the bounds, push it back up.
					dy = y0 - nodePadding - size[1];
					if (dy > 0) {{
						y0 = node.y -= dy;
						// Push any overlapping nodes back up.
						for (i = n - 2; i >= 0; --i) {{
							node = nodes[i];
							dy = node.y + node.dy + nodePadding - y0;
							if (dy > 0) node.y -= dy;
							y0 = node.y;
						}}
					}}
				}});
			}}
			function ascendingDepth(a, b) {{ return a.y - b.y; }}
		}}

		function computeLinkDepths() {{

			nodes.forEach(function(node) {{
				node.sourceLinks.sort(ascendingTargetDepth);
				node.targetLinks.sort(ascendingSourceDepth);
			}});
	
			nodes.forEach(function(node) {{
				var sy = 0, ty = 0;
				node.sourceLinks.forEach(function(link) {{
					link.sy = sy;
					sy += link.dy;
				}});
				node.targetLinks.forEach(function(link) {{
					link.ty = ty;
					ty += link.dy;
				}});
			}});
	
			function ascendingSourceDepth(a, b) {{
				return a.source.y - b.source.y;
			}}
			function ascendingTargetDepth(a, b) {{
				return a.target.y - b.target.y;
			}}
		}}
		
		function center(node){{
			return node.y + node.dy / 2;
		}}

		function value(link) {{
			return link.value;
		}}

		return sankey;
	}};
</script>

<script>
	var margin = {{top: 1, right: 1, bottom: 6, left: 1}},
			width = {width} - margin.left - margin.right,
			height = {height} - margin.top - margin.bottom;

	var formatNumber = d3.format(",.0f"),
			format = function(d) {{ return formatNumber(d) + " TWh"; }},
			color = d3.scale.category20();

	var svg = d3.select("#chart").append("svg")
  	  .attr("width", width + margin.left + margin.right)
    	.attr("height", height + margin.top + margin.bottom)
    	.append("g")
    	.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

  var sankey = d3.sankey()
    .nodeWidth(15)
    .nodePadding(10)
    .size([width, height]);

	var path = sankey.link();

	var nodes={nodes}
	var links={links}

	sankey
		.nodes(nodes)
		.links(links)
		.layout(32);
        
	var link = svg.append("g").selectAll(".link")
					.data(links)
					.enter().append("path")
					.attr("class", "link")
					.attr("d", path)
					.style("stroke-width", function(d) {{ return Math.max(1, d.dy); }})
					.sort(function(a, b) {{ return b.dy - a.dy; }});
        
	link.append("title")
			.text(function(d) {{ return d.source.name + " → " + d.target.name + "" + format(d.value); }});
        
	var node = svg.append("g").selectAll(".node")
					.data(nodes)
					.enter().append("g")
					.attr("class", "node")
					.attr("transform", function(d) {{ return "translate(" + d.x + "," + d.y + ")"; }})
					.call(
						d3.behavior.drag()
							.origin(function(d) {{ return d; }})
							.on("dragstart", function() {{ this.parentNode.appendChild(this); }})
							.on("drag", dragmove)
					);

	node.append("rect")
			.attr("height", function(d) {{ return d.dy; }})
			.attr("width", sankey.nodeWidth() )
			.style("fill", function(d) {{ return d.color = color(d.name.replace(/ .*/, "")); }})
			.style("stroke", function(d) {{ return d3.rgb(d.color).darker(2); }})
			.append("title")
			.text(function(d) {{ return d.name + "" + format(d.value); }});
        
	node.append("text")
			.attr("x", -6)
			.attr("y", function(d) {{ return d.dy / 2; }})
			.attr("dy", ".35em")
			.attr("text-anchor", "end")
			.attr("transform", null)
			.text(function(d) {{ return d.name; }})
			.filter(function(d) {{ return d.x < width / 2; }})
			.attr("x", 6 + sankey.nodeWidth())
			.attr("text-anchor", "start");
        
	function dragmove(d){{
		d3.select(this)
			.attr("transform", "translate(" + d.x + "," + (d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))) + ")");
		sankey.relayout();
		link.attr("d", path);
	}}
</script>
'''.format(title=title,
           nolabel=nolabel,
           d3js_str=vjs.ViewJs.d3jsMin(),
           nodes=nodes,
           links=links,
           width=w,
           height=h)

    html.write(outTemplate)

    if not oFile == None:
        html.close()
コード例 #6
0
ファイル: t2.py プロジェクト: The-Alchemist/nysol_python
vv = temo.file()
with open(vv, "w") as wfp:
    wfp.write(sampDAT1)

os.system("rm x1.html x2.html x3.html")

rls0 = nm.mcut(i=vv, f="a,b:e", tag="check")
rls1 = nm.mcut("i={} f=a,b,c".format(vv)).mjoin("k=a", f="e", m=rls0).run()
print(rls1)

xxx = []
nm.mcut(i=vv, f="a,b,c").mjoin("k=a", f="e",
                               m=rls0).writelist(xxx).run(msg="on")
print(xxx)
rls2 = nm.mcut(i=vv, f="a,b,c").mjoin("k=a", f="e", m=rls0,
                                      o=xxx).run(msg="on")
print(xxx)
rls2 = nm.mcut(i=vv, f="a,b,c").mjoin("k=a", f="e", m=rls0).run(msg="on")
print(rls2)

nm.mcut("i={} f=a,b,c".format(vv)).mjoin(
    "k=a", f="e", m=rls0).writelist(xxx).drawModel("x1.html")

nm.mcut(i=vv, f="a,b,c").mjoin("k=a", f="e", m=rls0,
                               o=xxx).drawModel("x2.html")

rls3 = nm.readcsv(vv)
rls4 = rls3.mcut(f="a,b:e")
rls3.mcut("f=a,b,c").mjoin("k=a", f="e",
                           m=rls4).writecsv("vvv").drawModel("x3.html")
コード例 #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()
コード例 #8
0
ファイル: mnetpie.py プロジェクト: nysol/viewpy
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()
コード例 #9
0
def __dotNode(iFile, nw, type, clusterLabel, oPath):
    '''
	# system "cat #{iFile}"
	# key,nam,keyNum%0,num,nl,nv,nvv,nc,leaf,nvKey,ncKey
	# ##NULL##,j,0,15,j_A,0.09090909091,1,FF0000,1,,
	# ##NULL##,i,0,14,i_A,0.09090909091,1,FF0000,1,,
	# #1_1,a,2,6,a_A,0.3636363636,1.857142857,FF0000,1,0.7272727273,
	# #1_1,b,2,7,b_B,0.3636363636,1.857142857,00FF00,1,0.7272727273,
	# #1_1,d,2,9,d_B,0.3636363636,1.857142857,00FF00,1,0.7272727273,
	# #1_1,e,2,10,e_C,0.4545454545,2.142857143,0000FF,1,0.7272727273,
	# #1_2,c,3,8,c_A,0.2727272727,1.571428571,FF0000,1,0.2727272727,
	# #1_2,f,3,11,f_A,0.1818181818,1.285714286,FF0000,1,0.2727272727,
	# #1_3,g,4,12,g_B,0.1818181818,1.285714286,00FF00,1,,
	# #1_3,h,4,13,h_C,0.1818181818,1.285714286,0000FF,1,,
	# #2_1,#1_2,5,3,#1_2_,0.2727272727,1.571428571,,,,
	# #2_1,#1_1,5,2,#1_1_,0.7272727273,3,,,,
	'''

    block = ""
    for flds, top, bot in nm.readcsv(iFile).getline(k="keyNum", otype="dict"):
        nam = flds["nam"]
        nl = flds["nl"]
        nv = flds["nv"]
        nc = flds["nc"]
        leaf = flds["leaf"]

        if leaf == None or leaf == "":
            prefix = "cluster"
        else:
            prefix = "n"

        nStr = "%s_%s" % (prefix, flds["num"])
        attrStr = ""

        if prefix != "cluster":
            # node label
            # labelがleafでなければ、-clusterLabelが指定されていない限りlabelを表示しない
            if leaf or clusterLabel:
                attrStr += 'label="%s" ' % (nl)
            else:
                attrStr += 'label="" '

            # node shape
            if nv:
                nRatioNorm = float(nv)
                attrStr += "height=%f width=%f " % (0.5 * nRatioNorm,
                                                    0.75 * nRatioNorm)

            # node color
            if nc:
                attrStr += 'color="#%s" ' % (nc)

            # node linewidth
            if nw:
                attrStr += 'style="setlinewidth(%s)" ' % (nw)

        block += "%s [%s]\n" % (nStr, attrStr)

        if bot:
            keyNum = flds["keyNum"]
            key = flds["key"]
            nlKey = flds["nlKey"]
            nvKey = flds["nvKey"]
            ncKey = flds["ncKey"]

            with open("%s/c_%s" % (oPath, keyNum), "w") as fpw:
                fpw.write(block)

            # クラスタのラベルや色も出力しておく
            attrStr = ""
            attrStr += 'label="%s"\n' % (nlKey)

            # node color
            if ncKey:
                attrStr += 'color="#%s"\n' % (ncKey)

            if nw and ncKey:
                if ncKey:  ## ?
                    attrStr += 'style="setlinewidth(%s)"\n' % (nw)

            with open("%s/L_%s" % (oPath, keyNum), "w") as fpw:
                fpw.write(attrStr)

            block = ""