Subject: Re: [Ufl] [Branch ~ufl-core/ufl/main] Rev 1568: Add ipython svg output for cells interval, triangle and quadrilateral, just for show.

From: Martin Sandve Alnæs <[email-masked]>
Date: Thu, 7 Feb 2013 15:13:48 +0100

I think this wins the "most useless feature of fenics" award.

Martin

On 7 February 2013 15:08, <[email-masked]> wrote:

> ------------------------------------------------------------
> revno: 1568
> committer: Martin Sandve Alnaes <[email-masked]>
> branch nick: work
> timestamp: Thu 2013-02-07 15:07:17 +0100
> message:
>   Add ipython svg output for cells interval, triangle and quadrilateral,
> just for show.
> added:
>   sandbox/ipytest/
>   sandbox/ipytest/graph.py
> modified:
>   ufl/geometry.py
>
>
> --
> lp:ufl
> https://code.launchpad.net/~ufl-core/ufl/main
>
> Your team UFL Core Team is subscribed to branch lp:ufl.
> To unsubscribe from this branch go to
> https://code.launchpad.net/~ufl-core/ufl/main/+edit-subscription
>
> === added directory 'sandbox/ipytest'
> === added file 'sandbox/ipytest/graph.py'
> --- sandbox/ipytest/graph.py    1970-01-01 00:00:00 +0000
> +++ sandbox/ipytest/graph.py    2013-02-07 14:07:17 +0000
> @@ -0,0 +1,39 @@
> +
> +class A:
> +    def _repr_png_(self):
> +        import matplotlib.pyplot as plt
> +        import networkx as nx
> +        G = nx.Graph()
> +        G.add_nodes_from([1, 2,3])
> +        G.add_edges_from([(1,2),(1,3), (2,3)])
> +
> +        fig = plt.figure()
> +        nx.draw_spring(G)
> +
> +        #fig, ax = plt.subplots()
> +
> +        from IPython.core.pylabtools import print_figure
> +        data = print_figure(fig, 'png')
> +        # We MUST close the figure, otherwise IPython's display machinery
> +        # will pick it up and send it as output, resulting in a double
> display
> +        plt.close(fig)
> +        return data
> +
> +if 0:
> +    # Other way found on internet:
> +    import networkx as nx
> +    import matplotlib.pyplot as plt
> +    import StringIO
> +    from matplotlib.figure import Figure
> +
> +    class MyGraph(nx.Graph):
> +        def _repr_svg_(self):
> +            plt.ioff() # turn off interactive mode
> +            fig = plt.figure(figsize=(2,2))
> +            ax = fig.add_subplot(111)
> +            nx.draw_shell(self, ax=ax)
> +            output = StringIO.StringIO()
> +            fig.savefig(output,format='svg')
> +            plt.ion() # turn on interactive mode
> +            return output.getvalue()
> +
>
> === modified file 'ufl/geometry.py'
> --- ufl/geometry.py     2013-01-18 16:32:39 +0000
> +++ ufl/geometry.py     2013-02-07 14:07:17 +0000
> @@ -483,6 +483,19 @@
>      def __repr__(self):
>          return self._repr
>
> +    def _repr_svg_(self):
> +        n = self.cellname()
> +        svg = '<svg xmlns="http://www.w3.org/2000/svg"
> version="1.1">\n<polyline points="%s"
> style="fill:none;stroke:black;stroke-width:3" />\n</svg>'
> +        if n == "interval":
> +            svg = svg % '0,0, 200,0'
> +        elif n == "triangle":
> +            svg = svg % '0,200 200,200 0,0 0,200'
> +        elif n == "quadrilateral":
> +            svg = svg % '0,200 200,200 200,0 0,0 0,200'
> +        else:
> +            svg = None
> +        return svg
> +
>  class ProductCell(Cell):
>      """Representation of a cell formed by Cartesian products of other
> cells."""
>      __slots__ = ("_cells",)
>
>
>

---

From: "Marie E. Rognes" <[email-masked]>
Date: Thu, 07 Feb 2013 15:21:23 +0100

Hey, I think there should be proper nomination process for that award! ;-)

--
Marie

On 02/07/2013 03:13 PM, Martin Sandve Alnæs wrote:
> On 7 February 2013 15:08, <[email-masked] 
> <mailto:[email-masked]>> wrote:
>
>     committer: Martin Sandve Alnaes <[email-masked]
>     <mailto:[email-masked]>>
>     branch nick: work
>       Add ipython svg output for cells interval, triangle and
>     quadrilateral, just for show.
>     added:
>       sandbox/ipytest/
>       sandbox/ipytest/graph.py
>     modified:
>       ufl/geometry.py
>
>
>     --
>     lp:ufl
>     <https://code.launchpad.net/%7Eufl-core/ufl/main>
>
>     <https://code.launchpad.net/%7Eufl-core/ufl/main/+edit-subscription>
>
>     +        # will pick it up and send it as output, resulting in a
>     double display
>     +        plt.close(fig)
>     +        return data
>     +
>     +if 0:
>          """Representation of a cell formed by Cartesian products of
>     other cells."""
>          __slots__ = ("_cells",)
>
>
>
>
>
> _______________________________________________
> Mailing list: https://launchpad.net/~ufl
> Post to     : [email-masked]
> Unsubscribe : https://launchpad.net/~ufl
> More help   : https://help.launchpad.net/ListHelp