From 8721df868a45adef3552b38a8fb5e6f0ca720847 Mon Sep 17 00:00:00 2001 From: Lawson Woods Date: Tue, 3 Dec 2024 23:40:20 -0700 Subject: [PATCH] error handling --- activity/activity.ipynb | 136 +++++++++++++++++++++++++-------------- activity/solutions.ipynb | 74 ++++++++++++--------- 2 files changed, 133 insertions(+), 77 deletions(-) diff --git a/activity/activity.ipynb b/activity/activity.ipynb index c12ccee..d645ed3 100644 --- a/activity/activity.ipynb +++ b/activity/activity.ipynb @@ -67,14 +67,21 @@ "\n", "u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n", "\n", - "# Get an atom from the topology\n", - "# Universe -> AtomGroup -> Atom\n", - "atom = u.atoms[0]\n", - "print(\" time [ position ] [ velocity ] [ force ] [ box ]\")\n", - "\n", - "# Analysis loop\n", - "for ts in u.trajectory:\n", - " print(f'{ts.time:8.3f} {atom.position:8.3f} {atom.velocity:8.3f} {atom.force:8.3f} {u.dimensions[0:3]}')" + "try:\n", + " # Get an atom from the topology\n", + " # Universe -> AtomGroup -> Atom\n", + " atom = u.atoms[0]\n", + " print(\" time [ position ] [ velocity ] [ force ] [ box ]\")\n", + "\n", + " # Analysis loop\n", + "\n", + " for ts in u.trajectory:\n", + " print(f'{ts.time:8.3f} {atom.position} {atom.velocity} {atom.force} {u.dimensions[0:3]}')\n", + "\n", + "except Exception as e:\n", + " print(e)\n", + "finally:\n", + " u.trajectory.close()" ] }, { @@ -101,13 +108,21 @@ "\n", "u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n", "\n", - "# Universe -> AtomGroup -> Atom\n", - "nter = u.select_atoms(\"resid 1 and name CA\").atoms[0]\n", - "cter = u.select_atoms(\"resid 129 and name CA\").atoms[0]\n", + "try:\n", + "\n", + " # Universe -> AtomGroup -> Atom\n", + " nter = u.select_atoms(\"resid 1 and name CA\").atoms[0]\n", + " cter = u.select_atoms(\"resid 129 and name CA\").atoms[0]\n", + "\n", "\n", - "for ts in u.trajectory:\n", - " distance = np.sqrt(np.sum((nter.position - cter.position) ** 2))\n", - " print(f'At time {ts.time:4.3f} ps, the n-terminus and c-terminus are {distance:4.3f} Angstroms apart', end='\\r')" + " for ts in u.trajectory:\n", + " distance = np.sqrt(np.sum((nter.position - cter.position) ** 2))\n", + " print(f'At time {ts.time:4.3f} ps, the n-terminus and c-terminus are {distance:4.3f} Angstroms apart', end='\\r')\n", + "\n", + "except Exception as e:\n", + " print(e)\n", + "finally:\n", + " u.trajectory.close()" ] }, { @@ -132,22 +147,28 @@ "\n", "u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n", "\n", - "nter = u.select_atoms(\"resid 1 and name CA\").atoms[0]\n", - "cter = u.select_atoms(\"resid 129 and name CA\").atoms[0]\n", - "\n", - "graph = LiveTimeseriesGraph(\n", - " time_window=1.0, # ps\n", - " dt=0.010, # ps\n", - " title='Distance vs. Time',\n", - " y_label='Distance (Å)',\n", - " legend_label='Nter-Cter',\n", - ")\n", - " \n", - "for ts in u.trajectory:\n", - " distance = np.sqrt(np.sum((nter.position - cter.position) ** 2))\n", - " graph.update(ts.time, distance)\n", + "try:\n", "\n", - "\n" + " nter = u.select_atoms(\"resid 1 and name CA\").atoms[0]\n", + " cter = u.select_atoms(\"resid 129 and name CA\").atoms[0]\n", + "\n", + " graph = LiveTimeseriesGraph(\n", + " time_window=1.0, # ps\n", + " dt=0.010, # ps\n", + " title='Distance vs. Time',\n", + " y_label='Distance (Å)',\n", + " legend_label='Nter-Cter',\n", + " )\n", + "\n", + "\n", + " for ts in u.trajectory:\n", + " distance = np.sqrt(np.sum((nter.position - cter.position) ** 2))\n", + " graph.update(ts.time, distance)\n", + "\n", + "except Exception as e:\n", + " print(e)\n", + "finally:\n", + " u.trajectory.close()" ] }, { @@ -169,17 +190,25 @@ "import MDAnalysis as mda\n", "\n", "u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n", - "sel = u.select_atoms(\"not resname SOL\")\n", "\n", - "desired_length = 10\n", - "i = 0\n", - "with mda.Writer(\"sample_simulation/protein.trr\", sel.atoms.n_atoms) as w:\n", - " for ts in u.trajectory:\n", - " w.write(sel.atoms)\n", - " i += 1\n", - " \n", - " if i == desired_length:\n", - " break\n" + "try:\n", + "\n", + " sel = u.select_atoms(\"not resname SOL\")\n", + "\n", + " desired_length = 10\n", + " i = 0\n", + " with mda.Writer(\"sample_simulation/protein.trr\", sel.atoms.n_atoms) as w:\n", + " for ts in u.trajectory:\n", + " w.write(sel.atoms)\n", + " i += 1\n", + " \n", + " if i == desired_length:\n", + " break\n", + "\n", + "except Exception as e:\n", + " print(e)\n", + "finally:\n", + " u.trajectory.close()" ] }, { @@ -243,10 +272,16 @@ "\n", "u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n", "\n", - "## Graph setup (optional)\n", + "try:\n", + " ## Graph setup (optional)\n", "\n", - "for ts in u.trajectory:\n", - " ## Your analysis code here" + " for ts in u.trajectory:\n", + " ## Your analysis code here\n", + "\n", + "except Exception as e:\n", + " print(e)\n", + "finally:\n", + " u.trajectory.close()" ] }, { @@ -317,16 +352,23 @@ "\n", "u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n", "\n", - "## Graph setup (optional)\n", + "try:\n", + "\n", + " ## Graph setup (optional)\n", + "\n", + " for ts in u.trajectory:\n", + " ## Your analysis code here\n", "\n", - "for ts in u.trajectory:\n", - " ## Your analysis code here" + "except Exception as e:\n", + " print(e)\n", + "finally:\n", + " u.trajectory.close()" ] } ], "metadata": { "kernelspec": { - "display_name": "workshop", + "display_name": "imdclient-test", "language": "python", "name": "python3" }, @@ -340,7 +382,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.7" + "version": "3.12.0" } }, "nbformat": 4, diff --git a/activity/solutions.ipynb b/activity/solutions.ipynb index 5c7b41d..7c4935e 100644 --- a/activity/solutions.ipynb +++ b/activity/solutions.ipynb @@ -21,17 +21,24 @@ "\n", "u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n", "\n", - "graph = LiveTimeseriesGraph(\n", - " time_window=1.0, # ps\n", - " dt=0.010, # ps\n", - " title='Number of water molecules within 4 Angstroms of protein vs. Time',\n", - " y_label='Number of water molecules',\n", - ")\n", - "\n", - "for ts in u.trajectory:\n", - " # Select all water molecules that are within 4 angstroms of a non-water (protein) molecule\n", - " sel = u.select_atoms(\"(resname SOL) and (around 4 not resname SOL)\")\n", - " graph.update(ts.time, len(sel))" + "try:\n", + "\n", + " graph = LiveTimeseriesGraph(\n", + " time_window=1.0, # ps\n", + " dt=0.010, # ps\n", + " title='Number of water molecules within 4 Angstroms of protein vs. Time',\n", + " y_label='Number of water molecules',\n", + " )\n", + "\n", + " for ts in u.trajectory:\n", + " # Select all water molecules that are within 4 angstroms of a non-water (protein) molecule\n", + " sel = u.select_atoms(\"(resname SOL) and (around 4 not resname SOL)\")\n", + " graph.update(ts.time, len(sel))\n", + "\n", + "except Exception as e:\n", + " print(e)\n", + "finally:\n", + " u.trajectory.close()" ] }, { @@ -56,30 +63,37 @@ "\n", "u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n", "\n", - "graph = LiveTimeseriesGraph(\n", - " time_window=1.0, # ps\n", - " dt=0.010, # ps\n", - " title='Radius of gyration vs. Time',\n", - " y_label='Radius of gyration (Angstroms)',\n", - ")\n", + "try:\n", + "\n", + " graph = LiveTimeseriesGraph(\n", + " time_window=1.0, # ps\n", + " dt=0.010, # ps\n", + " title='Radius of gyration vs. Time',\n", + " y_label='Radius of gyration (Angstroms)',\n", + " )\n", + "\n", + " sel = u.select_atoms(\"protein\")\n", + " total_mass = np.sum(u.atoms.masses)\n", "\n", - "sel = u.select_atoms(\"protein\")\n", - "total_mass = np.sum(u.atoms.masses)\n", + " for ts in u.trajectory:\n", + " \n", + " center_of_mass = sel.center_of_mass()\n", + " pos = sel.positions\n", + " masses = sel.masses\n", "\n", - "for ts in u.trajectory:\n", - " \n", - " center_of_mass = sel.center_of_mass()\n", - " pos = sel.positions\n", - " masses = sel.masses\n", + " ri_sq = np.square((pos - center_of_mass))\n", + " ri_sq = np.sum(ri_sq, axis=1)\n", + " sq = np.sum(ri_sq * masses)\n", + " sq = sq / total_mass\n", "\n", - " ri_sq = np.square((pos - center_of_mass))\n", - " ri_sq = np.sum(ri_sq, axis=1)\n", - " sq = np.sum(ri_sq * masses)\n", - " sq = sq / total_mass\n", + " radius_of_gyration = np.sqrt(sq)\n", "\n", - " radius_of_gyration = np.sqrt(sq)\n", + " graph.update(ts.time, radius_of_gyration)\n", "\n", - " graph.update(ts.time, radius_of_gyration)" + "except Exception as e:\n", + " print(e)\n", + "finally:\n", + " u.trajectory.close()" ] } ],