better error messages when reading timeseries

This commit is contained in:
Daniel Bauer
2018-11-30 12:58:24 +01:00
parent 4aa3552f1d
commit c03be1723e
6 changed files with 73 additions and 6 deletions

View File

@@ -7,6 +7,7 @@ use std::io::{BufReader,BufWriter};
use k_B; use k_B;
use std::path::Path; use std::path::Path;
use super::errors::*; use super::errors::*;
use f64;
// Returns the path to path2 relative to path1 // Returns the path to path2 relative to path1
// path1: "path/to/file.dat" // path1: "path/to/file.dat"
@@ -112,7 +113,7 @@ fn is_in_hist_boundaries(values: &Vec<f64>, cfg: &Config) -> bool {
// parse a timeseries file into a histogram // parse a timeseries file into a histogram
fn read_window_file(window_file: &str, cfg: &Config) -> Result<Histogram> { fn read_window_file(window_file: &str, cfg: &Config) -> Result<Histogram> {
let f = File::open(window_file) let f = File::open(window_file)
.chain_err(|| format!("Failed to open sample data file {}", window_file))?; .chain_err(|| format!("Failed to open sample data file {}.", window_file))?;
let mut buf = BufReader::new(&f); let mut buf = BufReader::new(&f);
// total number of bins is the product of all dimensions length // total number of bins is the product of all dimensions length
@@ -126,7 +127,9 @@ fn read_window_file(window_file: &str, cfg: &Config) -> Result<Histogram> {
// read and parse each timeseries line // read and parse each timeseries line
let mut line = String::new(); let mut line = String::new();
let mut linecount = 0;
while buf.read_line(&mut line).chain_err(|| "Failed to read line")? > 0 { while buf.read_line(&mut line).chain_err(|| "Failed to read line")? > 0 {
linecount += 1;
// skip comments and empty lines // skip comments and empty lines
if line.starts_with("#") || line.starts_with("@") || line.len() == 0 { if line.starts_with("#") || line.starts_with("@") || line.len() == 0 {
line.clear(); line.clear();
@@ -134,12 +137,22 @@ fn read_window_file(window_file: &str, cfg: &Config) -> Result<Histogram> {
} }
{ {
let mut split = line.split_whitespace();
split.next(); // skip time/step column let split: Vec<&str> = line.split_whitespace().collect();
let values: Vec<f64> = (0..cfg.dimens).collect::<Vec<usize>>().iter().map(|_| { if split.len() < cfg.dimens+1 {
split.next().unwrap().parse::<f64>().unwrap() bail!(format!("Wrong number of columns in line {} of window file {}. Empty Line?.", linecount, window_file));
}).collect(); }
let mut values: Vec<f64> = vec![f64::NAN; cfg.dimens];
for i in 0..values.len() {
values[i] = split[i+1].parse::<f64>()
.chain_err(|| format!("Failed to parse line {} of window file {}.", linecount, window_file))?;
}
println!("{:?}", values);
// (1..cfg.dimens).collect::<Vec<usize>>().iter().map(|_| {
// split.next().unwrap().parse::<f64>().unwrap()
// }).collect();
if is_in_hist_boundaries(&values, cfg) { if is_in_hist_boundaries(&values, cfg) {
let bin_indeces = (0..cfg.dimens).map(|dimen: usize| { let bin_indeces = (0..cfg.dimens).map(|dimen: usize| {

View File

@@ -0,0 +1,11 @@
84.840004 -2.878293
84.860004 -2.827750
84.880004 -2.962375
84.900004
84.920004 -2.748589
84.940004 -2.726197
84.960004 -2.802982
84.980004 -2.771980
85.000004 -2.706482
85.020004 -2.776983
85.040004 -2.883180

View File

@@ -0,0 +1,11 @@
84.840004 -2.878293
84.860004 -2.827750
84.880004 -2.962375
84.900004 qwee
84.920004 -2.748589
84.940004 -2.726197
84.960004 -2.802982
84.980004 -2.771980
85.000004 -2.706482
85.020004 -2.776983
85.040004 -2.883180

View File

@@ -77,6 +77,36 @@ mod integration {
)); ));
} }
#[test]
fn unparseable_timeseries() {
let output = Command::new("./target/debug/wham")
.args(&["--bins", "100", "--min", "-3.0", "--max", "3.0", "-T", "300"])
.args(&["-f", "tests/metadata_unparseable3.dat"])
.args(&["-o", "/dev/null"])
.output()
.expect("failed to execute process");
let output = String::from_utf8_lossy(&output.stderr);
println!("{}", output);
assert!(output.to_string().contains(
"Failed to parse line"
));
}
#[test]
fn unparseable_timeseries_empty() {
let output = Command::new("./target/debug/wham")
.args(&["--bins", "100", "--min", "-3.0", "--max", "3.0", "-T", "300"])
.args(&["-f", "tests/metadata_unparseable4.dat"])
.args(&["-o", "/dev/null"])
.output()
.expect("failed to execute process");
let output = String::from_utf8_lossy(&output.stderr);
println!("{}", output);
assert!(output.to_string().contains(
"Wrong number of columns in line"
));
}
#[test] #[test]
fn wham_1d() {; fn wham_1d() {;
Command::new("./target/debug/wham") Command::new("./target/debug/wham")

View File

@@ -0,0 +1 @@
COLVAR_unparseable_2.xvg -3.0 100

View File

@@ -0,0 +1 @@
COLVAR_unparseable_1.xvg -3.0 100