Skip to content

Instantly share code, notes, and snippets.

View CarstenSchelp's full-sized avatar

Carsten Schelp CarstenSchelp

View GitHub Profile
@CarstenSchelp
CarstenSchelp / Extract ellipse radii of covariance ellipse
Last active May 14, 2023 12:45
As a supplement to the plot_confidence_ellipse function in this gist: https://gist.github.com/CarstenSchelp/b992645537660bda692f218b562d0712 I have modified the same function here such that it also returns the radii of the ellipse. For instance to calculate the area of the ellipse.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import matplotlib.transforms as transforms
def confidence_ellipse(x, y, ax, n_std=3.0, facecolor='none', **kwargs):
"""
Create a plot of the covariance confidence ellipse of `x` and `y`
See how and why this works: https://carstenschelp.github.io/2018/09/14/Plot_Confidence_Ellipse_001.html
@CarstenSchelp
CarstenSchelp / covariance_ellipse_as_lissajous_curve.py
Last active May 14, 2023 12:48
Plotting single points of a covariance ellipse using a Lissajous principle
import numpy as np
from numpy import cos, sin
import matplotlib.pyplot as plt
"""
Looking at the covariance ellipse as a Lissajous figure.
In this gist: https://gist.github.com/CarstenSchelp/b992645537660bda692f218b562d0712
I plot a 2-D covariance ellipse by
- normalizing the covariance matrix to contain only two ones, on the diagonal, and a pearson coefficient
@CarstenSchelp
CarstenSchelp / arctan2_2pi.py
Last active March 1, 2021 20:02
convert arctan2 result from -1*pi .. +1*pi to 0 .. 2*pi
# The arctan2 funtion returns an angle between -PI and +PI (radians)
# If you need a range from zero to 2PI, this is how the
# arctan2 result can be converted.
# Zero still means "aligned with the 'x' axis"
# And the angle is still to be interpreted counter-clockwise.
import numpy as np
def arctan2_2pi(y, x):
return -1 * (
@CarstenSchelp
CarstenSchelp / IPyFileSelectionWidget.py
Last active November 29, 2019 15:47
Simple interactive IPython- or Jupyter Notebook widget-based file selection dialog
'''
A simple interactive file selection class based on IPython "Selection" widget.
Navigates through directories and displays all files that match the "file_pattern"
parameter.
'''
import ipywidgets as widgets
import glob
import os
class FileSelection:
@CarstenSchelp
CarstenSchelp / gist:b6fb490e8cc65e3ec7d62b950d1f3ad8
Created November 19, 2019 15:10
Convert .net / C# time ticks to posix or python datetime
'''
Convert .net-style ticks to posix or python datetime.
.net ticks (like in C#) are 100 nanosecond counts
starting from january 1st in year 0001
This one is not very thorougly tested but worked for me when I needed it.
'''
import datetime
_epoch = datetime.date(1970, 1 , 1)
_dotnet_minvalue = datetime.date(1, 1, 1)
@CarstenSchelp
CarstenSchelp / online_covariance.py
Last active May 8, 2019 14:00
Online Covariance Implementation as given on Wikipedia
import numpy as np
class OnlineCovariance:
"""
A class to calculate the mean and the covariance matrix
of the incrementally added data.
The implementation is fully vectorized, which means that
no for-loops or indices are involved when processing
vectors and matrices.
This gist attempts to cleanly implement and extend this
@CarstenSchelp
CarstenSchelp / plot_confidence_ellipse.py
Last active October 20, 2023 10:42
A function to plot the confidence ellipse of the covariance of a 2D dataset. Uses matplotlib.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import matplotlib.transforms as transforms
def confidence_ellipse(x, y, ax, n_std=3.0, facecolor='none', **kwargs):
"""
Create a plot of the covariance confidence ellipse of `x` and `y`
See how and why this works: https://carstenschelp.github.io/2018/09/14/Plot_Confidence_Ellipse_001.html