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

Simplify show model #68

Merged
merged 4 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions blobmodel/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import numpy as np
import xarray as xr
from matplotlib import animation
from typing import Union


def show_model(
dataset: xr.Dataset,
variable: str = "n",
interval: int = 100,
save: bool = False,
gif_name: str = "blobs.gif",
gif_name: Union[str, None] = None,
fps: int = 10,
) -> None:
"""
Expand All @@ -26,10 +26,8 @@ def show_model(
Variable to be animated (default: "n").
interval : int, optional
Time interval between frames in milliseconds (default: 100).
save : bool, optional
If True, save the animation as a GIF (default: False).
gif_name : str, optional
Set the name for the saved GIF (default: "blobs.gif").
If not None, save the animation as a GIF and name it acoridingly.
fps : int, optional
Set the frames per second for the saved GIF (default: 10).

Expand Down Expand Up @@ -103,7 +101,7 @@ def animate_2d(i: int) -> None:
fig, animate_2d, frames=dataset["t"].values.size, interval=interval
)

if save:
if gif_name:
ani.save(gif_name, writer="ffmpeg", fps=fps)
plt.show()

Expand Down
2 changes: 1 addition & 1 deletion examples/1d_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
)

ds = bm.make_realization(speed_up=True, error=1e-2)
show_model(dataset=ds, interval=100, save=True)
show_model(dataset=ds, interval=100, gif_name="1d_animation.gif")
2 changes: 1 addition & 1 deletion examples/2d_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
# create data
ds = bm.make_realization(speed_up=True, error=1e-2)
# show animation and save as gif
show_model(dataset=ds, interval=100, save=True, gif_name="example.gif", fps=10)
show_model(dataset=ds, interval=100, gif_name="2d_animation.gif", fps=10)
2 changes: 1 addition & 1 deletion examples/compare_to_analytical_sol.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
blob_factory=bf,
)

ds = tmp.make_realization(file_name="profile_comparison.nc", speed_up=True, error=1e-2)
ds = tmp.make_realization(file_name="profile_comparison.nc", speed_up=False, error=1e-4)


def plot_convergence_to_analytical_solution(ds):
Expand Down
2 changes: 1 addition & 1 deletion examples/single_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ def is_one_dimensional(self) -> bool:
ds = bm.make_realization(speed_up=True, error=1e-2)

# show animation and save as gif
show_model(dataset=ds, interval=100, save=True, gif_name="example.gif", fps=10)
show_model(dataset=ds, interval=100, gif_name="example.gif", fps=10)
4 changes: 2 additions & 2 deletions tests/test_show_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@patch("matplotlib.pyplot.show")
def test_plot_2d(mock_show):
warnings.filterwarnings("ignore")
show_model(dataset=ds_2d, interval=100, save=False, fps=10)
show_model(dataset=ds_2d, interval=100, gif_name=None, fps=10)


bm_1d = Model(
Expand All @@ -46,4 +46,4 @@ def test_plot_2d(mock_show):
@patch("matplotlib.pyplot.show")
def test_plot_1d(mock_show):
warnings.filterwarnings("ignore")
show_model(dataset=ds_1d, interval=100, save=False, fps=10)
show_model(dataset=ds_1d, interval=100, gif_name=None, fps=10)
Loading