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,225 @@
1
+ """Bounded, source-linked facts extracted from ROBERT prediction artifacts."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import csv
6
+ import math
7
+ import re
8
+ from dataclasses import dataclass
9
+ from pathlib import Path
10
+
11
+
12
+ @dataclass(frozen=True, slots=True)
13
+ class ResultInsight:
14
+ model: str
15
+ variant: str
16
+ category: str
17
+ summary: str
18
+ source: str
19
+
20
+
21
+ _SHAP_RE = re.compile(r"^\s*-\s+(.+?)\s+=\s+min:\s*([-+\deE.]+),\s*max:\s*([-+\deE.]+)\s*$")
22
+ _PFI_RE = re.compile(r"^\s*-\s+(.+?)\s+=\s+([-+\deE.]+)\s+\+-\s+([-+\deE.]+)\s*$")
23
+ _MAX_ROWS = 200_000
24
+ _AQME_SCAN_ROWS = 10_000
25
+ _MISSING_VALUES = {"", "nan", "none", "null", "n/a", "inf", "-inf"}
26
+
27
+
28
+ def extract_log_insights(text: str, model: str, source: str) -> tuple[ResultInsight, ...]:
29
+ """Read logged SHAP ranges and PFI scores without interpreting chemistry."""
30
+ variant = ""
31
+ category = ""
32
+ groups: dict[tuple[str, str], list[str]] = {}
33
+ pfi_metrics: dict[str, str] = {}
34
+ for line in text.splitlines():
35
+ folded = line.casefold()
36
+ if "starting model with all variables" in folded:
37
+ variant, category = "No_PFI", ""
38
+ elif "starting model with pfi" in folded:
39
+ variant, category = "PFI", ""
40
+ elif "shap plot saved" in folded:
41
+ category = "shap"
42
+ elif "pfi plot saved" in folded:
43
+ category = "pfi"
44
+ elif re.match(r"\s*o\s+", line):
45
+ category = ""
46
+ if not variant or not category:
47
+ continue
48
+ influence = re.match(r"^\s*Influence on\s+([A-Za-z0-9_-]+)\s*$", line, re.IGNORECASE)
49
+ if category == "pfi" and influence:
50
+ pfi_metrics[variant] = influence.group(1).upper()
51
+ pattern = _SHAP_RE if category == "shap" else _PFI_RE
52
+ match = pattern.match(line)
53
+ if not match:
54
+ continue
55
+ name, first, second = match.groups()
56
+ name = " ".join(name.split())[:100]
57
+ value = (
58
+ f"{name}: min={first}, max={second}"
59
+ if category == "shap" else f"{name}: {first} ± {second}"
60
+ )
61
+ groups.setdefault((variant, category), []).append(value)
62
+ insights = []
63
+ for (variant, category), values in groups.items():
64
+ prefix = (
65
+ "Reported SHAP value ranges (not an importance ranking)"
66
+ if category == "shap" else (
67
+ f"Permutation importance for {pfi_metrics[variant]} (mean ± SD, reported order)"
68
+ if variant in pfi_metrics else "Permutation importance (mean ± SD, reported order)"
69
+ )
70
+ )
71
+ summary = f"{prefix}: {'; '.join(values[:5])}"
72
+ if len(values) > 5:
73
+ summary += f"; {len(values) - 5} more descriptors in source"
74
+ insights.append(ResultInsight(model, variant, category, summary, source))
75
+ return tuple(insights)
76
+
77
+
78
+ def _finite_float(value: str | None) -> float | None:
79
+ try:
80
+ number = float(value)
81
+ except (TypeError, ValueError):
82
+ return None
83
+ return number if math.isfinite(number) else None
84
+
85
+
86
+ def _format_number(value: float) -> str:
87
+ return f"{value:.3g}"
88
+
89
+
90
+ def extract_prediction_insight(path: Path, model: str, variant: str, source: str) -> ResultInsight | None:
91
+ """Summarize regression residuals without treating prediction SD as an interval."""
92
+ try:
93
+ with path.open("r", encoding="utf-8-sig", errors="replace", newline="") as handle:
94
+ reader = csv.DictReader(handle)
95
+ fields = reader.fieldnames or []
96
+ prediction = next((name for name in fields if name.casefold().endswith("_pred")), None)
97
+ if prediction is None:
98
+ return None
99
+ observed = prediction[:-5]
100
+ observed_available = observed in fields
101
+ identifier = next((name for name in ("code_name", "point_id", "name", "id") if name in fields), None)
102
+ set_column = next((name for name in fields if name.casefold() == "set"), None)
103
+ total = evaluated = missing = 0
104
+ finite_predictions = 0
105
+ prediction_min = prediction_max = None
106
+ absolute_error_sum = 0.0
107
+ largest: list[tuple[float, int, str, float, float, str]] = []
108
+ set_totals: dict[str, tuple[int, float]] = {}
109
+ for row in reader:
110
+ total += 1
111
+ if total > _MAX_ROWS:
112
+ break
113
+ actual = _finite_float(row.get(observed))
114
+ predicted = _finite_float(row.get(prediction))
115
+ if predicted is not None:
116
+ finite_predictions += 1
117
+ prediction_min = predicted if prediction_min is None else min(prediction_min, predicted)
118
+ prediction_max = predicted if prediction_max is None else max(prediction_max, predicted)
119
+ if actual is None or predicted is None:
120
+ missing += 1
121
+ continue
122
+ error = abs(actual - predicted)
123
+ evaluated += 1
124
+ absolute_error_sum += error
125
+ label = " ".join(str(row.get(identifier) or total).split())[:80]
126
+ group = " ".join(str(row.get(set_column) or "").split())[:30] if set_column else ""
127
+ largest.append((error, total, label, actual, predicted, group))
128
+ largest.sort(key=lambda item: (-item[0], item[1]))
129
+ del largest[3:]
130
+ if group:
131
+ count, error_sum = set_totals.get(group, (0, 0.0))
132
+ set_totals[group] = (count + 1, error_sum + error)
133
+ except (OSError, csv.Error, UnicodeError):
134
+ return None
135
+ if total == 0:
136
+ return None
137
+ fragments = [f"{min(total, _MAX_ROWS)} rows, {evaluated} evaluated"]
138
+ if finite_predictions:
139
+ fragments.append(
140
+ f"{finite_predictions} finite predictions; predicted value range={_format_number(prediction_min)} "
141
+ f"to {_format_number(prediction_max)}"
142
+ )
143
+ if evaluated:
144
+ fragments.append(f"MAE={_format_number(absolute_error_sum / evaluated)} in target units")
145
+ for group, (count, error_sum) in sorted(set_totals.items()):
146
+ fragments.append(f"{group}: {count} evaluated, MAE={_format_number(error_sum / count)}")
147
+ top = "; ".join(
148
+ f"{label} ({group or 'unlabelled'}): observed={_format_number(actual)}, "
149
+ f"predicted={_format_number(predicted)}, absolute error={_format_number(error)}"
150
+ for error, _, label, actual, predicted, group in largest
151
+ )
152
+ fragments.append(f"largest observed errors: {top}")
153
+ if not observed_available:
154
+ fragments.append("observed target column absent; errors cannot be calculated")
155
+ elif missing:
156
+ fragments.append(f"{missing} rows without comparable observed/predicted values")
157
+ if total > _MAX_ROWS:
158
+ fragments.append(f"scan limited to first {_MAX_ROWS} rows")
159
+ return ResultInsight(model, variant, "prediction_errors", "; ".join(fragments), source)
160
+
161
+
162
+ def extract_aqme_insight(path: Path, input_columns: int | None, source: str) -> ResultInsight | None:
163
+ """Count missing AQME descriptor cells in a bounded scan of an output CSV."""
164
+ if input_columns is None:
165
+ return None
166
+ try:
167
+ with path.open("r", encoding="utf-8-sig", errors="replace", newline="") as handle:
168
+ reader = csv.reader(handle)
169
+ header = next(reader, [])
170
+ if len(header) <= input_columns:
171
+ return None
172
+ missing = [0] * (len(header) - input_columns)
173
+ rows = 0
174
+ for row in reader:
175
+ if rows >= _AQME_SCAN_ROWS:
176
+ break
177
+ rows += 1
178
+ for index in range(input_columns, len(header)):
179
+ value = row[index].strip().casefold() if index < len(row) else ""
180
+ if value in _MISSING_VALUES:
181
+ missing[index - input_columns] += 1
182
+ except (OSError, csv.Error, UnicodeError):
183
+ return None
184
+ affected = [(header[input_columns + index], count) for index, count in enumerate(missing) if count]
185
+ affected.sort(key=lambda item: (-item[1], item[0].casefold()))
186
+ summary = (
187
+ f"{rows} rows scanned; {len(missing)} descriptor columns; "
188
+ f"{len(affected)} columns with missing values"
189
+ )
190
+ if affected:
191
+ summary += "; most missing: " + "; ".join(
192
+ f"{' '.join(name.split())[:80]}: {count}/{rows} missing"
193
+ for name, count in affected[:5]
194
+ )
195
+ if rows >= _AQME_SCAN_ROWS:
196
+ summary += f"; scan limited to first {_AQME_SCAN_ROWS} rows"
197
+ return ResultInsight("", "", "aqme_completeness", summary, source)
198
+
199
+
200
+ def extract_dataset_overlap(main_path: Path, external_path: Path) -> ResultInsight | None:
201
+ """Check literal SMILES overlap between the selected main and external inputs."""
202
+ def read_smiles(path: Path) -> list[str] | None:
203
+ try:
204
+ with path.open("r", encoding="utf-8-sig", errors="replace", newline="") as handle:
205
+ reader = csv.DictReader(handle)
206
+ column = next((name for name in (reader.fieldnames or []) if name.casefold() == "smiles"), None)
207
+ if column is None:
208
+ return None
209
+ return [str(row.get(column) or "").strip() for _, row in zip(range(_MAX_ROWS), reader)]
210
+ except (OSError, csv.Error, UnicodeError):
211
+ return None
212
+
213
+ main_smiles = read_smiles(main_path)
214
+ external_smiles = read_smiles(external_path)
215
+ if main_smiles is None or not external_smiles:
216
+ return None
217
+ main_set = set(filter(None, main_smiles))
218
+ matches = [value for value in external_smiles if value and value in main_set]
219
+ summary = f"Exact SMILES overlap: {len(matches)}/{len(external_smiles)} external rows match the main input"
220
+ if matches:
221
+ summary += "; matching external rows are not independent by exact SMILES"
222
+ else:
223
+ summary += "; no exact string matches found"
224
+ summary += "; SMILES were not chemically canonicalized"
225
+ return ResultInsight("", "", "dataset_overlap", summary, f"{main_path.name}, {external_path.name}")
@@ -0,0 +1,75 @@
1
+ """Token accounting primitives shared by robBOT backends and the GUI."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+ import math
7
+
8
+ __all__ = ["MAX_USER_PROMPT_TOKENS", "TokenUsage", "estimate_token_count"]
9
+
10
+
11
+ MAX_USER_PROMPT_TOKENS = 500
12
+
13
+
14
+ def estimate_token_count(text: object) -> int:
15
+ """Return a conservative tokenizer-free estimate for user-facing limits."""
16
+ if type(text) is not str or not text:
17
+ return 0
18
+ ascii_count = sum(1 for character in text if ord(character) < 128)
19
+ non_ascii = [character for character in text if ord(character) >= 128]
20
+ non_ascii_bytes = sum(len(character.encode("utf-8")) for character in non_ascii)
21
+ return math.ceil(ascii_count / 4) + max(len(non_ascii), math.ceil(non_ascii_bytes / 3))
22
+
23
+
24
+ @dataclass(frozen=True, slots=True)
25
+ class TokenUsage:
26
+ prompt_tokens: int = 0
27
+ completion_tokens: int = 0
28
+ total_tokens: int = 0
29
+ cached_prompt_tokens: int = 0
30
+ estimated_cost_usd: float | None = 0.0
31
+ is_estimated: bool = False
32
+ provider: str = ""
33
+ model: str = ""
34
+ assessment_prompt_tokens: int = 0
35
+ assessment_completion_tokens: int = 0
36
+ search_content_tokens: int = 0
37
+ search_requests: int = 0
38
+ estimated_model_cost_usd: float | None = 0.0
39
+ estimated_search_cost_usd: float | None = 0.0
40
+
41
+ def __add__(self, other: object) -> "TokenUsage":
42
+ if not isinstance(other, TokenUsage):
43
+ return NotImplemented
44
+
45
+ def add_optional(first: float | None, second: float | None) -> float | None:
46
+ if first is None or second is None:
47
+ return None
48
+ return first + second
49
+
50
+ return TokenUsage(
51
+ prompt_tokens=self.prompt_tokens + other.prompt_tokens,
52
+ completion_tokens=self.completion_tokens + other.completion_tokens,
53
+ total_tokens=self.total_tokens + other.total_tokens,
54
+ cached_prompt_tokens=self.cached_prompt_tokens + other.cached_prompt_tokens,
55
+ assessment_prompt_tokens=(
56
+ self.assessment_prompt_tokens + other.assessment_prompt_tokens
57
+ ),
58
+ assessment_completion_tokens=(
59
+ self.assessment_completion_tokens + other.assessment_completion_tokens
60
+ ),
61
+ search_content_tokens=self.search_content_tokens + other.search_content_tokens,
62
+ search_requests=self.search_requests + other.search_requests,
63
+ estimated_model_cost_usd=add_optional(
64
+ self.estimated_model_cost_usd, other.estimated_model_cost_usd
65
+ ),
66
+ estimated_search_cost_usd=add_optional(
67
+ self.estimated_search_cost_usd, other.estimated_search_cost_usd
68
+ ),
69
+ estimated_cost_usd=add_optional(
70
+ self.estimated_cost_usd, other.estimated_cost_usd
71
+ ),
72
+ is_estimated=self.is_estimated or other.is_estimated,
73
+ provider=self.provider if self.provider == other.provider else "",
74
+ model=self.model if self.model == other.model else "",
75
+ )