Skip to content

Commit

Permalink
pass Option<kdam::Builder> to read_utils fns
Browse files Browse the repository at this point in the history
  • Loading branch information
robfitzgerald committed Dec 20, 2024
1 parent 11f3eaf commit 3e08804
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
},
util::fs::read_utils,
};
use kdam::Bar;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

Expand All @@ -30,8 +31,7 @@ impl NetworkCostRateBuilder {
let lookup = read_utils::from_csv::<NetworkTraversalUtilityRow>(
cost_input_file,
true,
Some("network edge cost lookup"),
None,
Some(Bar::builder().desc("network edge cost lookup")),
None,
)
.map_err(|source| {
Expand All @@ -49,8 +49,7 @@ impl NetworkCostRateBuilder {
let lookup = read_utils::from_csv::<NetworkAccessUtilityRow>(
cost_input_file,
true,
Some("network edge->edge cost lookup"),
None,
Some(Bar::builder().desc("network edge->edge cost lookup")),
None,
)
.map_err(|source| {
Expand Down
8 changes: 3 additions & 5 deletions rust/routee-compass-core/src/model/map/geometry_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
util::{fs::read_utils, geo::geo_io_utils},
};
use geo::LineString;
use kdam::BarExt;
use kdam::{Bar, BarExt};

/// model for link geometries by edge id. can be constructed either
/// from edge geometry dataset ([`GeometryModel::new_from_edges`]) or
Expand Down Expand Up @@ -50,8 +50,7 @@ fn read_linestrings(
let geoms = read_utils::read_raw_file(
geometry_input_file,
geo_io_utils::parse_wkt_linestring,
Some("link geometries"),
Some(n_edges),
Some(Bar::builder().desc("link geometries").total(n_edges)),
None,
)
.map_err(|e: std::io::Error| {
Expand Down Expand Up @@ -179,8 +178,7 @@ mod tests {

#[test]
fn test_geometry_deserialization() {
let result =
read_raw_file(mock_geometry_file(), parse_wkt_linestring, None, None, None).unwrap();
let result = read_raw_file(mock_geometry_file(), parse_wkt_linestring, None, None).unwrap();
assert_eq!(result.len(), 3);
}

Expand Down
20 changes: 15 additions & 5 deletions rust/routee-compass-core/src/model/network/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::util::compact_ordered_hash_map::CompactOrderedHashMap;
use crate::util::fs::read_utils;
use allocative::Allocative;
use itertools::Itertools;
use kdam::Bar;
use std::collections::HashSet;
use std::path::Path;

Expand Down Expand Up @@ -50,9 +51,13 @@ impl Graph {
edge_list_csv: &P,
vertex_list_csv: &P,
) -> Result<Graph, NetworkError> {
let vertices: Box<[Vertex]> =
read_utils::from_csv(&vertex_list_csv, true, Some("graph vertices"), None, None)
.map_err(|e| NetworkError::CsvError { source: e })?;
let vertices: Box<[Vertex]> = read_utils::from_csv(
&vertex_list_csv,
true,
Some(Bar::builder().desc("graph vertices")),
None,
)
.map_err(|e| NetworkError::CsvError { source: e })?;

let mut adj: Vec<CompactOrderedHashMap<EdgeId, VertexId>> =
vec![CompactOrderedHashMap::empty(); vertices.len()];
Expand All @@ -79,8 +84,13 @@ impl Graph {
}
});

let edges = read_utils::from_csv(&edge_list_csv, true, Some("graph edges"), None, Some(cb))
.map_err(|e| NetworkError::CsvError { source: e })?;
let edges = read_utils::from_csv(
&edge_list_csv,
true,
Some(Bar::builder().desc("graph edges")),
Some(cb),
)
.map_err(|e| NetworkError::CsvError { source: e })?;

let graph = Graph {
adj: adj.into_boxed_slice(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use kdam::Bar;

use crate::model::unit::DistanceUnit;
use crate::model::unit::{SpeedUnit, TimeUnit, BASE_DISTANCE_UNIT, BASE_TIME_UNIT};
use crate::util::fs::read_decoders;
Expand Down Expand Up @@ -25,8 +27,7 @@ impl SpeedTraversalEngine {
let speed_table: Box<[Speed]> = read_utils::read_raw_file(
speed_table_path,
read_decoders::default,
Some("link speeds"),
None,
Some(Bar::builder().desc("link speeds")),
None,
)
.map_err(|e| {
Expand Down
26 changes: 7 additions & 19 deletions rust/routee-compass-core/src/util/fs/read_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::fs_utils;
use csv::ReaderBuilder;
use flate2::read::GzDecoder;
use kdam::{Bar, BarExt};
use kdam::{BarBuilder, BarExt};

use std::{
fs::File,
Expand Down Expand Up @@ -52,19 +52,13 @@ where
pub fn from_csv<'a, T>(
filepath: &dyn AsRef<Path>,
has_headers: bool,
progress_message: Option<&str>,
progress_size: Option<usize>,
progress: Option<BarBuilder>,
callback: CsvCallback<'a, T>,
) -> Result<Box<[T]>, csv::Error>
where
T: serde::de::DeserializeOwned + 'a,
{
let bar_opt = match (progress_message, progress_size) {
(Some(desc), Some(total)) => Bar::builder().desc(desc).total(total).build().ok(),
(Some(desc), None) => Bar::builder().desc(desc).build().ok(),
(None, Some(total)) => Bar::builder().total(total).build().ok(),
_ => None,
};
let bar_opt = progress.and_then(|b| b.build().ok());

let row_callback: CsvCallback<'a, T> = match (callback, bar_opt) {
(None, None) => None,
Expand Down Expand Up @@ -95,19 +89,13 @@ where
pub fn read_raw_file<F, T>(
filepath: F,
op: impl Fn(usize, String) -> Result<T, io::Error>,
progress_message: Option<&str>,
progress_size: Option<usize>,
progress: Option<BarBuilder>,
callback: Option<Box<dyn FnMut()>>,
) -> Result<Box<[T]>, io::Error>
where
F: AsRef<Path>,
{
let bar_opt = match (progress_message, progress_size) {
(Some(desc), Some(total)) => Bar::builder().desc(desc).total(total).build().ok(),
(Some(desc), None) => Bar::builder().desc(desc).build().ok(),
(None, Some(total)) => Bar::builder().total(total).build().ok(),
_ => None,
};
let bar_opt = progress.and_then(|b| b.build().ok());

let row_callback: RawCallback = match (callback, bar_opt) {
(None, None) => None,
Expand Down Expand Up @@ -192,7 +180,7 @@ mod tests {
println!("loading file {:?}", filepath);
let bonus_word = " yay";
let op = |_idx: usize, row: String| Ok(row + bonus_word);
let result = read_raw_file(&filepath, op, None, None, None).unwrap();
let result = read_raw_file(&filepath, op, None, None).unwrap();
let expected = vec![
String::from("RouteE yay"),
String::from("FASTSim yay"),
Expand All @@ -217,7 +205,7 @@ mod tests {
println!("loading file {:?}", filepath);
let bonus_word = " yay";
let op = |_idx: usize, row: String| Ok(row + bonus_word);
let result = read_raw_file(&filepath, op, None, None, None).unwrap();
let result = read_raw_file(&filepath, op, None, None).unwrap();
let expected = vec![
String::from("RouteE yay"),
String::from("FASTSim yay"),
Expand Down
1 change: 1 addition & 0 deletions rust/routee-compass-powertrain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ smartcore = { version = "0.3.1", features = ["serde"] } # r
thiserror = { workspace = true }
log = { workspace = true }
geo = { workspace = true }
kdam = { workspace = true }
bincode = "1.3.3"
env_logger = { workspace = true }
serde = { workspace = true, features = ["derive"] }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::energy_traversal_model::EnergyTraversalModel;
use super::vehicle::VehicleType;
use kdam::Bar;
use routee_compass_core::model::traversal::traversal_model::TraversalModel;
use routee_compass_core::model::traversal::traversal_model_error::TraversalModelError;
use routee_compass_core::model::traversal::traversal_model_service::TraversalModelService;
Expand Down Expand Up @@ -40,8 +41,7 @@ impl EnergyModelService {
read_utils::read_raw_file(
gtp,
read_decoders::default,
Some("link grades"),
None,
Some(Bar::builder().desc("link grades")),
None,
)
.map_err(|e| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::app::compass::config::config_json_extension::ConfigJsonExtensions;
use kdam::Bar;
use routee_compass_core::{
model::access::{
access_model_builder::AccessModelBuilder,
Expand Down Expand Up @@ -32,8 +33,7 @@ impl AccessModelBuilder for TurnDelayAccessModelBuilder {
let edge_headings = read_utils::from_csv::<EdgeHeading>(
&file_path.as_path(),
true,
Some("edge headings"),
None,
Some(Bar::builder().desc("edge headings")),
None,
)
.map_err(|e| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::app::compass::config::{
compass_configuration_field::CompassConfigurationField,
config_json_extension::ConfigJsonExtensions,
};
use kdam::Bar;
use routee_compass_core::{
model::frontier::{
frontier_model_builder::FrontierModelBuilder, frontier_model_error::FrontierModelError,
Expand Down Expand Up @@ -35,8 +36,7 @@ impl FrontierModelBuilder for RoadClassBuilder {
let road_class_lookup: Box<[u8]> = read_utils::read_raw_file(
&road_class_file,
read_decoders::u8,
Some("road class"),
None,
Some(Bar::builder().desc("road class")),
None,
)
.map_err(|e| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::app::compass::config::{
compass_configuration_field::CompassConfigurationField,
config_json_extension::ConfigJsonExtensions,
};
use kdam::Bar;
use routee_compass_core::{
model::frontier::{
frontier_model_builder::FrontierModelBuilder, frontier_model_error::FrontierModelError,
Expand Down Expand Up @@ -36,8 +37,7 @@ impl FrontierModelBuilder for TurnRestrictionBuilder {
let restricted_edges: HashSet<RestrictedEdgePair> = read_utils::from_csv(
&turn_restriction_file,
true,
Some("turn restrictions"),
None,
Some(Bar::builder().desc("turn restrictions")),
None,
)
.map_err(|e| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::app::compass::config::{
compass_configuration_field::CompassConfigurationField,
config_json_extension::ConfigJsonExtensions,
};
use kdam::Bar;
use routee_compass_core::{
model::{
frontier::{
Expand Down Expand Up @@ -56,8 +57,7 @@ pub fn vehicle_restriction_lookup_from_file(
let rows: Vec<RestrictionRow> = read_utils::from_csv(
&vehicle_restriction_input_file,
true,
Some("vehicle restrictions"),
None,
Some(Bar::builder().desc("vehicle restrictions")),
None,
)
.map_err(|e| {
Expand Down
42 changes: 25 additions & 17 deletions rust/routee-compass/src/app/geom/geom_app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::app::compass::compass_app_error::CompassAppError;
use geo::LineString;
use kdam::Bar;
use routee_compass_core::util::fs::read_utils;
use routee_compass_core::util::geo::geo_io_utils::parse_wkt_linestring;
use std::io::ErrorKind;
Expand All @@ -25,14 +26,18 @@ impl TryFrom<&GeomAppConfig> for GeomApp {
Ok(result)
};

let geoms =
read_utils::read_raw_file(&conf.edge_file, op, Some("link geometries"), None, None)
.map_err(|e| {
CompassAppError::BuildFailure(format!(
"failure reading edge file {}: {}",
conf.edge_file, e
))
})?;
let geoms = read_utils::read_raw_file(
&conf.edge_file,
op,
Some(Bar::builder().desc("link geometries")),
None,
)
.map_err(|e| {
CompassAppError::BuildFailure(format!(
"failure reading edge file {}: {}",
conf.edge_file, e
))
})?;
eprintln!();
let app = GeomApp { geoms };
Ok(app)
Expand All @@ -56,15 +61,18 @@ impl GeomApp {
result
};

let result: Box<[LineString<f32>]> =
read_utils::read_raw_file(&file, op, Some("link geometries"), None, None).map_err(
|e| {
CompassAppError::BuildFailure(format!(
"failure reading linestring file {}: {}",
file, e
))
},
)?;
let result: Box<[LineString<f32>]> = read_utils::read_raw_file(
&file,
op,
Some(Bar::builder().desc("link geometries")),
None,
)
.map_err(|e| {
CompassAppError::BuildFailure(format!(
"failure reading linestring file {}: {}",
file, e
))
})?;
eprintln!();
Ok(result)
}
Expand Down
22 changes: 14 additions & 8 deletions rust/routee-compass/src/plugin/output/default/uuid/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::app::compass::compass_app_error::CompassAppError;
use crate::app::search::search_app_result::SearchAppResult;
use crate::plugin::output::default::uuid::output_json_extensions::UUIDJsonField;
use crate::plugin::output::{OutputPlugin, OutputPluginError};
use kdam::Bar;
use routee_compass_core::algorithm::search::search_instance::SearchInstance;
use routee_compass_core::util::fs::read_utils::read_raw_file;
use std::path::Path;
Expand All @@ -15,14 +16,19 @@ pub struct UUIDOutputPlugin {

impl UUIDOutputPlugin {
pub fn from_file<P: AsRef<Path>>(filename: &P) -> Result<UUIDOutputPlugin, OutputPluginError> {
let uuids = read_raw_file(filename, |_idx, row| Ok(row), Some("uuids"), None, None)
.map_err(|e| {
OutputPluginError::BuildFailed(format!(
"failure reading UUID file {}: {}",
filename.as_ref().to_str().unwrap_or_default(),
e
))
})?;
let uuids = read_raw_file(
filename,
|_idx, row| Ok(row),
Some(Bar::builder().desc("uuids")),
None,
)
.map_err(|e| {
OutputPluginError::BuildFailed(format!(
"failure reading UUID file {}: {}",
filename.as_ref().to_str().unwrap_or_default(),
e
))
})?;
eprintln!();

let o_key = UUIDJsonField::OriginVertexUUID.to_string();
Expand Down

0 comments on commit 3e08804

Please sign in to comment.