Metadata-Version: 2.4
Name: salca-bd
Version: 2.0.0
Summary: SALCA-BD: biodiversity indicators from agricultural practices (extracted from agroscope-ch/salca)
Author: vanakenm
Project-URL: Repository, https://github.com/BeOpenCompass/salca-bd-extract
Project-URL: Issues, https://github.com/BeOpenCompass/salca-bd-extract/issues
Project-URL: Upstream, https://github.com/agroscope-ch/salca
Keywords: SALCA,biodiversity,LCA,agriculture,Agroscope
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENCE.txt
Requires-Dist: pandas<4,>=2.0.3
Requires-Dist: numpy>=1.25
Requires-Dist: openpyxl>=3.1
Requires-Dist: xlsxwriter>=3.0.2
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Dynamic: license-file

# SALCA-BD (extract)

Standalone copy of the SALCA-BD biodiversity model, extracted from the
[agroscope-ch/salca](https://github.com/agroscope-ch/salca) repository
(`SALCA-BD/salcabd`, commit `2cf3c75`). The other SALCA models and the
SALCA-framework scripts (`salcabd_farm.py`, `salcabd_horti.py`, which need the
external `salcaconnector`) are not included.

The SALCA-BD model and its code are the work of [Agroscope](https://www.agroscope.admin.ch).
This package only repackages them: packaging, dependency updates and tests by vanakenm
([Open Compass](https://www.opencompass.org)). The package version follows the SALCA-BD model
version (2.0.0 = model 2.0); packaging-only fixes are released as post-releases
(2.0.0.post1, ...). Questions about the model itself belong upstream; questions about the packaging go to
[BeOpenCompass/salca-bd-extract](https://github.com/BeOpenCompass/salca-bd-extract/issues).

Licence: GNU LGPLv3 (see [`LICENCE.txt`](https://github.com/BeOpenCompass/salca-bd-extract/blob/main/LICENCE.txt)).

## Why

We want to use SALCA-BD from another python app. The current code as provided makes this difficult (no python package, some oudated librairies, no tests). This package is just that - an extract, made as a PyPi package with some dependencies update and tests to help ensure full parity with the reference package.

## Install

Requires Python 3.11+ (tested with pandas 2.0.3, 2.2 and 3.1).

```
pip install salca-bd
```

The package is imported as `salcabd`:

```python
import salcabd
ct = salcabd.get_default_control_table()
```

For development:

```
py -3.11 -m venv .venv311
.venv311\Scripts\activate
pip install -r requirements-lock.txt   # optional: exact verified versions
pip install -e .
```

## Control table

The default control table (coefficients, validation rules, min/max scenarios) is shipped as
plain CSV files in `salcabd/pkg_resources/control_table/`. Upstream ships it as a pickle,
which only loads with pandas 1.x. The CSVs were exported from that pickle
([`tools/export_control_table.py`](https://github.com/BeOpenCompass/salca-bd-extract/blob/main/tools/export_control_table.py)) and load into an identical `ControlTable`. The tests check
this exactly, against the pickle in a pandas 1.x environment and against pinned fingerprints
on any pandas version.

`salcabd.load_control_table(folder)` / `salcabd.save_control_table(ct, folder)` read and write
the same CSV format.

The original pickle is kept as a reference in `tools/control_table_original.ct`. To use it
(export again, or run the exact comparison test), set up the legacy environment described in
[`requirements-lock-py39.txt`](https://github.com/BeOpenCompass/salca-bd-extract/blob/main/requirements-lock-py39.txt) (Python 3.9 + pandas 1.2.4).

## Run

```
python -m salcabd samples\salcasample_fixed.xlsm
```

(`salcabd <file>` also works after install.) Results are written to a
`Results` folder next to the input.

## Input file rules (Produktionsinventar sheet)

The importer reads fixed positions of the template ([`samples/salcaempty.xlsm`](https://github.com/BeOpenCompass/salca-bd-extract/blob/main/samples/salcaempty.xlsm)):

- Don't delete columns or rows of the template (data starts in column H, row 16).
- Field names (row 6) must contain a number, e.g. `Parzelle 1 - Terre Bon-Dieu`.
- Year labels (row 10) are text like `1. Jahr`; no plain numbers (e.g. `2024`) in that row.
- Scenario labels (row 13) must contain their number, e.g. `Szenario 1 (TWI konform)`.
- Each culture's first scenario column must have measures marked with `1`; the
  first marked row determines the habitat, so don't mark ecological-infrastructure
  rows (top of the sheet) there.
- Every field needs at least one culture with measures.
- Zero options (e.g. "a. 0" applications) can't be combined with other options of the same group.

## Tests

```
pip install -e .[test]
pytest
```

`tests/test_samples.py` checks the farm-level summaries (Total/Mean, GAV/HÖA)
of both sample files against the values produced by the original CLI, both
through the Python API and by running the CLI end to end.

## Changes compared to upstream

Upstream (Python 3.9, pandas 1.2.4) doesn't run on pandas 2+. Changes to make it run on Python 3.11
with current dependencies, without changing results (Excel output verified identical cell by cell
to the upstream code on both samples):

- Default control table: pickle → CSV (see above).
- `DataFrame.iteritems()` → `.items()` (removed in pandas 2).
- Blank separator row in result tables: column converted to `object` before writing `''`
  (pandas 3 refuses strings in float columns).
- Single-column assignments use a 1-D array instead of an n×1 array.
- Excel export unwraps one-element Series before writing (`float(Series)` removed in pandas 3).
- `inplace=True` calls replaced by assignments; full-length tuple key for a MultiIndex column
  (both deprecated, to be removed in pandas 4).
- Fields with a border ("Rand" columns, border area > 0): `DataFrame.append` → `pd.concat`
  (removed in pandas 2) when building the R2 detail, and `series[0]` → `series.iloc[0]` for
  the habitat min/max in field aggregation (positional fallback removed in pandas 3). Output
  verified identical to upstream, except the farm `Mean` can differ in the last bits: pandas 3
  sums it pairwise (the more accurate result) where pandas 1.x summed sequentially.
