def _increase_layers(self, b): self.w_layers.disabled = False self.do_fit_button.disabled = False self.to_code_button.disabled = False self.save_model_button.disabled = False self.load_model_button.disabled = False how_many = self.w_layers.value - (len(self.model.structure) - 2) loc = self._location.value for i in range(how_many): slab = Slab(0, 0, 3) slab.thick.bounds = (0, 2 * slab.thick.value) slab.sld.real.bounds = (0, 2 * slab.sld.real.value) slab.sld.imag.bounds = (0, 2 * slab.sld.imag.value) slab.rough.bounds = (0, 2 * slab.rough.value) slab_view = SlabView(slab) self.model.structure.insert(loc, slab) self.structure_view.slab_views.insert(loc, slab_view) slab_view.observe(self._on_slab_params_modified) rename_params(self.model.structure) self._varying_layers = False # set the number of varying parameters self.num_varying = len(self.model.parameters.varying_parameters()) self.view_redraw = time.time()
def test_materialsld(self): p = MaterialSLD("SiO2", density=2.2, name="silica") sldc = complex(p) assert_allclose(sldc.real, 3.4752690258246504) assert_allclose(sldc.imag, 1.0508799522721932e-05) assert p.probe == "neutron" # is X-ray SLD correct? p.wavelength = 1.54 p.probe = "x-ray" sldc = complex(p) assert_allclose(sldc.real, 18.864796064009866) assert_allclose(sldc.imag, 0.2436013463223236) assert len(p.parameters) == 1 assert p.formula == "SiO2" # the density value should change the SLD p.probe = "neutron" p.density.value = 4.4 sldc = complex(p) assert_allclose(sldc.real, 3.4752690258246504 * 2) assert_allclose(sldc.imag, 1.0508799522721932e-05 * 2) # should be able to make a Slab from MaterialSLD slab = p(10, 3) assert isinstance(slab, Slab) slab = Slab(10, p, 3) assert isinstance(slab, Slab) # make a full structure and check that the reflectivity calc works air = SLD(0) sio2 = MaterialSLD("SiO2", density=2.2) si = MaterialSLD("Si", density=2.33) s = air | sio2(10, 3) | si(0, 3) s.reflectivity(np.linspace(0.005, 0.3, 100)) p = s.parameters assert len(list(flatten(p))) == 5 + 4 + 4
def __init__(self): # attributes for the graph # for the graph self.qmin = 0.005 self.qmax = 0.5 self.qpnt = 1000 self.fig = None self.ax_data = None self.ax_residual = None self.ax_sld = None # gridspecs specify how the plots are laid out. Gridspec1 is when the # residuals plot is displayed. Gridspec2 is when it's not visible self._gridspec1 = gridspec.GridSpec(2, 2, height_ratios=[5, 1], width_ratios=[1, 1], hspace=0.01) self._gridspec2 = gridspec.GridSpec(1, 2) self.theoretical_plot = None self.theoretical_plot_sld = None # attributes for a user dataset self.dataset = None self.objective = None self._curvefitter = None self.data_plot = None self.residuals_plot = None self.data_plot_sld = None self.dataset_name = widgets.Text(description="dataset:") self.dataset_name.disabled = True self.chisqr = widgets.FloatText(description="chi-squared:") self.chisqr.disabled = True # fronting slab0 = Slab(0, 0, 0) slab1 = Slab(25, 3.47, 3) slab2 = Slab(0, 2.07, 3) structure = slab0 | slab1 | slab2 rename_params(structure) self.model = ReflectModel(structure) structure = slab0 | slab1 | slab2 self.model = ReflectModel(structure) # give some default parameter limits self.model.scale.bounds = (0.1, 2) self.model.bkg.bounds = (1e-8, 2e-5) self.model.dq.bounds = (0, 20) for slab in self.model.structure: slab.thick.bounds = (0, 2 * slab.thick.value) slab.sld.real.bounds = (0, 2 * slab.sld.real.value) slab.sld.imag.bounds = (0, 2 * slab.sld.imag.value) slab.rough.bounds = (0, 2 * slab.rough.value) # the main GUI widget self.display_box = widgets.VBox() self.tab = widgets.Tab() self.tab.set_title(0, "Model") self.tab.set_title(1, "Limits") self.tab.set_title(2, "Options") self.tab.observe(self._on_tab_changed, names="selected_index") # an output area for messages. self.output = widgets.Output() # options tab self.plot_type = widgets.Dropdown( options=["lin", "logY", "YX4", "YX2"], value="lin", description="Plot Type:", disabled=False, ) self.plot_type.observe(self._on_plot_type_changed, names="value") self.use_weights = widgets.RadioButtons( options=["Yes", "No"], value="Yes", description="use dataset weights?", style={"description_width": "initial"}, ) self.use_weights.observe(self._on_use_weights_changed, names="value") self.transform = Transform("lin") self.display_residuals = widgets.Checkbox( value=False, description="Display residuals") self.display_residuals.observe(self._on_display_residuals_changed, names="value") self.model_view = None self.set_model(self.model)
def test_materialsld(self): p = MaterialSLD("SiO2", density=2.2, name="silica") sldc = complex(p) assert_allclose(sldc.real, 3.4752690258246504) assert_allclose(sldc.imag, 1.0508799522721932e-05) assert p.probe == "neutron" # is X-ray SLD correct? p.wavelength = 1.54 p.probe = "x-ray" sldc = complex(p) assert_allclose(sldc.real, 18.864796064009866) assert_allclose(sldc.imag, 0.2436013463223236) assert len(p.parameters) == 1 assert p.formula == "SiO2" # the density value should change the SLD p.probe = "neutron" p.density.value = 4.4 sldc = complex(p) assert_allclose(sldc.real, 3.4752690258246504 * 2) assert_allclose(sldc.imag, 1.0508799522721932e-05 * 2) # should be able to make a Slab from MaterialSLD slab = p(10, 3) assert isinstance(slab, Slab) slab = Slab(10, p, 3) assert isinstance(slab, Slab) # make a full structure and check that the reflectivity calc works air = SLD(0) sio2 = MaterialSLD("SiO2", density=2.2) si = MaterialSLD("Si", density=2.33) s = air | sio2(10, 3) | si(0, 3) s.reflectivity(np.linspace(0.005, 0.3, 100)) p = s.parameters assert len(list(flatten(p))) == 5 + 4 + 4 # check that energy dispersive calculation works au = MaterialSLD("Au", density=19.33, name="Gold", probe="x-ray", wavelength=1.54) si = SLD(20.1) s = air | au(100, 3) | si(0, 3) slabs = s.slabs(wavelength=0.5) assert_allclose(s.wavelength, 0.5) au_sld = complex(slabs[1, 1], slabs[1, 2]) au_check = MaterialSLD("Au", density=19.33, name="Gold", probe="x-ray", wavelength=0.5) assert_allclose(au_sld, complex(au_check)) # check that the Scatterer.complex method works as intended assert_allclose(au.complex(0.5), complex(au_check))