ho11laqe commited on
Commit
64a8f56
1 Parent(s): d77453d

task conversion

Browse files
nnunet/dataset_conversion/Task500_Glacier_inference.py CHANGED
@@ -1,11 +1,94 @@
1
- from batchgenerators.utilities.file_and_folder_operations import *
2
  from nnunet.utilities.file_conversions import convert_2d_image_to_nifti
3
  from nnunet.paths import nnUNet_raw_data, preprocessing_output_dir
4
  from nnunet.dataset_conversion.utils import generate_dataset_json
5
 
6
  import argparse
7
 
 
 
 
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  if __name__ == '__main__':
10
  parser = argparse.ArgumentParser()
11
  parser.add_argument("-data_percentage", default=100,
 
 
1
  from nnunet.utilities.file_conversions import convert_2d_image_to_nifti
2
  from nnunet.paths import nnUNet_raw_data, preprocessing_output_dir
3
  from nnunet.dataset_conversion.utils import generate_dataset_json
4
 
5
  import argparse
6
 
7
+ import os
8
+ import pickle
9
+ import json
10
+ from typing import List
11
 
12
+
13
+ def subdirs(folder: str, join: bool = True, prefix: str = None, suffix: str = None, sort: bool = True) -> List[str]:
14
+ if join:
15
+ l = os.path.join
16
+ else:
17
+ l = lambda x, y: y
18
+ res = [l(folder, i) for i in os.listdir(folder) if os.path.isdir(os.path.join(folder, i))
19
+ and (prefix is None or i.startswith(prefix))
20
+ and (suffix is None or i.endswith(suffix))]
21
+ if sort:
22
+ res.sort()
23
+ return res
24
+
25
+
26
+ def subfiles(folder: str, join: bool = True, prefix: str = None, suffix: str = None, sort: bool = True) -> List[str]:
27
+ if join:
28
+ l = os.path.join
29
+ else:
30
+ l = lambda x, y: y
31
+ res = [l(folder, i) for i in os.listdir(folder) if os.path.isfile(os.path.join(folder, i))
32
+ and (prefix is None or i.startswith(prefix))
33
+ and (suffix is None or i.endswith(suffix))]
34
+ if sort:
35
+ res.sort()
36
+ return res
37
+
38
+
39
+ def nifti_files(folder: str, join: bool = True, sort: bool = True) -> List[str]:
40
+ return subfiles(folder, join=join, sort=sort, suffix='.nii.gz')
41
+
42
+
43
+ def maybe_mkdir_p(directory: str) -> None:
44
+ os.makedirs(directory, exist_ok=True)
45
+
46
+
47
+ def load_pickle(file: str, mode: str = 'rb'):
48
+ with open(file, mode) as f:
49
+ a = pickle.load(f)
50
+ return a
51
+
52
+
53
+ def write_pickle(obj, file: str, mode: str = 'wb') -> None:
54
+ with open(file, mode) as f:
55
+ pickle.dump(obj, f)
56
+
57
+
58
+ def load_json(file: str):
59
+ with open(file, 'r') as f:
60
+ a = json.load(f)
61
+ return a
62
+
63
+
64
+ def save_json(obj, file: str, indent: int = 4, sort_keys: bool = True) -> None:
65
+ with open(file, 'w') as f:
66
+ json.dump(obj, f, sort_keys=sort_keys, indent=indent)
67
+
68
+
69
+ def pardir(path: str):
70
+ return os.path.join(path, os.pardir)
71
+
72
+
73
+ def split_path(path: str) -> List[str]:
74
+ """
75
+ splits at each separator. This is different from os.path.split which only splits at last separator
76
+ """
77
+ return path.split(os.sep)
78
+
79
+
80
+ # I'm tired of typing these out
81
+ join = os.path.join
82
+ isdir = os.path.isdir
83
+ isfile = os.path.isfile
84
+ listdir = os.listdir
85
+ makedirs = maybe_mkdir_p
86
+ os_split_path = os.path.split
87
+
88
+ # I am tired of confusing those
89
+ subfolders = subdirs
90
+ save_pickle = write_pickle
91
+ write_json = save_json
92
  if __name__ == '__main__':
93
  parser = argparse.ArgumentParser()
94
  parser.add_argument("-data_percentage", default=100,