brightway-lca / brightway2-data

Tools for the management of inventory databases and impact assessment methods. Part of the Brightway LCA framework.
https://docs.brightway.dev/
BSD 3-Clause "New" or "Revised" License
11 stars 24 forks source link

Fail in writing database from imported local database (previously exported as Excel from AB) #212

Open Christhel-Andrade opened 2 days ago

Christhel-Andrade commented 2 days ago

When trying to import the following file AB_LCI_HPO.xlsx which has been exported from Activity Browser, I get an error. This error happens with various files but not with all my files; while they all have been exported the same way, on the same computer, and on the same version of AB and ecoinvent.

Versions:

Application version: 2.10.3 bw2data version: 3.6.6 bw2io version: 1.8.2 bw2calc version: 0.8.12

Python code

I run the code using ExcelImporter

imp = bw.ExcelImporter(r'my_path\AB_LCI_HPO.xlsx') #give the path to your inventory excel file
imp.apply_strategies()
imp.match_database("consequential391", fields=('name','unit','location', 'reference product'))
#imp.match_database(fields=('name', 'unit', 'location'))
imp.statistics()
#imp.write_excel(only_unlinked=True)
imp.write_excel()
list(imp.unlinked)

imp.write_database()

ERROR in the console:

Error logs ``` --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[7], line 10 7 imp.write_excel() 8 list(imp.unlinked) ---> 10 imp.write_database() File [~\AppData\Local\anaconda3\envs\premise\Lib\site-packages\bw2io\importers\excel.py:284](http://localhost:8888/lab/tree/Users/andraded/Documents/autumn-school-2024/autumn-school-dds-psi-2024/tutorials/activity_browser/~/AppData/Local/anaconda3/envs/premise/Lib/site-packages/bw2io/importers/excel.py#line=283), in ExcelImporter.write_database(self, **kwargs) 282 """Same as base ``write_database`` method, but ``activate_parameters`` is True by default.""" 283 kwargs["activate_parameters"] = kwargs.get("activate_parameters", True) --> 284 super(ExcelImporter, self).write_database(**kwargs) File [~\AppData\Local\anaconda3\envs\premise\Lib\site-packages\bw2io\importers\base_lci.py:273](http://localhost:8888/lab/tree/Users/andraded/Documents/autumn-school-2024/autumn-school-dds-psi-2024/tutorials/activity_browser/~/AppData/Local/anaconda3/envs/premise/Lib/site-packages/bw2io/importers/base_lci.py#line=272), in LCIImporter.write_database(self, data, delete_existing, backend, activate_parameters, **kwargs) 270 self.write_database_parameters(activate_parameters, delete_existing) 272 existing.update(data) --> 273 db.write(existing) 275 if activate_parameters: 276 self._write_activity_parameters(activity_parameters) File [~\AppData\Local\anaconda3\envs\premise\Lib\site-packages\bw2data\project.py:358](http://localhost:8888/lab/tree/Users/andraded/Documents/autumn-school-2024/autumn-school-dds-psi-2024/tutorials/activity_browser/~/AppData/Local/anaconda3/envs/premise/Lib/site-packages/bw2data/project.py#line=357), in writable_project(wrapped, instance, args, kwargs) 356 if projects.read_only: 357 raise ReadOnlyProject(READ_ONLY_PROJECT) --> 358 return wrapped(*args, **kwargs) File [~\AppData\Local\anaconda3\envs\premise\Lib\site-packages\bw2data\backends\peewee\database.py:266](http://localhost:8888/lab/tree/Users/andraded/Documents/autumn-school-2024/autumn-school-dds-psi-2024/tutorials/activity_browser/~/AppData/Local/anaconda3/envs/premise/Lib/site-packages/bw2data/backends/peewee/database.py#line=265), in SQLiteBackend.write(self, data, process) 263 self.delete(warn=False) 264 raise --> 266 self.make_searchable(reset=True) 268 if process: 269 self.process() File [~\AppData\Local\anaconda3\envs\premise\Lib\site-packages\bw2data\project.py:358](http://localhost:8888/lab/tree/Users/andraded/Documents/autumn-school-2024/autumn-school-dds-psi-2024/tutorials/activity_browser/~/AppData/Local/anaconda3/envs/premise/Lib/site-packages/bw2data/project.py#line=357), in writable_project(wrapped, instance, args, kwargs) 356 if projects.read_only: 357 raise ReadOnlyProject(READ_ONLY_PROJECT) --> 358 return wrapped(*args, **kwargs) File [~\AppData\Local\anaconda3\envs\premise\Lib\site-packages\bw2data\backends\peewee\database.py:311](http://localhost:8888/lab/tree/Users/andraded/Documents/autumn-school-2024/autumn-school-dds-psi-2024/tutorials/activity_browser/~/AppData/Local/anaconda3/envs/premise/Lib/site-packages/bw2data/backends/peewee/database.py#line=310), in SQLiteBackend.make_searchable(self, reset) 309 databases.flush() 310 IndexManager(self.filename).delete_database() --> 311 IndexManager(self.filename).add_datasets(self) File [~\AppData\Local\anaconda3\envs\premise\Lib\site-packages\bw2data\search\indices.py:47](http://localhost:8888/lab/tree/Users/andraded/Documents/autumn-school-2024/autumn-school-dds-psi-2024/tutorials/activity_browser/~/AppData/Local/anaconda3/envs/premise/Lib/site-packages/bw2data/search/indices.py#line=46), in IndexManager.add_datasets(self, datasets) 45 writer = self.get().writer() 46 for ds in datasets: ---> 47 writer.add_document(**self._format_dataset(ds)) 48 writer.commit() File [~\AppData\Local\anaconda3\envs\premise\Lib\site-packages\bw2data\search\indices.py:35](http://localhost:8888/lab/tree/Users/andraded/Documents/autumn-school-2024/autumn-school-dds-psi-2024/tutorials/activity_browser/~/AppData/Local/anaconda3/envs/premise/Lib/site-packages/bw2data/search/indices.py#line=34), in IndexManager._format_dataset(self, ds) 29 def _format_dataset(self, ds): 30 fl = lambda o: o[1].lower() if isinstance(o, tuple) else o.lower() 31 return dict( 32 name=ds.get(u"name", u"").lower(), 33 comment=ds.get(u"comment", u"").lower(), 34 product=ds.get(u"reference product", u"").lower(), ---> 35 categories=u", ".join(ds.get(u"categories", [])).lower(), 36 location=fl(ds.get(u"location", u"")), 37 database=ds[u"database"], 38 code=ds['code'] 39 ) File [~\AppData\Local\anaconda3\envs\premise\Lib\_collections_abc.py:774](http://localhost:8888/lab/tree/Users/andraded/Documents/autumn-school-2024/autumn-school-dds-psi-2024/tutorials/activity_browser/~/AppData/Local/anaconda3/envs/premise/Lib/_collections_abc.py#line=773), in Mapping.get(self, key, default) 772 'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.' 773 try: --> 774 return self[key] 775 except KeyError: 776 return default File [~\AppData\Local\anaconda3\envs\premise\Lib\site-packages\bw2data\backends\peewee\proxies.py:125](http://localhost:8888/lab/tree/Users/andraded/Documents/autumn-school-2024/autumn-school-dds-psi-2024/tutorials/activity_browser/~/AppData/Local/anaconda3/envs/premise/Lib/site-packages/bw2data/backends/peewee/proxies.py#line=124), in Activity.__getitem__(self, key) 122 except ValueError: 123 raise KeyError --> 125 if key in rp.get('classifications', []): 126 return rp['classifications'][key] 127 if key in rp.get('properties', []): TypeError: argument of type 'float' is not iterable ```
nicolnt commented 2 days ago

I have the same issue with AB 2.9.1

conda list output ``` # packages in environment at C:\Users\lienart\AppData\Local\miniconda3\envs\ab: # # Name Version Build Channel activity-browser 2.9.7 pyhd8ed1ab_0 conda-forge appdirs 1.4.4 pyh9f0ad1d_0 conda-forge arrow 1.3.0 pyhd8ed1ab_0 conda-forge asteval 0.9.32 pyhd8ed1ab_0 conda-forge astunparse 1.6.3 pyhd8ed1ab_0 conda-forge blinker 1.8.2 pyhd8ed1ab_0 conda-forge brightway2 2.4.4 pyhd8ed1ab_0 conda-forge brotli 1.1.0 hcfcfb64_1 conda-forge brotli-bin 1.1.0 hcfcfb64_1 conda-forge brotli-python 1.1.0 py311h12c1d0e_1 conda-forge brotlicffi 1.1.0.0 py311h12c1d0e_1 conda-forge bw2analyzer 0.10 pyhd8ed1ab_0 conda-forge bw2calc 1.8.2 py311h1ea47a8_0 conda-forge bw2data 3.6.6 pyhd8ed1ab_0 conda-forge bw2io 0.8.10 pyhd8ed1ab_0 conda-forge bw2parameters 1.1.0 pyhd8ed1ab_0 conda-forge bw_migrations 0.2 pyhd8ed1ab_0 conda-forge bzip2 1.0.8 hcfcfb64_5 conda-forge ca-certificates 2024.2.2 h56e8100_0 conda-forge certifi 2024.2.2 pyhd8ed1ab_0 conda-forge cffi 1.16.0 py311ha68e1ae_0 conda-forge charset-normalizer 3.3.2 pyhd8ed1ab_0 conda-forge click 8.1.7 win_pyh7428d3b_0 conda-forge colorama 0.4.6 pyhd8ed1ab_0 conda-forge contourpy 1.2.1 py311h005e61a_0 conda-forge cycler 0.12.1 pyhd8ed1ab_0 conda-forge dill 0.3.8 pyhd8ed1ab_0 conda-forge docopt 0.6.2 py_1 conda-forge eidl 2.0.1 pyhd8ed1ab_0 conda-forge eight 1.0.1 pyhd8ed1ab_4 conda-forge et_xmlfile 1.1.0 pyhd8ed1ab_0 conda-forge fasteners 0.17.3 pyhd8ed1ab_0 conda-forge flask 3.0.3 pyhd8ed1ab_0 conda-forge fonttools 4.51.0 py311ha68e1ae_0 conda-forge freetype 2.12.1 hdaf720e_2 conda-forge future 1.0.0 pyhd8ed1ab_0 conda-forge gettext 0.22.5 h5728263_2 conda-forge gettext-tools 0.22.5 h7d00a51_2 conda-forge glib 2.80.2 h0df6a38_0 conda-forge glib-tools 2.80.2 h2f9d560_0 conda-forge gst-plugins-base 1.22.9 h001b923_1 conda-forge gstreamer 1.22.9 hb4038d2_1 conda-forge icu 73.2 h63175ca_0 conda-forge idna 3.7 pyhd8ed1ab_0 conda-forge importlib-metadata 7.1.0 pyha770c72_0 conda-forge importlib_metadata 7.1.0 hd8ed1ab_0 conda-forge inflate64 1.0.0 py311ha68e1ae_1 conda-forge intel-openmp 2024.1.0 h57928b3_965 conda-forge itsdangerous 2.2.0 pyhd8ed1ab_0 conda-forge jinja2 3.1.4 pyhd8ed1ab_0 conda-forge kiwisolver 1.4.5 py311h005e61a_1 conda-forge krb5 1.21.2 heb0366b_0 conda-forge lcms2 2.16 h67d730c_0 conda-forge lerc 4.0.0 h63175ca_0 conda-forge libasprintf 0.22.5 h5728263_2 conda-forge libasprintf-devel 0.22.5 h5728263_2 conda-forge libblas 3.9.0 22_win64_mkl conda-forge libbrotlicommon 1.1.0 hcfcfb64_1 conda-forge libbrotlidec 1.1.0 hcfcfb64_1 conda-forge libbrotlienc 1.1.0 hcfcfb64_1 conda-forge libcblas 3.9.0 22_win64_mkl conda-forge libclang13 18.1.5 default_hf64faad_0 conda-forge libdeflate 1.20 hcfcfb64_0 conda-forge libexpat 2.6.2 h63175ca_0 conda-forge libffi 3.4.2 h8ffe710_5 conda-forge libgettextpo 0.22.5 h5728263_2 conda-forge libgettextpo-devel 0.22.5 h5728263_2 conda-forge libglib 2.80.2 h0df6a38_0 conda-forge libhwloc 2.9.1 h51c2c0f_0 conda-forge libiconv 1.17 hcfcfb64_2 conda-forge libintl 0.22.5 h5728263_2 conda-forge libintl-devel 0.22.5 h5728263_2 conda-forge libjpeg-turbo 3.0.0 hcfcfb64_1 conda-forge liblapack 3.9.0 22_win64_mkl conda-forge libogg 1.3.4 h8ffe710_1 conda-forge libpng 1.6.43 h19919ed_0 conda-forge libsqlite 3.45.3 hcfcfb64_0 conda-forge libtiff 4.6.0 hddb2be6_3 conda-forge libvorbis 1.3.7 h0e60522_0 conda-forge libwebp 1.4.0 h2466b09_0 conda-forge libwebp-base 1.4.0 hcfcfb64_0 conda-forge libxcb 1.15 hcd874cb_0 conda-forge libxml2 2.10.4 hc3477c8_0 conda-forge libxslt 1.1.37 h0192164_0 conda-forge libzlib 1.2.13 hcfcfb64_5 conda-forge lxml 4.9.2 py311h5942461_0 conda-forge m2w64-gcc-libgfortran 5.3.0 6 conda-forge m2w64-gcc-libs 5.3.0 7 conda-forge m2w64-gcc-libs-core 5.3.0 7 conda-forge m2w64-gmp 6.1.0 2 conda-forge m2w64-libwinpthread-git 5.0.0.4634.697f757 2 conda-forge markupsafe 2.1.5 py311ha68e1ae_0 conda-forge matplotlib-base 3.8.4 py311h9b31f6e_2 conda-forge mkl 2024.1.0 h66d3029_692 conda-forge mrio_common_metadata 0.2.1 pyhd8ed1ab_0 conda-forge msys2-conda-epoch 20160418 1 conda-forge multiprocess 0.70.16 py311ha68e1ae_0 conda-forge multivolumefile 0.2.3 pyhd8ed1ab_0 conda-forge munkres 1.1.4 pyh9f0ad1d_0 conda-forge networkx 3.3 pyhd8ed1ab_1 conda-forge numpy 1.23.5 py311h95d790f_0 conda-forge openjpeg 2.5.2 h3d672ee_0 conda-forge openpyxl 3.1.2 py311ha68e1ae_1 conda-forge openssl 3.3.0 h2466b09_2 conda-forge packaging 24.0 pyhd8ed1ab_0 conda-forge pandas 2.1.4 py311hf63dbb6_0 conda-forge pathos 0.3.2 pyhd8ed1ab_1 conda-forge patsy 0.5.6 pyhd8ed1ab_0 conda-forge pcre2 10.43 h17e33f8_0 conda-forge peewee 3.17.3 py311had142b2_0 conda-forge pillow 10.3.0 py311h6819b35_0 conda-forge pint 0.21 pyhd8ed1ab_0 conda-forge pip 24.0 pyhd8ed1ab_0 conda-forge pox 0.3.4 pyhd8ed1ab_0 conda-forge ppft 1.7.6.8 pyhd8ed1ab_0 conda-forge psutil 5.9.8 py311ha68e1ae_0 conda-forge pthread-stubs 0.4 hcd874cb_1001 conda-forge pthreads-win32 2.9.1 hfa6e2cd_3 conda-forge py7zr 0.21.0 pyhd8ed1ab_0 conda-forge pybcj 1.0.2 py311ha68e1ae_0 conda-forge pycparser 2.22 pyhd8ed1ab_0 conda-forge pycryptodomex 3.19.0 py311ha68e1ae_1 conda-forge pypardiso 0.4.6 pyhd8ed1ab_0 conda-forge pyparsing 3.1.2 pyhd8ed1ab_0 conda-forge pyperclip 1.8.2 pyhd8ed1ab_2 conda-forge pyppmd 1.1.0 py311h12c1d0e_0 conda-forge pyprind 2.11.2 py311h1ea47a8_1005 conda-forge pyside2 5.15.8 py311h2411a93_2 conda-forge pysocks 1.7.1 pyh0701188_6 conda-forge python 3.11.9 h631f459_0_cpython conda-forge python-dateutil 2.9.0 pyhd8ed1ab_0 conda-forge python-tzdata 2024.1 pyhd8ed1ab_0 conda-forge python_abi 3.11 4_cp311 conda-forge pytz 2024.1 pyhd8ed1ab_0 conda-forge pyxlsb 1.0.10 pyhd8ed1ab_0 conda-forge pyzstd 0.16.0 py311h53056dc_0 conda-forge qt-main 5.15.8 h9e85ed6_20 conda-forge qt-webengine 5.15.8 h4bf5c4e_4 conda-forge requests 2.32.2 pyhd8ed1ab_0 conda-forge salib 1.5.0 pyhd8ed1ab_0 conda-forge scipy 1.13.0 py311hd4686c6_1 conda-forge seaborn 0.13.2 hd8ed1ab_2 conda-forge seaborn-base 0.13.2 pyhd8ed1ab_2 conda-forge setuptools 70.0.0 pyhd8ed1ab_0 conda-forge setuptools-scm 8.1.0 pyhd8ed1ab_0 conda-forge six 1.16.0 pyh6c4a22f_0 conda-forge stats_arrays 0.6.6 pyhd8ed1ab_0 conda-forge statsmodels 0.14.2 py311h0a17f05_0 conda-forge tabulate 0.9.0 pyhd8ed1ab_1 conda-forge tbb 2021.9.0 h91493d7_0 conda-forge texttable 1.7.0 pyhd8ed1ab_0 conda-forge tk 8.6.13 h5226925_1 conda-forge tomli 2.0.1 pyhd8ed1ab_0 conda-forge tqdm 4.66.4 pyhd8ed1ab_0 conda-forge types-python-dateutil 2.9.0.20240316 pyhd8ed1ab_0 conda-forge typing-extensions 4.11.0 hd8ed1ab_0 conda-forge typing_extensions 4.11.0 pyha770c72_0 conda-forge tzdata 2024a h0c530f3_0 conda-forge ucrt 10.0.22621.0 h57928b3_0 conda-forge unicodecsv 0.14.1 pyhd8ed1ab_2 conda-forge unidecode 1.3.8 pyhd8ed1ab_0 conda-forge urllib3 2.2.1 pyhd8ed1ab_0 conda-forge vc 14.3 ha32ba9b_20 conda-forge vc14_runtime 14.38.33135 h835141b_20 conda-forge voluptuous 0.14.2 pyhd8ed1ab_0 conda-forge vs2015_runtime 14.38.33135 h22015db_20 conda-forge werkzeug 3.0.3 pyhd8ed1ab_0 conda-forge wheel 0.43.0 pyhd8ed1ab_1 conda-forge whoosh 2.7.4 py311h1ea47a8_8 conda-forge win_inet_pton 1.1.0 pyhd8ed1ab_6 conda-forge wrapt 1.16.0 py311ha68e1ae_0 conda-forge xlrd 2.0.1 pyhd8ed1ab_3 conda-forge xlsxwriter 3.1.9 pyhd8ed1ab_0 conda-forge xorg-libxau 1.0.11 hcd874cb_0 conda-forge xorg-libxdmcp 1.1.3 hcd874cb_0 conda-forge xz 5.2.6 h8d14728_0 conda-forge zipfile-deflate64 0.2.0 py311ha68e1ae_4 conda-forge zipp 3.17.0 pyhd8ed1ab_0 conda-forge zstd 1.5.6 h0ea2cb4_0 conda-forge ```
nicolnt commented 2 days ago

Another file exported from AB 2.9.1 this time that doesn't want to import in bw using Christhel's code lci-Case_Study_ACV_LCI - ab.xlsx