我在Python 3.6.0中使用Spyder3.1.4 Anaconda 4.3.0(64位)|和IPython 6.0.0。
我的脚本相当长,使用相同的公式和一系列不同的系数计算一系列分子指数。该脚本使用np.savetxt()
很多次,但一次使用会产生标题错误。有问题的行是np.savetxt(name_s,I_z)
,其中I_z
是单个数字float64
,大小为1。
如果我注释掉包含np.savetxt
语句的行,则脚本将运行至完成状态np.savetxt(name_s,I_oe)
其中I_oe
为float64
个数字,但其大小为(1) )。
现在,在我看来,np.savetxt
语句中名称的大小不同会导致错误,但我该如何纠正呢?谷歌尚未找到解决方案。
以下是该脚本的缩写副本:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 8 15:17:07 2017
@author: comp
Copyright (c) 2017 Stephen P. Molnar, Ph.D. All rights reserved.
"""
import matplotlib.pyplot as plt
import numpy as np
start=1
finish=31
points=300
s = np.linspace(start, finish, points)
np.savetxt('s',s)
name = input("Enter Molecule ID: ")
name_in = name+'.dat'
dtype = [('NO', int), ('LB', 'S2'), ('ZA', float), ('FRAG', int),
('MASS', float), ('X', float), ('Y', float), ('Z', float)]
data = np.genfromtxt(name_in, dtype=dtype, skip_header=3)
N = data.shape[0]
a = np.array([data['X'], data['Y'], data['Z']])
anrows, ancols = np.shape(a)
a_new = a.reshape(anrows, 1, ancols)
diff = a_new - a
D = (diff ** 2).sum(2)
D = np.sqrt(D)
r = D
def eq7(a, s, Z):
"""
Computes equation 7 in Molnar & King (2001)
"""
N = r.shape[0]
I = np.zeros(s.shape)
for i in range(1, N):
for j in range(i):
I += Z[i] * Z[j] * np.sin(s * r[i, j])/(s * r[i, j])
return I
I = eq7(r, s, data['ZA'])
name_s = name+'-Iz'
np.savetxt(name_s,I)
I_sq = []
I_sq = I**2
Area = []
name_s = name+'-Iz'
Area = np.trapz(I_sq,x=None,dx=0.01,axis=-1)
I_z = np.sqrt(Area)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(s.T, I)
fig.gca().set_xlabel("Distance (Å)")
plt.ylabel('Atomic Number Transform (FT$_z$)')
plt.show()
print('FTz: ',I_z)
name_s = name+'-FTz'
print(name_s)
np.savetxt(name_s,I_z)
这是输入文件:
CARTESIAN COORDINATES (A.U.)
----------------------------
NO LB ZA FRAG MASS X Y Z
0 C 6.0000 0 12.011 0.000000 0.000000 0.000000
1 H 1.0000 0 1.008 2.059801 0.000000 0.000000
2 Br 35.0000 0 79.900 -1.203126 3.402953 0.000000
3 Cl 17.0000 0 35.453 -1.108639 -1.567853 2.715601
4 F 9.0000 0 18.998 -0.938564 -1.327330 -2.299003
test.py中的行76导致了问题。
问题解决了:
np.savetxt(name_s,I_z)应为:np.savetxt(name_s,[I_z])