Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

conftest.py 4.3 KB

You have to be logged in to leave a comment. Sign In
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
  1. import pathlib
  2. from argparse import Namespace
  3. import numpy as np
  4. import omegaconf as oc
  5. import pandas as pd
  6. import pytest
  7. import molbart.utils.data_utils as util
  8. from molbart.models import Chemformer
  9. from molbart.data import SynthesisDataModule
  10. from molbart.utils.tokenizers import ChemformerTokenizer, SpanTokensMasker
  11. @pytest.fixture
  12. def example_tokens():
  13. return [
  14. ["^", "C", "(", "=", "O", ")", "unknown", "&"],
  15. ["^", "C", "C", "<SEP>", "C", "Br", "&"],
  16. ]
  17. @pytest.fixture
  18. def regex_tokens():
  19. regex = r"\[[^\]]+]|Br?|Cl?|N|O|S|P|F|I|b|c|n|o|s|p|\(|\)|\.|=|#|-|\+|\\\\|\/|:|~|@|\?|>|\*|\$|\%[0-9]{2}|[0-9]"
  20. return regex.split("|")
  21. @pytest.fixture
  22. def smiles_data():
  23. return ["CCO.Ccc", "CCClCCl", "C(=O)CBr"]
  24. @pytest.fixture
  25. def mock_random_choice(mocker):
  26. class ToggleBool:
  27. def __init__(self):
  28. self.state = True
  29. def __call__(self, *args, **kwargs):
  30. states = []
  31. for _ in range(kwargs["k"]):
  32. states.append(self.state)
  33. self.state = not self.state
  34. return states
  35. mocker.patch("molbart.utils.tokenizers.tokenizers.random.choices", side_effect=ToggleBool())
  36. @pytest.fixture
  37. def setup_tokenizer(regex_tokens, smiles_data):
  38. def wrapper(tokens=None):
  39. return ChemformerTokenizer(smiles=smiles_data, tokens=tokens, regex_token_patterns=regex_tokens)
  40. return wrapper
  41. @pytest.fixture
  42. def setup_masker(setup_tokenizer):
  43. def wrapper(cls=SpanTokensMasker):
  44. tokenizer = setup_tokenizer()
  45. return tokenizer, cls(tokenizer)
  46. return wrapper
  47. @pytest.fixture
  48. def round_trip_params(shared_datadir):
  49. params = {
  50. "n_samples": 3,
  51. "beam_size": 5,
  52. "batch_size": 3,
  53. "round_trip_input_data": shared_datadir / "round_trip_input_data.csv",
  54. }
  55. return params
  56. @pytest.fixture
  57. def round_trip_namespace_args(shared_datadir):
  58. args = Namespace()
  59. args.input_data = shared_datadir / "example_data_uspto.csv"
  60. args.backward_predictions = shared_datadir / "example_data_backward_sampled_smiles_uspto50k.json"
  61. args.output_score_data = "temp_metrics.csv"
  62. args.dataset_part = "test"
  63. args.working_directory = "tests"
  64. args.target_column = "products"
  65. return args
  66. @pytest.fixture
  67. def round_trip_raw_prediction_data(shared_datadir):
  68. round_trip_df = pd.read_json(shared_datadir / "round_trip_predictions_raw.json", orient="table")
  69. round_trip_predictions = [np.array(smiles_lst) for smiles_lst in round_trip_df["round_trip_smiles"].values]
  70. data = {
  71. "sampled_smiles": round_trip_predictions,
  72. "target_smiles": round_trip_df["target_smiles"].values,
  73. }
  74. return data
  75. @pytest.fixture
  76. def round_trip_converted_prediction_data(shared_datadir):
  77. round_trip_df = pd.read_json(shared_datadir / "round_trip_predictions_converted.json", orient="table")
  78. round_trip_predictions = [np.array(smiles_lst) for smiles_lst in round_trip_df["round_trip_smiles"].values]
  79. data = {
  80. "sampled_smiles": round_trip_predictions,
  81. "target_smiles": round_trip_df["target_smiles"].values,
  82. }
  83. return data
  84. @pytest.fixture
  85. def model_batch_setup(round_trip_namespace_args):
  86. config = oc.OmegaConf.load("molbart/config/round_trip_inference.yaml")
  87. data = pd.read_csv(round_trip_namespace_args.input_data, sep="\t")
  88. config.d_model = 4
  89. config.batch_size = 3
  90. config.n_beams = 3
  91. config.n_layers = 1
  92. config.n_heads = 2
  93. config.d_feedforward = 2
  94. config.task = "forward_prediction"
  95. config.datamodule = None
  96. config.vocabulary_path = "bart_vocab_downstream.json"
  97. config.n_gpus = 0
  98. config.device = "cpu"
  99. config.data_device = "cpu"
  100. chemformer = Chemformer(config)
  101. datamodule = SynthesisDataModule(
  102. reactants=data["reactants"].values,
  103. products=data["products"].values,
  104. dataset_path="",
  105. tokenizer=chemformer.tokenizer,
  106. batch_size=config.batch_size,
  107. max_seq_len=util.DEFAULT_MAX_SEQ_LEN,
  108. reverse=False,
  109. )
  110. datamodule.setup()
  111. dataloader = datamodule.full_dataloader()
  112. batch_idx, batch_input = next(enumerate(dataloader))
  113. output_data = {
  114. "chemformer": chemformer,
  115. "tokenizer": chemformer.tokenizer,
  116. "batch_idx": batch_idx,
  117. "batch_input": batch_input,
  118. "max_seq_len": util.DEFAULT_MAX_SEQ_LEN,
  119. }
  120. return output_data
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...