MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / ensure_tuple_rep

Function ensure_tuple_rep

monai/utils/misc.py:194–226  ·  view source on GitHub ↗

Returns a copy of `tup` with `dim` values by either shortened or duplicated input. Raises: ValueError: When ``tup`` is a sequence and ``tup`` length is not ``dim``. Examples:: >>> ensure_tuple_rep(1, 3) (1, 1, 1) >>> ensure_tuple_rep(None, 3) (

(tup: Any, dim: int)

Source from the content-addressed store, hash-verified

192
193
194def ensure_tuple_rep(tup: Any, dim: int) -> tuple[Any, ...]:
195 """
196 Returns a copy of `tup` with `dim` values by either shortened or duplicated input.
197
198 Raises:
199 ValueError: When ``tup`` is a sequence and ``tup`` length is not ``dim``.
200
201 Examples::
202
203 >>> ensure_tuple_rep(1, 3)
204 (1, 1, 1)
205 >>> ensure_tuple_rep(None, 3)
206 (None, None, None)
207 >>> ensure_tuple_rep('test', 3)
208 ('test', 'test', 'test')
209 >>> ensure_tuple_rep([1, 2, 3], 3)
210 (1, 2, 3)
211 >>> ensure_tuple_rep(range(3), 3)
212 (0, 1, 2)
213 >>> ensure_tuple_rep([1, 2], 3)
214 ValueError: Sequence must have length 3, got length 2.
215
216 """
217 if isinstance(tup, torch.Tensor):
218 tup = tup.detach().cpu().numpy()
219 if isinstance(tup, np.ndarray):
220 tup = tup.tolist()
221 if not issequenceiterable(tup):
222 return (tup,) * dim
223 if len(tup) == dim:
224 return tuple(tup)
225
226 raise ValueError(f"Sequence must have length {dim}, got {len(tup)}.")
227
228
229def to_tuple_of_dictionaries(dictionary_of_tuples: dict, keys: Any) -> tuple[dict[Any, Any], ...]:

Callers 15

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90

Calls 1

issequenceiterableFunction · 0.85

Tested by 2

test_rand_affinedMethod · 0.72
test_spacingdMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…