Skip to content

Commit 05dd4e4

Browse files
pavelkomarovclaude
andcommitted
Update circular domain notebook to use simplified API
Replace wrap_angle() calls with inline arithmetic, update circular_vars/circular_units params to circular=True/False, remove degrees section, and simplify multidim demo to differentiate circular and non-circular dimensions separately. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent fd456b3 commit 05dd4e4

3 files changed

Lines changed: 167 additions & 366 deletions

File tree

notebooks/7_circular_domain.ipynb

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "8b85da6f",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import numpy as np\n",
11+
"import pynumdiff\n",
12+
"\n",
13+
"from pynumdiff.utils import simulate, evaluate\n"
14+
]
15+
},
16+
{
17+
"cell_type": "markdown",
18+
"id": "7a77ab05",
19+
"metadata": {},
20+
"source": [
21+
"# Generate testing data\n",
22+
"\n",
23+
"Here we amplify the usual signal to get outside the -pi to pi bound."
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"id": "e351d4d1",
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"noise_type = 'normal' # noise is generated using np.random, e.g. 'normal', 'uniform', 'poisson'\n",
34+
"noise_parameters = [0, 0.5] # compatible with np.random functions \n",
35+
"random_seed = 1\n",
36+
"\n",
37+
"dt = 0.01 # step size and series length in terms of independent variable\n",
38+
"duration = 4\n",
39+
"\n",
40+
"x, x_truth, dxdt_truth = simulate.lorenz_x(duration=duration, dt=dt, outliers=False,\n",
41+
" noise_type=noise_type, noise_parameters=noise_parameters)\n",
42+
"\n",
43+
"# amplify signal\n",
44+
"gain = 4\n",
45+
"x_truth *= gain\n",
46+
"dxdt_truth *= gain\n",
47+
"\n",
48+
"# add noise\n",
49+
"x = simulate._add_noise(x_truth, random_seed, noise_type, noise_parameters)\n",
50+
"\n",
51+
"# wrap to [-pi, pi]\n",
52+
"x = (x + np.pi) % (2*np.pi) - np.pi\n",
53+
"x_truth = (x_truth + np.pi) % (2*np.pi) - np.pi\n"
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"id": "a4ae1732",
59+
"metadata": {},
60+
"source": [
61+
"# Naive numerical differentiation (without considering wrapping)"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"id": "cfd04e33",
68+
"metadata": {},
69+
"outputs": [],
70+
"source": [
71+
"x_hat, dxdt_hat = pynumdiff.kalman_smooth.rtsdiff(x, dt, 1, 5, True, axis=0, circular=False)\n",
72+
"x_hat_wrapped = (x_hat + np.pi) % (2*np.pi) - np.pi\n",
73+
"\n",
74+
"evaluate.plot(x, dt, x_hat_wrapped, dxdt_hat, x_truth, dxdt_truth)\n"
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"id": "e9a43fff",
80+
"metadata": {},
81+
"source": [
82+
"# Now with circular=True\n"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": null,
88+
"id": "17083a61",
89+
"metadata": {},
90+
"outputs": [],
91+
"source": [
92+
"x_hat, dxdt_hat = pynumdiff.kalman_smooth.rtsdiff(x, dt, 1, 3, True, axis=0, circular=True)\n",
93+
"x_hat_wrapped = (x_hat + np.pi) % (2*np.pi) - np.pi\n",
94+
"\n",
95+
"evaluate.plot(x, dt, x_hat_wrapped, dxdt_hat, x_truth, dxdt_truth)\n"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"id": "a49062c2",
101+
"metadata": {},
102+
"source": [
103+
"# Test multidimensional"
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": null,
109+
"id": "baf0a4a0",
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"v, v_truth, dvdt_truth = simulate.triangle(duration=duration, dt=dt, outliers=False,\n",
114+
" noise_type=noise_type, noise_parameters=noise_parameters)\n"
115+
]
116+
},
117+
{
118+
"cell_type": "code",
119+
"execution_count": null,
120+
"id": "9dc4418f",
121+
"metadata": {},
122+
"outputs": [],
123+
"source": [
124+
"X = np.vstack((x, v)).T\n",
125+
"print('Shape:', X.shape)\n"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": null,
131+
"id": "67ffcb8c",
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"# Differentiate circular and non-circular dimensions separately\n",
136+
"x_hat_col, dxdt_hat_col = pynumdiff.kalman_smooth.rtsdiff(X[:,0], dt, 1, 3, True, circular=True)\n",
137+
"v_hat_col, dvdt_hat_col = pynumdiff.kalman_smooth.rtsdiff(X[:,1], dt, 1, 3, True)\n",
138+
"\n",
139+
"x_hat_wrapped = (x_hat_col + np.pi) % (2*np.pi) - np.pi\n",
140+
"evaluate.plot(x, dt, x_hat_wrapped, dxdt_hat_col, x_truth, dxdt_truth)\n"
141+
]
142+
}
143+
],
144+
"metadata": {
145+
"kernelspec": {
146+
"display_name": "Python 3 (ipykernel)",
147+
"language": "python",
148+
"name": "python3"
149+
},
150+
"language_info": {
151+
"codemirror_mode": {
152+
"name": "ipython",
153+
"version": 3
154+
},
155+
"file_extension": ".py",
156+
"mimetype": "text/x-python",
157+
"name": "python",
158+
"nbconvert_exporter": "python",
159+
"pygments_lexer": "ipython3",
160+
"version": "3.13.3"
161+
}
162+
},
163+
"nbformat": 4,
164+
"nbformat_minor": 5
165+
}

notebooks/7_circular_variables.ipynb

Lines changed: 0 additions & 365 deletions
This file was deleted.

notebooks/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
| [Automatic Method Suggestion](https://github.com/florisvb/PyNumDiff/blob/master/notebooks/3_automatic_method_suggestion.ipynb) | A short demo of how to allow `pynumdiff` to choose a differentiation method for your data. |
88
| [Performance Analysis](https://github.com/florisvb/PyNumDiff/blob/master/notebooks/4_performance_analysis.ipynb) | Experiments to compare methods' accuracy and bias across simulations. |
99
| [Robustness to Outliers Demo](https://github.com/florisvb/PyNumDiff/blob/master/notebooks/5_robust_outliers_demo.ipynb) | This notebook shows a head-to-head of `RTSDiff`'s and `RobustDiff`'s minimum-RMSE performances on simulations with outliers, to illustrate the value of using a Huber loss in the Kalman MAP problem. |
10-
| [Multidimensionality Demo](https://github.com/florisvb/PyNumDiff/blob/master/notebooks/6_multidimensionality_demo.ipynb) | Demonstration of differentating multidimensional data along particular axes. |
10+
| [Multidimensionality Demo](https://github.com/florisvb/PyNumDiff/blob/master/notebooks/6_multidimensionality_demo.ipynb) | Demonstration of differentating multidimensional data along particular axes. |
11+
| [Circular Domain](https://github.com/florisvb/PyNumDiff/blob/master/notebooks/7_circular_domain.ipynb) | Shows how using |

0 commit comments

Comments
 (0)