def calculate_roi_perf(self, roi_name): input_tac = np.array(self.rois.get_concentrations('aorta')) input_tac -= input_tac[0] real_tac = np.array(self.rois.get_concentrations(roi_name)) real_tac -= real_tac[0] times = self.rois.get_time_list() print 'times', times new_times = np.arange(times[0], times[-1] + 1, 1) print map(int, times) smoothed_tac, in_tac = express.spline_interpolation( input_tac, times, new_times) s_tissue_tac, s_tissue_tac_pars = express.spline_interpolation( real_tac, times, new_times, ss=0.1) modeled_tac, modeled_pars = express.calc_tissue_tac_mrx_conv( smoothed_tac, time_steps=new_times, time_subset=times, rc_type='lognorm', mtts=np.arange(1, 70, 4), bvs=np.arange(0, 1, 0.2), rc_sigma=np.arange(0.01, 1, 0.2)) # modeled_tac, modeled_pars = express.calc_tissue_tac_mrx(in_tac, times=times, mtts=np.arange(5,80,1), bvs=np.arange(0.1,0.9,0.1)) modeled_pars = np.array(modeled_pars) print modeled_pars.shape ssd = np.sum((modeled_tac - real_tac)**2, axis=-1) best_choice = modeled_tac[ssd.argmin()] subset_array = ssd.argsort()[:10] print modeled_pars[ssd.argmin()] plt.subplot(3, 1, 1) plt.plot(times, real_tac, 'o-') plt.plot(times, best_choice, '--') gr = np.array([modeled_tac[i] for i in subset_array]) # print gr.shape plt.plot(times, gr.T, '-', alpha=0.3) plt.subplot(3, 1, 2) bv_subset = modeled_pars[:, 1][subset_array] cmap = plt.cm.coolwarm(bv_subset / np.max(bv_subset)) print modeled_pars[subset_array] plt.scatter(modeled_pars[:, 0][subset_array], modeled_pars[:, 1][subset_array], marker='o', edgecolors=cmap) plt.subplot(3, 1, 3) plt.plot(modeled_pars[:, 0][subset_array], ssd[subset_array], 'o') plt.show()
def make_4dvol_frommaps(self): #load maps bv,mtt,bf,sigma perfmap_art = nib.load( self.dir_manager.get_path('perf_map')).get_data() perfmap_port = nib.load( os.path.join( self.dir_manager.get_path('perf_map')[:-7] + '2.nii.gz')).get_data() perfmap = np.concatenate( (perfmap_art[..., (1, 0, 3)], perfmap_port[..., (1, 0, 3)]), axis=3) perfmap[..., (0, 3)] /= 100. perfmap2d = perfmap.reshape( (np.prod(perfmap.shape[:-1]), perfmap.shape[-1])) pars_subset = perfmap2d[np.where(perfmap2d[..., 0] != 0)] #load input tacs times = self.rois.get_time_list() new_time_steps = np.arange(times[0], times[-1] + 1, 2) input_tac = np.array(self.rois.get_concentrations('aorta')) input_tac -= input_tac[0] input_tac_smoothed, input_tac_splines = express.spline_interpolation( input_tac, times, new_time_steps) try: input_tac2 = np.array(self.rois.get_concentrations('porta')) input_tac2 -= input_tac2[0] input_tac_smoothed2, input_tac_splines2 = express.spline_interpolation( input_tac2, times, new_time_steps) except: input_tac2 = None pass vol4d = express.calc_tissue_tac_from_pars(input_tac_smoothed, input_tac_smoothed2, time_steps=new_time_steps, time_subset=[60, 70, 80], params=pars_subset) vol4d = vol4d.reshape( (np.prod(perfmap.shape[:-1]), vol4d_2d.shape[-1])) hdr = nib.load(self.dir_manager.get_path('4d')).get_header() mrx = hdr.get_sform() nii_im = nib.Nifti1Image(out_vol, mrx, hdr) nib.save( nii_im, os.path.join(self.dir_manager.get_path('4D')[:-4] + '2.nii.gz'))
def make_4dvol_frommaps(self): #load maps bv,mtt,bf,sigma perfmap_art = nib.load(self.dir_manager.get_path('perf_map')).get_data() perfmap_port = nib.load(os.path.join(self.dir_manager.get_path('perf_map')[:-7]+'2.nii.gz')).get_data() perfmap = np.concatenate((perfmap_art[...,(1,0,3)],perfmap_port[...,(1,0,3)]),axis=3) perfmap[...,(0,3)] /= 100. perfmap2d = perfmap.reshape( (np.prod(perfmap.shape[:-1]),perfmap.shape[-1]) ) pars_subset = perfmap2d[ np.where(perfmap2d[...,0] != 0)] #load input tacs times = self.rois.get_time_list() new_time_steps = np.arange(times[0], times[-1] + 1, 2) input_tac = np.array(self.rois.get_concentrations('aorta')) input_tac -= input_tac[0] input_tac_smoothed, input_tac_splines = express.spline_interpolation(input_tac, times, new_time_steps) try: input_tac2 = np.array(self.rois.get_concentrations('porta')) input_tac2 -= input_tac2[0] input_tac_smoothed2, input_tac_splines2 = express.spline_interpolation(input_tac2, times, new_time_steps) except: input_tac2 = None pass vol4d = express.calc_tissue_tac_from_pars(input_tac_smoothed, input_tac_smoothed2, time_steps=new_time_steps, time_subset=[60,70,80], params=pars_subset) vol4d = vol4d.reshape( (np.prod(perfmap.shape[:-1]),vol4d_2d.shape[-1]) ) hdr = nib.load(self.dir_manager.get_path('4d')).get_header() mrx = hdr.get_sform() nii_im = nib.Nifti1Image(out_vol, mrx, hdr) nib.save(nii_im, os.path.join(self.dir_manager.get_path('4D')[:-4]+'2.nii.gz'))
def calculate_roi_perf(self): input_tac = np.array(self.rois.get_concentrations('aorta')) input_tac -= input_tac[0] real_tac = np.array(self.rois.get_concentrations('roi1')) print real_tac real_tac -= real_tac[0] times = self.rois.get_time_list() print 'times', times new_times = np.arange(times[0], times[-1] + 1, 1) print map(int, times) smoothed_tac, in_tac = express.spline_interpolation(input_tac, times, new_times) s_tissue_tac, s_tissue_tac_pars = express.spline_interpolation(real_tac, times, new_times, ss=0.1) modeled_tac, modeled_pars = express.calc_tissue_tac_mrx_conv(smoothed_tac, time_steps=new_times, time_subset=times, rc_type='lognorm', mtts=np.arange(1, 70, 4), bvs=np.arange(0, 1, 0.1), rc_sigma=np.arange(0.01, 1, 0.2)) # modeled_tac, modeled_pars = express.calc_tissue_tac_mrx(in_tac, times=times, mtts=np.arange(5,80,1), bvs=np.arange(0.1,0.9,0.1)) modeled_pars = np.array(modeled_pars) print modeled_pars.shape ssd = np.sum((modeled_tac - real_tac) ** 2, axis=-1) best_choice = modeled_tac[ssd.argmin()] subset_array = ssd.argsort()[:10] print modeled_pars[ssd.argmin()] plt.subplot(3, 1, 1) plt.plot(times, real_tac, 'o-') plt.plot(times, best_choice, '--') gr = np.array([modeled_tac[i] for i in subset_array]) #print gr.shape plt.plot(times, gr.T, '-', alpha=0.3) plt.subplot(3, 1, 2) bv_subset = modeled_pars[:, 1][subset_array] cmap = plt.cm.coolwarm(bv_subset / np.max(bv_subset)) print modeled_pars[subset_array] plt.scatter(modeled_pars[:, 0][subset_array], modeled_pars[:, 1][subset_array], marker='o', edgecolors=cmap) plt.subplot(3, 1, 3) plt.plot(modeled_pars[:, 0][subset_array], ssd[subset_array], 'o') plt.show()
def create_tacs(input_tac, times): new_time_steps = np.arange(times[0], times[-1] + 1, 1) input_tac_smoothed, input_tac_splines = express.spline_interpolation(input_tac, times, new_time_steps) pars_subset = express.combine_pars(bv=np.arange(0.1,0.60,0.1), a=np.arange(1,15,0.5), loc=np.arange(0.1,13,0.3), scale=np.arange(0.1,6,0.3)) tacs = express.calc_tissue_tacs_mrx_3gamma(input_tac_smoothed, params = pars_subset, time_steps = new_time_steps, time_subset = times) return tacs,pars_subset
def get_limit(self): #load input tacs times = self.rois.get_time_list() new_time_steps = np.arange(times[0], times[-1] + 1, 1) input_tac = np.array(self.rois.get_concentrations('aorta')) input_tac -= input_tac[0] input_tac_smoothed, input_tac_splines = express.spline_interpolation( input_tac, times, new_time_steps) try: input_tac2 = np.array(self.rois.get_concentrations('porta')) input_tac2 -= input_tac2[0] input_tac_smoothed2, input_tac_splines2 = express.spline_interpolation( input_tac2, times, new_time_steps) except: input_tac2 = None pass pars_subset = express.perf_pars_gen(bvs=np.arange(0.01, 0.1, 0.05), mtts=np.arange(10, 100, 10), sigmas=np.arange(0.1, 2, 0.5), lags=(0, )) vol4d = express.calc_tissue_tac_from_pars(input_tac_smoothed, input_tac_smoothed2, time_steps=new_time_steps, time_subset=times, params=pars_subset) max_hu = np.max(vol4d, axis=0) print np.array(np.where(pars_subset[:, 5] < 0.1)).shape sp = np.array( np.where(pars_subset[:, 1] < 0.1) + np.where(pars_subset[:, 5] < 0.1))[0] print sp.shape plt.plot(times, vol4d[:, sp], 'o-', alpha=0.05, color='red') plt.show()
def get_limit(self): #load input tacs times = self.rois.get_time_list() new_time_steps = np.arange(times[0], times[-1] + 1, 1) input_tac = np.array(self.rois.get_concentrations('aorta')) input_tac -= input_tac[0] input_tac_smoothed, input_tac_splines = express.spline_interpolation(input_tac, times, new_time_steps) try: input_tac2 = np.array(self.rois.get_concentrations('porta')) input_tac2 -= input_tac2[0] input_tac_smoothed2, input_tac_splines2 = express.spline_interpolation(input_tac2, times, new_time_steps) except: input_tac2 = None pass pars_subset = express.perf_pars_gen(bvs=np.arange(0.01,0.1,0.05), mtts=np.arange(10,100,10), sigmas=np.arange(0.1,2,0.5), lags=(0,)) vol4d = express.calc_tissue_tac_from_pars(input_tac_smoothed, input_tac_smoothed2, time_steps=new_time_steps, time_subset=times, params=pars_subset) max_hu = np.max(vol4d ,axis=0) print np.array(np.where(pars_subset[:,5]<0.1)).shape sp = np.array(np.where(pars_subset[:,1]<0.1)+np.where(pars_subset[:,5]<0.1))[0] print sp.shape plt.plot(times,vol4d[:,sp],'o-',alpha=0.05,color='red') plt.show()
tacs_real[l[0]]['value'].append(l[3]) else: tacs_real[l[0]] = {'times': [], 'value': []} print tacs_real sample_size = len(tacs_real.keys()) pancreatic_pars = { 'a': np.random.normal(2, 0.5, size=sample_size), 'loc': np.random.normal(1, 0.5, size=sample_size), 'scale': np.random.normal(14, 5, size=sample_size), 'bv': np.random.normal(40, 15, size=sample_size) } #make input tac new_time_steps = np.arange(times[0], times[-1] + 1, 1) input_tac_smoothed, input_tac_splines = express.spline_interpolation( AORTA, times, new_time_steps) time_steps = np.arange(10, 60, 2) #lag and scale input input_tac_smoothed = lag_input(input_tac_smoothed) input_tac_smoothed -= input_tac_smoothed[0] pars_subset = express.perf_pars_gen(bvs=np.arange(0.01, 0.7, 0.1), mtts=np.arange(2, 40, 5), sigmas=np.arange(0.1, 20, 10), lags=(0, ), bvs2=(0, ), mtts2=(0, ), sigmas2=(0, ), lags2=(0, )) pancreatic_pars = np.array([[1, 0.46, 0, 20, 0, 0, 0, 0], [9, 0.28, 0, 16, 0, 0, 0, 0],
26, 29, 31, 33, 42, 47, 52, 57, 62, 91, 101, 111]) """ #make input tac new_time_steps = np.arange(times[0], times[-1] + 1, 1) input_tac_smoothed, input_tac_splines = express.spline_interpolation(AORTA, times, new_time_steps) time_steps = np.arange(10,60,2) #lag and scale input input_tac_smoothed = lag_input(input_tac_smoothed) input_tac_smoothed-=input_tac_smoothed[0] pars_subset = express.perf_pars_gen(bvs=np.arange(0.01,0.7,0.1), mtts=np.arange(2,40,5), sigmas=np.arange(0.1,20,10), lags=(0,), bvs2=(0,), mtts2=(0,), sigmas2=(0,), lags2=(0,)) pancreatic_pars = np.array([[1.2,0.7,0,81,0,0,0,0], [9,0.28,0,16,0,0,0,0],
input_tac2 = np.array(input_tac2)-input_tac2[0] # BV_range_a = np.arange(0.01,0.99,0.4) BV_range_p = (0.01,1,0.1) MTT_range_a = np.arange(0.1,100,30) MTT_range_p = (0,100,1) S_range_a = np.arange(0.01,2,0.5) S_range_p = (0.01,2,0.1) #Simulation new_time_steps = np.arange(times[0], times[-1] + 1, 1) input_tac_smoothed, input_tac_splines = expr.spline_interpolation(input_tac, times, new_time_steps) input_tac_smoothed2, input_tac_splines2 = expr.spline_interpolation(input_tac2, times, new_time_steps) input_tac_smoothed2[new_time_steps < times[0]] = input_tac2[0] input_tac_smoothed[new_time_steps < times[0]] = input_tac[0] reference_tacs, reference_pars = expr.calc_tissue_tac_mrx_conv2(input_tac_smoothed, input_tac_smoothed2, time_steps=new_time_steps, time_subset=times, rc_type='lognorm', mtts=MTT_range_a, bvs=BV_range_a, rc_sigma=S_range_a, lags=[0])
input_tac2 = np.array(input_tac2) - input_tac2[0] # BV_range_a = np.arange(0.01, 0.99, 0.4) BV_range_p = (0.01, 1, 0.1) MTT_range_a = np.arange(0.1, 100, 30) MTT_range_p = (0, 100, 1) S_range_a = np.arange(0.01, 2, 0.5) S_range_p = (0.01, 2, 0.1) #Simulation new_time_steps = np.arange(times[0], times[-1] + 1, 1) input_tac_smoothed, input_tac_splines = expr.spline_interpolation( input_tac, times, new_time_steps) input_tac_smoothed2, input_tac_splines2 = expr.spline_interpolation( input_tac2, times, new_time_steps) input_tac_smoothed2[new_time_steps < times[0]] = input_tac2[0] input_tac_smoothed[new_time_steps < times[0]] = input_tac[0] reference_tacs, reference_pars = expr.calc_tissue_tac_mrx_conv2( input_tac_smoothed, input_tac_smoothed2, time_steps=new_time_steps, time_subset=times, rc_type='lognorm', mtts=MTT_range_a, bvs=BV_range_a, rc_sigma=S_range_a,