|
9 | 9 |
|
10 | 10 | import control as ct |
11 | 11 | from control import StateSpace, TransferFunction, c2d, isctime, ss2tf, tf2ss |
12 | | -from control.exception import slycot_check |
| 12 | +from control.exception import slycot_check, pandas_check |
13 | 13 | from control.tests.conftest import slycotonly |
14 | 14 | from control.timeresp import (_default_time_vector, _ideal_tfinal_and_dt, |
15 | 15 | forced_response, impulse_response, |
@@ -1180,3 +1180,55 @@ def test_response_transpose( |
1180 | 1180 | assert t.shape == (T.size, ) |
1181 | 1181 | assert y.shape == ysh_no |
1182 | 1182 | assert x.shape == (T.size, sys.nstates) |
| 1183 | + |
| 1184 | + |
| 1185 | +@pytest.mark.skipif(not pandas_check(), reason="pandas not installed") |
| 1186 | +def test_to_pandas(): |
| 1187 | + # Create a SISO time response |
| 1188 | + sys = ct.rss(2, 1, 1) |
| 1189 | + timepts = np.linspace(0, 10, 10) |
| 1190 | + resp = ct.input_output_response(sys, timepts, 1) |
| 1191 | + |
| 1192 | + # Convert to pandas |
| 1193 | + df = resp.to_pandas() |
| 1194 | + |
| 1195 | + # Check to make sure the data make senses |
| 1196 | + np.testing.assert_equal(df['time'], resp.time) |
| 1197 | + np.testing.assert_equal(df['u[0]'], resp.inputs) |
| 1198 | + np.testing.assert_equal(df['y[0]'], resp.outputs) |
| 1199 | + np.testing.assert_equal(df['x[0]'], resp.states[0]) |
| 1200 | + np.testing.assert_equal(df['x[1]'], resp.states[1]) |
| 1201 | + |
| 1202 | + # Create a MIMO time response |
| 1203 | + sys = ct.rss(2, 2, 1) |
| 1204 | + resp = ct.input_output_response(sys, timepts, np.sin(timepts)) |
| 1205 | + df = resp.to_pandas() |
| 1206 | + np.testing.assert_equal(df['time'], resp.time) |
| 1207 | + np.testing.assert_equal(df['u[0]'], resp.inputs[0]) |
| 1208 | + np.testing.assert_equal(df['y[0]'], resp.outputs[0]) |
| 1209 | + np.testing.assert_equal(df['y[1]'], resp.outputs[1]) |
| 1210 | + np.testing.assert_equal(df['x[0]'], resp.states[0]) |
| 1211 | + np.testing.assert_equal(df['x[1]'], resp.states[1]) |
| 1212 | + |
| 1213 | + # Change the time points |
| 1214 | + sys = ct.rss(2, 1, 1) |
| 1215 | + T = np.linspace(0, timepts[-1]/2, timepts.size * 2) |
| 1216 | + resp = ct.input_output_response(sys, timepts, np.sin(timepts), t_eval=T) |
| 1217 | + df = resp.to_pandas() |
| 1218 | + np.testing.assert_equal(df['time'], resp.time) |
| 1219 | + np.testing.assert_equal(df['u[0]'], resp.inputs) |
| 1220 | + np.testing.assert_equal(df['y[0]'], resp.outputs) |
| 1221 | + np.testing.assert_equal(df['x[0]'], resp.states[0]) |
| 1222 | + np.testing.assert_equal(df['x[1]'], resp.states[1]) |
| 1223 | + |
| 1224 | + |
| 1225 | +@pytest.mark.skipif(pandas_check(), reason="pandas installed") |
| 1226 | +def test_no_pandas(): |
| 1227 | + # Create a SISO time response |
| 1228 | + sys = ct.rss(2, 1, 1) |
| 1229 | + timepts = np.linspace(0, 10, 10) |
| 1230 | + resp = ct.input_output_response(sys, timepts, 1) |
| 1231 | + |
| 1232 | + # Convert to pandas |
| 1233 | + with pytest.raises(ImportError, match="pandas"): |
| 1234 | + df = resp.to_pandas() |
0 commit comments