Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reorganize pca-analysis #41

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions collector/src/mir_analyze/data/table_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl<X: Debug + Clone + Ord, Y: Debug + Clone + Ord + Display, T: Debug + Copy +
}
}

fn into_vec<X: Clone + Ord, Y: Clone + Ord, T: Copy + Into<f64>>(
pub fn into_vec<X: Clone + Ord, Y: Clone + Ord, T: Copy + Into<f64>>(
table_data: &TableDatas<X, Y, T>,
) -> Vec<f64> {
let mut data = vec![];
Expand All @@ -252,7 +252,7 @@ fn into_vec<X: Clone + Ord, Y: Clone + Ord, T: Copy + Into<f64>>(
}

/// Sort TableData by axis X and Y.
fn sort<X: Ord + Clone, Y: Ord + Clone, T: Clone>(
pub fn sort<X: Ord + Clone, Y: Ord + Clone, T: Clone>(
table_data: &TableDatas<X, Y, T>,
) -> Vec<(X, Vec<(Y, T)>)> {
let mut data_sorted = table_data
Expand Down
39 changes: 1 addition & 38 deletions collector/src/pca_analysis/plotter/coordinate_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ fn draw(
mod coordinate_map_test {
use std::{collections::HashMap, fs, path::PathBuf};

use nalgebra::DVector;

use crate::mir_analyze::data::table_data::{generate_benchmark_data, TableDatas};

use super::{draw_coordinate_map_2d, get_coordinate_2d, get_principle_components};
use super::{draw_coordinate_map_2d, get_principle_components};

fn generate_table_data() -> TableDatas<String, String, i32> {
vec![
Expand Down Expand Up @@ -149,41 +147,6 @@ mod coordinate_map_test {
.collect::<HashMap<String, HashMap<String, i32>>>()
}

/// A test for `get_coordinate_2d`.
///
/// Step1. Create datas and features.
///
/// Step2. Calculate coordinate of each data.
///
/// Step3. Verify the result.
#[test]
fn test_get_coordinate_2d() {
let datas = vec![
vec![1.0, 4.0, 7.0],
vec![2.0, 5.0, 8.0],
vec![3.0, 6.0, 9.0],
];
let feature_x = DVector::from_vec(vec![
0.8164965809277261,
-0.4082482904638633,
-0.408248290463863,
]);
let feature_y = DVector::from_vec(vec![0.0, -0.7071067811865475, 0.7071067811865477]);
let mut results = vec![
(-3.674234614174768, 2.1213203435596437),
(-3.674234614174768, 2.121320343559644),
(-3.6742346141747686, 2.1213203435596446),
]
.into_iter();

datas.into_iter().for_each(|d| {
assert_eq!(
get_coordinate_2d(&DVector::from(d), &feature_x, &feature_y),
results.next().unwrap()
);
});
}

/// A test for `draw_coordinate_map_2d`.
///
/// Step1. Draw PCA charts.
Expand Down
1 change: 1 addition & 0 deletions data_manage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ edition = "2021"
anyhow = "1"
collector = { path = "../collector" }
clap = { version = "3.0.9", features = ["derive"] }
nalgebra = "0.32.4"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
21 changes: 17 additions & 4 deletions data_manage/src/commannds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ pub enum Commands {
/// The path of output file.
#[clap(long = "out-path")]
out_path: PathBuf,
/// Metrics need to be merged.
/// Metrics need to be merged. Use ',' to concanate the metrics.
#[clap(long = "old-metrics")]
old_metrics: Vec<String>,
old_metrics: String,
/// New metric merged from old-metrics.
#[clap(long = "merged-metric")]
merged_metric: String,
Expand All @@ -116,8 +116,21 @@ pub enum Commands {
/// The path of output file.
#[clap(long = "out-path")]
out_path: PathBuf,
/// Metrics merged from stats fmt file.
/// Metrics merged from stats fmt file. Use ',' to concanate the metrics.
#[clap(long = "new_metrics")]
new_metrics: Vec<String>,
new_metrics: String,
},

/// Do pca analysis on a table fmt file.
PcaAnalysis {
/// The path of table data fmt file.
#[clap(long = "table-data")]
table_data_path: PathBuf,
/// The path of output dir.
#[clap(long = "out-dir")]
out_dir: PathBuf,
/// The maximun number principle components.
#[clap(long = "max-component-num")]
max_component_num: u32,
},
}
14 changes: 12 additions & 2 deletions data_manage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use clap::Parser;
use commannds::Cli;
use compare_stats::{compare_stat::compare_stat, compare_stat_2d::compare_stat_2d};
use merge_stats::{merge_runtime_stat::merge_runtime_stats, merge_stat::merge_compile_time_stats};
use pca_analysis::entry::pca_entry;
use table_data::merge_metrics::{
merge_metrics_from_compile_time_stats_to_table_data, merge_metrics_on_table_data,
};

mod commannds;
mod compare_stats;
mod merge_stats;
mod pca_analysis;
mod table_data;

fn main() {
Expand Down Expand Up @@ -59,7 +61,7 @@ fn main() {
} => match merge_metrics_on_table_data(
&table_data_path,
&out_path,
&old_metrics,
&old_metrics.split(',').map(|s| s.to_string()).collect(),
&merged_metric,
) {
Ok(p) => println!("Write merged table data to {}", p.to_str().unwrap()),
Expand All @@ -74,10 +76,18 @@ fn main() {
&table_data_path,
&stats_path,
out_path.as_path(),
new_metrics,
new_metrics.split(',').map(|s| s.to_string()).collect(),
) {
Ok(p) => println!("Write merged table data to {}", p.to_str().unwrap()),
Err(e) => eprintln!("{}", e),
},
commannds::Commands::PcaAnalysis {
table_data_path,
out_dir,
max_component_num,
} => match pca_entry(&table_data_path, out_dir, max_component_num) {
Ok(p) => println!("Write Pca analysis result to {}", p.to_str().unwrap()),
Err(e) => eprintln!("{}", e),
},
}
}
51 changes: 51 additions & 0 deletions data_manage/src/pca_analysis/entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::{
fs::{create_dir_all, File},
io::BufReader,
path::PathBuf,
};

use collector::mir_analyze::data::table_data::{sort, TableDatas};

use super::{pca_data::get_principle_components, plotter::coordinate_map::draw_coordinate_map_2d};

pub fn pca_entry(
table_data_path: &PathBuf,
out_dir: PathBuf,
max_component_num: u32,
) -> anyhow::Result<PathBuf> {
let table_data: TableDatas<String, String, f64> =
serde_json::from_reader(BufReader::new(File::open(table_data_path)?))?;

let pc = get_principle_components(&table_data, max_component_num);
display_pc(&pc, &table_data);

draw_coordinate_map_2d(&pc, &table_data, &out_dir)?;

create_dir_all(&out_dir)?;

Ok(out_dir)
}

pub(super) fn display_pc(pcs: &Vec<Vec<f64>>, table_data: &TableDatas<String, String, f64>) {
let labels = sort(table_data)
.into_iter()
.map(|(s, _)| s)
.collect::<Vec<String>>()
.join("\t");

let mut k = 0;
println!("=====================================================");
println!("{}", labels);
for pc in pcs {
k += 1;
println!(
"PC{}: {}",
k,
pc.into_iter()
.map(|p| format!("{:.2}", p))
.collect::<Vec<String>>()
.join("\t")
);
}
println!("=====================================================");
}
3 changes: 3 additions & 0 deletions data_manage/src/pca_analysis/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub(super) mod entry;
mod pca_data;
mod plotter;
Loading
Loading