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

Prevent accidental renaming when using with_suffix #1192

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Changes from 1 commit
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: 12 additions & 7 deletions lhotse/features/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ def write(self, key: str, value: np.ndarray) -> str:
# too many files in a single directory.
subdir = self.storage_path_ / key[:3]
subdir.mkdir(exist_ok=True)
output_features_path = (subdir / key).with_suffix(".llc")
p = subdir / key
output_features_path = p.with_suffix(p.suffix + ".llc" if p.suffix != ".llc" else ".llc")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the if/else needed here? (Same for all below changes)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if/else is to prevent adding of suffix if p contains the suffix for some reason say "test.llc". So it will not become "test.llc.llc".
Which is the expected behavior mentioned here
https://github.com/lhotse-speech/lhotse/blob/master/lhotse/features/io.py#L443

serialized_feats = lilcom.compress(value, tick_power=self.tick_power)
with open(output_features_path, "wb") as f:
f.write(serialized_feats)
Expand Down Expand Up @@ -343,7 +344,8 @@ def write(self, key: str, value: np.ndarray) -> str:
# too many files in a single directory.
subdir = self.storage_path_ / key[:3]
subdir.mkdir(exist_ok=True)
output_features_path = (subdir / key).with_suffix(".npy")
p = subdir / key
output_features_path = p.with_suffix(p.suffix + ".npy" if p.suffix != ".npy" else ".npy")
np.save(output_features_path, value, allow_pickle=False)
# Include sub-directory in the key, e.g. "abc/abcdef.npy"
return "/".join(output_features_path.parts[-2:])
Expand Down Expand Up @@ -449,8 +451,8 @@ def __init__(self, storage_path: Pathlike, mode: str = "w", *args, **kwargs):
super().__init__()
check_h5py_installed()
import h5py

self.storage_path_ = Path(storage_path).with_suffix(".h5")
p = Path(storage_path)
self.storage_path_ = p.with_suffix(p.suffix + ".h5" if p.suffix != ".h5" else ".h5")
self.hdf = h5py.File(self.storage_path, mode=mode)

@property
Expand Down Expand Up @@ -539,7 +541,8 @@ def __init__(
check_h5py_installed()
import h5py

self.storage_path_ = Path(storage_path).with_suffix(".h5")
p = Path(storage_path)
self.storage_path_ = p.with_suffix(p.suffix + ".h5" if p.suffix != ".h5" else ".h5")
self.hdf = h5py.File(self.storage_path, mode=mode)
self.tick_power = tick_power

Expand Down Expand Up @@ -664,7 +667,8 @@ def __init__(
check_h5py_installed()
import h5py

self.storage_path_ = Path(storage_path).with_suffix(".h5")
p = Path(storage_path)
self.storage_path_ = p.with_suffix(p.suffix + ".h5" if p.suffix != ".h5" else ".h5")
self.tick_power = tick_power
self.chunk_size = chunk_size
self.hdf = h5py.File(self.storage_path, mode=mode)
Expand Down Expand Up @@ -826,7 +830,8 @@ def __init__(
assert mode in ("wb", "ab")

# ".lca" -> "lilcom chunky archive"
self.storage_path_ = Path(storage_path).with_suffix(".lca")
p = Path(storage_path)
self.storage_path_ = p.with_suffix(p.suffix + ".lca" if p.suffix != ".lca" else ".lca")
self.tick_power = tick_power
self.file = open(self.storage_path, mode=mode)
self.curr_offset = self.file.tell()
Expand Down