Skip to content

Commit

Permalink
Make sil func
Browse files Browse the repository at this point in the history
  • Loading branch information
hkctkuy committed Dec 8, 2023
1 parent 89300a7 commit cc22f30
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
14 changes: 5 additions & 9 deletions casr/src/bin/casr-cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,10 @@ fn update_clusters(
};

// Make crashline deduplication
if !crashline.is_empty() && !unique_crashlines[number - 1].insert(crashline.to_string()) {
if dedup
&& !crashline.is_empty()
&& !unique_crashlines[number - 1].insert(crashline.to_string())
{
deduplicated += 1;
continue;
}
Expand Down Expand Up @@ -524,14 +527,7 @@ fn get_sil(dir: &Path, jobs: usize) -> Result<f64> {
// Calculate silhouette coefficient for each casrep
for i in 0..clusters.len() - 1 {
for num in 0..clusters[i].len() - 1 {
let sil = if clusters[i].len() != 1 {
let a = get_subcoef_a(num, &clusters[i]);
let b = get_subcoef_b(num, i, &clusters);
(b - a) / a.max(b)
} else {
0f64
};
sum += sil;
sum += sil_coef(num, i, &clusters);
}
}
Ok(sum / size as f64)
Expand Down
33 changes: 28 additions & 5 deletions libcasr/src/stacktrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ impl Cluster {
/// Get cluster diameter
pub fn diam(&mut self) -> f64 {
if self.diam.is_none() {
diam(&self.stacktraces)
} else {
self.diam.unwrap()
self.diam = Some(diam(&self.stacktraces));
}
self.diam.unwrap()
}
}

Expand Down Expand Up @@ -448,7 +447,7 @@ pub fn relation(
/// # Return value
///
/// "a" subcoefficient silhouette coefficient
pub fn get_subcoef_a(num: usize, stacktraces: &[Stacktrace]) -> f64 {
fn sil_subcoef_a(num: usize, stacktraces: &[Stacktrace]) -> f64 {
let mut sum = 0f64;
for i in 0..stacktraces.len() - 1 {
if i == num {
Expand All @@ -473,7 +472,7 @@ pub fn get_subcoef_a(num: usize, stacktraces: &[Stacktrace]) -> f64 {
/// # Return value
///
/// "b" subcoefficient silhouette coefficient
pub fn get_subcoef_b(num: usize, cl: usize, clusters: &[Vec<Stacktrace>]) -> f64 {
fn sil_subcoef_b(num: usize, cl: usize, clusters: &[Vec<Stacktrace>]) -> f64 {
let mut min = MAX;
for j in 0..clusters.len() - 1 {
if j == cl {
Expand All @@ -491,6 +490,30 @@ pub fn get_subcoef_b(num: usize, cl: usize, clusters: &[Vec<Stacktrace>]) -> f64
min
}

/// Get silhouette coefficient calculating for given stacktrace
/// Read more: https://en.wikipedia.org/wiki/Silhouette_(clustering)#Definition
///
/// # Arguments
///
/// * `num` - given stacktrace number
///
/// * `i` - cluster number of given stacktrace
///
/// * `clusters` - a vector of clusters represented as slice of `Stacktrace` structures
///
/// # Return value
///
/// "b" subcoefficient silhouette coefficient
pub 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);
(b - a) / a.max(b)
} else {
0f64
}
}

/// 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 cc22f30

Please sign in to comment.