-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeconvolution.py
More file actions
executable file
·74 lines (64 loc) · 2.65 KB
/
Deconvolution.py
File metadata and controls
executable file
·74 lines (64 loc) · 2.65 KB
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
import argparse
import glob
import os.path
import platform
import subprocess
from multiprocessing import Pool
import imageio
import scipy.stats
import numpy as np
from OPUtils import OPUtils
class Deconvolution:
def __init__(self):
self.inputpat = ""
self.outputdir = ""
self.psf = None
self.fnpostfix = '_deconv'
self.utils = OPUtils()
def deconvolve(self, files):
pool = Pool(processes=self.utils.dec_processes)
selfs = [self for i in range(len(files))]
outfiles = pool.map(process, zip(selfs, files))
return outfiles
def subtractBG(self, files):
for f in files:
img = imageio.volread(f)
mode = scipy.stats.mode(img, axis=None)
img = img.astype(np.float32)
img = img - mode.mode
img[img < 0] = 0
img = img.astype(np.uint16)
imageio.volwrite(f, img)
def run(self, inputpat, outputdir, psf, subtract=True):
self.inputpat = inputpat
self.outputdir = outputdir
self.psf = psf
files = glob.glob(inputpat)
outfiles = self.deconvolve(files)
if subtract:
self.subtractBG(outfiles)
def process(self, f):
shellBool = True
if platform.system() == 'Linux':
shellBool = False
cmd_stack = [self.utils.java, self.utils.java_params, '-jar', self.utils.deconvolutionlab, 'Run', '-algorithm', self.utils.dec_algorithm, self.utils.dec_params, '-path', self.outputdir, '-monitor', 'no', '-verbose', 'mute', '-stats', 'no', '-psf', 'file', self.psf]
basefn = os.path.basename(f)
outfile = basefn[:basefn.rfind('.')]+self.fnpostfix
cmd_stack += ['-image', 'file', f]
cmd_stack += ['-out', 'stack', outfile, 'intact', 'short', 'noshow']
subprocess.call(cmd_stack, shell=shellBool)
return os.path.join(self.outputdir,outfile+".tif")
def parseArgs():
parser = argparse.ArgumentParser(description="Deconvolve Opera Phenix imaged 3D confocal stacks. Run separately for each channel. Uses DeconvolutionLab2 (http://bigwww.epfl.ch/deconvolution/deconvolutionlab2/) library.")
parser.add_argument('inputpat', help="input file pattern e.g. \"*.tif\" or \"*A01*\"")
parser.add_argument('outputdir', help="output directory")
parser.add_argument('psf', help="PSF image file path")
parser.add_argument('-s', '--subtract', default=True, action='store_true', help="subtract background signal from deconvolved stacks")
args = parser.parse_args()
return args
def main():
args = parseArgs()
dec = Deconvolution()
dec.run(args.inputpat, args.outputdir, args.psf, args.subtract)
if __name__ == "__main__":
main()