diff --git a/docs/tutorials/brownian_motion.py b/docs/tutorials/brownian_motion.py index f867cd2..642fe2a 100644 --- a/docs/tutorials/brownian_motion.py +++ b/docs/tutorials/brownian_motion.py @@ -56,7 +56,7 @@ # Call the model to generate 1000 steps. # %% -df_1d = bm_1d(n_steps=1000) +df_1d = bm_1d.generate_from(n_steps=1000) # %% px.line(df_1d, x="t", y="x_0") @@ -79,7 +79,7 @@ # We call the model to generate 1000 steps. # %% -df_2d = bm_2d(n_steps=500) +df_2d = bm_2d.generate_from(n_steps=500) # %% ( diff --git a/docs/tutorials/harmonic_oscillator_chain.py b/docs/tutorials/harmonic_oscillator_chain.py index 87d794f..3ca0999 100644 --- a/docs/tutorials/harmonic_oscillator_chain.py +++ b/docs/tutorials/harmonic_oscillator_chain.py @@ -113,22 +113,22 @@ # # This system can be solved in terms of _travelling waves_, obtained by discrete Fourier transform. # - +# # We can complexify the system # $$S_L[x_i] = S_L[x_i, \phi_j] \equiv S_L[X^\ast_i, X_j] = \int_{t_0}^{t_1}\mathbb{d} t \left\\{ \frac{1}{2}m \dot X^\ast_i \delta_{ij} \dot X_j - \frac{1}{2}m X^\ast_i A_{ij} X_j\right\\}\\,,$$ # where $A_{ij} / \omega^2$ is equal to $(-2)$ if $i=j$, $1$ if $|i-j|=1$ or $|i-j|=N$, and $0$ otherwise; # $X_i \coloneqq x_i \mathbb{e}^{-\phi_i}$, $X^\ast_i \coloneqq x_i \mathbb{e}^{+\phi_i}$. - +# # $A_{ij}$ can be diagonalised by the inverse discrete Fourier transform # $$X_i = (F^{-1})_{ik} Y_k = \frac{1}{\sqrt{N}}\sum_k \mathbb{e}^{i \frac{2\mathbb{\pi}}{N} k\mathbb{i}} Y_k\\,.$$ - +# # Calculating gives # $$S_L[X^\ast_i, X_j] = S_L[Y^\ast_i, Y_j] = \sum_{k=0}^{N-1} \int_{t_0}^{t_1}\mathbb{d} t \left\\{ \frac{1}{2}m \dot Y^\ast_k \dot Y_k - \frac{1}{2}m \omega^2\cdot4\sin^2\frac{2\mathbb{\pi}k}{N} Y^\ast_k Y_k\right\\}\\,.$$ # We can arrive at an action for the Fourier modes # $$S_L[Y, Y^*] = \sum_{k=0}^{N-1} \int_{t_0}^{t_1}\mathbb{d} t \left\\{ \frac{1}{2}m \dot Y_k^* Y_k - \frac{1}{2}m \omega^2\cdot4\sin^2\frac{2\mathbb{\pi}k}{N} Y_k^* Y_k\right\\}\\,.$$ - +# # The origional system can then be solved by $N$ complex oscillators. - +# # Since the original degrees of freedom are real, the initial conditions of the propagating waves need to satisfy # $Y_k = Y^*_{-k \mod N}$, see [Wikipedia](https://en.wikipedia.org/wiki/Discrete_Fourier_transform#DFT_of_real_and_purely_imaginary_signals). diff --git a/docs/tutorials/pendulum.py b/docs/tutorials/pendulum.py index fe4246b..77e0a07 100644 --- a/docs/tutorials/pendulum.py +++ b/docs/tutorials/pendulum.py @@ -46,7 +46,9 @@ # ## Data # %% -df_pen = pen(n_periods=n_periods, n_samples_per_period=n_samples_per_period) +df_pen = pen.generate_from( + n_periods=n_periods, n_samples_per_period=n_samples_per_period +) df_pen.head() # %% diff --git a/hamilflow/models/brownian_motion.py b/hamilflow/models/brownian_motion.py index 9a5a6b3..e0c40d6 100644 --- a/hamilflow/models/brownian_motion.py +++ b/hamilflow/models/brownian_motion.py @@ -6,6 +6,8 @@ import scipy as sp from pydantic import BaseModel, Field, computed_field, field_validator +from hamilflow.models.utils.typing import TypeTime + class BrownianMotionSystem(BaseModel): r"""Definition of the Brownian Motion system @@ -183,17 +185,28 @@ def _trajectory(self, n_new_steps: int, seed: int) -> np.ndarray: return trajectory - def __call__(self, n_steps: int, seed: int = 42) -> pd.DataFrame: - """Simulate the coordinates of the particle + def generate_from(self, n_steps: int, seed: int = 42) -> pd.DataFrame: + """generate data from a set of interpretable params for this model :param n_steps: total number of steps to be simulated, including the inital step. :param seed: random generator seed for the stochastic process. Use it to reproduce results. """ + time_steps = np.arange(0, n_steps) * self.system.delta_t + + return self(t=time_steps, seed=seed) + + def __call__(self, t: TypeTime, seed: int = 42) -> pd.DataFrame: + """Simulate the coordinates of the particle + + :param t: the time sequence to be used to generate data, 1-D array like + :param seed: random generator seed for the stochastic process. + Use it to reproduce results. + """ + n_steps = np.array(t).size trajectory = self._trajectory(n_new_steps=n_steps - 1, seed=seed) df = pd.DataFrame(trajectory, columns=self._axis_names) - - df["t"] = np.arange(0, n_steps) * self.system.delta_t + df["t"] = t return df diff --git a/hamilflow/models/pendulum.py b/hamilflow/models/pendulum.py index b97b064..291046e 100644 --- a/hamilflow/models/pendulum.py +++ b/hamilflow/models/pendulum.py @@ -8,6 +8,8 @@ from pydantic import BaseModel, Field, computed_field from scipy.special import ellipj, ellipk +from hamilflow.models.utils.typing import TypeTime + class PendulumSystem(BaseModel): r"""The params for the pendulum. @@ -130,11 +132,26 @@ def theta(self, t: "Sequence[float] | ArrayLike[float]") -> np.ndarray[float]: return 2 * np.arcsin(cn / dn * self._k) - def __call__(self, n_periods: int, n_samples_per_period: int) -> pd.DataFrame: + def generate_from(self, n_periods: int, n_samples_per_period: int) -> pd.DataFrame: + """generate the time sequence from more interpretable params + + :param n_periods: number of periods to include + :param n_samples_per_period: number of samples in each period + :return: an array that contains all the timesteps + """ time_delta = self.period / n_samples_per_period time_steps = np.arange(0, n_periods * n_samples_per_period) * time_delta - thetas = self.theta(time_steps) - us = self.u(time_steps) + return self(time_steps) + + def __call__(self, t: TypeTime) -> pd.DataFrame: + """generate the variables of the pendulum in time together with the time steps + + :param t: time steps + :return: values of the variables + angle `x`, generalized coordinates `u`, and time `t`. + """ + thetas = self.theta(t) + us = self.u(t) - return pd.DataFrame(dict(t=time_steps, x=thetas, u=us)) + return pd.DataFrame(dict(t=t, x=thetas, u=us)) diff --git a/hamilflow/models/utils/__init__.py b/hamilflow/models/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hamilflow/models/utils/typing.py b/hamilflow/models/utils/typing.py new file mode 100644 index 0000000..2c2da43 --- /dev/null +++ b/hamilflow/models/utils/typing.py @@ -0,0 +1,5 @@ +from typing import Sequence, TypeVar + +from numpy.typing import ArrayLike + +TypeTime = TypeVar("TypeTime", Sequence[float], Sequence[int], ArrayLike) diff --git a/poetry.lock b/poetry.lock index 8d84b2d..c283246 100644 --- a/poetry.lock +++ b/poetry.lock @@ -941,13 +941,13 @@ files = [ [[package]] name = "jupytext" -version = "1.16.3" +version = "1.16.4" description = "Jupyter notebooks as Markdown documents, Julia, Python or R scripts" optional = false python-versions = ">=3.8" files = [ - {file = "jupytext-1.16.3-py3-none-any.whl", hash = "sha256:870e0d7a716dcb1303df6ad1cec65e3315a20daedd808a55cb3dae2d56e4ed20"}, - {file = "jupytext-1.16.3.tar.gz", hash = "sha256:1ebac990461dd9f477ff7feec9e3003fa1acc89f3c16ba01b73f79fd76f01a98"}, + {file = "jupytext-1.16.4-py3-none-any.whl", hash = "sha256:76989d2690e65667ea6fb411d8056abe7cd0437c07bd774660b83d62acf9490a"}, + {file = "jupytext-1.16.4.tar.gz", hash = "sha256:28e33f46f2ce7a41fb9d677a4a2c95327285579b64ca104437c4b9eb1e4174e9"}, ] [package.dependencies] @@ -1219,29 +1219,24 @@ files = [ {file = "matplotlib-3.9.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:dd2a59ff4b83d33bca3b5ec58203cc65985367812cb8c257f3e101632be86d92"}, {file = "matplotlib-3.9.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0fc001516ffcf1a221beb51198b194d9230199d6842c540108e4ce109ac05cc0"}, {file = "matplotlib-3.9.1-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:83c6a792f1465d174c86d06f3ae85a8fe36e6f5964633ae8106312ec0921fdf5"}, - {file = "matplotlib-3.9.1-cp310-cp310-win_amd64.whl", hash = "sha256:421851f4f57350bcf0811edd754a708d2275533e84f52f6760b740766c6747a7"}, {file = "matplotlib-3.9.1-cp311-cp311-macosx_10_12_x86_64.whl", hash = "sha256:b3fce58971b465e01b5c538f9d44915640c20ec5ff31346e963c9e1cd66fa812"}, {file = "matplotlib-3.9.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:a973c53ad0668c53e0ed76b27d2eeeae8799836fd0d0caaa4ecc66bf4e6676c0"}, {file = "matplotlib-3.9.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:82cd5acf8f3ef43f7532c2f230249720f5dc5dd40ecafaf1c60ac8200d46d7eb"}, {file = "matplotlib-3.9.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ab38a4f3772523179b2f772103d8030215b318fef6360cb40558f585bf3d017f"}, {file = "matplotlib-3.9.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:2315837485ca6188a4b632c5199900e28d33b481eb083663f6a44cfc8987ded3"}, - {file = "matplotlib-3.9.1-cp311-cp311-win_amd64.whl", hash = "sha256:a0c977c5c382f6696caf0bd277ef4f936da7e2aa202ff66cad5f0ac1428ee15b"}, {file = "matplotlib-3.9.1-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:565d572efea2b94f264dd86ef27919515aa6d629252a169b42ce5f570db7f37b"}, {file = "matplotlib-3.9.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:6d397fd8ccc64af2ec0af1f0efc3bacd745ebfb9d507f3f552e8adb689ed730a"}, {file = "matplotlib-3.9.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:26040c8f5121cd1ad712abffcd4b5222a8aec3a0fe40bc8542c94331deb8780d"}, {file = "matplotlib-3.9.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d12cb1837cffaac087ad6b44399d5e22b78c729de3cdae4629e252067b705e2b"}, {file = "matplotlib-3.9.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:0e835c6988edc3d2d08794f73c323cc62483e13df0194719ecb0723b564e0b5c"}, - {file = "matplotlib-3.9.1-cp312-cp312-win_amd64.whl", hash = "sha256:44a21d922f78ce40435cb35b43dd7d573cf2a30138d5c4b709d19f00e3907fd7"}, {file = "matplotlib-3.9.1-cp39-cp39-macosx_10_12_x86_64.whl", hash = "sha256:0c584210c755ae921283d21d01f03a49ef46d1afa184134dd0f95b0202ee6f03"}, {file = "matplotlib-3.9.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:11fed08f34fa682c2b792942f8902e7aefeed400da71f9e5816bea40a7ce28fe"}, {file = "matplotlib-3.9.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:0000354e32efcfd86bda75729716b92f5c2edd5b947200be9881f0a671565c33"}, {file = "matplotlib-3.9.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4db17fea0ae3aceb8e9ac69c7e3051bae0b3d083bfec932240f9bf5d0197a049"}, {file = "matplotlib-3.9.1-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:208cbce658b72bf6a8e675058fbbf59f67814057ae78165d8a2f87c45b48d0ff"}, - {file = "matplotlib-3.9.1-cp39-cp39-win_amd64.whl", hash = "sha256:dc23f48ab630474264276be156d0d7710ac6c5a09648ccdf49fef9200d8cbe80"}, {file = "matplotlib-3.9.1-pp39-pypy39_pp73-macosx_10_15_x86_64.whl", hash = "sha256:3fda72d4d472e2ccd1be0e9ccb6bf0d2eaf635e7f8f51d737ed7e465ac020cb3"}, {file = "matplotlib-3.9.1-pp39-pypy39_pp73-macosx_11_0_arm64.whl", hash = "sha256:84b3ba8429935a444f1fdc80ed930babbe06725bcf09fbeb5c8757a2cd74af04"}, {file = "matplotlib-3.9.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b918770bf3e07845408716e5bbda17eadfc3fcbd9307dc67f37d6cf834bb3d98"}, - {file = "matplotlib-3.9.1-pp39-pypy39_pp73-win_amd64.whl", hash = "sha256:f1f2e5d29e9435c97ad4c36fb6668e89aee13d48c75893e25cef064675038ac9"}, {file = "matplotlib-3.9.1.tar.gz", hash = "sha256:de06b19b8db95dd33d0dc17c926c7c9ebed9f572074b6fac4f65068a6814d010"}, ] @@ -1653,6 +1648,7 @@ optional = false python-versions = ">=3.9" files = [ {file = "pandas-2.2.2-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:90c6fca2acf139569e74e8781709dccb6fe25940488755716d1d354d6bc58bce"}, + {file = "pandas-2.2.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:c7adfc142dac335d8c1e0dcbd37eb8617eac386596eb9e1a1b77791cf2498238"}, {file = "pandas-2.2.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4abfe0be0d7221be4f12552995e58723c7422c80a659da13ca382697de830c08"}, {file = "pandas-2.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8635c16bf3d99040fdf3ca3db669a7250ddf49c55dc4aa8fe0ae0fa8d6dcc1f0"}, {file = "pandas-2.2.2-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:40ae1dffb3967a52203105a077415a86044a2bea011b5f321c6aa64b379a3f51"}, @@ -1673,6 +1669,7 @@ files = [ {file = "pandas-2.2.2-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:43498c0bdb43d55cb162cdc8c06fac328ccb5d2eabe3cadeb3529ae6f0517c32"}, {file = "pandas-2.2.2-cp312-cp312-win_amd64.whl", hash = "sha256:d187d355ecec3629624fccb01d104da7d7f391db0311145817525281e2804d23"}, {file = "pandas-2.2.2-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:0ca6377b8fca51815f382bd0b697a0814c8bda55115678cbc94c30aacbb6eff2"}, + {file = "pandas-2.2.2-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:9057e6aa78a584bc93a13f0a9bf7e753a5e9770a30b4d758b8d5f2a62a9433cd"}, {file = "pandas-2.2.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:001910ad31abc7bf06f49dcc903755d2f7f3a9186c0c040b827e522e9cef0863"}, {file = "pandas-2.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:66b479b0bd07204e37583c191535505410daa8df638fd8e75ae1b383851fe921"}, {file = "pandas-2.2.2-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:a77e9d1c386196879aa5eb712e77461aaee433e54c68cf253053a73b7e49c33a"}, @@ -2255,7 +2252,6 @@ files = [ {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:69b023b2b4daa7548bcfbd4aa3da05b3a74b772db9e23b982788168117739938"}, {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:81e0b275a9ecc9c0c0c07b4b90ba548307583c125f54d5b6946cfee6360c733d"}, {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ba336e390cd8e4d1739f42dfe9bb83a3cc2e80f567d8805e11b46f4a943f5515"}, - {file = "PyYAML-6.0.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:326c013efe8048858a6d312ddd31d56e468118ad4cdeda36c719bf5bb6192290"}, {file = "PyYAML-6.0.1-cp310-cp310-win32.whl", hash = "sha256:bd4af7373a854424dabd882decdc5579653d7868b8fb26dc7d0e99f823aa5924"}, {file = "PyYAML-6.0.1-cp310-cp310-win_amd64.whl", hash = "sha256:fd1592b3fdf65fff2ad0004b5e363300ef59ced41c2e6b3a99d4089fa8c5435d"}, {file = "PyYAML-6.0.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:6965a7bc3cf88e5a1c3bd2e0b5c22f8d677dc88a455344035f03399034eb3007"}, @@ -2263,16 +2259,8 @@ files = [ {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:42f8152b8dbc4fe7d96729ec2b99c7097d656dc1213a3229ca5383f973a5ed6d"}, {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:062582fca9fabdd2c8b54a3ef1c978d786e0f6b3a1510e0ac93ef59e0ddae2bc"}, {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d2b04aac4d386b172d5b9692e2d2da8de7bfb6c387fa4f801fbf6fb2e6ba4673"}, - {file = "PyYAML-6.0.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:e7d73685e87afe9f3b36c799222440d6cf362062f78be1013661b00c5c6f678b"}, {file = "PyYAML-6.0.1-cp311-cp311-win32.whl", hash = "sha256:1635fd110e8d85d55237ab316b5b011de701ea0f29d07611174a1b42f1444741"}, {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, - {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, - {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, - {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a08c6f0fe150303c1c6b71ebcd7213c2858041a7e01975da3a99aed1e7a378ef"}, - {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, - {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, - {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, - {file = "PyYAML-6.0.1-cp312-cp312-win_amd64.whl", hash = "sha256:0d3304d8c0adc42be59c5f8a4d9e3d7379e6955ad754aa9d6ab7a398b59dd1df"}, {file = "PyYAML-6.0.1-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:50550eb667afee136e9a77d6dc71ae76a44df8b3e51e41b77f6de2932bfe0f47"}, {file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1fe35611261b29bd1de0070f0b2f47cb6ff71fa6595c077e42bd0c419fa27b98"}, {file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:704219a11b772aea0d8ecd7058d0082713c3562b4e271b849ad7dc4a5c90c13c"}, @@ -2289,7 +2277,6 @@ files = [ {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a0cd17c15d3bb3fa06978b4e8958dcdc6e0174ccea823003a106c7d4d7899ac5"}, {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:28c119d996beec18c05208a8bd78cbe4007878c6dd15091efb73a30e90539696"}, {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7e07cbde391ba96ab58e532ff4803f79c4129397514e1413a7dc761ccd755735"}, - {file = "PyYAML-6.0.1-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:49a183be227561de579b4a36efbb21b3eab9651dd81b1858589f796549873dd6"}, {file = "PyYAML-6.0.1-cp38-cp38-win32.whl", hash = "sha256:184c5108a2aca3c5b3d3bf9395d50893a7ab82a38004c8f61c258d4428e80206"}, {file = "PyYAML-6.0.1-cp38-cp38-win_amd64.whl", hash = "sha256:1e2722cc9fbb45d9b87631ac70924c11d3a401b2d7f410cc0e3bbf249f2dca62"}, {file = "PyYAML-6.0.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:9eb6caa9a297fc2c2fb8862bc5370d0303ddba53ba97e71f08023b6cd73d16a8"}, @@ -2297,7 +2284,6 @@ files = [ {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5773183b6446b2c99bb77e77595dd486303b4faab2b086e7b17bc6bef28865f6"}, {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b786eecbdf8499b9ca1d697215862083bd6d2a99965554781d0d8d1ad31e13a0"}, {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bc1bf2925a1ecd43da378f4db9e4f799775d6367bdb94671027b73b393a7c42c"}, - {file = "PyYAML-6.0.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:04ac92ad1925b2cff1db0cfebffb6ffc43457495c9b3c39d3fcae417d7125dc5"}, {file = "PyYAML-6.0.1-cp39-cp39-win32.whl", hash = "sha256:faca3bdcf85b2fc05d06ff3fbc1f83e1391b3e724afa3feba7d13eeab355484c"}, {file = "PyYAML-6.0.1-cp39-cp39-win_amd64.whl", hash = "sha256:510c9deebc5c0225e8c96813043e62b680ba2f9c50a08d3724c7f28a747d1486"}, {file = "PyYAML-6.0.1.tar.gz", hash = "sha256:bfdf460b1736c775f2ba9f6a92bca30bc2095067b8a9d77876d1fad6cc3b4a43"}, @@ -2972,4 +2958,4 @@ dev = ["black (>=19.3b0)", "pytest (>=4.6.2)"] [metadata] lock-version = "2.0" python-versions = ">=3.10,<3.13" -content-hash = "f11275b268de798415ada707d667e6636ba74100b07bde00101de6a571ce0499" +content-hash = "8a0fcda5898fb0bba8e4d8015d8d216f144254d76823ba52f909ee6dc3e3a0ef" diff --git a/pyproject.toml b/pyproject.toml index 6263e77..3ee33e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ mkdocs-material = "^9.5.30" mkdocs-autorefs = "^1.0.1" mkdocstrings = {version = "^0.25.2", extras = ["python"]} mkdocs-jupyter = "^0.24.8" +jupytext = "^1.16.4" [tool.poetry.group.tutorial.dependencies] diff --git a/tests/test_models/test_brownian_motion.py b/tests/test_models/test_brownian_motion.py index e3f4490..01a5054 100644 --- a/tests/test_models/test_brownian_motion.py +++ b/tests/test_models/test_brownian_motion.py @@ -63,7 +63,7 @@ def test_brownian_motion_system_failed_spec(sigma, delta_t): ), ], ) -def test_brownian_motion(sigma, x0, expected): +def test_brownian_motion_generate_from(sigma, x0, expected): system = { "sigma": sigma, @@ -75,7 +75,62 @@ def test_brownian_motion(sigma, x0, expected): bm = BrownianMotion(system=system, initial_condition=initial_condition) pd.testing.assert_frame_equal( - bm(n_steps=5, seed=42), + bm.generate_from(n_steps=5, seed=42), + pd.DataFrame(expected), + check_exact=False, + check_like=True, + ) + + +@pytest.mark.parametrize( + "sigma, x0, t, expected", + [ + pytest.param( + 1, + 0, + [0.0, 1.0, 2.0, 3.0, 4.0], + { + "t": [0.0, 1.0, 2.0, 3.0, 4.0], + "x_0": [0.0, 0.49671415, 0.35844985, 1.00613839, 2.52916825], + }, + id="1d-5-steps", + ), + pytest.param( + 1, + [1, 1], + [0.0, 1.0, 2.0, 3.0, 4.0], + { + "t": [0.0, 1.0, 2.0, 3.0, 4.0], + "x_0": [1.0, 1.49671415, 2.14440269, 1.91024932, 3.48946213], + "x_1": [1.0, 0.8617357, 2.38476556, 2.1506286, 2.91806333], + }, + id="2d-5-steps", + ), + pytest.param( + 1, + 0, + 0.0, + { + "t": [0.0], + "x_0": [0.0], + }, + id="1d-scaler-t", + ), + ], +) +def test_brownian_motion(sigma, x0, t, expected): + + system = { + "sigma": sigma, + "delta_t": 1, + } + + initial_condition = {"x0": x0} + + bm = BrownianMotion(system=system, initial_condition=initial_condition) + + pd.testing.assert_frame_equal( + bm(t=t, seed=42), pd.DataFrame(expected), check_exact=False, check_like=True, diff --git a/tests/test_models/test_pendulum.py b/tests/test_models/test_pendulum.py index a077b65..323f897 100644 --- a/tests/test_models/test_pendulum.py +++ b/tests/test_models/test_pendulum.py @@ -70,3 +70,9 @@ def test_period_dynamic_theta( theta, theta_1 = p.theta(times), p.theta(arr_times_1) assert_array_almost_equal(theta, theta_1) + + def test_generate_from(self, omega0: float, theta0: float) -> None: + p = Pendulum(omega0, theta0) + + df = p.generate_from(n_periods=1, n_samples_per_period=10) + assert len(df) == 10