MolecularAI / aizynthfinder

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

Error while using `route cost` scorer #164

Closed shreyasvinaya closed 1 month ago

shreyasvinaya commented 1 month ago

Hi, I was trying to use the route cost scorer but I ended up getting the below error

{
    "name": "ScorerException",
    "message": "Unable to find 'route cost' in parent collection",
    "stack": "---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~/Retrosynthesis/aizynthfinder/aizynthfinder/context/scoring/collection.py:129, in ScorerCollection.make_subset(self, subset_names)
    128     print(self._items)
--> 129     scorer = self._items[name]
    130 except KeyError:

KeyError: 'route cost'

During handling of the above exception, another exception occurred:

ScorerException                           Traceback (most recent call last)
Cell In[3], line 6
      4 finder.filter_policy.select(\"uspto\")
      5 finder.target_smiles = smiles
----> 6 finder.tree_search()
      7 finder.build_routes()
      8 stats = finder.extract_statistics()

File ~/Retrosynthesis/aizynthfinder/aizynthfinder/aizynthfinder.py:208, in AiZynthFinder.tree_search(self, show_progress)
    201 \"\"\"
    202 Perform the actual tree search
    203 
    204 :param show_progress: if True, shows a progress bar
    205 :return: the time past in seconds
    206 \"\"\"
    207 if not self.tree:
--> 208     self.prepare_tree()
    209 # This is for type checking, prepare_tree is creating it.
    210 assert self.tree is not None

File ~/Retrosynthesis/aizynthfinder/aizynthfinder/aizynthfinder.py:176, in AiZynthFinder.prepare_tree(self)
    173 if self.config.search.break_bonds or self.config.search.freeze_bonds:
    174     self._setup_focussed_bonds(self.target_mol)
--> 176 self._setup_search_tree()
    177 self.analysis = None
    178 self.routes = RouteCollection([])

File ~/Retrosynthesis/aizynthfinder/aizynthfinder/aizynthfinder.py:287, in AiZynthFinder._setup_search_tree(self)
    285 self._logger.debug(f\"Defining tree root:  {self.target_smiles}\")
    286 if self.config.search.algorithm.lower() == \"mcts\":
--> 287     self.tree = MctsSearchTree(
    288         root_smiles=self.target_smiles, config=self.config
    289     )
    290 else:
    291     cls = load_dynamic_class(self.config.search.algorithm)

File ~/Retrosynthesis/aizynthfinder/aizynthfinder/search/mcts/search.py:68, in MctsSearchTree.__init__(self, config, root_smiles)
     66 self._logger.setLevel(logging.WARNING)  # Supress logging from `make_subset`
     67 self._logger.debug(f\"Selecting reward scorers: {config_rewards}\")
---> 68 self.reward_scorer = self.config.scorers.make_subset(config_rewards)
     69 self._logger.setLevel(logging.DEBUG)
     70 if self.mode == \"single-objective\":

File ~/Retrosynthesis/aizynthfinder/aizynthfinder/context/scoring/collection.py:131, in ScorerCollection.make_subset(self, subset_names)
    129     scorer = self._items[name]
    130 except KeyError:
--> 131     raise ScorerException(f\"Unable to find '{name}' in parent collection\")
    132 new_collection.load(scorer, silent=True)
    133 new_collection.select(name, append=True)

ScorerException: Unable to find 'route cost' in parent collection"
}

It would be amazing if i could get some help on how to get it working Thanks in advance

SGenheden commented 1 month ago

Hello,

The RouteCostScorer is not loaded by default. You need to add it to the config file.

This can be accomplished with

scorer:
  RouteCostScorer: 

however, it should be noted that for this to be more accurate you need to employ a stock that include price information. We don't official support such a stock, so you would need to create it yourself.

shreyasvinaya commented 1 month ago

Hi @SGenheden , Thanks for the reply, This solution worked, Could you please help me get the route cost for every step Thanks in advance

SGenheden commented 1 month ago

Hello, Great that it worked. The scorers, score the entire route - there is no such thing as a route cost for an individual step.

shreyasvinaya commented 1 month ago

Got it, Thank you for the clarification and all the help