Skip to content

Commit

Permalink
Add black formatting and isort
Browse files Browse the repository at this point in the history
  • Loading branch information
chiiyeh committed Oct 25, 2023
1 parent 99001b5 commit 7f32e01
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
25 changes: 19 additions & 6 deletions lhotse/features/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ def write(self, key: str, value: np.ndarray) -> str:
subdir = self.storage_path_ / key[:3]
subdir.mkdir(exist_ok=True)
p = subdir / key
output_features_path = p.with_suffix(p.suffix + ".llc" if p.suffix != ".llc" else ".llc")
output_features_path = p.with_suffix(
p.suffix + ".llc" if p.suffix != ".llc" else ".llc"
)
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 @@ -345,7 +347,9 @@ def write(self, key: str, value: np.ndarray) -> str:
subdir = self.storage_path_ / key[:3]
subdir.mkdir(exist_ok=True)
p = subdir / key
output_features_path = p.with_suffix(p.suffix + ".npy" if p.suffix != ".npy" else ".npy")
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 @@ -451,8 +455,11 @@ def __init__(self, storage_path: Pathlike, mode: str = "w", *args, **kwargs):
super().__init__()
check_h5py_installed()
import h5py

p = Path(storage_path)
self.storage_path_ = p.with_suffix(p.suffix + ".h5" if p.suffix != ".h5" else ".h5")
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 @@ -542,7 +549,9 @@ def __init__(
import h5py

p = Path(storage_path)
self.storage_path_ = p.with_suffix(p.suffix + ".h5" if p.suffix != ".h5" else ".h5")
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 @@ -668,7 +677,9 @@ def __init__(
import h5py

p = Path(storage_path)
self.storage_path_ = p.with_suffix(p.suffix + ".h5" if p.suffix != ".h5" else ".h5")
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 @@ -831,7 +842,9 @@ def __init__(

# ".lca" -> "lilcom chunky archive"
p = Path(storage_path)
self.storage_path_ = p.with_suffix(p.suffix + ".lca" if p.suffix != ".lca" else ".lca")
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
28 changes: 13 additions & 15 deletions test/features/test_feature_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@
from pathlib import Path

from lhotse import (
LilcomFilesWriter,
ChunkedLilcomHdf5Writer,
LilcomChunkyWriter,
LilcomFilesWriter,
LilcomHdf5Writer,
NumpyFilesWriter,
NumpyHdf5Writer,
LilcomHdf5Writer,
ChunkedLilcomHdf5Writer,
LilcomChunkyWriter,
)
)
from lhotse.utils import is_module_available


@pytest.mark.parametrize(
["writer_type", "ext"],
[
(LilcomFilesWriter, '.llc'),
(NumpyFilesWriter, '.npy'),
(LilcomFilesWriter, ".llc"),
(NumpyFilesWriter, ".npy"),
],
)
def test_writer_saved_file(writer_type, ext):
# Generate small random numbers that are nicely compressed with lilcom
arr = np.log(np.random.uniform(size=(11, 80)).astype(np.float32) / 100)

with TemporaryDirectory() as d, writer_type(d) as writer:

#testing that words after . is not replace
# testing that words after . is not replace
input_key = "random0.3_vad.alpha"
key = writer.write(input_key, arr)
assert key == f"ran/{input_key}{ext}"

#Testing when end with extension it is not added again
# Testing when end with extension it is not added again
input_key = f"temp0.2.alpha{ext}"
key = writer.write(input_key, arr)
assert key == f"tem/{input_key}"


@pytest.mark.parametrize(
["writer_type", "ext"],
[
Expand Down Expand Up @@ -65,19 +65,17 @@ def test_writer_saved_file(writer_type, ext):
reason="Requires h5py to run HDF5 tests.",
),
),
(LilcomChunkyWriter, ".lca")
(LilcomChunkyWriter, ".lca"),
],
)
def test_chunk_writer_saved_file(writer_type, ext):

with TemporaryDirectory() as d:

#testing that words after . is not replace
# testing that words after . is not replace
filename = "random0.3_vad.alpha"
with writer_type(f"{d}/{filename}") as writer:
assert writer.storage_path_ == Path(f"{d}/{filename}{ext}")

#Testing when end with extension it is not added again
# Testing when end with extension it is not added again
filename = f"random0.3_vad.alpha{ext}"
with writer_type(f"{d}/{filename}") as writer:
assert writer.storage_path_ == Path(f"{d}/{filename}")

0 comments on commit 7f32e01

Please sign in to comment.