-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.py
53 lines (38 loc) · 1.46 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
"""Utilities for density compensation."""
from functools import wraps
import numpy as np
from mrinufft._utils import MethodRegister, proper_trajectory
register_density = MethodRegister("density_compensation")
def get_density(name, *args, **kwargs):
"""Get the density compensation function from its name."""
try:
method = register_density.registry["density_compensation"][name]
except KeyError as e:
raise ValueError(
f"Unknown density compensation method {name}. Available methods are \n"
f"{list(register_density.registry['density_compensation'].keys())}"
) from e
if args or kwargs:
return method(*args, **kwargs)
return method
def flat_traj(normalize="unit"):
"""Decorate function to ensure that the trajectory is flatten before calling."""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
args = list(args)
args[0] = proper_trajectory(args[0], normalize=normalize)
return func(*args, **kwargs)
return wrapper
if callable(normalize): # call without argument
func = normalize
normalize = "unit"
return decorator(func)
else:
return decorator
def normalize_weights(weights):
"""Normalize samples weights to reflect their importance.
Higher weights have lower importance.
"""
inv_weights = np.sum(weights) / weights
return inv_weights / (np.sum(inv_weights))