-
Notifications
You must be signed in to change notification settings - Fork 18
/
plot.py
198 lines (161 loc) · 5.87 KB
/
plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Copyright (c) 2011 by California Institute of Technology
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the California Institute of Technology nor
# the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CALTECH
# OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
"""
Functions for plotting Partitions.
"""
import logging
logger = logging.getLogger(__name__)
from warnings import warn
import numpy as np
from scipy import sparse as sp
import networkx as nx
from polytope import cheby_ball
try:
import matplotlib as mpl
except Exception, e:
logger.error(e)
mpl = None
try:
from tulip.graphics import newax
except Exception, e:
logger.error(e)
mpl = None
def plot_partition(
ppp, trans=None, ppp2trans=None, only_adjacent=False,
ax=None, plot_numbers=True, color_seed=None, show=False
):
"""Plot partition with arrows from digraph.
For filtering edges based on label use L{plot_ts_on_partition}.
See Also
========
L{abstract.prop2partition.PropPreservingPartition}, L{plot_trajectory}
@type ppp: L{PropPreservingPartition}
@param trans: Transition matrix. If used,
then transitions in C{ppp} are shown with arrows.
Otherwise C{ppp.adj} is plotted.
To show C{ppp.adj}, pass: trans = True
@param plot_numbers: If True,
then annotate each Region center with its number.
@param show: If True, then show the plot.
Otherwise return axis object.
Axis object is good for creating custom plots.
@param ax: axes where to plot
@param color_seed: seed for reproducible random coloring
@param ppp2trans: order mapping ppp indices to trans states
@type ppp2trans: list of trans states
"""
if mpl is None:
warn('matplotlib not found')
return
# needs to be converted to adjacency matrix ?
if isinstance(trans, nx.MultiDiGraph):
if trans is not None and ppp2trans is None:
msg = 'trans is a networkx MultiDiGraph, '
msg += 'so ppp2trans required to define state order,\n'
msg += 'used when converting the graph to an adjacency matrix.'
raise Exception(msg)
trans = nx.to_numpy_matrix(trans, nodelist=ppp2trans)
trans = np.array(trans)
l,u = ppp.domain.bounding_box
arr_size = (u[0,0]-l[0,0])/50.0
# new figure ?
if ax is None:
ax, fig = newax()
# no trans given: use partition's
if trans is True and ppp.adj is not None:
ax.set_title('Adjacency from Partition')
trans = ppp.adj
elif trans is None:
trans = 'none'
else:
ax.set_title('Adjacency from given Transitions')
ax.set_xlim(l[0,0],u[0,0])
ax.set_ylim(l[1,0],u[1,0])
# repeatable coloring ?
if color_seed is not None:
prng = np.random.RandomState(color_seed)
else:
prng = np.random.RandomState()
# plot polytope patches
for i, reg in enumerate(ppp.regions):
# select random color,
# same color for all polytopes in each region
col = prng.rand(3)
# single polytope or region ?
reg.plot(color=col, ax=ax)
if plot_numbers:
reg.text(str(i), ax, color='red')
# not show trans ?
if trans is 'none':
if show:
mpl.pyplot.show()
return ax
# plot transition arrows between patches
rows, cols = np.nonzero(trans)
for i, j in zip(rows, cols):
# mask non-adjacent cell transitions ?
if only_adjacent:
if ppp.adj[i, j] == 0:
continue
plot_transition_arrow(ppp.regions[i], ppp.regions[j], ax, arr_size)
if show:
mpl.pyplot.show()
return ax
def plot_transition_arrow(polyreg0, polyreg1, ax, arr_size=None):
"""Plot arrow starting from polyreg0 and ending at polyreg1.
@type polyreg0: L{Polytope} or L{Region}
@type polyreg1: L{Polytope} or L{Region}
@param ax: axes where to plot
@return: arrow object
"""
# brevity
p0 = polyreg0
p1 = polyreg1
rc0, xc0 = cheby_ball(p0)
rc1, xc1 = cheby_ball(p1)
if np.sum(np.abs(xc1-xc0)) < 1e-7:
return None
if arr_size is None:
l,u = polyreg1.bounding_box
arr_size = (u[0,0]-l[0,0])/25.0
#TODO: 3d
x = xc0[0]
y = xc0[1]
dx = xc1[0] - xc0[0]
dy = xc1[1] - xc0[1]
arrow = mpl.patches.Arrow(
float(x), float(y), float(dx), float(dy),
width=arr_size, color='black'
)
ax.add_patch(arrow)
return arrow