def pad_trsfs(p, trsf_fmt): if not p.sequential and p.ref_path.split('.')[-1] == 'klb': im_shape = readheader(p.ref_path)['imagesize_tczyx'][-1:-4:-1] elif not p.sequential: im_shape = imread(p.ref_path).shape elif p.A0.split('.')[-1] == 'klb': im_shape = readheader( p.A0.format(t=p.ref_TP))['imagesize_tczyx'][-1:-4:-1] else: im_shape = imread(p.A0.format(t=p.ref_TP)).shape im = SpatialImage(np.ones(im_shape), dtype=np.uint8) im.voxelsize = p.voxel_size imsave(p.trsf_folder + 'tmp.klb', im) identity = np.identity(4) trsf_fmt_no_flo = trsf_fmt.replace('{flo:06d}', '%06d') new_trsf_fmt = 't{flo:06d}-{ref:06d}-padded.txt' new_trsf_fmt_no_flo = new_trsf_fmt.replace('{flo:06d}', '%06d') for t in p.not_to_do: np.savetxt(p.trsf_folder + trsf_fmt.format(flo=t, ref=p.ref_TP), identity) call(p.path_to_bin + 'changeMultipleTrsfs -trsf-format ' + p.trsf_folder + trsf_fmt_no_flo.format(ref=p.ref_TP) + \ ' -index-reference %d -first %d -last %d '%(p.ref_TP, min(p.time_points), max(p.time_points)) + \ ' -template ' + p.trsf_folder + 'tmp.klb ' + \ ' -res ' + p.trsf_folder + new_trsf_fmt_no_flo.format(ref=p.ref_TP) + \ ' -res-t ' + p.trsf_folder + 'template.klb ' + \ ' -trsf-type %s -vs %f %f %f'%((p.trsf_type,)+p.voxel_size), shell=True)
def apply_trsf(p): trsf_fmt = 't{flo:06d}-{ref:06d}.txt' if p.lowess: trsf_fmt = 't{flo:06d}-{ref:06d}-filtered.txt' if p.trsf_interpolation: trsf_fmt = 't{flo:06d}-{ref:06d}-interpolated.txt' if p.padding: trsf_fmt = 't{flo:06d}-{ref:06d}-padded.txt' X, Y, Z = readheader(p.trsf_folder + 'template.klb')['imagesize_tczyx'][-1:-4:-1] template = p.trsf_folder + 'template.klb' elif p.A0.split('.')[-1] == 'klb': X, Y, Z = readheader( p.A0.format(t=p.ref_TP))['imagesize_tczyx'][-1:-4:-1] template = p.A0.format(t=p.ref_TP) else: X, Y, Z = imread(p.A0.format(t=p.ref_TP)).shape template = p.A0.format(t=p.ref_TP) xy_proj = np.zeros((X, Y, len(p.time_points)), dtype=np.uint16) xz_proj = np.zeros((X, Z, len(p.time_points)), dtype=np.uint16) yz_proj = np.zeros((Y, Z, len(p.time_points)), dtype=np.uint16) for i, t in enumerate(sorted(p.time_points)): folder_tmp = os.path.split(p.A0_out.format(t=t))[0] if not os.path.exists(folder_tmp): os.makedirs(folder_tmp) call(p.path_to_bin + "applyTrsf '%s' '%s' -trsf "%(p.A0.format(t=t), p.A0_out.format(t=t)) + \ p.trsf_folder + trsf_fmt.format(flo=t, ref=p.ref_TP) + \ ' -template ' + template + \ ' -floating-voxel %f %f %f '%p.voxel_size + \ ' -reference-voxel %f %f %f '%p.voxel_size + \ ' -interpolation %s'%p.image_interpolation, shell=True) im = imread(p.A0_out.format(t=t)) if p.projection_path is not None: xy_proj[:, :, i] = SpatialImage(np.max(im, axis=2)) xz_proj[:, :, i] = SpatialImage(np.max(im, axis=1)) yz_proj[:, :, i] = SpatialImage(np.max(im, axis=0)) if p.projection_path is not None: if not os.path.exists(p.projection_path): os.makedirs(p.projection_path) p_to_data = p.projection_path num_s = p.file_name.find('{') num_e = p.file_name.find('}') + 1 f_name = p.file_name.replace(p.file_name[num_s:num_e], '') if not os.path.exists(p_to_data.format(t=-1)): os.makedirs(p_to_data.format(t=-1)) imsave((p_to_data + f_name.replace(p.im_ext, 'xyProjection.tif')), SpatialImage(xy_proj)) imsave((p_to_data + f_name.replace(p.im_ext, 'xzProjection.tif')), SpatialImage(xz_proj)) imsave((p_to_data + f_name.replace(p.im_ext, 'yzProjection.tif')), SpatialImage(yz_proj))
def read_folder(folder, parallel): from IO import imread, SpatialImage data_files = sorted(os.listdir(folder)) nb_images = len(data_files) first_image = imread(path.join(folder, data_files[0])) to_read = [] for i, p in enumerate(data_files): to_read.append([path.join(folder, p), i]) #(i, path.join(folder, p))) if parallel: pool = Pool() out = pool.map(populate_im, to_read) pool.close() pool.terminate() else: out = [] for param in to_read: out.append(populate_im(param)) return SpatialImage(np.array(out).transpose(1, 2, 0), voxelsize=first_image.voxelsize)
def assemble_projection(registered_folder, tp_list, folder_name, proj='xy'): ''' Build a "movie" of the MIP so they are easy to read Args: registered_folder: string, path to the folder that contain the registered images tp_list: [int, ], list of time points to process folder_name: string, output folder name proj: string ('xy', 'xz', 'yz'), projection to build; default 'xy' ''' t_for_string2 = (tp_list[0], ) * registered_folder.count('%') registered_folder_t = registered_folder % t_for_string2 pos_klb = registered_folder_t.find('.klb') dims = tuple( readheader(registered_folder_t[:pos_klb] + '_' + proj + 'Projection' + registered_folder_t[pos_klb:])['imagesize_tczyx'][:2:-1]) out = np.zeros(dims + (len(tp_list), ), dtype=np.uint16) for i, t in enumerate(tp_list): t_for_string2 = (t, ) * registered_folder.count('%') registered_folder_t = registered_folder % t_for_string2 pos_klb = registered_folder_t.find('.klb') out[:, :, i] = imread(registered_folder_t[:pos_klb] + '_' + proj + 'Projection' + registered_folder_t[pos_klb:])[:, :, 0] imsave(folder_name + '_out/' + proj + '_proj.klb', SpatialImage(out))
# build all the MIP for every time point pool = Pool() out = pool.map(run_shift_proj, mapping) pool.close() pool.terminate() XY_shape = np.array( list(np.max([(ii[0].shape) for ii in out], axis=0)) + [nb_times + 1]) YZ_shape = np.array( list(np.max([(ii[1].shape) for ii in out], axis=0)) + [nb_times + 1]) XZ_shape = np.array( list(np.max([(ii[2].shape) for ii in out], axis=0)) + [nb_times + 1]) XY = np.zeros(XY_shape, dtype=np.uint16) YZ = np.zeros(YZ_shape, dtype=np.uint16) XZ = np.zeros(XZ_shape, dtype=np.uint16) for xy, yz, xz, t in out: b1, b2 = (XY_shape[:-1] - xy.shape) / 2 XY[b1:b1 + xy.shape[0], b2:b2 + xy.shape[1], t] = xy b1, b2 = (YZ_shape[:-1] - yz.shape) / 2 YZ[b1:b1 + yz.shape[0], b2:b2 + yz.shape[1], t] = yz b1, b2 = (XZ_shape[:-1] - xz.shape) / 2 XZ[b1:b1 + xz.shape[0], b2:b2 + xz.shape[1], t] = xz imsave(folder_name + '_out/XY_proj.tif', SpatialImage(XY)) imsave(folder_name + '_out/YZ_proj.tif', SpatialImage(YZ)) imsave(folder_name + '_out/XZ_proj.tif', SpatialImage(XZ)) shutil.rmtree('.TMP')
if path_bary is not None: try: barycenters, b_dict = get_barycenter(path_bary, tb, te) except Exception as e: print "Wrong file path to barycenter, please specify the path to the .csv file." print "The process will continue as if no barycenter were provided," print "disabling the computation of the spherical coordinates" print "error raised: ", e path_bary = None im = imread(path_mask) for l in labels: masked_im = im == l tmp = nd.binary_opening(masked_im, iterations=3) tmp = nd.binary_closing(tmp, iterations=4) imsave('.mask_images/%03d.tif' % l, SpatialImage(tmp).astype(np.uint8)) mask_dir = '.mask_images/' if label_names is not None: masks = sorted([('.mask_images/%03d.tif' % l, label_names[i]) for i, l in enumerate(labels)], cmp=lambda x1, x2: cmp(x1[1], x2[1])) masks = [m[0] for m in masks] else: masks = ['.mask_images/%03d.tif' % l for l in labels] init_cells = {m: set() for m in range(len(masks))} x_max, y_max, z_max = 0, 0, 0 for i, path_mask in enumerate(masks): if invert:
list(to_register)) compose_trsf(max(to_register), ref_TP, p_out, list(to_register)) np.savetxt('{:s}t{t:06d}-{t:06d}.txt'.format(p_out, t=ref_TP), np.identity(4)) except Exception as e: print p_to_data print e if isinstance(ref_TP, int): if A0.split('.')[-1] == 'klb': im_shape = readheader( A0.format(t=ref_TP))['imagesize_tczyx'][-1:-4:-1] else: im_shape = imread(A0.format(t=ref_TP)).shape im = SpatialImage(np.ones(im_shape), dtype=np.uint8) im.voxelsize = v_size imsave(p_out + 'tmp.klb', im) identity = np.identity(4) for t in not_to_do: np.savetxt( p_out + 't{flo:06d}-{ref:06d}.txt'.format(flo=t, ref=ref_TP), identity) os.system('changeMultipleTrsfs -trsf-format ' + p_out + 't%%06d-%06d.txt '%(ref_TP) + \ '-index-reference %d -first %d -last %d '%(ref_TP, min(time_points), max(time_points)) + \ '-template ' + p_out + 'tmp.klb ' + \ '-res ' + p_out + 't%%06d-%06d-padded.txt '%(ref_TP) + \ '-res-t ' + p_out + 'template.klb ' + \ '-trsf-type %s -vs %f %f %f'%((trsf_type,)+v_size)) else: