Skip to content

Instantly share code, notes, and snippets.

@valosekj
Last active May 10, 2023 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valosekj/e22dd1200650c1eed3ee4e1266022eed to your computer and use it in GitHub Desktop.
Save valosekj/e22dd1200650c1eed3ee4e1266022eed to your computer and use it in GitHub Desktop.
import os
import glob
import hashlib
import argparse
def compute_checksum(file_path):
with open(file_path, 'rb') as f:
file_bytes = f.read()
return hashlib.md5(file_bytes).hexdigest()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Check if image in folder1 is presented in folder2 '
'(sct-testing-large).')
parser.add_argument('-folder1', type=str, help='path to folder 1 with NIfTI images')
parser.add_argument('-folder2', type=str, help='path to folder 2 with NIfTI images (sct-testing-large)')
args = parser.parse_args()
if not args.folder1 or not args.folder2:
parser.print_help()
exit(1)
# Get all .nii.gz files without seg.nii.gz suffix in the input folder 1
files1 = [os.path.abspath(f) for f in glob.glob(args.folder1 + '/*.nii.gz') if f.endswith('.nii.gz') and not f.endswith('seg.nii.gz')]
# Get all .nii.gz files in the input folder 2. This is a recursive search because sct-testing-large is BIDS
files2 = [os.path.abspath(f) for f in glob.glob(args.folder2 + '/**/*.nii.gz', recursive=True) if 'anat' in f]
# Loop across files in folder1
for file1 in files1:
print(file1)
checksum1 = compute_checksum(file1)
# Loop across files in folder2
for file2 in files2:
checksum2 = compute_checksum(file2)
if checksum1 == checksum2:
print(f"{file1} and {file2} are identical.")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment