File size: 10,906 Bytes
64c992d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a8256e57",
   "metadata": {},
   "source": [
    "# SI Figure S15: per-solute RMSE with delta-22 composite coefficients applied\n",
    "\n",
    "Per-solute ¹H and ¹³C RMSE for the natural-products / olefin-isomer test set with delta-22\n",
    "coefficients applied (reproduces Figure 5C in ¹H, plus the ¹³C analogue), plus fitting-RMSE\n",
    "comparisons across coefficient choices, feature-space coverage by solvent, delta-22-plane residuals,\n",
    "and the RMSE distribution shift by solvent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64581ad1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, sys\n",
    "\n",
    "# make the in-repo modules importable (not pip-installed)\n",
    "REPO = os.path.abspath(\"../..\")\n",
    "for _p in (\"data/applications\", \"data/delta22\", \"analysis/code\", \"analysis/code/shared\"):\n",
    "    sys.path.insert(0, os.path.join(REPO, _p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "206af146",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from applications_reader import Applications\n",
    "import applications\n",
    "import applications_plots\n",
    "import delta22\n",
    "import paths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69c8b5ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "DATA = os.path.join(REPO, \"data\", \"applications\")\n",
    "DELTA22_HDF5 = paths.dataset_file(\"delta22\", root=REPO)\n",
    "DELTA22_XLSX = os.path.join(REPO, \"data\", \"delta22\", \"delta22_experimental.xlsx\")\n",
    "\n",
    "def figure_path(name):\n",
    "    os.makedirs(\"figures\", exist_ok=True)\n",
    "    return os.path.join(\"figures\", name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cde73dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "loader = Applications(paths.dataset_file(\"applications\", root=REPO),\n",
    "                      os.path.join(DATA, \"applications_experimental.xlsx\"))\n",
    "\n",
    "# assemble the feature table and run the composite-model fits + bootstrap\n",
    "query_df_nn = applications.build_query_df_nn(loader)\n",
    "site_counts = (query_df_nn.drop_duplicates(subset=[\"solute\", \"nucleus\", \"site\"])\n",
    "               .groupby([\"solute\", \"nucleus\"]).size().unstack(fill_value=0))\n",
    "\n",
    "per_solute = applications.per_solute_fits(query_df_nn)      # \"scaled to solute\" baseline\n",
    "all_solute = applications.per_solvent_fits(query_df_nn)     # \"scaled to test set\" full fit\n",
    "\n",
    "seed = applications.build_bootstrap_seed_coeffs(loader)\n",
    "bootstrap_rmses = {}\n",
    "for nuc in [\"H\", \"C\"]:\n",
    "    preds = applications.apply_bootstrap_params_to_full_dataset(query_df_nn, seed[nuc], nucleus=nuc)\n",
    "    bootstrap_rmses[nuc] = applications.compute_solute_rmses(preds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d218de52",
   "metadata": {},
   "outputs": [],
   "source": [
    "# published solute order (isomers, then the two pyridines, then the natural products); matches\n",
    "# main-text Figure 5D. delta-22 is drawn first by the box-plot engine, so it is not listed here.\n",
    "S15_SOLUTE_ORDER = [\"isomer_1E\", \"isomer_1Z\", \"isomer_2E\", \"isomer_2Z\", \"isomer_3E\", \"isomer_3Z\",\n",
    "                    \"isomer_4N\", \"isomer_4O\", \"vomicine\", \"prednisone\", \"peptide\", \"flavone\",\n",
    "                    \"dihydrotanshinone_I\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85097f2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1H panel (reproduces Figure 5C)\n",
    "applications_plots.plot_nps_on_boxplot_delta22_simplified(\n",
    "    loader.rmse_distribution(\"H\"), bootstrap_rmses[\"H\"], per_solute[\"H\"],\n",
    "    nucleus=\"H\", formulas=[\"stationary_plus_qcd + openMM\"], colors=[\"#61a89a\"],\n",
    "    site_counts=site_counts, formula_remap=applications.FORMULA_REMAP,\n",
    "    solute_remap=applications.SOLUTE_DISPLAY, solute_order=S15_SOLUTE_ORDER,\n",
    "    solute_color_remap=applications.PEPTIDE_HIGHLIGHT_H,\n",
    "    figsize=(14, 8), box_width=0.20, box_gap=0.1,\n",
    "    show_baseline=True, baseline_annotation_text=\"Scaled to solute\",\n",
    "    baseline_annotation_x=0.215, baseline_annotation_y=0.39,\n",
    "    show_full_fit_line=True, full_fit_line_label=\"Scaled to Test Set\", full_fit_line_label_x=0.835,\n",
    "    all_solute_fitting_results=all_solute,\n",
    "    max_bar_height=0.06, site_count_axis_mode=\"inset\", site_count_inset_area_frac=0.1,\n",
    "    site_count_inset_axis_offset=-0.06, site_count_inset_axis_label_pad=0.045,\n",
    "    save_path=figure_path(\"si_figure_s15_1H.png\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "973af7b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 13C panel\n",
    "applications_plots.plot_nps_on_boxplot_delta22_simplified(\n",
    "    loader.rmse_distribution(\"C\"), bootstrap_rmses[\"C\"], per_solute[\"C\"],\n",
    "    nucleus=\"C\", formulas=[\"stationary_plus_op_vib + openMM\"], colors=[\"#61a89a\"],\n",
    "    site_counts=site_counts, formula_remap=applications.FORMULA_REMAP,\n",
    "    solute_remap=applications.SOLUTE_DISPLAY, solute_order=S15_SOLUTE_ORDER,\n",
    "    solute_color_remap=applications.PEPTIDE_HIGHLIGHT_C,\n",
    "    figsize=(14, 8), box_width=0.20, box_gap=0.1,\n",
    "    show_baseline=True, baseline_annotation_text=\"Scaled to solute\",\n",
    "    baseline_annotation_x=0.28, baseline_annotation_y=0.355,\n",
    "    show_full_fit_line=True, full_fit_line_label=\"Scaled to Test Set\", full_fit_line_label_x=0.98,\n",
    "    all_solute_fitting_results=all_solute,\n",
    "    max_bar_height=0.8, site_count_axis_mode=\"inset\", site_count_inset_area_frac=0.1,\n",
    "    site_count_inset_axis_offset=-0.06, site_count_inset_axis_label_pad=0.045,\n",
    "    save_path=figure_path(\"si_figure_s15_13C.png\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f079c075",
   "metadata": {},
   "source": [
    "## Fitting RMSE Comparisons (chloroform)\n",
    "\n",
    "Per-solute RMSE under three coefficient choices (Scaled to Solute / Scaled to Test Set / Extrapolated\n",
    "from delta22)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11ea3200",
   "metadata": {},
   "outputs": [],
   "source": [
    "for nucleus, formula in [(\"H\", \"stationary_plus_qcd + openMM\"), (\"C\", \"stationary_plus_op_vib + openMM\")]:\n",
    "    table = applications.fitting_rmse_comparison_table(query_df_nn, per_solute, all_solute, bootstrap_rmses[nucleus],\n",
    "                                            nucleus, \"chloroform\", formula)\n",
    "    table = table.rename(index=applications.SOLUTE_DISPLAY)\n",
    "    order = [applications.SOLUTE_DISPLAY.get(k, k) for k in S15_SOLUTE_ORDER]   # published order; peptide has no chloroform data\n",
    "    table = table.reindex([n for n in order if n in table.index])\n",
    "    print(f\"--- {nucleus} ---\")\n",
    "    display(table.round(3))\n",
    "    applications_plots.plot_fitting_rmse_comparison_bars(\n",
    "        table, nucleus, \"chloroform\",\n",
    "        save_path=figure_path(f\"si_figure_s15_fitting_rmse_comparisons_{'1H' if nucleus == 'H' else '13C'}.png\"))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47773c94",
   "metadata": {},
   "source": [
    "## Feature Space Coverage by Solvent\n",
    "\n",
    "Test-set vs delta-22 mean-centered feature values across the four explicit-solvent solvents."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa1de7fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "delta22_query_df_nn = delta22.add_composite_columns(delta22.load_query_df_nn(\n",
    "    DELTA22_HDF5, DELTA22_XLSX, verbose=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a19b095b",
   "metadata": {},
   "outputs": [],
   "source": [
    "_FEATURE_X_LABELS = {\n",
    "    \"H\": \"Gas-Phase Shielding + QCD Correction (centered, ppm)\",\n",
    "    \"C\": \"Gas-Phase Shielding + OpenMM Vibrational Correction (centered, ppm)\",\n",
    "}\n",
    "for nucleus in [\"H\", \"C\"]:\n",
    "    coverage = applications.feature_space_coverage_table(query_df_nn, delta22_query_df_nn, nucleus)\n",
    "    applications_plots.plot_feature_space_coverage_grid(\n",
    "        coverage, nucleus, _FEATURE_X_LABELS[nucleus],\n",
    "        save_path=figure_path(f\"si_figure_s15_feature_space_coverage_{'1H' if nucleus == 'H' else '13C'}.png\"))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d7bc6b3",
   "metadata": {},
   "source": [
    "## Residuals for Delta22 Fitting Coefficients\n",
    "\n",
    "Residuals of a delta-22-only 2-feature plane applied to both delta-22 and the test set, vs\n",
    "experimental shielding."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8dea4de5",
   "metadata": {},
   "outputs": [],
   "source": [
    "for nucleus in [\"H\", \"C\"]:\n",
    "    residuals = applications.delta22_plane_residuals_table(query_df_nn, delta22_query_df_nn, nucleus)\n",
    "    applications_plots.plot_delta22_plane_residuals_grid(\n",
    "        residuals, nucleus,\n",
    "        save_path=figure_path(f\"si_figure_s15_delta22_plane_residuals_{'1H' if nucleus == 'H' else '13C'}.png\"))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fd43b43",
   "metadata": {},
   "source": [
    "## Distribution Shift by Solvent\n",
    "\n",
    "Per-solvent mean test-set RMSE under the three coefficient choices."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24d4b26e",
   "metadata": {},
   "outputs": [],
   "source": [
    "for nucleus, formula in [(\"H\", \"stationary_plus_qcd + openMM\"), (\"C\", \"stationary_plus_op_vib + openMM\")]:\n",
    "    shift = applications.distribution_shift_by_solvent_table(per_solute, all_solute, bootstrap_rmses[nucleus],\n",
    "                                                  nucleus, formula)\n",
    "    print(f\"--- {nucleus} ---\")\n",
    "    display(shift.round(3))\n",
    "    applications_plots.plot_distribution_shift_by_solvent_bars(\n",
    "        shift, nucleus,\n",
    "        save_path=figure_path(f\"si_figure_s15_distribution_shift_by_solvent_{'1H' if nucleus == 'H' else '13C'}.png\"))\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}