Spaces:
Sleeping
Sleeping
File size: 7,407 Bytes
aea73e2 |
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# -*- coding: utf-8 -*-
# Utility functions
#
# @ Fabian Hörst, fabian.hoerst@uk-essen.de
# Institute for Artifical Intelligence in Medicine,
# University Medicine Essen
import importlib
import logging
import sys
import types
from datetime import timedelta
from timeit import default_timer as timer
from typing import Dict, List, Optional, Tuple, Union
from utils.__init__ import logger
# Helper timing functions
def start_timer() -> float:
"""Returns the number of seconds passed since epoch. The epoch is the point where the time starts,
and is platform dependent.
Returns:
float: The number of seconds passed since epoch
"""
return timer()
def end_timer(start_time: float, timed_event: str = "Time usage") -> None:
"""Prints the time passed from start_time.
Args:
start_time (float): The number of seconds passed since epoch when the timer started
timed_event (str, optional): A string describing the activity being monitored. Defaults to "Time usage".
"""
logger.info(f"{timed_event}: {timedelta(seconds=timer() - start_time)}")
def module_exists(
*names: Union[List[str], str],
error: str = "ignore",
warn_every_time: bool = False,
__INSTALLED_OPTIONAL_MODULES: Dict[str, bool] = {},
) -> Optional[Union[Tuple[types.ModuleType, ...], types.ModuleType]]:
"""Try to import optional dependencies.
Ref: https://stackoverflow.com/a/73838546/4900327
Args:
names (Union(List(str), str)): The module name(s) to import. Str or list of strings.
error (str, optional): What to do when a dependency is not found:
* raise : Raise an ImportError.
* warn: print a warning.
* ignore: If any module is not installed, return None, otherwise, return the module(s).
Defaults to "ignore".
warn_every_time (bool, optional): Whether to warn every time an import is tried. Only applies when error="warn".
Setting this to True will result in multiple warnings if you try to import the same library multiple times.
Defaults to False.
Raises:
ImportError: ImportError of Module
Returns:
Optional[ModuleType, Tuple[ModuleType...]]: The imported module(s), if all are found.
None is returned if any module is not found and `error!="raise"`.
"""
assert error in {"raise", "warn", "ignore"}
if isinstance(names, (list, tuple, set)):
names: List[str] = list(names)
else:
assert isinstance(names, str)
names: List[str] = [names]
modules = []
for name in names:
try:
module = importlib.import_module(name)
modules.append(module)
__INSTALLED_OPTIONAL_MODULES[name] = True
except ImportError:
modules.append(None)
def error_msg(missing: Union[str, List[str]]):
if not isinstance(missing, (list, tuple)):
missing = [missing]
missing_str: str = " ".join([f'"{name}"' for name in missing])
dep_str = "dependencies"
if len(missing) == 1:
dep_str = "dependency"
msg = f"Missing optional {dep_str} {missing_str}. Use pip or conda to install."
return msg
missing_modules: List[str] = [
name for name, module in zip(names, modules) if module is None
]
if len(missing_modules) > 0:
if error == "raise":
raise ImportError(error_msg(missing_modules))
if error == "warn":
for name in missing_modules:
# Ensures warning is printed only once
if warn_every_time is True or name not in __INSTALLED_OPTIONAL_MODULES:
logger.warning(f"Warning: {error_msg(name)}")
__INSTALLED_OPTIONAL_MODULES[name] = False
return None
if len(modules) == 1:
return modules[0]
return tuple(modules)
def close_logger(logger: logging.Logger) -> None:
"""Closing a logger savely
Args:
logger (logging.Logger): Logger to close
"""
handlers = logger.handlers[:]
for handler in handlers:
logger.removeHandler(handler)
handler.close()
logger.handlers.clear()
logging.shutdown()
class AverageMeter(object):
"""Computes and stores the average and current value
Original-Code: https://github.com/facebookresearch/simsiam
"""
def __init__(self, name, fmt=":f"):
self.name = name
self.fmt = fmt
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
fmtstr = "{name} {val" + self.fmt + "} ({avg" + self.fmt + "})"
return fmtstr.format(**self.__dict__)
def flatten_dict(d: dict, parent_key: str = "", sep: str = ".") -> dict:
"""Flatten a nested dictionary and insert the sep to seperate keys
Args:
d (dict): dict to flatten
parent_key (str, optional): parent key name. Defaults to ''.
sep (str, optional): Seperator. Defaults to '.'.
Returns:
dict: Flattened dict
"""
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, dict):
items.extend(flatten_dict(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
def unflatten_dict(d: dict, sep: str = ".") -> dict:
"""Unflatten a flattened dictionary (created a nested dictionary)
Args:
d (dict): Dict to be nested
sep (str, optional): Seperator of flattened keys. Defaults to '.'.
Returns:
dict: Nested dict
"""
output_dict = {}
for key, value in d.items():
keys = key.split(sep)
d = output_dict
for k in keys[:-1]:
d = d.setdefault(k, {})
d[keys[-1]] = value
return output_dict
def remove_parameter_tag(d: dict, sep: str = ".") -> dict:
"""Remove all paramter tags from dictionary
Args:
d (dict): Dict must be flattened with defined seperator
sep (str, optional): Seperator used during flattening. Defaults to ".".
Returns:
dict: Dict with parameter tag removed
"""
param_dict = {}
for k, _ in d.items():
unflattened_keys = k.split(sep)
new_keys = []
max_num_insert = len(unflattened_keys) - 1
for i, k in enumerate(unflattened_keys):
if i < max_num_insert and k != "parameters":
new_keys.append(k)
joined_key = sep.join(new_keys)
param_dict[joined_key] = {}
print(param_dict)
for k, v in d.items():
unflattened_keys = k.split(sep)
new_keys = []
max_num_insert = len(unflattened_keys) - 1
for i, k in enumerate(unflattened_keys):
if i < max_num_insert and k != "parameters":
new_keys.append(k)
joined_key = sep.join(new_keys)
param_dict[joined_key][unflattened_keys[-1]] = v
return param_dict
def get_size_of_dict(d: dict) -> int:
size = sys.getsizeof(d)
for key, value in d.items():
size += sys.getsizeof(key)
size += sys.getsizeof(value)
return size |