def get_profile_data(exp_x, result, excel=False): # This func will take the result of experimental fit, clear the background, print an excel file with clean data and return clean intensity center = [] amplitude = [] fraction = [] fwhm = [] for name, value in zip(list(result.params.valuesdict().keys()), list(result.params.valuesdict().values())): if 'center' in name: center.append(value) if 'amplitude' in name: amplitude.append(value) if 'fraction' in name: fraction.append(value) if 'fwhm' in name: fwhm.append(value) clean_intensity = [] for two_theta in exp_x: intensity = 0 for c, cent in enumerate(center): intensity += pseudo_voigt(two_theta, cent, fwhm[c], fraction[c], amplitude[c]) clean_intensity.append(intensity) spec = Spectrum(exp_x, clean_intensity) spec.normalize(mode='max', value=100) if excel == True: dict_intense = {'two_theta': spec.x, 'intensity': spec.y} dict_intense = pd.DataFrame(dict_intense) dict_intense.to_excel('data_400_exp_without_bck.xlsx') return fwhm, center, amplitude, fraction, spec.y
class SpectrumTest(PymatgenTest): def setUp(self): self.spec1 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100)) self.spec2 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100)) self.multi_spec1 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100, 2)) self.multi_spec2 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100, 2)) def test_normalize(self): self.spec1.normalize(mode="max") self.assertAlmostEqual(np.max(self.spec1.y), 1) self.spec1.normalize(mode="sum") self.assertAlmostEqual(np.sum(self.spec1.y), 1) self.multi_spec1.normalize(mode="sum") self.assertAlmostEqual(np.sum(self.multi_spec1.y[:, 0]), 1) self.assertAlmostEqual(np.sum(self.multi_spec1.y[:, 1]), 1) # XRD style mode. self.spec1.normalize(mode="max", value=100) self.assertAlmostEqual(np.max(self.spec1.y), 100) def test_operators(self): scaled_spect = 3 * self.spec1 + self.spec2 self.assertTrue( np.allclose(scaled_spect.y, 3 * self.spec1.y + self.spec2.y)) self.assertAlmostEqual( self.spec1.get_interpolated_value(0.05), (self.spec1.y[0] + self.spec1.y[1]) / 2, ) scaled_spect = self.spec1 - self.spec2 self.assertTrue( np.allclose(scaled_spect.y, self.spec1.y - self.spec2.y)) scaled_spect = self.spec1 / 3 self.assertTrue(np.allclose(scaled_spect.y, self.spec1.y / 3)) scaled_spect = 3 * self.multi_spec1 + self.multi_spec2 self.assertTrue( np.allclose(scaled_spect.y, 3 * self.multi_spec1.y + self.multi_spec2.y)) self.assertArrayAlmostEqual( self.multi_spec1.get_interpolated_value(0.05), (self.multi_spec1.y[0, :] + self.multi_spec1.y[1, :]) / 2, ) def test_smear(self): y = np.array(self.spec1.y) self.spec1.smear(0.2) self.assertFalse(np.allclose(y, self.spec1.y)) self.assertAlmostEqual(sum(y), sum(self.spec1.y)) y = np.array(self.multi_spec1.y) self.multi_spec1.smear(0.2) self.assertFalse(np.allclose(y, self.multi_spec1.y)) self.assertArrayAlmostEqual(np.sum(y, axis=0), np.sum(self.multi_spec1.y, axis=0)) def test_str(self): # Just make sure that these methods work. self.assertIsNotNone(str(self.spec1)) self.assertIsNotNone(str(self.multi_spec1)) def test_copy(self): spec1copy = self.spec1.copy() spec1copy.y[0] = spec1copy.y[0] + 1 self.assertNotEqual(spec1copy.y[0], self.spec1.y[0]) self.assertEqual(spec1copy.y[1], self.spec1.y[1])
uvw = [0.00556691, -0.00089748, 0.00282308] #D5000 if 'GADDS' in name_of_outfileinput: uvw = [0.04586492, 0.0146818, 0.01856546] #GADDS #-*-*-*-*-*-*-*-*-*-*-*-*-INPUT-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* #Read from out file two_theta, intensity = functions.read_out_file(name_of_outfileinput) index = [c for c, i in enumerate(two_theta) if i > 34 and i < 100] # this bump is coming from sample holder. intensity = [i for c, i in enumerate(intensity) if c in index] two_theta = [i for c, i in enumerate(two_theta) if c in index] #Normalize out file. Max intensity will be 100 spec = Spectrum(two_theta, intensity) spec.normalize(mode='max', value=100) #This will fit on the scatter and return the lmfit result result = functions.fit_experimental_data(spec.x, spec.y, expected_peaks, deg_of_bck_poly=deg_of_bck_poly) import sys sys.exit() # Show the fit f, (a0, a1) = plt.subplots(2, 1, gridspec_kw={'height_ratios': [3, 1]}) f.set_size_inches(8, 16) a0.scatter(spec.x, spec.y, s=8) a0.plot(spec.x, result.best_fit, 'r-', linewidth=1.4)
def plot_profiles_amplitudes(*args, structure=None, labellist=None): ''' Attributes: args > list of different measurements in pandas form structure > pymatgen structure Returns: plot of calculated versus measurement patterns ''' calc = XRDCalculator(wavelength="CuKa1", typ='dft') calc2 = XRDCalculator(wavelength="CuKa1", typ='ict') XRD = calc.get_pattern(structure, two_theta_range=(15, 105), scaled=True) XRD2 = calc2.get_pattern(structure, two_theta_range=(15, 105), scaled=True) ## for i in range(0,len(XRD.hkls)): ## print(XRD.hkls[i]) ## print(XRD.y[i]) fig, ax = plt.subplots() ax.set_xlim([9, 105]) ax.set_ylim([0, 130]) ax.scatter(XRD.x, np.multiply(XRD.y, 1.0), marker='X', color='b', label='Ab initio form factors') ax.scatter(XRD2.x, np.multiply(XRD2.y, 1.0), marker='X', color='r', label='Ict form factors') for i, j in zip(XRD.x, np.multiply(XRD.y, 1.0)): ax.plot([i, i], [0, j], linestyle='--', color='b', alpha=0.5) for i, j in zip(XRD2.x, np.multiply(XRD2.y, 1.0)): ax.plot([i, i], [0, j], linestyle='--', color='b', alpha=0.5) for c, i in enumerate(args): if c == 0: color = 'k' x = i['peak_two_thetas'] y = i['amplitude'] spec = Spectrum(x, y) spec.normalize(mode='max', value=90) ax.scatter(spec.x, spec.y, marker='X', color=color, label=labellist[c]) if c == 1: color = 'r' x = i['peak_two_thetas'] y = i['amplitude'] spec = Spectrum(x, y) spec.normalize(mode='max', value=100) ax.scatter(spec.x, spec.y, marker='X', color=color, label=labellist[c]) if c == 2: color = 'b' x = i['peak_two_thetas'] y = i['amplitude'] spec = Spectrum(x, y) spec.normalize(mode='max', value=100) ax.scatter(spec.x, spec.y, marker='X', color=color, label=labellist[c]) for i, j in zip(spec.x, spec.y): ax.plot([i, i], [0, j], linestyle='--', color=color, alpha=0.5) if labellist != None: plt.legend() plt.grid() plt.xlabel('2\u03B8') plt.ylabel('Intensity (a.u.)') plt.show()
class SpectrumTest(PymatgenTest): def setUp(self): self.spec1 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100)) self.spec2 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100)) self.multi_spec1 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100, 2)) self.multi_spec2 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100, 2)) def test_normalize(self): self.spec1.normalize(mode="max") self.assertAlmostEqual(np.max(self.spec1.y), 1) self.spec1.normalize(mode="sum") self.assertAlmostEqual(np.sum(self.spec1.y), 1) self.multi_spec1.normalize(mode="sum") self.assertAlmostEqual(np.sum(self.multi_spec1.y[:, 0]), 1) self.assertAlmostEqual(np.sum(self.multi_spec1.y[:, 1]), 1) # XRD style mode. self.spec1.normalize(mode="max", value=100) self.assertAlmostEqual(np.max(self.spec1.y), 100) def test_operators(self): scaled_spect = 3 * self.spec1 + self.spec2 self.assertTrue(np.allclose(scaled_spect.y, 3 * self.spec1.y + self.spec2.y)) self.assertAlmostEqual( self.spec1.get_interpolated_value(0.05), (self.spec1.y[0] + self.spec1.y[1]) / 2, ) scaled_spect = self.spec1 - self.spec2 self.assertTrue(np.allclose(scaled_spect.y, self.spec1.y - self.spec2.y)) scaled_spect = self.spec1 / 3 self.assertTrue(np.allclose(scaled_spect.y, self.spec1.y / 3)) scaled_spect = 3 * self.multi_spec1 + self.multi_spec2 self.assertTrue(np.allclose(scaled_spect.y, 3 * self.multi_spec1.y + self.multi_spec2.y)) self.assertArrayAlmostEqual( self.multi_spec1.get_interpolated_value(0.05), (self.multi_spec1.y[0, :] + self.multi_spec1.y[1, :]) / 2, ) def test_smear(self): y = np.zeros(100) y[25] = 1 y[50] = 1 y[75] = 1 spec = Spectrum(np.linspace(-10, 10, 100), y) spec.smear(0.3) self.assertFalse(np.allclose(y, spec.y)) self.assertAlmostEqual(sum(y), sum(spec.y)) # Test direct callable use of smearing. spec2 = Spectrum(np.linspace(-10, 10, 100), y) spec2.smear(0, func=lambda x: stats.norm.pdf(x, scale=0.3)) self.assertTrue(np.allclose(spec.y, spec2.y)) spec = Spectrum(np.linspace(-10, 10, 100), y) spec.smear(0.3, func="lorentzian") self.assertFalse(np.allclose(y, spec.y)) self.assertAlmostEqual(sum(y), sum(spec.y)) y = np.array(self.multi_spec1.y) self.multi_spec1.smear(0.2) self.assertFalse(np.allclose(y, self.multi_spec1.y)) self.assertArrayAlmostEqual(np.sum(y, axis=0), np.sum(self.multi_spec1.y, axis=0)) def test_str(self): # Just make sure that these methods work. self.assertIsNotNone(str(self.spec1)) self.assertIsNotNone(str(self.multi_spec1)) def test_copy(self): spec1copy = self.spec1.copy() spec1copy.y[0] = spec1copy.y[0] + 1 self.assertNotEqual(spec1copy.y[0], self.spec1.y[0]) self.assertEqual(spec1copy.y[1], self.spec1.y[1])
# Reading out files two_theta_D5000, intensity_D5000 = functions.read_out_file( 'outFiles//D5000//1mm//Al2O3_2h30min_stripped.out') two_theta_GADDS, intensity_GADDS = functions.read_out_file( 'outFiles//GADDS//alumina_15min.out') # In order make a better fit some intensity values are changed to 0. After that intensities are normalized that maximum value become 100. indexes_to_make_zero = [] for c, i in enumerate(two_theta_D5000): if 41 < i < 42 or 61 < i < 62 or 73 < i < 88.5 or 0 < i < 25 or 89.5 < i < 94 or ( intensity_D5000[c] < 0.45): indexes_to_make_zero.append(c) for c in indexes_to_make_zero: intensity_D5000[c] = 0 spec_D5000 = Spectrum(two_theta_D5000, intensity_D5000) spec_D5000.normalize(mode='max', value=100) #Expected peaks are selected for the fit. expected_peaks_D5000 = [ 25.6, 35.1, 37.79, 43.4, 52.6, 57.6, 66.56, 68.25, 89.1, 95.34, 101.14 ] #,61.35 #Fit is done on scatter data. result_D5000 = functions.fit_experimental_data(spec_D5000.x, spec_D5000.y, expected_peaks_D5000, deg_of_bck_poly=0) # Show the fit f1, (a0, a1) = plt.subplots(2, 1, gridspec_kw={'height_ratios': [3, 1]}) f1.set_size_inches(8, 16)
tick_list[0] = 'asdeposited' two_theta = np.linspace(30, 100, num=1000) plt.figure(figsize=(14, 10)) plt.gca().yaxis.tick_right() for c, material in enumerate(material_list): measurement = pd.read_excel( '..//excelSheets//GADDS_{}.xlsx'.format(material), sheet_name='RawSample') print(measurement) y = np.zeros(len(two_theta)) for LP in range(0, 6): y += pseudo_voigt(two_theta, measurement['peak_two_thetas'][LP], measurement['fwhm'][LP], measurement['fraction'][LP], 100) spect = Spectrum(two_theta, y) spect.normalize(mode="max", value=100) plt.plot(spect.x, spect.y + 110 * c, c='k') plt.yticks([c * 110 for c in range(len(tick_list))], tick_list, fontsize=20) plt.xlabel('2\u03F4 $(^\circ)$') plt.ylabel('Intensity (a.u.)') ##plt.grid(b=True, which='major', color='#666666', linestyle='-') ##plt.minorticks_on() ##plt.grid(b=True, which='minor', color='k', linestyle='--', alpha = 0.4) plt.show()
class SpectrumTest(PymatgenTest): def setUp(self): self.spec1 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100)) self.spec2 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100)) self.multi_spec1 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100, 2)) self.multi_spec2 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100, 2)) def test_normalize(self): self.spec1.normalize(mode="max") self.assertAlmostEqual(np.max(self.spec1.y), 1) self.spec1.normalize(mode="sum") self.assertAlmostEqual(np.sum(self.spec1.y), 1) self.multi_spec1.normalize(mode="sum") self.assertAlmostEqual(np.sum(self.multi_spec1.y[:, 0]), 1) self.assertAlmostEqual(np.sum(self.multi_spec1.y[:, 1]), 1) # XRD style mode. self.spec1.normalize(mode="max", value=100) self.assertAlmostEqual(np.max(self.spec1.y), 100) def test_operators(self): scaled_spect = 3 * self.spec1 + self.spec2 self.assertTrue(np.allclose(scaled_spect.y, 3 * self.spec1.y + self.spec2.y)) self.assertAlmostEqual(self.spec1.get_interpolated_value(0.05), (self.spec1.y[0] + self.spec1.y[1]) / 2) scaled_spect = self.spec1 - self.spec2 self.assertTrue(np.allclose(scaled_spect.y, self.spec1.y - self.spec2.y)) scaled_spect = self.spec1 / 3 self.assertTrue(np.allclose(scaled_spect.y, self.spec1.y / 3)) scaled_spect = 3 * self.multi_spec1 + self.multi_spec2 self.assertTrue(np.allclose(scaled_spect.y, 3 * self.multi_spec1.y + self.multi_spec2.y)) self.assertArrayAlmostEqual( self.multi_spec1.get_interpolated_value(0.05), (self.multi_spec1.y[0, :] + self.multi_spec1.y[1, :]) / 2) def test_smear(self): y = np.array(self.spec1.y) self.spec1.smear(0.2) self.assertFalse(np.allclose(y, self.spec1.y)) self.assertAlmostEqual(sum(y), sum(self.spec1.y)) y = np.array(self.multi_spec1.y) self.multi_spec1.smear(0.2) self.assertFalse(np.allclose(y, self.multi_spec1.y)) self.assertArrayAlmostEqual(np.sum(y, axis=0), np.sum(self.multi_spec1.y, axis=0)) def test_str(self): # Just make sure that these methods work. self.assertIsNotNone(str(self.spec1)) self.assertIsNotNone(str(self.multi_spec1)) def test_copy(self): spec1copy = self.spec1.copy() spec1copy.y[0] = spec1copy.y[0] + 1 self.assertNotEqual(spec1copy.y[0], self.spec1.y[0]) self.assertEqual(spec1copy.y[1], self.spec1.y[1])