lichtung-plot-ndarray 0.2.0

Direct scientific plotting from logical ndarray views and columns
Documentation
use ndarray::{array, s, Array2};

use crate::{
    binding::{ArraySource, ColumnProjection, SourceRowsInput},
    formatting::Labels,
    AdapterError, CategoryValues, Heatmap, Line, NumericValues, Plot,
};

fn selected_line() -> Line<'static> {
    let x = array![[0.], [5.], [20.]];
    let seven = array![[4.], [5.], [6.]];
    let two = array![[1.], [2.], [3.]];
    let x_view = x.view();
    let seven_view = seven.view();
    let two_view = two.view();
    let entries: [(usize, &dyn ArraySource); 3] = [(4, &x_view), (7, &seven_view), (2, &two_view)];
    let identities = [(4, "Variable"), (7, "seven"), (2, "two")];
    let source = ColumnProjection::new((3, 10), &entries)
        .unwrap()
        .with_identities(&identities)
        .unwrap();
    let a = Line::from_data(source, 4usize, [7usize, 2usize])
        .unwrap()
        .labels(Labels::raw().alias("Variable", "Time"))
        .column_labels(["Right", "Left"])
        .unwrap();
    let (plot, metadata) = a.into_parts();
    Line::from_parts(plot, metadata)
}

#[test]
fn reordered_identity_context_binds_only_fresh_roles_without_renaming_old_values() {
    let (plot, metadata) = selected_line().into_parts();
    // The old numeric key "two" now occupies column 1 and contains categories.
    // None of the original x/y views or their value entries survive this call.
    let categories = array![["A"], ["B"], ["A"]];
    let categories_view = categories.view();
    let entries: [(usize, &dyn ArraySource); 1] = [(1, &categories_view)];
    let identities = [(1, "two")];
    let source = ColumnProjection::new((3, 11), &entries)
        .unwrap()
        .with_identities(&identities)
        .unwrap();
    let a = Line::from_parts_with_data(plot, metadata, source).unwrap();
    let repeated = a.category_rows(1usize, "hue").unwrap();
    assert_eq!(repeated.name(), "two");
    assert_eq!(repeated.len(), 6);
    let a = a.hue(1usize).unwrap();
    let (plot, metadata) = a.into_parts();

    // A genuinely new key then occupies the old x index 4.
    let categories = array![["B"], ["A"], ["B"]];
    let categories_view = categories.view();
    let entries: [(usize, &dyn ArraySource); 1] = [(4, &categories_view)];
    let identities = [(4, "new")];
    let source = ColumnProjection::new((3, 11), &entries)
        .unwrap()
        .with_identities(&identities)
        .unwrap();
    let a = Line::from_parts_with_data(plot, metadata, source).unwrap();
    let labels = a.bindings.labels.clone().alias("new", "New");
    let a = a.hue(4usize).unwrap().labels(labels);
    assert_eq!(a.bindings.labels.resolve("Variable").unwrap(), "Time");
    assert_eq!(
        a.bindings.expansion.as_ref().unwrap().keys,
        ["seven", "two"]
    );
    let expected = Plot::line(
        NumericValues::new("Variable", [0., 0., 5., 5., 20., 20.]),
        NumericValues::new("y", [4., 1., 5., 2., 6., 3.]),
    )
    .group(
        CategoryValues::new(
            "Variable_",
            ["seven", "two", "seven", "two", "seven", "two"],
        )
        .unwrap(),
    )
    .hue(CategoryValues::new("new", ["B", "B", "A", "A", "B", "B"]).unwrap())
    .labels(Labels::raw().aliases([
        ("Variable", "Time"),
        ("seven", "Right"),
        ("two", "Left"),
        ("new", "New"),
    ]));
    assert_eq!(
        crate::tests::render(&a.into(), "projection-fresh"),
        crate::tests::render(&expected, "projection-fresh-native")
    );
}

#[test]
fn independent_column_alpha_uses_the_plot_namespace_and_actual_named_keys() {
    let (plot, metadata) = selected_line().into_parts();
    let main = ColumnProjection::from_identities((3, 11), &[(1, "two"), (2, "seven")]).unwrap();
    let plot = Line::from_parts_with_data(plot, metadata, main).unwrap();
    let indices = array![[1usize], [2]];
    let quality = array![[0.25], [0.75]];
    let indices = indices.view();
    let quality = quality.view();
    let columns: [(usize, &dyn ArraySource); 2] = [(0, &indices), (1, &quality)];
    let supplier = ColumnProjection::new((2, 2), &columns)
        .unwrap()
        .with_identities(&[(0, "target"), (1, "quality")])
        .unwrap();
    let adapted = plot
        .line_alpha_columns_from_data(supplier, 0usize, [] as [(&str, usize); 0], 1usize)
        .unwrap();
    let (keys, values) = adapted.get_line_alpha().get_keyed().unwrap();
    assert_eq!(
        keys.columns()[0]
            .values
            .get_strings()
            .unwrap()
            .iter()
            .collect::<Vec<_>>(),
        [Some("two"), Some("seven")]
    );
    assert_eq!(values.values(), &[0.25, 0.75]);
    adapted.render().unwrap();
}

#[test]
fn grid_positions_and_per_cell_ids_keep_original_origins_and_two_dimensional_shape() {
    let values = array![[1, 2, 3], [4, 5, 6]];
    let ids = array![[101usize, 102, 103], [104, 105, 106]];
    let seven = values.slice(s![.., ..1]);
    let two = values.slice(s![.., 1..2]);
    let four = values.slice(s![.., 2..3]);
    let id9 = ids.slice(s![.., ..1]);
    let id8 = ids.slice(s![.., 1..2]);
    let id6 = ids.slice(s![.., 2..3]);
    let entries: [(usize, &dyn ArraySource); 6] = [
        (4, &four),
        (2, &two),
        (7, &seven),
        (9, &id9),
        (8, &id8),
        (6, &id6),
    ];
    let identities = [
        (4, "c"),
        (2, "b"),
        (7, "a"),
        (9, "id-a"),
        (8, "id-b"),
        (6, "id-c"),
    ];
    let source = ColumnProjection::new((2, 10), &entries)
        .unwrap()
        .with_identities(&identities)
        .unwrap();
    let a = Heatmap::from_data(source, [7, 2, 4]).unwrap();
    assert_eq!(a.bindings.grid.as_ref().unwrap().columns(), 3);
    for (key, display) in [
        ("__lichtung_grid_x_0", "Column 8"),
        ("__lichtung_grid_x_1", "Column 3"),
        ("__lichtung_grid_x_2", "Column 5"),
        ("__lichtung_grid_y_0", "Row 1"),
        ("__lichtung_grid_y_1", "Row 2"),
    ] {
        assert_eq!(a.bindings.labels.resolve(key).unwrap(), display);
        assert_eq!(
            a.clone()
                .labels(Labels::raw())
                .get_labels()
                .resolve(key)
                .unwrap(),
            display
        );
    }
    assert_eq!(
        [9, 8, 6]
            .raw_source_rows(a.source.as_deref(), "cell IDs")
            .unwrap(),
        [101, 102, 103, 104, 105, 106]
    );
    let wrong = Array2::from_elem((3, 2), false);
    assert!(matches!(
        a.clone().highlight_cells(wrong.view()),
        Err(AdapterError::Binding { detail, .. })
            if detail.contains("[7, 2, 4]") && detail.contains("(3, 2)")
    ));
    let wrong_ids = Array2::<usize>::zeros((3, 2));
    assert!(matches!(
        a.clone().source_rows(wrong_ids.view()),
        Err(AdapterError::Binding { detail, .. }) if detail.contains("(3, 2)")
    ));
    assert!(matches!(
        a.clone().source_rows(9usize),
        Err(AdapterError::Binding { detail, .. }) if detail.contains("not repeated row IDs")
    ));
    let a = a.source_rows([9, 8, 6]).unwrap();
    let highlights = ndarray::array![[true, false, false], [false, true, false]];
    a.clone()
        .highlight_cells(highlights.view())
        .unwrap()
        .y_labels(["Top", "Bottom"])
        .unwrap()
        .size(900, 600)
        .preflight()
        .unwrap();
    a.clone()
        .highlight_cells(ndarray::array![true, false, false, false, true, false].view())
        .unwrap()
        .size(900, 600)
        .preflight()
        .unwrap();
    a.plot.size(900, 600).preflight().unwrap();
}