MolecularAI / aizynthfinder

A tool for retrosynthetic planning
https://molecularai.github.io/aizynthfinder/
MIT License
562 stars 128 forks source link

PolicyException: A QuickKerasFilter class needs to be initiated with keyword arguments: model" #149

Closed MachineGUN001 closed 5 months ago

MachineGUN001 commented 5 months ago

after re-installing the new version 4.0.0. tar.gz, Some errors occured by using the commandline

config_file ='./config_test.yml'
app = AiZynthApp(config_file)
{
    "name": "PolicyException",
    "message": "A QuickKerasFilter class needs to be initiated with keyword arguments: model",
    "stack": "---------------------------------------------------------------------------
PolicyException                           Traceback (most recent call last)
Cell In[3], line 3
      1 config_file =r'D:\\Cheminfo_Workshop\\7_Aisynth\\config5.yml'
      2 # config_file ='./config_test.yml'
----> 3 app = AiZynthApp(config_file)

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder\\interfaces\\aizynthapp.py:58, in AiZynthApp.__init__(self, configfile, setup)
     55 def __init__(self, configfile: str, setup: bool = True) -> None:
     56     # pylint: disable=used-before-assignment
     57     setup_logger(logging.INFO)
---> 58     self.finder = AiZynthFinder(configfile=configfile)
     59     self._input: StrDict = dict()
     60     self._output: StrDict = dict()

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder\\aizynthfinder.py:70, in AiZynthFinder.__init__(self, configfile, configdict)
     67 self._logger = logger()
     69 if configfile:
---> 70     self.config = Configuration.from_file(configfile)
     71 elif configdict:
     72     self.config = Configuration.from_dict(configdict)

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder\\context\\config.py:138, in Configuration.from_file(cls, filename)
    136     txt = txt.replace(item, os.environ[item[2:-1]])
    137 _config = yaml.load(txt, Loader=yaml.SafeLoader)
--> 138 return Configuration.from_dict(_config)

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder\\context\\config.py:108, in Configuration.from_dict(cls, source)
    105 config_obj._update_from_config(dict(source))
    107 config_obj.expansion_policy.load_from_config(**expansion_config)
--> 108 config_obj.filter_policy.load_from_config(**filter_config)
    109 config_obj.stock.load_from_config(**stock_config)
    110 config_obj.scorers.create_default_scorers()

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder\\context\\policy\\policies.py:223, in FilterPolicy.load_from_config(self, **config)
    221 if \"type\" in kwargs:
    222     del kwargs[\"type\"]
--> 223 obj = cls(key, self._config, **kwargs)
    224 self.load(obj)

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder\\context\\policy\\filter_strategies.py:81, in QuickKerasFilter.__init__(self, key, config, **kwargs)
     80 def __init__(self, key: str, config: Configuration, **kwargs: Any) -> None:
---> 81     super().__init__(key, config, **kwargs)
     82     source = kwargs[\"model\"]
     83     # self.settings = self._config.filter_settings

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder\\context\\policy\\filter_strategies.py:43, in FilterStrategy.__init__(self, key, config, **kwargs)
     41 def __init__(self, key: str, config: Configuration, **kwargs: Any) -> None:
     42     if any(name not in kwargs for name in self._required_kwargs):
---> 43         raise PolicyException(
     44             f\"A {self.__class__.__name__} class needs to be initiated \"
     45             f\"with keyword arguments: {', '.join(self._required_kwargs)}\"
     46         )
     47     self._config = config
     48     self._logger = logger()

PolicyException: A QuickKerasFilter class needs to be initiated with keyword arguments: model"
}

could you please help me how to fix that? many many thanks,

Best,

SGenheden commented 5 months ago

The format of the config file has changed, see the documentation here: https://molecularai.github.io/aizynthfinder/configuration.html

It should be straightforward to update your file if before you had

filter:
  files:
     uspto: name.onnx
policy:
  files:
    uspto:
       - a.onnx
       - b.csv.gz
stock:
  files: 
    zinc: zinc.hdf5

you now have to specify it like this

filter:
  uspto: name.onnx
expansion:
  uspto:
      - a.onnx
      - b.csv.gz
stock:
  zinc: zinc.hdf5

so we got rid of the "files"-section of the file, and renamed "policy" to "expansion"

MachineGUN001 commented 5 months ago

The format of the config file has changed, see the documentation here: https://molecularai.github.io/aizynthfinder/configuration.html

It should be straightforward to update your file if before you had

filter:
  files:
     uspto: name.onnx
policy:
  files:
    uspto:
       - a.onnx
       - b.csv.gz
stock:
  files: 
    zinc: zinc.hdf5

you now have to specify it like this

filter:
  uspto: name.onnx
expansion:
  uspto:
      - a.onnx
      - b.csv.gz
stock:
  zinc: zinc.hdf5

so we got rid of the "files"-section of the file, and renamed "policy" to "expansion"

thanks for your suggestions, and that works!

Does the current version 4.0 only support `uspto:.

SGenheden commented 5 months ago

The new version do support the hdf5 format of old models. What error did you receive when you tried using the old uspto_model.hdf5 file

MachineGUN001 commented 5 months ago

@SGenheden thanks for your explanation.

below is the error I received when I tried to use the old uspto_model.hdf5

{
    "name": "ValueError",
    "message": "Tensorflow is not installed - this installation only supports ONNX models",
    "stack": "---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[6], line 3
      1 config_file =r'D:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder40\\config1.yml'
      2 # config_file ='./config_test.yml'
----> 3 app = AiZynthApp(config_file)

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder40\\aizynthfinder\\interfaces\\aizynthapp.py:58, in AiZynthApp.__init__(self, configfile, setup)
     55 def __init__(self, configfile: str, setup: bool = True) -> None:
     56     # pylint: disable=used-before-assignment
     57     setup_logger(logging.INFO)
---> 58     self.finder = AiZynthFinder(configfile=configfile)
     59     self._input: StrDict = dict()
     60     self._output: StrDict = dict()

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder40\\aizynthfinder\\aizynthfinder.py:70, in AiZynthFinder.__init__(self, configfile, configdict)
     67 self._logger = logger()
     69 if configfile:
---> 70     self.config = Configuration.from_file(configfile)
     71 elif configdict:
     72     self.config = Configuration.from_dict(configdict)

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder40\\aizynthfinder\\context\\config.py:138, in Configuration.from_file(cls, filename)
    136     txt = txt.replace(item, os.environ[item[2:-1]])
    137 _config = yaml.load(txt, Loader=yaml.SafeLoader)
--> 138 return Configuration.from_dict(_config)

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder40\\aizynthfinder\\context\\config.py:107, in Configuration.from_dict(cls, source)
    104 config_obj = Configuration()
    105 config_obj._update_from_config(dict(source))
--> 107 config_obj.expansion_policy.load_from_config(**expansion_config)
    108 config_obj.filter_policy.load_from_config(**filter_config)
    109 config_obj.stock.load_from_config(**stock_config)

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder40\\aizynthfinder\\context\\policy\\policies.py:132, in ExpansionPolicy.load_from_config(self, **config)
    130 if \"type\" in kwargs:
    131     del kwargs[\"type\"]
--> 132 obj = cls(key, self._config, **kwargs)
    133 self.load(obj)

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder40\\aizynthfinder\\context\\policy\\expansion_strategies.py:198, in TemplateBasedExpansionStrategy.__init__(self, key, config, **kwargs)
    193 self.chiral_fingerprints = bool(kwargs.get(\"chiral_fingerprints\", False))
    195 self._logger.info(
    196     f\"Loading template-based expansion policy model from {source} to {self.key}\"
    197 )
--> 198 self.model = load_model(source, self.key, self.use_remote_models)
    200 self._logger.info(f\"Loading templates from {templatefile} to {self.key}\")
    201 if templatefile.endswith(\".csv.gz\") or templatefile.endswith(\".csv\"):

File d:\\Cheminfo_Workshop\\7_Aisynth\\aizynthfinder40\\aizynthfinder\\utils\\models.py:77, in load_model(source, key, use_remote_models)
     74     return LocalOnnxModel(source)
     76 if not SUPPORT_EXTERNAL_APIS:
---> 77     raise ValueError(
     78         \"Tensorflow is not installed - this installation only supports ONNX models\"
     79     )
     81 if not use_remote_models:
     82     return LocalKerasModel(source)

ValueError: Tensorflow is not installed - this installation only supports ONNX models"
}

when I tried using the current ONNX models, the Aizynthfinder program can work well.

however, I still have a confusion why the stock model (zinc_stock.hdf5) can be used, which is an old hdf5 model I used before.

stock:
  zinc: zinc_stock.hdf5

many thanks again to your team for the great work!

Best,

SGenheden commented 5 months ago

You would need to install aizynthfinder with tensorflow support something like this python -m pip install aizynthfinder[tf]

MachineGUN001 commented 5 months ago

@SGenheden thank you so much to provide the suggestion. after re-installing the aizynthfinder with tensorflow support, the old hf5.model can be read.

Best,