|
| 1 | +#!/usr/bin/env python |
| 2 | +import os, sys, shutil, socket, getopt, subprocess |
| 3 | + |
| 4 | +paths = { |
| 5 | + 'data': '/Users/ayla/devel/dhs_batch/U308', |
| 6 | + 'tools': '/Users/ayla/devel/Seg3D_DHS/tools', |
| 7 | + 'glia_bin': '/Users/ayla/devel/glia/code/build', |
| 8 | + 'hmtscript': '/Users/ayla/devel/Seg3D_DHS/hmt_test_batch.py', |
| 9 | + 'glia_results': '/Users/ayla/devel/dhs_batch/U308/output' |
| 10 | +} |
| 11 | + |
| 12 | +def seg3d_connect(port, size): |
| 13 | + clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 14 | + clientsocket.connect(("localhost", port)) |
| 15 | + data = clientsocket.recv(size) |
| 16 | + print "Received: [{}]".format(data) |
| 17 | + return clientsocket |
| 18 | + |
| 19 | +def seg3d_disconnect(clientsocket, size): |
| 20 | + clientsocket.send("exit\r\n") |
| 21 | + data = clientsocket.recv(size) |
| 22 | + clientsocket.close(); |
| 23 | + print "Received: [{}]".format(data) |
| 24 | + |
| 25 | + |
| 26 | +def seg3d_command(clientsocket, size, command): |
| 27 | + clientsocket.send(command) |
| 28 | + data = clientsocket.recv(size) |
| 29 | + #print "Received: [{}]".format(data) |
| 30 | + return data |
| 31 | + |
| 32 | +def ensure_directories(directories): |
| 33 | + for d in directories: |
| 34 | + if not os.path.exists(d): |
| 35 | + os.makedirs(d) |
| 36 | + |
| 37 | +def update_filepath(filename, strings): |
| 38 | + newfilename = filename |
| 39 | + for key, value in strings.items(): |
| 40 | + newfilename = newfilename.replace(key, value) |
| 41 | + #print(newfilename) |
| 42 | + return newfilename |
| 43 | + |
| 44 | +def main(argv): |
| 45 | + size = 1024 |
| 46 | + port = 9000 |
| 47 | + |
| 48 | + try: |
| 49 | + opts, args = getopt.getopt(argv, "p:", ["port="]) |
| 50 | + |
| 51 | + except getopt.GetoptError as e: |
| 52 | + print 'Exception: {0}.'.format(e.msg) |
| 53 | + print 'batch_processing.py [-p,--port=<port>]' |
| 54 | + sys.exit(2) |
| 55 | + |
| 56 | + for opt, arg in opts: |
| 57 | + if opt in ("-s", "--port"): |
| 58 | + port = int(arg) |
| 59 | + |
| 60 | + im = os.path.join(paths['data'], 'im') |
| 61 | + im_raw = os.path.join(im, 'raw') |
| 62 | + im_gray_training = os.path.join(im, 'gray') |
| 63 | + im_gray_all = os.path.join(im, 'gray_all') |
| 64 | + # expecting TIFF files |
| 65 | + im_raw_files = [os.path.join(im_raw, f) for f in os.listdir(im_raw) if os.path.isfile(os.path.join(im_raw, f)) and f.lower().endswith('.tif')] |
| 66 | + |
| 67 | + # test |
| 68 | + im_raw_test = os.path.join(im, 'raw_test') |
| 69 | + im_gray_test = os.path.join(im, 'gray_test') |
| 70 | + # expecting TIFF files |
| 71 | + im_raw_test_files = [os.path.join(im_raw_test, f) for f in os.listdir(im_raw_test) if os.path.isfile(os.path.join(im_raw_test, f)) and f.lower().endswith('.tif')] |
| 72 | + |
| 73 | + # setup directories |
| 74 | + im_cropped = os.path.join(paths['data'], 'im_cropped') |
| 75 | + im_cropped_gray = os.path.join(im_cropped, 'gray') |
| 76 | + im_cropped_chm = os.path.join(im_cropped, 'chm') |
| 77 | + im_cropped_blur = os.path.join(im_cropped, 'chm-blur') |
| 78 | + |
| 79 | + truth = os.path.join(paths['data'], 'truth') |
| 80 | + truth_raw = os.path.join(truth, 'raw') |
| 81 | + truth_region = os.path.join(truth, 'region') |
| 82 | + truth_raw_files = [os.path.join(truth_raw, f) for f in os.listdir(truth_raw) if os.path.isfile(os.path.join(truth_raw, f)) and f.lower().endswith('.png')] |
| 83 | + |
| 84 | + # setup directories |
| 85 | + truth_cropped = os.path.join(paths['data'], 'truth_cropped') |
| 86 | + truth_cropped_region = os.path.join(truth_cropped, 'region') |
| 87 | + # TODO: not sure if needed |
| 88 | + #truth_cropped_gray = os.path.join(truth_cropped, 'gray') |
| 89 | + |
| 90 | + seg_ushort = os.path.join(paths['glia_results'], 'seg') |
| 91 | + seg_uchar = os.path.join(paths['glia_results'], 'segtest') |
| 92 | + |
| 93 | + ensure_directories( (im_gray_training, im_gray_all, im_cropped, im_cropped_gray, im_cropped_chm, im_cropped_blur, im_gray_test, truth_region, truth_cropped, truth_cropped_region) ) |
| 94 | + |
| 95 | + rgbToGray2D_tool = os.path.join(paths['tools'], 'RGBToGray2D') |
| 96 | + padImage_tool = os.path.join(paths['tools'], 'PadImage') |
| 97 | + blur_image_tool = os.path.join(paths['tools'], 'blur_image') |
| 98 | + |
| 99 | + # clean up filenames |
| 100 | + for f in truth_raw_files: |
| 101 | + # shorten filename, remove spaces, change directory |
| 102 | + newfile = update_filepath(f, { 'U3O8_': '', 'Particle ': 'p', truth_raw: truth_region }) |
| 103 | + shutil.copyfile(f, newfile) |
| 104 | + |
| 105 | + raw_files = im_raw_files + im_raw_test_files |
| 106 | + |
| 107 | + # Raw data RGB files to grayscale (ITK tool) - RGBToGray2D |
| 108 | + # consolidate all data files for processing |
| 109 | + for f in raw_files: |
| 110 | + # shorten filename, remove spaces, change directory |
| 111 | + newfile = update_filepath(f, { 'U3O8_': '', 'Particle ': 'p', im_raw_test: im_gray_test, im_raw: im_gray_training, '.tif': '.mha' }) |
| 112 | + subprocess.check_call([rgbToGray2D_tool, f, newfile, 'float']) |
| 113 | + shutil.copy(newfile, im_gray_all) |
| 114 | + |
| 115 | + clientsocket = seg3d_connect(port, size) |
| 116 | + # add some variables to Seg3D's interpreter |
| 117 | + retval = seg3d_command(clientsocket, size, "im_gray_all='{}'\r\n".format(im_gray_all)) |
| 118 | + retval = seg3d_command(clientsocket, size, "im_cropped_gray='{}'\r\n".format(im_cropped_gray)) |
| 119 | + retval = seg3d_command(clientsocket, size, "im_cropped_chm='{}'\r\n".format(im_cropped_chm)) |
| 120 | + retval = seg3d_command(clientsocket, size, "truth_region='{}'\r\n".format(truth_region)) |
| 121 | + retval = seg3d_command(clientsocket, size, "truth_cropped_region='{}'\r\n".format(truth_cropped_region)) |
| 122 | + retval = seg3d_command(clientsocket, size, "exec(open('/Users/ayla/devel/Seg3D_DHS/scripts/seg3d_filters.py').read())\r\n") |
| 123 | + seg3d_disconnect(clientsocket, size) |
| 124 | + |
| 125 | + # cleanup |
| 126 | + truth_cropped_files = [os.path.join(truth_cropped_region, f) for f in os.listdir(truth_cropped_region) if os.path.isfile(os.path.join(truth_cropped_region, f)) and f.lower().endswith('.png')] |
| 127 | + for f in truth_cropped_files: |
| 128 | + newfile = update_filepath(f, { 'Crop_': '', '_label-00': '' }) |
| 129 | + os.rename(f, newfile) |
| 130 | + |
| 131 | + boundary_files = [os.path.join(im_cropped_chm, f) for f in os.listdir(im_cropped_chm) if os.path.isfile(os.path.join(im_cropped_chm, f)) and f.lower().endswith('.mha')] |
| 132 | + |
| 133 | + # Boundary files to blurred boundary files |
| 134 | + for f in boundary_files: |
| 135 | + # shorten filename, remove spaces, change directory |
| 136 | + newfile = update_filepath(f, { im_cropped_chm: im_cropped_blur }) |
| 137 | + subprocess.check_call([blur_image_tool, "--inputImage={}".format(f), "--outputImage={}".format(newfile), "--sigma=1", "--kernelWidth=3"]) |
| 138 | + |
| 139 | + # Call GLIA script |
| 140 | + subprocess.check_call(['python', paths['hmtscript'], im_gray_training, im_gray_test, im_gray_all, im_cropped_chm, im_cropped_blur, truth_cropped_region, paths['glia_bin'], paths['glia_results'] ]) |
| 141 | + |
| 142 | + seg_ushort_files = [os.path.join(seg_ushort, f) for f in os.listdir(seg_ushort) if os.path.isfile(os.path.join(seg_ushort, f)) and f.lower().endswith('.png')] |
| 143 | + seg_uchar_files = [os.path.join(seg_uchar, f) for f in os.listdir(seg_uchar) if os.path.isfile(os.path.join(seg_uchar, f)) and f.lower().endswith('.png')] |
| 144 | + |
| 145 | + # Pad to original dimensions |
| 146 | + for f in seg_ushort_files: |
| 147 | + # shorten filename, remove spaces, change directory |
| 148 | + newfile = update_filepath(f, { '.png': '_pad.png' }) |
| 149 | + subprocess.check_call([padImage_tool, f, newfile, '0', '0', '0', '59', '0', 'ushort']) |
| 150 | + |
| 151 | + for f in seg_uchar_files: |
| 152 | + # shorten filename, remove spaces, change directory |
| 153 | + newfile = update_filepath(f, { '.png': '_pad.png' }) |
| 154 | + subprocess.check_call([padImage_tool, f, newfile, '0', '0', '0', '59', '0', 'uchar']) |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + main(sys.argv[1:]) |
0 commit comments