def setup_logger(console_level, file_level=None): """ Setup the logger that should be used by all classes The logger configuration is read from the `logging.yml` file. :param console_level: the level of logging to the console :type console_level: int :param file_level: the level of logging to file, if not set logging to file is disabled, default to None :type file_level: int, optional :return: the logger object :rtype: logging.Logger """ filename = os.path.join(data_path(), "logging.yml") with open(filename, "r") as fileobj: config = yaml.load(fileobj.read(), Loader=yaml.SafeLoader) config["handlers"]["console"]["level"] = console_level if file_level: config["handlers"]["file"]["level"] = file_level else: del config["handlers"]["file"] config["loggers"]["aizynthfinder"]["handlers"].remove("file") logging.config.dictConfig(config) return logger()
def __init__(self): self._properties = {} filename = os.path.join(data_path(), "config.yml") with open(filename, "r") as fileobj: _config = yaml.load(fileobj.read(), Loader=yaml.SafeLoader) self._update_from_config(_config) self.stock = Stock() self.policy = Policy(self) self._logger = logger()
def __post_init__(self) -> None: self._properties: StrDict = {} filename = os.path.join(data_path(), "config.yml") with open(filename, "r") as fileobj: _config = yaml.load(fileobj.read(), Loader=yaml.SafeLoader) self._update_from_config(_config) self.stock = Stock() self.expansion_policy = ExpansionPolicy(self) self.filter_policy = FilterPolicy(self) self.scorers = ScorerCollection(self) self._logger = logger()
def make_graphviz_image( molecules: Union[Sequence[Molecule], Sequence[UniqueMolecule]], reactions: Union[Sequence[RetroReaction], Sequence[FixedRetroReaction]], edges: Sequence[Tuple[Any, Any]], frame_colors: Sequence[PilColor], ) -> PilImage: """ Create an image of a bipartite graph of molecules and reactions using the dot program of graphviz :param molecules: the molecules nodes :param reactions: the reaction nodes :param edges: the edges of the graph :param frame_colors: the color of the frame around each image :raises FileNotFoundError: if the image could not be produced :return: the create image """ def _create_image(use_splines): txt = template.render( molecules=mol_spec, reactions=reactions, edges=edges, use_splines=use_splines, ) _, input_name = tempfile.mkstemp(suffix=".dot") with open(input_name, "w") as this_fileobj: this_fileobj.write(txt) _, output_img2 = tempfile.mkstemp(suffix=".png") ext = ".bat" if sys.platform.startswith("win") else "" subprocess.call( [f"dot{ext}", "-T", "png", f"-o{output_img2}", input_name]) if not os.path.exists(output_img2) or os.path.getsize( output_img2) == 0: raise FileNotFoundError( "Could not produce graph with layout - check that 'dot' command is in path" ) return output_img2 mol_spec = save_molecule_images(molecules, frame_colors) template_filepath = os.path.join(data_path(), "templates", "reaction_tree.dot") with open(template_filepath, "r") as fileobj: template = Template(fileobj.read()) template.globals["id"] = id # type: ignore try: output_img = _create_image(use_splines=True) except FileNotFoundError: output_img = _create_image(use_splines=False) return Image.open(output_img)
def make_visjs_page(filename, molecules, reactions, edges, frame_colors, hierarchical=False): """ Create HTML code of a bipartite graph of molecules and reactions using the vis.js network library. Package the created HTML page and all images as tar-ball. :param filename: the basename of the archive :type filename: str :param molecules: the molecules nodes :type molecules: list of Molecules :param reactions: the reaction nodes :type reactions: list of Reactions :param edges: the edges of the graph :type edges: list of tuples :param frame_colors: the color of the frame around each image :type frame_colors: list of str :param hierarchical: if True, will produce a hierarchical layout :type hierarchical: bool, optional """ mol_spec = save_molecule_images(molecules, frame_colors) template_filepath = os.path.join(data_path(), "templates", "reaction_tree.thtml") with open(template_filepath, "r") as fileobj: template = Template(fileobj.read()) template.globals["id"] = id tmpdir = tempfile.mkdtemp() for image_filepath in mol_spec.values(): shutil.copy(image_filepath, tmpdir) mol_spec = { molecule: os.path.basename(path) for molecule, path in mol_spec.items() } input_name = os.path.join(tmpdir, "route.html") with open(input_name, "w") as fileobj: fileobj.write( template.render( molecules=mol_spec, reactions=reactions, edges=edges, hierarchical=hierarchical, )) basename, _ = os.path.splitext(filename) shutil.make_archive(basename, "tar", root_dir=tmpdir)
def __init__(self, config_filename=None): filename = os.path.join(data_path(), "default_training.yml") with open(filename, "r") as fileobj: default_config = yaml.load(fileobj.read(), Loader=yaml.SafeLoader) self._config = default_config if config_filename is None: return with open(config_filename, "r") as fileobj: user_config = yaml.load(fileobj.read(), Loader=yaml.SafeLoader) self._update_dict(default_config, user_config)
def make_visjs_page( filename: str, molecules: Sequence[Molecule], reactions: Sequence[FixedRetroReaction], edges: Union[Sequence[Tuple[Any, Any]], nx.digraph.OutEdgeView], frame_colors: Sequence[PilColor], hierarchical: bool = False, ) -> None: """ Create HTML code of a bipartite graph of molecules and reactions using the vis.js network library. Package the created HTML page and all images as tar-ball. :param filename: the basename of the archive :param molecules: the molecules nodes :param reactions: the reaction nodes :param edges: the edges of the graph :param frame_colors: the color of the frame around each image :param hierarchical: if True, will produce a hierarchical layout """ mol_spec = save_molecule_images(molecules, frame_colors) template_filepath = os.path.join(data_path(), "templates", "reaction_tree.thtml") with open(template_filepath, "r") as fileobj: template = Template(fileobj.read()) template.globals["id"] = id # type: ignore tmpdir = tempfile.mkdtemp() for image_filepath in mol_spec.values(): shutil.copy(image_filepath, tmpdir) mol_spec = { molecule: os.path.basename(path) for molecule, path in mol_spec.items() } input_name = os.path.join(tmpdir, "route.html") with open(input_name, "w") as fileobj: fileobj.write( template.render( molecules=mol_spec, reactions=reactions, edges=edges, hierarchical=hierarchical, )) basename, _ = os.path.splitext(filename) shutil.make_archive(basename, "tar", root_dir=tmpdir)