Skip to main content

Phosadsorption

Phosadsorption is a Python library for predicting phosphorus (P) adsorption in soils using machine learning. It replaces traditional empirical models like the Langmuir isotherm with a data-driven approach built on XGBoost.

Why Phosadsorption?

While the Langmuir isotherm has been widely used to model phosphorus adsorption, it often fails to capture the variability seen in real-world soils with diverse physicochemical characteristics. This library introduces a multioutput XGBoost regressor trained on soil data to predict phosphorus adsorption across multiple equilibrium concentrations simultaneously.

Key Features

  • 🔬 Predicts phosphorus adsorption at 1, 2, 4, 6, and 10 mg/L equilibrium concentrations.
  • 📈 Outperforms traditional models like Langmuir in predictive accuracy.
  • 🧪 Built for soil scientists, agronomists, and environmental engineers.
  • 📦 Easy to install and use with just a few lines of code.

Installation

Install from PyPI:

pip install phosadsorption

Or install from TestPyPI for pre-release versions:

pip install --index-url https://test.pypi.org/simple/ phosadsorption

Example Usage

Input Requirements: The Excel file should include the following soil parameters (all reported in mg/kg of soil unless otherwise noted):

S (Sand %),

C (Clay %),

pH,

EC (Electrical Conductivity, dS/m),

Organic matter (% or equivalent label),

P (Phosphorus),

Mg (Magnesium),

Mn (Manganese),

Cu (Copper)

Note: All nutrient concentrations provided as input should be reported in units of mg/kg of soil to ensure consistent and accurate predictions.

from phosadsorption.phosadsorptionlib import PhosAdsorptionLib
from phosadsorption.visuals import plot_pfp_vs_applied
import pandas as pd

# Load your input data
df = pd.read_excel("your_input.xlsx")

print(df.head(3).to_markdown(index=False))

|   Α.Μ.Δ |   ΒΑΘΟΣ | Soil texture   |   S |   C |     Si  |   pH |    EC |   Organic matter |    CaCO3    |   NO3 |   NO3-N |     P |     K |   Mg | Ca    |     Fe  |   Zn |    Mn |    Cu |    B |
|--------:|--------:|:---------------|----:|----:|--------:|-----:|------:|-----------------:|------------:|------:|--------:|------:|------:|-----:|:------|--------:|-----:|------:|------:|-----:|
|  251001 |     nan | L              |  44 |  22 |      34 | 7    | 0.479 |             3.57 |         0   |  79.4 |   17.92 | 44.85 |   420 | 1001 | 1008  |   53.92 | 5.96 | 10.51 | 63.12 | 1.24 |
|  251002 |     nan | SL             |  54 |  18 |      28 | 5.15 | 0.722 |             3.08 |         0   | 139.2 |   31.44 | 46.05 |   485 |  263 | 1360  |   67.98 | 1.75 | 47.42 | 14.78 | 1.5  |
|  251003 |     nan | CL             |  30 |  30 |      40 | 7.31 | 0.557 |             2.57 |         5.2 |  62   |   14    |  3.94 |   243 |  656 | >2000 |    5.62 | 0.36 |  4.48 |  3.22 | 0.35 |

# Initialize and run the model
model = PhosAdsorptionLib()
result = model.predict(df)



print(result.head(3).to_markdown(index=False))


|   P_applied_kg_per_ha_at_1ppm |   P_applied_kg_per_ha_at_2ppm |   P_applied_kg_per_ha_at_4ppm |   P_applied_kg_per_ha_at_6ppm |   P_applied_kg_per_ha_at_10ppm |   PFP1 |   PFP2 |   PFP4 |   PFP6 |   PFP10 |
|------------------------------:|------------------------------:|------------------------------:|------------------------------:|-------------------------------:|-------:|-------:|-------:|-------:|--------:|
|                          47.3 |                          94.6 |                         189.2 |                         283.8 |                          473   |   61.5 |   76.7 |   70.3 |   81.8 |    78.3 |
|                          49.1 |                          98.2 |                         196.4 |                         294.5 |                          490.9 |   78.6 |   86.3 |   87.7 |   92.4 |    90.7 |
|                          47.7 |                          95.4 |                         190.8 |                         286.2 |                          477   |   89.2 |   87.6 |   91.8 |   90.2 |    85.3 |

#PFP stands for Phosphorus Fraction Percentage and represents the percentage of applied phosphorus that was adsorbed by the soil (expressed as %).

# Plot results for first sample
plot_pfp_vs_applied(result, sample_index=0)

Project Structure

phosadsorption/
├── __init__.py
├── phosadsorptionlib/
│   ├── __init__.py
│   ├── model.py
│   ├── utils.py
│   └── multioutput_xgb_model.json
├── visuals/
│   ├── __init__.py
│   └── plot.py
README.md
MANIFEST.in
setup.py

License

MIT License

Citation

If you use this library in your work, please cite:

Iatrou, M.; Papadopoulos, A. Machine Learning vs. Langmuir: A Multioutput XGBoost Regressor Better Captures Soil Phosphorus Adsorption Dynamics. Crops 2025, 5, 55. https://doi.org/10.3390/crops5040055 GitHub Repository: https://github.com/Mil-afk/soil_phosphorus_adsorption_data

Release files for phosadsorption 0.2.8

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

Source distribution (sdist)

Source distribution for phosadsorption 0.2.8
File Size Uploaded
phosadsorption-0.2.8.tar.gz 201.0 kB Details

Built distribution (wheel)

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

Total release size: 404.1 kB

Release files / phosadsorption-0.2.8.tar.gz

Download URL phosadsorption-0.2.8.tar.gz
Size 201.0 kB
Tags Source
SHA-256 checksum
How to use checksums
31905dd55de0a43da0da44601ff4ce6be66138a8bf909d84359b24b7a26bfa1a
BLAKE2b-256 checksum
How to use checksums
d630d527bff237ac65fc73852ef52ea6725c74bb74ab1d1854cd0a06ba3c7c41
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 19, 2025.

Transparency log

Release files / phosadsorption-0.2.8-py3-none-any.whl

Download URL phosadsorption-0.2.8-py3-none-any.whl
Size 203.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fd30f401b5333f855e5bc2d0516b0397bef7998bfd89ab95adf42552604b8bd4
BLAKE2b-256 checksum
How to use checksums
3452ac6ab4aae42ddbc66e4bfda3352ee8177eec862b945e164215ce1450b1e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 19, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.8 This release

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

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