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 setupOutputs(self): self._filepath = self.FilePath.value header = pyklb.readheader(self._filepath) self.Output.meta.shape = tuple(header['imagesize_tczyx']) self.Output.meta.axistags = vigra.defaultAxistags('tczyx') self.Output.meta.dtype = header['datatype'].type
def test_readroi_inplace(self): header = pyklb.readheader(self.testread_filepath) lb = [9, 15, 15] ub = [11, 99, 99] img = np.zeros(1 + np.array(ub) - np.array(lb), header["datatype"]) pyklb.readroi_inplace(img, self.testread_filepath, lb, ub, nochecks=False) self.assertEqual(round(np.mean(img)), 568) img = np.zeros(1 + np.array(ub) - np.array(lb), header["datatype"]) pyklb.readroi_inplace(img, self.testread_filepath, lb, ub, nochecks=True) self.assertEqual(round(np.mean(img)), 568) img = np.zeros(1 + np.array(ub) - np.array(lb), np.uint8) with self.assertRaises(TypeError): pyklb.readroi_inplace(img, self.testread_filepath, lb, ub) img = np.zeros([666, 666], header["datatype"]) with self.assertRaises(IOError): pyklb.readroi_inplace(img, self.testread_filepath, lb, ub) img = np.zeros(1 + np.array(ub) - np.array(lb), header["datatype"]) with self.assertRaises(IOError): pyklb.readroi_inplace(img, self.testread_filepath, ub, lb)
def setupOutputs(self): self._filepath = self.FilePath.value header = pyklb.readheader(self._filepath) self.Output.meta.shape = tuple(header["imagesize_tczyx"]) self.Output.meta.axistags = vigra.defaultAxistags("tczyx") self.Output.meta.dtype = header["datatype"].type
def applyTrsf_from_run(trsf_p, nb_times, r, tp_list, vs, ors, first_TP, registered_folder, nb_cpu, TP_to_keep): ''' Apply transformations from paths to transforamtions Args: trsf_p: string, path the folder containing the transformations, the trsf files names should be in the form t%03d-%03d.txt nb_times (not used): int, number of time points on which to apply the transformation r: int, reference time point tp_list: [int, ], list of time points on which to apply the transformation vs: float, aspect ratio ors: float, original aspect ratio first_TP (not used): int, first time point on which to apply the transformation registered_folder: string, folder that will contain the registered images nb_cpu: int. number of cpus to use TP_to_keep: [int, ], list of time points to treat (if only as subset is necessary) ''' # reads all the transformations trsfs = np.round( get_trsf(trsf_p, nb_times, r, tp_list, vs, ors, first_TP, True)).astype(int) shapes = [] trsfs_dict = dict(zip(tp_list, trsfs)) # reads the shape of each time point in order to compute the minimum bounding box for t in tp_list: t_for_string = (t, ) * p.count('%') shapes += [ list(readheader(p % t_for_string)['imagesize_tczyx'][:1:-1]) ] # compute the new transformation to apply the right cropping of the image init_global_shape = np.max(shapes, axis=0) min_shape, max_shape = [], [] for i, t in enumerate(trsfs): min_s, max_s = get_offset(t, init_global_shape, shapes[i]) min_shape += [min_s] max_shape += [max_s] min_shape = np.min(min_shape, axis=0) max_shape = np.max(max_shape, axis=0) final_shape = np.ceil(max_shape - min_shape).astype(int) # apply the transformations in parallel mapping = [] if set(TP_to_keep).intersection(tp_list) != set(): TP_to_do = set(TP_to_keep).intersection(tp_list) else: TP_to_do = tp_list for i, t in enumerate(TP_to_do): t_for_string = (t, ) * p.count('%') t_for_string2 = (t, ) * registered_folder.count('%') mapping += [[ p % t_for_string, min_shape, init_global_shape, trsfs_dict[t], final_shape, registered_folder % t_for_string2, t ]] pool = Pool(processes=nb_cpu) out = pool.map(apply_single_trsf, mapping) pool.close() pool.terminate()
def read_klb(filename, SP_im = True): tmp = readfull(filename) if len(tmp.shape) == 2: tmp = tmp.reshape((1, ) + tmp.shape) if SP_im: im = SpatialImage(tmp.transpose(2, 1, 0), copy = False) im.voxelsize = readheader(filename).get('pixelspacing_tczyx', [1., ]*5)[:1:-1] else: im = tmp.transpose(2, 1, 0) return im
def test_writefull(self): print("Testing KLB writing to %s" % self.testwrite_filepath) if os.path.exists(self.testwrite_filepath): print("Skipping writing tests because file %s exists.\n Please move or delete this file and re-run the tests." % self.testwrite_filepath) return img = pyklb.readfull(self.testread_filepath) pyklb.writefull(img, self.testwrite_filepath, pixelspacing_tczyx=[0.5, 0.5, 5.0]) self.assertTrue(os.path.exists(self.testwrite_filepath)) img2 = pyklb.readfull(self.testwrite_filepath) self.assertEqual(img.dtype, img2.dtype) self.assertEqual(img.shape, img2.shape) self.assertEqual(np.mean(img), np.mean(img2)) header = pyklb.readheader(self.testwrite_filepath) self.assertTrue(np.all( header["pixelspacing_tczyx"] == [1, 1, 0.5, 0.5, 5.0] ))
def _get_schema(self) -> Schema: header = klb.readheader(self.uri) shape = dict(zip("tczyx", header["imagesize_tczyx"])) spacing = dict(zip("tczyx", header["pixelspacing_tczyx"])) axes = "".join(ax for ax, s in shape.items() if s > 1) for ax in [ax for ax in shape.keys() if ax not in axes]: shape.pop(ax) spacing.pop(ax) units = {ax: "s" if ax == "t" else "μm" for ax in spacing.keys()} shape = self._set_shape_metadata(axes, shape, spacing, units) self._set_fileheader(header) return Schema(dtype=np.dtype(header["datatype"]), shape=shape, npartitions=1, chunks=None)
def test_readfull_inplace(self): header = pyklb.readheader(self.testread_filepath) img = np.zeros(header["imagesize_tczyx"], header["datatype"]) pyklb.readfull_inplace(img, self.testread_filepath, nochecks=False) self.assertEqual(round(np.mean(img)), 352) img = np.zeros(header["imagesize_tczyx"], header["datatype"]) pyklb.readfull_inplace(img, self.testread_filepath, nochecks=True) self.assertEqual(round(np.mean(img)), 352) img = np.zeros(header["imagesize_tczyx"], np.uint8) with self.assertRaises(TypeError): pyklb.readfull_inplace(img, self.testread_filepath) img = np.zeros([666, 666], header["datatype"]) with self.assertRaises(IOError): pyklb.readfull_inplace(img, self.testread_filepath)
def sample(filename, n): header = pyklb.readheader(filename) size = header['imagesize_tczyx'][-3:] points = [] print("Sampling points...") while len(points) < n: z = random.randint(0, size[0] - 1) y = random.randint(0, size[1] - 1) x = random.randint(0, size[2] - 1) if pyklb.readroi(filename, (z, y, x), (z, y, x)) > foreground_threshold: points.append((z, y, x)) print("Done.") return points
def test_writefull(self): print("Testing KLB writing to %s" % self.testwrite_filepath) if os.path.exists(self.testwrite_filepath): print( "Skipping writing tests because file %s exists.\n Please move or delete this file and re-run the tests." % self.testwrite_filepath) return img = pyklb.readfull(self.testread_filepath) pyklb.writefull(img, self.testwrite_filepath, pixelspacing_tczyx=[0.5, 0.5, 5.0]) self.assertTrue(os.path.exists(self.testwrite_filepath)) img2 = pyklb.readfull(self.testwrite_filepath) self.assertEqual(img.dtype, img2.dtype) self.assertEqual(img.shape, img2.shape) self.assertEqual(np.mean(img), np.mean(img2)) header = pyklb.readheader(self.testwrite_filepath) self.assertTrue( np.all(header["pixelspacing_tczyx"] == [1, 1, 0.5, 0.5, 5.0]))
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))
trsf_type=trsf_type, not_to_do=not_to_do, registration_depth=registration_depth) 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) + \
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 test_header(self): header = pyklb.readheader(self.testread_filepath) self.assertEqual(np.prod(header["imagesize_tczyx"]), 442279)