Skip to main content

RipleyK

Calculation of the Ripley K (spatial statistics) value in python. This project is still being developed and currently only supports 'circle' based bounding regions for automated boundary correction. Can support 'rectangle' based bounding regions if you do not require boundary corrections. This package allows quick calculation (using kd-trees) the RipleyK values for 1D-3D systems.

Installation

You can install the RipleyK package using the following pip command:

pip install ripleyk

To get started from source quickly follow these steps:

  1. Clone or download this repository and launch python within the folder.

  2. Make a python environment to run in. Always recommend the use of virtual environments for safety. Must be Python3 and currently only tested with Python 3.8.

  3. Install requirement.txt file into your new python environment

pip install -r requirements.txt

Example

For these examples lets create a random subset of points in a sphere of radius 1:

import random
xs = []
ys = []
zs = []
radius = 1
random.seed(0)
for i in range(0,10000):
    positioned = False
    while positioned is False:
        x = random.uniform(-radius, radius)
        y = random.uniform(-radius, radius)
        z = random.uniform(-radius, radius)
        if (x**2)+(y**2)+(z**2) < radius**2:
            xs.append(x)
            ys.append(y)
            zs.append(z)
            positioned = True
xs = np.array(xs)
ys = np.array(ys)
zs = np.array(zs)

Now we can use our list of points within the RipleyK calculation method. Note that all point list are numpy arrays.

Single radius calculation (2D)

Let us start simply. We shall calculate the Ripley K value for the circle (r=1) bounding space. As it is a circle we only require two dimensions (d1 and d2). The calculate_ripley method should be given the radius and bounding space as it's first two inputs. The points should be given as a numpy list for each dimension, where the spatial location of point[0] would be xs[0], ys[0].

import ripleyk

radius = 0.5
bounding_radius = 1
k = ripleyk.calculate_ripley(radius, bounding_radius, d1=xs, d2=ys)
print(k)

You should get a value of 0.7576 as k.

By default this will not include any boundary correction. To add this simply add boundary_correct=True to the above function. This should always increase the value. Now we get a value of 0.8650 as k.

Finally, there is a normalisation which can be applied to evaluate the clustering of the k-value in comparison to a completely spatially random (CSR). If the distribution is close to CSR the value should tend to 0. Below is the same calculation, but now includes boundary correction and normalisation:

import ripleyk

radius = 0.5
bounding_radius = 1
k = ripleyk.calculate_ripley(radius, bounding_radius, d1=xs, d2=ys, boundary_correct=True, CSR_Normalise=True)
print(k)

As we generated the points randomly, we see that this normalisation does make the value much closer to 0, here we got 0.0796. For non randomly distributed points a postive value indicates clustering and a negative value indicates dispersion.

Multiple radii calculation (3D)

It is also possible to use this function to evaluate multiple radii for their Ripley K values. This is the predominant way to evaluate the distribution, by looking over a range of radii and plotting the k value or normalised k value against radius. Below is an example of doing this for the 3D dataset we generated:

import ripleyk

radii = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
k = ripleyk.calculate_ripley(radii, 1, d1=xs, d2=ys, d3=zs,boundary_correct=True, CSR_Normalise=True)
print(k)

You should get the following results:

[0.004040007680256669, 0.014698354446450401, 0.031055984373102252, 0.052700761751369396, 0.07963484721284364, 0.1114734068553147, 0.14820166805628499, 0.1880258651911193, 0.2271020312695291, 0.2608857812403329]

This can be simply plotted against the radii inputted as seen below (would require installation of matplotlib):

import ripleyk
import matplotlib.pyplot as plt

radii = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
k = ripleyk.calculate_ripley(radii, 1, d1=xs, d2=ys, d3=zs,boundary_correct=True, CSR_Normalise=True)

plt.plot(radii, k, 'bo')
plt.show()

Dependencies

Metadata

Release files for ripleyk 0.0.2

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

Source distribution (sdist)

Source distribution for ripleyk 0.0.2
File Size Uploaded Provenance
ripleyk-0.0.2.tar.gz 4.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ripleyk 0.0.2
File Interpreter ABI Platform
ripleyk-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / ripleyk-0.0.2.tar.gz

Download URL ripleyk-0.0.2.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f339d729974affbfb78df1e1f636ba0c134676d7d394ad59e27b2b9c130b4c59
BLAKE2b-256 checksum
How to use checksums
6a50bf17c9342e47d56ba3ba5480e949b9f95655d63f6d9f18269aad84cd45c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.2

Release files / ripleyk-0.0.2-py3-none-any.whl

Download URL ripleyk-0.0.2-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2d8478a184c3fd1711dc8561e37679d415217bc984b77405b7722b2dec72c5ec
BLAKE2b-256 checksum
How to use checksums
7aff2bef54e3f1a0296b4ec8992cebdeac93eed19da1020f382ded4b827797de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.2

Security for ripleyk 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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