-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathsegment.rs
More file actions
185 lines (165 loc) · 5.46 KB
/
Copy pathsegment.rs
File metadata and controls
185 lines (165 loc) · 5.46 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
use crate::error::PyFeosError;
use feos_core::parameter::*;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use pythonize::{depythonize, pythonize};
use serde::{Deserialize, Serialize};
use serde_json::Value;
/// Parameters describing individual segments.
#[derive(Serialize, Deserialize, Clone)]
#[serde(from = "SegmentRecord<Value, Value>")]
#[serde(into = "SegmentRecord<Value, Value>")]
#[pyclass(name = "SegmentRecord")]
pub struct PySegmentRecord {
#[pyo3(get)]
identifier: String,
#[pyo3(get)]
molarweight: f64,
model_record: Value,
association_sites: Vec<AssociationRecord<Value>>,
}
impl From<PySegmentRecord> for SegmentRecord<Value, Value> {
fn from(value: PySegmentRecord) -> Self {
Self {
identifier: value.identifier,
molarweight: value.molarweight,
model_record: value.model_record,
association_sites: value.association_sites,
}
}
}
impl From<SegmentRecord<Value, Value>> for PySegmentRecord {
fn from(value: SegmentRecord<Value, Value>) -> Self {
Self {
identifier: value.identifier,
molarweight: value.molarweight,
model_record: value.model_record,
association_sites: value.association_sites,
}
}
}
#[pymethods]
impl PySegmentRecord {
#[new]
#[pyo3(signature = (identifier, molarweight, **parameters))]
fn new(
identifier: String,
molarweight: f64,
parameters: Option<&Bound<'_, PyDict>>,
) -> PyResult<Self> {
let Some(parameters) = parameters else {
return Err(PyFeosError::Error(
"No model parameters provided for SegmentRecord".to_string(),
)
.into());
};
parameters.set_item("identifier", 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 [SegmentRecord]s from a JSON file.
///
/// Parameters
/// ----------
/// path : str
/// Path to file containing the segment records.
///
/// Returns
/// -------
/// [SegmentRecord]
#[staticmethod]
pub fn from_json(path: &str) -> PyResult<Vec<Self>> {
Ok(SegmentRecord::from_json(path)
.map_err(PyFeosError::from)?
.into_iter()
.map(|r| r.into())
.collect())
}
fn __repr__(&self) -> String {
SegmentRecord::from(self.clone()).to_string()
}
}
/// Binary segment/segment interaction parameters.
#[derive(Serialize, Deserialize, Clone)]
#[serde(from = "BinaryRecord<String, Value, Value>")]
#[serde(into = "BinaryRecord<String, Value, Value>")]
#[pyclass(name = "BinarySegmentRecord")]
pub struct PyBinarySegmentRecord {
#[pyo3(get)]
pub id1: String,
#[pyo3(get)]
pub id2: String,
pub model_record: Option<Value>,
pub association_sites: Vec<BinaryAssociationRecord<Value>>,
}
impl From<PyBinarySegmentRecord> for BinaryRecord<String, Value, Value> {
fn from(value: PyBinarySegmentRecord) -> Self {
Self {
id1: value.id1,
id2: value.id2,
model_record: value.model_record,
association_sites: value.association_sites,
}
}
}
impl From<BinaryRecord<String, Value, Value>> for PyBinarySegmentRecord {
fn from(value: BinaryRecord<String, Value, Value>) -> Self {
Self {
id1: value.id1,
id2: value.id2,
model_record: value.model_record,
association_sites: value.association_sites,
}
}
}
#[pymethods]
impl PyBinarySegmentRecord {
#[new]
#[pyo3(signature = (id1, id2, **parameters))]
fn new(id1: String, id2: String, 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", id1)?;
parameters.set_item("id2", 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()
}
}