Search code examples
pythonrmatlabrandomtopology

Creating a randomized circle from points in R/MatLab


I'm doing some research with a professor at my university, and he is asking that I create some data for the purposes of Topological Data Analysis (TDA).

I used two packages from R and MatLab, however the authors seem to have a similar idea of how to randomize the data when performing an operation that relates to persistence of Betti points.

The issue in R (and MatLab) is that the circle is created with:

X <- circleUnif(n=30)

This generates a circle with evenly spaced points (30) with equal radius around a central point. To randomize the data, both examples from the author of the packages randomly sample from the data. This leads to an image that looks like this: Alpha Complex What the professor asked is that I make it such that each point has some 'sigma' or deviation away from the radius. This would in essence create a 'fuzzy' ring with some degree of thickness. That way when persistence is performed on the data, the birth/death axes would have a more interesting output.

Around 2:30 in this video is an idea of what I'm trying to do: Persistent homology

If someone has an idea how to do this in Python, I'm willing to try other languages as well.


Solution

  • The following should work fine for you as long as you have numpy installed (using pip install numpy at the command line if you don't).

    import numpy as np
    
    def circleUnif(n=30, radius=1, sigma=0, center=(0, 0)):
        radii = np.random.randn(n) * sigma + radius
        angles = np.random.rand(n) * np.pi * 2
    
        x = radii * np.cos(angles)
        y = radii * np.sin(angles)
    
        return np.transpose([x, y]) + center
    

    As an example usage:

    >>> X = circleUnif(3, 1, 0, (2, 3))
    >>> print X
    [[1.29321773 3.70743115]
     [2.72817308 3.68539329]
     [2.35728855 2.06600595]]
    

    In my experience though, python does not have the same maturity as R for topological data analysis. There isn't anything wrong with using the data science tools available in python, and you could generate data with python for use in R if you would like. But this is one of the few instances where python is really lacking in quality, stable tooling for a data science task. I might make some eventually, or somebody else might, but you don't have many options for most TDA algorithms right now.