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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Module containing configuration classes."""

from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, List, Union
from chython import smarts

import yaml
from CGRtools.containers import MoleculeContainer, QueryContainer


@dataclass
class ConfigABC(ABC):
    """Abstract base class for configuration classes."""

    @staticmethod
    @abstractmethod
    def from_dict(config_dict: Dict[str, Any]):
        """Create an instance of the configuration from a dictionary."""

    def to_dict(self) -> Dict[str, Any]:
        """Convert the configuration into a dictionary."""
        return {
            k: str(v) if isinstance(v, Path) else v for k, v in self.__dict__.items()
        }

    @staticmethod
    @abstractmethod
    def from_yaml(file_path: str):
        """Deserialize a YAML file into a configuration object."""

    def to_yaml(self, file_path: str):
        """Serializes the configuration to a YAML file.

        :param file_path: The path to the output YAML file.
        """
        with open(file_path, "w", encoding="utf-8") as file:
            yaml.dump(self.to_dict(), file)

    @abstractmethod
    def _validate_params(self, params: Dict[str, Any]):
        """Validate configuration parameters."""

    def __post_init__(self):
        """Validates the configuration parameters."""
        # call _validate_params method after initialization
        params = self.to_dict()
        self._validate_params(params)


@dataclass
class RuleExtractionConfig(ConfigABC):
    """Configuration class for extracting reaction rules.

    :param multicenter_rules: If True, extracts a single rule
        encompassing all centers. If False, extracts separate reaction
        rules for each reaction center in a multicenter reaction.
    :param as_query_container: If True, the extracted rules are
        generated as QueryContainer objects, analogous to SMARTS objects
        for pattern matching in chemical structures.
    :param reverse_rule: If True, reverses the direction of the reaction
        for rule extraction.
    :param reactor_validation: If True, validates each generated rule in
        a chemical reactor to ensure correct generation of products from
        reactants.
    :param include_func_groups: If True, includes specific functional
        groups in the reaction rule in addition to the reaction center
        and its environment.
    :param func_groups_list: A list of functional groups to be
        considered when include_func_groups is True.
    :param include_rings: If True, includes ring structures in the
        reaction rules.
    :param keep_leaving_groups: If True, retains leaving groups in the
        extracted reaction rule.
    :param keep_incoming_groups: If True, retains incoming groups in the
        extracted reaction rule.
    :param keep_reagents: If True, includes reagents in the extracted
        reaction rule.
    :param environment_atom_count: Defines the size of the environment
        around the reaction center to be included in the rule (0 for
        only the reaction center, 1 for the first environment, etc.).
    :param min_popularity: Minimum number of times a rule must be
        applied to be considered for further analysis.
    :param keep_metadata: If True, retains metadata associated with the
        reaction in the extracted rule.
    :param single_reactant_only: If True, includes only reaction rules
        with a single reactant molecule.
    :param atom_info_retention: Controls the amount of information about
        each atom to retain ('none', 'reaction_center', or 'all').
    """

    # default low-level parameters
    single_reactant_only: bool = True
    keep_metadata: bool = False
    reactor_validation: bool = True
    reverse_rule: bool = True
    as_query_container: bool = True
    include_func_groups: bool = False
    func_groups_list: List[str] = field(default_factory=list)

    # adjustable parameters
    environment_atom_count: int = 1
    min_popularity: int = 3
    include_rings: bool = True
    multicenter_rules: bool = True
    keep_leaving_groups: bool = True
    keep_incoming_groups: bool = True
    keep_reagents: bool = False
    atom_info_retention: Dict[str, Dict[str, bool]] = field(default_factory=dict)

    def __post_init__(self):
        super().__post_init__()
        self._validate_params(self.to_dict())
        self._initialize_default_atom_info_retention()
        self._parse_functional_groups()

    def _initialize_default_atom_info_retention(self):
        default_atom_info = {
            "reaction_center": {
                "neighbors": True,
                "hybridization": True,
                "implicit_hydrogens": False,
                "ring_sizes": False,
            },
            "environment": {
                "neighbors": False,
                "hybridization": False,
                "implicit_hydrogens": False,
                "ring_sizes": False,
            },
        }

        if not self.atom_info_retention:
            self.atom_info_retention = default_atom_info
        else:
            for key in default_atom_info:
                self.atom_info_retention[key].update(
                    self.atom_info_retention.get(key, {})
                )

    def _parse_functional_groups(self):
        func_groups_list = []
        for group_smarts in self.func_groups_list:
            try:
                query = smarts(group_smarts)
                func_groups_list.append(query)
            except Exception as e:
                print(f"Functional group {group_smarts} was not parsed because of {e}")
        self.func_groups_list = func_groups_list

    @staticmethod
    def from_dict(config_dict: Dict[str, Any]) -> "RuleExtractionConfig":
        return RuleExtractionConfig(**config_dict)

    @staticmethod
    def from_yaml(file_path: str) -> "RuleExtractionConfig":

        with open(file_path, "r", encoding="utf-8") as file:
            config_dict = yaml.safe_load(file)
        return RuleExtractionConfig.from_dict(config_dict)

    def _validate_params(self, params: Dict[str, Any]) -> None:

        if not isinstance(params["multicenter_rules"], bool):
            raise ValueError("multicenter_rules must be a boolean.")

        if not isinstance(params["as_query_container"], bool):
            raise ValueError("as_query_container must be a boolean.")

        if not isinstance(params["reverse_rule"], bool):
            raise ValueError("reverse_rule must be a boolean.")

        if not isinstance(params["reactor_validation"], bool):
            raise ValueError("reactor_validation must be a boolean.")

        if not isinstance(params["include_func_groups"], bool):
            raise ValueError("include_func_groups must be a boolean.")

        if params["func_groups_list"] is not None and not all(
            isinstance(group, str) for group in params["func_groups_list"]
        ):
            raise ValueError("func_groups_list must be a list of SMARTS.")

        if not isinstance(params["include_rings"], bool):
            raise ValueError("include_rings must be a boolean.")

        if not isinstance(params["keep_leaving_groups"], bool):
            raise ValueError("keep_leaving_groups must be a boolean.")

        if not isinstance(params["keep_incoming_groups"], bool):
            raise ValueError("keep_incoming_groups must be a boolean.")

        if not isinstance(params["keep_reagents"], bool):
            raise ValueError("keep_reagents must be a boolean.")

        if not isinstance(params["environment_atom_count"], int):
            raise ValueError("environment_atom_count must be an integer.")

        if not isinstance(params["min_popularity"], int):
            raise ValueError("min_popularity must be an integer.")

        if not isinstance(params["keep_metadata"], bool):
            raise ValueError("keep_metadata must be a boolean.")

        if not isinstance(params["single_reactant_only"], bool):
            raise ValueError("single_reactant_only must be a boolean.")

        if params["atom_info_retention"] is not None:
            if not isinstance(params["atom_info_retention"], dict):
                raise ValueError("atom_info_retention must be a dictionary.")

            required_keys = {"reaction_center", "environment"}
            if not required_keys.issubset(params["atom_info_retention"]):
                missing_keys = required_keys - set(params["atom_info_retention"].keys())
                raise ValueError(
                    f"atom_info_retention missing required keys: {missing_keys}"
                )

            for key, value in params["atom_info_retention"].items():
                if key not in required_keys:
                    raise ValueError(f"Unexpected key in atom_info_retention: {key}")

                expected_subkeys = {
                    "neighbors",
                    "hybridization",
                    "implicit_hydrogens",
                    "ring_sizes",
                }
                if not isinstance(value, dict) or not expected_subkeys.issubset(value):
                    missing_subkeys = expected_subkeys - set(value.keys())
                    raise ValueError(
                        f"Invalid structure for {key} in atom_info_retention. Missing subkeys: {missing_subkeys}"
                    )

                for subkey, subvalue in value.items():
                    if not isinstance(subvalue, bool):
                        raise ValueError(
                            f"Value for {subkey} in {key} of atom_info_retention must be boolean."
                        )


@dataclass
class PolicyNetworkConfig(ConfigABC):
    """Configuration class for the policy network.

    :param vector_dim: Dimension of the input vectors.
    :param batch_size: Number of samples per batch.
    :param dropout: Dropout rate for regularization.
    :param learning_rate: Learning rate for the optimizer.
    :param num_conv_layers: Number of convolutional layers in the network.
    :param num_epoch: Number of training epochs.
    :param policy_type: Mode of operation, either 'filtering' or 'ranking'.
    """

    policy_type: str = "ranking"
    vector_dim: int = 256
    batch_size: int = 500
    dropout: float = 0.4
    learning_rate: float = 0.008
    num_conv_layers: int = 5
    num_epoch: int = 100
    weights_path: str = None

    # for filtering policy
    priority_rules_fraction: float = 0.5
    rule_prob_threshold: float = 0.0
    top_rules: int = 50

    @staticmethod
    def from_dict(config_dict: Dict[str, Any]) -> "PolicyNetworkConfig":
        return PolicyNetworkConfig(**config_dict)

    @staticmethod
    def from_yaml(file_path: str) -> "PolicyNetworkConfig":
        with open(file_path, "r", encoding="utf-8") as file:
            config_dict = yaml.safe_load(file)
        return PolicyNetworkConfig.from_dict(config_dict)

    def _validate_params(self, params: Dict[str, Any]):

        if params["policy_type"] not in ["filtering", "ranking"]:
            raise ValueError("policy_type must be either 'filtering' or 'ranking'.")

        if not isinstance(params["vector_dim"], int) or params["vector_dim"] <= 0:
            raise ValueError("vector_dim must be a positive integer.")

        if not isinstance(params["batch_size"], int) or params["batch_size"] <= 0:
            raise ValueError("batch_size must be a positive integer.")

        if (
            not isinstance(params["num_conv_layers"], int)
            or params["num_conv_layers"] <= 0
        ):
            raise ValueError("num_conv_layers must be a positive integer.")

        if not isinstance(params["num_epoch"], int) or params["num_epoch"] <= 0:
            raise ValueError("num_epoch must be a positive integer.")

        if not isinstance(params["dropout"], float) or not (
            0.0 <= params["dropout"] <= 1.0
        ):
            raise ValueError("dropout must be a float between 0.0 and 1.0.")

        if (
            not isinstance(params["learning_rate"], float)
            or params["learning_rate"] <= 0.0
        ):
            raise ValueError("learning_rate must be a positive float.")

        if (
            not isinstance(params["priority_rules_fraction"], float)
            or params["priority_rules_fraction"] < 0.0
        ):
            raise ValueError(
                "priority_rules_fraction must be a non-negative positive float."
            )

        if (
            not isinstance(params["rule_prob_threshold"], float)
            or params["rule_prob_threshold"] < 0.0
        ):
            raise ValueError("rule_prob_threshold must be a non-negative float.")

        if not isinstance(params["top_rules"], int) or params["top_rules"] <= 0:
            raise ValueError("top_rules must be a positive integer.")


@dataclass
class ValueNetworkConfig(ConfigABC):
    """Configuration class for the value network.

    :param vector_dim: Dimension of the input vectors.
    :param batch_size: Number of samples per batch.
    :param dropout: Dropout rate for regularization.
    :param learning_rate: Learning rate for the optimizer.
    :param num_conv_layers: Number of convolutional layers in the network.
    :param num_epoch: Number of training epochs.
    """

    weights_path: str = None
    vector_dim: int = 256
    batch_size: int = 500
    dropout: float = 0.4
    learning_rate: float = 0.008
    num_conv_layers: int = 5
    num_epoch: int = 100

    @staticmethod
    def from_dict(config_dict: Dict[str, Any]) -> "ValueNetworkConfig":
        return ValueNetworkConfig(**config_dict)

    @staticmethod
    def from_yaml(file_path: str) -> "ValueNetworkConfig":
        with open(file_path, "r", encoding="utf-8") as file:
            config_dict = yaml.safe_load(file)
        return ValueNetworkConfig.from_dict(config_dict)

    def to_yaml(self, file_path: str):
        with open(file_path, "w", encoding="utf-8") as file:
            yaml.dump(self.to_dict(), file)

    def _validate_params(self, params: Dict[str, Any]):

        if not isinstance(params["vector_dim"], int) or params["vector_dim"] <= 0:
            raise ValueError("vector_dim must be a positive integer.")

        if not isinstance(params["batch_size"], int) or params["batch_size"] <= 0:
            raise ValueError("batch_size must be a positive integer.")

        if (
            not isinstance(params["num_conv_layers"], int)
            or params["num_conv_layers"] <= 0
        ):
            raise ValueError("num_conv_layers must be a positive integer.")

        if not isinstance(params["num_epoch"], int) or params["num_epoch"] <= 0:
            raise ValueError("num_epoch must be a positive integer.")

        if not isinstance(params["dropout"], float) or not (
            0.0 <= params["dropout"] <= 1.0
        ):
            raise ValueError("dropout must be a float between 0.0 and 1.0.")

        if (
            not isinstance(params["learning_rate"], float)
            or params["learning_rate"] <= 0.0
        ):
            raise ValueError("learning_rate must be a positive float.")


@dataclass
class TuningConfig(ConfigABC):
    """Configuration class for the network training.

    :param batch_size: The number of targets per batch in the planning simulation step.
    :param num_simulations: The number of planning simulations.
    """

    batch_size: int = 100
    num_simulations: int = 1

    @staticmethod
    def from_dict(config_dict: Dict[str, Any]) -> "TuningConfig":
        return TuningConfig(**config_dict)

    @staticmethod
    def from_yaml(file_path: str) -> "TuningConfig":
        with open(file_path, "r", encoding="utf-8") as file:
            config_dict = yaml.safe_load(file)
        return TuningConfig.from_dict(config_dict)

    def _validate_params(self, params: Dict[str, Any]):

        if not isinstance(params["batch_size"], int) or params["batch_size"] <= 0:
            raise ValueError("batch_size must be a positive integer.")


@dataclass
class TreeConfig(ConfigABC):
    """Configuration class for the tree search algorithm.

    :param max_iterations: The number of iterations to run the algorithm
        for.
    :param max_tree_size: The maximum number of nodes in the tree.
    :param max_time: The time limit (in seconds) for the algorithm to
        run.
    :param max_depth: The maximum depth of the tree.
    :param ucb_type: Type of UCB used in the search algorithm. Options
        are "puct", "uct", "value", defaults to "uct".
    :param c_ucb: The exploration-exploitation balance coefficient used
        in Upper Confidence Bound (UCB).
    :param backprop_type: Type of backpropagation algorithm. Options are
        "muzero", "cumulative", defaults to "muzero".
    :param search_strategy: The strategy used for tree search. Options
        are "expansion_first", "evaluation_first".
    :param exclude_small: Whether to exclude small molecules during the
        search.
    :param evaluation_agg: Method for aggregating evaluation scores.
        Options are "max", "average", defaults to "max".
    :param evaluation_type: The method used for evaluating nodes.
        Options are "random", "rollout", "gcn".
    :param init_node_value: Initial value for a new node.
    :param epsilon: A parameter in the epsilon-greedy search strategy
        representing the chance of random selection of reaction rules
        during the selection stage in Monte Carlo Tree Search,
        specifically during Upper Confidence Bound estimation. It
        balances between exploration and exploitation.
    :param min_mol_size: Defines the minimum size of a molecule that is
        have to be synthesized. Molecules with 6 or fewer heavy atoms
        are assumed to be building blocks by definition, thus setting
        the threshold for considering larger molecules in the search,
        defaults to 6.
    :param silent: Whether to suppress progress output.
    """

    max_iterations: int = 100
    max_tree_size: int = 1000000
    max_time: float = 600
    max_depth: int = 6
    ucb_type: str = "uct"
    c_ucb: float = 0.1
    backprop_type: str = "muzero"
    search_strategy: str = "expansion_first"
    exclude_small: bool = True
    evaluation_agg: str = "max"
    evaluation_type: str = "gcn"
    init_node_value: float = 0.0
    epsilon: float = 0.0
    min_mol_size: int = 6
    silent: bool = False

    @staticmethod
    def from_dict(config_dict: Dict[str, Any]) -> "TreeConfig":
        return TreeConfig(**config_dict)

    @staticmethod
    def from_yaml(file_path: str) -> "TreeConfig":
        with open(file_path, "r", encoding="utf-8") as file:
            config_dict = yaml.safe_load(file)
        return TreeConfig.from_dict(config_dict)

    def _validate_params(self, params):
        if params["ucb_type"] not in ["puct", "uct", "value"]:
            raise ValueError(
                "Invalid ucb_type. Allowed values are 'puct', 'uct', 'value'."
            )
        if params["backprop_type"] not in ["muzero", "cumulative"]:
            raise ValueError(
                "Invalid backprop_type. Allowed values are 'muzero', 'cumulative'."
            )
        if params["evaluation_type"] not in ["random", "rollout", "gcn"]:
            raise ValueError(
                "Invalid evaluation_type. Allowed values are 'random', 'rollout', 'gcn'."
            )
        if params["evaluation_agg"] not in ["max", "average"]:
            raise ValueError(
                "Invalid evaluation_agg. Allowed values are 'max', 'average'."
            )
        if not isinstance(params["c_ucb"], float):
            raise TypeError("c_ucb must be a float.")
        if not isinstance(params["max_depth"], int) or params["max_depth"] < 1:
            raise ValueError("max_depth must be a positive integer.")
        if not isinstance(params["max_tree_size"], int) or params["max_tree_size"] < 1:
            raise ValueError("max_tree_size must be a positive integer.")
        if (
            not isinstance(params["max_iterations"], int)
            or params["max_iterations"] < 1
        ):
            raise ValueError("max_iterations must be a positive integer.")
        if not isinstance(params["max_time"], int) or params["max_time"] < 1:
            raise ValueError("max_time must be a positive integer.")
        if not isinstance(params["exclude_small"], bool):
            raise TypeError("exclude_small must be a boolean.")
        if not isinstance(params["silent"], bool):
            raise TypeError("silent must be a boolean.")
        if not isinstance(params["init_node_value"], float):
            raise TypeError("init_node_value must be a float if provided.")
        if params["search_strategy"] not in ["expansion_first", "evaluation_first"]:
            raise ValueError(
                f"Invalid search_strategy: {params['search_strategy']}: "
                f"Allowed values are 'expansion_first', 'evaluation_first'"
            )
        if not isinstance(params["epsilon"], float) or 0 >= params["epsilon"] >= 1:
            raise ValueError("epsilon epsilon be a positive float between 0 and 1.")
        if not isinstance(params["min_mol_size"], int) or params["min_mol_size"] < 0:
            raise ValueError("min_mol_size must be a non-negative integer.")


def convert_config_to_dict(config_attr: ConfigABC, config_type) -> Dict | None:
    """Converts a configuration attribute to a dictionary if it's either a dictionary or
    an instance of a specified configuration type.

    :param config_attr: The configuration attribute to be converted.
    :param config_type: The type to check against for conversion.
    :return: The configuration attribute as a dictionary, or None if it's not an
        instance of the given type or dict.
    """
    if isinstance(config_attr, dict):
        return config_attr
    if isinstance(config_attr, config_type):
        return config_attr.to_dict()
    return None