# Path: MEHI/paralleled/IO.py
# @exeTime
# def load_tiff(sc, 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]
#         n = len(names)
#         def func(name):
#             img_pwd = os.path.join(pwd, name)
#             if img_pwd.endswith('.tif'):
#                 img = tiff.imread(img_pwd)
#             return img
#         rdd_file = sc.parallelize(names)
#         return np.squeeze(np.array(rdd_file.map(func).collect()))
#     else:
#         return tiff.imread(pwd)


from MEHI.paralleled.preprocess import *
from MEHI.paralleled.IO import load_tiff
from test_utils import PySparkTestCase
import numpy as np
import os
    def test_invert(self):
        rdd = self.sc.parallelize(self.L_imgs)
        ret = invert(rdd).collect()
        ret = np.array(ret)
        assert (ret.shape == self.L_imgs.shape)
        assert (ret.dtype == self.L_imgs.dtype)
    
    def test_black_tophat(self):
        rdd = self.sc.parallelize(self.L_imgs)
        ret = black_tophat(rdd).collect()
        ret = np.array(ret)
        assert (ret.shape == self.L_imgs.shape)
        assert (ret.dtype == self.L_imgs.dtype)
    
    def test_subtract_Background(self):
        rdd = self.sc.parallelize(self.L_imgs)
        ret = subtract_Background(rdd).collect()
        ret = np.array(ret)
        assert (ret.shape == self.L_imgs.shape)
        assert (ret.dtype == self.L_imgs.dtype)
    
    def test_shrink(self):
        rdd = self.sc.parallelize(self.L_imgs)
        ret = shrink(rdd).collect()
        ret = np.array(ret)
        assert (ret.dtype == self.L_imgs.dtype)
        assert (ret.shape == (10, 256, 256))
    
    def test_projection(self):
        maxp = projection(self.L_imgs, 'max')

# Path: MEHI/paralleled/IO.py
# @exeTime
# def load_tiff(sc, 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]
#         n = len(names)
#         def func(name):
#             img_pwd = os.path.join(pwd, name)
#             if img_pwd.endswith('.tif'):
#                 img = tiff.imread(img_pwd)
#             return img
#         rdd_file = sc.parallelize(names)
#         return np.squeeze(np.array(rdd_file.map(func).collect()))
#     else:
#         return tiff.imread(pwd)


# Path: test/test_p_preprocess.py
from MEHI.paralleled.preprocess import *
from MEHI.paralleled.IO import load_tiff
from test_utils import PySparkTestCase
import numpy as np
import os

    def test_invert(self):
        rdd = self.sc.parallelize(self.L_imgs)
        ret = invert(rdd).collect()
        ret = np.array(ret)
        assert (ret.shape == self.L_imgs.shape)
        assert (ret.dtype == self.L_imgs.dtype)
    
    def test_black_tophat(self):
        rdd = self.sc.parallelize(self.L_imgs)
        ret = black_tophat(rdd).collect()
        ret = np.array(ret)
        assert (ret.shape == self.L_imgs.shape)
        assert (ret.dtype == self.L_imgs.dtype)
    
    def test_subtract_Background(self):
        rdd = self.sc.parallelize(self.L_imgs)
        ret = subtract_Background(rdd).collect()
        ret = np.array(ret)
        assert (ret.shape == self.L_imgs.shape)
        assert (ret.dtype == self.L_imgs.dtype)
    
    def test_shrink(self):
        rdd = self.sc.parallelize(self.L_imgs)
        ret = shrink(rdd).collect()
        ret = np.array(ret)
        assert (ret.dtype == self.L_imgs.dtype)
        assert (ret.shape == (10, 256, 256))
    
    def test_projection(self):
        maxp = projection(self.L_imgs, 'max')

        minp = projection(self.L_imgs, 'min')