Skip to content

Commit

Permalink
Merge pull request #3 from solaoi/feature_correlation-coefficient
Browse files Browse the repository at this point in the history
add correlation-coefficient
  • Loading branch information
solaoi authored Mar 27, 2022
2 parents 3bf1ebe + 170967b commit 444e9a2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ This command depends these commands(`head|tail|cut|sort|awk`) on bash.

```
colc [column] [file.csv|tsv|txt]
*1
When you set [column,column] on the column parameter,
you'll get the correlation coefficient of two columns.
(thie feature don't support below options)
```

## Option
Expand Down Expand Up @@ -48,7 +53,7 @@ you can download a binary release
```sh
# Install with wget or curl
## set the latest version on releases.
VERSION=v1.0.14
VERSION=v1.0.15
## case you use wget
wget https://github.com/solaoi/colc/releases/download/$VERSION/colc_linux_amd64.tar.gz
## case you use curl
Expand Down
43 changes: 42 additions & 1 deletion colc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ import { parse } from "https://deno.land/[email protected]/flags/mod.ts";

const { _, binsize, b, filter, f, check, c } = parse(Deno.args);
const [column, filename] = _;
if (typeof column !== "number" || typeof filename !== "string") {
const hasTwoColumn = (() => {
if (typeof column !== "string") return false;
if (column.indexOf(",") === -1) return false;
const columns = column.split(",");
if (columns.length !== 2) return false;
return Number.isInteger(parseInt(columns[0])) &&
Number.isInteger(parseInt(columns[1]));
})();

if (
(typeof column !== "number" && !hasTwoColumn) || typeof filename !== "string"
) {
console.log("Usage:\n colc [column] [file.csv|tsv|txt]");
Deno.exit(1);
}
Expand All @@ -35,6 +46,36 @@ const headerName = await runner.run(
);
const hasHeader = headerName !== "";

if (hasTwoColumn) {
const statsCommand = (() => {
const bash = [];
if (hasHeader) {
bash.push(
`tail -n +2 ${filename} | cut -f${column} ${isCsv ? "-d, " : ""}`,
);
} else {
bash.push(
`cut -f${column} ${isCsv ? "-d, " : ""}${filename} `,
);
}
bash.push("| awk");
bash.push(
`'BEGIN{OFMT="%.6f"}{split($1,col,",");asum+=col[1];a[NR]=col[1];bsum+=col[2];b[NR]=col[2]}END{amean=asum/NR;bmean=bsum/NR;for(i in a){as+=(a[i]-amean)^2;bs+=(b[i]-bmean)^2;sum+=(a[i]-amean)*(b[i]-bmean)};astddev=sqrt(as/NR);bstddev=sqrt(bs/NR);print sum/NR/astddev/bstddev}'`,
);
return bash.join(" ");
})();
const correlationCoefficient = await runner
.run(statsCommand);
const { println, showHeader, hr } = formatter(
24,
correlationCoefficient.length,
);
hasHeader ? showHeader(headerName) : hr();
println("correlation-coefficient", correlationCoefficient);
hr();
Deno.exit(0);
}

if (hasCheck) {
const checkCommand = (() => {
const bash = [];
Expand Down

0 comments on commit 444e9a2

Please sign in to comment.