# 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.morphology import disk
    from skimage.filters import rank

def shrink(rdd, shrink_size=2):
    def func(frame):
        _X,_Y = frame.shape
        _sX, _sY = _X / shrink_size, _Y / shrink_size
        shrink_frame = np.zeros([_sX,_sY])
        for i in range(_sX):
            for j in range(_sY):
                shrink_frame[i][j] = sum(frame[i*shrink_size:(i+1)*shrink_size,j*shrink_size:(j+1)*shrink_size].flatten()) / (shrink_size*shrink_size)
        return shrink_frame.astype(frame.dtype)
    return rdd.map(func)

@exeTime
def projection(img_stack, method='max'):
    if method == 'max':
        proj = img_stack.max(axis=0)
    elif method == 'min':
        proj = img_stack.min(axis=0)
    elif method == 'mean':
        proj = img_stack.mean(axis=0)
    else:
        raise "Bad Projection Method", method
    return proj

def smooth(rdd, smooth_size):
    def func(frame):
        smoothed = rank.median(frame,disk(smooth_size))
        smoothed = rank.enhance_contrast(smoothed, disk(smooth_size))
        return smoothed
    return rdd.map(func)


# 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/paralleled/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.morphology import disk
    from skimage.filters import rank

def shrink(rdd, shrink_size=2):
    def func(frame):
        _X,_Y = frame.shape
        _sX, _sY = _X / shrink_size, _Y / shrink_size
        shrink_frame = np.zeros([_sX,_sY])
        for i in range(_sX):
            for j in range(_sY):
                shrink_frame[i][j] = sum(frame[i*shrink_size:(i+1)*shrink_size,j*shrink_size:(j+1)*shrink_size].flatten()) / (shrink_size*shrink_size)
        return shrink_frame.astype(frame.dtype)
    return rdd.map(func)

@exeTime
def projection(img_stack, method='max'):
    if method == 'max':
        proj = img_stack.max(axis=0)
    elif method == 'min':
        proj = img_stack.min(axis=0)
    elif method == 'mean':
        proj = img_stack.mean(axis=0)
    else:
        raise "Bad Projection Method", method
    return proj

def smooth(rdd, smooth_size):
    def func(frame):
        smoothed = rank.median(frame,disk(smooth_size))
        smoothed = rank.enhance_contrast(smoothed, disk(smooth_size))
        return smoothed
    return rdd.map(func)


@exeTime
