Metadata-Version: 2.4
Name: FeedbackMachine
Version: 0.1.1
Summary: Model environmental feedback loops in Life Cycle Assessment, dynamically and prospectively, on top of Brightway, bw_timex and premise.
Author-email: Pierre Jouannais <jouannais.pierre@gmail.com>
License: BSD 3-Clause License
        
        Copyright (c) 2026, Pierre Jouannais and INRAE.
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.
        
        3. Neither the name of the copyright holder nor the names of its
           contributors may be used to endorse or promote products derived from
           this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
Project-URL: Homepage, https://github.com/PJGilmw/FeedbackMachine
Project-URL: Repository, https://github.com/PJGilmw/FeedbackMachine
Project-URL: Issues, https://github.com/PJGilmw/FeedbackMachine/issues
Keywords: LCA,life cycle assessment,brightway,bw_timex,premise,environmental feedbacks,prospective LCA,dynamic LCA
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: scipy
Requires-Dist: pandas
Requires-Dist: python-dateutil
Requires-Dist: bw2data
Requires-Dist: bw2calc
Requires-Dist: bw_timex
Requires-Dist: bw_temporalis
Requires-Dist: dynamic_characterization>=1.4.2
Provides-Extra: prospective
Requires-Dist: premise; extra == "prospective"
Provides-Extra: progress
Requires-Dist: tqdm; extra == "progress"
Provides-Extra: plotting
Requires-Dist: matplotlib; extra == "plotting"
Provides-Extra: parallel
Requires-Dist: ray; extra == "parallel"
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: tqdm; extra == "dev"
Requires-Dist: matplotlib; extra == "dev"
Dynamic: license-file

# FeedbackMachine

**Model environmental feedback loops in Life Cycle Assessment — dynamically and
prospectively — on top of [Brightway](https://docs.brightway.dev/),
[bw_timex](https://docs.brightway.dev/projects/bw-timex/) and
[premise](https://github.com/polca/premise).**

Life Cycle Assessment has traditionally relied on a strict separation between the
environment and the economy. This package breaks this boundary to compute feedback-aware CF: emissions trigger
environmental mechanisms, which in turn generate feedback flows back to the
technosphere, closing the loop.

`FeedbackMachine` operationalizes this idea. An elementary flow acts as an initial
perturbation (analogous to a functional-unit demand), and the package propagates
its feedbacks through time, producing a time-resolved, characterized inventory. It
rigorously separates **endogenous** effects (attributable to the initial emission)
from **exogenous** system dynamics using partial derivatives within a matrix-based
LCA formulation, which lets multiple feedback loops interact consistently.

This package is introduced and supported by the academic publication: **FeedbackMachine: A general computing framework for feedback-aware LCA characterization factors (Jouannais. P, under review)**

The demonstration example in the publication is supported by [`examples/demonstration_two_feedbacks_pGWP.ipynb`](examples/demonstration_two_feedbacks_pGWP.ipynb). 

## Installation

```bash
pip install FeedbackMachine
```

or, from source:

```bash
git clone https://github.com/PJGilmw/FeedbackMachine.git
cd FeedbackMachine
pip install -e .
```

To also build prospective background databases with premise (heavy, and gated by
a key you request from the premise authors):

```bash
pip install -e ".[prospective]"
```

For development (tests):

```bash
pip install -e ".[dev]"
```

### Requirements

- Python ≥ 3.10
- A Brightway 2.5 project (`bw2data`, `bw2calc`), with a biosphere database and, for
  realistic use, ecoinvent and premise-built prospective databases.
- `bw_timex`, `bw_temporalis`, `dynamic_characterization`>1.4.2 for the dynamic/temporal
  inventory and characterization.

`premise` is an **optional** dependency: it is only needed to generate the
prospective background databases and requires a decryption key (email
`romain.sacchi@psi.ch`).

## Quickstart

```python
import logging
import FeedbackMachine as fb

logging.basicConfig(level=logging.INFO)  # optional: see propagation progress

# 1. Response / modulation models (how the technosphere responds to drivers)
registry = fb.ModelRegistry()
# ... register fb.ExactModel(...) / fb.build_nd_rbf_from_samples(...) ...

# 2. Driver/demand time-patterns (how a driver or induced demand evolves over
#    time after an emission)
lookup = fb.PatternLookup(patterns, param_points=points, param_names=names)

# 3. Assemble the full input dictionary (settings + feedbacks) and run
machine = fb.FeedbackMachine(dict_feedbacks)
machine.propagate()

# 4. Assemble and characterize the total impact
machine.prepare_computation_total_feedback_impact()
machine.compute_impact_feedback_dynamic(perturbation_amount, metric, method)
```

See the full worked example — including building the initialization databases,
response functions, exogenous trajectories, and premise prospective databases —
in [`examples/demonstration_quickstart.ipynb`](examples/demonstration.ipynb).

The demonstration example in the publication is supported by [`examples/demonstration_two_feedbacks_pGWP.ipynb`](examples/demonstration_two_feedbacks_pGWP.ipynb). The healthcare sector are computed with 
[`examples/exiobase_health_FR_air_emissions.ipynb`](examples/exiobase_health_FR_air_emissions.ipynb)

## Examples

The [`examples/`](examples/) folder contains notebooks:


- `demonstration_quickstart.ipynb` — minimal single-feedback run.

- `demonstration_two_feedbacks_pGWP.ipynb` — the two-feedbacks case with prospective
  pGWP characterization (MESSAGE SSP2-4.5).
- `demonstration_batch_parallel.ipynb` — running scenario batches in parallel with
  Ray (`pip install "FeedbackMachine[parallel]"`).
- `verification_trivial_cases.ipynb` — hand-solvable regression checks.
- `explore_coupling_matrix.ipynb` — step-by-step walk-through of the coupling matrix
  built in `FeedbackMachine.propagate()`.
- `exiobase_health_FR_air_emissions.ipynb` — derives the EXIOBASE emission profile
  used in the two-feedbacks examples (results in `exiobase_health_FR_results/`).

Most notebooks need a Brightway project with ecoinvent and premise databases, and
some read external input data (temperature trajectories, REMIND scenarios, yield
sensitivities) from local paths that you must adapt.

## Public API

Engine:

- `FeedbackMachine` — propagate feedbacks and assemble the total impact.
- `FeedbackMatrixGenerator` — build the per-time-step temporalized feedback database.

Response / modulation utilities:

- `create_curve`, `make_derivative`
- `ExactModel`, `NDRBFModel`, `build_nd_rbf_from_samples`
- `ModelRegistry`
- `PatternLookup`
- `numerical_partial`, `finite_difference_partial`, `check_partial_consistency`

Project / batch utilities (for running independent scenario batches in
parallel processes/kernels — see `project_batches.py`'s module docstring for
why each process needs its own Brightway project, not a shared one):

- `create_project_copies`, `setup_project_batches` — make N independent
  copies of a Brightway project (via `bw2data`'s own `copy_project`), one per
  worker process/kernel.
- `estimate_project_size` — check a project's on-disk size before copying.
- `merge_batch_results` — combine multiple pickled per-batch result dicts
  into one, detecting keys that collide across batches.
- `delete_project_copies` — clean up batch project copies afterward.



## Tests

The response/modulation utilities are covered by a Brightway-free test suite:

```bash
pytest
```


## Citation & references

If you use this package, please cite the underlying academic works.

## License

BSD 3-Clause. See [LICENSE](LICENSE).
