Skip to content

Commit 140ac03

Browse files
committed
Fixed data size condition
1 parent 2f2ddc9 commit 140ac03

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

pydmd/havok.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,12 @@ def hankel(self, X):
291291
n, m = X.shape
292292

293293
# Check that the input data contains enough observations.
294-
if m < self._delays * self._lag:
294+
m_min = self._lag * (self._delays - 1) + 1
295+
if m < m_min:
295296
raise ValueError(
296297
"Not enough snapshots provided for "
297-
f"{self._delays} delays and lag {self._lag}. Please "
298-
f"provide at least {self._delays * self._lag} snapshots."
298+
f"{self._delays} delays and lag {self._lag}. "
299+
f"Please provide at least {m_min} snapshots."
299300
)
300301

301302
Hm = m - ((self._delays - 1) * self._lag)
@@ -325,15 +326,6 @@ def dehankel(self, H):
325326
X[:, i * self._lag : i * self._lag + Hm] = H[i * n : (i + 1) * n]
326327
return X
327328

328-
# dummy_data = np.array([[1, 2, 3, 4, 5, 6]])
329-
# H2 = np.array([
330-
# [1, 2, 3, 4],
331-
# [3, 4, 5, 6]]) # lag=2, delays=2
332-
# H3 = np.array([
333-
# [1, 2],
334-
# [3, 4],
335-
# [5, 6]]) # lag=2, delays=3
336-
337329
def fit(self, X, t):
338330
"""
339331
Perform the HAVOK analysis.

tutorials/tutorial19/tutorial-19-havok.ipynb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
{
3939
"cell_type": "code",
40-
"execution_count": null,
40+
"execution_count": 2,
4141
"id": "1567a968-7a03-4dda-ac6d-2dfbecbde882",
4242
"metadata": {},
4343
"outputs": [],
@@ -159,23 +159,21 @@
159159
},
160160
{
161161
"cell_type": "code",
162-
"execution_count": 9,
162+
"execution_count": 3,
163163
"id": "5e5a0b0b-68c0-44b6-982b-83e11400d4f6",
164164
"metadata": {},
165165
"outputs": [
166166
{
167-
"ename": "AssertionError",
168-
"evalue": "\nArrays are not equal\n\n(shapes (1, 5), (1, 6) mismatch)\n x: array([[1., 2., 3., 4., 6.]])\n y: array([[1, 2, 3, 4, 5, 6]])",
167+
"ename": "ValueError",
168+
"evalue": "Not enough snapshots provided for 3 delays and lag 3. Please provide at least 9 snapshots.",
169169
"output_type": "error",
170170
"traceback": [
171171
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
172-
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
173-
"Cell \u001b[0;32mIn[9], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m test_hankel_1()\n\u001b[1;32m 3\u001b[0m test_hankel_2()\n\u001b[0;32m----> 4\u001b[0m \u001b[43mtest_hankel_3\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
174-
"Cell \u001b[0;32mIn[8], line 105\u001b[0m, in \u001b[0;36mtest_hankel_3\u001b[0;34m()\u001b[0m\n\u001b[1;32m 103\u001b[0m havok \u001b[38;5;241m=\u001b[39m HAVOK(delays\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m, lag\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m)\n\u001b[1;32m 104\u001b[0m assert_equal(havok\u001b[38;5;241m.\u001b[39mhankel(dummy_data), H2)\n\u001b[0;32m--> 105\u001b[0m \u001b[43massert_equal\u001b[49m\u001b[43m(\u001b[49m\u001b[43mhavok\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdehankel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mhavok\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhankel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdummy_data\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdummy_data\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 107\u001b[0m havok \u001b[38;5;241m=\u001b[39m HAVOK(delays\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3\u001b[39m, lag\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3\u001b[39m)\n\u001b[1;32m 108\u001b[0m assert_equal(havok\u001b[38;5;241m.\u001b[39mhankel(dummy_data), H3)\n",
175-
" \u001b[0;31m[... skipping hidden 2 frame]\u001b[0m\n",
176-
"File \u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py:81\u001b[0m, in \u001b[0;36mContextDecorator.__call__.<locals>.inner\u001b[0;34m(*args, **kwds)\u001b[0m\n\u001b[1;32m 78\u001b[0m \u001b[38;5;129m@wraps\u001b[39m(func)\n\u001b[1;32m 79\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21minner\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds):\n\u001b[1;32m 80\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_recreate_cm():\n\u001b[0;32m---> 81\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m\n",
177-
"File \u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy/testing/_private/utils.py:713\u001b[0m, in \u001b[0;36massert_array_compare\u001b[0;34m(comparison, x, y, err_msg, verbose, header, precision, equal_nan, equal_inf, strict)\u001b[0m\n\u001b[1;32m 707\u001b[0m reason \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m(dtypes \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mx\u001b[38;5;241m.\u001b[39mdtype\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, \u001b[39m\u001b[38;5;132;01m{\u001b[39;00my\u001b[38;5;241m.\u001b[39mdtype\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m mismatch)\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 708\u001b[0m msg \u001b[38;5;241m=\u001b[39m build_err_msg([x, y],\n\u001b[1;32m 709\u001b[0m err_msg\n\u001b[1;32m 710\u001b[0m \u001b[38;5;241m+\u001b[39m reason,\n\u001b[1;32m 711\u001b[0m verbose\u001b[38;5;241m=\u001b[39mverbose, header\u001b[38;5;241m=\u001b[39mheader,\n\u001b[1;32m 712\u001b[0m names\u001b[38;5;241m=\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124my\u001b[39m\u001b[38;5;124m'\u001b[39m), precision\u001b[38;5;241m=\u001b[39mprecision)\n\u001b[0;32m--> 713\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAssertionError\u001b[39;00m(msg)\n\u001b[1;32m 715\u001b[0m flagged \u001b[38;5;241m=\u001b[39m bool_(\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m 716\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m isnumber(x) \u001b[38;5;129;01mand\u001b[39;00m isnumber(y):\n",
178-
"\u001b[0;31mAssertionError\u001b[0m: \nArrays are not equal\n\n(shapes (1, 5), (1, 6) mismatch)\n x: array([[1., 2., 3., 4., 6.]])\n y: array([[1, 2, 3, 4, 5, 6]])"
172+
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
173+
"Cell \u001b[0;32mIn[3], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m test_hankel_1()\n\u001b[1;32m 3\u001b[0m test_hankel_2()\n\u001b[0;32m----> 4\u001b[0m \u001b[43mtest_hankel_3\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
174+
"Cell \u001b[0;32mIn[2], line 108\u001b[0m, in \u001b[0;36mtest_hankel_3\u001b[0;34m()\u001b[0m\n\u001b[1;32m 105\u001b[0m assert_equal(havok\u001b[38;5;241m.\u001b[39mdehankel(havok\u001b[38;5;241m.\u001b[39mhankel(dummy_data)), dummy_data)\n\u001b[1;32m 107\u001b[0m havok \u001b[38;5;241m=\u001b[39m HAVOK(delays\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3\u001b[39m, lag\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3\u001b[39m)\n\u001b[0;32m--> 108\u001b[0m assert_equal(\u001b[43mhavok\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhankel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdummy_data\u001b[49m\u001b[43m)\u001b[49m, H3)\n\u001b[1;32m 109\u001b[0m assert_equal(havok\u001b[38;5;241m.\u001b[39mdehankel(havok\u001b[38;5;241m.\u001b[39mhankel(dummy_data)), dummy_data)\n\u001b[1;32m 111\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m raises(\u001b[38;5;167;01mValueError\u001b[39;00m):\n",
175+
"File \u001b[0;32m~/Desktop/PyDMD/PyDMD/pydmd/havok.py:295\u001b[0m, in \u001b[0;36mHAVOK.hankel\u001b[0;34m(self, X)\u001b[0m\n\u001b[1;32m 293\u001b[0m \u001b[38;5;66;03m# Check that the input data contains enough observations.\u001b[39;00m\n\u001b[1;32m 294\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m m \u001b[38;5;241m<\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_delays \u001b[38;5;241m*\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lag:\n\u001b[0;32m--> 295\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 296\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNot enough snapshots provided for \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 297\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_delays\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m delays and lag \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lag\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m. Please \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 298\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mprovide at least \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_delays\u001b[38;5;250m \u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;250m \u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lag\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m snapshots.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 299\u001b[0m )\n\u001b[1;32m 301\u001b[0m Hm \u001b[38;5;241m=\u001b[39m m \u001b[38;5;241m-\u001b[39m ((\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_delays \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1\u001b[39m) \u001b[38;5;241m*\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lag)\n\u001b[1;32m 302\u001b[0m H \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mempty((n \u001b[38;5;241m*\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_delays, Hm))\n",
176+
"\u001b[0;31mValueError\u001b[0m: Not enough snapshots provided for 3 delays and lag 3. Please provide at least 9 snapshots."
179177
]
180178
}
181179
],

0 commit comments

Comments
 (0)