Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

spdist

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

spdist

  • 0.1.3
  • PyPI
  • Socket score

Maintainers
1

spdist: Simple metrics for comparing the distance between two curves.

spdist is a simple metrics for comparing the distance between two given curves. The curves can be passed in as a numpy array with discrete values. It will interpolate between the values and calculate the minimum distance between each points in the curve and reference curve.

pip install spdist

How to use.

Currently spdist has only one function spdist.

import spdist
import numpy as np

x = np.linspace(0, 10, 100)
y = 2*x

x_ref = x
y_ref = 2*x + 1

distance = spdist.spdist(x,y,x_ref,y_ref)

print(f"{distance}")

Example plot

Following example plot a straight line $y = 2x$ and a line with a constant offset. The distance between the two lines is calculated using the spdist function. The normal vector is calculated as $(2/\sqrt{5}, -1/\sqrt{5})$. Normal vector scaled by the distance is plot green line.

test

Algorithm

The algorithm of the caculation is somewhat brute force and the time complexity is $O(n^2)$. The algorithm is as follows:

distance = 0

for i in zip(x,y):

  tmp_distance = 0
  for j in zip(x_ref, y_ref):
    if (x_ref == x_ref_next) and (y_ref == y_ref_next):
        # point to point distance
        tmp_distance = min(tmp_distance, ((x - x_ref)**2 + (y - y_ref)**2)**0.5)
        continue
    # point to line distance
    # https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Line_defined_by_two_points
    tmp_distance = min(tmp_distance, ((x_ref_next - x_ref) * (y_ref - y) - (x_ref - x) * (y_ref_next - y_ref)) / ((x_ref_next - x_ref)**2 + (y_ref_next - y_ref)**2)**0.5)

  distance += tmp_distance

distance /= len(x)

Although the algorithm itself is not optimized, the whole library is written in Rust Rust with parallel processing. (Thanks to Rust's borrow checker and rayon rayon, which is both great work.) Therefore, the calculation is fast enough.

Where to use

This library was originally developed to calculate the distance between the measured spectra and the reference spectra of XAS. The metrics is useful to quantify and capture the features of the spectra.

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc