|
a |
|
b/ants/internal.py |
|
|
1 |
import ants |
|
|
2 |
from ants import lib |
|
|
3 |
|
|
|
4 |
short_ptype_map = {"unsigned char": "UC", |
|
|
5 |
"unsigned int": "UI", |
|
|
6 |
"float": "F", |
|
|
7 |
"double": "D"} |
|
|
8 |
|
|
|
9 |
def infer_dtype(dtype): |
|
|
10 |
# supported dtypes: uint8, uint32, float32, float64 |
|
|
11 |
exchange_map = { |
|
|
12 |
'bool': 'uint8', |
|
|
13 |
'int8': 'uint32', |
|
|
14 |
'int16': 'uint32', |
|
|
15 |
'int32': 'uint32', |
|
|
16 |
'int64': 'uint32', |
|
|
17 |
'uint16': 'uint32', |
|
|
18 |
'uint64': 'uint32', |
|
|
19 |
'float16': 'float32' |
|
|
20 |
} |
|
|
21 |
new_dtype = exchange_map.get(str(dtype), dtype) |
|
|
22 |
return new_dtype |
|
|
23 |
|
|
|
24 |
def short_ptype(pixeltype): |
|
|
25 |
return short_ptype_map[pixeltype] |
|
|
26 |
|
|
|
27 |
def get_lib_fn(string): |
|
|
28 |
return getattr(lib, string) |
|
|
29 |
|
|
|
30 |
def get_pointer_string(image): |
|
|
31 |
return lib.ptrstr(image.pointer) |
|
|
32 |
|
|
|
33 |
def process_arguments(args): |
|
|
34 |
p_args = [] |
|
|
35 |
if isinstance(args, dict): |
|
|
36 |
for argname, argval in args.items(): |
|
|
37 |
|
|
|
38 |
# -MULTINAME- is used to support multiple items with the same key |
|
|
39 |
if "-MULTINAME-" in argname: |
|
|
40 |
argname = argname[: argname.find("-MULTINAME-")] |
|
|
41 |
|
|
|
42 |
if argval is not None: |
|
|
43 |
if len(argname) > 1: |
|
|
44 |
p_args.append("--%s" % argname) |
|
|
45 |
else: |
|
|
46 |
p_args.append("-%s" % argname) |
|
|
47 |
|
|
|
48 |
if ants.is_image(argval): |
|
|
49 |
p_args.append(get_pointer_string(argval)) |
|
|
50 |
elif isinstance(argval, list): |
|
|
51 |
for av in argval: |
|
|
52 |
if ants.is_image(av): |
|
|
53 |
av = get_pointer_string(av) |
|
|
54 |
elif str(arg) == "True": |
|
|
55 |
av = str(1) |
|
|
56 |
elif str(arg) == "False": |
|
|
57 |
av = str(0) |
|
|
58 |
p_args.append(av) |
|
|
59 |
else: |
|
|
60 |
p_args.append(str(argval)) |
|
|
61 |
|
|
|
62 |
elif isinstance(args, list): |
|
|
63 |
for arg in args: |
|
|
64 |
if ants.is_image(arg): |
|
|
65 |
pointer_string = get_pointer_string(arg) |
|
|
66 |
p_arg = pointer_string |
|
|
67 |
elif arg is None: |
|
|
68 |
pass |
|
|
69 |
elif str(arg) == "True": |
|
|
70 |
p_arg = str(1) |
|
|
71 |
elif str(arg) == "False": |
|
|
72 |
p_arg = str(0) |
|
|
73 |
else: |
|
|
74 |
p_arg = str(arg) |
|
|
75 |
p_args.append(p_arg) |
|
|
76 |
return p_args |