-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecksum.py
More file actions
67 lines (55 loc) · 1.95 KB
/
Copy pathchecksum.py
File metadata and controls
67 lines (55 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! /usr/bin/env python
# Time-stamp: <2025-12-11 m.utrosa@bcbl.eu>
import hashlib, os
import nibabel as nib
def sha256_checksum(filename, block_size=65536):
'''
Generate SHA-256 checksum for a file for secure data validation.
Checksum is a string that is used to verify file downloads/uploads and
detect accidental/malicious changes (integrity check).
Returns None on failure.
'''
sha = hashlib.sha256()
try:
with open(filename, 'rb') as f:
for block in iter(lambda: f.read(block_size), b''):
sha.update(block)
return sha.hexdigest()
except Exception:
return None
def validate_nifti(filepath):
'''
Try loading and reading a NIfTI file using nibabel.
'''
try:
img = nib.load(filepath)
_ = img.get_fdata()
return True, None
except Exception as e:
return False, str(e)
def check_dataset(root_dir, compute_hash=True):
corrupted_files = []
for root, _, files in os.walk(root_dir):
for fname in files:
path = os.path.join(root, fname)
# Only validate NIfTI images for corruption detection
if fname.endswith(('.nii', '.nii.gz')):
valid, error = validate_nifti(path)
if not valid:
corrupted_files.append((path, error))
# Optional checksum for *all* files
if compute_hash:
checksum = sha256_checksum(path)
if checksum is None:
corrupted_files.append((path, "Checksum read error"))
return corrupted_files
# Example usage
if __name__ == "__main__":
homePath = "/home/mutrosa/Documents/projects/select_fMRI/data_MRI"
corrupted = check_dataset(homePath)
if not corrupted:
print("No corrupted files detected.")
else:
print("\nCorrupted or unreadable files found:")
for f, err in corrupted:
print(f" - {f}\n Error: {err}")