Skip to content

Add pipeline visualizer? #14061

Closed
Closed

Description

@amueller

Working with more complex pipelines and ColumnTransformer it can be a bit hard to visualize what's going on.
I think it would be nice to have a way to visualize the estimator graph.
Unfortunately this will be tricky without using graphviz.
With graphviz, it's pretty easy though.

on the column transformer example this produces:
image

or (with graph_attr={'rankdir': 'LR'}):
image

I'll put this into dabl for sure, but might also be interesting for sklearn, I think.
This is obviously just a hack and we could make this much nicer, for example with graphviz records. But even better would be avoiding graphviz.

Details
def pipe_to_viz(est, name_from_parent=None, parent=None, **kwargs):
    # graph is not actually used in the singleton case
    # unless there's no parent (and it's not actually a graph)
    if name_from_parent is None:
        graph = Digraph("Some Pipeline", **kwargs)
    else:
        graph = Digraph("cluster " + name_from_parent, **kwargs)
        graph.attr(color='grey')
        graph.attr(label=name_from_parent)
    graph.attr(compound='true')

    if isinstance(est, Pipeline):
        prev_step = None
        prev_sub = None
        for step in est.steps:
            sub, node = pipe_to_viz(step[1], name_from_parent=step[0], parent=graph)
            graph.subgraph(sub)
            if prev_step is not None:
                ltail = getattr(prev_sub, 'name', None)
                lhead = getattr(sub, 'name', None)
                graph.edge(prev_step, node, ltail=ltail, lhead=lhead)


            prev_step = node
            prev_sub = sub
        return graph, node
    elif isinstance(est, ColumnTransformer):
        graph.node("out", shape="point")
        for trans in est.transformers:
            sub, node = pipe_to_viz(trans[1], name_from_parent=trans[0], parent=graph)
            graph.subgraph(sub)
            ltail = getattr(sub, 'name', None)
            graph.edge(node, 'out', ltail=ltail)
        return graph, "out"
    else:
        label = est.__class__.__name__
        node = label + "_" + str(np.random.randint(10000))
        if parent is None:
            graph.node(node, label=label, shape='box')
            return graph, node
        # we could draw boxes around each estimator with the name
        # or we could use the name in  the label? hm
        parent.node(node, label=label, shape='box')
        return None, node

Activity

  1. jnothman commented on Jun 11, 2019

    @jnothman
    Member
  2. mitar commented on Jun 11, 2019

    @mitar
    Contributor
  3. amueller commented on Jun 11, 2019

    @amueller
    MemberAuthor

    This was a half-hour hack, yes, meta-estimators and feature-union are missing but easy to add.
    With the "changed only" it might be possible to have the parameters look reasonable.
    I was also wondering if the names of the steps should be shown (also easy).

    Maybe we need a designer for this lol?

    An alternative to graphviz would be using JS/d3 if we mostly target Jupyter. probably don't want that inside sklearn but could be nice?

  4. dmartinez05 commented on Jun 11, 2019

    @dmartinez05

    Is there any reason for trying to avoid graphviz?

  5. amueller commented on Jun 11, 2019

    @amueller
    MemberAuthor

    image

  6. amueller commented on Jun 11, 2019

    @amueller
    MemberAuthor

    @dmartinez05 it's hell to install, in particular without conda. Even in conda, they do evil tricks with a binary because they can't actually compile it.

  7. dmartinez05 commented on Jun 11, 2019

    @dmartinez05

    Yeah, I agree, I implemented something like this (even more basic) for D3M, and there was a lot of challenges even using docker images.

  8. mitar commented on Jun 11, 2019

    @mitar
    Contributor

    Yea, probably a HTML/JS output would be easier to make. I have seen some profilers do that.

  9. amueller commented on Jun 12, 2019

    @amueller
    MemberAuthor

    yeah I think html + css might be enough. Without javascript it's easier to embed in a notebook.
    I feel like I'm not good enough at html + css and UX/UI design to actually prototype this. I tried and it looked terrible lol.

  10. thomasjpfan commented on Jun 12, 2019

    @thomasjpfan
    Member

    Here is a 20 minute hacky version in HTML and CSS, just to see if its possible: https://codepen.io/anon/pen/agONqE

    Screen Shot 2019-06-12 at 2 22 37 PM

  11. TimothyWillard commented on Jun 28, 2019

    @TimothyWillard

    What about a plain stdout version of this? Something similar to Kera's print_summary. Might be useful when using python scripts instead of notebooks for longer running models or models that need to be run remotely but still want a detailed log of the model.

  12. thomasjpfan commented on Jun 28, 2019

    @thomasjpfan
    Member
  13. benjamin-ny commented on Jul 24, 2019

    @benjamin-ny

    I've also worked on this in the past, though it is a little hacky:

    https://nbviewer.jupyter.org/github/ottogroup/dstoolbox/blob/master/notebooks/Examples_visualization.ipynb

    I believe it's helpful to show the name of the step, e.g. to better spot which hyper-parameters to tune.

  14. mitar commented on Jul 24, 2019

    @mitar
    Contributor

    I have made pipeline visualizer for one other project here: https://gitlab.com/datadrivendiscovery/d3m/merge_requests/283 it might be repurposed for skleran as well.

  15. amueller commented on Jul 24, 2019

    @amueller
    MemberAuthor

    @mitar that looks pretty nice :)

  16. amueller commented on Jul 24, 2019

    @amueller
    MemberAuthor

    @benjamin-ny we would prefer not to use graphviz as it's quite tricky to install and/or compile.

  17. mitar commented on Jul 24, 2019

    @mitar
    Contributor

    If there is interest, I could try to extract the layout engine out into a stand-alone package. It is pure Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions