• Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    Python
  • License
    Other
  • Created almost 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Methods for numerical differentiation of noisy data in python

PyNumDiff

Python methods for numerical differentiation of noisy data, including multi-objective optimization routines for automated parameter selection.

Python for Numerical Differentiation of noisy time series data

Documentation Status PyPI version DOI

Table of contents

Introduction

PyNumDiff is a Python package that implements various methods for computing numerical derivatives of noisy data, which can be a critical step in developing dynamic models or designing control. There are four different families of methods implemented in this repository: smoothing followed by finite difference calculation, local approximation with linear models, Kalman filtering based methods and total variation regularization methods. Most of these methods have multiple parameters involved to tune. We take a principled approach and propose a multi-objective optimization framework for choosing parameters that minimize a loss function to balance the faithfulness and smoothness of the derivative estimate. For more details, refer to this paper.

Structure

PyNumDiff/
  |- README.md
  |- pynumdiff/
     |- __init__.py
     |- __version__.py
     |- finite_difference/
     |- kalman_smooth/
     |- linear_model/
     |- smooth_finite_difference/
     |- total_variation_regularization/
     |- utils/
     |- optimize/
        |- __init__.py
        |- __optimize__.py
        |- finite_difference/
        |- kalman_smooth/
        |- linear_model/
        |- smooth_finite_difference/
        |- total_variation_regularization/
     |- tests/
  |- examples
     |- 1_basic_tutorial.ipynb
     |- 2a_optimizing_parameters_with_dxdt_known.ipynb
     |- 2b_optimizing_parameters_with_dxdt_unknown.ipynb
  |- docs/
     |- Makefile
     |- make.bat
     |- build/
     |- source/
        |- _static
        |- _summaries
        |- conf.py
        |- index.rst
        |- ...
  |- .gitignore
  |- .travis.yml
  |- LICENSE.txt
  |- requirements.txt

Citation

See CITATION.cff file as well as the following references.

PyNumDiff python package:

@article{PyNumDiff2022,
  doi = {10.21105/joss.04078},
  url = {https://doi.org/10.21105/joss.04078},
  year = {2022},
  publisher = {The Open Journal},
  volume = {7},
  number = {71},
  pages = {4078},
  author = {Floris van Breugel and Yuying Liu and Bingni W. Brunton and J. Nathan Kutz},
  title = {PyNumDiff: A Python package for numerical differentiation of noisy time-series data},
  journal = {Journal of Open Source Software}
}

Optimization algorithm:

@article{ParamOptimizationDerivatives2020, 
doi={10.1109/ACCESS.2020.3034077}
author={F. {van Breugel} and J. {Nathan Kutz} and B. W. {Brunton}}, 
journal={IEEE Access}, 
title={Numerical differentiation of noisy data: A unifying multi-objective optimization framework}, 
year={2020}
}

Getting Started

Prerequisite

PyNumDiff requires common packages like numpy, scipy, matplotlib, pytest (for unittests), pylint (for PEP8 style check). For a full list, you can check the file requirements.txt

In addition, it also requires certain additional packages for select functions, though these are not required for a successful install of PyNumDiff:

  • Total Variation Regularization methods: cvxpy

When using cvxpy, our default solver is set to be MOSEK (highly recommended), you would need to download their free academic license from their website. Otherwise, you can also use other solvers which are listed here.

Installing

The code is compatible with >=Python 3.5. It can be installed using pip or directly from the source code. Basic installation options include:

  • From PyPI using pip: pip install pynumdiff.
  • From source using pip git+: pip install git+https://github.com/florisvb/PyNumDiff
  • From local source code using setup.py: Run pip install . from inside this directory. See below for example.

For additional solvers, run pip install pynumdiff[advanced]. This includes cvxpy, which can be tricky when compiling from source. If an error occurs in installing cvxpy, see cvxpy install documentation, install cvxpy according to those instructions, and try pip install pynumdiff[advanced] again.

Note: If using the optional MOSEK solver for cvxpy you will also need a MOSEK license, free academic license.

Usage

PyNumDiff uses Sphinx for code documentation. So you can see more details about the API usage there.

Basic usages

  • Basic Usage: you provide the parameters
        x_hat, dxdt_hat = pynumdiff.sub_module.method(x, dt, params, options)     
  • Intermediate usage: automated parameter selection through multi-objective optimization
        params, val = pynumdiff.optimize.sub_module.method(x, dt, params=None, 
                                                           tvgamma=tvgamma, # hyperparameter
                                                           dxdt_truth=None, # no ground truth data
                                                           options={})
        print('Optimal parameters: ', params)
        x_hat, dxdt_hat = pynumdiff.sub_module.method(x, dt, params, options={'smooth': True})`
  • Advanced usage: automated parameter selection through multi-objective optimization using a user-defined cutoff frequency
        # cutoff_freq: estimate by (a) counting the number of true peaks per second in the data or (b) look at power spectra and choose cutoff
        log_gamma = -1.6*np.log(cutoff_frequency) -0.71*np.log(dt) - 5.1 # see: https://ieeexplore.ieee.org/abstract/document/9241009
        tvgamma = np.exp(log_gamma) 

        params, val = pynumdiff.optimize.sub_module.method(x, dt, params=None, 
                                                           tvgamma=tvgamma, # hyperparameter
                                                           dxdt_truth=None, # no ground truth data
                                                           options={})
        print('Optimal parameters: ', params)
        x_hat, dxdt_hat = pynumdiff.sub_module.method(x, dt, params, options={'smooth': True})`

Notebook examples

We will frequently update simple examples for demo purposes, and here are currently exisiting ones:

Important notes

  • Larger values of tvgamma produce smoother derivatives
  • The value of tvgamma is largely universal across methods, making it easy to compare method results
  • The optimization is not fast. Run it on subsets of your data if you have a lot of data. It will also be much faster with faster differentiation methods, like savgoldiff and butterdiff, and probably too slow for sliding methods like sliding DMD and sliding LTI fit.
  • The following heuristic works well for choosing tvgamma, where cutoff_frequency is the highest frequency content of the signal in your data, and dt is the timestep: tvgamma=np.exp(-1.6*np.log(cutoff_frequency)-0.71*np.log(dt)-5.1)

Running the tests

We are using Travis CI for continuous intergration testing. You can check out the current status here.

To run tests locally, type:

> pytest pynumdiff

License

This project utilizes the MIT LICENSE. 100% open-source, feel free to utilize the code however you like.

More Repositories

1

multi_tracker

Multiple object tracking for single camera in ROS.
Python
39
star
2

DMDbookJupyter

Jupyter Notebook Equivalents of the Matlab Code associated with the DMD book (Kutz, Brunton, Brunton, Proctor)
Jupyter Notebook
7
star
3

camera_aravis

camera aravis ROS driver for GigE cameras
C++
3
star
4

FlyView

Python scripts to render fly-view stills and movies using blender
Python
3
star
5

tcp_to_arduino_example

Example of a TCP server for Arduino Uno and Ethernet Shield, along with a ROS server for full ROS integration.
3
star
6

cs154ai

Python
3
star
7

pyUKFsqrt

Python implementation of the sqrt Unscented Kalman Filter using MATLAB style cholesky update
Python
2
star
8

ros_hydra

ros implementation of hydra
Python
2
star
9

flydra

misc bits of flydra
2
star
10

floris_ros_flydra

floris ros flydra stuff
Python
2
star
11

flybot

tcp controlled robot
Python
2
star
12

DataFit

various data fitting funcs
Python
2
star
13

FlydraAnalysisTools

A proper python package for analysis tools written for flydra data
Python
2
star
14

pyEKF

Dead simple discrete extended kalman filter
Python
2
star
15

dyneye

Dynamic Eye
Python
2
star
16

Lie_Algebra

Lie Algebra
Python
2
star
17

landing_paper_analysis

Python
1
star
18

odor_tracking

Python
1
star
19

OdorAnalysis

analysis code for odor exps
Python
1
star
20

FlyPlotLib

matplotlib plotting functions for making awesome plots
Python
1
star
21

OdorPackets

Python
1
star
22

analysis

flydra analysis, landing
Python
1
star
23

odor_control_analysis

Python
1
star
24

gps_wind_station

Data logging trisonica wind station data with GPS time stamps using teensy firmware and python analysis
Jupyter Notebook
1
star
25

ros_flydra

ROS support for flydra
Python
1
star
26

floris

basic scripts for a variety of things
Python
1
star
27

shellscripts

Shell
1
star
28

hydragui

gui for hydra tracking
Python
1
star
29

flydra_analysis_tools

Tools for analyzing flydra trajectories
Python
1
star
30

RaspiTracker

C
1
star
31

braid_trigger

Example for using ROS to trigger on braid trajectories
Python
1
star
32

hydra

hydra code
Python
1
star
33

pymovie

Python
1
star
34

windobs

Supplemental analysis for wind estimation paper
Jupyter Notebook
1
star
35

landing_analysis

Python
1
star
36

nano_odor_control

Arduino firmware for controlling odor pulse and reading odor signal
Python
1
star
37

phidget_stepper_velocity_control

Example ROS node for controlling a phidgets stepper motor
CMake
1
star
38

joystick_ps3

ps3 controller for ros
Python
1
star
39

nonlinear_controls

1
star
40

ArduinoDAQ

Simple Arduino firmware with python and ros api for recording and streaming sensor data
C++
1
star
41

Bronkhorst

Python API for Bronkhorst flow controllers
Python
1
star
42

FlyPlumeTracking

Simple simulation to demonstrate the quantitative importance of parameters in a fruit fly's odor plume tracking behavior, as described in (van Breugel and Dickinson, in review).
Python
1
star
43

alicate_flow_control

ros package for controlling alicat flow controllers
Python
1
star
44

landing_model

landing model code
Python
1
star
45

visionegg

visionegg scripts
Python
1
star
46

flivver

Insect inspired visual-based velocity estimation through spatial pooling of optic flow during linear motion
Jupyter Notebook
1
star
47

active_anemosensing

Active Anemosensing Hypothesis: How Flying Insects Could Estimate Ambient Wind Direction Through Sensory Integration & Active Movement
Jupyter Notebook
1
star
48

ps3_interpreter

playstation controller interpreter and sender
Python
1
star
49

pymovie2

new, better, version
Python
1
star
50

braid_tunnel

ROS nodes and generic analysis code for braid
Jupyter Notebook
1
star
51

Flyatar

Flyatar Project
Python
1
star
52

floris_functions

General analysis and plotting functions
Python
1
star
53

flydrome

Python
1
star