Skip to content

Commit 295aa4f

Browse files
committed
Add toy plot helpers
1 parent 37dda93 commit 295aa4f

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

toy_plot_helpers.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import matplotlib.pylab as pylab
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
5+
from openpathsampling.engines.toy import Snapshot
6+
7+
8+
# A little class we use for visualizing these 2D PESs
9+
class CallablePES(object):
10+
def __init__(self, pes):
11+
self.pes = pes
12+
13+
def __call__(self, x, y):
14+
self.positions = [x, y]
15+
return self.pes.V(self)
16+
17+
class CallableVolume(object):
18+
def __init__(self, vol):
19+
self.vol = vol
20+
21+
def __call__(self, x, y):
22+
snapshot = Snapshot(coordinates=np.array([[x,y,0.0]]))
23+
return 1.0 if self.vol(snapshot) else 0.0
24+
25+
class ToyPlot(object):
26+
def __init__(self):
27+
range_x = np.arange(-1.1, 1.1, 0.01)
28+
range_y = np.arange(-1.1, 1.1, 0.01)
29+
self.extent = [range_x[0], range_x[-1], range_y[0], range_y[-1]]
30+
self.X, self.Y = np.meshgrid(range_x, range_y)
31+
pylab.rcParams['figure.figsize'] = 9, 6
32+
self.repcolordict = {0 : 'k-', 1 : 'r-', 2 : 'g-', 3 : 'b-',
33+
4 : 'r-'}
34+
35+
self.contour_range = np.arange(0.0, 1.5, 0.1)
36+
37+
self._states = None
38+
self._pes = None
39+
self._interfaces = None
40+
self._initcond = None
41+
42+
43+
def add_pes(self, pes):
44+
if self._pes is None:
45+
self._pes = np.vectorize(CallablePES(pes))(self.X, self.Y)
46+
47+
def add_states(self, states):
48+
if self._states is None:
49+
state = states[0]
50+
self._states = np.vectorize(CallableVolume(state))(self.X, -self.Y)
51+
for state in states[1:]:
52+
self._states += np.vectorize(CallableVolume(state))(self.X, -self.Y)
53+
54+
def add_interfaces(self, ifaces):
55+
if self._interfaces is None:
56+
self._interfaces = []
57+
for iface in ifaces:
58+
self._interfaces.append(
59+
np.vectorize(CallableVolume(iface))(self.X,self.Y)
60+
)
61+
62+
def add_initial_condition(self, initcond):
63+
self._initcond = initcond
64+
65+
def plot_pes_initcond(self, trajectories):
66+
fig, ax = plt.subplots()
67+
if self._pes is not None:
68+
plt.contour(self.X, self.Y, self._pes,
69+
levels=np.arange(0.0, 1.5, 0.1), colors='k')
70+
if self._initcond is not None:
71+
ax.plot(self._initcond.coordinates[0,0],
72+
self._initcond.coordinates[0,1],
73+
'ro', zorder=3)
74+
for traj in trajectories:
75+
plt.plot(traj.coordinates()[:,0,0], traj.coordinates()[:,0,1],
76+
self.repcolordict[trajectories.index(traj)],
77+
zorder=2)
78+
79+
def plot(self, trajectories=[], bold=[]):
80+
fig, ax = plt.subplots()
81+
if self._states is not None:
82+
plt.imshow(self._states, extent=self.extent, cmap="Blues",
83+
interpolation='nearest', vmin=0.0, vmax=2.0,
84+
aspect='auto')
85+
if self._pes is not None:
86+
plt.contour(self.X, self.Y, self._pes,
87+
levels=self.contour_range, colors='k')
88+
if self._interfaces is not None:
89+
for iface in self._interfaces:
90+
plt.contour(self.X, self.Y, iface,
91+
colors='r', interpolation='none', levels=[0.5])
92+
if self._initcond is not None:
93+
ax.plot(self._initcond.coordinates[0,0],
94+
self._initcond.coordinates[0,1],
95+
'ro', zorder=3)
96+
for traj in bold:
97+
plt.plot(traj.xyz[:,0,0], traj.xyz[:,0,1],
98+
self.repcolordict[bold.index(traj)], linewidth=2,
99+
zorder=1)
100+
for traj in trajectories:
101+
plt.plot(traj.xyz[:,0,0], traj.xyz[:,0,1],
102+
self.repcolordict[trajectories.index(traj) % 5],
103+
zorder=2)
104+
return fig
105+
106+
def reset(self):
107+
self._pes = None
108+
self._interfaces = None
109+
self._initcond = None
110+
self._states = None
111+
112+

0 commit comments

Comments
 (0)