def test_main(rec=False, verb=False, throw=True): pet.MessageRedirector() for scheme in ("file", "memory"): pet.AcquisitionData.set_storage_scheme(scheme) original_verb = pet.get_verbosity() pet.set_verbosity(False) # create an acq_model that is explicitly a RayTracingMatrix am = pet.AcquisitionModelUsingRayTracingMatrix() # load sample data data_path = pet.examples_data_path('PET') raw_data_file = pet.existing_filepath(data_path, 'Utahscat600k_ca_seg4.hs') ad = pet.AcquisitionData(raw_data_file) # create sample image image = pet.ImageData() image.initialise(dim=(31, 111, 111), vsize=(2.25, 2.25, 2.25)) # set up Acquisition Model am.set_up(ad, image) # test for adjointnesss if not is_operator_adjoint(am, verbose=verb): raise AssertionError( 'AcquisitionModelUsingRayTracingMatrix is not adjoint') # Reset original verbose-ness pet.set_verbosity(original_verb) return 0, 1
def test_main(rec=False, verb=False, throw=True): msg_red = pet.MessageRedirector() data_path = pet.examples_data_path('PET') raw_data_file = pet.existing_filepath(data_path, 'mMR/list.l.hdr') lm2sino = pet.ListmodeToSinograms() lm2sino.set_input(raw_data_file) num_prompts_threshold = 73036. known_time = 22. time_at_which_num_prompts_exceeds_threshold = \ lm2sino.get_time_at_which_num_prompts_exceeds_threshold(num_prompts_threshold) if abs(time_at_which_num_prompts_exceeds_threshold - known_time) > 1.e-4: raise AssertionError( "ListmodeToSinograms::get_time_at_which_num_prompts_exceeds_threshold failed" ) return 0, 1
def main(): # direct all engine's messages to files msg_red = PET.MessageRedirector('info.txt', 'warn.txt', 'errr.txt') PET.AcquisitionData.set_storage_scheme('memory') # Create the Scatter Estimator # We can use a STIR parameter file like this # par_file_path = os.path.join(os.path.dirname(__file__), '..', '..', 'parameter_files') # se = PET.ScatterEstimator(PET.existing_filepath(par_file_path, 'scatter_estimation.par')) # However, we will just use all defaults here, and set variables below. se = PET.ScatterEstimator() prompts = PET.AcquisitionData(raw_data_file) se.set_input(prompts) se.set_attenuation_image(PET.ImageData(mu_map_file)) if randoms_data_file is None: randoms = None else: randoms = PET.AcquisitionData(randoms_data_file) se.set_randoms(randoms) if not(norm_file is None): se.set_asm(PET.AcquisitionSensitivityModel(norm_file)) if not(acf_file is None): se.set_attenuation_correction_factors(PET.AcquisitionData(acf_file)) # could set number of iterations if you want to se.set_num_iterations(1) print("number of scatter iterations that will be used: %d" % se.get_num_iterations()) se.set_output_prefix(output_prefix) se.set_up() se.process() scatter_estimate = se.get_output() ## show estimated scatter data scatter_estimate_as_array = scatter_estimate.as_array() show_2D_array('Scatter estimate', scatter_estimate_as_array[0, 0, :, :]) ## let's draw some profiles to check # we will average over all sinograms to reduce noise PET_plot_functions.plot_sinogram_profile(prompts, randoms=randoms, scatter=scatter_estimate)
acq_model.set_num_tangential_LORs(5) # set recon, OSEM recon = Pet.OSMAPOSLReconstructor() num_subsets = 7 num_subiterations = 4 recon.set_num_subsets(num_subsets) recon.set_num_subiterations(num_subiterations) # definitions for detector sensitivity modelling asm_norm = Pet.AcquisitionSensitivityModel(norm_file) acq_model.set_acquisition_sensitivity(asm_norm) #%% redirect STIR messages to some files # you can check these if things go wrong msg_red = Pet.MessageRedirector('info.txt', 'warn.txt') #%% List of sino and rand list_sino = [ f for f in os.listdir(working_folder + '/sino/') if f.endswith(".hs") ] list_rando = [ f for f in os.listdir(working_folder + '/rando/') if f.endswith(".hs") ] #%% NAC reconstruction tprint('Start NAC Recon') for i, sino, random in zip(range(len(path_sino)), sorted_alphanumeric(list_sino),
def main(): ## PET.AcquisitionData.set_storage_scheme('memory') # no info printing from the engine, warnings and errors sent to stdout msg_red = PET.MessageRedirector() # Create a template Acquisition Model #acq_template = AcquisitionData('Siemens mMR', 1, 0, 1) acq_template = PET.AcquisitionData( acq_template_filename) #q.get_uniform_copy() # create the attenuation image atten_image = PET.ImageData(acq_template) image_size = atten_image.dimensions() voxel_size = atten_image.voxel_sizes() # create a cylindrical water phantom water_cyl = PET.EllipticCylinder() water_cyl.set_length(image_size[0] * voxel_size[0]) water_cyl.set_radii((image_size[1]*voxel_size[1]*0.25, \ image_size[2]*voxel_size[2]*0.25)) water_cyl.set_origin((image_size[0] * voxel_size[0] * 0.5, 0, 0)) # add the shape to the image atten_image.add_shape(water_cyl, scale=9.687E-02) # z-pixel coordinate of the xy-crossection to show z = int(image_size[0] * 0.5) # show the phantom image atten_image_array = atten_image.as_array() show_2D_array('Attenuation image', atten_image_array[z, :, :]) # Create the activity image act_image = atten_image.clone() act_image.fill(0.0) # create the activity cylinder act_cyl = PET.EllipticCylinder() act_cyl.set_length(image_size[0] * voxel_size[0]) act_cyl.set_radii((image_size[1] * voxel_size[1] * 0.125, \ image_size[2] * voxel_size[2] * 0.125)) act_cyl.set_origin((0, image_size[1] * voxel_size[1] * 0.06, \ image_size[2] * voxel_size[2] * 0.06)) # add the shape to the image act_image.add_shape(act_cyl, scale=1) # z-pixel coordinate of the xy-crossection to show z = int(image_size[0] * 0.5) # show the phantom image act_image_array = act_image.as_array() show_2D_array('Activity image', act_image_array[z, :, :]) # Create the Single Scatter Simulation model sss = PET.SingleScatterSimulator() # Set the attenuation image sss.set_attenuation_image(atten_image) # set-up the scatter simulator sss.set_up(acq_template, act_image) # Simulate! sss_data = sss.forward(act_image) # show simulated scatter data simulated_scatter_as_array = sss_data.as_array() show_2D_array('scatter simulation', simulated_scatter_as_array[0, 0, :, :]) sss_data.write(output_file) ## let's also compute the unscattered counts (at the same low resolution) and compare acq_model = PET.AcquisitionModelUsingRayTracingMatrix() asm = PET.AcquisitionSensitivityModel(atten_image, acq_model) acq_model.set_acquisition_sensitivity(asm) acq_model.set_up(acq_template, act_image) #unscattered_data = acq_template.get_uniform_copy() unscattered_data = acq_model.forward(act_image) simulated_unscatter_as_array = unscattered_data.as_array() show_2D_array('unscattered simulation', simulated_unscatter_as_array[0, 0, :, :]) plt.figure() ax = plt.subplot(111) plt.plot(simulated_unscatter_as_array[0, 4, 0, :], label='unscattered') plt.plot(simulated_scatter_as_array[0, 4, 0, :], label='scattered') ax.legend() plt.show()
outp_prefix = args['--outp'] # Initial estimate initial_estimate = args['--initial'] visualisations = True if args['--visualisations'] and have_pylab else False nifti = True if args['--nifti'] else False use_gpu = True if args['--gpu'] else False descriptive_fname = True if args['--descriptive_fname'] else False update_obj_fn_interval = int(args['--update_obj_fn_interval']) # Verbosity verbosity = int(args['--verbosity']) pet.set_verbosity(verbosity) if verbosity == 0: msg_red = pet.MessageRedirector(None, None, None) # Save interval save_interval = int(args['--save_interval']) save_interval = min(save_interval, num_iters) # Convergence variables r_alpha = float(args['--alpha']) precond = True if args['--precond'] else False def get_resampler(image, ref=None, trans=None): """returns a NiftyResampler object for the specified transform and image""" if ref is None: ref = image resampler = reg.NiftyResampler()