def save_klb( image: Any, uri: str, compress: bool = True, partition: Optional[str] = None, # Format-specific kwargs block_shape: Tuple[int, ...] = None, compression_type: str = "bzip2"): if partition is None: partition = "tczyx" if not compress: compression_type = "none" if block_shape is not None: block_shape = block_shape[::-1] for img, _uri in partition_gen(image, partition, uri): axes = get_axes(img) shape = [ img.shape[axes.index(ax)] if ax in axes else 1 for ax in "tczyx" ] spacing = [get_spacing(img, ax) or 1.0 for ax in "tczyx"] # TODO: Convert spacing units to match convention. klb.writefull(to_numpy(img).reshape(shape), _uri, pixelspacing_tczyx=spacing, blocksize_xyzct=block_shape, compression=compression_type)
def test_pyklb(): """ This merely tests pyklb itself, to make sure it was compiled correctly. """ if not _klb_available: raise nose.SkipTest # Create some simple data. Should compress well. shape = (1, 1, 30, 40, 50) data_tczyx = np.indices(shape).sum(0).astype(np.uint8) assert data_tczyx.shape == shape # Write the data as KLB #print "writing..." filepath = '/tmp/test_data.klb' pyklb.writefull(data_tczyx, filepath, blocksize_xyzct=np.array([10, 10, 10, 1, 1], dtype=np.uint32)) # Read it back #print "reading..." #readback = pyklb.readfull(filepath) readback = pyklb.readroi(filepath, (0, 0, 0, 0, 0), np.array(shape) - 1) assert (readback == data_tczyx).all()
def _convert(fname): print("converting {}".format(fname)) try: with h5py.File('{}/{}'.format(dense_pred_dir, fname), 'r') as pred_file: print("loading") predictions = pred_file['predictions'][:].squeeze()[np.newaxis, np.newaxis, ...] predictions = np.ascontiguousarray(predictions) print("writing") pyklb.writefull(predictions, '{}/{}.klb'.format(klb_pred_dir, fname[:-3])) except: print("Failed on {}".format(fname))
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 testBasic(self): tmpdir = tempfile.mkdtemp() try: filepath = tmpdir + '/random_data.klb' shape = (1,1,30,40,50) data_tczyx = np.indices( shape ).sum(0).astype( np.uint8 ) pyklb.writefull( data_tczyx, filepath, blocksize_xyzct=np.array([10,10,10,1,1], dtype=np.uint32) ) readback = pyklb.readroi(filepath, (0,0,0,0,0), np.array(shape)-1) op = OpKlbReader( graph=Graph() ) op.FilePath.setValue( filepath ) assert op.Output.meta.shape == shape assert op.Output.meta.dtype == np.uint8 read_data = op.Output[:].wait() assert ( read_data == data_tczyx ).all() finally: shutil.rmtree(tmpdir)
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 test_pyklb(): """ This merely tests pyklb itself, to make sure it was compiled correctly. """ if not _klb_available: raise nose.SkipTest # Create some simple data. Should compress well. shape = (1,1,30,40,50) data_tczyx = np.indices( shape ).sum(0).astype( np.uint8 ) assert data_tczyx.shape == shape # Write the data as KLB #print "writing..." filepath = '/tmp/test_data.klb' pyklb.writefull( data_tczyx, filepath, blocksize_xyzct=np.array([10,10,10,1,1], dtype=np.uint32) ) # Read it back #print "reading..." #readback = pyklb.readfull(filepath) readback = pyklb.readroi(filepath, (0,0,0,0,0), np.array(shape)-1) assert (readback == data_tczyx).all()
def testBasic(self): tmpdir = tempfile.mkdtemp() try: filepath = tmpdir + '/random_data.klb' shape = (1, 1, 30, 40, 50) data_tczyx = np.indices(shape).sum(0).astype(np.uint8) pyklb.writefull(data_tczyx, filepath, blocksize_xyzct=np.array([10, 10, 10, 1, 1], dtype=np.uint32)) readback = pyklb.readroi(filepath, (0, 0, 0, 0, 0), np.array(shape) - 1) op = OpKlbReader(graph=Graph()) op.FilePath.setValue(filepath) assert op.Output.meta.shape == shape assert op.Output.meta.dtype == np.uint8 read_data = op.Output[:].wait() assert (read_data == data_tczyx).all() finally: shutil.rmtree(tmpdir)
def write_klb(filename, im): if np.isfortran(im): if len(im.shape) == 3: writefull(im.transpose(2, 1, 0), filename, pixelspacing_tczyx=(1, 1) + tuple(im.resolution[::-1])) else: writefull(im.transpose(1, 0), filename, pixelspacing_tczyx=(1, 1) + tuple(im.resolution[::-1])) else: writefull(im, filename)
def _klb_writer(klb_path, image): from pyklb import writefull writefull(image, str(klb_path))
def klb_writer(data, klb_path): from pyklb import writefull writefull(data, klb_path)
import re for root, dirs, files in os.walk("."): for filename in files: if re.search( "\d\.h5", filename ): #( filename.endswith("00.h5") or filename.endswith("01.h5") ): #print(filename) #print "Please note that renaming rules are coded in this python script; please modify script to change rules for renaming if desired!" file_to_modify = filename print("Exporting KLBs from file", file_to_modify) f = h5py.File(file_to_modify, "r") group_names = f.keys() last_group_name = list(group_names)[len(group_names) - 1] print("Working on group:", last_group_name) last_group = f["/" + last_group_name + "/"] subgroup_names = last_group.keys() #print "Subgroups: ", subgroup_names for this_name in list(subgroup_names): print(" subgroup:", this_name) pyklb.writefull( np.array(f["/" + last_group_name + "/" + this_name + "/0/cells"]).astype("uint8"), last_group_name + "_" + this_name + ".klb") f.close()