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

[ENH] - Update savefig default to save when given file_name #204

Merged
merged 1 commit into from
Apr 15, 2021
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
6 changes: 5 additions & 1 deletion fooof/plts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,14 @@ def savefig(func):
@wraps(func)
def decorated(*args, **kwargs):

save_fig = kwargs.pop('save_fig', False)
# Grab file name and path arguments, if they are in kwargs
file_name = kwargs.pop('file_name', None)
file_path = kwargs.pop('file_path', None)

# Check for an explicit argument for whether to save figure or not
# Defaults to saving when file name given (since bool(str)->True; bool(None)->False)
save_fig = kwargs.pop('save_fig', bool(file_name))

func(*args, **kwargs)

if save_fig:
Expand Down
20 changes: 14 additions & 6 deletions fooof/tests/plts/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Tests for fooof.plts.utils."""

import os
import tempfile

from fooof.tests.tutils import plot_test

from fooof.core.modutils import safe_import

from fooof.tests.tutils import plot_test
from fooof.tests.settings import TEST_PLOTS_PATH

from fooof.plts.utils import *

mpl = safe_import('matplotlib')
Expand Down Expand Up @@ -79,6 +79,14 @@ def test_savefig():
def example_plot():
plt.plot([1, 2], [3, 4])

with tempfile.NamedTemporaryFile(mode='w+') as file:
example_plot(save_fig=True, file_name=file.name)
assert os.path.exists(file.name)
# Test defaults to saving given file path & name
example_plot(file_path=TEST_PLOTS_PATH, file_name='test_savefig1.pdf')
assert os.path.exists(os.path.join(TEST_PLOTS_PATH, 'test_savefig1.pdf'))

# Test works the same when explicitly given `save_fig`
example_plot(save_fig=True, file_path=TEST_PLOTS_PATH, file_name='test_savefig2.pdf')
assert os.path.exists(os.path.join(TEST_PLOTS_PATH, 'test_savefig2.pdf'))

# Test does not save when `save_fig` set to False
example_plot(save_fig=False, file_path=TEST_PLOTS_PATH, file_name='test_savefig3.pdf')
assert not os.path.exists(os.path.join(TEST_PLOTS_PATH, 'test_savefig3.pdf'))