Spaces:
Running
Running
File size: 17,257 Bytes
5a1721f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 | """Module containing commands line scripts for training and planning steps."""
import os
from pathlib import Path
import warnings
import click
import yaml
from synplan.chem.data.filtering import ReactionFilterConfig, filter_reactions_from_file
from synplan.chem.data.standardizing import (
ReactionStandardizationConfig,
standardize_reactions_from_file,
)
from synplan.chem.reaction_routes.clustering import run_cluster_cli
from synplan.chem.reaction_rules.extraction import extract_rules_from_reactions
from synplan.chem.utils import standardize_building_blocks
from synplan.mcts.search import run_search
from synplan.ml.training.reinforcement import run_updating
from synplan.ml.training.supervised import create_policy_dataset, run_policy_training
from synplan.utils.config import (
PolicyEvaluationConfig,
PolicyNetworkConfig,
RDKitEvaluationConfig,
RandomEvaluationConfig,
RolloutEvaluationConfig,
RuleExtractionConfig,
TreeConfig,
TuningConfig,
ValueNetworkConfig,
ValueNetworkEvaluationConfig,
)
from synplan.utils.loading import (
download_all_data,
load_building_blocks,
load_policy_function,
load_reaction_rules,
)
try:
from importlib.metadata import PackageNotFoundError, version as _dist_version
except Exception: # pragma: no cover
_dist_version = None # type: ignore[assignment]
PackageNotFoundError = Exception # type: ignore[assignment]
def _resolve_cli_version() -> str:
# Prefer installed distribution version
if _dist_version is not None:
try:
return _dist_version("SynPlanner")
except PackageNotFoundError:
pass
# Fallback to package attribute in editable/dev mode
try:
from synplan import __version__ as _pkg_version
return _pkg_version
except Exception:
return "0.0.0+unknown"
warnings.filterwarnings("ignore")
@click.group(name="synplan")
@click.version_option(version=_resolve_cli_version(), prog_name="synplan")
def synplan():
"""SynPlanner command line interface."""
@synplan.command(name="download_all_data")
@click.option(
"--save_to",
"save_to",
help="Path to the folder where downloaded data will be stored.",
)
def download_all_data_cli(save_to: str = ".") -> None:
"""Downloads all data for training, planning and benchmarking SynPlanner."""
download_all_data(save_to=save_to)
@synplan.command(name="building_blocks_standardizing")
@click.option(
"--input",
"input_file",
required=True,
type=click.Path(exists=True),
help="Path to the file with building blocks to be standardized.",
)
@click.option(
"--output",
"output_file",
required=True,
type=click.Path(),
help="Path to the file where standardized building blocks will be stored.",
)
def building_blocks_standardizing_cli(input_file: str, output_file: str) -> None:
"""Standardizes building blocks."""
standardize_building_blocks(input_file=input_file, output_file=output_file)
@synplan.command(name="reaction_standardizing")
@click.option(
"--config",
"config_path",
required=True,
type=click.Path(exists=True),
help="Path to the configuration file for reactions standardizing.",
)
@click.option(
"--input",
"input_file",
required=True,
type=click.Path(exists=True),
help="Path to the file with reactions to be standardized.",
)
@click.option(
"--output",
"output_file",
type=click.Path(),
help="Path to the file where standardized reactions will be stored.",
)
@click.option(
"--num_cpus", default=4, type=int, help="The number of CPUs to use for processing."
)
def reaction_standardizing_cli(
config_path: str, input_file: str, output_file: str, num_cpus: int
) -> None:
"""Standardizes reactions and remove duplicates."""
stand_config = ReactionStandardizationConfig.from_yaml(config_path)
standardize_reactions_from_file(
config=stand_config,
input_reaction_data_path=input_file,
standardized_reaction_data_path=output_file,
num_cpus=num_cpus,
batch_size=100,
)
@synplan.command(name="reaction_filtering")
@click.option(
"--config",
"config_path",
required=True,
type=click.Path(exists=True),
help="Path to the configuration file for reactions filtering.",
)
@click.option(
"--input",
"input_file",
required=True,
type=click.Path(exists=True),
help="Path to the file with reactions to be filtered.",
)
@click.option(
"--output",
"output_file",
default=Path("./"),
type=click.Path(),
help="Path to the file where successfully filtered reactions will be stored.",
)
@click.option(
"--num_cpus", default=4, type=int, help="The number of CPUs to use for processing."
)
def reaction_filtering_cli(
config_path: str, input_file: str, output_file: str, num_cpus: int
):
"""Filters erroneous reactions."""
reaction_check_config = ReactionFilterConfig().from_yaml(config_path)
filter_reactions_from_file(
config=reaction_check_config,
input_reaction_data_path=input_file,
filtered_reaction_data_path=output_file,
num_cpus=num_cpus,
batch_size=100,
)
@synplan.command(name="rule_extracting")
@click.option(
"--config",
"config_path",
required=True,
type=click.Path(exists=True),
help="Path to the configuration file for reaction rules extracting.",
)
@click.option(
"--input",
"input_file",
required=True,
type=click.Path(exists=True),
help="Path to the file with reactions for reaction rules extraction.",
)
@click.option(
"--output",
"output_file",
required=True,
type=click.Path(),
help="Path to the file where extracted reaction rules will be stored.",
)
@click.option(
"--num_cpus", default=4, type=int, help="The number of CPUs to use for processing."
)
def rule_extracting_cli(
config_path: str, input_file: str, output_file: str, num_cpus: int
):
"""Reaction rules extraction."""
reaction_rule_config = RuleExtractionConfig.from_yaml(config_path)
extract_rules_from_reactions(
config=reaction_rule_config,
reaction_data_path=input_file,
reaction_rules_path=output_file,
num_cpus=num_cpus,
batch_size=100,
)
@synplan.command(name="ranking_policy_training")
@click.option(
"--config",
"config_path",
required=True,
type=click.Path(exists=True),
help="Path to the configuration file for ranking policy training.",
)
@click.option(
"--reaction_data",
required=True,
type=click.Path(exists=True),
help="Path to the file with reactions for ranking policy training.",
)
@click.option(
"--reaction_rules",
required=True,
type=click.Path(exists=True),
help="Path to the file with extracted reaction rules.",
)
@click.option(
"--results_dir",
default=Path("."),
type=click.Path(),
help="Path to the directory where the trained policy network will be stored.",
)
@click.option(
"--num_cpus",
default=4,
type=int,
help="The number of CPUs to use for training set preparation.",
)
def ranking_policy_training_cli(
config_path: str,
reaction_data: str,
reaction_rules: str,
results_dir: str,
num_cpus: int,
) -> None:
"""Ranking policy network training."""
policy_config = PolicyNetworkConfig.from_yaml(config_path)
policy_config.policy_type = "ranking"
policy_dataset_file = os.path.join(results_dir, "policy_dataset.dt")
datamodule = create_policy_dataset(
reaction_rules_path=reaction_rules,
molecules_or_reactions_path=reaction_data,
output_path=policy_dataset_file,
dataset_type="ranking",
batch_size=policy_config.batch_size,
num_cpus=num_cpus,
)
run_policy_training(datamodule, config=policy_config, results_path=results_dir)
@synplan.command(name="filtering_policy_training")
@click.option(
"--config",
"config_path",
required=True,
type=click.Path(exists=True),
help="Path to the configuration file for filtering policy training.",
)
@click.option(
"--molecule_data",
required=True,
type=click.Path(exists=True),
help="Path to the file with molecules for filtering policy training.",
)
@click.option(
"--reaction_rules",
required=True,
type=click.Path(exists=True),
help="Path to the file with extracted reaction rules.",
)
@click.option(
"--results_dir",
default=Path("."),
type=click.Path(),
help="Path to the directory where the trained policy network will be stored.",
)
@click.option(
"--num_cpus",
default=8,
type=int,
help="The number of CPUs to use for training set preparation.",
)
def filtering_policy_training_cli(
config_path: str,
molecule_data: str,
reaction_rules: str,
results_dir: str,
num_cpus: int,
):
"""Filtering policy network training."""
policy_config = PolicyNetworkConfig.from_yaml(config_path)
policy_config.policy_type = "filtering"
policy_dataset_file = os.path.join(results_dir, "policy_dataset.ckpt")
datamodule = create_policy_dataset(
reaction_rules_path=reaction_rules,
molecules_or_reactions_path=molecule_data,
output_path=policy_dataset_file,
dataset_type="filtering",
batch_size=policy_config.batch_size,
num_cpus=num_cpus,
)
run_policy_training(datamodule, config=policy_config, results_path=results_dir)
@synplan.command(name="value_network_tuning")
@click.option(
"--config",
"config_path",
required=True,
type=click.Path(exists=True),
help="Path to the configuration file for value network training.",
)
@click.option(
"--targets",
required=True,
type=click.Path(exists=True),
help="Path to the file with target molecules for planning simulations.",
)
@click.option(
"--reaction_rules",
required=True,
type=click.Path(exists=True),
help="Path to the file with extracted reaction rules. Needed for planning simulations.",
)
@click.option(
"--building_blocks",
required=True,
type=click.Path(exists=True),
help="Path to the file with building blocks. Needed for planning simulations.",
)
@click.option(
"--policy_network",
required=True,
type=click.Path(exists=True),
help="Path to the file with trained policy network. Needed for planning simulations.",
)
@click.option(
"--value_network",
default=None,
type=click.Path(exists=True),
help="Path to the file with trained value network. Needed in case of additional value network fine-tuning",
)
@click.option(
"--results_dir",
default=".",
type=click.Path(exists=False),
help="Path to the directory where the trained value network will be stored.",
)
def value_network_tuning_cli(
config_path: str,
targets: str,
reaction_rules: str,
building_blocks: str,
policy_network: str,
value_network: str,
results_dir: str,
):
"""Value network tuning."""
with open(config_path, "r", encoding="utf-8") as file:
config = yaml.safe_load(file)
policy_config = PolicyNetworkConfig.from_dict(config["node_expansion"])
policy_config.weights_path = policy_network
value_config = ValueNetworkConfig.from_dict(config["value_network"])
if value_network is None:
value_config.weights_path = os.path.join(
results_dir, "weights", "value_network.ckpt"
)
tree_config = TreeConfig.from_dict(config["tree"])
tuning_config = TuningConfig.from_dict(config["tuning"])
run_updating(
targets_path=targets,
tree_config=tree_config,
policy_config=policy_config,
value_config=value_config,
reinforce_config=tuning_config,
reaction_rules_path=reaction_rules,
building_blocks_path=building_blocks,
results_root=results_dir,
)
@synplan.command(name="planning")
@click.option(
"--config",
"config_path",
required=True,
type=click.Path(exists=True),
help="Path to the configuration file for retrosynthetic planning.",
)
@click.option(
"--targets",
required=True,
type=click.Path(exists=True),
help="Path to the file with target molecules for retrosynthetic planning.",
)
@click.option(
"--reaction_rules",
required=True,
type=click.Path(exists=True),
help="Path to the file with extracted reaction rules.",
)
@click.option(
"--building_blocks",
required=True,
type=click.Path(exists=True),
help="Path to the file with building blocks.",
)
@click.option(
"--policy_network",
required=True,
type=click.Path(exists=True),
help="Path to the file with trained policy network.",
)
@click.option(
"--value_network",
default=None,
type=click.Path(exists=True),
help="Path to the file with trained value network.",
)
@click.option(
"--results_dir",
default=".",
type=click.Path(exists=False),
help="Path to the file where retrosynthetic planning results will be stored.",
)
def planning_cli(
config_path: str,
targets: str,
reaction_rules: str,
building_blocks: str,
policy_network: str,
value_network: str,
results_dir: str,
):
"""Retrosynthetic planning."""
with open(config_path, "r", encoding="utf-8") as file:
config = yaml.safe_load(file)
search_config = {**config["tree"], **config["node_evaluation"]}
policy_config = PolicyNetworkConfig.from_dict(
{**config["node_expansion"], **{"weights_path": policy_network}}
)
# Create evaluation config based on evaluation_type
node_evaluation = config.get("node_evaluation", {})
evaluation_type = node_evaluation.get("evaluation_type", "rollout")
if evaluation_type == "gcn":
# Value network evaluation
if value_network is None:
raise ValueError("value_network is required when evaluation_type is 'gcn'")
evaluation_config = ValueNetworkEvaluationConfig(
weights_path=value_network,
normalize=node_evaluation.get("normalize", False),
)
elif evaluation_type == "rollout":
# Rollout evaluation - need to load resources
policy_function = load_policy_function(weights_path=policy_network)
reaction_rules_list = load_reaction_rules(reaction_rules)
building_blocks_set = load_building_blocks(building_blocks, standardize=False)
evaluation_config = RolloutEvaluationConfig(
policy_network=policy_function,
reaction_rules=reaction_rules_list,
building_blocks=building_blocks_set,
min_mol_size=search_config.get("min_mol_size", 6),
max_depth=search_config.get("max_depth", 6),
normalize=node_evaluation.get("normalize", False),
)
elif evaluation_type == "random":
evaluation_config = RandomEvaluationConfig(
normalize=node_evaluation.get("normalize", False),
)
elif evaluation_type == "policy":
evaluation_config = PolicyEvaluationConfig(
normalize=node_evaluation.get("normalize", False),
)
elif evaluation_type == "rdkit":
evaluation_config = RDKitEvaluationConfig(
score_function=node_evaluation.get("score_function", "sascore"),
normalize=node_evaluation.get("normalize", False),
)
else:
raise ValueError(
f"Unknown evaluation_type: {evaluation_type}. "
f"Expected one of: 'gcn', 'rollout', 'random', 'policy', 'rdkit'"
)
run_search(
targets_path=targets,
search_config=search_config,
policy_config=policy_config,
evaluation_config=evaluation_config,
reaction_rules_path=reaction_rules,
building_blocks_path=building_blocks,
results_root=results_dir,
)
@synplan.command(name="clustering")
@click.option(
"--targets",
required=True,
type=click.Path(exists=True),
help="Path to the file with target molecules for retrosynthetic planning.",
)
@click.option(
"--routes_file",
default=".",
type=click.Path(exists=False),
help="Path to the file where the planning results are stored.",
)
@click.option(
"--cluster_results_dir",
default=".",
type=click.Path(exists=False),
help="Path to the file where clustering results will be stored.",
)
@click.option(
"--perform_subcluster",
default=None,
type=click.Path(exists=False),
help="Perform subclustering.",
)
@click.option(
"--subcluster_results_dir",
default=".",
type=click.Path(exists=False),
help="Path to the file where subclustering results will be stored.",
)
def cluster_route_from_file_cli(
targets: str,
routes_file: str,
cluster_results_dir: str,
perform_subcluster: bool,
subcluster_results_dir: str,
):
"""Clustering the routes from planning"""
run_cluster_cli(
routes_file=routes_file,
cluster_results_dir=cluster_results_dir,
perform_subcluster=perform_subcluster,
subcluster_results_dir=subcluster_results_dir if perform_subcluster else None,
)
if __name__ == "__main__":
synplan()
|