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

html representation for jupyter notebooks #1217

Merged
merged 32 commits into from
Jun 9, 2020

Conversation

percygautam
Copy link
Contributor

@percygautam percygautam commented Jun 1, 2020

Description

This PR implements the html representation for arviz.InferenceData object in jupyter notebooks. Related to discussion in #1066.

Checklist

  • Follows official PR format
  • Code style correct (follows pylint and black guidelines)
  • Changes are listed in changelog

arviz/utils.py Outdated Show resolved Hide resolved
Copy link
Member

@canyon289 canyon289 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may have a second round of comments after this first one. Also in regards to CI errors let us know if you need help learning how to get to them, or understand what they are saying.

Can you add a screenshot as well showing what these changes do in the notebook? I thought the xarray repr was already in html in jupyter if we set the xarray option. What is this adding on top of that?

Copy link
Member

@OriolAbril OriolAbril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also update cookbook and quickstart notebooks to show the html goodness :)

arviz/data/inference_data.py Outdated Show resolved Hide resolved
arviz/data/inference_data.py Outdated Show resolved Hide resolved
arviz/utils.py Outdated Show resolved Hide resolved
@percygautam
Copy link
Contributor Author

percygautam commented Jun 1, 2020

@canyon289 Yes the xarray's repr is in html if we set the option. This PR is making the whole inferenceData object dynamic. The result can be seen in this jupyter notebook: https://nbviewer.jupyter.org/github/percygautam/arviz-examples/blob/master/HTML%20repr.ipynb

arviz/utils.py Outdated
</div>
"""
element_template = """
<ul class="xr-sections">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the vertical spacing between groups is very large. I think this could be due to ul being part of the element template as this is defining a different list for each group. Can you try moving it to the html template so that we have a single list (only one ul defined in html template) and the groups are then elements of this single list (one li per group)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ul/li change is also marked as resolved but it is not and again I have a vague memory of seeing the ul moved to html instead of element template :/

Copy link
Member

@canyon289 canyon289 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Havent had time to rereview but don't want to block if this PR is ready to go. Deferring judgement to whoever gets to it before me

@OriolAbril
Copy link
Member

LGTM, we can merge after updating a couple notebooks.

@canyon289
Copy link
Member

canyon289 commented Jun 3, 2020

I am going to request tests before merge. IMO all code should be tested if it can, and this is no exception :). I would add a test that calls _repr_html_ directly and tests that the html string is returned.

I would also add a tests the correct repr is returned without an explicit call. I assume this will require some mocking and to "trick" the non jupyter notebook test to provide the html repr.

As is such still requesting some changes :)

arviz/utils.py Outdated


class HtmlTemplate:
"""Contains html templates."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expand docstring. Contains html templates for what?

Would change this to something along the lines of contains html templates for InferenceData repr

arviz/utils.py Outdated Show resolved Hide resolved
@canyon289
Copy link
Member

@percygautam this is already looking great and youre on the right track. Hope you dont mind my additional comments :)

@percygautam
Copy link
Contributor Author

I am going to request tests before merge. IMO all code should be tested if it can, and this is no exception :). I would add a test that calls _repr_html_ directly and tests that the html string is returned.

I would also add a tests the correct repr is returned without an explicit call. I assume this will require some mocking and to "trick" the non jupyter notebook test to provide the html repr.

I have added the basic test. How do I add the second test? I looked through but couldn't find the way.

@OriolAbril OriolAbril mentioned this pull request Jun 5, 2020
6 tasks
Copy link
Member

@OriolAbril OriolAbril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pystan refitting and numba notebooks have no inferencedata so there is no need to rerun them

arviz/utils.py Outdated Show resolved Hide resolved
@percygautam
Copy link
Contributor Author

Actually I did revert some commits as I have forgot to gitignore the cache of notebooks and pushed them. I'll check and make all the changes again.

@codecov
Copy link

codecov bot commented Jun 6, 2020

Codecov Report

Merging #1217 into master will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1217      +/-   ##
==========================================
- Coverage   93.09%   93.09%   -0.01%     
==========================================
  Files          95       94       -1     
  Lines        9591     9525      -66     
==========================================
- Hits         8929     8867      -62     
+ Misses        662      658       -4     
Impacted Files Coverage Δ
arviz/data/inference_data.py 80.11% <100.00%> (+0.71%) ⬆️
arviz/utils.py 89.56% <100.00%> (+0.23%) ⬆️
arviz/data/__init__.py 100.00% <0.00%> (ø)
arviz/data/io_pyjags.py

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d8b14cf...696b8a4. Read the comment docs.

Copy link
Member

@canyon289 canyon289 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needs more tests, Please add them. Approving so I don't block you

msg = "Inference data with groups:\n\t> {options}".format(
options="\n\t> ".join(self._groups)
)
if self._groups_warmup:
msg += "\n\nWarmup iterations saved ({}*).".format(WARMUP_TAG)
return msg

def _repr_html_(self):
"""Make html representation of InferenceData object."""
display_style = OPTIONS["display_style"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to request tests before merge. IMO all code should be tested if it can, and this is no exception :). I would add a test that calls _repr_html_ directly and tests that the html string is returned.
I would also add a tests the correct repr is returned without an explicit call. I assume this will require some mocking and to "trick" the non jupyter notebook test to provide the html repr.

I have added the basic test. How do I add the second test? I looked through but couldn't find the way.

This is going to be fun one. Making tests is sometimes a puzzle and harder than the actual programming itself, but important if you want you feature to be stable and not broken by any others.

In the test you'll have to monkeypatch OPTIONS to so when the test executes the value is text to test that this if condition works. Just being up front here it's going to be hard and confusing, but youre going to learn a lot more about python.

I don't want to block this whole PR for that one thing though. We can merge but I really would like you to add this test in a followup PR. At a minimum create an issue so we can track

https://docs.pytest.org/en/latest/monkeypatch.html

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will not be necessary to monekypatch because the display style option is already customizable similarly to an rcParam. We can start the test by setting the display_style to text/html, make sure the correct repr is returned and then finish the test by going back to the original displaystyle (much like your original implementation of the repr @percygautam).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its typically bad practice to have tests rely on global state like rcparam although in this case lets just do it cause monkeypatching would be annoying :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am okay with both options. We can currently implement the test using rcParams, and create the issue to track the monkeypatch test and merge this PR. I will implement the test later.
The html_repr really facilitates to check other methods that I am adding, so it'll be better to merge this one as soon as possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with merging as soon as possible! Seems like youre only a couple linting errors away :)

@canyon289
Copy link
Member

Add changelog and this one should be good to go

@OriolAbril
Copy link
Member

@percygautam can you rebase and rerun notebook? from_pyjags has just been added so it is one more case for which to show the new repr

@OriolAbril OriolAbril merged commit 661e057 into arviz-devs:master Jun 9, 2020
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

Successfully merging this pull request may close these issues.

4 participants