Skip to content

Commit ea4676b

Browse files
committed
Apply black to notebook
1 parent 88bce0a commit ea4676b

1 file changed

Lines changed: 60 additions & 33 deletions

File tree

openmm_septop/septop_analysis.ipynb

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
"metadata": {},
4646
"outputs": [],
4747
"source": [
48-
"def _load_valid_result_json(fpath:os.PathLike|str)->tuple[tuple|None, dict|None]:\n",
48+
"def _load_valid_result_json(\n",
49+
" fpath: os.PathLike | str,\n",
50+
") -> tuple[tuple | None, dict | None]:\n",
4951
" \"\"\"Load the data from a results JSON into a dict.\n",
5052
"\n",
5153
" Parameters\n",
@@ -69,14 +71,16 @@
6971
" except (ValueError, IndexError):\n",
7072
" print(f\"{fpath}: Missing ligand names. Skipping.\")\n",
7173
" return None, None\n",
72-
" if result['estimate'] is None:\n",
74+
" if result[\"estimate\"] is None:\n",
7375
" errormsg = f\"{fpath}: No 'estimate' found, assuming to be a failed simulation.\"\n",
7476
" raise ValueError(errormsg)\n",
7577
" # return names, None\n",
76-
" if result['uncertainty'] is None:\n",
77-
" errormsg = f\"{fpath}: No 'uncertainty' found, assuming to be a failed simulation.\"\n",
78+
" if result[\"uncertainty\"] is None:\n",
79+
" errormsg = (\n",
80+
" f\"{fpath}: No 'uncertainty' found, assuming to be a failed simulation.\"\n",
81+
" )\n",
7882
" raise ValueError(errormsg)\n",
79-
" if all('exception' in u for u in result['unit_results'].values()):\n",
83+
" if all(\"exception\" in u for u in result[\"unit_results\"].values()):\n",
8084
" errormsg = f\"{fpath}: Exception found in all 'unit_results', assuming to be a failed simulation.\"\n",
8185
" raise ValueError(errormsg)\n",
8286
"\n",
@@ -91,7 +95,7 @@
9195
"outputs": [],
9296
"source": [
9397
"def _get_legs_from_result_jsons(\n",
94-
" result_fns: list[pathlib.Path]\n",
98+
" result_fns: list[pathlib.Path],\n",
9599
") -> dict[tuple[str, str], dict[str, list]]:\n",
96100
" \"\"\"\n",
97101
" Iterate over a list of result JSONs and populate a dict of dicts with all data needed\n",
@@ -119,24 +123,32 @@
119123
" if names is None: # this means it couldn't find names and/or simtype\n",
120124
" continue\n",
121125
"\n",
122-
" ddgs[names]['overall'].append([result[\"estimate\"], result[\"uncertainty\"]])\n",
126+
" ddgs[names][\"overall\"].append([result[\"estimate\"], result[\"uncertainty\"]])\n",
123127
" proto_key = [\n",
124-
" k\n",
125-
" for k in result[\"unit_results\"].keys()\n",
126-
" if k.startswith(\"ProtocolUnitResult\") \n",
127-
" ]\n",
128+
" k\n",
129+
" for k in result[\"unit_results\"].keys()\n",
130+
" if k.startswith(\"ProtocolUnitResult\")\n",
131+
" ]\n",
128132
" for p in proto_key:\n",
129133
" if \"unit_estimate\" in result[\"unit_results\"][p][\"outputs\"]:\n",
130134
" simtype = result[\"unit_results\"][p][\"outputs\"][\"simtype\"]\n",
131135
" dg = result[\"unit_results\"][p][\"outputs\"][\"unit_estimate\"]\n",
132136
" dg_error = result[\"unit_results\"][p][\"outputs\"][\"unit_estimate_error\"]\n",
133-
" \n",
137+
"\n",
134138
" ddgs[names][simtype].append([dg, dg_error])\n",
135139
" elif \"standard_state_correction_A\" in result[\"unit_results\"][p][\"outputs\"]:\n",
136-
" corr_A = result[\"unit_results\"][p][\"outputs\"][\"standard_state_correction_A\"]\n",
137-
" corr_B = result[\"unit_results\"][p][\"outputs\"][\"standard_state_correction_B\"]\n",
138-
" ddgs[names][\"standard_state_correction_A\"].append([corr_A, 0*unit.kilocalorie_per_mole])\n",
139-
" ddgs[names][\"standard_state_correction_B\"].append([corr_B, 0*unit.kilocalorie_per_mole])\n",
140+
" corr_A = result[\"unit_results\"][p][\"outputs\"][\n",
141+
" \"standard_state_correction_A\"\n",
142+
" ]\n",
143+
" corr_B = result[\"unit_results\"][p][\"outputs\"][\n",
144+
" \"standard_state_correction_B\"\n",
145+
" ]\n",
146+
" ddgs[names][\"standard_state_correction_A\"].append(\n",
147+
" [corr_A, 0 * unit.kilocalorie_per_mole]\n",
148+
" )\n",
149+
" ddgs[names][\"standard_state_correction_B\"].append(\n",
150+
" [corr_B, 0 * unit.kilocalorie_per_mole]\n",
151+
" )\n",
140152
" else:\n",
141153
" continue\n",
142154
"\n",
@@ -150,7 +162,7 @@
150162
"metadata": {},
151163
"outputs": [],
152164
"source": [
153-
"def _get_names(result:dict) -> tuple[str, str]:\n",
165+
"def _get_names(result: dict) -> tuple[str, str]:\n",
154166
" \"\"\"Get the ligand names from a unit's results data.\n",
155167
"\n",
156168
" Parameters\n",
@@ -164,14 +176,14 @@
164176
" Ligand names corresponding to the results.\n",
165177
" \"\"\"\n",
166178
" try:\n",
167-
" nm = list(result['unit_results'].values())[0]['name']\n",
179+
" nm = list(result[\"unit_results\"].values())[0][\"name\"]\n",
168180
"\n",
169181
" except KeyError:\n",
170182
" raise ValueError(\"Failed to guess names\")\n",
171183
"\n",
172184
" # TODO: make this more robust by pulling names from inputs.state[A/B].name\n",
173185
"\n",
174-
" toks = nm.split(',')\n",
186+
" toks = nm.split(\",\")\n",
175187
" toks = toks[1].split()\n",
176188
" return toks[1], toks[3]"
177189
]
@@ -183,7 +195,7 @@
183195
"metadata": {},
184196
"outputs": [],
185197
"source": [
186-
"def _generate_raw(legs:dict) -> None:\n",
198+
"def _generate_raw(legs: dict) -> None:\n",
187199
" \"\"\"\n",
188200
" Write out all legs found and their DG values, or indicate that they have failed.\n",
189201
"\n",
@@ -195,9 +207,11 @@
195207
" data = []\n",
196208
" for ligpair, results in sorted(legs.items()):\n",
197209
" for simtype, repeats in sorted(results.items()):\n",
198-
" if simtype != 'overall':\n",
210+
" if simtype != \"overall\":\n",
199211
" for repeat in repeats:\n",
200-
" m, u = format_estimate_uncertainty(repeat[0].m, repeat[1].m, unc_prec=2)\n",
212+
" m, u = format_estimate_uncertainty(\n",
213+
" repeat[0].m, repeat[1].m, unc_prec=2\n",
214+
" )\n",
201215
" data.append((simtype, ligpair[0], ligpair[1], m, u))\n",
202216
"\n",
203217
" df = pd.DataFrame(\n",
@@ -226,6 +240,7 @@
226240
" \"\"\"\n",
227241
" return np.std([v[0].m for v in r[\"overall\"]])\n",
228242
"\n",
243+
"\n",
229244
"def error_mbar(r):\n",
230245
" \"\"\"\n",
231246
" Calculate the error of the estimate using the reported MBAR errors.\n",
@@ -234,7 +249,7 @@
234249
" \"\"\"\n",
235250
" complex_errors = [x[1].m for x in r[\"complex\"]]\n",
236251
" solvent_errors = [x[1].m for x in r[\"solvent\"]]\n",
237-
" return math.sqrt(np.mean(complex_errors)**2 + np.mean(solvent_errors)**2)"
252+
" return math.sqrt(np.mean(complex_errors) ** 2 + np.mean(solvent_errors) ** 2)"
238253
]
239254
},
240255
{
@@ -244,7 +259,7 @@
244259
"metadata": {},
245260
"outputs": [],
246261
"source": [
247-
"def _generate_ddg(legs:dict) -> None:\n",
262+
"def _generate_ddg(legs: dict) -> None:\n",
248263
" \"\"\"Compute and write out DDG values for the given legs.\n",
249264
"\n",
250265
" Parameters\n",
@@ -254,15 +269,23 @@
254269
" \"\"\"\n",
255270
" data = []\n",
256271
" # check the type of error which should be used based on the number of repeats\n",
257-
" repeats = {len(v['overall']) for v in legs.values()}\n",
272+
" repeats = {len(v[\"overall\"]) for v in legs.values()}\n",
258273
" error_func = error_mbar if 1 in repeats else error_std\n",
259274
" for ligpair, results in sorted(legs.items()):\n",
260275
" ddg = np.mean([v[0].m for v in results[\"overall\"]])\n",
261276
" error = error_func(results)\n",
262277
" m, u = format_estimate_uncertainty(ddg, error, unc_prec=2)\n",
263278
" data.append((ligpair[0], ligpair[1], m, u))\n",
264279
"\n",
265-
" df = pd.DataFrame(data, columns=[\"ligand_i\", \"ligand_j\", \"DDG(i->j) (kcal/mol)\", \"uncertainty (kcal/mol)\"])\n",
280+
" df = pd.DataFrame(\n",
281+
" data,\n",
282+
" columns=[\n",
283+
" \"ligand_i\",\n",
284+
" \"ligand_j\",\n",
285+
" \"DDG(i->j) (kcal/mol)\",\n",
286+
" \"uncertainty (kcal/mol)\",\n",
287+
" ],\n",
288+
" )\n",
266289
" return df"
267290
]
268291
},
@@ -291,7 +314,7 @@
291314
" labelB=ligB,\n",
292315
" DG=DDGbind * unit.kilocalorie_per_mole,\n",
293316
" uncertainty=bind_unc * unit.kilocalorie_per_mole,\n",
294-
" computational=True\n",
317+
" computational=True,\n",
295318
" )\n",
296319
" fe_results.append(m)\n",
297320
"\n",
@@ -304,7 +327,7 @@
304327
" femap.generate_absolute_values()\n",
305328
" df = femap.get_absolute_dataframe()\n",
306329
" df = df.iloc[:, :3]\n",
307-
" df.rename({'label': 'ligand'}, axis='columns', inplace=True)\n",
330+
" df.rename({\"label\": \"ligand\"}, axis=\"columns\", inplace=True)\n",
308331
"\n",
309332
" return df"
310333
]
@@ -317,7 +340,7 @@
317340
"outputs": [],
318341
"source": [
319342
"def get_ddgs_dict(\n",
320-
" results: List[os.PathLike | str]\n",
343+
" results: List[os.PathLike | str],\n",
321344
") -> dict[tuple[str, str], dict[str, list]]:\n",
322345
" # find and filter result jsons\n",
323346
" result_fns = _collect_result_jsons(results)\n",
@@ -359,7 +382,11 @@
359382
],
360383
"source": [
361384
"# Specify paths to result directories\n",
362-
"results_dir = [pathlib.Path('results_0'), pathlib.Path('results_1'), pathlib.Path('results_2')]\n",
385+
"results_dir = [\n",
386+
" pathlib.Path(\"results_0\"),\n",
387+
" pathlib.Path(\"results_1\"),\n",
388+
" pathlib.Path(\"results_2\"),\n",
389+
"]\n",
363390
"ddgs = get_ddgs_dict(results_dir)"
364391
]
365392
},
@@ -381,7 +408,7 @@
381408
"outputs": [],
382409
"source": [
383410
"df_ddg = _generate_ddg(ddgs)\n",
384-
"df_ddg.to_csv('ddg.tsv', sep=\"\\t\", lineterminator=\"\\n\", index=False)"
411+
"df_ddg.to_csv(\"ddg.tsv\", sep=\"\\t\", lineterminator=\"\\n\", index=False)"
385412
]
386413
},
387414
{
@@ -485,7 +512,7 @@
485512
"outputs": [],
486513
"source": [
487514
"df_dg = _generate_dg_mle(ddgs)\n",
488-
"df_dg.to_csv('dg.tsv', sep=\"\\t\", lineterminator=\"\\n\", index=False)"
515+
"df_dg.to_csv(\"dg.tsv\", sep=\"\\t\", lineterminator=\"\\n\", index=False)"
489516
]
490517
},
491518
{
@@ -582,7 +609,7 @@
582609
"outputs": [],
583610
"source": [
584611
"df_raw = _generate_raw(ddgs)\n",
585-
"df_raw.to_csv('ddg_raw.tsv', sep=\"\\t\", lineterminator=\"\\n\", index=False)"
612+
"df_raw.to_csv(\"ddg_raw.tsv\", sep=\"\\t\", lineterminator=\"\\n\", index=False)"
586613
]
587614
},
588615
{

0 commit comments

Comments
 (0)