[Tutor] python 3 np.savetxt(name_s, I_z) produces IndexError: tuple index out of range

Stephen P. Molnar s.molnar at sbcglobal.net
Thu May 11 15:46:18 EDT 2017


On 05/11/2017 02:26 PM, Stephen P. Molnar wrote:
>   0
> down vote
> favorite
>
>
> I am using Spyder3.1.4 with Python 3.6.0 | Anaconda 4.3.0 (64-bit) | and
> IPython 6.0.0.
>
> My script is rather long and calculates a series of molecular indices
> using the same formula and a series of different coefficients. The
> script uses the np.savetxt() quite a few times, but one use generates
> the title error. The offending line is np.savetxt(name_s,I_z) where I_z
> is a single number of type float64 with a size of 1.
>
> If I comment out the line containing the np.savetxt statement the script
> runs to completion and np.savetxt(name_s,I_oe) where I_oe is a float64
> number except it has a size of (1,).
>
> Now, it would seems to me that the different size of the name in the
> np.savetxt statement causes the error, but how do I correct it? Google
> has not resulted in a solution.
>
> Here is an abbreviated copy of the script:
>
> #!/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.
>
> """
> 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)
>
>
> #--------------------------------------------------------------------------
> # MASS Mollecular Transform  FT_m
>
>
> I = eq7(r, s, data['MASS'])
>
> name_s = name+'-Im'
>
> np.savetxt(name_s,I)
> fig = plt.figure()
> ax = fig.add_subplot(111)
> ax.plot(s.T, I)
> fig.gca().set_xlabel("Distance (Å)")
> plt.ylabel('MASS Transform (FT$_m$)')
> plt.show()
>
> I_sq = I**2
> Area = np.trapz(I_sq,x=None,dx=0.01,axis=-1)
> np.savetxt('I_m',I,delimiter=' ')
> I_m = np.sqrt([Area])
>
> print('FTm: ',I_m)
> name_s = name+'-FTm'
> np.savetxt(name_s,I_m)
>
> """
> #------------------------------------------------------------------------------
>
>
> name_in = name+'.mpa.dat'
>
> dtype_1 = [('NO', int), ('ATOM', 'S2'), ('NA', float), ('ZA', float),
>           ('QA', float), ('VA', float), ('BVA', float), ('FA', float)]
>
> data_1 = np.genfromtxt(name_in, dtype=dtype_1, skip_header=11)
>
> qa = []
> qa = np.array(data_1['QA'])
> va = []
> va = np.array([data_1['VA']])
> bva =[]
> bva = np.array([data_1['BVA']])
> fa = []
> fa = np.array([data_1['FA']])
>
> #------------------------------------------------------------------------------
>
> #Charge Molecular Transform  FT_c
>
> I = eq7(r, s, data_1['QA'])
> I_c = I
>
> name_s = name+'-Ic'
>
> np.savetxt(name_s,I)
> fig = plt.figure()
> ax = fig.add_subplot(111)
> ax.plot(s.T, I)
> fig.gca().set_xlabel("Distance (Å)")
> plt.ylabel('Charge Transform (FT$_C$)')
> plt.show()
>
> I_sq = I_c**2
> Area = np.trapz(I_sq,x=None,dx=0.01,axis=-1)
> np.savetxt(name_s,I,delimiter=' ')
> I_c = np.sqrt([Area])
>
> print('FTc: ',I_c)
> name_s = name+'-FTc'
>
> np.savetxt(name_s,I_c)
>
>
> """
>
> And here is the input file:
>
> 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
>
> The last line in the script is causing the problem.
>
> A pointer towards the solution to the problem will be much apprecialted.
>
> Thanks in advance.
>

Problem solved: np.savetxt(name_s,I_z) should be np.savetxt(name_s,[I_z])

-- 
Stephen P. Molnar, Ph.D.		Life is a fuzzy set
www.molecular-modeling.net		Stochastic and multivariate
(614)312-7528 (c)
Skype: smolnar1


More information about the Tutor mailing list