@@ -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,24 @@ 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
+ new_dims = (child .input_dims , child .output_dims )
1723
+ old_dims = (self ._child .input_dims , self ._child .output_dims )
1724
+ if new_dims != old_dims :
1725
+ raise ValueError (
1726
+ f"The input and output dims of the new child { new_dims } "
1727
+ f"do not match those of current child { old_dims } " )
1728
+ self ._child ._parents .pop (id (self ), None )
1729
+
1722
1730
self ._child = child
1731
+ self .set_children (child )
1723
1732
1724
1733
self .transform = child .transform
1725
1734
self .transform_affine = child .transform_affine
@@ -1730,31 +1739,16 @@ def _set(self, child):
1730
1739
self .get_affine = child .get_affine
1731
1740
self .inverted = child .inverted
1732
1741
self .get_matrix = child .get_matrix
1733
-
1734
1742
# note we do not wrap other properties here since the transform's
1735
1743
# child can be changed with WrappedTransform.set and so checking
1736
1744
# is_affine and other such properties may be dangerous.
1737
1745
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
1746
self ._invalid = 0
1755
1747
self .invalidate ()
1756
1748
self ._invalid = 0
1757
1749
1750
+ input_dims = property (lambda self : self ._child .input_dims )
1751
+ output_dims = property (lambda self : self ._child .output_dims )
1758
1752
is_affine = property (lambda self : self ._child .is_affine )
1759
1753
is_separable = property (lambda self : self ._child .is_separable )
1760
1754
has_inverse = property (lambda self : self ._child .has_inverse )
0 commit comments