Re: [Tutor] python 3 np.savetxt(name_s, I_z) produces IndexError: tuple index out of range
On 11/05/17 20:46, Stephen P. Molnar wrote: >> 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. I'm glad you found the problem but please, when you post, do NOT summarize the error. Always, always, send the full error trace, there is a wealth of data in those messages and if we only get your best guess at what they mean we are deprived of essential clues in diagnosing problems. > Problem solved: np.savetxt(name_s,I_z) should be np.savetxt(name_s,[I_z]) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python 3 np.savetxt(name_s, I_z) produces IndexError: tuple index out of range
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 ZAFRAG MASS X Y Z 0 C 6.012.0110.000.000.00 1 H 1.0 1.0082.0598010.000.00 2 Br 35.079.900 -1.2031263.4029530.00 3 Cl 17.035.453 -1.108639 -1.5678532.715601 4 F 9.018.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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] python 3 np.savetxt(name_s, I_z) produces IndexError: tuple index out of range
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 ZAFRAG MASS X Y Z 0 C 6.012.0110.000.000.00 1 H 1.0 1.0082.0598010.000.00 2 Br 35.079.900 -1.2031263.4029530.00 3 Cl 17.035.453 -1.108639 -1.5678532.715601 4 F 9.018.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. -- Stephen P. Molnar, Ph.D.Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: Re: While Loop Question
Thank you all I finally understood that the while code traps j and runs independently of the for loop until while is false and a new i is picked. On Thu, May 11, 2017 at 10:19 AM Abdur-Rahmaan Janhangeer < arj.pyt...@gmail.com> wrote: > sorry j is set to zero. grave typo > > Abdur-Rahmaan Janhangeer, > Mauritius > https://abdurrahmaanjanhangeer.wordpress.com > > On 11 May 2017 12:29 pm, "Abdur-Rahmaan Janhangeer" > wrote: > > > > > > > -- Forwarded message -- > > From: "Abdur-Rahmaan Janhangeer" > > Date: 11 May 2017 12:26 pm > > Subject: Re: [Tutor] While Loop Question > > To: "Rafael Skovron" > > Cc: > > > > i modified your code to make it look like that : > > > > for i in range(1, 5): > > > > j=0 > > print("outer, i=",i) > > while j < i: > > print("inner, j=",j) > > > > j += 1 > > > > so it shows that each time the outer loop is reexecuted, i is set to > zero. > > > > putting j at the begining completely, it is not reset to zero > > > > Abdur-Rahmaan Janhangeer, > > Mauritius > > https://abdurrahmaanjanhangeer.wordpress.com > > > > > > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While Loop Question
2017-05-11 3:53 GMT+02:00 Rafael Skovron : > Sorry I left out the indents in my previous email. It seems like j is > always reset to zero. Why does j vary? > > Are there two different instances of j going on? > > > for i in range(1, 5): > > j=0 > >while j < i: > > print(j, end = " ") > > j += 1 > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Hi, As there is a j = 0 statement in the for cycle, it will be set to 0 every time the while cycle finishes. (The while cycle will then increment j, then if j == i j will be set to 0 again). George ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: Re: While Loop Question
sorry j is set to zero. grave typo Abdur-Rahmaan Janhangeer, Mauritius https://abdurrahmaanjanhangeer.wordpress.com On 11 May 2017 12:29 pm, "Abdur-Rahmaan Janhangeer" wrote: > > > -- Forwarded message -- > From: "Abdur-Rahmaan Janhangeer" > Date: 11 May 2017 12:26 pm > Subject: Re: [Tutor] While Loop Question > To: "Rafael Skovron" > Cc: > > i modified your code to make it look like that : > > for i in range(1, 5): > > j=0 > print("outer, i=",i) > while j < i: > print("inner, j=",j) > > j += 1 > > so it shows that each time the outer loop is reexecuted, i is set to zero. > > putting j at the begining completely, it is not reset to zero > > Abdur-Rahmaan Janhangeer, > Mauritius > https://abdurrahmaanjanhangeer.wordpress.com > > > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: Re: While Loop Question
-- Forwarded message -- From: "Abdur-Rahmaan Janhangeer" Date: 11 May 2017 12:26 pm Subject: Re: [Tutor] While Loop Question To: "Rafael Skovron" Cc: i modified your code to make it look like that : for i in range(1, 5): j=0 print("outer, i=",i) while j < i: print("inner, j=",j) j += 1 so it shows that each time the outer loop is reexecuted, i is set to zero. putting j at the begining completely, it is not reset to zero Abdur-Rahmaan Janhangeer, Mauritius https://abdurrahmaanjanhangeer.wordpress.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While Loop Question
On Wed, May 10, 2017 at 06:53:21PM -0700, Rafael Skovron wrote: > Sorry I left out the indents in my previous email. It seems like j is > always reset to zero. Why does j vary? Because you run code that adds 1 to j. > Are there two different instances of j going on? No. Try to run the code in your head and see what happens. See below: > for i in range(1, 5): > j=0 > while j < i: > print(j, end = " ") > j += 1 We start by setting i = 1, then the body of the for-loop begins. That sets j = 0, and since 0 < 1, we enter the while-loop. Inside the while-loop, we print j (0), then add 1 to j which makes it 1. Then we return to the top of the while-loop. Since 1 is NOT less than 1, we exit the while-loop and return to the top of the for-loop. Now we set i = 2, and continue into the body of the for-loop. That sets j = 0 (again!) and since 0 < 2, we enter the while-loop. Inside the while-loop, we print j (0), then add 1 to j which makes it 1. Then we return to the top of the while-loop. Since 1 < 2, we continue inside the body, print j (1), then add 1 to j which makes it 2. Since 2 is not LESS than 2, we exit the while-loop and return to the top of the for-loop. Now we set i = 3, and continue into the body of the for-loop. That sets j = 0 (again!) and since 0 < 3, we enter the while-loop. Inside the while-loop, we follow the same steps and print 0, then 1, then 2, then exit the while-loop and return to the top of the for-loop. Now we set i = 4, and again continue into the while-loop to print 0, 1, 2 and finally 3, then exit the while-loop and return to the top of the for-loop, which is now complete. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple while loop question
On 11/05/17 02:50, Rafael Skovron wrote: > I dont understand why j can have any value other than zero in this: > > for i in range(1, 5): j = 0 while j < i: print(j, end = " ") j += 1 Because i can go as high as 4 (from range(1,5)->1,2,3,4). So lets consider that final case: i->4 j->0 j True print 0 j->1 jTrue print 1 j->2 jTrue j->3 print 3 j->4 j<4->False So you'd expect the output to go like this: i=1, j->0 i=2, j->0,1 i=3, j->0,1,2 i=4, j->0,1,2,3 And since you set the print end to be a space it will all run into one line: 0 0 1 0 1 2 0 1 2 3 Is that what you see? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] While Loop Question
Sorry I left out the indents in my previous email. It seems like j is always reset to zero. Why does j vary? Are there two different instances of j going on? for i in range(1, 5): j=0 while j < i: print(j, end = " ") j += 1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Simple while loop question
I dont understand why j can have any value other than zero in this: for i in range(1, 5): j = 0 while j < i: print(j, end = " ") j += 1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor