Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve print(mesh) #142

Closed
prisae opened this issue Mar 20, 2019 · 20 comments
Closed

Improve print(mesh) #142

prisae opened this issue Mar 20, 2019 · 20 comments

Comments

@prisae
Copy link
Member

prisae commented Mar 20, 2019

I think the current print-utility of meshes was made with small meshes in mind. It sort of explodes for big, stretched meshes:

screenshot

I think something along these lines would be preferable (maybe as a rendered table in notebooks):

discretize.TensorMesh: 128 x 128 x 128 cells; 2,097,152 in total

    dir          min/max N            min/max h
    -----------------------------------------------------
     x:      -4176.3 / 4176.3;     25.0 / 143.0
     y:      -5902.6 / 5902.6;     50.0 / 160.8
     z:      -7151.7 / 7151.7;     20.0 / 355.8
@prisae
Copy link
Member Author

prisae commented Mar 20, 2019

Related, to some extent, to #21.

@banesullivan
Copy link
Member

I could help implement a _repr_html_ method like what I did for vtki:

Screen Shot 2019-03-20 at 10 20 54 AM

@prisae
Copy link
Member Author

prisae commented Mar 20, 2019

I think that would be great! But I think @lheagy and @rowanc1 and probably others of SimPEG have to chime in with their opinion, as it is their brainchild.

A few points:

  • What happens if you are not in a notebook? Is there a plain text fallback?
  • I think minimum cell-width in each distance would be useful.
  • Maybe format the bounds as .1f instead of .3e

@banesullivan
Copy link
Member

banesullivan commented Mar 20, 2019

The _repr_html_ method is only ever used in an IPythonJupyter notebook environment. If you want a plain text representation to fall back on, then it can be implemented in the __repr__ method:

Screen Shot 2019-03-20 at 12 39 09 PM

@prisae
Copy link
Member Author

prisae commented Mar 20, 2019

It would also go nicely along with the versions.
Selection_002

@prisae
Copy link
Member Author

prisae commented Mar 20, 2019

That is interesting. I never figured out, how to distinguish between a Notebook kernel and an IPython/Qt kernel. If you do the same in an IPython terminal, what will happen?

@prisae
Copy link
Member Author

prisae commented Mar 20, 2019

(The discussion around this problem is here: simpeg/simpeg#698).

@banesullivan
Copy link
Member

banesullivan commented Mar 20, 2019

As a developer, you don't have to do anything besides implement a method called _repr_html_ that returns a string of your HTML representation - Jupyter handles figuring out that it should be used. Ipython/Qt would call the __repr__ method by default

@banesullivan
Copy link
Member

banesullivan commented Mar 20, 2019

@prisae - if you want an HTML representation in notebooks and a plain text representation in IPython/Qt then simpll make class object for your versions info that implements the two __repr__ and _repr_html_ methods

@prisae
Copy link
Member Author

prisae commented Mar 20, 2019

I don't think it is that easy. See this screenshot from IPython:
Selection_001

@banesullivan
Copy link
Member

banesullivan commented Mar 20, 2019

What happens if you call repr(examples.load_rectilinear())?

@banesullivan
Copy link
Member

Also here's something to test:

class Information(object):

    @property
    def n_cells(self):
        return 6

    @property
    def bounds(self):
        return (-1,1, -1,1, -1,1)

    def _get_attrs(self):
        """An internal helper for the representation methods"""
        attrs = []
        attrs.append(("N Cells", self.n_cells, "{}"))
        bds = self.bounds
        attrs.append(("X Bounds", (bds[0], bds[1]), "{:.3e}, {:.3e}"))
        attrs.append(("Y Bounds", (bds[2], bds[3]), "{:.3e}, {:.3e}"))
        attrs.append(("Z Bounds", (bds[4], bds[5]), "{:.3e}, {:.3e}"))
        return attrs


    def __repr__(self):
        fmt = "{} ({})\n".format(type(self).__name__, hex(id(self)))
        # now make a call on the object to get its attributes as a list of len 2 tuples
        row = "  {}:\t{}\n"
        for attr in self._get_attrs():
            try:
                fmt += row.format(attr[0], attr[2].format(*attr[1]))
            except:
                fmt += row.format(attr[0], attr[2].format(attr[1]))
        return fmt

    def _repr_html_(self):
        fmt = ""
        # HTML version
        fmt += "\n"
        fmt += "<table>\n"
        fmt += "<tr><th>{}</th><th>Information</th></tr>\n".format(type(self).__name__)
        row = "<tr><td>{}</td><td>{}</td></tr>\n"
        # now make a call on the object to get its attributes as a list of len 2 tuples
        for attr in self._get_attrs():
            try:
                fmt += row.format(attr[0], attr[2].format(*attr[1]))
            except:
                fmt += row.format(attr[0], attr[2].format(attr[1]))
        fmt += "</table>\n"
        fmt += "\n"
        return fmt


Information()

@prisae
Copy link
Member Author

prisae commented Mar 20, 2019

Selection_001

@banesullivan
Copy link
Member

That's a bug on my part in vtki will fix now... try the basic Information class shared above

@prisae
Copy link
Member Author

prisae commented Mar 20, 2019

Selection_002

@banesullivan
Copy link
Member

Went ahead and added in the bare bones for you @prisae: #143

@lheagy
Copy link
Member

lheagy commented Mar 20, 2019

This is looking good and would be a great contribution!

Just a couple questions at this early stage:

  • what do you mean be N in the first column? Would 'domain' be appropriate here?
  • Visually, it might be more intuitive if the number of cells in each dimension is in the table rather than the first line

@rowanc1
Copy link
Member

rowanc1 commented Mar 20, 2019

I think that it would be absolutely awesome to improve this print out. Starting down the road of useful (albeit at the start) static widgets is a really cool avenue to explore.

The print function was originally done by @dwfmarchant, back in 2014 or so! Probably in the first versions of the python implementation - lots of room for improvement. :)

@prisae
Copy link
Member Author

prisae commented Mar 21, 2019

Thanks for the help @bane, and for the feedback @lheagy, @rowanc1. I will look into this, hopefully next week or the week after.

@prisae
Copy link
Member Author

prisae commented Jun 11, 2019

This was fixed in #143 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants