Skip to content

Commit

Permalink
Merge pull request #123 from NREL/f2/to-rust-debug
Browse files Browse the repository at this point in the history
Add `skip_init` option to SerdeAPI from_* methods
  • Loading branch information
calbaker authored May 7, 2024
2 parents c09da0f + 9f72a54 commit b6db185
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 90 deletions.
2 changes: 1 addition & 1 deletion python/fastsim/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ def copy_vehicle(veh: Vehicle, return_type: str = None, deep: bool = True) -> Di
veh_dict = {key: to_native_type(veh_dict[key]) for key in veh_dict if key not in [
"large_baseline_eff", "small_baseline_eff"]}
veh_yaml = yaml.dump(veh_dict)
v = RustVehicle.from_yaml(veh_yaml)
v = RustVehicle.from_yaml(veh_yaml, skip_init=True)
v.scenario_name = veh.scenario_name
v.selection = veh.selection
v.veh_year = veh.veh_year
Expand Down
14 changes: 7 additions & 7 deletions rust/fastsim-cli/src/bin/fastsim-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ pub fn main() -> anyhow::Result<()> {
anyhow::bail!("Need to provide coastdown test coefficients for drag and wheel rr coefficient calculation");
}
} else {
RustCycle::from_file(&cyc_file_path)
RustCycle::from_file(&cyc_file_path, false)
}
} else if is_adopt_hd && adopt_hd_has_cycle {
RustCycle::from_file(adopt_hd_string)
RustCycle::from_file(adopt_hd_string, false)
} else {
//TODO? use pathbuff to string, for robustness
Ok(RustCycle {
Expand All @@ -248,11 +248,11 @@ pub fn main() -> anyhow::Result<()> {
let (veh_string, pwr_out_perc, h2share) = json_rewrite(veh_string)?;
hd_h2_diesel_ice_h2share = h2share;
fc_pwr_out_perc = pwr_out_perc;
let mut veh = RustVehicle::from_json(&veh_string)?;
let mut veh = RustVehicle::from_json(&veh_string, false)?;
veh.set_derived()?;
Ok(veh)
} else {
let mut veh = RustVehicle::from_json(&veh_string)?;
let mut veh = RustVehicle::from_json(&veh_string, false)?;
veh.set_derived()?;
Ok(veh)
}
Expand All @@ -262,11 +262,11 @@ pub fn main() -> anyhow::Result<()> {
let (veh_string, pwr_out_perc, h2share) = json_rewrite(veh_string)?;
hd_h2_diesel_ice_h2share = h2share;
fc_pwr_out_perc = pwr_out_perc;
let mut veh = RustVehicle::from_json(&veh_string)?;
let mut veh = RustVehicle::from_json(&veh_string, false)?;
veh.set_derived()?;
Ok(veh)
} else {
RustVehicle::from_file(&veh_file_path)
RustVehicle::from_file(&veh_file_path, false)
}
} else {
Ok(RustVehicle::mock_vehicle())
Expand All @@ -288,7 +288,7 @@ pub fn main() -> anyhow::Result<()> {
let cyc = if adopt_hd_has_cycle {
cyc
} else {
RustCycle::from_resource("cycles/HHDDTCruiseSmooth.csv")?
RustCycle::from_resource("cycles/HHDDTCruiseSmooth.csv", false)?
};
let mut sim_drive = RustSimDrive::new(cyc, veh.clone());
sim_drive.sim_drive(None, None)?;
Expand Down
24 changes: 12 additions & 12 deletions rust/fastsim-core/fastsim-proc-macros/src/add_pyo3_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
#[cfg(feature = "resources")]
#[staticmethod]
#[pyo3(name = "from_resource")]
pub fn from_resource_py(filepath: &PyAny) -> PyResult<Self> {
Self::from_resource(PathBuf::extract(filepath)?).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
pub fn from_resource_py(filepath: &PyAny, skip_init: Option<bool>) -> PyResult<Self> {
Self::from_resource(PathBuf::extract(filepath)?, skip_init.unwrap_or_default()).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
}

/// Write (serialize) an object to a file.
Expand All @@ -242,8 +242,8 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
///
#[staticmethod]
#[pyo3(name = "from_file")]
pub fn from_file_py(filepath: &PyAny) -> PyResult<Self> {
Self::from_file(PathBuf::extract(filepath)?).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
pub fn from_file_py(filepath: &PyAny, skip_init: Option<bool>) -> PyResult<Self> {
Self::from_file(PathBuf::extract(filepath)?, skip_init.unwrap_or_default()).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
}

/// Write (serialize) an object into a string
Expand All @@ -266,8 +266,8 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
///
#[staticmethod]
#[pyo3(name = "from_str")]
pub fn from_str_py(contents: &str, format: &str) -> PyResult<Self> {
Self::from_str(contents, format).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
pub fn from_str_py(contents: &str, format: &str, skip_init: Option<bool>) -> PyResult<Self> {
Self::from_str(contents, format, skip_init.unwrap_or_default()).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
}

/// Write (serialize) an object to a JSON string
Expand All @@ -284,8 +284,8 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
///
#[staticmethod]
#[pyo3(name = "from_json")]
pub fn from_json_py(json_str: &str) -> PyResult<Self> {
Self::from_json(json_str).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
pub fn from_json_py(json_str: &str, skip_init: Option<bool>) -> PyResult<Self> {
Self::from_json(json_str, skip_init.unwrap_or_default()).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
}

/// Write (serialize) an object to a YAML string
Expand All @@ -302,8 +302,8 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
///
#[staticmethod]
#[pyo3(name = "from_yaml")]
pub fn from_yaml_py(yaml_str: &str) -> PyResult<Self> {
Self::from_yaml(yaml_str).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
pub fn from_yaml_py(yaml_str: &str, skip_init: Option<bool>) -> PyResult<Self> {
Self::from_yaml(yaml_str, skip_init.unwrap_or_default()).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
}

/// Write (serialize) an object to bincode-encoded `bytes`
Expand All @@ -322,8 +322,8 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
#[cfg(feature = "bincode")]
#[staticmethod]
#[pyo3(name = "from_bincode")]
pub fn from_bincode_py(encoded: &PyBytes) -> PyResult<Self> {
Self::from_bincode(encoded.as_bytes()).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
pub fn from_bincode_py(encoded: &PyBytes, skip_init: Option<bool>) -> PyResult<Self> {
Self::from_bincode(encoded.as_bytes(), skip_init.unwrap_or_default()).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
}
});

Expand Down
36 changes: 20 additions & 16 deletions rust/fastsim-core/src/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,16 +487,16 @@ impl RustCycleCache {
#[staticmethod]
#[pyo3(name = "from_csv")]
pub fn from_csv_py(filepath: &PyAny) -> anyhow::Result<Self> {
Self::from_csv_file(PathBuf::extract(filepath)?)
pub fn from_csv_py(filepath: &PyAny, skip_init: Option<bool>) -> anyhow::Result<Self> {
Self::from_csv_file(PathBuf::extract(filepath)?, skip_init.unwrap_or_default())
}
pub fn to_rust(&self) -> Self {
self.clone()
}
#[staticmethod]
pub fn from_dict(dict: &PyDict) -> anyhow::Result<Self> {
pub fn from_dict(dict: &PyDict, skip_init: Option<bool>) -> anyhow::Result<Self> {
let time_s = Array::from_vec(PyAny::get_item(dict, "time_s")?.extract()?);
let cyc_len = time_s.len();
let mut cyc = Self {
Expand All @@ -515,7 +515,9 @@ impl RustCycleCache {
name: PyAny::get_item(dict, "name").and_then(String::extract).unwrap_or_default(),
orphaned: false,
};
cyc.init()?;
if !skip_init.unwrap_or_default() {
cyc.init()?;
}
Ok(cyc)
}
Expand Down Expand Up @@ -686,12 +688,12 @@ impl SerdeAPI for RustCycle {

/// Note that using this method to instantiate a RustCycle from CSV, rather
/// than the `from_csv_str` method, sets the cycle name to an empty string
fn from_str<S: AsRef<str>>(contents: S, format: &str) -> anyhow::Result<Self> {
fn from_str<S: AsRef<str>>(contents: S, format: &str, skip_init: bool) -> anyhow::Result<Self> {
Ok(
match format.trim_start_matches('.').to_lowercase().as_str() {
"yaml" | "yml" => Self::from_yaml(contents)?,
"json" => Self::from_json(contents)?,
"csv" => Self::from_reader(contents.as_ref().as_bytes(), "csv")?,
"yaml" | "yml" => Self::from_yaml(contents, skip_init)?,
"json" => Self::from_json(contents, skip_init)?,
"csv" => Self::from_reader(contents.as_ref().as_bytes(), "csv", skip_init)?,
_ => bail!(
"Unsupported format {format:?}, must be one of {:?}",
Self::ACCEPTED_STR_FORMATS
Expand All @@ -700,7 +702,7 @@ impl SerdeAPI for RustCycle {
)
}

fn from_reader<R: std::io::Read>(rdr: R, format: &str) -> anyhow::Result<Self> {
fn from_reader<R: std::io::Read>(rdr: R, format: &str, skip_init: bool) -> anyhow::Result<Self> {
let mut deserialized = match format.trim_start_matches('.').to_lowercase().as_str() {
"yaml" | "yml" => serde_yaml::from_reader(rdr)?,
"json" => serde_json::from_reader(rdr)?,
Expand All @@ -722,7 +724,9 @@ impl SerdeAPI for RustCycle {
)
}
};
deserialized.init()?;
if !skip_init {
deserialized.init()?;
}
Ok(deserialized)
}
}
Expand Down Expand Up @@ -794,21 +798,21 @@ impl RustCycle {
}

/// Load cycle from CSV file, parsing name from filepath
pub fn from_csv_file<P: AsRef<Path>>(filepath: P) -> anyhow::Result<Self> {
pub fn from_csv_file<P: AsRef<Path>>(filepath: P, skip_init: bool) -> anyhow::Result<Self> {
let filepath = filepath.as_ref();
let name = filepath
.file_stem()
.and_then(OsStr::to_str)
.with_context(|| format!("Could not parse cycle name from filepath: {filepath:?}"))?
.to_string();
let mut cyc = Self::from_file(filepath)?;
let mut cyc = Self::from_file(filepath, skip_init)?;
cyc.name = name;
Ok(cyc)
}

/// Load cycle from CSV string
pub fn from_csv_str<S: AsRef<str>>(csv_str: S, name: String) -> anyhow::Result<Self> {
let mut cyc = Self::from_str(csv_str, "csv")?;
pub fn from_csv_str<S: AsRef<str>>(csv_str: S, name: String, skip_init: bool) -> anyhow::Result<Self> {
let mut cyc = Self::from_str(csv_str, "csv", skip_init)?;
cyc.name = name;
Ok(cyc)
}
Expand Down Expand Up @@ -1238,7 +1242,7 @@ mod tests {
fn test_loading_a_cycle_from_the_filesystem() {
let cyc_file_path = resources_path().join("cycles/udds.csv");
let expected_udds_length = 1370;
let cyc = RustCycle::from_csv_file(cyc_file_path).unwrap();
let cyc = RustCycle::from_csv_file(cyc_file_path, false).unwrap();
let num_entries = cyc.len();
assert_eq!(cyc.name, String::from("udds"));
assert!(num_entries > 0);
Expand All @@ -1254,7 +1258,7 @@ mod tests {
let cyc = RustCycle::test_cyc();
for format in RustCycle::ACCEPTED_STR_FORMATS {
let csv_str = cyc.to_str(format).unwrap();
RustCycle::from_str(&csv_str, format).unwrap();
RustCycle::from_str(&csv_str, format, false).unwrap();
}
}
}
2 changes: 1 addition & 1 deletion rust/fastsim-core/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl SerdeAPI for AdjCoef {}
impl Default for RustLongParams {
fn default() -> Self {
let long_params_str = include_str!("../resources/longparams.json");
let long_params = Self::from_json(long_params_str).unwrap();
let long_params = Self::from_json(long_params_str, false).unwrap();
long_params
}
}
Expand Down
4 changes: 2 additions & 2 deletions rust/fastsim-core/src/simdrivelabel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ pub fn get_label_fe(
// load the cycles and intstantiate simdrive objects
cyc.insert("accel", make_accel_trace());

cyc.insert("udds", RustCycle::from_resource("cycles/udds.csv")?);
cyc.insert("hwy", RustCycle::from_resource("cycles/hwfet.csv")?);
cyc.insert("udds", RustCycle::from_resource("cycles/udds.csv", false)?);
cyc.insert("hwy", RustCycle::from_resource("cycles/hwfet.csv", false)?);

// run simdrive for non-phev powertrains
sd.insert("udds", RustSimDrive::new(cyc["udds"].clone(), veh.clone()));
Expand Down
Loading

0 comments on commit b6db185

Please sign in to comment.