scikit-hep / histbook

Versatile, high-performance histogram toolkit for Numpy.
BSD 3-Clause "New" or "Revised" License
109 stars 9 forks source link

Multiple object output in JupyterLab #52

Closed veprbl closed 5 years ago

veprbl commented 5 years ago

Currently, the README advises to just drop .to(canvas) call and let jupyterlab try to display the histogram object. Often times it is convenient to output multiple histograms from the same cell. For that case one can use

from IPython.display import display
display(histogram.step("data"))

I think many people would want to know about that. Even further, it would make sense to unify interface for this. I think something like this can be implemented as a part of histbook.vega:

import numpy
from histbook import *
from IPython.display import display
class VegaJson:
    def __init__(self, x):
        self._mimejson = {"application/vnd.vegalite.v2+json": x}
    def _repr_mimebundle_(self, **kwargs):
        return self._mimejson
canvas = lambda x: display(VegaJson(x))

array = numpy.random.normal(0, 1, 1000000)
histogram = Hist(bin("data", 10, -5, 5))
histogram.fill(data=array)
histogram.step("data").to(canvas)

What do you think about this?

jpivarski commented 5 years ago

Just to let you know, the future of histbook is uncertain because I learned about the existence of Boost.Histogram and Physt, both excellent histogramming libraries available in Python. (Boost.Histogram was once and will again be available in Python, after it gets refactored from Boost.Python to pybind11.)

Now, about your question: have you tried just

histogram.step("data")

in a cell? The object returned by step (and the other plotting routines) has a _repr_mimebundle_ already. That makes these things easier to view in JupyterLab than in Jupyter Notebook, but I had too many Notebook users to complicate the instructions about needing .to(canvas) (and there was no way to make it work in both Lab and Notebook). Since each object has a _repr_mimebundle_, doing

histogram.step("data1")
histogram.step("data2")
histogram.step("data3")

ought to get you three displays, no?

veprbl commented 5 years ago

Just to let you know, the future of histbook is uncertain because I learned about the existence of Boost.Histogram and Physt, both excellent histogramming libraries available in Python.

I remember that you say that you'd rather contribute ideas than maintain a code, but I fail to understand how any of these packages can cover functionality of histbook.

histogram.step("data1")
histogram.step("data2")
histogram.step("data3")

ought to get you three displays, no?

Ofcourse not. It happens for the same reason why

1
2
3

gives only 3

veprbl commented 5 years ago

and there was no way to make it work in both Lab and Notebook

I think I'm proposing exactly the way to do that.

jpivarski commented 5 years ago

Last I tried it, Notebook ignored the Vega mimetype.

veprbl commented 5 years ago

@jpivarski Absolutely. I meant that both Notebook and Lab to use .to(canvas).

jpivarski commented 5 years ago

I haven't yet wrapped my head around how this makes it work for Notebook (since Notebook required a custom-Javascript solution, pyvega, which isn't allowed in Lab, and then it overrides the mimetype— that's why I couldn't have both). However, if it does work, then congratulations (!) and could you submit a pull request?

To keep you aware of developments, I've been working on a cross-library, cross-language histogram RPC format with all the bells and whistles so that histograms from any histogramming library can be encoded in it. Then we can let Boost.Histogram becomes the best filler, Physt be the best plotter, etc. This is histos (from the Greek; we had a long exchange about the etymology of the name "histogram").

I also (just this morning) got rights to the pip name "hist". I might put a nice front-end to histos in a package that can be installed with "pip install hist" that would take most of the functionality of histbook. The one thing that would be held back is histbook's call-graph optimization of expressions— this rightly belongs in a computation engine like awkward-array.