Пример #1
0
def test_get_signal_chunk_slice_not_square(sig_chunks, index, expected):
    data = da.zeros((2, 2, 10, 20), chunks=(2, 2, *sig_chunks[::-1]))
    if expected == 'error':
        with pytest.raises(ValueError):
            chunk_slice = get_signal_chunk_slice(index, data.chunks)
    else:
        chunk_slice = get_signal_chunk_slice(index, data.chunks)
        assert chunk_slice == expected
Пример #2
0
def test_get_signal_chunk_slice(sig_chunks, index, expected):
    ndim = 1 + len(index)
    data = da.zeros([20]*ndim, chunks=(10, *sig_chunks[::-1]))
    if expected == 'error':
        with pytest.raises(ValueError):
            chunk_slice = get_signal_chunk_slice(index, data.chunks)
    else:
        chunk_slice = get_signal_chunk_slice(index, data.chunks)
        assert chunk_slice == expected
Пример #3
0
    def compute_navigator(self,
                          index=None,
                          chunks_number=None,
                          show_progressbar=None):
        """
        Compute the navigator by taking the sum over a single chunk contained
        the specified coordinate. Taking the sum over a single chunk is a
        computationally efficient approach to compute the navigator. The data
        can be rechunk by specifying the ``chunks_number`` argument.

        Parameters
        ----------
        index : (int, float, None) or iterable, optional
            Specified where to take the sum, follows HyperSpy indexing syntax
            for integer and float. If None, the index is the centre of the
            signal_space
        chunks_number : (int, None) or iterable, optional
            Define the number of chunks in the signal space used for rechunk
            the when calculating of the navigator. Useful to define the range
            over which the sum is calculated.
            If None, the existing chunking will be considered when picking the
            chunk used in the navigator calculation.
        %s

        Returns
        -------
        None.

        Note
        ----
        The number of chunks will affect where the sum is taken. If the sum
        needs to be taken in the centre of the signal space (for example, in
        the case of diffraction pattern), the number of chunk needs to be an
        odd number, so that the middle is centered.

        """

        signal_shape = self.axes_manager.signal_shape

        if index is None:
            index = [round(shape / 2) for shape in signal_shape]
        else:
            if not isiterable(index):
                index = [index] * len(signal_shape)
            index = [
                axis._get_index(_idx)
                for _idx, axis in zip(index, self.axes_manager.signal_axes)
            ]
        _logger.info(f"Using index: {index}")

        if chunks_number is None:
            chunks = self.data.chunks
        else:
            if not isiterable(chunks_number):
                chunks_number = [chunks_number] * len(signal_shape)
            # Determine the chunk size
            signal_chunks = da.core.normalize_chunks([
                int(size / cn) for cn, size in zip(chunks_number, signal_shape)
            ],
                                                     shape=signal_shape)
            # Needs to reverse the chunks list to match dask chunking order
            signal_chunks = list(signal_chunks)[::-1]
            navigation_chunks = ['auto'] * len(
                self.axes_manager.navigation_shape)
            if LooseVersion(dask.__version__) >= LooseVersion("2.30.0"):
                kwargs = {'balance': True}
            else:
                kwargs = {}
            chunks = self.data.rechunk([*navigation_chunks, *signal_chunks],
                                       **kwargs).chunks

        # Get the slice of the corresponding chunk
        signal_size = len(signal_shape)
        signal_chunks = tuple(chunks[i - signal_size]
                              for i in range(signal_size))
        _logger.info(f"Signal chunks: {signal_chunks}")
        isig_slice = get_signal_chunk_slice(index, chunks)

        _logger.info(f'Computing sum over signal dimension: {isig_slice}')
        axes = [axis.index_in_array for axis in self.axes_manager.signal_axes]
        navigator = self.isig[isig_slice].sum(axes)
        navigator.compute(show_progressbar=show_progressbar)
        navigator.original_metadata.set_item('sum_from', isig_slice)

        self.navigator = navigator.T