-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* caching parsed output files * switch to absolute paths * add tests for caching * rename cache clearing method to avoid clashes * fix lint
- Loading branch information
Showing
9 changed files
with
185 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import unittest | ||
|
||
from custodian.utils import tracked_lru_cache | ||
|
||
|
||
class TrackedLruCacheTest(unittest.TestCase): | ||
def setUp(self): | ||
# clear cache before and after each test to avoid | ||
# unexpected caching from other tests | ||
tracked_lru_cache.tracked_cache_clear() | ||
|
||
def test_cache_and_clear(self): | ||
n_calls = 0 | ||
|
||
@tracked_lru_cache | ||
def some_func(x): | ||
nonlocal n_calls | ||
n_calls += 1 | ||
return x | ||
|
||
assert some_func(1) == 1 | ||
assert n_calls == 1 | ||
assert some_func(2) == 2 | ||
assert n_calls == 2 | ||
assert some_func(1) == 1 | ||
assert n_calls == 2 | ||
|
||
assert len(tracked_lru_cache.cached_functions) == 1 | ||
|
||
tracked_lru_cache.tracked_cache_clear() | ||
|
||
assert len(tracked_lru_cache.cached_functions) == 0 | ||
|
||
assert some_func(1) == 1 | ||
assert n_calls == 3 | ||
|
||
def tearDown(self): | ||
tracked_lru_cache.tracked_cache_clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
Helper functions for dealing with vasp files. | ||
""" | ||
|
||
from pymatgen.io.vasp.outputs import Outcar, Vasprun | ||
|
||
from custodian.utils import tracked_lru_cache | ||
|
||
|
||
@tracked_lru_cache | ||
def load_vasprun(filepath, **vasprun_kwargs): | ||
""" | ||
Load Vasprun object from file path. | ||
Caches the output for reuse. | ||
Args: | ||
filepath: path to the vasprun.xml file. | ||
**vasprun_kwargs: kwargs arguments passed to the Vasprun init. | ||
Returns: | ||
The Vasprun object | ||
""" | ||
return Vasprun(filepath, **vasprun_kwargs) | ||
|
||
|
||
@tracked_lru_cache | ||
def load_outcar(filepath): | ||
""" | ||
Load Outcar object from file path. | ||
Caches the output for reuse. | ||
Args: | ||
filepath: path to the OUTCAR file. | ||
Returns: | ||
The Vasprun object | ||
""" | ||
return Outcar(filepath) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import os | ||
import unittest | ||
|
||
from custodian.utils import tracked_lru_cache | ||
from custodian.vasp.io import load_outcar, load_vasprun | ||
|
||
test_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files") | ||
|
||
|
||
class IOTest(unittest.TestCase): | ||
def test_load_outcar(self): | ||
outcar = load_outcar(os.path.join(test_dir, "large_sigma", "OUTCAR")) | ||
assert outcar is not None | ||
outcar2 = load_outcar(os.path.join(test_dir, "large_sigma", "OUTCAR")) | ||
|
||
assert outcar is outcar2 | ||
|
||
assert len(tracked_lru_cache.cached_functions) == 1 | ||
|
||
def test_load_vasprun(self): | ||
vr = load_vasprun(os.path.join(test_dir, "large_sigma", "vasprun.xml")) | ||
assert vr is not None | ||
vr2 = load_vasprun(os.path.join(test_dir, "large_sigma", "vasprun.xml")) | ||
|
||
assert vr is vr2 | ||
|
||
assert len(tracked_lru_cache.cached_functions) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters