Git Product home page Git Product logo

sphere's Introduction

PyPI version Build Status Python versions

Getting started

pip install fb8

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from sphere.distribution import fb8


def grid(npts):
    return [_.flatten() for _ in np.meshgrid(np.linspace(0, np.pi, npts), np.linspace(0,2*np.pi, npts))]


def plot_fb8(fb8, npts):
    """
    Plot fb8 on 3D sphere
    """
    xs = fb8.spherical_coordinates_to_nu(*grid(npts))
    pdfs = fb8.pdf(xs)
    z,x,y = xs.T #!!! Note the ordering for xs here is used consistently throughout. Follows Kent's 1982 paper.

    fig = plt.figure(figsize=plt.figaspect(1.))
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(x.reshape(npts, npts),
                    y.reshape(npts, npts),
                    z.reshape(npts, npts),
                    alpha=0.5,
                    rstride=1, cstride=1,
                    facecolors=cm.plasma(pdfs.reshape(npts, npts)/pdfs.max()))
    ax.set_axis_off()
    plt.tight_layout(-5)
    plt.show()


plot_fb8(fb8(np.pi/16,-np.pi/3,0,10,10,-1,0.5,0.3), 200)

Basic information

Implements calculation of the density and fitting (using maximum likelihood estimate) of the FB8 distribution on a sphere, which is a generalization of the FB6, FB5 (Kent), and FB4 (Bingham-Mardia) distributions described below.

Implements the FB6 distribution that is first introduced in Rivest (1984).

Implements calculation of the density and fitting (using maximum likelihood estimate) of the Kent distribution based on Kent (1982). A unittest is performed if distribution.py is called from the command line.

Implements the Bingham-Mardia distribution whose mode is a small-circle on the sphere based on Bingham, Mardia (1978).

Also calculates directional, percentile levels which can be used to indicate the N% highest-posterior-density regions in the sky.

maps

Additional references

Kent, Hussein, Jah, Directional distributions in tracking of space debris

Terdik, Jammalamadaka, Wainwright, Simulation and visualization of spherical distributions

Mardia, Jupp, Directional statistics

Notes

Currently the scipy.special.hyp2f1 is used and may exhibit inaccuracies for large parameters. See github issues.

Acknowledgements

This project was originally developed for the FB5 (Kent) distribution here.

Tianlu Yuan

sphere's People

Contributors

austinschneider avatar dependabot[bot] avatar edfraenkel avatar github-actions[bot] avatar tianluyuan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sphere's Issues

Question: Additional Guidance on Using Sphere

Hi! I stumbled upon this package, and it looks great. For my application, I have spatial ellipses (lat/long, major/minor axes, orientation) on the globe representing locations of some entities. I want to map these ellipses to distributions on a sphere so I can sample from them. I'm struggling to understand how to map the parameters I have to one of the constructors you provide. Are you aware of any references that may help? I've tried Googling to no avail! Either way, thank you so much for this package!

Possibility to pass numpy random generator as argument in rvs

Hello! I am using your package to sample quaternions in non isotropic distributions, in particular Kent distribution.

I would like to suggest a small addition to make sampling from seeded generators easier by changing the class method rvs(self, n_samples=None) to rvs(self, n_samples=None,random_gen=None) by adding the possibility to pass a Numpy generator as argument like the model of the rvs_continuous class of scipy.stats.

For instance, if using scipy.stats:

    def _rvs_helper(self,random_gen=None):
        num_samples = 10000
        xs = gauss(0, 1).rvs((num_samples, 3),random_state=random_gen)
        xs = np.divide(xs, np.reshape(norm(xs, 1), (num_samples, 1)))
        lpvalues = self.log_pdf(xs, normalize=False)
        lfmax = self.log_pdf_max(normalize=False)
        shifted = lpvalues - lfmax
        return xs[uniform(0, 1).rvs(num_samples,random_state=random_gen) < np.exp(shifted)]

Where random_state is eitheir a None, int or numpy.random.generator .

another possibility if using numpy.random

from numpy.random import default_gen

# [...]

    def _rvs_helper(self,random_gen=None):
        num_samples = 10000
        if random_gen is None:
            random_gen = default_gen()
        xs = random_gen.normal(0, 1,(num_samples,3))
        xs = np.divide(xs, np.reshape(norm(xs, 1), (num_samples, 1)))
        lpvalues = self.log_pdf(xs, normalize=False)
        lfmax = self.log_pdf_max(normalize=False)
        shifted = lpvalues - lfmax
        return xs[random_gen.uniform(0, 1, num_samples) < np.exp(shift)] 

Thank you for your package !

Get HGM evaluation working

I've moved the HGM code from master into a new branch git checkout --track origin/hgm. At some point we should get that working.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.