def estimate_activity(self, iterations=DEFAULT_ITERATIONS, subset_size=DEFAULT_SUBSET_SIZE, subset_mode='random'): progress_bar = ProgressBar() progress_bar.set_percentage(0.1) activity = ones((self._p_n_pix_x,self._p_n_pix_y,self._p_n_pix_x),dtype=float32, order="F") for i in range(iterations): # Subsets: if subset_size is None: subsets_array=None subset_size=self._p_gantry_angular_positions elif subset_size>=self._p_gantry_angular_positions: subsets_array=None subset_size=self._p_gantry_angular_positions else: subsets_array = self._subset_generator.new_subset(subset_mode,subset_size) if subsets_array is not None: proj = self.project(activity,subsets_array=subsets_array).data P = (self._measurement[:,:,where(subsets_array)].reshape((self._p_n_pix_x,self._p_n_pix_y,subset_size))+EPS)/(proj+EPS) norm = self.backproject(ones(( self._p_n_pix_x,self._p_n_pix_y,subset_size ),dtype=float32, order="F"), subsets_array=subsets_array).data update = (self.backproject( P ,subsets_array=subsets_array).data+EPS) / (norm +EPS) else: proj = self.project(activity).data P = (self._measurement+EPS)/(proj+EPS) norm = self.get_normalization() update = (self.backproject( P ).data+EPS) / (norm +EPS) activity = activity * update #* self.get_mask().data progress_bar.set_percentage((i+1)*100.0/iterations) #print "Iteration: %d max act: %f min act: %f max proj: %f min proj: %f max norm: %f min norm: %f"%(i, activity.max(), activity.min(), proj.max(), proj.min(), norm.data.max(), norm.data.min() ) progress_bar.set_percentage(100.0) return Image3D(activity)
def display(self, scale=None, open_browser=False): data = self.data d = DisplayNode() images = [] progress_bar = ProgressBar(height='6px', width='100%%', background_color=LIGHT_GRAY, foreground_color=GRAY) if scale is not None: scale = scale * 255.0 / (data.max() + 1e-12) else: scale = 255.0 / (data.max() + 1e-12) N_projections = self.data.shape[2] N_x = self.data.shape[0] N_y = self.data.shape[1] print "SPECT Projection [N_projections: %d N_x: %d N_y: %d]" % ( N_projections, N_x, N_y) for i in range(N_projections): images.append( self.to_image(data, i, scale=scale, absolute_scale=True)) progress_bar.set_percentage(i * 100.0 / N_projections) progress_bar.set_percentage(100.0) return d.display('tipix', images, open_browser)
def estimate_activity(self, attenuation=None, psf=None, iterations=DEFAULT_ITERATIONS, subset_size=DEFAULT_SUBSET_SIZE, subset_mode='random', method="EM", m=32, factr=0.01, pgtol=1e-16, maxfun=10000, smoothing=0.0, activity=None): progress_bar = ProgressBar() progress_bar.set_percentage(0.1) if activity == None: activity = ones( (self._p_n_pix_x, self._p_n_pix_y, self._p_n_pix_x), dtype=float32, order="F") if attenuation is None: attenuation = self._attenuation if attenuation is not None: if isinstance(attenuation, ndarray): attenuation = float32(attenuation) else: attenuation = float32(attenuation.data) if psf is None: psf = self._psf if method == "EM": print "Reconstruction method: EM" for i in range(iterations): # Subsets: if subset_size is None: subsets_array = None subset_size = self._p_gantry_angular_positions elif subset_size >= self._p_gantry_angular_positions: subsets_array = None subset_size = self._p_gantry_angular_positions else: subsets_array = self._subset_generator.new_subset( subset_mode, subset_size) if subsets_array is not None: proj = self.project(activity, attenuation=attenuation, psf=psf, subsets_array=subsets_array).data measurement = self._measurement[:, :, where(subsets_array )].reshape( (self._p_n_pix_x, self._p_n_pix_y, subset_size)) measurement = asfortranarray( ascontiguousarray(measurement)) P = (measurement + EPS) / (proj + EPS) norm = self.backproject(ones( (self._p_n_pix_x, self._p_n_pix_y, subset_size), dtype=float32, order="F"), attenuation=attenuation, psf=psf, subsets_array=subsets_array).data update = (self.backproject( P, attenuation=attenuation, psf=psf, subsets_array=subsets_array).data + EPS) / (norm + EPS) else: proj = self.project(activity, attenuation=attenuation, psf=psf).data P = (self._measurement + EPS) / (proj + EPS) norm = self.get_normalization() update = (self.backproject( P, attenuation=attenuation, psf=psf).data + EPS) / (norm + EPS) activity = activity * update #* self.get_mask().data progress_bar.set_percentage((i + 1) * 100.0 / iterations) #print "Iteration: %d max act: %f min act: %f max proj: %f min proj: %f max norm: %f min norm: %f"%(i, activity.max(), activity.min(), proj.max(), proj.min(), norm.data.max(), norm.data.min() ) progress_bar.set_percentage(100.0) elif method == "LBFGS": print "Reconstruction method: LBFGS-B" bounds = [(None, None)] * activity.size for i in range(0, activity.size): bounds[i] = (0, None) args = [activity.shape, smoothing] activity0 = float64(activity.reshape(activity.size)) # print "SIZE ACTIVITY0: ",activity0.shape activity_rec, f, d = optimize.fmin_l_bfgs_b( self.get_likelihood, activity0, fprime=self.get_gradient_activity, m=m, factr=factr, pgtol=pgtol, args=args, maxfun=maxfun, iprint=0, bounds=bounds) activity = float32(activity_rec.reshape(activity.shape)) progress_bar.set_percentage(100.0) else: raise UnexpectedParameter("Reconstruction method %s unknown" % method) return Image3D(activity)