Re: [Tutor] Question about loop and assigning variables

2017-04-05 Thread Steven D'Aprano
On Wed, Apr 05, 2017 at 12:07:05PM -0700, Fazal Khan wrote:
> Hello,
> 
> Heres another newbie python question: How can I loop through some data and
> assign different variables with each loop

You don't. That's a bad idea.

Instead, you use a sequence (a tuple, or a list), and use an index:

TxTableAngle[0]  # the first item
TxTableAngle[1]  # the second item
etc.


> So this is my original code:
> 
> def BeamInfo(x):
>   for Bnum in x:
> if plan1.IonBeamSequence[Bnum].ScanMode == 'MODULATED':
> TxTableAngle =
> plan1.IonBeamSequence[Bnum].IonControlPointSequence[0].PatientSupportAngle
> print(TxTableAngle)
> else:
> None


Let's say you succeed in doing what you want, and you have a bunch of 
variables called TxTableAngle_0, TxTableAngle_1, TxTableAngle_2, and so 
on. How do you use them?

The first problem is that you don't know if they even exist! You might 
not have *any* TxTableAngle variables at all, if x is empty, of if 
ScanMode is never MODULATED. So even

TxTableAngle_0

might fail with a NameError exception. But even if you can guarantee 
that there is *at least one* variable, you don't know how many there 
will be! That depends on how many times the ScanMode is MODULATED. 
Perhaps there is only one, perhaps there is a thousand. How do you know 
if it is safe to refer to:

TxTableAngle_3

or not? This rapidly becomes hard to code, difficult to write, harder to 
debug and maintain, a real nightmare.

Better is to collect the results into a list:

def BeamInfo(x):
results = []
for bnum in x:
beam = plan1.IonBeamSequence[bnum]
if beam.ScanMode == 'MODULATED':
TxTableAngle = beam.IonControlPointSequence[0].PatientSupportAngle
results.append(TxTableAngle)
print(TxtTableAngle)
return results

TxTableAngles = BeamInfo([0,1,2,3,4,5,6])

Now you know that TxTableAngles *must* exist. You can find out how many 
items there actually are:

len(TxTableAngles)

you can grab any one specific item (provided the index actually exists):

TxTableAngles[i]

and most importantly you can process all of the angles one after the 
other:

for angle in TxTableAngles:
print(angle)


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about loop and assigning variables

2017-04-05 Thread Alan Gauld via Tutor
On 05/04/17 20:07, Fazal Khan wrote:

> assign different variables with each loop

You can't, but you can fake it...

> def BeamInfo(x):
>   for Bnum in x:
> if plan1.IonBeamSequence[Bnum].ScanMode == 'MODULATED':
> TxTableAngle =
> plan1.IonBeamSequence[Bnum].IonControlPointSequence[0].PatientSupportAngle
> print(TxTableAngle)
> else:
> None

> Ideally what I want is this: when Bnum is 0, then "TxTableAngle" will be
> appended with "_0" (eg.TxTableAngle_0). When Bnum is 1 then TxTableAngle_1,
> etc. How do I do this in python?

Use a list (or a dictionary if the values are not sequential or numeric)

So
TxTableAngle_0 -> TxTableAngle[0]
TxTableAngle_1 -> TxTableAngle[1]

It's one extra character but hopefully that won't be too painful...

-- 
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] Euclidean Distances between Atoms in a Molecule.

2017-04-05 Thread Sergio Rojas


--

Message: 2
Date: Mon, 03 Apr 2017 11:36:10 +0200
From: Peter Otten <__pete...@web.de>
To: tutor@python.org
Subject: Re: [Tutor] Euclidean Distances between Atoms in a Molecule.
Message-ID: 
Content-Type: text/plain; charset="ISO-8859-1"

Stephen P. Molnar wrote:

> I am trying to port a program that I wrote in FORTRAN twenty years ago
> into Python 3 and am having a hard time trying to calculate the
> Euclidean distance between each atom in the molecule and every other
> atom in the molecule.
>
> Here is a typical table of coordinates:
>
>
> MASS X Y Z
> 0 12.011 -3.265636 0.198894 0.090858
> 1 12.011 -1.307161 1.522212 1.003463
> 2 12.011 1.213336 0.948208 -0.033373
> 3 14.007 3.238650 1.041523 1.301322
> 4 12.011 -5.954489 0.650878 0.803379
> 5 12.011 5.654476 0.480066 0.013757
> 6 12.011 6.372043 2.731713 -1.662411
> 7 12.011 7.655753 0.168393 2.096802
> 8 12.011 5.563051 -1.990203 -1.511875
> 9 1.008 -2.939469 -1.327967 -1.247635
> 10 1.008 -1.460475 2.993912 2.415410
> 11 1.008 1.218042 0.451815 -2.057439
> 12 1.008 -6.255901 2.575035 1.496984
> 13 1.008 -6.560562 -0.695722 2.248982
> 14 1.008 -7.152500 0.390758 -0.864115
> 15 1.008 4.959548 3.061356 -3.139100
> 16 1.008 8.197613 2.429073 -2.588339
> 17 1.008 6.503322 4.471092 -0.543939
> 18 1.008 7.845274 1.892126 3.227577
> 19 1.008 9.512371 -0.273198 1.291080
> 20 1.008 7.147039 -1.365346 3.393778
> 21 1.008 4.191488 -1.928466 -3.057804
> 22 1.008 5.061650 -3.595015 -0.302810
> 23 1.008 7.402586 -2.392148 -2.374554
>
> What I need for further calculation is a matrix of the Euclidean
> distances between the atoms.
>
> So far in searching the Python literature I have only managed to confuse
> myself and would greatly appreciate any pointers towards a solution.
>
> Thanks in advance.
>

Stitched together with heavy use of a search engine:

$ cat data.txt
MASS X Y Z
0 12.011 -3.265636 0.198894 0.090858
1 12.011 -1.307161 1.522212 1.003463
2 12.011 1.213336 0.948208 -0.033373
3 14.007 3.238650 1.041523 1.301322
4 12.011 -5.954489 0.650878 0.803379
5 12.011 5.654476 0.480066 0.013757
6 12.011 6.372043 2.731713 -1.662411
7 12.011 7.655753 0.168393 2.096802
8 12.011 5.563051 -1.990203 -1.511875
9 1.008 -2.939469 -1.327967 -1.247635
10 1.008 -1.460475 2.993912 2.415410
11 1.008 1.218042 0.451815 -2.057439
12 1.008 -6.255901 2.575035 1.496984
13 1.008 -6.560562 -0.695722 2.248982
14 1.008 -7.152500 0.390758 -0.864115
15 1.008 4.959548 3.061356 -3.139100
16 1.008 8.197613 2.429073 -2.588339
17 1.008 6.503322 4.471092 -0.543939
18 1.008 7.845274 1.892126 3.227577
19 1.008 9.512371 -0.273198 1.291080
20 1.008 7.147039 -1.365346 3.393778
21 1.008 4.191488 -1.928466 -3.057804
22 1.008 5.061650 -3.595015 -0.302810
23 1.008 7.402586 -2.392148 -2.374554
$ python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy, pandas, scipy.spatial.distance as dist
>>> df = pandas.read_table("data.txt", sep=" ", skipinitialspace=True)
>>> a = numpy.array(df[["X", "Y", "Z"]])
>>> dist.squareform(dist.pdist(a, "euclidean"))


Here's an example with just the first 4 atoms:

>>> dist.squareform(dist.pdist(a[:4], "euclidean"))
array([[ 0. , 2.53370139, 4.54291701, 6.6694065 ],
[ 2.53370139, 0. , 2.78521357, 4.58084922],
[ 4.54291701, 2.78521357, 0. , 2.42734737],
[ 6.6694065 , 4.58084922, 2.42734737, 0. ]])

See
https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html[https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html]
There may be a way to do this with pandas.pivot_table(), but I didn't manage
to find that.

As Alan says, this is not the appropriate forum for the topic you are
belabouring.

Work your way through a Python tutorial to pick up the basics (we can help
you with this), then go straight to where the (numpy/scipy) experts are.

--
An alternative starting from the numpy array "a" from Peter answer:

import numpy as np

#Taking the number of rows and columns of the array
anrows, ancols = np.shape(a)

# Gather the coordinates as one dimensional arrays
a_new = a.reshape(anrows, 1, ancols)

# Takes the difference between each of the elements (one Vs all)
diff = a_new - a   

# Computes the sum of the squared difference
D = (diff ** 2).sum(2)

# Takes the square root
D = np.sqrt(D)

#Check that both answers are the same:
print(D-dist.squareform(dist.pdist(a, "euclidean")))


Salut,

Sergio
Avoiding for loops section of:
https://www.packtpub.com/big-data-and-business-intelligence/numerical-and-scientific-computing-scipy-video
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Question about loop and assigning variables

2017-04-05 Thread Fazal Khan
Hello,

Heres another newbie python question: How can I loop through some data and
assign different variables with each loop

So this is my original code:

def BeamInfo(x):
  for Bnum in x:
if plan1.IonBeamSequence[Bnum].ScanMode == 'MODULATED':
TxTableAngle =
plan1.IonBeamSequence[Bnum].IonControlPointSequence[0].PatientSupportAngle
print(TxTableAngle)
else:
None

BeamInfo([0,1,2,3,4,5,6])


Ideally what I want is this: when Bnum is 0, then "TxTableAngle" will be
appended with "_0" (eg.TxTableAngle_0). When Bnum is 1 then TxTableAngle_1,
etc. How do I do this in python?

Thanks
-Fuz
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-05 Thread Neil Cerutti
On 2017-04-03, Mats Wichmann  wrote:
> If you're talking about 4-digit year numbers using a Western
> calendar in digits of PI, the overlap effect seems unlikely to
> matter - let's say the year is 1919, do we think PI contains
> the sequence 191919? count would report back one instead of two
> in that case. In other cases it might matter; count is written
> specifically to not care about overlaps: "Return the number of
> (non-overlapping) occurrences"  So that's worth keeping in mind
> when you think about what you need from substrings-in-strings
> cases.

Composing a FSA (finite state automata) by hand would be an
excellent exercise in addition to covering the overlapping cases.

Another fun exercise might be to find the bithdays using
arithmetic operations instead of string operations.

-- 
Neil Cerutti

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do we create a GUI to run a simple calculation program in Python?

2017-04-05 Thread Lisa Hasler Waters
Wonderful! Thank you all so very much for all this invaluable expertise. We
have a lot of ways to make this work. We are excited to continue learning
so much.

Lisa

On Tue, Apr 4, 2017 at 8:15 PM, Marc Tompkins 
wrote:

> On Tue, Apr 4, 2017 at 9:55 AM, Lisa Hasler Waters 
> wrote:
>
>> Hello Tutor,
>>
>> A middle school student of mine created a program to calculate simple and
>> compound interest. He built it in PyCharm EDU using a Mac running 10.11.6.
>>
>> He would like to create a GUI to run this program. Please, can you advise
>> on how he could build this?
>>
>> Others have mentioned Tkinter and HTML; I''d like to put in my two cents'
> worth for wxPython .  HTML is probably the best
> cross-platform solution, and requires the least extra stuff bolted on, but
> wxPython lets you create desktop apps that look like they were actually
> developed for the computer they run on.
>
> Alan Gauld's comments about separating the GUI from the program logic are,
> as usual, spot-on.
>
>


-- 
Lisa Waters, PhD
Technology Integration
Middle School Coding
Lower School Digital Literacy
Flint Hill School
703.584.2300
*www.flinthill.org* 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] A contribution for enhancing your Python SciPy skills

2017-04-05 Thread Sergio Rojas
Hello Guys,

I am just very happy to have finished my video project with Pack on a brief 
introduction to Machine Learning via SciPy :

https://www.packtpub.com/big-data-and-business-intelligence/numerical-and-scientific-computing-scipy-video

Previously, as you might know, we finished this one:

https://www.packtpub.com/big-data-and-business-intelligence/learning-scipy-numerical-and-scientific-computing-second-edition

https://github.com/rojassergio/Learning-Scipy 

Hope you can spread the word.

Salut,

Sergio
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor