def test_calc_ref_basic(self): layer1 = dataformat.SLDPro(1., 0., 0.) layer2 = dataformat.SLDPro(1., 5., 0.) sld = [[layer1, layer2]] data1 = dataformat.QData(0.05, 0., 0., 0.05 * 0.05) data2 = dataformat.QData(0.25, 0., 0., 0.05 * 0.25) data3 = dataformat.QData(0.50, 0., 0., 0.05 * 0.50) data = [data1, data2, data3] a = reflect.Reflect(sld, data) a.calc_ref() assert_equal(len(a.reflect), 1) assert_equal(len(a.reflect[0]), 3)
def calc_ref(self): """Calculate reflectometry. The calculation of the reflectometry profiles based on the sld profiles calculated from each of the timesteps under study. """ if len(self.exp_data) > 0: self.reflect = [] prog = 0 k = 0 print("Calculating reflectometry\n[ 0 % ]") for i in range(0, len(self.sld_profile)): k += 1 prog_new = np.floor(k / (len(self.sld_profile)) * 100) if prog_new > prog + 9: prog = prog_new print("[{} {} % ]".format('#' * int(prog / 10), int(prog / 10) * 10)) refl = convolution(self.exp_data, self.sld_profile[i]) a = [] for j in range(0, len(self.exp_data)): a.append( dataformat.QData(self.exp_data[j].q, refl[j], 0, self.exp_data[j].dq)) self.reflect.append(a) else: raise ValueError( 'No q vectors have been defined -- either read a .dat file or get q vectors.' )
def read_dat(self): """Parses .dat. Parses the .dat file, supporting 2, 3, and 4 column files consisting of q, i, di, and dq with comments in lines where the first character is a '#'. If there is no .dat file the get_qs() function should be used to generate q vectors to allow for the calculation of the reflectometry profile. """ self.expdata = [] if self.datfile: lines = line_count(self.datfile) print("Reading DAT file") percentage = 0 print_update(percentage) file = open(self.datfile, 'r') for i, line in enumerate(file): percentage_new = np.floor(i / lines * 100) percentage = check_update(percentage, percentage_new) if line[0] != '#': line_list = line.split() if len(line_list) == 2: self.expdata.append( dataformat.QData( float(line_list[0]), float(line_list[1]), float(line_list[1]) * (self.ierror / 100), float(line_list[0]) * (self.resolution / 100))) if len(line_list) == 3: self.expdata.append( dataformat.QData( float(line_list[0]), float(line_list[1]), float(line_list[2]), float(line_list[0]) * (self.resolution / 100))) if len(line_list) == 4: self.expdata.append( dataformat.QData(float(line_list[0]), float(line_list[1]), float(line_list[2]), float(line_list[3]))) print_update(100) file.close() else: print( 'No DAT file has been given, therefore no comparison will be conducted, please use the get_qs ' 'function. Alternatively the DAT file can be added using the setFile function.' ) return
def return_fitted(self): """Return fitted. Return the fitted calculated reflectometry data for use. """ self.sim_data_fitted = [] for i in range(0, len(self.sim_data)): a = self.sim_data[i].i * self.scale + self.background b = self.sim_data[i].di * self.scale self.sim_data_fitted.append( dataformat.QData(self.sim_data[i].q, a, b, self.sim_data[i].dq))
def test_reflect(self): layer1 = dataformat.SLDPro(1., 0., 0.) layer2 = dataformat.SLDPro(1., 5., 0.) sld = [[layer1, layer2]] data1 = dataformat.QData(0.05, 0., 0., 0.05 * 0.05) data2 = dataformat.QData(0.25, 0., 0., 0.05 * 0.25) data3 = dataformat.QData(0.50, 0., 0., 0.05 * 0.50) data = [data1, data2, data3] a = reflect.Reflect(sld, data) assert_almost_equal(a.sld_profile[0][0].thick, 1.) assert_almost_equal(a.sld_profile[0][0].real, 0.) assert_almost_equal(a.sld_profile[0][0].imag, 0.) assert_almost_equal(a.sld_profile[0][1].thick, 1.) assert_almost_equal(a.sld_profile[0][1].real, 5.) assert_almost_equal(a.sld_profile[0][1].imag, 0.) assert_almost_equal(a.exp_data[0].q, 0.05) assert_almost_equal(a.exp_data[0].dq, 0.05 * 0.05) assert_almost_equal(a.exp_data[1].q, 0.25) assert_almost_equal(a.exp_data[1].dq, 0.25 * 0.05) assert_almost_equal(a.exp_data[2].q, 0.50) assert_almost_equal(a.exp_data[2].dq, 0.50 * 0.05)
def get_qs(self, start=0.005, end=0.5, number=50): """Make custom q-vectors. If no datfile exists this function should be used to generate a linear-spaced range of q-vector for the calculation of the reflectometry over. Parameters ---------- start: float, optional The first q-vector for which the reflectometry should be calculated. end: float, optional The last q-vector for which the reflectometry should be calculated. number: int, optional The number of q-vectors. """ q_values = np.linspace(start, end, number) for i in range(0, len(q_values)): self.expdata.append( dataformat.QData(q_values[i], None, None, q_values[i] * (self.resolution / 100))) return
def test_return_fitted(self): data1 = dataformat.QData(0.05, 3., 0.3, 0.05 * 0.05) data2 = dataformat.QData(0.25, 2., 0.2, 0.05 * 0.25) data3 = dataformat.QData(0.50, 1., 0.1, 0.05 * 0.50) data = [data1, data2, data3] sdata1 = dataformat.QData(0.05, 1., 0.1, 0.05 * 0.05) sdata2 = dataformat.QData(0.25, 2., 0.2, 0.05 * 0.25) sdata3 = dataformat.QData(0.50, 3., 0.3, 0.05 * 0.50) sdata = [sdata1, sdata2, sdata3] a = compare.Compare(data, sdata, 2., 0.) a.return_fitted() assert_almost_equal(a.sim_data_fitted[0].i, 2.) assert_almost_equal(a.sim_data_fitted[1].i, 4.) assert_almost_equal(a.sim_data_fitted[2].i, 6.) assert_almost_equal(a.sim_data_fitted[0].di, 0.2) assert_almost_equal(a.sim_data_fitted[1].di, 0.4) assert_almost_equal(a.sim_data_fitted[2].di, 0.6)
def average_ref(self): """Average reflectometry profiles. The averaging of the reflectometry profiles as calculated by the calc_ref() function. """ if len(self.exp_data) > 0: self.averagereflect = [] for i in range(0, len(self.reflect[0])): self.averagereflect.append( dataformat.QData(self.exp_data[i].q, 0, 0, self.exp_data[i].dq)) for i in range(0, len(self.reflect[0])): for j in range(0, len(self.reflect)): self.averagereflect[i].i += self.reflect[j][i].i self.averagereflect[i].i /= len(self.reflect) for j in range(0, len(self.reflect)): self.averagereflect[i].di += np.square( self.reflect[j][i].i - self.averagereflect[i].i) self.averagereflect[i].di = np.sqrt( 1. / (len(self.reflect) - 1) * self.averagereflect[i].di) else: raise ValueError( 'No q vectors have been defined -- either read a .dat file or get q vectors.' )
def test_compare(self): data1 = dataformat.QData(0.05, 3., 0.3, 0.05 * 0.05) data2 = dataformat.QData(0.25, 2., 0.2, 0.05 * 0.25) data3 = dataformat.QData(0.50, 1., 0.1, 0.05 * 0.50) data = [data1, data2, data3] sdata1 = dataformat.QData(0.05, 1., 0.1, 0.05 * 0.05) sdata2 = dataformat.QData(0.25, 2., 0.2, 0.05 * 0.25) sdata3 = dataformat.QData(0.50, 3., 0.3, 0.05 * 0.50) sdata = [sdata1, sdata2, sdata3] a = compare.Compare(data, sdata, 1., 0.) assert_almost_equal(a.exp_data[0].q, 0.05) assert_almost_equal(a.exp_data[0].i, 3.) assert_almost_equal(a.exp_data[0].di, 0.3) assert_almost_equal(a.exp_data[0].dq, 0.05 * 0.05) assert_almost_equal(a.exp_data[1].q, 0.25) assert_almost_equal(a.exp_data[1].i, 2.) assert_almost_equal(a.exp_data[1].di, 0.2) assert_almost_equal(a.exp_data[1].dq, 0.05 * 0.25) assert_almost_equal(a.exp_data[2].q, 0.50) assert_almost_equal(a.exp_data[2].i, 1.) assert_almost_equal(a.exp_data[2].di, 0.1) assert_almost_equal(a.exp_data[2].dq, 0.05 * 0.50) assert_almost_equal(a.sim_data[0].q, 0.05) assert_almost_equal(a.sim_data[0].i, 1.) assert_almost_equal(a.sim_data[0].di, 0.1) assert_almost_equal(a.sim_data[0].dq, 0.05 * 0.05) assert_almost_equal(a.sim_data[1].q, 0.25) assert_almost_equal(a.sim_data[1].i, 2.) assert_almost_equal(a.sim_data[1].di, 0.2) assert_almost_equal(a.sim_data[1].dq, 0.05 * 0.25) assert_almost_equal(a.sim_data[2].q, 0.50) assert_almost_equal(a.sim_data[2].i, 3.) assert_almost_equal(a.sim_data[2].di, 0.3) assert_almost_equal(a.sim_data[2].dq, 0.05 * 0.50) assert_almost_equal(a.scale, 1.) assert_almost_equal(a.background, 0.)
def test_average_reflect(self): data11 = dataformat.QData(0.05, 0.1, 0., 0.05 * 0.05) data12 = dataformat.QData(0.25, 0.05, 0., 0.05 * 0.25) data13 = dataformat.QData(0.50, 0.01, 0., 0.05 * 0.50) data1 = [data11, data12, data13] data21 = dataformat.QData(0.05, 0.12, 0., 0.05 * 0.05) data22 = dataformat.QData(0.25, 0.03, 0., 0.05 * 0.25) data23 = dataformat.QData(0.50, 0.015, 0., 0.05 * 0.50) data2 = [data21, data22, data23] data31 = dataformat.QData(0.05, 0.14, 0., 0.05 * 0.05) data32 = dataformat.QData(0.25, 0.07, 0., 0.05 * 0.25) data33 = dataformat.QData(0.50, 0.005, 0., 0.05 * 0.50) data3 = [data31, data32, data33] layer1 = dataformat.SLDPro(1., 0., 0.) layer2 = dataformat.SLDPro(1., 5., 0.) sld = [[layer1, layer2]] ddata1 = dataformat.QData(0.05, 0., 0., 0.05 * 0.05) ddata2 = dataformat.QData(0.25, 0., 0., 0.05 * 0.25) ddata3 = dataformat.QData(0.50, 0., 0., 0.05 * 0.50) ddata = [ddata1, ddata2, ddata3] a = reflect.Reflect(sld, ddata) a.reflect = [data1, data2, data3] a.average_ref() assert_almost_equal(a.averagereflect[0].i, 0.12) assert_almost_equal(a.averagereflect[1].i, 0.05) assert_almost_equal(a.averagereflect[2].i, 0.01)
def test_qdata(self): a = dataformat.QData(1., 2., 3., 4.) assert_equal(a.q, 1.) assert_equal(a.i, 2.) assert_equal(a.di, 3.) assert_equal(a.dq, 4.)
def test_fit_noi(self): data11 = dataformat.QData(0.05, 0.1, 0., 0.05 * 0.05) data12 = dataformat.QData(0.25, 0.05, 0., 0.05 * 0.25) data13 = dataformat.QData(0.50, 0.01, 0., 0.05 * 0.50) data1 = [data11, data12, data13] data21 = dataformat.QData(0.05, 0.12, 0., 0.05 * 0.05) data22 = dataformat.QData(0.25, 0.03, 0., 0.05 * 0.25) data23 = dataformat.QData(0.50, 0.015, 0., 0.05 * 0.50) data2 = [data21, data22, data23] data31 = dataformat.QData(0.05, 0.14, 0., 0.05 * 0.05) data32 = dataformat.QData(0.25, 0.07, 0., 0.05 * 0.25) data33 = dataformat.QData(0.50, 0.005, 0., 0.05 * 0.50) data3 = [data31, data32, data33] layer1 = dataformat.SLDPro(1., 0., 0.) layer2 = dataformat.SLDPro(1., 5., 0.) sld = [[layer1, layer2]] ddata1 = dataformat.QData(0.05, 0., 0., 0.05 * 0.05) ddata2 = dataformat.QData(0.25, 0., 0., 0.05 * 0.25) ddata3 = dataformat.QData(0.50, 0., 0., 0.05 * 0.50) ddata = [ddata1, ddata2, ddata3] a = reflect.Reflect(sld, ddata) a.reflect = [data1, data2, data3] a.average_ref() ddata1 = dataformat.QData(0.05, None, 0., 0.05 * 0.05) ddata2 = dataformat.QData(0.25, 0., 0., 0.05 * 0.25) ddata3 = dataformat.QData(0.50, 0., 0., 0.05 * 0.50) ddata = [ddata1, ddata2, ddata3] k = compare.Compare(ddata, a.averagereflect, 1, 0) with self.assertRaises(ValueError) as context: k.fit() self.assertTrue( 'No experimental data has been set for comparison, please read in a a .dat file.' in str(context.exception))
def test_fit_noq(self): data11 = dataformat.QData(0.05, 0.1, 0., 0.05 * 0.05) data12 = dataformat.QData(0.25, 0.05, 0., 0.05 * 0.25) data13 = dataformat.QData(0.50, 0.01, 0., 0.05 * 0.50) data1 = [data11, data12, data13] data21 = dataformat.QData(0.05, 0.12, 0., 0.05 * 0.05) data22 = dataformat.QData(0.25, 0.03, 0., 0.05 * 0.25) data23 = dataformat.QData(0.50, 0.015, 0., 0.05 * 0.50) data2 = [data21, data22, data23] data31 = dataformat.QData(0.05, 0.14, 0., 0.05 * 0.05) data32 = dataformat.QData(0.25, 0.07, 0., 0.05 * 0.25) data33 = dataformat.QData(0.50, 0.005, 0., 0.05 * 0.50) data3 = [data31, data32, data33] layer1 = dataformat.SLDPro(1., 0., 0.) layer2 = dataformat.SLDPro(1., 5., 0.) sld = [[layer1, layer2]] ddata1 = dataformat.QData(0.05, 0., 0., 0.05 * 0.05) ddata2 = dataformat.QData(0.25, 0., 0., 0.05 * 0.25) ddata3 = dataformat.QData(0.50, 0., 0., 0.05 * 0.50) ddata = [ddata1, ddata2, ddata3] a = reflect.Reflect(sld, ddata) a.reflect = [data1, data2, data3] a.average_ref() ddata = [] k = compare.Compare(ddata, a.averagereflect, 1, 0) with self.assertRaises(ValueError) as context: k.fit() self.assertTrue( 'No q vectors have been defined -- either read a .dat file or get q vectors.' in str(context.exception))