robert 2.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. robert/__init__.py +9 -0
  2. robert/__main__.py +20 -0
  3. robert/_version.py +3 -0
  4. robert/api.py +646 -0
  5. robert/aqme.py +280 -0
  6. robert/argument_parser.py +98 -0
  7. robert/checkml.py +433 -0
  8. robert/checkml_score.py +50 -0
  9. robert/curate.py +217 -0
  10. robert/evaluate_bot_retrieval.py +125 -0
  11. robert/evaluate_bot_support.py +102 -0
  12. robert/evaluate_workflow_summaries.py +87 -0
  13. robert/export_bot_docs_knowledge.py +1632 -0
  14. robert/generate.py +249 -0
  15. robert/generate_utils.py +311 -0
  16. robert/gui_easyrob/__init__.py +0 -0
  17. robert/gui_easyrob/assets/gif_easyrob.gif +0 -0
  18. robert/gui_easyrob/bot/__init__.py +47 -0
  19. robert/gui_easyrob/bot/answer_metadata.py +198 -0
  20. robert/gui_easyrob/bot/bot_context.py +679 -0
  21. robert/gui_easyrob/bot/bot_engine.py +1100 -0
  22. robert/gui_easyrob/bot/bot_panel.py +1231 -0
  23. robert/gui_easyrob/bot/bot_rag.py +1178 -0
  24. robert/gui_easyrob/bot/bot_window.py +39 -0
  25. robert/gui_easyrob/bot/bot_worker.py +149 -0
  26. robert/gui_easyrob/bot/credential_store.py +143 -0
  27. robert/gui_easyrob/bot/error_parser.py +105 -0
  28. robert/gui_easyrob/bot/evidence_policy.py +164 -0
  29. robert/gui_easyrob/bot/heuristics.py +859 -0
  30. robert/gui_easyrob/bot/icons.py +56 -0
  31. robert/gui_easyrob/bot/input_csv_audit.py +212 -0
  32. robert/gui_easyrob/bot/knowledge/aqme_context.json +5907 -0
  33. robert/gui_easyrob/bot/knowledge/robert_context.json +4817 -0
  34. robert/gui_easyrob/bot/knowledge/ui_context.json +8293 -0
  35. robert/gui_easyrob/bot/knowledge_schema.py +1252 -0
  36. robert/gui_easyrob/bot/knowledge_sources/aqme_user_guidance.json +442 -0
  37. robert/gui_easyrob/bot/knowledge_sources/coverage_manifest.json +95 -0
  38. robert/gui_easyrob/bot/knowledge_sources/robert_user_guidance.json +471 -0
  39. robert/gui_easyrob/bot/knowledge_sources/ui_user_guidance.json +1312 -0
  40. robert/gui_easyrob/bot/llm_providers.py +1573 -0
  41. robert/gui_easyrob/bot/online_docs.py +105 -0
  42. robert/gui_easyrob/bot/prompt_policy.py +575 -0
  43. robert/gui_easyrob/bot/question_intent.py +562 -0
  44. robert/gui_easyrob/bot/response_quality.py +104 -0
  45. robert/gui_easyrob/bot/result_insights.py +225 -0
  46. robert/gui_easyrob/bot/token_usage.py +75 -0
  47. robert/gui_easyrob/bot/workflow_results.py +1396 -0
  48. robert/gui_easyrob/checkml_launcher.py +15 -0
  49. robert/gui_easyrob/easyrob.py +29 -0
  50. robert/gui_easyrob/easyrob_launcher.py +113 -0
  51. robert/gui_easyrob/main/__init__.py +9 -0
  52. robert/gui_easyrob/main/window.py +5152 -0
  53. robert/gui_easyrob/result_navigation.py +14 -0
  54. robert/gui_easyrob/tabs/__init__.py +23 -0
  55. robert/gui_easyrob/tabs/advanced_options.py +370 -0
  56. robert/gui_easyrob/tabs/aqme.py +1234 -0
  57. robert/gui_easyrob/tabs/checkml.py +898 -0
  58. robert/gui_easyrob/tabs/images.py +427 -0
  59. robert/gui_easyrob/tabs/interactive_predictions.py +555 -0
  60. robert/gui_easyrob/tabs/molssi.py +447 -0
  61. robert/gui_easyrob/tabs/predictions.py +484 -0
  62. robert/gui_easyrob/tabs/result_catalog.py +170 -0
  63. robert/gui_easyrob/tabs/results.py +490 -0
  64. robert/gui_easyrob/tabs/results_workspace.py +120 -0
  65. robert/gui_easyrob/test_bot_input_csv_audit.py +152 -0
  66. robert/gui_easyrob/test_bot_question_routing.py +383 -0
  67. robert/gui_easyrob/test_bot_result_analysis.py +356 -0
  68. robert/gui_easyrob/test_bot_retrieval_evaluation.py +110 -0
  69. robert/gui_easyrob/test_bot_ui_integration.py +278 -0
  70. robert/gui_easyrob/test_checkml_app_mode.py +124 -0
  71. robert/gui_easyrob/test_checkml_completion.py +57 -0
  72. robert/gui_easyrob/test_checkml_defaults.py +37 -0
  73. robert/gui_easyrob/test_checkml_result_summary.py +113 -0
  74. robert/gui_easyrob/test_checkml_web_mode.py +57 -0
  75. robert/gui_easyrob/test_dialog_layout.py +33 -0
  76. robert/gui_easyrob/test_gui_bot_regressions.py +320 -0
  77. robert/gui_easyrob/test_molssi_auto.py +569 -0
  78. robert/gui_easyrob/test_tutorial_view.py +215 -0
  79. robert/gui_easyrob/tutorials/check_model.md +25 -0
  80. robert/gui_easyrob/tutorials/chemdraw.md +49 -0
  81. robert/gui_easyrob/tutorials/csv.md +53 -0
  82. robert/gui_easyrob/tutorials/descriptors.md +21 -0
  83. robert/gui_easyrob/tutorials/overview.md +65 -0
  84. robert/gui_easyrob/tutorials/predictions.md +25 -0
  85. robert/gui_easyrob/tutorials/robbot.md +29 -0
  86. robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_1.png +0 -0
  87. robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_2.png +0 -0
  88. robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_3.png +0 -0
  89. robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_4.png +0 -0
  90. robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_5.png +0 -0
  91. robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_6.png +0 -0
  92. robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_7.png +0 -0
  93. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_1.png +0 -0
  94. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_10.png +0 -0
  95. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_11.png +0 -0
  96. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_12.png +0 -0
  97. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_13.png +0 -0
  98. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_2.png +0 -0
  99. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_3.png +0 -0
  100. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_4.png +0 -0
  101. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_5.png +0 -0
  102. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_6.png +0 -0
  103. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_7.png +0 -0
  104. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_8.png +0 -0
  105. robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_9.png +0 -0
  106. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_1.png +0 -0
  107. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_1_1.png +0 -0
  108. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_1_2.png +0 -0
  109. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_1_3.png +0 -0
  110. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_1_4.png +0 -0
  111. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_1_5.png +0 -0
  112. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_2.png +0 -0
  113. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_3.png +0 -0
  114. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_4.png +0 -0
  115. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_5.png +0 -0
  116. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_6.png +0 -0
  117. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_7.png +0 -0
  118. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_8.png +0 -0
  119. robert/gui_easyrob/tutorials/tutorial_images/csv/csv_9.png +0 -0
  120. robert/gui_easyrob/tutorials/tutorial_images/descriptors/descriptors_1.png +0 -0
  121. robert/gui_easyrob/tutorials/tutorial_images/descriptors/descriptors_2.png +0 -0
  122. robert/gui_easyrob/tutorials/tutorial_images/descriptors/descriptors_3.png +0 -0
  123. robert/gui_easyrob/tutorials/tutorial_images/descriptors/descriptors_4.png +0 -0
  124. robert/gui_easyrob/tutorials/tutorial_images/descriptors/descriptors_5.png +0 -0
  125. robert/gui_easyrob/tutorials/tutorial_images/descriptors/descriptors_6.png +0 -0
  126. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1.png +0 -0
  127. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_1.png +0 -0
  128. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_2.png +0 -0
  129. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_3.png +0 -0
  130. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_4.png +0 -0
  131. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_5.png +0 -0
  132. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_6.png +0 -0
  133. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_7.png +0 -0
  134. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_8.png +0 -0
  135. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_2.png +0 -0
  136. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_3.png +0 -0
  137. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_4.png +0 -0
  138. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_5.png +0 -0
  139. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_5_1.png +0 -0
  140. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_5_2.png +0 -0
  141. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_5_3.png +0 -0
  142. robert/gui_easyrob/tutorials/tutorial_images/overview/overview_6.png +0 -0
  143. robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_1.png +0 -0
  144. robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_1_1.png +0 -0
  145. robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_2.png +0 -0
  146. robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_3.png +0 -0
  147. robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_4.png +0 -0
  148. robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_5.png +0 -0
  149. robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_6.png +0 -0
  150. robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_1.png +0 -0
  151. robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_2.png +0 -0
  152. robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_3.png +0 -0
  153. robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_4.png +0 -0
  154. robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_5.png +0 -0
  155. robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_6.png +0 -0
  156. robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_7.png +0 -0
  157. robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_8.png +0 -0
  158. robert/gui_easyrob/utils/__init__.py +0 -0
  159. robert/gui_easyrob/utils/aqme_utils.py +238 -0
  160. robert/gui_easyrob/utils/molssi_utils.py +584 -0
  161. robert/gui_easyrob/utils/predictions_utils.py +985 -0
  162. robert/gui_easyrob/utils/utils_gui.py +738 -0
  163. robert/gui_easyrob/version.py +68 -0
  164. robert/gui_easyrob/web/app.css +484 -0
  165. robert/gui_easyrob/web/app.html +134 -0
  166. robert/gui_easyrob/web/app.js +764 -0
  167. robert/gui_easyrob/web/checkml_brand.png +0 -0
  168. robert/gui_easyrob/web/easyrob_background.gif +0 -0
  169. robert/gui_easyrob/web/easyrob_background.png +0 -0
  170. robert/gui_easyrob/web/images.css +89 -0
  171. robert/gui_easyrob/web/images.html +39 -0
  172. robert/gui_easyrob/web/images.js +101 -0
  173. robert/gui_easyrob/web/results.js +499 -0
  174. robert/gui_easyrob/web/robert_brand.png +0 -0
  175. robert/gui_easyrob/web_dialogs.py +64 -0
  176. robert/gui_easyrob/web_results.py +253 -0
  177. robert/gui_easyrob/web_shell.py +684 -0
  178. robert/gui_easyrob/workflow_progress.py +89 -0
  179. robert/gui_easyrob/workflow_run_lock.py +27 -0
  180. robert/icons/CheckML_icon.png +0 -0
  181. robert/icons/Robert_icon.png +0 -0
  182. robert/icons/Robert_logo.jpg +0 -0
  183. robert/icons/Robert_logo_transparent.png +0 -0
  184. robert/icons/documentation_icon.png +0 -0
  185. robert/icons/info_icon.png +0 -0
  186. robert/icons/pdf_icon.png +0 -0
  187. robert/icons/play_icon.png +0 -0
  188. robert/icons/stop_icon.png +0 -0
  189. robert/icons/youtube_icon.ico +0 -0
  190. robert/predict.py +193 -0
  191. robert/predict_utils.py +410 -0
  192. robert/report/CheckML_logo.jpg +0 -0
  193. robert/report/GENERATE.png +0 -0
  194. robert/report/Robert_icon.ico +0 -0
  195. robert/report/Robert_logo.jpg +0 -0
  196. robert/report/Robert_logo1.jpg +0 -0
  197. robert/report/abbrev.png +0 -0
  198. robert/report/adv_anal.png +0 -0
  199. robert/report/checkml_pass.svg +3 -0
  200. robert/report/checkml_unclear.svg +3 -0
  201. robert/report/checkml_warning.svg +3 -0
  202. robert/report/features.png +0 -0
  203. robert/report/outliers.png +0 -0
  204. robert/report/pred.png +0 -0
  205. robert/report/repro.png +0 -0
  206. robert/report/robust_x.png +0 -0
  207. robert/report/robust_y_high.png +0 -0
  208. robert/report/robust_y_low.png +0 -0
  209. robert/report/score.png +0 -0
  210. robert/report/score_0.jpg +0 -0
  211. robert/report/score_1.jpg +0 -0
  212. robert/report/score_10.jpg +0 -0
  213. robert/report/score_2.jpg +0 -0
  214. robert/report/score_3.jpg +0 -0
  215. robert/report/score_4.jpg +0 -0
  216. robert/report/score_5.jpg +0 -0
  217. robert/report/score_6.jpg +0 -0
  218. robert/report/score_7.jpg +0 -0
  219. robert/report/score_8.jpg +0 -0
  220. robert/report/score_9.jpg +0 -0
  221. robert/report/score_gray_0.jpg +0 -0
  222. robert/report/score_gray_1.jpg +0 -0
  223. robert/report/score_gray_10.jpg +0 -0
  224. robert/report/score_gray_2.jpg +0 -0
  225. robert/report/score_gray_3.jpg +0 -0
  226. robert/report/score_gray_4.jpg +0 -0
  227. robert/report/score_gray_5.jpg +0 -0
  228. robert/report/score_gray_6.jpg +0 -0
  229. robert/report/score_gray_7.jpg +0 -0
  230. robert/report/score_gray_8.jpg +0 -0
  231. robert/report/score_gray_9.jpg +0 -0
  232. robert/report/score_w_1_0.jpg +0 -0
  233. robert/report/score_w_1_1.jpg +0 -0
  234. robert/report/score_w_2_0.jpg +0 -0
  235. robert/report/score_w_2_1.jpg +0 -0
  236. robert/report/score_w_2_2.jpg +0 -0
  237. robert/report/score_w_2_gray_0.jpg +0 -0
  238. robert/report/score_w_2_gray_1.jpg +0 -0
  239. robert/report/score_w_2_gray_2.jpg +0 -0
  240. robert/report/score_w_3_0.jpg +0 -0
  241. robert/report/score_w_3_1.jpg +0 -0
  242. robert/report/score_w_3_2.jpg +0 -0
  243. robert/report/score_w_3_3.jpg +0 -0
  244. robert/report/score_w_4_0.jpg +0 -0
  245. robert/report/score_w_4_1.JPG +0 -0
  246. robert/report/score_w_4_2.jpg +0 -0
  247. robert/report/score_w_4_3.jpg +0 -0
  248. robert/report/score_w_4_4.jpg +0 -0
  249. robert/report/transpa.png +0 -0
  250. robert/report/y_distrib.png +0 -0
  251. robert/report.py +2880 -0
  252. robert/report_utils.py +2270 -0
  253. robert/robert.py +181 -0
  254. robert/utils.py +4763 -0
  255. robert/verify.py +606 -0
  256. robert-2.2.0.dist-info/METADATA +55 -0
  257. robert-2.2.0.dist-info/RECORD +261 -0
  258. robert-2.2.0.dist-info/WHEEL +5 -0
  259. robert-2.2.0.dist-info/entry_points.txt +3 -0
  260. robert-2.2.0.dist-info/licenses/LICENSE +21 -0
  261. robert-2.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,89 @@
1
+ """Track AQME preparation and the visible milestones of a ROBERT run."""
2
+
3
+ import re
4
+
5
+
6
+ STAGES = ("CURATE", "GENERATE", "VERIFY", "PREDICT", "REPORT")
7
+ _TIME_MARKER = re.compile(r"\bTime (CURATE|GENERATE|VERIFY|PREDICT):\s*\d", re.IGNORECASE)
8
+
9
+
10
+ class WorkflowProgress:
11
+ """Convert confirmed process output into stage states for the GUI."""
12
+
13
+ def __init__(self, workflow, include_aqme=False):
14
+ self.include_aqme = include_aqme or workflow == "AQME"
15
+ self.selected = () if workflow == "AQME" else STAGES if workflow == "Full Workflow" else (workflow,)
16
+ self.completed = set()
17
+ self.failed = False
18
+ self.stopped = False
19
+ self.verify_time_seen = False
20
+ self.predict_time_seen = False
21
+
22
+ def observe(self, line):
23
+ """Record a stage only when its completion marker appears in output."""
24
+ if self.include_aqme and (
25
+ re.search(r"\bTime AQME:\s*\d", line, re.IGNORECASE)
26
+ or "Starting data curation with the CURATE module" in line
27
+ ):
28
+ self.complete_aqme()
29
+ match = _TIME_MARKER.search(line)
30
+ if match and match.group(1).upper() in self.selected:
31
+ stage = match.group(1).upper()
32
+ if stage == "VERIFY":
33
+ self.verify_time_seen = True
34
+ elif stage == "PREDICT":
35
+ self.predict_time_seen = True
36
+ else:
37
+ self.completed.add(stage)
38
+ if self.selected == STAGES and "with the PREDICT module" in line:
39
+ self.completed.add("VERIFY")
40
+ if self.selected == STAGES and "Starting REPORT module" in line:
41
+ self.completed.add("PREDICT")
42
+
43
+ def complete_aqme(self):
44
+ """Confirm that descriptor generation finished before advancing."""
45
+ if self.include_aqme:
46
+ self.completed.add("AQME")
47
+
48
+ def finish(self, exit_code, report_created=False):
49
+ """Confirm report creation and mark an incomplete run as failed."""
50
+ self.stopped = exit_code == -1
51
+ if self.selected == ("VERIFY",) and exit_code == 0 and self.verify_time_seen:
52
+ self.completed.add("VERIFY")
53
+ if self.selected == ("PREDICT",) and exit_code == 0 and self.predict_time_seen:
54
+ self.completed.add("PREDICT")
55
+ if "REPORT" in self.selected and exit_code == 0 and report_created:
56
+ self.completed.add("REPORT")
57
+ self.failed = (
58
+ exit_code != 0
59
+ or (self.include_aqme and "AQME" not in self.completed)
60
+ or any(stage not in self.completed for stage in self.selected)
61
+ )
62
+
63
+ def aqme_status(self):
64
+ """Return the optional preparation stage independently of ROBERT."""
65
+ if not self.include_aqme:
66
+ return "inactive"
67
+ if "AQME" in self.completed:
68
+ return "done"
69
+ if self.stopped:
70
+ return "stopped"
71
+ return "failed" if self.failed else "active"
72
+
73
+ def statuses(self):
74
+ """Return stage states in display order."""
75
+ current = next((stage for stage in self.selected if stage not in self.completed), None)
76
+ waiting_for_aqme = self.include_aqme and "AQME" not in self.completed
77
+ states = []
78
+ for stage in STAGES:
79
+ if stage not in self.selected:
80
+ states.append("inactive")
81
+ elif stage in self.completed:
82
+ states.append("done")
83
+ elif waiting_for_aqme:
84
+ states.append("pending")
85
+ elif stage == current:
86
+ states.append("stopped" if self.stopped else "failed" if self.failed else "active")
87
+ else:
88
+ states.append("pending")
89
+ return tuple(states)
@@ -0,0 +1,27 @@
1
+ """Coordinate GUI workflow runs across EasyROB and CheckML processes."""
2
+
3
+ import tempfile
4
+ from pathlib import Path
5
+
6
+ from PySide6.QtCore import QLockFile
7
+
8
+
9
+ class WorkflowRunLock:
10
+ """Hold a single user-level lock for workflows that write ROBERT results."""
11
+
12
+ def __init__(self, path=None):
13
+ lock_path = Path(path) if path is not None else Path(tempfile.gettempdir()) / "robert_gui_workflow.lock"
14
+ self._lock = QLockFile(str(lock_path))
15
+ self._lock.setStaleLockTime(0)
16
+ self._held = False
17
+
18
+ def try_acquire(self):
19
+ if self._held:
20
+ return False
21
+ self._held = self._lock.tryLock(0)
22
+ return self._held
23
+
24
+ def release(self):
25
+ if self._held:
26
+ self._lock.unlock()
27
+ self._held = False
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
robert/predict.py ADDED
@@ -0,0 +1,193 @@
1
+ """
2
+ Parameters
3
+ ----------
4
+
5
+ destination : str, default=None,
6
+ Directory to create the output file(s).
7
+ varfile : str, default=None
8
+ Option to parse the variables using a yaml file (specify the filename, i.e. varfile=FILE.yaml).
9
+ params_dir : str, default=''
10
+ Folder containing the database and parameters of the ML model.
11
+ csv_test : str, default=''
12
+ Name of the CSV file containing the test set (if any). A path can be provided (i.e.
13
+ 'C:/Users/FOLDER/FILE.csv').
14
+ t_value : float, default=2
15
+ t-value that will be the threshold to identify outliers (check tables for t-values elsewhere).
16
+ The higher the t-value the more restrictive the analysis will be (i.e. there will be more
17
+ outliers with t-value=1 than with t-value = 4).
18
+ alpha : float, default=0.05
19
+ Significance level, or probability of making a wrong decision. This parameter is related to
20
+ the confidence intervals (i.e. 1-alpha is the confidence interval). By default, an alpha value
21
+ of 0.05 is used, which corresponds to a confidence interval of 95%.
22
+ shap_show : int, default=10,
23
+ Number of descriptors shown in the plot of the SHAP analysis.
24
+ pfi_show : int, default=10,
25
+ Number of descriptors shown in the plot of the PFI analysis.
26
+ pfi_epochs : int, default=5,
27
+ Sets the number of times a feature is randomly shuffled during the PFI analysis
28
+ (standard from sklearn webpage: 5).
29
+ names : str, default=''
30
+ Column of the names for each datapoint. Names are used to print outliers.
31
+
32
+ """
33
+ #####################################################.
34
+ # This file stores the PREDICT class #
35
+ # used to analyze and generate ML predictors #
36
+ #####################################################.
37
+
38
+ import os
39
+ import time
40
+ from robert.predict_utils import (plot_predictions,
41
+ save_predictions,
42
+ print_predict,
43
+ full_data_cv,
44
+ pearson_map_predict
45
+ )
46
+ from robert.utils import (load_variables,
47
+ load_db_n_params,
48
+ load_n_predict,
49
+ load_model,
50
+ _apply_full_refit_split_conformal,
51
+ finish_print,
52
+ print_pfi,
53
+ PFI_plot,
54
+ linear_equation,
55
+ shap_analysis,
56
+ outlier_plot,
57
+ distribution_plot,
58
+ boundary_plot,
59
+ applicability_domain_plot,
60
+ build_params_dirs,
61
+ Logger,
62
+ )
63
+
64
+ class predict:
65
+ """
66
+ Class containing all the functions from the PREDICT module.
67
+
68
+ Parameters
69
+ ----------
70
+ kwargs : argument class
71
+ Specify any arguments from the PREDICT module (for a complete list of variables, visit the ROBERT documentation)
72
+ """
73
+
74
+ def __init__(self, **kwargs):
75
+
76
+ start_time = time.time()
77
+
78
+ # load default and user-specified variables
79
+ self.args = load_variables(kwargs, "predict")
80
+
81
+ # if params_dir = '', the program performs the tests for the No_PFI and PFI folders
82
+ # (or, with --all_models, for every model staged under GENERATE/All_models)
83
+ params_dirs, suffixes, suffix_titles, model_names = build_params_dirs(self)
84
+
85
+ # --all_models: each model gets its own PREDICT_{model}_data.dat instead of merging
86
+ # every model's output into one shared log, where REPORT would have no reliable way
87
+ # to tell which lines belong to which model. The original shared log (already holds
88
+ # the run header from load_variables) is closed right away in that case.
89
+ default_log = self.args.log
90
+ if any(m is not None for m in model_names):
91
+ default_log.finalize()
92
+
93
+ # model_names repeats each model once per PFI variant (No_PFI then PFI) - only
94
+ # open a new Logger when the model actually changes, otherwise the PFI pass would
95
+ # re-open (and truncate, Logger uses mode 'w') the same file the No_PFI pass just
96
+ # wrote, silently discarding the No_PFI block
97
+ current_model = None
98
+ model_time_start = None
99
+ model_durations = {}
100
+ for (params_dir,suffix,suffix_title,model_name) in zip(params_dirs,suffixes,suffix_titles,model_names):
101
+ if os.path.exists(params_dir):
102
+
103
+ if model_name is not None and model_name != current_model:
104
+ if self.args.log is not default_log:
105
+ self.args.log.finalize()
106
+ if current_model is not None:
107
+ model_durations[current_model] = time.time() - model_time_start
108
+ self.args.log = Logger(self.args.destination / f'PREDICT_{model_name}', 'data')
109
+ current_model = model_name
110
+ model_time_start = time.time()
111
+
112
+ _ = print_pfi(self,params_dir)
113
+
114
+ # load the Xy databse and model parameters
115
+ Xy_data, model_data, suffix_title = load_db_n_params(self,params_dir,suffix,suffix_title,"verify",True) # module 'verify' since PREDICT follows similar protocols
116
+
117
+ # get results from training, test and external test (if any)
118
+ Xy_data = load_n_predict(self, model_data, Xy_data, BO_opt=False)
119
+ Xy_data = full_data_cv(self, Xy_data, model_data)
120
+ if getattr(self.args, "_api_predict", False):
121
+ loaded_model = load_model(self, model_data['model'], **model_data['params'])
122
+ Xy_data = _apply_full_refit_split_conformal(
123
+ self,
124
+ model_data,
125
+ Xy_data,
126
+ loaded_model,
127
+ y_cv_mean_train=Xy_data["y_pred_train"],
128
+ overwrite_predictions=True,
129
+ )
130
+
131
+ # save predictions for all sets
132
+ path_n_suffix, name_points, Xy_data = save_predictions(self,Xy_data,model_data,suffix_title)
133
+
134
+ # represent y vs predicted y
135
+ colors = plot_predictions(self,model_data,Xy_data,path_n_suffix)
136
+
137
+ # print results
138
+ _ = print_predict(self,Xy_data,model_data,suffix_title)
139
+
140
+ # boundary robustness plots (Low/High sorted CV + bias) for the ROBERT report -
141
+ # logged right after "Summary of results" so report_utils.get_predict_scores()
142
+ # can find them nearby
143
+ if model_data['type'].lower() == 'reg':
144
+ _ = boundary_plot(self,model_data,Xy_data,path_n_suffix)
145
+ _ = applicability_domain_plot(self,model_data,Xy_data,path_n_suffix)
146
+
147
+ # fit once and reuse for the linear equation/SHAP/PFI analyses below instead of
148
+ # each of them independently loading and refitting the same model on the same data
149
+ fitted_model = load_model(self, model_data['model'], **model_data['params'])
150
+ fitted_model.fit(Xy_data['X_train_scaled'], Xy_data['y_train'])
151
+
152
+ # linear model equation (MVL only) - must be logged before SHAP/PFI,
153
+ # REPORT's print_features() reads it back out of the log to show it above those plots
154
+ if model_data['model'].upper() == 'MVL':
155
+ _ = linear_equation(self,Xy_data,model_data,fitted_model)
156
+
157
+ # SHAP analysis
158
+ _ = shap_analysis(self,Xy_data,model_data,path_n_suffix,fitted_model)
159
+
160
+ # PFI analysis
161
+ _ = PFI_plot(self,Xy_data,model_data,path_n_suffix,fitted_model)
162
+
163
+ # create Pearson heatmap - unlike path_n_suffix-based outputs above, this
164
+ # builds its own filename directly from suffix_title, so the model name has
165
+ # to be included here explicitly (matches the same f"{model}_{suffix_title}"
166
+ # convention used everywhere else, e.g. save_predictions())
167
+ _ = pearson_map_predict(self,Xy_data,params_dir,f"{model_data['model']}_{suffix_title}")
168
+
169
+ # Outlier analysis
170
+ if model_data['type'].lower() == 'reg':
171
+ _ = outlier_plot(self,Xy_data,path_n_suffix,name_points,colors)
172
+
173
+ # y distribution
174
+ _ = distribution_plot(self,Xy_data,path_n_suffix,model_data)
175
+
176
+ # --all_models: each model gets its own "Time PREDICT" line reflecting only the time
177
+ # spent processing that model (not the cumulative time for every model), since each
178
+ # model's own PDF report should show its own PREDICT duration. finish_print() can't be
179
+ # reused here because it always measures from the original start_time (the whole
180
+ # multi-model run), and it also only writes into whichever log is CURRENTLY open (the
181
+ # last model processed) - every other model's log needs its line appended separately
182
+ # to its own (already-closed) file
183
+ if current_model is not None:
184
+ model_durations[current_model] = time.time() - model_time_start
185
+ for m, duration in model_durations.items():
186
+ if m == current_model:
187
+ continue
188
+ with open(self.args.destination / f'PREDICT_{m}_data.dat', 'a', encoding='utf-8') as f:
189
+ f.write(f"\nTime PREDICT: {round(duration, 2)} seconds\n")
190
+ self.args.log.write(f"\nTime PREDICT: {round(model_durations[current_model], 2)} seconds\n")
191
+ self.args.log.finalize()
192
+ else:
193
+ _ = finish_print(self,start_time,'PREDICT')