# Path: MEHI/paralleled/preprocess.py
# def stripe_removal(rdd):
#     def func(frame):
# def intensity_normalization(rdd, dtype=None):
#     def func(frame):
# def saturation(rdd, precent):
#     def func(frame):
# def flip(rdd):
#     def func(frame):
# def invert(rdd):
#     def func(frame):
# def black_tophat(rdd, size=15):
#     def func(frame):
# def subtract_Background(rdd, size=12):
#     def func(frame):
# def shrink(rdd, shrink_size=2):
#     def func(frame):
# def projection(img_stack, method='max'):
# def smooth(rdd, smooth_size):
#     def func(frame):
# def blockshaped_all(img_stack, nrows, ncols):
#     def blockshaped(arr, nrows, ncols):
# def recovershape_all(img_stack, nrows, ncols):
#     def recovershape(arr_list, nrows, ncols):
#         _X,_Y = frame.shape
# 
# Path: MEHI/paralleled/segmentation.py
# def threshold(rdd, method='adaptive', *args):
#     def adaptive(frame):
#     def otsu(frame):
#     def duel(frame):
#     def phansalkar(frame):
# def peak_filter(rdd, smooth_size):
#     def func(frame):
# def watershed(rdd, min_radius):
#     def func(dframe):
# def properties(labeled_stack, image_stack, min_radius, max_radius):
# def clustering(prop, threshold):
# def df_average(df, weights_column):
# def fusion(labeled_stack, image_stack, min_radius, max_radius):  
# def debug(labeled_stack, image_stack, min_radius, max_radiusm, prop):
# def watershed_3d(image_stack, binary, min_distance=10, min_radius=6):
# def properties_3d(labeled_stack):
# 
# Path: MEHI/paralleled/IO.py
# def load_tiff(sc, pwd, start_index=None, end_index=None):
#         def func(name):
# def save_tiff(img_stack, pwd):
# def load_table(pwd):
# def save_table(tab, pwd):
# 
# 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 log(level):
#     '''
#     Usage:
#      - this will just show info
#      - e.g. log('info')(msg)
#     '''
#     from MEHI.utils.configuration import loglevel
#     if level in loglevel:
#         def wraper(msg):
#             print '[' + bcolors.decode(level) + level + bcolors.decode('endc') + '] '+ msg
#         return wraper
#     else:
#         def wraper(msg):
#             pass
#         return wraper


from MEHI.paralleled import preprocess as prep
from MEHI.paralleled import segmentation as seg
from MEHI.paralleled import IO
from MEHI.utils.tool import exeTime, log
from pyspark import SparkContext, SparkConf
import numpy as np
import time
################################
# Author   : septicmk
# Date     : 2015/08/17 11:13:23
# FileName : test.py
################################


conf = SparkConf().setAppName('seg').setMaster('local[128]').set('spark.executor.memory','20g').set('spark.driver.maxResultSize','20g').set('spark.driver.memory','40g').set('spark.local.dir','/dev/shm').set('spark.storage.memoryFraction','0.2').set('spark.default.parallelism','256')

sc = SparkContext(conf=conf)

pwd = '/mnt/xfs_snode21/0401fusion'
s = time.time()

log('info')('load tiff ...')
fus = IO.load_tiff(sc, pwd+'/fus_dev/')
log('info')('tiff load over ...')

ret = []
for y in range(fus.shape[1]):
    t = fus[:,y,:]
    ret.append(t)

ret = np.array(ret)

# Path: MEHI/paralleled/preprocess.py
# def stripe_removal(rdd):
#     def func(frame):
# def intensity_normalization(rdd, dtype=None):
#     def func(frame):
# def saturation(rdd, precent):
#     def func(frame):
# def flip(rdd):
#     def func(frame):
# def invert(rdd):
#     def func(frame):
# def black_tophat(rdd, size=15):
#     def func(frame):
# def subtract_Background(rdd, size=12):
#     def func(frame):
# def shrink(rdd, shrink_size=2):
#     def func(frame):
# def projection(img_stack, method='max'):
# def smooth(rdd, smooth_size):
#     def func(frame):
# def blockshaped_all(img_stack, nrows, ncols):
#     def blockshaped(arr, nrows, ncols):
# def recovershape_all(img_stack, nrows, ncols):
#     def recovershape(arr_list, nrows, ncols):
#         _X,_Y = frame.shape
# 
# Path: MEHI/paralleled/segmentation.py
# def threshold(rdd, method='adaptive', *args):
#     def adaptive(frame):
#     def otsu(frame):
#     def duel(frame):
#     def phansalkar(frame):
# def peak_filter(rdd, smooth_size):
#     def func(frame):
# def watershed(rdd, min_radius):
#     def func(dframe):
# def properties(labeled_stack, image_stack, min_radius, max_radius):
# def clustering(prop, threshold):
# def df_average(df, weights_column):
# def fusion(labeled_stack, image_stack, min_radius, max_radius):  
# def debug(labeled_stack, image_stack, min_radius, max_radiusm, prop):
# def watershed_3d(image_stack, binary, min_distance=10, min_radius=6):
# def properties_3d(labeled_stack):
# 
# Path: MEHI/paralleled/IO.py
# def load_tiff(sc, pwd, start_index=None, end_index=None):
#         def func(name):
# def save_tiff(img_stack, pwd):
# def load_table(pwd):
# def save_table(tab, pwd):
# 
# 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 log(level):
#     '''
#     Usage:
#      - this will just show info
#      - e.g. log('info')(msg)
#     '''
#     from MEHI.utils.configuration import loglevel
#     if level in loglevel:
#         def wraper(msg):
#             print '[' + bcolors.decode(level) + level + bcolors.decode('endc') + '] '+ msg
#         return wraper
#     else:
#         def wraper(msg):
#             pass
#         return wraper


# Path: script/test.py
from MEHI.paralleled import preprocess as prep
from MEHI.paralleled import segmentation as seg
from MEHI.paralleled import IO
from MEHI.utils.tool import exeTime, log
from pyspark import SparkContext, SparkConf
import numpy as np
import time

################################
# Author   : septicmk
# Date     : 2015/08/17 11:13:23
# FileName : test.py
################################


conf = SparkConf().setAppName('seg').setMaster('local[128]').set('spark.executor.memory','20g').set('spark.driver.maxResultSize','20g').set('spark.driver.memory','40g').set('spark.local.dir','/dev/shm').set('spark.storage.memoryFraction','0.2').set('spark.default.parallelism','256')

sc = SparkContext(conf=conf)

pwd = '/mnt/xfs_snode21/0401fusion'
s = time.time()

log('info')('load tiff ...')
fus = IO.load_tiff(sc, pwd+'/fus_dev/')
log('info')('tiff load over ...')

ret = []
for y in range(fus.shape[1]):
    t = fus[:,y,:]
    ret.append(t)

ret = np.array(ret)

IO.save_tiff(ret, pwd+'/yz/yz')