# Path: MEHI/utils/tool.py
# def exeTime(func):
#     '''
#     Usage:
#      - just put '@exeTime'(with out quotation) before your function
#      - will show the running time
#     '''
#     import time
#     def wraper(*args, **args2):
#         s = time.time()
#         ret = func(*args, **args2)
#         e = time.time()
#         msg = "%3.3fs taken for {%s}" % (e-s, func.__name__)
#         log('time')(msg)
#         return ret
#     return wraper


import skimage.external.tifffile as tiff
import numpy as np
import math
    import skimage.morphology as mor
    import skimage.morphology as mor
from MEHI.utils.tool import exeTime
    from MEHI.udf._intensity import normalization
    from MEHI.udf._intensity import saturation
    from MEHI.udf._subtract_bg import subtract_Background
    from skimage.filters import rank
    from skimage.morphology import disk

# Date     : 2015/07/23 20:10:23
# FileName : preprocess.py
################################


@exeTime
def stripe_removal(img_stack):
    def func(frame):
        _dtype = frame.dtype
        kernel = mor.disk(3)
        frameWP = frame - mor.white_tophat(frame, kernel) * (mor.white_tophat(frame, kernel) > 1000).astype(float)
        kernel = mor.rectangle(25, 1)
        closed = mor.closing(frameWP, kernel)
        opened = mor.opening(closed, kernel)
        result = ((frameWP.astype(float) / opened.astype(float)) * 3000.0)
        return result.astype(_dtype)
    return np.array(map(func, img_stack)).astype(img_stack[0].dtype)

@exeTime
def intensity_normalization(img_stack, dtype=None):
    def func(frame):
        return normalization(frame, dtype)
    return np.array(map(func, img_stack))

@exeTime
def saturation(img_stack, precent):
    def func(frame):
        return saturation(frame, precent)
    return np.array(map(func, img_stack))


# Path: MEHI/utils/tool.py
# def exeTime(func):
#     '''
#     Usage:
#      - just put '@exeTime'(with out quotation) before your function
#      - will show the running time
#     '''
#     import time
#     def wraper(*args, **args2):
#         s = time.time()
#         ret = func(*args, **args2)
#         e = time.time()
#         msg = "%3.3fs taken for {%s}" % (e-s, func.__name__)
#         log('time')(msg)
#         return ret
#     return wraper


# Path: MEHI/serial/preprocess.py
import skimage.external.tifffile as tiff
import numpy as np
import math
    import skimage.morphology as mor
    import skimage.morphology as mor
from MEHI.utils.tool import exeTime
    from MEHI.udf._intensity import normalization
    from MEHI.udf._intensity import saturation
    from MEHI.udf._subtract_bg import subtract_Background
    from skimage.filters import rank
    from skimage.morphology import disk

# Date     : 2015/07/23 20:10:23
# FileName : preprocess.py
################################


@exeTime
def stripe_removal(img_stack):
    def func(frame):
        _dtype = frame.dtype
        kernel = mor.disk(3)
        frameWP = frame - mor.white_tophat(frame, kernel) * (mor.white_tophat(frame, kernel) > 1000).astype(float)
        kernel = mor.rectangle(25, 1)
        closed = mor.closing(frameWP, kernel)
        opened = mor.opening(closed, kernel)
        result = ((frameWP.astype(float) / opened.astype(float)) * 3000.0)
        return result.astype(_dtype)
    return np.array(map(func, img_stack)).astype(img_stack[0].dtype)

@exeTime
def intensity_normalization(img_stack, dtype=None):
    def func(frame):
        return normalization(frame, dtype)
    return np.array(map(func, img_stack))

@exeTime
def saturation(img_stack, precent):
    def func(frame):
        return saturation(frame, precent)
    return np.array(map(func, img_stack))


@exeTime
