Python looping through sets of parameters and not all combinations of parameters

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Python looping through sets of parameters and not all combinations of parameters

Krinsky, Alan-2
Python looping through sets of parameters and not all combinations of parameters

Thanks to those of you who have offered help in looping and macros and python. I still have a couple of questions.

1. Most importantly, how do I loop through sets of parameters instead of all possibilities?

For example:
BEGIN PROGRAM.
submit spss
parameter1 = ('a', 'b', 'c')
parameter2 = ('x', 'y', 'z')
for p1 in parameter1:
    for p2 in parameter2:

        spss.Submit(r"""
          GET DATA.
               /TYPE=ODBC
               /CONNECT='DSN=datawarehouse;Description=datawarehouse;UID=;APP=SPSS For Windows;WSID=GN1MGCW08;DATABASE=datawarehouse;Trusted_Connection=Yes'

               /SQL="SELECT ....FROM....WHERE... "
                         +"AND data1 = '%(p1)s' )"
                            /COMPRESSED.
          SAVE OUTFILE='\\Server1\project3\%(p2)s dataset.sav
""" %locals())
END PROGRAM.

When I run something like this, I get 9 files, ax, ay, az, bx, by, etc...
What I want to produce is 3 files, ax, by, and cz.
Any ideas on how do I get this to loop once through each set of parameters (in the end I will have multiple sets of at least 4 parameters) rather than looping through each combination? I have tried a number of approaches, including putting all the values in one list and trying to call up 2 at a time, using a matrix and a dictionary, and nothing is providing the desired results.

2. Does anyone know if there are different rules for string, numeric, and date variables for looping?

Thanks, and be well!
Alan


"If people don't want to come out to the ballpark, how are you going to stop them?"
"I always thought that record would stand until it was broken."
-Yogi Berra

Alan D. Krinsky  PhD, MPH
Medical Management Interventions Manager
UMass Memorial Health Care
Hahnemann Campus
281 Lincoln St.
Worcester, MA 01605
Phone: 508-334-5854
Fax: 508-793-6086
E-mail: [hidden email]

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, transmission, re-transmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.
Reply | Threaded
Open this post in threaded view
|

Re: Python looping through sets of parameters and not all combinations of parameters

Jon K Peck

Instead of nesting the loops, pass them in parallel:
for p1, p2 in zip(parameter1, parameter2):
   ...

That will pick off the values in parallel for the two tuples.

HTH,
Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: "Krinsky, Alan" <[hidden email]>
To: [hidden email]
Date: 02/26/2010 01:54 PM
Subject: [SPSSX-L] Python looping through sets of parameters and not all              combinations of parameters
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Thanks to those of you who have offered help in looping and macros and python. I still have a couple of questions.

1. Most importantly, how do I loop through sets of parameters instead of all possibilities?

For example:
BEGIN PROGRAM.

submit spss

parameter1 = ('a', 'b', 'c')

parameter2 = ('x', 'y', 'z')

for p1 in parameter1:

   for p2 in parameter2:

        spss.Submit(r"""
         GET DATA.

              /TYPE=ODBC
              /CONNECT='DSN=datawarehouse;Description=datawarehouse;UID=;APP=SPSS For Windows;WSID=GN1MGCW08;DATABASE=datawarehouse;Trusted_Connection=Yes'

               /SQL="SELECT ....FROM....WHERE... "
       
                +"AND data1 = '%(p1)s' )"
                           /COMPRESSED.
         SAVE OUTFILE='\\Server1\project3\%(p2)s dataset.sav

""" %locals())

END PROGRAM.

When I run something like this, I get 9 files, ax, ay, az, bx, by, etc...
What I want to produce is 3 files, ax, by, and cz.

Any ideas on how do I get this to loop once through each set of parameters (in the end I will have multiple sets of at least 4 parameters) rather than looping through each combination? I have tried a number of approaches, including putting all the values in one list and trying to call up 2 at a time, using a matrix and a dictionary, and nothing is providing the desired results.

2. Does anyone know if there are different rules for string, numeric, and date variables for looping?

Thanks, and be well!
Alan

"If people don't want to come out to the ballpark, how are you going to stop them?"
"I always thought that record would stand until it was broken."

-Yogi Berra


Alan D. Krinsky  PhD, MPH

Medical Management Interventions Manager

UMass Memorial Health Care

Hahnemann Campus

281 Lincoln St.

Worcester, MA 01605

Phone: 508-334-5854

Fax: 508-793-6086

E-mail: [hidden email]

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, transmission, re-transmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.

Reply | Threaded
Open this post in threaded view
|

Re: Python looping through sets of parameters and not all combinations of parameters

Albert-Jan Roskam
Hi Alan,

zip() is the nicest way, but be aware that you don't get an error when the parameter lists are (for whatever reason) of unequal length.

x, y = range(10), range(15)
for a, b in zip(x, y):
  print a, b

I sometimes use 'raise' or 'assert' to catch something like that.
>>> assert len(x) == len(y), "Paramlists must be of equal length"

Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    assert len(x) == len(y), "Paramlists must be of equal length"
AssertionError: Paramlists must be of equal length

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Fri, 2/26/10, Jon K Peck <[hidden email]> wrote:

From: Jon K Peck <[hidden email]>
Subject: Re: [SPSSX-L] Python looping through sets of parameters and not all combinations of parameters
To: [hidden email]
Date: Friday, February 26, 2010, 11:07 PM


Instead of nesting the loops, pass them in parallel:
for p1, p2 in zip(parameter1, parameter2):
   ...

That will pick off the values in parallel for the two tuples.

HTH,
Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: "Krinsky, Alan" <[hidden email]>
To: [hidden email]
Date: 02/26/2010 01:54 PM
Subject: [SPSSX-L] Python looping through sets of parameters and not all              combinations of parameters
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Thanks to those of you who have offered help in looping and macros and python. I still have a couple of questions.

1. Most importantly, how do I loop through sets of parameters instead of all possibilities?

For example:
BEGIN PROGRAM.

submit spss

parameter1 = ('a', 'b', 'c')

parameter2 = ('x', 'y', 'z')

for p1 in parameter1:

   for p2 in parameter2:

        spss.Submit(r"""
         GET DATA.

              /TYPE=ODBC
              /CONNECT='DSN=datawarehouse;Description=datawarehouse;UID=;APP=SPSS For Windows;WSID=GN1MGCW08;DATABASE=datawarehouse;Trusted_Connection=Yes'

               /SQL="SELECT ....FROM....WHERE... "
       
                +"AND data1 = '%(p1)s' )"
                           /COMPRESSED.
         SAVE OUTFILE='\\Server1\project3\%(p2)s dataset.sav

""" %locals())

END PROGRAM.

When I run something like this, I get 9 files, ax, ay, az, bx, by, etc...
What I want to produce is 3 files, ax, by, and cz.

Any ideas on how do I get this to loop once through each set of parameters (in the end I will have multiple sets of at least 4 parameters) rather than looping through each combination? I have tried a number of approaches, including putting all the values in one list and trying to call up 2 at a time, using a matrix and a dictionary, and nothing is providing the desired results.

2. Does anyone know if there are different rules for string, numeric, and date variables for looping?

Thanks, and be well!
Alan

"If people don't want to come out to the ballpark, how are you going to stop them?"
"I always thought that record would stand until it was broken."

-Yogi Berra


Alan D. Krinsky  PhD, MPH

Medical Management Interventions Manager

UMass Memorial Health Care

Hahnemann Campus

281 Lincoln St.

Worcester, MA 01605

Phone: 508-334-5854

Fax: 508-793-6086

E-mail: [hidden email]

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, transmission, re-transmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.


Reply | Threaded
Open this post in threaded view
|

Re: Python looping through sets of parameters and not all combinations of parameters

Jon K Peck

It's true that izip stops as soon as any iterable is exhausted.  If you want the opposite behavior and are on Python 2.6, you can do this.
from itertools import izip_longest

for a,b in izip_longest(seq1, seq2)
  ...

The default value for the shorter series is None.


Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435

(from Australia)

From: Albert-Jan Roskam <[hidden email]>
To: [hidden email], Jon K Peck/Chicago/IBM@IBMUS
Date: 02/27/2010 06:02 AM
Subject: Re: [SPSSX-L] Python looping through sets of parameters and not              all              combinations of parameters





Hi Alan,

zip() is the nicest way, but be aware that you don't get an error when the parameter lists are (for whatever reason) of unequal length.

x, y = range(10), range(15)
for a, b in zip(x, y):
 print a, b

I sometimes use 'raise' or 'assert' to catch something like that.
>>> assert len(x) == len(y), "Paramlists must be of equal length"

Traceback (most recent call last):
 File "<pyshell#23>", line 1, in <module>
   assert len(x) == len(y), "Paramlists must be of equal length"
AssertionError: Paramlists must be of equal length

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Fri, 2/26/10, Jon K Peck <[hidden email]> wrote:


From: Jon K Peck <[hidden email]>
Subject: Re: [SPSSX-L] Python looping through sets of parameters and not all combinations of parameters
To: [hidden email]
Date: Friday, February 26, 2010, 11:07 PM


Instead of nesting the loops, pass them in parallel:

for p1, p2 in zip(parameter1, parameter2):

  ...


That will pick off the values in parallel for the two tuples.


HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: "Krinsky, Alan" <[hidden email]>
To: [hidden email]
Date: 02/26/2010 01:54 PM
Subject: [SPSSX-L] Python looping through sets of parameters and not all              combinations of parameters
Sent by: "SPSSX(r) Discussion" <[hidden email]>






Thanks to those of you who have offered help in looping and macros and python. I still have a couple of questions.

1. Most importantly, how do I loop through sets of parameters instead of all possibilities?

For example:
BEGIN PROGRAM.

submit spss

parameter1 = ('a', 'b', 'c')

parameter2 = ('x', 'y', 'z')

for p1 in parameter1:

  for p2 in parameter2:

        spss.Submit(r"""
        GET DATA.

             /TYPE=ODBC
             /CONNECT='DSN=datawarehouse;Description=datawarehouse;UID=;APP=SPSS For Windows;WSID=GN1MGCW08;DATABASE=datawarehouse;Trusted_Connection=Yes'

               /SQL="SELECT ....FROM....WHERE... "
     
                 +"AND data1 = '%(p1)s' )"
                          /COMPRESSED.
        SAVE OUTFILE='\\Server1\project3\%(p2)s dataset.sav

""" %locals())

END PROGRAM.

When I run something like this, I get 9 files, ax, ay, az, bx, by, etc...
What I want to produce is 3 files, ax, by, and cz.

Any ideas on how do I get this to loop once through each set of parameters (in the end I will have multiple sets of at least 4 parameters) rather than looping through each combination? I have tried a number of approaches, including putting all the values in one list and trying to call up 2 at a time, using a matrix and a dictionary, and nothing is providing the desired results.

2. Does anyone know if there are different rules for string, numeric, and date variables for looping?

Thanks, and be well!
Alan

"If people don't want to come out to the ballpark, how are you going to stop them?"
"I always thought that record would stand until it was broken."

-Yogi Berra


Alan D. Krinsky  PhD, MPH

Medical Management Interventions Manager

UMass Memorial Health Care

Hahnemann Campus

281 Lincoln St.

Worcester, MA 01605

Phone: 508-334-5854

Fax: 508-793-6086

E-mail: [hidden email]

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, transmission, re-transmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.

Reply | Threaded
Open this post in threaded view
|

Ver. 17 patch #3 and update Python plug-in

Roberts, Michael
In reply to this post by Jon K Peck

Good Afternoon List,

 

I want to use the third patch for ver. 17 but need to know whether I will have to update all my Python plug-ins as well.  Will someone who has already travelled this path let us know whether this is a requirement for continued happiness with Python as is, or do we need to traverse the path of patch and angst J

 

TIA

 

Mike