Exemple #1
0
def execute(path, grammar_file_name, example_file_name, export_dot, export_png):
    """U svrhe brzeg testiranja, metoda koja prima putanju do foldera, naziv fajla gde je gramatika i naziv fajla gde je
        primer programa u nasem jeziku i indikator da li da se eksportuju .dot i .png fajlovi"""

    meta_path = os.path.join(SRC_DIR, 'model', grammar_file_name)
    meta_name = os.path.splitext(meta_path)[0]
    metamodel = metamodel_from_file(meta_path)

    if export_dot:
        metamodel_export(metamodel, meta_name + '.dot')
        if export_png:
            graph = pydot.graph_from_dot_file(meta_name + '.dot')
            graph.write_png(meta_name + '.png')

    model_path = os.path.join(path, example_file_name)
    model_name = os.path.splitext(model_path)[0]

    model = metamodel.model_from_file(model_path)

    if export_dot:
        model_export(model, model_name + '.dot')
    if export_png:
        graph = pydot.graph_from_dot_file(model_name + '.dot')
        graph.write_png(model_name + '.png')

    return model
Exemple #2
0
    def open_file(self):
        """Abre el fichero .csv o .dot indicado en el primer argumento del
        terminal.
        Formato del fichero CSV: el número de línea-1 marca el número de nodo
        Separados por comas los nodos destino"""
        try:
            fileR = open(sys.argv[1], "r")
        except IOError:
            print("Imposible abrir fichero")
        except:
            print("Introduce nombre de fichero")
        else:
            #Se ha seleccionado un fichero CSV
            if re.match(".*(\.csv)$", sys.argv[1]):
                fileC = csv.reader(fileR, delimiter=',')
                filecsv = [n for n in fileC]
                self.create_nodes(filecsv)

            #Se ha seleccionado un fichero DOT
            elif re.match(".*(\.dot)$", sys.argv[1]):
                filedot = [[] for n in range(0, 15)]
                graph = pydot.graph_from_dot_file(sys.argv[1])
                res = graph.get_edges()
                for n in res:
                    filedot[int(n.get_source())].append(int(n.get_destination()))
                self.create_nodes(filedot)
            else:
                print('Extensión de fichero no válida')
def max_tree(max_depth=None, out_file=None):
    """
    Creates decision tree of max_depth. If max_depth is None the tree's depth won't be bounded.

    If out_file is specificed, function will make a dot file and png of generated tree

    prints training and testing error to stdout
    """

    data = np.loadtxt("fourier/energy.txt", delimiter=",")

    X = []
    y = []
    for row in data:
        y.append(int(row[1]))
        X.append(map(int, row[2:]))

    X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.4, random_state=0)

    print X_train

    clf = tree.DecisionTreeClassifier(max_depth=max_depth)
    clf = clf.fit(X_train, y_train)

    print "trained tree of depth %s" % (max_depth)
    print "training error:  %f" % (1 - clf.score(X_train, y_train))
    print "testing error:  %f" % (1 - clf.score(X_test, y_test))

    if out_file:
        with open(out_file + ".dot", "w") as f:
            f = tree.export_graphviz(clf, out_file=f)

        graph = pydot.graph_from_dot_file(out_file + ".dot")
        graph.write_png(out_file + ".png")
def visualize_mdp(mdp, filename):

    log.info('in the visualize_mdp function')
    import pydot
    import networkx as nx
    from networkx.drawing.nx_agraph import write_dot

    G=nx.DiGraph()

    for s in mdp['states']:
        for a in s['actions']:
            for t in a['transitions']:
                ecolor='red' if a['id'] else 'green'
                elabel='p={}, r={}'.format(t['probability'], t['reward'])
                G.add_edge(s['id'], t['to'],
                        color=ecolor,
                        label=elabel)

    log.info('writing dot file')
    write_dot(G, filename.replace('.json', '.dot'))
    g = pydot.graph_from_dot_file(filename.replace('.json', '.dot'))[0]

    log.info('writing png from dot file')
    g.write_png(filename.replace('.json', '.png'))

    log.info('removing intermediate dot file')
    os.remove(filename.replace('.json', '.dot'))
    return filename.replace('.json', '.png')
def max_tree(X, y, max_depth = None, out_file = None):
    """
    Creates decision tree of max_depth. If max_depth is None the tree's depth won't be bounded.

    If out_file is specificed, function will make a dot file and png of generated tree

    prints training and testing error to stdout
    """

    X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=.4, random_state=0)

    clf = tree.DecisionTreeClassifier(max_depth=max_depth)
    clf = clf.fit(X_train, y_train)

    print "trained tree of depth %s" % (max_depth) 
    print "training error:  %f" % (1-clf.score(X_train, y_train)) 
    print "testing error:  %f" % (1-clf.score(X_test, y_test)) 


    out_file = None
    if out_file:
        with open(out_file+".dot", 'w') as f:
            f = tree.export_graphviz(clf, out_file=f)

        graph = pydot.graph_from_dot_file(out_file+".dot")
        graph.write_png(out_file+'.png')
Exemple #6
0
def makeGraph(n, nNodes, connections, messages, label):
    filename = "out%d"%n
    file = open("%s.dot"%filename, "w")
    file.write("digraph G {\nlayout=\"neato\"\n")
    file.write("graph [dpi = 300];")

    radius = nNodes*0.2

    for i in range(1, nNodes+1):
        angle = 2*math.pi*i/nNodes
        x = radius*math.cos(angle)
        y = radius*math.sin(angle)

        file.write("  %d[pos=\"%f,%f!\",shape=circle];\n"%(i-1, x, y))

    for i in range(len(connections)):
        for j in connections[i]:
            if j not in messages[i]: file.write("%d->%d\n"%(i, j))
        for j in messages[i]:
            file.write("%d->%d[color=\"red\"];\n"%(i, j))

    file.write("labelloc=\"t\";\n")
    file.write("label=\"%s\";\n"%label)
    file.write("}\n")
    file.close()
    
    (graph,) = pydot.graph_from_dot_file("%s.dot"%filename)
    graph.write_png('%s.png'%filename)
	def OnPopupItemGraph(self, event):

		for row in self.ReportGrid.GetSelectedRows():
			label = self.ReportGrid.GetCellValue(row,0)
			id = self.ReportGrid.GetCellValue(row,1)

			### plot the graph
			### TODO link with properties frame
			for fct in ('extTransition','intTransition', 'outputFnc', 'timeAdvance'):
				filename = "%s(%s)_%s.dot"%(label,str(id),fct)
				path = os.path.join(tempfile.gettempdir(), filename)

				### if path exist
				if os.path.exists(path):
					graph = pydot.graph_from_dot_file(path)
					filename_png = os.path.join(tempfile.gettempdir(),"%s(%s)_%s.png"%(label,str(id),fct))
					graph.write_png(filename_png, prog='dot')

					pylab.figure()
					img = pylab.imread(filename_png)
					pylab.imshow(img)

					fig = pylab.gcf()
					fig.canvas.set_window_title(filename)

					pylab.axis('off')
					pylab.show()
Exemple #8
0
 def _render_with_pydot(self, filename, encoding):
     c = pydot.graph_from_dot_file(filename, encoding=encoding)
     sha = ''
     for g in c:
         jpe_data = g.create(format='jpe')
         sha += sha256(jpe_data).hexdigest()
     return sha
Exemple #9
0
    def draw(self):
        g = nx.MultiDiGraph()
        lr_content = {}
        for k,v in self.progress_present.iteritems():
            label = 'I %d\n'%k
            count = 0
            for rule in v:
                break
                label += rule[0] + ' --> '
                for r in rule[1]:
                    if r == u'dot':
                        label += '^ '
                    else:
                        label += r + ' '
                label += '\n'
            lr_content[k] = label

        for k,v in self.state_present.iteritems():
            for edge, to in v.iteritems():
                g.add_edge(lr_content[k], lr_content[to], label = edge)

        name = 'goto'
        nx.write_dot(g, name + '.dot')      
        g = pydot.graph_from_dot_file(name+'.dot')
        g.write_jpg(name+'.jpg')
def processFile(filename, dictionary=None, debug=0):
    """
        parse a file, solve the model, return the results

        This is a useful function in its own right, as well as an example
        of how to use the MarkovAvail class and make sense of the results.

        Args:
            filename (string): name of dot format input file
            dictionary (string): name of transition rates file
                    space/tab separated <name,value> pairs, w/comments
            debug (int): level of desired debug output
                0:  none
                1:  parsed and interpreted parameters
                2:  painful for code (not model) debugging

        Returns:
            MarkovAvail: parameters and solutions
            list: sorted (state #, occupancy) tupples
    """
    # process the input file
    g = pydot.graph_from_dot_file(filename)

    # process the dictionary
    valueDict = {}
    if dictionary is not None:
        if debug > 1:
            print("Using value dictionary: %s" % (dictionary))
        with open(dictionary) as f:
            for line in f:
                # skip comment lines
                if line.startswith('#') or line.startswith('/'):
                    continue
                # a value line needs at least two fields
                parts = line.split()
                if len(parts) >= 2:
                    key = parts[0]
                    value = parts[1]
                    valueDict[key] = value
                    if debug > 1:
                        print("    dictionary[%s]:\t%s" % (key, value))

    # process the model
    m = MarkovAvail(g, valueDict, debug)

    # solve the model and print the results
    m.solve()

    # create a list of states, sorted by decreasing occupancy
    stateOccupancies = {}
    for i in range(m.numstates):
        o = m.occupancy[i]
        stateOccupancies[i] = o
    sortedStates = sorted(stateOccupancies.iteritems(),
                          key=operator.itemgetter(1),
                          reverse=True)

    # return the solution and the sorted state/occupancy list
    return (m, sortedStates)
Exemple #11
0
 def draw(self):
     g = nx.MultiDiGraph()
     for id,path in self.s_DFA.iteritems():
         for label, to_id in path.iteritems():
             g.add_edge(id,to_id,label=label)
     name = 'dfa'
     nx.write_dot(g, name + '.dot')      
     g = pydot.graph_from_dot_file(name+'.dot')
     g.write_jpg(name+'.jpg')
Exemple #12
0
def make_graph(sciid,outfmt='pdf'):
    gc = graph_from_dot_file(TMP+'{}.dot'.format(sciid))
    gc.set_overlap(0)

    if 'png' in outfmt:
        gc.write_png('{}.png'.format(sciid), prog='dot')

    if 'pdf' in outfmt:
        gc.write_pdf('{}.pdf'.format(sciid), prog='dot') #fdp, dot,
def write_image_of_decision_tree(filename, decision_tree, columns):
  """Create an image visualising a decision tree."""

  dot_filename = str(filename) + '.dot'
  dotfile = open(dot_filename, 'w')
  tree.export_graphviz(decision_tree, out_file = dotfile, feature_names = columns)
  dotfile.close()
  graph = pydot.graph_from_dot_file(dot_filename)
  graph.write_png(str(filename) + '.png')
def main(infilename):
    """
    Get all graph info, and then search for cycle, sources and sinks
    """
    graph = pydot.graph_from_dot_file(infilename)
    nodes = get_nodes_info(graph)
    load_links(graph, nodes)
    propagate_lineage(nodes)
    dump_status(nodes)
Exemple #15
0
    def write_svg(self):
        """
            Create SVG out of the dotfile

            @dotfile: In-dotfile
        """
        self._write_stop()

        graph = pydot.graph_from_dot_file(self.dotfile)
        svg   = graph.write_svg(os.path.join(self.resdir, "map.svg"))
Exemple #16
0
    def process_graphs(self, file_names, squash_versions=False, analyze=False):
    
        graphs = []
        for x in file_names:
            xg = pydot.graph_from_dot_file(x)
            graphs.append(xg)
        
        final_graph = graphs[0]

        if len(graphs) > 1:

            if squash_versions:
                for g in graphs:
                    GraphProcessor.do_squash_versions(g)
        
            final_graph = GraphProcessor.merge_graphs(graphs)    

            # we're a little broken here, because the version squashing 
            # plus merging does some implicit analysis of version conflicts
            if analyze:

                per_graph_node_sets, intersection = GraphProcessor.intersecting_nodes(graphs)

                style=NodeStyleRule.globalRule({'fillcolor': GraphProcessor.DEFAULT_COLORS['intersect'], 'style':'filled'}) 

                for x in intersection:        
                    style.apply_node(final_graph, x)

                non_intersect = GraphProcessor.non_intersecting_nodes_per_graph(graphs, per_graph_node_sets)

                for x in non_intersect:

                    color = GraphProcessor.DEFAULT_COLORS['non_intersect_list'].pop(0)
                    style = NodeStyleRule.globalRule({'fillcolor':color, 'style':'filled'})

                    for y in non_intersect[x]:
                        style.apply_node(final_graph, y)


        if squash_versions:
            # necessary because we wedged a non-standard
            # attribute into the nodes marking their versions
            for n in final_graph.get_nodes():
                SquashVersionRule.clean_version_tag(n)

        for rule in self.style_rules:
            for e in final_graph.get_edges():
                rule.apply(final_graph, e)

        # otherwise it's unreadable
        final_graph.set('rankdir', 'LR')

        return final_graph
Exemple #17
0
def gf_dependencies(path, unlink=True):
	from subprocess import Popen, PIPE
	from tempfile import TemporaryFile
	import os 
	dotFile = "_gfdepgraph.dot"
	with TemporaryFile() as f:
		f.write('dg\n')
		f.seek(0)
		p = Popen(('gf', '-s', '-retain', path), stdin=f, stdout=PIPE)
	p.wait()
	dot = pydot.graph_from_dot_file(dotFile)
	if unlink: os.unlink(dotFile)
	return dot
    def image (self, file, im) :
        # on crée un graphe : on récupère le code du graphe
        graph = self.chaine_graphe ()
        # auquel on ajoute un début et une fin
        graph = "digraph GA {\n" + graph + "}\n"
        
        # puis on l'écrit dans le fichier file
        g = open (file, "w")
        g.write (graph)
        g.close ()

        # enfin, on convertit ce fichier en image
        dot = pydot.graph_from_dot_file (file)
        dot.write_png (im, prog="dot")
Exemple #19
0
    def _render_with_pydot(self, filename):
        #f = open(filename, 'rt')
        #graph_data = f.read()
        #f.close()

        #g = pydot.parse_from_dot_data(graph_data)
        g = pydot.graph_from_dot_file(filename)

        if not isinstance(g, list):
            g = [g]

        jpe_data = NULL_SEP.join([_g.create(format='jpe') for _g in g])

        return sha256(jpe_data).hexdigest()
def main(args):
    if args.output.endswith('tsv'):
        if not args.perform_closure:
            graph = pydot.graph_from_dot_file(args.input)
            edges = graph.get_edges()
            with open(args.output, "wb") as f:
                for edge in edges:
                    src = edge.get_source()
                    dest = edge.get_destination()
                    edge_type = edge.get('type') # edge.type
                    f.write('%s %s %s\n'%(src, dest, edge_type))
            return

    raise NotImplementedError
def build_nodes_tree_from_amr_dot_file(amr_dot_file_path):
    # extract interactions and complex formations (including complex disintegration) from the dot file format for an amr
    amr = pd.graph_from_dot_file(cap.absolute_path + amr_dot_file_path)
    sentence = amr.get_label()
    sentence = sentence.replace("\ ", "")
    nodes = {}
    # iterate over all edges to build a tree of nodes
    edges = amr.get_edge_list()
    # print 'edges: ', edges
    for edge in edges:
        # print 'edge', edge
        # print 'edge: ', edge
        source = edge.get_source()
        # print 'source: ', source
        destination = edge.get_destination()
        # print 'destination: ', destination
        edge_label = edge.get_label()
        edge_label = constants.no_quotes_regexp.sub("", edge_label)
        # print 'edge_label: ', edge_label
        edge = None
        # source not in the nodes_list
        if not nodes.has_key(source):
            source_node = amr.get_node(source)[0]
            nodes[source] = Node(source, source_node.get_label())
            source_node = None
        if not nodes.has_key(destination):
            destination_node = amr.get_node(destination)[0]
            nodes[destination] = Node(destination, destination_node.get_label())
            destination_node = None
        if edge_label == "TOP":
            if source != destination:
                raise AssertionError
            nodes[constants.root] = nodes[source]  # source and destination are same for TOP edge
            nodes[constants.root].is_root = True
        else:
            # linking destination as child of source, there can be multiple children with a given edge_label
            if edge_label not in nodes[source].children:
                nodes[source].children[edge_label] = [nodes[destination]]
            else:
                if nodes[destination] not in nodes[source].children[edge_label]:
                    nodes[source].children[edge_label].append(nodes[destination])
            # linking source as parent of destination, there can be multiple parents with same edge_label (eg: a protein can be a catalyst for multiple interactions)
            if edge_label not in nodes[destination].parents:  # if list is empty
                nodes[destination].parents[edge_label] = [nodes[source]]
            else:
                if nodes[source] not in nodes[destination].parents[edge_label]:
                    nodes[destination].parents[edge_label].append(nodes[source])
        # print 'nodes: ', nodes
        # amr.write_pdf(cap.absolute_path+amr_dot_file_path+'.pdf')
    return nodes, sentence
Exemple #22
0
    def parse(self, file):
        self.g = pydot.graph_from_dot_file(file)

        self.name = self.g.get_name()
        self.log.info("##### GRAPH NAME %s..." % (self.name))

        # Pick out all nodes and populate local nodes dict. The model
        # from pydot.Graph includes only explicitly mentioned nodes
        # in get_nodes(), others must be extracted from get_edges()
        #
        self.log.info("nodes:")
        for n in self.g.get_nodes():
            node = self.add_node(n.get_name())
            
        self.log.info("edges:")
        conneg_default = True #first conneg edge will be default
        for e in self.g.get_edges():
            src = self.add_node(e.get_source())
            dst_name = self.add_node(e.get_destination()).name
            label = self.normalize_label(e.get_label())
            m = re.match( r'conneg(\s+(\d+))?(\s+(\S+))?', label )
            if (m):
                if (m.group(4) is None):
                    self.log.info("Bad conneg label: %s" % (label))
                else:
                    code = int(m.group(2))
                    content_type = m.group(4)
                    self.log.info("%s conneg %s, %d -> %s" % (src.name, content_type, code, dst_name))
                    src.conneg[content_type] = [code,dst_name,conneg_default]
                    conneg_default = False
            elif (re.match('html\s+link',label,re.I)):
                src.html_links.append(dst_name)
            elif (re.match('html\s+img',label,re.I)):
                src.html_imgs.append(dst_name)
            else:
                # assume space separated set of links (last word might me mime type)
                words = label.split()
                mime_type = None
                if (re.match(r'\w+/\w+$',words[-1])):
                    mime_type = words.pop()
                for rel in words:
                    self.log.info("%s rel=\"%s\" %s %s" % (src.name,rel,dst_name,mime_type))
                    src.links.append([rel,dst_name,mime_type])

            # Do we have an svg file for this graph?
            svg_file = os.path.splitext(file)[0] + '.svg'
            if (os.path.exists(svg_file)):
                self.svg=svg_file
Exemple #23
0
	def render(self, mode='instance', number=3, unscale=False):
		if self.dot_string == "":
			return
		# Print out the merges and splits for this node
		print self.pretty_print(False), self.surprise_num
		self.describe_merges_and_splits()
		# make picture and display
		# Render dot file and save
		dot_list = self.dot_string.split("REPLACE_STRING")
		string = dot_list[0]
		for i in range(len(dot_list)-1):
			string += self.dot_descriptions[i]['desc']
			if number == 0:
				number = 1000
			for j in range(number):
				if j >= len(self.dot_descriptions[i][mode]):
					break
				if unscale:
					attribute = self.dot_descriptions[i][mode][j]
					att_name = attribute.split(":")[0]
					try:
						att_num = float(attribute.split(":")[1])
						att_num *= self.pastCalc[att_name]['std']
						if mode == "no_compare":
							att_num += self.pastCalc[att_name]['avg']
						att_num = round(att_num,2)
						string += att_name+":"+str(att_num)+"\\n"
					except IndexError:
						string += self.dot_descriptions[i][mode][j]+"\\n"
				else:
					string += self.dot_descriptions[i][mode][j]+"\\n"
			string += dot_list[i+1]
		dot_file = open('tmp.dot', 'w+')
		dot_file.write(string)
		dot_file.close()
		# Read dot to graph and render
		graph = pydot.graph_from_dot_file('tmp.dot')
		graph.write_png('tmp.png')
		graph_image = scipy.misc.imread('tmp.png')
		if not Instance.fig is None:
			pylab.close(Instance.fig)
			Instance.fig = None
		Instance.fig = pylab.figure(frameon=False)
		ax_size = [0,0,1,1]
		Instance.fig.add_axes(ax_size)
		pylab.imshow(graph_image, vmin=1, vmax=99, origin='upper')
		pylab.axis('off')
		pylab.show()
def loadDotFile(dotfile):
	dotGraph = pydot.graph_from_dot_file(dotfile)
	graphEdges = dotGraph.get_edges()
	for edge in graphEdges:
		edgeList.append( (str(edge.get_source().replace('\"','')), edge.get_destination().replace('\"','')) )
		if edge.get_source().replace('\"','') not in nodeList:
			nodeList.append(edge.get_source().replace('\"',''))
		if edge.get_destination().replace('\"','') not in nodeList:
			nodeList.append(edge.get_destination().replace('\"',''))

#		edgeList.append( (str(edge.get_source()[1:-1]), edge.get_destination()[1:-1]) )
#		if edge.get_source()[1:-1] not in nodeList:
#			nodeList.append(edge.get_source()[1:-1])
#		if edge.get_destination()[1:-1] not in nodeList:
#			nodeList.append(edge.get_destination()[1:-1])
	nodeList.sort()
Exemple #25
0
	def _write_state_graph(self):
		file_path = '%s/graph_state.dot' % self.__work_directory
		nx.write_dot(self.__state_graph, file_path)
		graph = pydot.graph_from_dot_file(file_path)
		
		# get the graph nodes
		nodes = graph.get_nodes()
		
		if nodes:
			for node in nodes:
				name = node.get_name()
				# id is set based on the id column in state table
				if name:
					# an double quote is read around the name
					node.set('id', int(name.split('-')[-1].strip('"')))
					
				try:
					node.set_shape('box')
				except AttributeError:
					node.set('shape', 'box')
					
				try:
					node.set_width('3')
				except AttributeError:
					node.set('width', '3')
				
				try:
					node.set_height('1')
				except AttributeError:
					node.set('height', '1')
					
				try:
					node.set_imagescale('true')
				except AttributeError:
					node.set('imagescale', 'true')
				
				#TODO: also set the nodes name and the nodes image
		
		"""	
		edges = graph.get_edges()
		
		if edges:
			for edge in edges:
				pass
		"""
		
		graph.write_dot('%s/graph_state_layout.dot' % self.__work_directory)
Exemple #26
0
    def parse(self, fileName):
        project = DotGraphProject()

        # parse the dotfile
        self.graph = pydot.graph_from_dot_file(fileName)
        self.graph.project = project

        # parse tags from dot file
        self.graph.tags = self.parseTags(fileName)

        ensureSupported('dotgraphprojecthelpers.ProjectSupport', self.graph, project)

        # allow support to find and add more items
        for support in getPlugins(DotGraphSupport, package=plugins):
            support.support(self.graph, project)

        return project
Exemple #27
0
def makeFrames(m, b, c):
    graphTemplate = 'digraph %s_%s {\n\tnode[shape=plaintext];\n\tgraph[bb="0,0,407.8,333.47"];\n%s\n%s}'
    nodeTemplate = "\t%s[label=%s, pos=%s, fontcolor=%s];\n"
    edgeTemplate = "\t%s -> %s [color=%s];\n"
    baseEdgeTemplate = "\t%s -> %s [dir=none,color=gray];\n"
    # baseEdgeTemplate = "\t%s -> %s [dir=none,color=gray, pos = %s];\n"

    baseGraph = pydot.graph_from_dot_file("cases/%smap.gv" % c)

    deltaFiles = os.listdir("%s" % m)
    deltaFiles = filter(lambda x: x.startswith(b) and x.endswith(".delta"), deltaFiles)

    for deltaFilename in deltaFiles:
        deltaFile = file("%s/%s" % (m, deltaFilename), "r")
        nodes, edges = eval(deltaFile.read())
        deltaFile.close()

        gvNodes = dict()

        nodesStr = ""
        for node in baseGraph.get_nodes():
            name = node.get_label()
            if name in nodes.keys():
                color = stressColorcode(nodes[name])
                pos = node.get_pos()
                nodesStr += nodeTemplate % (name, name, pos, color)

        edgesStr = ""
        for edge in baseGraph.get_edges():
            source = edge.get_source()
            target = edge.get_destination()
            edgesStr += baseEdgeTemplate % (source, target)
            # pos = edge.get_pos()
            # edgesStr += baseEdgeTemplate%(source,target,pos)

        for edge in edges:
            (source, target, color) = edge
            edgesStr += edgeTemplate % (source, target, color)

        graphName = deltaFilename.replace(".delta", "")
        graphStr = graphTemplate % (c, graphName, nodesStr, edgesStr)

        graphFile = file("%s/%s.gv" % (m, graphName), "w")
        graphFile.write(graphStr)
        graphFile.close()
    def __init__(self, cluster_vectors_fn, cluster_labels_fn, movie_names_fn, label_names_fn, cluster_names_fn, filename, training_data, cluster_to_classify, max_depth):

        vectors = dt.importVectors(cluster_vectors_fn)
        labels = dt.importLabels(cluster_labels_fn)
        cluster_names = dt.importString(cluster_names_fn)
        vector_names = dt.importString(movie_names_fn)
        label_names = dt.importString(label_names_fn)
        scores_array = []
        for l in range(len(labels[0])):
            new_labels = [0] * 15000
            for x in range(len(labels)):
                new_labels[x] = labels[x][l]
            x_train = np.asarray(vectors[:training_data])
            x_test = np.asarray(vectors[training_data:])
            y_train = np.asarray(new_labels[:training_data])
            y_test = np.asarray(new_labels[training_data:])


            self.clf = tree.DecisionTreeClassifier( max_depth=max_depth)
            self.clf = self.clf.fit(x_train, y_train)

            y_pred = self.clf.predict(x_test)
            f1 = f1_score(y_test, y_pred, average='binary')
            accuracy = accuracy_score(y_test, y_pred)
            scores = [[label_names[l], "f1", f1, "accuracy", accuracy]]
            print scores[0]
            scores_array.append(scores)

            class_names = [ label_names[l], "NOT "+label_names[l]]
            tree.export_graphviz(self.clf, feature_names=cluster_names, class_names=class_names, out_file='Rules/'+label_names[l]+filename+'.dot', max_depth=10)
            """
            rewrite_dot_file = dt.importString('Rules/'+filename+label_names[l]+'.dot')
            new_dot_file = []
            for s in rewrite_dot_file:
                new_string = s
                if "->" not in s and "digraph" not in s and "node" not in s and "(...)" not in s and "}" not in s:
                    index = s.index("value")
                    new_string = s[:index] + '"] ;'
                new_dot_file.append(new_string)
            dt.write1dArray(new_dot_file, 'Rules/Cleaned'+filename+label_names[l]+'.dot')
            """
            graph = pydot.graph_from_dot_file('Rules/'+label_names[l]+filename+'.dot')
            graph.write_png('Rules/Images/'+label_names[l]+filename+".png")
            self.get_code(self.clf, cluster_names, class_names, label_names[l]+filename)
        dt.write1dArray(scores_array, 'Rules/Scores/'+filename+'.scores')
def add_file_to_graph(file_path, final_graph):
    print "[+] Loading graph from: {}.".format(file_path)

    graph = pydot.graph_from_dot_file(file_path)
    node_names = map(lambda node: node.get_name(), final_graph.get_nodes())
    edge_names = map(lambda edge: (edge.get_source(), edge.get_destination()),
                     final_graph.get_edges())

    for edge in graph.get_edges():
        (src_relevant, dst_relevant) = is_relevant_nodes(edge)

        if src_relevant and (edge.get_source() not in node_names):
            final_graph.add_node(pydot.Node(edge.get_source()))

        if dst_relevant and (edge.get_destination() not in node_names):
            final_graph.add_node(pydot.Node(edge.get_destination()))

        if src_relevant and dst_relevant and (edge.get_source(), edge.get_destination()) not in edge_names:
            final_graph.add_edge(pydot.Edge(edge.get_source(), edge.get_destination()))
Exemple #30
0
def runall(fname):
	gs = pydot.graph_from_dot_file(fname)
	if not isinstance(gs,list):
		gs = [gs]

	gdx = 0
	for g in gs:
		info = getinfo(g)
		done = True
		iters = 0
		while not done and iters < 10:
			done = apply_rpctimes(g)
			iters += 1
	
		outname = "%d" % gdx
		if info:
			outname = info["Cluster ID"]
		savegraph(g, outname + ".json")
		gdx += 1
Exemple #31
0
    table_numeric = pd.read_csv('./data/' + sys.argv[1] + '_overlap_numeric.csv')
    
    features = list(table_ordinal.columns[2:-1])
    target = list(table_ordinal.columns[-1:])[0]

    X_train, X_test, y_train, y_test = train_test_split(table_ordinal[features], table_ordinal[target], random_state=1)

    dt_entropy = DecisionTreeClassifier(criterion='entropy', max_depth=3)    
    dt_entropy = dt_entropy.fit(X_train, y_train)

    # output a PNG of the decision tree
    dotfile = sys.argv[1] + '_entropy.dot'
    png     = sys.argv[1] + '_entropy.png'

    export_graphviz(dt_entropy, out_file=dotfile, feature_names=features)
    (tree, ) = pydot.graph_from_dot_file(dotfile)
    tree.write_png(png)

    # test
    y_predict = dt_entropy.predict(X_test)
    print "Entropy: {}".format(accuracy_score(y_test, y_predict))

    X_train, X_test, y_train, y_test = train_test_split(table_numeric[features], table_numeric[target], random_state=1)
    dt_gini = DecisionTreeClassifier(criterion='gini', max_depth=3)    
    dt_gini = dt_gini.fit(X_train, y_train)

    # output a PNG of the decision tree
    dotfile = sys.argv[1] + '_gini.dot'
    png     = sys.argv[1] + '_gini.png'

    export_graphviz(dt_gini, out_file=dotfile, feature_names=features)
Exemple #32
0
    logger.info(infoMsg)

    # Create dot file by using extra/gprof2dot/gprof2dot.py
    # http://code.google.com/p/jrfonseca/wiki/Gprof2Dot
    dotFilePointer = codecs.open(dotOutputFile, 'wt', UNICODE_ENCODING)
    parser = gprof2dot.PstatsParser(profileOutputFile)
    profile = parser.parse()
    profile.prune(0.5 / 100.0, 0.1 / 100.0)
    dot = gprof2dot.DotWriter(dotFilePointer)
    dot.graph(profile, gprof2dot.TEMPERATURE_COLORMAP)
    dotFilePointer.close()

    infoMsg = "converting dot file into a graph image '%s'" % imageOutputFile
    logger.info(infoMsg)

    # Create graph image (png) by using pydot (python-pydot)
    # http://code.google.com/p/pydot/
    pydotGraph = pydot.graph_from_dot_file(dotOutputFile)
    pydotGraph.write_png(imageOutputFile)

    infoMsg = "displaying interactive graph with xdot library"
    logger.info(infoMsg)

    # Display interactive Graphviz dot file by using extra/xdot/xdot.py
    # http://code.google.com/p/jrfonseca/wiki/XDot
    win = xdot.DotWindow()
    win.connect('destroy', gtk.main_quit)
    win.set_filter("dot")
    win.open_file(dotOutputFile)
    gtk.main()
Exemple #33
0
yTrain, yTest = yTrain[:, np.newaxis], yTest[:, np.newaxis]
dt = tree.DecisionTreeClassifier()
dt.fit(xTrain, yTrain)

dotfile = open("dt.dot", 'w')
tree.export_graphviz(dt,
                     out_file=dotfile,
                     feature_names=[
                         'Clump_Thickness',
                         'Uniformity_of_Cell_Size',
                         'Uniformity_of_Cell_Shape',
                         'Marginal_Adhesion',
                         'Single_Epithelial_Cell_Size',
                         'Bare_Nuclei',
                         'Bland_Chromatin',
                         'Normal_Nucleoli',
                         'Mitoses',
                     ])
dotfile.close()

# In[2]:

import pydot

(graph, ) = pydot.graph_from_dot_file('dt.dot')
graph.write_png('dt.png')

# In[3]:

# In[ ]:
import pydot

for i in range(1, 7):
    try:
        dot_file = 'gini_model_' + str(i) + '.dot'
        (graph, ) = pydot.graph_from_dot_file(dot_file)
        png_file = 'gini_model_' + str(i) + '.png'
        graph.write_png(png_file)
    except:
        print("Cannot Find File" + dot_file)
Exemple #35
0
    def layout(self, type_):

        if pygraphviz:
            G = pygraphviz.AGraph(strict=False, directed=True)
            if type_ == 'dot LR':
                G.graph_attr['rankdir'] = 'LR'
            type_ = type_.split()[0]
            G.graph_attr['ranksep'] = '0.125'
        elif pydot:
            G = pydot.Dot('graphname', graph_type='digraph')
            if type_ == 'dot LR':
                G.set_layout('dot')
                G.set('rankdir', 'LR')
            else:
                G.set_layout(type_)
            G.set('ranksep', '0.125')
        for from_, to in self.link.values():
            if pygraphviz:
                G.add_edge(
                    (from_, from_.gnx),
                    (to, to.gnx),
                )
            elif pydot:
                G.add_edge(pydot.Edge(from_.gnx, to.gnx))

        for i in self.nodeItem:
            if pygraphviz:
                G.add_node((i, i.gnx))
            elif pydot:
                gnode = pydot.Node(i.gnx)
                # rect = self.nodeItem[i].boundingRect()
                G.add_node(gnode)
                for child in i.children:
                    key = (i, child)
                    if key not in self.hierarchyLinkItem or child not in self.nodeItem:
                        continue
                    G.add_edge(pydot.Edge(i.gnx, child.gnx))
        if pygraphviz:
            G.layout(prog=type_)
        elif pydot:
            tempName = tempfile.NamedTemporaryFile(dir=tempfile.gettempdir(),
                                                   delete=False)
            G.write_dot(tempName.name)
            G = pydot.graph_from_dot_file(tempName.name)

        for i in self.nodeItem:
            if pygraphviz:
                gn = G.get_node((i, i.gnx))
                x, y = map(float, gn.attr['pos'].split(','))
                i.u['_bklnk']['x'] = x
                i.u['_bklnk']['y'] = -y
                self.nodeItem[i].setPos(x, -y)
                self.nodeItem[i].do_update()
            elif pydot:
                lst = G.get_node(''.join(['"', i.gnx, '"']))
                if lst:
                    x, y = map(float, lst[0].get_pos().strip('"').split(','))
                    i.u['_bklnk']['x'] = x
                    i.u['_bklnk']['y'] = -y
                    self.nodeItem[i].setPos(x, -y)
                    self.nodeItem[i].do_update()
        if pydot:
            x, y, width, height = map(float, G.get_bb().strip('"').split(','))
            self.ui.canvasView.setSceneRect(
                self.ui.canvas.sceneRect().adjusted(x, y, width, height))
        self.do_update(adjust=False)
        self.center_graph()
def get_graphs(dotfile_path):
    """
    get four graphs from the dot file

    Parameters
    ----------
    dotfile_path: str
        The path of dot file. You need to first generate the dot file using the method generateDotFIle()

    Returns
    -------
    four graphs; the whole graph, CFG graph, DD graph and CD graph.The CFG,DD,CD graphs is a part of the whole graph
    """
    print(dotfile_path)
    print('start generate graph')
    import networkx as nx
    import pydot
    graph = pydot.graph_from_dot_file(dotfile_path)

    nodes_bb = {}
    graph_dot = pydot.Dot(graph_type="digraph")
    graph_DD = pydot.Dot(graph_type="digraph")
    graph_CD = pydot.Dot(graph_type="digraph")
    graph_CFG = pydot.Dot(graph_type="digraph")

    # traverse node
    for cluster in graph[0].get_subgraphs():
        #print(cluster.obj_dict['name'])
        if cluster.obj_dict['name'].startswith('cluster_'):
            basic_block_id = cluster.obj_dict['name'].split('_')[-1]
            #print(basic_block_id)
            for node in cluster.obj_dict['nodes'].keys():
                #print(cluster.obj_dict['nodes'][node][0]['attributes'])

                # nodes_bb[node] = str(basic_block_id)
                nodes_bb[node] = cluster.obj_dict['nodes'][node]
                graph_dot.add_node(cluster.get_node(node)[0])
                graph_DD.add_node(cluster.get_node(node)[0])
                graph_CD.add_node(cluster.get_node(node)[0])
                graph_CFG.add_node(cluster.get_node(node)[0])

            print("***********")

    # traverse edge
    # dd
    # edges
    # color: cyan4
    # use
    # edges
    # color: black, dashed
    # cd
    # edges
    # color: blue
    # cfg
    # edges
    # color: gray

    for cluster in graph[0].get_subgraphs():
        #print(cluster.obj_dict['name'])
        if cluster.obj_dict['name'].startswith('cluster_'):
            basic_block_id = cluster.obj_dict['name'].split('_')[-1]
            #print(basic_block_id)

            for edge in cluster.get_edges():
                # print(edge.__get_attribute__('color'))
                graph_dot.add_edge(edge)
                color = edge.__get_attribute__('color')
                color = color.strip()

                #print(color == '"gray"')
                #print(color + " " + 'gray')
                # color str contains "" in its str
                if edge.__get_attribute__('color') == '"gray"':
                    #print('gray')
                    graph_CFG.add_edge(edge)
                if edge.__get_attribute__('color') == '"blue"':
                    #print('blue')
                    graph_CD.add_edge(edge)
                if edge.__get_attribute__('color') == '"cyan4"':
                    #print('add cyan4')
                    graph_DD.add_edge(edge)

            print("***********")

    #print(graph_dot)
    #print(graph_DD)

    nx_g = nx.nx_pydot.from_pydot(graph_dot)
    nx_CFG = nx.nx_pydot.from_pydot(graph_CFG)
    nx_DD = nx.nx_pydot.from_pydot(graph_DD)
    nx_CD = nx.nx_pydot.from_pydot(graph_CD)

    print('end generate graph')
    return nx_g, nx_CFG, nx_DD, nx_CD
Exemple #37
0
def plot_dot_plot(tmpdir, _id, features, target, top_features_name, indices,
                  random_state, mode):
    """Make dot plot for based on decision tree.

    Parameters
    ----------
    tmpdir: string
        Temporary directory for saving experiment results
    _id: string
        Experiment ID in PennAI
    features: np.darray/pd.DataFrame
        Features in training dataset
    target: np.darray/pd.DataFrame
        Target in training dataset
    top_features: list
        Top feature_names
    indices: ndarray
        Array of indices of top important features
    random_state: int
        Random seed for permuation importances
    mode:  string
        'classification': Run classification analysis
        'regression': Run regression analysis

    Returns
    -------
    dtree_train_score, float
        Test score from fitting decision tree on top important feat'

    """
    # plot bar charts for top important features
    import pydot
    from sklearn.tree import export_graphviz

    top_features = features[:, indices]
    if mode == 'classification':
        from sklearn.tree import DecisionTreeClassifier
        dtree = DecisionTreeClassifier(random_state=random_state,
                                       max_depth=DT_MAX_DEPTH)
        scoring = SCORERS['balanced_accuracy']
    else:
        from sklearn.tree import DecisionTreeRegressor
        dtree = DecisionTreeRegressor(random_state=random_state,
                                      max_depth=DT_MAX_DEPTH)
        scoring = SCORERS["neg_mean_squared_error"]

    dtree.fit(top_features, target)
    dtree_train_score = scoring(dtree, top_features, target)
    dot_file = '{0}{1}/dtree_{1}.dot'.format(tmpdir, _id)
    png_file = '{0}{1}/dtree_{1}.png'.format(tmpdir, _id)
    class_names = None
    if mode == 'classification':
        class_names = [str(i) for i in dtree.classes_]
    export_graphviz(dtree,
                    out_file=dot_file,
                    feature_names=top_features_name,
                    class_names=class_names,
                    filled=True,
                    rounded=True,
                    special_characters=True)
    (graph, ) = pydot.graph_from_dot_file(dot_file)
    graph.write_png(png_file)
    return dtree_train_score
Exemple #38
0
def gen_graph_from_gv(ifile, odir, oformat="png"):
    (graph, ) = pydot.graph_from_dot_file(ifile)
    gen_graph_func = getattr(graph, "write_" + oformat)
    filename = os.path.basename(ifile)
    ofile = odir + "/" + os.path.splitext(filename)[0] + "." + oformat
    gen_graph_func(ofile)
# [[5.1 3.5 1.4 0.2]
#  [7.  3.2 4.7 1.4]
#  [6.3 3.3 6.  2.5]]
test_target = iris.target[test_idx]
# [0 1 2]
model.fit(train_data, train_target)

print("correct data")
print(test_target)  # [0 1 2]
print("predictions")
print(model.predict(test_data))  # [0 1 2]


baby = export_graphviz(model, out_file="baby.dot",
                       feature_names=["sepal_length", "sepal_width",
                                      "petal_length", "petal_width"],
                       label='all',
                       filled=True,
                       rounded=True
                       )
# corrupted but works in tutorial 2016 year
dot_data = StringIO(baby)

graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("tree2.pdf")

# works perfect
# straight reading from dot file
graph = pydot.graph_from_dot_file("baby.dot")
graph[0].write_pdf("tree.pdf")
Exemple #40
0
# List of features sorted from most to least important
sorted_importances = [importance[1] for importance in feature_importances]
sorted_features = [importance[0] for importance in feature_importances]
# Cumulative importances
cumulative_importances = np.cumsum(sorted_importances)
# Make a line graph
plt.plot(x_values, cumulative_importances, 'g-')
# Draw line at 95% of importance retained
plt.hlines(y = 0.95, xmin=0, xmax=len(sorted_importances), color = 'r', linestyles = 'dashed')
# Format x ticks and labels
plt.xticks(x_values, sorted_features, rotation = 'vertical')
# Axis labels and title
plt.xlabel('Variable')
plt.ylabel('Cumulative Importance')
plt.title('Cumulative Importances')
plt.savefig('fig_SA_06_importance_cumulative.png')
plt.show()


# Tree sample creation. DOT and PNG files.
# Activate if needed
"""""
import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'
"""""
tree_small = clf.estimators_[5]
# Save the tree as a png image
export_graphviz(tree_small, out_file = 'Tree_sample.dot', feature_names = feature_list, rounded = True, precision = 1)
(graph, ) = pydot.graph_from_dot_file('Tree_sample.dot')
graph.write_png('Tree_sample.png')
Exemple #41
0
# -*- coding:utf-8 -*-

import numpy as np
from sklearn import tree
from sklearn.datasets import load_iris
import pydot
import os

print(os.getcwd())

# 定义特征选择标准为entropy,深度为7
clf = tree.DecisionTreeClassifier(criterion="entropy", max_depth=7)
# 加载鸢尾花数据
iris = load_iris()
# 训练模型
clf.fit(iris.data, iris.target)
# 输出图
tree.export_graphviz(clf, out_file='tree.dot')
(graph, ) = pydot.graph_from_dot_file('tree.dot')
graph.write_png('1.png')
graph.write_png('tree.png')
Exemple #42
0
    #dot_file =  pydot.graph_from_dot_file('/home/{0}/Dropbox/ASL/ASL_Summer_2016/exclusions/firefighting/firefighting_stay_in_place_all.dot'.format(getpass.getuser()))

    #load inputs and outputs
    #input_prop_to_ros_info, output_prop_to_ros_info = file_operations.loadYAMLFile(\
    #    '/home/{0}/LTLROS_ws/src/controller_executor/examples/firefighting/firefighting.yaml'.format(getpass.getuser()))
    #example_name = "firefighting"

    # ---- simple ---- #
    #dot_file =  pydot.graph_from_dot_file('/home/{0}/Dropbox/ASL/ASL_Summer_2016/exclusions/simple/simple_all.dot'.format(getpass.getuser()))
    #input_prop_to_ros_info, output_prop_to_ros_info = file_operations.loadYAMLFile(\
    #    '/home/{0}/LTLROS_ws/src/controller_executor/examples/simple/simple.yaml'.format(getpass.getuser()))
    #example_name = "simple"

    # --- move_group_and_move_base ---- #
    #load dot file
    dot_file =  pydot.graph_from_dot_file('/home/{0}/Dropbox/ASL/ASL_Summer_2016/exclusions/move_group_and_move_base/youbot_lab.dot'.format(getpass.getuser()))

    #load inputs and outputs
    input_prop_to_ros_info, output_prop_to_ros_info = file_operations.loadYAMLFile(\
        '/home/{0}/LTLROS_ws/src/controller_executor/examples/move_group_and_move_base/move_group_and_move_base.yaml'.format(getpass.getuser()))
    example_name = "move_group_and_move_base"

    ##################  #################
    ##### NODES ######  ###### EDGES ####
    ##################  #################
    #first grab all the nodes (both n__ and t__)
    nodes_dict, edges_dict = {}, {}
    for subgraph_name, subgraph in dot_file.obj_dict['subgraphs'].iteritems():
        check_resources_logger.debug("subgraph_name: {0}, length of subgraph: {1}".format(subgraph_name, len(subgraph)))
        check_resources_logger.log(8, "subgraph[0] keys: {0}".format(subgraph[0].keys()))
        check_resources_logger.log(8, subgraph[0]['edges'].keys())
data = np.array(data)
n_samples = len(data)
n_features = len(data[0])
print('Number of samples:', n_samples)
print('Number of features:', n_features)

del headers[-1]



X = data
y = targets
X_train, X_test, Y_train, Y_test= train_test_split(data,targets,test_size=0.25,random_state=0)

clf_regressor = DecisionTreeRegressor(max_features=3)
clf_regressor .fit(X_train,Y_train)



dot_data = tree.export_graphviz(clf_regressor, out_file="forest.dot",
                         feature_names=headers,
                         filled=True, rounded=True,
                         special_characters=False)
(graph,) = pydot.graph_from_dot_file('forest.dot')
graph.write_png('forest.png')

print(clf_regressor.score(X_train,Y_train))
print(clf_regressor.score(X_test,Y_test))


Exemple #44
0
                self.y += command.steps * move[1]
                    
    def __str__(self):
        return "Robot position is {}, {}.".format(self.x, self.y)

def move_command_processor(move_cmd):
    if move_cmd.steps < 0:
        raise TextXSemanticError('Broj koraka ne moze biti negativan')
    elif move_cmd.steps == 0:
        move_cmd.steps = 1

if __name__ == '__main__':

    robot_mm = metamodel_from_file('robot.tx', debug = False)
    metamodel_export(robot_mm, 'robot_meta.dot')
    graph = pydot.graph_from_dot_file('robot_meta.dot')
    graph.write_png('robot_meta.png')
    
    object_processors = {'MoveCommand' : move_command_processor}
    robot_mm.register_obj_processors(object_processors)
    
    robot_model = robot_mm.model_from_file('test.rbt')
    model_export(robot_model, 'robot_model.dot')
    graph = pydot.graph_from_dot_file('robot_model.dot')
    graph.write_png('robot_model.png')
    
    robot = Robot()
    robot.interpret(robot_model)
    print(robot)
   
    
Exemple #45
0
    def from_dot(graph_name, path, resources=[], containers=[]):
        def trim(str):
            return str[1:-1]

        import pydot
        graph = pydot.graph_from_dot_file(path)
        graph_nodes = graph.get_node_list()
        nodes = []
        for graph_node in graph_nodes:
            name = trim(graph_node.get_name())
            if name in resources:
                node = Resource(name)
            elif name in containers:
                continue
            else:
                shape = graph_node.get_attributes()["shape"]
                if shape == "parallelogram":
                    node = HardGoal(name)
                elif shape == "polygon":
                    node = Task(name)
                elif shape == "box":
                    node = SoftGoal(name)
                elif shape in ["circle", "ellipse"]:
                    # containers
                    continue
                else:
                    #print(name)
                    continue
            node.id = node.name
            nodes.append(node)

        graph_edges = graph.get_edge_list()
        edges = []
        labels = set()
        for graph_edge in graph_edges:
            source = trim(graph_edge.get_source())
            target = trim(graph_edge.get_destination())
            if source in containers or target in containers:
                continue
            labels.add(graph_edge.get_attributes().get("label", None))
            label = graph_edge.get_attributes().get("label", None)
            if not label:
                value = "or"
                type = "decompositions"
            elif "make" in label.lower():
                value = "make"
                type = "contribution"
            elif "break" in label.lower():
                value = "break"
                type = "contribution"
            elif "help" in label.lower():
                value = "help"
                type = "contribution"
            elif "hurt" in label.lower():
                value = "hurt"
                type = "contribution"
            elif "some +" in label.lower():
                value = "someplus"
                type = "contribution"
            elif "some -" in label.lower():
                value = "someminus"
                type = "contribution"
            elif "d" in label.lower():
                value = "dependency"
                type = "dependency"
                source, target = target, source
            else:
                #print(source, target, label)
                continue
            edge = Component("edge")
            edge.value = value
            edge.type = type
            edge.source = source
            edge.target = target
            edges.append(edge)
        return Graph(name=graph_name, nodes=nodes, edges=edges)
Exemple #46
0
import pydot
with open(sys.argv[1]) as f:
    data = json.load(f)

tables = dict()
actions = dict()
stages = OrderedDict()
variables = OrderedDict()
cfg = dict()

# context.json, tableplacement.html, swi.dot, swe.dot
num_actions = 0
added_actions = 0

i_cfg = pydot.graph_from_dot_file(sys.argv[3])
e_cfg = pydot.graph_from_dot_file(sys.argv[4])

with open(sys.argv[2]) as table_placement:
    table_name = ""
    stage = -1
    flag = False
    skip = 0
    for line in table_placement.readlines():
        if skip > 0:
            skip -= 1
            continue
        if line.startswith("|Stage"):
            tables = dict()
            actions = dict()
            stages = OrderedDict()
Exemple #47
0
#Should predict healthy [0] and does
print('Predicting...')
X_new = np.array(
    [[22.0, 1.0, 4.0, 130.0, 110.0, 0.0, 0.0, 180.0, 0.0, 0.3, 2.0, 0.0, 3.0]])
y_classification = treePruned.predict(X_new)
print('Prediction: {}->{}'.format(X_new[0], y_classification))

featureNames = [
    'age', 'sex', 'cp', 'thresBPS', 'chol', 'fbs', 'restECG', 'thalach',
    'exang', 'oldPeak', 'slope', 'ca', 'thal'
]
# now create a bar chart and save it to a file
n_features = 13
plt.barh(range(n_features), treePruned.feature_importances_, align='center')
plt.yticks(np.arange(n_features), featureNames)
plt.xlabel("Feature importance")
plt.ylabel("Feature")
plt.ylim(1, n_features)
plt.savefig(r"feature_importance.png", bbox_inches='tight')

export_graphviz(
    treePruned,
    out_file="Heart_Disease_Tree.dot",
    class_names=["Healthy", "Class I", "Class II", "Class III", "Class IV"],
    feature_names=featureNames,
    impurity=False,
    filled=True)

(graph, ) = pydot.graph_from_dot_file('./Heart_Disease_Tree.dot')
graph.write_png('./Heart_Disease_tree.png')
Exemple #48
0
something I could maximize in a for loop?
"""

# Just output the decision tree to a .dot file and then let graphviz convert
# the .dot file to a .png file (an image).
export_graphviz(dt_appx,
                out_file='dt_appx_of_mlp.dot',
                feature_names=bundle_of_data.feature_names,
                class_names=bundle_of_data.target_names,
                filled=True,
                impurity=False,
                rounded=True,
                proportion=True)

export_graphviz(dt_regular,
                out_file='dt_regular.dot',
                feature_names=bundle_of_data.feature_names,
                class_names=bundle_of_data.target_names,
                filled=True,
                impurity=False,
                rounded=True,
                proportion=True)

import pydot

(graph, ) = pydot.graph_from_dot_file('dt_appx_of_mlp.dot')
graph.write_png('dt_appx_of_mlp.png')

(graph, ) = pydot.graph_from_dot_file('dt_regular.dot')
graph.write_png('dt_regular.png')
Exemple #49
0
			   accuracy_dec, accuracy_jan, accuracy_feb, accuracy_mar, \
			   accuracy_apr, accuracy_may, accuracy_jun = \
					evaluate(df, label=l, numerical=False)
		rmse = 'n/a'
	performance.append((l, rmse, accuracy, accuracy_away, accuracy_home, accuracy_heavyfav, \
			   accuracy_closegame, accuracy_homeisfavored, accuracy_awayisfavored, \
			   accuracy_2009, accuracy_2010, accuracy_2011, accuracy_2012, \
			   accuracy_2013, accuracy_2014, accuracy_2015, accuracy_nov, \
			   accuracy_dec, accuracy_jan, accuracy_feb, accuracy_mar, \
			   accuracy_apr, accuracy_may, accuracy_jun))

	if a in ['tree']:
		# create decision tree
		with open(tables_path + 'dtree.dot', 'w') as f:
			f = tree.export_graphviz(m, out_file=f)
		graph = pydot.graph_from_dot_file(tables_path + 'dtree.dot')
		graph.write_pdf(charts_path + 'dtree_%s' % l)


performance = pd.DataFrame(performance,
						   columns=['technique',
						   			'rmse',
						   			'accuracy',
						   			'accuracy_home',
						   			'accuracy_away',
						   			'accuracy_heavyfav',
						   			'accuracy_closegame',
						   			'accuracy_homeisfavored',
						   			'accuracy_awayisfavored',
						   			'accuracy_2009',
						   			'accuracy_2010',
from sklearn.datasets import load_iris
from sklearn import tree
import os
import pydot # need install
print(os.getcwd())
clf = tree.DecisionTreeClassifier(criterion = "entropy") #gini
iris = load_iris()
print(iris.data[0:5])
print(iris.target[0:5])

clf = clf.fit(iris.data, iris.target)

tree.export_graphviz(clf, out_file='0_DT_Tree/tree.dot')         
(graph,) = pydot.graph_from_dot_file('0_DT_Tree/tree.dot')
graph.write_png('0_DT_Tree/tree.png')
Exemple #51
0
 def __convert_dot_to_image(self):
     (graph, ) = pydot.graph_from_dot_file(f'src/static/images/{self.id}.gv')
     graph.write_png(f'src/static/images/{self.id}.png')
Exemple #52
0
    'SCORE', 'VIOLATION CODE_F007', 'VIOLATION CODE_F050',
    'VIOLATION CODE_F054', 'VIOLATION CODE_F014', 'VIOLATION CODE_F023',
    'VIOLATION CODE_F006', 'VIOLATION CODE_F035', 'VIOLATION CODE_F052',
    'VIOLATION CODE_F036', 'VIOLATION CODE_F033'
]
data = inspection_data[cols]
data = pd.DataFrame(data)
y = data['SCORE']
data.drop('SCORE', axis=1, inplace=True)

feature_list = list(data.columns)
# create training and testing vars
X_train, X_test, y_train, y_test = train_test_split(data,
                                                    y,
                                                    test_size=0.3,
                                                    random_state=42)

rf_small = RandomForestRegressor(n_estimators=10, max_depth=3)
rf_small.fit(X_train, y_train)

# Extract the small tree
tree_small = rf_small.estimators_[5]

# Save the tree as a png image
export_graphviz(tree_small,
                out_file='small_tree.dot',
                feature_names=feature_list,
                rounded=True,
                precision=1)
(graph, ) = pydot.graph_from_dot_file('small_tree.dot')
graph.write_png('small_tree.png')
Exemple #53
0
                    required=False)
    ap.add_argument("--pdf",
                    help="path of the pdf that will be exported",
                    required=False)
    ap.add_argument("--svg",
                    help="path of the svg that will be exported",
                    required=False)

    # parsing
    args = ap.parse_args()

    # the design file
    design = pd.read_csv(args.csv)

    # the dot file
    (g, ) = pydot.graph_from_dot_file(args.dot)

    # extract the selected dot node names by their labels
    labels = design["label"].tolist()
    name_to_label = get_label_map(g, labels)
    names = sorted(list(name_to_label.keys()))

    # add the dot node names to the design
    tmp_df = pd.DataFrame.from_records(list(tuple(name_to_label.items())),
                                       columns=["name", "label"])
    design = design.merge(tmp_df, left_on="label", right_on="label")

    # useful maps
    name_to_color = get_map_from_df(design, "name", "color")
    name_to_newlabel = get_map_from_df(design, "name", "new_label")
    name_to_software = get_map_from_df(design, "name", "software")
# Add Transactions
for block_time in block_time_list:
    vin = blocks[block_time]['vin']
    vout = blocks[block_time]['vout']

    for in_address in vin.keys():
        if vin[in_address] > float(attention_money_value):
            dot_data += in_address + '_' + block_time + ' [style = "solid,filled",fillcolor=black,fontcolor=white];\n'
            dot_data += in_address + '_' + block_time + ' -> ' + block_time + ' [label = ' + str(
                vin[in_address]) + ',penwidth=4,color=red ];\n'
        else:
            dot_data += in_address + '_' + block_time + ' -> ' + block_time + ' [label = ' + str(
                vin[in_address]) + ' ];\n'

    for out_address in vout.keys():
        if vout[out_address] > float(attention_money_value):
            dot_data += out_address + '_' + block_time + ' [style = "solid,filled",fillcolor=black,fontcolor=white];\n'
            dot_data += block_time + ' -> ' + out_address + '_' + block_time + ' [label = ' + str(
                vout[out_address]) + ',penwidth=4,color= red];\n'
        else:
            dot_data += block_time + ' -> ' + out_address + '_' + block_time + ' [label = ' + str(
                vout[out_address]) + ' ];\n'

dot_data += '}'

with open(address + '_money_flow.dot', 'w') as file:
    file.write(dot_data)

(graph, ) = pydot.graph_from_dot_file(address + '_money_flow.dot')
graph.write_svg(address + '_money_flow.svg')
Exemple #55
0
def visualize_to_png(filepath, pngfilename):
    (graph, ) = pydot.graph_from_dot_file(filepath)
    #graph.draw(pngfilename)
    graph.write_png(pngfilename)
    check_call(['dot', '-Tpng', filepath, '-o', pngfilename])
Exemple #56
0
def make_money_flow_graph(output_path, target_address, transactions):
    # Make .dot File

    TIME_ZONE_OFFSET = 60 * 60 * 7

    simple_transactions = {}

    for transaction in transactions['txs']:
        try:
            simple_transaction = {}
            simple_transaction['input'] = [{
                'value': float(vin['value']),
                'addr': vin['addr']
            } for vin in transaction['vin']]
            simple_transaction['output'] = [{
                'value':
                float(vout['value']),
                'addr':
                vout['scriptPubKey']['addresses'][0]
            } for vout in transaction['vout']]
        except:
            continue

        block_time = int(transaction['blocktime']) + TIME_ZONE_OFFSET
        simple_transactions[block_time] = simple_transaction

    time_listed_transactions = sorted(simple_transactions.items())
    address_appear_times = {}

    for time_listed_transaction in time_listed_transactions:
        block_time = int(time_listed_transaction[0])

        for input_address in time_listed_transaction[1]['input']:
            if input_address['addr'] not in address_appear_times:
                address_appear_times[input_address['addr']] = [block_time]
            else:
                address_appear_times[input_address['addr']].append(block_time)

        for output_address in time_listed_transaction[1]['output']:
            if output_address['addr'] not in address_appear_times:
                address_appear_times[output_address['addr']] = [block_time]
            else:
                address_appear_times[output_address['addr']].append(block_time)

    for address in address_appear_times.keys():
        address_appear_times[address] = list(set(
            address_appear_times[address]))

    graph_data = "digraph{rankdir=TB;"
    graph_edges = []

    for time_listed_transaction in time_listed_transactions:
        block_time = int(time_listed_transaction[0])
        block_time = datetime.fromtimestamp(block_time).strftime(
            "%Y%m%d_%Hh%Mm%Ss")

        for input_address in time_listed_transaction[1]['input']:
            for output_address in time_listed_transaction[1]['output']:
                graph_edges.append(input_address['addr'] + '_' +
                                   str(block_time) + ' -> ' +
                                   output_address['addr'] + '_' +
                                   str(block_time) + ';\n')

    for address in address_appear_times.keys():
        address_appear_times[address].sort()

        for n in range(len(address_appear_times[address]) - 1):
            graph_edges.append(
                address + '_' +
                datetime.fromtimestamp(float(address_appear_times[address][n])
                                       ).strftime("%Y%m%d_%Hh%Mm%Ss") +
                ' -> ' + address + '_' + datetime.fromtimestamp(
                    float(address_appear_times[address][n + 1])).strftime(
                        "%Y%m%d_%Hh%Mm%Ss") + ';\n')

    graph_edges = list(set(graph_edges))

    for edge in graph_edges:
        graph_data += edge

    graph_data += '}'

    with open(output_path + target_address + '_money_flow_graph.dot',
              'w') as file:
        file.write(graph_data)

    #Make SVG File
    (graph, ) = pydot.graph_from_dot_file(output_path + target_address +
                                          '_money_flow_graph.dot')
    graph.write_svg(output_path + target_address + '_money_flow_graph.svg')
                  indent=4,
                  separators=(',', ': ')))


if __name__ == '__main__':
    parser = ArgumentParser(description='graphwiz to geojson.')
    parser.add_argument('-p', '--poles', help='Poles file name')
    parser.add_argument('-g', '--graphviz', help='Graphviz file name')
    parser.add_argument('-o', '--output', help='Output file name')
    args = parser.parse_args()

    poles_file_name = args.poles or 'downtown-pole-export-edited.csv'
    graphviz_file_name = args.graphviz or 'random-graphviz.txt'
    output_file_name = args.output or 'feature-collection-export.json'

    graph = graph_from_dot_file(graphviz_file_name)
    pole_index = load_known_poles(poles_file_name)

    features = {}

    for edge in graph.get_edge_list():
        source = normalize_edge_name(edge.get_source())
        destination = normalize_edge_name(edge.get_destination())
        found_source = source in pole_index.keys()
        found_destination = destination in pole_index.keys()
        if found_source and found_destination:
            pole_index[source]['mounting'] = mounting_style(edge.get_source())
            pole_index[destination]['mounting'] = mounting_style(
                edge.get_destination())
            features[source + destination] = create_feature(
                pole_index, source, destination)
 arrTestLines = f1.read().strip().split('\n')
 f1.close()
 print('begin {}'.format(fopResultItem))
 for i in range(0, numTests):
     arrTabs = arrTestLines[i].split('\t')
     keyG = arrTabs[3] + arrTabs[4]
     if keyG not in dictGraphVisit.keys():
         dictGraphVisit[keyG] = 1
     else:
         continue
     try:
         fpDot = arrTabs[3] + '/' + arrTabs[4] + '_graph.dot'
         fpPng = arrTabs[3] + '/' + arrTabs[4] + '_graph.png'
         if os.path.exists(fpPng):
             continue
         (graphPydot, ) = pydot.graph_from_dot_file(fpDot)
         graphPydot.write_png(fpPng)
     except:
         traceback.print_exc()
     print('end {} {}'.format(i, arrTestLines[i]))
 for i in range(len(arrTestLines) - 1 - numTests, len(arrTestLines)):
     arrTabs = arrTestLines[i].split('\t')
     keyG = arrTabs[3] + arrTabs[4]
     if keyG not in dictGraphVisit.keys():
         dictGraphVisit[keyG] = 1
     else:
         continue
     try:
         fpDot = arrTabs[3] + '/' + arrTabs[4] + '_graph.dot'
         fpPng = arrTabs[3] + '/' + arrTabs[4] + '_graph.png'
         if os.path.exists(fpPng):
Exemple #59
0
                    dest='formats',
                    action='append',
                    type=str,
                    default=[],
                    help='The graphviz output formats that will be generated.')
parser.add_argument('-l',
                    '--add-graph-label',
                    dest='add_graph_label',
                    default=False,
                    action='store_true',
                    help='Whether to add a label to each generated graph')

if __name__ == '__main__':
    args = parser.parse_args()

    graph = pydot.graph_from_dot_file(args.filename)
    base_filename = args.base_filename or os.path.splitext(args.filename)[0]

    print(graph)
    if isinstance(graph, list):
        graph = graph[0]

    assert isinstance(graph, pydot.Dot)

    phased_graphviz = PhasedGraphviz(graph)
    phased_graphviz.generate(base_filename,
                             generate_gif=args.generate_gif,
                             gif_delay=args.gif_delay,
                             formats=args.formats,
                             add_graph_label=args.add_graph_label)
import pydot

graph = pydot.graph_from_dot_file('w_nottear_loan.dot')
file = open("notear_loan.dot", 'r')
lines = file.readlines()
lines = [line.replace(' ', '') for line in lines]
lines = [line.replace('\n', '') for line in lines]
lines = [line.replace('\t', '') for line in lines]
with open('Final_t.dot', 'w') as f:
    f.writelines(lines)