Skip to main content

Phosphorus adsorption modeling in soils

Project description

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

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

phosadsorption-0.2.8.tar.gz (201.0 kB view details)

Uploaded Source

Built Distribution

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

phosadsorption-0.2.8-py3-none-any.whl (203.2 kB view details)

Uploaded Python 3

File details

Details for the file phosadsorption-0.2.8.tar.gz.

File metadata

  • Download URL: phosadsorption-0.2.8.tar.gz
  • Upload date:
  • Size: 201.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for phosadsorption-0.2.8.tar.gz
Algorithm Hash digest
SHA256 31905dd55de0a43da0da44601ff4ce6be66138a8bf909d84359b24b7a26bfa1a
MD5 2a5b88c9166152c4165368734e6d2cc3
BLAKE2b-256 d630d527bff237ac65fc73852ef52ea6725c74bb74ab1d1854cd0a06ba3c7c41

See more details on using hashes here.

Provenance

The following attestation bundles were made for phosadsorption-0.2.8.tar.gz:

Publisher: publish.yml on Mil-afk/soil_phosphorus_adsorption_data

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phosadsorption-0.2.8-py3-none-any.whl.

File metadata

  • Download URL: phosadsorption-0.2.8-py3-none-any.whl
  • Upload date:
  • Size: 203.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for phosadsorption-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 fd30f401b5333f855e5bc2d0516b0397bef7998bfd89ab95adf42552604b8bd4
MD5 6fe4c85e6677bafae1a6f9ee80ad2edb
BLAKE2b-256 3452ac6ab4aae42ddbc66e4bfda3352ee8177eec862b945e164215ce1450b1e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for phosadsorption-0.2.8-py3-none-any.whl:

Publisher: publish.yml on Mil-afk/soil_phosphorus_adsorption_data

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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