exploit_CR.py 2.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- 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

def statistics(path_read, file, paht_save, sample_name):
    # reading    
    chdir(path_read)
    print("reading...", path_read, file)
    image = tifffile.imread(file)

    # counting number of pores
    print("extracting labels...")
    labels = np.unique(image)
    labels = labels[labels !=0]
    print("{} labels where found !".format(len(labels)))
    
    # number of pores
    num_pores = len(labels)
    
    # Variables
    v = np.zeros(num_pores, dtype = int) # volume of pores
    x0 = np.zeros(num_pores, dtype = int)
    y0 = np.zeros(num_pores, dtype = int) 
    z0 = np.zeros(num_pores, dtype = int) 
    xf = np.zeros(num_pores, dtype = int)
    yf = np.zeros(num_pores, dtype = int) 
    zf = np.zeros(num_pores, dtype = int) 
    
    
    for i in range(num_pores):
        label = labels[i]
        pore = (image == label)
        v[i] = np.count_nonzero(pore)
        coords = np.where(image == label) # z, y, x
        x0[i] = np.min(coords[2])
        y0[i] = np.min(coords[1])
        z0[i] = np.min(coords[0])
        xf[i] = np.max(coords[2])
        yf[i] = np.max(coords[1])
        zf[i] = np.max(coords[0])
    
    delta_x = xf - x0
    delta_y = yf - y0
    delta_z = zf - z0
    
    connected_to_top = (z0 == 0)
    
    my_data = dict()
    frame = pd.DataFrame(my_data) 
    frame["Label"] = labels
    frame["volume (vox)"] = v
    frame["x0"] = x0
    frame["xf"] = xf
    frame["delta_x"] = delta_x
    frame["y0"] = y0
    frame["yf"] = yf
    frame["delta_y"] = delta_y
    frame["z0"] = z0
    frame["zf"] = zf
    frame["delta_z"] = delta_z
    frame["connected to top"] = np.array(connected_to_top, dtype = np.int)
    
    
    chdir(path_save)
   
    frame.to_csv("statistics_CR_{}.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)