Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More fitting algorithms and type hints #10

Merged
merged 11 commits into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
161 changes: 160 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,160 @@
__pycache__/
# 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
sharepython-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 dateother 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
cover

# 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
.pybuilder
target

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in
# .python-version

# pipenv
# According to pypapipenv#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

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# httpspython-poetry.orgdocsbasic-usage#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# httpspdm.fming.dev#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.comDavid-OConnorpyflow and github.compdm-projectpdm
__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

# pytype static type analyzer
.pytype

# Cython debug symbols
cython_debug

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at httpsgithub.comgithubgitignoreblobmainGlobalJetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/circle-fit2.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2019 Michael Klear
Copyright (c) 2022 Magne Lauritzen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
60 changes: 47 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
# Circle-Fit
## Simple Circle Fitting Library for Python
Fitting circles is a simple problem. Given a set of points in 2-D space, let's find a "best fit" circle. A simple least-squares algorithm is a simple and effective solution. A novel algorithm came out in 2011 called ["Hyper Fit"](https://www.sciencedirect.com/science/article/pii/S0167947310004809?via%3Dihub) by Kanatani, et al.
# Circle-Fit 2
## A Circle Fitting Library for Python
Given a collection of points in 2D space, a common problem is finding the parameters of a circle that best approximate
these points. This library implements a collection of different circle fitting algorithms:

```
- hyperLSQ() : Least squares circle fit with "hyperaccuracy" by Kenichi Kanatani, Prasanna Rangarajan
- standardLSQ() : Least squares circle fit, standard version.
- riemannSWFLa() : Riemann circle fit, SWFL version A
- lm() : Levenberg-Marquardt in the full (a,b,R) parameter space
- prattSVD() : Algebraic circle fit by V. Pratt
- taubinSVD() : Algebraic circle fit by G. Taubin
- hyperSVD() : Algebraic circle fit with "hyperaccuracy"
- kmh() : Consistent circle fit by A. Kukush, I. Markovsky, S. Van Huffel
```

Most of these algorithms are based on the original MATLAB implementations by Nikolai Chernov:
https://people.cas.uab.edu/~mosya/cl/MATLABcircle.html

Each algorithm may work better in specific cases. If you are in doubt about which to use, `taubinSVD()`
is a good starting point.

## Installation
`circle-fits2` is not yet available from PyPi. Coming very soon!

## Example
Fit a circle to four `(x,y)` points.
```
pip install circle-fit
from circle-fit2 import taubinSVD
point_coordinates = [[1, 0], [-1, 0], [0, 1], [0, -1]]
xc, yc, r, sigma = taubinSVD(point_coordinates)
```

## Usage
### Prepare the Data
Your data must have at least two points in 2-D space. Circle-fit expects an array (or similar structure) of shape `(n, 2)`, where n is the number of points in your dataset.<br>
There are two algorithms available: `hyper_fit` and `least_squares_circle`. Both return four values:
## Data format
Your data must have at least two points in 2-D space. The algorithms in `circle-fit2` expects either
a 2D List or numpy ndarray of shape `(n, 2)`, where n is the number of points in your dataset.

All the algorithms available in this library return four values:
```
- xc : x-coordinate of solution center (float)
- yc : y-coordinate of solution center (float)
- R : Radius of solution (float)
- variance or residual (float)
- xc : x-coordinate of solution center (float)
- yc : y-coordinate of solution center (float)
- r : Radius of solution (float)
- sigma : Residual error of solution (float)
```

### View the fit
The function `plot_data_circle(coords, xc, yc, r)` can be used to open a plot window which shows you data points with
a circle fit overlaid on top. Example use:
```
xc, yc, r, sigma = taubinSVD(point_coordinates)
plot_data_circle(point_coordinates, xc, yc, r)
```

### Contributors and Maintainers
Expand All @@ -28,4 +61,5 @@ Please open a pull request with the changes you would like to contribute ([examp
As we are volunteers, please be patient when requesting support. You can either open an issue if you think you've found a bug with the code, or contact one of us directly if you have a user issue:

- michael.r.klear@gmail.com
- etc.
- mag.lauritzen@gmail.com

1 change: 0 additions & 1 deletion circle_fit/__init__.py

This file was deleted.