<jupyter_start><jupyter_text>Graph of ChainIn this notebook, we construct a graph of a chain of models.  Preliminaries<jupyter_code>import os
import sys

from os.path import dirname

note_dir = os.getcwd()

root_dir = dirname(note_dir)
data_dir = os.path.join(root_dir, 'resc', 'data', 'tidy','nltcs')
src_dir = os.path.join(root_dir, "src")

sys.path.append(src_dir)

import morpheus
from morpheus.tests import default_chain
from morpheus.graph import (add_positions,
                            model_to_graph,
                            model_graph_traces,
                            model_graph_layout, 
                            convert_positions_to_dot_format)
import plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)<jupyter_output><empty_output><jupyter_text>Build Chain<jupyter_code>chain = default_chain()
chain.pretty_print()<jupyter_output># Main
        Descriptive attributes:     [0. 1. 2.]
        Target attributes:          [4. 7.]

        ## On types (mostly nominals)
        Target attribute types:                     ['numeric', 'nominal']
        N_classes of nominal target attributes:     [2]
        Classes of nominal target attributes:       [array([0., 1.])]

        ## Weights
        Total weights of target attributes:         [1. 1.]

         
        ## Estimators
        Estimators used:        ['RandomForestRegressor', 'RandomForestClassifier']
        N_estimators:           2<jupyter_text>Build Graph<jupyter_code>model_one = chain.estimators_[0]
model_two = chain.estimators_[1]
G_one = model_to_graph(model_one, idx=1)
add_positions(G_one)

G_two = model_to_graph(model_two, idx=2)
add_positions(G_two)
fig = go.Figure(data=list(model_graph_traces(G_one)),
                layout=model_graph_layout(title="Model O"))

iplot(fig)
fig = go.Figure(data=list(model_graph_traces(G_two)),
                layout=model_graph_layout(title="Model Two"))

iplot(fig)<jupyter_output><empty_output><jupyter_text>Build graph from chainNow,we try to combine.<jupyter_code>G_one.nodes()
for node in G_one.nodes():
    G_one.nodes()[node]["shape"] = "circle"
G_two.nodes()
import networkx as nx
G = nx.compose(G_one, G_two)
G.nodes()
fig = go.Figure(data=list(model_graph_traces(G)),
                layout=model_graph_layout(title="Composition"))

iplot(fig)
G_one = model_to_graph(model_one, idx=1)

for node in G_one.nodes():
    G_one.nodes()[node]["shape"] = "\"{}\"".format("square")

G_two = model_to_graph(model_two, idx=2)

G = nx.compose(G_one, G_two)

from networkx.drawing.nx_pydot import write_dot, to_pydot
#G = convert_positions_to_dot_format(G)
write_dot(G, 'multi.dot')
X = to_pydot(G)
X.set('rankdir', 'LR')
X.to_string()
with open('multi.dot') as f:
    
!dot -T png multi.dot > multi.png  # Bash command
from IPython.display import Image, display
from IPython.core.display import HTML 
display(Image('multi.png', unconfined=True))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1158173395102581, 'fraction_numerical': 0.005625413633355393, 'mean_word_length': 3.643625192012289, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '277876', 'license': 'MIT', 'n_tokens_mistral': 1078, 'n_tokens_neox': 1043, 'n_words': 213, 'path': 'note/dev-1905/190416 - Graph of Chain.ipynb', 'repo_name': 'eliavw/morpheus', 'size': '3022'}