A helper class that holds a single child transform and acts equivalently to it. This is useful if a node of the transform tree must be replaced at run time with a transform of a different type. This class allows that replacement to correctly trigger invalidation. `Transfo
| 1743 | |
| 1744 | |
| 1745 | class TransformWrapper(Transform): |
| 1746 | """ |
| 1747 | A helper class that holds a single child transform and acts |
| 1748 | equivalently to it. |
| 1749 | |
| 1750 | This is useful if a node of the transform tree must be replaced at |
| 1751 | run time with a transform of a different type. This class allows |
| 1752 | that replacement to correctly trigger invalidation. |
| 1753 | |
| 1754 | `TransformWrapper` instances must have the same input and output dimensions |
| 1755 | during their entire lifetime, so the child transform may only be replaced |
| 1756 | with another child transform of the same dimensions. |
| 1757 | """ |
| 1758 | |
| 1759 | pass_through = True |
| 1760 | |
| 1761 | def __init__(self, child): |
| 1762 | """ |
| 1763 | *child*: A `Transform` instance. This child may later |
| 1764 | be replaced with :meth:`set`. |
| 1765 | """ |
| 1766 | _api.check_isinstance(Transform, child=child) |
| 1767 | super().__init__() |
| 1768 | self.set(child) |
| 1769 | |
| 1770 | def __eq__(self, other): |
| 1771 | return self._child.__eq__(other) |
| 1772 | |
| 1773 | __str__ = _make_str_method("_child") |
| 1774 | |
| 1775 | def frozen(self): |
| 1776 | # docstring inherited |
| 1777 | return self._child.frozen() |
| 1778 | |
| 1779 | def set(self, child): |
| 1780 | """ |
| 1781 | Replace the current child of this transform with another one. |
| 1782 | |
| 1783 | The new child must have the same number of input and output |
| 1784 | dimensions as the current child. |
| 1785 | """ |
| 1786 | if hasattr(self, "_child"): # Absent during init. |
| 1787 | self.invalidate() |
| 1788 | new_dims = (child.input_dims, child.output_dims) |
| 1789 | old_dims = (self._child.input_dims, self._child.output_dims) |
| 1790 | if new_dims != old_dims: |
| 1791 | raise ValueError( |
| 1792 | f"The input and output dims of the new child {new_dims} " |
| 1793 | f"do not match those of current child {old_dims}") |
| 1794 | self._child._parents.pop(id(self), None) |
| 1795 | |
| 1796 | self._child = child |
| 1797 | self.set_children(child) |
| 1798 | |
| 1799 | self.transform = child.transform |
| 1800 | self.transform_affine = child.transform_affine |
| 1801 | self.transform_non_affine = child.transform_non_affine |
| 1802 | self.transform_path = child.transform_path |
nothing calls this directly
no test coverage detected
searching dependent graphs…