def check_final_value(self): report = {} if self.samplerEngine.check_ftval is not None: if self.trueValue is None: pyhrf.verbose(4, "Warning: no true val to check against for %s" % self.name) elif self.sampleFlag: fv = self.get_final_value() tv = self.get_true_value() rtol = 0.1 atol = 0.1 # report['true_value'] = tv # report['final_value'] = fv abs_error = np.abs(tv - fv) report["abs_error"] = abs_error rel_error = abs_error / np.maximum(np.abs(tv), np.abs(fv)) report["rel_error"] = rel_error report["accuracy"] = self.get_accuracy(abs_error, rel_error, fv, tv, atol, rtol) is_accurate = report["accuracy"][1].all() pyhrf.verbose( 2, "Fit error for %s: aerr=%f, rerr=%f, " "is_accurate=%s" % (self.name, abs_error.mean(), rel_error.mean(), is_accurate), ) if not is_accurate: m = ( "Final value of %s is not close to " "true value.\n -> aerror: %s\n -> rerror: %s\n" " Final value:\n %s\n True value:\n %s\n" % ( self.name, array_summary(report["abs_error"]), array_summary(report["rel_error"]), str(fv), str(tv), ) ) if self.samplerEngine.check_ftval == "raise": raise Exception(m) elif self.samplerEngine.check_ftval == "print": print "\n".join(["!! " + s for s in m.split("\n")]) self.report_check_ft_val = report
def check_against_truth(self, atol, rtol, inaccuracy_handling='print'): fv = self.get_estim_value_for_check() tv = self.get_true_value_for_check() if tv is None: pyhrf.verbose(4, 'Warning: no true val to check against for %s' \ %self.name) elif self.do_sampling: #when sampling is off, assume there is no need # to check against truth abs_error = np.abs(tv - fv) rel_error = abs_error/np.maximum(np.abs(tv),np.abs(fv)) acc = self.get_accuracy_against_truth(abs_error, rel_error, fv, tv, atol, rtol) is_accurate = acc[1].all() pyhrf.verbose(3, 'Check error for "%s" -> estim: %s, '\ 'truth: %s, atol=%f, rtol=%f' %(self.name, str(fv), str(tv), atol, rtol)) pyhrf.verbose(2, 'Fit error for "%s": avg aerr=%f, avg rerr=%f, '\ 'is_accurate=%s' %(self.name, abs_error.mean(), rel_error.mean(), is_accurate)) if not is_accurate: m = "Final value of %s is not close to " \ "true value.\n -> aerror: %s\n -> rerror: %s\n" \ " Final value:\n %s\n True value:\n %s\n" \ %(self.name, array_summary(abs_error), array_summary(rel_error), str(fv), str(tv)) if inaccuracy_handling == 'raise': raise Exception(m) elif inaccuracy_handling == 'print': print '\n'.join(['!! '+ s for s in m.split('\n')]) self.truth_checking_report = { 'abs_error' : abs_error, 'rel_error' : rel_error}
def check_final_value(self): report = {} if self.samplerEngine.check_ftval is not None: if self.trueValue is None: logger.info('Warning: no true val to check against for %s', self.name) elif self.sampleFlag: fv = self.get_final_value() tv = self.get_true_value() rtol = 0.1 # Relative tolerance value: 10% # Absolute tolerance value. TODO: dependency to parameter atol = 0.1 # report['true_value'] = tv # report['final_value'] = fv abs_error = np.abs(tv - fv) report['abs_error'] = abs_error rel_error = abs_error / np.maximum(np.abs(tv), np.abs(fv)) report['rel_error'] = rel_error report['accuracy'] = self.get_accuracy(abs_error, rel_error, fv, tv, atol, rtol) is_accurate = report['accuracy'][1].all() logger.info('Fit error for %s: aerr=%f, rerr=%f, is_accurate=%s', self.name, abs_error.mean(), rel_error.mean(), is_accurate) if not is_accurate: m = "Final value of %s is not close to " \ "true value.\n -> aerror: %s\n -> rerror: %s\n" \ " Final value:\n %s\n True value:\n %s\n" \ % (self.name, array_summary(report['abs_error']), array_summary(report['rel_error']), str(fv), str(tv)) if self.samplerEngine.check_ftval == 'raise': raise Exception(m) elif self.samplerEngine.check_ftval == 'print': print '\n'.join(['!! ' + s for s in m.split('\n')]) self.report_check_ft_val = report
def check_final_value(self): report = {} if self.samplerEngine.check_ftval is not None: if self.trueValue is None: logger.info('Warning: no true val to check against for %s', self.name) elif self.sampleFlag: fv = self.get_final_value() tv = self.get_true_value() rtol = 0.1 # Relative tolerance value: 10% # Absolute tolerance value. TODO: dependency to parameter atol = 0.1 # report['true_value'] = tv # report['final_value'] = fv abs_error = np.abs(tv - fv) report['abs_error'] = abs_error rel_error = abs_error / np.maximum(np.abs(tv), np.abs(fv)) report['rel_error'] = rel_error report['accuracy'] = self.get_accuracy(abs_error, rel_error, fv, tv, atol, rtol) is_accurate = report['accuracy'][1].all() logger.info( 'Fit error for %s: aerr=%f, rerr=%f, is_accurate=%s', self.name, abs_error.mean(), rel_error.mean(), is_accurate) if not is_accurate: m = "Final value of %s is not close to " \ "true value.\n -> aerror: %s\n -> rerror: %s\n" \ " Final value:\n %s\n True value:\n %s\n" \ % (self.name, array_summary(report['abs_error']), array_summary(report['rel_error']), str(fv), str(tv)) if self.samplerEngine.check_ftval == 'raise': raise Exception(m) elif self.samplerEngine.check_ftval == 'print': print '\n'.join(['!! ' + s for s in m.split('\n')]) self.report_check_ft_val = report
def check_against_truth(self, atol, rtol, inaccuracy_handling='print'): fv = self.get_estim_value_for_check() tv = self.get_true_value_for_check() if tv is None: logger.info( 'Warning: no true val to check against for %s', self.name) elif self.do_sampling: # when sampling is off, assume there is no need # to check against truth abs_error = np.abs(tv - fv) rel_error = abs_error / np.maximum(np.abs(tv), np.abs(fv)) acc = self.get_accuracy_against_truth(abs_error, rel_error, fv, tv, atol, rtol) is_accurate = acc[1].all() logger.info('Check error for "%s" -> ' 'estim: %s, truth: %s, atol=%f, rtol=%f', self.name, str(fv), str(tv), atol, rtol) logger.info('Fit error for "%s": avg aerr=%f, avg rerr=%f, ' 'is_accurate=%s', self.name, abs_error.mean(), rel_error.mean(), is_accurate) if not is_accurate: m = "Final value of %s is not close to " \ "true value.\n -> aerror: %s\n -> rerror: %s\n" \ " Final value:\n %s\n True value:\n %s\n" \ % (self.name, array_summary(abs_error), array_summary(rel_error), str(fv), str(tv)) if inaccuracy_handling == 'raise': raise Exception(m) elif inaccuracy_handling == 'print': print '\n'.join(['!! ' + s for s in m.split('\n')]) self.truth_checking_report = { 'abs_error': abs_error, 'rel_error': rel_error}
def get_string_value(self, v): if v.size == 1: return "%1.4f" % v[0] if self.axes_names is not None and "condition" == self.axes_names[0]: cNames = self.dataInput.cNames if v.ndim == 1: return get_2Dtable_string(v, cNames, ["Value"]) else: # print 'cNames:', cNames # print 'v.shape:', v.shape v = v[: len(cNames), :] return get_2Dtable_string(v.reshape(v.shape[0], 1, -1), cNames, ["Value"]) else: return array_summary(v)
def run(self): """ function to launch the analysis """ logger.info('Starting voxel-wise HRF estimation ...') logger.info('nvox=%d, ncond=%d, nscans=%d', self.nbVoxels, self.M, self.ImagesNb) # initialization of the matrices that will store all voxel resuls logger.info("Init storage ...") self.InitStorageMat() logger.info("Compute onset matrix ...") self.Compute_onset_matrix3() self.stop_iterations = np.zeros(self.bold.shape[1], dtype=int) # voxelwise analysis. This loop handles the currently analyzed voxels. for POI in xrange(self.bold.shape[1]): # POI = point of interest t0 = time() logger.info("Point %s / %s", str(POI), str(self.nbVoxels)) self.ReadPointOfInterestData(POI) # initialize with zeros or ones all matrix and vector used in # the class logger.info("Init matrix and vectors ...") self.InitMatrixAndVectors(POI) # compute onset matrix # compute low frequency basis logger.info('build low freq mat ...') self.buildLowFreqMat() # precompute usefull data logger.info('Compute inv R ...') self.Compute_INV_R_and_R_and_DET_R() # solve find response functions and hyperparameters logger.info('EM solver ...') self.EM_solver(POI) # store current results in matrices initialized in 'InitStoringMat' logger.info('Store res ...') self.StoreRes(POI) logger.info("Done in %s", format_duration(time() - t0)) self.clean_memory() logger.info('Nb of iterations to reach stop crit: %s', array_summary(self.stop_iterations))
def get_string_value(self, v): if v.size == 1: return '%1.4f' % v[0] if self.axes_names is not None and 'condition' == self.axes_names[0]: cNames = self.dataInput.cNames if v.ndim == 1: return get_2Dtable_string(v, cNames, ['Value']) else: # print 'cNames:', cNames # print 'v.shape:', v.shape v = v[:len(cNames), :] return get_2Dtable_string(v.reshape(v.shape[0], 1, -1), cNames, ['Value']) else: return array_summary(v)
def run(self): """ function to launch the analysis """ pyhrf.verbose(1, 'Starting voxel-wise HRF estimation ...') pyhrf.verbose(1, 'nvox=%d, ncond=%d, nscans=%d' \ %(self.nbVoxels,self.M,self.ImagesNb)) #initialization of the matrices that will store all voxel resuls pyhrf.verbose(3,"Init storage ...") self.InitStorageMat() pyhrf.verbose(3,"Compute onset matrix ...") self.Compute_onset_matrix3() self.stop_iterations = np.zeros(self.bold.shape[1], dtype=int) #voxelwise analysis. This loop handles the currently analyzed voxels. for POI in xrange(self.bold.shape[1]): # POI = point of interest t0 = time() pyhrf.verbose(3,"Point "+str(POI)+" / "+str(self.nbVoxels)) self.ReadPointOfInterestData(POI) #print "Signal in point " , POI , "read" # if hasattr(self, 'X'): # prevX = copyModule.deepcopy(self.X) # else: # prevX = None #initialize with zeros or ones all matrix and vector used in #the class pyhrf.verbose(4, "Init matrix and vectors ...") self.InitMatrixAndVectors(POI) #print "Matrix and vectors initialized" #compute onset matrix # print "Onset matrix computed" # if prevX is not None: # for i,x in enumerate(self.X): # print 'sess %d ->' %i, (prevX[i]!=x).any() # if 0: # for m in xrange(prevX[i].shape[0]): # for n in xrange(prevX[i].shape[1]): # for k in xrange(prevX[i].shape[2]): # print int(prevX[i][m,n,k]),'', # print '' # print '' # print '' # for m in xrange(x.shape[0]): # for n in xrange(x.shape[1]): # for k in xrange(x.shape[2]): # print int(x[m,n,k]), # print '' # print '' # print '' #compute low frequency basis pyhrf.verbose(4, 'build low freq mat ...') self.buildLowFreqMat() #print "Low frequency matrix generated" #precompute usefull data pyhrf.verbose(4, 'Compute inv R ...') self.Compute_INV_R_and_R_and_DET_R() #print "R^{-1} and det(R) precomputed" #solve find response functions and hyperparameters pyhrf.verbose(4, 'EM solver ...') self.EM_solver(POI) #store current results in matrices initialized in 'InitStoringMat' pyhrf.verbose(4, 'Store res ...') self.StoreRes(POI) pyhrf.verbose(3,"Done in %s" %format_duration(time() - t0) ) self.clean_memory() pyhrf.verbose(1, 'Nb of iterations to reach stop crit: %s' \ %array_summary(self.stop_iterations))