示例#1
0
    project.import_raw_csv_files('test_data')
    #project.import_raw_csv_files('/home/iant/Desktop/Johan_Lissenberg_Xmas2015',
    #                             ['Ca K series.csv', 'Mg K series.csv'])
else:
    project.import_raw_csv_files(
        'test_data', ['Ca K series.csv', 'Na K series.csv', 'Mg K series.csv'])

project.filter(pixel_totals=True, median=True)
project.normalise()
project.create_h_factor()

project.create_phase_map_by_thresholding(
    'test', [['Ca', 1500, 1700], ['Na', 100, 800]])
project.create_phase_map_by_thresholding('single', [['Mg', 100, 1000]])
print('Phases:', project.phases)
phase = project.get_phase('test')

plt.subplot(211)
plt.imshow(project.get_filtered('Mg'))
plt.colorbar()
plt.subplot(212)

if 1:
    plt.imshow(phase)
else:
    cmap_int_max = 2
    norm = Normalize(-0.5, cmap_int_max - 0.5)
    image = plt.imshow(phase, norm=norm)
    cmap_ticks = np.arange(0, cmap_int_max)
    figure = plt.gcf()
    colorbar = figure.colorbar(image, ticks=cmap_ticks)
示例#2
0
def consistency_impl(directory, pixel_totals, median):
    # Test that project is consistent, e.g. size of arrays, common masks.
    # This test works on all models regardless of elements, etc.
    p = QACDProject()
    p.set_filename(temp_filename)
    p.import_raw_csv_files(
        directory)  #, [el+' K series.csv' for el in ['Mg', 'Ca', 'Na']])
    p.filter(pixel_totals=pixel_totals, median=median)
    p.normalise()
    p.create_h_factor()

    elements = p.elements
    files = fnmatch.filter(os.listdir(directory), '* K series.csv')
    assert elements == sorted([file_.split()[0] for file_ in files])

    for i, element in enumerate(elements):
        raw, raw_stats = p.get_raw(element, want_stats=True)
        filtered, filtered_stats = p.get_filtered(element, want_stats=True)
        normalised, normalised_stats = p.get_normalised(element,
                                                        want_stats=True)
        if i == 0:
            ny, nx = raw.shape
            raw_cumulative = raw.copy()
            raw_mask = raw.mask
            filtered_cumulative = filtered.copy()
            filtered_mask = filtered.mask
            normalised_cumulative = normalised.copy()
            normalised_mask = normalised.mask
        else:
            raw_cumulative += raw
            filtered_cumulative += filtered
            normalised_cumulative += normalised

        check_masked_array(raw, (ny, nx), np.int32, raw_mask, -1)
        check_stats(raw, raw_stats, 'min max mean median std invalid valid')

        check_masked_array(filtered, (ny, nx), np.float64, filtered_mask,
                           np.nan)
        check_stats(filtered, filtered_stats,
                    'min max mean median std invalid valid')

        check_masked_array(normalised, (ny, nx), np.float64, normalised_mask,
                           np.nan)
        check_stats(normalised, normalised_stats,
                    'min max mean median std invalid valid')

        # Check raw masked carry across to filtered and normalised.
        assert np.all(filtered.data[raw.mask] == np.nan)
        assert np.all(normalised.data[raw.mask] == np.nan)

    raw_total, raw_total_stats = p.get_raw_total(want_stats=True)
    assert np.allclose(raw_total, raw_cumulative)
    check_masked_array(raw_total, (ny, nx), np.int32, raw_mask, -1)
    check_stats(raw_total, raw_total_stats,
                'min max mean median std invalid valid')

    filtered_total, filtered_total_stats = p.get_filtered_total(
        want_stats=True)
    assert np.allclose(filtered_total, filtered_cumulative)
    check_masked_array(filtered_total, (ny, nx), np.float64, filtered_mask,
                       np.nan)
    check_stats(filtered_total, filtered_total_stats,
                'min max mean median std invalid valid')

    assert np.allclose(normalised_cumulative, 1.0)
    np.testing.assert_array_equal(normalised.mask, filtered.mask)

    # h factor.
    h_factor, h_factor_stats = p.get_h_factor(want_stats=True)
    np.testing.assert_array_equal(h_factor.mask, filtered.mask)
    check_masked_array(h_factor, (ny, nx), np.float64, h_factor.mask, np.nan)
    check_stats(h_factor, h_factor_stats,
                'min max mean median std invalid valid')

    # Ratio.
    ratio_elements = ['Ca', 'Mg']
    formula = '{0}/({0}+{1})'.format(ratio_elements[0], ratio_elements[1])
    for name, correction_model in zip(['ratioA', 'ratioB'], [None, 'garnet']):
        p.create_ratio_map(name,
                           elements=ratio_elements,
                           correction_model=correction_model)
        ratio_entry = p.ratios[name]
        assert formula, name == ratio_entry
        ratio, ratio_stats = p.get_ratio(name, want_stats=True)
        ratio_stats.pop('formula')
        ratio_stats.pop('correction_model')
        ratio_stats.pop('preset')

        check_masked_array(ratio, (ny, nx), np.float64, ratio.mask, np.nan)
        check_stats(ratio, ratio_stats,
                    'min max mean median std invalid valid')

    # k-means clustering
    k_min = 5
    k_max = 10
    p.k_means_clustering(k_min, k_max, want_all_elements=True)
    for k in range(k_min, k_max + 1):
        cluster, cluster_stats = p.get_cluster_indices(k, want_stats=True)
        np.testing.assert_array_equal(cluster.mask, filtered.mask)
        check_masked_array(cluster, (ny, nx), np.int8, cluster.mask, -1)
        check_stats(cluster, cluster_stats, 'min max invalid valid')

    # Phase map by thresholding.
    elements_and_thresholds = [['Ca', 100, 1000], ['Mg', 0, 10000]]
    name = 'two'
    p.create_phase_map_by_thresholding(name, elements_and_thresholds)
    phase, phase_stats = p.get_phase(name, want_stats=True)
    source = phase_stats.pop('source')
    assert source == 'thresholding'
    phase_stats.pop('elements_and_thresholds')
    assert p.phases[name] == ('thresholding', elements_and_thresholds)

    mask = phase == False
    check_masked_array(phase, (ny, nx), np.bool, mask, False)
    check_stats(phase, phase_stats, 'invalid valid')

    # Phase map from cluster.
    k = k_min
    original_values = [0]
    name = 'three'
    phase_map = cluster == 0
    p.create_phase_map_from_cluster(name, phase_map, k, original_values)
    phase, phase_stats = p.get_phase(name, want_stats=True)
    source = phase_stats.pop('source')
    assert source == 'cluster'
    phase_stats.pop('k')
    phase_stats.pop('original_values')
    assert p.phases[name] == ('cluster', k, original_values)

    mask = phase == False
    check_masked_array(phase, (ny, nx), np.bool, mask, False)
    check_stats(phase, phase_stats, 'invalid valid')

    # Region.
    region = p.calculate_region_ellipse((100, 60), (40, 20))
    name = 'ellipse1'
    shape_string = 'ellipse'
    p.create_region(name, shape_string, region)
    region, region_stats = p.get_region(name, want_stats=True)
    shape = region_stats.pop('shape')
    assert shape == shape_string

    mask = region == False
    check_masked_array(region, (ny, nx), np.bool, mask, False)
    check_stats(region, region_stats, 'invalid valid')

    # Display options.
    options = p.display_options
    # Check defaults.
    assert options.colourmap_name == 'rainbow'
    assert options.show_ticks_and_labels == True
    assert options.overall_title == ''
    assert options.show_project_filename == False
    assert options.show_date == False
    assert options.use_scale == False
    assert options.pixel_size == 1.0
    assert options.units == '\u03BCm'
    assert options.show_scale_bar == True
    assert options.scale_bar_location == 'lower left'
    assert options.scale_bar_colour == 'black'
    assert options.histogram_bin_count == 100
    # Set options and check can read them back OK.
    options.set_colourmap_name('viridis')
    assert options.colourmap_name == 'viridis'

    options.set_labels_and_scale(False, 'Title', True, True, True, 23.2, 'mm',
                                 False, 'upper right', 'white')
    assert options.show_ticks_and_labels == False
    assert options.overall_title == 'Title'
    assert options.show_project_filename == True
    assert options.show_date == True
    assert options.use_scale == True
    assert options.pixel_size == 23.2
    assert options.units == 'mm'
    assert options.show_scale_bar == False
    assert options.scale_bar_location == 'upper right'
    assert options.scale_bar_colour == 'white'
    options.set_labels_and_scale(False, 'Title', True, True, True, 23.2, 'nm',
                                 False, 'upper left', 'white')
    assert options.units == 'nm'
    assert options.scale_bar_location == 'upper left'
    options.set_labels_and_scale(False, 'Title', True, True, True, 23.2, 'nm',
                                 False, 'lower right', 'white')
    assert options.scale_bar_location == 'lower right'
    with pytest.raises(RuntimeError):  # Invalid colourmap name.
        options.set_colourmap_name('unknown colourmap')
    with pytest.raises(RuntimeError):  # Zero pixel size.
        options.set_labels_and_scale(False, 'Title', True, True, True, 0.0,
                                     'mm', False, 'lower left', 'white')
    with pytest.raises(RuntimeError):  # Negative pixel size.
        options.set_labels_and_scale(False, 'Title', True, True, True, -0.1,
                                     'mm', False, 'lower left', 'white')
    with pytest.raises(RuntimeError):  # Invalid units.
        options.set_labels_and_scale(False, 'Title', True, True, True, 23.2,
                                     'pm', False, 'lower left', 'white')
    with pytest.raises(RuntimeError):  # Invalid scale bar location.
        options.set_labels_and_scale(False, 'Title', True, True, True, 23.2,
                                     'mm', False, 'centre', 'white')
    with pytest.raises(RuntimeError):  # Invalid scale bar colour.
        options.set_labels_and_scale(False, 'Title', True, True, True, 23.2,
                                     'mm', False, 'lower left', 'purple')

    options.set_histogram(True, 20, 0.1, 1000, False)
    assert options.histogram_bin_count == 20

    # Cleanup.
    if os.path.isfile(temp_filename):
        os.remove(temp_filename)