utils
ðŸ§
Collection of utility functions for xai4mri
.
Author: Simon M. Hofmann
Years: 2023
Bcolors
ðŸ§
Use for color print-commands in console.
Usage
print(Bcolors.HEADER + "Warning: No active frommets remain. Continue?" + Bcolors.ENDC)
print(Bcolors.OKBLUE + "Warning: No active frommets remain. Continue?" + Bcolors.ENDC)
For more colors:
Name | Color Code |
---|---|
CSELECTED | \33[7m |
CBLACK | \33[30m |
CRED | \33[31m |
CGREEN | \33[32m |
CYELLOW | \33[33m |
CBLUE | \33[34m |
CVIOLET | \33[35m |
CBEIGE | \33[36m |
CWHITE | \33[37m |
CBLACKBG | \33[40m |
CREDBG | \33[41m |
CGREENBG | \33[42m |
CYELLOWBG | \33[43m |
CBLUEBG | \33[44m |
CVIOLETBG | \33[45m |
CBEIGEBG | \33[46m |
CWHITEBG | \33[47m |
CGREY | \33[90m |
CBEIGE2 | \33[96m |
CWHITE2 | \33[97m |
CGREYBG | \33[100m |
CREDBG2 | \33[101m |
CGREENBG2 | \33[102m |
CYELLOWBG2 | \33[103m |
CBLUEBG2 | \33[104m |
CVIOLETBG2 | \33[105m |
CBEIGEBG2 | \33[106m |
CWHITEBG2 | \33[107m |
For preview use:
for i in (
[1, 4, 7] + list(range(30, 38)) + list(range(40, 48)) + list(range(90, 98)) + list(range(100, 108))
): # range(107+1)
print(i, "\33[{}m".format(i) + "ABC & abc" + "\33[0m")
ask_true_false
ðŸ§
Ask user for input for a given True
-or-False
question.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
question |
str
|
Question to be asked to the user. |
required |
col |
str
|
Print-color of question ['b'(lue), 'g'(reen), 'y'(ellow), 'r'(ed)] |
'b'
|
Returns:
Type | Description |
---|---|
None
|
Answer to the question. |
Source code in src/xai4mri/utils.py
389 390 391 392 393 394 395 396 397 398 |
|
browse_files
ðŸ§
Interactively browse and choose a file from the finder.
This function is a wrapper around the tkinter.filedialog.askopenfilename
function
and uses a GUI to select a file.
Note
ARGS MUST BE NAMED 'initialdir' and 'filetypes'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initialdir |
str | None
|
Directory, where the search should start |
None
|
filetypes |
str | None
|
What type of file-ending is searched for (suffix, e.g., |
None
|
Returns:
Type | Description |
---|---|
str
|
Path to the chosen file. |
Source code in src/xai4mri/utils.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
bytes_to_rep_string
ðŸ§
Convert the number of bytes into representative string.
The function is used to convert the number of bytes into a human-readable format.
The function rounds the number of bytes to two decimal places.
Example
print(bytes_to_rep_string(1_500_000)) # 1.5 MB
print(bytes_to_rep_string(1_005_500_000)) # 1.01 GB
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number_of_bytes |
int
|
Number of bytes. |
required |
Returns:
Type | Description |
---|---|
str
|
Representative string of the given bytes number. |
Source code in src/xai4mri/utils.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
|
check_storage_size
ðŸ§
Return the storage size of a given object in an appropriate unit.
Example
import numpy as np
a = np.random.rand(500, 500)
size_in_bytes = check_storage_size(obj=a, verbose=True) # "Size of given object: 2.0 MB"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Any
|
Any object in the workspace. |
required |
verbose |
bool
|
Print human-readable size of the object and additional information. |
True
|
Returns:
Type | Description |
---|---|
int
|
Object size in bytes. |
Source code in src/xai4mri/utils.py
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
|
chop_microseconds
ðŸ§
Chop microseconds from given time delta.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
delta |
timedelta
|
time delta |
required |
Returns:
Type | Description |
---|---|
timedelta
|
time delta without microseconds |
Source code in src/xai4mri/utils.py
142 143 144 145 146 147 148 149 |
|
compute_array_size
ðŸ§
compute_array_size(
shape: tuple[int, ...] | list[int, ...],
dtype: dtype | int | float,
verbose: bool = False,
) -> int
Compute the theoretical size of a NumPy array with the given shape and data type.
The idea is to compute the size of the array before creating it to avoid potential memory issues.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shape |
tuple[int, ...] | list[int, ...]
|
Shape of the array, e.g., |
required |
dtype |
dtype | int | float
|
Data type of the array elements (e.g., np.float32, np.int64, np.uint8, int, float) |
required |
verbose |
bool
|
Print the size of the array in readable format or not. |
False
|
Returns:
Type | Description |
---|---|
int
|
Size of the array in bytes. |
Source code in src/xai4mri/utils.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
|
cprint
ðŸ§
Colorize and format print-out.
Add leading time-stamp (fs) if required.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string |
str
|
Print message. |
required |
col |
str | None
|
Color:'p'(ink), 'b'(lue), 'g'(reen), 'y'(ellow), OR 'r'(ed). |
None
|
fm |
str | None
|
Format: 'ul'(:underline) OR 'bo'(:bold). |
None
|
ts |
bool
|
Add leading time-stamp. |
False
|
Source code in src/xai4mri/utils.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
|
function_timed
ðŸ§
function_timed(
dry_funct: Callable[..., Any] | None = None,
ms: bool | None = None,
) -> Callable[..., Any]
Time the processing duration of wrapped function.
How to use the function_timed
The following returns the duration of the function call without micro-seconds:
# Implement a function to be timed
@function_timed
def abc():
return 2 + 2
# Call the function and get the processing time
abc()
The following returns micro-seconds as well:
@function_timed(ms=True)
def abcd():
return 2 + 2
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dry_funct |
Callable[..., Any] | None
|
Parameter can be ignored. Results in output without micro-seconds. |
None
|
ms |
bool | None
|
If micro-seconds should be printed, set to |
None
|
Returns:
Type | Description |
---|---|
Callable[..., Any]
|
Wrapped function with processing time. |
Source code in src/xai4mri/utils.py
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 |
|
get_string_overlap
ðŸ§
Find the longest overlap between two strings, starting from the left.
Example
get_string_overlap("Hello there Bob", "Hello there Alice") # "Hello there "
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s1 |
str
|
First string. |
required |
s2 |
str
|
Second string. |
required |
Returns:
Type | Description |
---|---|
str
|
Longest overlap between the two strings. |
Source code in src/xai4mri/utils.py
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
|
normalize
ðŸ§
normalize(
array: ndarray,
lower_bound: int | float,
upper_bound: int | float,
global_min: int | float | None = None,
global_max: int | float | None = None,
) -> ndarray
Min-max-scaling: Normalize an input array to lower and upper bounds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
ndarray
|
Array to be transformed. |
required |
lower_bound |
int | float
|
Lower bound |
required |
upper_bound |
int | float
|
Upper bound |
required |
global_min |
int | float | None
|
Global minimum. If the array is part of a larger tensor, normalize w.r.t. global min and ... |
None
|
global_max |
int | float | None
|
Global maximum. If the array is part of a larger tensor, normalize w.r.t. ... and global max (i.e., tensor min/max) |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
Normalized array. |
Source code in src/xai4mri/utils.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
run_gpu_test
ðŸ§
Test GPU implementation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_device_placement |
bool
|
Log device placement. |
False
|
Returns:
Type | Description |
---|---|
bool
|
GPU available or not. |
Source code in src/xai4mri/utils.py
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
|
tree
ðŸ§
Print the directory tree starting at directory
.
Use the same way as shell
command tree
.
This leads to output such as:
directory/
├── _static/
│ ├── embedded/
│ │ ├── deep_file
│ │ └── very/
│ │ └── deep/
│ │ └── folder/
│ │ └── very_deep_file
│ └── less_deep_file
├── about.rst
├── conf.py
└── index.rst
Source code in src/xai4mri/utils.py
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 |
|