-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix DPOSPath.save_numpy, DPH5Path.is_file, DPH5Path.is_dir (#3631)
Signed-off-by: Jinzhe Zeng <[email protected]>
- Loading branch information
Showing
2 changed files
with
69 additions
and
3 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,53 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
import tempfile | ||
import unittest | ||
from pathlib import ( | ||
Path, | ||
) | ||
|
||
import h5py | ||
import numpy as np | ||
|
||
from deepmd.utils.path import ( | ||
DPPath, | ||
) | ||
|
||
|
||
class PathTest: | ||
path: DPPath | ||
|
||
def test_numpy(self): | ||
numpy_path = self.path / "testcase" | ||
arr1 = np.ones(3) | ||
self.assertFalse(numpy_path.is_file()) | ||
numpy_path.save_numpy(arr1) | ||
self.assertTrue(numpy_path.is_file()) | ||
arr2 = numpy_path.load_numpy() | ||
np.testing.assert_array_equal(arr1, arr2) | ||
|
||
def test_dir(self): | ||
dir_path = self.path / "testcase" | ||
self.assertFalse(dir_path.is_dir()) | ||
dir_path.mkdir() | ||
self.assertTrue(dir_path.is_dir()) | ||
|
||
|
||
class TestOSPath(PathTest, unittest.TestCase): | ||
def setUp(self): | ||
self.tempdir = tempfile.TemporaryDirectory() | ||
self.path = DPPath(self.tempdir.name, "a") | ||
|
||
def tearDown(self): | ||
self.tempdir.cleanup() | ||
|
||
|
||
class TestH5Path(PathTest, unittest.TestCase): | ||
def setUp(self): | ||
self.tempdir = tempfile.TemporaryDirectory() | ||
h5file = str((Path(self.tempdir.name) / "testcase.h5").resolve()) | ||
with h5py.File(h5file, "w") as f: | ||
pass | ||
self.path = DPPath(h5file, "a") | ||
|
||
def tearDown(self): | ||
self.tempdir.cleanup() |