python program to loop GPL syntax

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

python program to loop GPL syntax

Kylie
Hi all,

I wish to run an existing GGRAPH/GPL syntax on a number of variables from
the current data file. I have been searching the archives and it has been
suggested previously that for GPL syntax this is easier to achieve via a
Python program, rather than a macro. I am new to using Python but I found a
similar example written by Jon Peck some years ago that I have taken as a
starting point.

My very simple question is how to pass the Python program the list of
variables? In the example below 'varcat' holds the variable list, and is
created by identifying all of the nominal and ordinal variables in the data
file. I simply want to list the variables individually (eg, as in a simple
macro call), or use the TO syntax if possible. How do I assign that to
varcat?

Thanks,
Kylie.


begin program.
 # Example python loop of GPL syntax posted by Jon Peck to SPSSX-L in 2006.
 import spss, spssaux

chartsyntax=\
"""GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=Time_1hr_slots
MEAN(%(variable)s)[name="MEANnut"]
    rurality_first MISSING=LISTWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  SOURCE: s=userSource(id("graphdataset"))
  DATA: Time_1hr_slots=col(source(s), name("Time_1hr_slots"),
unit.category())
  DATA: MEANnut=col(source(s), name("MEANnut"))
  DATA: rurality_first=col(source(s), name("rurality_first"),
unit.category())
  GUIDE: axis(dim(1), label("1 hr time slots"))
  GUIDE: axis(dim(2), label("%(label)s"))
  GUIDE: legend(aesthetic(aesthetic.color.interior), label("Urban/Rural"))
  SCALE: cat(dim(1), include("1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12",
    "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"))
  SCALE: linear(dim(2), include(0))
  SCALE: cat(aesthetic(aesthetic.color.interior), include("0", "1", "2",
"3", "4", "5", "6", "7"))
  ELEMENT: line(position(Time_1hr_slots*MEANnut),
color.interior(rurality_first), missing.wings())
END GPL.
"""
varcat = spssaux.VariableDict(variableLevel=['nominal', 'ordinal'])
for v in varcat:
   cmd = (chartsyntax % {'variable' : v.VariableName, 'label' :
v.VariableLabel})
   print cmd
   spss.Submit(cmd)
end program.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: python program to loop GPL syntax

Jon K Peck

See below.

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



From: Kylie Lange <[hidden email]>
To: [hidden email]
Date: 09/08/2010 12:55 AM
Subject: [SPSSX-L] python program to loop GPL syntax
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Hi all,

I wish to run an existing GGRAPH/GPL syntax on a number of variables from
the current data file. I have been searching the archives and it has been
suggested previously that for GPL syntax this is easier to achieve via a
Python program, rather than a macro. I am new to using Python but I found a
similar example written by Jon Peck some years ago that I have taken as a
starting point.

My very simple question is how to pass the Python program the list of
variables? In the example below 'varcat' holds the variable list, and is
created by identifying all of the nominal and ordinal variables in the data
file. I simply want to list the variables individually (eg, as in a simple
macro call), or use the TO syntax if possible. How do I assign that to
varcat?

Thanks,
Kylie.


begin program.
# Example python loop of GPL syntax posted by Jon Peck to SPSSX-L in 2006.
import spss, spssaux

chartsyntax=\
"""GGRAPH
 /GRAPHDATASET NAME="graphdataset" VARIABLES=Time_1hr_slots
MEAN(%(variable)s)[name="MEANnut"]
   rurality_first MISSING=LISTWISE REPORTMISSING=NO
 /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
 SOURCE: s=userSource(id("graphdataset"))
 DATA: Time_1hr_slots=col(source(s), name("Time_1hr_slots"),
unit.category())
 DATA: MEANnut=col(source(s), name("MEANnut"))
 DATA: rurality_first=col(source(s), name("rurality_first"),
unit.category())
 GUIDE: axis(dim(1), label("1 hr time slots"))
 GUIDE: axis(dim(2), label("%(label)s"))
 GUIDE: legend(aesthetic(aesthetic.color.interior), label("Urban/Rural"))
 SCALE: cat(dim(1), include("1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12",
   "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"))
 SCALE: linear(dim(2), include(0))
 SCALE: cat(aesthetic(aesthetic.color.interior), include("0", "1", "2",
"3", "4", "5", "6", "7"))
 ELEMENT: line(position(Time_1hr_slots*MEANnut),
color.interior(rurality_first), missing.wings())
END GPL.
"""


#Right here, you can create a variable list in either of these ways:
#To use an explicit list
varcat = ['x','y','z']
#or, easier to type,
varcat = 'x y z'
varcat = varcat.split()

#To use the TO convention,
varcat = spssaux.VariableDict().expand("x to z y to w")
#in place of the following line.
varcat = spssaux.VariableDict(variableLevel=['nominal', 'ordinal'])



for v in varcat:
  cmd = (chartsyntax % {'variable' : v.VariableName, 'label' :
v.VariableLabel})
  print cmd
  spss.Submit(cmd)
end program.

HTH,

Jon Peck

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: python program to loop GPL syntax

Kylie
Thanks Jon, all of those will be useful.

Now that I have something to test, I am getting the following error. This is my
first python program so I'm sure it's something simple. Does anyone have any
suggestions?

File "<string>", line 28, in <module>
AttributeError: 'str' object has no attribute 'VariableName'

Thanks,
Kylie.


Quoting Jon K Peck <[hidden email]>:

> See below.
>
> Jon Peck
> SPSS, an IBM Company
> [hidden email]
> 312-651-3435
>
>
>
> From:
> Kylie Lange <[hidden email]>
> To:
> [hidden email]
> Date:
> 09/08/2010 12:55 AM
> Subject:
> [SPSSX-L] python program to loop GPL syntax
> Sent by:
> "SPSSX(r) Discussion" <[hidden email]>
>
>
>
> Hi all,
>
> I wish to run an existing GGRAPH/GPL syntax on a number of variables from
> the current data file. I have been searching the archives and it has been
> suggested previously that for GPL syntax this is easier to achieve via a
> Python program, rather than a macro. I am new to using Python but I found
> a
> similar example written by Jon Peck some years ago that I have taken as a
> starting point.
>
> My very simple question is how to pass the Python program the list of
> variables? In the example below 'varcat' holds the variable list, and is
> created by identifying all of the nominal and ordinal variables in the
> data
> file. I simply want to list the variables individually (eg, as in a simple
> macro call), or use the TO syntax if possible. How do I assign that to
> varcat?
>
> Thanks,
> Kylie.
>
>
> begin program.
>  # Example python loop of GPL syntax posted by Jon Peck to SPSSX-L in
> 2006.
>  import spss, spssaux
>
> chartsyntax=\
> """GGRAPH
>   /GRAPHDATASET NAME="graphdataset" VARIABLES=Time_1hr_slots
> MEAN(%(variable)s)[name="MEANnut"]
>     rurality_first MISSING=LISTWISE REPORTMISSING=NO
>   /GRAPHSPEC SOURCE=INLINE.
> BEGIN GPL
>   SOURCE: s=userSource(id("graphdataset"))
>   DATA: Time_1hr_slots=col(source(s), name("Time_1hr_slots"),
> unit.category())
>   DATA: MEANnut=col(source(s), name("MEANnut"))
>   DATA: rurality_first=col(source(s), name("rurality_first"),
> unit.category())
>   GUIDE: axis(dim(1), label("1 hr time slots"))
>   GUIDE: axis(dim(2), label("%(label)s"))
>   GUIDE: legend(aesthetic(aesthetic.color.interior), label("Urban/Rural"))
>   SCALE: cat(dim(1), include("1", "2", "3", "4", "5", "6", "7", "8", "9",
> "10", "11", "12",
>     "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
> "24"))
>   SCALE: linear(dim(2), include(0))
>   SCALE: cat(aesthetic(aesthetic.color.interior), include("0", "1", "2",
> "3", "4", "5", "6", "7"))
>   ELEMENT: line(position(Time_1hr_slots*MEANnut),
> color.interior(rurality_first), missing.wings())
> END GPL.
> """
>
> #Right here, you can create a variable list in either of these ways:
> #To use an explicit list
> varcat = ['x','y','z']
> #or, easier to type,
> varcat = 'x y z'
> varcat = varcat.split()
>
> #To use the TO convention,
> varcat = spssaux.VariableDict().expand("x to z y to w")
> #in place of the following line.
> varcat = spssaux.VariableDict(variableLevel=['nominal', 'ordinal'])
>
>
> for v in varcat:
>    cmd = (chartsyntax % {'variable' : v.VariableName, 'label' :
> v.VariableLabel})
>    print cmd
>    spss.Submit(cmd)
> end program.
>
> HTH,
> Jon Peck
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: python program to loop GPL syntax

Jon K Peck

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



From: Kylie Lange <[hidden email]>
To: [hidden email]
Date: 09/08/2010 08:54 PM
Subject: Re: [SPSSX-L] python program to loop GPL syntax
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Thanks Jon, all of those will be useful.

Now that I have something to test, I am getting the following error. This is my
first python program so I'm sure it's something simple. Does anyone have any
suggestions?

File "<string>", line 28, in <module>
AttributeError: 'str' object has no attribute 'VariableName'

Thanks,
Kylie.


Quoting Jon K Peck <[hidden email]>:

> See below.
>
> Jon Peck
> SPSS, an IBM Company
> [hidden email]
> 312-651-3435
>
>
>
> From:
> Kylie Lange <[hidden email]>
> To:
> [hidden email]
> Date:
> 09/08/2010 12:55 AM
> Subject:
> [SPSSX-L] python program to loop GPL syntax
> Sent by:
> "SPSSX(r) Discussion" <[hidden email]>
>
>
>
> Hi all,
>
> I wish to run an existing GGRAPH/GPL syntax on a number of variables from
> the current data file. I have been searching the archives and it has been
> suggested previously that for GPL syntax this is easier to achieve via a
> Python program, rather than a macro. I am new to using Python but I found
> a
> similar example written by Jon Peck some years ago that I have taken as a
> starting point.
>
> My very simple question is how to pass the Python program the list of
> variables? In the example below 'varcat' holds the variable list, and is
> created by identifying all of the nominal and ordinal variables in the
> data
> file. I simply want to list the variables individually (eg, as in a simple
> macro call), or use the TO syntax if possible. How do I assign that to
> varcat?
>
> Thanks,
> Kylie.
>
>
> begin program.
>  # Example python loop of GPL syntax posted by Jon Peck to SPSSX-L in
> 2006.
>  import spss, spssaux
>
> chartsyntax=\
> """GGRAPH
>   /GRAPHDATASET NAME="graphdataset" VARIABLES=Time_1hr_slots
> MEAN(%(variable)s)[name="MEANnut"]
>     rurality_first MISSING=LISTWISE REPORTMISSING=NO
>   /GRAPHSPEC SOURCE=INLINE.
> BEGIN GPL
>   SOURCE: s=userSource(id("graphdataset"))
>   DATA: Time_1hr_slots=col(source(s), name("Time_1hr_slots"),
> unit.category())
>   DATA: MEANnut=col(source(s), name("MEANnut"))
>   DATA: rurality_first=col(source(s), name("rurality_first"),
> unit.category())
>   GUIDE: axis(dim(1), label("1 hr time slots"))
>   GUIDE: axis(dim(2), label("%(label)s"))
>   GUIDE: legend(aesthetic(aesthetic.color.interior), label("Urban/Rural"))
>   SCALE: cat(dim(1), include("1", "2", "3", "4", "5", "6", "7", "8", "9",
> "10", "11", "12",
>     "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
> "24"))
>   SCALE: linear(dim(2), include(0))
>   SCALE: cat(aesthetic(aesthetic.color.interior), include("0", "1", "2",
> "3", "4", "5", "6", "7"))
>   ELEMENT: line(position(Time_1hr_slots*MEANnut),
> color.interior(rurality_first), missing.wings())
> END GPL.
> """
>
> #Right here, you can create a variable list in either of these ways:
> #To use an explicit list
> varcat = ['x','y','z']
> #or, easier to type,
> varcat = 'x y z'
> varcat = varcat.split()
>
> #To use the TO convention,
> varcat = spssaux.VariableDict().expand("x to z y to w")
> #in place of the following line.
> varcat = spssaux.VariableDict(variableLevel=['nominal', 'ordinal'])


>>>The difference is that varcat as created by expand is simply a list of strings while varcat as created by the original call is a variable dictionary object.  That has properties such as name and label associated with each item.

To use those properties, you need a variable dictionary object.
Breaking it down,
vardict = spssaux.VariableDict()
varstrings = vardict.expand("x to z y to w")
varcat = spssaux.VariableDict(varstrings)

That produces a variable dictionary object containing only the variables found in the expand expression.  The same idea would work for the first example, but it would be a little simpler.
varcat = ['x','y','z']
varcat = spssaux.VariableDict(varcat)

Then the rest of the code should work.

HTH,
Jon

>
>
> for v in varcat:
>    cmd = (chartsyntax % {'variable' : v.VariableName, 'label' :
> v.VariableLabel})
>    print cmd
>    spss.Submit(cmd)
> end program.
>
> HTH,
> Jon Peck
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD