# 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


from MEHI.utils.tool import exeTime
    from skimage.filters import threshold_otsu, threshold_adaptive
    from skimage.morphology import disk, binary_opening
    from skimage.feature import peak_local_max
    from skimage.morphology import watershed, remove_small_objects
    from scipy import ndimage
    from MEHI.udf._moment import moment
    from scipy import ndimage as ndi
import numpy as np
    import scipy.ndimage as ndi
    import pandas as pd
        threshold = threshold_otsu(frame)
        binary2 = frame > threshold
        binary = binary1 & binary2
        return binary
    if method=='adaptive':
        block_size = args[0]
        return np.array(map(adaptive, img_stack))
    elif method == 'otsu':
        return np.array(map(otsu, img_stack))
    elif method == 'duel':
        return np.array(map(duel, img_stack))
    else:
        raise "Bad Threshold Method", method

def peak_filter(image_stack, smooth_size):
    def func(frame):
        opened = binary_opening(frame, disk(smooth_size))
        opened = opened & frame
        return opened
    return np.array(map(func, img_stack))

@exeTime
def watershed_3d(image_stack, binary, min_distance=10, min_radius=6):
    binary = remove_small_objects(binary, min_radius, connectivity=3)
    distance = ndimage.distance_transform_edt(binary)
    local_maxi = peak_local_max(distance, min_distance=min_distance, indices=False, labels=image_stack)
    markers = ndimage.label(local_maxi)[0]
    labeled_stack = watershed(-distance, markers, mask=binary)
    return labeled_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/segmentation.py
from MEHI.utils.tool import exeTime
    from skimage.filters import threshold_otsu, threshold_adaptive
    from skimage.morphology import disk, binary_opening
    from skimage.feature import peak_local_max
    from skimage.morphology import watershed, remove_small_objects
    from scipy import ndimage
    from MEHI.udf._moment import moment
    from scipy import ndimage as ndi
import numpy as np
    import scipy.ndimage as ndi
    import pandas as pd

        threshold = threshold_otsu(frame)
        binary2 = frame > threshold
        binary = binary1 & binary2
        return binary
    if method=='adaptive':
        block_size = args[0]
        return np.array(map(adaptive, img_stack))
    elif method == 'otsu':
        return np.array(map(otsu, img_stack))
    elif method == 'duel':
        return np.array(map(duel, img_stack))
    else:
        raise "Bad Threshold Method", method

def peak_filter(image_stack, smooth_size):
    def func(frame):
        opened = binary_opening(frame, disk(smooth_size))
        opened = opened & frame
        return opened
    return np.array(map(func, img_stack))

@exeTime
def watershed_3d(image_stack, binary, min_distance=10, min_radius=6):
    binary = remove_small_objects(binary, min_radius, connectivity=3)
    distance = ndimage.distance_transform_edt(binary)
    local_maxi = peak_local_max(distance, min_distance=min_distance, indices=False, labels=image_stack)
    markers = ndimage.label(local_maxi)[0]
    labeled_stack = watershed(-distance, markers, mask=binary)
    return labeled_stack


@exeTime