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
|
"""
Application to explore the difference between sasview 3.x orientation
dispersity and possible replacement algorithms.
"""
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import CheckButtons, Slider
from numpy import cos, exp, radians, sin
def draw_sphere(ax, radius=10., steps=100):
u = np.linspace(0, 2 * np.pi, steps)
v = np.linspace(0, np.pi, steps)
x = radius * np.outer(np.cos(u), np.sin(v))
y = radius * np.outer(np.sin(u), np.sin(v))
z = radius * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='w')
def draw_mesh_current(ax, theta, dtheta, phi, dphi, radius=10., dist='gauss'):
theta = radians(theta)
phi = radians(phi)
dtheta = radians(dtheta)
dphi = radians(dphi)
# 10 point 3-sigma gaussian weights
t = np.linspace(-3., 3., 11)
if dist == 'gauss':
weights = exp(-0.5*t**2)
elif dist == 'rect':
weights = np.ones_like(t)
else:
raise ValueError("expected dist to be 'gauss' or 'rect'")
theta = theta + dtheta*t
phi = phi + dphi*t
x = radius * np.outer(cos(phi), cos(theta))
y = radius * np.outer(sin(phi), cos(theta))
z = radius * np.outer(np.ones_like(phi), sin(theta))
w = np.outer(weights, weights*abs(cos(theta)))
x,y,z,w = [v.flatten() for v in (x,y,z,w)]
ax.scatter(x, y, z, c=w, marker='o', vmin=0., vmax=1.0)
def draw_mesh_new(ax, theta, dtheta, phi, dphi, flow, radius=10., dist='gauss'):
theta_center = radians(90-theta)
phi_center = radians(phi)
flow_center = radians(flow)
dtheta = radians(dtheta)
dphi = radians(dphi)
# 10 point 3-sigma gaussian weights
t = np.linspace(-3., 3., 11)
if dist == 'gauss':
weights = exp(-0.5*t**2)
elif dist == 'rect':
weights = np.ones_like(t)
else:
raise ValueError("expected dist to be 'gauss' or 'rect'")
theta = dtheta*t
phi = dphi*t
x = radius * np.outer(cos(phi), cos(theta))
y = radius * np.outer(sin(phi), cos(theta))
z = radius * np.outer(np.ones_like(phi), sin(theta))
#w = np.outer(weights, weights*abs(cos(dtheta*t)))
w = np.outer(weights, weights*abs(cos(theta)))
x, y, z, w = [v.flatten() for v in (x,y,z,w)]
x, y, z = rotate(x, y, z, phi_center, theta_center, flow_center)
ax.scatter(x, y, z, c=w, marker='o', vmin=0., vmax=1.)
def rotate(x, y, z, phi, theta, psi):
R = rotation_matrix(psi, theta, phi)
p = np.vstack([x,y,z])
q = np.dot(R,p)
return q
def rotation_matrix(xa,ya,za):
Rz = [[cos(za), -sin(za), 0.],
[sin(za), cos(za), 0.],
[0., 0., 1.]]
Ry = [[cos(ya), 0., -sin(ya)],
[0., 1., 0.],
[sin(ya), 0., cos(ya)]]
Rx = [[1., 0., 0.],
[0., cos(xa), sin(xa)],
[0., -sin(xa), cos(xa)]]
R = np.dot(np.dot(Rz, Ry), Rx)
return R
def main():
plt.hold(True)
plt.set_cmap('gist_earth')
plt.clf()
#gs = gridspec.GridSpec(2,1,height_ratios=[4,1])
#ax = plt.subplot(gs[0], projection='3d')
ax = plt.axes([0.0, 0.2, 1.0, 0.8], projection='3d')
phi, dphi = -45., 3.
theta, dtheta = 70., 10.
flow = 0.
#dist = 'rect'
dist = 'gauss'
axcolor = 'lightgoldenrodyellow'
axphi = plt.axes([0.1, 0.1, 0.45, 0.04], axisbg=axcolor)
axtheta = plt.axes([0.1, 0.15, 0.45, 0.04], axisbg=axcolor)
sphi = Slider(axphi, 'Phi', -180, 180, valinit=phi)
stheta = Slider(axtheta, 'Theta', -180, 180, valinit=theta)
axdphi = plt.axes([0.75, 0.1, 0.15, 0.04], axisbg=axcolor)
axdtheta = plt.axes([0.75, 0.15, 0.15, 0.04], axisbg=axcolor)
sdphi = Slider(axdphi, 'dPhi', 0, 30, valinit=dphi)
sdtheta = Slider(axdtheta, 'dTheta', 0, 30, valinit=dtheta)
axflow = plt.axes([0.1, 0.05, 0.45, 0.04], axisbg=axcolor)
sflow = Slider(axflow, 'Flow', -180, 180, valinit=flow)
axusenew= plt.axes([0.75, 0.05, 0.15, 0.04], axisbg=axcolor)
susenew = CheckButtons(axusenew, ['New'], [True])
def update(val, axis=None):
phi, theta = sphi.val, stheta.val
dphi, dtheta = sdphi.val, sdtheta.val
flow = sflow.val
use_new = susenew.lines[0][0].get_visible()
ax.cla()
draw_sphere(ax)
if use_new:
draw_mesh_new(ax, theta=theta, dtheta=dtheta, phi=phi, dphi=dphi,
flow=flow, radius=11., dist=dist)
else:
draw_mesh_current(ax, theta=theta, dtheta=dtheta, phi=phi, dphi=dphi,
radius=11., dist=dist)
if not axis.startswith('d'):
ax.view_init(elev=90-theta if use_new else theta, azim=phi)
plt.gcf().canvas.draw()
stheta.on_changed(lambda v: update(v,'theta'))
sphi.on_changed(lambda v: update(v, 'phi'))
sdtheta.on_changed(lambda v: update(v, 'dtheta'))
sdphi.on_changed(lambda v: update(v, 'dphi'))
sflow.on_changed(lambda v: update(v, 'dflow'))
susenew.on_clicked(lambda v: update(v, 'use_new'))
update(None, 'phi')
plt.show()
if __name__ == "__main__":
main()
|