Skip to content

Commit

Permalink
Create subdirs automatically with Figure
Browse files Browse the repository at this point in the history
Resolves #120
  • Loading branch information
asgerius committed Dec 24, 2022
1 parent 4b5fb27 commit d76abf9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Removed `TickTock.remove_outliers`
- Moved `is_windows` to static `OS` attribute, which also contains `is_mac` and `is_linux`
- Added `HardwareInfo`, which contains a bunch of system hardware information
- `Figure` now automatically creates necessary subdirectories

### Bug fixes

Expand Down
4 changes: 4 additions & 0 deletions pelutils/ds/plots.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import time
from typing import Any, Callable, List, Optional, Union

Expand Down Expand Up @@ -233,6 +234,9 @@ def __exit__(self, et, ev, tb):
if self._tight_layout:
plt.tight_layout()
if not et:
directory = os.path.split(self._savepath)[0]
if directory:
os.makedirs(directory, exist_ok=True)
plt.savefig(self._savepath)
plt.close()

Expand Down
6 changes: 5 additions & 1 deletion tests/ds/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ class TestFigure(UnitTestCollection):
def test_save(self):
with Figure(self.savepath):
pass
assert os.path.exists(self.savepath)
assert os.path.isfile(self.savepath)
path = os.path.join(UnitTestCollection.test_dir, "many", "long", "subdirectories.png")
with Figure(path):
pass
assert os.path.isfile(path)

def test_no_save_if_error(self):
if os.path.exists(self.savepath):
Expand Down

0 comments on commit d76abf9

Please sign in to comment.