-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 73b2ab7
Showing
15 changed files
with
775 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
codecov: | ||
token: ad953cd4-94de-4240-98cd-fe7037ca4277 | ||
ignore: | ||
- "examples" | ||
- "tests" | ||
- "setup.py" | ||
comment: | ||
layout: "reach, diff, flags, files" | ||
behavior: default | ||
require_changes: false # if true: only post the comment if coverage changes | ||
require_base: no # [yes :: must have a base report to post] | ||
require_head: yes # [yes :: must have a head report to post] | ||
branches: # branch names that can post comment | ||
- "main" | ||
github_checks: | ||
annotations: false | ||
coverage: | ||
status: | ||
project: | ||
default: | ||
target: auto | ||
threshold: 80% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: Lint | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-python@v2 | ||
- uses: psf/black@stable | ||
with: | ||
args: ". --check" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Run Tests | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
build-images: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest] | ||
#os: [ubuntu-latest, macos-latest, windows-latest] | ||
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"] | ||
env: | ||
os: ${{ matrix.os }} | ||
python: ${{ matrix.python-version }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
submodules: true | ||
- uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install | ||
run: | | ||
pip install -e .[dev] | ||
- name: Execute tests | ||
run: | | ||
pytest --cov=ndc --cov-report=xml | ||
- name: Upload Coverage to Codecov | ||
uses: codecov/codecov-action@v1 | ||
with: | ||
file: ./coverage.xml | ||
flags: unittests | ||
env_vars: OS,PYTHON | ||
fail_ci_if_error: true | ||
verbose: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
pip-wheel-metadata/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# IDEs | ||
.idea | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Fabio Muratore | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Numerical Differentiation Leveraging Convolution (ndc) | ||
|
||
[![License](https://img.shields.io/badge/license-MIT-brightgreen)](https://opensource.org/licenses/MIT) | ||
[![codecov](https://codecov.io/gh/famura/ndc/branch/main/graph/badge.svg?token=ESUTNFwtYY)](https://codecov.io/gh/famura/ndc) | ||
[![isort](https://img.shields.io/badge/imports-isort-green)](https://pycqa.github.io/isort/) | ||
[![codestyle](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) | ||
|
||
**What for?** | ||
|
||
Differentiate signals stored as PyTorch tensors, e.g. measurements obtained from a device or simulation, where automatic differentiation can not be applied. | ||
|
||
**Features** | ||
|
||
* Theoretically **any order, any stencils, and any step size** (see [this Wiki page](https://en.wikipedia.org/wiki/Finite_difference_coefficient) for information). Be aware that there are numerical limits when computing the filter kernel's coefficients, e.g. small step sized and high orders lead to numerical issues. | ||
* Works for **multidimensional signals**, assuming that all dimensions share the same step size. | ||
* Computations can be executed on **CUDA**. However, this has not been tested extensively. | ||
* Straightforward implementation which you can easily adapt to your needs. | ||
|
||
**How?** | ||
|
||
The idea of this small repository is to use the duality between convolution, i.e., filtering, and [numerical differentiation](https://en.wikipedia.org/wiki/Numerical_differentiation) to leverage the existing functions for 1-dimensional convolution in order to compute the (time) derivatives. | ||
|
||
**Why PyTorch?** | ||
|
||
More often then not I received (recorded) simulation data as PyTorch tensors rather than numpy arrays. | ||
Thus, I think it is nice to have a function to differentiate measurement signals without switching the data type or computation device. | ||
Moreover, the `torch.conv1d` function fits perfectly for this purpose. | ||
|
||
|
||
## Citing | ||
|
||
If you use code or ideas from this repository for your projects or research, please cite it. | ||
``` | ||
@misc{Muratore_ncd, | ||
author = {Fabio Muratore}, | ||
title = {ndc - Numerical differentiation leveraging convolutions}, | ||
year = {2022}, | ||
publisher = {GitHub}, | ||
journal = {GitHub repository}, | ||
howpublished = {\url{https://github.com/famura/ndc}} | ||
} | ||
``` | ||
|
||
## Installation | ||
|
||
To install the core part of the package run | ||
``` | ||
pip install ndc | ||
``` | ||
|
||
For (local) development install the dependencies with | ||
``` | ||
pip install -e .[dev] | ||
``` | ||
|
||
## Usage | ||
|
||
Consider a signal `x`, e.g. a measurement you obtained form a device. This package assumes that the signal to differentiate is of shape `(num_steps, dim_data)` | ||
|
||
```python | ||
import torch | ||
import ndc | ||
|
||
# Assuming you got x(t) from somewhere. | ||
assert isinstance(x, torch.Tensor) | ||
num_steps, dim_data = x.shape | ||
|
||
# Specify the derivative. Here, the first order central derivative. | ||
stencils = [-1, 0, 1] | ||
order = 1 | ||
step_size = dt # should be known from your signal x(t), else use 1 | ||
padding = True # if true, the initial and final values are repeated as often as necessary to match the length of x | ||
|
||
dx_dt_num = ndc.differentiate_numerically(x, stencils, order, step_size, padding) | ||
assert dx_dt_num.device == x.device | ||
if padding: | ||
assert dx_dt_num.shape == (num_steps, dim_data) | ||
else: | ||
assert dx_dt_num.shape == (num_steps - sum(s != 0 for s in stencils), dim_data) | ||
``` | ||
|
||
|
||
## Contributions | ||
|
||
Maybe you want another padding mode, or you found a way to improve the CUDA support. Please feel free to leave a pull request or issue. |
Oops, something went wrong.