# Path: molyso/generic/signal.py
# def find_phase(signal_1=None, signal_2=None,
#                fft_1=None, fft_2=None,
#                return_1=False, return_2=False):
#     """
#     Finds the phase (time shift) between two signals.
#     Either signalX or fftX should be set; the FFTs can be returned
#     in order to cache them locally...
# 
#     :param signal_1: first input signal
#     :type signal_1: numpy.ndarray or None
#     :param signal_2: second input signal
#     :type signal_2: numpy.ndarray or None
#     :param fft_1: first input fft
#     :type fft_1: numpy.ndarray or None
#     :param fft_2: second input fft
#     :type fft_2: numpy.ndarray or None
#     :param return_1: whether fft1 should be returned
#     :type return_1: bool
#     :param return_2: whether fft2 should be returned
#     :type return_2: bool
#     :return: (shift, (fft1 if return_1), (fft2 if return_2))
#     :rtype: tuple
# 
#     >>> find_phase(np.array([0, 1, 0, 0, 0]), np.array([0, 0, 0, 1, 0]))
#     (2,)
#     """
# 
#     if signal_1 is not None and fft_1 is None:
#         fft_1 = fft(signal_1)
#     if signal_2 is not None and fft_2 is None:
#         fft_2 = fft(signal_2)
# 
#     corr = ifft(fft_1 * -fft_2.conjugate())
# 
#     corr = np.absolute(corr)
#     the_max = np.argmax(corr)
#     # if the_max > 2 and the_max < (len(corr) - 2):
#     #    sur = corr[the_max-1:the_max+2]
#     #    the_max += -0.5*sur[0] + 0.5*sur[2]
# 
#     the_max = -the_max if the_max < len(fft_1) / 2 else len(fft_1) - the_max
# 
#     result = (the_max,)
#     if return_1:
#         result += (fft_1,)
#     if return_2:
#         result += (fft_2,)
#     return result
# 
# def vertical_mean(image):
#     """
#     Calculates the vertical mean of an image.
#     Note: Image is assumed HORIZONTAL x VERTICAL.
# 
#     :param image:
#     :return:
# 
#     >>> vertical_mean(np.array([[ 1,  2,  3,  4],
#     ...                            [ 5,  6,  7,  8],
#     ...                            [ 9, 10, 11, 12],
#     ...                            [13, 14, 15, 16]]))
#     array([ 2.5,  6.5, 10.5, 14.5])
#     """
#     return np.mean(image, axis=1)
# 
# def horizontal_mean(image):
#     """
#     Calculates the horizontal mean of an image.
#     Note: Image is assumed HORIZONTAL x VERTICAL.
# 
#     :param image: input image
#     :type image: numpy.ndarray
#     :return:
# 
#     >>> horizontal_mean(np.array([[ 1,  2,  3,  4],
#     ...                            [ 5,  6,  7,  8],
#     ...                            [ 9, 10, 11, 12],
#     ...                            [13, 14, 15, 16]]))
#     array([ 7.,  8.,  9., 10.])
#     """
#     return np.mean(image, axis=0)


from .signal import find_phase, vertical_mean, horizontal_mean
import numpy as np
which registers images by checking the individual horizontal and vertical shifts
"""
from __future__ import division, unicode_literals, print_function




def translation_2x1d(image_a=None, image_b=None, ffts_a=(), ffts_b=(), return_a=False, return_b=False):
    """

    :param image_a:
    :param image_b:
    :param ffts_a:
    :param ffts_b:
    :param return_a:
    :param return_b:
    :return:
    """
    if ffts_a != ():
        fft_av, fft_ah = ffts_a
        signal_av, signal_ah = None, None
    else:
        fft_av, fft_ah = None, None
        signal_av, signal_ah = vertical_mean(image_a), horizontal_mean(image_a)

    if ffts_b != ():
        fft_bv, fft_bh = ffts_b
        signal_bv, signal_bh = None, None
    else:
        fft_bv, fft_bh = None, None

# Path: molyso/generic/signal.py
# def find_phase(signal_1=None, signal_2=None,
#                fft_1=None, fft_2=None,
#                return_1=False, return_2=False):
#     """
#     Finds the phase (time shift) between two signals.
#     Either signalX or fftX should be set; the FFTs can be returned
#     in order to cache them locally...
# 
#     :param signal_1: first input signal
#     :type signal_1: numpy.ndarray or None
#     :param signal_2: second input signal
#     :type signal_2: numpy.ndarray or None
#     :param fft_1: first input fft
#     :type fft_1: numpy.ndarray or None
#     :param fft_2: second input fft
#     :type fft_2: numpy.ndarray or None
#     :param return_1: whether fft1 should be returned
#     :type return_1: bool
#     :param return_2: whether fft2 should be returned
#     :type return_2: bool
#     :return: (shift, (fft1 if return_1), (fft2 if return_2))
#     :rtype: tuple
# 
#     >>> find_phase(np.array([0, 1, 0, 0, 0]), np.array([0, 0, 0, 1, 0]))
#     (2,)
#     """
# 
#     if signal_1 is not None and fft_1 is None:
#         fft_1 = fft(signal_1)
#     if signal_2 is not None and fft_2 is None:
#         fft_2 = fft(signal_2)
# 
#     corr = ifft(fft_1 * -fft_2.conjugate())
# 
#     corr = np.absolute(corr)
#     the_max = np.argmax(corr)
#     # if the_max > 2 and the_max < (len(corr) - 2):
#     #    sur = corr[the_max-1:the_max+2]
#     #    the_max += -0.5*sur[0] + 0.5*sur[2]
# 
#     the_max = -the_max if the_max < len(fft_1) / 2 else len(fft_1) - the_max
# 
#     result = (the_max,)
#     if return_1:
#         result += (fft_1,)
#     if return_2:
#         result += (fft_2,)
#     return result
# 
# def vertical_mean(image):
#     """
#     Calculates the vertical mean of an image.
#     Note: Image is assumed HORIZONTAL x VERTICAL.
# 
#     :param image:
#     :return:
# 
#     >>> vertical_mean(np.array([[ 1,  2,  3,  4],
#     ...                            [ 5,  6,  7,  8],
#     ...                            [ 9, 10, 11, 12],
#     ...                            [13, 14, 15, 16]]))
#     array([ 2.5,  6.5, 10.5, 14.5])
#     """
#     return np.mean(image, axis=1)
# 
# def horizontal_mean(image):
#     """
#     Calculates the horizontal mean of an image.
#     Note: Image is assumed HORIZONTAL x VERTICAL.
# 
#     :param image: input image
#     :type image: numpy.ndarray
#     :return:
# 
#     >>> horizontal_mean(np.array([[ 1,  2,  3,  4],
#     ...                            [ 5,  6,  7,  8],
#     ...                            [ 9, 10, 11, 12],
#     ...                            [13, 14, 15, 16]]))
#     array([ 7.,  8.,  9., 10.])
#     """
#     return np.mean(image, axis=0)


# Path: molyso/generic/registration.py
from .signal import find_phase, vertical_mean, horizontal_mean
import numpy as np

which registers images by checking the individual horizontal and vertical shifts
"""
from __future__ import division, unicode_literals, print_function




def translation_2x1d(image_a=None, image_b=None, ffts_a=(), ffts_b=(), return_a=False, return_b=False):
    """

    :param image_a:
    :param image_b:
    :param ffts_a:
    :param ffts_b:
    :param return_a:
    :param return_b:
    :return:
    """
    if ffts_a != ():
        fft_av, fft_ah = ffts_a
        signal_av, signal_ah = None, None
    else:
        fft_av, fft_ah = None, None
        signal_av, signal_ah = vertical_mean(image_a), horizontal_mean(image_a)

    if ffts_b != ():
        fft_bv, fft_bh = ffts_b
        signal_bv, signal_bh = None, None
    else:
        fft_bv, fft_bh = None, None

        signal_bv, signal_bh = vertical_mean(image_b), horizontal_mean(image_b)