Beispiel #1
0
def test_wave_length_rw():
    """Test writing and reading for wave length

    Returns
    -------

    """
    # Set up for testing
    test_file_name = 'test_wave_length.h5'
    # Create a detector mask
    gold_wave_length = 1.23456

    # Generate a HiDRA project file
    test_project_file = HidraProjectFile(test_file_name,
                                         HidraProjectFileMode.OVERWRITE)
    test_project_file.save(True)
    test_project_file.close()

    # Open file
    verify_project_file = HidraProjectFile(test_file_name,
                                           HidraProjectFileMode.READONLY)

    # Read wave length (not exist)
    wave_length_test = verify_project_file.read_wavelengths()
    assert np.isnan(wave_length_test), 'No wave length read out'

    # Close
    verify_project_file.close()

    # Generate a HiDRA project file
    test_project_file = HidraProjectFile(test_file_name,
                                         HidraProjectFileMode.READWRITE)

    # Write wave length
    test_project_file.write_wavelength(gold_wave_length)

    # Save and close
    test_project_file.save(True)
    test_project_file.close()

    # Open file again to verify
    verify_project_file2 = HidraProjectFile(test_file_name,
                                            HidraProjectFileMode.READONLY)

    # Read wave length (not exist)
    wave_length_test = verify_project_file2.read_wavelengths()
    assert wave_length_test == gold_wave_length

    # Clean
    os.remove(test_file_name)
Beispiel #2
0
    def test_peak_fitting_result_io(self):
        """Test peak fitting result's writing and reading

        Returns
        -------

        """
        # Generate a unique test file
        now = datetime.datetime.now()
        test_file_name = 'test_peak_io_{}.hdf'.format(now.toordinal())

        # Generate a HiDRA project file
        test_project_file = HidraProjectFile(test_file_name,
                                             HidraProjectFileMode.OVERWRITE)
        test_project_file.write_wavelength(1.54)

        # Create a ND array for output parameters
        param_names = PeakShape.PSEUDOVOIGT.native_parameters + BackgroundFunction.LINEAR.native_parameters
        data_type = list()
        for param_name in param_names:
            data_type.append((param_name, np.float32))
        test_error_array = np.zeros(3, dtype=data_type)
        test_params_array = np.zeros(3, dtype=data_type)

        for i in range(3):
            # sub run
            for j, par_name in enumerate(param_names):
                test_params_array[par_name][i] = 2**i + 0.1 * 3**j
                test_error_array[par_name][i] = np.sqrt(
                    abs(test_params_array[par_name][i]))
        # END-FOR
        chi2_array = np.array([0.323, 0.423, 0.523])

        # Add some original test data
        peaks = PeakCollection('test fake', PeakShape.PSEUDOVOIGT,
                               BackgroundFunction.LINEAR)
        peaks.set_peak_fitting_values(np.array([11, 21, 31]),
                                      np.ones(3, dtype=data_type),
                                      np.ones(3, dtype=data_type),
                                      np.array([1.323, 1.423, 1.523]))

        test_project_file.write_peak_parameters(peaks)

        # Replace the peaks data with the real data that will be tested for
        peaks = PeakCollection('test fake', PeakShape.PSEUDOVOIGT,
                               BackgroundFunction.LINEAR)
        peaks.set_peak_fitting_values(np.array([1, 2, 3]), test_params_array,
                                      test_error_array, chi2_array)

        test_project_file.write_peak_parameters(peaks)

        test_project_file.save(False)

        # Check
        assert os.path.exists(test_file_name), 'Test project file for peak fitting result {} cannot be found.' \
                                               ''.format(test_file_name)
        print('[INFO] Peak parameter test project file: {}'.format(
            test_file_name))

        # Import
        verify_project_file = HidraProjectFile(test_file_name,
                                               HidraProjectFileMode.READONLY)

        # get the tags
        peak_tags = verify_project_file.read_peak_tags()
        assert 'test fake' in peak_tags
        assert len(peak_tags) == 1

        # get the parameter of certain
        peak_info = verify_project_file.read_peak_parameters('test fake')

        # peak profile
        assert peak_info.peak_profile == str(PeakShape.PSEUDOVOIGT)
        assert peak_info.background_type == str(BackgroundFunction.LINEAR)

        # sub runs
        assert np.allclose(peak_info.sub_runs, np.array([1, 2, 3]))

        # parameter values
        # print('DEBUG:\n  Expected: {}\n  Found: {}'.format(test_params_array, peak_info[3]))
        peak_values, peak_errors = peak_info.get_native_params()
        assert_allclose_structured_numpy_arrays(test_params_array, peak_values)
        # np.testing.assert_allclose(peak_info[3], test_params_array, atol=1E-12)

        # parameter values
        # assert np.allclose(peak_info[4], test_error_array, 1E-12)
        assert_allclose_structured_numpy_arrays(test_error_array, peak_errors)

        dspacing, _ = peak_info.get_dspacing_center()
        np.testing.assert_allclose(dspacing, [46.441864, 30.429281, 18.012734])

        # Clean
        os.remove(test_file_name)