@@ -1699,15 +1699,8 @@ def __init__(self, child):
1699
1699
be replaced with :meth:`set`.
1700
1700
"""
1701
1701
_api .check_isinstance (Transform , child = child )
1702
- self ._init (child )
1703
- self .set_children (child )
1704
-
1705
- def _init (self , child ):
1706
- Transform .__init__ (self )
1707
- self .input_dims = child .input_dims
1708
- self .output_dims = child .output_dims
1709
- self ._set (child )
1710
- self ._invalid = 0
1702
+ super ().__init__ ()
1703
+ self .set (child )
1711
1704
1712
1705
def __eq__ (self , other ):
1713
1706
return self ._child .__eq__ (other )
@@ -1718,8 +1711,25 @@ def frozen(self):
1718
1711
# docstring inherited
1719
1712
return self ._child .frozen ()
1720
1713
1721
- def _set (self , child ):
1714
+ def set (self , child ):
1715
+ """
1716
+ Replace the current child of this transform with another one.
1717
+
1718
+ The new child must have the same number of input and output
1719
+ dimensions as the current child.
1720
+ """
1721
+ if hasattr (self , "_child" ): # Absent during init.
1722
+ if (child .input_dims != self ._child .input_dims or
1723
+ child .output_dims != self ._child .output_dims ):
1724
+ raise ValueError (
1725
+ f"The input and output dims of the new child "
1726
+ f"({ child .input_dims } , { child .output_dims } ) "
1727
+ f"do not match those of current child "
1728
+ f"({ self ._child .input_dims } , { self ._child .output_dims } )" )
1729
+ self ._child ._parents .pop (id (self ), None )
1730
+
1722
1731
self ._child = child
1732
+ self .set_children (child )
1723
1733
1724
1734
self .transform = child .transform
1725
1735
self .transform_affine = child .transform_affine
@@ -1730,31 +1740,16 @@ def _set(self, child):
1730
1740
self .get_affine = child .get_affine
1731
1741
self .inverted = child .inverted
1732
1742
self .get_matrix = child .get_matrix
1733
-
1734
1743
# note we do not wrap other properties here since the transform's
1735
1744
# child can be changed with WrappedTransform.set and so checking
1736
1745
# is_affine and other such properties may be dangerous.
1737
1746
1738
- def set (self , child ):
1739
- """
1740
- Replace the current child of this transform with another one.
1741
-
1742
- The new child must have the same number of input and output
1743
- dimensions as the current child.
1744
- """
1745
- if (child .input_dims != self .input_dims or
1746
- child .output_dims != self .output_dims ):
1747
- raise ValueError (
1748
- "The new child must have the same number of input and output "
1749
- "dimensions as the current child" )
1750
-
1751
- self .set_children (child )
1752
- self ._set (child )
1753
-
1754
1747
self ._invalid = 0
1755
1748
self .invalidate ()
1756
1749
self ._invalid = 0
1757
1750
1751
+ input_dims = property (lambda self : self ._child .input_dims )
1752
+ output_dims = property (lambda self : self ._child .output_dims )
1758
1753
is_affine = property (lambda self : self ._child .is_affine )
1759
1754
is_separable = property (lambda self : self ._child .is_separable )
1760
1755
has_inverse = property (lambda self : self ._child .has_inverse )
0 commit comments