From f11cbfed01541eec86c9c161509ac52b88654b18 Mon Sep 17 00:00:00 2001 From: Bryan Rumsey Date: Wed, 9 Jun 2021 15:40:16 -0400 Subject: [PATCH 1/2] Cleaned up width and height arguments and docstrings for the plot species and plot property functions. --- spatialpy/Result.py | 53 +++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/spatialpy/Result.py b/spatialpy/Result.py index b231febf..e9c9da22 100644 --- a/spatialpy/Result.py +++ b/spatialpy/Result.py @@ -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, @@ -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 """ @@ -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: @@ -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']): @@ -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 ---------- @@ -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", @@ -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: @@ -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))] @@ -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() @@ -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 = {} From ca678322a46babd95c84f232e7b246203b915f6f Mon Sep 17 00:00:00 2001 From: Bryan Rumsey Date: Wed, 9 Jun 2021 15:41:38 -0400 Subject: [PATCH 2/2] Added type ploting support using matplotlib or plotly for domains. --- spatialpy/Domain.py | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/spatialpy/Domain.py b/spatialpy/Domain.py index 8a210837..476d87a5 100644 --- a/spatialpy/Domain.py +++ b/spatialpy/Domain.py @@ -3,6 +3,8 @@ import numpy from scipy.spatial import KDTree +from spatialpy.Result import _plotly_iterate + class Domain(): """ Domain class for SpatialPy """ @@ -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):