PascalLesage / presamples

Package to write, load, manage and verify numerical arrays, called presamples.
BSD 3-Clause "New" or "Revised" License
14 stars 11 forks source link

Use of presamples in LCA and index advancement could be improved #67

Open PascalLesage opened 4 years ago

PascalLesage commented 4 years ago

Suppose a presamples package containing matrix data with the following properties:

If used in MonteCarloLCA, the indices start at 0 (thanks to the reset of the indexers here). For each iteration (i.e. each time next(mc_object) is called), the matrix data indexer goes up by 1. All this is sane and easy to use.

For LCA, however, things are more complicated.

Suppose the data in the presamples package is some time series, and we want to generate LCA results for each time step by calling LCA n times. If we just use the LCA.lci() method, things don't work:

>>> import brightway2 as bw
>>> import presamples as ps
>>> lca = bw.LCA({some_act:1}, presamples=[pp_dirpath])
>>> print("iteration\tindex")
>>> for i in range(5):
...     lca.lci()
...     print(i, "\t", lca.presamples.matrix_indexer[0].index)

iteration   index
0        0
1        0
2        0
3        0
4        0

We need to actively update indices to get the indices moving up. If we naively use update_matrices, we don't get the first column.

>>> lca = bw.LCA({some_act:1}, presamples=[pp_dirpath])
>>> print("iteration\tindex")
>>> for i in range(5):
...     lca.presamples.update_matrices()
...     lca.lci()
...     print(i, "\t", lca.presamples.matrix_indexer[0].index)

iteration   index
0        1
1        2
2        3
3        4
4        5

Resetting sequential indices, like for MonteCarloLCA, gives the expected behaviour to the problem described above:

>>> lca = bw.LCA({some_act:1}, presamples=[pp_dirpath])
>>> lca.presamples.reset_sequential_indices()
>>> print("iteration\tindex")
>>> for i in range(5):
...     lca.presamples.update_matrices()
...     lca.lci()
...     print(i, "\t", lca.presamples.matrix_indexer[0].index)

iteration   index
0        0
1        1
2        2
3        3
4        4

This resetting could simply be added here Tried it, no presamples tests fail.

We may want to consider moving indices directly when the lci method is invoked.

PascalLesage commented 4 years ago

The simple fix above has tests failing in bw.LCA.

PascalLesage commented 4 years ago

Tests in LCA show that the expected behaviour is not what would be allowed by the change described above.

Indeed, the expected behaviour is that:

So the expected usage is sequentially seeded presample packages in LCA would be something like this:

>>> lca = bw.LCA({some_act:1}, presamples=[pp_dirpath])
>>> print("iteration\tindex")
>>> for i in range(5):
...     if i != 0:
...         lca.presamples.update_matrices()
...     lca.lci()
...     print(i, "\t", lca.presamples.matrix_indexer[0].index)

iteration   index
0        0
1        1
2        2
3        3
4        4

The if clause is a bit annoying, so we need to decide what approach is better:

@cmutel any preference?