def fitter(prob, steps, num_steps, burn, num_burn, key): problem = FitProblem(prob) opts = Opts(DreamFit, '/tmp/bland/store_' + key, [burn, steps]) setup_logging() problem.path = '/mnt/hgfs/Ubuntu_Shared/mysite/bland/views.py' mapper = SerialMapper monitor = CustomMonitor(problem, key) #monitor = StepMonitor(problem, fp) extra_opts = { 'burn': num_burn, 'pop': 10, 'init': 'eps', 'steps': num_steps, 'thin': 1, 'samples': 10000 } fitdriver = FitDriver(DreamFit, problem=problem, monitors=[monitor], abort_test=lambda: False, **extra_opts) make_store(problem, opts, exists_handler=store_overwrite_query) resume_path = None fitdriver.mapper = mapper.start_mapper(problem, opts.args) best, fbest = fitdriver.fit(resume=resume_path) save_best(fitdriver, problem, best) mapper.stop_mapper(fitdriver.mapper) problem.model_update() #chisq = "\n" + fitdriver.problem.chisq_str() with open("/tmp/bland/store_" + key + "/out.txt", 'a') as txt_file: #txt_file.write(chisq) txt_file.write("\nComplete!") return
def calcScatteringPlane(self, h1, h2, UBmatrix, ei, stars): "Returns the chi and phi for the scattering plane defined by h1 and h2. Used with calcIdealAngles2." #Accepts two scattering plane vectors, h1 and h2, the UB matrix, and the wavelength #Should we allow the scattering plane to be defined by two vectors at different energies? #For now, I say NoN h1p = np.dot(UBmatrix, h1) h2p = np.dot(UBmatrix, h2) x0 = [0.0, 0.0, 0.0, 0.0] wavelength = e_to_wavelength(ei) q1 = calcq(h1[0], h1[1], h1[2], stars) twotheta1 = np.degrees(2.0 * np.arcsin(wavelength * q1 / 4.0 / np.pi)) q2 = calcq(h2[0], h2[1], h2[2], stars) twotheta2 = np.degrees(2.0 * np.arcsin(wavelength * q2 / 4.0 / np.pi)) if 1: #original openopt #outp=self.scatteringEquations([45,90,-90,0],h1p, h2p, q1, q2,wavelength,twotheta1/2,twotheta2/2) p0 = SNLE(self.scatteringEquations, x0, args=(h1p, h2p, q1, q2), contol=1e-15, ftol=1e-15, maxFunEvals=1e8, maxIter=1e5) r0 = p0.solve('nlp:ralg') #outp=self.scatteringEquations(r0.xf,h1p, h2p, q1, q2,wavelength,twotheta1/2,twotheta2/2) chi = r0.xf[0] #xf is the final array, xf[0] = chi phi = r0.xf[1] # xf[1] = phi if 0: import bumps from bumps.fitters import FIT_OPTIONS, FitDriver, DreamFit, StepMonitor, ConsoleMonitor import bumps.modelfn, bumps.fitproblem fn = lambda chi, phi, omega1, omega2: np.linalg.norm( self.scatteringEquations([chi, phi, omega1, omega2], h1p, h2p, q1, q2)) print fn(chi=45, phi=72.4, omega1=-90, omega2=0) M = bumps.modelfn.ModelFunction(fn, chi=0.0, phi=0.0, omega1=0.0, omega2=0.0) M._parameters['chi'].range(30, 60.) M._parameters['phi'].range(0, 90.) M._parameters['omega1'].range(-90, 90.) M._parameters['omega2'].range(-90, 90.) problem = bumps.fitproblem.FitProblem(M) fitdriver = FitDriver(DreamFit, problem=problem, burn=1000) best, fbest = fitdriver.fit() print best, fbest print 'done' print 'chi, phi', chi, phi return chi, phi
def bumps_fit(self, method='dream', pop=15, samples=1e5, burn=100, steps=0, thin=1, alpha=0, outliers='none', trim=False, monitors=[], problem=None, **options): # create a fitter similar to the bumps.fitters.fit function but with option for GUI monitoring from scipy.optimize import OptimizeResult from bumps.fitters import FitDriver, FIT_AVAILABLE_IDS, FITTERS, FIT_ACTIVE_IDS options['pop'] = pop options['samples'] = samples options['burn'] = burn options['steps'] = steps options['thin'] = thin options['alpha'] = alpha options['outliers'] = outliers options['trim'] = trim if problem is None: problem = self.bumps_problem() # verbose = True if method not in FIT_AVAILABLE_IDS: raise ValueError("unknown method %r not one of %s" % (method, ", ".join(sorted(FIT_ACTIVE_IDS)))) for fitclass in FITTERS: if fitclass.id == method: break driver = FitDriver(fitclass=fitclass, problem=problem, monitors=monitors, **options) driver.clip() # make sure fit starts within domain x0 = problem.getp() x, fx = driver.fit() problem.setp(x) dx = driver.stderr() result = OptimizeResult(x=x, dx=driver.stderr(), fun=fx, cov=driver.cov(), success=True, status=0, message="successful termination") if hasattr(driver.fitter, 'state'): result.state = driver.fitter.state return result
def calcScatteringPlane(self,h1, h2, UBmatrix, ei,stars): "Returns the chi and phi for the scattering plane defined by h1 and h2. Used with calcIdealAngles2." #Accepts two scattering plane vectors, h1 and h2, the UB matrix, and the wavelength #Should we allow the scattering plane to be defined by two vectors at different energies? #For now, I say NoN h1p = np.dot(UBmatrix, h1) h2p = np.dot(UBmatrix, h2) x0 = [0.0, 0.0, 0.0, 0.0] wavelength=e_to_wavelength(ei) q1 = calcq (h1[0], h1[1], h1[2], stars) twotheta1 = np.degrees(2.0 * np.arcsin(wavelength * q1 / 4.0 / np.pi)) q2 = calcq (h2[0], h2[1], h2[2], stars) twotheta2 = np.degrees(2.0 * np.arcsin(wavelength * q2 / 4.0 / np.pi)) if 1: #original openopt #outp=self.scatteringEquations([45,90,-90,0],h1p, h2p, q1, q2,wavelength,twotheta1/2,twotheta2/2) p0 = SNLE(self.scatteringEquations, x0, args=(h1p, h2p, q1, q2),contol=1e-15, ftol=1e-15,maxFunEvals=1e8,maxIter=1e5) r0 = p0.solve('nlp:ralg') #outp=self.scatteringEquations(r0.xf,h1p, h2p, q1, q2,wavelength,twotheta1/2,twotheta2/2) chi = r0.xf[0] #xf is the final array, xf[0] = chi phi = r0.xf[1] # xf[1] = phi if 0: import bumps from bumps.fitters import FIT_OPTIONS, FitDriver, DreamFit, StepMonitor, ConsoleMonitor import bumps.modelfn, bumps.fitproblem fn=lambda chi,phi,omega1,omega2: np.linalg.norm(self.scatteringEquations([chi,phi,omega1,omega2], h1p, h2p,q1,q2)) print fn(chi=45, phi=72.4, omega1=-90, omega2=0) M=bumps.modelfn.ModelFunction(fn,chi=0.0,phi=0.0,omega1=0.0,omega2=0.0) M._parameters['chi'].range(30,60.) M._parameters['phi'].range(0,90.) M._parameters['omega1'].range(-90,90.) M._parameters['omega2'].range(-90,90.) problem=bumps.fitproblem.FitProblem(M) fitdriver = FitDriver(DreamFit, problem=problem, burn=1000) best, fbest = fitdriver.fit() print best,fbest print 'done' print 'chi, phi', chi, phi return chi, phi
def fitter(prob, steps, num_steps, burn, num_burn, key): problem = FitProblem(prob) opts = Opts(DreamFit, '/tmp/bland/store_' + key, [burn, steps]) setup_logging() problem.path = '/mnt/hgfs/Ubuntu_Shared/mysite/bland/views.py' mapper = SerialMapper monitor = CustomMonitor(problem, key) #monitor = StepMonitor(problem, fp) extra_opts = {'burn': num_burn, 'pop': 10, 'init': 'eps', 'steps': num_steps, 'thin': 1, 'samples': 10000} fitdriver = FitDriver( DreamFit, problem=problem, monitors=[monitor], abort_test=lambda: False, **extra_opts) make_store(problem, opts, exists_handler=store_overwrite_query) resume_path = None fitdriver.mapper = mapper.start_mapper(problem, opts.args) best, fbest = fitdriver.fit(resume=resume_path) save_best(fitdriver, problem, best) mapper.stop_mapper(fitdriver.mapper) problem.model_update() #chisq = "\n" + fitdriver.problem.chisq_str() with open("/tmp/bland/store_" + key + "/out.txt", 'a') as txt_file: #txt_file.write(chisq) txt_file.write("\nComplete!") return
if 1: from bumps.mapper import MPMapper from bumps.cli import remember_best import pylab #mydirectory=r'D:\BiFeO3film\Mar27_2011' mydirectory = r'/net/charlotte/var/ftp/pub/ncnrdata/bt9/201102/ylem/BiFeO3film/Mar27_2011' myend = 'bt9' dataset = "mesh" + (sys.argv[1] if len(sys.argv) > 1 else "g") cost = sys.argv[2] if len(sys.argv) > 2 else "poisson" problem = build_problem(mydirectory, dataset, myend, cost) peak1 = problem.fitness.parts[0] if dataset[4] in 'tu': peak1.xc.value = -0.49 peak1.xc.range(-0.55, -0.4) if dataset[4] in 't': peak1.yc.value = -0.49 peak1.yc.range(-0.55, -0.4) if __name__ == "__main__": fitdriver = FitDriver(DreamFit, problem=problem, burn=50000) #mapper = MPMapper #fitdriver.mapper = mapper.start_mapper(problem, ()) #make_store(problem,opts,exists_handler=store_overwrite_query) problem.output_path = r'/tmp/TestBumpDir' best, fbest = fitdriver.fit() print best, fbest remember_best(fitdriver, problem, best) pylab.show() print 'done'