Metadata-Version: 2.4
Name: trufl
Version: 0.2.1
Summary: Plan multi-round spatial surveys: rank areas by sampling need with multiple-criteria decision-making (MCDM), then split each round's sampling budget across them.
Author: Floris Abrams
Author-email: Franck Albinet <franckalbinet@gmail.com>
License-Expression: Apache-2.0
Project-URL: Repository, https://github.com/franckalbinet/trufl
Project-URL: Documentation, https://franckalbinet.github.io/trufl
Keywords: adaptive-sampling,spatial-sampling,mcdm,topsis,survey-design,geospatial
Classifier: Natural Language :: English
Classifier: Intended Audience :: Developers
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastcore
Requires-Dist: geopandas
Requires-Dist: rasterio
Requires-Dist: pysal==24.1
Dynamic: license-file

# Trufl


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

Trufl helps you decide where to sample next in a survey that runs over several rounds. At each round, it ranks areas, such as administrative units, by how much they need more samples. It then splits your sampling budget across the areas according to their rank. The ranking uses multiple-criteria decision-making (MCDM) on criteria that you choose, computed from the measurements collected so far and from prior knowledge such as an airborne survey.

Trufl was built to plan soil surveys of farmland contaminated after a nuclear accident. The same approach works for any phenomenon you monitor over time against several criteria, such as legacy contaminants.

trufl is part of the ecosystem of DSS4NAFA, a decision support system for nuclear emergencies affecting food and agriculture. [Where trufl comes from](https://franckalbinet.github.io/trufl/explanation/background.html) describes the research behind trufl and how it works with DSS4NAFA.

## Why trufl

Sampling theory and the research literature already cover multi-stage and adaptive spatial sampling well. Their methods rest on assumptions, such as a planned probability sampling design or a known model of spatial variation. In a nuclear emergency, the data rarely meet those assumptions. Measurements arrive over time from many sources, such as airborne surveys, laboratory analyses and samples taken where it was convenient to take them. They cover different locations at different resolutions. Sound statistical inference from such data is difficult. Decisions still have to be made, and you can’t ignore the data you have. trufl takes a pragmatic approach and uses all of it.

The reasons to sample one area before another also vary, and they change during an emergency. They include the levels measured so far, the uncertainty that remains, prior knowledge, land use and population. trufl ranks areas on criteria that you choose, with weights that you set. You can change both from one round to the next.

Like DSS4NAFA, trufl is not a black box. Each rank follows from the state of the area, the criteria, the weights and the scoring method. You can inspect each of them, and adjust them as the situation develops.

You can also use trufl to prepare for an emergency. Simulate a campaign over several rounds on a map of a possible contamination, and compare sampling strategies before you need them.

## Install

`pip install trufl`

## Example

This example plans one sampling round over a simulated contamination map, split into a 10 x 10 grid of areas:

``` python
import pandas as pd
import geopandas as gpd

from trufl.utils import gridder
from trufl.callbacks import State, PriorCB
from trufl.optimizer import Optimizer
from trufl.sampler import Sampler, rank_to_sample
```

``` python
fname_raster = './files/ground-truth-01-4326-simulated.tif'
gdf_grid = gridder(fname_raster, nrows=10, ncols=10)
```

Before any measurement, the state of each area is its prior: the mean of the map over the area. Calling a [`State`](https://franckalbinet.github.io/trufl/callbacks.html#state) computes it:

``` python
no_samples = gpd.GeoDataFrame(index=pd.Index([], name='loc_id'), geometry=None, data={'value': None})
state = State(no_samples, gdf_grid, cbs=[PriorCB(fname_raster)])()
state.head()
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

|        | Prior    |
|--------|----------|
| loc_id |          |
| 0      | 0.102492 |
| 1      | 0.125727 |
| 2      | 0.161802 |
| 3      | 0.184432 |
| 4      | 0.201405 |

</div>

With the prior as the only criterion, an area with a higher prior gets a higher sampling priority. Rank 1 is the highest:

``` python
ranks = Optimizer(state).get_rank(is_benefit_x=[True], w_vector=[1], s_method='CP')
ranks.head()
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

|        | rank |
|--------|------|
| loc_id |      |
| 92     | 1    |
| 93     | 2    |
| 91     | 3    |
| 94     | 4    |
| 82     | 5    |

</div>

[`rank_to_sample`](https://franckalbinet.github.io/trufl/sampler.html#rank_to_sample) splits a budget of 600 samples across the areas by rank, with at least one sample in each area (`min=1`). A [`Sampler`](https://franckalbinet.github.io/trufl/sampler.html#sampler) then picks the sample locations in each area:

``` python
n = rank_to_sample(ranks['rank'].sort_index().values, budget=600, min=1, policy='quantiles')
sample_locs = Sampler(gdf_grid).sample(n, method='uniform')
sample_locs.head()
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

|        | geometry                         |
|--------|----------------------------------|
| loc_id |                                  |
| 0      | MULTIPOINT ((-1.20861 43.26337)) |
| 1      | MULTIPOINT ((-1.21204 43.27385)) |
| 2      | MULTIPOINT ((-1.21957 43.28701)) |
| 3      | MULTIPOINT ((-1.22048 43.28873)) |
| 4      | MULTIPOINT ((-1.21362 43.30199)) |

</div>

In the next round, you compute the state from the values measured at these locations, and rank the areas again.

## Documentation

- [Getting started](https://franckalbinet.github.io/trufl/tutorials/getting_started.html) runs two sampling rounds on the simulated map, with plots.
- [Run a campaign over several years](https://franckalbinet.github.io/trufl/how-to/multi_year_campaign.html) re-ranks and resamples the areas every year with a fixed budget.
- [Where trufl comes from](https://franckalbinet.github.io/trufl/explanation/background.html) describes the research behind trufl and its place in the DSS4NAFA ecosystem.
- [How trufl ranks areas](https://franckalbinet.github.io/trufl/explanation/ranking.html) explains the criteria, weights and scoring methods.
- The [API reference](https://franckalbinet.github.io/trufl/reader.html) documents each module.

## Acknowledgements

trufl started within the IAEA Coordinated Research Project D15019, [“Monitoring and Predicting Radionuclide Uptake and Dynamics for Optimizing Remediation of Radioactive Contamination in Agriculture”](https://www.iaea.org/newscenter/news/new-crp-monitoring-and-predicting-radionuclide-uptake-and-dynamics-for-optimizing-remediation-of-radioactive-contamination-in-agriculture-crp-d15019), led by the [Joint FAO/IAEA Centre of Nuclear Techniques in Food and Agriculture](https://www.fao.org/agriculture/fao-iaea-nuclear-techniques/en). It is actively maintained.

It builds on the PhD research of Floris Abrams at [SCK CEN](https://www.sckcen.be) and [KU Leuven](https://www.kuleuven.be). Floris Abrams and Franck Albinet wrote the package. [Where trufl comes from](https://franckalbinet.github.io/trufl/explanation/background.html) describes the research and how trufl works with DSS4NAFA.
