# Path: rivuletpy/utils/io.py
# def writetiff3d(filepath, block):
#     from libtiff import TIFF
#     try:
#         os.remove(filepath)
#     except OSError:
#         pass
# 
#     tiff = TIFF.open(filepath, mode='w')
#     block = np.swapaxes(block, 0, 1)
# 
#     for z in range(block.shape[2]):
#         tiff.write_image(np.flipud(block[:, :, z]), compression=None)
#     tiff.close()


from itertools import cycle
from scipy.ndimage import binary_dilation, binary_erosion
from scipy.ndimage import gaussian_filter, gaussian_gradient_magnitude
from scipy.ndimage.measurements import center_of_mass
from scipy.ndimage.morphology import generate_binary_structure
from rivuletpy.utils.io import writetiff3d
    from matplotlib import pyplot as ppl
    from mayavi import mlab
import math
import numpy as np
import skfmm

            # Calculate the new centroid using the soma volume
            newsomapos = center_of_mass(full_soma_mask)

            # Round the float coordinates into integers
            newsomapos = [math.floor(p) for p in newsomapos]
            self.centroid = newsomapos
            self.radius = somaradius
            self.mask = full_soma_mask
        else:
            if not silent:
                print('Reconstructing Soma with Simple Mask')
            self.centroid = somapos
            self.radius = somaradius
            self.simple_mask(bimg)

    def pad(self, crop_region, original_shape):
        xmin = crop_region[0, 0]
        ymin = crop_region[1, 0]
        zmin = crop_region[2, 0]
        xmax = crop_region[0, 1]
        ymax = crop_region[1, 1]
        zmax = crop_region[2, 1]
        self.mask = np.pad(self.mask, ((xmin, original_shape[0] - xmax),
                                       (ymin, original_shape[1] - ymax),
                                       (zmin, original_shape[2] - zmax)),
                           mode='constant',
                           constant_values=0)

    def save(self, fname):

# Path: rivuletpy/utils/io.py
# def writetiff3d(filepath, block):
#     from libtiff import TIFF
#     try:
#         os.remove(filepath)
#     except OSError:
#         pass
# 
#     tiff = TIFF.open(filepath, mode='w')
#     block = np.swapaxes(block, 0, 1)
# 
#     for z in range(block.shape[2]):
#         tiff.write_image(np.flipud(block[:, :, z]), compression=None)
#     tiff.close()


# Path: rivuletpy/soma.py
from itertools import cycle
from scipy.ndimage import binary_dilation, binary_erosion
from scipy.ndimage import gaussian_filter, gaussian_gradient_magnitude
from scipy.ndimage.measurements import center_of_mass
from scipy.ndimage.morphology import generate_binary_structure
from rivuletpy.utils.io import writetiff3d
    from matplotlib import pyplot as ppl
    from mayavi import mlab
import math
import numpy as np
import skfmm


            # Calculate the new centroid using the soma volume
            newsomapos = center_of_mass(full_soma_mask)

            # Round the float coordinates into integers
            newsomapos = [math.floor(p) for p in newsomapos]
            self.centroid = newsomapos
            self.radius = somaradius
            self.mask = full_soma_mask
        else:
            if not silent:
                print('Reconstructing Soma with Simple Mask')
            self.centroid = somapos
            self.radius = somaradius
            self.simple_mask(bimg)

    def pad(self, crop_region, original_shape):
        xmin = crop_region[0, 0]
        ymin = crop_region[1, 0]
        zmin = crop_region[2, 0]
        xmax = crop_region[0, 1]
        ymax = crop_region[1, 1]
        zmax = crop_region[2, 1]
        self.mask = np.pad(self.mask, ((xmin, original_shape[0] - xmax),
                                       (ymin, original_shape[1] - ymax),
                                       (zmin, original_shape[2] - zmax)),
                           mode='constant',
                           constant_values=0)

    def save(self, fname):

        writetiff3d(fname, self.mask * 255)