Skip to content

Commit

Permalink
copy: add skip option
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus committed Mar 4, 2022
1 parent 6478ffb commit 7e7a7ff
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
8 changes: 7 additions & 1 deletion GooseHDF5/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ def copy(
dest_datasets: list[str] = None,
root: str = None,
recursive: bool = True,
skip: bool = False,
):
"""
Copy groups/datasets from one HDF5-archive ``source`` to another HDF5-archive ``dest``.
Expand All @@ -577,6 +578,7 @@ def copy(
:param dest_datasets: List of dataset-paths in ``dest``, defaults to ``source_datasets``.
:param root: Path prefix for all ``dest_datasets``.
:param recursive: If the source is a group, copy all objects within that group recursively.
:param skip: Skip datasets that are not present in source.
"""

if len(source_datasets) == 0:
Expand All @@ -589,6 +591,11 @@ def copy(

dest_datasets = np.array(dest_datasets)

if skip:
keep = [i in source for i in source_datasets]
source_datasets = source_datasets[keep]
dest_datasets = dest_datasets[keep]

if root:
dest_datasets = np.array([join(root, path, root=True) for path in dest_datasets])

Expand Down Expand Up @@ -838,7 +845,6 @@ def compare(
b: Union[str, h5py.File],
paths_a: list[str] = None,
paths_b: list[str] = None,
root: str = None,
attrs: bool = True,
matching_dtype: bool = False,
):
Expand Down
69 changes: 31 additions & 38 deletions tests/copying.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@
import GooseHDF5 as g5


dirname = os.path.join(os.path.abspath(os.path.dirname(__file__)), "mytest")


class Test_itereator(unittest.TestCase):
def test_copy_plain(self):
""" """

dirname = "mytest"
sourcepath = os.path.join(dirname, "foo_1.h5")
destpath = os.path.join(dirname, "bar_1.h5")
@classmethod
def setUpClass(self):

if not os.path.isdir(dirname):
os.makedirs(dirname)

@classmethod
def tearDownClass(self):

shutil.rmtree(dirname)

def test_copy_plain(self):

sourcepath = os.path.join(dirname, "foo_1.h5")
destpath = os.path.join(dirname, "bar_1.h5")
datasets = ["/a", "/b/foo", "/c/d/foo"]

with h5py.File(sourcepath, "w") as source:

with h5py.File(destpath, "w") as dest:

for d in datasets:
Expand All @@ -32,21 +42,30 @@ def test_copy_plain(self):
for path in datasets:
self.assertTrue(g5.equal(source, dest, path))

shutil.rmtree(dirname)

def test_copy_nonrecursive(self):
def test_copy_skip(self):

dirname = "mytest"
sourcepath = os.path.join(dirname, "foo_1.h5")
destpath = os.path.join(dirname, "bar_1.h5")
datasets = ["/a", "/b/foo", "/c/d/foo"]

if not os.path.isdir(dirname):
os.makedirs(dirname)
with h5py.File(sourcepath, "w") as source:
with h5py.File(destpath, "w") as dest:

for d in datasets:
source[d] = np.random.rand(10)

g5.copy(source, dest, datasets + ["/nonexisting"], skip=True)

for path in datasets:
self.assertTrue(g5.equal(source, dest, path))

def test_copy_nonrecursive(self):

sourcepath = os.path.join(dirname, "foo_1.h5")
destpath = os.path.join(dirname, "bar_1.h5")
datasets = ["/a", "/b/foo", "/b/bar", "/c/d/foo"]

with h5py.File(sourcepath, "w") as source:

with h5py.File(destpath, "w") as dest:

for d in datasets:
Expand All @@ -61,21 +80,13 @@ def test_copy_nonrecursive(self):
self.assertFalse(g5.exists(dest, "/b/foo"))
self.assertFalse(g5.exists(dest, "/b/bar"))

shutil.rmtree(dirname)

def test_copy_recursive(self):

dirname = "mytest"
sourcepath = os.path.join(dirname, "foo_1.h5")
destpath = os.path.join(dirname, "bar_1.h5")

if not os.path.isdir(dirname):
os.makedirs(dirname)

datasets = ["/a", "/b/foo", "/b/bar", "/c/d/foo"]

with h5py.File(sourcepath, "w") as source:

with h5py.File(destpath, "w") as dest:

for d in datasets:
Expand All @@ -86,21 +97,13 @@ def test_copy_recursive(self):
for path in datasets:
self.assertTrue(g5.equal(source, dest, path))

shutil.rmtree(dirname)

def test_copy_attrs(self):

dirname = "mytest"
sourcepath = os.path.join(dirname, "foo_2.h5")
destpath = os.path.join(dirname, "bar_2.h5")

if not os.path.isdir(dirname):
os.makedirs(dirname)

datasets = ["/a", "/b/foo", "/c/d/foo"]

with h5py.File(sourcepath, "w") as source:

with h5py.File(destpath, "w") as dest:

for d in datasets:
Expand All @@ -115,21 +118,13 @@ def test_copy_attrs(self):
for path in datasets:
self.assertTrue(g5.equal(source, dest, path))

shutil.rmtree(dirname)

def test_copy_groupattrs(self):

dirname = "mytest"
sourcepath = os.path.join(dirname, "foo_3.h5")
destpath = os.path.join(dirname, "bar_3.h5")

if not os.path.isdir(dirname):
os.makedirs(dirname)

datasets = ["/a", "/b/foo", "/c/d/foo"]

with h5py.File(sourcepath, "w") as source:

with h5py.File(destpath, "w") as dest:

for d in datasets:
Expand All @@ -143,8 +138,6 @@ def test_copy_groupattrs(self):
for path in datasets:
self.assertTrue(g5.equal(source, dest, path))

shutil.rmtree(dirname)


if __name__ == "__main__":

Expand Down

0 comments on commit 7e7a7ff

Please sign in to comment.