|
11 | 11 | import numpy as np |
12 | 12 |
|
13 | 13 | from anypytools.tools import ( |
| 14 | + _parse_data, |
14 | 15 | array2anyscript, |
15 | 16 | get_anybodycon_path, |
16 | 17 | define2str, |
@@ -146,6 +147,55 @@ def test_get_anybodycon_path(): |
146 | 147 | assert os.path.exists(abc) |
147 | 148 |
|
148 | 149 |
|
| 150 | +@pytest.mark.parametrize( |
| 151 | + "str_val, expected, expected_dtype", |
| 152 | + [ |
| 153 | + ( |
| 154 | + """{{{0.9959166, nan, -nan}, {-0.08322453, 0.952727, -0.292207}, {0.0349831, 0.2958367, 0.9545977}},{{0.996183, -0.0689204, 0.05356734}, {0.07839193, 0.9763008, -0.2017211}, {-0.03839513, 0.2051504, 0.9779771}}}""", |
| 155 | + np.array([[[0.9959166, float("nan"), float("nan") ], [-0.08322453, 0.952727, -0.292207], [0.0349831, 0.2958367, 0.9545977]],[[0.996183, -0.0689204, 0.05356734], [0.07839193, 0.9763008, -0.2017211], [-0.03839513, 0.2051504, 0.9779771]]]), |
| 156 | + np.float64, |
| 157 | + ), |
| 158 | + ( |
| 159 | + """{{inf, 4.0}, {3.0, -inf}}""", |
| 160 | + np.array([[float("inf"), 4.0],[3.0, float("-inf")]]), |
| 161 | + np.float64, |
| 162 | + ), |
| 163 | + ( |
| 164 | + """{1.0, inf, 3.0}""", |
| 165 | + np.array([1.0, float("inf"), 3.0]), |
| 166 | + np.float64, |
| 167 | + ), |
| 168 | + ] |
| 169 | +) |
| 170 | +def test_parse_anybodydata_arrays(str_val, expected, expected_dtype): |
| 171 | + data_np = _parse_data(str_val) |
| 172 | + assert data_np.dtype == expected_dtype |
| 173 | + assert np.isclose(data_np, expected, equal_nan=True).all() |
| 174 | + |
| 175 | + |
| 176 | +@pytest.mark.parametrize( |
| 177 | + "str_val, expected, expected_type", |
| 178 | + [ |
| 179 | + ("1.0", 1.0, float), |
| 180 | + ('"some_str"', "some_str", str), |
| 181 | + ("1", 1, int), |
| 182 | + ] |
| 183 | + ) |
| 184 | +def test_parse_anybodydata_scalars(str_val, expected, expected_type): |
| 185 | + out = _parse_data(str_val) |
| 186 | + assert isinstance(out, expected_type) |
| 187 | + assert out == expected |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
149 | 199 | if __name__ == "__main__": |
150 | 200 | os.chdir(Path(__file__).parent) |
151 | 201 | pytest.main([str("test_tools.py::test_AnyPyProcessOutputList_to_dataframe")]) |
0 commit comments