def test_sublayers(self): layers = layer_data_for_testing() sublayers, corresponding = generateSublayers(layers) _, Thick, Re_NSLD, Im_NSLD, MSLD_rho, MSLD_phi, MSLD_theta = np.loadtxt( os.path.join(os.path.dirname(__file__), 'data/profile_sublayers.dat'), unpack=True) Thick = Thick[1:-2] Re_NSLD = Re_NSLD[1:-2] Im_NSLD = Im_NSLD[1:-2] MSLD_rho = MSLD_rho[1:-2] MSLD_phi = MSLD_phi[1:-2] MSLD_theta = MSLD_theta[1:-2] sl_thick = np.array([s.thickness.value for s in sublayers[1:-1]]) sl_nsld = np.array([s.nsld_real.value for s in sublayers[1:-1]]) sl_nsldi = np.array([s.nsld_imaginary.value for s in sublayers[1:-1]]) sl_msldr = np.array([s.msld.rho.value for s in sublayers[1:-1]]) np.testing.assert_allclose(Thick, sl_thick, atol=1e-3) np.testing.assert_allclose(Re_NSLD, sl_nsld, atol=1e-10) np.testing.assert_allclose(Im_NSLD, sl_nsldi, atol=1e-13) np.testing.assert_allclose(MSLD_rho, sl_msldr, atol=1e-11) #Now test that msld.theta and msld.phi are constant in a layer sl_msldt = np.array([s.msld.theta.value for s in sublayers[1:-1]]) sl_msldp = np.array([s.msld.phi.value for s in sublayers[1:-1]]) np.testing.assert_allclose(MSLD_theta, sl_msldt, atol=1e-3) np.testing.assert_allclose(MSLD_phi, sl_msldp, atol=1e-3) corresponding = corresponding[1:-1] for t, p, l in zip(sl_msldt, sl_msldp, corresponding): self.assertAlmostEqual(t, layers[int(l)].msld.theta.value) self.assertAlmostEqual(p, layers[int(l)].msld.phi.value)
def calculate_residuals(self, parameters): ma = ModelAdapter(self.sample_model) ma.update_model_from_params(parameters) layers = [self.sample_model.incoming_media ] + self.sample_model.layers + [self.sample_model.substrate] sublayers = generateSublayers(layers)[0] chi_array = [] for ds in self.data_model.datasets: if ds.R is not None and len(ds.R) > 1: q = ds.Q / 2. r = reflection(q, sublayers) pol_eff = np.ones(len(q), dtype=np.complex128) an_eff = np.ones(len(q), dtype=np.complex128) rr = np.real( spin_av(r, ds.pol_Polarizer, ds.pol_Analyzer, pol_eff, an_eff)) sigma = ds.sigmaQ rrr = resolut( rr, q, sigma, 4 ) * self.data_model.theory_factor + self.data_model.background chi_array.append( (rrr / self.data_model.experiment_factor - ds.R) / ds.E) chi = np.array(chi_array).ravel() self.chiSquaredChanged.emit((chi**2).mean()) return chi
def test_plot(self): layers = layer_data_for_testing() sublayers, corresponding = generateSublayers(layers) fig, ax = plt.subplots(2, 1, sharex=False) plot_sublayers(ax[0], layers, parameter='NSLD_REAL') plot_sublayers(ax[1], layers, parameter='ROUGHNESS') #nsld_real plot pc = ax[0].get_children()[0] np.testing.assert_equal( pc.get_array(), corresponding) #colors are corresponding to the correct layer self.assertEqual(ax[0].get_xlabel(), 'Depth') self.assertEqual(ax[0].get_ylabel(), 'NSLD REAL') #sublayer nslds sl_height_plot = np.array( [path.vertices[2][1] for path in pc.get_paths()]) sl_height_input = np.array([sl.nsld_real.value for sl in sublayers]) np.testing.assert_allclose(sl_height_plot, sl_height_input, atol=1e-12) #roughness plot x, y = ax[1].get_children()[1].get_data() np.testing.assert_allclose(x, np.array([0, 90., 130., 205., 330]), atol=1e-9) np.testing.assert_allclose(y, np.array([0., 8., 5., 4., 5.]), atol=1e-9) self.assertEqual(ax[1].get_xlabel(), 'Depth') self.assertEqual(ax[1].get_ylabel(), 'ROUGHNESS') plt.close()
def generate_sub_layers(self): """ Wrapper to generate sub layers. TODO: There is an inconsistency between the reflectivity calculation and the sublayer generation. The sublayer generation generates layer.msld = MSLD(rho, theta, phi) but the reflectivity calculation expects layer.msld = [rho_x, rho_y, rho_z] """ _sublayers, _ = generateSublayers(self._model_layers) for l in _sublayers: l._thickness = l.thickness.value l._msld = [0, 0, 0] return _sublayers
def plot_sublayers(ax, layers, parameter='NSLD_REAL'): sublayers, corresponding = generateSublayers(layers) thick = [sl.thickness.value for sl in sublayers] depth = np.array(thick) try: thic_kmax = depth[np.isfinite(depth)].max() except ValueError: # all layers are infinite (maybe just incoming media and substrate) thic_kmax = 1 depth[np.isinf(depth)] = thic_kmax th1 = depth[corresponding.index(1)] depth = depth.cumsum() depth -= depth[corresponding.index(1)] - th1 depth = np.insert(depth, 0, depth[0] - thic_kmax) ax.clear() if parameter == 'NSLD_REAL': val = np.array([sl.nsld_real.value for sl in sublayers]) elif parameter == 'NSLD_IMAGINARY': val = np.array([sl.nsld_imaginary.value for sl in sublayers]) elif parameter == 'MSLD_RHO': val = np.array([sl.msld.rho.value for sl in sublayers]) elif parameter == 'MSLD_THETA': val = np.array([sl.msld.theta.value for sl in sublayers]) elif parameter == 'MSLD_PHI': val = np.array([sl.msld.phi.value for sl in sublayers]) elif parameter == 'ROUGHNESS': val = np.array([layer.roughness.value for layer in layers[1:]]) else: raise ValueError('The variable to be plotted could not be found') if parameter != 'ROUGHNESS': ax.plot(depth[1:], val, visible=False) ax.plot([-1], [0.], visible=False) patches = [] for i, v in enumerate(val): polygon = Polygon([[depth[i], 0.], [depth[i], v], [depth[i + 1], v], [depth[i + 1], 0]], True) patches.append(polygon) xmin, xmax = ax.get_xlim() patches[0] = Polygon( [[xmin, 0.], [xmin, val[0]], [depth[1], val[0]], [depth[1], 0]], True) patches[-1] = Polygon([[depth[-2], 0.], [depth[-2], val[-1]], [xmax, val[-1]], [xmax, 0]], True) p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4, picker=True) p.set_array(np.array(corresponding)) ax.add_collection(p) ax.ticklabel_format(axis='y', style='sci', scilimits=(-2, 2)) if parameter == 'ROUGHNESS': ax.plot([-1, depth[1], depth[-1]], [0, np.max(val), np.min(val)], visible=False) xmin, xmax = ax.get_xlim() lthick = [l.thickness.value for l in layers[1:]] lthick.insert(0, 0.) lthick = np.array(lthick[:-1]) depth = lthick.cumsum() ax.stem(depth, val, linefmt='--') ax.set_xlim(xmin, xmax) ax.axhline(y=0) ax.set_xlabel('Depth') ax.set_ylabel(parameter.replace('_', ' ')) return sublayers, corresponding