Skip to content

Commit f7c0308

Browse files
committed
revive from_json_segments
1 parent 465977a commit f7c0308

File tree

1 file changed

+86
-82
lines changed

1 file changed

+86
-82
lines changed

py-feos/src/parameter/mod.rs

Lines changed: 86 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -481,88 +481,92 @@ impl PyGcParameters {
481481
}
482482
}
483483

484-
// /// Creates parameters using segments from json file.
485-
// ///
486-
// /// Parameters
487-
// /// ----------
488-
// /// substances : List[str]
489-
// /// The substances to search.
490-
// /// pure_path : str
491-
// /// Path to file containing pure substance parameters.
492-
// /// segments_path : str
493-
// /// Path to file containing segment parameters.
494-
// /// binary_path : str, optional
495-
// /// Path to file containing binary segment-segment parameters.
496-
// /// identifier_option : IdentifierOption, optional, defaults to IdentifierOption.Name
497-
// /// Identifier that is used to search substance.
498-
// #[staticmethod]
499-
// #[pyo3(
500-
// signature = (substances, pure_path, segments_path, binary_path=None, identifier_option=PyIdentifierOption::Name),
501-
// text_signature = "(substances, pure_path, segments_path, binary_path=None, identifier_option=IdentiferOption.Name)"
502-
// )]
503-
// fn from_json_segments(
504-
// substances: Vec<PyBackedStr>,
505-
// pure_path: PyBackedStr,
506-
// segments_path: PyBackedStr,
507-
// binary_path: Option<PyBackedStr>,
508-
// identifier_option: PyIdentifierOption,
509-
// ) -> PyResult<Self> {
510-
// let queried: IndexSet<_> = substances
511-
// .iter()
512-
// .map(|identifier| identifier as &str)
513-
// .collect();
514-
515-
// let reader = BufReader::new(File::open(&pure_path as &str)?);
516-
// let chemical_records: Vec<ChemicalRecord> = serde_json::from_reader(reader)?;
517-
// let mut record_map: IndexMap<_, _> = chemical_records
518-
// .into_iter()
519-
// .filter_map(|record| {
520-
// record
521-
// .identifier
522-
// .as_str(identifier_option.into())
523-
// .map(|i| i.to_owned())
524-
// .map(|i| (i, record))
525-
// })
526-
// .collect();
527-
528-
// // Compare queried components and available components
529-
// let available: IndexSet<_> = record_map
530-
// .keys()
531-
// .map(|identifier| identifier as &str)
532-
// .collect();
533-
// if !queried.is_subset(&available) {
534-
// let missing: Vec<_> = queried.difference(&available).cloned().collect();
535-
// return Err(FeosError::ComponentsNotFound(format!("{:?}", missing)));
536-
// };
537-
538-
// // Collect all pure records that were queried
539-
// let chemical_records: Vec<_> = queried
540-
// .into_iter()
541-
// .filter_map(|identifier| record_map.shift_remove(identifier))
542-
// .map(|r| r.into())
543-
// .collect();
544-
545-
// // Read segment records
546-
// let segment_records: Vec<PySegmentRecord> =
547-
// PySegmentRecord::from_json(&segments_path as &str)?;
548-
549-
// // Read binary records
550-
// let binary_records = binary_path
551-
// .as_ref()
552-
// .map(|file_binary| {
553-
// let reader = BufReader::new(File::open(file_binary as &str)?);
554-
// let binary_records: Result<Vec<BinarySegmentRecord>, ParameterError> =
555-
// Ok(serde_json::from_reader(reader)?);
556-
// binary_records
557-
// })
558-
// .transpose()?;
559-
560-
// Ok(Self::from_segments(
561-
// chemical_records,
562-
// segment_records,
563-
// binary_records,
564-
// ))
565-
// }
484+
/// Creates parameters using segments from json file.
485+
///
486+
/// Parameters
487+
/// ----------
488+
/// substances : List[str]
489+
/// The substances to search.
490+
/// pure_path : str
491+
/// Path to file containing pure substance parameters.
492+
/// segments_path : str
493+
/// Path to file containing segment parameters.
494+
/// binary_path : str, optional
495+
/// Path to file containing binary segment-segment parameters.
496+
/// identifier_option : IdentifierOption, optional, defaults to IdentifierOption.Name
497+
/// Identifier that is used to search substance.
498+
#[staticmethod]
499+
#[pyo3(
500+
signature = (substances, pure_path, segments_path, binary_path=None, identifier_option=PyIdentifierOption::Name),
501+
text_signature = "(substances, pure_path, segments_path, binary_path=None, identifier_option=IdentiferOption.Name)"
502+
)]
503+
fn from_json_segments(
504+
substances: Vec<PyBackedStr>,
505+
pure_path: PyBackedStr,
506+
segments_path: PyBackedStr,
507+
binary_path: Option<PyBackedStr>,
508+
identifier_option: PyIdentifierOption,
509+
) -> PyResult<Self> {
510+
let queried: IndexSet<_> = substances
511+
.iter()
512+
.map(|identifier| identifier as &str)
513+
.collect();
514+
515+
let reader = BufReader::new(File::open(&pure_path as &str)?);
516+
let chemical_records: Vec<ChemicalRecord> =
517+
serde_json::from_reader(reader).map_err(PyFeosError::from)?;
518+
let mut record_map: IndexMap<_, _> = chemical_records
519+
.into_iter()
520+
.filter_map(|record| {
521+
record
522+
.identifier
523+
.as_str(identifier_option.into())
524+
.map(|i| i.to_owned())
525+
.map(|i| (i, record))
526+
})
527+
.collect();
528+
529+
// Compare queried components and available components
530+
let available: IndexSet<_> = record_map
531+
.keys()
532+
.map(|identifier| identifier as &str)
533+
.collect();
534+
if !queried.is_subset(&available) {
535+
let missing: Vec<_> = queried.difference(&available).cloned().collect();
536+
return Err(PyFeosError::FeosError(FeosError::ComponentsNotFound(
537+
format!("{:?}", missing),
538+
)))?;
539+
};
540+
541+
// Collect all pure records that were queried
542+
let chemical_records: Vec<_> = queried
543+
.into_iter()
544+
.filter_map(|identifier| record_map.shift_remove(identifier))
545+
.map(|r| r.into())
546+
.collect();
547+
548+
// Read segment records
549+
let segment_records: Vec<PySegmentRecord> =
550+
PySegmentRecord::from_json(&segments_path as &str)?;
551+
552+
// Read binary records
553+
let binary_records = binary_path
554+
.as_ref()
555+
.map(|file_binary| {
556+
let reader = BufReader::new(File::open(file_binary as &str)?);
557+
let binary_records: Result<Vec<PyBinarySegmentRecord>, FeosError> =
558+
Ok(serde_json::from_reader(reader)?);
559+
binary_records
560+
})
561+
.transpose()
562+
.map_err(PyFeosError::from)?;
563+
564+
Ok(Self::from_segments(
565+
chemical_records,
566+
segment_records,
567+
binary_records,
568+
))
569+
}
566570

567571
/// Creates parameters from SMILES and segment records.
568572
///

0 commit comments

Comments
 (0)