Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 12 additions & 12 deletions trx/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def md5sum(filename):
Hexadecimal MD5 digest.
"""
h = hashlib.md5()
with open(filename, "rb") as f:
for chunk in iter(lambda: f.read(128 * h.block_size), b""):
with open(filename, "rb") as file:
for chunk in iter(lambda: file.read(128 * h.block_size), b""):
h.update(chunk)
return h.hexdigest()

Expand All @@ -104,8 +104,8 @@ def sha256sum(filename):
Hexadecimal SHA256 digest.
"""
h = hashlib.sha256()
with open(filename, "rb") as f:
for chunk in iter(lambda: f.read(128 * h.block_size), b""):
with open(filename, "rb") as file:
for chunk in iter(lambda: file.read(128 * h.block_size), b""):
h.update(chunk)
return h.hexdigest()

Expand Down Expand Up @@ -140,34 +140,34 @@ def fetch_data(files_dict, keys=None): # noqa: C901
elif isinstance(keys, str):
keys = [keys]

for f in keys:
file_entry = files_dict[f]
for fname in keys:
file_entry = files_dict[fname]
if len(file_entry) == 2:
url, expected_md5 = file_entry
expected_sha = None
else:
url, expected_md5, expected_sha = file_entry
full_path = os.path.join(trx_home, f)
full_path = os.path.join(trx_home, fname)

logging.info("Downloading {} to {}".format(f, trx_home))
logging.info(f"Downloading {fname} to {trx_home}")
if not os.path.exists(full_path):
urllib.request.urlretrieve(url, full_path)

actual_md5 = md5sum(full_path)
if expected_md5 != actual_md5:
raise ValueError(
f"Md5sum for {f} does not match. "
f"Md5sum for {fname} does not match. "
"Please remove the file to download it again: " + full_path
)

if expected_sha is not None:
actual_sha = sha256sum(full_path)
if expected_sha != actual_sha:
raise ValueError(
f"SHA256 for {f} does not match. "
f"SHA256 for {fname} does not match. "
"Please remove the file to download it again: " + full_path
)

if f.endswith(".zip"):
dst_dir = os.path.join(trx_home, f[:-4])
if fname.endswith(".zip"):
dst_dir = os.path.join(trx_home, fname[:-4])
shutil.unpack_archive(full_path, extract_dir=dst_dir, format="zip")
10 changes: 3 additions & 7 deletions trx/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,16 @@ def load_sft_with_reference(filepath, reference=None, bbox_check=True):
_, ext = os.path.splitext(filepath)
if ext == ".trk":
if reference is not None and reference != "same":
logging.warning(
"Reference is discarded for this file format {}.".format(filepath)
)
logging.warning(f"Reference is discarded for this file format {filepath}.")
sft = load_tractogram(filepath, "same", bbox_valid_check=bbox_check)
elif ext in [".tck", ".fib", ".vtk", ".dpy"]:
if reference is None or reference == "same":
raise IOError(
"--reference is required for this file format {}.".format(filepath)
)
raise IOError(f"--reference is required for this file format {filepath}.")
else:
sft = load_tractogram(filepath, reference, bbox_valid_check=bbox_check)

else:
raise IOError("{} is an unsupported file format".format(filepath))
raise IOError(f"{filepath} is an unsupported file format")

return sft

Expand Down
2 changes: 1 addition & 1 deletion trx/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_multi_load_save_rasmm(path):

obj = load(path, os.path.join(gs_dir, "gs.nii"))
for i in range(3):
out_path = os.path.join(tmp_gs_dir, "{}_tmp{}_{}".format(basename, i, ext))
out_path = os.path.join(tmp_gs_dir, f"{basename}_tmp{i}_{ext}")
save(obj, out_path)

if isinstance(obj, TrxFile):
Expand Down
Loading
Loading