def run_fitting(): """ main function to run fitting """ simulation = get_simulation() sample = get_sample() simulation.setSample(sample) # the core method of this example which adds masks to the simulation add_mask_to_simulation(simulation) real_data = create_real_data() fit_suite = ba.FitSuite() fit_suite.addSimulationAndRealData(simulation, real_data) fit_suite.initPrint(10) draw_observer = ba.DefaultFitObserver(draw_every_nth=10) fit_suite.attachObserver(draw_observer) # setting fitting parameters with starting values fit_suite.addFitParameter("*/Cylinder/Radius", 6. * nm).setLimited(4., 8.) fit_suite.addFitParameter("*/Cylinder/Height", 9. * nm).setLimited(8., 12.) # running fit fit_suite.runFit() print("Fitting completed.") fit_suite.printResults() print("chi2:", fit_suite.getChi2()) print("chi2:", fit_suite.getChi2()) for fitPar in fit_suite.fitParameters(): print(fitPar.name(), fitPar.value(), fitPar.error())
def run_fitting(): """ main function to run fitting """ simulation = get_simulation() simulation.setSampleBuilder(MySampleBuilder()) real_data = create_real_data() fit_suite = ba.FitSuite() fit_suite.addSimulationAndRealData(simulation, real_data) fit_suite.initPrint(10) draw_observer = ba.DefaultFitObserver(draw_every_nth=10) fit_suite.attachObserver(draw_observer) # setting fitting parameters with starting values fit_suite.addFitParameter("*radius", 8. * nm, ba.AttLimits.limited(4., 12.)) fit_suite.addFitParameter("*lattice_constant", 8. * nm, ba.AttLimits.limited(4., 12.)) print(fit_suite.treeToString()) print(fit_suite.parametersToString()) # running fit fit_suite.runFit() print("Fitting completed.") print("chi2:", fit_suite.getChi2()) for fitPar in fit_suite.fitParameters(): print(fitPar.name(), fitPar.value(), fitPar.error())
def run_fitting(): """ main function to run fitting """ simulation = get_simulation() sample = get_sample() simulation.setSample(sample) real_data = create_real_data() fit_suite = ba.FitSuite() #fit_suite.setMinimizer("GSLLMA") fit_suite.addSimulationAndRealData(simulation, real_data) fit_suite.initPrint(10) draw_observer = ba.DefaultFitObserver(draw_every_nth=10) fit_suite.attachObserver(draw_observer) fit_suite.addFitParameter("*/FullSphere/Radius", 8. * nm, ba.AttLimits.limited(4., 12.)) fit_suite.addFitParameter("*HexagonalLattice/LatticeLength", 8. * nm, ba.AttLimits.limited(4., 12.)) print(fit_suite.treeToString()) print(fit_suite.parametersToString()) # running fit fit_suite.runFit() print("Fitting completed.") print("chi2:", fit_suite.getChi2()) for fitPar in fit_suite.fitParameters(): print(fitPar.name(), fitPar.value(), fitPar.error())
def run_fitting(): simulation = create_simulation() sample_builder = MySampleBuilder() simulation.setSampleBuilder(sample_builder) real_data = load_exp_data() fit_suite = ba.FitSuite() draw_observer = ba.DefaultFitObserver(draw_every_nth=10) fit_suite.attachObserver(draw_observer) fit_suite.initPrint(10) fit_suite.addSimulationAndRealData(simulation, real_data) print("1.8") # setting fitting parameters with starting values fit_suite.addFitParameter("*radius", 5.0 * ba.nm, ba.AttLimits.limited(4.0, 6.0), 0.1 * ba.nm) fit_suite.addFitParameter("*sigma", 0.55, ba.AttLimits.limited(0.2, 0.8), 0.01 * ba.nm) fit_suite.addFitParameter("*distance", 27. * ba.nm, ba.AttLimits.limited(20, 70), 0.1 * ba.nm) # set fit strategies # strategy1 = ba.AdjustMinimizerStrategy("Genetic") # fit_suite.addFitStrategy(strategy1) # Second fit strategy will use another algorithm. # It will use best parameters found from previous minimization round. # strategy2 = ba.AdjustMinimizerStrategy("Minuit2", "Migrad") # fit_suite.addFitStrategy(strategy2) # running fit fit_suite.runFit() plt.show()
def run_fitting(): """ main function to run fitting """ simulation = get_simulation() sample = get_sample() simulation.setSample(sample) real_data = create_real_data() fit_suite = ba.FitSuite() fit_suite.addSimulationAndRealData(simulation, real_data) fit_suite.initPrint(10) draw_observer = ba.DefaultFitObserver(draw_every_nth=10) fit_suite.attachObserver(draw_observer) # setting fitting parameters with starting values # Here we select starting values being quite far from true values # to puzzle our minimizer's as much as possible fit_suite.addFitParameter("*Height", 1.*nm).setLimited(0.01, 30.)\ .setStep(0.05*nm) fit_suite.addFitParameter("*Radius", 20.*nm).setLimited(0.01, 30.)\ .setStep(0.05*nm) # Now we create first fig strategy which will run first minimization round # using the Genetic minimizer. # The Genetic minimizer is able to explore large parameter space # without being trapped by some local minima. strategy1 = ba.AdjustMinimizerStrategy("Genetic", "", "MaxIterations=2;RandomSeed=1") fit_suite.addFitStrategy(strategy1) # Second fit strategy will use another minimizer. # It starts from best parameters found in previous minimization # and then continues until fit converges. strategy2 = ba.AdjustMinimizerStrategy("Minuit2", "Migrad") fit_suite.addFitStrategy(strategy2) # running fit fit_suite.runFit() print("Fitting completed.") print("chi2:", fit_suite.getChi2()) for fitPar in fit_suite.fitParameters(): print(fitPar.name(), fitPar.value(), fitPar.error())
def run_fitting(): """ main function to run fitting """ ax_values, real_data = create_real_data() axis = make_axis(ax_values) simulation = get_simulation(axis) sample = get_sample() simulation.setSample(sample) print(simulation.treeToString()) print(simulation.parametersToString()) fit_suite = ba.FitSuite() fit_suite.addSimulationAndRealData(simulation, real_data) fit_suite.initPrint(10) draw_observer = ba.DefaultFitObserver(draw_every_nth=10, SimulationType='Specular') fit_suite.attachObserver(draw_observer) fitPar = ba.FitParameter(5. * ba.nm, ba.AttLimits.limited(1. * ba.nm, 7. * ba.nm)) fitPar.setName("thickness") for odd in [1, 3, 5, 7, 9]: # adding patterns for all odd layers' thicknesses fitPar.addPattern("*" + str(odd) + "/Thickness*") fit_suite.addFitParameter(fitPar) strategy1 = ba.AdjustMinimizerStrategy("Minuit2", "Migrad", "Strategy=2;Tolerance=1e-5") fit_suite.addFitStrategy(strategy1) # prints defined fit parameters and their relation to instrument parameters print(fit_suite.setupToString()) # running fit print("Starting the fitting") fit_suite.runFit() print("Fitting completed.") print("chi2:", fit_suite.getChi2()) for fitPar in fit_suite.fitParameters(): print(fitPar.name(), fitPar.value(), fitPar.error())
def run_fitting(): """ main function to run fitting """ simulation = get_simulation() sample = get_sample() simulation.setSample(sample) print(simulation.treeToString()) print(simulation.parametersToString()) real_data = create_real_data() fit_suite = ba.FitSuite() fit_suite.addSimulationAndRealData(simulation, real_data) fit_suite.initPrint(10) draw_observer = ba.DefaultFitObserver(draw_every_nth=10) fit_suite.attachObserver(draw_observer) # this fit parameter fits 4 sample parameter with one value fit_suite.addFitParameter("*Lattice/LatticeLength*", 9.*nm).\ setLimited(6., 10.).addPattern("*Cylinder/Radius").\ addPattern("*Cylinder/Height").setName("custom_length") # alternatively, following syntax is possible # fitPar = ba.FitParameter(10.*nm, ba.AttLimits.limited(4., 12.)) # fitPar.setName("custom_length") # fitPar.addPattern("*Lattice/LatticeLength*") # fitPar.addPattern("*Cylinder/Radius").addPattern("*Cylinder/Height") # fit_suite.addFitParameter(fitPar) # prints defined fit parameters and their relation to instrument parameters print(fit_suite.setupToString()) # running fit print("Starting the fitting") fit_suite.runFit() print("Fitting completed.") print("chi2:", fit_suite.getChi2()) for fitPar in fit_suite.fitParameters(): print(fitPar.name(), fitPar.value(), fitPar.error())
def run_fitting(): """ main function to run fitting """ simulation = get_simulation() sample = get_sample() simulation.setSample(sample) real_data = create_real_data() fit_suite = ba.FitSuite() fit_suite.addSimulationAndRealData(simulation, real_data) chiModule = ba.ChiSquaredModule() chiModule.setIntensityNormalizer(ba.IntensityScaleAndShiftNormalizer()) fit_suite.setChiSquaredModule(chiModule) fit_suite.initPrint(10) draw_observer = ba.DefaultFitObserver(draw_every_nth=10) fit_suite.attachObserver(draw_observer) # print all defined parameters for sample and simulation print(fit_suite.parametersToString()) # setting fitting parameters with starting values fit_suite.addFitParameter("*/Cylinder/Radius", 6. * nm, ba.AttLimits.limited(4., 8.)) fit_suite.addFitParameter("*/Cylinder/Height", 9. * nm, ba.AttLimits.limited(8., 12.)) fit_suite.addFitParameter("*/Normalizer/scale", 1.5, ba.AttLimits.limited(1.0, 3.0)) fit_suite.addFitParameter("*/Normalizer/shift", 50., ba.AttLimits.limited(1, 500.)) # running fit fit_suite.runFit() print("Fitting completed.") print("chi2:", fit_suite.getChi2()) for fitPar in fit_suite.fitParameters(): print(fitPar.name(), fitPar.value(), fitPar.error())
def run_fitting(): """ Setup simulation and fit """ sample = get_sample() simulation = get_simulation() simulation.setSample(sample) real_data = create_real_data() fit_suite = ba.FitSuite() fit_suite.addSimulationAndRealData(simulation, real_data) # fit_suite.setMinimizer("Minuit2", "Migrad") # ba.Default # fit_suite.setMinimizer("Minuit2", "Fumili") # fit_suite.setMinimizer("GSLLMA") fit_suite.initPrint(10) # setting fitting parameters with starting values fit_suite.addFitParameter("*Cylinder/Height", 4. * nm).setLowerLimited(0.01) fit_suite.addFitParameter("*Cylinder/Radius", 6. * nm).setLowerLimited(0.01) fit_suite.addFitParameter("*Prism3/Height", 4. * nm).setLowerLimited(0.01) fit_suite.addFitParameter("*Prism3/BaseEdge", 12. * nm).setLowerLimited(0.01) draw_observer = ba.DefaultFitObserver(draw_every_nth=10) fit_suite.attachObserver(draw_observer) fit_suite.runFit() print("Fitting completed.") print("chi2:", fit_suite.getChi2()) for fitPar in fit_suite.fitParameters(): print(fitPar.name(), fitPar.value(), fitPar.error()) return fit_suite