Changeset 8678a34 in sasmodels
- Timestamp:
- Apr 9, 2017 5:45:20 PM (8 years ago)
- Branches:
- master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
- Children:
- a0ebc96
- Parents:
- 85190c2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
explore/jitter.py
r85190c2 r8678a34 3 3 dispersity and possible replacement algorithms. 4 4 """ 5 import sys 6 5 7 import mpl_toolkits.mplot3d # Adds projection='3d' option to subplot 6 8 import matplotlib.pyplot as plt 7 9 from matplotlib.widgets import Slider, CheckButtons 8 10 from matplotlib import cm 9 10 11 import numpy as np 11 12 from numpy import pi, cos, sin, sqrt, exp, degrees, radians 12 13 13 def draw_beam(ax ):14 def draw_beam(ax, view=(0, 0)): 14 15 #ax.plot([0,0],[0,0],[1,-1]) 15 16 #ax.scatter([0]*100,[0]*100,np.linspace(1, -1, 100), alpha=0.8) … … 22 23 x = r*np.outer(np.cos(u), np.ones_like(v)) 23 24 y = r*np.outer(np.sin(u), np.ones_like(v)) 24 z = np.outer(np.ones_like(u), v) 25 z = 1.3*np.outer(np.ones_like(u), v) 26 27 theta, phi = view 28 shape = x.shape 29 points = np.matrix([x.flatten(), y.flatten(), z.flatten()]) 30 points = Rz(phi)*Ry(theta)*points 31 x, y, z = [v.reshape(shape) for v in points] 25 32 26 33 ax.plot_surface(x, y, z, rstride=4, cstride=4, color='y', alpha=0.5) 27 34 28 def draw_shimmy(ax, theta, phi, psi, dtheta, dphi, dpsi): 29 size=[0.1, 0.4, 1.0] 30 view=[theta, phi, psi] 31 shimmy=[0,0,0] 32 #draw_shape = draw_parallelepiped 33 draw_shape = draw_ellipsoid 35 def draw_jitter(ax, view, jitter): 36 size = [0.1, 0.4, 1.0] 37 draw_shape = draw_parallelepiped 38 #draw_shape = draw_ellipsoid 34 39 35 40 #np.random.seed(10) … … 64 69 [ 1, 1, 1], 65 70 ] 71 dtheta, dphi, dpsi = jitter 66 72 if dtheta == 0: 67 73 cloud = [v for v in cloud if v[0] == 0] … … 70 76 if dpsi == 0: 71 77 cloud = [v for v in cloud if v[2] == 0] 72 draw_shape(ax, size, view, shimmy, steps=100, alpha=0.8)78 draw_shape(ax, size, view, [0, 0, 0], steps=100, alpha=0.8) 73 79 for point in cloud: 74 shimmy=[dtheta*point[0], dphi*point[1], dpsi*point[2]]75 draw_shape(ax, size, view, shimmy, alpha=0.8)80 delta = [dtheta*point[0], dphi*point[1], dpsi*point[2]] 81 draw_shape(ax, size, view, delta, alpha=0.8) 76 82 for v in 'xyz': 77 83 a, b, c = size … … 80 86 getattr(ax, v+'axis').label.set_text(v) 81 87 82 def draw_ellipsoid(ax, size, view, shimmy, steps=25, alpha=1):88 def draw_ellipsoid(ax, size, view, jitter, steps=25, alpha=1): 83 89 a,b,c = size 84 theta, phi, psi = view85 dtheta, dphi, dpsi = shimmy86 87 90 u = np.linspace(0, 2 * np.pi, steps) 88 91 v = np.linspace(0, np.pi, steps) … … 90 93 y = b*np.outer(np.sin(u), np.sin(v)) 91 94 z = c*np.outer(np.ones_like(u), np.cos(v)) 92 93 shape = x.shape 94 points = np.matrix([x.flatten(),y.flatten(),z.flatten()]) 95 points = Rz(dpsi)*Ry(dtheta)*Rx(dphi)*points 96 points = Rz(phi)*Ry(theta)*Rz(psi)*points 97 x,y,z = [v.reshape(shape) for v in points] 95 x, y, z = transform_xyz(view, jitter, x, y, z) 98 96 99 97 ax.plot_surface(x, y, z, rstride=4, cstride=4, color='w', alpha=alpha) 100 98 101 def draw_parallelepiped(ax, size, view, shimmy, alpha=1): 99 draw_labels(ax, view, jitter, [ 100 ('c+', [ 0, 0, c], [ 1, 0, 0]), 101 ('c-', [ 0, 0,-c], [ 0, 0,-1]), 102 ('a+', [ a, 0, 0], [ 0, 0, 1]), 103 ('a-', [-a, 0, 0], [ 0, 0,-1]), 104 ('b+', [ 0, b, 0], [-1, 0, 0]), 105 ('b-', [ 0,-b, 0], [-1, 0, 0]), 106 ]) 107 108 def draw_parallelepiped(ax, size, view, jitter, steps=None, alpha=1): 102 109 a,b,c = size 103 theta, phi, psi = view104 dtheta, dphi, dpsi = shimmy105 106 110 x = a*np.array([ 1,-1, 1,-1, 1,-1, 1,-1]) 107 111 y = b*np.array([ 1, 1,-1,-1, 1, 1,-1,-1]) … … 118 122 ]) 119 123 120 points = np.matrix([x,y,z]) 121 points = Rz(dpsi)*Ry(dtheta)*Rx(dphi)*points 122 points = Rz(phi)*Ry(theta)*Rz(psi)*points 123 124 x,y,z = [np.array(v).flatten() for v in points] 124 x, y, z = transform_xyz(view, jitter, x, y, z) 125 125 ax.plot_trisurf(x, y, triangles=tri, Z=z, color='w', alpha=alpha) 126 126 127 def draw_sphere(ax, radius=10., steps=100): 128 u = np.linspace(0, 2 * np.pi, steps) 129 v = np.linspace(0, np.pi, steps) 130 131 x = radius * np.outer(np.cos(u), np.sin(v)) 132 y = radius * np.outer(np.sin(u), np.sin(v)) 133 z = radius * np.outer(np.ones(np.size(u)), np.cos(v)) 134 ax.plot_surface(x, y, z, rstride=4, cstride=4, color='w') 135 136 def draw_mesh_new(ax, theta, dtheta, phi, dphi, flow, radius=10., dist='gauss'): 137 theta_center = radians(theta) 138 phi_center = radians(phi) 139 flow_center = radians(flow) 140 dtheta = radians(dtheta) 141 dphi = radians(dphi) 142 143 # 10 point 3-sigma gaussian weights 144 t = np.linspace(-3., 3., 11) 127 draw_labels(ax, view, jitter, [ 128 ('c+', [ 0, 0, c], [ 1, 0, 0]), 129 ('c-', [ 0, 0,-c], [ 0, 0,-1]), 130 ('a+', [ a, 0, 0], [ 0, 0, 1]), 131 ('a-', [-a, 0, 0], [ 0, 0,-1]), 132 ('b+', [ 0, b, 0], [-1, 0, 0]), 133 ('b-', [ 0,-b, 0], [-1, 0, 0]), 134 ]) 135 136 def draw_mesh(ax, view, jitter, radius=1.2, n=11, dist='gauss'): 137 theta, phi, psi = view 138 dtheta, dphi, dpsi = jitter 145 139 if dist == 'gauss': 140 t = np.linspace(-3, 3, n) 146 141 weights = exp(-0.5*t**2) 147 142 elif dist == 'rect': 143 t = np.linspace(0, 1, n) 148 144 weights = np.ones_like(t) 149 145 else: 150 146 raise ValueError("expected dist to be 'gauss' or 'rect'") 151 theta = dtheta*t 152 phi = dphi*t 153 154 x = radius * np.outer(cos(phi), cos(theta)) 155 y = radius * np.outer(sin(phi), cos(theta)) 156 z = radius * np.outer(np.ones_like(phi), sin(theta)) 157 #w = np.outer(weights, weights*abs(cos(dtheta*t))) 158 w = np.outer(weights, weights*abs(cos(theta))) 159 160 x, y, z, w = [v.flatten() for v in (x,y,z,w)] 161 x, y, z = rotate(x, y, z, phi_center, theta_center, flow_center) 162 163 ax.scatter(x, y, z, c=w, marker='o', vmin=0., vmax=1.) 164 165 def rotate(x, y, z, phi, theta, psi): 166 R = Rz(phi)*Ry(theta)*Rz(psi) 167 p = np.vstack([x,y,z]) 168 return R*p 147 148 # mesh in theta, phi formed by rotating z 149 z = np.matrix([[0], [0], [radius]]) 150 points = np.hstack([Rx(phi_i)*Ry(theta_i)*z 151 for theta_i in dtheta*t 152 for phi_i in dphi*t]) 153 # rotate relative to beam 154 points = orient_relative_to_beam(view, points) 155 156 w = np.outer(weights, weights) 157 158 x, y, z = [np.array(v).flatten() for v in points] 159 ax.scatter(x, y, z, c=w.flatten(), marker='o', vmin=0., vmax=1.) 169 160 170 161 def Rx(angle): … … 188 179 [0., 0., 1.]] 189 180 return np.matrix(R) 181 182 def transform_xyz(view, jitter, x, y, z): 183 x, y, z = [np.asarray(v) for v in (x, y, z)] 184 shape = x.shape 185 points = np.matrix([x.flatten(),y.flatten(),z.flatten()]) 186 points = apply_jitter(jitter, points) 187 points = orient_relative_to_beam(view, points) 188 x, y, z = [np.array(v).reshape(shape) for v in points] 189 return x, y, z 190 191 def apply_jitter(jitter, points): 192 dtheta, dphi, dpsi = jitter 193 points = Rz(dpsi)*Ry(dtheta)*Rx(dphi)*points 194 return points 195 196 def orient_relative_to_beam(view, points): 197 theta, phi, psi = view 198 points = Rz(phi)*Ry(theta)*Rz(psi)*points 199 return points 200 201 def draw_labels(ax, view, jitter, text): 202 labels, locations, orientations = zip(*text) 203 px, py, pz = zip(*locations) 204 dx, dy, dz = zip(*orientations) 205 206 px, py, pz = transform_xyz(view, jitter, px, py, pz) 207 dx, dy, dz = transform_xyz(view, jitter, dx, dy, dz) 208 209 for label, p, zdir in zip(labels, zip(px, py, pz), zip(dx, dy, dz)): 210 zdir = np.asarray(zdir).flatten() 211 ax.text(p[0], p[1], p[2], label, zdir=zdir) 212 213 def draw_sphere(ax, radius=10., steps=100): 214 u = np.linspace(0, 2 * np.pi, steps) 215 v = np.linspace(0, np.pi, steps) 216 217 x = radius * np.outer(np.cos(u), np.sin(v)) 218 y = radius * np.outer(np.sin(u), np.sin(v)) 219 z = radius * np.outer(np.ones(np.size(u)), np.cos(v)) 220 ax.plot_surface(x, y, z, rstride=4, cstride=4, color='w') 190 221 191 222 def main(): … … 206 237 207 238 axcolor = 'lightgoldenrodyellow' 239 208 240 axtheta = plt.axes([0.1, 0.15, 0.45, 0.04], axisbg=axcolor) 209 241 axphi = plt.axes([0.1, 0.1, 0.45, 0.04], axisbg=axcolor) … … 212 244 sphi = Slider(axphi, 'Phi', -180, 180, valinit=phi) 213 245 spsi = Slider(axpsi, 'Psi', -180, 180, valinit=psi) 246 214 247 axdtheta = plt.axes([0.75, 0.15, 0.15, 0.04], axisbg=axcolor) 215 248 axdphi = plt.axes([0.75, 0.1, 0.15, 0.04], axisbg=axcolor) … … 217 250 sdtheta = Slider(axdtheta, 'dTheta', 0, 30, valinit=dtheta) 218 251 sdphi = Slider(axdphi, 'dPhi', 0, 30, valinit=dphi) 219 sdpsi = Slider(axdpsi, 'dPsi', 0, 30, valinit=dp hi)252 sdpsi = Slider(axdpsi, 'dPsi', 0, 30, valinit=dpsi) 220 253 221 254 def update(val, axis=None): 222 theta, phi, psi= stheta.val, sphi.val, spsi.val223 dtheta, dphi, dpsi= sdtheta.val, sdphi.val, sdpsi.val255 view = stheta.val, sphi.val, spsi.val 256 jitter = sdtheta.val, sdphi.val, sdpsi.val 224 257 ax.cla() 225 draw_beam(ax) 226 draw_shimmy(ax, theta, phi, psi, dtheta, dphi, dpsi) 227 #if not axis.startswith('d'): 228 # ax.view_init(elev=theta, azim=phi) 258 draw_beam(ax, (0, 0)) 259 if 0: 260 draw_jitter(ax, view, jitter) 261 else: 262 draw_jitter(ax, view, (0,0,0)) 263 draw_mesh(ax, view, jitter) 229 264 plt.gcf().canvas.draw() 230 265
Note: See TracChangeset
for help on using the changeset viewer.