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

Plot functions for tensorboard data #593

Merged
merged 4 commits into from Jan 26, 2022
Merged

Plot functions for tensorboard data #593

merged 4 commits into from Jan 26, 2022

Conversation

lappalainenj
Copy link
Contributor

I added extraction for any tensorboard data and basic plotting for any scalar tensorboard data for diagnostics.

Usage examples

  1. Plotting tensorboard data from inference object. The warning prompt is to support the user to start tensorboard for interactive visualization, as well as with hints on the valid tags.
    image

  2. Plotting tensorboard data from Path returned by sbi.utils.list_all_logs(inference), to be able to iterate over log directories of past runs directly.
    image

Note: For possible future extensions, I wrote _get_event_data_from_log_dir to parse the tensorboard data into a convenient tabular format.

@lappalainenj lappalainenj linked an issue Jan 24, 2022 that may be closed by this pull request
@lappalainenj lappalainenj changed the title Janne Plot functions for tensorboard data Jan 24, 2022
@codecov-commenter
Copy link

Codecov Report

Merging #593 (5b1e6d0) into main (b8551d0) will decrease coverage by 0.75%.
The diff coverage is 26.25%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #593      +/-   ##
==========================================
- Coverage   66.77%   66.02%   -0.76%     
==========================================
  Files          67       68       +1     
  Lines        4199     4279      +80     
==========================================
+ Hits         2804     2825      +21     
- Misses       1395     1454      +59     
Flag Coverage Δ
unittests 66.02% <26.25%> (-0.76%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
sbi/inference/base.py 77.53% <ø> (ø)
sbi/utils/tensorboard_output.py 25.31% <25.31%> (ø)
sbi/utils/__init__.py 100.00% <100.00%> (ø)

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 b8551d0...5b1e6d0. Read the comment docs.

Copy link
Contributor

@michaeldeistler michaeldeistler left a comment

Choose a reason for hiding this comment

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

Awesome, I think this is great! Left minor comments below, thanks a lot!

plot_kwargs: Dict[str, Any] = {},
) -> Tuple[Figure, Axes]:
"""Plots tensorboard scalar events logged by the summary writer held by an
inference object.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: first line of docstring should be a single line PEP 257

log_dir: Union[str, Path], size_guidance=DEFAULT_SIZE_GUIDANCE
) -> Dict[str, Dict[str, Dict[str, List[Any]]]]:
"""Returns an exhaustive nested dictionary of all event data stored by tensorboards
SummaryWriter.
Copy link
Contributor

Choose a reason for hiding this comment

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

Single line

3. tag type attribute: attribute of the event.

Args:
log_dir
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing


Args:
log_dir
size_guidance: to avoid OOming. Defaults to tensorboards default size_guidance.
Copy link
Contributor

Choose a reason for hiding this comment

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

What's OOming?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OOM = out of memory, because it loads most event data to memory at once

size_guidance: to avoid OOming. Defaults to tensorboards default size_guidance.

Returns:
Dict[str, Dict[str, Dict[str, Any]]]
Copy link
Contributor

Choose a reason for hiding this comment

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

type is already indicated above. Maybe add a one-line description?

sbi/utils/tensorboard_output.py Show resolved Hide resolved
@lappalainenj lappalainenj marked this pull request as draft January 25, 2022 11:35
@michaeldeistler
Copy link
Contributor

michaeldeistler commented Jan 25, 2022

Great, thanks a lot! One last request (sorry I didn't think of it before): could you add a test that checks the API? The test should run each of the three methods (SNPE, SNRE, SNLE) and run the plotting functions on the resulting inference object. Single-round is sufficient, and you can set max_num_epochs to keep the test fast. I.e.:

_ = inference.append_simulations(theta, x).train(max_num_epochs=5)

You can create a new file plot_tests.py in the tests folder for that.

Thanks again!

@michaeldeistler michaeldeistler linked an issue Jan 25, 2022 that may be closed by this pull request
from matplotlib.axes import Axes


def test_plot_summary(tmp_path):
Copy link
Contributor

Choose a reason for hiding this comment

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

To avoid code repetition, you could parameterize the test as done e.g. here

from matplotlib.figure import Figure
from matplotlib.axes import Axes


Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused...how does this test work? tmp_path is required, so what is it set to?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That’s some pytest magic, to avoid that files persist on the filesystem after a test. We can use it for any test writing sbi-logs.

https://docs.pytest.org/en/6.2.x/tmpdir.html

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, cool, thanks!

Copy link
Contributor

@michaeldeistler michaeldeistler left a comment

Choose a reason for hiding this comment

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

Thanks a lot! I left two minor comments (questions), but feel free to ignore them and merge :)

@lappalainenj lappalainenj merged commit 02f431a into main Jan 26, 2022
@lappalainenj lappalainenj deleted the janne branch January 26, 2022 14:35
janfb pushed a commit that referenced this pull request Jan 26, 2022
* added vscode code-workspace to gitignore

* Plot functions for tensorboard data #586

* fixed minor style issues

* adding test for plot_summary
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.

Plot functions for tensorboard data Logging covers only basic training stats
3 participants