Skip to content

Commit

Permalink
fix: support both nt and posix relative paths in basins
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Apr 20, 2024
1 parent 08df6b1 commit dee0296
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
0.58.4
- fix: support relative paths in basins with both nt `\\` and
posix `/` path separators
- enh: store original metadata in .tsv export (#255)
0.58.3
- fix: keep only 1000 contours in LazyContourList, because keeping
Expand Down
14 changes: 13 additions & 1 deletion dclab/rtdc_dataset/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""RT-DC dataset core classes and methods"""
import abc
import hashlib
import os.path
import pathlib
from typing import Literal
import uuid
Expand Down Expand Up @@ -787,7 +788,18 @@ def basins_retrieve(self):
f"'{self.format}'")
# stop processing this basin
continue
for pp in bdict["paths"]:
p_paths = list(bdict["paths"])
# translate Windows and Unix relative paths
for pi in list(p_paths): # [sic] create a copy of the list
if pi.count(".."):
if pi[2:].count("/") and os.path.sep == r"\\":
# Windows
p_paths.append(pi.replace("/", r"\\"))
elif pi[2:].count(r"\\") and os.path.sep == "/":
# Unix
p_paths.append(pi.replace(r"\\", "/"))
# perform the actual check
for pp in p_paths:
pp = pathlib.Path(pp)
# Instantiate the proper basin class
b_cls = bc[bdict["format"]]
Expand Down
41 changes: 38 additions & 3 deletions tests/test_rtdc_feat_basin.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_basin_hierarchy_trace():
}
blines = json.dumps(bdat, indent=2).split("\n")
basins = hw.h5file.require_group("basins")
hw.write_text(basins, "invalid_format_basin", blines)
hw.write_text(basins, "trace_basin", blines)

ds = dclab.new_dataset(h5path_small)
assert "trace" not in ds.features_innate
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_basin_hierarchy_trace_missing():
}
blines = json.dumps(bdat, indent=2).split("\n")
basins = hw.h5file.require_group("basins")
hw.write_text(basins, "invalid_format_basin", blines)
hw.write_text(basins, "trace missing", blines)

h5path.unlink()

Expand Down Expand Up @@ -128,7 +128,7 @@ def test_basin_not_allowed_to_have_local_basin_in_remote_basin():

# Because wo do not have the resources to upload anything and access
# it via HTTP to test this, we simply modify the `_local_basins_allowed`
# property of an `RTDC_HDF5` instance. This is a hacky workaround but
# property of an `RTDC_HDF5` instance. This is a hacky workaround, but
# it should be fine.

h5path = retrieve_data("fmt-hdf5_fl_wide-channel_2023.zip")
Expand Down Expand Up @@ -163,9 +163,44 @@ def test_basin_not_allowed_to_have_local_basin_in_remote_basin():
with pytest.warns(
UserWarning,
match="Basin type 'file' not allowed for format 'hdf5"):
# Read the comments above carefully, if you wonder why this is.
assert "image" not in ds2, "not there, local basins are forbidden"


@pytest.mark.filterwarnings(
"ignore::dclab.rtdc_dataset.config.WrongConfigurationTypeWarning")
@pytest.mark.parametrize("path_sep", [r"/", r"\\"])
def test_basin_relative_paths(path_sep):
"""Relative paths should work for both `\\` and ¸`/`."""
h5path = retrieve_data("fmt-hdf5_fl_wide-channel_2023.zip")
h5path_small = h5path.parent / "subdirectory" / "relative.rtdc"
h5path_small.parent.mkdir()

with dclab.new_dataset(h5path) as ds0:
assert "image" in ds0

# Dataset creation
with h5py.File(h5path) as src, RTDCWriter(h5path_small) as hw:
# first, copy all the scalar features to the new file
rtdc_dataset.rtdc_copy(src_h5file=src,
dst_h5file=hw.h5file,
features="scalar")
# Next, store the basin information in the new dataset
bdat = {
"type": "file",
"format": "hdf5",
"features": ["image"],
"paths": [f"..{path_sep}{h5path.name}"]
}
blines = json.dumps(bdat, indent=2).split("\n")
basins = hw.h5file.require_group("basins")
hw.write_text(basins, "relative_paths_basin", blines)

with dclab.new_dataset(h5path_small) as ds:
assert "image" not in ds.features_innate
assert "image" in ds


def test_basin_sorting_basic():
bnlist = [
{"type": "remote", "format": "dcor", "ident": 0},
Expand Down

0 comments on commit dee0296

Please sign in to comment.