r"""Converts possibly 1D tuple to nd tuple. Args: allow_zero: whether to allow zero tuple value
(value, *, n, allow_zero=True)
| 4 | |
| 5 | |
| 6 | def get_ndtuple(value, *, n, allow_zero=True): |
| 7 | r"""Converts possibly 1D tuple to nd tuple. |
| 8 | |
| 9 | Args: |
| 10 | allow_zero: whether to allow zero tuple value |
| 11 | """ |
| 12 | if not isinstance(value, collections.abc.Iterable): |
| 13 | value = int(value) |
| 14 | value = tuple([value for i in range(n)]) |
| 15 | else: |
| 16 | assert len(value) == n, "tuple len is not equal to n: {}".format(value) |
| 17 | spatial_axis = map(int, value) |
| 18 | value = tuple(spatial_axis) |
| 19 | if allow_zero: |
| 20 | minv = 0 |
| 21 | else: |
| 22 | minv = 1 |
| 23 | assert min(value) >= minv, "invalid value: {}".format(value) |
| 24 | return value |
| 25 | |
| 26 | |
| 27 | _single = functools.partial(get_ndtuple, n=1, allow_zero=True) |