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_mask_and_save(path, t, X, ors, vs=(2., 2., 10.)): ''' Reads, resamples, masks and saves an image from klb to inr format Args: path: string, path of the input klb image t: int, time point to read X: int, value above which the image data is considered ors: 3x1 array_like, original aspect ratio vs: 3x1 array_like, new aspect ratio Return: im: string, path to the resampled masked image ''' out = ('.TMP/' + os.path.basename(path) % (t)).replace('.klb', '.inr') if not os.path.exists(out): t_for_string = (t, ) * p.count('%') im = imread(path % t_for_string) im.voxelsize = vs mask = (slice(get_position(t, X), -1), slice(0, -1), slice(0, -1)) im[mask] = 0 if (ors != vs).any(): im.voxelsize = ors imsave(out, im) os.system(path_to_bin + 'applyTrsf ' + out + ' ' + out + ' -resize -vs %f %f %f ' % tuple(vs)) else: imsave(out, im) return out
def read_mask(path, t, X, ors, vs=(2., 2., 10.)): ''' Reads, masks and saves an image from klb to inr format Args: path: string, path of the input klb image t: int, time point to read X: int, value above which the image data is considered ors: 3x1 array_like, original aspect ratio vs: 3x1 array_like, new aspect ratio Return: im: NxMxL array_like, masked image ''' out = ('.TMP/' + os.path.basename(path) % t).replace('.klb', '.inr') if not os.path.exists(out): t_for_string = (t, ) * p.count('%') im = imread(path % t_for_string) im.voxelsize = vs mask = (slice(get_position(t, X), -1), slice(0, -1), slice(0, -1)) im[mask] = 0 imsave(out, im) else: im = imread(out) return im
def apply_single_trsf(params): ''' Apply a transformation to an image and build the xy, xz, yz MIP. Meant to be called in parallel by a multiprocess.Pool ''' im_p, init_trsf, init_global_shape, trsf, final_shape, registered_folder_t, t = params curr_state = 'Not even started' # Since the process is error prone, we allow to catch preciselly the errors with the try/catch try: curr_state = 'reading input image.' im = imread(im_p) # build the final image for time t new_image = np.zeros(final_shape[::-1], dtype=im.dtype) starting_point = (trsf - init_trsf).astype(int) ending_point = starting_point + im.shape curr_state = 'applying trsf.' new_image[[ slice(starting_point[i], ending_point[i]) for i in range(2, -1, -1) ]] = im.transpose(2, 1, 0) if not os.path.exists(os.path.dirname(registered_folder_t)): os.makedirs(os.path.dirname(registered_folder_t)) curr_state = 'saving image.' imsave(registered_folder_t, new_image) # build the final MIP for time t curr_state = 'building max projections.' pos_klb = registered_folder_t.find('.klb') projXY = np.max(new_image, axis=0) projXY = projXY.reshape(*((1, ) + projXY.shape)) projXZ = np.max(new_image, axis=1) projXZ = projXZ.reshape(*((1, ) + projXZ.shape)) projYZ = np.max(new_image, axis=2) projYZ = projYZ.reshape(*((1, ) + projYZ.shape)) curr_state = 'saving xy projection.' imsave( registered_folder_t[:pos_klb] + '_xyProjection' + registered_folder_t[pos_klb:], projXY) curr_state = 'saving xz projection.' imsave( registered_folder_t[:pos_klb] + '_xzProjection' + registered_folder_t[pos_klb:], projXZ) curr_state = 'saving yz projection.' imsave( registered_folder_t[:pos_klb] + '_yzProjection' + registered_folder_t[pos_klb:], projYZ) except Exception as e: print 'Error for time point %d.' % t print 'Happened while ' + curr_state print 'Error raised:' print e
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 run_shift_proj(params): ''' Given the path to an image, the path to a transformation, at time and a reference time, build the 'xy', 'xz', 'yz' MIP of the shifted image. Meant to be run with multiprocess.Pool Returns: - list of the three projections and the time of the image ''' p_im, p_trsf, t, r, vs = params im = imread(p_im % t) if not os.path.exists(p_trsf % (t, r)): final_shift = np.zeros((4, 4)) final_shift[np.diag_indices(4)] = 1 else: final_shift = read_trsf(p_trsf % (t, r)) im_proj_1 = build_and_shift_proj(im, [final_shift[0, 3], final_shift[1, 3]], 2) im_proj_2 = build_and_shift_proj(im, [final_shift[1, 3], final_shift[2, 3]], 0) im_proj_3 = build_and_shift_proj(im, [final_shift[0, 3], final_shift[2, 3]], 1) return [im_proj_1, im_proj_2, im_proj_3, t]
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))
def build_bdv(p): if not p.im_ext in ['klb']: #['tif', 'klb', 'tiff']: print('Image format not adapted for BigDataViewer') return SpimData = ET.Element('SpimData') SpimData.set('version', "0.2") SpimData.set('encoding', "UTF-8") base_path = ET.SubElement(SpimData, 'BasePath') base_path.set('type', 'relative') base_path.text = '.' SequenceDescription = ET.SubElement(SpimData, 'SequenceDescription') ImageLoader = ET.SubElement(SequenceDescription, 'ImageLoader') ImageLoader.set('format', p.im_ext) Resolver = ET.SubElement(ImageLoader, 'Resolver') Resolver.set('type', "org.janelia.simview.klb.bdv.KlbPartitionResolver") ViewSetupTemplate = ET.SubElement(Resolver, 'ViewSetupTemplate') template = ET.SubElement(ViewSetupTemplate, 'template') template.text = p.bdv_im.format(t=p.to_register[0]) timeTag = ET.SubElement(ViewSetupTemplate, 'timeTag') timeTag.text = p.time_tag ViewSetups = ET.SubElement(SequenceDescription, 'ViewSetups') ViewSetup = ET.SubElement(ViewSetups, 'ViewSetup') if p.im_ext == 'klb': im_size = tuple( readheader(p.A0.format(t=p.ref_TP))['imagesize_tczyx'][-1:-4:-1]) else: im_size = tuple(imread(p.A0.format(t=p.ref_TP)).shape) do_viewSetup(ViewSetup, p, im_size, 0) Attributes = ET.SubElement(ViewSetups, 'Attributes') Attributes.set('name', 'illumination') Illumination = ET.SubElement(Attributes, 'Illumination') id_ = ET.SubElement(Illumination, 'id') id_.text = '0' name = ET.SubElement(Illumination, 'name') name.text = '0' Attributes = ET.SubElement(ViewSetups, 'Attributes') Attributes.set('name', 'channel') Channel = ET.SubElement(Attributes, 'Channel') id_ = ET.SubElement(Channel, 'id') id_.text = '0' name = ET.SubElement(Channel, 'name') name.text = '0' Attributes = ET.SubElement(ViewSetups, 'Attributes') Attributes.set('name', 'tile') Tile = ET.SubElement(Attributes, 'Tile') id_ = ET.SubElement(Tile, 'id') id_.text = '0' name = ET.SubElement(Tile, 'name') name.text = '0' Attributes = ET.SubElement(ViewSetups, 'Attributes') Attributes.set('name', 'angle') Angle = ET.SubElement(Attributes, 'Angle') id_ = ET.SubElement(Angle, 'id') id_.text = '0' name = ET.SubElement(Angle, 'name') name.text = '0' TimePoints = ET.SubElement(SequenceDescription, 'Timepoints') TimePoints.set('type', 'range') first = ET.SubElement(TimePoints, 'first') first.text = '%d' % min(p.to_register) last = ET.SubElement(TimePoints, 'last') last.text = '%d' % max(p.to_register) ViewRegistrations = ET.SubElement(SpimData, 'ViewRegistrations') b = min(p.to_register) e = max(p.to_register) for t in range(b, e + 1): do_ViewRegistration(ViewRegistrations, p, t) with open(p.out_bdv, 'w') as f: f.write(prettify(SpimData)) f.close()
def populate_im(parameters): from IO import imread, SpatialImage path, pos = parameters return imread(path)[:, :, 0]
os.makedirs('.mask_images/') VF = lineageTree(path_VF) tb = VF.t_b te = VF.t_e 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]
compose_trsf(min(to_register), ref_TP, p_out, 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))