Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Average patterns with nearest neighbours #134

Merged
merged 20 commits into from
Feb 13, 2020

Conversation

hakonanes
Copy link
Member

@hakonanes hakonanes commented Jan 11, 2020

Description

This PR adds averaging of patterns with its nearest neighbours. The user can decide:

  • kernel size (both symmetrical and asymmetrical)
  • kernel coefficients

So far, all neighbouring patterns are averaged equally. However, it should be fairly easy to add weighted averaging.

Type of change

  • New feature (non-breaking change which adds functionality)

Progress

  • Divide summed intensities by number of nearest neighbours
  • Figure out conversion between data types: back to initial intensities after averaging? Perhaps a user option.
  • Figure out warning or error messages when un-allowed scans, e.g. too small scans like (2 x 2) pattern scans, are passed
  • Docstrings updated
  • Code is commented, particularly in hard-to-understand areas
  • Documentation and/or user guide updated
  • Tests have been written
  • Ready for review!

How has this been tested?

  • Expected output intensities
  • Expected warnings
  • Expected errors

Minimal example of the bug fix or new feature

>>>import numpy as np
>>> import kikuchipy as kp
>>> s = kp.signals.EBSD(np.ones((4, 4, 2, 2), dtype=np.uint8))
>>> k = 1
>>> for i in range(4):
        for j in range(4):
            s.inav[j, i].data *= k
            k += 1
>>> print(s.mean(s.axes_manager.signal_axes).data)
array([[ 1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.],
       [ 9., 10., 11., 12.],
       [13., 14., 15., 16.]])
>>> s.average_neighbour_patterns(kernel="rectangular", kernel_size=(3, 3))
>>> print(s.mean(s.axes_manager.signal_axes).data)
array([[ 3.5,  4. ,  5. ,  5.5],
       [ 5.5,  6. ,  7. ,  7.5],
       [ 9.5, 10. , 11. , 11.5],
       [11.5, 12. , 13. , 13.5]])

References

Implemented using scipy.ndimage.correlate, taking a queue from Stack Overflow user Evan Davis' solution. Also use Dask's overlapping computations functionality.

Addresses #13.

Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
@hakonanes hakonanes added the enhancement New feature or request label Jan 11, 2020
@hakonanes hakonanes added this to the v0.2.0 milestone Jan 11, 2020
@coveralls
Copy link

coveralls commented Jan 11, 2020

Coverage Status

Coverage remained the same at 100.0% when pulling dc8bea2 on hakonanes:pattern-averaging into 247851f on kikuchipy:master.

Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
@hakonanes hakonanes requested a review from tinabe January 19, 2020 13:50
@hakonanes hakonanes changed the title [WIP] Average patterns with nearest neighbours Average patterns with nearest neighbours Jan 19, 2020
Copy link
Collaborator

@tinabe tinabe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. :)
I only added a couple of comments in regards of the clarity of documentation at specific points.

kikuchipy/signals/ebsd.py Outdated Show resolved Hide resolved
kikuchipy/util/experimental.py Outdated Show resolved Hide resolved
doc/pattern_processing.rst Outdated Show resolved Hide resolved
@hakonanes
Copy link
Member Author

hakonanes commented Jan 30, 2020

I realised that my implementation, with a few tweaks, can be made a lot more general, with options for the user to:

Have also created a simple drawing tool to plot the kernel and its coefficients, with this output for a Gaussian kernel with std=1:
test

@hakonanes
Copy link
Member Author

hakonanes commented Jan 30, 2020

A natural continuation of this averaging would be to implement different coefficients for each pattern, e.g. ones determined by similarity between the pattern at the kernel origin and its nearest neighbours in e.g. a (5 x 5) window like the ones used in the NLPAR method (https://github.com/USNavalResearchLaboratory/NLPAR).

This is actually pretty easy with Dask's map_blocks!

Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
@hakonanes
Copy link
Member Author

I again realise that my implementation should be even more generalised, in that a kernel class should be made! Kernel objects can then be obtained for pattern averaging, or for detection of peaks in the Hough/Radon transform. These are the two use cases I can think of so far...

I will implement the kernel class to work for pattern averaging now, with plans for generalising and/or extending it for peak detection in the future. Have therefore reverted this PR to work in progress.

Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
@hakonanes
Copy link
Member Author

The macOS build fails because it cannot find pytest, although it is listed among installed packages... Don't know why, but all tests pass on Linux and Windows.

@hakonanes
Copy link
Member Author

Note that Travis macOS with pip fails here, however it is fixed in #143.

Copy link
Collaborator

@tinabe tinabe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great - the documentation well explains the functionality, and there are several good examples. I have no major comments. You have done a great job on this!

Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
@hakonanes
Copy link
Member Author

hakonanes commented Feb 13, 2020

Updated the documentation with your suggested explanation @tinabe (via personal correspondance, thanks a lot!). Also clarified the docstring for make_circular(). Left the Kernel methods is_valid() and scan_compatible() public, because why not. They are tested, and what they return, whether a kernel is valid or a kernel is compatible with an EBSD scan, will not change.

@hakonanes
Copy link
Member Author

Tests fail on all Travis builds because of intensity comparisons for static and dynamic background corrections to predefined answers which apparently are not correct anymore, although they were correct yesterday... Because the data type used in those corrections were changed in PR #148, I will merge this since all other tests pass.

@hakonanes hakonanes merged commit 83717e2 into pyxem:master Feb 13, 2020
@hakonanes hakonanes deleted the pattern-averaging branch February 13, 2020 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants