コード例 #1
0
 def analyzeEndpoint():
     form = MyForm()
     if form.validate_on_submit():
         profile = request.form.get('profile')
         start = time.time()
         CFGForest, taintedSink, stats, err = analyze(
             form.source_code.data, profile)
         end = time.time()
         timetaken = (end - start)
         if err:
             return json.dumps({"error": err.message}), 400
         sinks = []
         profileJson = profiles.getProfiles()
         if profile in profileJson:
             profile = profileJson[profile]
         for sink in taintedSink:
             sinkDict = dict()
             sinkDict['startLine'] = sink.startLine
             sinkDict['endLine'] = sink.endLine
             sinkDict['state'] = sink.state
             sinks.append(sinkDict)
         graph = ""
         if CFGForest:
             graphviz = Source(CFGForest.generateGraphViz(True))
             graphviz.format = 'svg'
             graph = graphviz.pipe().decode('utf-8')
     return json.dumps({
         'sinks': sinks,
         'graph': graph,
         'stats': stats,
         'profile': profile,
         'timetaken': timetaken
     })
コード例 #2
0
    def graficar(self):
        self.grafo = "digraph G {\n" + "graph [rankdir = TB];\n" + "node [shape = record,height=.1];  {\n"

        if self.estaVacia() == True:
            self.grafo += "\"ListaVacia\" [label = \"Lista Vacia\"]"
        else:
            temp = self.inicio
            i = 0
            while temp != None:
                self.grafo += "\"" + str(i) + "\" [label = \"" + str(
                    temp.getDia()) + "\"];\n"
                if i > 0:
                    self.grafo += "\"" + str(i - 1) + "\" -> \"" + str(
                        i) + "\" ;\n"
                    self.grafo += "\"" + str(i) + "\" -> \"" + str(
                        i - 1) + "\" ;\n"

                temp = temp.siguiente
                i = i + 1

        self.grafo += "} labelloc=\"t\"; label=\" LISTA DOBLE DIAS\";}"
        print(self.grafo)
        src = Source(self.grafo)
        src.format = "png"
        src.render('test-output/ListaDobleUsuarios', view=True)
コード例 #3
0
ファイル: depgraph.py プロジェクト: IARI/iHateDarcs
def draw_graph(cwd):
    ui = GraphOptions()
    ui.edit()

    max_patchcount = 2 * ui.limit

    # changes = get_changes(cwd, None, ui.limit, author=Config.AUTHOR)
    from_upstream = get_changes_from_upstream(cwd, Config.UPSTREAM_REPO, None, max_patchcount)
    changes = get_changes(cwd, None, max_patchcount)

    # print('format picked: {}'.format(ui.format))
    dotSrcLines = darcs_show(cwd, ui.limit, ui.filename).splitlines()

    def changeColor(i, color):
        dotSrcLines[i] = dotSrcLines[i][:-2] + 'color="{}"];'.format(color)

    for i, l in enumerate(dotSrcLines):
        m = PATCH_PATTERN.search(l)
        if not m:
            continue
        title = m.group(1).replace("\\n", " ").strip()
        print(title)

        def find_in(lst):
            try:
                return next(c for c in lst if title == c.title)
            except StopIteration:
                return None

        up = find_in(from_upstream)  # type: Patch
        loc = find_in(changes)  # type: Patch
        if up:
            if not loc:
                print("patch in upstream but not local?? {}".format(title))
            if up.author == Config.AUTHOR:
                changeColor(i, ui.acolor)
            else:
                changeColor(i, ui.bcolor)
        elif loc:
            if loc.author == Config.AUTHOR:
                changeColor(i, ui.color)

    dotSrc = "\n".join(dotSrcLines)

    print(dotSrc)

    # for l in enumerate(dotSrc.splitlines()):
    #     print("{}:  {}".format(*l))

    # dot = parse_dot_data(dotSrc)

    # print(dot)
    dot = Source(dotSrc)
    #
    # print(dotObj.source)
    #
    # dot.graph_attr['rankdir'] = 'LR'
    dot.format = ui.format
    dot.render(ui.filename, view=ui.open, cleanup=ui.cleanup)
コード例 #4
0
ファイル: main.py プロジェクト: chaits0921/BDD-Visualizations
def bdd_img(exp):
	# print(exp)
	exp = expr(exp)
	f = expr2bdd(exp)

	gv = Source(f.to_dot())
	gv.format = "png"
	global filename
	filename = str(gv.render('../images/bdd_img' + str(randrange(0, 100))))
コード例 #5
0
def preview_dot(filename, kernel=None, style=None):
    from graphviz import Source
    with open(filename) as dot:
        src = Source(dot.read())
    src.format = 'png'
    outfile = src.render()
    with open(outfile, 'rb') as content:
        data = content.read()
    return {'image/png': base64.b64encode(data).decode('ascii')}
コード例 #6
0
ファイル: preview.py プロジェクト: BoPeng/SOS
def preview_dot(filename):
    from graphviz import Source

    with open(filename) as dot:
        src = Source(dot.read())
    src.format = "png"
    outfile = src.render()
    with open(outfile, "rb") as content:
        data = content.read()
    return {"image/png": base64.b64encode(data).decode("ascii")}
コード例 #7
0
ファイル: evaluation.py プロジェクト: buptxz/EQL_Tensorflow
def expression_graph_as_png(expr, output_file, view=True):
    """ Save a PNG of rendered graph (graphviz) of the symbolic expression.
    :param expr: sympy expression
    :param output_file: string with .png extension
    :param view: set to True if system default PNG viewer should pop up
    :return: None
    """
    assert output_file.endswith('.png')
    graph = Source(dotprint(expr))
    graph.format = 'png'
    graph.render(output_file.rpartition('.png')[0], view=view, cleanup=True)
コード例 #8
0
def grafic(grafo, name):
    global ruta, lista
    if path.exists('' + ruta + '\\' + name + '.dot'):
        os.remove('' + ruta + '\\' + name + '.dot')
        os.remove('' + ruta + '\\' + name + '.dot.png')
        #d = Digraph(format='png')
        d = Source(str(grafo))
        #d.source(grafo)
        d.format = 'png'
        d.render('' + name + '.dot', ruta, view=True)
    else:
        #d = Digraph(format='png')
        d = Source(str(grafo))
        #d.source(grafo)
        d.format = 'png'
        #d.format='pdf'
        d.render('' + name + '.dot', ruta, view=True)
        im = Image.open(ruta + '\\' + name + '.dot.png')
        imagen = im.convert('RGB')
        lista.append(imagen)
コード例 #9
0
 def graph(self, format='svg'):
     try:
         from graphviz import Source
     except ImportError:
         raise ImportError("You don't have graphviz package installed.\n"
                           "Please install graphviz or use write_file function to save dot file and generate the "
                           "graph manually.")
     dot = self.generate()
     graph = Source(dot)
     graph.format = format
     return graph
コード例 #10
0
def expression_graph_as_png(expr, output_file, view=True):
    """ Save a PNG of rendered graph (graphviz) of the symbolic expression.
    :param expr: sympy expression
    :param output_file: string with .png extension
    :param view: set to True if system default PNG viewer should pop up
    :return: None
    """
    assert output_file.endswith('.png')
    graph = Source(dotprint(expr))
    graph.format = 'png'
    graph.render(output_file.rpartition('.png')[0], view=view, cleanup=True)
コード例 #11
0
    def build(self, label=""):
        (_, result) = self.build_node(self.__root)
        result = [
            'digraph Tree {\n', 'node [shape=box];\n', 'label="' + label + '";'
        ] + result + ['}\n']

        text = ""
        for line in result:
            text += line + "\n"
        dot = Source(text)
        dot.format = 'png'
        dot.render(self.__filename)
        os.remove(self.__filename)
コード例 #12
0
ファイル: run_angus.py プロジェクト: herberthobregon/compi2py
def debug_ast(txt: str, cnxx: any, console_handlerx: any) -> ASTNode:
    global cnx, console_handler, next_ats
    console_handler = console_handlerx
    cnx = cnxx
    lexer = ply.lex.lex(module=lex1)
    parser = ply.yacc.yacc(module=yacc1, tabmodule="yacc1_tab")
    root: ASTNode = parser.parse(txt)  # the input

    dot = Source(root.to_str())
    dot.format = 'png'
    dot.render('graph')

    lista = root.genarar_lista()
    next_ats = debug(lista)
    return next_ats
コード例 #13
0
    def graficarAVL(self):
        self.grafo = "digraph G {node[shape=record, height=0.1];\n"
        self.grafo += "{ \n"

        if self.raiz == None:
            print("La raiz es nula")
            self.grafo += "arbolvacio [label = \"Arbol Vacio\",shape=record];\n"
        else:
            self.graficarNodo(self.raiz)

        self.grafo += "} \n"
        self.grafo += "labelloc=\"t\";\nlabel=\" AVL\";}"
        print(self.grafo)
        src = Source(self.grafo)
        src.format = "png"
        src.render('test-output/AVLTree', view=True)
コード例 #14
0
ファイル: run_angus.py プロジェクト: herberthobregon/compi2py
def get_ast(txt: str, cnxx: any, console_handlerx: any) -> ASTNode:
    global cnx, console_handler
    console_handler = console_handlerx
    cnx = cnxx
    lexer = ply.lex.lex(module=lex1)
    parser = ply.yacc.yacc(module=yacc1, tabmodule="yacc1_tab")
    root: ASTNode = parser.parse(input=txt, lexer=lexer)  # the input

    dot = Source(root.to_str())
    dot.format = 'svg'
    dot.render('report_angus_graph')

    lista = root.genarar_lista()
    run(lista)
    print("\n".join([str(i) for i in globales.sym_table]))
    parser.restart()
    return root
コード例 #15
0
def exec_swipl(code):
    temp_path, output_path, code_path, dirpath = setup_env()
    with open(temp_path, 'w') as f:
        f.write(code)
    ''' Parser '''
    textlist = []
    graphviz = False
    with open(temp_path) as lines:
        '''
        Parse for graphviz input
        '''
        for line in lines:
            textlist.append(line)
            if '%graphviz' in line:
                textlist.pop()
                textlist.append("?- print('GRAPHVIZ').\n")
                graphviz = True  # output is graphviz
    with open(code_path, 'w') as rules:
        rules.write(''.join(textlist))
    ''' Get Prolog Output from code.pl and assign to output_path file '''
    os.system("swipl {0:s} > {1:s}  2>&1 ".format(code_path, output_path))
    ''' Remove Prolog welcome text '''
    base = open(output_path, 'r')
    lines = base.readlines()
    base.close()
    lines = lines[:-9]

    outputlist = []
    for line in lines:
        outputlist.append(line)
        if 'GRAPHVIZ' in line:
            outputlist = []
    with open(output_path, 'w') as rules:
        rules.write(''.join(outputlist))
    ''' Return final output '''
    final = open(output_path, 'r')
    output = final.read()

    if graphviz == True:
        src = Source(output)
        src.format = 'png'
        print(output)
        output = src.render('image.gv')
    return output
コード例 #16
0
    def graficar(self):
        self.grafo = "digraph G {\n" + "node [shape = record,height=.1];  {\n"

        if self.estaVacia() == True:
            self.grafo += "\"ListaVacia\" [label = \"Lista Circular Vacia\"]"
        else:
            temp = self.inicio
            i = 1
            while True:
                self.grafo += "\"" + str(
                    i) + "\" [label = \"" + "Nombre: " + temp.getName()
                self.grafo += "\\nContraseña: " + temp.getPassword() + "\"];\n"
                temp = temp.siguiente
                if temp == self.inicio:
                    self.grafo += "\"" + str(i) + "\" -> \"" + str(
                        1) + "\" ;\n"
                    self.grafo += "\"" + str(1) + "\" -> \"" + str(
                        i) + "\" ;\n"
                else:
                    self.grafo += "\"" + str(
                        i + 1
                    ) + "\" [label = \"" + "Nombre: " + temp.siguiente.getName(
                    )
                    self.grafo += "\\nContraseña: " + temp.siguiente.getPassword(
                    ) + "\"];\n"

                    self.grafo += "\"" + str(i) + "\" -> \"" + str(
                        i + 1) + "\" ;\n"
                    self.grafo += "\"" + str(i + 1) + "\" -> \"" + str(
                        i) + "\" ;\n"
                i = i + 1
                if (temp == self.inicio):
                    break

        self.grafo += "} labelloc=\"t\"; label=\" LISTA CIRCULAR DOBLE\";}"
        print(self.grafo)
        src = Source(self.grafo)
        src.format = "png"
        src.render('test-output/ListaCircularDoble', view=True)


#***********************************************************************************#
コード例 #17
0
    def build(self):
        if not self._proto_module:
            raise ValueError("No Protobuf Python module!")
        if not self._rendered_filename:
            raise ValueError("No output location!")
        if not self._file_format:
            raise ValueError("No file format!")

        uml_template = _get_uml_template(self._proto_module)

        if logger.isEnabledFor(logging.DEBUG):
            logger.debug("UML template:")
            logger.debug(uml_template)

        src = Source(uml_template)
        src.format = self._file_format
        logger.info(
            f"Writing diagram to {self._rendered_filename}.{self._file_format}"
        )
        src.render(filename=self._rendered_filename, view=False, cleanup=True)
コード例 #18
0
    def graficar(self):
        grafo = "digraph g {\n graph [rankdir=UD];\n node [shape = record,height=.1];  { \n"

        i = 0
        for nodo in self.table:
            if nodo != None:
                grafo += "\"" + str(i) + "\" [label = \"" + "Pos: " + str(
                    nodo.getPosicion())
                grafo += "\\nNombre: " + nodo.getNombre(
                ) + "\\nDescripcion: " + nodo.getDescripcion()
                grafo += "\\nDireccion: " + nodo.getDireccion(
                ) + "\\nHora: " + nodo.getHora() + "\"];\n"
                if i > 0:
                    grafo += "\"" + str(i - 1) + "\" -> \"" + str(
                        i) + "\"[dir=none] ;\n"
                i = i + 1
        grafo += "}  labelloc=\"t\"; label=\" TABLA HASH EVENTOS\";}"
        print(grafo)
        src = Source(grafo)
        src.format = "png"
        src.render('test-output/TablaHash', view=True)
コード例 #19
0
def main(proto: str, output: Path):
    type_mapping = {}
    message_mapping = {}
    types = {}
    proto_file = _module(proto)
    _build_mappings(proto_file, types, type_mapping, message_mapping)

    uml_template = _get_uml_template(types=types,
                                     type_mapping=type_mapping,
                                     message_mapping=message_mapping)

    if logger.isEnabledFor(logging.DEBUG):
        logger.debug("UML template:")
        logger.debug(uml_template)

    src = Source(uml_template)
    src.format = "png"
    rendered_filename = str(
        output.joinpath(_get_uml_filename(proto_file.__file__)))
    logger.info(f"Writing PNG diagram to {rendered_filename}.png")
    src.render(filename=rendered_filename, view=False, cleanup=True)
コード例 #20
0
    def graficarAB(self):
        pagina = 0
        self.grafo = "digraph G { node[shape=record, height=0.1];\n"
        self.grafo += "{ \n"
        #print(self.raiz.claves[0].nombre)
        if self.raiz == None:
            print("La raiz es nula")
            self.grafo += "arbolvacio [label = \"Arbol Vacio\",shape=record];\n"
        else:
            self.grafo += self.graficarNodoAB()

        self.grafo += "} \n"
        self.grafo += "labelloc=\"t\";\nlabel=\" ARBOL B\";}"
        print(self.grafo)
        src = Source(self.grafo)
        src.format = "png"
        src.render('test-output/ArbolB', view=True)
        #return self.grafo


#***********************************************************************************#
コード例 #21
0
def render_dot(dot, format):
    """
    Clunky means to render and return the dot string. We render to a temp
    file, then read that back in
    """

    # create a temporary file and render to it, we will then
    # load and return this file to the caller
    temp = tempfile.NamedTemporaryFile(delete=False)
    outfile_name = os.path.splitext(temp.name)[0]

    # create a graphviz source to hold the dot data and set
    # it to the requested format
    source = Source(dot)
    source.format = format

    # now render to the temp file, the render will add the extension based
    # on the format
    filepath = source.render(filename=outfile_name,
                             format=format,
                             cleanup=True)

    # flush to disk and close
    temp.flush()
    temp.close()

    file_stream = None

    # read the file back in
    with open(filepath, "rb") as fp:
        file_stream = io.BytesIO(fp.read())

    # now remove it
    if os.path.exists(filepath):
        os.unlink(filepath)

    # return the bytes
    return file_stream
コード例 #22
0
def get_ast(txt: str, cnxx: any, console_handlerx: any):
    global cnx, console_handler
    globales2.restart_all()
    console_handler = console_handlerx
    cnx = cnxx
    lexer = ply.lex.lex(module=lex2)
    globales2.entrada = txt
    parser = ply.yacc.yacc(module=yacc2, tabmodule="yacc2_tab", start='init')
    root: ASTNode = parser.parse(input=txt, lexer=lexer)  # the input

    dot = Source(root.to_str())
    dot.format = 'svg'
    dot.render('report_minorc_graph')

    lista: List[ASTNode] = []
    for sentencias in root.childs[0]:
        for s in sentencias:
            lista.append(s)

    gen = Generator(lista)
    st = gen.get3d()
    # print("\n".join([str(i) for i in globales2.sym_table]))
    parser.restart()
    return st
コード例 #23
0
    def plotPipelineStructure(self):
        '''
        Plot pipeline structure

        @return iPython display object
        '''

        #Graph setup
        g1 = gv.Digraph(format='svg')
        g1.graph_attr['rankdir'] = 'LR'
        g1.node_attr['shape'] = 'rounded'
        g1.node_attr['fontname'] = 'Arial'
        g1.node_attr['fontsize'] = '9'
        g1.node_attr['style'] = 'filled'
        g1.node_attr['margin'] = '0.1'
        g1.node_attr['height'] = '0.1'
        g1.node_attr['fillcolor'] = '#fff7da'
        #g1.node_attr['shape'] = 'plaintext'  #use this to remove boxes around nodes

        nodelist = self.getMetadataNestedGraph()

        print(nodelist)
        src = Source(nodelist)
        print(dir(src))
        src.format = 'svg'
        src.render('img/plotPipelineStructure')

        #        for s in nodelist:
        #            g1.node(s)
        #        g1.edges(zip(nodelist, nodelist[1:]))
        #        g1.render('img/plotPipelineStructure')
        #        print(nodelist)
        #        print(g1.source)
        #        return display(SVG('img/plotPipelineStructure.svg'))

        return display(SVG('img/plotPipelineStructure.svg'))
コード例 #24
0
    def graficar(self):
        grafo = "digraph G {\n" + "rankdir = TB;\n" + "rank = min;\n" + "node[style=filled,shape=box, label=\"Inicio\", rankdir=UD];\n"

        tempX = self.ejeX.inicio
        tempXinterno = None
        tempY = self.ejeY.inicio

        j = 0
        i = 0

        while tempY != None:
            if tempY == self.ejeY.inicio:
                grafo += "\"" + str(i) + "," + str(
                    j) + "\"[label=\"raiz\", style=filled];\n"
                i += 1
                while tempX != None:
                    grafo += "\"" + str(i) + "," + str(
                        j) + "\"[label=\"" + tempX.getIndice(
                        ) + "\", style=filled];\n"
                    i += 1
                    tempX = tempX.siguiente
                i = 0
                j += 1
            tempXinterno = tempY.listaNodos.inicio
            tempX = self.ejeX.inicio
            grafo += "\"" + str(i) + "," + str(
                j) + "\"[label=\"" + tempY.getIndice() + "\", style=filled];\n"
            i += 1
            while tempX != None:
                if tempXinterno != None:
                    if tempXinterno.padreX == tempX:
                        grafo += "\"" + str(i) + "," + str(
                            j) + "\"[label=\"Dia: " + str(
                                tempXinterno.getDia()) + "\", style=filled];\n"
                        tempXinterno = tempXinterno.derecha
                    else:
                        grafo += "\"" + str(i) + "," + str(
                            j) + "\"[label=\"no existe\", style=filled];\n"
                else:
                    grafo += "\"" + str(i) + "," + str(
                        j) + "\"[label=\"no existe\", style=filled];\n"
                i += 1
                tempX = tempX.siguiente
            i = 0
            j += 1
            tempY = tempY.siguiente
        print(str(i))
        print(str(j))
        tempX = self.ejeX.inicio
        while tempX != None:
            i += 1
            tempX = tempX.siguiente
        print(str(i))
        print(str(j))
        i += 1
        for y in range(0, j):
            for x in range(0, i - 1):
                grafo += "\"" + str(x) + "," + str(y) + "\" -> \"" + str(
                    x + 1) + "," + str(y) + "\"[constraint=false];\n"
                grafo += "\"" + str(x + 1) + "," + str(y) + "\" -> \"" + str(
                    x) + "," + str(y) + "\"[constraint=false];\n"
                grafo += "{rank=same;\"" + str(x) + "," + str(
                    y) + "\" \"" + str(x + 1) + "," + str(y) + "\"}\n"
                grafo += "{rank=same;\"" + str(x + 1) + "," + str(
                    y) + "\" \"" + str(x) + "," + str(y) + "\"}\n"

        for y in range(0, j - 1):
            for x in range(0, i):
                grafo += "\"" + str(x) + "," + str(y) + "\" -> \"" + str(
                    x) + "," + str(y + 1) + "\"[rankdir=UD];\n"
                grafo += "\"" + str(x) + "," + str(y + 1) + "\" -> \"" + str(
                    x) + "," + str(y) + "\"[rankdir=UD];\n"

        grafo += "labelloc=\"t\"; label=\" MATRIZ DISPERSA CALENDARIO\";}"
        print(grafo)

        src = Source(grafo)
        src.format = "png"
        src.render('test-output/MatrizDispersa', view=True)
コード例 #25
0
ファイル: automata.py プロジェクト: roussePaul/pwa
					node.attr['label'] = str(n) + "\n" + str(d['label'])
			for n,c in colors.items():
				node = A.get_node(n)
				node.attr['color'] = c
				node.attr['style'] = 'filled'
				print n,c
			for n,c in edges_color.items():
				node = A.get_edge(*n)
				node.attr['color'] = c
				node.attr['penwidth'] = 2
				print n,c

		except Exception,e:
			print "Error on printing graph: ",e
		src = Source(A)
		src.format = 'png'
		src.render("test-output/"+name,view=True)

	def minimize(self):
		for n in self.nodes():
			path_exist = False
			for m in self.graph['initial']:
				if nx.has_path(self,m,n):
					path_exist = True
					break
			if path_exist==False:
				self.remove_node_init_accept(n)

	def run_trace(self,state_trace):
		labels = nx.get_node_attributes(self,'label')
		return [labels[n] for n in state_trace]
コード例 #26
0
	Certs -> Courses [label=clean]
	Tracks_s -> Tracks [label=""]
	Certs -> Tracks [label="Fix missing letters"]
	Tracks -> Excel [label=""]
	Courses -> Excel [label=filter]
	Topics -> Excel [label=""]
	Tracks -> Chart [label=reshape]
	Courses -> Chart [label=reshape]
	Tracks -> Document [label=TODO]
	Courses -> Document [label=TODO]
	Chart -> Document [label=TODO]
	Tracks -> Dataset [label=TODO]
	Courses -> Dataset [label=TODO]
}""")

dot_src.format = 'png'
print(dot.source)
dot_src.render('test-output/datacamp_certs.gv', view=True)
dot
#g = Graph('G', filename='process.gv', engine='sfdp')
#
#g.edge('run', 'intr')
#g.edge('intr', 'runbl')
#g.edge('runbl', 'run')
#g.edge('run', 'kernel')
#g.edge('kernel', 'zombie')
#g.edge('kernel', 'sleep')
#g.edge('kernel', 'runmem')
#g.edge('sleep', 'swap')
#g.edge('swap', 'runswap')
#g.edge('runswap', 'new')
コード例 #27
0
clf.fit(X, y)

#Нарисуем дерево
import os
os.environ["PATH"] += os.pathsep + 'E:/Anaconda3/Library/bin/graphviz'
graph = Source(
    tree.export_graphviz(clf,
                         feature_names=list(X),
                         out_file="tree.dot",
                         class_names=['Died', 'Survived'],
                         filled=True))

with open("tree.dot") as f:
    dot_graph = f.read()
g = Source(dot_graph)
g.format = "png"
g.render("file_name")  #дерево получилось слишком глубоким и большим

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.33, random_state=42)  #тестовое множество 33%
X_train.shape  #596 наблюдений
X_test.shape  #295 наблюдений
#Посмотрим, насколько плохо было получившееся дерево
clf.score(X, y)  #0.9797979797979798
clf.fit(
    X_train, y_train
)  #обучим часть модели и посмотрим точность классификации на обучающем множестве
clf.score(X_train, y_train)  #0.9798657718120806
#Какая точность на данных, который классификатор не "видел"?:
コード例 #28
0
# lets visualize the tree
import os
os.environ["PATH"] += os.pathsep + 'C:\\Program Files (x86)\\Graphviz2.38\\bin'
from graphviz import Source
from sklearn import tree
Source(
    tree.export_graphviz(decision_tree2,
                         out_file=None,
                         feature_names=X.columns))
from IPython.display import SVG
graph = Source(
    tree.export_graphviz(decision_tree2,
                         out_file=None,
                         feature_names=X.columns))
SVG(graph.pipe(format='svg'))
graph = Source(
    tree.export_graphviz(decision_tree2,
                         out_file=None,
                         feature_names=X.columns))
graph.format = 'png'
graph.render('decision_tree2_render', view=True)
graph = Source(
    tree.export_graphviz(decision_tree2,
                         out_file=None,
                         feature_names=X.columns))
png_bytes = graph.pipe(format='png')
with open('decision_tree2.png', 'wb') as f:
    f.write(png_bytes)

from IPython.display import Image
Image(png_bytes)
コード例 #29
0
ファイル: comparison.py プロジェクト: kfly89/pm4py-core
def apply(fp1, fp2, parameters=None):
    """
    Visualize a comparison between two footprint tables

    Parameters
    ---------------
    fp1
        Footprints associated to the log (NOT a list)
    fp2
        Footprints associated to the model
    parameters
        Parameters of the algorithm, including:
            - Parameters.FORMAT => Format of the visualization

    Returns
    ---------------
    gviz
        Graphviz object
    """
    if parameters is None:
        parameters = {}

    if type(fp1) is list or type(fp2) is list:
        raise Exception(
            "footprints visualizer does not work on list of footprints!")

    activities = sorted(
        list(
            set(x[0] for x in fp1["sequence"]).union(
                set(x[1] for x in fp1["sequence"])).union(
                    set(x[0] for x in fp1["parallel"])).union(
                        set(x[1] for x in fp1["parallel"]))))
    fp_table = {}

    for a1 in activities:
        fp_table[a1] = {}
        for a2 in activities:
            fp_table[a1][a2] = ("black", XOR_SYMBOL)

    for x in fp1["sequence"]:
        if x not in fp1["parallel"]:
            if x in fp2["sequence"]:
                fp_table[x[0]][x[1]] = ("black", SEQUENCE_SYMBOL)
                fp_table[x[1]][x[0]] = ("black", PREV_SYMBOL)
            else:
                fp_table[x[0]][x[1]] = ("red", SEQUENCE_SYMBOL)
                fp_table[x[1]][x[0]] = ("red", PREV_SYMBOL)
    for x in fp1["parallel"]:
        if x in fp2["parallel"]:
            fp_table[x[0]][x[1]] = ("black", PARALLEL_SYMBOL)
        else:
            fp_table[x[0]][x[1]] = ("red", PARALLEL_SYMBOL)

    image_format = exec_utils.get_param_value(Parameters.FORMAT, parameters,
                                              "png")

    filename = tempfile.NamedTemporaryFile(suffix='.gv')

    footprints_table = [
        "digraph {\n", "tbl [\n", "shape=plaintext\n", "label=<\n"
    ]
    footprints_table.append(
        "<table border='0' cellborder='1' color='blue' cellspacing='0'>\n")

    footprints_table.append("<tr><td></td>")
    for act in activities:
        footprints_table.append("<td><b>" + act + "</b></td>")
    footprints_table.append("</tr>\n")
    for a1 in activities:
        footprints_table.append("<tr><td><b>" + a1 + "</b></td>")
        for a2 in activities:
            footprints_table.append("<td><font color=\"" +
                                    fp_table[a1][a2][0] + "\">" +
                                    fp_table[a1][a2][1] + "</font></td>")
        footprints_table.append("</tr>\n")

    footprints_table.append("</table>\n")
    footprints_table.append(">];\n")
    footprints_table.append("}\n")

    footprints_table = "".join(footprints_table)

    gviz = Source(footprints_table, filename=filename.name)
    gviz.format = image_format

    return gviz
コード例 #30
0
def apply(fp: Dict[str, Any], parameters: Optional[Dict[Union[str, Parameters], Any]] = None) -> Source:
    """
    Visualize a footprints table

    Parameters
    ---------------
    fp
        Footprints
    parameters
        Parameters of the algorithm, including:
            - Parameters.FORMAT => Format of the visualization

    Returns
    ---------------
    gviz
        Graphviz object
    """
    if parameters is None:
        parameters = {}

    if type(fp) is list:
        raise Exception("footprints visualizer does not work on list of footprints!")

    activities = sorted(list(set(x[0] for x in fp["sequence"]).union(set(x[1] for x in fp["sequence"])).union(
        set(x[0] for x in fp["parallel"])).union(set(x[1] for x in fp["parallel"]))))
    fp_table = {}

    for a1 in activities:
        fp_table[a1] = {}
        for a2 in activities:
            fp_table[a1][a2] = XOR_SYMBOL

    for x in fp["sequence"]:
        if x not in fp["parallel"]:
            fp_table[x[0]][x[1]] = SEQUENCE_SYMBOL
            fp_table[x[1]][x[0]] = PREV_SYMBOL

    for x in fp["parallel"]:
        fp_table[x[0]][x[1]] = PARALLEL_SYMBOL

    image_format = exec_utils.get_param_value(Parameters.FORMAT, parameters, "png")

    filename = tempfile.NamedTemporaryFile(suffix='.gv')

    footprints_table = ["digraph {\n", "tbl [\n", "shape=plaintext\n", "label=<\n"]
    footprints_table.append("<table border='0' cellborder='1' color='blue' cellspacing='0'>\n")
    footprints_table.append("<tr><td></td>")
    for act in activities:
        footprints_table.append("<td><b>"+act+"</b></td>")
    footprints_table.append("</tr>\n")
    for a1 in activities:
        footprints_table.append("<tr><td><b>"+a1+"</b></td>")
        for a2 in activities:
            footprints_table.append("<td>"+fp_table[a1][a2]+"</td>")
        footprints_table.append("</tr>\n")

    footprints_table.append("</table>\n")
    footprints_table.append(">];\n")
    footprints_table.append("}\n")

    footprints_table = "".join(footprints_table)

    gviz = Source(footprints_table, filename=filename.name)
    gviz.format = image_format

    return gviz
コード例 #31
0
ファイル: monitoring.py プロジェクト: makaryb/ml_1sem_5kurs
def monitoringWorker(x_train, y_train, x_test, y_test):
    crit = 'entropy'

    t1 = time()
    clf_tree = DecisionTreeClassifier(criterion=crit,
                                      max_depth=19,
                                      random_state=20,
                                      presort=True)

    clf_tree.fit(X=x_train, y=y_train)

    err_train = round(np.mean(y_train != clf_tree.predict(x_train)) * 100, 4)
    err_test = round(np.mean(y_test != clf_tree.predict(x_test)) * 100, 4)

    t = -(t1 - time())

    print(
        "Глубина дерева: {}, ошибка на обучающей: {}, ошибка на тестовой: {}, время {}"
        .format(clf_tree.get_depth(), err_train, err_test, t))

    dotfile = export_graphviz(
        clf_tree,
        class_names=[
            'ArrayIndexOutOfBoundsException', 'BadTokenException',
            'CalledFromWrongThreadException', 'ClassNotFoundException',
            'IllegalMonitorStateExceptio', 'IllegalStateException',
            'InternalError', 'NetworkOnMainThreadException',
            'NoClassDefFoundError', 'NullPointerException', 'OutOfMemoryError',
            'RuntimeException', 'NormalBehaviou'
        ],
        out_file=None,
        filled=True,
        node_ids=True)
    graph = Source(dotfile)
    # # Сохраним дерево как toy_example_tree_X.png, где Х - entropy или gini, критерий качестве разбиения
    graph.format = 'png'
    graph.render(str(clf_tree.get_depth()) +
                 "tree_example_tree_{}".format(crit),
                 view=True)

    knn = KNeighborsRegressor(n_neighbors=6,
                              weights='uniform',
                              algorithm='auto',
                              leaf_size=30,
                              p=2,
                              metric='minkowski',
                              metric_params=None,
                              n_jobs=None)
    knn.fit(x_train, y_train)

    predictions_test = knn.predict(x_test)
    predictions_test = pd.DataFrame({"error_name": predictions_test})

    mae = mean_absolute_error(y_test, predictions_test)

    print("Для KNeighborsRegressor:\n")
    print("Для n_neighbours = ", 6)
    print('mean_squared_log_error:\t%.5f' %
          mean_squared_log_error(y_test, predictions_test))
    print('mean_absolute_error:\t(пункты)%.4f' %
          mean_absolute_error(y_test, predictions_test))
    print("Median Absolute Error: " +
          str(round(median_absolute_error(predictions_test, y_test), 2)))
    RMSE = round(sqrt(mean_squared_error(predictions_test, y_test)), 2)
    print("Root mean_squared_error: " + str(RMSE))
    print("\n")
    print("\n")

    t1 = time()
    while True:
        if -(t1 - time()) >= 0.25:

            createDataset(dataset_0, dataset_1, dataset_2, dataset_3,
                          dataset_4, dataset_5, dataset_6, dataset_7,
                          dataset_8, dataset_9, dataset_10, dataset_11,
                          dataset_12, dataset_13, dataset_14, dataset_15,
                          dataset_16, dataset_17, dataset_18, dataset_19,
                          dataset_20, dataset_21, dataset_22, dataset_23,
                          dataset_24, dataset_25, dataset_26, dataset_27,
                          dataset_28, dataset_29, dataset_30, dataset_31,
                          dataset_32, dataset_33, dataset_34, dataset_35,
                          dataset_36, dataset_37, dataset_38, dataset_39)

            data1 = pd.read_csv('monitoring/state.csv')
            print(data1)

            df_knn = data1[[
                'VmPeak', 'VmSize', 'VmLck', 'VmHWM', 'VmRSS', 'VmData',
                'VmLib', 'VmPTE', 'VmSwap', 'Threads',
                'voluntary_ctxt_switches', 'nonvoluntary_ctxt_switches',
                'crash'
            ]]
            df_knn.apply(pd.to_numeric)

            df_knn = shuffle(df_knn)

            x_train, x_test, y_train, y_test = train_test_split(
                df_knn[[
                    'VmPeak', 'VmSize', 'VmLck', 'VmHWM', 'VmRSS', 'VmData',
                    'VmLib', 'VmPTE', 'VmSwap', 'Threads',
                    'voluntary_ctxt_switches', 'nonvoluntary_ctxt_switches'
                ]],
                df_knn['crash'],
                test_size=0.5,
                random_state=42)

            predictions1 = knn.predict(x_train.fillna(0))
            predictions1 = pd.DataFrame({'crash': predictions1})

            value = int(predictions1['crash'].iloc[0])
            coef = predictions1['crash'].iloc[0] - value
            if coef > 0.5:
                value = value + 1

            if coef == 0:
                probability = 100
            elif coef > 0.5:
                probability = coef * 100
            else:
                probability = (1 - coef) * 100

            probability = probability - (mae * 100)

            if value == 1:
                print(
                    "/***************************************************\n\nWARNING: ",
                    "NullPointerException ", "probability:", probability, "%",
                    "\n\n***************************************************/")
            elif value == 2:
                print(
                    "/***************************************************\n\nWARNING: ",
                    "ArrayIndexOutOfBoundsException", "probability:",
                    probability, "%",
                    "\n\n***************************************************/")
            elif value == 3:
                print(
                    "/***************************************************\n\nWARNING: ",
                    "OutOfMemoryError", "probability:", probability, "%",
                    "\n\n***************************************************/")
            elif value == 4:
                print(
                    "/***************************************************\n\nWARNING: ",
                    "ClassNotFoundException", "probability:", probability, "%",
                    "\n\n***************************************************/")
            elif value == 5:
                print(
                    "/***************************************************\n\nWARNING: ",
                    "IllegalMonitorStateException", "probability:",
                    probability, "%",
                    "\n\n***************************************************/")
            elif value == 6:
                print(
                    "/***************************************************\n\nWARNING: ",
                    "NetworkOnMainThreadException", "probability:",
                    probability, "%",
                    "\n\n***************************************************/")
            elif value == 7:
                print(
                    "/***************************************************\n\nWARNING: ",
                    "NoClassDefFoundError", "probability:", probability, "%",
                    "\n\n***************************************************/")
            elif value == 8:
                print(
                    "/***************************************************\n\nWARNING: ",
                    "CalledFromWrongThreadException", "probability:",
                    probability, "%",
                    "\n\n***************************************************/")
            elif value == 9:
                print(
                    "/***************************************************\n\nWARNING: ",
                    "BadTokenException", "probability:", probability, "%",
                    "\n\n***************************************************/")
            elif value == 10:
                print(
                    "/***************************************************\n\nWARNING: ",
                    "IllegalStateException", "probability:", probability, "%",
                    "\n\n***************************************************/")
            elif value == 11:
                print(
                    "/***************************************************\n\nWARNING: ",
                    "InternalError", "probability:", probability, "%",
                    "\n\n***************************************************/")
            elif value == 12:
                print(
                    "/***************************************************\n\nWARNING: ",
                    "RuntimeException", "probability:", probability, "%",
                    "\n\n***************************************************/")
            else:
                print(
                    "/***************************************************\n\nALL RIGHT: ",
                    "NormalBehaviour", "probability:", probability, "%",
                    "\n\n***************************************************/")

            t1 = time()
コード例 #32
0
ファイル: plotGraphviz.py プロジェクト: doorfsimite/the-One
import graphviz
from graphviz import Source

arq = open("UFAM_EPIDEMIC_MultiCast_emauc_trace_AdjacencyGraphvizReport.txt",
           'r')
#arq = open("gteste.txt",'r')
arq_text = arq.read()

#h = graphviz.Graph(format='png')

print(arq_text)
src = Source(arq_text)
src.format = 'png'
src.render()
print(src)
コード例 #33
0
def apply(log, aligned_traces, parameters=None):
    """
    Gets the alignment table visualization from the alignments output

    Parameters
    -------------
    log
        Event log
    aligned_traces
        Aligned traces
    parameters
        Parameters of the algorithm

    Returns
    -------------
    gviz
        Graphviz object
    """
    if parameters is None:
        parameters = {}

    variants_idx_dict = variants_filter.get_variants_from_log_trace_idx(log, parameters=parameters)

    variants_idx_list = []
    for variant in variants_idx_dict:
        variants_idx_list.append((variant, variants_idx_dict[variant]))
    variants_idx_list = sorted(variants_idx_list, key=lambda x: len(x[1]), reverse=True)

    image_format = parameters["format"] if "format" in parameters else "png"

    table_alignments_list = ["digraph {\n", "tbl [\n", "shape=plaintext\n", "label=<\n"]
    table_alignments_list.append("<table border='0' cellborder='1' color='blue' cellspacing='0'>\n")

    table_alignments_list.append("<tr><td>Variant</td><td>Alignment</td></tr>\n")

    for index, variant in enumerate(variants_idx_list):
        al_tr = aligned_traces[variant[1][0]]
        table_alignments_list.append("<tr>")
        table_alignments_list.append(
            "<td><font point-size='9'>Variant " + str(index + 1) + " (" + str(len(variant[1])) + " occurrences)<br />" + variant[
                0] + "</font></td>")
        table_alignments_list.append("<td><font point-size='6'><table border='0'><tr>")
        for move in al_tr['alignment']:
            move_descr = str(move[1]).replace(">", "&gt;")
            if not move[0][0] == ">>" or move[0][1] == ">>":
                table_alignments_list.append("<td bgcolor=\"green\">" + move_descr + "</td>")
            elif move[0][1] == ">>":
                table_alignments_list.append("<td bgcolor=\"violet\">" + move_descr + "</td>")
            elif move[0][0] == ">>":
                table_alignments_list.append("<td bgcolor=\"gray\">" + move_descr + "</td>")
        table_alignments_list.append("</tr></table></font></td>")
        table_alignments_list.append("</tr>")

    table_alignments_list.append("</table>\n")
    table_alignments_list.append(">];\n")
    table_alignments_list.append("}\n")

    table_alignments = "".join(table_alignments_list)

    filename = tempfile.NamedTemporaryFile(suffix='.gv')

    gviz = Source(table_alignments, filename=filename.name)
    gviz.format = image_format

    return gviz
コード例 #34
0
y_pred=clf.predict(xv_b)
kappa=cohen_kappa_score(y_v,y_pred)
score_2 = cross_val_score(clf, x_b, y, cv=k_fold, n_jobs=-1)
print('Average accuracy:', np.mean(score_2))
print("Kappa: ", kappa)
print("Score DT: ", clf.score(xv_b,y_v))
print("Depth DT: ", clf.get_depth())
print("Recall: ", recall_score(y_v,y_pred))
print("Precision: ", precision_score(y_v,y_pred))
print("confussion: ", confusion_matrix(y_v,y_pred))

#Decision tree plot
graph = Source(tree.export_graphviz(clf, \
    out_file=None, feature_names=x.loc[:,\
        selector.get_support()].columns))
graph.format = 'pdf'
graph.render('plots\\dt')

#############
#pruned
############
#parameter grid
lf=np.arange(2,30,2)

#Setting up DT and Gridsearch
cflp = tree.DecisionTreeClassifier(criterion = "entropy", random_state=21)
clfp = GridSearchCV(estimator=cflp, param_grid=dict(min_samples_leaf=lf), \
        n_jobs=-1, cv=k_fold)

#Fitting and printing CV accuracy
clfp.fit(x_b,y) #fitting