Skip to main content

image image image image Documentation Status image

Introduction

This package provides spatial objects based on NumPy arrays, as well as computations using these objects. The package includes computations for 2D, 3D, and higher-dimensional space.

The following spatial objects are provided:

  • Point
  • Points
  • Vector
  • Line
  • LineSegment
  • Plane
  • Circle
  • Sphere
  • Triangle
  • Cylinder

Most of the computations fall into the following categories:

  • Measurement
  • Comparison
  • Projection
  • Intersection
  • Fitting
  • Transformation

All spatial objects are equipped with plotting methods based on matplotlib. Both 2D and 3D plotting are supported. Spatial computations can be easily visualized by plotting multiple objects at once.

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 object-oriented 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)
np.float64(0.667)

Additional methods from scikit-spatial:

>>> vector.norm()
np.float64(2.0)

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

Because Point and Vector are both subclasses of ndarray, a Vector can be added to a Point. This produces a new Point.

>>> from skspatial.objects import Point

>>> Point([1, 2]) + Vector([3, 4])
Point([4, 6])

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.

Note that most methods inherited from NumPy return a regular NumPy object, instead of the spatial object class.

>>> vector.sum()
np.int64(2)

This is to avoid getting a spatial object with a forbidden shape, like a zero dimension Vector. Trying to convert this back to a Vector causes an exception.

>>> Vector(vector.sum())
Traceback (most recent call last):
ValueError: The array must be 1D.

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 with pip.

$ pip install scikit-spatial

It can also be installed with conda.

$ conda install scikit-spatial -c conda-forge

The matplotlib dependency is optional. To enable plotting, you can install scikit-spatial with the extra plotting.

   $ pip install 'scikit-spatial[plotting]'

Example Usage

Measurement

Measure the cosine similarity between two vectors.

>>> from skspatial.objects import Vector

>>> Vector([1, 0]).cosine_similarity([1, 1]).round(3)
np.float64(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. ])

Intersection

Find the intersection of two planes.

>>> from skspatial.objects import Plane

>>> plane_a = Plane(point=[0, 0, 0], normal=[0, 0, 1])
>>> plane_b = Plane(point=[5, 16, -94], normal=[1, 0, 0])

>>> plane_a.intersect_plane(plane_b)
Line(point=Point([5., 0., 0.]), direction=Vector([0, 1, 0]))

An error is raised if the computation is undefined.

>>> plane_b = Plane(point=[0, 0, 1], normal=[0, 0, 1])

>>> plane_a.intersect_plane(plane_b)
Traceback (most recent call last):
ValueError: The planes must not be parallel.

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.

Metadata

Release files for scikit-spatial 9.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scikit-spatial 9.0.1
File Size Uploaded
scikit_spatial-9.0.1.tar.gz 135.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scikit-spatial 9.0.1
File Interpreter ABI Platform
scikit_spatial-9.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 185.6 kB

Release files / scikit_spatial-9.0.1.tar.gz

Download URL scikit_spatial-9.0.1.tar.gz
Size 135.1 kB
Tags Source
SHA-256 checksum
How to use checksums
1c56608088daae35b2caddee2b95e54d15bbfd48f0b760fa8a86817d0bcd0acc
BLAKE2b-256 checksum
How to use checksums
9e5b290cfd3e72550604b650672623414d78e60329e9511c62b7bdc106f50897
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.4.18

Release files / scikit_spatial-9.0.1-py3-none-any.whl

Download URL scikit_spatial-9.0.1-py3-none-any.whl
Size 50.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ce05254f394f3789f4e647c65175f3c2878e92066cb788c13c5577e1e3012d05
BLAKE2b-256 checksum
How to use checksums
82183a465d24c4da7f724469608aae93176e98020defa37810927ab05e37a657
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.4.18

Release history Release notifications | RSS feed

This release

9.0.1 This release

2 release files

9.0.0

2 release files

8.1.0

2 release files

8.0.0

2 release files

7.2.0

2 release files

7.1.1

2 release files

7.1.0

2 release files

7.0.0

2 release files

6.8.1

2 release files

6.8.0

2 release files

6.7.0

2 release files

6.6.0

2 release files

6.5.0

2 release files

6.4.1

2 release files

6.4.0

2 release files

6.3.0

2 release files

6.2.1

2 release files

6.2.0

2 release files

6.1.1

2 release files

6.1.0

2 release files

6.0.1

2 release files

6.0.0

2 release files

5.2.0

2 release files

5.1.0

2 release files

5.0.0

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.0.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.5.0

2 release files

1.4.2

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page