pymc-devs / pymc

Bayesian Modeling and Probabilistic Programming in Python
https://docs.pymc.io/
Other
8.48k stars 1.97k forks source link

Implement CustomProgress that does not output empty divs when disabled #7290

Closed tomicapretto closed 1 month ago

tomicapretto commented 2 months ago

Description

This PR adds a new class CustomProgress that inherits from rich.progress.Progress. I overloaded the necessary methods to make it not output anything when we disable the progress bar. I feel it's a bit hacky so I would like to know what others think about this modification.

This problem is noticeable especially in VS Code. It's not such a big issue in Jupyter Notebook. Some screenshots:

VS Code progressbar=True (before and after we get the same result, I cannot get rid of those empty spaces)

capture1

VS Code progressbar=False before the changes

capture2

VS Code progressbar=False after the changes

image

Jupyter Notebook after the changes

capture3

Update

In the terminal, after the change

image

Related Issue


📚 Documentation preview 📚: https://pymc--7290.org.readthedocs.build/en/7290/

tomicapretto commented 2 months ago

Some tests are failing, it's because there's something I missed in the child class I created.

tomicapretto commented 2 months ago

@ricardoV94 this is ready to be reviewed

ricardoV94 commented 2 months ago

@tomicapretto you seem to have picked commits from main, can you rebase? Hard to review otherwise

tomicapretto commented 2 months ago

@tomicapretto you seem to have picked commits from main, can you rebase? Hard to review otherwise

I think I did not the cleanest thing but I think it does not show anything from main now

tomicapretto commented 2 months ago

Converted to a draft because I found other points of improvement.

image

fonnesbeck commented 2 months ago

The refresh argument was added because (1) without it progress bars did not always end at 100% after sampling was complete (as you noted above) and (2) the divergences text did not update without it.What about adding a single refresh update at the end of sampling? Perhaps that would ensure the last update is complete.

You can use the example in #7278 to test for (2).

tomicapretto commented 2 months ago

@fonnesbeck I'll test some ideas / combinations and update on my findings

drbenvincent commented 2 months ago

FYI: This has also affected doctests in CausalPy, see https://github.com/pymc-labs/CausalPy/issues/323 Would be very pleased to see a fix :)

tomicapretto commented 1 month ago

Before the last commit, it wouldn't update to 100% using one chain (because it doesn't use multiprocess sampling)

completed_not_full

Now it works well

image

I can confirm divergences are updated and it goes up to 100% divergences

The progressbar for posterior predictive sampling was also broken: posterior_predictive_not

Now it works fine: posterior_predictive_ok

tomicapretto commented 1 month ago

I'm now trying to see if I can get those two extra divs not added. It does not take any relevant space when using jupyter notebook, but it appears on VS Code and it's really bothering.

image

tomicapretto commented 1 month ago

Ok I have some findings. It has to do with how rich handles the output in jupyter notebooks. Console has a force_jupyter parameter which is None by default (and with notebooks it's set to True internally). See what happens when we change it.

In VS Code, it removes the extra space that gets added, but notice it changes the color and the description: force_jupyter_vs

In VS Code, it doesn't do any harm, but it stillchanges the color and the description: force_jupyter_jpt

I'm not sure if this is something we can take care of. It's really bothering for the ones who use VS Code, but I don't see any simple solution. I'll do a bit more research but I'm not going to invest a lot of time on it.

EDIT

It's not that it does not produce the same output on both platforms. It does produce the same output and it's this:

<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"></pre>

But I guess Jupyter Notebooks must be using some CSS that causes the height of the output to be very small, while VS Code does not.

ricardoV94 commented 1 month ago

Not to be a bummer, but should we revert to the old progessbar? @fonnesbeck @zaxtax ?

tomicapretto commented 1 month ago

Not to be a bummer, but should we revert to the old progessbar? @fonnesbeck @zaxtax ?

I'm not sure what was the reason to move away from the old one. But definitely the goodness from rich are not for free.

fonnesbeck commented 1 month ago

I can dig into it a bit later today. If it comes down to color differences, I'm fine with it. If its deeper than that then we probably should seek alternatives (including reverting).

aseyboldt commented 1 month ago

As I noticed in nutpie, it is also surprisingly easy to build a custom progress bar. We could also adapt something like the nutpie one into pymc if that makes sense. It won't work (without modifications) in the terminal however.

fonnesbeck commented 1 month ago

@aseyboldt you are using fastprogress in nutpie, correct?

aseyboldt commented 1 month ago

Not anymore. This included an updated progressbar: https://github.com/pymc-devs/nutpie/pull/105 image

It's not released yet though. The code is around here: https://github.com/pymc-devs/nutpie/blob/main/python/nutpie/sample.py#L308

fonnesbeck commented 1 month ago

After testing this for a bit, I'm inclined to blame VS Code here rather than rich. The Python interactive pane is very buggy, not just here but in a variety of settings. Buttons on the toolbar often do not work, the input field occasionally gets resized so you can't read the text in it, and so on. Its VS Code's responsibility to have its Jupyter interface behave like a Jupyter notebook, and it often does not.

fonnesbeck commented 1 month ago

In the meantime, should we at least go ahead and merge some version of this PR? It clearly fixes things for a lot of people.

tomicapretto commented 1 month ago

In the meantime, should we at least go ahead and merge some version of this PR? It clearly fixes things for a lot of people.

I agree. I've seen the speed being a lot worse just because of the progressbar. So this is already something.

fonnesbeck commented 1 month ago

The outstanding failure appears to be related to Jax versioning(?) Its complaining about using max as an argument to clip instead of a_max.

ricardoV94 commented 1 month ago

The outstanding failure appears to be related to Jax versioning(?) Its complaining about using max as an argument to clip instead of a_max.

It's blackjax fault: https://github.com/pymc-devs/pymc/pull/7317

fonnesbeck commented 1 month ago

@tomicapretto can you rebase this to get the fix in #7320 ?

tomicapretto commented 1 month ago

@tomicapretto can you rebase this to get the fix in #7320 ?

@fonnesbeck I think rebased correctly, but I'm not 100% as I don't find myself doing it often.

codecov-commenter commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 92.50%. Comparing base (0216473) to head (292dae9).

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290/graphs/tree.svg?width=650&height=150&src=pr&token=JFuXtOJ4Cb&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs)](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs) ```diff @@ Coverage Diff @@ ## main #7290 +/- ## ========================================== + Coverage 91.84% 92.50% +0.65% ========================================== Files 102 102 Lines 17149 17177 +28 ========================================== + Hits 15751 15889 +138 + Misses 1398 1288 -110 ``` | [Files](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs) | Coverage Δ | | |---|---|---| | [pymc/backends/arviz.py](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290?src=pr&el=tree&filepath=pymc%2Fbackends%2Farviz.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs#diff-cHltYy9iYWNrZW5kcy9hcnZpei5weQ==) | `96.61% <100.00%> (ø)` | | | [pymc/sampling/forward.py](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290?src=pr&el=tree&filepath=pymc%2Fsampling%2Fforward.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs#diff-cHltYy9zYW1wbGluZy9mb3J3YXJkLnB5) | `95.96% <100.00%> (+0.03%)` | :arrow_up: | | [pymc/sampling/mcmc.py](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290?src=pr&el=tree&filepath=pymc%2Fsampling%2Fmcmc.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs#diff-cHltYy9zYW1wbGluZy9tY21jLnB5) | `88.00% <100.00%> (+0.02%)` | :arrow_up: | | [pymc/sampling/parallel.py](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290?src=pr&el=tree&filepath=pymc%2Fsampling%2Fparallel.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs#diff-cHltYy9zYW1wbGluZy9wYXJhbGxlbC5weQ==) | `88.50% <100.00%> (ø)` | | | [pymc/sampling/population.py](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290?src=pr&el=tree&filepath=pymc%2Fsampling%2Fpopulation.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs#diff-cHltYy9zYW1wbGluZy9wb3B1bGF0aW9uLnB5) | `73.71% <100.00%> (ø)` | | | [pymc/smc/sampling.py](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290?src=pr&el=tree&filepath=pymc%2Fsmc%2Fsampling.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs#diff-cHltYy9zbWMvc2FtcGxpbmcucHk=) | `99.20% <100.00%> (ø)` | | | [pymc/tuning/starting.py](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290?src=pr&el=tree&filepath=pymc%2Ftuning%2Fstarting.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs#diff-cHltYy90dW5pbmcvc3RhcnRpbmcucHk=) | `91.22% <100.00%> (ø)` | | | [pymc/util.py](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290?src=pr&el=tree&filepath=pymc%2Futil.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs#diff-cHltYy91dGlsLnB5) | `81.39% <100.00%> (+1.99%)` | :arrow_up: | | [pymc/variational/inference.py](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290?src=pr&el=tree&filepath=pymc%2Fvariational%2Finference.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs#diff-cHltYy92YXJpYXRpb25hbC9pbmZlcmVuY2UucHk=) | `87.93% <100.00%> (ø)` | | ... and [3 files with indirect coverage changes](https://app.codecov.io/gh/pymc-devs/pymc/pull/7290/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pymc-devs)