exec_postprocessingΒΆ

This section contains the exec_postprocessing script.

Download file: exec_postprocessing.py

  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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
###########################################################################
# (C) 2016 Elettra - Sincrotrone Trieste S.C.p.A.. All rights reserved.   #
#                                                                         #
#                                                                         #
# This file is part of STP-Core, the Python core of SYRMEP Tomo Project,  #
# a software tool for the reconstruction of experimental CT datasets.     #
#                                                                         #
# STP-Core is free software: you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the   #
# Free Software Foundation, either version 3 of the License, or (at your  #
# option) any later version.                                              #
#                                                                         #
# STP-Core is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or   #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License    #
# for more details.                                                       #
#                                                                         #
# You should have received a copy of the GNU General Public License       #
# along with STP-Core. If not, see <http://www.gnu.org/licenses/>.        #
#                                                                         #
###########################################################################

#
# Author: Francesco Brun
# Last modified: July, 8th 2016
#

from sys import argv, exit
from glob import glob
from os import linesep
from os.path import sep, basename, exists
from time import time
from multiprocessing import Process, Lock

# pystp-specific:
from stp_core.postprocess.postprocess import postprocess

from tifffile import imread, imsave

def _write_log(lock, fname, logfilename, cputime, iotime):            

    lock.acquire()
    try: 
        # Print out execution time:
        log = open(logfilename,"a")
        log.write(linesep + "\t%s processed (CPU: %0.3f sec - I/O: %0.3f sec)." % (basename(fname), cputime, iotime))
        log.close() 

    finally:
        lock.release()  

def _process(lock, int_from, int_to, files, outpath, convert_opt, crop_opt, outprefix, logfilename):

    # Process the required subset of images:
    for i in range(int_from, int_to + 1):                 
        
        # Read i-th slice:
        t0 = time() 
        im = imread(files[i])
        t1 = time() 
            
        # Post process the image:
        im = postprocess(im, convert_opt, crop_opt) 

        # Write down post-processed slice:
        t2 = time()
        fname = outpath + outprefix + '_' + str(i).zfill(4) + '.tif'
        imsave(fname, im)
        t3 = time()
                                
        # Write log (atomic procedure - lock used):
        _write_log(lock, fname, logfilename, t2 - t1, (t3 - t2) + (t1 - t0) )


def main(argv):          
    """To do...

    Usage
    -----
    

    Parameters
    ---------
           
    Example
    --------------------------
    The following line processes the first ten TIFF files of input path 
    "/home/in" and saves the processed files to "/home/out" with the 
    application of the Boin and Haibel filter with smoothing via a Butterworth
    filter of order 4 and cutoff frequency 0.01:

    reconstruct 0 4 C:\Temp\Dullin_Aug_2012\sino_noflat C:\Temp\Dullin_Aug_2012\sino_noflat\output 
    9.0 10.0 0.0 0.0 0.0 true sino slice C:\Temp\Dullin_Aug_2012\sino_noflat\tomo_conv flat dark

    """
    lock = Lock()
    # Get the from and to number of files to process:
    int_from = int(argv[0])
    int_to = int(argv[1])
       
    # Get input and output paths:
    inpath = argv[2]
    outpath = argv[3]
    
    if not inpath.endswith(sep): inpath += sep
    if not outpath.endswith(sep): outpath += sep

    # Get parameters:
    convert_opt = argv[4]
    crop_opt = argv[5]  

    outprefix = argv[6]

    # Number of threads to use and logfile:
    nr_threads = int(argv[7])
    logfilename = argv[8]       

    # Get the files in infile:
    log = open(logfilename,"w")
    log.write(linesep + "\tInput TIFF folder: %s" % (inpath))   
    log.write(linesep + "\tOutput TIFF folder: %s" % (outpath))     
    log.write(linesep + "\t--------------")         
    if (int_to != -1):
        log.write(linesep + "\tThe subset [%d,%d] of the input files will be considered." % (int_from, int_to))
    log.write(linesep + "\tCropping:")
    crop_opt_num = crop_opt.split(":")
    log.write(linesep + "\t\tTop: %s pixels" % (crop_opt_num[0]))
    log.write(linesep + "\t\tBottom: %s pixels" % (crop_opt_num[1]))
    log.write(linesep + "\t\tLeft: %s pixels" % (crop_opt_num[2]))
    log.write(linesep + "\t\tRight: %s pixels" % (crop_opt_num[3]))
    conv_method, conv_args = convert_opt.split(":", 1)
    if (conv_method == "linear8"):
        min, max = conv_args.split(";")   
        log.write(linesep + "\tConversion to 8-bit by remapping range [%s,%s] to [0,255]." % (min, max))
    elif (conv_method == "linear"):
        min, max = conv_args.split(";")   
        log.write(linesep + "\tConversion to 16-bit by remapping range [%s,%s] to [0,65535]." % (min, max))
    log.write(linesep + "\t--------------") 
    log.write(linesep + "\tBrowsing input folder...")   
    log.close()
        
    files = sorted(glob(inpath + '*.tif*'))
    num_files = len(files)      
    
    if ((int_to >= num_files) or (int_to == -1)):
        int_to = num_files - 1
    
    # Log infos:
    log = open(logfilename,"a")
    log.write(linesep + "\tInput folder browsed correctly.")    
    log.close() 

    # Run several threads for independent computation without waiting for threads completion:
    for num in range(nr_threads):
        start = ( (int_to - int_from + 1) / nr_threads)*num + int_from
        if (num == nr_threads - 1):
            end = int_to
        else:
            end = ( (int_to - int_from + 1) / nr_threads)*(num + 1) + int_from - 1
        Process(target=_process, args=(lock, start, end, files, outpath, convert_opt, crop_opt, outprefix, logfilename )).start()

    #start = 0
    #end = num_files - 1
    #process(lock, start, end, files, outpath, convert_opt, crop_opt, outprefix, logfilename )

    #0 -1 C:\Temp\BrunGeorgos C:\Temp\BrunGeorgos\slice_8 linear8:-0.01;0.01 10:10:10:20 slice C:\Temp\log_00_conv.txt


if __name__ == "__main__":
    main(argv[1:])