-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
135 lines (103 loc) · 3.98 KB
/
utils.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Copyright 2021 Division of Medical Image Computing, German Cancer Research Center (DKFZ)
# and Applied Computer Vision Lab, Helmholtz Imaging Platform
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import pickle
import json
from typing import List
import logging
def make_dirs(save_dir):
existing_versions = os.listdir(save_dir)
if len(existing_versions) > 0:
max_version = int(existing_versions[0].split("_")[-1])
for v in existing_versions:
ver = int(v.split("_")[-1])
if ver > max_version:
max_version = ver
version = int(max_version) + 1
else:
version = 0
return f"{save_dir}/exp_{version}"
def get_logger(filename, verbosity=1, name=None):
level_dict = {0: logging.DEBUG, 1: logging.INFO, 2: logging.WARNING}
formatter = logging.Formatter(
"[%(asctime)s][%(filename)s][line:%(lineno)d][%(levelname)s] %(message)s"
)
logger = logging.getLogger(name)
logger.setLevel(level_dict[verbosity])
fh = logging.FileHandler(filename, "w")
fh.setFormatter(formatter)
logger.addHandler(fh)
sh = logging.StreamHandler()
sh.setFormatter(formatter)
logger.addHandler(sh)
return logger
def subdirs(folder: str, join: bool = True, prefix: str = None, suffix: str = None, sort: bool = True) -> List[str]:
if join:
l = os.path.join
else:
l = lambda x, y: y
res = [l(folder, i) for i in os.listdir(folder) if os.path.isdir(os.path.join(folder, i))
and (prefix is None or i.startswith(prefix))
and (suffix is None or i.endswith(suffix))]
if sort:
res.sort()
return res
def subfiles(folder: str, join: bool = True, prefix: str = None, suffix: str = None, sort: bool = True) -> List[str]:
if join:
l = os.path.join
else:
l = lambda x, y: y
res = [l(folder, i) for i in os.listdir(folder) if os.path.isfile(os.path.join(folder, i))
and (prefix is None or i.startswith(prefix))
and (suffix is None or i.endswith(suffix))]
if sort:
res.sort()
return res
def nifti_files(folder: str, join: bool = True, sort: bool = True) -> List[str]:
return subfiles(folder, join=join, sort=sort, suffix='.nii.gz')
def maybe_mkdir_p(directory: str) -> None:
os.makedirs(directory, exist_ok=True)
def load_pickle(file: str, mode: str = 'rb'):
with open(file, mode) as f:
a = pickle.load(f)
return a
def write_pickle(obj, file: str, mode: str = 'wb') -> None:
with open(file, mode) as f:
pickle.dump(obj, f)
def load_json(file: str):
with open(file, 'r') as f:
a = json.load(f)
return a
def save_json(obj, file: str, indent: int = 4, sort_keys: bool = True) -> None:
with open(file, 'w') as f:
json.dump(obj, f, sort_keys=sort_keys, indent=indent)
def pardir(path: str):
return os.path.join(path, os.pardir)
def split_path(path: str) -> List[str]:
"""
splits at each separator. This is different from os.path.split which only splits at last separator
"""
return path.split(os.sep)
# I'm tired of typing these out
join = os.path.join
isdir = os.path.isdir
isfile = os.path.isfile
listdir = os.listdir
makedirs = maybe_mkdir_p
os_split_path = os.path.split
# I am tired of confusing those
subfolders = subdirs
save_pickle = write_pickle
write_json = save_json