Andrew-S-Rosen / QMOF

The QMOF Database: A database of quantum-mechanical properties for metal-organic frameworks.
MIT License
125 stars 25 forks source link

Pymatgen import error with OFM #17

Closed Andrew-S-Rosen closed 3 years ago

Andrew-S-Rosen commented 3 years ago

Moved from https://github.com/arosen93/QMOF/discussions/16, which stated: _This is my prolem,ImportError: cannot import name 'VoronoiCoordFinder' from 'pymatgen.analysis.structureanalyzer'. What is the version of pymatgen package?Has the function been removed? How can I solve this problem? Thank you very much.

Hi @tuantuan-lin. I'm happy to try and help. Pymatgen recently introduced some backwards-incompatible changes with how functions are called, as noted here. I imagine this is the likely cause.

In order to help you, let's make sure we're talking about the same thing. I assume that you are referring to ofm_feature_generator.py, correct? And the problem appears upon calling the OrbitalFieldMatrix function from matminer? Please let me know the full traceback error (e.g. what lines it's failing on).

If this is the case, please try the following:

  1. Uninstall matminer.
  2. Uninstall pymatgen.
  3. Install the most recent version of pymatgen (pip install pymatgen).
  4. Clone the current matminer repository (git clone https://github.com/hackingmaterials/matminer.git).
  5. In the base directory of the downloaded matminer repository, install matminer via pip install .
  6. Try the code again.

As for the specific versions used in the QMOF Database paper, please see the "Additional Software and Hardware Details for Machine Learning" section (bottom of pg 8) in the Supporting Information. I used Pymatgen v.2020.12.3 with matminer v.0.6.4.

tuantuan-lin commented 3 years ago

Thank you very much for your help. The problem does appear upon calling the OrbitalFieldMatrix function from matminer.And I tried the methods you gave, there are still the following problems: Traceback (most recent call last): File "C:\Users\Administrator.DESKTOP-M4OGFT6\AppData\Roaming\Python\Python37\site packages\IPython\core\interactiveshell.py", line 3441, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 1, in runfile('E:/1/10-Model/04-OFM/ofm_feature_generator.py', wdir='E:/1/10-Model/04-OFM') File "D:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pydev_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "D:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "E:/1/10-Model/04-OFM/ofm_feature_generator.py", line 1, in from matminer.featurizers.structure import OrbitalFieldMatrix File "D:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pydev_pydev_bundle\pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, **kwargs) File "D:\ProgramData\Anaconda3\lib\site-packages\matminer\featurizers\structure.py", line 21, in from pymatgen import Structure, Lattice ImportError: cannot import name 'Structure' from 'pymatgen' (unknown location)

So I changed from pymatgen import Structure, Lattice to from pymatgen.core import Structure, Lattice in file structure.py But there is still the following problem: Traceback (most recent call last): File "C:\Users\Administrator.DESKTOP-M4OGFT6\AppData\Roaming\Python\Python37\site packages\IPython\core\interactiveshell.py", line 3441, in run_code exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in runfile('E:/1/10-Model/04-OFM/ofm_feature_generator.py', wdir='E:/1/10-Model/04-OFM') File "D:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pydev_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "D:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "E:/1/10-Model/04-OFM/ofm_feature_generator.py", line 1, in from matminer.featurizers.structure import OrbitalFieldMatrix File "D:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pydev_pydev_bundle\pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, kwargs) File "D:\ProgramData\Anaconda3\lib\site-packages\matminer\featurizers\structure.py", line 34, in from matminer.featurizers.site import OPSiteFingerprint, \ File "D:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pydev_pydev_bundle\pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, *kwargs) File "D:\ProgramData\Anaconda3\lib\site-packages\matminer\featurizers\site.py", line 11, in from matminer.utils.data import MagpieData File "D:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pydev_pydev_bundle\pydev_import_hook.py", line 21, in do_import module = self._system_import(name, args, kwargs) File "D:\ProgramData\Anaconda3\lib\site-packages\matminer\utils\data.py", line 17, in from pymatgen import Element ImportError: cannot import name 'Element' from 'pymatgen' (unknown location)

What can I do to correct my mistakes? Thank you very much !

Andrew-S-Rosen commented 3 years ago

From your information, it looks like there is a conflict between Pymatgen and matminer, as I alluded to in my original message.

You have two options:

  1. Use a version of Pymatgen that is before the backwards-incompatible changes introduced in v.2022.0.*. This could be done by doing pip uninstall pymatgen followed by pip install pymatgen==2020.12.3, for instance. Check the version number with import pymatgen as pm followed by print(pm.__version__). OR
  2. Use the most recent version of matminer on GitHub, which has been updated for Pymatgen v.2022.0.* and newer. This could be done by doing pip uninstall matminer (confirm it's not installed anymore by trying to import matminer) followed by git clone https://github.com/hackingmaterials/matminer.git and using pip install . (or pip install -r requirements.txt) in the base directory.

In my prior message, I walked through the steps on how to address Option 2. However, it looks like you did not properly uninstall your version of matminer to replace it with the most recent version on GitHub. This is most clearly seen in the following error message:

File "D:\ProgramData\Anaconda3\lib\site-packages\matminer\featurizers\structure.py", line 21, in from pymatgen import Structure, Lattice
ImportError: cannot import name 'Structure' from 'pymatgen' (unknown location)

The current version of matminer on GitHub already has the updated from pymatgen.core import Structure, Lattice line, which means that you are not using this version of matminer despite using a version of Pymatgen that is >= 2022.0.*.

If you are still having issues after trying the above options, create a minimal Python script that only has the following to help with debugging:

from matminer.featurizers.structure import OrbitalFieldMatrix

If the above fails, I recommend opening an issue on the matminer page since it's a matminer-specific issue.

tuantuan-lin commented 3 years ago

As you said, the above method successfully solved my problem.You're so brillian. Thank you very much for your help!

Andrew-S-Rosen commented 3 years ago

Glad to help!