Handle None in axes_to_array
None values should be treated the same as missing ones - previously it would just crash.
This commit is contained in:
parent
e86e4d983a
commit
2eade9ed30
1 changed files with 30 additions and 7 deletions
|
|
@ -4,7 +4,7 @@ import logging
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from typing import Dict, List, Optional, Tuple, Type, Union
|
from typing import Dict, List, Optional, Tuple, Type, Union, Sequence
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
@ -102,12 +102,29 @@ def set_properties(obj, **kwargs):
|
||||||
|
|
||||||
|
|
||||||
def axes_to_array(
|
def axes_to_array(
|
||||||
coordinate_dictionary: Dict[str, int],
|
coordinate_dictionary: Dict[str, Optional[int]],
|
||||||
axis_keys=("x", "y", "z"),
|
axis_keys: Sequence[str] = ("x", "y", "z"),
|
||||||
base_array: Optional[List[int]] = None,
|
base_array: Optional[List[int]] = None,
|
||||||
asint: bool = True,
|
asint: bool = True,
|
||||||
) -> List[int]:
|
) -> List[int]:
|
||||||
"""Takes key-value pairs of a JSON value, and maps onto an array"""
|
"""Takes key-value pairs of a JSON value, and maps onto an array
|
||||||
|
|
||||||
|
This is designed to take a dictionary like `{"x": 1, "y":2, "z":3}`
|
||||||
|
and return a list like `[1, 2, 3]` to convert between the argument
|
||||||
|
format expected by most of our stages, and the usual argument
|
||||||
|
format in JSON.
|
||||||
|
|
||||||
|
`axis_keys` is an ordered sequence of key names to extract from
|
||||||
|
the input dictionary.
|
||||||
|
|
||||||
|
`base_array` specifies a default value for each axis. It must
|
||||||
|
have the same length as `axis_keys`.
|
||||||
|
|
||||||
|
`asint` casts values to integers if it is `True` (default).
|
||||||
|
|
||||||
|
Missing keys, or keys that have a `None` value will be left
|
||||||
|
at the specified default value, or zero if none is specified.
|
||||||
|
"""
|
||||||
# If no base array is given
|
# If no base array is given
|
||||||
if not base_array:
|
if not base_array:
|
||||||
# Create an array of zeros
|
# Create an array of zeros
|
||||||
|
|
@ -119,8 +136,14 @@ def axes_to_array(
|
||||||
# Do the mapping
|
# Do the mapping
|
||||||
for axis, key in enumerate(axis_keys):
|
for axis, key in enumerate(axis_keys):
|
||||||
if key in coordinate_dictionary:
|
if key in coordinate_dictionary:
|
||||||
base_array[axis] = (
|
value = coordinate_dictionary[key]
|
||||||
int(coordinate_dictionary[key]) if asint else coordinate_dictionary[key]
|
if value is None:
|
||||||
)
|
# Values set to None should be treated as if they
|
||||||
|
# are missing
|
||||||
|
# i.e. we leave the default value in place.
|
||||||
|
break
|
||||||
|
if asint:
|
||||||
|
value = int(value)
|
||||||
|
base_array[axis] = value
|
||||||
|
|
||||||
return base_array
|
return base_array
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue