Debian Codegraph @

docker/validate_results.py

raw
1 import os
2 import argparse
3 import h5py
4 import numpy as np
5 from pathlib import Path
6
7 def test_basic_recon(reference_filename, testdata_filename):
8 reference_data = h5py.File(reference_filename)
9 input_data = h5py.File(testdata_filename)
10 coil_images = reference_data['dataset']['coil_images']
11 coil_images = np.squeeze(np.array(coil_images['real']) + 1j*np.array(coil_images['imag']))
12 recon_reference = np.abs(np.sqrt(np.sum(coil_images * np.conj(coil_images), 0)))
13 ro_length = recon_reference.shape[-1]
14 recon_reference = recon_reference[:, int(ro_length/4):int(ro_length/4+ro_length/2)]
15
16 # Take care of the different data types
17 if (len(input_data['dataset']['image_0']['data'].dtype)) == 2:
18 reconstruction = np.squeeze(np.array(input_data['dataset']['image_0']['data']['real']) + 1j*np.array(input_data['dataset']['image_0']['data']['imag']))
19 else:
20 reconstruction = np.squeeze(np.array(input_data['dataset']['image_0']['data']))
21
22 # We need to scale the images, since the recon does not
23 reconstruction = np.abs(reconstruction) / np.sqrt(coil_images.shape[-1]*coil_images.shape[-2])
24
25 assert np.linalg.norm(reconstruction - recon_reference) / np.linalg.norm(recon_reference) < 2e-5
26
27 if __name__ == '__main__':
28 parser = argparse.ArgumentParser(description='Validate results of an ismrmrd reconstruction')
29 parser.add_argument('--reference', type=str, help='Reference file', required=True)
30 parser.add_argument('--testdata', type=str, help='Test data file', required=True)
31 args = parser.parse_args()
32 test_basic_recon(args.reference, args.testdata)