Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions spatialpy/Domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import numpy
from scipy.spatial import KDTree

from spatialpy.Result import _plotly_iterate


class Domain():
""" Domain class for SpatialPy """
Expand Down Expand Up @@ -198,6 +200,85 @@ def calculate_vol(self):
self.vol[v4] += t_vol/4


def plot_types(self, width=None, height=None, colormap=None, size=5, title=None,
use_matplotlib=False, return_plotly_figure=False):
'''
Plots the domain using plotly. Can only be viewed in a Jupyter Notebook.

Attributes
----------
width: int (default 500)
Width in pixels of output plot box or for matplotlib inches of output plot box
height: int (default 500)
Height in pixels of output plot box or for matplotlib inches of output plot box
colormap : str
colormap to use. Plotly specification, valid values: "Plotly3","Jet","Blues","YlOrRd",
"PuRd","BuGn","YlOrBr","PuBuGn","BuPu","YlGnBu", "PuBu","GnBu","YlGn","Greens","Reds",
"Greys","RdPu","OrRd","Purples","Oranges".
size : int
Size in pixels of the particle
title : str
The title of the graph
return_plotly_figure : bool
whether or not to return a figure dictionary of data(graph object traces) and layout options
which may be edited by the user.
use_matplotlib : bool
whether or not to plot the proprties results using matplotlib.
'''

if width is None:
width = 6.4 if use_matplotlib else 500
if height is None:
height = 4.8 if use_matplotlib else 500

if use_matplotlib:
import matplotlib.pyplot as plt

plt.figure(figsize=(width, height))
plt.scatter(self.vertices[:,0], self.vertices[:,1], c=self.type, cmap=colormap)
plt.axis('scaled')
plt.colorbar()
if title is not None:
plt.title(title)
plt.grid(linestyle='--', linewidth=1)
plt.plot()
return

from plotly.offline import init_notebook_mode, iplot

types = {}
for i, val in enumerate(self.type):
name = "type {}".format(val)

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

is_2d = self.zlim[0] == self.zlim[1]

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

scene = {
"aspectmode": 'data',
}
layout = {"width": width, "height": width, "scene":scene,
"xaxis":{"range":self.xlim}, "yaxis":{"range":self.ylim}
}

if title is not None:
layout["title"] = title

fig = {"data":trace_list, "layout":layout}

if return_plotly_figure:
return fig
else:
init_notebook_mode(connected=True)
iplot(fig)


@classmethod
def read_xml_mesh(cls, filename):
Expand Down
53 changes: 30 additions & 23 deletions spatialpy/Result.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,9 @@ def get_species(self, species, timepoints=None, concentration=False, determinist
ret = ret.flatten()
return ret

def plot_species(self, species, t_ndx=0, concentration=False, deterministic=False, width=500, height=500, colormap=None, size=5, title=None,
def plot_species(self, species, t_ndx=0, concentration=False, deterministic=False, width=None, height=None, colormap=None, size=5, title=None,
animated=False, t_ndx_list=None, speed=1, f_duration=500, t_duration=300, return_plotly_figure=False,
use_matplotlib=False, mpl_width=6.4, mpl_height=4.8,
debug=False):
use_matplotlib=False, debug=False):
""" Plots the Results using plotly. Can only be viewed in a Jupyter Notebook.

If concentration is False (default), the integer, raw, trajectory data is returned,
Expand Down Expand Up @@ -348,10 +347,6 @@ def plot_species(self, species, t_ndx=0, concentration=False, deterministic=Fals
which may be edited by the user.
use_matplotlib : bool
whether or not to plot the proprties results using matplotlib.
mpl_width: int (default 6.4)
Width in inches of output plot box
mpl_height: int (default 4.8)
Height in inches of output plot box
debug: bool
output debugging info
"""
Expand All @@ -373,6 +368,11 @@ def plot_species(self, species, t_ndx=0, concentration=False, deterministic=Fals
if use_matplotlib:
import matplotlib.pyplot as plt

if width is None:
width = 6.4
if height is None:
height = 4.8

if deterministic or not concentration:
d = data[spec_name]
else:
Expand All @@ -390,6 +390,11 @@ def plot_species(self, species, t_ndx=0, concentration=False, deterministic=Fals
plt.plot()
return

if width is None:
width = 500
if height is None:
height = 500

# map data to types
types = {}
for i, val in enumerate(data['type']):
Expand Down Expand Up @@ -549,15 +554,11 @@ def get_property(self, property_name, timepoints=None):
ret = ret.flatten()
return ret

def plot_property(self, property_name, t_ndx=0, p_ndx=0, width=500, height=500, colormap=None, size=5, title=None,
animated=False, t_ndx_list=None, speed=1, f_duration=500, t_duration=300, return_plotly_figure=False,
use_matplotlib=False, mpl_width=6.4, mpl_height=4.8):
""" Plots the Results using plotly. Can only be viewed in a Jupyter Notebook.

If concentration is False (default), the integer, raw, trajectory data is returned,
if set to True, the concentration (=copy_number/volume) is returned.

If deterministic is True, show results for determinstic (instead of stochastic) values
def plot_property(self, property_name, t_ndx=0, p_ndx=0, width=None, height=None, colormap=None, size=5, title=None,
animated=False, t_ndx_list=None, speed=1, f_duration=500, t_duration=300,
return_plotly_figure=False, use_matplotlib=False):
"""
Plots the Results using plotly. Can only be viewed in a Jupyter Notebook.

Attributes
----------
Expand All @@ -568,9 +569,9 @@ def plot_property(self, property_name, t_ndx=0, p_ndx=0, width=500, height=500,
p_ndx : int
The property index of the results to be plotted
width: int (default 500)
Width in pixels of output plot box
Width in pixels of output plot box or for matplotlib inches of output plot box
height: int (default 500)
Height in pixels of output plot box
Height in pixels of output plot box or for matplotlib inches of output plot box
colormap : str
colormap to use. Plotly specification, valid values: "Plotly3","Jet","Blues","YlOrRd",
"PuRd","BuGn","YlOrBr","PuBuGn","BuPu","YlGnBu", "PuBu","GnBu","YlGn","Greens","Reds",
Expand All @@ -595,10 +596,6 @@ def plot_property(self, property_name, t_ndx=0, p_ndx=0, width=500, height=500,
which may be edited by the user.
use_matplotlib : bool
whether or not to plot the proprties results using matplotlib.
mpl_width: int (default 6.4)
Width in inches of output plot box
mpl_height: int (default 4.8)
Height in inches of output plot box
"""

if t_ndx < 0:
Expand All @@ -614,6 +611,11 @@ def plot_property(self, property_name, t_ndx=0, p_ndx=0, width=500, height=500,
if use_matplotlib:
import matplotlib.pyplot as plt

if width is None:
width = 6.4
if height is None:
height = 4.8

if property_name == 'v':
d = data[property_name]
d = [d[i][p_ndx] for i in range(0,len(d))]
Expand All @@ -622,7 +624,7 @@ def plot_property(self, property_name, t_ndx=0, p_ndx=0, width=500, height=500,
if colormap is None:
colormap = "viridis"

plt.figure(figsize=(mpl_width,mpl_height))
plt.figure(figsize=(width, height))
plt.scatter(points[:,0],points[:,1],c=d,cmap=colormap)
plt.axis('scaled')
plt.colorbar()
Expand All @@ -632,6 +634,11 @@ def plot_property(self, property_name, t_ndx=0, p_ndx=0, width=500, height=500,
plt.plot()
return

if width is None:
width = 500
if height is None:
height = 500

from plotly.offline import init_notebook_mode, iplot

types = {}
Expand Down