Example #1
0
def run() -> None:
    """Main program loop"""

    args = parse_args()

    dm = DataModel(args.url, args.http_username, args.http_password, args.uid,
                   args.cacert)
    dm.load()

    if dm.status != 200:
        print("{} Status code {}".format(str(datetime.datetime.now()),
                                         dm.status))
        return

    try:
        old_dm = pickle.load(open("cache.dat", "rb"))
        if old_dm == dm:
            return
    except FileNotFoundError:
        print("First run")

    print("{} Graphing changes".format(str(datetime.datetime.now())))

    pickle.dump(dm, open("cache.dat", "wb"))

    dot = graphviz.Digraph(comment="Double edge facts")

    # for obj in dm.objects:
    #     dot.node(obj, obj)

    for fact in dm.facts:
        name, s, d, di = fact
        if name == "mentions":
            continue
        if not d:
            continue

        dot.node(s, s)
        dot.node(d, d)
        if di:
            dot.edge(s, d, label=name, dir="both")
        else:
            dot.edge(s, d, label=name)

    if args.dump_source:
        with open(os.path.join(args.dump_source, "double.dot"), "w") as f:
            f.write(dot.source)

    dot.render("output/double", format="png", renderer="cairo")

    dot = graphviz.Digraph(comment="Single edge facts")

    for fact in dm.facts:

        name, s, d, di = fact

        if d:
            continue

        dot.node(name, label=name, shape="diamond")
        dot.node(s, s)

        dot.edge(s, name)

    if args.dump_source:
        with open(os.path.join(args.dump_source, "single.dot"), "w") as f:
            f.write(dot.source)

    dot.render("output/single", format="png", renderer="cairo")

    dot = graphviz.Digraph(comment="All Double edge facts")

    for obj in dm.objects:
        dot.node(obj, obj)

    for fact in dm.facts:
        name, s, d, di = fact
        if not d:
            continue

        dot.node(s, s)
        dot.node(d, d)
        if di:
            dot.edge(s, d, label=name, dir="both")
        else:
            dot.edge(s, d, label=name)

    if args.dump_source:
        with open(os.path.join(args.dump_source, "complete.dot"), "w") as f:
            f.write(dot.source)

    dot.render("output/complete", format="png", renderer="cairo")

    if args.parent_id:
        try:
            os.environ.pop("https_proxy")
        except KeyError:
            pass
        try:
            os.environ.pop("http_proxy")
        except KeyError:
            pass

        verify_ssl = False if args.cacert else True

        confluence = Confluence(
            url=args.confluence_url,
            username=args.confluence_user,
            password=args.confluence_password,
            verify_ssl=verify_ssl,
        )
        confluence.attach_file(
            "output/double.cairo.png",
            page_id=args.parent_id,
            title="Double Edged Facts",
        )
        confluence.attach_file(
            "output/single.cairo.png",
            page_id=args.parent_id,
            title="Single Edged Facts",
        )
        confluence.attach_file(
            "output/complete.cairo.png",
            page_id=args.parent_id,
            title="Single Edged Facts",
        )

        if args.dump_source:
            confluence.attach_file(
                os.path.join(args.dump_source, "complete.dot"),
                page_id=args.parent_id,
                title="complete source",
            )
            confluence.attach_file(
                os.path.join(args.dump_source, "double.dot"),
                page_id=args.parent_id,
                title="double source",
            )
            confluence.attach_file(
                os.path.join(args.dump_source, "single.dot"),
                page_id=args.parent_id,
                title="single source",
            )
Example #2
0
def draw_net(config,
             genome,
             view=False,
             filename=None,
             node_names=None,
             show_disabled=True,
             prune_unused=False,
             node_colors=None,
             fmt='svg'):
    """ Receives a genome and draws a neural network with arbitrary topology. """
    # Attributes for network nodes.
    if graphviz is None:
        warnings.warn(
            "This display is not available due to a missing optional dependency (graphviz)"
        )
        return

    if node_names is None:
        node_names = {}

    assert type(node_names) is dict

    if node_colors is None:
        node_colors = {}

    assert type(node_colors) is dict

    node_attrs = {
        'shape': 'circle',
        'fontsize': '9',
        'height': '0.2',
        'width': '0.2'
    }

    dot = graphviz.Digraph(format=fmt, node_attr=node_attrs)

    inputs = set()
    for k in config.genome_config.input_keys:
        inputs.add(k)
        name = node_names.get(k, str(k))
        input_attrs = {
            'style': 'filled',
            'shape': 'box',
            'fillcolor': node_colors.get(k, 'lightgray')
        }
        dot.node(name, _attributes=input_attrs)

    outputs = set()
    for k in config.genome_config.output_keys:
        outputs.add(k)
        name = node_names.get(k, str(k))
        node_attrs = {
            'style': 'filled',
            'fillcolor': node_colors.get(k, 'lightblue')
        }

        dot.node(name, _attributes=node_attrs)

    if prune_unused:
        connections = set()
        for cg in genome.connections.values():
            if cg.enabled or show_disabled:
                connections.add((cg.in_node_id, cg.out_node_id))

        used_nodes = copy.copy(outputs)
        pending = copy.copy(outputs)
        while pending:
            new_pending = set()
            for a, b in connections:
                if b in pending and a not in used_nodes:
                    new_pending.add(a)
                    used_nodes.add(a)
            pending = new_pending
    else:
        used_nodes = set(genome.nodes.keys())

    for n in used_nodes:
        if n in inputs or n in outputs:
            continue

        attrs = {'style': 'filled', 'fillcolor': node_colors.get(n, 'white')}
        dot.node(str(n), _attributes=attrs)

    for cg in genome.connections.values():
        if cg.enabled or show_disabled:
            #if cg.input not in used_nodes or cg.output not in used_nodes:
            #    continue
            input, output = cg.key
            a = node_names.get(input, str(input))
            b = node_names.get(output, str(output))
            style = 'solid' if cg.enabled else 'dotted'
            color = 'green' if cg.weight > 0 else 'red'
            width = str(0.1 + abs(cg.weight / 5.0))
            dot.edge(a,
                     b,
                     _attributes={
                         'style': style,
                         'color': color,
                         'penwidth': width
                     })

    dot.render(filename, view=view)

    return dot
Example #3
0
def tree_dot(outfile, tree):
    tree_dot = gv.Digraph(format='svg', engine='dot')
    traversal(tree, 'root', tree_dot)
    f = open(outfile, 'w+')
    f.write(tree_dot.source)
    f.close()
Example #4
0
#print(model.labels_)
print('Estimated number of clusters: %d' % n_clusters_)
print('Estimated number of noise points: %d' % list(model.labels_).count(-1))
print(model2.cluster_centers_)

color_groups = []

for i in all_predictions:
    if i == 0:
        color_groups.append("green")
    if i == 1:
        color_groups.append("red")
    if i == 2:
        color_groups.append("blue")

d = gv.Digraph(directory=None,
               edge_attr=dict(dir='none', labeldistance='1.5', minlen='2'))
red = Color("red")
file1 = open("concepts.txt", "a")
for m in matrix:
    #print(m)
    file1.write(str(m) + '\n')
file1.close()
matrixForGraph = []

for tup in matrix:
    if len([x for x in matrixForGraph if x[1] == tup[1]]) == 0:
        matrixForGraph.append(tup)
matrixForGraph = sorted(matrixForGraph, key=lambda x: x[2])
colors = list(red.range_to(Color("green"), len(matrixForGraph)))

color_counter = 0
Example #5
0
File: ir.py Project: ml-lab/yaps
 def __init__(self, blocks):
     self.blocks = blocks
     self.dot = graphviz.Digraph()
     self.dot.attr('graph', rankdir='LR')
Example #6
0
    def visualizeTree(self, root, fname):
        """
        Visualizes tree from the root node
        """

        g = gv.Digraph(fname, filename=fname)
        g.format = 'png'
        nodes, edges = self.getAllnodesAndEdges(root)
        searched_nodes = self.getSearchedNum(nodes)
        total_nodes = len(nodes)
        # Default values
        searched = False
        node_shape = 'box'
        pen_color = 'black'
        node_style = ''
        color = ''

        if root['searched']:  # If the root is searched then search algortihm worked
            searched = True

        for node in nodes:
            if node['type'] == 'MAX':
                node_shape = 'box'
            elif node['type'] == 'MIN':
                node_shape = 'circle'
            else:
                if node['parent_type'] == 'MIN':
                    node_shape = 'box'
                else:
                    node_shape = 'circle'

            if searched and not node['searched']:
                pen_color = 'lightgrey'
                node_style = 'filled'
                color = 'lightgrey'
            else:
                pen_color = 'black'
                node_style = ''
                color = ''

            g.attr('node',
                   shape=node_shape,
                   pencolor=pen_color,
                   style=node_style,
                   color=color)

            node_label = "?"
            node_xlabel = ""
            try:
                node_label = str(node['value'])
                node_xlabel = node['name']
            except KeyError:
                node_label = "?"
                node_xlabel = ""

            if node['id'] == root['id']:
                node_xlabel = f"Total number of nodes: {total_nodes}\nSearched nodes: {searched_nodes}"

            g.node(node['id'], label=node_label, xlabel=node_xlabel)

        for node1, node2 in edges:
            g.edge(node1['id'], node2['id'])

        # Styling
        # penwidth='4'
        g.edge_attr.update(arrowhead='none')

        g.render(view=True, cleanup=True, format='png')
Example #7
0
def to_graphviz(topology_class, node_attr=None, edge_attr=None, **kwargs):
    """Convert a Topology into a DiGraph"""
    if not HAVE_GRAPHVIZ:
        raise ImportError(
            "The visualize command requires the `graphviz` Python"
            " library and `graphviz` system library to be "
            "installed."
        )
    attributes = {
        "fontsize": "16",
        "fontcolor": "white",
        "bgcolor": "#333333",
        "rankdir": "LR",
    }
    node_attributes = {
        "fontname": "Helvetica",
        "fontcolor": "white",
        "color": "white",
        "style": "filled",
        "fillcolor": "#006699",
    }
    edge_attributes = {
        "style": "solid",
        "color": "white",
        "arrowhead": "open",
        "fontname": "Helvetica",
        "fontsize": "12",
        "fontcolor": "white",
    }
    attributes.update(kwargs)
    if node_attr is not None:
        node_attributes.update(node_attr)
    if edge_attr is not None:
        edge_attributes.update(edge_attr)
    g = graphviz.Digraph(
        graph_attr=attributes, node_attr=node_attributes, edge_attr=edge_attributes
    )

    all_specs = {}
    all_specs.update(topology_class.thrift_bolts)
    all_specs.update(topology_class.thrift_spouts)

    sametail_nodes = set()

    for spec in topology_class.specs:
        if isinstance(spec, (JavaSpoutSpec, ShellSpoutSpec)):
            shape = "box"
        else:
            shape = None
        g.node(spec.name, label=spec.name, shape=shape)
        for stream_id, grouping in list(iteritems(spec.inputs)):
            parent = stream_id.componentId
            outputs = all_specs[parent].common.streams[stream_id.streamId].output_fields
            label = "Stream: {}\lFields: {}\lGrouping: {}\l".format(
                stream_id.streamId, outputs, grouping
            )
            sametail = "{}-{}".format(parent, stream_id.streamId)
            if sametail not in sametail_nodes:
                g.node(sametail, shape="point", width="0")
                g.edge(parent, sametail, label=label, dir="none")
                sametail_nodes.add(sametail)
            g.edge(sametail, spec.name, samehead=str(outputs))

    return g
Example #8
0
#!/usr/bin/env python3
"""https://graphviz.org/Gallery/directed/hello.html"""

import graphviz

g = graphviz.Digraph('G', filename='hello.gv')

g.edge('Hello', 'World')

g.view()
Example #9
0
def _make_graph(bsname, version=None, data_dir=None):
    '''
    Create a DOT graph file of the files included in a basis set
    '''

    if not graphviz_avail:
        raise RuntimeError("graphviz package is not installed")

    data_dir = api.fix_data_dir(data_dir)

    md = api._get_basis_metadata(bsname, data_dir)

    if version is None:
        version = md['latest_version']
    else:
        version = str(version)

    if version not in md['versions']:
        raise RuntimeError("Version {} of {} doesn't exist".format(version, bsname))

    gr = graphviz.Digraph(comment='Basis Set Graph: ' + bsname)

    # Read the table file
    table_path = os.path.join(data_dir, md['versions'][version]['file_relpath'])
    table_data = fileio.read_json_basis(table_path)

    table_edges = {}
    for el, entry in table_data['elements'].items():
        if entry not in table_edges:
            table_edges[entry] = []
        table_edges[entry].append(el)

    for k, v in table_edges.items():
        gr.edge(bsname, k, label=compact_elements(v))

    # Element file
    for elfile in table_edges.keys():
        element_path = os.path.join(data_dir, elfile)
        element_data = fileio.read_json_basis(element_path)

        element_edges = {}

        for el, components in element_data['elements'].items():
            components = components['components']
            components_str = '\n'.join(components)

            # skip if this element for the table basis doesn't come from this file
            if el not in table_data['elements']:
                continue
            if table_data['elements'][el] != elfile:
                continue

            if components_str not in element_edges:
                element_edges[components_str] = []
            element_edges[components_str].append(el)

        for k, v in element_edges.items():
            if len(v):
                gr.edge(elfile, k, label=compact_elements(v))

    return gr
if __name__ == '__main__':

    import sys
    fname = 'hello.bin'
    if len(sys.argv) >= 2:
        fname = sys.argv[1]
    disass, instr_eq = make_disassembler(fname)

    bbs = {}
    grp = {}
    call_targets = []
    call_triples = []
    construct_cfg(bbs, grp, 0, disass, call_targets, call_triples)

    if '--graphviz' in sys.argv:
        g1 = apply_styles(gv.Digraph(format='svg', node_attr={"shape": "box"}),
                          styles)
        for i in bbs:
            if i in call_targets:
                g1.node(hex(i),
                        label="\l".join(bbs[i]).replace("\x00", "") + "\l",
                        color='red')
            else:
                g1.node(hex(i),
                        label="\l".join(bbs[i]).replace("\x00", "") + "\l")
        for i in grp:
            for j in grp[i]:
                g1.edge(hex(i), hex(j))
        call_counts = {}
        for (sourcebb, destbb, sourceinst) in call_triples:
            g1.edge(hex(sourcebb),
Example #11
0
 def __init__(self):
     self.graph = gv.Digraph()
Example #12
0
    def draw(self, max_depth: int = None):
        """Draws the tree using the `graphviz` library.

        Parameters:
            max_depth: Only the root will be drawn when set to `0`. Every node will be drawn when
                set to `None`.

        Example:

            >>> from creme import datasets
            >>> from creme import tree

            >>> model = tree.DecisionTreeClassifier(
            ...    patience=10,
            ...    confidence=1e-5,
            ...    criterion='gini',
            ...    max_depth=10,
            ...    tie_threshold=0.05,
            ...    min_child_samples=0,
            ... )

            >>> for x, y in datasets.Phishing():
            ...    model = model.fit_one(x, y)

            >>> dot = model.draw()

        .. image:: /img/dtree_draw.svg
            :align: center

        """

        if max_depth is None:
            max_depth = math.inf

        dot = graphviz.Digraph(graph_attr={'splines': 'ortho'},
                               node_attr={
                                   'shape': 'box',
                                   'penwidth': '1.2',
                                   'fontname': 'trebuchet',
                                   'fontsize': '11',
                                   'margin': '0.1,0.0'
                               },
                               edge_attr={
                                   'penwidth': '0.6',
                                   'center': 'true'
                               })

        # Do a first pass to guess the number of classes
        n_classes = len(
            set(
                itertools.chain(*[
                    list(node.target_dist) for node, _ in self.root.iter_dfs()
                ])))

        # Pick a color palette which maps classes to colors
        new_color = functools.partial(next, iter(_color_brew(n_classes)))
        palette = collections.defaultdict(new_color)

        for parent_no, child_no, _, child, child_depth in self.root.iter_edges(
        ):

            if child_depth > max_depth:
                continue

            if isinstance(child, leaf.Branch):
                text = f'{child.split} \n {child.target_dist} \n samples: {child.n_samples}'
            elif isinstance(child, leaf.Leaf):
                text = f'{child.target_dist} \n samples: {child.n_samples}'

            # Pick a color, the hue depends on the class and the transparency on the distribution
            mode = child.target_dist.mode
            if mode is not None:
                p_mode = child.target_dist.pmf(mode)
                alpha = (p_mode - 1 / n_classes) / (1 - 1 / n_classes)
                fillcolor = str(
                    transparency_hex(color=palette[mode], alpha=alpha))
            else:
                fillcolor = '#FFFFFF'

            dot.node(f'{child_no}', text, fillcolor=fillcolor, style='filled')

            if parent_no is not None:
                dot.edge(f'{parent_no}', f'{child_no}')

        return dot
Example #13
0
 def plot(self):
     gtree = gv.Digraph(format='png')
     return self.racine.to_graph(gtree)
Example #14
0
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import streamlit as st

import graphviz as graphviz

# basic graph
hello = graphviz.Digraph("Hello World")
hello.edge("Hello", "World")

# styled graph
styled = graphviz.Graph("G", filename="g_c_n.gv")
styled.attr(bgcolor="purple:pink", label="agraph", fontcolor="white")

with styled.subgraph(name="cluster1") as c:
    c.attr(
        fillcolor="blue:cyan",
        label="acluster",
        fontcolor="white",
        style="filled",
        gradientangle="270",
    )
    c.attr("node",
Example #15
0
def main(argv):			
		with open(argv[1], encoding='utf-8') as f:
		    s = re.sub(r'\s+', ' ', f.read(), flags=re.M)
		f=re.split(r'(?<=[.!?…]) ',s)
		sentens=[]
		for i,t in enumerate(f):
		    sentens.append(t)
		    print(str(i)," ",t)




		morph = pymorphy2.MorphAnalyzer() 

		ZnakiP=[",","!","/n",".",":",";",'"',"'","\n","...","?","!","(",")","-"," ","  "]
		t = Mystem()
		PARS=[]
		for sent in sentens:
		    input_file=open("input.txt","w",encoding="utf-8")
		    input_file.write(sent)
		    input_file.close()
		    
		    # Делаем синтаксический анализ текста, находим граматические основы
		    process = subprocess.Popen('tomitaparser.exe config.proto', stdout=subprocess.PIPE,shell=True) 
		    process.communicate()
		    process.wait()
		    
		    predicate=[]
		    Nouns=[]
		    DOP=[]
		    DOP.append({})
		    OPR=[]
		    with open("pretty.html",encoding='utf8') as fp:
		            soup = BeautifulSoup(fp,"html.parser")    
		    par_f=soup.find_all('table')
		    for table in par_f:
		        th=table.find('th')    
		        if(th.text=="Noun1"):
		            slovo=th.find_parent("table").find('a').text
		            Nouns.append(slovo)
		        if(th.text=="Verb1"):
		            slovo=th.find_parent("table").find('a').text
		            predicate.append(slovo)
		        if(th.text=="OPR1"):
		            sl=th.find_parent("table").find_all('a')
		            for slovo in sl:
		                OPR.append(slovo.text)
		        if(th.text=="DOP1"):
		            sl=th.find_parent("table").find_all('a')
		            for slovo in sl:
		                DOP[0][slovo.text.lower()]=slovo.next_element.next_element.next_element.next_element
		    TREE={}
		    TREE[Nouns[0]]={} 

		    

		    for v in predicate:
		        TREE[Nouns[0]][v]={}
		    if(OPR!=[]):
		            for temp in OPR:
		                for noun in TREE:
		                    if(len(re.split(r"[,' ']",temp))==1):
		                        TREE[Nouns[0]][temp]=t.analyze(temp)[0]['analysis'][0]['gr']
		                    else:
		                            m2=[]
		                            for f in re.split(r"[,' ']",temp):
		                                if(f!=''):
		                                    m2.append(f)
		                            if(noun in m2):
		                                mk=t.analyze(temp)
		                                wsp=[]
		                                for tr in mk:
		                                    if(not tr['text'] in ZnakiP):
		                                        if(not 'CONJ' in tr['analysis'][0]['gr']):
		                                            wsp.append(tr['text'])
		                                for tl in wsp:
		                                    if(tl!=noun):
		                                        TREE[Nouns[0]][tl]=t.analyze(tl)[0]['analysis'][0]['gr']



		    for temp in TREE[Nouns[0]]:
		        if(temp in DOP[0].values()):
		            for sp in DOP[0]:
		                if(DOP[0][sp]==temp):
		                    m2=[]
		                    for f in re.split(r"[,' ']",sp):
		                        if(f!=''):
		                            m2.append(f)                         
		                    for rg in m2:                    
		                        TREE[Nouns[0]][temp][rg]={}
		                        for _opr in OPR:
		                            reg=re.split(r"[,' ']",temp)                        
		                            if(noun in reg):
		                                mk=t.analyze(_opr)
		                                wsp=[]
		                                for tr in mk:
		                                    if(not tr['text'] in ZnakiP):
		                                        if(not 'CONJ' in tr['analysis'][0]['gr']):
		                                            wsp.append(tr['text'])
		                                for tl in wsp:
		                                    if(tl!=rg):                                
		                                        TREE[Nouns[0]][temp][rg][tl]=t.analyze(tl)[0]['analysis'][0]['gr']


		  
		    
		    for noun in TREE:
		        d1=[noun]
		        for verb in TREE[noun]:
		            if(morph.parse(verb)[0].tag.POS=='ADJF'):            
		                d2=[noun,'быть']
		                d2.append(verb)
		                if(not d2 in PARS):
		                    PARS.append(d2.copy()) 
		                d2.pop()
		            else:
		                d4=[verb,"может быть"]
		                d1.append(verb)            
		                for temp in TREE[noun][verb]:            
		                            if(morph.parse(temp)[0].tag.POS=='NOUN'):
		                                d1.append(morph.parse(temp)[0].normal_form)
		                                if(not d1 in PARS):
		                                        PARS.append(d1.copy())
		                                d1.pop()
		                                d3=[temp,'быть']    
		                                
		                                for temp2 in TREE[noun][verb][temp]:
		                                        d3.append(temp2)
		                                        PARS.append(d3.copy())
		                                        d3.pop()
		                            else:
		                                d4.append(temp)
		                                if(not d4 in PARS):
		                                    PARS.append(d4.copy())
		                                d4.pop()


		    
		obj = PARS.copy()

		g1=gv.Digraph(format='png')

		for temp in obj:
		    a=morph.parse(temp[0])[0].tag.POS
		    if(a=='VERB' or a=='INFN'):
		            for t in obj:
		                if(t[1]==temp[0]):
		                    g1.node(t[0],shape='rect',style='filled',fillcolor='#cccccc')
		                    g1.node(temp[0])
		                    g1.node(temp[2],shape='rect',style='filled',fillcolor='#cccccc')
		                    g1.edge(t[0],temp[0])                    
		                    g1.edge(temp[0],temp[2],label=temp[1])
		                    g1.edge(temp[0],t[2])
		                    
		    else:
		        g1.node(temp[0],shape='rect',style='filled',fillcolor='#cccccc')
		        g1.node(temp[2],shape='rect',style='filled',fillcolor='#cccccc')
		        g1.edge(temp[0],temp[2],label=temp[1])

		print(g1.source)
		g1.render('img/'+argv[2])
Example #16
0
def visualize_ast(context,
                  comment='MOA AST',
                  with_attrs=True,
                  vector_value=True):
    if graphviz is None:
        raise ImportError(
            'The graphviz package is required to draw expressions')

    dot = graphviz.Digraph(comment=comment)
    counter = itertools.count()
    default_node_attr = dict(color='black',
                             fillcolor='white',
                             fontcolor='black')

    def _visualize_node_label(dot, context):
        unique_id = str(next(counter))

        node_label = _node_label(context)
        for key, value in node_label.items():
            node_label[key] = escape_dot_string(value)

        labels = []
        if ast.is_array(context):
            shape = 'box'
        else:  # operation
            shape = 'ellipse'

        if len(node_label) > 1:
            labels.append('<TR><TD>{}</TD></TR>'.format(node_label['name']))
            if 'shape' in node_label:
                labels.append('\n<TR><TD>{}</TD></TR>'.format(
                    node_label['shape']))
            if 'value' in node_label:
                labels.append('\n<TR><TD>{}</TD></TR>'.format(
                    node_label['value']))

            node_description = f'''<
            <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
               {''.join(labels)}
            </TABLE>>'''
        else:
            node_description = node_label['name']

        dot.node(unique_id, label=node_description, shape=shape)
        return unique_id

    def _visualize_node(dot, context):
        node_id = _visualize_node_label(dot, context)

        # no need to traverse condition node since converted to python source
        if context.ast.symbol == (ast.NodeSymbol.CONDITION, ):
            child_context = ast.select_node(context, (1, ))
            child_node_id = _visualize_node(dot, child_context)
            dot.edge(node_id, child_node_id)
            return node_id

        for i in range(ast.num_node_children(context)):
            child_context = ast.select_node(context, (i, ))
            child_node_id = _visualize_node(dot, child_context)
            dot.edge(node_id, child_node_id)

        return node_id

    _visualize_node(dot, context)
    return dot
Example #17
0
 def __init__(self, format='png'):
     self.graph = gv.Digraph(format=format)
Example #18
0
def make_graph(
    model,
    current_tags,
    existing_resources,
    existing_dependencies,
    resources_to_up,
    resources_to_down
):
    # TODO show renames
    graph = graphviz.Digraph()

    def res_color(res_name):
        if res_name in resources_to_up:
            return 'green'

        if res_name in resources_to_down:
            return 'red'

        return 'black'

    for res_name, res in sorted(model.resources.items()):
        is_existing = res_name in existing_resources
        is_dirty = is_existing and existing_resources[res_name]['dirty']
        label_suffix = ''
        if is_dirty:
            label_suffix += '*'
        graph.node(
            'res-' + res_name,
            label=res_name + label_suffix,
            color=res_color(res_name),
            style=['solid', 'bold'][is_existing],
            group=['new', 'existing'][is_existing]
        )

        dependencies = {}

        for imp_name, (dep_res_name, dep_export_name) in res.data.imports.items():
            dependencies.setdefault(dep_res_name, []).append(dep_export_name)

        for dep_res_name, imports in sorted(dependencies.items()):
            graph.edge(
                'res-' + dep_res_name,
                'res-' + res_name,
                label=",\n".join(sorted(imports))
            )

    for res_name, _ in sorted(existing_resources.items()):
        if res_name in model.resources:
            continue

        graph.node(
            'res-' + res_name,
            label=res_name,
            color=res_color(res_name),
            style='dashed',
            group='old'
        )

        for dep_res_name in sorted(existing_dependencies[res_name]):
            graph.edge(
                'res-' + dep_res_name,
                'res-' + res_name
            )

    all_tags = set()
    for res_name, tags in current_tags.items():
        for tag in tags:
            all_tags.add(tag)
            graph.edge(
                'tag-' + tag,
                'res-' + res_name,
                style='dashed',
                arrowhead='none'
            )

    with graph.subgraph(name='cluster_tags', graph_attr=dict(style='invis')) as subgraph:
        for tag in all_tags:
            subgraph.node(
                'tag-' + tag,
                label=tag,
                shape='rectangle',
                fillcolor='yellow',
                style='filled'
            )

    for seq, seq_style in (
        (resources_to_down, dict(color='red')),
        (resources_to_up, dict(color='green'))
    ):
        for i, j in zip(seq[:-1], seq[1:]):
            graph.edge(
                'res-' + i,
                'res-' + j,
                constraint='false',
                **seq_style
            )

    return graph
Example #19
0
def workflow_to_digraph(name, workflow):
    """
    Generate a `graphviz.Digraph` from a `xworkflows.Workflow`.

    :param workflow: workflows to generate the digraph from
    :type workflows: xworkflows.Workflow

    :returns: a dot digraph
    :rtype: graphviz.Digraph
    """
    sources, targets, edges = set(), set(), set()

    # dump nodes and edges
    for transition in workflow.transitions._transitions.values():
        for source in transition.source:
            source_name = '%s.%s' % (name, source)
            target_name = '%s.%s' % (name, transition.target)
            sources.add((source_name, str(source)))
            targets.add((target_name, str(transition.target)))
            edges.add((source_name, target_name, (('label',
                                                   str(transition.name)), )))

    # construct subgraph
    subgraph = graphviz.Digraph(
        name="cluster_%s" % (name, ),
        graph_attr={
            'label': "%s" % (name, ),
            'labelloc': 'top',
            'labeljust': 'left',
        },
    )

    final_states = targets - sources
    for name, label in final_states:
        subgraph.node(name, label=label, shape='doublecircle')

    for name, label in (sources | targets) - final_states:
        label = label.replace('_', '\n')  # XXX hack to avoid lenghty names

        # HACK customize some states with specific colors
        if label == 'on':
            subgraph.node(name,
                          label=label,
                          shape='doublecircle',
                          fillcolor='palegreen',
                          style='filled')
        elif label == 'error':
            subgraph.node(name,
                          label=label,
                          shape='doublecircle',
                          fillcolor='red',
                          style='filled')
        elif label == 'off':
            subgraph.node(name,
                          label=label,
                          shape='doublecircle',
                          style='filled')
        else:
            # "normal" state
            subgraph.node(name, label=label, shape='circle')

        if workflow.initial_state:  # Adding initial state notation
            if label == workflow.initial_state:
                subgraph.node('.', shape='point')
                subgraph.edge('.', name)

    for source_name, target_name, attrs in edges:
        subgraph.edge(source_name, target_name, **dict(attrs))

    return subgraph
Example #20
0
def render_graph(graph_specification, render_distributions=False):
    """
    Create a graphviz object given a graph specification.

    :param bool render_distributions: Show distribution of each RV in plot.
    """
    try:
        import graphviz  # noqa: F401
    except ImportError as e:
        raise ImportError(
            "Looks like you want to use graphviz (https://graphviz.org/) "
            "to render your model. "
            "You need to install `graphviz` to be able to use this feature. "
            "It can be installed with `pip install graphviz`."
        ) from e

    plate_groups = graph_specification["plate_groups"]
    plate_data = graph_specification["plate_data"]
    node_data = graph_specification["node_data"]
    edge_list = graph_specification["edge_list"]

    graph = graphviz.Digraph()

    # add plates
    plate_graph_dict = {
        plate: graphviz.Digraph(name=f"cluster_{plate}")
        for plate in plate_groups
        if plate is not None
    }
    for plate, plate_graph in plate_graph_dict.items():
        plate_graph.attr(label=plate.split("__CLONE")[0], labeljust="r", labelloc="b")

    plate_graph_dict[None] = graph

    # add nodes
    for plate, rv_list in plate_groups.items():
        cur_graph = plate_graph_dict[plate]

        for rv in rv_list:
            color = "grey" if node_data[rv]["is_observed"] else "white"
            cur_graph.node(
                rv, label=rv, shape="ellipse", style="filled", fillcolor=color
            )

    # add leaf nodes first
    while len(plate_data) >= 1:
        for plate, data in plate_data.items():
            parent_plate = data["parent"]
            is_leaf = True

            for plate2, data2 in plate_data.items():
                if plate == data2["parent"]:
                    is_leaf = False
                    break

            if is_leaf:
                plate_graph_dict[parent_plate].subgraph(plate_graph_dict[plate])
                plate_data.pop(plate)
                break

    # add edges
    for source, target in edge_list:
        graph.edge(source, target)

    # render distributions if requested
    if render_distributions:
        dist_label = ""
        for rv, data in node_data.items():
            rv_dist = data["distribution"]
            dist_label += rf"{rv} ~ {rv_dist}\l"

        graph.node("distribution_description_node", label=dist_label, shape="plaintext")

    # return whole graph
    return graph
Example #21
0
def build_datastructure_doc(is_nt):
    pd.set_option('display.max_colwidth', int(1e9))

    st = get_context(is_nt)
    xT = 'nT' if is_nt else '1T'
    # Too lazy to write proper graph sorter
    # Make dictionary {total number of dependencies below -> list of plugins}

    plugins_by_deps = get_plugins_deps(st)

    # Make graph for each suffix ('' referring to TPC)
    for suffix in tree_suffices:
        title = titles[suffix].format(xT=xT)
        out = page_header.format(
            title=title, context='xenonnt_online' if is_nt else 'xenon1t_dali')
        if not is_nt and suffix != '':
            # No NV/MV/HE for 1T
            continue

        print(f'------------ {xT}{suffix} ------------')
        os.makedirs(this_dir + f'/graphs{suffix}_{xT}', exist_ok=True)
        for n_deps in list(
                reversed(sorted(list(plugins_by_deps[suffix].keys())))):
            for data_type in plugins_by_deps[suffix][n_deps]:
                plugins = st._get_plugins((data_type, ), run_id='0')

                # Create dependency graph
                g = graphviz.Digraph(format='svg')
                # g.attr('graph', autosize='false', size="25.7,8.3!")
                for d, p in plugins.items():
                    if skip(p, d, suffix, data_type):
                        continue
                    g.node(d,
                           style='filled',
                           href='#' + d.replace('_', '-'),
                           fillcolor=kind_colors.get(p.data_kind_for(d),
                                                     'grey'))
                    for dep in p.depends_on:
                        g.edge(d, dep)

                fn = this_dir + f'/graphs{suffix}_{xT}/' + data_type
                g.render(fn)
                with open(f'{fn}.svg', mode='r') as f:
                    svg = add_spaces(f.readlines()[5:])

                config_df = st.show_config(data_type).sort_values(by='option')
                # Filter out the config options of lower level datatypes
                config_mask = []
                for ap_to in config_df['applies_to'].values:
                    config_mask.append(any([data_type in a for a in ap_to]))
                keep_cols = ['option', 'default', 'current', 'help']
                config_df = config_df[config_mask][keep_cols]

                # Shorten long default values
                config_df['default'] = [
                    x[:10] + '...' +
                    x[-10:] if isinstance(x, str) and len(x) > 30 else x
                    for x in config_df['default'].values
                ]

                p = plugins[data_type]

                out += template.format(
                    p=p,
                    context='',
                    module=str(p.__module__).replace('.', '/'),
                    svg=svg,
                    data_type=data_type,
                    columns=add_spaces(
                        st.data_info(data_type).to_html(index=False)),
                    kind=p.data_kind_for(data_type),
                    docstring=p.__doc__
                    if p.__doc__ else '(no plugin description)',
                    config_options=add_spaces(config_df.to_html(index=False)))

        with open(this_dir + f'/reference/datastructure{suffix}_{xT}.rst',
                  mode='w') as f:
            f.write(out)

        shutil.rmtree(this_dir + f'/graphs{suffix}_{xT}')
Example #22
0
 def _make_graphviz(self):
     d = graphviz.Digraph()
     d.attr(labelloc='t', label=str(self))
     self._add_nodes(d)
     return d
Example #23
0
def draw_graph(outputs,
               draw_hyperparameters=True,
               draw_io_labels=True,
               draw_module_hyperparameter_info=True,
               out_folderpath=None,
               graph_name='graph',
               print_to_screen=True):
    """Draws a graph representation of the current state of the search space.

    All edges are directed. An edge between two modules represents the output of
    the first module going into the input of the second module. An edge between
    a module and an hyperparameter represents the dependency of that module on
    that hyperparameter.

    This visualization functionality is useful to verify that the description of
    the search space done through the domain-specific language encodes the
    intended search space.

    .. note::
        All drawing options are set to `True` to give a sense of all the
        information that can be displayed simultaneously. This can lead to
        cluttered graphs and slow rendering. We recommend changing the defaults
        according to the desired behavior.

        For generating a representation for a fully specified model,
        we recommend setting `draw_hyperparameters` to `False`, as if we are
        only concerned with the resulting model, the sharing structure of
        hyperparameters does not really matter. We recommend using
        `draw_hyperparameters` set to `True` when the user wishes to visualize
        the hyperparameter sharing pattern, e.g., to verify that it has been
        implemented correctly.

    Args:
        outputs (dict[str, deep_architect.core.Output]): Dictionary of named
            outputs from which we can reach all the modules in the search space
            by backwards traversal.
        draw_hyperparameters (bool, optional): Draw hyperparameter nodes in the
            graph, representing the dependencies between hyperparameters and
            modules.
        draw_io_labels (bool, optional): If `True`,
            draw edge labels connecting different modules
            with the local names of the input and output that are being connected.
        draw_module_hyperparameter_info (bool, optional): If `True`,
            draw the hyperparameters of a module alongside the module.
        graph_name (str, optional): Name of the file used to store the
            rendered graph. Only needs to be provided if we desire to output
            the graph to a file, rather than just show it.
        out_folderpath (str, optional): Folder to which to store the PDF file with
            the rendered graph. If no path is provided, no file is created.
        print_to_screen (bool, optional): Shows the result of rendering the
            graph directly to screen.
    """
    assert print_to_screen or out_folderpath is not None

    g = graphviz.Digraph()
    edge_fs = '10'
    h_fs = '10'
    penwidth = '1'

    def _draw_connected_input(ix_localname, ix):
        ox = ix.get_connected_output()
        if not draw_io_labels:
            label = ''
        else:
            ox_localname = None
            for ox_iter_localname, ox_iter in ox.get_module().outputs.items():
                if ox_iter == ox:
                    ox_localname = ox_iter_localname
                    break
            assert ox_localname is not None
            label = ix_localname + ':' + ox_localname

        g.edge(ox.get_module().get_name(),
               ix.get_module().get_name(),
               label=label,
               fontsize=edge_fs)

    def _draw_unconnected_input(ix_localname, ix):
        g.node(ix.get_name(),
               shape='invhouse',
               penwidth=penwidth,
               fillcolor='firebrick',
               style='filled')
        g.edge(ix.get_name(), ix.get_module().get_name())

    def _draw_module_hyperparameter(m, h_localname, h):
        if h.has_value_assigned():
            label = h_localname + '=' + str(h.get_value())
        else:
            label = h_localname

        g.edge(h.get_name(), m.get_name(), label=label, fontsize=edge_fs)

    def _draw_dependent_hyperparameter_relations(h_dep):
        for h_localname, h in h_dep._hyperps.items():
            if h.has_value_assigned():
                label = h_localname + '=' + str(h.get_value())
            else:
                label = h_localname

            g.edge(h.get_name(),
                   h_dep.get_name(),
                   label=label,
                   fontsize=edge_fs)

    def _draw_module_hyperparameter_info(m):
        g.node(
            m.get_name(),
            xlabel="<" + '<br align="right"/>'.join([
                '<FONT POINT-SIZE="%s">' % h_fs + h_localname +
                ('=' + str(h.get_value()) if h.has_value_assigned() else '') +
                "</FONT>" for h_localname, h in m.hyperps.items()
            ]) + ">")

    def _draw_output_terminal(ox_localname, ox):
        g.node(ox.get_name(),
               shape='house',
               penwidth=penwidth,
               fillcolor='deepskyblue',
               style='filled')
        g.edge(ox.get_module().get_name(), ox.get_name())

    nodes = set()

    def fn(m):
        """Adds the module information to the graph that is local to the module.
        """
        nodes.add(m.get_name())
        for ix_localname, ix in m.inputs.items():
            if ix.is_connected():
                _draw_connected_input(ix_localname, ix)
            else:
                _draw_unconnected_input(ix_localname, ix)

        if draw_hyperparameters:
            for h_localname, h in m.hyperps.items():
                _draw_module_hyperparameter(m, h_localname, h)

        if draw_module_hyperparameter_info:
            _draw_module_hyperparameter_info(m)
        return False

    # generate most of the graph.
    co.traverse_backward(outputs, fn)

    # drawing the hyperparameter graph.
    if draw_hyperparameters:
        hs = co.get_all_hyperparameters(outputs)

        for h in hs:
            if isinstance(h, co.DependentHyperparameter):
                _draw_dependent_hyperparameter_relations(h)

            g.node(h.get_name(),
                   fontsize=h_fs,
                   fillcolor='darkseagreen',
                   style='filled')

    # add the output terminals.
    for m in co.extract_unique_modules(list(outputs.values())):
        for ox_localname, ox in m.outputs.items():
            _draw_output_terminal(ox_localname, ox)

    # minor adjustments to attributes.
    for s in nodes:
        g.node(s,
               shape='rectangle',
               penwidth=penwidth,
               fillcolor='goldenrod',
               style='filled')

    if print_to_screen or out_folderpath is not None:
        g.render(graph_name,
                 out_folderpath,
                 view=print_to_screen,
                 cleanup=True)
Example #24
0
def make_sitemap_graph(df,
                       layers=graph_depth,
                       limit=limit,
                       size=size,
                       output_format=output_format,
                       skip=skip):
    ''' Make a sitemap graph up to a specified layer depth.

    sitemap_layers : DataFrame
        The dataframe created by the peel_layers function
        containing sitemap information.

    layers : int
        Maximum depth to plot.

    limit : int
        The maximum number node edge connections. Good to set this
        low for visualizing deep into site maps.
    
    output_format : string
        The type of file you want to save in PDF, PNG, TIFF, JPG

    skip : list
        List of branches that you do not want to expand.
    '''

    # Check to make sure we are not trying to plot too many layers
    if layers > len(df) - 1:
        layers = len(df) - 1
        print('There are only %d layers available to plot, setting layers=%d' %
              (layers, layers))

    # Initialize graph
    f = graphviz.Digraph('sitemap',
                         filename='sitemap_graph_%d_layer' % layers,
                         format='%s' % output_format)
    f.body.extend(['rankdir=LR', 'size="%s"' % size])

    def add_branch(f, names, vals, limit, connect_to=''):
        ''' Adds a set of nodes and edges to nodes on the previous layer. '''

        # Get the currently existing node names
        node_names = [item.split('"')[1] for item in f.body if 'label' in item]

        # Only add a new branch it it will connect to a previously created node
        if connect_to:
            if connect_to in node_names:
                for name, val in list(zip(names, vals))[:limit]:
                    f.node(name='%s-%s' % (connect_to, name), label=name)
                    f.edge(connect_to,
                           '%s-%s' % (connect_to, name),
                           label='{:,}'.format(val))

    f.attr('node', shape='rectangle')  # Plot nodes as rectangles

    # Add the first layer of nodes
    for name, counts in df.groupby(['0'])['counts'].sum().reset_index()\
                          .sort_values(['counts'], ascending=False).values:
        f.node(name=name, label='{} ({:,})'.format(name, counts))

    if layers == 0:
        return f

    f.attr('node', shape='oval')  # Plot nodes as ovals
    f.graph_attr.update()

    # Loop over each layer adding nodes and edges to prior nodes
    for i in range(1, layers + 1):
        cols = [str(i_) for i_ in range(i)]
        nodes = df[cols].drop_duplicates().values
        for j, k in enumerate(nodes):

            # Compute the mask to select correct data
            mask = True
            for j_, ki in enumerate(k):
                mask &= df[str(j_)] == ki

            # Select the data then count branch size, sort, and truncate
            data = df[mask].groupby([str(i)])['counts'].sum()\
                    .reset_index().sort_values(['counts'], ascending=False)

            # Add to the graph unless specified that we do not want to expand k-1
            if (not skip) or (k[-1] not in skip):
                add_branch(f,
                           names=data[str(i)].values,
                           vals=data['counts'].values,
                           limit=limit,
                           connect_to='-'.join(['%s'] * i) % tuple(k))

            print(('Built graph up to node %d / %d in layer %d' % (j, len(nodes), i))\
                    .ljust(50), end='\r')

    return f
Example #25
0
df=df.sort_values(by='R_num', ascending=True)
df=df.reset_index()
df=df.drop('index',axis=1)
df['level']=nan  # using nan to keep all value float


for num,nam in enumerate(df.name):
    df.loc[num,'level']=next((df.loc[i].R_num for i, val in enumerate(df.ORG) if (val == nam and i>num and (df.loc[i].R_num not in list(df.level)))), nan)
df.level=df.level.map('{:.0f}'.format)  # int format

df.loc[df['level']=='nan','level'] = df[df['level']=='nan']['R_num']  # replace nan with R_num to distinct feeds

print('GRAPH')################## GRAPH #################
os.environ["PATH"] += os.pathsep + r'C:\Users\e650188\AppData\Local\graphviz238\bin'

dot = gv.Digraph(node_attr={'shape': 'box'},edge_attr={'dir':'forward'},graph_attr={'rankdir':'RL'})

for num in range(df.shape[0]):
    dot.edge(df.iloc[num]['ORG']+'  '+str(df.iloc[num]['R_num']),df.iloc[num]['name']+'  '+str(df.iloc[num]['level']))

dot.render(r'C:\Users\e650188\Desktop\automation_files\test-output\{}_{}.gv'.format(mart_name,(str(datetime.datetime.now())[:-7]).replace(":","-")), view=True)

print('DM formula')################### DM formula #################
dfcol_param=['DM_Name','Parameter','Default_Value'] # if this list changes often, will need a dict
df_param=pd.DataFrame(columns=dfcol_param)

dfcol_element = ['DM_Name','Element','status','alias','key','Formula','displayName']  # if this list changes often, will need a dict
df_element=pd.DataFrame(columns=dfcol_element)

dfcol_join = ['DM_Name','alias','Source_Element','Target_Element','Main_Table(Target)']
df_join=pd.DataFrame(columns=dfcol_join)
Example #26
0
class Arbre:
    # @classmethod
    # def sortie(cls, noeud, nom_fichier, format):

    #     cls._graphe = graphviz.Digraph()
    #     for k in Arbre._options_graphe:
    #         cls._graphe.attr(k, **Arbre._options_graphe[k])
    #     #
    #     cls._visunoeud(noeud)
    #     cls._graphe.render(nom_fichier, format=format)

    # @classmethod

    @classmethod
    def options(cls, classe, dictionnaire):
        cls._options_graphe[classe].update(dictionnaire)

    etiquette = "valeur"
    _graphe = graphviz.Digraph()
    _options_graphe = {
        "node": {
            "shape": "record"
        },
        "edge": {
            "headport": "n",
            "tailclip": "false",
            "arrowsize": ".5",
            "arrowhead": "vee"
        },
        "graph": {
            "engine": "dot",
            "splines": "false"
        }
    }

    def __init__(self, racine=None):
        self.racine = racine
        self._fonction_ordre = lambda x, y: x.valeur < y.valeur

    def __str__(self):
        return str(self.racine)

    def __len__(self):
        return len(self.infixe)

    def est_vide(self):
        return self.racine is None

    @property
    def fonction_ordre(self):
        return inspect.getsource(self._fonction_ordre)

    @fonction_ordre.setter
    def fonction_ordre(self, fonction):
        self._fonction_ordre = fonction

    @property
    def prefixe(self):
        return self._parcours_prof("prefixe")

    @property
    def infixe(self):
        return self._parcours_prof("infixe")

    @property
    def suffixe(self):
        return self._parcours_prof("suffixe")

    @property
    def largeur(self):
        return self._parcours_largeur()

    def liste_aplatie(self, noeud=None):
        return self._parcours_largeur_numerote(noeud)

    def inserer(self, noeud, noeud_courant=None):
        if self.racine is None:
            self.racine = noeud
            return noeud

        # racine de l'arbre
        if noeud_courant is None:
            noeud_courant = self.racine

        if self._fonction_ordre(noeud, noeud_courant):
            if noeud_courant.gauche is None:
                noeud_courant.gauche = noeud
                noeud.parent = noeud_courant
                return noeud
            else:
                return self.inserer(noeud, noeud_courant.gauche)
        else:
            if noeud_courant.droit is None:
                noeud_courant.droit = noeud
                noeud.parent = noeud_courant
                return noeud
            else:
                return self.inserer(noeud, noeud_courant.droit)

    def maximum(self, noeud):
        # renvoie le maximum et le noeud
        courant = noeud
        while courant.droit is not None:
            courant = courant.droit
        return courant, courant.valeur

    def supprimer(self, noeud, noeud_courant=None, orientation=None):
        """Se placer au niveau du parent pour pouvoir pointer vers None, en
        effet supprimer un objet par lui-même semble impossible en
        Python !

        Donc on sait que l'élément cherché n'est pas celui actuel
        mais peut-être un des deux descendants.

        Néanmoins en ajoutant un attribut parent à chaque noeud, il
        est aisé de remonter au parent.

        orientation permet de savoir si on est à gauche ou à droite
        du parent pour supprimer le bon lien

        """

        if noeud_courant is None:
            noeud_courant = self.racine
        #
        if noeud.valeur == noeud_courant.valeur:
            # pas de descendant
            if noeud_courant.gauche is None and noeud_courant.droit is None:
                # on supprime le lien qui le lie à son parent
                # le garbage collector prend la suite
                if orientation == 'g':
                    # print(noeud_courant.parent.valeur)
                    noeud_courant.parent.gauche = None
                else:
                    noeud_courant.parent.droit = None
            # un seul à droite
            elif noeud_courant.gauche is None:
                if orientation == 'g':
                    noeud_courant.parent.gauche = noeud_courant.droit
                else:
                    noeud_courant.parent.droit = noeud_courant.droit
                noeud_courant.droit.parent = noeud_courant.parent
            # un seul à gauche
            elif noeud_courant.droit is None:
                if orientation == 'g':
                    noeud_courant.parent.gauche = noeud_courant.gauche
                else:
                    noeud_courant.parent.droit = noeud_courant.gauche
                noeud_courant.gauche.parent = noeud_courant.parent
            # deux ! on choisit le plus grand (le plus à
            # droite) du sous-arbre gauche qu'on devra donc
            # supprimer
            else:
                n, M = self.maximum(noeud_courant.gauche)
                # destruction du lien de droite du parent (le noeud
                # max vient de la droite !)
                # n.parent.droit = None
                self.supprimer(n, noeud_courant.gauche, "g")
                noeud_courant.valeur = M
                #
        elif noeud.valeur < noeud_courant.valeur:
            self.supprimer(noeud, noeud_courant.gauche, 'g')
        else:
            self.supprimer(noeud, noeud_courant.droit, 'd')

    def rechercher(self, element_a_chercher, type_element="valeur"):
        # Besoin d'un noeud pour la fonction de comparaison
        if type_element == "valeur":
            noeud_factice = Noeud(element_a_chercher)
        elif type_element == "contenu":
            noeud_factice = Noeud(-1, contenu=element_a_chercher)
        #
        courant = self.racine
        #
        # recherche simple
        if type_element == "valeur":
            while courant.valeur != element_a_chercher:
                if self._fonction_ordre(noeud_factice, courant):
                    courant = courant.gauche
                else:
                    courant = courant.droit
        elif type_element == "contenu":
            # parcours préfixe
            liste = self.prefixe
            courant = liste.pop(0)
            while courant.contenu != element_a_chercher:
                courant = liste.pop(0)
        return courant

    def chemin_vers(self, noeud):
        courant = self.racine
        liste_noeuds = [courant]

        while courant != noeud:
            if self._fonction_ordre(noeud, courant):
                courant = courant.gauche
            else:
                courant = courant.droit
            liste_noeuds.append(courant)

        return liste_noeuds

    def sortie(self, noeud, nom_fichier, format, style="compact"):
        graphe = graphviz.Digraph()
        for k in Arbre._options_graphe:
            graphe.attr(k, **Arbre._options_graphe[k])
        #
        if style == "compact":
            self._visunoeud(noeud, graphe)
        elif style == "complet":
            self._visunoeud2(noeud, graphe)
        graphe.render(nom_fichier, format=format)

    def _visunoeud(self, noeud, graphe):
        identifiant = noeud.valeur
        contenu = eval(f"noeud.{Arbre.etiquette}")
        if noeud.gauche is not None:
            identifiantg = noeud.gauche.valeur
        else:
            identifiantg = None
        if noeud.droit is not None:
            identifiantd = noeud.droit.valeur
        else:
            identifiantd = None

        with graphe.subgraph(name=f"sub_{identifiant}") as gsub:
            gsub.node(str(identifiant),
                      label="{" + str(contenu) + "|{<g>|<d>}}")
            if identifiantg is None:
                gsub.node(str(identifiant) + "invisg",
                          label="",
                          width=".1",
                          style="invis")
                gsub.edge(str(identifiant) + ":g:c",
                          str(identifiant) + "invisg",
                          style="invis")
            else:
                gsub.edge(str(identifiant) + ":g:c", str(identifiantg))

            if identifiantd is None:
                gsub.node(str(identifiant) + "invisd",
                          label="",
                          width=".1",
                          style="invis")
                gsub.edge(str(identifiant) + ":d:c",
                          str(identifiant) + "invisd",
                          style="invis")
            else:
                gsub.edge(str(identifiant) + ":d:c", str(identifiantd))

            if noeud.gauche is not None:
                self._visunoeud(noeud.gauche, gsub)
            if noeud.droit is not None:
                self._visunoeud(noeud.droit, gsub)

    def _visunoeud2(self, noeud, graphe):
        compteur = 1
        liste = self.liste_aplatie(noeud)
        liste += [None
                  ] * (2**(int(math.log2(len(liste))) + 1) - 1 - len(liste))

        for i in range(self.hauteur(noeud)):
            # sub (ou autre) plutôt que cluster qui contraint à
            # l'intérieur du rectangle
            with graphe.subgraph(name=f"sub_{i}") as gsub:
                gsub.attr(rankdir="LR")
                # hack pourri parce que sinon les boeuds ne sont
                # pas dans le bon ordre sur une même ligne
                for indice in range(2**(i + 1) - 2, 2**i - 2, -1):
                    noeud = liste[indice]
                    compteur_du_parent = int((indice - 1) // 2)
                    identifiant = str(indice)
                    contenu = " " if not noeud \
                        else eval(f"noeud.{Arbre.etiquette}")
                    style = "invis" if not noeud else "solid"
                    # contenu, style))
                    gsub.node(identifiant,
                              label="{" + str(contenu) + "|{<g>|<d>}}",
                              style=style)
                    #
                    provenance = ":g:c" if compteur % 2 else ":d:c"
                    identifiant_parent = str(compteur_du_parent)
                    if compteur != 1:
                        graphe.edge(identifiant_parent + provenance,
                                    identifiant,
                                    style=style)  # , weight=str(2**i))
                    compteur += 1
        #
        # ci-dessous première version
        # mais les noeuds sont dans l'ordre contraire !!!
        #
        # for i in range(self.hauteur()):
        #     with graphe.subgraph(name=f"sub_{i}") as gsub:
        #         gsub.attr(rankdir="RL")
        #         while compteur <= 2 ** (i + 1) - 1:
        #             noeud = liste[compteur - 1]
        #             compteur_du_parent = int(compteur // 2)
        #             if noeud is not None:
        #                 # valeur = noeud.valeur
        #                 contenu = eval(f"noeud.{Arbre.etiquette}")
        #                 gsub.node(str(compteur),
        #                           label="{" + str(contenu) + "|{<g>|<d>}}")
        #                 if compteur % 2 == 0:
        #                     gsub.edge(str(compteur_du_parent) + ":d:c",
        #                               str(compteur))  # , weight=str(2**i-1))
        #                 elif compteur != 1:
        #                     gsub.edge(str(compteur_du_parent) + ":g:c",
        #                               str(compteur))  # , weight=str(2**i-1))
        #             else:
        #                 gsub.node(str(compteur),
        #                           label="{ |{<g>|<d>}}",
        #                           style="invis")
        #                 if compteur % 2 == 0:
        #                     gsub.edge(str(compteur_du_parent) + ":d:c",
        #                               str(compteur),
        #                               style="invis")  # , weight=str(2**i-1))
        #                 elif compteur != 1:
        #                     gsub.edge(str(compteur_du_parent) + ":g:c",
        #                               str(compteur),
        #                               style="invis")  # , weight=str(2**i-1))
        #             compteur += 1

    def hauteur(self, noeud=None):
        if noeud is None:
            noeud = self.racine
        return self._hauteur_rec(noeud, 0)

    def _hauteur_rec(self, noeud, h):
        if noeud.gauche is None and noeud.droit is None:
            return h + 1
        g = h if noeud.gauche is None else self._hauteur_rec(
            noeud.gauche, h + 1)
        d = h if noeud.droit is None else self._hauteur_rec(noeud.droit, h + 1)
        return max(g, d)

    def _parcours_prof(self, type_parcours, noeud=None):
        if noeud is None:
            noeud = self.racine
        return self._parcours_prof_rec(type_parcours, noeud, [])

    def _parcours_prof_rec(self, type_parcours, noeud_courant=None, liste=[]):
        #
        if type_parcours == "prefixe":
            liste.append(noeud_courant)
        if noeud_courant.gauche is not None:
            self._parcours_prof_rec(type_parcours, noeud_courant.gauche, liste)
        if type_parcours == "infixe":
            liste.append(noeud_courant)
        if noeud_courant.droit is not None:
            self._parcours_prof_rec(type_parcours, noeud_courant.droit, liste)
        if type_parcours == "suffixe":
            liste.append(noeud_courant)
        return liste

    def _parcours_largeur(self, noeud=None):
        liste = []
        f = File()
        if noeud is None:
            noeud = self.racine
        f.enfiler(noeud)
        while not f.est_vide:
            n = f.defiler()
            liste.append(n)
            if n.gauche is not None:
                f.enfiler(n.gauche)
            if n.droit is not None:
                f.enfiler(n.droit)
        return liste

    def _parcours_largeur_numerote(self, noeud=None):
        liste = []
        f = File()
        if not noeud:
            noeud = self.racine
        #
        f.enfiler((noeud, 1))
        while not f.est_vide:
            n, numero = f.defiler()
            # on comble le vide depuis la dernière insertion
            liste += [None] * (numero - len(liste) - 1)
            liste.append(n)
            if n.gauche is not None:
                f.enfiler((n.gauche, 2 * numero))
            if n.droit is not None:
                f.enfiler((n.droit, 2 * numero + 1))
        return liste

    def rotation_droite(self, noeud):
        changement_racine = noeud == self.racine
        # processus à partir de :
        # https://upload.wikimedia.org/wikipedia/commons/1/15/Tree_Rotations.gif
        pivot = noeud.gauche
        if pivot.droit is not None:
            noeud.gauche = pivot.droit
            pivot.droit.parent = noeud
        else:
            noeud.gauche = None
        pivot.droit = noeud
        # on agit aussi sur le parent de noeud
        if noeud.parent is not None and noeud.parent.gauche == noeud:
            noeud.parent.gauche = pivot
        elif noeud.parent is not None and noeud.parent.droit == noeud:
            noeud.parent.droit = pivot
        #
        pivot.parent = noeud.parent
        noeud.parent = pivot
        #
        if changement_racine:
            self.racine = pivot

    def rotation_gauche(self, noeud):
        changement_racine = noeud == self.racine
        # processus à partir de :
        # https://upload.wikimedia.org/wikipedia/commons/1/15/Tree_Rotations.gif
        pivot = noeud.droit
        if pivot.gauche is not None:
            pivot.gauche.parent = noeud
            noeud.droit = pivot.gauche
        else:
            noeud.droit = None
        pivot.gauche = noeud
        # on agit aussi sur le parent de noeud
        if noeud.parent is not None and noeud.parent.gauche == noeud:
            noeud.parent.gauche = pivot
        elif noeud.parent is not None and noeud.parent.droit == noeud:
            noeud.parent.droit = pivot
        #
        pivot.parent = noeud.parent
        noeud.parent = pivot
        #
        if changement_racine:
            self.racine = pivot
# Dependencies
import numpy as np
import io
from mydict import dictionary
from Tree_Node import Node, BinaryTree
import random
import pandas as pd
from pprint import pprint
import pydot
import os
# from PIL import Image
import graphviz
from graphviz import Source
Tree_plot = graphviz.Digraph('Tree', format='png')

G = pydot.Dot(graph_type="digraph")


# first, this function is used to make sure that the data after splits will be pure to continue with the next step in
# the decision treee algorithm
def CheckPurity(data):
    label_column = data[:, -1]
    unique_classific = np.unique(label_column)
    if len(unique_classific) == 1:
        return True
    else:
        return False


#this function is used to work on data classification where it classify data base on
# majority class
Example #28
0
def type_graph(simplify=False):
    """
    Render graph of current type system.
    """
    graph = graphviz.Digraph(format='svg')
    graph.node(
        u'\u03b5',
        shape='circle',
        style='dotted',
    )

    edge_pairs = set()

    def add_edge(*pair):
        if pair in edge_pairs:
            return
        graph.edge(*pair)
        edge_pairs.add(pair)

    simplified_graph = defaultdict(set)
    for t in REGISTERED_TYPES:
        targeting_functions = lookup_rtype(t, convert=False)
        pretty_t = prettify_converted_type(t)
        for targeting_function in targeting_functions:

            params = targeting_function.readable_param_list

            if not params:
                add_edge(u'\u03b5', pretty_t)
                continue

            for param in params:
                simplified_graph[param].add(pretty_t)

            if simplify:
                # pretend these can go directly to the return type
                for param in params:
                    add_edge(param, pretty_t)
                continue

            elif len(list(params)) > 1:
                # show composition of constructed type from constituents
                graph.node(
                    targeting_function.readable_params,
                    shape='rect',
                    style='dashed',
                )
                for param in params:
                    add_edge(param, targeting_function.readable_params)

            add_edge(targeting_function.readable_params, pretty_t)

    end_states = {
        t
        for t in map(prettify_converted_type, REGISTERED_TYPES)
        if not simplified_graph[t] or simplified_graph[t] == {t}
    }
    for end_state in end_states:
        graph.node(
            end_state,
            peripheries='2',
        )

    return graph
Example #29
0
def trt_network_to_dot_graph(network):
    dot = graphviz.Digraph(comment='Network')

    # add nodes (layers)
    for i in range(network.num_layers):
        layer = network.get_layer(i)
        dot.node(layer.name)

    # add nodes (inputs)
    for i in range(network.num_inputs):
        dot.node(network.get_input(i).name)

    # add nodes (outputs)
    for i in range(network.num_outputs):
        dot.node(network.get_output(i).name)

    # add layer->layer edges
    for a in range(network.num_layers):
        layer_a = network.get_layer(a)

        for b in range(network.num_layers):
            layer_b = network.get_layer(b)

            for i in range(layer_a.num_outputs):
                output_i = layer_a.get_output(i)

                for j in range(layer_b.num_inputs):
                    input_j = layer_b.get_input(j)

                    if output_i == input_j:
                        dot.edge(layer_a.name,
                                 layer_b.name,
                                 label=str(input_j.shape))

    # add input->layer edges
    for i in range(network.num_inputs):
        input_i = network.get_input(i)

        for b in range(network.num_layers):
            layer_b = network.get_layer(b)

            for j in range(layer_b.num_inputs):
                input_j = layer_b.get_input(j)

                if input_i == input_j:
                    dot.edge(input_i.name,
                             layer_b.name,
                             label=str(input_j.shape))

    # add layer->output edges
    for i in range(network.num_outputs):
        input_i = network.get_output(i)

        for b in range(network.num_layers):
            layer_b = network.get_layer(b)

            for j in range(layer_b.num_outputs):
                input_j = layer_b.get_output(j)

                if input_i == input_j:
                    dot.edge(layer_b.name,
                             input_i.name,
                             label=str(input_j.shape))

    return dot
Example #30
0
    def draw(self):

        import graphviz

        G = graphviz.Digraph()

        drawn_subclusters = set()

        def draw_node(a):
            if isinstance(a, Network):
                for part in a:
                    draw_node(part)
            else:
                G.node(a)

        for a in self:
            draw_node(a)

        def draw_link(a, b):

            if isinstance(a, Network):

                # Connect the last part of a with b
                if a.directed:
                    draw_link(a[-1], b)
                # Connect each part of a with b
                else:
                    for part in a:
                        draw_link(part, b)

            elif isinstance(b, Network):

                # Connect the first part of b with a
                if b.directed:

                    if str(b) not in drawn_subclusters:

                        sub = b.draw()

                        # If the graph has a name, then we treat is as a cluster
                        if b.name is not None:
                            sub.attr(label=b.name, labelloc=b.labelloc)
                            sub.name = f"cluster_{b.name}"

                        G.subgraph(sub)

                        drawn_subclusters.add(str(b))

                    draw_link(a, b[0])

                # Connect each part of b with a
                else:
                    for part in b:
                        draw_link(a, part)

            else:
                G.edge(a, b)

        for a, b in self.edges:
            draw_link(self[a], self[b])

        return G