Skip to main content

Hansen Solubility Parameters in Python

Project description

PyPI version

HSPiPy

Hansen Solubility Parameters in Python.

Introduction


HSPiPy is a Python library designed for calculating and visualizing Hansen Solubility Parameters (HSP). It provides machine-learning–friendly estimators, convenient data import, and plotting tools for analyzing solvent compatibility in materials science, polymers, and coatings.

Features


  • Read solvent data from multiple formats (CSV, HSD, HSDX)
  • Calculate Hansen Solubility Parameters using robust optimization methods
  • Support for single or multiple (up to 2) solubility spheres
  • Generate 2D and 3D visualizations of solubility spheres
  • Fully compatible with scikit-learn for machine learning workflows

Documentation


Installation


Install HSPiPy easily with pip:

pip install HSPiPy

Dependencies

  • numpy
  • pandas
  • matplotlib
  • scipy
  • scikit-learn

Usage


Reading HSP Data

To read HSP data from a file (CSV, HSD or HSDX), create an instance of the HSP class and use the read method:

from hspipy import HSP

hsp = HSP()
hsp.read('path_to_your_hsp_file.csv')

Calculating HSP

Use the get method to calculate the Hansen Solubility Parameters (HSP) from your data:

# For a single sphere model (default)
result = hsp.get(inside_limit=1)

# For a double sphere model
result = hsp.get(inside_limit=1, n_spheres=2)

get() returns an HSPResult object. In a Jupyter notebook it renders as a formatted table automatically. In a script use print(result) or access attributes directly:

print(result.hsp)             # fitted D, P, H center coordinates
print(result.radius)          # sphere radius
print(result.accuracy)        # classification accuracy
print(result.datafit)         # DATAFIT value
print(result.n_solvents_in)   # number of good solvents
print(result.n_solvents_out)  # number of bad solvents
print(result.n_total)         # total solvents
print(result.n_wrong_in)      # bad solvents inside sphere (false positives)
print(result.n_wrong_out)     # good solvents outside sphere (false negatives)

The inside_limit parameter defines the threshold score value to consider a solvent as "inside" the solubility sphere (default: inside_limit=1).

Visualizing HSP

Use the plot_3d and plot_2d methods to visualize the HSP data in 3D and 2D formats, respectively:

# Generate individual plots
hsp.plot_3d()
hsp.plot_2d()

# Or generate both plots at once
hsp.plots()

3dHSP 2dHSP

HSP class methods:

Method Description
read(path) Reads solvent data from a CSV, HSD, or HSDX file.
get(inside_limit=1, n_spheres=1) Fits HSP sphere(s) and returns an HSPResult object.
plot_3d() Plots the HSP data in 3D. Returns the figure.
plot_2d() Plots the HSP data in 2D. Returns the figure.
plots() Generates both 2D and 3D plots. Returns (fig_3d, fig_2d).

get() returns an HSPResult object. The following attributes are available on it and also set on the HSP instance after calling get():

Attribute Description
result.hsp Numpy array — Fitted HSP coordinates. Shape: (3,) for single-sphere (D, P, H), or (n_spheres, 3) for multiple spheres.
result.radius Float or array — Radius (or radii) of the solubility sphere(s).
result.error Float — Objective function value from the optimization (lower is better).
result.accuracy Float — Classification accuracy of the fitted model on the dataset.
result.datafit Float — DATAFIT value (geometric mean fitness; 1.0 = perfect classification).
result.n_solvents_in Int — Number of good solvents (0 < score <= inside_limit).
result.n_solvents_out Int — Number of bad solvents (score == 0 or score > inside_limit).
result.n_total Int — Total number of solvents.
result.n_wrong_in Int — Bad solvents predicted inside the sphere (false positives).
result.n_wrong_out Int — Good solvents predicted outside the sphere (false negatives).
hsp.d, hsp.p, hsp.h Float — Individual HSP components (δD, δP, δH). Single-sphere only; None for multi-sphere.
hsp.inside List — Solvents classified as inside the solubility sphere(s), with their HSP values and scores.
hsp.outside List — Solvents classified as outside the solubility sphere(s), with their HSP values and scores.
hsp.grid Pandas DataFrame — The full input dataset, standardized with columns: Solvent, D, P, H, and Score.

Using scikit-learn style estimator

import numpy as np
from hspipy import HSPEstimator

# Example dataset (D, P, H values with scores)
X = np.array([
    [16.0, 8.0, 5.0],
    [18.0, 7.5, 9.0],
    [20.0, 10.0, 12.0],
])
y = np.array([1, 1, 0])  # Inside/outside labels or scores

est = HSPEstimator(n_spheres=1)
est.fit(X, y)

print("Fitted HSP:", est.hsp_)
print("Accuracy:", est.score(X, y))

Contributing


Contributions are welcome! If you have any suggestions, feature requests, or bug reports, please open an issue on the GitHub repository.

License


This library is licensed under the MIT License. See the LICENSE file for details.

Acknowledgements


HSPiPy was inspired by the well-known HSP software suit Hansen Solubility Parameters in Practice (HSPiP) and by the HSP community.

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

hspipy-1.1.6.tar.gz (39.4 kB view details)

Uploaded Source

Built Distribution

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

hspipy-1.1.6-py3-none-any.whl (41.3 kB view details)

Uploaded Python 3

File details

Details for the file hspipy-1.1.6.tar.gz.

File metadata

  • Download URL: hspipy-1.1.6.tar.gz
  • Upload date:
  • Size: 39.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for hspipy-1.1.6.tar.gz
Algorithm Hash digest
SHA256 4de4ce7ec1585c97712b6041c3f3db3433af45857d6cc77c673e7f063a752f20
MD5 c2022b61f64010df8fce79bd15d1a3c5
BLAKE2b-256 cc08ff9eca92a30b1b1ad4f918533b204abe864d12aa764ae6c5be2c1d60c8f4

See more details on using hashes here.

File details

Details for the file hspipy-1.1.6-py3-none-any.whl.

File metadata

  • Download URL: hspipy-1.1.6-py3-none-any.whl
  • Upload date:
  • Size: 41.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for hspipy-1.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 37d15b96a3ab96d6d1814e1d58db0f367d9f6cd3aaa7d6f449ee0afff556f268
MD5 95a84b37419c6a8d41799808b0b2a1d2
BLAKE2b-256 93ac494cc63b364ae4465d7c8868e6490f3596af4223c882299438fd9396c5c0

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