Skip to content

Commit

Permalink
refactor: Fix cohen's D to d (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHedmad committed Feb 12, 2024
1 parent 1fb2a3f commit 261c27c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Fast Cohen's D calculator
# Fast Cohen's d calculator
![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/mrhedmad/fast-cohen/rust.yml)

A fast implementation of a Cohen's D calculator.
A fast implementation of a Cohen's d calculator.

## Installation
You will need [cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html) installed.
Expand All @@ -23,7 +23,7 @@ fast-cohen "control_samples" "case_samples" "output_csv"

You can use `fast-cohen --help` to see the help message:
```
Calculate cohen's D of expression values.
Calculate cohen's d of expression values.
Usage: fast-cohen [OPTIONS] <CASE_EXPRESSION_MATRIX> <CONTROL_EXPRESSION_MATRIX> <OUTPUT_PATH>
Expand All @@ -37,7 +37,7 @@ Options:
-h, --help
```

The output csv will have a `row_names` column with the row names, and a `cohen_d` column with the Cohen's D values.
The output csv will have a `row_names` column with the row names, and a `cohen_d` column with the Cohen's d values.

NOTE: The order of the samples in the two input files MUST be the same between the two input files.
If you need to sort the rows, and the first column is the sample names, you can use [`xsv`](https://github.com/BurntSushi/xsv) to sort the columns with the following command:
Expand Down
27 changes: 14 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::path::PathBuf;
#[derive(Parser, Debug)]
#[command(
author = "Luca Visentin",
about = "Calculate cohen's D of expression values."
about = "Calculate cohen's d of expression values."
)]
struct Args {
/// Path to the expression matrix with the 'case' expression matrix
Expand Down Expand Up @@ -86,7 +86,7 @@ where
return ();
};

println!("Computing cohen's D...");
println!("Computing cohen's d...");
let result: Vec<f64> = case_samples
.records()
.zip(control_samples.records())
Expand Down Expand Up @@ -164,7 +164,7 @@ where
variance
}

/// Calculate cohen's D statistic from a case and control numeric vectors.
/// Calculate cohen's d statistic from a case and control numeric vectors.
fn cohen<T>(case: Vec<T>, control: Vec<T>) -> T
where
T: Float + std::iter::Sum,
Expand All @@ -184,21 +184,22 @@ where
(mean(case).unwrap() - mean(control).unwrap()) / pooled_var
}

fn kinda_equal<T, G>(a: T, b: T, tolerance: G) -> bool
where
T: Sub + PartialEq,
G: PartialEq,
<T as Sub>::Output: PartialOrd<G>,
<T as Sub>::Output: Signed,
{
abs(a - b) < tolerance
}

#[cfg(test)]
mod tests {
use crate::*;
use csv::{ReaderBuilder, WriterBuilder};
use std::io::Cursor;

fn kinda_equal<T, G>(a: T, b: T, tolerance: G) -> bool
where
T: Sub + PartialEq,
G: PartialEq,
<T as Sub>::Output: PartialOrd<G>,
<T as Sub>::Output: Signed,
{
abs(a - b) < tolerance
}

#[test]
fn mean_of_values() {
assert_eq!(mean(vec![1., 2., 3.]).unwrap(), 2.);
Expand Down

0 comments on commit 261c27c

Please # to comment.