Skip to main content

Spatial objects and computations based on NumPy arrays.

Project description

images/logo.svg
https://img.shields.io/pypi/pyversions/scikit-spatial.svg https://img.shields.io/travis/ajhynes7/scikit-spatial.svg Documentation Status https://pyup.io/repos/github/ajhynes7/scikit-spatial/shield.svg https://codecov.io/gh/ajhynes7/scikit-spatial/branch/master/graph/badge.svg

Introduction

This package provides spatial objects (Point, Points, Vector, Line, and Plane) based on NumPy arrays, as well as computations using these objects. The package includes computations for 2D, 3D, and higher-dimensional space.

The computations can be grouped into the following main categories:

  • Measurement

  • Comparison

  • Projection

  • Intersection

  • Fitting

  • Transformation

The package has been built using contracts and is tested with hypothesis (see this PyCon talk for a good introduction to both libraries). The contracts prevent spatial computations that are undefined in Euclidean space, such as finding the intersection of two parallel lines.

Why this instead of scipy.spatial or sympy.geometry?

This package has little to no overlap with the functionality of scipy.spatial. It can be viewed as an extension.

While similar spatial objects and computations exist in the sympy.geometry module, scikit-spatial is based on NumPy rather than symbolic math. The primary objects of scikit-spatial (Point, Points, and Vector) are actually subclasses of the NumPy ndarray. This gives them all the regular functionality of the ndarray, plus additional methods from this package.

>>> from skspatial.objects import Vector
>>> vector = Vector([2, 0, 0])

Behaviour inherited from NumPy:

>>> vector.size
3
>>> vector.mean().round(3)
0.667

Additional methods from scikit-spatial:

>>> vector.norm()
2.0
>>> vector.unit()
Vector([1., 0., 0.])

Point and Vector are based on a 1D NumPy array, and Points is based on a 2D NumPy array, where each row represents a point in space. The Line and Plane objects have Point and Vector objects as attributes.

Because the computations of scikit-spatial are also based on NumPy, keyword arguments can be passed to NumPy functions. For example, a tolerance can be specified while testing for collinearity. The tol keyword is passed to numpy.linalg.matrix_rank.

>>> from skspatial.objects import Points
>>> points = Points([[1, 2, 3], [4, 5, 6], [7, 8, 8]])
>>> points.are_collinear()
False
>>> points.are_collinear(tol=1)
True

Installation

The package can be installed via pip.

$ pip install scikit-spatial

Example Usage

Measurement

Measure the cosine similarity between two vectors.

>>> from skspatial.objects import Vector
>>> Vector([1, 0]).cosine_similarity([1, 1]).round(3)
0.707

Comparison

Check if multiple points are collinear.

>>> from skspatial.objects import Points
>>> points = Points([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
>>> points.are_collinear()
True

Projection

Project a point onto a line.

>>> from skspatial.objects import Line
>>> line = Line(point=[0, 0, 0], direction=[1, 1, 0])
>>> line.project_point([5, 6, 7])
Point([5.5, 5.5, 0. ])

An error is returned if the computation is undefined.

>>> line_a = Line([0, 0], [1, 0])
>>> line_b = Line([1, 0], [1, 0])
>>> line_a.intersect_line(line_b)
Traceback (most recent call last):
...
dpcontracts.PreconditionError: The lines must not be parallel.

Intersection

Find the intersection of two planes.

>>> from skspatial.objects import Plane
>>> plane_a = Plane([0, 0, 0], [0, 0, 1])
>>> plane_b = Plane([5, 16, -94], [1, 0, 0])
>>> plane_a.intersect_plane(plane_b)
Line(point=Point([5., 0., 0.]), direction=Vector([0., 1., 0.]))

Fitting

Find the plane of best fit for multiple points.

>>> points = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0]]
>>> Plane.best_fit(points)
Plane(point=Point([0.5, 0.5, 0. ]), normal=Vector([0., 0., 1.]))

Transformation

Transform multiple points to 1D coordinates along a line.

>>> line = Line(point=[0, 0, 0], direction=[1, 2, 0])
>>> points = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> line.transform_points(points).round(3)
array([ 2.236,  6.261, 10.286])

Acknowledgment

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scikit-spatial-1.1.0.tar.gz (40.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scikit_spatial-1.1.0-py2.py3-none-any.whl (23.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file scikit-spatial-1.1.0.tar.gz.

File metadata

  • Download URL: scikit-spatial-1.1.0.tar.gz
  • Upload date:
  • Size: 40.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for scikit-spatial-1.1.0.tar.gz
Algorithm Hash digest
SHA256 84b6717fcecd5c8b2ad441e8d116162d55e0cae93d3ff5d85948d4862358da15
MD5 8361d05cd94da6c6e7986beb3f6f0012
BLAKE2b-256 c17b887058934de013253281eea221a73b496bb4d1aad25252a509a11ff3674d

See more details on using hashes here.

File details

Details for the file scikit_spatial-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: scikit_spatial-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for scikit_spatial-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2e53b2f2e30b2571aebcbacecb90542a75b260342413cb838515346169ea3b01
MD5 294f9a791f48d3fd883fe8c443b7509c
BLAKE2b-256 42dc03eec533850fd1d618557182e9f2df13f0a15ec9d574bf0478b9b55f74e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page