def test_spline_smoke(self): # smoke test to make Spline at least gives us something a = Spline(100, [2], [0.5], zgrad=False, microslab_max_thickness=1) s = self.left | a | self.right | self.solvent b = a.slabs(s) assert_equal(b[:, 2], 0) # microslabs are assessed in the middle of the slab assert_equal(b[0, 1], a(0.5 * b[0, 0], s)) # with the ends turned off the profile should be a straight line assert_equal(a(50, s), 2.0) # construct a structure a = Spline( 100, [2.0, 3.0, 4.0], [0.25] * 3, zgrad=False, microslab_max_thickness=1, ) # s.solvent = None s = self.left | a | self.right | self.solvent # calculate an SLD profile s.sld_profile() # ask for the parameters for p in flatten(s.parameters): assert_(isinstance(p, Parameter)) # s.solvent is not None s.solvent = self.solvent # calculate an SLD profile s.sld_profile()
def test_slab_addition(self): # The slabs method for the main Structure component constructs # the overall slabs by concatenating Component slabs. This checks that # the slab concatenation is correct. si = SLD(2.07) sio2 = SLD(3.47) polymer = SLD(1.5) d2o = SLD(6.36) d2o_layer = d2o(0, 3) polymer_layer = polymer(20, 3) a = Spline(400, [4, 5.9], [0.2, .4], zgrad=True) film = si | sio2(10, 3) | polymer_layer | a | d2o_layer film.sld_profile() structure = si(0, 0) for i in range(200): p = SLD(i)(i, i) structure |= p structure |= d2o(0, 3) slabs = structure.slabs() assert_equal(slabs[1:-1, 0], np.arange(200)) assert_equal(slabs[1:-1, 1], np.arange(200)) assert_equal(slabs[1:-1, 3], np.arange(200)) assert_equal(slabs[-1, 1], 6.36) assert_equal(slabs[0, 1], 2.07) assert_equal(len(slabs), 202)
def test_add_spline_save(qtbot, tmpdir): # test if we can add a spline to a model and save an experiment myapp, model = mysetup(qtbot) # get index of theoretical dataset --> structure --> slab1 data_object_node = model.data_object_node("theoretical") model_node = data_object_node.child(1) assert isinstance(model_node, ReflectModelNode) structs = model_node.structures for struct in structs: assert isinstance(struct, Structure) structure_node = model_node.child(4) assert isinstance(structure_node, StructureNode) # selection_model = myapp.ui.treeView.selectionModel() # slab_node = structure_node.child(1) # selection_model.select(slab_node.index, # QtCore.QItemSelectionModel.Select) # add a Spline after the slab component = Spline(50, [-1.0, -1.0], [0.33, 0.33], name="spline") structure_node.insert_component(1, component) save_and_reload_experiment(myapp, tmpdir)
def test_spline_solvation(self): a = Spline(100, [2], [0.5], zgrad=False, microslab_max_thickness=1) front = SLD(0.1) air = SLD(0.0) s = front | self.left | a | self.right | self.solvent # assign a solvent s.solvent = air self.left.vfsolv.value = 0.5 self.right.vfsolv.value = 0.5 assert_equal(s.slabs()[1, 1], 0.75) assert_equal(s.slabs()[-2, 1], 1.25) assert_almost_equal(a(0, s), 0.75) assert_almost_equal(a(100, s), 1.25) # remove solvent, should be solvated by backing medium s.solvent = None assert_equal(s.slabs()[1, 1], 5.75) assert_equal(s.slabs()[-2, 1], 6.25) assert_almost_equal(a(0, s), 5.75) assert_almost_equal(a(100, s), 6.25) # reverse structure, should be solvated by fronting medium s.reverse_structure = True assert_equal(s.slabs()[1, 1], 1.3) assert_equal(s.slabs()[-2, 1], 0.8) # note that a(0, s) end becomes the end when the structure is reversed assert_almost_equal(a(0, s), 0.8) assert_almost_equal(a(100, s), 1.3)
def test_spline_no_knots(self): # try and make Spline with no knots a = Spline(100, [], [], zgrad=False, microslab_max_thickness=1) s = self.left | a | self.right | self.solvent b = a.slabs(s) assert_equal(b[:, 2], 0) # microslabs are assessed in the middle of the slab assert_equal(b[0, 1], a(0.5 * b[0, 0], s)) # with the ends turned off the profile should be a straight line assert_equal(a(50, s), 2.0) q = np.linspace(0.01, 0.5, 1001) s.reflectivity(q)
def test_left_right_influence(self): # make sure that if the left and right components change, so does the # spline a = Spline(100, [2], [0.5], zgrad=False, microslab_max_thickness=1) s = self.left | a | self.right | self.solvent # change the SLD of the left component, spline should respond self.left.sld.real.value = 2.0 assert_almost_equal(a(0, s), 2) # check that the spline responds if it's a vfsolv that changes self.left.vfsolv.value = 0.5 assert_almost_equal( Structure.overall_sld(self.left.slabs(), self.solvent)[0, 1], 6.0) assert_almost_equal(a(0, s), 6.0) # check that the right side responds. self.right.sld.real.value = 5.0 assert_almost_equal(a(100, s), 5.0) # the spline should respond if the knot SLD's are changed a.vs[0].value = 3.0 assert_almost_equal(a(50, s), 3.0) # spline responds if the interval knot spacing is changed a.dz[0].value = 0.9 assert_almost_equal(a(90, s), 3.0)
def test_spline_repeat(self): # can't have two splines in a row. a = Spline(100, [2], [0.5], zgrad=False, microslab_max_thickness=1) s = self.left | a | a | self.right | self.solvent from pytest import raises with raises(ValueError): s.slabs()
def test_spine_interfaces(self): a = Spline(100, [2, 3], [0.3, 0.3], zgrad=False, microslab_max_thickness=1) s = self.left | a | self.right | self.solvent # this is a check to ensure that: # 1) _micro_slabs is being called by Structure.slabs() # 2) _micro_slabs works correctly when a Spline is being used. # Spline.interfaces should be `None`, which should expanded from # that default in _micro_slabs to `[Erf] * len(Structure.slabs())`. s[-1].interfaces = Linear() s._micro_slabs() assert_equal(len(s._micro_slabs()), len(s.slabs())) # should be able to set all the interfaces in Spline to Linear. a.interfaces = Linear() a.interfaces = [Linear()]
def test_repr(self): # make sure that if the left and right components change, so does the # spline a = Spline(100, [2, 3, 4], [0.1, 0.2, 0.3], zgrad=False, microslab_max_thickness=1) s = self.left | a | self.right | self.solvent # should be able to repr the whole lot q = repr(s) r = eval(q) assert_equal(r.slabs(), s.slabs())
def test_pickle(self): a = Spline(100, [2, 3], [0.3, 0.3], zgrad=False, microslab_max_thickness=1) s = self.left | a | self.right | self.solvent # cause the spline to be evaluated s.sld_profile() assert a._Spline__cached_interpolator["interp"] is not None pkl = pickle.dumps(s) r = pickle.loads(pkl) assert isinstance(r, Structure)
def component(self): # return a SplineComponent dz = [] vs = [] extent = self.extent.value() for i in range(self.knots.rowCount()): dz.append(float(self.knots.item(i, 0).text())) vs.append(float(self.knots.item(i, 1).text())) return Spline(extent, vs, dz, name='spline', microslab_max_thickness=1.0)
def test_add_spline_save(qtbot, tmpdir): # test if we can add a spline to a model and save an experiment myapp, model = mysetup(qtbot) # get index of theoretical dataset --> structure --> slab1 data_object_node = model.data_object_node('theoretical') model_node = data_object_node.child(1) structure_node = model_node.child(3) # selection_model = myapp.ui.treeView.selectionModel() # slab_node = structure_node.child(1) # selection_model.select(slab_node.index, # QtCore.QItemSelectionModel.Select) # add a Spline after the slab component = Spline(50, [-1., -1.], [0.33, 0.33], name='spline') structure_node.insert_component(1, component) save_and_reload_experiment(myapp, tmpdir)
def make_model(names, bs, thicks, roughs, fig_i, data, show=False, mcmc=False): extent = sum( thicks[:, 0]) # (float or Parameter) – Total extent of spline region vs = array( bs )[:, 0] #(Sequence of float/Parameter) – the real part of the SLD values of each of the knots. dz = cum_sum( array(thicks[:, 0]) ) #(Sequence of float/Parameter) – the lateral offset between successive knots. print(dz) name = "number of nots " + str(len(names)) #(str) – Name of component component = Spline(extent, vs, dz, name) front = SLD(0) front = front(0, 0) back = SLD(0) back = back(0, 0) structure = front | component | back model = ReflectModel(structure, bkg=3e-6, dq=5.0) objective = Objective(model, data, transform=Transform('logY')) fitter = CurveFitter(objective) fitter.fit('differential_evolution') return structure, fitter, objective, fig_i + 1