lpnguyen commited on
Commit
671cdcb
1 Parent(s): a74749d

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +49 -28
tools.py CHANGED
@@ -4,7 +4,7 @@ import numpy as np
4
 
5
  def plot_3D_invfitness(trait, fitness, resident, range, color="RdBu"):
6
  X, Y = np.meshgrid(trait, trait)
7
- f_projection = (np.min(fitness) - 2) * np.ones(fitness.shape)
8
  axis = dict(
9
  showbackground=True,
10
  backgroundcolor="rgb(230, 230,230)",
@@ -15,15 +15,15 @@ def plot_3D_invfitness(trait, fitness, resident, range, color="RdBu"):
15
 
16
  layout = go.Layout(
17
  autosize=False,
18
- width=600,
19
- height=500,
20
  scene=dict(
21
  xaxis=dict(axis),
22
  yaxis=dict(axis),
23
  zaxis=dict(axis, range=range),
24
  aspectratio=dict(x=1, y=1, z=1),
25
- xaxis_title="Resident trait",
26
- yaxis_title="Mutant trait",
27
  zaxis_title="Invasion fitness",
28
  ),
29
  )
@@ -44,7 +44,7 @@ def plot_3D_invfitness(trait, fitness, resident, range, color="RdBu"):
44
  z=(fitness * 1e5),
45
  surfacecolor=x_projection,
46
  colorscale="Greys",
47
- opacity=0.5,
48
  showlegend=False,
49
  showscale=False,
50
  )
@@ -63,29 +63,28 @@ def plot_invasionfitness(zm, zlist, fitness_func, pars, range):
63
  inv_fitness = fitness_func(zm, zlist, pars)
64
 
65
  fig = px.line(
66
- x=zlist, y=inv_fitness, labels={
67
- "x": "Mutant trait value (z)", "y": "Invasion fitness"}
68
  )
69
  fig.add_vline(x=zm, line_dash="dashdot")
70
  fig.add_hline(y=0, line_dash="dash")
71
  fig.update_layout(
72
  title="Interactive invasion process",
73
  xaxis=dict(range=[0, zlist[-1]], autorange=False),
74
- yaxis=dict(range=range, autorange=False), autosize=False,
 
75
  width=450,
76
- height=400
77
  )
78
  return fig
79
 
80
 
81
- def make_interactive_video(z_start, z_end, step, zlist, fitness_func, pars, range):
82
  inv_vid = []
83
- for z_val in np.arange(z_start, z_end, step):
84
  inv_vid.append(fitness_func(z_val, zlist, pars))
85
  vid = go.Figure(
86
  data=[
87
- go.Line(x=zlist, y=fitness_func(
88
- z_start, zlist, pars), name="invasion fitness"),
89
  go.Line(
90
  x=zlist,
91
  y=np.zeros(len(zlist)),
@@ -101,21 +100,42 @@ def make_interactive_video(z_start, z_end, step, zlist, fitness_func, pars, rang
101
  ),
102
  ],
103
  layout=go.Layout(
104
- title="Invasion process video", autosize=False,
105
- width=550,
106
  height=500,
107
  xaxis=dict(range=[0, zlist[-1]], autorange=False),
108
  yaxis=dict(range=range, autorange=False),
109
- xaxis_title="Mutant trait value",
110
  updatemenus=[
111
- dict(type="buttons", buttons=[
112
- dict(label="Play", method="animate", args=[None, {"frame": {"duration": 500, "redraw": False},
113
- "fromcurrent": True, "transition": {"duration": 300,
114
- "easing": "quadratic-in-out"}}]),
115
- dict(label="Pause", method="animate", args=[[None], {"frame": {"duration": 0, "redraw": False},
116
- "mode": "immediate",
117
- "transition": {"duration": 0}}])]),
118
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  ],
120
  ),
121
  frames=[
@@ -135,7 +155,7 @@ def make_interactive_video(z_start, z_end, step, zlist, fitness_func, pars, rang
135
  ),
136
  ]
137
  )
138
- for i, z_val in zip(inv_vid, np.arange(z_start, z_end, step))
139
  ],
140
  )
141
  return vid
@@ -162,7 +182,8 @@ def plot_PIP(zlist, fitness_func, pars):
162
  autosize=False,
163
  width=400,
164
  height=500,
165
- xaxis_title="Resident trait",
166
- yaxis_title="Mutant trait",
 
167
  )
168
  return fig
 
4
 
5
  def plot_3D_invfitness(trait, fitness, resident, range, color="RdBu"):
6
  X, Y = np.meshgrid(trait, trait)
7
+ f_projection = (np.min(fitness) - np.mean(fitness)) * np.ones(fitness.shape)
8
  axis = dict(
9
  showbackground=True,
10
  backgroundcolor="rgb(230, 230,230)",
 
15
 
16
  layout = go.Layout(
17
  autosize=False,
18
+ width=700,
19
+ height=600,
20
  scene=dict(
21
  xaxis=dict(axis),
22
  yaxis=dict(axis),
23
  zaxis=dict(axis, range=range),
24
  aspectratio=dict(x=1, y=1, z=1),
25
+ xaxis_title="Resident trait (z_r)",
26
+ yaxis_title="Mutant trait (z_m)",
27
  zaxis_title="Invasion fitness",
28
  ),
29
  )
 
44
  z=(fitness * 1e5),
45
  surfacecolor=x_projection,
46
  colorscale="Greys",
47
+ opacity=0.7,
48
  showlegend=False,
49
  showscale=False,
50
  )
 
63
  inv_fitness = fitness_func(zm, zlist, pars)
64
 
65
  fig = px.line(
66
+ x=zlist, y=inv_fitness, labels={"x": "Mutant trait value (z_m)", "y": "Invasion fitness"}
 
67
  )
68
  fig.add_vline(x=zm, line_dash="dashdot")
69
  fig.add_hline(y=0, line_dash="dash")
70
  fig.update_layout(
71
  title="Interactive invasion process",
72
  xaxis=dict(range=[0, zlist[-1]], autorange=False),
73
+ yaxis=dict(range=range, autorange=False),
74
+ autosize=False,
75
  width=450,
76
+ height=400,
77
  )
78
  return fig
79
 
80
 
81
+ def make_interactive_video(z_start, z_end, steps, zlist, fitness_func, pars, range):
82
  inv_vid = []
83
+ for z_val in np.linspace(z_start, z_end, steps):
84
  inv_vid.append(fitness_func(z_val, zlist, pars))
85
  vid = go.Figure(
86
  data=[
87
+ go.Line(x=zlist, y=fitness_func(z_start, zlist, pars), name="invasion fitness"),
 
88
  go.Line(
89
  x=zlist,
90
  y=np.zeros(len(zlist)),
 
100
  ),
101
  ],
102
  layout=go.Layout(
103
+ autosize=False,
104
+ width=650,
105
  height=500,
106
  xaxis=dict(range=[0, zlist[-1]], autorange=False),
107
  yaxis=dict(range=range, autorange=False),
108
+ xaxis_title="Mutant trait value (z_m)",
109
  updatemenus=[
110
+ dict(
111
+ type="buttons",
112
+ buttons=[
113
+ dict(
114
+ label="Play",
115
+ method="animate",
116
+ args=[
117
+ None,
118
+ {
119
+ "frame": {"duration": 500, "redraw": False},
120
+ "fromcurrent": True,
121
+ "transition": {"duration": 300, "easing": "quadratic-in-out"},
122
+ },
123
+ ],
124
+ ),
125
+ dict(
126
+ label="Pause",
127
+ method="animate",
128
+ args=[
129
+ [None],
130
+ {
131
+ "frame": {"duration": 0, "redraw": False},
132
+ "mode": "immediate",
133
+ "transition": {"duration": 0},
134
+ },
135
+ ],
136
+ ),
137
+ ],
138
+ ),
139
  ],
140
  ),
141
  frames=[
 
155
  ),
156
  ]
157
  )
158
+ for i, z_val in zip(inv_vid, np.linspace(z_start, z_end, steps))
159
  ],
160
  )
161
  return vid
 
182
  autosize=False,
183
  width=400,
184
  height=500,
185
+ xaxis_title=r"Resident trait (z_r)",
186
+ yaxis_title=r"Mutant trait (z_m)",
187
+ title="Pairwise invasibility plot (PIP)",
188
  )
189
  return fig