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

fix: fix DPOSPath.save_numpy, DPH5Path.is_file, DPH5Path.is_dir #3631

Merged
merged 1 commit into from
Apr 1, 2024
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
19 changes: 16 additions & 3 deletions deepmd/utils/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
Path,
)
from typing import (
ClassVar,
Dict,
List,
Optional,
)
Expand Down Expand Up @@ -200,7 +202,8 @@
"""
if self.mode == "r":
raise ValueError("Cannot save to read-only path")
np.save(str(self.path), arr)
with self.path.open("wb") as f:
np.save(f, arr)

Check warning on line 206 in deepmd/utils/path.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/path.py#L205-L206

Added lines #L205 - L206 were not covered by tests

def glob(self, pattern: str) -> List["DPPath"]:
"""Search path using the glob pattern.
Expand Down Expand Up @@ -354,6 +357,7 @@
del self.root[self._name]
self.root.create_dataset(self._name, data=arr)
self.root.flush()
self._new_keys.append(self._name)

Check warning on line 360 in deepmd/utils/path.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/path.py#L360

Added line #L360 was not covered by tests

def glob(self, pattern: str) -> List["DPPath"]:
"""Search path using the glob pattern.
Expand Down Expand Up @@ -396,6 +400,14 @@
"""Walk all groups and dataset."""
return self._file_keys(self.root)

__file_new_keys: ClassVar[Dict[h5py.File, List[str]]] = {}

@property
def _new_keys(self):
"""New keys that haven't been cached."""
self.__file_new_keys.setdefault(self.root, [])
return self.__file_new_keys[self.root]

Check warning on line 409 in deepmd/utils/path.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/path.py#L408-L409

Added lines #L408 - L409 were not covered by tests

@classmethod
@lru_cache(None)
def _file_keys(cls, file: h5py.File) -> List[str]:
Expand All @@ -406,15 +418,15 @@

def is_file(self) -> bool:
"""Check if self is file."""
if self._name not in self._keys:
if self._name not in self._keys and self._name not in self._new_keys:

Check warning on line 421 in deepmd/utils/path.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/path.py#L421

Added line #L421 was not covered by tests
return False
return isinstance(self.root[self._name], h5py.Dataset)

def is_dir(self) -> bool:
"""Check if self is directory."""
if self._name == "/":
return True
if self._name not in self._keys:
if self._name not in self._keys and self._name not in self._new_keys:

Check warning on line 429 in deepmd/utils/path.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/path.py#L429

Added line #L429 was not covered by tests
return False
return isinstance(self.root[self._name], h5py.Group)

Expand Down Expand Up @@ -461,3 +473,4 @@
self.root.require_group(self._name)
else:
self.root.create_group(self._name)
self._new_keys.append(self._name)

Check warning on line 476 in deepmd/utils/path.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/path.py#L476

Added line #L476 was not covered by tests
53 changes: 53 additions & 0 deletions source/tests/common/test_path.py
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()