A lot of exploration of various things in rust
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Joshua Coles 2023-03-18 15:59:36 +00:00
parent a85e8e33ea
commit 2b20df273b
6 changed files with 1394 additions and 37 deletions

1344
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -49,6 +49,22 @@ kiddo = "0.2.5"
anyhow = "1.0.69"
itertools = "0.10.5"
svg = "0.13.0"
polars = { version = "0.27.2", features = [
"docs",
"zip_with",
"csv-file",
"temporal",
"fmt",
"dtype-slim",
"csv-file",
"lazy",
"nightly",
"performant"
] }
rayon = "1.7.0"
walkdir = "2.3.3"
parquet = { version = "35.0.0", features = ["serde"] }
colorous = "1.0.10"
[build-dependencies]
cbindgen = "0.24.3"

33
src/tools/analysis.rs Normal file
View File

@ -0,0 +1,33 @@
use crate::DataAnalysisCli;
use polars::prelude::*;
use polars::io::RowCount;
use rayon::prelude::*;
use walkdir::WalkDir;
pub fn main(cli: &DataAnalysisCli) {
let a: Vec<_> = WalkDir::new(&cli.sp_dir)
.into_iter()
.par_bridge()
.filter(|run_file| run_file.as_ref().unwrap().path().is_file())
.map(|run_file| {
let run_file = run_file.unwrap().path().to_path_buf();
let probability = run_file.parent().unwrap().file_name().unwrap().to_str().unwrap().parse::<f32>().unwrap();
let run_seed = run_file.file_stem().unwrap().to_str().unwrap();
LazyCsvReader::new(&run_file)
.has_header(true)
// Add N, done at this stage to only count within single CSV file
.with_row_count(Some(RowCount { name: String::from("N"), offset: 0 }))
.finish().unwrap()
.with_columns([
lit(probability).alias("probability"),
lit(run_seed).alias("run")
])
}).collect();
let p = concat(a, true, true).unwrap();
let p = p.collect().unwrap();
println!("{:?}", p);
}

View File

@ -7,6 +7,7 @@ use crate::system::Position;
pub mod boxcount;
pub mod render;
pub mod analysis;
pub fn read<T: Position>(path: &Path, format: OutputFormat) -> Vec<T> where T: DeserializeOwned {
match format {

View File

@ -8,6 +8,7 @@ use crate::cli::cli::OutputFormat;
use crate::system::model::HistoryLine;
use crate::system::spaces::square_grid::Grid2D;
use clap::Parser;
use colorous::Color;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use svg::Node;
@ -25,13 +26,13 @@ struct Args {
}
trait ToSvg {
fn to_svg(&self, size: i32) -> Box<dyn Node>;
fn to_svg(&self, size: i32, colour: Color) -> Box<dyn Node>;
}
impl ToSvg for Grid2D {
fn to_svg(&self, size: i32) -> Box<dyn Node> {
fn to_svg(&self, size: i32, colour: Color) -> Box<dyn Node> {
Box::new(Rectangle::new()
.set("fill", "rgb(0, 0, 0)")
.set("fill", format!("rgb({}, {}, {})", colour.r, colour.g, colour.b))
.set("width", size)
.set("height", size)
.set("x", self.x * size)
@ -40,7 +41,7 @@ impl ToSvg for Grid2D {
}
impl ToSvg for HexPosition {
fn to_svg(&self, size: i32) -> Box<dyn Node> {
fn to_svg(&self, size: i32, colour: Color) -> Box<dyn Node> {
let points = [
[25.045, 128.0], [256.0, 0.0], [486.955, 128.0], [486.955, 384.0], [256.0, 512.0], [25.045, 384.0]
];
@ -56,7 +57,7 @@ impl ToSvg for HexPosition {
let [x, y] = self.to_cartesian();
Box::new(Rectangle::new()
.set("fill", "rgb(0, 0, 0)")
.set("fill", format!("rgb({}, {}, {})", colour.r, colour.g, colour.b))
.set("x", x * size)
.set("y", y * size))
}
@ -83,8 +84,9 @@ pub(crate) fn main(args: &RenderCli) {
.set("y", -max_size)
);
for position in positions {
svg.append(position.to_svg(size));
for (n, position) in positions.iter().enumerate() {
let colour = if args.colour { colorous::VIRIDIS.eval_rational(n, positions.len()) } else { Color::default() };
svg.append(position.to_svg(size, colour));
}
svg::write(File::create(&args.output).unwrap(), &svg).unwrap();

View File

@ -2,16 +2,20 @@
#![feature(let_chains)]
use std::fs::File;
use std::ops::{Index, Mul};
use std::path::PathBuf;
use anyhow::Context;
use crate::cli::cli::OutputFormat;
use crate::system::model::HistoryLine;
use crate::system::spaces::square_grid::Grid2D;
use clap::{Parser, Command, Args, Subcommand};
use itertools::{concat, Itertools};
use polars::io::RowCount;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use svg::Node;
use svg::node::element::Rectangle;
use walkdir::WalkDir;
use crate::system::Position;
use crate::system::spaces::hexagonal::HexPosition;
@ -22,7 +26,8 @@ mod tools;
#[derive(Debug, Parser)]
enum ToolsCli {
Render(RenderCli),
BoxCount(BoxCountCli)
BoxCount(BoxCountCli),
DataAnalysis(DataAnalysisCli),
}
#[derive(clap::ValueEnum, Clone, Debug, Copy)]
@ -43,6 +48,9 @@ struct RenderCli {
#[arg(short, long, default_value_t = 800)]
image_size: u32,
#[arg(short, long, default_value_t = false)]
colour: bool,
}
#[derive(Debug, Args)]
@ -53,12 +61,21 @@ struct BoxCountCli {
output: PathBuf,
}
fn main() {
#[derive(Debug, Args)]
struct DataAnalysisCli {
sp_dir: PathBuf,
}
fn main() -> anyhow::Result<()> {
let args = ToolsCli::parse();
dbg!(&args);
match args {
ToolsCli::Render(cli) => tools::render::main(&cli),
ToolsCli::BoxCount(cli) => tools::boxcount::main(&cli),
ToolsCli::DataAnalysis(cli) => {
}
}
Ok(())
}