Skip to content

Commit

Permalink
Merge pull request #202 from StochSS/result-plot-cleanup
Browse files Browse the repository at this point in the history
Cleaned up result plotting functions
  • Loading branch information
seanebum authored Nov 29, 2021
2 parents ffda632 + aea0371 commit 0036155
Showing 1 changed file with 106 additions and 170 deletions.
276 changes: 106 additions & 170 deletions spatialpy/Result.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,102 @@ def __del__(self):
except Exception as e:
pass

def __configure_buttons(self, f_duration, t_duration):
play_btn = {"args": [None, {"frame": {"duration": f_duration, "redraw": False},
"fromcurrent": True,
"transition": {"duration": t_duration, "easing": "quadratic-in-out"}}],
"label": "Play",
"method": "animate"
}
pause_btn = {"args": [[None], {"frame": {"duration": 0, "redraw": False},
"mode": "immediate",
"transition": {"duration": 0}}],
"label": "Pause",
"method": "animate"
}
return [play_btn, pause_btn]

def __setup_updatemenues(self, f_duration, t_duration):
menues = {"buttons": self.__configure_buttons(f_duration, t_duration),
"direction": "left",
"pad": {"r": 10, "t": 87},
"showactive": False,
"type": "buttons",
"x": 0.1,
"xanchor": "right",
"y": 0,
"yanchor": "top"
}
return menues

def __setup_sliders(self, t_duration):
sliders = {"active": 0,
"yanchor": "top",
"xanchor": "left",
"currentvalue": {
"font": {"size": 20},
"prefix": "Time:",
"visible": True,
"xanchor": "right"
},
"transition": {"duration": t_duration, "easing": "cubic-in-out"},
"pad": {"b": 10, "t": 50},
"len": 0.9,
"x": 0.1,
"y": 0,
"steps": []
}
return sliders

def __get_slider_step(self, t_ndx_list, index, f_duration, t_duration):
step = {"args": [[str(t_ndx_list[index])],
{"frame": {"duration": f_duration, "redraw": True},
"mode": "immediate",
"transition": {"duration": t_duration}
}],
"label": str(t_ndx_list[index]),
"method": "animate"}
return step

def __map_species_to_types(self, data, name, spec_name, deterministic, concentration, points):
types = {}
for i, val in enumerate(data['type']):
if (deterministic or not concentration):
spec_data = data[spec_name][i]
else:
spec_data = data[spec_name][i] / (data['mass'][i] / data['rho'][i])

if name in types.keys():
types[name]['points'].append(points[i])
types[name]['data'].append(spec_data)
else:
types[name] = {"points":[points[i]], "data":[spec_data]}
return types

def __map_property_to_type(self, property_name, data, included_types_list, points, p_ndx):
types = {}
if property_name == 'type':
for i, val in enumerate(data['type']):
name = "type {}".format(val)

if included_types_list is None or val in included_types_list:
if name in types.keys():
types[name]['points'].append(points[i])
types[name]['data'].append(data[property_name][i])
else:
types[name] = {"points":[points[i]], "data":[data[property_name][i]]}
elif property_name == 'v':
types[property_name] = {
"points": points,
"data" : [data[property_name][i][p_ndx] for i in range(0,len(data[property_name]))]
}
else:
types[property_name] = {
"points": points,
"data" : data[property_name]
}
return types

def read_step(self, step_num, debug=False):
""" Read the data for simulation step 'step_num'. Returns a tuple containing a numpy.ndarray \
of point coordinates [0] along with a dictionary of property and species data [1]
Expand Down Expand Up @@ -422,22 +518,8 @@ def plot_species(self, species, t_ndx=None, t_val=None, concentration=False,
return

# map data to types
types = {}
for i, val in enumerate(data['type']):
name = species
if (deterministic or not concentration):
spec_data = data[spec_name][i]
else:
spec_data = data[spec_name][i] / (data['mass'][i] / data['rho'][i])

if name in types.keys():
types[name]['points'].append(points[i])
types[name]['data'].append(spec_data)
else:
types[name] = {"points":[points[i]], "data":[spec_data]}

types = self.__map_species_to_types(data, species, spec_name, deterministic, concentration, points)
is_2d = self.model.domain.dimensions == 2

trace_list = _plotly_iterate(types, size=size, colormap=colormap, is_2d=is_2d)

scene = {
Expand All @@ -453,46 +535,8 @@ def plot_species(self, species, t_ndx=None, t_val=None, concentration=False,

# function for 3D animations
if animated and len(t_ndx_list) > 1:
fig["layout"]["updatemenus"] = [
{"buttons": [
{"args": [None, {"frame": {"duration": f_duration, "redraw": False},
"fromcurrent": True,
"transition": {"duration": t_duration, "easing": "quadratic-in-out"}}],
"label": "Play",
"method": "animate"
},
{"args": [[None], {"frame": {"duration": 0, "redraw": False},
"mode": "immediate",
"transition": {"duration": 0}}],
"label": "Pause",
"method": "animate"
}],
"direction": "left",
"pad": {"r": 10, "t": 87},
"showactive": False,
"type": "buttons",
"x": 0.1,
"xanchor": "right",
"y": 0,
"yanchor": "top"
}]

sliders_dict = {
"active": 0,
"yanchor": "top",
"xanchor": "left",
"currentvalue": {
"font": {"size": 20},
"prefix": "Time:",
"visible": True,
"xanchor": "right"
},
"transition": {"duration": t_duration, "easing": "cubic-in-out"},
"pad": {"b": 10, "t": 50},
"len": 0.9,
"x": 0.1,
"y": 0,
"steps": []}
fig["layout"]["updatemenus"] = [self.__setup_updatemenues(f_duration, t_duration)]
sliders_dict = self.__setup_sliders(t_duration)

_data = data[spec_name] if (deterministic or not concentration) else data[spec_name] / (data['mass'] / data['rho'])
cmin = min(_data)
Expand All @@ -510,33 +554,13 @@ def plot_species(self, species, t_ndx=None, t_val=None, concentration=False,
points, data = self.read_step(index)

# map data to types
types = {}
for i, val in enumerate(data['type']):
name = species
if (deterministic or not concentration):
spec_data = data[spec_name][i]
else:
spec_data = data[spec_name][i] / (data['mass'][i] / data['rho'][i])

if name in types.keys():
types[name]['points'].append(points[i])
types[name]['data'].append(spec_data)
else:
types[name] = {"points":[points[i]], "data":[spec_data]}

types = self.__map_species_to_types(data, species, spec_name, deterministic, concentration, points)
trace_list = _plotly_iterate(types, size=size, colormap=colormap, cmin=cmin, cmax=cmax, is_2d=is_2d)

frame = {"data":trace_list, "name":str(t_ndx_list[index])}
frames.append(frame)

slider_step = {"args": [[str(t_ndx_list[index])],
{"frame": {"duration": f_duration, "redraw": True},
"mode": "immediate",
"transition": {"duration": t_duration}
}],
"label": str(t_ndx_list[index]),
"method": "animate"}

slider_step = self.__get_slider_step(t_ndx_list, index, f_duration, t_duration)
sliders_dict['steps'].append(slider_step)

fig["layout"]["sliders"] = [sliders_dict]
Expand Down Expand Up @@ -702,30 +726,8 @@ def plot_property(self, property_name, t_ndx=None, t_val=None, p_ndx=0, width=No

from plotly.offline import init_notebook_mode, iplot

types = {}
if property_name == 'type':
for i, val in enumerate(data['type']):
name = "type {}".format(val)

if included_types_list is None or val in included_types_list:
if name in types.keys():
types[name]['points'].append(points[i])
types[name]['data'].append(data[property_name][i])
else:
types[name] = {"points":[points[i]], "data":[data[property_name][i]]}
elif property_name == 'v':
types[property_name] = {
"points": points,
"data" : [data[property_name][i][p_ndx] for i in range(0,len(data[property_name]))]
}
else:
types[property_name] = {
"points": points,
"data" : data[property_name]
}

types = self.__map_property_to_type(property_name, data, included_types_list, points, p_ndx)
is_2d = self.model.domain.dimensions == 2

trace_list = _plotly_iterate(types, size=size, property_name=property_name,
colormap=colormap, is_2d=is_2d)

Expand All @@ -743,46 +745,8 @@ def plot_property(self, property_name, t_ndx=None, t_val=None, p_ndx=0, width=No

# function for 3D animations
if animated and len(t_ndx_list) > 1:
fig["layout"]["updatemenus"] = [
{"buttons": [
{"args": [None, {"frame": {"duration": f_duration, "redraw": False},
"fromcurrent": True,
"transition": {"duration": t_duration, "easing": "quadratic-in-out"}}],
"label": "Play",
"method": "animate"
},
{"args": [[None], {"frame": {"duration": 0, "redraw": False},
"mode": "immediate",
"transition": {"duration": 0}}],
"label": "Pause",
"method": "animate"
}],
"direction": "left",
"pad": {"r": 10, "t": 87},
"showactive": False,
"type": "buttons",
"x": 0.1,
"xanchor": "right",
"y": 0,
"yanchor": "top"
}]

sliders_dict = {
"active": 0,
"yanchor": "top",
"xanchor": "left",
"currentvalue": {
"font": {"size": 20},
"prefix": "Time:",
"visible": True,
"xanchor": "right"
},
"transition": {"duration": t_duration, "easing": "cubic-in-out"},
"pad": {"b": 10, "t": 50},
"len": 0.9,
"x": 0.1,
"y": 0,
"steps": []}
fig["layout"]["updatemenus"] = [self.__setup_updatemenues(f_duration, t_duration)]
sliders_dict = self.__setup_sliders(t_duration)

cmin = min(data[property_name]) if property_name != "v" else min(data[property_name], key=lambda val: val[p_ndx])[p_ndx]
cmax = max(data[property_name]) if property_name != "v" else max(data[property_name], key=lambda val: val[p_ndx])[p_ndx]
Expand All @@ -800,42 +764,14 @@ def plot_property(self, property_name, t_ndx=None, t_val=None, p_ndx=0, width=No
points, data = self.read_step(index)

# map data to types
types = {}
if property_name == 'type':
for i, val in enumerate(data['type']):
name = "type {}".format(val)

if included_types_list is None or val in included_types_list:
if name in types.keys():
types[name]['points'].append(points[i])
types[name]['data'].append(data[property_name][i])
else:
types[name] = {"points":[points[i]], "data":[data[property_name][i]]}
elif property_name == 'v':
types[property_name] = {
"points": points,
"data" : [data[property_name][i][p_ndx] for i in range(0,len(data[property_name]))]
}
else:
types[property_name] = {
"points": points,
"data" : data[property_name]
}

types = self.__map_property_to_type(property_name, data, included_types_list, points, p_ndx)
trace_list = _plotly_iterate(types, size=size, property_name=property_name,
colormap=colormap, cmin=cmin, cmax=cmax, is_2d=is_2d)

frame = {"data":trace_list, "name":str(t_ndx_list[index])}
frames.append(frame)

slider_step = {"args": [[str(t_ndx_list[index])],
{"frame": {"duration": f_duration, "redraw": False},
"mode": "immediate",
"transition": {"duration": t_duration}
}],
"label": str(t_ndx_list[index]),
"method": "animate"}

slider_step = self.__get_slider_step(t_ndx_list, index, f_duration, t_duration)
sliders_dict['steps'].append(slider_step)

fig["layout"]["sliders"] = [sliders_dict]
Expand Down

0 comments on commit 0036155

Please sign in to comment.