{"id":15362498,"url":"https://github.com/patrick-kidger/torchcubicspline","last_synced_at":"2025-11-17T15:32:29.311Z","repository":{"id":39373042,"uuid":"246104188","full_name":"patrick-kidger/torchcubicspline","owner":"patrick-kidger","description":"Interpolating natural cubic splines. Includes batching, GPU support, support for missing values, evaluating derivatives of the spline, and backpropagation.","archived":false,"fork":false,"pushed_at":"2022-10-04T15:06:59.000Z","size":36,"stargazers_count":269,"open_issues_count":9,"forks_count":23,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-11T22:56:49.752Z","etag":null,"topics":["interpolation","pytorch","spline"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patrick-kidger.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.txt","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["patrick-kidger"]}},"created_at":"2020-03-09T17:50:11.000Z","updated_at":"2025-10-07T08:33:19.000Z","dependencies_parsed_at":"2023-01-19T05:16:22.675Z","dependency_job_id":null,"html_url":"https://github.com/patrick-kidger/torchcubicspline","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/patrick-kidger/torchcubicspline","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrick-kidger%2Ftorchcubicspline","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrick-kidger%2Ftorchcubicspline/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrick-kidger%2Ftorchcubicspline/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrick-kidger%2Ftorchcubicspline/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patrick-kidger","download_url":"https://codeload.github.com/patrick-kidger/torchcubicspline/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrick-kidger%2Ftorchcubicspline/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279009159,"owners_count":26084554,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["interpolation","pytorch","spline"],"created_at":"2024-10-01T13:01:58.990Z","updated_at":"2025-10-11T22:56:51.937Z","avatar_url":"https://github.com/patrick-kidger.png","language":"Python","funding_links":["https://github.com/sponsors/patrick-kidger"],"categories":[],"sub_categories":[],"readme":"# torchcubicspline\nInterpolating natural cubic splines using PyTorch. Includes support for:\n- Batching\n- GPU support and backpropagation via PyTorch\n- Support for missing values (represent them as NaN)\n- Evaluating the first derivative of the spline\n\n## Installation\n\n```bash\npip install git+https://github.com/patrick-kidger/torchcubicspline.git\n```\n\n## Example\n\nSimple example:\n```python\nimport torch\nfrom torchcubicspline import(natural_cubic_spline_coeffs, \n                             NaturalCubicSpline)\n\nlength, channels = 7, 3\nt = torch.linspace(0, 1, length)\nx = torch.rand(length, channels)\ncoeffs = natural_cubic_spline_coeffs(t, x)\nspline = NaturalCubicSpline(coeffs)\npoint = torch.tensor(0.4)\nout = spline.evaluate(point)\n```\n\nWith multiple batch and evaluation dimensions:\n```python\nimport torch\nfrom torchcubicspline import(natural_cubic_spline_coeffs, \n                             NaturalCubicSpline)\n\nt = torch.linspace(0, 1, 7)\n# (2, 1) are batch dimensions. 7 is the time dimension\n# (of the same length as t). 3 is the channel dimension.\nx = torch.rand(2, 1, 7, 3)\ncoeffs = natural_cubic_spline_coeffs(t, x)\n# coeffs is a tuple of tensors\n\n# ...at this point you can save the coeffs, put them\n# through PyTorch's Datasets and DataLoaders, etc...\n\nspline = NaturalCubicSpline(coeffs)\n\npoint = torch.tensor(0.4)\n# will be a tensor of shape (2, 1, 3), corresponding to\n# batch, batch, and channel dimensions\nout = spline.derivative(point)\n\npoint = torch.tensor([[0.4, 0.5]])\n# will be a tensor of shape (2, 1, 1, 2, 3), corresponding to\n# batch, batch, time, time and channel dimensions\nout = spline.derivative(point)\n```\n\n## Functionality\n\nFunctionality is provided via the `natural_cubic_spline_coeffs` function and `NaturalCubicSpline` class.\n\n`natural_cubic_spline_coeffs` takes an increasing sequence of times represented by a tensor `t` of shape `(length,)` and some corresponding observations `x` of shape `(..., length, channels)`, where `...` are batch dimensions, and each `(length, channels)` slice represents a sequence of `length` points, each point with `channels` many values.\n\nThen calling\n```python\ncoeffs = natural_cubic_spline_coeffs(t, x)\nspline = NaturalCubicSpline(coeffs)\n```\nproduces an instance `spline` such that\n```\nspline.evaluate(t[i]) == x[..., i, :]\n```\nfor all `i`.\n\n#### Why is there a function and a class?\n\nThe slow bit is done during `natural_cubic_spline_coeffs`. The fast bit is `NaturalCubicSpline`. The returned `coeffs` are a tuple of PyTorch tensors, so you can take this opportunity to save or load them, push them through `torch.utils.data.Dataset` or `torch.utils.data.DataLoader`, etc.\n\n#### Derivatives\n\nThe derivative of the spline at a point may be calculated via `spline.derivative`. (Not be confused with backpropagation, which is also supported through both `spline.evaluate` and `spline.derivative`.)\n\n#### Missing values\n\nSupport for missing values is done by setting that element of `x` to `NaN`. In particular this allows for batching elements with different observation times: take `times` to be the observation times of all elements in the batch, and just set each element to have a missing observation `NaN` at the times of the observations of the other batch elements.\n\n## Limitations\n\nIf possible, you should cache the coefficients returned by `natural_cubic_spline_coeffs`. In particular if there are missing values then the computation can be quite slow.\n\n## Any issues?\n\nAny issues or questions - open an issue to let me know. :)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrick-kidger%2Ftorchcubicspline","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatrick-kidger%2Ftorchcubicspline","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrick-kidger%2Ftorchcubicspline/lists"}