99import time
1010import numpy as np
1111import matplotlib .pyplot as plt
12+ from skimage import measure
13+ from pathlib import Path
14+ import imageio
1215
1316
14-
15- def plot_conditional_idx_improvment (cond_idx ,cond_count = 2 ):
17+ def plot_conditional_idx_improvment (cond_idx , config ,cond_count = 2 ,save = True ):
1618
1719 found = (np .array (cond_idx )>= cond_count ).astype (np .float )
1820
@@ -21,10 +23,116 @@ def plot_conditional_idx_improvment(cond_idx,cond_count=2):
2123 count [i ] = np .average (found [:i ])
2224
2325 plt .plot (count )
26+ plt .xlabel ("Iteration" )
27+ plt .ylabel ("Empirical probability of conditional idx reaching %i" % cond_count )
28+
29+ if save : plt .savefig (config ['save_dir' ]+ 'improvment.png' )
30+
2431 plt .show ()
2532
2633
2734
35+ def extract_volume_from_gpr (lb ,ub ,res ,gpr ):
36+
37+
38+ axes_linspace = [np .linspace (ub [i ],lb [i ],res ) for i in range (len (lb ))]
39+
40+ X = np .array (np .meshgrid (* axes_linspace )).reshape ([3 ,- 1 ])
41+ X = np .swapaxes (X ,0 ,1 )
42+ X_r = np .linalg .norm (X ,axis = 1 )[...,np .newaxis ]
43+ U = X / X_r
44+
45+ r_est , sig = gpr .predict (U )
46+ Y = X_r <= r_est
47+
48+ Y = Y .reshape ([res ]* len (axes_linspace ))
49+
50+ return Y
51+
52+
53+ def extract_volume_from_mock_device (lb ,ub ,res ,device ):
54+
55+
56+ axes_linspace = [np .linspace (ub [i ],lb [i ],res ) for i in range (len (lb ))]
57+
58+ X = np .array (np .meshgrid (* axes_linspace )).reshape ([len (lb ),- 1 ])
59+ X = np .swapaxes (X ,0 ,1 )
60+ Y = device .arr_measure (X )
61+ Y = Y .reshape ([res ]* len (axes_linspace ))
62+
63+ return Y
64+
65+
66+ def plot_volume (vols ,plot_lb ,plot_ub ,vol_origin ,res ,cmap_func = None ,cmap = 'winter' ,perm = [0 ,1 ,2 ],ax = None ):
67+ verts , faces , normals , values = measure .marching_cubes_lewiner (vols ,0 )
68+
69+ vol_origin = np .array (vol_origin )
70+ vol_origin [[0 ,1 ,2 ]]= vol_origin [[1 ,0 ,2 ]]
71+ verts = (((verts )/ res )* - 2000 )+ vol_origin
72+
73+ points_surf = verts [faces [:,0 ],:]
74+ points_surf [:,[0 ,1 ,2 ]] = points_surf [:,[1 ,0 ,2 ]]
75+
76+
77+ #preds = sampler.gpc.predict_comb_prob(points_surf)
78+ preds = cmap_func (points_surf ) if cmap_func is not None else np .array ([0.5 ]* points_surf .shape [0 ])
79+ cmap = plt .get_cmap (cmap )
80+ c_preds = cmap (preds ).squeeze ()
81+ if ax is None :
82+ fig = plt .figure (figsize = (10 , 10 ))
83+ ax = fig .add_subplot (111 , projection = '3d' )
84+
85+ ax .view_init (azim = 0 , elev = 15 )
86+
87+
88+ #if points is not None: ax.scatter(*points,c=condidx,zorder=1)
89+
90+ surf = ax .plot_trisurf (verts [:, perm [0 ]], verts [:, perm [1 ]], faces , verts [:, perm [2 ]],
91+ lw = 0.1 ,edgecolor = "black" ,alpha = 0.5 ,vmin = 0 ,vmax = 1. )
92+ surf .set_facecolor (c_preds .tolist ())
93+
94+ ax .set_xlim ([plot_ub [0 ],plot_lb [0 ]])
95+ ax .set_ylim ([plot_ub [1 ],plot_lb [1 ]])
96+ ax .set_zlim ([plot_ub [2 ],plot_lb [2 ]])
97+
98+ ax .set_ylabel ("Gate 1 / mV" )
99+ ax .set_xlabel ("Gate 2 / mV" )
100+ ax .set_zlabel ("Gate 3 / mV" )
101+ return ax
102+
103+ def plot_3d_scatter (points ,condidx = None ,condidx_max = 3 ,cmap = 'plasma' ,ax = None ):
104+ points = np .array (points )
105+ points [:,[0 ,1 ,2 ]] = points [:,[1 ,0 ,2 ]]
106+
107+ if ax is None :
108+ fig = plt .figure (figsize = (10 , 10 ))
109+ ax = fig .add_subplot (111 , projection = '3d' )
110+
111+ ax .view_init (azim = 0 , elev = 15 )
112+
113+ if condidx is not None :
114+ ax .scatter (* points .T ,c = condidx )
115+ else :
116+ ax .scatter (* points .T )
117+
118+ return ax
119+
120+ def rotate_save (ax ,path ,step = 9 ):
121+
122+ Path (path ).mkdir (parents = True ,exist_ok = True )
123+
124+ fnames = []
125+ for i in range (0 ,360 ,step ):
126+
127+ ax .view_init (azim = i , elev = 15 )
128+ fnames += [path + '/%i.png' % i ]
129+ plt .savefig (fnames [- 1 ])
130+
131+ images = []
132+ for fname in fnames :
133+ images .append (imageio .imread (fname ))
134+ imageio .mimsave (path + '/movie.gif' , images )
135+
28136
29137
30138
0 commit comments