-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifs_properties_plot.py
356 lines (274 loc) · 11.1 KB
/
ifs_properties_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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# import numpy as np
# from matplotlib.lines import Line2D
# from matplotlib.artist import Artist
# from ifs_operators_plot import *
import abc
import numpy as np
import json
import os
from collections import OrderedDict
from ifs_2Dplot import plot_triangular_
def annotate_points_draw(ax_, line2d_):
linepoints = zip(*line2d_.get_data())
for idx, pt in enumerate(linepoints):
artist_ann = ax_.annotate(str(idx), pt)
ax_.draw_artist(artist_ann)
class PropertiesBasic(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def get_data(self):
return
@abc.abstractmethod
def set_data(self, data):
return
@abc.abstractclassmethod
def get_color(self):
return
@abc.abstractclassmethod
def set_color(self):
return
class PropertiesAnnotations(PropertiesBasic):
# def __init__(self, posetpool):
# self.posetpool = posetpool
def __init__(self, label=None,
holder=None,
radius=5,
annotations=None,
alpha_marker=0.5,
labels_size=12,
hide_ifs=False,
show_ann=True,
showverts=True,
showedges=False,
showlabels=False):
self.label = label
self.holder = holder
self.annotations = annotations
self.radius = radius
self.alpha_marker = alpha_marker
self.labels_size = labels_size
if annotations is not None:
self.set_fontsize_annotations(self.labels_size)
self.hide_ifs = hide_ifs
self.show_ann = show_ann
self.showverts = self.holder.get_visible() if showverts is None else showverts
self.showedges = showedges
self.showlabels = showlabels
@abc.abstractclassmethod
def get_markersize(self):
return
@abc.abstractclassmethod
def set_markersize(self, size):
return
@abc.abstractclassmethod
def draw_holder_annotations(self, ax):
return
def init_default(self, ax):
self.holder.set_visible(True)
self.holder.set_animated(False)
self.create_annotations(ax)
self.set_visible_annotations(True)
self.set_animated_annotations(False)
def set_animated(self, value):
self.set_animated_annotations(value)
self.holder.set_animated(value)
def create_annotations(self, ax):
data = self.get_data()
# data = list(zip(*self.get_data()))
fontsize = self.labels_size if self.labels_size is not None else 10
self.annotations = [ax.annotate(str(idx),pt,fontsize=fontsize,zorder=30)
for idx, pt in enumerate(data) ]
def set_visible_annotations(self, value):
self.show_ann = value
for a in self.annotations:
a.set_visible(value)
def set_animated_annotations(self, value):
for a in self.annotations:
a.set_animated(value)
def set_data_annotations(self, positions):
for ann, pos in zip(self.annotations, positions):
ann.xyann = pos
ann.xy = pos
ann.xytext = pos
self.set_fontsize_annotations(self.labels_size)
def set_data_annotations_single(self, idx, pos):
assert(0 <= idx < len(self.annotations))
ann = self.annotations[idx]
ann.xyann = pos
ann.xy = pos
ann.xytext = pos
# @abc.abstractclassmethod
def set_fontsize_annotations(self, fontsize):
for ann in self.annotations:
ann.set_fontsize(fontsize)
# return
def set_zorder_annotations(self, zorder):
for ann in self.annotations:
ann.set_zorder(zorder)
def draw_annotations(self, ax):
for a in self.annotations:
if self.show_ann:
ax.draw_artist(a)
class PropertiesPath(PropertiesAnnotations):
def __init__(self, label=None,
holder=None,
radius=5,
annotations=None,
alpha_marker=0.5,
labels_size=12,
hide_ifs=False,
show_ann=True,
showverts=True,
showedges=False,
showlabels=False):
super(PropertiesPath, self).__init__(label,
holder,
radius,
annotations,
alpha_marker,
labels_size,
hide_ifs,
show_ann,
showverts,
showedges,
showlabels)
# @abc.abstractmethod
def get_data(self):
# return PropertiesBasic.get_data(self)
return self.holder.get_offsets()
# @abc.abstractmethod
def set_data(self, data):
self.holder.set_offsets(data)
def get_color(self):
return self.holder._facecolors_original
def set_color(self, color):
self.holder.set_facecolor(color)
def get_markersize(self):
return self.holder._sizes
def set_markersize(self,size):
self.holder.set_sizes(size)
def draw_holder_annotations(self, ax):
self.draw_annotations(ax)
if self.holder.get_visible():
# ax.figure.canvas.renderer.draw_path_collection(self.holder)
self.holder.draw(ax.figure.canvas.renderer)
# ax.draw_artist(self.holder)
import collections as col
class PropertiesIFS(PropertiesAnnotations):
epsilon = 0.02
def __init__(self, label=None,
holder=None,
radius=2,
annotations=None,
alpha_marker=0.5,
labels_size=12,
hide_ifs=False,
show_ann=True,
showverts=True,
showedges=False,
showlabels=False):
super(PropertiesIFS, self).__init__(label,
holder,
radius,
annotations,
alpha_marker,
labels_size,
hide_ifs,
show_ann,
showverts,
showedges,
showlabels)
def save_to_json(self, json_path):
data = self.holder.get_data()
data_lst = list(zip(data[0], data[1]))
data = OrderedDict(list(enumerate(data_lst)))
# data = dict(data)
tmp_holder = OrderedDict([('data', data),
#('data', list(zip(data[0], data[1]))),
# ('radius', self.radius),
('contrast', self.holder.get_alpha()),
('labels_size', self.labels_size),
('color', self.holder.get_color()),
('marker', self.holder.get_marker()),
('marker_size', self.holder.get_markersize()),
('label', self.label)])
with open(json_path, 'w+') as fp:
json.dump(tmp_holder, fp, indent=4)
print('Saving property: ' + json_path)
@property
def default_path(self):
path = os.path.abspath(os.path.join(os.path.dirname( __file__ ),
'..',
'working_dir',
self.label + "_.json"))
return path
def save_to_json_default(self, event):
path = self.default_path
self.save_to_json(self, path )
@classmethod
def from_json(cls, json_path, ax, bins, rotation,
color=None, marker=None, alpha=None, markersize=None):
with open(json_path) as data_file:
data = json.load(data_file, object_pairs_hook=col.OrderedDict)
#assert set(data.keys())==set(['data','radius','alpha_marker',
# 'labels_size'])
marker = marker if marker is not None else data['marker']
alpha = alpha if alpha is not None else data['contrast']
color = color if color is not None else data['color']
markersize = markersize if markersize is not None else data['markersize']
ifs_holder = list(data['data'].values())
indices = range(len(ifs_holder))
dat = list(map(tuple, ifs_holder))
mus, nus = list(zip(*dat))
mus, nus = list(mus), list(nus)
print(mus)
print(nus)
ax_01, line2d1_01 = plot_triangular_(ax,
mus, nus, rang=1.0, bins=10,
rotation={'x':45, 'y':0},
markersize=markersize,
color=color,
marker=marker,
alpha=alpha)
ax_01.set_ylabel(r'$\nu$', fontsize=20)
ax_01.set_xlabel(r'$\mu$', fontsize=20)
line2d1_01.set_linestyle(' ')
# line2d1_01.set_markersize(data['marker_size'])
line2d1_01.set_markerfacecolor(color)
line2d1_01.set_color(color)
# line2d1_01.set_marker(marker=r'$\odot$')
line2d1_01.set_marker(marker=marker)
line2d1_01.set_zorder(2)
# ax_01.set_aspect('equal', 'datalim')
prop = PropertiesIFS(label='ifs01_ax01',
holder=line2d1_01,
labels_size=data['labels_size'])
prop.init_default(ax)
return prop
# @abc.abstractmethod
def get_data(self):
# return PropertiesBasic.get_data(self)
# return self.holder.get_data()
return list(zip(*self.holder.get_data()))
# @abc.abstractmethod
def set_data(self, mus, nus):
self.holder.set_data(mus, nus)
def get_data_pair(self):
return self.holder.get_data()
def get_color(self):
return self.holder.get_color()
def set_color(self):
self.holder.set_color()
def get_markersize(self):
return self.holder.get_markersize()
def set_markersize(self,size):
self.holder.set_markersize(size)
def draw_holder_annotations(self, ax):
self.draw_annotations(ax)
if self.holder.get_visible():
# ax.figure.canvas.renderer.draw_path_collection(self.holder)
# self.holder.draw(ax.figure.canvas.renderer)
ax.draw_artist(self.holder)
##############################################################
#######
##############################################################