def __init__(self, key: str, config: Configuration, **kwargs: Any) -> None:
     if any(name not in kwargs for name in self._required_kwargs):
         raise PolicyException(
             f"A {self.__class__.__name__} class needs to be initiated "
             f"with keyword arguments: {', '.join(self._required_kwargs)}")
     self._config = config
     self._logger = logger()
     self.key = key
Beispiel #2
0
    def load(self, source: ExpansionStrategy) -> None:  # type: ignore
        """
        Add a pre-initialized expansion strategy object to the policy

        :param source: the item to add
        """
        if not isinstance(source, ExpansionStrategy):
            raise PolicyException(
                "Only objects of classes inherited from ExpansionStrategy can be added"
            )
        self._items[source.key] = source
Beispiel #3
0
    def apply(self, reaction: RetroReaction) -> None:
        """
        Apply the all the selected filters on the reaction. If the reaction
        should be rejected a `RejectionException` is raised

        :param reaction: the reaction to filter
        :raises: if the reaction should be rejected or if a policy is selected
        """
        if not self.selection:
            raise PolicyException("No filter policy selected")

        for name in self.selection:
            self[name](reaction)
Beispiel #4
0
    def __init__(self, key: str, config: Configuration, **kwargs: str) -> None:
        super().__init__(key, config, **kwargs)

        source = kwargs["source"]
        templatefile = kwargs["templatefile"]

        self._logger.info(
            f"Loading template-based expansion policy model from {source} to {self.key}"
        )
        self.model = load_model(source, self.key,
                                self._config.use_remote_models)

        self._logger.info(
            f"Loading templates from {templatefile} to {self.key}")
        self.templates: pd.DataFrame = pd.read_hdf(templatefile, "table")

        if hasattr(self.model, "output_size") and len(
                self.templates) != self.model.output_size:  # type: ignore
            raise PolicyException(
                f"The number of templates ({len(self.templates)}) does not agree with the "  # type: ignore
                f"output dimensions of the model ({self.model.output_size})")
Beispiel #5
0
    def get_actions(
        self, molecules: Sequence[TreeMolecule]
    ) -> Tuple[List[RetroReaction], List[float]]:
        """
        Get all the probable actions of a set of molecules, using the selected policies

        :param molecules: the molecules to consider
        :return: the actions and the priors of those actions
        :raises: PolicyException: if the policy isn't selected
        """
        if not self.selection:
            raise PolicyException("No expansion policy selected")

        all_possible_actions = []
        all_priors = []
        for name in self.selection:
            possible_actions, priors = self[name].get_actions(molecules)
            all_possible_actions.extend(possible_actions)
            all_priors.extend(priors)
            if not self._config.additive_expansion and all_possible_actions:
                break
        return all_possible_actions, all_priors