pylops.utils.decorators.reshapedยถ
- pylops.utils.decorators.reshaped(func=None, forward=None, swapaxis=False, axis=-1)[source]ยถ
Decorator for the common reshape/flatten pattern used in many operators.
- Parameters:
- func
callable, optional Function to be decorated when no arguments are provided
- forward
bool, optional Reshape to
dimsif True, otherwise todimsd. If not provided, the decorated functionโs name will be inspected to infer the mode. Any operator having a name with โrmatโ as substring or whose name is โdivโ or โ__truediv__โ will reshape todimsd.- swapaxis
bool, optional If True, swaps the
axisof the input array of the decorated function withself.axis. Only use if the decorated LinearOperator hasaxisattribute.- axis
int, optional Axis to be swapped when
swapaxisis True.
- func
Notes
A
_matvec(forward) function can be simplified fromdef _matvec(self, x): x = x.reshape(self.dims) x = x.swapaxes(self.axis, -1) y = do_things_to_reshaped_swapped(y) y = y.swapaxes(self.axis, -1) y = y.ravel() return y
to
@reshaped(swapaxis=True, axis=-1) def _matvec(self, x): y = do_things_to_reshaped_swapped(y) return y
When the decorator has no arguments, it can be called without parenthesis, e.g.:
@reshaped def _matvec(self, x): y = do_things_to_reshaped(y) return y