exploit_CR_parallelized.py 2.39 KiB
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 11 19:09:06 2019

@author: german.martinez-carvajal
"""

# -*- coding: utf-8 -*-
"""
Created on Wed Sep 11 18:25:20 2019

@author: german.martinez-carvajal
"""

import tifffile
from os import chdir
import numpy as np
import pandas as pd
import multiprocessing as mp

def computations(label):
    pore = (image == label)
    v = np.count_nonzero(pore)
    coords = np.where(image == label) # z, y, x
    x0 = np.min(coords[2])
    y0 = np.min(coords[1])
    z0 = np.min(coords[0])
    xf = np.max(coords[2])
    yf = np.max(coords[1])
    zf = np.max(coords[0])
    delta_x = xf - x0
    delta_y = yf - y0
    delta_z = zf - z0
    connected = int(z0 == 0)
    return(np.array([label,v,x0,y0,z0,xf,yf,zf, delta_x, delta_y, delta_z, connected]))
    
    
def statistics(path_read, file, paht_save, sample_name, n_p):
    # reading    
    chdir(path_read)
    print("reading...", path_read, file)
    global image
    image = tifffile.imread(file)

    # counting number of pores
    print("extracting labels...")
    labels = np.unique(image)
    labels = labels[labels !=0]
    print("{} labels were found !".format(len(labels)))
    # number of pores
    num_pores = len(labels)

    print('computing ...')
    # 12 columns : label,v,x0,y0,z0,xf,yf,zf, delta_x, delta_y, delta_z, connected
    my_statistics = np.zeros((num_pores, 12) ,dtype = int)
    
    # Step 1: Init multiprocessing.Pool
    if n_p == None:
        pool = mp.Pool(mp.cpu_count())
    else:
        assert n_p < mp.cpu_count()
        pool = mp.Pool(n_p)
    
    # Step 2: `pool.apply` the `howmany_within_range()`
    my_statistics = pool.map(computations, [label for label in labels]) 
    # Step 3: Don't forget to close
    pool.close()     
    
    frame = pd.DataFrame(data=my_statistics,    # values
                                index = range(num_pores),    # 1st column as index
                                columns=["label","v (vox)","x0","y0","z0","xf","yf","zf", "delta_x", "delta_y", "delta_z", "connected to top ?"] )
    chdir(path_save)
    frame.to_csv("statistics_CR_yes_paralelized_{}.csv".format(sample_name) ,sep = ';')
    print('finished treating sample {}'.format(sample_name))

path_read = '/home/german.martinez-carvajal/Desktop/These/Connecting_CR/test'
path_save = path_read
file = 'small_top.tif'
sample_name = "small_top"
statistics(path_read, file, path_save, sample_name, n_p = None)