| 1 |
#!/usr/bin/env python |
| 2 |
# |
| 3 |
# saxstats.py |
| 4 |
# SAXStats |
| 5 |
# A collection of python functions useful for solution scattering |
| 6 |
# |
| 7 |
# Tested using Anaconda / Python 2.7, 3.7 |
| 8 |
# |
| 9 |
# Author: Thomas D. Grant |
| 10 |
# Email: <[email protected]> |
| 11 |
# Alt Email: <[email protected]> |
| 12 |
# Copyright 2017, 2018, 2019, 2020 The Research Foundation for SUNY |
| 13 |
# |
| 14 |
# Additional authors: |
| 15 |
# Nhan D. Nguyen |
| 16 |
# Jesse Hopkins |
| 17 |
# Andrew Bruno |
| 18 |
# Esther Gallmeier |
| 19 |
# |
| 20 |
# This program is free software: you can redistribute it and/or modify |
| 21 |
# it under the terms of the GNU General Public License as published by |
| 22 |
# the Free Software Foundation, either version 3 of the License, or |
| 23 |
# (at your option) any later version. |
| 24 |
# |
| 25 |
# This program is distributed in the hope that it will be useful, |
| 26 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 |
# GNU General Public License for more details. |
| 29 |
# |
| 30 |
# You should have received a copy of the GNU General Public License |
| 31 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 32 |
# |
| 33 |
|
| 34 |
from __future__ import print_function, division, unicode_literals |
| 35 |
from builtins import object, range, map, zip, str |
| 36 |
from io import open |
| 37 |
|
| 38 |
import sys |
| 39 |
import re |
| 40 |
import os |
| 41 |
import json |
| 42 |
import struct |
| 43 |
import logging |
| 44 |
from functools import partial |
| 45 |
import multiprocessing |
| 46 |
import datetime, time |
| 47 |
|
| 48 |
import numpy as np |
| 49 |
from scipy import ndimage, interpolate, spatial, special, optimize, signal |
| 50 |
from functools import reduce |
| 51 |
|
| 52 |
try: |
| 53 |
import cupy as cp |
| 54 |
except ImportError: |
| 55 |
DENSS_GPU = False |
| 56 |
|
| 57 |
def myfftn(x, DENSS_GPU=False): |
| 58 |
if DENSS_GPU: |
| 59 |
return cp.fft.fftn(x) |
| 60 |
else: |
| 61 |
return np.fft.fftn(x) |
| 62 |
|
| 63 |
def myabs(x, DENSS_GPU=False): |
| 64 |
if DENSS_GPU: |
| 65 |
return cp.abs(x) |
| 66 |
else: |
| 67 |
return np.abs(x) |
| 68 |
|
| 69 |
def mybinmean(x,bins, DENSS_GPU=False): |
| 70 |
if DENSS_GPU: |
| 71 |
xsum = cp.bincount(bins.ravel(), x.ravel()) |
| 72 |
xcount = cp.bincount(bins.ravel()) |
| 73 |
return xsum/xcount |
| 74 |
else: |
| 75 |
xsum = np.bincount(bins.ravel(), x.ravel()) |
| 76 |
xcount = np.bincount(bins.ravel()) |
| 77 |
return xsum/xcount |
| 78 |
|
| 79 |
def myones(x, DENSS_GPU=False): |
| 80 |
if DENSS_GPU: |
| 81 |
return cp.ones(x) |
| 82 |
else: |
| 83 |
return np.ones(x) |
| 84 |
|
| 85 |
def mysqrt(x, DENSS_GPU=False): |
| 86 |
if DENSS_GPU: |
| 87 |
return cp.sqrt(x) |
| 88 |
else: |
| 89 |
return np.sqrt(x) |
| 90 |
|
| 91 |
def mysum(x, DENSS_GPU=False): |
| 92 |
if DENSS_GPU: |
| 93 |
return cp.sum(x) |
| 94 |
else: |
| 95 |
return np.sum(x) |
| 96 |
|
| 97 |
def myifftn(x, DENSS_GPU=False): |
| 98 |
if DENSS_GPU: |
| 99 |
return cp.fft.ifftn(x) |
| 100 |
else: |
| 101 |
return np.fft.ifftn(x) |
| 102 |
|
| 103 |
def myzeros_like(x, DENSS_GPU=False): |
| 104 |
if DENSS_GPU: |
| 105 |
return cp.zeros_like(x) |
| 106 |
else: |
| 107 |
return np.zeros_like(x) |
| 108 |
|
| 109 |
def mystd(x, DENSS_GPU=False): |
| 110 |
if DENSS_GPU: |
| 111 |
return cp.std(x) |
| 112 |
else: |
| 113 |
return np.std(x) |
| 114 |
|
| 115 |
def mymean(x, DENSS_GPU=False): |
| 116 |
if DENSS_GPU: |
| 117 |
return cp.mean(x) |
| 118 |
else: |
| 119 |
return np.mean(x) |
| 120 |
|
| 121 |
def chi2(exp, calc, sig): |
| 122 |
"""Return the chi2 discrepancy between experimental and calculated data""" |
| 123 |
return np.sum(np.square(exp - calc) / np.square(sig)) |
| 124 |
|
| 125 |
def center_rho(rho, centering="com", return_shift=False): |
| 126 |
"""Move electron density map so its center of mass aligns with the center of the grid |
| 127 |
|
| 128 |
centering - which part of the density to center on. By default, center on the |
| 129 |
center of mass ("com"). Can also center on maximum density value ("max"). |
| 130 |
""" |
| 131 |
ne_rho= np.sum((rho)) |
| 132 |
if centering == "max": |
| 133 |
rhocom = np.unravel_index(rho.argmax(), rho.shape) |
| 134 |
else: |
| 135 |
rhocom = np.array(ndimage.measurements.center_of_mass(rho)) |
| 136 |
gridcenter = np.array(rho.shape)/2. |
| 137 |
shift = gridcenter-rhocom |
| 138 |
rho = ndimage.interpolation.shift(rho,shift,order=3,mode='wrap') |
| 139 |
rho = rho*ne_rho/np.sum(rho) |
| 140 |
if return_shift: |
| 141 |
return rho, shift |
| 142 |
else: |
| 143 |
return rho |
| 144 |
|
| 145 |
def rho2rg(rho,side=None,r=None,support=None,dx=None): |
| 146 |
"""Calculate radius of gyration from an electron density map.""" |
| 147 |
if side is None and r is None: |
| 148 |
print("Error: To calculate Rg, must provide either side or r parameters.") |
| 149 |
sys.exit() |
| 150 |
if side is not None and r is None: |
| 151 |
n = rho.shape[0] |
| 152 |
x_ = np.linspace(-side/2.,side/2.,n) |
| 153 |
x,y,z = np.meshgrid(x_,x_,x_,indexing='ij') |
| 154 |
r = np.sqrt(x**2 + y**2 + z**2) |
| 155 |
if support is None: |
| 156 |
support = np.ones_like(rho,dtype=bool) |
| 157 |
if dx is None: |
| 158 |
print("Error: To calculate Rg, must provide dx") |
| 159 |
sys.exit() |
| 160 |
rhocom = (np.array(ndimage.measurements.center_of_mass(rho))-np.array(rho.shape)/2.)*dx |
| 161 |
rg2 = np.sum(r[support]**2*rho[support])/np.sum(rho[support]) |
| 162 |
rg2 = rg2 - np.linalg.norm(rhocom)**2 |
| 163 |
rg = np.sign(rg2)*np.abs(rg2)**0.5 |
| 164 |
return rg |
| 165 |
|
| 166 |
def write_mrc(rho,side,filename="map.mrc"): |
| 167 |
"""Write an MRC formatted electron density map. |
| 168 |
See here: http://www2.mrc-lmb.cam.ac.uk/research/locally-developed-software/image-processing-software/#image |
| 169 |
""" |
| 170 |
xs, ys, zs = rho.shape |
| 171 |
nxstart = -xs//2+1 |
| 172 |
nystart = -ys//2+1 |
| 173 |
nzstart = -zs//2+1 |
| 174 |
side = np.atleast_1d(side) |
| 175 |
if len(side) == 1: |
| 176 |
a,b,c = side, side, side |
| 177 |
elif len(side) == 3: |
| 178 |
a,b,c = side |
| 179 |
else: |
| 180 |
print("Error. Argument 'side' must be float or 3-tuple") |
| 181 |
with open(filename, "wb") as fout: |
| 182 |
# NC, NR, NS, MODE = 2 (image : 32-bit reals) |
| 183 |
fout.write(struct.pack('<iiii', xs, ys, zs, 2)) |
| 184 |
# NCSTART, NRSTART, NSSTART |
| 185 |
fout.write(struct.pack('<iii', nxstart, nystart, nzstart)) |
| 186 |
# MX, MY, MZ |
| 187 |
fout.write(struct.pack('<iii', xs, ys, zs)) |
| 188 |
# X length, Y, length, Z length |
| 189 |
fout.write(struct.pack('<fff', a, b, c)) |
| 190 |
# Alpha, Beta, Gamma |
| 191 |
fout.write(struct.pack('<fff', 90.0, 90.0, 90.0)) |
| 192 |
# MAPC, MAPR, MAPS |
| 193 |
fout.write(struct.pack('<iii', 1, 2, 3)) |
| 194 |
# DMIN, DMAX, DMEAN |
| 195 |
fout.write(struct.pack('<fff', np.min(rho), np.max(rho), np.average(rho))) |
| 196 |
# ISPG, NSYMBT, mlLSKFLG |
| 197 |
fout.write(struct.pack('<iii', 1, 0, 0)) |
| 198 |
# EXTRA |
| 199 |
fout.write(struct.pack('<'+'f'*12, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0)) |
| 200 |
for i in range(0, 12): |
| 201 |
fout.write(struct.pack('<f', 0.0)) |
| 202 |
|
| 203 |
# XORIGIN, YORIGIN, ZORIGIN |
| 204 |
fout.write(struct.pack('<fff', 0.,0.,0. )) #nxstart*(a/xs), nystart*(b/ys), nzstart*(c/zs) )) |
| 205 |
# MAP |
| 206 |
fout.write('MAP '.encode()) |
| 207 |
# MACHST (little endian) |
| 208 |
fout.write(struct.pack('<BBBB', 0x44, 0x41, 0x00, 0x00)) |
| 209 |
# RMS (std) |
| 210 |
fout.write(struct.pack('<f', np.std(rho))) |
| 211 |
# NLABL |
| 212 |
fout.write(struct.pack('<i', 0)) |
| 213 |
# LABEL(20,10) 10 80-character text labels |
| 214 |
for i in range(0, 800): |
| 215 |
fout.write(struct.pack('<B', 0x00)) |
| 216 |
|
| 217 |
# Write out data |
| 218 |
s = struct.pack('=%sf' % rho.size, *rho.flatten('F')) |
| 219 |
fout.write(s) |
| 220 |
|
| 221 |
def read_mrc(filename="map.mrc",returnABC=False): |
| 222 |
""" |
| 223 |
See MRC format at http://bio3d.colorado.edu/imod/doc/mrc_format.txt for offsets |
| 224 |
""" |
| 225 |
with open(filename, 'rb') as fin: |
| 226 |
MRCdata=fin.read() |
| 227 |
nx = struct.unpack_from('<i',MRCdata, 0)[0] |
| 228 |
ny = struct.unpack_from('<i',MRCdata, 4)[0] |
| 229 |
nz = struct.unpack_from('<i',MRCdata, 8)[0] |
| 230 |
|
| 231 |
#side = struct.unpack_from('<f',MRCdata,40)[0] |
| 232 |
a, b, c = struct.unpack_from('<fff',MRCdata,40) |
| 233 |
side = a |
| 234 |
|
| 235 |
fin.seek(1024, os.SEEK_SET) |
| 236 |
rho = np.fromfile(file=fin, dtype=np.dtype(np.float32)).reshape((nx,ny,nz),order='F') |
| 237 |
fin.close() |
| 238 |
if returnABC: |
| 239 |
return rho, (a,b,c) |
| 240 |
else: |
| 241 |
return rho, side |
| 242 |
|
| 243 |
def write_xplor(rho,side,filename="map.xplor"): |
| 244 |
"""Write an XPLOR formatted electron density map.""" |
| 245 |
xs, ys, zs = rho.shape |
| 246 |
title_lines = ['REMARK FILENAME="'+filename+'"','REMARK DATE= '+str(datetime.datetime.today())] |
| 247 |
with open(filename,'w') as f: |
| 248 |
f.write("\n") |
| 249 |
f.write("%8d !NTITLE\n" % len(title_lines)) |
| 250 |
for line in title_lines: |
| 251 |
f.write("%-264s\n" % line) |
| 252 |
#f.write("%8d%8d%8d%8d%8d%8d%8d%8d%8d\n" % (xs,0,xs-1,ys,0,ys-1,zs,0,zs-1)) |
| 253 |
f.write("%8d%8d%8d%8d%8d%8d%8d%8d%8d\n" % (xs,-xs/2+1,xs/2,ys,-ys/2+1,ys/2,zs,-zs/2+1,zs/2)) |
| 254 |
f.write("% -.5E% -.5E% -.5E% -.5E% -.5E% -.5E\n" % (side,side,side,90,90,90)) |
| 255 |
f.write("ZYX\n") |
| 256 |
for k in range(zs): |
| 257 |
f.write("%8s\n" % k) |
| 258 |
for j in range(ys): |
| 259 |
for i in range(xs): |
| 260 |
if (i+j*ys) % 6 == 5: |
| 261 |
f.write("% -.5E\n" % rho[i,j,k]) |
| 262 |
else: |
| 263 |
f.write("% -.5E" % rho[i,j,k]) |
| 264 |
f.write("\n") |
| 265 |
f.write(" -9999\n") |
| 266 |
f.write(" %.4E %.4E" % (np.average(rho), np.std(rho))) |
| 267 |
|
| 268 |
def pad_rho(rho,newshape): |
| 269 |
"""Pad rho with zeros to achieve new shape""" |
| 270 |
a = rho |
| 271 |
a_nx,a_ny,a_nz = a.shape |
| 272 |
b_nx,b_ny,b_nz = newshape |
| 273 |
padx1 = (b_nx-a_nx)//2 |
| 274 |
padx2 = (b_nx-a_nx) - padx1 |
| 275 |
pady1 = (b_ny-a_ny)//2 |
| 276 |
pady2 = (b_ny-a_ny) - pady1 |
| 277 |
padz1 = (b_nz-a_nz)//2 |
| 278 |
padz2 = (b_nz-a_nz) - padz1 |
| 279 |
#np.pad cannot take negative values, i.e. where the array will be cropped |
| 280 |
#however, can instead just use slicing to do the equivalent |
| 281 |
#but first need to identify which pad values are negative |
| 282 |
slcx1, slcx2, slcy1, slcy2, slcz1, slcz2 = None, None, None, None, None, None |
| 283 |
if padx1 < 0: |
| 284 |
slcx1 = -padx1 |
| 285 |
padx1 = 0 |
| 286 |
if padx2 < 0: |
| 287 |
slcx2 = padx2 |
| 288 |
padx2 = 0 |
| 289 |
if pady1 < 0: |
| 290 |
slcy1 = -pady1 |
| 291 |
pady1 = 0 |
| 292 |
if pady2 < 0: |
| 293 |
slcy2 = pady2 |
| 294 |
pady2 = 0 |
| 295 |
if padz1 < 0: |
| 296 |
slcz1 = -padz1 |
| 297 |
padz1 = 0 |
| 298 |
if padz2 < 0: |
| 299 |
slcz2 = padz2 |
| 300 |
padz2 = 0 |
| 301 |
a = np.pad(a,((padx1,padx2),(pady1,pady2),(padz1,padz2)),'constant')[ |
| 302 |
slcx1:slcx2, slcy1:slcy2, slcz1:slcz2] |
| 303 |
return a |
| 304 |
|
| 305 |
def zoom_rho(rho,vx,dx): |
| 306 |
"""Resample rho to have new voxel size. |
| 307 |
|
| 308 |
rho - map to resample (3D array) |
| 309 |
vx - length of voxel of rho, float or tuple of three sides (a,b,c) |
| 310 |
dx - desired voxel size (only float allowed, assumes cubic grid desired) |
| 311 |
""" |
| 312 |
vx = np.atleast_1d(vx) |
| 313 |
if len(vx) == 1: |
| 314 |
vx, vy, vz = vx, vx, vx |
| 315 |
elif len(vx) == 3: |
| 316 |
vx, vy, vz = vx |
| 317 |
else: |
| 318 |
print("Error. Argument 'side' must be float or 3-tuple") |
| 319 |
#zoom factors |
| 320 |
zx, zy, zz = vx/dx, vy/dx, vz/dx |
| 321 |
newrho = ndimage.zoom(rho,(zx, zy, zz),order=1,mode="wrap") |
| 322 |
|
| 323 |
return newrho |
| 324 |
|
| 325 |
def loadOutFile(filename): |
| 326 |
"""Loads a GNOM .out file and returns q, Ireg, sqrt(Ireg), and a |
| 327 |
dictionary of miscellaneous results from GNOM. Taken from the BioXTAS |
| 328 |
RAW software package, used with permission under the GPL license.""" |
| 329 |
|
| 330 |
five_col_fit = re.compile('\s*\d*[.]\d*[+eE-]*\d+\s+-?\d*[.]\d*[+eE-]*\d+\s+\d*[.]\d*[+eE-]*\d+\s+\d*[.]\d*[+eE-]*\d+\s+\d*[.]\d*[+eE-]*\d+\s*$') |
| 331 |
three_col_fit = re.compile('\s*\d*[.]\d*[+eE-]*\d+\s+-?\d*[.]\d*[+eE-]*\d+\s+\d*[.]\d*[+eE-]*\d+\s*$') |
| 332 |
two_col_fit = re.compile('\s*\d*[.]\d*[+eE-]*\d+\s+-?\d*[.]\d*[+eE-]*\d+\s*$') |
| 333 |
|
| 334 |
results_fit = re.compile('\s*Current\s+\d*[.]\d*[+eE-]*\d*\s+\d*[.]\d*[+eE-]*\d*\s+\d*[.]\d*[+eE-]*\d*\s+\d*[.]\d*[+eE-]*\d*\s+\d*[.]\d*[+eE-]*\d*\s+\d*[.]\d*[+eE-]*\d*\s*\d*[.]?\d*[+eE-]*\d*\s*$') |
| 335 |
|
| 336 |
te_fit = re.compile('\s*Total\s+[Ee]stimate\s*:\s+\d*[.]\d+\s*\(?[A-Za-z\s]+\)?\s*$') |
| 337 |
te_num_fit = re.compile('\d*[.]\d+') |
| 338 |
te_quality_fit = re.compile('[Aa][A-Za-z\s]+\)?\s*$') |
| 339 |
|
| 340 |
p_rg_fit = re.compile('\s*Real\s+space\s*\:?\s*Rg\:?\s*\=?\s*\d*[.]\d+[+eE-]*\d*\s*\+-\s*\d*[.]\d+[+eE-]*\d*') |
| 341 |
q_rg_fit = re.compile('\s*Reciprocal\s+space\s*\:?\s*Rg\:?\s*\=?\s*\d*[.]\d+[+eE-]*\d*\s*') |
| 342 |
|
| 343 |
p_i0_fit = re.compile('\s*Real\s+space\s*\:?[A-Za-z0-9\s\.,+-=]*\(0\)\:?\s*\=?\s*\d*[.]\d+[+eE-]*\d*\s*\+-\s*\d*[.]\d+[+eE-]*\d*') |
| 344 |
q_i0_fit = re.compile('\s*Reciprocal\s+space\s*\:?[A-Za-z0-9\s\.,+-=]*\(0\)\:?\s*\=?\s*\d*[.]\d+[+eE-]*\d*\s*') |
| 345 |
|
| 346 |
qfull = [] |
| 347 |
qshort = [] |
| 348 |
Jexp = [] |
| 349 |
Jerr = [] |
| 350 |
Jreg = [] |
| 351 |
Ireg = [] |
| 352 |
|
| 353 |
R = [] |
| 354 |
P = [] |
| 355 |
Perr = [] |
| 356 |
|
| 357 |
outfile = [] |
| 358 |
|
| 359 |
#In case it returns NaN for either value, and they don't get picked up in the regular expression |
| 360 |
q_rg=None #Reciprocal space Rg |
| 361 |
q_i0=None #Reciprocal space I0 |
| 362 |
|
| 363 |
|
| 364 |
with open(filename) as f: |
| 365 |
for line in f: |
| 366 |
twocol_match = two_col_fit.match(line) |
| 367 |
threecol_match = three_col_fit.match(line) |
| 368 |
fivecol_match = five_col_fit.match(line) |
| 369 |
results_match = results_fit.match(line) |
| 370 |
te_match = te_fit.match(line) |
| 371 |
p_rg_match = p_rg_fit.match(line) |
| 372 |
q_rg_match = q_rg_fit.match(line) |
| 373 |
p_i0_match = p_i0_fit.match(line) |
| 374 |
q_i0_match = q_i0_fit.match(line) |
| 375 |
|
| 376 |
outfile.append(line) |
| 377 |
|
| 378 |
if twocol_match: |
| 379 |
# print line |
| 380 |
found = twocol_match.group().split() |
| 381 |
|
| 382 |
qfull.append(float(found[0])) |
| 383 |
Ireg.append(float(found[1])) |
| 384 |
|
| 385 |
elif threecol_match: |
| 386 |
#print line |
| 387 |
found = threecol_match.group().split() |
| 388 |
|
| 389 |
R.append(float(found[0])) |
| 390 |
P.append(float(found[1])) |
| 391 |
Perr.append(float(found[2])) |
| 392 |
|
| 393 |
elif fivecol_match: |
| 394 |
#print line |
| 395 |
found = fivecol_match.group().split() |
| 396 |
|
| 397 |
qfull.append(float(found[0])) |
| 398 |
qshort.append(float(found[0])) |
| 399 |
Jexp.append(float(found[1])) |
| 400 |
Jerr.append(float(found[2])) |
| 401 |
Jreg.append(float(found[3])) |
| 402 |
Ireg.append(float(found[4])) |
| 403 |
|
| 404 |
elif results_match: |
| 405 |
found = results_match.group().split() |
| 406 |
Actual_DISCRP = float(found[1]) |
| 407 |
Actual_OSCILL = float(found[2]) |
| 408 |
Actual_STABIL = float(found[3]) |
| 409 |
Actual_SYSDEV = float(found[4]) |
| 410 |
Actual_POSITV = float(found[5]) |
| 411 |
Actual_VALCEN = float(found[6]) |
| 412 |
|
| 413 |
if len(found) == 8: |
| 414 |
Actual_SMOOTH = float(found[7]) |
| 415 |
else: |
| 416 |
Actual_SMOOTH = -1 |
| 417 |
|
| 418 |
elif te_match: |
| 419 |
te_num_search = te_num_fit.search(line) |
| 420 |
te_quality_search = te_quality_fit.search(line) |
| 421 |
|
| 422 |
TE_out = float(te_num_search.group().strip()) |
| 423 |
quality = te_quality_search.group().strip().rstrip(')').strip() |
| 424 |
|
| 425 |
|
| 426 |
if p_rg_match: |
| 427 |
found = p_rg_match.group().split() |
| 428 |
try: |
| 429 |
rg = float(found[-3]) |
| 430 |
except: |
| 431 |
rg = float(found[-2]) |
| 432 |
try: |
| 433 |
rger = float(found[-1]) |
| 434 |
except: |
| 435 |
rger = float(found[-1].strip('+-')) |
| 436 |
|
| 437 |
elif q_rg_match: |
| 438 |
found = q_rg_match.group().split() |
| 439 |
q_rg = float(found[-1]) |
| 440 |
|
| 441 |
|
| 442 |
if p_i0_match: |
| 443 |
found = p_i0_match.group().split() |
| 444 |
i0 = float(found[-3]) |
| 445 |
i0er = float(found[-1]) |
| 446 |
|
| 447 |
elif q_i0_match: |
| 448 |
found = q_i0_match.group().split() |
| 449 |
q_i0 = float(found[-1]) |
| 450 |
|
| 451 |
name = os.path.basename(filename) |
| 452 |
|
| 453 |
chisq = np.sum(np.square(np.array(Jexp)-np.array(Jreg))/np.square(Jerr))/(len(Jexp)-1) #DOF normalied chi squared |
| 454 |
|
| 455 |
results = { 'dmax' : R[-1], #Dmax |
| 456 |
'TE' : TE_out, #Total estimate |
| 457 |
'rg' : rg, #Real space Rg |
| 458 |
'rger' : rger, #Real space rg error |
| 459 |
'i0' : i0, #Real space I0 |
| 460 |
'i0er' : i0er, #Real space I0 error |
| 461 |
'q_rg' : q_rg, #Reciprocal space Rg |
| 462 |
'q_i0' : q_i0, #Reciprocal space I0 |
| 463 |
'quality' : quality, #Quality of GNOM out file |
| 464 |
'discrp' : Actual_DISCRP,#DISCRIP, kind of chi squared (normalized by number of points, with a regularization parameter thing thrown in) |
| 465 |
'oscil' : Actual_OSCILL,#Oscillation of solution |
| 466 |
'stabil' : Actual_STABIL,#Stability of solution |
| 467 |
'sysdev' : Actual_SYSDEV,#Systematic deviation of solution |
| 468 |
'positv' : Actual_POSITV,#Relative norm of the positive part of P(r) |
| 469 |
'valcen' : Actual_VALCEN,#Validity of the chosen interval in real space |
| 470 |
'smooth' : Actual_SMOOTH,#Smoothness of the chosen interval? -1 indicates no real value, for versions of GNOM < 5.0 (ATSAS <2.8) |
| 471 |
'filename' : name, #GNOM filename |
| 472 |
'algorithm' : 'GNOM', #Lets us know what algorithm was used to find the IFT |
| 473 |
'chisq' : chisq #Actual chi squared value |
| 474 |
} |
| 475 |
|
| 476 |
Jerr = np.array(Jerr) |
| 477 |
prepend = np.zeros((len(Ireg)-len(Jerr))) |
| 478 |
prepend += np.mean(Jerr[:10]) |
| 479 |
Jerr = np.concatenate((prepend,Jerr)) |
| 480 |
|
| 481 |
return np.array(qfull), np.array(Ireg), Jerr, results |
| 482 |
|
| 483 |
def loadDatFile(filename): |
| 484 |
''' Loads a Primus .dat format file. Taken from the BioXTAS RAW software package, |
| 485 |
used with permission under the GPL license.''' |
| 486 |
|
| 487 |
iq_pattern = re.compile('\s*\d*[.]\d*[+eE-]*\d+\s+-?\d*[.]\d*[+eE-]*\d+\s+\d*[.]\d*[+eE-]*\d+\s*') |
| 488 |
|
| 489 |
i = [] |
| 490 |
q = [] |
| 491 |
err = [] |
| 492 |
|
| 493 |
with open(filename) as f: |
| 494 |
lines = f.readlines() |
| 495 |
|
| 496 |
comment = '' |
| 497 |
line = lines[0] |
| 498 |
j=0 |
| 499 |
while line.split() and line.split()[0].strip()[0] == '#': |
| 500 |
comment = comment+line |
| 501 |
j = j+1 |
| 502 |
line = lines[j] |
| 503 |
|
| 504 |
fileHeader = {'comment':comment} |
| 505 |
parameters = {'filename' : os.path.split(filename)[1], |
| 506 |
'counters' : fileHeader} |
| 507 |
|
| 508 |
if comment.find('model_intensity') > -1: |
| 509 |
#FoXS file with a fit! has four data columns |
| 510 |
is_foxs_fit=True |
| 511 |
imodel = [] |
| 512 |
else: |
| 513 |
is_foxs_fit = False |
| 514 |
|
| 515 |
for line in lines: |
| 516 |
iq_match = iq_pattern.match(line) |
| 517 |
|
| 518 |
if iq_match: |
| 519 |
if not is_foxs_fit: |
| 520 |
found = iq_match.group().split() |
| 521 |
q.append(float(found[0])) |
| 522 |
i.append(float(found[1])) |
| 523 |
err.append(float(found[2])) |
| 524 |
else: |
| 525 |
found = line.split() |
| 526 |
q.append(float(found[0])) |
| 527 |
i.append(float(found[1])) |
| 528 |
imodel.append(float(found[2])) |
| 529 |
err.append(float(found[3])) |
| 530 |
|
| 531 |
i = np.array(i) |
| 532 |
q = np.array(q) |
| 533 |
err = np.array(err) |
| 534 |
|
| 535 |
if is_foxs_fit: |
| 536 |
i = np.array(imodel) |
| 537 |
|
| 538 |
#Check to see if there is any header from RAW, and if so get that. |
| 539 |
header = [] |
| 540 |
for j in range(len(lines)): |
| 541 |
if '### HEADER:' in lines[j]: |
| 542 |
header = lines[j+1:] |
| 543 |
|
| 544 |
hdict = None |
| 545 |
results = {} |
| 546 |
|
| 547 |
if len(header)>0: |
| 548 |
hdr_str = '' |
| 549 |
for each_line in header: |
| 550 |
hdr_str=hdr_str+each_line |
| 551 |
try: |
| 552 |
hdict = dict(json.loads(hdr_str)) |
| 553 |
except Exception: |
| 554 |
hdict = {} |
| 555 |
|
| 556 |
if hdict: |
| 557 |
for each in hdict.keys(): |
| 558 |
if each != 'filename': |
| 559 |
results[each] = hdict[each] |
| 560 |
|
| 561 |
if 'analysis' in results: |
| 562 |
if 'GNOM' in results['analysis']: |
| 563 |
results = results['analysis']['GNOM'] |
| 564 |
|
| 565 |
return q, i, err, results |
| 566 |
|
| 567 |
def loadProfile(fname, units="a"): |
| 568 |
"""Determines which loading function to run, and then runs it.""" |
| 569 |
|
| 570 |
if os.path.splitext(fname)[1] == '.out': |
| 571 |
q, I, Ierr, results = loadOutFile(fname) |
| 572 |
isout = True |
| 573 |
else: |
| 574 |
q, I, Ierr, results = loadDatFile(fname) |
| 575 |
isout = False |
| 576 |
|
| 577 |
keys = {key.lower().strip().translate(str.maketrans('','', '_ ')): key for key in list(results.keys())} |
| 578 |
|
| 579 |
if 'dmax' in keys: |
| 580 |
dmax = float(results[keys['dmax']]) |
| 581 |
else: |
| 582 |
dmax = -1. |
| 583 |
|
| 584 |
if units == "nm": |
| 585 |
#DENSS assumes 1/angstrom, so convert from 1/nm to 1/angstrom |
| 586 |
q /= 10 |
| 587 |
dmax *= 10 |
| 588 |
print("Angular units converted from 1/nm to 1/angstrom") |
| 589 |
|
| 590 |
return q, I, Ierr, dmax, isout |
| 591 |
|
| 592 |
def denss(q, I, sigq, dmax, ne=None, voxel=5., oversampling=3., limit_dmax=False, |
| 593 |
limit_dmax_steps=[500], recenter=True, recenter_steps=None, |
| 594 |
recenter_mode="com", positivity=True, extrapolate=True, output="map", |
| 595 |
steps=None, seed=None, minimum_density=None, maximum_density=None, |
| 596 |
flatten_low_density=True, rho_start=None, add_noise=None, shrinkwrap=True, |
| 597 |
shrinkwrap_sigma_start=3, shrinkwrap_sigma_end=1.5, |
| 598 |
shrinkwrap_sigma_decay=0.99, shrinkwrap_threshold_fraction=0.2, |
| 599 |
shrinkwrap_iter=20, shrinkwrap_minstep=100, chi_end_fraction=0.01, |
| 600 |
write_xplor_format=False, write_freq=100, enforce_connectivity=True, |
| 601 |
enforce_connectivity_steps=[500], cutout=True, quiet=False, ncs=0, |
| 602 |
ncs_steps=[500],ncs_axis=1, abort_event=None, my_logger=logging.getLogger(), |
| 603 |
path='.', gui=False, DENSS_GPU=False): |
| 604 |
"""Calculate electron density from scattering data.""" |
| 605 |
if abort_event is not None: |
| 606 |
if abort_event.is_set(): |
| 607 |
my_logger.info('Aborted!') |
| 608 |
return [] |
| 609 |
|
| 610 |
fprefix = os.path.join(path, output) |
| 611 |
|
| 612 |
D = dmax |
| 613 |
|
| 614 |
my_logger.info('q range of input data: %3.3f < q < %3.3f', q.min(), q.max()) |
| 615 |
my_logger.info('Maximum dimension: %3.3f', D) |
| 616 |
my_logger.info('Sampling ratio: %3.3f', oversampling) |
| 617 |
my_logger.info('Requested real space voxel size: %3.3f', voxel) |
| 618 |
my_logger.info('Number of electrons: %3.3f', ne) |
| 619 |
my_logger.info('Limit Dmax: %s', limit_dmax) |
| 620 |
my_logger.info('Limit Dmax Steps: %s', limit_dmax_steps) |
| 621 |
my_logger.info('Recenter: %s', recenter) |
| 622 |
my_logger.info('Recenter Steps: %s', recenter_steps) |
| 623 |
my_logger.info('Recenter Mode: %s', recenter_mode) |
| 624 |
my_logger.info('NCS: %s', ncs) |
| 625 |
my_logger.info('NCS Steps: %s', ncs_steps) |
| 626 |
my_logger.info('NCS Axis: %s', ncs_axis) |
| 627 |
my_logger.info('Positivity: %s', positivity) |
| 628 |
my_logger.info('Minimum Density: %s', minimum_density) |
| 629 |
my_logger.info('Maximum Density: %s', maximum_density) |
| 630 |
my_logger.info('Extrapolate high q: %s', extrapolate) |
| 631 |
my_logger.info('Shrinkwrap: %s', shrinkwrap) |
| 632 |
my_logger.info('Shrinkwrap sigma start: %s', shrinkwrap_sigma_start) |
| 633 |
my_logger.info('Shrinkwrap sigma end: %s', shrinkwrap_sigma_end) |
| 634 |
my_logger.info('Shrinkwrap sigma decay: %s', shrinkwrap_sigma_decay) |
| 635 |
my_logger.info('Shrinkwrap threshold fraction: %s', shrinkwrap_threshold_fraction) |
| 636 |
my_logger.info('Shrinkwrap iterations: %s', shrinkwrap_iter) |
| 637 |
my_logger.info('Shrinkwrap starting step: %s', shrinkwrap_minstep) |
| 638 |
my_logger.info('Enforce connectivity: %s', enforce_connectivity) |
| 639 |
my_logger.info('Enforce connectivity steps: %s', enforce_connectivity_steps) |
| 640 |
my_logger.info('Chi2 end fraction: %3.3e', chi_end_fraction) |
| 641 |
|
| 642 |
#Initialize variables |
| 643 |
|
| 644 |
side = oversampling*D |
| 645 |
halfside = side/2 |
| 646 |
|
| 647 |
n = int(side/voxel) |
| 648 |
#want n to be even for speed/memory optimization with the FFT, ideally a power of 2, but wont enforce that |
| 649 |
if n%2==1: |
| 650 |
n += 1 |
| 651 |
#store n for later use if needed |
| 652 |
nbox = n |
| 653 |
|
| 654 |
dx = side/n |
| 655 |
dV = dx**3 |
| 656 |
V = side**3 |
| 657 |
x_ = np.linspace(-halfside,halfside,n) |
| 658 |
x,y,z = np.meshgrid(x_,x_,x_,indexing='ij') |
| 659 |
r = np.sqrt(x**2 + y**2 + z**2) |
| 660 |
|
| 661 |
df = 1/side |
| 662 |
qx_ = np.fft.fftfreq(x_.size)*n*df*2*np.pi |
| 663 |
qx, qy, qz = np.meshgrid(qx_,qx_,qx_,indexing='ij') |
| 664 |
qr = np.sqrt(qx**2+qy**2+qz**2) |
| 665 |
qmax = np.max(qr) |
| 666 |
qstep = np.min(qr[qr>0]) - 1e-8 #subtract a tiny bit to deal with floating point error |
| 667 |
nbins = int(qmax/qstep) |
| 668 |
qbins = np.linspace(0,nbins*qstep,nbins+1) |
| 669 |
|
| 670 |
#create modified qbins and put qbins in center of bin rather than at left edge of bin. |
| 671 |
qbinsc = np.copy(qbins) |
| 672 |
qbinsc[1:] += qstep/2. |
| 673 |
|
| 674 |
#create an array labeling each voxel according to which qbin it belongs |
| 675 |
qbin_labels = np.searchsorted(qbins,qr,"right") |
| 676 |
qbin_labels -= 1 |
| 677 |
|
| 678 |
#allow for any range of q data |
| 679 |
qdata = qbinsc[np.where( (qbinsc>=q.min()) & (qbinsc<=q.max()) )] |
| 680 |
Idata = np.interp(qdata,q,I) |
| 681 |
if extrapolate: |
| 682 |
qextend = qbinsc[qbinsc>=qdata.max()] |
| 683 |
Iextend = qextend**-4 |
| 684 |
Iextend = Iextend/Iextend[0] * Idata[-1] |
| 685 |
qdata = np.concatenate((qdata,qextend[1:])) |
| 686 |
Idata = np.concatenate((Idata,Iextend[1:])) |
| 687 |
|
| 688 |
#create list of qbin indices just in region of data for later F scaling |
| 689 |
qbin_args = np.in1d(qbinsc,qdata,assume_unique=True) |
| 690 |
sigqdata = np.interp(qdata,q,sigq) |
| 691 |
|
| 692 |
scale_factor = ne**2 / Idata[0] |
| 693 |
Idata *= scale_factor |
| 694 |
sigqdata *= scale_factor |
| 695 |
I *= scale_factor |
| 696 |
sigq *= scale_factor |
| 697 |
|
| 698 |
if steps == 'None' or steps is None or np.int(steps) < 1: |
| 699 |
stepsarr = np.concatenate((enforce_connectivity_steps,[shrinkwrap_minstep])) |
| 700 |
maxec = np.max(stepsarr) |
| 701 |
steps = int(shrinkwrap_iter * (np.log(shrinkwrap_sigma_end/shrinkwrap_sigma_start)/np.log(shrinkwrap_sigma_decay)) + maxec) |
| 702 |
#add enough steps for convergence after shrinkwrap is finished |
| 703 |
#something like 7000 seems reasonable, likely will finish before that on its own |
| 704 |
#then just make a round number when using defaults |
| 705 |
steps += 7621 |
| 706 |
else: |
| 707 |
steps = np.int(steps) |
| 708 |
|
| 709 |
Imean = np.zeros((steps+1,len(qbins))) |
| 710 |
chi = np.zeros((steps+1)) |
| 711 |
rg = np.zeros((steps+1)) |
| 712 |
supportV = np.zeros((steps+1)) |
| 713 |
support = np.ones(x.shape,dtype=bool) |
| 714 |
|
| 715 |
if seed is None: |
| 716 |
#Have to reset the random seed to get a random in different from other processes |
| 717 |
prng = np.random.RandomState() |
| 718 |
seed = prng.randint(2**31-1) |
| 719 |
else: |
| 720 |
seed = int(seed) |
| 721 |
|
| 722 |
prng = np.random.RandomState(seed) |
| 723 |
|
| 724 |
if rho_start is not None: |
| 725 |
rho = rho_start |
| 726 |
if add_noise is not None: |
| 727 |
rho += prng.random_sample(size=x.shape)*add_noise |
| 728 |
else: |
| 729 |
rho = prng.random_sample(size=x.shape) #- 0.5 |
| 730 |
|
| 731 |
sigma = shrinkwrap_sigma_start |
| 732 |
#convert density values to absolute number of electrons |
| 733 |
#since FFT and rho given in electrons, not density, until converted at the end |
| 734 |
rho_min = minimum_density |
| 735 |
rho_max = maximum_density |
| 736 |
if rho_min is not None: |
| 737 |
rho_min *= dV |
| 738 |
#print rho_min |
| 739 |
if rho_max is not None: |
| 740 |
rho_max *= dV |
| 741 |
#print rho_max |
| 742 |
|
| 743 |
my_logger.info('Maximum number of steps: %i', steps) |
| 744 |
my_logger.info('Grid size (voxels): %i x %i x %i', n, n, n) |
| 745 |
my_logger.info('Real space box width (angstroms): %3.3f', side) |
| 746 |
my_logger.info('Real space box range (angstroms): %3.3f < x < %3.3f', x_.min(), x_.max()) |
| 747 |
my_logger.info('Real space box volume (angstroms^3): %3.3f', V) |
| 748 |
my_logger.info('Real space voxel size (angstroms): %3.3f', dx) |
| 749 |
my_logger.info('Real space voxel volume (angstroms^3): %3.3f', dV) |
| 750 |
my_logger.info('Reciprocal space box width (angstroms^(-1)): %3.3f', qx_.max()-qx_.min()) |
| 751 |
my_logger.info('Reciprocal space box range (angstroms^(-1)): %3.3f < qx < %3.3f', qx_.min(), qx_.max()) |
| 752 |
my_logger.info('Maximum q vector (diagonal) (angstroms^(-1)): %3.3f', qr.max()) |
| 753 |
my_logger.info('Number of q shells: %i', nbins) |
| 754 |
my_logger.info('Width of q shells (angstroms^(-1)): %3.3f', qstep) |
| 755 |
my_logger.info('Random seed: %i', seed) |
| 756 |
if not quiet: |
| 757 |
if gui: |
| 758 |
my_logger.info("\n Step Chi2 Rg Support Volume") |
| 759 |
my_logger.info(" ----- --------- ------- --------------") |
| 760 |
else: |
| 761 |
print("\n Step Chi2 Rg Support Volume") |
| 762 |
print(" ----- --------- ------- --------------") |
| 763 |
|
| 764 |
if DENSS_GPU: |
| 765 |
rho = cp.array(rho) |
| 766 |
qbin_labels = cp.array(qbin_labels) |
| 767 |
qbins = cp.array(qbins) |
| 768 |
Idata = cp.array(Idata) |
| 769 |
qbin_args = cp.array(qbin_args) |
| 770 |
sigqdata = cp.array(sigqdata) |
| 771 |
support = cp.array(support) |
| 772 |
chi = cp.array(chi) |
| 773 |
supportV = cp.array(supportV) |
| 774 |
Imean = cp.array(Imean) |
| 775 |
|
| 776 |
for j in range(steps): |
| 777 |
if abort_event is not None: |
| 778 |
if abort_event.is_set(): |
| 779 |
my_logger.info('Aborted!') |
| 780 |
return [] |
| 781 |
|
| 782 |
F = myfftn(rho, DENSS_GPU=DENSS_GPU) |
| 783 |
|
| 784 |
#APPLY RECIPROCAL SPACE RESTRAINTS |
| 785 |
#calculate spherical average of intensities from 3D Fs |
| 786 |
I3D = myabs(F, DENSS_GPU=DENSS_GPU)**2 |
| 787 |
Imean = mybinmean(I3D, qbin_labels, DENSS_GPU=DENSS_GPU) |
| 788 |
""" |
| 789 |
if j==0: |
| 790 |
np.savetxt(fprefix+'_step0_saxs.dat',np.vstack((qbinsc,Imean[j],Imean[j]*.05)).T,delimiter=" ",fmt="%.5e") |
| 791 |
write_mrc(rho,side,fprefix+"_step"+str(j)+".mrc") |
| 792 |
""" |
| 793 |
|
| 794 |
#scale Fs to match data |
| 795 |
#factors = myones((len(qbins))) |
| 796 |
factors = mysqrt(Idata/Imean, DENSS_GPU=DENSS_GPU) |
| 797 |
F *= factors[qbin_labels] |
| 798 |
|
| 799 |
chi[j] = mysum(((Imean-Idata)/sigqdata)**2, DENSS_GPU=DENSS_GPU)/Imean.size |
| 800 |
#APPLY REAL SPACE RESTRAINTS |
| 801 |
rhoprime = myifftn(F, DENSS_GPU=DENSS_GPU) |
| 802 |
rhoprime = rhoprime.real |
| 803 |
|
| 804 |
if not DENSS_GPU and j%write_freq == 0: |
| 805 |
if write_xplor_format: |
| 806 |
write_xplor(rhoprime/dV, side, fprefix+"_current.xplor") |
| 807 |
write_mrc(rhoprime/dV, side, fprefix+"_current.mrc") |
| 808 |
|
| 809 |
if DENSS_GPU: |
| 810 |
#havent yet updated rho2rg to work with cupy |
| 811 |
rg[j] = 1.0 |
| 812 |
else: |
| 813 |
rg[j] = rho2rg(rhoprime,r=r,support=support,dx=dx) |
| 814 |
newrho = myzeros_like(rho, DENSS_GPU=DENSS_GPU) |
| 815 |
|
| 816 |
#Error Reduction |
| 817 |
newrho[support] = rhoprime[support] |
| 818 |
newrho[~support] = 0.0 |
| 819 |
|
| 820 |
#enforce positivity by making all negative density points zero. |
| 821 |
if positivity: |
| 822 |
netmp = mysum(newrho, DENSS_GPU=DENSS_GPU) |
| 823 |
newrho[newrho<0] = 0.0 |
| 824 |
if mysum(newrho, DENSS_GPU=DENSS_GPU) != 0: |
| 825 |
newrho *= netmp / mysum(newrho, DENSS_GPU=DENSS_GPU) |
| 826 |
|
| 827 |
#apply non-crystallographic symmetry averaging |
| 828 |
if ncs != 0 and j in ncs_steps: |
| 829 |
if DENSS_GPU: |
| 830 |
newrho = cp.asnumpy(newrho) |
| 831 |
newrho = align2xyz(newrho) |
| 832 |
if DENSS_GPU: |
| 833 |
newrho = cp.array(newrho) |
| 834 |
|
| 835 |
if ncs != 0 and j in [stepi+1 for stepi in ncs_steps]: |
| 836 |
if DENSS_GPU: |
| 837 |
newrho = cp.asnumpy(newrho) |
| 838 |
degrees = 360./ncs |
| 839 |
if ncs_axis == 1: axes=(1,2) |
| 840 |
if ncs_axis == 2: axes=(0,2) |
| 841 |
if ncs_axis == 3: axes=(0,1) |
| 842 |
newrhosym = np.zeros_like(newrho) |
| 843 |
for nrot in range(0,ncs+1): |
| 844 |
newrhosym += ndimage.rotate(newrho,degrees*nrot,axes=axes,reshape=False) |
| 845 |
newrho = newrhosym/ncs |
| 846 |
if DENSS_GPU: |
| 847 |
newrho = cp.array(newrho) |
| 848 |
|
| 849 |
#update support using shrinkwrap method |
| 850 |
if recenter and j in recenter_steps: |
| 851 |
if DENSS_GPU: |
| 852 |
newrho = cp.asnumpy(newrho) |
| 853 |
support = cp.asnumpy(support) |
| 854 |
if recenter_mode == "max": |
| 855 |
rhocom = np.unravel_index(newrho.argmax(), newrho.shape) |
| 856 |
else: |
| 857 |
rhocom = np.array(ndimage.measurements.center_of_mass(newrho)) |
| 858 |
gridcenter = np.array(newrho.shape)/2. |
| 859 |
shift = gridcenter-rhocom |
| 860 |
shift = shift.astype(int) |
| 861 |
newrho = np.roll(np.roll(np.roll(newrho, shift[0], axis=0), shift[1], axis=1), shift[2], axis=2) |
| 862 |
support = np.roll(np.roll(np.roll(support, shift[0], axis=0), shift[1], axis=1), shift[2], axis=2) |
| 863 |
if DENSS_GPU: |
| 864 |
newrho = cp.array(newrho) |
| 865 |
support = cp.array(support) |
| 866 |
|
| 867 |
if shrinkwrap and j >= shrinkwrap_minstep and j%shrinkwrap_iter==1: |
| 868 |
if DENSS_GPU: |
| 869 |
newrho = cp.asnumpy(newrho) |
| 870 |
|
| 871 |
if recenter_mode == "max": |
| 872 |
rhocom = np.unravel_index(newrho.argmax(), newrho.shape) |
| 873 |
else: |
| 874 |
rhocom = np.array(ndimage.measurements.center_of_mass(newrho)) |
| 875 |
gridcenter = np.array(newrho.shape)/2. |
| 876 |
shift = gridcenter-rhocom |
| 877 |
shift = shift.astype(int) |
| 878 |
newrho = np.roll(np.roll(np.roll(newrho, shift[0], axis=0), shift[1], axis=1), shift[2], axis=2) |
| 879 |
|
| 880 |
if j>500: |
| 881 |
tmp = np.abs(newrho) |
| 882 |
else: |
| 883 |
tmp = newrho |
| 884 |
|
| 885 |
rho_blurred = ndimage.filters.gaussian_filter(tmp,sigma=sigma,mode='wrap') |
| 886 |
support = np.zeros(newrho.shape,dtype=bool) |
| 887 |
support[rho_blurred >= shrinkwrap_threshold_fraction*rho_blurred.max()] = True |
| 888 |
|
| 889 |
if sigma > shrinkwrap_sigma_end: |
| 890 |
sigma = shrinkwrap_sigma_decay*sigma |
| 891 |
|
| 892 |
if DENSS_GPU: |
| 893 |
newrho = cp.array(newrho) |
| 894 |
support = cp.array(support) |
| 895 |
|
| 896 |
if enforce_connectivity and j in enforce_connectivity_steps: |
| 897 |
if DENSS_GPU: |
| 898 |
newrho = cp.asnumpy(newrho) |
| 899 |
if recenter_mode == "max": |
| 900 |
rhocom = np.unravel_index(newrho.argmax(), newrho.shape) |
| 901 |
else: |
| 902 |
rhocom = np.array(ndimage.measurements.center_of_mass(newrho)) |
| 903 |
|
| 904 |
gridcenter = np.array(newrho.shape)/2. |
| 905 |
shift = gridcenter-rhocom |
| 906 |
shift = shift.astype(int) |
| 907 |
newrho = np.roll(np.roll(np.roll(newrho, shift[0], axis=0), shift[1], axis=1), shift[2], axis=2) |
| 908 |
|
| 909 |
#first run shrinkwrap to define the features |
| 910 |
tmp = np.abs(newrho) |
| 911 |
rho_blurred = ndimage.filters.gaussian_filter(tmp,sigma=sigma,mode='wrap') |
| 912 |
support = np.zeros(newrho.shape,dtype=bool) |
| 913 |
support[rho_blurred >= shrinkwrap_threshold_fraction*rho_blurred.max()] = True |
| 914 |
|
| 915 |
#label the support into separate segments based on a 3x3x3 grid |
| 916 |
struct = ndimage.generate_binary_structure(3, 3) |
| 917 |
labeled_support, num_features = ndimage.label(support, structure=struct) |
| 918 |
sums = np.zeros((num_features)) |
| 919 |
if not quiet: |
| 920 |
if not gui: |
| 921 |
print(num_features) |
| 922 |
|
| 923 |
#find the feature with the greatest number of electrons |
| 924 |
for feature in range(num_features+1): |
| 925 |
sums[feature-1] = np.sum(newrho[labeled_support==feature]) |
| 926 |
big_feature = np.argmax(sums)+1 |
| 927 |
|
| 928 |
#remove features from the support that are not the primary feature |
| 929 |
support[labeled_support != big_feature] = False |
| 930 |
newrho[~support] = 0 |
| 931 |
|
| 932 |
#reset the support to be the entire grid again |
| 933 |
support = np.ones(newrho.shape,dtype=bool) |
| 934 |
|
| 935 |
if DENSS_GPU: |
| 936 |
newrho = cp.array(newrho) |
| 937 |
support = cp.array(support) |
| 938 |
|
| 939 |
supportV[j] = mysum(support, DENSS_GPU=DENSS_GPU)*dV |
| 940 |
|
| 941 |
if not quiet: |
| 942 |
if gui: |
| 943 |
my_logger.info("% 5i % 4.2e % 3.2f % 5i ", j, chi[j], rg[j], supportV[j]) |
| 944 |
else: |
| 945 |
sys.stdout.write("\r% 5i % 4.2e % 3.2f % 5i " % (j, chi[j], rg[j], supportV[j])) |
| 946 |
sys.stdout.flush() |
| 947 |
|
| 948 |
|
| 949 |
if j > 101 + shrinkwrap_minstep and mystd(chi[j-100:j], DENSS_GPU=DENSS_GPU) < chi_end_fraction * mymean(chi[j-100:j], DENSS_GPU=DENSS_GPU): |
| 950 |
break |
| 951 |
|
| 952 |
rho = newrho |
| 953 |
|
| 954 |
#convert back to numpy outside of for loop |
| 955 |
if DENSS_GPU: |
| 956 |
rho = cp.asnumpy(rho) |
| 957 |
qbin_labels = cp.asnumpy(qbin_labels) |
| 958 |
qbin_args = cp.asnumpy(qbin_args) |
| 959 |
sigqdata = cp.asnumpy(sigqdata) |
| 960 |
Imean = cp.asnumpy(Imean) |
| 961 |
chi = cp.asnumpy(chi) |
| 962 |
qbins = cp.asnumpy(qbins) |
| 963 |
Idata = cp.asnumpy(Idata) |
| 964 |
support = cp.asnumpy(support) |
| 965 |
supportV = cp.asnumpy(supportV) |
| 966 |
Idata = cp.asnumpy(Idata) |
| 967 |
|
| 968 |
|
| 969 |
F = np.fft.fftn(rho) |
| 970 |
#calculate spherical average intensity from 3D Fs |
| 971 |
Imean = ndimage.mean(np.abs(F)**2, labels=qbin_labels, index=np.arange(0,qbin_labels.max()+1)) |
| 972 |
#chi[j+1] = np.sum(((Imean[j+1,qbin_args]-Idata)/sigqdata)**2)/qbin_args.size |
| 973 |
|
| 974 |
#scale Fs to match data |
| 975 |
#factors = np.ones((len(qbins))) |
| 976 |
factors = np.sqrt(Idata/Imean) |
| 977 |
F *= factors[qbin_labels] |
| 978 |
rho = np.fft.ifftn(F,rho.shape) |
| 979 |
rho = rho.real |
| 980 |
|
| 981 |
#negative images yield the same scattering, so flip the image |
| 982 |
#to have more positive than negative values if necessary |
| 983 |
#to make sure averaging is done properly |
| 984 |
#whether theres actually more positive than negative values |
| 985 |
#is ambiguous, but this ensures all maps are at least likely |
| 986 |
#the same designation when averaging |
| 987 |
if np.sum(np.abs(rho[rho<0])) > np.sum(rho[rho>0]): |
| 988 |
rho *= -1 |
| 989 |
|
| 990 |
#scale total number of electrons |
| 991 |
if ne is not None: |
| 992 |
rho *= ne / np.sum(rho) |
| 993 |
|
| 994 |
rg[j+1] = rho2rg(rho=rho,r=r,support=support,dx=dx) |
| 995 |
supportV[j+1] = supportV[j] |
| 996 |
|
| 997 |
#change rho to be the electron density in e-/angstroms^3, rather than number of electrons, |
| 998 |
#which is what the FFT assumes |
| 999 |
rho /= dV |
| 1000 |
my_logger.info('FINISHED DENSITY REFINEMENT') |
| 1001 |
|
| 1002 |
|
| 1003 |
if cutout: |
| 1004 |
#here were going to cut rho out of the large real space box |
| 1005 |
#to the voxels that contain the particle |
| 1006 |
#use D to estimate particle size |
| 1007 |
#assume the particle is in the center of the box |
| 1008 |
#calculate how many voxels needed to contain particle of size D |
| 1009 |
#use bigger than D to make sure we don't crop actual particle in case its larger than expected |
| 1010 |
#lets clip it to a maximum of 2*D to be safe |
| 1011 |
nD = int(2*D/dx)+1 |
| 1012 |
#make sure final box will still have even samples |
| 1013 |
if nD%2==1: |
| 1014 |
nD += 1 |
| 1015 |
|
| 1016 |
nmin = nbox//2 - nD//2 |
| 1017 |
nmax = nbox//2 + nD//2 + 2 |
| 1018 |
#create new rho array containing only the particle |
| 1019 |
newrho = rho[nmin:nmax,nmin:nmax,nmin:nmax] |
| 1020 |
rho = newrho |
| 1021 |
#do the same for the support |
| 1022 |
newsupport = support[nmin:nmax,nmin:nmax,nmin:nmax] |
| 1023 |
support = newsupport |
| 1024 |
#update side to new size of box |
| 1025 |
side = dx * (nmax-nmin) |
| 1026 |
|
| 1027 |
if write_xplor_format: |
| 1028 |
write_xplor(rho,side,fprefix+".xplor") |
| 1029 |
write_xplor(np.ones_like(rho)*support, side, fprefix+"_support.xplor") |
| 1030 |
|
| 1031 |
write_mrc(rho,side,fprefix+".mrc") |
| 1032 |
write_mrc(np.ones_like(rho)*support,side, fprefix+"_support.mrc") |
| 1033 |
|
| 1034 |
#Write some more output files |
| 1035 |
fit = np.zeros(( len(qbinsc),5 )) |
| 1036 |
fit[:len(qdata),0] = qdata |
| 1037 |
fit[:len(Idata),1] = Idata |
| 1038 |
fit[:len(sigqdata),2] = sigqdata |
| 1039 |
fit[:len(qbinsc),3] = qbinsc |
| 1040 |
fit[:len(Imean),4] = Imean |
| 1041 |
np.savetxt(fprefix+'_map.fit', fit, delimiter=' ', fmt='%.5e'.encode('ascii'), |
| 1042 |
header='q(data),I(data),error(data),q(density),I(density)') |
| 1043 |
|
| 1044 |
np.savetxt(fprefix+'_stats_by_step.dat',np.vstack((chi, rg, supportV)).T, |
| 1045 |
delimiter=" ", fmt="%.5e".encode('ascii'), header='Chi2 Rg SupportVolume') |
| 1046 |
|
| 1047 |
my_logger.info('Number of steps: %i', j) |
| 1048 |
my_logger.info('Final Chi2: %.3e', chi[j]) |
| 1049 |
my_logger.info('Final Rg: %3.3f', rg[j+1]) |
| 1050 |
my_logger.info('Final Support Volume: %3.3f', supportV[j+1]) |
| 1051 |
# my_logger.info('END') |
| 1052 |
|
| 1053 |
#return original unscaled values of Idata (and therefore Imean) for comparison with real data |
| 1054 |
Idata /= scale_factor |
| 1055 |
sigqdata /= scale_factor |
| 1056 |
Imean /= scale_factor |
| 1057 |
I /= scale_factor |
| 1058 |
sigq /= scale_factor |
| 1059 |
|
| 1060 |
return qdata, Idata, sigqdata, qbinsc, Imean, chi, rg, supportV, rho, side |
| 1061 |
|
| 1062 |
def center_rho_roll(rho): |
| 1063 |
"""Move electron density map so its center of mass aligns with the center of the grid""" |
| 1064 |
rhocom = np.array(ndimage.measurements.center_of_mass(rho)) |
| 1065 |
#rhocom = np.unravel_index(rho.argmax(), rho.shape) |
| 1066 |
gridcenter = np.array(rho.shape)/2. |
| 1067 |
shift = gridcenter-rhocom |
| 1068 |
shift = shift.astype(int) |
| 1069 |
rho = np.roll(np.roll(np.roll(rho, shift[0], axis=0), shift[1], axis=1), shift[2], axis=2) |
| 1070 |
return rho |
| 1071 |
|
| 1072 |
def euler_grid_search(refrho, movrho, topn=1, abort_event=None): |
| 1073 |
"""Simple grid search on uniformly sampled sphere to optimize alignment. |
| 1074 |
Return the topn candidate maps (default=1, i.e. the best candidate).""" |
| 1075 |
#taken from https://stackoverflow.com/a/44164075/2836338 |
| 1076 |
|
| 1077 |
num_pts = 18 #~20 degrees between points |
| 1078 |
indices = np.arange(0, num_pts, dtype=float) + 0.5 |
| 1079 |
phi = np.arccos(1 - 2*indices/num_pts) |
| 1080 |
theta = np.pi * (1 + 5**0.5) * indices |
| 1081 |
scores = np.zeros((len(phi),len(theta))) |
| 1082 |
for p in range(len(phi)): |
| 1083 |
for t in range(len(theta)): |
| 1084 |
scores[p,t] = -minimize_rho_score(T=[phi[p],theta[t],0,0,0,0],refrho=np.abs(refrho),movrho=np.abs(movrho)) |
| 1085 |
|
| 1086 |
if abort_event is not None: |
| 1087 |
if abort_event.is_set(): |
| 1088 |
return None, None |
| 1089 |
|
| 1090 |
#best_pt = np.unravel_index(scores.argmin(), scores.shape) |
| 1091 |
best_pt = largest_indices(scores, topn) |
| 1092 |
best_scores = scores[best_pt] |
| 1093 |
movrhos = np.zeros((topn,movrho.shape[0],movrho.shape[1],movrho.shape[2])) |
| 1094 |
|
| 1095 |
for i in range(topn): |
| 1096 |
movrhos[i] = transform_rho(movrho, T=[phi[best_pt[0][i]],theta[best_pt[1][i]],0,0,0,0]) |
| 1097 |
|
| 1098 |
if abort_event is not None: |
| 1099 |
if abort_event.is_set(): |
| 1100 |
return movrhos, best_scores |
| 1101 |
|
| 1102 |
return movrhos, best_scores |
| 1103 |
|
| 1104 |
def largest_indices(a, n): |
| 1105 |
"""Returns the n largest indices from a numpy array.""" |
| 1106 |
flat = a.flatten() |
| 1107 |
indices = np.argpartition(flat, -n)[-n:] |
| 1108 |
indices = indices[np.argsort(-flat[indices])] |
| 1109 |
return np.unravel_index(indices, a.shape) |
| 1110 |
|
| 1111 |
def coarse_then_fine_alignment(refrho, movrho, topn=1, |
| 1112 |
abort_event=None): |
| 1113 |
"""Course alignment followed by fine alignment. |
| 1114 |
Select the topn candidates from the grid search |
| 1115 |
and minimize each, selecting the best fine alignment. |
| 1116 |
""" |
| 1117 |
movrhos, scores = euler_grid_search(refrho, movrho, topn=topn, |
| 1118 |
abort_event=abort_event) |
| 1119 |
|
| 1120 |
if abort_event is not None: |
| 1121 |
if abort_event.is_set(): |
| 1122 |
return None, None |
| 1123 |
|
| 1124 |
for i in range(movrhos.shape[0]): |
| 1125 |
movrhos[i], scores[i] = minimize_rho(refrho, movrhos[i]) |
| 1126 |
|
| 1127 |
if abort_event is not None: |
| 1128 |
if abort_event.is_set(): |
| 1129 |
return None, None |
| 1130 |
|
| 1131 |
best_i = np.argmax(scores) |
| 1132 |
movrho = movrhos[best_i] |
| 1133 |
score = scores[best_i] |
| 1134 |
return movrho, score |
| 1135 |
|
| 1136 |
def minimize_rho(refrho, movrho, T = np.zeros(6)): |
| 1137 |
"""Optimize superposition of electron density maps. Move movrho to refrho.""" |
| 1138 |
bounds = np.zeros(12).reshape(6,2) |
| 1139 |
bounds[:3,0] = -20*np.pi |
| 1140 |
bounds[:3,1] = 20*np.pi |
| 1141 |
bounds[3:,0] = -5 |
| 1142 |
bounds[3:,1] = 5 |
| 1143 |
save_movrho = np.copy(movrho) |
| 1144 |
save_refrho = np.copy(refrho) |
| 1145 |
result = optimize.fmin_l_bfgs_b(minimize_rho_score, T, factr= 0.1, |
| 1146 |
maxiter=100, maxfun=200, epsilon=0.05, |
| 1147 |
args=(np.abs(refrho),np.abs(movrho)), approx_grad=True) |
| 1148 |
Topt = result[0] |
| 1149 |
newrho = transform_rho(save_movrho, Topt) |
| 1150 |
finalscore = -rho_overlap_score(save_refrho,newrho) |
| 1151 |
return newrho, finalscore |
| 1152 |
|
| 1153 |
def minimize_rho_score(T, refrho, movrho): |
| 1154 |
"""Scoring function for superposition of electron density maps. |
| 1155 |
|
| 1156 |
refrho - fixed, reference rho |
| 1157 |
movrho - moving rho |
| 1158 |
T - 6-element list containing alpha, beta, gamma, Tx, Ty, Tz in that order |
| 1159 |
to move movrho by. |
| 1160 |
""" |
| 1161 |
newrho = transform_rho(movrho, T) |
| 1162 |
score = rho_overlap_score(refrho,newrho) |
| 1163 |
return score |
| 1164 |
|
| 1165 |
def rho_overlap_score(rho1,rho2): |
| 1166 |
"""Scoring function for superposition of electron density maps.""" |
| 1167 |
n=2*np.sum(np.abs(rho1*rho2)) |
| 1168 |
d=(2*np.sum(rho1**2)**0.5*np.sum(rho2**2)**0.5) |
| 1169 |
score = n/d |
| 1170 |
#-score for least squares minimization, i.e. want to minimize, not maximize score |
| 1171 |
return -score |
| 1172 |
|
| 1173 |
def transform_rho(rho, T, order=1): |
| 1174 |
""" Rotate and translate electron density map by T vector. |
| 1175 |
|
| 1176 |
T = [alpha, beta, gamma, x, y, z], angles in radians |
| 1177 |
order = interpolation order (0-5) |
| 1178 |
""" |
| 1179 |
ne_rho= np.sum((rho)) |
| 1180 |
R = euler2matrix(T[0],T[1],T[2]) |
| 1181 |
c_in = np.array(ndimage.measurements.center_of_mass(rho)) |
| 1182 |
c_out = np.array(rho.shape)/2. |
| 1183 |
offset = c_in-c_out.dot(R) |
| 1184 |
offset += T[3:] |
| 1185 |
rho = ndimage.interpolation.affine_transform(rho,R.T, order=order, |
| 1186 |
offset=offset, output=np.float64, mode='wrap') |
| 1187 |
rho *= ne_rho/np.sum(rho) |
| 1188 |
return rho |
| 1189 |
|
| 1190 |
def euler2matrix(alpha=0.0,beta=0.0,gamma=0.0): |
| 1191 |
"""Convert Euler angles alpha, beta, gamma to a standard rotation matrix. |
| 1192 |
|
| 1193 |
alpha - yaw, counterclockwise rotation about z-axis, upper-left quadrant |
| 1194 |
beta - pitch, counterclockwise rotation about y-axis, four-corners |
| 1195 |
gamma - roll, counterclockwise rotation about x-axis, lower-right quadrant |
| 1196 |
all angles given in radians |
| 1197 |
|
| 1198 |
""" |
| 1199 |
R = [] |
| 1200 |
cosa = np.cos(alpha) |
| 1201 |
sina = np.sin(alpha) |
| 1202 |
cosb = np.cos(beta) |
| 1203 |
sinb = np.sin(beta) |
| 1204 |
cosg = np.cos(gamma) |
| 1205 |
sing = np.sin(gamma) |
| 1206 |
R.append(np.array( |
| 1207 |
[[cosa, -sina, 0], |
| 1208 |
[sina, cosa, 0], |
| 1209 |
[0, 0, 1]])) |
| 1210 |
R.append(np.array( |
| 1211 |
[[cosb, 0, sinb], |
| 1212 |
[0, 1, 0], |
| 1213 |
[-sinb, 0, cosb]])) |
| 1214 |
R.append(np.array( |
| 1215 |
[[1, 0, 0], |
| 1216 |
[0, cosg, -sing], |
| 1217 |
[0, sing, cosg]])) |
| 1218 |
return reduce(np.dot,R[::-1]) |
| 1219 |
|
| 1220 |
def inertia_tensor(rho,side): |
| 1221 |
"""Calculate the moment of inertia tensor for the given electron density map.""" |
| 1222 |
halfside = side/2. |
| 1223 |
n = rho.shape[0] |
| 1224 |
x_ = np.linspace(-halfside,halfside,n) |
| 1225 |
x,y,z = np.meshgrid(x_,x_,x_,indexing='ij') |
| 1226 |
Ixx = np.sum((y**2 + z**2)*rho) |
| 1227 |
Iyy = np.sum((x**2 + z**2)*rho) |
| 1228 |
Izz = np.sum((x**2 + y**2)*rho) |
| 1229 |
Ixy = -np.sum(x*y*rho) |
| 1230 |
Iyz = -np.sum(y*z*rho) |
| 1231 |
Ixz = -np.sum(x*z*rho) |
| 1232 |
I = np.array([[Ixx, Ixy, Ixz], |
| 1233 |
[Ixy, Iyy, Iyz], |
| 1234 |
[Ixz, Iyz, Izz]]) |
| 1235 |
return I |
| 1236 |
|
| 1237 |
def principal_axes(I): |
| 1238 |
"""Calculate the principal inertia axes and order them Ia < Ib < Ic.""" |
| 1239 |
w,v = np.linalg.eigh(I) |
| 1240 |
return w,v |
| 1241 |
|
| 1242 |
def principal_axis_alignment(refrho,movrho): |
| 1243 |
""" Align movrho principal axes to refrho.""" |
| 1244 |
side = 1.0 |
| 1245 |
ne_movrho = np.sum((movrho)) |
| 1246 |
#first center refrho and movrho, save refrho shift |
| 1247 |
rhocom = np.array(ndimage.measurements.center_of_mass(refrho)) |
| 1248 |
gridcenter = np.array(refrho.shape)/2. |
| 1249 |
shift = gridcenter-rhocom |
| 1250 |
refrho = ndimage.interpolation.shift(refrho,shift,order=3,mode='wrap') |
| 1251 |
#calculate, save and perform rotation of refrho to xyz for later |
| 1252 |
refI = inertia_tensor(refrho, side) |
| 1253 |
refw,refv = principal_axes(refI) |
| 1254 |
refR = refv.T |
| 1255 |
refrho = align2xyz(refrho) |
| 1256 |
#align movrho to xyz too |
| 1257 |
#check for best enantiomer, eigh is ambiguous in sign |
| 1258 |
movrho = align2xyz(movrho) |
| 1259 |
enans = generate_enantiomers(movrho) #explicitly performs align2xyz() |
| 1260 |
scores = np.zeros(enans.shape[0]) |
| 1261 |
for i in range(enans.shape[0]): |
| 1262 |
scores[i] = -rho_overlap_score(refrho,enans[i]) |
| 1263 |
movrho = enans[np.argmax(scores)] |
| 1264 |
#now rotate movrho by the inverse of the refrho rotation |
| 1265 |
R = np.linalg.inv(refR) |
| 1266 |
c_in = np.array(ndimage.measurements.center_of_mass(movrho)) |
| 1267 |
c_out = np.array(movrho.shape)/2. |
| 1268 |
offset=c_in-c_out.dot(R) |
| 1269 |
movrho = ndimage.interpolation.affine_transform(movrho,R.T,order=3,offset=offset,mode='wrap') |
| 1270 |
#now shift it back to where refrho was originally |
| 1271 |
movrho = ndimage.interpolation.shift(movrho,-shift,order=3,mode='wrap') |
| 1272 |
movrho *= ne_movrho/np.sum(movrho) |
| 1273 |
return movrho |
| 1274 |
|
| 1275 |
def align2xyz(rho, return_transform=False): |
| 1276 |
""" Align rho such that principal axes align with XYZ axes.""" |
| 1277 |
side = 1.0 |
| 1278 |
ne_rho = np.sum(rho) |
| 1279 |
#shift refrho to the center |
| 1280 |
rhocom = np.array(ndimage.measurements.center_of_mass(rho)) |
| 1281 |
gridcenter = np.array(rho.shape)/2. |
| 1282 |
shift = gridcenter-rhocom |
| 1283 |
rho = ndimage.interpolation.shift(rho,shift,order=3,mode='wrap') |
| 1284 |
#calculate, save and perform rotation of refrho to xyz for later |
| 1285 |
I = inertia_tensor(rho, side) |
| 1286 |
w,v = principal_axes(I) |
| 1287 |
R = v.T |
| 1288 |
refR = np.copy(R) |
| 1289 |
refshift = np.copy(shift) |
| 1290 |
#apparently need to run this a few times to get good alignment |
| 1291 |
#maybe due to interpolation artifacts? |
| 1292 |
for i in range(3): |
| 1293 |
I = inertia_tensor(rho, side) |
| 1294 |
w,v = np.linalg.eigh(I) #principal axes |
| 1295 |
R = v.T #rotation matrix |
| 1296 |
c_in = np.array(ndimage.measurements.center_of_mass(rho)) |
| 1297 |
c_out = np.array(rho.shape)/2. |
| 1298 |
offset=c_in-c_out.dot(R) |
| 1299 |
rho = ndimage.interpolation.affine_transform(rho, R.T, order=3, |
| 1300 |
offset=offset, mode='wrap') |
| 1301 |
rho *= ne_rho/np.sum(rho) |
| 1302 |
if return_transform: |
| 1303 |
return rho, refR, refshift |
| 1304 |
else: |
| 1305 |
return rho |
| 1306 |
|
| 1307 |
def generate_enantiomers(rho): |
| 1308 |
""" Generate all enantiomers of given density map. |
| 1309 |
Output maps are flipped over x,y,z,xy,yz,zx, and xyz, respectively. |
| 1310 |
Assumes rho is prealigned to xyz. |
| 1311 |
""" |
| 1312 |
rho_xflip = rho[::-1,:,:] |
| 1313 |
""" |
| 1314 |
rho_yflip = rho[:,::-1,:] |
| 1315 |
rho_zflip = rho[:,:,::-1] |
| 1316 |
rho_xyflip = rho_xflip[:,::-1,:] |
| 1317 |
rho_yzflip = rho_yflip[:,:,::-1] |
| 1318 |
rho_zxflip = rho_zflip[::-1,:,:] |
| 1319 |
rho_xyzflip = rho_xyflip[:,:,::-1] |
| 1320 |
enans = np.array([rho,rho_xflip,rho_yflip,rho_zflip, |
| 1321 |
rho_xyflip,rho_yzflip,rho_zxflip, |
| 1322 |
rho_xyzflip]) |
| 1323 |
""" |
| 1324 |
enans = np.array([rho,rho_xflip]) |
| 1325 |
return enans |
| 1326 |
|
| 1327 |
def align(refrho, movrho, abort_event=None): |
| 1328 |
""" Align second electron density map to the first.""" |
| 1329 |
if abort_event is not None: |
| 1330 |
if abort_event.is_set(): |
| 1331 |
return None, None |
| 1332 |
|
| 1333 |
ne_rho = np.sum((movrho)) |
| 1334 |
#movrho, score = minimize_rho(refrho, movrho) |
| 1335 |
movrho, score = coarse_then_fine_alignment(refrho, movrho, topn=5, |
| 1336 |
abort_event=abort_event) |
| 1337 |
|
| 1338 |
if movrho is not None: |
| 1339 |
movrho *= ne_rho/np.sum(movrho) |
| 1340 |
|
| 1341 |
return movrho, score |
| 1342 |
|
| 1343 |
def select_best_enantiomers(rhos, refrho=None, cores=1, avg_queue=None, |
| 1344 |
abort_event=None, single_proc=False): |
| 1345 |
""" Select the best enantiomer from each map in the set (or a single map). |
| 1346 |
refrho should not be binary averaged from the original |
| 1347 |
denss maps, since that would likely lose handedness. |
| 1348 |
By default, refrho will be set to the first map.""" |
| 1349 |
if rhos.ndim == 3: |
| 1350 |
rhos = rhos[np.newaxis,...] |
| 1351 |
#can't have nested parallel jobs, so run enantiomer selection |
| 1352 |
#in parallel, but run each map in a loop |
| 1353 |
if refrho is None: |
| 1354 |
refrho = rhos[0] |
| 1355 |
xyz_refrho, refR, refshift = align2xyz(refrho, return_transform=True) |
| 1356 |
scores = np.zeros(rhos.shape[0]) |
| 1357 |
for i in range(rhos.shape[0]): |
| 1358 |
if abort_event is not None: |
| 1359 |
if abort_event.is_set(): |
| 1360 |
return None, None |
| 1361 |
if avg_queue is not None: |
| 1362 |
avg_queue.put_nowait('Selecting enantiomer for model {}\n'.format(i+1)) |
| 1363 |
#align rho to xyz and generate the enantiomers, then shift/rotate each enan |
| 1364 |
#by inverse of refrho, then perform minimization around the original refrho location, |
| 1365 |
#and select the best enantiomer from that set, |
| 1366 |
#rather than doing the minimization around the xyz_refrho location |
| 1367 |
#and then shifting the final best enan back. |
| 1368 |
#this way the final rotation is defined by the optimized score, not |
| 1369 |
#by the inverse refrho xyz alignment, which appears to suffer from |
| 1370 |
#interpolation artifacts |
| 1371 |
xyz_rho = align2xyz(rhos[i]) |
| 1372 |
enans = generate_enantiomers(xyz_rho) |
| 1373 |
#now rotate rho by the inverse of the refrho rotation for each enantiomer |
| 1374 |
R = np.linalg.inv(refR) |
| 1375 |
c_in = np.array(ndimage.measurements.center_of_mass(rhos[i])) |
| 1376 |
c_out = np.array(rhos[i].shape)/2. |
| 1377 |
offset = c_in-c_out.dot(R) |
| 1378 |
for j in range(len(enans)): |
| 1379 |
enans[j] = ndimage.interpolation.affine_transform(enans[j],R.T,order=3,offset=offset,mode='wrap') |
| 1380 |
enans[j] = ndimage.interpolation.shift(enans[j],-refshift,order=3,mode='wrap') |
| 1381 |
#now minimize each enan around the original refrho location |
| 1382 |
|
| 1383 |
if not single_proc: |
| 1384 |
pool = multiprocessing.Pool(cores) |
| 1385 |
try: |
| 1386 |
mapfunc = partial(align, refrho) |
| 1387 |
results = pool.map(mapfunc, enans) |
| 1388 |
pool.close() |
| 1389 |
pool.join() |
| 1390 |
except KeyboardInterrupt: |
| 1391 |
pool.terminate() |
| 1392 |
pool.close() |
| 1393 |
raise |
| 1394 |
|
| 1395 |
else: |
| 1396 |
results = [align(refrho, enan, abort_event=abort_event) for |
| 1397 |
enan in enans] |
| 1398 |
|
| 1399 |
#now select the best enantiomer and set it as the new rhos[i] |
| 1400 |
enans = np.array([results[k][0] for k in range(len(results))]) |
| 1401 |
enans_scores = np.array([results[k][1] for k in range(len(results))]) |
| 1402 |
|
| 1403 |
best_i = np.argmax(enans_scores) |
| 1404 |
rhos[i], scores[i] = enans[best_i], enans_scores[best_i] |
| 1405 |
if avg_queue is not None: |
| 1406 |
avg_queue.put_nowait('Best enantiomer for model {} has score {}\n'.format(i+1, round(scores[i],3))) |
| 1407 |
|
| 1408 |
return rhos, scores |
| 1409 |
|
| 1410 |
def align_multiple(refrho, rhos, cores=1, abort_event=None, single_proc=False): |
| 1411 |
""" Align multiple (or a single) maps to the reference.""" |
| 1412 |
if rhos.ndim == 3: |
| 1413 |
rhos = rhos[np.newaxis,...] |
| 1414 |
#first, center all the rhos, then shift them to where refrho is |
| 1415 |
cen_refrho, refshift = center_rho(refrho, return_shift=True) |
| 1416 |
for i in range(rhos.shape[0]): |
| 1417 |
rhos[i] = center_rho(rhos[i]) |
| 1418 |
ne_rho = np.sum(rhos[i]) |
| 1419 |
#now shift each rho back to where refrho was originally |
| 1420 |
rhos[i] = ndimage.interpolation.shift(rhos[i],-refshift,order=3,mode='wrap') |
| 1421 |
rhos[i] *= ne_rho/np.sum(rhos[i]) |
| 1422 |
|
| 1423 |
if abort_event is not None: |
| 1424 |
if abort_event.is_set(): |
| 1425 |
return None, None |
| 1426 |
|
| 1427 |
if not single_proc: |
| 1428 |
pool = multiprocessing.Pool(cores) |
| 1429 |
try: |
| 1430 |
mapfunc = partial(align, refrho) |
| 1431 |
results = pool.map(mapfunc, rhos) |
| 1432 |
pool.close() |
| 1433 |
pool.join() |
| 1434 |
except KeyboardInterrupt: |
| 1435 |
pool.terminate() |
| 1436 |
pool.close() |
| 1437 |
raise |
| 1438 |
|
| 1439 |
else: |
| 1440 |
results = [align(refrho, rho, abort_event=abort_event) for rho in rhos] |
| 1441 |
|
| 1442 |
rhos = np.array([results[i][0] for i in range(len(results))]) |
| 1443 |
scores = np.array([results[i][1] for i in range(len(results))]) |
| 1444 |
|
| 1445 |
return rhos, scores |
| 1446 |
|
| 1447 |
def average_two(rho1, rho2, abort_event=None): |
| 1448 |
""" Align two electron density maps and return the average.""" |
| 1449 |
rho2, score = align(rho1, rho2, abort_event=abort_event) |
| 1450 |
average_rho = (rho1+rho2)/2 |
| 1451 |
return average_rho |
| 1452 |
|
| 1453 |
def multi_average_two(niter, **kwargs): |
| 1454 |
""" Wrapper script for averaging two maps for multiprocessing.""" |
| 1455 |
kwargs['rho1']=kwargs['rho1'][niter] |
| 1456 |
kwargs['rho2']=kwargs['rho2'][niter] |
| 1457 |
time.sleep(1) |
| 1458 |
return average_two(**kwargs) |
| 1459 |
|
| 1460 |
def average_pairs(rhos, cores=1, abort_event=None, single_proc=False): |
| 1461 |
""" Average pairs of electron density maps, second half to first half.""" |
| 1462 |
#create even/odd pairs, odds are the references |
| 1463 |
rho_args = {'rho1':rhos[::2], 'rho2':rhos[1::2], 'abort_event': abort_event} |
| 1464 |
|
| 1465 |
if not single_proc: |
| 1466 |
pool = multiprocessing.Pool(cores) |
| 1467 |
try: |
| 1468 |
mapfunc = partial(multi_average_two, **rho_args) |
| 1469 |
average_rhos = pool.map(mapfunc, list(range(rhos.shape[0]//2))) |
| 1470 |
pool.close() |
| 1471 |
pool.join() |
| 1472 |
except KeyboardInterrupt: |
| 1473 |
pool.terminate() |
| 1474 |
pool.close() |
| 1475 |
raise |
| 1476 |
|
| 1477 |
else: |
| 1478 |
average_rhos = [multi_average_two(niter, **rho_args) for niter in |
| 1479 |
range(rhos.shape[0]//2)] |
| 1480 |
|
| 1481 |
return np.array(average_rhos) |
| 1482 |
|
| 1483 |
def binary_average(rhos, cores=1, abort_event=None, single_proc=False): |
| 1484 |
""" Generate a reference electron density map using binary averaging.""" |
| 1485 |
twos = 2**np.arange(20) |
| 1486 |
nmaps = np.max(twos[twos<=rhos.shape[0]]) |
| 1487 |
levels = int(np.log2(nmaps))-1 |
| 1488 |
rhos = rhos[:nmaps] |
| 1489 |
for level in range(levels): |
| 1490 |
rhos = average_pairs(rhos, cores, abort_event=abort_event, |
| 1491 |
single_proc=single_proc) |
| 1492 |
refrho = center_rho(rhos[0]) |
| 1493 |
return refrho |
| 1494 |
|
| 1495 |
def calc_fsc(rho1, rho2, side): |
| 1496 |
""" Calculate the Fourier Shell Correlation between two electron density maps.""" |
| 1497 |
df = 1.0/side |
| 1498 |
n = rho1.shape[0] |
| 1499 |
qx_ = np.fft.fftfreq(n)*n*df |
| 1500 |
qx, qy, qz = np.meshgrid(qx_,qx_,qx_,indexing='ij') |
| 1501 |
qx_max = qx.max() |
| 1502 |
qr = np.sqrt(qx**2+qy**2+qz**2) |
| 1503 |
qmax = np.max(qr) |
| 1504 |
qstep = np.min(qr[qr>0]) |
| 1505 |
nbins = int(qmax/qstep) |
| 1506 |
qbins = np.linspace(0,nbins*qstep,nbins+1) |
| 1507 |
#create an array labeling each voxel according to which qbin it belongs |
| 1508 |
qbin_labels = np.searchsorted(qbins, qr, "right") |
| 1509 |
qbin_labels -= 1 |
| 1510 |
F1 = np.fft.fftn(rho1) |
| 1511 |
F2 = np.fft.fftn(rho2) |
| 1512 |
numerator = ndimage.sum(np.real(F1*np.conj(F2)), labels=qbin_labels, |
| 1513 |
index=np.arange(0,qbin_labels.max()+1)) |
| 1514 |
term1 = ndimage.sum(np.abs(F1)**2, labels=qbin_labels, |
| 1515 |
index=np.arange(0,qbin_labels.max()+1)) |
| 1516 |
term2 = ndimage.sum(np.abs(F2)**2, labels=qbin_labels, |
| 1517 |
index=np.arange(0,qbin_labels.max()+1)) |
| 1518 |
denominator = (term1*term2)**0.5 |
| 1519 |
FSC = numerator/denominator |
| 1520 |
qidx = np.where(qbins<qx_max) |
| 1521 |
return np.vstack((qbins[qidx],FSC[qidx])).T |
| 1522 |
|
| 1523 |
class Sasrec(object): |
| 1524 |
def __init__(self, Iq, D, qc=None, r=None, alpha=0.0, ne=2): |
| 1525 |
self.q = Iq[:,0] |
| 1526 |
self.I = Iq[:,1] |
| 1527 |
self.Ierr = Iq[:,2] |
| 1528 |
self.q.clip(1e-10) |
| 1529 |
self.I[np.abs(self.I)<1e-10] = 1e-10 |
| 1530 |
self.Ierr.clip(1e-10) |
| 1531 |
self.D = D |
| 1532 |
self.qmin = np.min(self.q) |
| 1533 |
self.qmax = np.max(self.q) |
| 1534 |
self.nq = len(self.q) |
| 1535 |
self.qi = np.arange(self.nq) |
| 1536 |
if qc is None: |
| 1537 |
self.qc = self.q |
| 1538 |
else: |
| 1539 |
self.qc = qc |
| 1540 |
if r is None: |
| 1541 |
self.nr = self.nq |
| 1542 |
self.r = np.linspace(0,self.D,self.nr) |
| 1543 |
else: |
| 1544 |
self.r = r |
| 1545 |
self.nr = len(self.r) |
| 1546 |
self.alpha = alpha |
| 1547 |
self.ne = ne |
| 1548 |
self.update() |
| 1549 |
|
| 1550 |
def update(self): |
| 1551 |
self.ri = np.arange(self.nr) |
| 1552 |
self.n = self.shannon_channels(self.qmax,self.D) + self.ne |
| 1553 |
self.Ni = np.arange(self.n) |
| 1554 |
self.N = self.Ni + 1 |
| 1555 |
self.Mi = np.copy(self.Ni) |
| 1556 |
self.M = np.copy(self.N) |
| 1557 |
self.qn = np.pi/self.D * self.N |
| 1558 |
self.In = np.zeros((self.nq)) |
| 1559 |
self.Inerr = np.zeros((self.nq)) |
| 1560 |
self.B = np.zeros((self.n, self.nq)) |
| 1561 |
self.S = np.zeros((self.n, self.nr)) |
| 1562 |
self.Y = np.zeros((self.n)) |
| 1563 |
self.C = np.zeros((self.n, self.n)) |
| 1564 |
self.B[self.Ni] = self.Bt(self.N[:, None], self.q, self.D) |
| 1565 |
self.S[self.Ni] = self.St(self.N[:, None], self.r, self.D) |
| 1566 |
self.Y[self.Mi] = self.Yt(I=self.I, Ierr=self.Ierr, Bm=self.B[self.Mi]) |
| 1567 |
self.C[self.Mi[:, None], self.Ni] = self.Ct2(Ierr=self.Ierr, Bm=self.B[self.Mi], Bn=self.B[self.Ni], alpha=self.alpha, D=self.D) |
| 1568 |
self.Cinv = np.linalg.inv(self.C) |
| 1569 |
self.In = 0.5*np.linalg.solve(self.C,self.Y) |
| 1570 |
#self.In = 0.5*optimize.nnls(self.C, self.Y)[0] |
| 1571 |
self.Inerr = 0.5*(np.diagonal(self.Cinv))**(0.5) |
| 1572 |
self.Ic = self.Ish2Iq(Ish=self.In,D=self.D,q=self.qc)[:,1] |
| 1573 |
self.P = self.Ish2P(self.In,self.D,self.r) |
| 1574 |
self.Perr = self.Perrf(r=self.r, D=self.D, Sn=self.S[self.Ni[:, None], self.ri], Sm=self.S[self.Mi[:, None], self.ri], Cinv=self.Cinv) |
| 1575 |
self.I0 = self.Ish2I0(self.In,self.D) |
| 1576 |
self.I0err = self.I0errf(self.Cinv) |
| 1577 |
self.rg2 = self.Ish2rg2(self.In,self.D) |
| 1578 |
self.rg = self.Ish2rg(self.In,self.D) |
| 1579 |
self.rgerr = self.rgerrf(self.In,self.D,self.Cinv) |
| 1580 |
self.ravg = self.Ish2ravg(self.In,self.D) |
| 1581 |
self.ravgerr = self.ravgerrf(self.In,self.D,self.Cinv) |
| 1582 |
self.Q = self.Ish2Q(self.In,self.D) |
| 1583 |
self.Qerr = self.Qerrf(self.D,self.Cinv) |
| 1584 |
self.Vp = self.Ish2Vp(self.In,self.D) |
| 1585 |
self.Vperr = self.Vperrf(self.In,self.D,self.Cinv) |
| 1586 |
self.mwVp = self.Ish2mwVp(self.In,self.D) |
| 1587 |
self.mwVperr = self.mwVperrf(self.In,self.D,self.Cinv) |
| 1588 |
self.Vc = self.Ish2Vc(self.In,self.D) |
| 1589 |
self.Vcerr = self.Vcerrf(self.In,self.D,self.Cinv) |
| 1590 |
self.mwVc = self.Ish2mwVc(self.In,self.D) |
| 1591 |
self.mwVcerr = self.mwVcerrf(self.In,self.D,self.Cinv) |
| 1592 |
self.lc = self.Ish2lc(self.In,self.D) |
| 1593 |
self.lcerr = self.lcerrf(self.In,self.D,self.Cinv) |
| 1594 |
|
| 1595 |
|
| 1596 |
def shannon_channels(self, D, qmax=0.5, qmin=0.0): |
| 1597 |
"""Return the number of Shannon channels given a q range and maximum particle dimension""" |
| 1598 |
width = np.pi / D |
| 1599 |
num_channels = int((qmax-qmin) / width) |
| 1600 |
return num_channels |
| 1601 |
|
| 1602 |
def Bt(self,n,q,D): |
| 1603 |
return (n*np.pi)**2/((n*np.pi)**2-(q*D)**2) * np.sinc(q*D/np.pi) * (-1)**(n+1) |
| 1604 |
|
| 1605 |
def St(self,n,r,D): |
| 1606 |
return r/D**2 * n * np.sin(n*np.pi*r/D) |
| 1607 |
|
| 1608 |
def Yt(self,I, Ierr, Bm): |
| 1609 |
"""Return the values of Y, an m-length vector.""" |
| 1610 |
return np.einsum('i, ki->k', I/Ierr**2, Bm) |
| 1611 |
|
| 1612 |
def Ct(self,Ierr, Bm, Bn): |
| 1613 |
"""Return the values of C, a m x n variance-covariance matrix""" |
| 1614 |
return np.einsum('ij,kj->ik', Bm/Ierr**2, Bn) |
| 1615 |
|
| 1616 |
def Amn(self,m,n,D): |
| 1617 |
"""Return the mxn matrix of coefficients for the integral of (2nd deriv of P(r))**2 used for smoothing""" |
| 1618 |
m = np.atleast_1d(m).astype(float) |
| 1619 |
n = np.atleast_1d(n).astype(float) |
| 1620 |
nm = len(m) |
| 1621 |
nn = len(n) |
| 1622 |
amn = np.zeros((nm,nn)) |
| 1623 |
for i in range(nm): |
| 1624 |
for j in range(nn): |
| 1625 |
if m[i] != n[j]: |
| 1626 |
amn[i,j] = np.pi**2/(2*D**5) * (m[i]*n[j])**2 * (m[i]**4+n[j]**4)/(m[i]**2+n[j]**2)**2 * (-1)**(m[i]+n[j]) |
| 1627 |
if m[i] == n[j]: |
| 1628 |
amn[i,j] = np.pi**4/(2*D**5) * n[j]**6 |
| 1629 |
return amn |
| 1630 |
|
| 1631 |
def Ct2(self,Ierr, Bm, Bn, alpha, D): |
| 1632 |
"""Return the values of C, a m x n variance-covariance matrix while smoothing P(r)""" |
| 1633 |
m = Bm.shape[0] |
| 1634 |
n = Bn.shape[0] |
| 1635 |
ni = Bm.shape[1] |
| 1636 |
M = np.arange(m)+1 |
| 1637 |
N = np.arange(n)+1 |
| 1638 |
amn = self.Amn(M,N,D) |
| 1639 |
return ni/8. * alpha * amn + np.einsum('ij,kj->ik', Bm/Ierr**2, Bn) |
| 1640 |
|
| 1641 |
def Perrt(self,r, Sn, Sm, Cinv): |
| 1642 |
"""Return the standard errors on P(r). |
| 1643 |
|
| 1644 |
To work, Sn will generally equal something like S[N[:, None], pts] where N is |
| 1645 |
the array of n shannon channels, same for Sm, and pts is the array of |
| 1646 |
indices for the number of data points, e.g. np.arange(npts). |
| 1647 |
This is required for correct broadcasting. |
| 1648 |
Example: Perr(r=r, Sn=S[N[:, None], pts]), Sm=S[M[:, None], pts], Cinv=Cinv) |
| 1649 |
""" |
| 1650 |
return r*np.einsum('ni,mi,nm->i', Sn, Sm, Cinv)**(.5) |
| 1651 |
|
| 1652 |
def Iq2Ish(self,q,I,sigq,D,ne=2): |
| 1653 |
"""Calculate Shannon intensities from experimental I(q) profile.""" |
| 1654 |
npts = len(q) |
| 1655 |
qmax = np.max(q) |
| 1656 |
D = float(D) |
| 1657 |
nmax = shannon_channels(qmax, D)+ne |
| 1658 |
|
| 1659 |
sigq.clip(1e-10) |
| 1660 |
|
| 1661 |
B = np.zeros((nmax, npts)) |
| 1662 |
C = np.zeros((nmax, nmax)) |
| 1663 |
Y = np.zeros((nmax)) |
| 1664 |
Ni = np.arange(nmax) #indices |
| 1665 |
Mi = np.arange(nmax) #indices |
| 1666 |
N = Ni+1 #values |
| 1667 |
M = Mi+1 #values |
| 1668 |
pts = np.arange(npts) |
| 1669 |
|
| 1670 |
qsh = N*shannon_width(D) |
| 1671 |
Ish = np.zeros((nmax,3)) |
| 1672 |
|
| 1673 |
B[Ni] = Bt(N[:, None], q, D) |
| 1674 |
Y[Mi] = Yt(I=I,Ierr=sigq,Bm=B[Mi]) |
| 1675 |
C[Mi[:, None], Ni] = Ct(Ierr=sigq, Bm=B[Mi], Bn=B[Ni]) |
| 1676 |
|
| 1677 |
Cinv = np.linalg.inv(C) |
| 1678 |
|
| 1679 |
Ish[:,0] = qsh |
| 1680 |
Ish[:,1] = 0.5*np.linalg.solve(C,Y) |
| 1681 |
Ish[:,2] = 0.5*(np.diagonal(Cinv))**(0.5) |
| 1682 |
|
| 1683 |
return Ish |
| 1684 |
|
| 1685 |
def Iq2Cinv(self,q,I,sigq,D,ne=2): |
| 1686 |
"""Calculate Shannon intensities from experimental I(q) profile.""" |
| 1687 |
npts = len(q) |
| 1688 |
qmax = np.max(q) |
| 1689 |
D = float(D) |
| 1690 |
nmax = shannon_channels(qmax, D)+ne |
| 1691 |
|
| 1692 |
sigq.clip(1e-10) |
| 1693 |
|
| 1694 |
B = np.zeros((nmax, npts)) |
| 1695 |
C = np.zeros((nmax, nmax)) |
| 1696 |
Y = np.zeros((nmax)) |
| 1697 |
Ni = np.arange(nmax) #indices |
| 1698 |
Mi = np.arange(nmax) #indices |
| 1699 |
N = Ni+1 #values |
| 1700 |
M = Mi+1 #values |
| 1701 |
pts = np.arange(npts) |
| 1702 |
|
| 1703 |
qsh = N*shannon_width(D) |
| 1704 |
Ish = np.zeros((nmax,3)) |
| 1705 |
|
| 1706 |
B[Ni] = Bt(N[:, None], q, D) |
| 1707 |
Y[Mi] = Yt(I=I,sigq=sigq,Bm=B[Mi]) |
| 1708 |
C[Mi[:, None], Ni] = Ct(sigq=sigq, Bm=B[Mi], Bn=B[Ni]) |
| 1709 |
|
| 1710 |
Cinv = np.linalg.inv(C) |
| 1711 |
|
| 1712 |
Ish[:,0] = qsh |
| 1713 |
Ish[:,1] = 0.5*np.linalg.solve(C,Y) |
| 1714 |
Ish[:,2] = 0.5*(np.diagonal(Cinv))**(0.5) |
| 1715 |
|
| 1716 |
return Cinv |
| 1717 |
|
| 1718 |
def Ish2Iq(self,Ish,D,q=(np.arange(500)+1.)/1000): |
| 1719 |
"""Calculate I(q) from intensities at Shannon points.""" |
| 1720 |
q = np.atleast_1d(q) |
| 1721 |
Ish = np.atleast_1d(Ish) |
| 1722 |
Iq = np.zeros((len(q),2)) |
| 1723 |
Iq[:,0] = q |
| 1724 |
n = len(Ish) |
| 1725 |
N = np.arange(n)+1 |
| 1726 |
denominator = (N[:,None]*np.pi)**2-(q*D)**2 |
| 1727 |
I = 2*np.einsum('k,ki->i',Ish,(N[:,None]*np.pi)**2 / denominator * np.sinc(q*D/np.pi) * (-1)**(N[:,None]+1)) |
| 1728 |
Iq[:,1] = I |
| 1729 |
return Iq |
| 1730 |
|
| 1731 |
def Ish2P(self,Ish,D,r=None,dr=None): |
| 1732 |
"""Calculate P(r) from intensities at Shannon points.""" |
| 1733 |
if r is None: |
| 1734 |
if dr is None: |
| 1735 |
dr = 0.10 |
| 1736 |
r = np.linspace(0,D,D/dr) |
| 1737 |
r = np.atleast_1d(r) |
| 1738 |
Ish = np.atleast_1d(Ish) |
| 1739 |
N = np.arange(len(Ish))+1 |
| 1740 |
P = 1./(2*D**2) * np.einsum('k,kj->k',r,N*Ish*np.sin(N*np.pi*r[:,None]/D)) |
| 1741 |
return P |
| 1742 |
|
| 1743 |
def _Ish2P(self,Ish,D,r=None,dr=None,extend=True,nstart=0): |
| 1744 |
"""Calculate P(r) from intensities at Shannon points.""" |
| 1745 |
if r is None: |
| 1746 |
if dr is None: |
| 1747 |
dr = 0.10 |
| 1748 |
r = np.linspace(0,D,D/dr) |
| 1749 |
r = np.atleast_1d(r) |
| 1750 |
Ish = np.atleast_1d(Ish) |
| 1751 |
Ish = Ish[nstart:] |
| 1752 |
P = np.zeros((len(r),2)) |
| 1753 |
P[:,0] = r |
| 1754 |
N = np.arange(len(Ish))+1+nstart |
| 1755 |
for i in range(len(r)): |
| 1756 |
if r[i]>D: |
| 1757 |
P[i,1] = 0.0 |
| 1758 |
else: |
| 1759 |
P[i,1] = r[i]/(2*D**2) * np.sum(N*Ish*np.sin(N*np.pi*r[i]/D)) |
| 1760 |
if extend: |
| 1761 |
Np = np.arange(N[-1]+1,100) |
| 1762 |
k = Ish[-1]/(N[-1]*np.pi/D)**(-4) |
| 1763 |
P[i,1] += r[i]/(2*D**2) * k * np.sum(Np*(Np*np.pi/D)**(-4)*np.sin(Np*np.pi*r[i]/D)) |
| 1764 |
return P |
| 1765 |
|
| 1766 |
def Perrf(self,r, D, Sn, Sm, Cinv): |
| 1767 |
"""Return the standard errors on P(r). |
| 1768 |
|
| 1769 |
To work, Sn will generally equal something like S[N[:, None], pts] where N is |
| 1770 |
the array of n shannon channels, same for Sm, and pts is the array of |
| 1771 |
indices for the number of data points, e.g. np.arange(npts). |
| 1772 |
This is required for correct broadcasting. |
| 1773 |
Example: Perr(r=r, Sn=S[N[:, None], pts]), Sm=S[M[:, None], pts], Cinv=Cinv) |
| 1774 |
""" |
| 1775 |
nmax = len(Sn) |
| 1776 |
Ni = np.arange(nmax) #indices |
| 1777 |
Mi = np.arange(nmax) #indices |
| 1778 |
N = Ni+1 #values |
| 1779 |
M = Mi+1 #values |
| 1780 |
#THIS IS WRONG, FIX IT WITH CORRECT EQUATION |
| 1781 |
return r/(4*D**2)*np.einsum('ni,mi,nm->i', N[:,None]*Sn, M[:,None]*Sm, Cinv)**(.5) |
| 1782 |
|
| 1783 |
def Ish2I0(self,Ish,D): |
| 1784 |
"""Calculate I0 from Shannon intensities""" |
| 1785 |
n = len(Ish) |
| 1786 |
N = np.arange(n)+1 |
| 1787 |
I0 = 2 * np.sum(Ish*(-1)**(N+1)) |
| 1788 |
return I0 |
| 1789 |
|
| 1790 |
def I0errf(self,Cinv): |
| 1791 |
"""Calculate error on I0 from Shannon intensities from inverse C variance-covariance matrix""" |
| 1792 |
nmax = len(Cinv) |
| 1793 |
Ni = np.arange(nmax) #indices |
| 1794 |
Mi = np.arange(nmax) #indices |
| 1795 |
N = Ni+1 #values |
| 1796 |
M = Mi+1 #values |
| 1797 |
s2 = np.einsum('n,m,nm->',(-1)**(N),(-1)**M,Cinv) |
| 1798 |
return s2**(0.5) |
| 1799 |
|
| 1800 |
def Ish2rg(self,Ish,D): |
| 1801 |
"""Calculate Rg from Shannon intensities""" |
| 1802 |
n = len(Ish) |
| 1803 |
N = np.arange(n)+1 |
| 1804 |
I0 = self.Ish2I0(Ish,D) |
| 1805 |
summation = np.sum(Ish*(1-6/(N*np.pi)**2)*(-1)**(N+1)) |
| 1806 |
rg = np.sqrt(D**2/I0 * summation) |
| 1807 |
return rg |
| 1808 |
|
| 1809 |
def Ish2rg2(self,Ish,D): |
| 1810 |
"""Calculate Rg^2 from Shannon intensities""" |
| 1811 |
n = len(Ish) |
| 1812 |
N = np.arange(n)+1 |
| 1813 |
I0 = self.Ish2I0(Ish,D) |
| 1814 |
summation = np.sum(Ish*(1-6/(N*np.pi)**2)*(-1)**(N+1)) |
| 1815 |
rg2 = D**2/I0 * summation |
| 1816 |
#rg = np.sqrt(rg2) |
| 1817 |
return rg2 |
| 1818 |
|
| 1819 |
def rgerrf(self,Ish,D,Cinv): |
| 1820 |
"""Calculate error on Rg from Shannon intensities from inverse C variance-covariance matrix""" |
| 1821 |
rg = self.Ish2rg(Ish,D) |
| 1822 |
I0 = self.Ish2I0(Ish,D) |
| 1823 |
nmax = len(Cinv) |
| 1824 |
Ni = np.arange(nmax) #indices |
| 1825 |
Mi = np.arange(nmax) #indices |
| 1826 |
N = Ni+1 #values |
| 1827 |
M = Mi+1 #values |
| 1828 |
s2 = np.einsum('n,m,nm->',(1-6/(N*np.pi)**2)*(-1)**(N),(1-6/(M*np.pi)**2)*(-1)**M,Cinv) |
| 1829 |
return D**2/(I0*rg)*s2**(0.5) |
| 1830 |
|
| 1831 |
def Ish2ravg(self,Ish,D): |
| 1832 |
"""Calculate average vector length r from Shannon intensities""" |
| 1833 |
n = len(Ish) |
| 1834 |
N = np.arange(n)+1 |
| 1835 |
I0 = self.Ish2I0(Ish,D) |
| 1836 |
summation = np.sum(Ish * ( ((-1)**N-1)/(N*np.pi)**2 - (-1)**N/2. )) |
| 1837 |
avgr = 4*D/I0 * summation |
| 1838 |
return avgr |
| 1839 |
|
| 1840 |
def ravgerrf(self,Ish,D,Cinv): |
| 1841 |
"""Calculate error on Rg from Shannon intensities from inverse C variance-covariance matrix""" |
| 1842 |
avgr = self.Ish2ravg(Ish,D) |
| 1843 |
I0 = self.Ish2I0(Ish,D) |
| 1844 |
nmax = len(Cinv) |
| 1845 |
Ni = np.arange(nmax) #indices |
| 1846 |
Mi = np.arange(nmax) #indices |
| 1847 |
N = Ni+1 #values |
| 1848 |
M = Mi+1 #values |
| 1849 |
s2 = np.einsum('n,m,nm->',((-1)**N-1)/(N*np.pi)**2 - (-1)**N/2.,((-1)**M-1)/(M*np.pi)**2 - (-1)**M/2.,Cinv) |
| 1850 |
return 2*D/I0 * s2**(0.5) |
| 1851 |
|
| 1852 |
def Ish2Q(self,Ish,D): |
| 1853 |
"""Calculate Porod Invariant Q from Shannon intensities""" |
| 1854 |
n = len(Ish) |
| 1855 |
N = np.arange(n)+1 |
| 1856 |
Q = (np.pi/D)**3 * np.sum(Ish*N**2) |
| 1857 |
return Q |
| 1858 |
|
| 1859 |
def Qerrf(self,D,Cinv): |
| 1860 |
"""Calculate error on Q from Shannon intensities from inverse C variance-covariance matrix""" |
| 1861 |
nmax = len(Cinv) |
| 1862 |
Ni = np.arange(nmax) #indices |
| 1863 |
Mi = np.arange(nmax) #indices |
| 1864 |
N = Ni+1 #values |
| 1865 |
M = Mi+1 #values |
| 1866 |
s2 = np.einsum('n,m,nm->', N**2, M**2,Cinv) |
| 1867 |
return (np.pi/D)**3 * s2**(0.5) |
| 1868 |
|
| 1869 |
def gamma0(self,Ish, D): |
| 1870 |
"""Calculate gamma at r=0. gamma is P(r)/4*pi*r^2""" |
| 1871 |
Q = self.Ish2Q(Ish,D) |
| 1872 |
return 1/(8*np.pi**3) * Q |
| 1873 |
|
| 1874 |
def Ish2Vp(self,Ish,D): |
| 1875 |
"""Calculate Porod Volume from Shannon intensities""" |
| 1876 |
Q = self.Ish2Q(Ish,D) |
| 1877 |
I0 = self.Ish2I0(Ish,D) |
| 1878 |
Vp = 2*np.pi**2 * I0/Q |
| 1879 |
return Vp |
| 1880 |
|
| 1881 |
def Vperrf(self,Ish, D, Cinv): |
| 1882 |
"""Calculate error on Vp from Shannon intensities from inverse C variance-covariance matrix""" |
| 1883 |
I0 = self.Ish2I0(Ish,D) |
| 1884 |
Q = self.Ish2Q(Ish,D) |
| 1885 |
I0s = self.I0errf(Cinv) |
| 1886 |
Qs = self.Qerrf(D,Cinv) |
| 1887 |
s2 = (2*np.pi/Q)**2*(I0s)**2 + (2*np.pi*I0/Q**2)**2*Qs**2 |
| 1888 |
return s2**(0.5) |
| 1889 |
|
| 1890 |
def Ish2mwVp(self,Ish,D): |
| 1891 |
"""Calculate molecular weight via Porod Volume from Shannon intensities""" |
| 1892 |
Vp = self.Ish2Vp(Ish,D) |
| 1893 |
mw = Vp/1.66 |
| 1894 |
return mw |
| 1895 |
|
| 1896 |
def mwVperrf(self,Ish,D,Cinv): |
| 1897 |
"""Calculate error on mwVp from Shannon intensities from inverse C variance-covariance matrix""" |
| 1898 |
Vps = self.Vperrf(Ish,D,Cinv) |
| 1899 |
return Vps/1.66 |
| 1900 |
|
| 1901 |
def Ish2Vc(self,Ish,D): |
| 1902 |
"""Calculate Volume of Correlation from Shannon intensities""" |
| 1903 |
n = len(Ish) |
| 1904 |
N = np.arange(n)+1 |
| 1905 |
I0 = self.Ish2I0(Ish,D) |
| 1906 |
area_qIq = 2*np.pi/D**2 * np.sum(N * Ish * special.sici(N*np.pi)[0]) |
| 1907 |
Vc = I0/area_qIq |
| 1908 |
return Vc |
| 1909 |
|
| 1910 |
def Vcerrf(self,Ish, D, Cinv): |
| 1911 |
"""Calculate error on Vc from Shannon intensities from inverse C variance-covariance matrix""" |
| 1912 |
I0 = self.Ish2I0(Ish,D) |
| 1913 |
Vc = self.Ish2Vc(Ish,D) |
| 1914 |
nmax = len(Cinv) |
| 1915 |
Ni = np.arange(nmax) #indices |
| 1916 |
Mi = np.arange(nmax) #indices |
| 1917 |
N = Ni+1 #values |
| 1918 |
M = Mi+1 #values |
| 1919 |
s2 = np.einsum('n,m,nm->', N*special.sici(N*np.pi)[0], M*special.sici(M*np.pi)[0],Cinv) |
| 1920 |
return (2*np.pi*Vc**2/(D**2*I0)) * s2**(0.5) |
| 1921 |
|
| 1922 |
def Ish2Qr(self,Ish,D): |
| 1923 |
"""Calculate Rambo Invariant Qr (Vc^2/Rg) from Shannon intensities""" |
| 1924 |
Vc = self.Ish2Vc(Ish,D) |
| 1925 |
Rg = self.Ish2rg(Ish,D) |
| 1926 |
Qr = Vc**2/Rg |
| 1927 |
return Qr |
| 1928 |
|
| 1929 |
def Ish2mwVc(self,Ish,D,RNA=False): |
| 1930 |
"""Calculate molecular weight via the Volume of Correlation from Shannon intensities""" |
| 1931 |
Qr = self.Ish2Qr(Ish,D) |
| 1932 |
if RNA: |
| 1933 |
mw = (Qr/0.00934)**(0.808) |
| 1934 |
else: |
| 1935 |
mw = (Qr/0.1231)**(1.00) |
| 1936 |
return mw |
| 1937 |
|
| 1938 |
def mwVcerrf(self,Ish,D,Cinv): |
| 1939 |
Vc = self.Ish2Vc(Ish,D) |
| 1940 |
Rg = self.Ish2rg(Ish,D) |
| 1941 |
Vcs = self.Vcerrf(Ish,D,Cinv) |
| 1942 |
Rgs = self.rgerrf(Ish,D,Cinv) |
| 1943 |
mwVcs = Vc/(0.1231*Rg) * (4*Vcs**2 + (Vc/Rg*Rgs)**2)**(0.5) |
| 1944 |
return mwVcs |
| 1945 |
|
| 1946 |
def Ish2lc(self,Ish,D): |
| 1947 |
"""Calculate length of correlation from Shannon intensities""" |
| 1948 |
Vp = self.Ish2Vp(Ish,D) |
| 1949 |
Vc = self.Ish2Vc(Ish,D) |
| 1950 |
lc = Vp/(2*np.pi*Vc) |
| 1951 |
return lc |
| 1952 |
|
| 1953 |
def lcerrf(self,Ish, D, Cinv): |
| 1954 |
"""Calculate error on lc from Shannon intensities from inverse C variance-covariance matrix""" |
| 1955 |
Vp = self.Ish2Vp(Ish,D) |
| 1956 |
Vc = self.Ish2Vc(Ish,D) |
| 1957 |
Vps = self.Vperrf(Ish,D,Cinv) |
| 1958 |
Vcs = self.Vcerrf(Ish,D,Cinv) |
| 1959 |
s2 = Vps**2 + (Vp/Vc)**2*Vcs**2 |
| 1960 |
return 1/(2*np.pi*Vc) * s2**(0.5) |
| 1961 |
|
| 1962 |
def Ish2areaIq(self,Ish,D): |
| 1963 |
"""Calculate area under I(q) from Shannon intensities""" |
| 1964 |
I0 = self.Ish2I0(Ish,D) |
| 1965 |
area_Iq = np.pi/D * (I0/2 + np.sum(Ish)) |
| 1966 |
#the following also works |
| 1967 |
#n = len(Ish) |
| 1968 |
#N = np.arange(n)+1 |
| 1969 |
#area_Iq = np.pi/D * np.sum(Ish*(1+(-1)**(N+1))) |
| 1970 |
return area_Iq |
| 1971 |
|
| 1972 |
class PDB(object): |
| 1973 |
"""Load pdb file.""" |
| 1974 |
def __init__(self, filename): |
| 1975 |
self.natoms = 0 |
| 1976 |
with open(filename) as f: |
| 1977 |
for line in f: |
| 1978 |
if line[0:4] != "ATOM" and line[0:4] != "HETA": |
| 1979 |
continue # skip other lines |
| 1980 |
self.natoms += 1 |
| 1981 |
self.atomnum = np.zeros((self.natoms),dtype=int) |
| 1982 |
self.atomname = np.zeros((self.natoms),dtype=np.dtype((np.str,3))) |
| 1983 |
self.atomalt = np.zeros((self.natoms),dtype=np.dtype((np.str,1))) |
| 1984 |
self.resname = np.zeros((self.natoms),dtype=np.dtype((np.str,3))) |
| 1985 |
self.resnum = np.zeros((self.natoms),dtype=int) |
| 1986 |
self.chain = np.zeros((self.natoms),dtype=np.dtype((np.str,1))) |
| 1987 |
self.coords = np.zeros((self.natoms, 3)) |
| 1988 |
self.occupancy = np.zeros((self.natoms)) |
| 1989 |
self.b = np.zeros((self.natoms)) |
| 1990 |
self.atomtype = np.zeros((self.natoms),dtype=np.dtype((np.str,2))) |
| 1991 |
self.charge = np.zeros((self.natoms),dtype=np.dtype((np.str,2))) |
| 1992 |
self.nelectrons = np.zeros((self.natoms),dtype=int) |
| 1993 |
with open(filename) as f: |
| 1994 |
atom = 0 |
| 1995 |
for line in f: |
| 1996 |
if line[0:6] == "CRYST1": |
| 1997 |
cryst = line.split() |
| 1998 |
self.cella = float(cryst[1]) |
| 1999 |
self.cellb = float(cryst[2]) |
| 2000 |
self.cellc = float(cryst[3]) |
| 2001 |
self.cellalpha = float(cryst[4]) |
| 2002 |
self.cellbeta = float(cryst[5]) |
| 2003 |
self.cellgamma = float(cryst[6]) |
| 2004 |
if line[0:4] != "ATOM" and line[0:4] != "HETA": |
| 2005 |
continue # skip other lines |
| 2006 |
try: |
| 2007 |
self.atomnum[atom] = int(line[6:11]) |
| 2008 |
except ValueError as e: |
| 2009 |
self.atomnum[atom] = int(line[6:11],36) |
| 2010 |
self.atomname[atom] = line[12:16].split()[0] |
| 2011 |
self.atomalt[atom] = line[16] |
| 2012 |
self.resname[atom] = line[17:20] |
| 2013 |
try: |
| 2014 |
self.resnum[atom] = int(line[22:26]) |
| 2015 |
except ValueError as e: |
| 2016 |
self.resnum[atom] = int(line[22:26],36) |
| 2017 |
self.chain[atom] = line[21] |
| 2018 |
self.coords[atom, 0] = float(line[30:38]) |
| 2019 |
self.coords[atom, 1] = float(line[38:46]) |
| 2020 |
self.coords[atom, 2] = float(line[46:54]) |
| 2021 |
self.occupancy[atom] = float(line[54:60]) |
| 2022 |
self.b[atom] = float(line[60:66]) |
| 2023 |
atomtype = line[76:78].strip() |
| 2024 |
if len(atomtype) == 2: |
| 2025 |
atomtype0 = atomtype[0].upper() |
| 2026 |
atomtype1 = atomtype[1].lower() |
| 2027 |
atomtype = atomtype0 + atomtype1 |
| 2028 |
self.atomtype[atom] = atomtype |
| 2029 |
self.charge[atom] = line[78:80].strip('\n') |
| 2030 |
self.nelectrons[atom] = electrons.get(self.atomtype[atom].upper(),6) |
| 2031 |
atom += 1 |
| 2032 |
|
| 2033 |
|
| 2034 |
def write(self, filename): |
| 2035 |
"""Write PDB file format using pdb object as input.""" |
| 2036 |
records = [] |
| 2037 |
anum,rc = (np.unique(self.atomnum,return_counts=True)) |
| 2038 |
if np.any(rc>1): |
| 2039 |
#in case default atom numbers are repeated, just renumber them |
| 2040 |
self_numbering=True |
| 2041 |
else: |
| 2042 |
self_numbering=False |
| 2043 |
for i in range(self.natoms): |
| 2044 |
if self_numbering: |
| 2045 |
atomnum = '%5i' % ((i+1)%99999) |
| 2046 |
else: |
| 2047 |
atomnum = '%5i' % (self.atomnum[i]%99999) |
| 2048 |
atomname = '%3s' % self.atomname[i] |
| 2049 |
atomalt = '%1s' % self.atomalt[i] |
| 2050 |
resnum = '%4i' % (self.resnum[i]%9999) |
| 2051 |
resname = '%3s' % self.resname[i] |
| 2052 |
chain = '%1s' % self.chain[i] |
| 2053 |
x = '%8.3f' % self.coords[i,0] |
| 2054 |
y = '%8.3f' % self.coords[i,1] |
| 2055 |
z = '%8.3f' % self.coords[i,2] |
| 2056 |
o = '% 6.2f' % self.occupancy[i] |
| 2057 |
b = '%6.2f' % self.b[i] |
| 2058 |
atomtype = '%2s' % self.atomtype[i] |
| 2059 |
charge = '%2s' % self.charge[i] |
| 2060 |
records.append(['ATOM ' + atomnum + ' ' + atomname + ' ' + resname + ' ' + chain + resnum + ' ' + x + y + z + o + b + ' ' + atomtype + charge]) |
| 2061 |
np.savetxt(filename, records, fmt='%80s'.encode('ascii')) |
| 2062 |
|
| 2063 |
def pdb2map_gauss(pdb,xyz,sigma,mode="slow",eps=1e-6): |
| 2064 |
"""Simple isotropic gaussian sum at coordinate locations. |
| 2065 |
|
| 2066 |
Fast mode uses KDTree to only calculate density at grid points with |
| 2067 |
a density above a threshold. |
| 2068 |
see https://stackoverflow.com/questions/52208434""" |
| 2069 |
n = int(round(xyz.shape[0]**(1/3.))) |
| 2070 |
sigma /= 4. |
| 2071 |
dx = xyz[1,2] - xyz[0,2] |
| 2072 |
shift = np.ones(3)*dx/2. |
| 2073 |
#dist = spatial.distance.cdist(pdb.coords, xyz) |
| 2074 |
#rho = np.sum(values,axis=0).reshape(n,n,n) |
| 2075 |
#run cdist in a loop over atoms to avoid overloading memory |
| 2076 |
print("\n Calculate density map from PDB... ") |
| 2077 |
if mode == "fast": |
| 2078 |
if eps is None: |
| 2079 |
eps = np.finfo('float64').eps |
| 2080 |
thr = -np.log(eps) * 2 * sigma**2 |
| 2081 |
data_tree = spatial.cKDTree(pdb.coords-shift) |
| 2082 |
discr = 1000 # you can tweak this to get best results on your system |
| 2083 |
values = np.empty(n**3) |
| 2084 |
for i in range(n**3//discr + 1): |
| 2085 |
sys.stdout.write("\r%i / %i chunks" % (i+1, n**3//discr + 1)) |
| 2086 |
sys.stdout.flush() |
| 2087 |
slc = slice(i * discr, i * discr + discr) |
| 2088 |
grid_tree = spatial.cKDTree(xyz[slc]) |
| 2089 |
dists = grid_tree.sparse_distance_matrix(data_tree, thr, output_type = 'coo_matrix') |
| 2090 |
dists.data = 1./np.sqrt(2*np.pi*sigma**2) * np.exp(-dists.data/(2*sigma**2)) |
| 2091 |
values[slc] = dists.sum(1).squeeze() |
| 2092 |
else: |
| 2093 |
values = np.zeros((xyz.shape[0])) |
| 2094 |
for i in range(pdb.coords.shape[0]): |
| 2095 |
sys.stdout.write("\r% 5i / % 5i atoms" % (i+1,pdb.coords.shape[0])) |
| 2096 |
sys.stdout.flush() |
| 2097 |
dist = spatial.distance.cdist(pdb.coords[None,i]-shift, xyz) |
| 2098 |
dist *= dist |
| 2099 |
values += pdb.nelectrons[i]*1./np.sqrt(2*np.pi*sigma**2) * np.exp(-dist[0]/(2*sigma**2)) |
| 2100 |
print() |
| 2101 |
return values.reshape(n,n,n) |
| 2102 |
|
| 2103 |
def pdb2map_fastgauss(pdb,x,y,z,sigma,r=20.0,ignore_waters=True): |
| 2104 |
"""Simple isotropic gaussian sum at coordinate locations. |
| 2105 |
|
| 2106 |
This fastgauss function only calculates the values at |
| 2107 |
grid points near the atom for speed. |
| 2108 |
|
| 2109 |
pdb - instance of PDB class (required) |
| 2110 |
x,y,z - meshgrids for x, y, and z (required) |
| 2111 |
sigma - width of Gaussian, i.e. effectively resolution |
| 2112 |
r - maximum distance from atom to calculate density |
| 2113 |
""" |
| 2114 |
side = x[-1,0,0] - x[0,0,0] |
| 2115 |
halfside = side/2 |
| 2116 |
n = x.shape[0] |
| 2117 |
dx = side/n |
| 2118 |
dV = dx**3 |
| 2119 |
V = side**3 |
| 2120 |
x_ = x[:,0,0] |
| 2121 |
sigma /= 4. #to make compatible with e2pdb2mrc/chimera sigma |
| 2122 |
shift = np.ones(3)*dx/2. |
| 2123 |
print("\n Calculate density map from PDB... ") |
| 2124 |
values = np.zeros(x.shape) |
| 2125 |
for i in range(pdb.coords.shape[0]): |
| 2126 |
if ignore_waters and pdb.resname[i]=="HOH": |
| 2127 |
continue |
| 2128 |
sys.stdout.write("\r% 5i / % 5i atoms" % (i+1,pdb.coords.shape[0])) |
| 2129 |
sys.stdout.flush() |
| 2130 |
#this will cut out the grid points that are near the atom |
| 2131 |
#first, get the min and max distances for each dimension |
| 2132 |
#also, convert those distances to indices by dividing by dx |
| 2133 |
xa, ya, za = pdb.coords[i] # for convenience, store up x,y,z coordinates of atom |
| 2134 |
xmin = int(np.floor((xa-r)/dx)) + n//2 |
| 2135 |
xmax = int(np.ceil((xa+r)/dx)) + n//2 |
| 2136 |
ymin = int(np.floor((ya-r)/dx)) + n//2 |
| 2137 |
ymax = int(np.ceil((ya+r)/dx)) + n//2 |
| 2138 |
zmin = int(np.floor((za-r)/dx)) + n//2 |
| 2139 |
zmax = int(np.ceil((za+r)/dx)) + n//2 |
| 2140 |
#handle edges |
| 2141 |
xmin = max([xmin,0]) |
| 2142 |
xmax = min([xmax,n]) |
| 2143 |
ymin = max([ymin,0]) |
| 2144 |
ymax = min([ymax,n]) |
| 2145 |
zmin = max([zmin,0]) |
| 2146 |
zmax = min([zmax,n]) |
| 2147 |
#now lets create a slice object for convenience |
| 2148 |
slc = np.s_[xmin:xmax,ymin:ymax,zmin:zmax] |
| 2149 |
nx = xmax-xmin |
| 2150 |
ny = ymax-ymin |
| 2151 |
nz = zmax-zmin |
| 2152 |
#now lets create a column stack of coordinates for the cropped grid |
| 2153 |
xyz = np.column_stack((x[slc].ravel(),y[slc].ravel(),z[slc].ravel())) |
| 2154 |
dist = spatial.distance.cdist(pdb.coords[None,i]-shift, xyz) |
| 2155 |
dist *= dist |
| 2156 |
tmpvalues = pdb.nelectrons[i]*1./np.sqrt(2*np.pi*sigma**2) * np.exp(-dist[0]/(2*sigma**2)) |
| 2157 |
values[slc] += tmpvalues.reshape(nx,ny,nz) |
| 2158 |
print() |
| 2159 |
return values |
| 2160 |
|
| 2161 |
def pdb2map_multigauss(pdb,x,y,z,r=20.0,ignore_waters=True): |
| 2162 |
"""5-term gaussian sum at coordinate locations using Cromer-Mann coefficients. |
| 2163 |
|
| 2164 |
This fastgauss function only calculates the values at |
| 2165 |
grid points near the atom for speed. |
| 2166 |
|
| 2167 |
pdb - instance of PDB class (required) |
| 2168 |
x,y,z - meshgrids for x, y, and z (required) |
| 2169 |
sigma - width of Gaussian, i.e. effectively resolution |
| 2170 |
r - maximum distance from atom to calculate density |
| 2171 |
""" |
| 2172 |
side = x[-1,0,0] - x[0,0,0] |
| 2173 |
halfside = side/2 |
| 2174 |
n = x.shape[0] |
| 2175 |
dx = side/n |
| 2176 |
dV = dx**3 |
| 2177 |
V = side**3 |
| 2178 |
x_ = x[:,0,0] |
| 2179 |
shift = np.ones(3)*dx/2. |
| 2180 |
print("\n Calculate density map from PDB... ") |
| 2181 |
values = np.zeros(x.shape) |
| 2182 |
support = np.zeros(x.shape,dtype=bool) |
| 2183 |
for i in range(pdb.coords.shape[0]): |
| 2184 |
if ignore_waters and pdb.resname[i]=="HOH": |
| 2185 |
continue |
| 2186 |
sys.stdout.write("\r% 5i / % 5i atoms" % (i+1,pdb.coords.shape[0])) |
| 2187 |
sys.stdout.flush() |
| 2188 |
#this will cut out the grid points that are near the atom |
| 2189 |
#first, get the min and max distances for each dimension |
| 2190 |
#also, convert those distances to indices by dividing by dx |
| 2191 |
xa, ya, za = pdb.coords[i] # for convenience, store up x,y,z coordinates of atom |
| 2192 |
xmin = int(np.floor((xa-r)/dx)) + n//2 |
| 2193 |
xmax = int(np.ceil((xa+r)/dx)) + n//2 |
| 2194 |
ymin = int(np.floor((ya-r)/dx)) + n//2 |
| 2195 |
ymax = int(np.ceil((ya+r)/dx)) + n//2 |
| 2196 |
zmin = int(np.floor((za-r)/dx)) + n//2 |
| 2197 |
zmax = int(np.ceil((za+r)/dx)) + n//2 |
| 2198 |
#handle edges |
| 2199 |
xmin = max([xmin,0]) |
| 2200 |
xmax = min([xmax,n]) |
| 2201 |
ymin = max([ymin,0]) |
| 2202 |
ymax = min([ymax,n]) |
| 2203 |
zmin = max([zmin,0]) |
| 2204 |
zmax = min([zmax,n]) |
| 2205 |
#now lets create a slice object for convenience |
| 2206 |
slc = np.s_[xmin:xmax,ymin:ymax,zmin:zmax] |
| 2207 |
nx = xmax-xmin |
| 2208 |
ny = ymax-ymin |
| 2209 |
nz = zmax-zmin |
| 2210 |
#now lets create a column stack of coordinates for the cropped grid |
| 2211 |
xyz = np.column_stack((x[slc].ravel(),y[slc].ravel(),z[slc].ravel())) |
| 2212 |
dist = spatial.distance.cdist(pdb.coords[None,i]-shift, xyz) |
| 2213 |
dist *= dist |
| 2214 |
#tmpvalues = pdb.nelectrons[i]*1./np.sqrt(2*np.pi*sigma**2) * np.exp(-dist[0]/(2*sigma**2)) |
| 2215 |
try: |
| 2216 |
element = pdb.atomtype[i] |
| 2217 |
ffcoeff[element] |
| 2218 |
except: |
| 2219 |
element = pdb.atomname[i][0] |
| 2220 |
tmpvalues = realspace_formfactor(element=element,r=dist) |
| 2221 |
try: |
| 2222 |
values[slc] += tmpvalues.reshape(nx,ny,nz) |
| 2223 |
except: |
| 2224 |
print() |
| 2225 |
print("Atom %d outside boundary of cell ignored."%i) |
| 2226 |
support[slc] = True |
| 2227 |
print() |
| 2228 |
return values, support |
| 2229 |
|
| 2230 |
def pdb2map_FFT(pdb,x,y,z,radii=None,restrict=True): |
| 2231 |
"""Calculate electron density from pdb coordinates by FFT of Fs. |
| 2232 |
|
| 2233 |
pdb - instance of PDB class (required) |
| 2234 |
x,y,z - meshgrids for x, y, and z (required) |
| 2235 |
radii - float or list of radii of atoms in pdb (optional, uses spherical form factor rather than Kromer-Mann) |
| 2236 |
""" |
| 2237 |
radii = np.atleast_1d(radii) |
| 2238 |
side = x[-1,0,0] - x[0,0,0] |
| 2239 |
halfside = side/2 |
| 2240 |
n = x.shape[0] |
| 2241 |
dx = side/n |
| 2242 |
dV = dx**3 |
| 2243 |
V = side**3 |
| 2244 |
x_ = x[:,0,0] |
| 2245 |
df = 1/side |
| 2246 |
qx_ = np.fft.fftfreq(x_.size)*n*df*2*np.pi |
| 2247 |
qx, qy, qz = np.meshgrid(qx_,qx_,qx_,indexing='ij') |
| 2248 |
qr = np.sqrt(qx**2+qy**2+qz**2) |
| 2249 |
qmax = np.max(qr) |
| 2250 |
qstep = np.min(qr[qr>0]) |
| 2251 |
nbins = int(qmax/qstep) |
| 2252 |
qbins = np.linspace(0,nbins*qstep,nbins+1) |
| 2253 |
#create modified qbins and put qbins in center of bin rather than at left edge of bin. |
| 2254 |
qbinsc = np.copy(qbins) |
| 2255 |
#only move the non-zero terms, since the zeroth term should be at q=0. |
| 2256 |
qbinsc[1:] += qstep/2. |
| 2257 |
#create an array labeling each voxel according to which qbin it belongs |
| 2258 |
qbin_labels = np.searchsorted(qbins,qr,"right") |
| 2259 |
qbin_labels -= 1 |
| 2260 |
F = np.zeros(qr.shape,dtype=complex) |
| 2261 |
Fmean = np.zeros((len(qbins))) |
| 2262 |
natoms = pdb.coords.shape[0] |
| 2263 |
if radii[0] is None: |
| 2264 |
useradii = False |
| 2265 |
else: |
| 2266 |
useradii = True |
| 2267 |
radii = np.ones(radii.size)*radii |
| 2268 |
for i in range(pdb.natoms): |
| 2269 |
sys.stdout.write("\r% 5i / % 5i atoms" % (i+1,pdb.natoms)) |
| 2270 |
sys.stdout.flush() |
| 2271 |
if useradii: |
| 2272 |
F += sphere(q=qr, R=radii[i], I0=pdb.nelectrons[i],amp=True) * np.exp(-1j * (qx*pdb.coords[i,0] + qy*pdb.coords[i,1] + qz*pdb.coords[i,2])) |
| 2273 |
else: |
| 2274 |
F += formfactor(element=pdb.atomtype[i],q=qr) * np.exp(-1j * (qx*pdb.coords[i,0] + qy*pdb.coords[i,1] + qz*pdb.coords[i,2])) |
| 2275 |
print() |
| 2276 |
print("Total number of electrons = %f " % np.abs(F[0,0,0])) |
| 2277 |
qbin_labels = np.zeros(F.shape, dtype=int) |
| 2278 |
qbin_labels = np.digitize(qr, qbins) |
| 2279 |
qbin_labels -= 1 |
| 2280 |
Imean = ndimage.mean(np.abs(F)**2, labels=qbin_labels, index=np.arange(0,qbin_labels.max()+1)) |
| 2281 |
rho = np.fft.ifftn(F,x.shape).real |
| 2282 |
rho[rho<0] = 0 |
| 2283 |
#need to shift rho to center of grid, since FFT is offset by half a grid length |
| 2284 |
shift = np.array(rho.shape)/2 |
| 2285 |
shift -= 1 |
| 2286 |
rho = np.roll(np.roll(np.roll(rho, shift[0], axis=0), shift[1], axis=1), shift[2], axis=2) |
| 2287 |
if restrict: |
| 2288 |
xyz = np.column_stack([x.flat,y.flat,z.flat]) |
| 2289 |
pdb.coords -= np.ones(3)*dx/2. |
| 2290 |
pdbidx = pdb2support(pdb, xyz=xyz,probe=0.0) |
| 2291 |
rho[~pdbidx] = 0.0 |
| 2292 |
return rho, pdbidx |
| 2293 |
|
| 2294 |
def pdb2support(pdb,xyz,probe=0.0): |
| 2295 |
"""Calculate support from pdb coordinates.""" |
| 2296 |
n = int(round(xyz.shape[0]**(1/3.))) |
| 2297 |
support = np.zeros((n,n,n),dtype=bool) |
| 2298 |
xyz_nearby = [] |
| 2299 |
xyz_nearby_i = [] |
| 2300 |
for i in range(pdb.natoms): |
| 2301 |
sys.stdout.write("\r% 5i / % 5i" % (i+1,pdb.natoms)) |
| 2302 |
sys.stdout.flush() |
| 2303 |
xyz_nearby_i.append(np.where(spatial.distance.cdist(pdb.coords[i,None],xyz) < 1.7+probe)[1]) |
| 2304 |
xyz_nearby_i = np.unique(np.concatenate(xyz_nearby_i)) |
| 2305 |
support[np.unravel_index(xyz_nearby_i,support.shape)] = True |
| 2306 |
return support |
| 2307 |
|
| 2308 |
def sphere(R, q=np.linspace(0,0.5,501), I0=1.,amp=False): |
| 2309 |
"""Calculate the scattering of a uniform sphere.""" |
| 2310 |
q = np.atleast_1d(q) |
| 2311 |
a = np.where(q==0.0,1.0,(3 * (np.sin(q*R)-q*R*np.cos(q*R))/(q*R)**3)) |
| 2312 |
if np.isnan(a).any(): |
| 2313 |
a[np.where(np.isnan(a))] = 1. |
| 2314 |
if amp: |
| 2315 |
return I0 * a |
| 2316 |
else: |
| 2317 |
return I0 * a**2 |
| 2318 |
|
| 2319 |
def formfactor(element, q=(np.arange(500)+1)/1000.): |
| 2320 |
"""Calculate atomic form factors""" |
| 2321 |
q = np.atleast_1d(q) |
| 2322 |
ff = np.zeros(q.shape) |
| 2323 |
for i in range(4): |
| 2324 |
ff += ffcoeff[element]['a'][i] * np.exp(-ffcoeff[element]['b'][i]*(q/(4*np.pi))**2) |
| 2325 |
ff += ffcoeff[element]['c'] |
| 2326 |
return ff |
| 2327 |
|
| 2328 |
def realspace_formfactor(element, r=(np.arange(501))/1000.): |
| 2329 |
"""Calculate real space atomic form factors (just testing this out)""" |
| 2330 |
r = np.atleast_1d(r) |
| 2331 |
ff = np.zeros(r.shape) |
| 2332 |
for i in range(4): |
| 2333 |
ai = ffcoeff[element]['a'][i] |
| 2334 |
bi = ffcoeff[element]['b'][i] |
| 2335 |
#print i, ai, bi, ffcoeff[element]['c'] |
| 2336 |
#print i, 4*ai * (np.pi**3/bi)**0.5 |
| 2337 |
ff += 4*ai * (np.pi**3/bi)**0.5 * np.exp(-(2*np.pi*r)**2/bi) |
| 2338 |
i = np.where((r==0)) |
| 2339 |
ff += signal.unit_impulse(r.shape, i) * ffcoeff[element]['c'] |
| 2340 |
return ff |
| 2341 |
|
| 2342 |
def denss_3DFs(rho_start, dmax, ne=None, voxel=5., oversampling=3., positivity=True, |
| 2343 |
output="map", steps=2001, seed=None, shrinkwrap=True, shrinkwrap_sigma_start=3, |
| 2344 |
shrinkwrap_sigma_end=1.5, shrinkwrap_sigma_decay=0.99, shrinkwrap_threshold_fraction=0.2, |
| 2345 |
shrinkwrap_iter=20, shrinkwrap_minstep=50, write_freq=100,support=None, |
| 2346 |
enforce_connectivity=True, enforce_connectivity_steps=[6000],quiet=False): |
| 2347 |
"""Calculate electron density from starting map by refining phases only.""" |
| 2348 |
D = dmax |
| 2349 |
side = oversampling*D |
| 2350 |
halfside = side/2 |
| 2351 |
n = int(side/voxel) |
| 2352 |
#want n to be even for speed/memory optimization with the FFT, ideally a power of 2, but wont enforce that |
| 2353 |
if n%2==1: n += 1 |
| 2354 |
#store n for later use if needed |
| 2355 |
nbox = n |
| 2356 |
dx = side/n |
| 2357 |
dV = dx**3 |
| 2358 |
V = side**3 |
| 2359 |
x_ = np.linspace(-halfside,halfside,n) |
| 2360 |
x,y,z = np.meshgrid(x_,x_,x_,indexing='ij') |
| 2361 |
r = np.sqrt(x**2 + y**2 + z**2) |
| 2362 |
df = 1/side |
| 2363 |
qx_ = np.fft.fftfreq(x_.size)*n*df*2*np.pi |
| 2364 |
qz_ = np.fft.rfftfreq(x_.size)*n*df*2*np.pi |
| 2365 |
qx, qy, qz = np.meshgrid(qx_,qx_,qx_,indexing='ij') |
| 2366 |
qr = np.sqrt(qx**2+qy**2+qz**2) |
| 2367 |
qmax = np.max(qr) |
| 2368 |
qstep = np.min(qr[qr>0]) |
| 2369 |
nbins = int(qmax/qstep) |
| 2370 |
qbins = np.linspace(0,nbins*qstep,nbins+1) |
| 2371 |
#create modified qbins and put qbins in center of bin rather than at left edge of bin. |
| 2372 |
qbinsc = np.copy(qbins) |
| 2373 |
qbinsc[1:] += qstep/2. |
| 2374 |
#create an array labeling each voxel according to which qbin it belongs |
| 2375 |
qbin_labels = np.searchsorted(qbins,qr,"right") |
| 2376 |
qbin_labels -= 1 |
| 2377 |
if steps == 'None' or steps is None or steps < 1: |
| 2378 |
steps = int(shrinkwrap_iter * (np.log(shrinkwrap_sigma_end/shrinkwrap_sigma_start)/np.log(shrinkwrap_sigma_decay)) + shrinkwrap_minstep) |
| 2379 |
steps += 3000 |
| 2380 |
else: |
| 2381 |
steps = np.int(steps) |
| 2382 |
Imean = np.zeros((steps+1,len(qbins))) |
| 2383 |
chi = np.zeros((steps+1)) |
| 2384 |
rg = np.zeros((steps+1)) |
| 2385 |
supportV = np.zeros((steps+1)) |
| 2386 |
chibest = np.inf |
| 2387 |
usesupport = True |
| 2388 |
if support is None: |
| 2389 |
support = np.ones(x.shape,dtype=bool) |
| 2390 |
else: |
| 2391 |
support = support.astype(bool) |
| 2392 |
update_support = True |
| 2393 |
sigma = shrinkwrap_sigma_start |
| 2394 |
|
| 2395 |
rho = rho_start |
| 2396 |
F = np.fft.fftn(rho) |
| 2397 |
Amp = np.abs(F) |
| 2398 |
|
| 2399 |
if not quiet: |
| 2400 |
print("\n Step Chi2 Rg Support Volume") |
| 2401 |
print(" ----- --------- ------- --------------") |
| 2402 |
|
| 2403 |
for j in range(steps): |
| 2404 |
F = np.fft.fftn(rho) |
| 2405 |
#APPLY RECIPROCAL SPACE RESTRAINTS |
| 2406 |
#calculate spherical average of intensities from 3D Fs |
| 2407 |
I3D = np.abs(F)**2 |
| 2408 |
Imean[j] = ndimage.mean(I3D, labels=qbin_labels, index=np.arange(0,qbin_labels.max()+1)) |
| 2409 |
#scale Fs to match data |
| 2410 |
F *= Amp/np.abs(F) |
| 2411 |
chi[j] = 1.0 |
| 2412 |
#APPLY REAL SPACE RESTRAINTS |
| 2413 |
rhoprime = np.fft.ifftn(F,rho.shape) |
| 2414 |
rhoprime = rhoprime.real |
| 2415 |
if j%write_freq == 0: |
| 2416 |
write_mrc(rhoprime/dV,side,output+"_current.mrc") |
| 2417 |
rg[j] = rho2rg(rhoprime,r=r,support=support,dx=dx) |
| 2418 |
newrho = np.zeros_like(rho) |
| 2419 |
#Error Reduction |
| 2420 |
newrho[support] = rhoprime[support] |
| 2421 |
newrho[~support] = 0.0 |
| 2422 |
#enforce positivity by making all negative density points zero. |
| 2423 |
if positivity: |
| 2424 |
netmp = np.sum(newrho) |
| 2425 |
newrho[newrho<0] = 0.0 |
| 2426 |
if np.sum(newrho) != 0: |
| 2427 |
newrho *= netmp / np.sum(newrho) |
| 2428 |
supportV[j] = np.sum(support)*dV |
| 2429 |
|
| 2430 |
if not quiet: |
| 2431 |
sys.stdout.write("\r% 5i % 4.2e % 3.2f % 5i " % (j, chi[j], rg[j], supportV[j])) |
| 2432 |
sys.stdout.flush() |
| 2433 |
|
| 2434 |
rho = newrho |
| 2435 |
|
| 2436 |
if not quiet: |
| 2437 |
print() |
| 2438 |
|
| 2439 |
F = np.fft.fftn(rho) |
| 2440 |
#calculate spherical average intensity from 3D Fs |
| 2441 |
Imean[j+1] = ndimage.mean(np.abs(F)**2, labels=qbin_labels, index=np.arange(0,qbin_labels.max()+1)) |
| 2442 |
#scale Fs to match data |
| 2443 |
F *= Amp/np.abs(F) |
| 2444 |
rho = np.fft.ifftn(F,rho.shape) |
| 2445 |
rho = rho.real |
| 2446 |
|
| 2447 |
#scale total number of electrons |
| 2448 |
if ne is not None: |
| 2449 |
rho *= ne / np.sum(rho) |
| 2450 |
|
| 2451 |
rg[j+1] = rho2rg(rho=rho,r=r,support=support,dx=dx) |
| 2452 |
supportV[j+1] = supportV[j] |
| 2453 |
|
| 2454 |
#change rho to be the electron density in e-/angstroms^3, rather than number of electrons, |
| 2455 |
#which is what the FFT assumes |
| 2456 |
rho /= dV |
| 2457 |
|
| 2458 |
return rho |
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
electrons = {'H': 1,'HE': 2,'He': 2,'LI': 3,'Li': 3,'BE': 4,'Be': 4,'B': 5,'C': 6,'N': 7,'O': 8,'F': 9,'NE': 10,'Ne': 10,'NA': 11,'Na': 11,'MG': 12,'Mg': 12,'AL': 13,'Al': 13,'SI': 14,'Si': 14,'P': 15,'S': 16,'CL': 17,'Cl': 17,'AR': 18,'Ar': 18,'K': 19,'CA': 20,'Ca': 20,'SC': 21,'Sc': 21,'TI': 22,'Ti': 22,'V': 23,'CR': 24,'Cr': 24,'MN': 25,'Mn': 25,'FE': 26,'Fe': 26,'CO': 27,'Co': 27,'NI': 28,'Ni': 28,'CU': 29,'Cu': 29,'ZN': 30,'Zn': 30,'GA': 31,'Ga': 31,'GE': 32,'Ge': 32,'AS': 33,'As': 33,'SE': 34,'Se': 34,'Se': 34,'Se': 34,'BR': 35,'Br': 35,'KR': 36,'Kr': 36,'RB': 37,'Rb': 37,'SR': 38,'Sr': 38,'Y': 39,'ZR': 40,'Zr': 40,'NB': 41,'Nb': 41,'MO': 42,'Mo': 42,'TC': 43,'Tc': 43,'RU': 44,'Ru': 44,'RH': 45,'Rh': 45,'PD': 46,'Pd': 46,'AG': 47,'Ag': 47,'CD': 48,'Cd': 48,'IN': 49,'In': 49,'SN': 50,'Sn': 50,'SB': 51,'Sb': 51,'TE': 52,'Te': 52,'I': 53,'XE': 54,'Xe': 54,'CS': 55,'Cs': 55,'BA': 56,'Ba': 56,'LA': 57,'La': 57,'CE': 58,'Ce': 58,'PR': 59,'Pr': 59,'ND': 60,'Nd': 60,'PM': 61,'Pm': 61,'SM': 62,'Sm': 62,'EU': 63,'Eu': 63,'GD': 64,'Gd': 64,'TB': 65,'Tb': 65,'DY': 66,'Dy': 66,'HO': 67,'Ho': 67,'ER': 68,'Er': 68,'TM': 69,'Tm': 69,'YB': 70,'Yb': 70,'LU': 71,'Lu': 71,'HF': 72,'Hf': 72,'TA': 73,'Ta': 73,'W': 74,'RE': 75,'Re': 75,'OS': 76,'Os': 76,'IR': 77,'Ir': 77,'PT': 78,'Pt': 78,'AU': 79,'Au': 79,'HG': 80,'Hg': 80,'TL': 81,'Tl': 81,'PB': 82,'Pb': 82,'BI': 83,'Bi': 83,'PO': 84,'Po': 84,'AT': 85,'At': 85,'RN': 86,'Rn': 86,'FR': 87,'Fr': 87,'RA': 88,'Ra': 88,'AC': 89,'Ac': 89,'TH': 90,'Th': 90,'PA': 91,'Pa': 91,'U': 92,'NP': 93,'Np': 93,'PU': 94,'Pu': 94,'AM': 95,'Am': 95,'CM': 96,'Cm': 96,'BK': 97,'Bk': 97,'CF': 98,'Cf': 98,'ES': 99,'Es': 99,'FM': 100,'Fm': 100,'MD': 101,'Md': 101,'NO': 102,'No': 102,'LR': 103,'Lr': 103,'RF': 104,'Rf': 104,'DB': 105,'Db': 105,'SG': 106,'Sg': 106,'BH': 107,'Bh': 107,'HS': 108,'Hs': 108,'MT': 109,'Mt': 109} |
| 2463 |
|
| 2464 |
#form factors taken from http://lampx.tugraz.at/~hadley/ss1/crystaldiffraction/atomicformfactors/formfactors.php |
| 2465 |
ffcoeff = { |
| 2466 |
"H": {"a": [0.489918, 0.262003, 0.196767, 0.049879], "b": [20.6593, 7.74039, 49.5519, 2.20159], "c": 0.001305}, |
| 2467 |
"H1-": {"a": [0.897661, 0.565616, 0.415815, 0.116973], "b": [53.1368, 15.187, 186.576, 3.56709], "c": 0.002389}, |
| 2468 |
"He": {"a": [0.8734, 0.6309, 0.3112, 0.178], "b": [9.1037, 3.3568, 22.9276, 0.9821], "c": 0.0064}, |
| 2469 |
"Li": {"a": [1.1282, 0.7508, 0.6175, 0.4653], "b": [3.9546, 1.0524, 85.3905, 168.261], "c": 0.0377}, |
| 2470 |
"Li1+": {"a": [0.6968, 0.7888, 0.3414, 0.1563], "b": [4.6237, 1.9557, 0.6316, 10.0953], "c": 0.0167}, |
| 2471 |
"Be": {"a": [1.5919, 1.1278, 0.5391, 0.7029], "b": [43.6427, 1.8623, 103.483, 0.542], "c": 0.0385}, |
| 2472 |
"Be2+": {"a": [6.2603, 0.8849, 0.7993, 0.1647], "b": [0.0027, 0.8313, 2.2758, 5.1146], "c": -6.1092}, |
| 2473 |
"B": {"a": [2.0545, 1.3326, 1.0979, 0.7068], "b": [23.2185, 1.021, 60.3498, 0.1403], "c": -0.1932}, |
| 2474 |
"C": {"a": [2.31, 1.02, 1.5886, 0.865], "b": [20.8439, 10.2075, 0.5687, 51.6512], "c": 0.2156}, |
| 2475 |
"Cval": {"a": [2.26069, 1.56165, 1.05075, 0.839259], "b": [22.6907, 0.656665, 9.75618, 55.5949], "c": 0.286977}, |
| 2476 |
"N": {"a": [12.2126, 3.1322, 2.0125, 1.1663], "b": [0.0057, 9.8933, 28.9975, 0.5826], "c": -11.529}, |
| 2477 |
"O": {"a": [3.0485, 2.2868, 1.5463, 0.867], "b": [13.2771, 5.7011, 0.3239, 32.9089], "c": 0.2508}, |
| 2478 |
"O1-": {"a": [4.1916, 1.63969, 1.52673, -20.307], "b": [12.8573, 4.17236, 47.0179, -0.01404], "c": 21.9412}, |
| 2479 |
"F": {"a": [3.5392, 2.6412, 1.517, 1.0243], "b": [10.2825, 4.2944, 0.2615, 26.1476], "c": 0.2776}, |
| 2480 |
"F1-": {"a": [3.6322, 3.51057, 1.26064, 0.940706], "b": [5.27756, 14.7353, 0.442258, 47.3437], "c": 0.653396}, |
| 2481 |
"Ne": {"a": [3.9553, 3.1125, 1.4546, 1.1251], "b": [8.4042, 3.4262, 0.2306, 21.7184], "c": 0.3515}, |
| 2482 |
"Na": {"a": [4.7626, 3.1736, 1.2674, 1.1128], "b": [3.285, 8.8422, 0.3136, 129.424], "c": 0.676}, |
| 2483 |
"Na1+": {"a": [3.2565, 3.9362, 1.3998, 1.0032], "b": [2.6671, 6.1153, 0.2001, 14.039], "c": 0.404}, |
| 2484 |
"Mg": {"a": [5.4204, 2.1735, 1.2269, 2.3073], "b": [2.8275, 79.2611, 0.3808, 7.1937], "c": 0.8584}, |
| 2485 |
"Mg2+": {"a": [3.4988, 3.8378, 1.3284, 0.8497], "b": [2.1676, 4.7542, 0.185, 10.1411], "c": 0.4853}, |
| 2486 |
"Al": {"a": [6.4202, 1.9002, 1.5936, 1.9646], "b": [3.0387, 0.7426, 31.5472, 85.0886], "c": 1.1151}, |
| 2487 |
"Al3+": {"a": [4.17448, 3.3876, 1.20296, 0.528137], "b": [1.93816, 4.14553, 0.228753, 8.28524], "c": 0.706786}, |
| 2488 |
"Siv": {"a": [6.2915, 3.0353, 1.9891, 1.541], "b": [2.4386, 32.3337, 0.6785, 81.6937], "c": 1.1407}, |
| 2489 |
"Sival": {"a": [5.66269, 3.07164, 2.62446, 1.3932], "b": [2.6652, 38.6634, 0.916946, 93.5458], "c": 1.24707}, |
| 2490 |
"Si4+": {"a": [4.43918, 3.20345, 1.19453, 0.41653], "b": [1.64167, 3.43757, 0.2149, 6.65365], "c": 0.746297}, |
| 2491 |
"P": {"a": [6.4345, 4.1791, 1.78, 1.4908], "b": [1.9067, 27.157, 0.526, 68.1645], "c": 1.1149}, |
| 2492 |
"S": {"a": [6.9053, 5.2034, 1.4379, 1.5863], "b": [1.4679, 22.2151, 0.2536, 56.172], "c": 0.8669}, |
| 2493 |
"Cl": {"a": [11.4604, 7.1964, 6.2556, 1.6455], "b": [0.0104, 1.1662, 18.5194, 47.7784], "c": -9.5574}, |
| 2494 |
"Cl1-": {"a": [18.2915, 7.2084, 6.5337, 2.3386], "b": [0.0066, 1.1717, 19.5424, 60.4486], "c": -16.378}, |
| 2495 |
"Ar": {"a": [7.4845, 6.7723, 0.6539, 1.6442], "b": [0.9072, 14.8407, 43.8983, 33.3929], "c": 1.4445}, |
| 2496 |
"K": {"a": [8.2186, 7.4398, 1.0519, 0.8659], "b": [12.7949, 0.7748, 213.187, 41.6841], "c": 1.4228}, |
| 2497 |
"K1+": {"a": [7.9578, 7.4917, 6.359, 1.1915], "b": [12.6331, 0.7674, -0.002, 31.9128], "c": -4.9978}, |
| 2498 |
"Ca": {"a": [8.6266, 7.3873, 1.5899, 1.0211], "b": [10.4421, 0.6599, 85.7484, 178.437], "c": 1.3751}, |
| 2499 |
"Ca2+": {"a": [15.6348, 7.9518, 8.4372, 0.8537], "b": [-0.0074, 0.6089, 10.3116, 25.9905], "c": -14.875}, |
| 2500 |
"Sc": {"a": [9.189, 7.3679, 1.6409, 1.468], "b": [9.0213, 0.5729, 136.108, 51.3531], "c": 1.3329}, |
| 2501 |
"Sc3+": {"a": [13.4008, 8.0273, 1.65943, 1.57936], "b": [0.29854, 7.9629, -0.28604, 16.0662], "c": -6.6667}, |
| 2502 |
"Ti": {"a": [9.7595, 7.3558, 1.6991, 1.9021], "b": [7.8508, 0.5, 35.6338, 116.105], "c": 1.2807}, |
| 2503 |
"Ti2+": {"a": [9.11423, 7.62174, 2.2793, 0.087899], "b": [7.5243, 0.457585, 19.5361, 61.6558], "c": 0.897155}, |
| 2504 |
"Ti3+": {"a": [17.7344, 8.73816, 5.25691, 1.92134], "b": [0.22061, 7.04716, -0.15762, 15.9768], "c": -14.652}, |
| 2505 |
"Ti4+": {"a": [19.5114, 8.23473, 2.01341, 1.5208], "b": [0.178847, 6.67018, -0.29263, 12.9464], "c": -13.28}, |
| 2506 |
"V": {"a": [10.2971, 7.3511, 2.0703, 2.0571], "b": [6.8657, 0.4385, 26.8938, 102.478], "c": 1.2199}, |
| 2507 |
"V2+": {"a": [10.106, 7.3541, 2.2884, 0.0223], "b": [6.8818, 0.4409, 20.3004, 115.122], "c": 1.2298}, |
| 2508 |
"V3+": {"a": [9.43141, 7.7419, 2.15343, 0.016865], "b": [6.39535, 0.383349, 15.1908, 63.969], "c": 0.656565}, |
| 2509 |
"V5+": {"a": [15.6887, 8.14208, 2.03081, -9.576], "b": [0.679003, 5.40135, 9.97278, 0.940464], "c": 1.7143}, |
| 2510 |
"Cr": {"a": [10.6406, 7.3537, 3.324, 1.4922], "b": [6.1038, 0.392, 20.2626, 98.7399], "c": 1.1832}, |
| 2511 |
"Cr2+": {"a": [9.54034, 7.7509, 3.58274, 0.509107], "b": [5.66078, 0.344261, 13.3075, 32.4224], "c": 0.616898}, |
| 2512 |
"Cr3+": {"a": [9.6809, 7.81136, 2.87603, 0.113575], "b": [5.59463, 0.334393, 12.8288, 32.8761], "c": 0.518275}, |
| 2513 |
"Mn": {"a": [11.2819, 7.3573, 3.0193, 2.2441], "b": [5.3409, 0.3432, 17.8674, 83.7543], "c": 1.0896}, |
| 2514 |
"Mn2+": {"a": [10.8061, 7.362, 3.5268, 0.2184], "b": [5.2796, 0.3435, 14.343, 41.3235], "c": 1.0874}, |
| 2515 |
"Mn3+": {"a": [9.84521, 7.87194, 3.56531, 0.323613], "b": [4.91797, 0.294393, 10.8171, 24.1281], "c": 0.393974}, |
| 2516 |
"Mn4+": {"a": [9.96253, 7.97057, 2.76067, 0.054447], "b": [4.8485, 0.283303, 10.4852, 27.573], "c": 0.251877}, |
| 2517 |
"Fe": {"a": [11.7695, 7.3573, 3.5222, 2.3045], "b": [4.7611, 0.3072, 15.3535, 76.8805], "c": 1.0369}, |
| 2518 |
"Fe2+": {"a": [11.0424, 7.374, 4.1346, 0.4399], "b": [4.6538, 0.3053, 12.0546, 31.2809], "c": 1.0097}, |
| 2519 |
"Fe3+": {"a": [11.1764, 7.3863, 3.3948, 0.0724], "b": [4.6147, 0.3005, 11.6729, 38.5566], "c": 0.9707}, |
| 2520 |
"Co": {"a": [12.2841, 7.3409, 4.0034, 2.3488], "b": [4.2791, 0.2784, 13.5359, 71.1692], "c": 1.0118}, |
| 2521 |
"Co2+": {"a": [11.2296, 7.3883, 4.7393, 0.7108], "b": [4.1231, 0.2726, 10.2443, 25.6466], "c": 0.9324}, |
| 2522 |
"Co3+": {"a": [10.338, 7.88173, 4.76795, 0.725591], "b": [3.90969, 0.238668, 8.35583, 18.3491], "c": 0.286667}, |
| 2523 |
"Ni": {"a": [12.8376, 7.292, 4.4438, 2.38], "b": [3.8785, 0.2565, 12.1763, 66.3421], "c": 1.0341}, |
| 2524 |
"Ni2+": {"a": [11.4166, 7.4005, 5.3442, 0.9773], "b": [3.6766, 0.2449, 8.873, 22.1626], "c": 0.8614}, |
| 2525 |
"Ni3+": {"a": [10.7806, 7.75868, 5.22746, 0.847114], "b": [3.5477, 0.22314, 7.64468, 16.9673], "c": 0.386044}, |
| 2526 |
"Cu": {"a": [13.338, 7.1676, 5.6158, 1.6735], "b": [3.5828, 0.247, 11.3966, 64.8126], "c": 1.191}, |
| 2527 |
"Cu1+": {"a": [11.9475, 7.3573, 6.2455, 1.5578], "b": [3.3669, 0.2274, 8.6625, 25.8487], "c": 0.89}, |
| 2528 |
"Cu2+": {"a": [11.8168, 7.11181, 5.78135, 1.14523], "b": [3.37484, 0.244078, 7.9876, 19.897], "c": 1.14431}, |
| 2529 |
"Zn": {"a": [14.0743, 7.0318, 5.1652, 2.41], "b": [3.2655, 0.2333, 10.3163, 58.7097], "c": 1.3041}, |
| 2530 |
"Zn2+": {"a": [11.9719, 7.3862, 6.4668, 1.394], "b": [2.9946, 0.2031, 7.0826, 18.0995], "c": 0.7807}, |
| 2531 |
"Ga": {"a": [15.2354, 6.7006, 4.3591, 2.9623], "b": [3.0669, 0.2412, 10.7805, 61.4135], "c": 1.7189}, |
| 2532 |
"Ga3+": {"a": [12.692, 6.69883, 6.06692, 1.0066], "b": [2.81262, 0.22789, 6.36441, 14.4122], "c": 1.53545}, |
| 2533 |
"Ge": {"a": [16.0816, 6.3747, 3.7068, 3.683], "b": [2.8509, 0.2516, 11.4468, 54.7625], "c": 2.1313}, |
| 2534 |
"Ge4+": {"a": [12.9172, 6.70003, 6.06791, 0.859041], "b": [2.53718, 0.205855, 5.47913, 11.603], "c": 1.45572}, |
| 2535 |
"As": {"a": [16.6723, 6.0701, 3.4313, 4.2779], "b": [2.6345, 0.2647, 12.9479, 47.7972], "c": 2.531}, |
| 2536 |
"Se": {"a": [17.0006, 5.8196, 3.9731, 4.3543], "b": [2.4098, 0.2726, 15.2372, 43.8163], "c": 2.8409}, |
| 2537 |
"Br": {"a": [17.1789, 5.2358, 5.6377, 3.9851], "b": [2.1723, 16.5796, 0.2609, 41.4328], "c": 2.9557}, |
| 2538 |
"Br1-": {"a": [17.1718, 6.3338, 5.5754, 3.7272], "b": [2.2059, 19.3345, 0.2871, 58.1535], "c": 3.1776}, |
| 2539 |
"Kr": {"a": [17.3555, 6.7286, 5.5493, 3.5375], "b": [1.9384, 16.5623, 0.2261, 39.3972], "c": 2.825}, |
| 2540 |
"Rb": {"a": [17.1784, 9.6435, 5.1399, 1.5292], "b": [1.7888, 17.3151, 0.2748, 164.934], "c": 3.4873}, |
| 2541 |
"Rb1+": {"a": [17.5816, 7.6598, 5.8981, 2.7817], "b": [1.7139, 14.7957, 0.1603, 31.2087], "c": 2.0782}, |
| 2542 |
"Sr": {"a": [17.5663, 9.8184, 5.422, 2.6694], "b": [1.5564, 14.0988, 0.1664, 132.376], "c": 2.5064}, |
| 2543 |
"Sr2+": {"a": [18.0874, 8.1373, 2.5654, -34.193], "b": [1.4907, 12.6963, 24.5651, -0.0138], "c": 41.4025}, |
| 2544 |
"Y": {"a": [17.776, 10.2946, 5.72629, 3.26588], "b": [1.4029, 12.8006, 0.125599, 104.354], "c": 1.91213}, |
| 2545 |
"Y3+": {"a": [17.9268, 9.1531, 1.76795, -33.108], "b": [1.35417, 11.2145, 22.6599, -0.01319], "c": 40.2602}, |
| 2546 |
"Zr": {"a": [17.8765, 10.948, 5.41732, 3.65721], "b": [1.27618, 11.916, 0.117622, 87.6627], "c": 2.06929}, |
| 2547 |
"Zr4+": {"a": [18.1668, 10.0562, 1.01118, -2.6479], "b": [1.2148, 10.1483, 21.6054, -0.10276], "c": 9.41454}, |
| 2548 |
"Nb": {"a": [17.6142, 12.0144, 4.04183, 3.53346], "b": [1.18865, 11.766, 0.204785, 69.7957], "c": 3.75591}, |
| 2549 |
"Nb3+": {"a": [19.8812, 18.0653, 11.0177, 1.94715], "b": [0.019175, 1.13305, 10.1621, 28.3389], "c": -12.912}, |
| 2550 |
"Nb5+": {"a": [17.9163, 13.3417, 10.799, 0.337905], "b": [1.12446, 0.028781, 9.28206, 25.7228], "c": -6.3934}, |
| 2551 |
"Mo": {"a": [3.7025, 17.2356, 12.8876, 3.7429], "b": [0.2772, 1.0958, 11.004, 61.6584], "c": 4.3875}, |
| 2552 |
"Mo3+": {"a": [21.1664, 18.2017, 11.7423, 2.30951], "b": [0.014734, 1.03031, 9.53659, 26.6307], "c": -14.421}, |
| 2553 |
"Mo5+": {"a": [21.0149, 18.0992, 11.4632, 0.740625], "b": [0.014345, 1.02238, 8.78809, 23.3452], "c": -14.316}, |
| 2554 |
"Mo6+": {"a": [17.8871, 11.175, 6.57891, 0], "b": [1.03649, 8.48061, 0.058881, 0], "c": 0.344941}, |
| 2555 |
"Tc": {"a": [19.1301, 11.0948, 4.64901, 2.71263], "b": [0.864132, 8.14487, 21.5707, 86.8472], "c": 5.40428}, |
| 2556 |
"Ru": {"a": [19.2674, 12.9182, 4.86337, 1.56756], "b": [0.80852, 8.43467, 24.7997, 94.2928], "c": 5.37874}, |
| 2557 |
"Ru3+": {"a": [18.5638, 13.2885, 9.32602, 3.00964], "b": [0.847329, 8.37164, 0.017662, 22.887], "c": -3.1892}, |
| 2558 |
"Ru4+": {"a": [18.5003, 13.1787, 4.71304, 2.18535], "b": [0.844582, 8.12534, 0.36495, 20.8504], "c": 1.42357}, |
| 2559 |
"Rh": {"a": [19.2957, 14.3501, 4.73425, 1.28918], "b": [0.751536, 8.21758, 25.8749, 98.6062], "c": 5.328}, |
| 2560 |
"Rh3+": {"a": [18.8785, 14.1259, 3.32515, -6.1989], "b": [0.764252, 7.84438, 21.2487, -0.01036], "c": 11.8678}, |
| 2561 |
"Rh4+": {"a": [18.8545, 13.9806, 2.53464, -5.6526], "b": [0.760825, 7.62436, 19.3317, -0.0102], "c": 11.2835}, |
| 2562 |
"Pd": {"a": [19.3319, 15.5017, 5.29537, 0.605844], "b": [0.698655, 7.98929, 25.2052, 76.8986], "c": 5.26593}, |
| 2563 |
"Pd2+": {"a": [19.1701, 15.2096, 4.32234, 0], "b": [0.696219, 7.55573, 22.5057, 0], "c": 5.2916}, |
| 2564 |
"Pd4+": {"a": [19.2493, 14.79, 2.89289, -7.9492], "b": [0.683839, 7.14833, 17.9144, 0.005127], "c": 13.0174}, |
| 2565 |
"Ag": {"a": [19.2808, 16.6885, 4.8045, 1.0463], "b": [0.6446, 7.4726, 24.6605, 99.8156], "c": 5.179}, |
| 2566 |
"Ag1+": {"a": [19.1812, 15.9719, 5.27475, 0.357534], "b": [0.646179, 7.19123, 21.7326, 66.1147], "c": 5.21572}, |
| 2567 |
"Ag2+": {"a": [19.1643, 16.2456, 4.3709, 0], "b": [0.645643, 7.18544, 21.4072, 0], "c": 5.21404}, |
| 2568 |
"Cd": {"a": [19.2214, 17.6444, 4.461, 1.6029], "b": [0.5946, 6.9089, 24.7008, 87.4825], "c": 5.0694}, |
| 2569 |
"Cd2+": {"a": [19.1514, 17.2535, 4.47128, 0], "b": [0.597922, 6.80639, 20.2521, 0], "c": 5.11937}, |
| 2570 |
"In": {"a": [19.1624, 18.5596, 4.2948, 2.0396], "b": [0.5476, 6.3776, 25.8499, 92.8029], "c": 4.9391}, |
| 2571 |
"In3+": {"a": [19.1045, 18.1108, 3.78897, 0], "b": [0.551522, 6.3247, 17.3595, 0], "c": 4.99635}, |
| 2572 |
"Sn": {"a": [19.1889, 19.1005, 4.4585, 2.4663], "b": [5.8303, 0.5031, 26.8909, 83.9571], "c": 4.7821}, |
| 2573 |
"Sn2+": {"a": [19.1094, 19.0548, 4.5648, 0.487], "b": [0.5036, 5.8378, 23.3752, 62.2061], "c": 4.7861}, |
| 2574 |
"Sn4+": {"a": [18.9333, 19.7131, 3.4182, 0.0193], "b": [5.764, 0.4655, 14.0049, -0.7583], "c": 3.9182}, |
| 2575 |
"Sb": {"a": [19.6418, 19.0455, 5.0371, 2.6827], "b": [5.3034, 0.4607, 27.9074, 75.2825], "c": 4.5909}, |
| 2576 |
"Sb3+": {"a": [18.9755, 18.933, 5.10789, 0.288753], "b": [0.467196, 5.22126, 19.5902, 55.5113], "c": 4.69626}, |
| 2577 |
"Sb5+": {"a": [19.8685, 19.0302, 2.41253, 0], "b": [5.44853, 0.467973, 14.1259, 0], "c": 4.69263}, |
| 2578 |
"Te": {"a": [19.9644, 19.0138, 6.14487, 2.5239], "b": [4.81742, 0.420885, 28.5284, 70.8403], "c": 4.352}, |
| 2579 |
"I": {"a": [20.1472, 18.9949, 7.5138, 2.2735], "b": [4.347, 0.3814, 27.766, 66.8776], "c": 4.0712}, |
| 2580 |
"I1-": {"a": [20.2332, 18.997, 7.8069, 2.8868], "b": [4.3579, 0.3815, 29.5259, 84.9304], "c": 4.0714}, |
| 2581 |
"Xe": {"a": [20.2933, 19.0298, 8.9767, 1.99], "b": [3.9282, 0.344, 26.4659, 64.2658], "c": 3.7118}, |
| 2582 |
"Cs": {"a": [20.3892, 19.1062, 10.662, 1.4953], "b": [3.569, 0.3107, 24.3879, 213.904], "c": 3.3352}, |
| 2583 |
"Cs1+": {"a": [20.3524, 19.1278, 10.2821, 0.9615], "b": [3.552, 0.3086, 23.7128, 59.4565], "c": 3.2791}, |
| 2584 |
"Ba": {"a": [20.3361, 19.297, 10.888, 2.6959], "b": [3.216, 0.2756, 20.2073, 167.202], "c": 2.7731}, |
| 2585 |
"Ba2+": {"a": [20.1807, 19.1136, 10.9054, 0.77634], "b": [3.21367, 0.28331, 20.0558, 51.746], "c": 3.02902}, |
| 2586 |
"La": {"a": [20.578, 19.599, 11.3727, 3.28719], "b": [2.94817, 0.244475, 18.7726, 133.124], "c": 2.14678}, |
| 2587 |
"La3+": {"a": [20.2489, 19.3763, 11.6323, 0.336048], "b": [2.9207, 0.250698, 17.8211, 54.9453], "c": 2.4086}, |
| 2588 |
"Ce": {"a": [21.1671, 19.7695, 11.8513, 3.33049], "b": [2.81219, 0.226836, 17.6083, 127.113], "c": 1.86264}, |
| 2589 |
"Ce3+": {"a": [20.8036, 19.559, 11.9369, 0.612376], "b": [2.77691, 0.23154, 16.5408, 43.1692], "c": 2.09013}, |
| 2590 |
"Ce4+": {"a": [20.3235, 19.8186, 12.1233, 0.144583], "b": [2.65941, 0.21885, 15.7992, 62.2355], "c": 1.5918}, |
| 2591 |
"Pr": {"a": [22.044, 19.6697, 12.3856, 2.82428], "b": [2.77393, 0.222087, 16.7669, 143.644], "c": 2.0583}, |
| 2592 |
"Pr3+": {"a": [21.3727, 19.7491, 12.1329, 0.97518], "b": [2.6452, 0.214299, 15.323, 36.4065], "c": 1.77132}, |
| 2593 |
"Pr4+": {"a": [20.9413, 20.0539, 12.4668, 0.296689], "b": [2.54467, 0.202481, 14.8137, 45.4643], "c": 1.24285}, |
| 2594 |
"Nd": {"a": [22.6845, 19.6847, 12.774, 2.85137], "b": [2.66248, 0.210628, 15.885, 137.903], "c": 1.98486}, |
| 2595 |
"Nd3+": {"a": [21.961, 19.9339, 12.12, 1.51031], "b": [2.52722, 0.199237, 14.1783, 30.8717], "c": 1.47588}, |
| 2596 |
"Pm": {"a": [23.3405, 19.6095, 13.1235, 2.87516], "b": [2.5627, 0.202088, 15.1009, 132.721], "c": 2.02876}, |
| 2597 |
"Pm3+": {"a": [22.5527, 20.1108, 12.0671, 2.07492], "b": [2.4174, 0.185769, 13.1275, 27.4491], "c": 1.19499}, |
| 2598 |
"Sm": {"a": [24.0042, 19.4258, 13.4396, 2.89604], "b": [2.47274, 0.196451, 14.3996, 128.007], "c": 2.20963}, |
| 2599 |
"Sm3+": {"a": [23.1504, 20.2599, 11.9202, 2.71488], "b": [2.31641, 0.174081, 12.1571, 24.8242], "c": 0.954586}, |
| 2600 |
"Eu": {"a": [24.6274, 19.0886, 13.7603, 2.9227], "b": [2.3879, 0.1942, 13.7546, 123.174], "c": 2.5745}, |
| 2601 |
"Eu2+": {"a": [24.0063, 19.9504, 11.8034, 3.87243], "b": [2.27783, 0.17353, 11.6096, 26.5156], "c": 1.36389}, |
| 2602 |
"Eu3+": {"a": [23.7497, 20.3745, 11.8509, 3.26503], "b": [2.22258, 0.16394, 11.311, 22.9966], "c": 0.759344}, |
| 2603 |
"Gd": {"a": [25.0709, 19.0798, 13.8518, 3.54545], "b": [2.25341, 0.181951, 12.9331, 101.398], "c": 2.4196}, |
| 2604 |
"Gd3+": {"a": [24.3466, 20.4208, 11.8708, 3.7149], "b": [2.13553, 0.155525, 10.5782, 21.7029], "c": 0.645089}, |
| 2605 |
"Tb": {"a": [25.8976, 18.2185, 14.3167, 2.95354], "b": [2.24256, 0.196143, 12.6648, 115.362], "c": 3.58324}, |
| 2606 |
"Tb3+": {"a": [24.9559, 20.3271, 12.2471, 3.773], "b": [2.05601, 0.149525, 10.0499, 21.2773], "c": 0.691967}, |
| 2607 |
"Dy": {"a": [26.507, 17.6383, 14.5596, 2.96577], "b": [2.1802, 0.202172, 12.1899, 111.874], "c": 4.29728}, |
| 2608 |
"Dy3+": {"a": [25.5395, 20.2861, 11.9812, 4.50073], "b": [1.9804, 0.143384, 9.34972, 19.581], "c": 0.68969}, |
| 2609 |
"Ho": {"a": [26.9049, 17.294, 14.5583, 3.63837], "b": [2.07051, 0.19794, 11.4407, 92.6566], "c": 4.56796}, |
| 2610 |
"Ho3+": {"a": [26.1296, 20.0994, 11.9788, 4.93676], "b": [1.91072, 0.139358, 8.80018, 18.5908], "c": 0.852795}, |
| 2611 |
"Er": {"a": [27.6563, 16.4285, 14.9779, 2.98233], "b": [2.07356, 0.223545, 11.3604, 105.703], "c": 5.92046}, |
| 2612 |
"Er3+": {"a": [26.722, 19.7748, 12.1506, 5.17379], "b": [1.84659, 0.13729, 8.36225, 17.8974], "c": 1.17613}, |
| 2613 |
"Tm": {"a": [28.1819, 15.8851, 15.1542, 2.98706], "b": [2.02859, 0.238849, 10.9975, 102.961], "c": 6.75621}, |
| 2614 |
"Tm3+": {"a": [27.3083, 19.332, 12.3339, 5.38348], "b": [1.78711, 0.136974, 7.96778, 17.2922], "c": 1.63929}, |
| 2615 |
"Yb": {"a": [28.6641, 15.4345, 15.3087, 2.98963], "b": [1.9889, 0.257119, 10.6647, 100.417], "c": 7.56672}, |
| 2616 |
"Yb2+": {"a": [28.1209, 17.6817, 13.3335, 5.14657], "b": [1.78503, 0.15997, 8.18304, 20.39], "c": 3.70983}, |
| 2617 |
"Yb3+": {"a": [27.8917, 18.7614, 12.6072, 5.47647], "b": [1.73272, 0.13879, 7.64412, 16.8153], "c": 2.26001}, |
| 2618 |
"Lu": {"a": [28.9476, 15.2208, 15.1, 3.71601], "b": [1.90182, 9.98519, 0.261033, 84.3298], "c": 7.97628}, |
| 2619 |
"Lu3+": {"a": [28.4628, 18.121, 12.8429, 5.59415], "b": [1.68216, 0.142292, 7.33727, 16.3535], "c": 2.97573}, |
| 2620 |
"Hf": {"a": [29.144, 15.1726, 14.7586, 4.30013], "b": [1.83262, 9.5999, 0.275116, 72.029], "c": 8.58154}, |
| 2621 |
"Hf4+": {"a": [28.8131, 18.4601, 12.7285, 5.59927], "b": [1.59136, 0.128903, 6.76232, 14.0366], "c": 2.39699}, |
| 2622 |
"Ta": {"a": [29.2024, 15.2293, 14.5135, 4.76492], "b": [1.77333, 9.37046, 0.295977, 63.3644], "c": 9.24354}, |
| 2623 |
"Ta5+": {"a": [29.1587, 18.8407, 12.8268, 5.38695], "b": [1.50711, 0.116741, 6.31524, 12.4244], "c": 1.78555}, |
| 2624 |
"W": {"a": [29.0818, 15.43, 14.4327, 5.11982], "b": [1.72029, 9.2259, 0.321703, 57.056], "c": 9.8875}, |
| 2625 |
"W6+": {"a": [29.4936, 19.3763, 13.0544, 5.06412], "b": [1.42755, 0.104621, 5.93667, 11.1972], "c": 1.01074}, |
| 2626 |
"Re": {"a": [28.7621, 15.7189, 14.5564, 5.44174], "b": [1.67191, 9.09227, 0.3505, 52.0861], "c": 10.472}, |
| 2627 |
"Os": {"a": [28.1894, 16.155, 14.9305, 5.67589], "b": [1.62903, 8.97948, 0.382661, 48.1647], "c": 11.0005}, |
| 2628 |
"Os4+": {"a": [30.419, 15.2637, 14.7458, 5.06795], "b": [1.37113, 6.84706, 0.165191, 18.003], "c": 6.49804}, |
| 2629 |
"Ir": {"a": [27.3049, 16.7296, 15.6115, 5.83377], "b": [1.59279, 8.86553, 0.417916, 45.0011], "c": 11.4722}, |
| 2630 |
"Ir3+": {"a": [30.4156, 15.862, 13.6145, 5.82008], "b": [1.34323, 7.10909, 0.204633, 20.3254], "c": 8.27903}, |
| 2631 |
"Ir4+": {"a": [30.7058, 15.5512, 14.2326, 5.53672], "b": [1.30923, 6.71983, 0.167252, 17.4911], "c": 6.96824}, |
| 2632 |
"Pt": {"a": [27.0059, 17.7639, 15.7131, 5.7837], "b": [1.51293, 8.81174, 0.424593, 38.6103], "c": 11.6883}, |
| 2633 |
"Pt2+": {"a": [29.8429, 16.7224, 13.2153, 6.35234], "b": [1.32927, 7.38979, 0.263297, 22.9426], "c": 9.85329}, |
| 2634 |
"Pt4+": {"a": [30.9612, 15.9829, 13.7348, 5.92034], "b": [1.24813, 6.60834, 0.16864, 16.9392], "c": 7.39534}, |
| 2635 |
"Au": {"a": [16.8819, 18.5913, 25.5582, 5.86], "b": [0.4611, 8.6216, 1.4826, 36.3956], "c": 12.0658}, |
| 2636 |
"Au1+": {"a": [28.0109, 17.8204, 14.3359, 6.58077], "b": [1.35321, 7.7395, 0.356752, 26.4043], "c": 11.2299}, |
| 2637 |
"Au3+": {"a": [30.6886, 16.9029, 12.7801, 6.52354], "b": [1.2199, 6.82872, 0.212867, 18.659], "c": 9.0968}, |
| 2638 |
"Hg": {"a": [20.6809, 19.0417, 21.6575, 5.9676], "b": [0.545, 8.4484, 1.5729, 38.3246], "c": 12.6089}, |
| 2639 |
"Hg1+": {"a": [25.0853, 18.4973, 16.8883, 6.48216], "b": [1.39507, 7.65105, 0.443378, 28.2262], "c": 12.0205}, |
| 2640 |
"Hg2+": {"a": [29.5641, 18.06, 12.8374, 6.89912], "b": [1.21152, 7.05639, 0.284738, 20.7482], "c": 10.6268}, |
| 2641 |
"Tl": {"a": [27.5446, 19.1584, 15.538, 5.52593], "b": [0.65515, 8.70751, 1.96347, 45.8149], "c": 13.1746}, |
| 2642 |
"Tl1+": {"a": [21.3985, 20.4723, 18.7478, 6.82847], "b": [1.4711, 0.517394, 7.43463, 28.8482], "c": 12.5258}, |
| 2643 |
"Tl3+": {"a": [30.8695, 18.3481, 11.9328, 7.00574], "b": [1.1008, 6.53852, 0.219074, 17.2114], "c": 9.8027}, |
| 2644 |
"Pb": {"a": [31.0617, 13.0637, 18.442, 5.9696], "b": [0.6902, 2.3576, 8.618, 47.2579], "c": 13.4118}, |
| 2645 |
"Pb2+": {"a": [21.7886, 19.5682, 19.1406, 7.01107], "b": [1.3366, 0.488383, 6.7727, 23.8132], "c": 12.4734}, |
| 2646 |
"Pb4+": {"a": [32.1244, 18.8003, 12.0175, 6.96886], "b": [1.00566, 6.10926, 0.147041, 14.714], "c": 8.08428}, |
| 2647 |
"Bi": {"a": [33.3689, 12.951, 16.5877, 6.4692], "b": [0.704, 2.9238, 8.7937, 48.0093], "c": 13.5782}, |
| 2648 |
"Bi3+": {"a": [21.8053, 19.5026, 19.1053, 7.10295], "b": [1.2356, 6.24149, 0.469999, 20.3185], "c": 12.4711}, |
| 2649 |
"Bi5+": {"a": [33.5364, 25.0946, 19.2497, 6.91555], "b": [0.91654, 0.39042, 5.71414, 12.8285], "c": -6.7994}, |
| 2650 |
"Po": {"a": [34.6726, 15.4733, 13.1138, 7.02588], "b": [0.700999, 3.55078, 9.55642, 47.0045], "c": 13.677}, |
| 2651 |
"At": {"a": [35.3163, 19.0211, 9.49887, 7.42518], "b": [0.68587, 3.97458, 11.3824, 45.4715], "c": 13.7108}, |
| 2652 |
"Rn": {"a": [35.5631, 21.2816, 8.0037, 7.4433], "b": [0.6631, 4.0691, 14.0422, 44.2473], "c": 13.6905}, |
| 2653 |
"Fr": {"a": [35.9299, 23.0547, 12.1439, 2.11253], "b": [0.646453, 4.17619, 23.1052, 150.645], "c": 13.7247}, |
| 2654 |
"Ra": {"a": [35.763, 22.9064, 12.4739, 3.21097], "b": [0.616341, 3.87135, 19.9887, 142.325], "c": 13.6211}, |
| 2655 |
"Ra2+": {"a": [35.215, 21.67, 7.91342, 7.65078], "b": [0.604909, 3.5767, 12.601, 29.8436], "c": 13.5431}, |
| 2656 |
"Ac": {"a": [35.6597, 23.1032, 12.5977, 4.08655], "b": [0.589092, 3.65155, 18.599, 117.02], "c": 13.5266}, |
| 2657 |
"Ac3+": {"a": [35.1736, 22.1112, 8.19216, 7.05545], "b": [0.579689, 3.41437, 12.9187, 25.9443], "c": 13.4637}, |
| 2658 |
"Th": {"a": [35.5645, 23.4219, 12.7473, 4.80703], "b": [0.563359, 3.46204, 17.8309, 99.1722], "c": 13.4314}, |
| 2659 |
"Th4+": {"a": [35.1007, 22.4418, 9.78554, 5.29444], "b": [0.555054, 3.24498, 13.4661, 23.9533], "c": 13.376}, |
| 2660 |
"Pa": {"a": [35.8847, 23.2948, 14.1891, 4.17287], "b": [0.547751, 3.41519, 16.9235, 105.251], "c": 13.4287}, |
| 2661 |
"U": {"a": [36.0228, 23.4128, 14.9491, 4.188], "b": [0.5293, 3.3253, 16.0927, 100.613], "c": 13.3966}, |
| 2662 |
"U3+": {"a": [35.5747, 22.5259, 12.2165, 5.37073], "b": [0.52048, 3.12293, 12.7148, 26.3394], "c": 13.3092}, |
| 2663 |
"U4+": {"a": [35.3715, 22.5326, 12.0291, 4.7984], "b": [0.516598, 3.05053, 12.5723, 23.4582], "c": 13.2671}, |
| 2664 |
"U6+": {"a": [34.8509, 22.7584, 14.0099, 1.21457], "b": [0.507079, 2.8903, 13.1767, 25.2017], "c": 13.1665}, |
| 2665 |
"Np": {"a": [36.1874, 23.5964, 15.6402, 4.1855], "b": [0.511929, 3.25396, 15.3622, 97.4908], "c": 13.3573}, |
| 2666 |
"Np3+": {"a": [35.7074, 22.613, 12.9898, 5.43227], "b": [0.502322, 3.03807, 12.1449, 25.4928], "c": 13.2544}, |
| 2667 |
"Np4+": {"a": [35.5103, 22.5787, 12.7766, 4.92159], "b": [0.498626, 2.96627, 11.9484, 22.7502], "c": 13.2116}, |
| 2668 |
"Np6+": {"a": [35.0136, 22.7286, 14.3884, 1.75669], "b": [0.48981, 2.81099, 12.33, 22.6581], "c": 13.113}, |
| 2669 |
"Pu": {"a": [36.5254, 23.8083, 16.7707, 3.47947], "b": [0.499384, 3.26371, 14.9455, 105.98], "c": 13.3812}, |
| 2670 |
"Pu3+": {"a": [35.84, 22.7169, 13.5807, 5.66016], "b": [0.484938, 2.96118, 11.5331, 24.3992], "c": 13.1991}, |
| 2671 |
"Pu4+": {"a": [35.6493, 22.646, 13.3595, 5.18831], "b": [0.481422, 2.8902, 11.316, 21.8301], "c": 13.1555}, |
| 2672 |
"Pu6+": {"a": [35.1736, 22.7181, 14.7635, 2.28678], "b": [0.473204, 2.73848, 11.553, 20.9303], "c": 13.0582}, |
| 2673 |
"Am": {"a": [36.6706, 24.0992, 17.3415, 3.49331], "b": [0.483629, 3.20647, 14.3136, 102.273], "c": 13.3592}, |
| 2674 |
"Cm": {"a": [36.6488, 24.4096, 17.399, 4.21665], "b": [0.465154, 3.08997, 13.4346, 88.4834], "c": 13.2887}, |
| 2675 |
"Bk": {"a": [36.7881, 24.7736, 17.8919, 4.23284], "b": [0.451018, 3.04619, 12.8946, 86.003], "c": 13.2754}, |
| 2676 |
"Cf": {"a": [36.9185, 25.1995, 18.3317, 4.24391], "b": [0.437533, 3.00775, 12.4044, 83.7881], "c": 13.2674}, |
| 2677 |
} |
| 2678 |
|
| 2679 |
|
| 2680 |
|
| 2681 |
|
| 2682 |
|
| 2683 |
|
| 2684 |
|