pyxem / pyxem-demos

Examples and tutorials of multi-dimensional diffraction microscopy workflows using pyxem.
30 stars 38 forks source link

Passing a Signal2D as a 3x3 matrix to apply_affine_transformation() #21

Closed hakonanes closed 5 years ago

hakonanes commented 5 years ago

Encountered a small bug in the strain mapping notebook:

x_l = []
for x in [0, 0, -0.01, 0.02]:
    x_s = np.eye(3)
    x_s[0, 0] += x
    x_l.append(x_s)

angles = hs.signals.Signal2D(np.asarray(x_l).reshape(2, 2, 3, 3))
dp = dp.apply_affine_transformation(D=angles, order=5, inplace=False)
dp.set_diffraction_calibration(1)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-44-24ce614d8f54> in <module>
      6 
      7 angles = hs.signals.Signal2D(np.asarray(x_l).reshape(2, 2, 3, 3))
----> 8 dp = dp.apply_affine_transformation(D=angles, order=5, inplace=False)
      9 dp.set_diffraction_calibration(1)

~/miniconda3/envs/pxm/lib/python3.7/site-packages/pyxem/signals/electron_diffraction.py in apply_affine_transformation(self, D, order, inplace, *args, **kwargs)
    287 
    288         # This defines the transform you want to perform
--> 289         distortion = tf.AffineTransform(matrix=D)
    290 
    291         # skimage transforms can be added like this, does matrix multiplication,

~/miniconda3/envs/pxm/lib/python3.7/site-packages/skimage/transform/_geometric.py in __init__(self, matrix, scale, rotation, shear, translation)
    761                              " the implicit parameters at the same time.")
    762         elif matrix is not None:
--> 763             if matrix.shape != (3, 3):
    764                 raise ValueError("Invalid shape of transformation matrix.")
    765             self.params = matrix

AttributeError: 'Signal2D' object has no attribute 'shape'
pc494 commented 5 years ago

@dnjohnstone (pretending he was JMorzy) refactored apply_affine_transform which has caused this break. I think he was doing it for speed (can you confirm Duncan?), and forgot this particular edge case.

Will fix either fix the notebook or file a bug at pyxem once I've spoken to him.

dnjohnstone commented 5 years ago

I did refactor apply_affine_transformation so that the affine transformation was defined as a skimage transform outside of the map function, which did increase the speed quite substantially. To be honest I didn't realise we were ever allowing a Signal2D of transformation matrices to be passed to this function and I'm pretty sure the docstring never said that. It that's behaviour we want I think we should keep the existing functionality somehow for the speed benefits and also test for D being passed both as a numpy array and a Signal2D.

pc494 commented 5 years ago

This should now be fixed in 0.8.1