-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmodel_record.rs
More file actions
205 lines (185 loc) · 6.08 KB
/
Copy pathmodel_record.rs
File metadata and controls
205 lines (185 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//! Structs for parameter objects.
//!
//! - PyPureRecord
//! - PyBinaryRecord
use super::identifier::{PyIdentifier, PyIdentifierOption};
use crate::error::PyFeosError;
use feos_core::parameter::*;
use pyo3::types::PyDict;
use pyo3::{exceptions::PyValueError, prelude::*};
use pythonize::{depythonize, pythonize};
use serde::{Deserialize, Serialize};
use serde_json::Value;
/// Parameters that describe a pure component.
#[derive(Serialize, Deserialize, Clone)]
#[serde(from = "PureRecord<Value, Value>")]
#[serde(into = "PureRecord<Value, Value>")]
#[pyclass(name = "PureRecord")]
pub struct PyPureRecord {
#[pyo3(get)]
pub identifier: PyIdentifier,
#[pyo3(get)]
pub molarweight: f64,
pub model_record: Value,
pub association_sites: Vec<AssociationRecord<Value>>,
}
impl From<PyPureRecord> for PureRecord<Value, Value> {
fn from(value: PyPureRecord) -> Self {
Self {
identifier: value.identifier.0,
molarweight: value.molarweight,
model_record: value.model_record,
association_sites: value.association_sites,
}
}
}
impl From<PureRecord<Value, Value>> for PyPureRecord {
fn from(value: PureRecord<Value, Value>) -> Self {
Self {
identifier: PyIdentifier(value.identifier),
molarweight: value.molarweight,
model_record: value.model_record,
association_sites: value.association_sites,
}
}
}
#[pymethods]
impl PyPureRecord {
#[new]
#[pyo3(signature = (identifier, molarweight, **parameters))]
fn new(
py: Python,
identifier: PyIdentifier,
molarweight: f64,
parameters: Option<&Bound<'_, PyDict>>,
) -> PyResult<Self> {
let Some(parameters) = parameters else {
return Err(PyErr::new::<PyValueError, _>(
"No model parameters provided for PureRecord",
));
};
parameters.set_item("identifier", pythonize(py, &identifier)?)?;
parameters.set_item("molarweight", molarweight)?;
Ok(depythonize(parameters)?)
}
#[getter]
fn get_model_record<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
pythonize(py, &self.model_record).map_err(PyErr::from)
}
#[getter]
fn get_association_sites<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
pythonize(py, &self.association_sites).map_err(PyErr::from)
}
pub fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
pythonize(py, &self).map_err(PyErr::from)
}
#[staticmethod]
pub fn from_json_str(s: &str) -> PyResult<Self> {
Ok(serde_json::from_str(s).map_err(PyFeosError::from)?)
}
pub fn to_json_str(&self) -> PyResult<String> {
Ok(serde_json::to_string(&self).map_err(PyFeosError::from)?)
}
/// Read a list of [PureRecord]s from a JSON file.
///
/// Parameters
/// ----------
/// substances : list[str]
/// List of component identifiers.
/// path : str
/// Path to file containing the segment records.
/// identifier_option : IdentifierOption
/// The type of identifier used in the substance list.
///
/// Returns
/// -------
/// [SegmentRecord]
#[staticmethod]
pub fn from_json(
substances: Vec<String>,
file: &str,
identifier_option: PyIdentifierOption,
) -> PyResult<Vec<Self>> {
Ok(
PureRecord::from_json(&substances, file, identifier_option.into())
.map_err(PyFeosError::from)?
.into_iter()
.map(|r| r.into())
.collect(),
)
}
fn __repr__(&self) -> String {
PureRecord::from(self.clone()).to_string()
}
}
/// Binary interaction parameters.
#[derive(Serialize, Deserialize, Clone)]
#[serde(from = "BinaryRecord<Identifier, Value, Value>")]
#[serde(into = "BinaryRecord<Identifier, Value, Value>")]
#[pyclass(name = "BinaryRecord")]
pub struct PyBinaryRecord {
#[pyo3(get)]
pub id1: PyIdentifier,
#[pyo3(get)]
pub id2: PyIdentifier,
pub model_record: Option<Value>,
pub association_sites: Vec<BinaryAssociationRecord<Value>>,
}
impl From<PyBinaryRecord> for BinaryRecord<Identifier, Value, Value> {
fn from(value: PyBinaryRecord) -> Self {
Self {
id1: value.id1.0,
id2: value.id2.0,
model_record: value.model_record,
association_sites: value.association_sites,
}
}
}
impl From<BinaryRecord<Identifier, Value, Value>> for PyBinaryRecord {
fn from(value: BinaryRecord<Identifier, Value, Value>) -> Self {
Self {
id1: PyIdentifier(value.id1),
id2: PyIdentifier(value.id2),
model_record: value.model_record,
association_sites: value.association_sites,
}
}
}
#[pymethods]
impl PyBinaryRecord {
#[new]
#[pyo3(signature = (id1, id2, **parameters))]
fn new(
py: Python,
id1: PyIdentifier,
id2: PyIdentifier,
parameters: Option<&Bound<'_, PyDict>>,
) -> PyResult<Self> {
let Some(parameters) = parameters else {
return Err(PyFeosError::Error(
"No model parameters provided for BinaryRecord".to_string(),
)
.into());
};
parameters.set_item("id1", pythonize(py, &id1)?)?;
parameters.set_item("id2", pythonize(py, &id2)?)?;
Ok(depythonize(parameters)?)
}
#[getter]
fn get_model_record<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
pythonize(py, &self.model_record).map_err(PyErr::from)
}
pub fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
pythonize(py, &self).map_err(PyErr::from)
}
#[staticmethod]
pub fn from_json_str(s: &str) -> PyResult<Self> {
Ok(serde_json::from_str(s).map_err(PyFeosError::from)?)
}
pub fn to_json_str(&self) -> PyResult<String> {
Ok(serde_json::to_string(&self).map_err(PyFeosError::from)?)
}
fn __repr__(&self) -> String {
BinaryRecord::from(self.clone()).to_string()
}
}