def draw_tree(clf,sub_columns):
    print "绘制图"
    import pydot,StringIO
    dot_data = StringIO.StringIO()
    tree.export_graphviz(clf, out_file=dot_data,feature_names=sub_columns,max_depth=5)
    dot_data.getvalue()
    pydot.graph_from_dot_data(dot_data.getvalue())
    graph = pydot.graph_from_dot_data(dot_data.getvalue())
    graph.write_png('titanic.png')
Exemple #2
0
    def graph(self):
        if self.code is '':
            return None
        
        stdin = SpooledTemporaryFile()
        stdout = SpooledTemporaryFile()

        stdin.write('@startuml\n')
        stdin.write(self.code)
        stdin.write('@enduml\n')
        
        stdin.seek(0)

        args = [
           self.java,
           '-jar',
           self.jar,
           '-p',
           '-tdot',
        ]

        p = Popen(args, stdin=stdin, stdout=stdout)

        if p.wait() != 0:
            return None

        stdout.seek(0)
        graph = stdout.read()
        return graph_from_dot_data(graph)
def make_tree_test():
    from sklearn import tree
    
    import StringIO
    
    import pydot
    
    from IPython.display import display, Image     
    
    x,y,dates,movies = load_data()
    
    #x =  add_missed_value_indicator(x)   
                
    test_x, train_x, test_y, train_y = create_test_train_set(x, y)          
    
    clf = tree.DecisionTreeClassifier(min_samples_split=3000)
    
    fit = clf.fit(train_x,train_y)
    
    dot_data = StringIO.StringIO()
    
    tree.export_graphviz(fit, 
       feature_names=train_x.columns,
       class_names=["1","2","3","4","5"],
       out_file=dot_data)
    
    graph = pydot.graph_from_dot_data(dot_data.getvalue())   
    
    graph[0].write_png("tree_toy.png")
    
    img = Image(graph[0].create_png()) 
    
    display(img)

    return fit
    def plot_pdf(self, path):
        self.build_polt()

        c = open(".tmp.dot", 'w')
        c.write(self.dot_data.getvalue())
        graph = pydot.graph_from_dot_data(self.dot_data.getvalue())
        graph.write_pdf(path)
Exemple #5
0
    def draw(self, name='goto'):
        g = nx.MultiDiGraph()
        lr_content = {}
        for k, v in self.progress_present.iteritems():
            label = 'I %d\n' % k

            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)

        g_str = to_pydot(g).to_string()
        g = pydot.graph_from_dot_data(g_str)
        g[0].write_jpg('graph/%s.jpg' % name)
        print '%s图生成成功!' % name
def main():
	if (len(sys.argv) < 2):
		print("One Argument Required; Training Set")
		return
	X_train, Y_train = ParseTraining(sys.argv[1])
    #X_test, Y_test = ParseTraining(sys.argv[2])
    #X_train, X_test, Y_train, Y_test = cross_validation.train_test_split(X, Y, test_size=0.2, random_state=99)
    #X_train, X_test, Y_train, Y_test = X, X, Y, Y
    #clf = tree.DecisionTreeClassifier()
	clf = tree.DecisionTreeClassifier(max_depth=6)
    #clf = OneVsRestClassifier(SVC(kernel="linear", C=0.025))
    #clf = RandomForestClassifier(max_depth=6, n_estimators=10, max_features=1)
    #clf = SVC(kernel="linear", C=0.025)
    #clf = AdaBoostClassifier()
    #clf = SVC(gamma=2, C=1)
	clf = clf.fit(X_train, Y_train)


    #feature_names = ["partAvg", "recavg", "latency", "ReadRate"]
	feature_names = ["partConf", "recAvg", "latency", "ReadRate", "homeconf"]
    #feature_names = ["partAvg", "recAvg", "recVar", "ReadRate"]
    #feature_names = ["partAvg", "recAvg", "recVar"]
    #feature_names = ["recAvg", "recVar", "Read"]
    #feature_names = ["partAvg", "recVar"]
    ##class_names = ["Partition", "OCC", "2PL"]
    #class_names = ["OCC", "2PL"]
	class_names = ["Partition", "No Partition"]
	dot_data = StringIO()
	tree.export_graphviz(clf, out_file=dot_data,
						feature_names=feature_names,
						class_names=class_names,
						filled=True, rounded=True,
						special_characters=True)
	graph = pydot.graph_from_dot_data(dot_data.getvalue())
	graph.write_png("partition.png")
Exemple #7
0
    def test_graph_with_shapefiles(self):
        shapefile_dir = os.path.join(test_dir, 'from-past-to-future')
        # image files are omitted from sdist
        if not os.path.isdir(shapefile_dir):
            warnings.warn('Skipping tests that involve images, '
                          'they can be found in the `git` repository.')
            return
        dot_file = os.path.join(shapefile_dir, 'from-past-to-future.dot')


        pngs = [
            os.path.join(shapefile_dir, fname) for
            fname in os.listdir(shapefile_dir)
            if fname.endswith('.png')]

        f = open(dot_file, 'rt')
        graph_data = f.read()
        f.close()

        #g = dot_parser.parse_dot_data(graph_data)
        graphs = pydot.graph_from_dot_data(graph_data)
        (g,) = graphs
        g.set_shape_files( pngs )

        jpe_data = g.create( format='jpe' )

        hexdigest = sha256(jpe_data).hexdigest()
        hexdigest_original = self._render_with_graphviz(
            dot_file, encoding='ascii')
        self.assertEqual( hexdigest, hexdigest_original )
def draw_ff(chromosome):
    ''' Draws a feedforward neural network '''

    output = 'digraph G {\n  node [shape=circle, fontsize=9, height=0.2, width=0.2]'

    # subgraph for inputs and outputs
    output += '\n  subgraph cluster_inputs { \n  node [style=filled, shape=box] \n    color=white'
    for ng in chromosome.node_genes:
        if ng.type== 'INPUT':
            output += '\n    '+str(ng.id)
    output += '\n  }'

    output += '\n  subgraph cluster_outputs { \n    node [style=filled, color=lightblue] \n    color=white'
    for ng in chromosome.node_genes:
        if ng.type== 'OUTPUT':
            output += '\n    '+str(ng.id)
    output += '\n  }'
    # topology
    for cg in chromosome.conn_genes:
        output += '\n  '+str(cg.innodeid)+' -> '+str(cg.outnodeid)
        if cg.enabled is False:
            output += ' [style=dotted, color=cornflowerblue]'

    output += '\n }'

    if has_pydot:
        g = pydot.graph_from_dot_data(output)
        g.write('feedforward.svg', prog='dot', format='svg')
    else:
        print('You do not have the PyDot package.')
Exemple #9
0
def positionGraph():
  """Uses graphviz to position nodes of the graph.
  """
  try:
    import pydot
  except ImportError:
    return jsonify({})

  graph = pydot.Dot()

  for node_id, node in request.json['nodes'].iteritems():
    graph.add_node(pydot.Node(node_id))
  for edge in request.json['edges'].itervalues():
    graph.add_edge(pydot.Edge(edge[0], edge[1]))

  new_graph = pydot.graph_from_dot_data(graph.create_dot())

  # calulate the ratio from the size of the bounding box
  ratio = new_graph.get_bb()
  origin_left, origin_top, max_left, max_top = [float(p) for p in
    new_graph.get_bb().strip('"').split(',')]
  ratio_top = max_top - origin_top
  ratio_left = max_left - origin_left

  preference_dict = dict()
  for node in new_graph.get_nodes():
    # skip technical nodes
    if node.get_name() in ('graph', 'node', 'edge'):
      continue
    left, top = [float(p) for p in node.get_pos()[1:-1].split(",")]
    preference_dict[node.get_name().strip('"')] = dict(
      top=1-(top/ratio_top),
      left=1-(left/ratio_left),)

  return jsonify(preference_dict)
Exemple #10
0
def method2format( output, _format="png", mx = None, raw = False ) :
    """
        Export method to a specific file format

        @param output : output filename
        @param _format : format type (png, jpg ...) (default : png)
        @param mx : specify the MethodAnalysis object
        @param raw : use directly a dot raw buffer
    """
    try :
        import pydot
    except ImportError :
        error("module pydot not found")

    buff = "digraph code {\n"
    buff += "graph [bgcolor=white];\n"
    buff += "node [color=lightgray, style=filled shape=box fontname=\"Courier\" fontsize=\"8\"];\n"

    if raw == False :
        buff += method2dot( mx )
    else :
        buff += raw

    buff += "}"

    d = pydot.graph_from_dot_data( buff )
    if d :
        getattr(d, "write_" + _format)( output )
Exemple #11
0
def drawDecisionTree(dt, filename, featureNames, classNames):
    dot_data = StringIO()
    print featureNames
    print classNames
    tree.export_graphviz(dt, out_file=dot_data, feature_names=featureNames, class_names=classNames, rounded=True, special_characters=True, filled=True)
    graph = pydot.graph_from_dot_data(dot_data.getvalue())
    graph.write_png(filename) 
Exemple #12
0
    def test_graph_with_shapefiles(self):

        shapefile_dir = os.path.join(TEST_DIR, 'from-past-to-future')
        dot_file = os.path.join(shapefile_dir, 'from-past-to-future.dot')

        pngs = [
            os.path.join(shapefile_dir, fname)
            for fname in os.listdir(shapefile_dir)
            if fname.endswith('.png')
            ]

        f = open(dot_file, 'rt')
        graph_data = f.read()
        f.close()

        g = pydot.graph_from_dot_data(graph_data)

        g.set_shape_files(pngs)

        jpe_data = g.create(format='jpe')

        hexdigest = sha256(jpe_data).hexdigest()

        hexdigest_original = self._render_with_graphviz(dot_file)

        self.assertEqual(hexdigest, hexdigest_original)
Exemple #13
0
    def test_attribute_with_implicit_value(self):

        d = 'digraph {\na -> b[label="hi", decorate];\n}'
        g = pydot.graph_from_dot_data(d)
        attrs = g.get_edges()[0].get_attributes()

        self.assertEqual('decorate' in attrs, True)
Exemple #14
0
def method2format(output, _format="png", mx=None, raw=None):
    """
        Export method to a specific file format

        @param output : output filename
        @param _format : format type (png, jpg ...) (default : png)
        @param mx : specify the MethodAnalysis object
        @param raw : use directly a dot raw buffer if None
    """
    try:
        import pydot
    except ImportError:
        error("module pydot not found")

    buff = "digraph {\n"
    buff += "graph [rankdir=TB]\n"
    buff += "node [shape=plaintext]\n"

    if raw:
        data = raw
    else:
        data = method2dot(mx)

    # subgraphs cluster
    buff += "subgraph cluster_" + hashlib.md5(output).hexdigest() + " {\nlabel=\"%s\"\n" % data['name']
    buff += data['nodes']
    buff += "}\n"

    # subgraphs edges
    buff += data['edges']
    buff += "}\n"

    d = pydot.graph_from_dot_data(buff)
    if d:
        getattr(d, "write_" + _format.lower())(output)
Exemple #15
0
def layout_graph(graph, fname):
    print('writing un-layouted graph to "%s.dot"' % fname)

    graph.set_overlap("scale")
    dot = graph.create_dot()
    f = open("%s.dot" % fname, "w")
    f.write(dot)
    f.close()

    neato_dot = subprocess.check_output(["/usr/bin/neato", "-Tdot", "%s.dot" % fname])
    neato_png = subprocess.check_output(["/usr/bin/neato", "-Tpng", "%s.dot" % fname])
    neato_pdf = subprocess.check_output(["/usr/bin/neato", "-Tpdf", "%s.dot" % fname])
    print('writing layouted graph to "%s.dot.layouted.dot"' % fname)
    f = open("%s.dot.layouted.dot" % fname, "w")
    f.write(neato_dot)
    f.close()
    print('writing layouted graph to "%s.dot.png"' % fname)
    f = open("%s.dot.png" % fname, "w")
    f.write(neato_png)
    f.close()
    print('writing layouted graph to "%s.dot.pdf"' % fname)
    f = open("%s.dot.pdf" % fname, "w")
    f.write(neato_pdf)
    f.close()

    print("re-reading layouted dot from internal variable")
    graph = pydot.graph_from_dot_data(neato_dot)
    return graph
Exemple #16
0
def positionGraph(g):
  import pydot
  graph = pydot.Dot()
  for node in g['nodes']:
    graph.add_node(pydot.Node(node))
  for edge, (source, destination, data) in g['edges'].items():
    graph.add_edge(pydot.Edge(source, destination))

  new_graph = pydot.graph_from_dot_data(graph.create_dot())

  # calulate the ratio from the size of the bounding box
  ratio = new_graph.get_bb()
  origin_left, origin_top, max_left, max_top = [float(p) for p in
    new_graph.get_bb()[1:-1].split(',')]
  ratio_top = max_top - origin_top
  ratio_left = max_left - origin_left

  preference_dict = dict()
  for node in new_graph.get_nodes():
    # skip technical nodes
    if node.get_name() in ('graph', 'node', 'edge'):
      continue
    left, top = [float(p) for p in node.get_pos()[1:-1].split(",")]
    preference_dict[get_name(node)] = dict(
      top=1-(top/ratio_top),
      left=1-(left/ratio_left),)
  return preference_dict
Exemple #17
0
def classfyWithScipy(dataSet,labels,dataToClassfy):
    clf = tree.DecisionTreeClassifier(criterion="entropy").fit(dataSet,labels)
    dot_data = StringIO.StringIO() 
    tree.export_graphviz(clf, out_file=dot_data) 
    graph = pydot.graph_from_dot_data(dot_data.getvalue()) 
    graph.write_pdf("entropy.pdf") 
    return clf.predict(dataToClassfy)
Exemple #18
0
def mainTree():
	header=re.sub(' |\t','','id|gender|age|height|edu|salary|nation|car|house|body|face|hair|\
	smoke|drink|child|parent|bmi|where0|where1|\
	marriage0|marriage1|look0|look1|where2').split('|')
	MaleData=pd.read_csv('/home/idanan/jiayuan/code/resources/transed_M.txt',names=header,sep='|')
	FemaleData=pd.read_csv('/home/idanan/jiayuan/code/resources/cluster_female.txt',names=header+['class'],sep='|')
	matches=matchDict('/home/idanan/jiayuan/code/resources/lovers_ids.txt')
	FemaleData['id']=FemaleData['id'].map(partial(match,matches=matches))
	FemaleClass=FemaleData[['id','class']]
	newMaleData=concatData(MaleData,FemaleClass)
	MaleArrays=scaleData(newMaleData,['id','gender'])
	pca=factors(MaleArrays[:,:-1],17)
	print 'PCA explained variance:', sum(pca.explained_variance_ratio_)
	pcaMaleArray=pca.transform(MaleArrays[:,:-1])
	MaleArrays=np.c_[pcaMaleArray,MaleArrays]


	trainData,testData=departData(MaleArrays,0.9)
	trainModel=decisionModel(trainData)

	dot_data = StringIO()
	tree.export_graphviz(trainModel, out_file=dot_data)
	graph = pydot.graph_from_dot_data(dot_data.getvalue())
	graph.write_pdf("/home/idanan/jiayuan/code/resources/marriage.pdf") 
	

	rate=test(trainModel,testData)
	print 'Decision Model true rate',rate
Exemple #19
0
def read_hypergraph(string):
    """
    Read a hypergraph from a string in dot format. Nodes and edges specified in the input will be
    added to the current hypergraph.
    
    @type  string: string
    @param string: Input string in dot format specifying a graph.
    
    @rtype:  hypergraph
    @return: Hypergraph
    """
    hgr = hypergraph()
    dotG = pydot.graph_from_dot_data(string)
    
    # Read the hypernode nodes...
    # Note 1: We need to assume that all of the nodes are listed since we need to know if they
    #           are a hyperedge or a normal node
    # Note 2: We should read in all of the nodes before putting in the links
    for each_node in dotG.get_nodes():
        if 'hypernode' == each_node.get('hyper_node_type'):
            hgr.add_node(each_node.get_name())
        elif 'hyperedge' == each_node.get('hyper_node_type'):
            hgr.add_hyperedge(each_node.get_name())
    
    # Now read in the links to connect the hyperedges
    for each_link in dotG.get_edges():
        if hgr.has_node(each_link.get_source()):
            link_hypernode = each_link.get_source()
            link_hyperedge = each_link.get_destination()
        elif hgr.has_node(each_link.get_destination()):
            link_hypernode = each_link.get_destination()
            link_hyperedge = each_link.get_source()
        hgr.link(link_hypernode, link_hyperedge)
    
    return hgr
Exemple #20
0
def read_dot(path):
    """Return a NetworkX MultiGraph or MultiDiGraph from a dot file on path.


    Parameters
    ----------
    path : filename or file handle

    Returns
    -------
    G : NetworkX multigraph
        A MultiGraph or MultiDiGraph.  
    
    Notes
    -----
    Use G=nx.Graph(nx.read_dot(path)) to return a Graph instead of a MultiGraph.
    """
    try:
        import pydot
    except ImportError:
        raise ImportError("read_dot() requires pydot",
                          "http://dkbza.org/pydot.html/")

    data=path.read()        
    P=pydot.graph_from_dot_data(data)
    return from_pydot(P)
Exemple #21
0
def dumpcircuit(ind, printfun):
        circuit = ind.phenotype
        print circuit.getcircuit()
        arnet = ind.genotype
        print arnet.code.bin
        g = graph_from_dot_data(printfun(circuit,arnet = arnet))
        g.write_png('temp.png')
Exemple #22
0
def draw_tree(clf):
    import pydot
    import StringIO
    output = StringIO.StringIO()
    tree.export_graphviz(clf, out_file=output)
    graph = pydot.graph_from_dot_data(output.getvalue())
    graph.write_pdf('tree.pdf')
Exemple #23
0
def loadtasksdot(fp):
    import pydot
    graphs = pydot.graph_from_dot_data(fp.read())
    (g2,) = graphs
    tasks = []
    tasksd = {}
    # if present use the attribute cost
    for n in g2.get_nodes():   
        ad =      n.get_attributes()
        #print n.get_name(),[a for a in ad]
        t = MTask(n.get_name(),float(ad.get("cost",1)),int(ad.get("items",1)),int(ad.get("maxnp",defaultcore)),int(ad.get("deadline",MTask.deadlinemaxtime)),float(ad.get("reductioncost",0)),int(ad.get("evennp",0)) != 0)
        tasks.append(t)
        tasksd[t.id] = t
    # if present use the attribute cost
    for e in g2.get_edges():
        #get_source
        #get_destination
        #get_attributes
        st = tasksd.get(e.get_source(),None)
        if st is None:
            st = MTask(e.get_source(),1,1,defaultcore)
            tasks.append(st)
            tasksd[st.id] = st
        dt = tasksd.get(e.get_destination(),None)
        if dt is None:
            dt = MTask(e.get_destination(),1,1,defaultcore)
            tasks.append(dt)
            tasksd[dt.id] = dt
        dt.parents.append(MTaskEdge(st,dt,float(e.get_attributes().get("delay",0)),float(e.get_attributes().get("reduction",0))))

        #print e.get_source(),e.get_destination(),[a for a in e.get_attributes().iteritems()]
    return tasks
Exemple #24
0
 def test_multiple_graphs(self):
     graph_data = 'graph A { a->b };\ngraph B {c->d}'
     graphs = pydot.graph_from_dot_data(graph_data)
     n = len(graphs)
     assert n == 2, n
     names = [g.get_name() for g in graphs]
     assert names == ['A', 'B'], names
Exemple #25
0
def classifyTree(Xtr, ytr, Xte, yte, splitCriterion="gini", maxDepth=0, visualizeTree=False):
    """ Classifies data using CART """
    try:
        accuracyRate, probabilities, timing = 0.0, [], 0.0
        # Perform classification
        cartClassifier = tree.DecisionTreeClassifier(criterion=splitCriterion, max_depth=maxDepth)
        startTime = time.time()
        prettyPrint("Training a CART tree for classification using \"%s\" and maximum depth of %s" % (splitCriterion, maxDepth), "debug")
        cartClassifier.fit(numpy.array(Xtr), numpy.array(ytr))
        prettyPrint("Submitting the test samples", "debug")
        predicted = cartClassifier.predict(Xte)
        endTime = time.time()
        # Compare the predicted and ground truth and append result to list
        accuracyRate = round(metrics.accuracy_score(predicted, yte), 2)
        # Also append the probability estimates
        probs = cartClassifier.predict_proba(Xte)
        probabilities.append(probs)
        timing = endTime-startTime # Keep track of performance
        if visualizeTree:
            # Visualize the tree
            dot_data = StringIO()
            tree.export_graphviz(cartClassifier, out_file=dot_data)
            graph = pydot.graph_from_dot_data(dot_data.getvalue())
            prettyPrint("Saving learned CART to \"tritonTree_%s.pdf\"" % getTimestamp(), "debug")
            graph.write_pdf("tree_%s.pdf" % getTimestamp())
  
    except Exception as e:
        prettyPrint("Error encountered in \"classifyTree\": %s" % e, "error")

    return accuracyRate, timing, probabilities, predicted
def plot_decision_tree(clf):
    import StringIO, pydot 
    dot_data = StringIO.StringIO() 
    sklearn.tree.export_graphviz(clf, out_file=dot_data) 
    graph = pydot.graph_from_dot_data(dot_data.getvalue()) 
    graph.write_png('tmp.png') 
    os.system('sxiv tmp.png')
def visualize_tree(dtree):
    dot_data = StringIO()
    tree.export_graphviz(dtree, out_file=dot_data,
                         filled=True, rounded=True,
                         special_characters=True)
    graph = pydot.graph_from_dot_data(dot_data.getvalue())
    display(Image(graph.create_png()))
Exemple #28
0
    def displayGraph(self):
        dot = self.graph.write(fmt="dot")

        import pydot

        dotgraph = pydot.graph_from_dot_data(dot)

        # Tmpfile
        import tempfile

        fh = tempfile.NamedTemporaryFile(suffix=".png")

        # dotgraph.write('graph.dot')
        dotgraph.write_png(fh.name)

        image = wx.Image(fh.name, wx.BITMAP_TYPE_ANY)

        fh.close()

        # Init the panel
        panel = GraphPanel(self.parent, image)
        panel.identifierTag = "Huffmann Tree"

        # Show the panel
        self.parent.AddPage(panel, "Huffmann Tree")
def generate_plot(clf):
    print "\nGenerating plot..."
    dot_data = StringIO()
    tree.export_graphviz(clf, out_file=dot_data)
    graph = pydot.graph_from_dot_data(dot_data.getvalue())
    graph.write_pdf("weather_forecast.pdf")
    print "Plot generated!"
Exemple #30
0
def graph_to_page():
    file_vals = next(request.files.values())
    file_contents = file_vals.stream.read().decode('utf-8')
    file_contents = pydot.graph_from_dot_data(file_contents).to_string()
    compressed = zlib.compress(file_contents.encode('utf-8'), 9)
    encoded = urlsafe_b64encode(compressed)
    return request.host_url + "view?s=" + encoded.decode('utf-8')
Exemple #31
0
                       inplace=True)

X_train = restaurant_train1
y_train = restaurant_train['revenue']

rf_estimator = ensemble.RandomForestRegressor(random_state=10)
rf_grid = {'n_estimators': [3, 4, 5], 'max_features': [7, 8]}


def rmse(y_true, y_pred):
    return math.sqrt(metrics.mean_squared_error(y_true, y_pred))


rf_grid_estimator = model_selection.GridSearchCV(rf_estimator,
                                                 rf_grid,
                                                 scoring='mean_squared_error',
                                                 cv=10,
                                                 n_jobs=5)
rf_grid_estimator.fit(X_train, y_train)
rf_grid_estimator.grid_scores_
rf_grid_estimator.best_estimator_

n_tree = 0
for est in rf_grid_estimator.best_estimator_.estimators_:
    dot_data = io.StringIO()
    tmp = est.tree_
    tree.export_graphviz(tmp, out_file=dot_data, feature_names=X_train.columns)
    graph = pydot.graph_from_dot_data(dot_data.getvalue())[0]
    graph.write_pdf("regtree" + str(n_tree) + ".pdf")
    n_tree = n_tree + 1
Exemple #32
0
def assert_valid_graph(dot: str):
    return pydot.graph_from_dot_data(dot)
Exemple #33
0
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
from IPython.display import Image
from pydot import graph_from_dot_data
import pandas as pd
import numpy as np

iris = load_iris()
X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = pd.Categorical.from_codes(iris.target, iris.target_names)

X.head()
y = pd.get_dummies(y)

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
dt = DecisionTreeClassifier()
dt.fit(X_train, y_train)
dot_data = StringIO()
export_graphviz(dt, out_file=dot_data, feature_names=iris.feature_names)
(graph, ) = graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())
y_pred = dt.predict(X_test)

species = np.array(y_test).argmax(axis=1)
predictions = np.array(y_pred).argmax(axis=1)
confusion_matrix(species, predictions)
Exemple #34
0
 def to_pdf(self, filename, dic_var=None):
     pydot.graph_from_dot_data(self.to_dot(dic_var))[0].write_pdf(filename)
	    a += 'h{i} -> o[penwidth={weight:.2f},weight="{weight:.2f}"];\n'.format(i=i, weight=np.sqrt(abs(weight)))
	return a + '\n}'


#import sklearn.neural_network
#mlp_classifier = sklearn.neural_network.MLPClassifier(algorithm='l-bfgs', alpha=1e-5, hidden_layer_sizes=(4,), random_state=1)

mlp_classifier = NeuralNetwork(3, 3)
mlp_classifier.fit(X_train, y_train)
mlp_prediction = mlp_classifier.predict_proba(X_test)[:, 1]
plotData(X_train, y_train)
plotContour(mlp_classifier.predict_proba)
savePlot('mlp_classifier.png')
plotTestStatistic(y_test, mlp_prediction)
savePlot('mlp_statistic.png')

plotROC(y_test, ('Neyman Pearson Lemma', np_prediction), ('Multi Layer Perceptron', mlp_prediction))
savePlot('mlp_roc.png')

dot_data = NeuralNetwork.export_graphviz(mlp_classifier)
print dot_data
graph = pydot.graph_from_dot_data(dot_data)
graph.write_png('mlp_visualization.png')
subprocess.call("convert -resize 2000x1200 mlp_visualization.png mlp_visualization.png", shell=True)

from scipy.special import expit
sns.set(font_scale=8.0)
X = np.linspace(-6, 6, 100)
plt.plot(X, expit(X), label=None, lw=16)
savePlot('sigmoid.png')
Exemple #36
0
def from_dot_string(string: str) -> str:
    # Assuming there's only one graph/digraph in the string
    return from_dot_graph(graph_from_dot_data(string)[0])
Exemple #37
0
    def __init__(self,
                 treatment_name,
                 outcome_name,
                 graph=None,
                 common_cause_names=None,
                 instrument_names=None,
                 observed_node_names=None):
        self.treatment_name = parse_state(treatment_name)
        self.outcome_name = parse_state(outcome_name)
        instrument_names = parse_state(instrument_names)
        common_cause_names = parse_state(common_cause_names)
        self.fullname = "_".join(self.treatment_name + self.outcome_name +
                                 common_cause_names + instrument_names)

        self.logger = logging.getLogger(__name__)

        if graph is None:
            self._graph = nx.DiGraph()
            self._graph = self.build_graph(common_cause_names,
                                           instrument_names)
        elif re.match(r".*\.dot", graph):
            # load dot file
            try:
                import pygraphviz as pgv
                self._graph = nx.DiGraph(nx.drawing.nx_agraph.read_dot(graph))
            except Exception as e:
                self.logger.error("Pygraphviz cannot be loaded. " + str(e) +
                                  "\nTrying pydot...")
                try:
                    import pydot
                    self._graph = nx.DiGraph(
                        nx.drawing.nx_pydot.read_dot(graph))
                except Exception as e:
                    self.logger.error("Error: Pydot cannot be loaded. " +
                                      str(e))
                    raise e
        elif re.match(r".*\.gml", graph):
            self._graph = nx.DiGraph(nx.read_gml(graph))
        elif re.match(r".*graph\s*\{.*\}\s*", graph):
            try:
                import pygraphviz as pgv
                self._graph = pgv.AGraph(graph, strict=True, directed=True)
                self._graph = nx.drawing.nx_agraph.from_agraph(self._graph)
            except Exception as e:
                self.logger.error("Error: Pygraphviz cannot be loaded. " +
                                  str(e) + "\nTrying pydot ...")
                try:
                    import pydot
                    P_list = pydot.graph_from_dot_data(graph)
                    self._graph = nx.drawing.nx_pydot.from_pydot(P_list[0])
                except Exception as e:
                    self.logger.error("Error: Pydot cannot be loaded. " +
                                      str(e))
                    raise e
        elif re.match(".*graph\s*\[.*\]\s*", graph):
            self._graph = nx.DiGraph(nx.parse_gml(graph))
        else:
            self.logger.error(
                "Error: Please provide graph (as string or text file) in dot or gml format."
            )
            self.logger.error("Error: Incorrect graph format")
            raise ValueError

        self._graph = self.add_node_attributes(observed_node_names)
        self._graph = self.add_unobserved_common_cause(observed_node_names)
Exemple #38
0
 def sdd_to_dot(self):
     s = sdd_to_dot(self._results['sdd'],
                    self._results['literals'],
                    node_annotations=None,
                    edge_annotations=None)
     return pydot.graph_from_dot_data(s)[0]
Exemple #39
0
def plot_graph(dot_graph, file):
    graphs = pydot.graph_from_dot_data(dot_graph)[0]
    if file[-4:].lower() == '.png':
        graphs.write_png(file)
    else:
        graphs.write_pdf(file)
 def draw_graph(self, classifier, filename="out.svg"):
     dot_data = StringIO()    
     tree.export_graphviz(classifier, out_file=dot_data)
     graph = pydot.graph_from_dot_data(dot_data.getvalue())
     graph.write_svg(filename+".svg")
Exemple #41
0
def main():
    with sys.stdin as dot:
        g = pydot.graph_from_dot_data(dot.read())
    print(tikzFromPos(g[0]).compile())
Exemple #42
0
    def __init__(self,
                 treatment_name,
                 outcome_name,
                 graph=None,
                 common_cause_names=None,
                 instrument_names=None,
                 effect_modifier_names=None,
                 observed_node_names=None,
                 missing_nodes_as_confounders=False):
        self.treatment_name = parse_state(treatment_name)
        self.outcome_name = parse_state(outcome_name)
        instrument_names = parse_state(instrument_names)
        common_cause_names = parse_state(common_cause_names)
        self.logger = logging.getLogger(__name__)

        if graph is None:
            self._graph = nx.DiGraph()
            self._graph = self.build_graph(common_cause_names,
                                           instrument_names,
                                           effect_modifier_names)
        elif re.match(r".*\.dot", graph):
            # load dot file
            try:
                import pygraphviz as pgv
                self._graph = nx.DiGraph(nx.drawing.nx_agraph.read_dot(graph))
            except Exception as e:
                self.logger.error("Pygraphviz cannot be loaded. " + str(e) +
                                  "\nTrying pydot...")
                try:
                    import pydot
                    self._graph = nx.DiGraph(
                        nx.drawing.nx_pydot.read_dot(graph))
                except Exception as e:
                    self.logger.error("Error: Pydot cannot be loaded. " +
                                      str(e))
                    raise e
        elif re.match(r".*\.gml", graph):
            self._graph = nx.DiGraph(nx.read_gml(graph))
        elif re.match(r".*graph\s*\{.*\}\s*", graph):
            try:
                import pygraphviz as pgv
                self._graph = pgv.AGraph(graph, strict=True, directed=True)
                self._graph = nx.drawing.nx_agraph.from_agraph(self._graph)
            except Exception as e:
                self.logger.error("Error: Pygraphviz cannot be loaded. " +
                                  str(e) + "\nTrying pydot ...")
                try:
                    import pydot
                    P_list = pydot.graph_from_dot_data(graph)
                    self._graph = nx.drawing.nx_pydot.from_pydot(P_list[0])
                except Exception as e:
                    self.logger.error("Error: Pydot cannot be loaded. " +
                                      str(e))
                    raise e
        elif re.match(".*graph\s*\[.*\]\s*", graph):
            self._graph = nx.DiGraph(nx.parse_gml(graph))
        else:
            self.logger.error(
                "Error: Please provide graph (as string or text file) in dot or gml format."
            )
            self.logger.error("Error: Incorrect graph format")
            raise ValueError
        if missing_nodes_as_confounders:
            self._graph = self.add_missing_nodes_as_common_causes(
                observed_node_names)
        # Adding node attributes
        self._graph = self.add_node_attributes(observed_node_names)
        #TODO do not add it here. CausalIdentifier should call causal_graph to add an unobserved common cause if needed. This also ensures that we do not need get_common_causes in this class.
        self._graph = self.add_unobserved_common_cause(observed_node_names)
    def __init__(self, game, agent, Config, numSearch):
        """Initialize the pygame"""
        pygame.init()
        pygame.font.init()
        self.default_font = pygame.font.get_default_font()
        self.text_size = 10
        self.font_color = (40, 40, 40)
        self.font_renderer = pygame.font.Font(self.default_font,
                                              self.text_size)
        """Variables"""
        self.game = copy.deepcopy(game)

        self.Config = Config

        self.start_pos = np.copy(game.board)
        self.agent = agent
        self.side_length = 100
        self.line_th = 5
        self.height = self.game.board_dims[1]
        self.width = self.game.board_dims[2]
        self.image = pygame.image.load("nevraltnett.png")
        self.imagerect = (0, 0)
        self.black = (0, 0, 0)
        self.white = (255, 255, 255)
        self.piece_size = self.side_length // 3
        self.screen = pygame.display.set_mode([
            self.side_length * self.width + self.line_th + self.imagerect[0],
            max(self.side_length * (self.height + 1) + self.line_th,
                self.imagerect[1])
        ])  # + self.imagerect[0],self.imagerect[1]
        self.weights = [0] * len(self.game.get_moves(
        ))  # Set weight to empty array to represent all moves
        self.count = int(input())  # Switches sides of human and machine
        self.primary_line = []
        self.won = 0
        self.tied = 0
        self.lost = 0
        """find what game is given"""
        self.tictactoe = False
        self.fourinarow = False
        if self.game.name == "TicTacToe":
            self.tictactoe = True
            self.background_color = (0, 109, 50)
        elif self.game.name == "FourInARow":
            self.fourinarow = True
            self.background_color = (0, 100, 150)
        """check if graphviz is installed and in path"""
        try:
            self.test_graph = "digraph g{rankdir=LR;testing -> testing -> tested}"
            self.test_graph = pydot.graph_from_dot_data(self.test_graph)[0]
            self.test_graph.write_png('graph.png')
        except FileNotFoundError:
            print(
                "Error:Graphviz is not installed or not on path, skipping visualization"
            )
            self.draw_graph = False
        except:
            print("Error: Unknown error with graph visualization, skipping")
            self.draw_graph = False
        else:
            self.draw_graph = True

        self.update_screen()
        """continuosly check for updates"""
        while True:
            self.mouse_pos = pygame.mouse.get_pos()
            if self.game.is_final():
                sleep(4)
                """Show death screen"""
                self.screen.fill(self.black)
                font_size = max(
                    (self.side_length * self.width + self.line_th) // 35, 20)
                myfont = pygame.font.SysFont(self.default_font, 2 * font_size)
                """Who won"""
                if self.game.get_outcome()[0] == 0:
                    winner = myfont.render('Tied', False, (255, 255, 255))
                    self.tied += 1
                elif (self.game.get_outcome()[0] == 1 and self.count % 2
                      == 0) or (self.game.get_outcome()[1] == 1
                                and self.count % 2 == 1):
                    winner = myfont.render('AI won!', False, (255, 255, 255))
                    self.lost += 1
                else:
                    winner = myfont.render('Human won', False, (255, 255, 255))
                    self.won += 1
                self.screen.blit(
                    winner, ((self.side_length * self.width + self.line_th +
                              self.imagerect[0]) // 2 - winner.get_width() / 2,
                             self.side_length // 3 - winner.get_height() // 2))

                myfont = pygame.font.SysFont('Comic Sans MS', font_size)
                switch_side = myfont.render('(Switching sides)', False,
                                            (0, 255, 0))
                self.screen.blit(
                    switch_side,
                    ((self.side_length * self.width + self.line_th +
                      self.imagerect[0]) // 2 - switch_side.get_width() / 2,
                     self.side_length // 3 - switch_side.get_height() // 2 +
                     winner.get_height()))
                """Shows the score"""
                myfont = pygame.font.SysFont(self.default_font, 2 * font_size)
                wtl = myfont.render('Win/Tie/Loss', False, self.white)
                self.screen.blit(
                    wtl, ((self.side_length * self.width + self.line_th +
                           self.imagerect[0]) // 2 - wtl.get_width() / 2,
                          (self.side_length * self.height) // 2 -
                          wtl.get_height() // 2))
                score = myfont.render(
                    str(self.won) + "-" + str(self.tied) + "-" +
                    str(self.lost), False, self.white)
                self.screen.blit(
                    score,
                    ((self.side_length * self.width + self.line_th +
                      self.imagerect[0]) // 2 - score.get_width() / 2,
                     (self.side_length * self.height) // 2 + wtl.get_height()))

                pygame.display.flip()
                print("GAME IS OVER")
                self.count += 1  # Switches sides

                sleep(1)  # Catch glitchy graohics
                # sleep(4)    #Hold the death screen open
                """clean the board and graphics"""
                self.weights = [0] * len(self.weights)
                self.game.board = np.copy(
                    self.start_pos)  # don't want to change it
                self.game.history = []
                self.imagerect = (0, 0)
                self.screen = pygame.display.set_mode([
                    self.side_length * self.width + self.line_th +
                    self.imagerect[0],
                    max(self.side_length * (self.height + 1) + self.line_th,
                        self.imagerect[1])
                ])
                self.update_screen()

            elif (self.game.get_turn() +
                  self.count) % 2 and not self.game.is_final():
                """look for human input"""
                for event in pygame.event.get():
                    pressed = pygame.key.get_pressed()
                    numbers = [
                        pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4,
                        pygame.K_5, pygame.K_6, pygame.K_7
                    ]

                    if event.type == pygame.QUIT or pressed[
                            27]:  # Escape quits the game
                        sys.exit()
                    # elif pygame.key.get_pressed()[27]:

                    elif pygame.mouse.get_pressed()[0] and self.mouse_pos[
                            0] < self.side_length * self.width + self.line_th and self.mouse_pos[
                                1] < self.side_length * self.height + self.line_th:  # Check if mouse is pressed in board
                        self.execute_move()
                        self.update_screen()
                    elif pygame.key.get_pressed()[8]:  # Backspace to undo move
                        self.game.undo_move()
                        self.game.undo_move()
                        self.update_screen()

            elif not self.game.is_final():
                """If machines turn, machine do move"""
                # if self.game.get_turn()%2==0:
                # tree = MCTS.MCTS(self.game, self.game.board, self.agent, self.Config)
                tree = MCTSTraining.MCTS()
                tree.set_game(self.game)
                tree.set_evaluation(self.agent)
                tree.dirichlet_noise = False
                tree.NN_output_to_moves_func = Config.NN_output_to_moves
                tree.number_to_move_func = Config.number_to_move
                tree.move_to_number_func = Config.move_to_number
                tree.policy_output_dim = Config.policy_output_dim
                tree.reset_search()
                # else:
                #     tree = MCTS.MCTS(self.game, self.game.board, agent1, self.Config)
                # if len(self.game.get_moves()) > 1:  # Does not compute last possible move very deeply
                #     for searches in range(numSearch):
                #         if searches>750:
                # print()
                tree.search_series(numSearch)
                print(
                    tree.get_prior_probabilities(
                        self.game.get_state()).round(2))
                print(tree.get_posterior_probabilities(self.game.get_state()),
                      tree.get_value(self.game.get_state()))
                print(tree.tree_children)
                # if (searches % 100 == 0 and searches != 0):
                #     """update weight on screen every 200 search"""
                #     self.NNvisual(tree, num_nodes=20)

                # self.NNvisual(tree, num_nodes=20)
                # else:
                #     tree.search_series(numSearch)
                # predict = tree.get_most_searched_move(tree.root)
                #                print("Stillingen vurderes som: ",self.agent.predict(np.array([self.game.get_board()]))[1])
                # self.game.execute_move(predict)
                self.game.execute_move(
                    tree.get_most_searched_move(self.game.get_state()))
                self.update_screen()
                self.show_gamelines(self.primary_line)
                self.see_valuation()
Exemple #44
0
from sklearn import datasets
X, y = datasets.make_classification(1000, 20, n_informative=3)

from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier()
dt.fit(X, y)

#Ok, so now that we have a basic classifier fit, we can view it quite simply:

from StringIO import StringIO
from sklearn import tree
import pydot

str_buffer = StringIO()
tree.export_graphviz(dt, out_file=str_buffer)
graph = pydot.graph_from_dot_data(str_buffer.getvalue())
graph.write("myfile.jpg")

#This is a very complex tree. It will most likely overfit the data. First, 
#let's reduce the max depth value:
dt = DecisionTreeClassifier(max_depth=5)
dt.fit(X, y);

dt = DecisionTreeClassifier(max_depth=5).fit(X, y)

def plot_dt(model, filename):
    str_buffer = StringIO()
tree.export_graphviz(model, out_file=str_buffer)

graph = pydot.graph_from_dot_data(str_buffer.getvalue())
graph.write_jpg(filename)
Exemple #45
0
def synthesize(formula, env_vars=None, sys_vars=None):
    """Return Moore transducer if C{formula} is realizable.

    Variable C{dict}s have variable names as keys and their type as
    value. The types should be 'boolean'. These parameters are only
    used if formula is of type C{str}. Else, the variable dictionaries
    associated with the L{LTL} or L{GRSpec} object are used.

    @param formula: linear temporal logic formula
    @type formula: C{str}, L{LTL}, or L{GRSpec}

    @param env_vars: uncontrolled variables (inputs); used only if
        C{formula} is of type C{str}
    @type env_vars: C{dict} or None

    @param sys_vars: controlled variables (outputs); used only if
        C{formula} is of type C{str}
    @type sys_vars: C{dict} or None

    @return: symbolic Moore transducer
        (guards are conjunctions, not sets)
    @rtype: L{MooreMachine}
    """
    if isinstance(formula, GRSpec):
        env_vars = formula.env_vars
        sys_vars = formula.sys_vars
    elif isinstance(formula, LTL):
        env_vars = formula.input_variables
        sys_vars = formula.output_variables

    all_vars = dict(env_vars)
    all_vars.update(sys_vars)
    if not all(v == 'boolean' for v in all_vars.values()):
        raise TypeError(
            'all variables should be Boolean:\n{v}'.format(v=all_vars))

    if isinstance(formula, GRSpec):
        f = translate(formula, 'wring')
    else:
        T = parse(str(formula))
        f = translate_ast(T, 'wring').flatten(env_vars=env_vars,
                                              sys_vars=sys_vars)

    # dump partition file
    s = '.inputs {inp}\n.outputs {out}'.format(inp=' '.join(env_vars),
                                               out=' '.join(sys_vars))
    with open(IO_PARTITION_FILE, 'w') as fid:
        fid.write(s)

    # call lily
    try:
        p = subprocess.Popen([LILY, '-f', f, '-syn', IO_PARTITION_FILE],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             universal_newlines=True)
        out = p.stdout.read()
    except OSError as e:
        if e.errno == errno.ENOENT:
            raise Exception('lily.pl not found in path.\n'
                            'See the Lily docs for setting PERL5LIB and PATH.')
        else:
            raise

    dotf = open(DOTFILE, 'r')
    fail_msg = 'Formula is not realizable'
    if dotf.read(len(fail_msg)) == fail_msg:
        return None
    dotf.seek(0)
    data = dotf.read()
    (pd, ) = pydot.graph_from_dot_data(data)
    g = nx.drawing.nx_pydot.from_pydot(pd)
    dotf.close()
    moore = lily_strategy2moore(g, env_vars, sys_vars)
    return moore
Exemple #46
0
def write_tree_image(clf):
    for i in xrange(clf.n_estimators):
        dot_data = StringIO.StringIO()
        tree.export_graphviz(clf.estimators_[0], out_file=dot_data)
        graph = pydot.graph_from_dot_data(dot_data.getvalue())
        image = graph.write_png("tree" + str(i) + ".png")
Exemple #47
0
def pydot_layout(G, prog="neato", root=None):
    """Create node positions using :mod:`pydot` and Graphviz.

    Parameters
    ----------
    G : Graph
        NetworkX graph to be laid out.
    prog : string  (default: 'neato')
        Name of the GraphViz command to use for layout.
        Options depend on GraphViz version but may include:
        'dot', 'twopi', 'fdp', 'sfdp', 'circo'
    root : Node from G or None (default: None)
        The node of G from which to start some layout algorithms.

    Returns
    -------
    dict
        Dictionary of positions keyed by node.

    Examples
    --------
    >>> G = nx.complete_graph(4)
    >>> pos = nx.nx_pydot.pydot_layout(G)
    >>> pos = nx.nx_pydot.pydot_layout(G, prog="dot")

    Notes
    -----
    If you use complex node objects, they may have the same string
    representation and GraphViz could treat them as the same node.
    The layout may assign both nodes a single location. See Issue #1568
    If this occurs in your case, consider relabeling the nodes just
    for the layout computation using something similar to::

        H = nx.convert_node_labels_to_integers(G, label_attribute='node_label')
        H_layout = nx.nx_pydot.pydot_layout(G, prog='dot')
        G_layout = {H.nodes[n]['node_label']: p for n, p in H_layout.items()}

    """
    import pydot

    P = to_pydot(G)
    if root is not None:
        P.set("root", str(root))

    # List of low-level bytes comprising a string in the dot language converted
    # from the passed graph with the passed external GraphViz command.
    D_bytes = P.create_dot(prog=prog)

    # Unique string decoded from these bytes with the preferred locale encoding
    D = str(D_bytes, encoding=getpreferredencoding())

    if D == "":  # no data returned
        print(f"Graphviz layout with {prog} failed")
        print()
        print("To debug what happened try:")
        print("P = nx.nx_pydot.to_pydot(G)")
        print('P.write_dot("file.dot")')
        print(f"And then run {prog} on file.dot")
        return

    # List of one or more "pydot.Dot" instances deserialized from this string.
    Q_list = pydot.graph_from_dot_data(D)
    assert len(Q_list) == 1

    # The first and only such instance, as guaranteed by the above assertion.
    Q = Q_list[0]

    node_pos = {}
    for n in G.nodes():
        pydot_node = pydot.Node(str(n)).get_name()
        node = Q.get_node(pydot_node)

        if isinstance(node, list):
            node = node[0]
        pos = node.get_pos()[1:-1]  # strip leading and trailing double quotes
        if pos is not None:
            xx, yy = pos.split(",")
            node_pos[n] = (float(xx), float(yy))
    return node_pos
Exemple #48
0
titanic_train.info()  #Not mandatory though!!

#Let's start the journey with non categorical and non missing data columns
X_titanic_train = titanic_train[['Pclass', 'SibSp', 'Parch']]  #X-Axis
y_titanic_train = titanic_train['Survived']  #Y-Axis

#Build the decision tree model
dt = tree.DecisionTreeClassifier()
dt.fit(X_titanic_train, y_titanic_train)

#visualize the decission tree
objStringIO = io.StringIO()
tree.export_graphviz(dt,
                     out_file=objStringIO,
                     feature_names=X_titanic_train.columns)
#Use out_file = objStringIO to getvalues()
file1 = pydot.graph_from_dot_data(objStringIO.getvalue())  #[0]
#os.chdir("D:\\Data Science\\Data\\")
file1.write_pdf("DecissionTree1_Small.pdf")

#Predict the outcome using decision tree
#Read the Test Data
titanic_test = pd.read_csv("D:\\Data Science\\Data\\titanic_test.csv")
X_test = titanic_test[['Pclass', 'SibSp', 'Parch']]
#Use .predict method on Test data using the model which we built
titanic_test['Survived'] = dt.predict(X_test)
os.getcwd()  #To get current working directory
titanic_test.to_csv("submission_Titanic.csv",
                    columns=['PassengerId', 'Survived'],
                    index=False)
Exemple #49
0
def generate_flux_diagram(reaction_model,
                          times,
                          concentrations,
                          reaction_rates,
                          output_directory,
                          central_species_list=None,
                          superimpose=False,
                          species_directory=None,
                          settings=None):
    """
    For a given `reaction_model` and simulation results stored as arrays of
    `times`, species `concentrations`, and `reaction_rates`, generate a series
    of flux diagrams as frames of an animation, then stitch them together into
    a movie. The individual frames and the final movie are saved on disk at
    `output_directory.`
    """
    global max_node_count, max_edge_count, concentration_tol, species_rate_tol, max_node_pen_width, max_edge_pen_width, radius, central_reaction_count
    # Allow user defined settings for flux diagram generation if given
    if settings:
        max_node_count = settings.get('max_node_count', max_node_count)
        max_edge_count = settings.get('max_edge_count', max_edge_count)
        concentration_tol = settings.get('concentration_tol',
                                         concentration_tol)
        species_rate_tol = settings.get('species_rate_tol', species_rate_tol)
        max_node_pen_width = settings.get('max_node_pen_width',
                                          max_node_pen_width)
        max_edge_pen_width = settings.get('max_edge_pen_width',
                                          max_edge_pen_width)
        radius = settings.get('radius', radius)
        central_reaction_count = settings.get('central_reaction_count',
                                              central_reaction_count)

    # Get the species and reactions corresponding to the provided concentrations and reaction rates
    species_list = reaction_model.core.species[:]
    num_species = len(species_list)
    reaction_list = reaction_model.core.reactions[:]

    # Search for indices of central species
    central_species_indices = []
    if central_species_list is not None:
        for centralSpecies in central_species_list:
            for i, species in enumerate(species_list):
                if species.index == centralSpecies:
                    central_species_indices.append(i)
                    break
            else:
                raise Exception(
                    "Central species '{}' could not be found in species list.".
                    format(centralSpecies))

    # Compute the rates between each pair of species (big matrix warning!)
    species_rates = np.zeros((len(times), num_species, num_species),
                             np.float64)
    for index, reaction in enumerate(reaction_list):
        rate = reaction_rates[:, index]
        if not reaction.pairs: reaction.generate_pairs()
        for reactant, product in reaction.pairs:
            reactant_index = species_list.index(reactant)
            product_index = species_list.index(product)
            species_rates[:, reactant_index, product_index] += rate
            species_rates[:, product_index, reactant_index] -= rate

    # Determine the maximum concentration for each species and the maximum overall concentration
    max_concentrations = np.max(np.abs(concentrations), axis=0)
    max_concentration = np.max(max_concentrations)

    # Determine the maximum reaction rates
    max_reaction_rates = np.max(np.abs(reaction_rates), axis=0)

    # Determine the maximum rate for each species-species pair and the maximum overall species-species rate
    max_species_rates = np.max(np.abs(species_rates), axis=0)
    max_species_rate = np.max(max_species_rates)
    species_index = max_species_rates.reshape(
        (num_species * num_species)).argsort()

    # Determine the nodes and edges to keep
    nodes = []
    edges = []
    if not superimpose and central_species_list is not None:
        for central_species_index in central_species_indices:
            nodes.append(central_species_index)
            add_adjacent_nodes(central_species_index,
                               nodes,
                               edges,
                               species_list,
                               reaction_list,
                               max_reaction_rates,
                               max_species_rates,
                               reactionCount=central_reaction_count,
                               rad=radius)
    else:
        for i in range(num_species * num_species):
            product_index, reactant_index = divmod(species_index[-i - 1],
                                                   num_species)
            if reactant_index > product_index:
                # Both reactant -> product and product -> reactant are in this list,
                # so only keep one of them
                continue
            if max_species_rates[reactant_index, product_index] == 0:
                break
            if reactant_index not in nodes and len(nodes) < max_node_count:
                nodes.append(reactant_index)
            if product_index not in nodes and len(nodes) < max_node_count:
                nodes.append(product_index)
            if [reactant_index, product_index] not in edges and [
                    product_index, reactant_index
            ] not in edges:
                edges.append([reactant_index, product_index])
            if len(nodes) > max_node_count:
                break
            if len(edges) >= max_edge_count:
                break

        if superimpose and central_species_list is not None:
            nodes_copy = nodes[:]
            for central_species_index in central_species_indices:
                if central_species_index not in nodes:  # Only add central species if it doesn't already exist
                    nodes.append(central_species_index)
                    # Recursively add nodes until they connect with main graph
                    add_adjacent_nodes(
                        central_species_index,
                        nodes,
                        edges,
                        species_list,
                        reaction_list,
                        max_reaction_rates,
                        max_species_rates,
                        reactionCount=central_reaction_count,
                        rad=
                        -1,  # "-1" signifies that we add nodes until they connect to the main graph
                        mainNodes=nodes_copy)

    # Create the master graph
    # First we're going to generate the coordinates for all of the nodes; for
    # this we use the thickest pen widths for all nodes and edges
    graph = pydot.Dot('flux_diagram', graph_type='digraph', overlap="false")
    graph.set_rankdir('LR')
    graph.set_fontname('sans')
    graph.set_fontsize('10')

    # Add a node for each species
    for index in nodes:
        species = species_list[index]
        node = pydot.Node(name=str(species))
        node.set_penwidth(max_node_pen_width)
        graph.add_node(node)
        # Try to use an image instead of the label
        species_index = str(species) + '.png'
        image_path = ''
        if not species_directory or not os.path.exists(species_directory):
            continue
        for root, dirs, files in os.walk(species_directory):
            for f in files:
                if f.endswith(species_index):
                    image_path = os.path.join(root, f)
                    break
        if os.path.exists(image_path):
            node.set_image(image_path)
            node.set_label(" ")
    # Add an edge for each species-species rate
    for reactant_index, product_index in edges:
        if reactant_index in nodes and product_index in nodes:
            reactant = species_list[reactant_index]
            product = species_list[product_index]
            edge = pydot.Edge(str(reactant), str(product))
            edge.set_penwidth(max_edge_pen_width)
            graph.add_edge(edge)

    # Generate the coordinates for all of the nodes using the specified program
    graph = pydot.graph_from_dot_data(
        graph.create_dot(prog=program).decode('utf-8'))[0]

    # Now iterate over the time points, setting the pen widths appropriately
    # This should preserve the coordinates of the nodes from frame to frame
    frame_number = 1
    for t in range(len(times)):
        # Update the nodes
        slope = -max_node_pen_width / math.log10(concentration_tol)
        for index in nodes:
            species = species_list[index]
            if re.search(r'^[a-zA-Z0-9_]*$', str(species)) is not None:
                species_string = str(species)
            else:
                # species name contains special characters
                species_string = '"{0}"'.format(str(species))

            node = graph.get_node(species_string)[0]
            concentration = concentrations[t, index] / max_concentration
            if concentration < concentration_tol:
                penwidth = 0.0
            else:
                penwidth = round(
                    slope * math.log10(concentration) + max_node_pen_width, 3)
            node.set_penwidth(penwidth)
        # Update the edges
        slope = -max_edge_pen_width / math.log10(species_rate_tol)
        for index in range(len(edges)):
            reactant_index, product_index = edges[index]
            if reactant_index in nodes and product_index in nodes:
                reactant = species_list[reactant_index]
                product = species_list[product_index]

                if re.search(r'^[a-zA-Z0-9_]*$', str(reactant)) is not None:
                    reactant_string = str(reactant)
                else:
                    reactant_string = '"{0}"'.format(str(reactant))

                if re.search(r'^[a-zA-Z0-9_]*$', str(product)) is not None:
                    product_string = str(product)
                else:
                    product_string = '"{0}"'.format(str(product))

                edge = graph.get_edge(reactant_string, product_string)[0]
                # Determine direction of arrow based on sign of rate
                species_rate = species_rates[t, reactant_index,
                                             product_index] / max_species_rate
                if species_rate < 0:
                    edge.set_dir("back")
                    species_rate = -species_rate
                else:
                    edge.set_dir("forward")
                # Set the edge pen width
                if species_rate < species_rate_tol:
                    penwidth = 0.0
                    edge.set_dir("none")
                else:
                    penwidth = round(
                        slope * math.log10(species_rate) + max_edge_pen_width,
                        3)
                edge.set_penwidth(penwidth)
        # Save the graph at this time to a dot file and a PNG image
        if times[t] == 0:
            label = 't = 0 s'
        else:
            label = 't = 10^{0:.1f} s'.format(math.log10(times[t]))
        graph.set_label(label)
        if t == 0:
            repeat = video_fps * initial_padding
        elif t == len(times) - 1:
            repeat = video_fps * final_padding
        else:
            repeat = 1
        for r in range(repeat):
            graph.write_dot(
                os.path.join(output_directory,
                             'flux_diagram_{0:04d}.dot'.format(frame_number)))
            graph.write_png(
                os.path.join(output_directory,
                             'flux_diagram_{0:04d}.png'.format(frame_number)))
            frame_number += 1

    # Use ffmpeg to stitch the PNG images together into a movie
    import subprocess

    command = [
        'ffmpeg',
        '-framerate',
        '{0:d}'.format(video_fps),  # Duration of each image
        '-i',
        'flux_diagram_%04d.png',  # Input file format
        '-c:v',
        'mpeg4',  # Encoder
        '-r',
        '30',  # Video framerate
        '-pix_fmt',
        'yuv420p',  # Pixel format
        'flux_diagram.avi'
    ]  # Output filename

    subprocess.check_call(command, cwd=output_directory)
Exemple #50
0
print(len(leave_id))

#limit to 20 features
indices = indices[:20]

for i in indices:
    print(feature_names[i], ":", importances[i])

# Visualising
import pydot
from io import StringIO
from sklearn.tree import export_graphviz

dotfile = StringIO()
export_graphviz(model, out_file=dotfile, feature_names=X.columns)
graph = pydot.graph_from_dot_data(dotfile.getvalue())
graph[0].write_png("Pancake_Decision_Tree_1.png")

#variable is used for the first split and competing splits for this first split
feature_names[model.tree_.feature]

from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report, accuracy_score

# hyperparameters and model performance
# max_leaf_nodes
test_score = []
train_score = []

# check model performance for max depth from 2-200
for max_leaf_nodes in range(2, 200):
Exemple #51
0
def write_clf_pdf(clf, output_name):
    dot_data = StringIO()
    tree.export_graphviz(clf, out_file=dot_data)
    graph = pydot.graph_from_dot_data(dot_data.getvalue())
    graph[0].write_pdf(output_name)
Exemple #52
0
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.model_selection import train_test_split

if __name__ == '__main__':

    df = pd.read_csv('auto-mpg.csv', delimiter=',')
    data = df.values

    encoder = LabelEncoder()
    # car_name encode
    data[:, -1] = encoder.fit_transform(data[:, -1])
    # sort by mpg column
    data = data[data[:, 0].argsort()]

    X, y = data[:, 1:], data[:, 0]

    for class_num, i in enumerate(range(0, y.shape[0], 150)):
        y[i:i + 150] = class_num
    y = y.astype(np.int)
    X_train, X_test, y_train, y_test = train_test_split(X, y)
    clf = DecisionTreeClassifier(max_depth=3)

    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    print('Test accuracy: %.3f' % np.mean(y_pred == y_test))
    # graph visualization
    clf_dot = export_graphviz(clf)
    (graph, ) = pydot.graph_from_dot_data(clf_dot)
    graph.write_png('graph2.png')
Exemple #53
0
def Protocol_Inference(file_path):
    print("[#] IN&S PROTOCOL INFERENCE PROGRAM")
    print("[>] 파일 " + str(file_path) + "에 대한 분석을 수행합니다")
    messages = PCAPImporter.readFile(
        file_path).values()  # 파일을 PCAPImporter를 통해 읽음

    print(
        "-----------------------------------------------------------------------------------------------------------------"
    )
    print("[1] 프로토콜을 통한 메시지의 종류")
    for message in messages:
        print(message)

    symbol = Symbol(messages=messages)
    Format.splitDelimiter(symbol, ASCII("#"))
    symbols = Format.clusterByKeyField(symbol, symbol.fields[0])

    print(
        "-----------------------------------------------------------------------------------------------------------------"
    )
    # print("[+] Number of symbols after clustering: {0}".format(len(symbols)))
    print("[2] 프로토콜 내 Symbol의 종류:")
    for keyFieldName, s in symbols.items():
        try:
            print("   - " +
                  bytes.fromhex(keyFieldName.decode("utf-8")).decode('utf-8'))
            message_symbols.append(
                bytes.fromhex(keyFieldName.decode("utf-8")).decode('utf-8'))
        except:
            print("  * {0}".format(keyFieldName))
    print("---------------------------------------------------------------")

    symbol_cnt = 1
    for symbol in symbols.values():
        rels = RelationFinder.findOnSymbol(symbol)
        for rel in rels:
            # Apply first found relationship
            rel = rels[0]
            rel["x_fields"][0].domain = Size(rel["y_fields"], factor=1 / 8.0)

        print("[" + str(symbol_cnt) + "] Packet(Message) Struct")

        # recieve row message struct
        message = symbol._str_debug()
        message = message.replace('Raw', 'Message')
        message = message.replace('Field', 'Attribute')
        message = message.replace('((', 'Range(Min/Max)=')
        message = message.replace('))', '')

        # [1] message_symbol
        message_symbol = message[message.find('Symbol_'):message.find('|')]
        message_symbol = message_symbol.replace('Symbol_', '')

        # convert string -> byte
        try:
            byte_message_symbol = str.encode(message_symbol.replace('\n', ''))
            byte_message_symbol = bytes.fromhex(
                byte_message_symbol.decode("utf-8")).decode('utf-8')
        except:
            byte_message_symbol = message_symbol

        # [2] message_offset
        print('[ Message keyword in IN&S inference tool ] ' +
              byte_message_symbol + message[message.find('|'):])

        symbol_cnt += 1
        print(
            "---------------------------------------------------------------")

    session = Session(messages)
    abstractSession = session.abstract(list(symbols.values()))
    automata = Automata.generateChainedStatesAutomata(abstractSession,
                                                      list(symbols.values()))

    file_name = os.path.basename(file_path).replace(".pcap", "")
    graph_path = os.path.join("FSM", file_name + ".png")  # 파일이 저장될 경로
    print("[>] 프로토콜의 동작원리 (FSM:Finite-State Machine) 이미지 저장 및 출력 완료")
    print("[-] 이미지 저장경로 : " + str(graph_path))
    dotcode = automata.generateDotCode()  # netzob를 통한 결과를 dotcode 형식으로 받아옴
    graph = pydot.graph_from_dot_data(dotcode)[
        0]  # dotcode 형식을 pydot 라이브러리를 통해 float 형식으로 바꿈
    graph.write_png(graph_path)  # float 형식의 결과 이미지를 png 구조로 저장
    image = Image.open(graph_path)  # png 파일을 plt 라이브러리를 통해 읽어드림
    image.show()  # 이미지를 화면에 출력함
Exemple #54
0
def visualize_decision_tree(dm_model, feature_names, save_name):
	dotfile = StringIO()
	export_graphviz(dm_model, out_file=dotfile, feature_names=feature_names)
	graph = pydot.graph_from_dot_data(dotfile.getvalue())
	graph[0].write_png(save_name) # saved in the following file
Exemple #55
0
def dag_drawer(dag, scale=0.7, filename=None, style="color"):
    """Plot the directed acyclic graph (dag) to represent operation dependencies
    in a quantum circuit.

    Note this function leverages
    `pydot <https://github.com/erocarrera/pydot>`_ to generate the graph, which
    means that having `Graphviz <https://www.graphviz.org/>`_ installed on your
    system is required for this to work.

    The current release of Graphviz can be downloaded here: <https://graphviz.gitlab.io/download/>.
    Download the version of the software that matches your environment and follow the instructions
    to install Graph Visualization Software (Graphviz) on your operating system.

    Args:
        dag (DAGCircuit): The dag to draw.
        scale (float): scaling factor
        filename (str): file path to save image to (format inferred from name)
        style (str): 'plain': B&W graph
                     'color' (default): color input/output/op nodes

    Returns:
        PIL.Image: if in Jupyter notebook and not saving to file,
            otherwise None.

    Raises:
        VisualizationError: when style is not recognized.
        MissingOptionalLibraryError: when pydot or pillow are not installed.

    Example:
        .. jupyter-execute::

            %matplotlib inline
            from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
            from qiskit.dagcircuit import DAGCircuit
            from qiskit.converters import circuit_to_dag
            from qiskit.visualization import dag_drawer

            q = QuantumRegister(3, 'q')
            c = ClassicalRegister(3, 'c')
            circ = QuantumCircuit(q, c)
            circ.h(q[0])
            circ.cx(q[0], q[1])
            circ.measure(q[0], c[0])
            circ.rz(0.5, q[1]).c_if(c, 2)

            dag = circuit_to_dag(circ)
            dag_drawer(dag)
    """
    try:
        import pydot
    except ImportError as ex:
        raise MissingOptionalLibraryError(
            libname="PyDot",
            name="dag_drawer",
            pip_install="pip install pydot",
        ) from ex
    # NOTE: use type str checking to avoid potential cyclical import
    # the two tradeoffs ere that it will not handle subclasses and it is
    # slower (which doesn't matter for a visualization function)
    type_str = str(type(dag))
    if "DAGDependency" in type_str:
        graph_attrs = {"dpi": str(100 * scale)}

        def node_attr_func(node):
            if style == "plain":
                return {}
            if style == "color":
                n = {}
                n["label"] = str(node.node_id) + ": " + str(node.name)
                if node.name == "measure":
                    n["color"] = "blue"
                    n["style"] = "filled"
                    n["fillcolor"] = "lightblue"
                if node.name == "barrier":
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "green"
                if node.op._directive:
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "red"
                if node.op.condition:
                    n["label"] = str(node.node_id) + ": " + str(
                        node.name) + " (conditional)"
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "lightgreen"
                return n
            else:
                raise VisualizationError(
                    "Unrecognized style %s for the dag_drawer." % style)

        edge_attr_func = None

    else:
        bit_labels = {
            bit: f"{reg.name}[{idx}]"
            for reg in list(dag.qregs.values()) + list(dag.cregs.values())
            for (idx, bit) in enumerate(reg)
        }

        graph_attrs = {"dpi": str(100 * scale)}

        def node_attr_func(node):
            if style == "plain":
                return {}
            if style == "color":
                n = {}
                if isinstance(node, DAGOpNode):
                    n["label"] = node.name
                    n["color"] = "blue"
                    n["style"] = "filled"
                    n["fillcolor"] = "lightblue"
                if isinstance(node, DAGInNode):
                    n["label"] = bit_labels[node.wire]
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "green"
                if isinstance(node, DAGOutNode):
                    n["label"] = bit_labels[node.wire]
                    n["color"] = "black"
                    n["style"] = "filled"
                    n["fillcolor"] = "red"
                return n
            else:
                raise VisualizationError("Invalid style %s" % style)

        def edge_attr_func(edge):
            e = {}
            e["label"] = bit_labels[edge]
            return e

    dot_str = dag._multi_graph.to_dot(node_attr_func, edge_attr_func,
                                      graph_attrs)
    dot = pydot.graph_from_dot_data(dot_str)[0]

    if filename:
        extension = filename.split(".")[-1]
        dot.write(filename, format=extension)
        return None
    elif ("ipykernel" in sys.modules) and ("spyder" not in sys.modules):
        if not HAS_PIL:
            raise MissingOptionalLibraryError(
                libname="pillow",
                name="dag_drawer",
                pip_install="pip install pillow",
            )

        with tempfile.TemporaryDirectory() as tmpdirname:
            tmp_path = os.path.join(tmpdirname, "dag.png")
            dot.write_png(tmp_path)
            with Image.open(tmp_path) as test_image:
                image = test_image.copy()
            os.remove(tmp_path)
            return image
    else:
        if not HAS_PIL:
            raise MissingOptionalLibraryError(
                libname="pillow",
                name="dag_drawer",
                pip_install="pip install pillow",
            )
        with tempfile.TemporaryDirectory() as tmpdirname:
            tmp_path = os.path.join(tmpdirname, "dag.png")
            dot.write_png(tmp_path)
            image = Image.open(tmp_path)
            image.show()
            os.remove(tmp_path)
            return None
Exemple #56
0
    def to_graph(self, png=False, svg=False):
        import pydot
        graph = pydot.Dot(graph_type='digraph',
                          rankdir='LR',
                          splines='ortho',
                          decorate=True)

        nodes = {}
        for c in list(self.containers.values()) + list(self.inputs.values()):
            node = pydot.Node(c.name, shape='rect')
            nodes[c.name] = node
            graph.add_node(node)

        edges = []
        for c in self.containers.values():
            target = c.name
            v = nodes[target]
            for key, val in c.inputs.items():
                if isinstance(val, Symbol):
                    source = val.container.name
                    label = val.key
                elif isinstance(val, Input):
                    source = val.name
                    label = ''
                else:
                    raise Exception()
                u = nodes[source]
                graph.add_edge(pydot.Edge(u, v, label=label))
                edges.append((source, target, label))
        if png:
            png_str = graph.create_png(prog='dot')

            return png_str

        else:
            D_bytes = graph.create_dot(prog='dot')

            D = str(D_bytes, encoding='utf-8')

            if D == "":  # no data returned
                print("Graphviz layout with %s failed" % (prog))
                print()
                print("To debug what happened try:")
                print("P = nx.nx_pydot.to_pydot(G)")
                print("P.write_dot(\"file.dot\")")
                print("And then run %s on file.dot" % (prog))

            # List of "pydot.Dot" instances deserialized from this string.
            Q_list = pydot.graph_from_dot_data(D)
            assert len(Q_list) == 1
            Q = Q_list[0]

            # return Q

            def get_node(Q, n):
                node = Q.get_node(n)

                if isinstance(node, list) and len(node) == 0:
                    node = Q.get_node('"{}"'.format(n))
                    assert node

                return node[0]

            def get_label_xy(x, y, ex, ey):
                min_dist = np.inf
                min_ex, min_ey = [0, 0], [0, 0]
                for _ex, _ey in zip(zip(ex, ex[1:]), zip(ey, ey[1:])):
                    dist = (np.mean(_ex) - x)**2 + (np.mean(_ey) - y)**2
                    if dist < min_dist:
                        min_dist = dist
                        min_ex[:] = _ex[:]
                        min_ey[:] = _ey[:]
                if min_ex[0] == min_ex[1]:
                    _x = min_ex[0]
                    _x = np.sign(x - _x) * 10 + _x
                    _y = y
                else:
                    _x = x
                    _y = min_ey[0]
                    _y = np.sign(y - _y) * 10 + _y
                return _x, _y - 3

            elements = []
            bb = Q.get_bb()
            viewbox = bb[1:-1].replace(',', ' ')

            for n in nodes.keys():

                node = get_node(Q, n)

                # strip leading and trailing double quotes
                pos = node.get_pos()[1:-1]

                if pos is not None:
                    obj = self.get_obj(n)
                    w = float(node.get_width())
                    h = float(node.get_height())

                    x, y = map(float, pos.split(","))
                    attrs = {
                        'width': w,
                        'height': h,
                        'rx': 5,
                        'ry': 5,
                        'x': x,
                        'y': y,
                        'stroke-width': 1.5,
                        'fill': 'none',
                        'stroke': '#48caf9'
                    }

                    elements.append({
                        'label': [n, x, y],
                        'shape': 'rect',
                        'attrs': attrs,
                        'latex': obj.latex_src,
                        'graph': obj.graph_src
                    })

            min_x, min_y, scale_w, scale_h = np.inf, np.inf, 0, 0
            for el in elements:
                if min_x > el['attrs']['x']:
                    min_x = el['attrs']['x']
                    scale_w = 2 * min_x / el['attrs']['width']
                if min_y > el['attrs']['y']:
                    min_y = el['attrs']['y']
                    scale_h = 2 * min_y / el['attrs']['height']
            for el in elements:
                w = scale_w * el['attrs']['width']
                h = scale_h * el['attrs']['height']
                el['attrs']['x'] = el['attrs']['x'] - w / 2
                el['attrs']['y'] = el['attrs']['y'] - h / 2
                el['attrs']['width'] = w
                el['attrs']['height'] = h

            for e in Q.get_edge_list():
                pos = (e.get_pos()[1:-1]).split(' ')
                ax, ay = [float(v) for v in pos[0].split(',')[1:]]
                pos = [v.split(',') for v in pos[1:]]

                xx = [float(v[0]) for v in pos] + [ax]
                yy = [float(v[1]) for v in pos] + [ay]
                x, y, _x, _y = [], [], 0, 0
                for __x, __y in zip(xx, yy):
                    if not (__x == _x and __y == _y):
                        x.append(__x)
                        y.append(__y)
                    _x = __x
                    _y = __y
                path = ["{} {}".format(_x, _y) for _x, _y in zip(x, y)]
                p = 'M' + " L".join(path)
                attrs = {
                    'd': p,
                    'stroke-width': 1.5,
                    'fill': 'none',
                    'stroke': 'black'
                }
                lp = e.get_lp()
                if lp:
                    lx, ly = [float(v) for v in lp[1:-1].split(',')]
                    lx, ly = get_label_xy(lx, ly, x, y)
                    label = [e.get_label() or '', lx, ly]
                elements.append({
                    'label': label,
                    'shape': 'path',
                    'attrs': attrs
                })
            output = {'elements': elements, 'viewbox': viewbox}
            return output
import pandas as pd
import pydot
from IPython.display import SVG

data_dir = os.path.join(os.getcwd(), 'data', 'charity.txt')
df = pd.read_table(data_dir, sep="\t")

from pycausal.pycausal import pycausal as pc
pc = pc()
pc.start_vm()

from pycausal import search as s
tetrad = s.tetradrunner()
tetrad.run(algoId='fges',
           dfs=df,
           scoreId='sem-bic',
           dataType='continuous',
           penaltyDiscount=2,
           maxDegree=-1,
           faithfulnessAssumed=True,
           verbose=True)

tetrad.getNodes()
tetrad.getEdges()

dot_str = pc.tetradGraphToDot(tetrad.getTetradGraph())
graphs = pydot.graph_from_dot_data(dot_str)
graphs[0].write_svg('fges-continuous.svg')

pc.stop_vm()
Exemple #58
0
def read(string):
    """
    Read a graph from a string in Dot language and return it. Nodes and edges specified in the
    input will be added to the current graph.

    @type  string: string
    @param string: Input string in Dot format specifying a graph.

    @rtype: graph
    @return: Graph
    """

    dotG = pydot.graph_from_dot_data(string)

    if (dotG.get_type() == "graph"):
        G = graph()
    elif (dotG.get_type() == "digraph"):
        G = digraph()
    elif (dotG.get_type() == "hypergraph"):
        return read_hypergraph(string)
    else:
        raise InvalidGraphType

    # Read nodes...
    # Note: If the nodes aren't explicitly listed, they need to be
    for each_node in dotG.get_nodes():
        G.add_node(each_node.get_name())
        for each_attr_key, each_attr_val in each_node.get_attributes().items():
            G.add_node_attribute(each_node.get_name(),
                                 (each_attr_key, each_attr_val))

    # Read edges...
    for each_edge in dotG.get_edges():
        # Check if the nodes have been added
        if not G.has_node(each_edge.get_source()):
            G.add_node(each_edge.get_source())
        if not G.has_node(each_edge.get_destination()):
            G.add_node(each_edge.get_destination())

        # See if there's a weight
        if 'weight' in each_edge.get_attributes().keys():
            _wt = each_edge.get_attributes()['weight']
        else:
            _wt = 1

        # See if there is a label
        if 'label' in each_edge.get_attributes().keys():
            _label = each_edge.get_attributes()['label']
        else:
            _label = ''

        G.add_edge((each_edge.get_source(), each_edge.get_destination()),
                   wt=_wt,
                   label=_label)

        for each_attr_key, each_attr_val in each_edge.get_attributes().items():
            if not each_attr_key in ['weight', 'label']:
                G.add_edge_attribute((each_edge.get_source(), each_edge.get_destination()), \
                                     (each_attr_key, each_attr_val))

    return G
Exemple #59
0
    # Basic decision tree
    print("\n\nPart (a-b): simplified decision tree")
    #dt = DecisionTree(max_depth=3, feature_labels=features)
    #dt.fit(X, y)
    #print("Predictions", dt.predict(Z)[:100])

    print("\n\nPart (c): sklearn's decision tree")
    clf = sklearn.tree.DecisionTreeClassifier(random_state=0, **params)
    clf.fit(X, y)
    evaluate(clf)
    out = io.StringIO()
    sklearn.tree.export_graphviz(clf,
                                 out_file=out,
                                 feature_names=features,
                                 class_names=class_names)
    graph = pydot.graph_from_dot_data(out.getvalue())
    pydot.graph_from_dot_data(out.getvalue())[0].write_pdf("%s-tree.pdf" %
                                                           dataset)

    # TODO implement and evaluate parts c-h
    bt = BaggedTrees()
    bt.fit(X, y)
    #evaluate(bt)
    zp = bt.predict(Z)
    np.savetxt('zp_spam', zp, fmt='%d')

    #bt = RandomForest(m=6)
    #bt.fit(X,y)
    #evaluate(bt)

    #bt = BoostedRandomForest()
Exemple #60
0
    def __init__(self,
                 treatment_name,
                 outcome_name,
                 graph=None,
                 common_cause_names=None,
                 instrument_names=None,
                 effect_modifier_names=None,
                 mediator_names=None,
                 observed_node_names=None,
                 missing_nodes_as_confounders=False):
        self.treatment_name = parse_state(treatment_name)
        self.outcome_name = parse_state(outcome_name)
        instrument_names = parse_state(instrument_names)
        common_cause_names = parse_state(common_cause_names)
        effect_modifier_names = parse_state(effect_modifier_names)
        mediator_names = parse_state(mediator_names)
        self.logger = logging.getLogger(__name__)

        #re.sub only takes string parameter so the first if is to avoid error
        #if the input is a text file, convert the contained data into string
        if isinstance(graph, str) and re.match(r".*\.txt", str(graph)):
            text_file = open(graph, "r")
            graph = text_file.read()
            text_file.close()

        if isinstance(graph, str) and re.match(
                r"^dag", graph):  #Convert daggity output to dot format
            graph = daggity_to_dot(graph)

        if isinstance(graph, str):
            graph = graph.replace("\n", " ")

        if graph is None:
            self._graph = nx.DiGraph()
            self._graph = self.build_graph(common_cause_names,
                                           instrument_names,
                                           effect_modifier_names,
                                           mediator_names)
        elif re.match(r".*\.dot", graph):
            # load dot file
            try:
                import pygraphviz as pgv
                self._graph = nx.DiGraph(nx.drawing.nx_agraph.read_dot(graph))
            except Exception as e:
                self.logger.error("Pygraphviz cannot be loaded. " + str(e) +
                                  "\nTrying pydot...")
                try:
                    import pydot
                    self._graph = nx.DiGraph(
                        nx.drawing.nx_pydot.read_dot(graph))
                except Exception as e:
                    self.logger.error("Error: Pydot cannot be loaded. " +
                                      str(e))
                    raise e
        elif re.match(r".*\.gml", graph):
            self._graph = nx.DiGraph(nx.read_gml(graph))
        elif re.match(r".*graph\s*\{.*\}\s*", graph):
            try:
                import pygraphviz as pgv
                self._graph = pgv.AGraph(graph, strict=True, directed=True)
                self._graph = nx.drawing.nx_agraph.from_agraph(self._graph)
            except Exception as e:
                self.logger.error("Error: Pygraphviz cannot be loaded. " +
                                  str(e) + "\nTrying pydot ...")
                try:
                    import pydot
                    P_list = pydot.graph_from_dot_data(graph)
                    self._graph = nx.drawing.nx_pydot.from_pydot(P_list[0])
                except Exception as e:
                    self.logger.error("Error: Pydot cannot be loaded. " +
                                      str(e))
                    raise e
        elif re.match(".*graph\s*\[.*\]\s*", graph):
            self._graph = nx.DiGraph(nx.parse_gml(graph))
        else:
            self.logger.error(
                "Error: Please provide graph (as string or text file) in dot or gml format."
            )
            self.logger.error("Error: Incorrect graph format")
            raise ValueError
        if missing_nodes_as_confounders:
            self._graph = self.add_missing_nodes_as_common_causes(
                observed_node_names)
        # Adding node attributes
        self._graph = self.add_node_attributes(observed_node_names)