Skip to content

Commit b376e00

Browse files
committed
add separate save_figure func for saving
1 parent 674c495 commit b376e00

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

fooof/plts/utils.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,28 @@ def decorated(*args, **kwargs):
199199
func(*args, **kwargs)
200200

201201
if save_fig:
202-
if not file_name:
203-
raise ValueError("Input 'file_name' is required to save out the plot.")
204-
plt.savefig(fpath(file_path, fname(file_name, 'png')), **save_kwargs)
205-
206-
if close:
207-
plt.close()
202+
save_figure(file_name, file_path, close, **save_kwargs)
208203

209204
return decorated
205+
206+
207+
def save_figure(file_name, file_path=None, close=False, **save_kwargs):
208+
"""Save out a figure.
209+
210+
Parameters
211+
----------
212+
file_name : str
213+
File name for the figure file to save out.
214+
file_path : str or Path
215+
Path for where to save out the figure to.
216+
close : bool, optional, default: False
217+
Whether to close the plot after saving.
218+
save_kwargs
219+
Additional arguments to pass into the save function.
220+
"""
221+
222+
full_path = fpath(file_path, fname(file_name, 'png'))
223+
plt.savefig(full_path, **save_kwargs)
224+
225+
if close:
226+
plt.close()

fooof/tests/plts/test_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,9 @@ def example_plot():
9595
# Test does not save when `save_fig` set to False
9696
example_plot(save_fig=False, file_path=TEST_PLOTS_PATH, file_name='test_savefig_nope.pdf')
9797
assert not os.path.exists(os.path.join(TEST_PLOTS_PATH, 'test_savefig_nope.pdf'))
98+
99+
def test_save_figure():
100+
101+
plt.plot([1, 2], [3, 4])
102+
save_figure(file_name='test_save_figure.pdf', file_path=TEST_PLOTS_PATH)
103+
assert os.path.exists(os.path.join(TEST_PLOTS_PATH, 'test_save_figure.pdf'))

0 commit comments

Comments
 (0)