Exemplo n.º 1
0
def test_write_csv_from_project(project_file_name, csv_filename,
                                expected_header, num_subruns, num_logs,
                                startswith, endswith):
    """Test the method to export CSV file
    """
    # load project file
    if not os.path.exists(project_file_name):
        pytest.skip('Project file {} does not exist'.format(project_file_name))
    project = HidraProjectFile(project_file_name)

    # get information from the project file
    peak_tags = project.read_peak_tags()
    peak_collections = [
        project.read_peak_parameters(tag) for tag in peak_tags
    ]  # all tags
    sample_logs = project.read_sample_logs()
    assert sample_logs.subruns.size == num_subruns  # just as a quick check

    # write out the csv file
    generator = SummaryGenerator(csv_filename)
    generator.setHeaderInformation({'project': '/some/place/random.h5'
                                    })  # only set one value
    generator.write_csv(sample_logs, peak_collections)

    # testing
    assert os.path.exists(csv_filename), '{} was not created'.format(
        csv_filename)

    # verify the file contents
    with open(csv_filename, 'r') as handle:
        # read in the file and remove whitespace
        contents = [line.strip() for line in handle.readlines()]

    # verify exact match on the header
    for exp, obs in zip(contents[:len(expected_header)], expected_header):
        assert exp == obs

    # verify the column headers
    assert contents[len(expected_header)].startswith(startswith)
    assert contents[len(expected_header)].endswith(endswith)

    assert len(contents) == len(
        expected_header) + 1 + num_subruns, 'Does not have full body'
    # verify that the number of columns is correct
    # columns are (subruns, seven logs, parameter values, uncertainties, d_spacing values,
    # strain values and uncertainties, chisq)
    for line in contents[len(expected_header) +
                         1:]:  # skip past header and constant log
        assert len(line.split(',')) == 1 + num_logs + 7 * 2 + (2 * 2) + 1

    # cleanup
    os.remove(csv_filename)
Exemplo n.º 2
0
 def load_hidra_project_file(self, filename, direction):
     try:
         source_project = HidraProjectFile(
             filename, mode=HidraProjectFileMode.READONLY)
         ws = HidraWorkspace(direction)
         ws.load_hidra_project(source_project, False, False)
         peaks = dict()
         for peak in source_project.read_peak_tags():
             peaks[peak] = source_project.read_peak_parameters(peak)
         return ws, peaks
     except Exception as e:
         self.failureMsg.emit(
             f"Failed to load {filename}. Check that this is a Hidra Project File",
             str(e), traceback.format_exc())
         return None, dict()
Exemplo n.º 3
0
def test_strain_io():
    """Test PeakCollection writing and reading with *D reference*

    Returns
    -------

    """
    # Generate a unique test file
    now = datetime.datetime.now()
    test_file_name = 'test_strain_io_{}.h5'.format(now.toordinal())
    test_ref_d = 1.23454321
    test_ref_d2 = np.array([1.23, 1.24, 1.25])
    peak_tag = 'Fake Peak D'
    peak_tag_2 = 'Fake Peak D Diff'

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

    # 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 test data to output
    peaks = PeakCollection(peak_tag, PeakShape.PSEUDOVOIGT,
                           BackgroundFunction.LINEAR)
    peaks.set_peak_fitting_values(np.array([1, 2, 3]), test_params_array,
                                  test_error_array, chi2_array)
    peaks.set_d_reference(test_ref_d)

    # Add 2nd peak
    peaks2 = PeakCollection(peak_tag_2, PeakShape.PSEUDOVOIGT,
                            BackgroundFunction.LINEAR)
    peaks2.set_peak_fitting_values(np.array([1, 2, 3]), test_params_array,
                                   test_error_array, chi2_array)
    peaks2.set_d_reference(test_ref_d2)

    # Write
    test_project_file.write_peak_parameters(peaks)
    test_project_file.write_peak_parameters(peaks2)
    # Save
    test_project_file.save(verbose=False)

    # Verify
    assert os.path.exists(test_file_name), 'Test project file for peak fitting result {} cannot be found.' \
                                           ''.format(test_file_name)

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

    # check tags
    peak_tags = verify_project_file.read_peak_tags()
    assert peak_tag in peak_tags and peak_tag_2 in peak_tags
    assert len(peak_tags) == 2

    # Get d-reference of peak 1 to check
    peak_info = verify_project_file.read_peak_parameters(peak_tag)
    verify_d_ref = peak_info.get_d_reference()
    gold_ref_d = np.array([test_ref_d] * 3)
    np.testing.assert_allclose(verify_d_ref, gold_ref_d)

    # Get d-reference of peak 2 to check
    peak_info2 = verify_project_file.read_peak_parameters(peak_tag_2)
    verify_d_ref_2 = peak_info2.get_d_reference()
    np.testing.assert_allclose(verify_d_ref_2, test_ref_d2)

    # Clean
    os.remove(test_file_name)

    return
Exemplo n.º 4
0
def test_peak_fitting_result_io():
    """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)

    # 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 test data to output
    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)

    # Clean
    os.remove(test_file_name)

    return