-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add decompression preprocessing step to
TotalSegmentator2D
for more…
… efficient slice loading (#705)
- Loading branch information
Showing
11 changed files
with
206 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
"""Core I/O utilities.""" | ||
|
||
from eva.core.utils.io.dataframe import read_dataframe | ||
from eva.core.utils.io.gz import gunzip_file | ||
|
||
__all__ = ["read_dataframe"] | ||
__all__ = ["read_dataframe", "gunzip_file"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Utils for .gz files.""" | ||
|
||
import gzip | ||
import os | ||
|
||
|
||
def gunzip_file(path: str, unpack_dir: str | None = None, keep: bool = True) -> str: | ||
"""Unpacks a .gz file to the provided directory. | ||
Args: | ||
path: Path to the .gz file to extract. | ||
unpack_dir: Directory to extract the file to. If `None`, it will use the | ||
same directory as the compressed file. | ||
keep: Whether to keep the compressed .gz file. | ||
Returns: | ||
The path to the extracted file. | ||
""" | ||
unpack_dir = unpack_dir or os.path.dirname(path) | ||
os.makedirs(unpack_dir, exist_ok=True) | ||
save_path = os.path.join(unpack_dir, os.path.basename(path).replace(".gz", "")) | ||
if not os.path.isfile(save_path): | ||
with gzip.open(path, "rb") as f_in: | ||
with open(save_path, "wb") as f_out: | ||
f_out.write(f_in.read()) | ||
if not keep: | ||
os.remove(path) | ||
return save_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Tests the core io utilities.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Tests for .gz file utilities.""" | ||
|
||
import os | ||
import shutil | ||
|
||
import pytest | ||
|
||
from eva.core.utils.io import gz | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"subdir, keep", | ||
[ | ||
(None, True), | ||
("test_subdir", True), | ||
(None, False), | ||
], | ||
) | ||
def test_gunzip(tmp_path: str, gzip_file: str, subdir: str | None, keep: bool) -> None: | ||
"""Verifies proper extraction of gzip file contents.""" | ||
unpack_dir = os.path.join(tmp_path, subdir) if subdir else tmp_path | ||
tmp_gzip_path = os.path.join(tmp_path, os.path.basename(gzip_file)) | ||
shutil.copy(gzip_file, tmp_gzip_path) | ||
gz.gunzip_file(tmp_gzip_path, unpack_dir=unpack_dir, keep=keep) | ||
|
||
uncompressed_path = os.path.join(unpack_dir, "test.txt") | ||
assert os.path.isfile(uncompressed_path) | ||
with open(uncompressed_path, "r") as f: | ||
assert f.read() == "gz file test" | ||
|
||
if keep: | ||
assert os.path.isfile(tmp_gzip_path) | ||
else: | ||
assert not os.path.isfile(tmp_gzip_path) | ||
|
||
|
||
@pytest.fixture() | ||
def gzip_file(assets_path: str) -> str: | ||
"""Provides the path to the test gzip file asset.""" | ||
return os.path.join(assets_path, "core/archives/test.txt.gz") |
Oops, something went wrong.