forked from jf-lab/chendoscope-minipipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minipipe.py
137 lines (112 loc) · 6.37 KB
/
minipipe.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
'''
Author: Lina Tran
Date: July 14, 2017
Pipeline using motion correction and pre_cnmfe to motion correct, downsample,
and turn files into .tiffs
Requirements:
mkvmerge
install mkvmerge using:
$ sudo apt-get install mkvtoolnix mkvtoolnix-gui
tiffcp
install tiffcp using:
$ sudo apt-get install libtiff-tools
avimerge
install avimerge via transcode:
$ sudo apt-get install transcode
dependencies for av
$ sudo apt-get install libavformat-dev libavcodec-dev libavdevice-dev libavutil-dev \
libswscale-dev libavresample-dev libavfilter-dev
Command Line Usage:
$ python minipipe.py file1.mkv file2.mkv file3.mkv -d 4 -c 5000 --correct_motion
-t 1.8 --target_frame 0 --cores 2 --bigtiff --merge -o output.mkv
Flags:
-d/--downsample: downsample factor, default is 4 (typically 20fps -> 5fps)
-c/--chunk_size: chunk_size, default is 2000
--motion_corr: if you want to motion correct_motion, default is False
-t/--threshold: if you want to indicate threshold, default is 1.0
-target_frame: if you want to choose a frame other than the first to reference
--cores: number of threads to run in parallel, default is 4
--bigtiff: If .mkv(s) amount to > 12Gb, must use this mode or memory error will occur
--merge: merge all the files instead of individually processing them
-o/--output: If --merge, then the name for the merged .tiff file
'''
from pre_cnmfe import process_chunk
import argparse
import pims
import numpy as np
from os import system, path
from joblib import Parallel, delayed
def get_args():
parser = argparse.ArgumentParser(description='Convert and downsample .mkv files to .tiff')
parser.add_argument('input', help='files', nargs='+')
parser.add_argument('-d', '--downsample', help='downsample factor, default is 4', type=int, default=4)
parser.add_argument('-c', '--chunk_size', help='chunk_size of frames, default is 2000', type=int, default=2000)
parser.add_argument('--motion_corr', dest='correct_motion', help='motion correct the given video', action='store_true')
parser.add_argument('--no_motion_corr', dest='correct_motion', help='motion correct the given video', action='store_false')
parser.set_defaults(correct_motion=False)
parser.add_argument('-t', '--threshold', help='threshold for moco, default is 1.0', type=float, default=1.0)
parser.add_argument('--target_frame', help='target frame to reference, default is 0', type=int, default=0)
parser.add_argument('--bigtiff', dest='bigtiff', help='use bigtiff file format for large (>4Gb) .tiffs', action='store_true')
parser.set_defaults(bigtiff=True)
parser.add_argument('--merge', dest='merge', help='merge input files instead of serially processing', action='store_true')
parser.set_defaults(merge=False)
parser.add_argument('-o', '--output', help='if --merge, name of merged file', type=str)
parser.add_argument('-f', '--format', help='format of output file : tiff or avi', type=str, default='tiff')
parser.add_argument('--cores', help='cores to use, default is 1', type=int, default=4)
parser.add_argument('--remove_dead_pixels', action='store_true')
parser.set_defaults(remove_dead_pixels=False)
parser.add_argument('-p', '--pixel_thresh', type=float, default=1.1)
return parser.parse_args()
if __name__ == '__main__':
args = get_args()
if args.merge:
assert args.output != None, "Please provide an output filename if using --merge"
directory = path.dirname(args.input[0])
args.output = directory + '/' + args.output
files = ' + '.join(args.input)
print(files)
system("mkvmerge -o {} {}".format(args.output, files))
filename = args.output
print("Processing {}".format(filename))
directory = path.dirname(filename)
vid = pims.ImageIOReader(filename)
reference = np.round(np.mean(np.array(vid[args.target_frame:args.downsample])[:,:,:,0], axis=0))
save_name = filename.replace('.mkv', '_proc')
if len(vid) % args.chunk_size < 10:
args.chunk_size += 5
starts = np.arange(0,len(vid),args.chunk_size)
stops = starts+args.chunk_size
frames = list(zip(starts, stops))
Parallel(n_jobs=args.cores)(delayed(process_chunk)(filename=filename, start=start, stop=stop, reference=reference, save_name=save_name, format=args.format, ds_factor=args.downsample, correct_motion=args.correct_motion, thresh=args.threshold) for start, stop in frames)
if args.format == 'tiff':
if args.bigtiff:
system("tiffcp -8 {}/*_temp_* {}.tiff".format(directory, save_name))
else:
system("tiffcp {}/*_temp_* {}.tiff".format(directory, save_name))
system("rm {}/*_temp_*".format(directory))
elif args.format == 'avi':
system("avimerge -o {}.avi -i {}/*_temp_*.avi".format(save_name, directory))
system("rm {}/*_temp_*".format(directory))
else:
for filename in args.input:
print("Processing {}".format(filename))
directory = path.dirname(filename)
vid = pims.ImageIOReader(filename)
reference = np.round(np.mean(np.array(vid[args.target_frame:args.downsample])[:,:,:,0], axis=0))
save_name = filename.replace('.mkv', '_proc')
if len(vid) % args.chunk_size < 10:
args.chunk_size += 5
starts = np.arange(0,len(vid),args.chunk_size)
stops = starts+args.chunk_size
frames = list(zip(starts, stops))
print(args.cores)
Parallel(n_jobs=args.cores)(delayed(process_chunk)(filename=filename, start=start, stop=stop, reference=reference, save_name=save_name, format=args.format, ds_factor=args.downsample, correct_motion=args.correct_motion, thresh=args.threshold, clean_pixels=args.remove_dead_pixels, pixel_thresh=args.pixel_thresh) for start, stop in frames)
if args.format == 'tiff':
if args.bigtiff:
system("tiffcp -8 {}/*_temp_*.tiff {}.tiff".format(directory, save_name))
else:
system("tiffcp {}/*_temp_*.tiff {}.tiff".format(directory, save_name))
system("rm {}/*_temp_*.tiff".format(directory))
elif args.format == 'avi':
system("avimerge -o {}.avi -i {}/*_temp_*.avi".format(save_name, directory))
system("rm {}/*_temp_*".format(directory))