Skip to content

Commit

Permalink
Add -k column selector. Remove serde, use StringRecord directly to ke…
Browse files Browse the repository at this point in the history
…ep performance.
  • Loading branch information
ahcm committed May 24, 2022
1 parent 11f2eb0 commit 395ccbe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hist-cli"
version = "0.4.3"
version = "0.4.4"
edition = "2021"
authors = ["Andreas Hauser <Andreas.Hauser@LMU.de>"]
description = "Commandline tool for plotting frequency ranked histograms of TSV/CSV data"
Expand All @@ -10,7 +10,6 @@ repository = "https://github.com/ahcm/hist-cli"

[dependencies]
plotters = "^0.3"
serde = { version = "^1.0", features = ["derive"] }
csv = "^1.1"
structopt = "^0.3"
textplots = "^0.8"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export PATH="$HOME/.cargo/bin:$PATH"

## Usage
```
hist 0.4.2
hist 0.4.4
Plots histogram of input
USAGE:
Expand All @@ -25,6 +25,7 @@ FLAGS:
OPTIONS:
-T, --Title <Title> optional title above the plot [default: Counts distribution]
-g, --geometry <geometry> the x and y size of the plot [default: 1280x960]
-k, --key <key> key (column) selector [default: 1]
-o, --output <output> file to save PNG plot to [default: histogram.png]
-s, --save <save> save counts data to file as TSV, use - for STDOUT
--xdesc <xdesc> x-axis label [default: Rank]
Expand Down
34 changes: 15 additions & 19 deletions src/bin/hist.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
extern crate plotters;
extern crate serde;
extern crate csv;
use csv::StringRecord;
use plotters::prelude::*;
use serde::Deserialize;
use std::io;
use std::collections::BTreeMap;
use structopt::StructOpt;
Expand All @@ -19,6 +18,10 @@ struct Opt
/// optional file with on entry per line [default: STDIN]
input: Option<PathBuf>,

#[structopt(long, short, default_value = "1")]
/// key (column) selector
key: usize,

#[structopt(parse(from_os_str), long, short, default_value = "histogram.png")]
/// file to save PNG plot to
output: PathBuf,
Expand Down Expand Up @@ -52,12 +55,6 @@ struct Opt
ydesc: String,
}

#[derive(Debug, Deserialize)]
struct Record
{
key: String,
}

fn main() -> Result<(), Box<dyn Error>>
{
let opt = Opt::from_args();
Expand All @@ -72,19 +69,18 @@ fn main() -> Result<(), Box<dyn Error>>
Box::new(io::stdin())
};

let key_counts =
csv::ReaderBuilder::new()
let mut reader = csv::ReaderBuilder::new()
.has_headers(false)
.delimiter(b'\t')
.from_reader(input)
.deserialize::<Record>()
.fold(BTreeMap::new(), |mut map,rec|
{
let s = rec.unwrap().key;
//*map.entry(s).or_insert(0) += 1;
map.entry(s).and_modify(|e| *e += 1 ).or_insert(1);
map
});
.from_reader(input);

let mut key_counts = BTreeMap::new();
let mut record = StringRecord::new();
while reader.read_record(&mut record)?
{
let s = record.get(opt.key - 1).unwrap().to_string();
key_counts.entry(s).and_modify(|e| *e += 1 ).or_insert(1);
}

if let Some(path) = &opt.save
{
Expand Down

0 comments on commit 395ccbe

Please sign in to comment.