Search code examples
brightway

Identify which activity or which product is leading to a non-square technosphere matrix


I made several changes to my lci database using the Wurst python package. I then re-wrote my database using write_brightway2_database().

Obtaining 16718 datasets, 462812 exchanges and 0 unlinked exchanges.

Then once, I try to calculate LCA score with the modified database, I obtain a technosphere matrix that is not square, with the following dimension: 16718 activities (columns) and 16717 products (rows).

This is how I attempt to calculate an LCA score:

lca = LCA({db.random(): 1}, method=lcia_methods['CC'])
lca.lci()
lca.lcia()
print(lca.score)

The error message that I get:

NonsquareTechnosphere: Technosphere matrix is not square: 16718 activities (columns) and 16717 products (rows). Use LeastSquaresLCA to solve this system, or fix the input data

Then, I tried the following, plus some variations as recommended here :

for a in Database("database"):
     assert len(a.production()) == 1

But there is no dataset poping-up.

I also tried before re-writing my database from Wurst in BW2 format to do the following:

producion = {}

for ds in db:
      key = ds['code']
      producion[key] = []
      for exc in ds['exchanges']:
           if exc['type'] == 'production':
                  producion[key].append(exc['name'])


for v in producion.values():
    if len(v) != 1:
        print(v)

But again, I cannot identify any problematic dataset doing this.

Is there a simple way to identify which activity or which product is leading to a non-square technosphere matrix in order to fix my input data?


Solution

  • The error is that I created a dataset where the "name" field in my production exchange was different than the "name" of the dataset itself.

    To identify my dataset I did:

    for ds in Database('distribution and use'):
        for prod_exc in ds.production():
            try : assert (prod_exc['name'] == ds['name'])
            except : print(ds['name'])
    

    You can do the same test with ['location'], ['unit'], and other important fields.

    Other interesting fields to check if they coincide are the ['code'] from the dataset itself and the 'code' of the production exchange located in the field ['input'][1]

    for ds in Database("db_name"):
        for prod_exc in ds.production():
            if ((prod_exc['input'][1]) != ds['code']):
                print((ds['name'],ds['code'],ds['location'],prod_exc['input'], prod_exc['name']))