pyhsp 0.1.0rc1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyHSP/__init__.py +9 -0
- pyHSP/_conf.py +5 -0
- pyHSP/_utils.py +120 -0
- pyHSP/data/pla.json +191 -0
- pyHSP/examples.py +22 -0
- pyHSP/functional.py +349 -0
- pyHSP/io.py +269 -0
- pyHSP/pandas.py +285 -0
- pyHSP/solvents.py +265 -0
- pyHSP/sphere.py +1385 -0
- pyHSP/types.py +15 -0
- pyhsp-0.1.0rc1.dist-info/METADATA +105 -0
- pyhsp-0.1.0rc1.dist-info/RECORD +15 -0
- pyhsp-0.1.0rc1.dist-info/WHEEL +4 -0
- pyhsp-0.1.0rc1.dist-info/licenses/LICENSE +21 -0
pyHSP/__init__.py
ADDED
pyHSP/_conf.py
ADDED
pyHSP/_utils.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
import pandas as pd
|
|
5
|
+
|
|
6
|
+
# import matplotlib.pyplot as plt
|
|
7
|
+
# import os
|
|
8
|
+
# import argparse
|
|
9
|
+
|
|
10
|
+
from typing import Callable, Concatenate, overload
|
|
11
|
+
from numpy.typing import ArrayLike, NDArray
|
|
12
|
+
from sklearn.utils import check_array as _skl_check_array, check_consistent_length
|
|
13
|
+
from functools import wraps
|
|
14
|
+
|
|
15
|
+
from ._conf import BETA_MAX
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def check_X(
|
|
19
|
+
X: ArrayLike, *, n_features: int = 3, min_samples: int = 1, estimator=None
|
|
20
|
+
) -> NDArray[np.floating]:
|
|
21
|
+
"""Check that X is a valid array with the specified minimum features and samples."""
|
|
22
|
+
X_ = _skl_check_array(
|
|
23
|
+
X,
|
|
24
|
+
ensure_2d=True,
|
|
25
|
+
allow_nd=False,
|
|
26
|
+
ensure_all_finite=True,
|
|
27
|
+
accept_sparse=False,
|
|
28
|
+
ensure_min_samples=min_samples,
|
|
29
|
+
ensure_non_negative=True,
|
|
30
|
+
estimator=estimator,
|
|
31
|
+
input_name="X",
|
|
32
|
+
)
|
|
33
|
+
if X_.shape[1] != n_features:
|
|
34
|
+
raise ValueError(f"X must have exactly {n_features} features.")
|
|
35
|
+
return X_
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def check_y(
|
|
39
|
+
y: ArrayLike, *, graded: bool = False, min_samples: int = 1, estimator=None
|
|
40
|
+
) -> NDArray[np.floating]:
|
|
41
|
+
"""Check scores are either 0 or 1, or between 0 and 1 (inclusive) for graded scoring."""
|
|
42
|
+
y_ = _skl_check_array(
|
|
43
|
+
y,
|
|
44
|
+
ensure_2d=False,
|
|
45
|
+
allow_nd=False,
|
|
46
|
+
ensure_all_finite=True,
|
|
47
|
+
accept_sparse=False,
|
|
48
|
+
ensure_min_samples=min_samples,
|
|
49
|
+
estimator=estimator,
|
|
50
|
+
input_name="y",
|
|
51
|
+
)
|
|
52
|
+
if y_.ndim != 1:
|
|
53
|
+
raise ValueError("y must be a 1-dimensional array.")
|
|
54
|
+
if not graded and not np.all((y_ == 1) + (y_ == 0)):
|
|
55
|
+
raise ValueError(
|
|
56
|
+
"Invalid score values for non-graded fit. All score "
|
|
57
|
+
"values must be 0 or 1."
|
|
58
|
+
)
|
|
59
|
+
if np.any((y_ > 1) + (y_ < 0)):
|
|
60
|
+
raise ValueError(
|
|
61
|
+
"Invalid score values. All scores must be between 0 and 1 inclusive."
|
|
62
|
+
)
|
|
63
|
+
return y_
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def check_X_y(
|
|
67
|
+
X: ArrayLike,
|
|
68
|
+
y: ArrayLike,
|
|
69
|
+
*,
|
|
70
|
+
n_features: int = 3,
|
|
71
|
+
min_samples: int = 1,
|
|
72
|
+
graded: bool = False,
|
|
73
|
+
estimator=None,
|
|
74
|
+
) -> tuple[NDArray[np.floating], NDArray[np.floating]]:
|
|
75
|
+
|
|
76
|
+
X_ = check_X(X, n_features=n_features, min_samples=min_samples, estimator=estimator)
|
|
77
|
+
y_ = check_y(y, graded=graded, min_samples=min_samples, estimator=estimator)
|
|
78
|
+
check_consistent_length(X_, y_)
|
|
79
|
+
|
|
80
|
+
return X_, y_
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def check_beta(beta: int | float) -> float:
|
|
84
|
+
"""Check that beta is a valid float."""
|
|
85
|
+
if not isinstance(beta, (int, float)):
|
|
86
|
+
raise TypeError("beta must be a float.")
|
|
87
|
+
if beta <= 0 or beta > BETA_MAX:
|
|
88
|
+
raise ValueError(
|
|
89
|
+
f"beta must be a float greater than 0 and less than or equal to {BETA_MAX}."
|
|
90
|
+
)
|
|
91
|
+
return float(beta)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# def validates_dataframe(func) -> Callable:
|
|
95
|
+
# """Decorator to validate that the input is a pandas dataframe."""
|
|
96
|
+
|
|
97
|
+
# @wraps(func)
|
|
98
|
+
# def wrapper(self, X, *args, **kwargs) -> pd.DataFrame | np.ndarray:
|
|
99
|
+
# if isinstance(X, pd.DataFrame):
|
|
100
|
+
# X = validate_dataframe(X)
|
|
101
|
+
# return func(self, X, *args, **kwargs)
|
|
102
|
+
|
|
103
|
+
# return wrapper
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def keep_index[_Self, **P](
|
|
107
|
+
func: Callable[Concatenate[_Self, ArrayLike, P], NDArray],
|
|
108
|
+
) -> Callable[Concatenate[_Self, ArrayLike, P], pd.Series | NDArray]:
|
|
109
|
+
"""Decorator to output a series if input is a dataframe."""
|
|
110
|
+
|
|
111
|
+
@wraps(func)
|
|
112
|
+
def wrapper(
|
|
113
|
+
self: _Self, X: ArrayLike, *args: P.args, **kwargs: P.kwargs
|
|
114
|
+
) -> pd.Series | NDArray:
|
|
115
|
+
result = func(self, np.asarray(X), *args, **kwargs)
|
|
116
|
+
if isinstance(X, pd.DataFrame):
|
|
117
|
+
return pd.Series(result, index=X.index)
|
|
118
|
+
return result
|
|
119
|
+
|
|
120
|
+
return wrapper
|
pyHSP/data/pla.json
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
{
|
|
2
|
+
"Pyridine":{
|
|
3
|
+
"dD":19.0,
|
|
4
|
+
"dP":8.8,
|
|
5
|
+
"dH":5.9,
|
|
6
|
+
"MVol":80.9,
|
|
7
|
+
"Score":1.0
|
|
8
|
+
},
|
|
9
|
+
"N-Methyl-2-Pyrrolidone (NMP)":{
|
|
10
|
+
"dD":18.0,
|
|
11
|
+
"dP":12.3,
|
|
12
|
+
"dH":7.2,
|
|
13
|
+
"MVol":96.6,
|
|
14
|
+
"Score":1.0
|
|
15
|
+
},
|
|
16
|
+
"Methylene Dichloride (Dichloromethane)":{
|
|
17
|
+
"dD":17.0,
|
|
18
|
+
"dP":7.3,
|
|
19
|
+
"dH":7.1,
|
|
20
|
+
"MVol":64.4,
|
|
21
|
+
"Score":1.0
|
|
22
|
+
},
|
|
23
|
+
"1,3-Dioxolane":{
|
|
24
|
+
"dD":18.1,
|
|
25
|
+
"dP":6.6,
|
|
26
|
+
"dH":9.3,
|
|
27
|
+
"MVol":69.9,
|
|
28
|
+
"Score":1.0
|
|
29
|
+
},
|
|
30
|
+
"Methyl Ethyl Ketone (MEK)":{
|
|
31
|
+
"dD":16.0,
|
|
32
|
+
"dP":9.0,
|
|
33
|
+
"dH":5.1,
|
|
34
|
+
"MVol":90.2,
|
|
35
|
+
"Score":1.0
|
|
36
|
+
},
|
|
37
|
+
"Tetrahydrofuran (THF)":{
|
|
38
|
+
"dD":16.8,
|
|
39
|
+
"dP":5.7,
|
|
40
|
+
"dH":8.0,
|
|
41
|
+
"MVol":81.9,
|
|
42
|
+
"Score":1.0
|
|
43
|
+
},
|
|
44
|
+
"Chloroform":{
|
|
45
|
+
"dD":17.8,
|
|
46
|
+
"dP":3.1,
|
|
47
|
+
"dH":5.7,
|
|
48
|
+
"MVol":80.5,
|
|
49
|
+
"Score":1.0
|
|
50
|
+
},
|
|
51
|
+
"Acetone":{
|
|
52
|
+
"dD":15.5,
|
|
53
|
+
"dP":10.4,
|
|
54
|
+
"dH":7.0,
|
|
55
|
+
"MVol":73.8,
|
|
56
|
+
"Score":1.0
|
|
57
|
+
},
|
|
58
|
+
"Dimethyl Formamide (DMF)":{
|
|
59
|
+
"dD":17.4,
|
|
60
|
+
"dP":13.7,
|
|
61
|
+
"dH":11.3,
|
|
62
|
+
"MVol":77.4,
|
|
63
|
+
"Score":1.0
|
|
64
|
+
},
|
|
65
|
+
"Dimethyl Sulfoxide (DMSO)":{
|
|
66
|
+
"dD":18.4,
|
|
67
|
+
"dP":16.4,
|
|
68
|
+
"dH":10.2,
|
|
69
|
+
"MVol":71.3,
|
|
70
|
+
"Score":1.0
|
|
71
|
+
},
|
|
72
|
+
"1,4-Dioxane":{
|
|
73
|
+
"dD":17.5,
|
|
74
|
+
"dP":1.8,
|
|
75
|
+
"dH":9.0,
|
|
76
|
+
"MVol":85.7,
|
|
77
|
+
"Score":1.0
|
|
78
|
+
},
|
|
79
|
+
"Ethyl Acetate":{
|
|
80
|
+
"dD":15.8,
|
|
81
|
+
"dP":5.3,
|
|
82
|
+
"dH":7.2,
|
|
83
|
+
"MVol":98.6,
|
|
84
|
+
"Score":1.0
|
|
85
|
+
},
|
|
86
|
+
"Furan":{
|
|
87
|
+
"dD":17.0,
|
|
88
|
+
"dP":1.8,
|
|
89
|
+
"dH":5.3,
|
|
90
|
+
"MVol":73.1,
|
|
91
|
+
"Score":1.0
|
|
92
|
+
},
|
|
93
|
+
"m-Cresol":{
|
|
94
|
+
"dD":18.5,
|
|
95
|
+
"dP":6.5,
|
|
96
|
+
"dH":13.7,
|
|
97
|
+
"MVol":105.0,
|
|
98
|
+
"Score":1.0
|
|
99
|
+
},
|
|
100
|
+
"Toluene":{
|
|
101
|
+
"dD":18.0,
|
|
102
|
+
"dP":1.4,
|
|
103
|
+
"dH":2.0,
|
|
104
|
+
"MVol":106.6,
|
|
105
|
+
"Score":1.0
|
|
106
|
+
},
|
|
107
|
+
"p-Xylene":{
|
|
108
|
+
"dD":17.8,
|
|
109
|
+
"dP":1.0,
|
|
110
|
+
"dH":3.1,
|
|
111
|
+
"MVol":121.1,
|
|
112
|
+
"Score":1.0
|
|
113
|
+
},
|
|
114
|
+
"Benzene":{
|
|
115
|
+
"dD":18.4,
|
|
116
|
+
"dP":0.0,
|
|
117
|
+
"dH":2.0,
|
|
118
|
+
"MVol":89.5,
|
|
119
|
+
"Score":1.0
|
|
120
|
+
},
|
|
121
|
+
"Hexafluoro Isopropanol":{
|
|
122
|
+
"dD":17.2,
|
|
123
|
+
"dP":4.5,
|
|
124
|
+
"dH":14.7,
|
|
125
|
+
"MVol":105.3,
|
|
126
|
+
"Score":1.0
|
|
127
|
+
},
|
|
128
|
+
"Isoamyl Alcohol (3-Methyl-1-Butanol)":{
|
|
129
|
+
"dD":15.8,
|
|
130
|
+
"dP":5.2,
|
|
131
|
+
"dH":13.3,
|
|
132
|
+
"MVol":109.3,
|
|
133
|
+
"Score":1.0
|
|
134
|
+
},
|
|
135
|
+
"Acetonitrile":{
|
|
136
|
+
"dD":15.3,
|
|
137
|
+
"dP":18.0,
|
|
138
|
+
"dH":6.1,
|
|
139
|
+
"MVol":52.9,
|
|
140
|
+
"Score":0.0
|
|
141
|
+
},
|
|
142
|
+
"Diethyl Ether":{
|
|
143
|
+
"dD":14.5,
|
|
144
|
+
"dP":2.9,
|
|
145
|
+
"dH":4.6,
|
|
146
|
+
"MVol":104.7,
|
|
147
|
+
"Score":0.0
|
|
148
|
+
},
|
|
149
|
+
"Cyclohexane":{
|
|
150
|
+
"dD":16.8,
|
|
151
|
+
"dP":0.0,
|
|
152
|
+
"dH":0.2,
|
|
153
|
+
"MVol":108.9,
|
|
154
|
+
"Score":0.0
|
|
155
|
+
},
|
|
156
|
+
"Isopropyl Ether":{
|
|
157
|
+
"dD":15.1,
|
|
158
|
+
"dP":3.2,
|
|
159
|
+
"dH":3.2,
|
|
160
|
+
"MVol":141.8,
|
|
161
|
+
"Score":0.0
|
|
162
|
+
},
|
|
163
|
+
"Hexane":{
|
|
164
|
+
"dD":14.9,
|
|
165
|
+
"dP":0.0,
|
|
166
|
+
"dH":0.0,
|
|
167
|
+
"MVol":131.4,
|
|
168
|
+
"Score":0.0
|
|
169
|
+
},
|
|
170
|
+
"Ethanol":{
|
|
171
|
+
"dD":15.8,
|
|
172
|
+
"dP":8.8,
|
|
173
|
+
"dH":19.4,
|
|
174
|
+
"MVol":58.6,
|
|
175
|
+
"Score":0.0
|
|
176
|
+
},
|
|
177
|
+
"Methanol":{
|
|
178
|
+
"dD":14.7,
|
|
179
|
+
"dP":12.3,
|
|
180
|
+
"dH":22.3,
|
|
181
|
+
"MVol":40.6,
|
|
182
|
+
"Score":0.0
|
|
183
|
+
},
|
|
184
|
+
"Water":{
|
|
185
|
+
"dD":15.5,
|
|
186
|
+
"dP":16.0,
|
|
187
|
+
"dH":42.3,
|
|
188
|
+
"MVol":18.0,
|
|
189
|
+
"Score":0.0
|
|
190
|
+
}
|
|
191
|
+
}
|
pyHSP/examples.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
# prgma: no cover
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from .io import read_json
|
|
7
|
+
|
|
8
|
+
DATA_DIR = Path(__file__).parent / "data"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def load_pla():
|
|
12
|
+
"""
|
|
13
|
+
Load polylatic acid (PLA) example.
|
|
14
|
+
|
|
15
|
+
Returns a dataframe with 'dD', 'dP' and 'dH' columns and a series of binary scores
|
|
16
|
+
(1 for soluble, 0 for insoluble).
|
|
17
|
+
|
|
18
|
+
This data was published on the `HSPiP website
|
|
19
|
+
<https://hansen-solubility.com/HSP-science/sphere.php>`__ (accessed Nov 2026).
|
|
20
|
+
|
|
21
|
+
"""
|
|
22
|
+
return read_json(DATA_DIR / "pla.json")
|
pyHSP/functional.py
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
# import pandas as pd
|
|
6
|
+
# import matplotlib.pyplot as plt
|
|
7
|
+
# import os
|
|
8
|
+
# import argparse
|
|
9
|
+
|
|
10
|
+
from typing import Literal
|
|
11
|
+
from numpy.typing import ArrayLike, NDArray
|
|
12
|
+
|
|
13
|
+
from .types import Numeric
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def inverse_sum(a: Numeric, b: Numeric) -> Numeric:
|
|
17
|
+
r"""
|
|
18
|
+
Returns the inverse summation, or harmonic mean, of a and b.
|
|
19
|
+
|
|
20
|
+
.. math::
|
|
21
|
+
inverse\_sum(a,b) = \dfrac{1}{\dfrac{1}{a} + \dfrac{1}{b}} = \dfrac{ab}{a+b}
|
|
22
|
+
|
|
23
|
+
"""
|
|
24
|
+
r_eff = a * b / (a + b)
|
|
25
|
+
return r_eff
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def conc_mol_to_wt(conc_mol: Numeric, Mw: Numeric) -> Numeric:
|
|
29
|
+
"""
|
|
30
|
+
Convert mol/L to mg/mL.
|
|
31
|
+
|
|
32
|
+
Parameters
|
|
33
|
+
----------
|
|
34
|
+
conc_mol : float
|
|
35
|
+
The concentration in mol/L.
|
|
36
|
+
Mw : float
|
|
37
|
+
The solute molecular weight.
|
|
38
|
+
|
|
39
|
+
Returns
|
|
40
|
+
-------
|
|
41
|
+
conc_wt : float
|
|
42
|
+
Concentration in mg/mL.
|
|
43
|
+
|
|
44
|
+
"""
|
|
45
|
+
return conc_mol * Mw
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def conc_wt_to_mol(conc_wt: Numeric, Mw: Numeric) -> Numeric:
|
|
49
|
+
"""
|
|
50
|
+
Convert mg/mL to molar (mol/L) concentration.
|
|
51
|
+
|
|
52
|
+
Parameters
|
|
53
|
+
----------
|
|
54
|
+
conc_wt : float
|
|
55
|
+
The concentration in mg/mL.
|
|
56
|
+
Mw : float
|
|
57
|
+
The solute molecular weight.
|
|
58
|
+
|
|
59
|
+
Returns
|
|
60
|
+
-------
|
|
61
|
+
conc_mol : float
|
|
62
|
+
Concentration in mol/L.
|
|
63
|
+
|
|
64
|
+
"""
|
|
65
|
+
return conc_wt / Mw
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def conc_correction(x: Numeric, benchmark: float = 0.5) -> Numeric:
|
|
69
|
+
r"""
|
|
70
|
+
Calculate the radius concentration scaling factor.
|
|
71
|
+
|
|
72
|
+
.. math::
|
|
73
|
+
conc\_correction( x ) = \dfrac{[x ln(x) + (1-x) ln (1-x)][y(1-y)]}{[y ln(y) + (1-y) ln (1-y)][x(1-x)]}
|
|
74
|
+
Where y is the benchmark concentration.
|
|
75
|
+
|
|
76
|
+
Parameters
|
|
77
|
+
----------
|
|
78
|
+
x : float
|
|
79
|
+
The solution concentration as a mole fraction.
|
|
80
|
+
benchmark : float, default=0.5
|
|
81
|
+
The solution concentration (as a mole fraction) for which the benchmark
|
|
82
|
+
radius is defined.
|
|
83
|
+
|
|
84
|
+
Returns
|
|
85
|
+
-------
|
|
86
|
+
float
|
|
87
|
+
A factor to scale solubilty radius between desired and benchmark
|
|
88
|
+
concentrations.
|
|
89
|
+
|
|
90
|
+
"""
|
|
91
|
+
y = benchmark
|
|
92
|
+
num = (x * np.log(x) + (1 - x) * np.log(1 - x)) * y * (1 - y)
|
|
93
|
+
den = (y * np.log(y) + (1 - y) * np.log(1 - y)) * x * (1 - x)
|
|
94
|
+
return num / den
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def molar_volume(Mw: Numeric, density: Numeric) -> Numeric:
|
|
98
|
+
"""
|
|
99
|
+
The volume occupied by one mole (MVol).
|
|
100
|
+
|
|
101
|
+
Parameters
|
|
102
|
+
----------
|
|
103
|
+
Mw : float
|
|
104
|
+
The molecular weight of the substance. By convention given in g/mol.
|
|
105
|
+
density : float
|
|
106
|
+
The density of the substance. By convention given in g/cm3.
|
|
107
|
+
|
|
108
|
+
Returns
|
|
109
|
+
-------
|
|
110
|
+
float
|
|
111
|
+
The molar volume. By convention in cm3/mol.
|
|
112
|
+
|
|
113
|
+
"""
|
|
114
|
+
return Mw / density
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def solvent_molarity(molar_volume: Numeric) -> Numeric:
|
|
118
|
+
"""
|
|
119
|
+
Calculate molarity (moles per litre) of a solvent.
|
|
120
|
+
|
|
121
|
+
Parameters
|
|
122
|
+
----------
|
|
123
|
+
molar_volume : float
|
|
124
|
+
The molar volume of the solvent given in mL/mol. See `molar_volume()`.
|
|
125
|
+
|
|
126
|
+
Returns
|
|
127
|
+
-------
|
|
128
|
+
float
|
|
129
|
+
Molarity in moles per litre.
|
|
130
|
+
|
|
131
|
+
"""
|
|
132
|
+
return 1000 / molar_volume
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def mole_fraction(molar_conc: Numeric, solvent_molarity: Numeric) -> Numeric:
|
|
136
|
+
"""
|
|
137
|
+
Calculate solute concentration as mole ratio of solute to solvent.
|
|
138
|
+
|
|
139
|
+
Parameters
|
|
140
|
+
----------
|
|
141
|
+
molar_conc : float
|
|
142
|
+
Solute concentration in mol/L.
|
|
143
|
+
solvent_molarity : float
|
|
144
|
+
Molaraity of solvent in mol/L.
|
|
145
|
+
|
|
146
|
+
Returns
|
|
147
|
+
-------
|
|
148
|
+
float
|
|
149
|
+
"""
|
|
150
|
+
total_moles = solvent_molarity + molar_conc
|
|
151
|
+
return molar_conc / total_moles
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _da_dist_euclid(
|
|
155
|
+
da_a: NDArray[np.floating], da_b: NDArray[np.floating]
|
|
156
|
+
) -> NDArray[np.floating]:
|
|
157
|
+
"""Return the squared euclidean distance of donor/acceptor interaction."""
|
|
158
|
+
assert da_a.ndim == 2
|
|
159
|
+
assert da_b.ndim == 2
|
|
160
|
+
diff = da_a - da_b
|
|
161
|
+
return (diff**2).sum(axis=1)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _da_dist_beerbower(
|
|
165
|
+
da_a: NDArray[np.floating], da_b: NDArray[np.floating]
|
|
166
|
+
) -> NDArray[np.floating]:
|
|
167
|
+
"""
|
|
168
|
+
Return beerbower distance for donor/acceptor interaction
|
|
169
|
+
|
|
170
|
+
For use as an alternative to the squared distance.
|
|
171
|
+
"""
|
|
172
|
+
assert da_a.ndim == 2
|
|
173
|
+
assert da_b.ndim == 2
|
|
174
|
+
diff = da_a - da_b
|
|
175
|
+
return 2 * diff[:, 0] * diff[:, 1]
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _da_dist_louwerse(
|
|
179
|
+
da_a: NDArray[np.floating], da_b: NDArray[np.floating]
|
|
180
|
+
) -> NDArray[np.floating]:
|
|
181
|
+
"""
|
|
182
|
+
Return distance for donor/acceptor interaction as proposed by Louwerse et al.
|
|
183
|
+
|
|
184
|
+
Use as an alternative to squared distance in tandem with rescaled
|
|
185
|
+
donor/acceptor parameters.
|
|
186
|
+
"""
|
|
187
|
+
assert da_a.ndim == 2
|
|
188
|
+
assert da_b.ndim == 2
|
|
189
|
+
dist = (
|
|
190
|
+
da_a[:, 0] * da_b[:, 1]
|
|
191
|
+
+ da_b[:, 0] * da_a[:, 1]
|
|
192
|
+
- da_a[:, 0] * da_a[:, 1]
|
|
193
|
+
- da_b[:, 0] * da_b[:, 1]
|
|
194
|
+
)
|
|
195
|
+
return -dist
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def calculate_distance(
|
|
199
|
+
HSP_A: ArrayLike,
|
|
200
|
+
HSP_B: ArrayLike,
|
|
201
|
+
*,
|
|
202
|
+
root: bool = True,
|
|
203
|
+
classic: bool = True,
|
|
204
|
+
da: Literal[True, False, "euclid", "beerbower", "louwerse"] = False,
|
|
205
|
+
) -> NDArray[np.floating]:
|
|
206
|
+
r"""
|
|
207
|
+
Calculate distance between points in Hansen solubility space.
|
|
208
|
+
|
|
209
|
+
Parameters
|
|
210
|
+
----------
|
|
211
|
+
HSP_A, HSP_B : array-like of shape (n_features,) or (n, n_features)
|
|
212
|
+
Points in Hansen solubility space. n_features is expected to be 3 (dD, dP, dH) or 4 when using donor/acceptor terms (dD, dP, dH_donor, dH_acceptor).
|
|
213
|
+
When n>1 the distance calculation is broadcast
|
|
214
|
+
on the first axis.
|
|
215
|
+
root : bool, default=True
|
|
216
|
+
Return the distance squared if False.
|
|
217
|
+
classic : bool, default=True
|
|
218
|
+
Include factor of 4 in dD part of distance as per classic Hansen formula.
|
|
219
|
+
da : bool or str, default=False
|
|
220
|
+
Use donor/acceptor interaction instead of dH. With this mode only the
|
|
221
|
+
squared distance can be returned. The HSP arrays must be of shape (4,)
|
|
222
|
+
or (n,4) in the order dD, dP, dH_donor, dH_acceptor. Valid arguments are
|
|
223
|
+
'euclid', 'beerbower' or 'louwerse'. A value of `True` is the same as
|
|
224
|
+
'euclid'. See notes for a description of each option.
|
|
225
|
+
|
|
226
|
+
Returns
|
|
227
|
+
-------
|
|
228
|
+
ndarray
|
|
229
|
+
The distance between the given points. When more than one entry is given in either
|
|
230
|
+
`HSP_A` or `HSP_B` an array of distances is calculuated following numpy
|
|
231
|
+
broadcasting rules.
|
|
232
|
+
|
|
233
|
+
Notes
|
|
234
|
+
-----
|
|
235
|
+
Various methods have been proposed for including hydrogen bonding
|
|
236
|
+
donor/acceptor interatcions in the distance metric, but no single method has
|
|
237
|
+
yet been determined to be 'correct'. The result of all methods will depend
|
|
238
|
+
on how the dH parameter is split into donor and acceptor parts and it is the
|
|
239
|
+
responsibility of the user to provide suitable data. It should be
|
|
240
|
+
noted that for a vast majority of problems, the 3 term HSP (dD, dP, dH) is
|
|
241
|
+
sufficient. for these reasons the support for donor/acceptor interactions
|
|
242
|
+
should be considered *experimental*.
|
|
243
|
+
|
|
244
|
+
Three methods are included here:
|
|
245
|
+
|
|
246
|
+
'euclid'
|
|
247
|
+
Extends the Euclidean distance metric across 4 dimensions, such that
|
|
248
|
+
:math:`dist_H^2 = (\delta HD_1 - \delta HD_2)^2
|
|
249
|
+
+ (\delta HA_1 - \delta HA_2)^2`. This (as far as I can tell) is the
|
|
250
|
+
method used by HSPiP so is most appropriate to use when using
|
|
251
|
+
donor/acceptor parameters from HSPiP.
|
|
252
|
+
'beerbower'
|
|
253
|
+
Uses the rule proposed by Beerbower: :math:`dist_H^2 =
|
|
254
|
+
2 * (\delta HD_1 - \delta HD_2)* (\delta HA_1 - \delta HA_2)`.
|
|
255
|
+
This form can result in a negative distance.
|
|
256
|
+
'louwerse'
|
|
257
|
+
Uses the rule proposed by Louwerse et al: :math:`dist_H^2 = -
|
|
258
|
+
(\delta HD_1 \delta HA_2 + \delta HD_2 \delta HA_1 -
|
|
259
|
+
\delta HD_1 \delta HA_1 - \delta HD_2 \delta HA_2)`. This form can result
|
|
260
|
+
in a negative distance. Louwerse et al propose an alternative method for
|
|
261
|
+
splitting dH into donor/acceptor parameters, therefore using another
|
|
262
|
+
form of parameter splitting with this method is unlikely to yield
|
|
263
|
+
useable results.
|
|
264
|
+
|
|
265
|
+
"""
|
|
266
|
+
HSP_A = np.atleast_2d(HSP_A)
|
|
267
|
+
HSP_B = np.atleast_2d(HSP_B)
|
|
268
|
+
diff = np.subtract(HSP_A, HSP_B)
|
|
269
|
+
if classic:
|
|
270
|
+
diff[:, 0] *= 2
|
|
271
|
+
D_squared = diff**2
|
|
272
|
+
if da:
|
|
273
|
+
valid_str = ("euclid", "beerbower", "louwerse")
|
|
274
|
+
if isinstance(da, str) and da not in valid_str:
|
|
275
|
+
raise ValueError(
|
|
276
|
+
"Accepted strings for parameter `da` are " + ", ".join(valid_str)
|
|
277
|
+
)
|
|
278
|
+
if root and da in ("beerbower", "louwerse"):
|
|
279
|
+
raise ValueError(
|
|
280
|
+
f"Specifying `root=True` and `da='{da}'` is not supported."
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
da_a, da_b = HSP_A[:, [2, 3]], HSP_B[:, [2, 3]]
|
|
284
|
+
if da == "beerbower":
|
|
285
|
+
D_squared[:, 2] = _da_dist_beerbower(da_a, da_b)
|
|
286
|
+
elif da == "louwerse":
|
|
287
|
+
D_squared[:, 2] = _da_dist_louwerse(da_a, da_b)
|
|
288
|
+
else:
|
|
289
|
+
D_squared[:, 2] = _da_dist_euclid(da_a, da_b)
|
|
290
|
+
|
|
291
|
+
# index to eliminate last parameter in d/a case
|
|
292
|
+
D_squared = D_squared[:, :3]
|
|
293
|
+
|
|
294
|
+
dist = D_squared.sum(axis=1)
|
|
295
|
+
if root:
|
|
296
|
+
# root=True and da=True handled above
|
|
297
|
+
dist = np.sqrt(dist)
|
|
298
|
+
|
|
299
|
+
# dist = dist.squeeze()
|
|
300
|
+
# if dist.size == 1:
|
|
301
|
+
# return dist.item()
|
|
302
|
+
return dist
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def RED(solute_HSP: ArrayLike, solvent_HSP: ArrayLike) -> NDArray[np.floating]:
|
|
306
|
+
"""
|
|
307
|
+
Calculate the Relative Energy Difference.
|
|
308
|
+
|
|
309
|
+
The Relative Energy Difference parametrizes the distance of a solvent
|
|
310
|
+
inside or outside the solute solubility sphere. It is simply the ratio of
|
|
311
|
+
the solute-solvent distance to the solute radius. Solvents with a high
|
|
312
|
+
probability of dissolving the solute have RED < 0, marginal solvents
|
|
313
|
+
have RED ~ 1 and poor solvents have RED > 1.
|
|
314
|
+
|
|
315
|
+
Parameters
|
|
316
|
+
----------
|
|
317
|
+
solute_HSP : array-like, shape (4,) or (n,4)
|
|
318
|
+
The solubility parameters of one or many solutes, including radius.
|
|
319
|
+
solvent_HSP : array-like, shape (3,) or (n,3)
|
|
320
|
+
The solubility parameters of one or many solvents.
|
|
321
|
+
|
|
322
|
+
Returns
|
|
323
|
+
-------
|
|
324
|
+
array of shape (n,)
|
|
325
|
+
The Relative Energy Difference. When a 2D array is given for either
|
|
326
|
+
solute or solvent the result is broadcast following numpy rules.
|
|
327
|
+
|
|
328
|
+
Notes
|
|
329
|
+
-----
|
|
330
|
+
Calculating RED is only supported for the "classic" HSP using dD, dP and dH
|
|
331
|
+
as a matter of compatibility.
|
|
332
|
+
"""
|
|
333
|
+
solute_HSP = np.atleast_2d(solute_HSP)
|
|
334
|
+
solvent_HSP = np.atleast_2d(solvent_HSP)
|
|
335
|
+
radius = solute_HSP[:, 3]
|
|
336
|
+
dist = calculate_distance(solute_HSP[:, :3], solvent_HSP)
|
|
337
|
+
red = dist / radius
|
|
338
|
+
return red
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
# def solvent_radius(molar_volume: float):
|
|
342
|
+
# """
|
|
343
|
+
# Approximate the radius of the solvent sphere from molar volume.
|
|
344
|
+
|
|
345
|
+
# Note this is the radius of the 'solubility sphere' for the solvent in
|
|
346
|
+
# parameter space not the radius of the molecule (or any other physical
|
|
347
|
+
# dimension). The radius is given based on 0.5 mole fraction.
|
|
348
|
+
# """
|
|
349
|
+
# return 1520 / molar_volume # value given in SI for no dD correction factor
|