-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcompute_masks.py
51 lines (38 loc) · 1.32 KB
/
compute_masks.py
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
# coding=utf-8
# Copyright (c) DIRECT Contributors
import argparse
import pathlib
import h5py
import numpy as np
from tqdm import tqdm
def extract_mask(filename):
"""
Extract the mask from masked k-space data, these are not explicitly given.
Parameters
----------
filename : pathlib.Path
Returns
-------
np.ndarray
"""
with h5py.File(filename, "r") as f:
kspace = f["kspace"]
size = kspace.shape[0]
out = np.abs(kspace[0])
for idx in range(1, size):
out += np.abs(kspace[idx])
sampling_mask = ~(np.abs(out).sum(axis=-1) == 0)
return sampling_mask
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("testing_root", type=pathlib.Path, help="Path to the testing set.")
parser.add_argument("output_directory", type=pathlib.Path, help="Path to the DoIterationOutput directory.")
args = parser.parse_args()
# Find all h5 files in the testing root
testing_files = list(args.testing_root.glob("*.h5"))
print(f"Found {len(testing_files)} files in {args.testing_root}.")
print("Computing kspace masks...")
for testing_file in tqdm(testing_files):
mask = extract_mask(testing_file)
np.save(args.output_directory / (testing_file.stem + ".npy"), mask)
print("Computed masks.")