Skip to content

Visualisation of the Kmean generated cluster #5

Description

@ThomAub

Hello,
This is the code for the visulalisation of the workshop using rust and vega_lite_3.

Cargo.toml

[dependencies]
vega_lite_3 = {version = "0.1", features = ["show_vega"]}
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0.39"
derive_builder = "0.9.0"
showata =  "0.1.0"
failure = "0.1.5"
csv = "1.1.1"

main.rs

use csv;
use failure;
use serde::{Deserialize, Serialize};
use showata::Showable;
use std::path::Path;
use vega_lite_3::*;

#[derive(Serialize, Deserialize, Debug)]
pub struct Item {
    pub x: f64,
    pub y: f64,
    pub idx: usize,
}

macro_rules! build {
    ($s:expr ) => {
        $s.build().map_err(|s| failure::format_err!("{}", s))?
    };
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Read in the data using the csv crate
    // The data are organised this way:
    //
    // Point_ID ,Point_x,Point_y, Cluster_idx
    // 0, 9.887634 , 10.700742, 3
    // 1, 10.895053 , 9.602507, 3
    // ...
    // 1475, 1.959400, 13.254207, 1
    let mut rdr = csv::Reader::from_path(Path::new("clustered_points.csv"))?;
    let values = rdr
        .deserialize()
        .into_iter()
        .collect::<Result<Vec<Item>, csv::Error>>()?;
    // Create the plot
    let chart = build!(VegaliteBuilder::default()
        .title("Dataset Points")
        .width(800.0)
        .height(800.0)
        .description("Plot of the clustered dataset generated in by ndarray workshop.")
        .data(&values)
        .mark(Mark::Point)
        .encoding(build!(EncodingBuilder::default()
            .x(build!(XClassBuilder::default()
                .field("x")
                .def_type(StandardType::Quantitative)))
            .y(build!(YClassBuilder::default()
                .field("y")
                .def_type(StandardType::Quantitative)))
            .color(ValueDefWithConditionMarkPropFieldDefStringNull {
                aggregate: None,
                bin: None,
                condition: None,
                field: Some(Field::String("idx".to_string())),
                legend: None,
                scale: None,
                sort: None,
                time_unit: None,
                title: None,
                value: None,
                value_def_with_condition_mark_prop_field_def_string_null_type: None
            }))));
    chart.show()?;
    Ok(())
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions