def test_write_hdf5_full(self): data = phantom.shepp3d(size=32, dtype='float16') file_io.write_hdf5(data, fname=self.hdf_filename) # Check that the data were written properly with h5py.File(self.hdf_filename, mode='r') as h5fp: self.assertIn('volume', h5fp) np.testing.assert_array_equal(h5fp['volume'], data)
def test_write_hdf5_partial_overwrite(self): # Create some existing data to overwrite with h5py.File(self.hdf_filename, mode='a') as h5fp: h5fp.create_dataset('volume', data=np.ones((8, 8, 8))) data = phantom.shepp3d(size=32, dtype='float32') data_partial = data[10:16] data_partial_a = data[10:13] data_partial_b = data[13:16] # Write the data to the HDF5 file file_io.write_hdf5(data_partial_a, fname=self.hdf_filename, maxsize=data.shape, dest_idx=slice(10, 13), overwrite=True) file_io.write_hdf5(data_partial_b, fname=self.hdf_filename, maxsize=data.shape, dest_idx=slice(13, 16), overwrite=False) # Check that the data were written properly with h5py.File(self.hdf_filename, mode='r') as h5fp: self.assertIn('volume', h5fp) # Check that the target data were saved in the right place np.testing.assert_array_equal(h5fp['volume'][10:16], data_partial) # Check that the rest of the data are np.nan self.assertTrue(np.all(np.isnan(h5fp['volume'][:10]))) self.assertTrue(np.all(np.isnan(h5fp['volume'][16:])))
def test_3d_parallel(): phantom = np.squeeze(shepp3d(128)) angles = np.arange(0, 180, 1) sinogram = astra_fp_3d_parallel(phantom, angles) rec = astra_recon_3d_parallel(sinogram, angles, [['CGLS3D_CUDA', 10]]) diff = rec - phantom err = np.sqrt(np.sum(diff**2)) / np.prod(rec.shape) assert (err < 0.1)
def test_write_hdf5_mismatched_dtypes(self): """Try to write partial data with mismatch dtypes should fail.""" # Create some existing data to overwrite with h5py.File(self.hdf_filename, mode='a') as h5fp: h5fp.create_dataset('volume', data=np.ones((8, 8, 8))) data = phantom.shepp3d(size=8, dtype='float32') # Write the data to the HDF5 file with self.assertRaises(TypeError): file_io.write_hdf5(data[10:13], fname=self.hdf_filename, maxsize=data.shape, dest_idx=slice(10, 13), overwrite=False, dtype='int')
def test_write_hdf5_partial(self): data = phantom.shepp3d(size=32, dtype='float16') data_partial = data[10:16] # Write the data to the HDF5 file file_io.write_hdf5(data_partial, fname=self.hdf_filename, maxsize=data.shape, dest_idx=slice(10, 16)) # Check that the data were written properly with h5py.File(self.hdf_filename, mode='r') as h5fp: self.assertIn('volume', h5fp) # Check that the target data were saved in the right place np.testing.assert_array_equal(h5fp['volume'][10:16], data_partial) # Check that the rest of the data are zero self.assertTrue(np.all(np.isnan(h5fp['volume'][:10]))) self.assertTrue(np.all(np.isnan(h5fp['volume'][16:])))
def main(): with h5py.File(h5file, mode='a') as h5fp: # vol = phantom.shepp3d(vol_size, 'int') combos = [ ('volume', 'float32', 1), ('volume_int', 'int', 16), ('volume_uint', 'uint', 16), ('volume_bool', 'bool', 1.), ] vol = phantom.shepp3d(vol_size) for (name, dtype, mult) in combos: # Delete existing datasets if name in h5fp.keys(): del h5fp[name] # Scale the dataset to match the datatype new_vol = (vol * mult).astype(dtype) # Save to disk h5fp.create_dataset(name, data=new_vol) return 0
def test_shepp3d(self): assert_equals(shepp3d(size=(6, 8, 10)).dtype, 'float32') assert_equals(shepp3d(size=(6, 8, 10)).shape, (6, 8, 10)) assert_equals(shepp3d(size=(6, 8, 10)).min(), 0) assert_equals(shepp3d(size=6).shape, (6, 6, 6))
#!/usr/bin/env python import numpy as np from tomopy.misc.phantom import shepp3d output = 'shepp3d.npy' phantom = shepp3d() np.save(output, phantom)
def main(): vol = phantom.shepp3d(256) with h5py.File('data/phantom3d_big.h5', mode='a') as h5fp: h5fp.create_dataset('volume', data=vol)