# 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
# 
# def bar(level):
#     '''
#     Usage:
#      - this will show a progress bar
#      - e.g. bar('info')(msg, i, 100)
#     '''
#     from MEHI.utils.configuration import loglevel
#     if level in loglevel:
#         def wraper(msg, i, end):
#             if i <= end:
#                 sys.stdout.write('[' + bcolors.decode(level) + level + bcolors.decode('endc') + '] ('+ str(i)+'/' + str(end) + ') '+ msg + '\r')
#                 sys.stdout.flush()
#             if i == end:
#                 print
#         return wraper
#     else:
#         def wraper(msg, i, end):
#             pass
#         return wraper


import skimage.external.tifffile as tiff
import numpy as np
import os
    import pandas as pd
    import pandas as pd
    import zlib
from MEHI.utils.tool import exeTime,bar
################################
# Author   : septicmk
# Date     : 2015/07/25 10:44:53
# FileName : IO.py
################################


@exeTime
def load_tiff(pwd, start_index=None, end_index=None):
    '''
    Usage:
     - just read all the image under the pwd
    '''
    if os.path.isdir(pwd):
        img_stack = []
        names = []
        for imgname in os.listdir(pwd):
            if imgname.endswith('.tif'):
                names.append(imgname)
        names = sorted(names, key = lambda x: int(filter(str.isdigit, x)))
        if (start_index) and (end_index) and (0 <= start_index < end_index) and (start_index < end_index <= len(names)):
            names = names[start_index:end_index]
        for z,imgname in enumerate(names):
            msg = "reading %d-th frame" % (z+1)
            end = len(names)

# 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
# 
# def bar(level):
#     '''
#     Usage:
#      - this will show a progress bar
#      - e.g. bar('info')(msg, i, 100)
#     '''
#     from MEHI.utils.configuration import loglevel
#     if level in loglevel:
#         def wraper(msg, i, end):
#             if i <= end:
#                 sys.stdout.write('[' + bcolors.decode(level) + level + bcolors.decode('endc') + '] ('+ str(i)+'/' + str(end) + ') '+ msg + '\r')
#                 sys.stdout.flush()
#             if i == end:
#                 print
#         return wraper
#     else:
#         def wraper(msg, i, end):
#             pass
#         return wraper


# Path: MEHI/serial/IO.py
import skimage.external.tifffile as tiff
import numpy as np
import os
    import pandas as pd
    import pandas as pd
    import zlib
from MEHI.utils.tool import exeTime,bar

################################
# Author   : septicmk
# Date     : 2015/07/25 10:44:53
# FileName : IO.py
################################


@exeTime
def load_tiff(pwd, start_index=None, end_index=None):
    '''
    Usage:
     - just read all the image under the pwd
    '''
    if os.path.isdir(pwd):
        img_stack = []
        names = []
        for imgname in os.listdir(pwd):
            if imgname.endswith('.tif'):
                names.append(imgname)
        names = sorted(names, key = lambda x: int(filter(str.isdigit, x)))
        if (start_index) and (end_index) and (0 <= start_index < end_index) and (start_index < end_index <= len(names)):
            names = names[start_index:end_index]
        for z,imgname in enumerate(names):
            msg = "reading %d-th frame" % (z+1)
            end = len(names)

            bar('info')(msg, z+1, end)