def main(): """Do main.""" # Acq model and template sino acq_model = pet.AcquisitionModelUsingRayTracingMatrix() acq_data = pet.AcquisitionData(sino_file) # If norm is present asm_norm = None if norm_e8_file: # create acquisition sensitivity model from ECAT8 normalisation data asm_norm = pet.AcquisitionSensitivityModel(norm_e8_file) # If attenuation is present asm_attn = None if attn_im_file: attn_image = pet.ImageData(attn_im_file) if trans: attn_image = resample_attn_image(attn_image) asm_attn = pet.AcquisitionSensitivityModel(attn_image, acq_model) # temporary fix pending attenuation offset fix in STIR: # converting attenuation into 'bin efficiency' asm_attn.set_up(acq_data) bin_eff = pet.AcquisitionData(acq_data) bin_eff.fill(1.0) print('applying attenuation (please wait, may take a while)...') asm_attn.unnormalise(bin_eff) asm_attn = pet.AcquisitionSensitivityModel(bin_eff) # Get ASM dependent on attn and/or norm if asm_norm and asm_attn: print("AcquisitionSensitivityModel contains norm and attenuation...") asm = pet.AcquisitionSensitivityModel(asm_norm, asm_attn) elif asm_norm: print("AcquisitionSensitivityModel contains norm...") asm = asm_norm elif asm_attn: print("AcquisitionSensitivityModel contains attenuation...") asm = asm_attn else: raise ValueError("Need norm and/or attn") # only need to project again if normalisation is added # (since attenuation has already been projected) if asm_norm: asm_attn.set_up(acq_data) bin_eff = pet.AcquisitionData(acq_data) bin_eff.fill(1.0) print('getting sinograms for multiplicative factors...') asm.set_up(acq_data) asm.unnormalise(bin_eff) print('writing multiplicative sinogram: ' + outp_file) bin_eff.write(outp_file)
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 setUp(self): path = os.path.join(examples_data_path('PET'), 'thorax_single_slice', 'template_sinogram.hs') if os.path.exists(path): template = pet.AcquisitionData(path) self.image1 = template.get_uniform_copy(0) self.image2 = template.get_uniform_copy(0) # assert False self.set_storage_scheme()
def get_asm_attn(sino, attn, acq_model): """Get attn ASM from sino, attn image and acq model.""" asm_attn = pet.AcquisitionSensitivityModel(attn, acq_model) # temporary fix pending attenuation offset fix in STIR: # converting attenuation into 'bin efficiency' asm_attn.set_up(sino) bin_eff = pet.AcquisitionData(sino) bin_eff.fill(1.0) asm_attn.unnormalise(bin_eff) asm_attn = pet.AcquisitionSensitivityModel(bin_eff) return asm_attn
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)
def setUp(self): if os.path.exists( os.path.join(examples_data_path('PET'), 'mMR', 'mMR_template_span11_small.hs')): template = pet.AcquisitionData( os.path.join(examples_data_path('PET'), 'mMR', 'mMR_template_span11_small.hs')) self.image1 = template.get_uniform_copy(0) self.image2 = template.get_uniform_copy(0) # assert False self.set_storage_scheme()
def get_acquisition_model(uMap, templ_sino): """Create acquisition model""" am = pet.AcquisitionModelUsingRayTracingMatrix() am.set_num_tangential_LORs(5) # Set up sensitivity due to attenuation asm_attn = pet.AcquisitionSensitivityModel(uMap, am) asm_attn.set_up(templ_sino) bin_eff = pet.AcquisitionData(templ_sino) bin_eff.fill(1.0) asm_attn.unnormalise(bin_eff) asm_attn = pet.AcquisitionSensitivityModel(bin_eff) am.set_acquisition_sensitivity(asm_attn) am.set_up(templ_sino, uMap) return am
def setUp(self): data_path = os.path.join(examples_data_path('PET'), 'thorax_single_slice') image = pet.ImageData(os.path.join(data_path, 'emission.hv')) am = pet.AcquisitionModelUsingRayTracingMatrix() am.set_num_tangential_LORs(5) templ = pet.AcquisitionData( os.path.join(data_path, 'template_sinogram.hs')) am.set_up(templ, image) acquired_data = am.forward(image) obj_fun = pet.make_Poisson_loglikelihood(acquired_data) obj_fun.set_acquisition_model(am) obj_fun.set_up(image) self.obj_fun = obj_fun self.image = image
def setUp(self): os.chdir(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') image = pet.ImageData('emission.hv') am = pet.AcquisitionModelUsingRayTracingMatrix() am.set_num_tangential_LORs(5) templ = pet.AcquisitionData('template_sinogram.hs') am.set_up(templ,image) acquired_data=am.forward(image) obj_fun = pet.make_Poisson_loglikelihood(acquired_data) obj_fun.set_acquisition_model(am) obj_fun.set_up(image) self.obj_fun = obj_fun self.image = image
# define the tm in the middle of the motion states num_tm = [] for time, dur in zip(time_intervals, time_frames): t = int(time / 2 + dur / 4) num_tm.append(t) print('Correct TM: {}'.format(num_tm)) #%% Files attn_file = py_path + '/UKL_data/mu_Map/stir_mu_map.hv' # .nii possible, requires ITK print('mu-Map: {}'.format(attn_file)) # template for acq_data template_acq_data = Pet.AcquisitionData('Siemens_mMR', span=11, max_ring_diff=16, view_mash_factor=1) template_acq_data.write('template.hs') #%% resample mu-Map into correct space and transform via invers tm tprint('Start Resampling') attn_image = Pet.ImageData(attn_file) template_image = template_acq_data.create_uniform_image(1.0) # define space matrices tm_fwd = numpy.loadtxt(py_path + '/UKL_data/tm_epi/reg_NAC_EPI.txt') tm_inv = numpy.loadtxt(py_path + '/UKL_data/tm_epi/reg_NAC_EPI_inv.txt') # settings for attn resampler resamplers_attn = Reg.NiftyResample()
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()
print('Create Folder: {}'.format(path_tm)) if not os.path.exists(path_mu): os.makedirs(path_mu, mode=0o770) print('Create Folder: {}'.format(path_mu)) if not os.path.exists(path_AC): os.makedirs(path_AC, mode=0o770) print('Create Folder: {}'.format(path_AC)) if not os.path.exists(path_moco): os.makedirs(path_moco, mode=0o770) print('Create Folder: {}'.format(path_moco)) #%% create template and set lm2sino converter # template for acq_data template_acq_data = Pet.AcquisitionData('Siemens_mMR', span=11, max_ring_diff=16, view_mash_factor=1) template_acq_data.write('template.hs') ### Create listmode-to-sinograms converter object ### lm2sino = Pet.ListmodeToSinograms() ### set input, output and template files ### lm2sino.set_input(list_file) lm2sino.set_output_prefix(sino_file) lm2sino.set_template('template.hs') #%% Define time frames (read frames.txt) and time intervals ### # path to folder with frames.txt frames_path = '/home/eric/Dokumente/PythonProjects/SIRF/UKL_data/frames/'
from ccpi.optimisation.functions import KullbackLeibler, BlockFunction, IndicatorBox, MixedL21Norm from ccpi.optimisation.operators import CompositionOperator, BlockOperator, LinearOperator from ccpi.plugins.regularisers import FGP_TV from ccpi.filters import regularisers # from ccpi.utilities.multiprocessing import NUM_THREADS from ccpi.utilities.display import plotter2D import time pet.AcquisitionData.set_storage_scheme('memory') pet.set_verbosity(0) # In[ ]: # load data template_acq_data = pet.AcquisitionData('Siemens_mMR', span=11, max_ring_diff=15, view_mash_factor=1) # data_path = '/home/sirfuser/devel/buildVM/sources/SIRF/data/examples/PET/mMR' # acq_data = pet.AcquisitionData('{}/sino_f1g1d0b0.hs'.format(data_path)) seconds = 600 data_path = '/home/edo/scratch/code/PETMR/install/share/sirf/NEMA' os.chdir(os.path.abspath(data_path)) acq_data = pet.AcquisitionData('NEMA_sino_0-{}s.hs'.format(seconds)) # fix a problem with the header which doesn't allow # to do algebra with randoms and sinogram # rand_arr = pet.AcquisitionData('{}/sino_randoms_f1g1d0b0.hs'.format(data_path)).as_array() rand_arr = pet.AcquisitionData('NEMA_randoms_0-{}s.hs'.format(seconds)) rand = acq_data * 0 rand.fill(rand_arr)
def main(): ########################################################################### # Parse input files ########################################################################### if trans_pattern is None: raise AssertionError("--trans missing") if sino_pattern is None: raise AssertionError("--sino missing") trans_files = sorted(glob(trans_pattern)) sino_files = sorted(glob(sino_pattern)) attn_files = sorted(glob(attn_pattern)) rand_files = sorted(glob(rand_pattern)) num_ms = len(sino_files) # Check some sinograms found if num_ms == 0: raise AssertionError("No sinograms found!") # Should have as many trans as sinos if num_ms != len(trans_files): raise AssertionError("#trans should match #sinos. " "#sinos = " + str(num_ms) + ", #trans = " + str(len(trans_files))) # If any rand, check num == num_ms if len(rand_files) > 0 and len(rand_files) != num_ms: raise AssertionError("#rand should match #sinos. " "#sinos = " + str(num_ms) + ", #rand = " + str(len(rand_files))) # For attn, there should be 0, 1 or num_ms images if len(attn_files) > 1 and len(attn_files) != num_ms: raise AssertionError("#attn should be 0, 1 or #sinos") ########################################################################### # Read input ########################################################################### if trans_type == "tm": trans = [reg.AffineTransformation(file) for file in trans_files] elif trans_type == "disp": trans = [ reg.NiftiImageData3DDisplacement(file) for file in trans_files ] elif trans_type == "def": trans = [reg.NiftiImageData3DDeformation(file) for file in trans_files] else: raise error("Unknown transformation type") sinos_raw = [pet.AcquisitionData(file) for file in sino_files] attns = [pet.ImageData(file) for file in attn_files] rands = [pet.AcquisitionData(file) for file in rand_files] # Loop over all sinograms sinos = [0] * num_ms for ind in range(num_ms): # If any sinograms contain negative values # (shouldn't be the case), set them to 0 sino_arr = sinos_raw[ind].as_array() if (sino_arr < 0).any(): print("Input sinogram " + str(ind) + " contains -ve elements. Setting to 0...") sinos[ind] = sinos_raw[ind].clone() sino_arr[sino_arr < 0] = 0 sinos[ind].fill(sino_arr) else: sinos[ind] = sinos_raw[ind] # If rebinning is desired segs_to_combine = 1 if args['--numSegsToCombine']: segs_to_combine = int(args['--numSegsToCombine']) views_to_combine = 1 if args['--numViewsToCombine']: views_to_combine = int(args['--numViewsToCombine']) if segs_to_combine * views_to_combine > 1: sinos[ind] = sinos[ind].rebin(segs_to_combine, views_to_combine) # only print first time if ind == 0: print(f"Rebinned sino dimensions: {sinos[ind].dimensions()}") ########################################################################### # Initialise recon image ########################################################################### if initial_estimate: image = pet.ImageData(initial_estimate) else: # Create image based on ProjData image = sinos[0].create_uniform_image(0.0, (nxny, nxny)) # If using GPU, need to make sure that image is right size. if use_gpu: dim = (127, 320, 320) spacing = (2.03125, 2.08626, 2.08626) # elif non-default spacing desired elif args['--dxdy']: dim = image.dimensions() dxdy = float(args['--dxdy']) spacing = (image.voxel_sizes()[0], dxdy, dxdy) if use_gpu or args['--dxdy']: image.initialise(dim=dim, vsize=spacing) image.fill(0.0) ########################################################################### # Set up resamplers ########################################################################### resamplers = [get_resampler(image, trans=tran) for tran in trans] ########################################################################### # Resample attenuation images (if necessary) ########################################################################### resampled_attns = None if len(attns) > 0: resampled_attns = [0] * num_ms # if using GPU, dimensions of attn and recon images have to match ref = image if use_gpu else None for i in range(len(attns)): # if we only have 1 attn image, then we need to resample into # space of each gate. However, if we have num_ms attn images, then # assume they are already in the correct position, so use None as # transformation. tran = trans[i] if len(attns) == 1 else None # If only 1 attn image, then resample that. If we have num_ms attn # images, then use each attn image of each frame. attn = attns[0] if len(attns) == 1 else attns[i] resam = get_resampler(attn, ref=ref, trans=tran) resampled_attns[i] = resam.forward(attn) ########################################################################### # Set up acquisition models ########################################################################### print("Setting up acquisition models...") if not use_gpu: acq_models = num_ms * [pet.AcquisitionModelUsingRayTracingMatrix()] else: acq_models = num_ms * [pet.AcquisitionModelUsingNiftyPET()] for acq_model in acq_models: acq_model.set_use_truncation(True) acq_model.set_cuda_verbosity(verbosity) # If present, create ASM from ECAT8 normalisation data asm_norm = None if norm_file: asm_norm = pet.AcquisitionSensitivityModel(norm_file) # Loop over each motion state for ind in range(num_ms): # Create attn ASM if necessary asm_attn = None if resampled_attns: asm_attn = get_asm_attn(sinos[ind], resampled_attns[i], acq_models[ind]) # Get ASM dependent on attn and/or norm asm = None if asm_norm and asm_attn: if ind == 0: print("ASM contains norm and attenuation...") asm = pet.AcquisitionSensitivityModel(asm_norm, asm_attn) elif asm_norm: if ind == 0: print("ASM contains norm...") asm = asm_norm elif asm_attn: if ind == 0: print("ASM contains attenuation...") asm = asm_attn if asm: acq_models[ind].set_acquisition_sensitivity(asm) if len(rands) > 0: acq_models[ind].set_background_term(rands[ind]) # Set up acq_models[ind].set_up(sinos[ind], image) ########################################################################### # Set up reconstructor ########################################################################### print("Setting up reconstructor...") # Create composition operators containing acquisition models and resamplers C = [ CompositionOperator(am, res, preallocate=True) for am, res in zip(*(acq_models, resamplers)) ] # Configure the PDHG algorithm if args['--normK'] and not args['--onlyNormK']: normK = float(args['--normK']) else: kl = [KullbackLeibler(b=sino, eta=(sino * 0 + 1e-5)) for sino in sinos] f = BlockFunction(*kl) K = BlockOperator(*C) # Calculate normK print("Calculating norm of the block operator...") normK = K.norm(iterations=10) print("Norm of the BlockOperator ", normK) if args['--onlyNormK']: exit(0) # Optionally rescale sinograms and BlockOperator using normK scale_factor = 1. / normK if args['--normaliseDataAndBlock'] else 1.0 kl = [ KullbackLeibler(b=sino * scale_factor, eta=(sino * 0 + 1e-5)) for sino in sinos ] f = BlockFunction(*kl) K = BlockOperator(*C) * scale_factor # If preconditioned if precond: def get_nonzero_recip(data): """Get the reciprocal of a datacontainer. Voxels where input == 0 will have their reciprocal set to 1 (instead of infinity)""" inv_np = data.as_array() inv_np[inv_np == 0] = 1 inv_np = 1. / inv_np data.fill(inv_np) tau = K.adjoint(K.range_geometry().allocate(1)) get_nonzero_recip(tau) tmp_sigma = K.direct(K.domain_geometry().allocate(1)) sigma = 0. * tmp_sigma get_nonzero_recip(sigma[0]) def precond_proximal(self, x, tau, out=None): """Modify proximal method to work with preconditioned tau""" pars = { 'algorithm': FGP_TV, 'input': np.asarray(x.as_array() / tau.as_array(), dtype=np.float32), 'regularization_parameter': self.lambdaReg, 'number_of_iterations': self.iterationsTV, 'tolerance_constant': self.tolerance, 'methodTV': self.methodTV, 'nonneg': self.nonnegativity, 'printingOut': self.printing } res, info = regularisers.FGP_TV(pars['input'], pars['regularization_parameter'], pars['number_of_iterations'], pars['tolerance_constant'], pars['methodTV'], pars['nonneg'], self.device) if out is not None: out.fill(res) else: out = x.copy() out.fill(res) out *= tau return out FGP_TV.proximal = precond_proximal print("Will run proximal with preconditioned tau...") # If not preconditioned else: sigma = float(args['--sigma']) # If we need to calculate default tau if args['--tau']: tau = float(args['--tau']) else: tau = 1 / (sigma * normK**2) if regularisation == 'none': G = IndicatorBox(lower=0) elif regularisation == 'FGP_TV': r_iterations = float(args['--reg_iters']) r_tolerance = 1e-7 r_iso = 0 r_nonneg = 1 r_printing = 0 device = 'gpu' if use_gpu else 'cpu' G = FGP_TV(r_alpha, r_iterations, r_tolerance, r_iso, r_nonneg, r_printing, device) else: raise error("Unknown regularisation") if precond: def PDHG_new_update(self): """Modify the PDHG update to allow preconditioning""" # save previous iteration self.x_old.fill(self.x) self.y_old.fill(self.y) # Gradient ascent for the dual variable self.operator.direct(self.xbar, out=self.y_tmp) self.y_tmp *= self.sigma self.y_tmp += self.y_old self.f.proximal_conjugate(self.y_tmp, self.sigma, out=self.y) # Gradient descent for the primal variable self.operator.adjoint(self.y, out=self.x_tmp) self.x_tmp *= -1 * self.tau self.x_tmp += self.x_old self.g.proximal(self.x_tmp, self.tau, out=self.x) # Update self.x.subtract(self.x_old, out=self.xbar) self.xbar *= self.theta self.xbar += self.x PDHG.update = PDHG_new_update # Get filename outp_file = outp_prefix if descriptive_fname: if len(attn_files) > 0: outp_file += "_wAC" if norm_file: outp_file += "_wNorm" if use_gpu: outp_file += "_wGPU" outp_file += "_Reg-" + regularisation if regularisation == 'FGP_TV': outp_file += "-alpha" + str(r_alpha) outp_file += "-riters" + str(r_iterations) if args['--normK']: outp_file += '_userNormK' + str(normK) else: outp_file += '_calcNormK' + str(normK) if args['--normaliseDataAndBlock']: outp_file += '_wDataScale' else: outp_file += '_noDataScale' if not precond: outp_file += "_sigma" + str(sigma) outp_file += "_tau" + str(tau) else: outp_file += "_wPrecond" outp_file += "_nGates" + str(len(sino_files)) if resamplers is None: outp_file += "_noMotion" pdhg = PDHG(f=f, g=G, operator=K, sigma=sigma, tau=tau, max_iteration=num_iters, update_objective_interval=update_obj_fn_interval, x_init=image, log_file=outp_file + ".log") def callback_save(iteration, objective_value, solution): """Callback function to save images""" if (iteration + 1) % save_interval == 0: out = solution if not nifti else reg.NiftiImageData(solution) out.write(outp_file + "_iters" + str(iteration + 1)) pdhg.run(iterations=num_iters, callback=callback_save, verbose=True, very_verbose=True) if visualisations: # show reconstructed image out = pdhg.get_output() out_arr = out.as_array() z = out_arr.shape[0] // 2 show_2D_array('Reconstructed image', out.as_array()[z, :, :]) pylab.show()
def main(): # Make output folder if necessary if not os.path.isdir(data_path): os.makedirs(data_path) os.chdir(data_path) # Download the data print("downloading brainweb data...") [FDG_arr, uMap_arr, T1_arr] = download_data() # Get template PET image from template raw template_PET_raw = pet.AcquisitionData(template_PET_raw_path) template_PET_im = pet.ImageData(template_PET_raw) # Get template MR image from template raw template_MR_raw = mr.AcquisitionData(template_MR_raw_path) template_MR_raw.sort_by_time() template_MR_raw = mr.preprocess_acquisition_data(template_MR_raw) template_MR_im = simple_mr_recon(template_MR_raw) # Number voxels in (x,y) directions - nxy (dictated by MR image) nxy = template_MR_im.get_geometrical_info().get_size()[0] if nxy != template_MR_im.get_geometrical_info().get_size()[1]: raise AssertionError("Expected square image in (x,y) direction") if template_MR_im.get_geometrical_info().get_size()[2] > 1: raise AssertionError("Only currently designed for 2D image") # Create PET image dim = (1, nxy, nxy) size = FDG_arr.shape z_slice = size[0] // 2 xy_min = (size[1] - nxy) // 2 xy_max = xy_min + nxy voxel_size = template_PET_im.voxel_sizes() template_PET_im.initialise(dim, voxel_size) # Reorient template MR image with template PET image such that it's compatible with both template_MR_im.reorient(template_PET_im.get_geometrical_info()) ############################################################################################ # Crop brainweb image to right size ############################################################################################ # Convert brainweb's (127,344,344) to desired size print("Cropping brainweb images to size...") [FDG, uMap, T1] = [crop_brainweb(template_MR_im, im_arr, z_slice, xy_min, xy_max) \ for im_arr in [FDG_arr, uMap_arr, T1_arr]] ############################################################################################ # Apply motion ############################################################################################ print("Resampling images to different motion states...") FDGs = [0] * num_ms uMaps = [0] * num_ms T1s = [0] * num_ms for ind in range(num_ms): # Get TM for given motion state tm = get_and_save_tm(ind) # Get resampler res = get_resampler_from_tm(tm, template_MR_im) # Resample for im, modality in zip([FDG, uMap, T1], ['FDG', 'uMap', 'T1']): resampled = res.forward(im) if modality == 'FDG': FDGs[ind] = resampled elif modality == 'uMap': uMaps[ind] = resampled elif modality == 'T1': T1s[ind] = resampled else: raise AssertionError("Unknown modality") reg.NiftiImageData(resampled).write(modality + '_ms' + str(ind)) ############################################################################################ # MR: create k-space data for motion states ############################################################################################ # Create coil sensitivity data print("Calculating coil sensitivity map...") csm = mr.CoilSensitivityData() csm.smoothness = 500 csm.calculate(template_MR_raw) # Create interleaved sampling print("Creating raw k-space data for MR motion states...") mvec = [] for ind in range(num_ms): mvec.append(np.arange(ind, template_MR_raw.number(), num_ms)) # Go through motion states and create k-space for ind in range(num_ms): acq_ms = template_MR_raw.new_acquisition_data(empty=True) # Set first two (??) acquisition acq_ms.append_acquisition(template_MR_raw.acquisition(0)) acq_ms.append_acquisition(template_MR_raw.acquisition(1)) # Add motion resolved data for jnd in range(len(mvec[ind])): if mvec[ind][jnd] < template_MR_raw.number() - 1 and mvec[ind][ jnd] > 1: # Ensure first and last are not added twice cacq = template_MR_raw.acquisition(mvec[ind][jnd]) acq_ms.append_acquisition(cacq) # Set last acquisition acq_ms.append_acquisition( template_MR_raw.acquisition(template_MR_raw.number() - 1)) # Create acquisition model AcqMod = mr.AcquisitionModel(acq_ms, T1s[ind]) AcqMod.set_coil_sensitivity_maps(csm) # Forward project! acq_ms_sim = AcqMod.forward(T1s[ind]) # Save print("writing: " + 'raw_T1_ms' + str(ind) + '.h5') acq_ms_sim.write('raw_T1_ms' + str(ind) + '.h5') ############################################################################################ # PET: create sinograms ############################################################################################ print("Creating singorams for PET motion states...") stir_uMap = template_PET_im.clone() stir_FDG = template_PET_im.clone() for ind in range(num_ms): stir_uMap.fill(uMaps[ind].as_array()) stir_FDG.fill(FDGs[ind].as_array()) am = get_acquisition_model(stir_uMap, template_PET_raw) FDG_sino = am.forward(stir_FDG) FDG_sino = add_noise(0.25, FDG_sino) FDG_sino.write('raw_FDG_ms' + str(ind))
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), sorted_alphanumeric(list_rando)): sino_pet = Pet.AcquisitionData(path_sino + sino) print(sino) randoms_pet = Pet.AcquisitionData(path_rando + random) print(random) # reconstruct the data (without mu-map) obj_fun = Pet.make_Poisson_loglikelihood(sino_pet) acq_model.set_background_term(randoms_pet) recon.set_objective_function(obj_fun) initial_image = sino_pet.create_uniform_image(1.0) image = initial_image recon.set_up(image) recon.set_current_estimate(image) recon.process()
def test_main(rec=False, verb=False, throw=True): # Set STIR verbosity to off original_verb = pet.get_verbosity() pet.set_verbosity(1) time.sleep(0.5) sys.stderr.write("Testing NiftyPET projector...") time.sleep(0.5) # Get image image = get_image() # Get AM try: acq_model = pet.AcquisitionModelUsingNiftyPET() except: return 1, 1 acq_model.set_cuda_verbosity(verb) data_path = examples_data_path('PET') # raw_data_path = pet.existing_filepath(os.path.join(data_path, 'mMR'), 'mMR_template_span11.hs') raw_data_path = os.path.join(data_path, 'mMR') template_acq_data = pet.AcquisitionData( os.path.join(raw_data_path, 'mMR_template_span11.hs')) acq_model.set_up(template_acq_data, image) # Test operator adjointness if verb: print('testing adjointness') if not is_operator_adjoint(acq_model, num_tests=1, verbose=True): raise AssertionError('NiftyPet AcquisitionModel is not adjoint') # Generate test data simulated_acq_data = acq_model.forward(image) simulated_acq_data_w_noise = add_noise(simulated_acq_data, 10) obj_fun = pet.make_Poisson_loglikelihood(template_acq_data) obj_fun.set_acquisition_model(acq_model) recon = pet.OSMAPOSLReconstructor() recon.set_objective_function(obj_fun) recon.set_num_subsets(1) recon.set_num_subiterations(1) recon.set_input(simulated_acq_data_w_noise) if verb: print('setting up, please wait...') initial_estimate = image.get_uniform_copy() recon.set_up(initial_estimate) if verb: print('reconstructing...') recon.set_current_estimate(initial_estimate) recon.process() reconstructed_im = recon.get_output() if not reconstructed_im: raise AssertionError() # Reset original verbose-ness pet.set_verbosity(original_verb) return 0, 1
] list_rando = [ f for f in os.listdir(working_folder + '/rando/') if f.endswith(".hs") ] list_mu = [f for f in os.listdir(path_mu) if f.endswith(".nii")] #%% AC reconstruction tprint('Start AC Recon') for i, sino, random, mu in zip(range(len(path_sino)), sorted_alphanumeric(list_sino), sorted_alphanumeric(list_rando), sorted_alphanumeric(list_mu)): sino_pet = Pet.AcquisitionData(path_sino + sino) print(sino) randoms_pet = Pet.AcquisitionData(path_rando + random) print(random) mu_pet = Pet.ImageData(path_mu + mu) print(mu) # definitions for attenuation attn_acq_model = Pet.AcquisitionModelUsingRayTracingMatrix() asm_attn = Pet.AcquisitionSensitivityModel(mu_pet, attn_acq_model) # reconstruct the data (includes all) obj_fun = Pet.make_Poisson_loglikelihood(sino_pet) asm_attn.set_up(sino_pet) attn_factors = Pet.AcquisitionData(sino_pet) attn_factors.fill(1.0)