Skip to content

Commit b2c9ad9

Browse files
committed
define custom plot in model file for CrAu_POPC
1 parent 198a921 commit b2c9ad9

File tree

1 file changed

+106
-2
lines changed

1 file changed

+106
-2
lines changed

public/examples/CrAu_POPC/crau_popc.py

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
# === Constant definition section ===
1313
# Canvas
14-
DIMENSION = 400
14+
DIMENSION = 300
1515
STEPSIZE = 0.5
1616

1717
# Hermite Spline
@@ -187,4 +187,108 @@ def make_samples(func, substrate, contrasts, **kwargs):
187187

188188
#problem = FitProblem([model0, model0h, model, model_prot, modelh_prot])
189189
problem = FitProblem([model, modelh])
190-
#problem = FitProblem([model, modelh])
190+
#problem = FitProblem([model, modelh])
191+
192+
def custom_plot():
193+
194+
import plotly.graph_objs as go
195+
from refl1d.webview.server.colors import COLORS
196+
197+
moldat = problem.moldat
198+
199+
def hex_to_rgb(hex_string):
200+
r_hex = hex_string[1:3]
201+
g_hex = hex_string[3:5]
202+
b_hex = hex_string[5:7]
203+
return int(r_hex, 16), int(g_hex, 16), int(b_hex, 16)
204+
205+
n_lipids = 1
206+
group_names = {'gold substrate': ['bilayer.substrate'],
207+
#'silicon oxide': ['bilayer.siox'],
208+
'bME': ['bilayer.bME'],
209+
'tether': ['bilayer.tether_bme', 'bilayer.tether_free', 'bilayer.tether_hg'],
210+
'tether acyl chains': ['bilayer.tether_methylene', 'bilayer.tether_methyl'],
211+
'inner headgroups': [f'bilayer.headgroup1_{i}' for i in range(1, n_lipids + 1)],
212+
'inner acyl chains': [f'bilayer.methylene1_{i}' for i in range(1, n_lipids + 1)] + [f'bilayer.methyl1_{i}' for i in range(1, n_lipids + 1)],
213+
'outer acyl chains': [f'bilayer.methylene2_{i}' for i in range(1, n_lipids + 1)] + [f'bilayer.methyl2_{i}' for i in range(1, n_lipids + 1)],
214+
'outer headgroups': [f'bilayer.headgroup2_{i}' for i in range(1, n_lipids + 1)],
215+
}
216+
217+
normarea = moldat['bilayer.normarea']['area']
218+
219+
fig = go.Figure()
220+
traces = []
221+
MOD_COLORS = COLORS[1:]
222+
color_idx = 1
223+
sumarea = 0
224+
for lbl, item in group_names.items():
225+
area = 0
226+
for gp in item:
227+
if gp in moldat.keys():
228+
zaxis = moldat[gp]['zaxis']
229+
area += moldat[gp]['area']
230+
else:
231+
print(f'Warning: {gp} not found')
232+
233+
color = MOD_COLORS[color_idx % len(MOD_COLORS)]
234+
plotly_color = ','.join(map(str, hex_to_rgb(color)))
235+
traces.append(go.Scatter(x=zaxis,
236+
y=area / normarea,
237+
mode='lines',
238+
name=lbl,
239+
line=dict(color=color)))
240+
traces.append(go.Scatter(x=zaxis,
241+
y=area / normarea,
242+
mode='lines',
243+
line=dict(width=0),
244+
fill='tozeroy',
245+
fillcolor=f'rgba({plotly_color},0.3)',
246+
showlegend=False
247+
))
248+
color_idx += 1
249+
sumarea += area
250+
251+
color = COLORS[0]
252+
plotly_color = ','.join(map(str, hex_to_rgb(color)))
253+
254+
traces.append(go.Scatter(x=zaxis,
255+
y=sumarea / normarea,
256+
mode='lines',
257+
name='buffer',
258+
line=dict(color=color)))
259+
traces.append(go.Scatter(x=zaxis,
260+
y=sumarea / normarea,
261+
mode='lines',
262+
line=dict(width=0),
263+
fill='tonexty',
264+
fillcolor=f'rgba({plotly_color},0.3)',
265+
showlegend=False
266+
))
267+
traces.append(go.Scatter(x=zaxis,
268+
y=[1.0] * len(zaxis),
269+
mode='lines',
270+
line=dict(color=color, width=0),
271+
showlegend=False))
272+
273+
274+
fig.add_traces(traces[::-1])
275+
276+
fig.update_layout(
277+
title='Component Volume Occupancy',
278+
template = 'plotly_white',
279+
xaxis_title=dict(text='z (Ang)'),
280+
yaxis_title=dict(text='volume occupancy')
281+
)
282+
283+
return fig
284+
285+
setattr(problem, 'custom_plot', custom_plot)
286+
287+
if __name__ == '__main__':
288+
289+
problem.chisq_str()
290+
291+
moldat = problem.moldat
292+
293+
fig = custom_plot(moldat)
294+
fig.show()

0 commit comments

Comments
 (0)