예제 #1
0
 def setUp(self):
     self.scan = XRDScan(
         os.path.join(TESTDIR, 'lmo-two-phase.brml'),
         phases=[LMOHighV(), LMOMidV()],
     )
     # For measuring FWHM
     self.onephase_scan = XRDScan('test-sample-frames/LMO-sample-data.plt',
                                  phases=[LMOLowAngle()])
예제 #2
0
 def test_get_scattering_lengths(self):
     # Check for a .brml file
     scan = XRDScan(filename=os.path.join(TESTDIR, 'corundum.brml'))
     q = scan.scattering_lengths
     self.assertEqual(q.shape, (6851, ))
     # Check for a .plt file
     scan = XRDScan(filename=os.path.join(TESTDIR, 'LMO-sample-data.plt'))
     q = scan.scattering_lengths
예제 #3
0
    def test_fit_background(self):
        corundum = standards.Corundum()
        # Get sample data from Mew XRD
        scan = XRDScan(filename=COR_BRML, phase=corundum)
        df = scan.diffractogram
        q = df.index
        I = df.counts
        # Remove expected XRD peaks
        for reflection in corundum.reflection_list:
            q, I = remove_peak_from_df(x=q, y=I, xrange=reflection.qrange)
            # Do the background fitting
        refinement = NativeRefinement(phases=[corundum])
        bg = refinement.refine_background(q, I)
        # plt.plot(q, I)
        # plt.plot(q, bg)
        # plt.show()

        from scipy.signal import find_peaks
        result = find_peaks(df.counts.values)
        peaks, props = result
        # Spot-check some values on the resulting background
        new_bg = refinement.background(df.index)
        # plt.plot(df.counts.values)
        # plt.plot(new_bg)
        # plt.plot(df.counts.values - new_bg)
        # plt.show()
        # Check the median diffraction value for whether fitting is good
        subtracted = df.counts.values - new_bg
        median = abs(np.median(subtracted))
        self.assertTrue(median < 5)
예제 #4
0
 def test_overall_fit_discharged(self):
     scan = XRDScan(self.discharged_file)
     TTs = scan.two_theta
     Is = scan.intensities
     # Check that the residuals of the fit are low
     predicted = self.refinement.predict(TTs, Is)
     rms_error = np.sqrt(np.mean((Is - predicted)**2))
예제 #5
0
 def test_cell_params_charged(self):
     scan = XRDScan(self.charged_file)
     TTs = scan.two_theta
     Is = scan.intensities
     cell_params = self.refinement.cell_params(TTs, Is)
     expected = [[8.141, 8.141, 8.141, 90., 90., 90.],
                 [8.061, 8.061, 8.061, 90., 90., 90.]]
     np.testing.assert_almost_equal(cell_params, expected, decimal=3)
예제 #6
0
 def test_cell_params_discharged(self):
     scan = XRDScan(self.discharged_file)
     TTs = scan.two_theta
     Is = scan.intensities
     cell_params = self.refinement.cell_params(TTs, Is)
     expected = [8.233, 8.233, 8.233, 90, 90, 90]
     # Only check for the first phase, the second phase is nothing
     np.testing.assert_almost_equal(cell_params[0], expected, decimal=3)
예제 #7
0
 def test_overall_fit_discharged2(self):
     scan = XRDScan(self.discharged_file2)
     refinement = lmo.LmoFullRefinement(wavelengths=self.wavelengths)
     TTs = scan.two_theta
     Is = scan.intensities
     # Check that the residuals of the fit are low
     predicted = refinement.predict(TTs, Is)
     rms_error = np.sqrt(np.mean((Is - predicted)**2))
     self.assertLess(rms_error, 0.65)
예제 #8
0
 def test_bad_refinement(self):
     # This specific plot did not fit well
     plt_file = os.path.join(TESTDIR,
                             'charged_2C_to47V_quarterlithium-map-c0.plt')
     print(plt_file)
     scan = XRDScan(plt_file)
     TTs = scan.two_theta
     Is = scan.intensities
     smoothed = self.refinement.smooth_data(Is)
예제 #9
0
 def test_background_discharged(self):
     scan = XRDScan(self.discharged_file)
     TTs = scan.two_theta
     Is = scan.intensities
     bg = self.refinement.background(TTs, Is)
     # Check that background has the right shape
     self.assertEqual(bg.shape, TTs.shape)
     # Check that the background doesn't go past 7, which is about
     # as high as the noise. Higher values mean it's fitting peaks
     self.assertLess(np.max(bg), 7)
예제 #10
0
 def test_bad_phase_fraction(self):
     """This .plt gaves a wrong value when refining phase fraction."""
     plt_file = os.path.join(TESTDIR, '1C_charged_plateau-map-13c.plt')
     scan = XRDScan(plt_file)
     TTs = scan.two_theta
     Is = scan.intensities
     predicted = self.refinement.predict(TTs, Is)
     bg = self.refinement.background(TTs, Is)
     fractions = self.refinement.phase_fractions(TTs, Is)
     np.testing.assert_almost_equal(fractions, [0.87, 0.13], decimal=2)
예제 #11
0
 def test_bad_refinement(self):
     """This .plt file did not refine originally, so let's fix it."""
     plt_file = os.path.join(TESTDIR, '1C_charged_plateau-map-129.plt')
     scan = XRDScan(plt_file)
     TTs = scan.two_theta
     Is = scan.intensities
     predicted = self.refinement.predict(TTs, Is)
     bg = self.refinement.background(TTs, Is)
     fractions = self.refinement.phase_fractions(TTs, Is)
     unit_cells = self.refinement.cell_params(TTs, Is)
     np.testing.assert_almost_equal(fractions, [0.897, 0.103], decimal=3)
예제 #12
0
 def test_overall_fit_discharged3(self):
     scan = XRDScan(self.discharged_file3)
     TTs = scan.two_theta
     Is = scan.intensities
     # Check that the residuals of the fit are low
     predicted = self.refinement.predict(TTs, Is)
     rms_error = np.sqrt(np.mean((Is - predicted)**2))
     # plt.plot(TTs, Is)
     # plt.plot(TTs, predicted)
     # plt.show()
     self.assertLess(rms_error, 0.65)
예제 #13
0
 def test_contains_peak(self):
     scan = XRDScan(filename=COR_BRML, phases=[])
     df = scan.diffractogram
     q = df.index
     I = df.counts
     # Check for an existent peak
     self.assertTrue(contains_peak(q, (1, 2)))
     # Check for a nonexistent peak
     self.assertFalse(contains_peak(q, (8, 9)))
     # Check for a partially existent peak
     self.assertTrue(contains_peak(q, (0, 1)))
예제 #14
0
class NativeRefinementTest(unittest.TestCase):
    def setUp(self):
        self.scan = XRDScan(
            os.path.join(TESTDIR, 'lmo-two-phase.brml'),
            phases=[LMOHighV(), LMOMidV()],
        )
        # For measuring FWHM
        self.onephase_scan = XRDScan('test-sample-frames/LMO-sample-data.plt',
                                     phases=[LMOLowAngle()])

    def test_peak_area(self):
        reflection = LMOMidV().diagnostic_reflection
        self.assertAlmostEqual(self.refinement.net_area(reflection.qrange),
                               205)

    # @unittest.expectedFailure
    def test_peak_fwhm(self):
        """Method for computing full-width at half max of a peak."""
        result = self.onephase_scan.refinement.fwhm()
        # Plotting for diagnostics
        ax = self.onephase_scan.plot_diffractogram()
        ax.set_xlim(35.3, 37)
        ax.set_ylim(0, 15)
        ax.grid(True, which='both')
        ax.figure.savefig('refinement.png', dpi=200)
        # This is the real answer:
        # self.assertAlmostEqual(
        #     result,
        #     0.233 # Measured with a ruler
        # )
        # Ignoring kα1/kα2 overlap, you get this:
        # self.assertAlmostEqual(
        #     result,
        #     0.275 # Measured with a ruler
        # )
        self.assertAlmostEqual(result, 0.1702)

    def test_peak_list(self):
        corundum_scan = XRDScan(corundum_path, phase=standards.Corundum())
        refinement = NativeRefinement(scan=corundum_scan)
        refinement.fit_peaks()
        peak_list = refinement.peak_list
        two_theta_list = [peak.center_kalpha for peak in peak_list]
        hkl_list = [peak.reflection.hkl_string for peak in peak_list]
        self.assertAlmostEqual(two_theta_list, [
            25.599913304005099, 35.178250906935716, 37.790149818489454,
            41.709732482339412, 43.388610036562113, 52.594640340604649,
            57.54659705350258
        ],
                               tolerance=0.001)
        self.assertEqual(
            hkl_list,
            [reflection.hkl_string for reflection in Corundum.reflection_list])
예제 #15
0
 def test_remove_peaks(self):
     corundum = standards.Corundum()
     # Get sample data from Mew XRD
     scan = XRDScan(filename="test-data-xrd/corundum.brml", phase=corundum)
     df = scan.diffractogram
     q = df.index
     old_I = df.counts
     # Remove expected XRD peaks
     new_I = corundum.remove_peaks(q, old_I)
     # Check that the result is the same shape as the input data
     self.assertEqual(old_I.shape, new_I.shape)
     # Spot-check a few intensity values against previously verified results
     assert False, "TODO: Write a test for spot-checking the removed peaks"
예제 #16
0
 def test_peak_widths(self):
     corundum = standards.Corundum()
     # Get sample data from Mew XRD
     scan = XRDScan(filename=COR_BRML, phases=[corundum, corundum])
     df = scan.diffractogram
     q = df.index
     I_raw = df.counts.values
     # Background fitting
     refinement = NativeRefinement(phases=[corundum])
     bg = refinement.refine_background(q, I_raw)
     I = I_raw - bg
     # Do the peak width fitting
     result = refinement.refine_peak_widths(q, I)
     self.assertEqual(len(result), 1)  # (only one phase was refined)
     self.assertAlmostEqual(result[0], 0.00328, places=5)
예제 #17
0
 def test_peak_list(self):
     corundum_scan = XRDScan(corundum_path, phase=standards.Corundum())
     refinement = NativeRefinement(scan=corundum_scan)
     refinement.fit_peaks()
     peak_list = refinement.peak_list
     two_theta_list = [peak.center_kalpha for peak in peak_list]
     hkl_list = [peak.reflection.hkl_string for peak in peak_list]
     self.assertAlmostEqual(two_theta_list, [
         25.599913304005099, 35.178250906935716, 37.790149818489454,
         41.709732482339412, 43.388610036562113, 52.594640340604649,
         57.54659705350258
     ],
                            tolerance=0.001)
     self.assertEqual(
         hkl_list,
         [reflection.hkl_string for reflection in Corundum.reflection_list])
예제 #18
0
 def test_refine(self):
     # Prepare the test objects
     scan = XRDScan(COR_BRML, phase=standards.Corundum())
     two_theta = q_to_twotheta(scan.scattering_lengths, wavelength=scan.wavelength)
     refinement = PawleyRefinement(wavelengths=self.wavelengths,
                                   phases=[scan.phases[0]],
                                   num_bg_coeffs=9)
     
     # Set starting parameters a bit closer to keep from taking too long
     scan.phases[0].unit_cell.a = 4.755
     scan.phases[0].unit_cell.c = 12.990
     for r in scan.phases[0].reflection_list:
         r.intensity *= 200
     # Do the refinement
     refinement.refine(two_theta=two_theta, intensities=scan.intensities)
     predicted = refinement.predict(two_theta=two_theta)
     actual = scan.diffractogram.counts.values
     refinement.plot(two_theta=two_theta, intensities=scan.intensities)
     plt.show()
     # Check scale factors
     scale = refinement.scale_factor(two_theta=two_theta)
     print(scale)
     # Check phase fractions
     fracs = refinement.phase_fractions(two_theta=two_theta)
     np.testing.assert_almost_equal(fracs, (1, ))
     # Check background fitting
     bg = refinement.background(two_theta=two_theta)
     self.assertEqual(bg.shape, actual.shape)
     # Check refined unit-cell parameters
     unit_cell = refinement.unit_cells()
     self.assertAlmostEqual(
         unit_cell[0],
         4.758877,
         places=3,
     )
     self.assertAlmostEqual(
         unit_cell[2],
         12.992877,
         places=3
     )
     peak_widths = refinement.peak_breadths(two_theta, scan.intensities)
     self.assertAlmostEqual(peak_widths[0], 0.046434, places=5)        
     # Check that the refinement is of sufficient quality
     error = np.sqrt(np.sum((actual-predicted)**2))/len(actual)
     self.assertLess(error, 1.5)
예제 #19
0
 def test_net_area(self):
     scan = XRDScan(filename=COR_BRML, phases=[])
     df = scan.diffractogram
     q = df.index
     I = df.counts.values
     # Pick an arbitrary q range
     idx1 = 2400
     idx2 = 2500
     qrange = (q[idx1], q[idx2])
     # Find expected area (idx2+1 to make it inclusive)
     expected = np.trapz(x=q[idx1:idx2 + 1], y=I[idx1:idx2 + 1])
     # Calculate the net area and compare
     area = peak_area(q, I, qrange=qrange)
     self.assertEqual(area, expected)
     # Choose something outside the qrange (should be zero)
     qrange = (0, 0.5)
     area = peak_area(q, I, qrange=qrange)
     self.assertEqual(area, 0)
예제 #20
0
    def test_phase_ratios(self):
        corundum = standards.Corundum()
        # Get sample data from Mew XRD
        scan = XRDScan(filename=COR_BRML, phases=[corundum, corundum])
        df = scan.diffractogram
        q = df.index
        I = df.counts
        # Remove expected XRD peaks
        for reflection in corundum.reflection_list:
            q, I = remove_peak_from_df(x=q, y=I, xrange=reflection.qrange)
        # Do the background fitting
        refinement = NativeRefinement(phases=[corundum, corundum])
        q = df.index
        bg = refinement.refine_background(df.index, df.counts.values)
        subtracted = df.counts.values - bg

        # Do phase fraction refinement
        result = refinement.refine_phase_fractions(q, subtracted)
        np.testing.assert_equal(result, [0.5, 0.5])
예제 #21
0
 def setUp(self):
     self.scan = XRDScan(self.plt_file)
     wavelengths = tubes['Cu'].wavelengths
     self.refinement = lmo.TwoPhaseRefinement(wavelengths=wavelengths)
예제 #22
0
 def test_init_wavelength(self):
     scan = XRDScan()
     self.assertAlmostEqual(scan.wavelength, 1.54187, places=4)
     # Try supplying a wavelength explicitly
     scan = XRDScan(wavelength=1.90)
     self.assertEqual(scan.wavelength, 1.90)
예제 #23
0
 def test_diffractogram(self):
     scan = XRDScan(filename=COR_BRML)
     df = scan.diffractogram
     # Check that the diffractogram loads 2θ and intensities
     np.testing.assert_equal(df.index, scan.two_theta)
예제 #24
0
 def setUp(self):
     self.scan = XRDScan(self.plt_file)
     wavelengths = tubes['Cu'].wavelengths
     self.refinement = lmo.SolidSolutionRefinement(wavelengths=wavelengths)