Example #1
0
    def uml(self, line, cell):
        """plantuml cell magic
        Ignores arguments. """

        data = runplantuml(cell)
        if data:
            display_svg(data, raw=True)
Example #2
0
    def uml(self, line, cell):
        """plantuml cell magic
        Ignores arguments. """

        data = runplantuml(cell)
        if data:
            display_svg(data, raw=True)
Example #3
0
 def _from_cell(self, line, cell=None, layout_engine='dot'):
     if cell is None:
         s = line
     else:
         s = line + '\n' + cell
     data = run_graphviz(s, layout_engine)
     if data:
         display_svg(data, raw=True)
 def display(self, show_buffs_params=False, show_weights=True):
     try:
         from IPython.core.display import display_svg
         display_svg(self.build_dot(show_buffs_params,
                                    show_weights=show_weights),
                     raw=False)
     except ImportError as _:
         print("only works in python notebooks")
Example #5
0
 def _from_cell(self, line, cell, layout_engine):
     if cell is None:
         s = line
     else:
         s = line + '\n' + cell
     data = run_graphviz(s, layout_engine)
     if data:
         display_svg(data, raw=True)
 def _dot(self, args, string):
     """ Run the Graphviz dot command. """
     # Parse the arguments
     image = run_dot(string, args.options, format=args.format)
     if args.format == 'png':
         display_png(image, raw=True)
     elif args.format == 'svg':
         display_svg(image, raw=True)
Example #7
0
 def dot(self, line, cell=None):
     """dot line/cell magic"""
     if cell is None:
         s = line
     else:
         s = line + '\n' + cell
     data = rundot(s)
     if data:
         display_svg(data, raw=True)
 def dot(self, line, cell=None):
     """dot line/cell magic"""
     if cell is None:
         s = line
     else:
         s = line + '\n' + cell
     data = rundot(s)
     if data:
         display_svg(data, raw=True)
    def dot(self, line, cell):
        """Draw a figure using Graphviz dot command."""
        args = parse_argstring(self.dot, line)

        image = run_dot(cell, args.options, format=args.format)

        if args.format == 'png':
            display_png(image, raw=True)
        elif args.format == 'svg':
            display_svg(image, raw=True)
Example #10
0
    def dot(self, line, cell):
        """Draw a figure using Graphviz dot command."""
        args = parse_argstring(self.dot, line)

        image = run_dot(cell, args.options, format=args.format)

        if args.format == 'png':
            display_png(image, raw=True)
        elif args.format == 'svg':
            display_svg(image, raw=True)
 def dotobj(self, line):
     """dot object magic"""
     obj = self.shell.ev(line)
     try:
         s = obj.to_dot()
     except AttributeError:
         error("expected object to implement 'to_dot()' method")
     else:
         data = rundot(s)
         if data:
             display_svg(data, raw=True)
 def dotobj(self, line):
     """dot object magic"""
     obj = self.shell.ev(line)
     try:
         s = obj.to_dot()
     except AttributeError:
         error("expected object to implement 'to_dot()' method")
     else:
         data = rundot(s)
         if data:
             display_svg(data, raw=True)
Example #13
0
    def dot(self, line, cell=None):
        """Draw a figure using Graphviz dot command."""

        if cell is None:
            s = line
        else:
            s = line + '\n' + cell

        data = run_dot(s)

        if data:
            display_svg(data, raw=True)
Example #14
0
 def _from_obj(self, line, layout_engine):
     obj = self.shell.ev(line)
     try:
         s = obj.to_dot()
     except AttributeError:
         error("expected object to implement 'to_dot()' method")
     except TypeError:
         error("expected to_dot method to be callable w/o args")
     else:
         data = run_graphviz(s, layout_engine)
         if data:
             display_svg(data, raw=True)
Example #15
0
 def _from_obj(self, line, layout_engine):
     obj = self.shell.ev(line)
     try:
         s = obj.to_dot()
     except AttributeError:
         error("expected object to implement 'to_dot()' method")
     except TypeError:
         error("expected to_dot method to be callable w/o args")
     else:
         data = run_graphviz(s, layout_engine)
         if data:
             display_svg(data, raw=True)
Example #16
0
    def dot(self, line, cell=None):
        """Draw a figure using Graphviz dot command."""

        if cell is None:
            s = line
        else:
            s = line + '\n' + cell

        data = run_dot(s)

        if data:
            display_svg(data, raw=True)
 def dotobjs(self, line):
     """dot objects magic"""
     objs = self.shell.ev(line)
     for i, obj in enumerate(objs):
         try:
             s = obj.to_dot()
         except AttributeError:
             error("expected object to implement 'to_dot()' method")
         else:
             data = rundot(s)
             if data:
                 info("object {}:".format(i))
                 display_svg(data, raw=True)
 def dotobjs(self, line):
     """dot objects magic"""
     objs = self.shell.ev(line)
     for i, obj in enumerate(objs):
         try:
             s = obj.to_dot()
         except AttributeError:
             error("expected object to implement 'to_dot()' method")
         else:
             data = rundot(s)
             if data:
                 info("object {}:".format(i))
                 display_svg(data, raw=True)
Example #19
0
        def dot(self, line="", cell=None):
            """Draw a figure using Graphviz dot command."""
            args = parse_argstring(self.dot, line)

            if cell is None:
                sh = get_ipython()
                cell = sh.ev(args.code[0])

            image = run_dot(cell, [], format=args.format)

            if args.format == 'png':
                display_png(image, raw=True)
            elif args.format == 'svg':
                display_svg(image, raw=True)
        def dot(self, line="", cell=None):
            """Draw a figure using Graphviz dot command."""
            args = parse_argstring(self.dot, line)

            if cell is None:
                sh = get_ipython()
                cell = sh.ev(args.code[0])

            image = run_dot(cell, [], format=args.format)

            if args.format == 'png':
                display_png(image, raw=True)
            elif args.format == 'svg':
                display_svg(image, raw=True)
Example #21
0
 def _from_objs(self, line, layout_engine):
     """dot objects magic"""
     objs = self.shell.ev(line)
     for i, obj in enumerate(objs):
         try:
             s = obj.to_dot()
         except AttributeError:
             error("expected object to implement 'to_dot()' method")
         except TypeError:
             error("expected to_dot method to be callable w/o args")
         else:
             data = run_graphviz(s, layout_engine)
             if data:
                 info("object {}:".format(i))
                 display_svg(data, raw=True)
Example #22
0
 def _from_objs(self, line, layout_engine):
     """dot objects magic"""
     objs = self.shell.ev(line)
     for i, obj in enumerate(objs):
         try:
             s = obj.to_dot()
         except AttributeError:
             error("expected object to implement 'to_dot()' method")
         except TypeError:
             error("expected to_dot method to be callable w/o args")
         else:
             data = run_graphviz(s, layout_engine)
             if data:
                 info("object {}:".format(i))
                 display_svg(data, raw=True)
    def display(self, show_buffs_params=False, show_weights=True):
        '''
        display the graph in Jupyter

        Parameters
        ----------
        show_buffs_params:
            whether to display also buffers and parameters which are not encased in the graph scopes
        show_weights:
            whether to display the nodes weight
        '''
        try:
            from IPython.core.display import display_svg
            display_svg(self.build_dot(show_buffs_params, show_weights=show_weights), raw=False)
        except ImportError as _:
            print("only works in python notebooks")
Example #24
0
 def open_output_path(self, output_path):
     with output_path.open() as file_pointer:
         contents = file_pointer.read()
     delete_attributes = True
     document = minidom.parseString(contents)
     svg_element = document.getElementsByTagName("svg")[0]
     view_box = svg_element.getAttribute("viewBox")
     view_box = [float(_) for _ in view_box.split()]
     if delete_attributes:
         if svg_element.attributes.get("height", None):
             del (svg_element.attributes["height"])
         if svg_element.attributes.get("width", None):
             del (svg_element.attributes["width"])
     else:
         height = "{}pt".format(int(view_box[-1] * 0.6))
         width = "{}pt".format(int(view_box[-2] * 0.6))
         svg_element.setAttribute("height", height)
         svg_element.setAttribute("width", width)
     svg_element.setAttribute("preserveAspectRatio", "xMinYMin")
     contents = document.toprettyxml()
     display_svg(contents, raw=True)
Example #25
0
def noise_feature_influence_plots(exp_result, feature_name=NOISE_FEATURE_NAME):
    """Interactive plotting tool for comparing the relationship between
    the noise_feature and classification correctness and rejections."""
    # Prepare dataset and result summaries.
    all_summaries = get_all_summaries(exp_result)
    dataset_parts = prepare_dataset(
        CLASSIFIERS[exp_result['config']['classifier']],
        DATASETS[exp_result['config']['dataset']],
        exp_result['config']['random_state'],
        exp_result['config']['test_size'],
        apply_preprocessing=False,
    )
    test_X, test_y = dataset_parts['test_X'], dataset_parts['test_y']

    # Classifier selection widgets.
    model_a_widget = widgets.Dropdown(description='Classifier:',
                                      options=all_summaries.keys())
    quantile_a_widget = widgets.Dropdown(description='CT t:',
                                         options=all_summaries[CT_SUMMARY_KEY].keys())
    model_b_widget = widgets.Dropdown(description='Classifier:',
                                      options=all_summaries.keys())
    quantile_b_widget = widgets.Dropdown(description='CT t:',
                                         options=all_summaries[CT_SUMMARY_KEY].keys())
    controls = widgets.HBox([
        widgets.VBox([widgets.Label('Classifier A'), model_a_widget, quantile_a_widget],
                     layout={'width': '50%'}),
        widgets.VBox([widgets.Label('Classifier B'), model_b_widget, quantile_b_widget],
                     layout={'width': '50%'}),
    ])

    # Plots
    plots = [widgets.Output(layout={'width': '50%'}) for _ in range(4)]
    plots_output = widgets.VBox([
        widgets.HBox([plots[0], plots[1]]),
        widgets.HBox([plots[2], plots[3]]),
    ])
    correctness_palette = {
        PLOT_CORRECT_LABEL: '#146614',
        PLOT_INCORRECT_LABEL: '#ff3333',
        PLOT_NULL_LABEL: '#2626bf',
    }

    def do_plot(correctness):
        """Plots a histogram of the correctness at different noise_feature values."""
        plot_df = test_X.assign(correctness=correctness, classes=test_y)
        fig = px.histogram(plot_df.sort_values(by='correctness'), x=feature_name, color='correctness',
                           color_discrete_map=correctness_palette)
        standard_fig_style(fig)
        fig.update_layout(
            width=470,
            height=470,
            showlegend=False,
            xaxis_title=feature_name,
            yaxis_title='Instances',
            legend={'traceorder': 'normal'}
        )
        render_svg_fig(fig)

    test_preds_base = pd.Series(
        get_experiment_base_summary(exp_result)['test_preds'],
        index=test_y.index
    )
    correctness_base = get_preds_correctness(test_y, test_preds_base)
    with plots[0]:
        # Base classifier (without rejection)
        print('Base classifier')
        do_plot(correctness_base)
    with plots[1]:
        # Legend
        display_svg(plot_legend_svg(correctness_palette), raw=True)

    def update(change):
        """Update correctness and plots for selected classifiers."""
        test_preds_a = pd.Series(
            all_summaries[model_a_widget.value][quantile_a_widget.value]['test_preds'],
            index=test_y.index
        )
        test_preds_b = pd.Series(
            all_summaries[model_b_widget.value][quantile_b_widget.value]['test_preds'],
            index=test_y.index
        )
        correctness_a = get_preds_correctness(test_y, test_preds_a)
        stats_a = correctness_stats(correctness_a)
        correctness_b = get_preds_correctness(test_y, test_preds_b)
        stats_b = correctness_stats(correctness_b)

        with plots[2]:
            plots[2].clear_output()
            print(f'{model_a_widget.value}, t={quantile_a_widget.value} (C={stats_a["coverage"]:.2%}; E\'={stats_a["conditional_error"]:.2%})')
            do_plot(correctness_a)
        with plots[3]:
            plots[3].clear_output()
            print(f'{model_b_widget.value}, t={quantile_b_widget.value} (C={stats_b["coverage"]:.2%}; E\'={stats_b["conditional_error"]:.2%})')
            do_plot(correctness_b)
        return

    # Update when selections change.
    model_a_widget.observe(update, names='value')
    quantile_a_widget.observe(update, names='value')
    model_b_widget.observe(update, names='value')
    quantile_b_widget.observe(update, names='value')

    display(controls, plots_output)
    update(None)
Example #26
0
 def dotstr(self, line):
     """dot string magic"""
     s = self.shell.ev(line)
     data = rundot(s)
     if data:
         display_svg(data, raw=True)
Example #27
0
def render_svg_fig(fig):
    display_svg(fig.to_image(format='svg').decode(), raw=True)
Example #28
0
def display_svg(output_path):
    from IPython.core.display import display_svg

    with output_path.open() as file_pointer:
        contents = file_pointer.read()
    display_svg(contents, raw=True)
 def dotstr(self, line):
     """dot string magic"""
     s = self.shell.ev(line)
     data = rundot(s)
     if data:
         display_svg(data, raw=True)
Example #30
0
 def open_output_path(self, output_path):
     display_svg(output_path)
Example #31
0
 def _from_str(self, line, layout_engine):
     s = self.shell.ev(line)
     data = run_graphviz(s, layout_engine)
     if data:
         display_svg(data, raw=True)
Example #32
0
 def _from_str(self, line, layout_engine):
     s = self.shell.ev(line)
     data = run_graphviz(s, layout_engine)
     if data:
         display_svg(data, raw=True)