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

Function flip

monai/transforms/spatial/functional.py:275–308  ·  view source on GitHub ↗

Functional implementation of flip. This function operates eagerly or lazily according to ``lazy`` (default ``False``). Args: img: data to be changed, assuming `img` is channel-first. sp_axes: spatial axes along which to flip over. If None, will flip over

(img, sp_axes, lazy, transform_info)

Source from the content-addressed store, hash-verified

273
274
275def flip(img, sp_axes, lazy, transform_info):
276 """
277 Functional implementation of flip.
278 This function operates eagerly or lazily according to
279 ``lazy`` (default ``False``).
280
281 Args:
282 img: data to be changed, assuming `img` is channel-first.
283 sp_axes: spatial axes along which to flip over.
284 If None, will flip over all of the axes of the input array.
285 If axis is negative it counts from the last to the first axis.
286 If axis is a tuple of ints, flipping is performed on all of the axes
287 specified in the tuple.
288 lazy: a flag that indicates whether the operation should be performed lazily or not
289 transform_info: a dictionary with the relevant information pertaining to an applied transform.
290 """
291 sp_size = img.peek_pending_shape() if isinstance(img, MetaTensor) else img.shape[1:]
292 sp_size = convert_to_numpy(sp_size, wrap_sequence=True).tolist()
293 extra_info = {"axes": sp_axes} # track the spatial axes
294 axes = monai.transforms.utils.map_spatial_axes(img.ndim, sp_axes) # use the axes with channel dim
295 rank = img.peek_pending_rank() if isinstance(img, MetaTensor) else torch.tensor(3.0, dtype=torch.double)
296 # axes include the channel dim
297 xform = torch.eye(int(rank) + 1, dtype=torch.double)
298 for axis in axes:
299 sp = axis - 1
300 xform[sp, sp], xform[sp, -1] = xform[sp, sp] * -1, sp_size[sp] - 1
301 meta_info = TraceableTransform.track_transform_meta(
302 img, sp_size=sp_size, affine=xform, extra_info=extra_info, transform_info=transform_info, lazy=lazy
303 )
304 out = _maybe_new_metatensor(img)
305 if lazy:
306 return out.copy_meta_from(meta_info) if isinstance(out, MetaTensor) else meta_info
307 out = torch.flip(out, axes)
308 return out.copy_meta_from(meta_info) if isinstance(out, MetaTensor) else out
309
310
311def resize(

Callers 10

__call__Method · 0.90
test_invalid_casesMethod · 0.85
test_correct_resultsMethod · 0.85
test_correct_resultsMethod · 0.85
test_correct_resultsMethod · 0.85
test_invalid_inputsMethod · 0.85
test_correct_resultsMethod · 0.85
test_invalid_inputsMethod · 0.85
test_correct_resultsMethod · 0.85
test_correct_resultsMethod · 0.85

Calls 6

convert_to_numpyFunction · 0.90
_maybe_new_metatensorFunction · 0.85
peek_pending_shapeMethod · 0.80
peek_pending_rankMethod · 0.80
track_transform_metaMethod · 0.80
copy_meta_fromMethod · 0.80

Tested by 9

test_invalid_casesMethod · 0.68
test_correct_resultsMethod · 0.68
test_correct_resultsMethod · 0.68
test_correct_resultsMethod · 0.68
test_invalid_inputsMethod · 0.68
test_correct_resultsMethod · 0.68
test_invalid_inputsMethod · 0.68
test_correct_resultsMethod · 0.68
test_correct_resultsMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…