def try_stirtonifti(): time.sleep(0.5) sys.stderr.write( '\n# --------------------------------------------------------------------------------- #\n' ) sys.stderr.write( '# Starting STIR to Nifti test...\n') sys.stderr.write( '# --------------------------------------------------------------------------------- #\n' ) time.sleep(0.5) import pSTIR # Input filenames nifti_filename = SIRF_PATH + '/data/examples/Registration/test2.nii.gz' # Load the image as a NiftiImageData3D image_nifti = pReg.NiftiImageData3D(nifti_filename) # Read as STIRImageData, convert to NiftiImageData3D and save to file image_stir = pSTIR.ImageData(nifti_filename) image_nifti_from_stir = pReg.NiftiImageData3D(image_stir) image_nifti_from_stir.write('results/stir_to_nifti.nii', image_nifti.get_original_datatype()) # Compare the two if image_nifti != image_nifti_from_stir: raise AssertionError("Conversion from STIR to Nifti failed.") time.sleep(0.5) sys.stderr.write( '\n# --------------------------------------------------------------------------------- #\n' ) sys.stderr.write( '# Finished STIR to Nifti test.\n') sys.stderr.write( '# --------------------------------------------------------------------------------- #\n' ) time.sleep(0.5)
def try_stir_to_sirfreg(): time.sleep(0.5) sys.stderr.write( '\n# --------------------------------------------------------------------------------- #\n' ) sys.stderr.write( '# Starting STIR to SIRFReg test...\n') sys.stderr.write( '# --------------------------------------------------------------------------------- #\n' ) time.sleep(0.5) # Open stir image pet_image_data = pSTIR.ImageData(ref_aladin_filename) image_data_from_stir = pSIRFReg.NiftiImageData3D(pet_image_data) # Now fill the stir and sirfreg images with 1 and 100, respectively pet_image_data.fill(1.) image_data_from_stir.fill(100) if pet_image_data.as_array().max() == image_data_from_stir.get_max(): raise AssertionError() # Fill the stir image with the sirfreg image_data_from_stir.copy_data_to(pet_image_data) if pet_image_data.as_array().max() != image_data_from_stir.get_max(): raise AssertionError() time.sleep(0.5) sys.stderr.write( '\n# --------------------------------------------------------------------------------- #\n' ) sys.stderr.write( '# Finished STIR to SIRFReg test.\n') sys.stderr.write( '# --------------------------------------------------------------------------------- #\n' ) time.sleep(0.5)
filter = pet.TruncateToCylinderProcessor() filter.apply(image) #%% go to directory with input files # adapt this path to your situation (or start everything in the relevant directory) os.chdir(pet.examples_data_path('PET')) #%% copy files to working folder and change directory to where the output files are shutil.rmtree('working_folder/thorax_single_slice', True) shutil.copytree('thorax_single_slice', 'working_folder/thorax_single_slice') os.chdir('working_folder/thorax_single_slice') #%% We will first create some simulated data from ground-truth images #%% Read in images image = pet.ImageData('emission.hv') image_array = image.as_array() * .05 image.fill(image_array) mu_map = pet.ImageData('attenuation.hv') mu_map_array = mu_map.as_array() #%% bitmap display of images slice_num = image_array.shape[0] // 2 plt.figure() #plt.subplot(1,2,1); imshow(image_array[slice_num, :, :, ], [], 'emission image') #plt.subplot(1,2,2); #imshow(mu_map_array[slice,:,:,], [], 'attenuation image'); #%% save max for future displays cmax = image_array.max() * .6
def main(): # output goes to files msg_red = pet.MessageRedirector('info.txt', 'warn.txt', 'errr.txt') # create acquisition model acq_model = pet.AcquisitionModelUsingRayTracingMatrix() # PET acquisition data to be read from the file specified by --file option print('raw data: %s' % raw_data_file) acq_data = pet.AcquisitionData(raw_data_file) # Noisy data for testing noisy_data = acq_data.clone() noisy_data_array = np.random.poisson(acq_data.as_array() / 10) noisy_data.fill(noisy_data_array) # create filter that zeroes the image outside a cylinder of the same # diameter as the image xy-section size filter = pet.TruncateToCylinderProcessor() # create initial image estimate image_size = (111, 111, 31) voxel_size = (3, 3, 3.375) # voxel sizes are in mm image = pet.ImageData() image.initialise(image_size, voxel_size) image.fill(1.0) filter.apply(image) # create objective function obj_fun = pet.make_Poisson_loglikelihood(noisy_data) obj_fun.set_acquisition_model(acq_model) obj_fun.set_num_subsets(num_subsets) obj_fun.set_up(image) # create new obj_fun to get sensitivity image for one subset for dePierro MAPEM obj_fun2 = pet.make_Poisson_loglikelihood(noisy_data) obj_fun2.set_acquisition_model(acq_model) obj_fun2.set_num_subsets(1) obj_fun2.set_up(image) sensitivity_image = obj_fun2.get_subset_sensitivity(0) # create new noise-free obj_fun to use for guidance in dePierro MAPEM obj_fun3 = pet.make_Poisson_loglikelihood(acq_data) obj_fun3.set_acquisition_model(acq_model) obj_fun3.set_num_subsets(num_subsets) obj_fun3.set_up(image) # uniform weights weightsUniform = np.ones([sensitivity_image.as_array().size, 27], dtype='float') weightsUniform = weightsUniform / 27.0 # noise free recon with beta = 0 for guidance image_noiseFree = my_dePierroMap \ (image, obj_fun3, 0, filter, num_subsets, num_subiterations, weightsUniform, sensitivity_image) # create a Prior for computing Bowsher weights myPrior = pr.Prior(sensitivity_image.as_array().shape) weightsBowsher = myPrior.BowshserWeights(image_noiseFree.as_array(), 10) weightsBowsher = np.float64(weightsBowsher / 10.0) # dePierro MAPEM with uniform and Bowsher weights beta = 5000.0 image_dp_b = my_dePierroMap \ (image, obj_fun, beta, filter, num_subsets, num_subiterations, weightsBowsher, sensitivity_image) image_dp_u = my_dePierroMap \ (image, obj_fun, beta, filter, num_subsets, num_subiterations, weightsUniform, sensitivity_image) # show reconstructed images at z = 20 image_dp_b_array = image_dp_b.as_array() image_dp_u_array = image_dp_u.as_array() show_2D_array('Noise free image', image_noiseFree.as_array()[20, :, :]) show_2D_array('DP Bowsher', image_dp_b_array[20, :, :]) show_2D_array('DP Uniform', image_dp_u_array[20, :, :]) show_2D_array('Difference DP', image_dp_b_array[20, :, :] - image_dp_u_array[20, :, :])