Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hkctkuy committed Jan 10, 2024
1 parent 3e7857d commit 8110daf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
18 changes: 5 additions & 13 deletions casr/src/bin/casr-cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ fn merge_clusters(
/// # Return value
///
/// Silhouette coefficient
fn avg_sil(dir: &Path, jobs: usize) -> Result<f64> {
fn calc_avg_sil(dir: &Path, jobs: usize) -> Result<f64> {
// Get cluster dirs
let mut dirs: Vec<PathBuf> = fs::read_dir(dir)
.unwrap()
Expand Down Expand Up @@ -538,16 +538,8 @@ fn avg_sil(dir: &Path, jobs: usize) -> Result<f64> {
if size == 0 {
bail!("{} valid reports, nothing to calculate...", size);

Check warning on line 539 in casr/src/bin/casr-cluster.rs

View check run for this annotation

Codecov / codecov/patch

casr/src/bin/casr-cluster.rs#L539

Added line #L539 was not covered by tests
}
// Init sil sum
let mut sum = 0f64;
// Calculate silhouette coefficient for each casrep
for i in 0..clusters.len() {
for num in 0..clusters[i].len() {
let sil = sil_coef(num, i, &clusters);
sum += sil;
}
}
Ok(sum / size as f64)
let avg_sil = avg_sil_ceof(&clusters, size);
Ok(avg_sil)
}

fn main() -> Result<()> {
Expand Down Expand Up @@ -781,11 +773,11 @@ fn main() -> Result<()> {
if before != after {
println!("Number of reports after crashline deduplication in new clusters: {after}");
}
let sil = avg_sil(paths[1], jobs)?;
let sil = calc_avg_sil(paths[1], jobs)?;
println!("Cluster silhouette score: {sil}");
} else if matches.contains_id("estimate") {
let path: &PathBuf = matches.get_one::<PathBuf>("estimate").unwrap();
let sil = avg_sil(path, jobs)?;
let sil = calc_avg_sil(path, jobs)?;
println!("Cluster silhouette score: {sil}");

Check warning on line 781 in casr/src/bin/casr-cluster.rs

View check run for this annotation

Codecov / codecov/patch

casr/src/bin/casr-cluster.rs#L781

Added line #L781 was not covered by tests
}

Expand Down
27 changes: 26 additions & 1 deletion libcasr/src/stacktrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ fn sil_subcoef_b(num: usize, i: usize, clusters: &[Vec<Stacktrace>]) -> f64 {
/// # Return value
///
/// Silhouette coefficient
pub fn sil_coef(num: usize, i: usize, clusters: &[Vec<Stacktrace>]) -> f64 {
fn sil_coef(num: usize, i: usize, clusters: &[Vec<Stacktrace>]) -> f64 {
if clusters[i].len() != 1 {
let a = sil_subcoef_a(num, &clusters[i]);
let b = sil_subcoef_b(num, i, clusters);
Expand All @@ -652,6 +652,31 @@ pub fn sil_coef(num: usize, i: usize, clusters: &[Vec<Stacktrace>]) -> f64 {
}
}

/// Get average silhouette coefficient calculating for given stacktraces
/// Read more: https://en.wikipedia.org/wiki/Silhouette_(clustering)#Definition
///
/// # Arguments
///
/// * `clusters` - a vector of clusters represented as slice of `Stacktrace` structures
///
/// * `size` - total amount of elements in clusters
///
/// # Return value
///
/// Average silhouette coefficient
pub fn avg_sil_ceof(clusters: &[Vec<Stacktrace>], size: usize) -> f64 {
// Init sil sum
let mut sum = 0f64;
// Calculate silhouette coefficient for each casrep
for i in 0..clusters.len() {
for num in 0..clusters[i].len() {
let sil = sil_coef(num, i, clusters);
sum += sil;
}
}
sum / size as f64
}

/// Stack trace filtering trait.
pub trait Filter {
/// Filter frames from the stack trace that are not related to analyzed code containing crash.
Expand Down

0 comments on commit 8110daf

Please sign in to comment.