SPSS runs out of memory using Python code for changing titles in tabels

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

SPSS runs out of memory using Python code for changing titles in tabels

la volta statistics
Hi all

I run into a 'insufficient memory' problem by renumbering the tables in my
output viewer using Python. I have written a Python code to renumber my
tables. When I have a large number of tables (such as more than 200) in the
viewer file I get the following error message after about 200 tables. I use
SPSS 15. My worksapace is set to 6148 by default. Doubling it to 12296 did
not change the behaviour.

Traceback (most recent call last):
  File "<string>", line 12, in ?
  File
"C:\Python24\lib\site-packages\win32com\gen_py\8B362432-1200-4131-925D-516ED
42F480Cx1033x7x2\ISpssItem.py", line 51, in ActivateTable
    ret = self._oleobj_.InvokeTypes(16, LCID, 1, (9, 0), (),)
pywintypes.com_error: (-2147352567, 'Ausnahmefehler aufgetreten.', (0, 'SPSS
15.0 for Windows', 'Insufficient memory to perform operation.', None,
0, -2147024882), None)

Below you find some code to reproduce the failure: The macro produces 250
equal tables for demonstration purpose.
The Python code should renumber the tables from 1 to 250, but fails around
200.

Can somebody help me here?

Thanks in advances
Christian

New File.
DATASET CLOSE all.

DATA LIST FREE /var1 var2 .
BEGIN DATA
1 2
END DATA.

DEFINE !Doit().
!LET !nby1 = 250.
!DO !cnt1=1 !TO !nby1.
CTABLES
  /VLABELS VARIABLES=var1 var2 DISPLAY=DEFAULT
  /TABLE var2 BY var1 [COUNT F40.0]
  /CATEGORIES VARIABLES=var1 var2 ORDER=A KEY=VALUE EMPTY=EXCLUDE
  /TITLES
   TITLE= 'Table 1' 'second line' 'third line'.
!DOEND.
!ENDDEFINE.

!DoIt.
Exec.


BEGIN PROGRAM.
import spss,viewer
spssappObj = viewer.spssapp()
outputdoc = spssappObj.GetDesignatedOutput()
objItems = outputdoc.Items
outputdoc.ClearSelection()
strLangTitle = "Table"
k = 0
for i in range(objItems.Count):
        objItem = objItems.GetItem(i)
        if objItem.SPSSType == 5 :
                objItem.Selected = True
                objPivotTable = objItem.ActivateTable()
                strOldTitle = objPivotTable.TitleText
                if ((strOldTitle.find("Tabelle") >= 0) or (strOldTitle.find("Table") >=
0)):
                        position =  strOldTitle.find(chr(13))
                        k = k + 1
                        strNewTitle = strLangTitle + " " + str(k) + strOldTitle[position:]
                        objPivotTable.TitleText = strNewTitle
                        strLabel = strLangTitle + " " + str(k)
                        objItem.Label = strLabel
                else:
                        k = k + 1
                        strNewTitle = strLangTitle + " " + str(k) + chr(13) + strOldTitle
                        objPivotTable.TitleText = strNewTitle
                        strLabel = strLangTitle + " " + str(k)
                        objItem.Label = strLabel

End Program.


*******************************
la volta statistics
Christian Schmidhauser, Dr.phil.II
Weinbergstrasse 108
Ch-8006 Zürich
Tel: +41 (043) 233 98 01
Fax: +41 (043) 233 98 02
email: mailto:[hidden email]
internet: http://www.lavolta.ch/
Reply | Threaded
Open this post in threaded view
|

Re: SPSS runs out of memory using Python code for changing titles in tabels

Peck, Jon
Despite what the message (which comes from Windows) says, you are actually not running out of memory.  You are running out of another, more obscure Windows resource, probably handles or something else in the Windows GDI.  This is caused by having a very large number of activations.

There are some small things that would make the script run a little better, but they won't solve the resource exhaustion problem.  A better way to do this task is to embed the titling in Python code that does not require the automation calls.  Put your Ctables commands in a loop with spss.Submit, and put the desired title into the syntax.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of la volta statistics
Sent: Tuesday, March 20, 2007 6:15 AM
To: [hidden email]
Subject: [SPSSX-L] SPSS runs out of memory using Python code for changing titles in tabels

Hi all

I run into a 'insufficient memory' problem by renumbering the tables in my
output viewer using Python. I have written a Python code to renumber my
tables. When I have a large number of tables (such as more than 200) in the
viewer file I get the following error message after about 200 tables. I use
SPSS 15. My worksapace is set to 6148 by default. Doubling it to 12296 did
not change the behaviour.

Traceback (most recent call last):
  File "<string>", line 12, in ?
  File
"C:\Python24\lib\site-packages\win32com\gen_py\8B362432-1200-4131-925D-516ED
42F480Cx1033x7x2\ISpssItem.py", line 51, in ActivateTable
    ret = self._oleobj_.InvokeTypes(16, LCID, 1, (9, 0), (),)
pywintypes.com_error: (-2147352567, 'Ausnahmefehler aufgetreten.', (0, 'SPSS
15.0 for Windows', 'Insufficient memory to perform operation.', None,
0, -2147024882), None)

Below you find some code to reproduce the failure: The macro produces 250
equal tables for demonstration purpose.
The Python code should renumber the tables from 1 to 250, but fails around
200.

Can somebody help me here?

Thanks in advances
Christian

New File.
DATASET CLOSE all.

DATA LIST FREE /var1 var2 .
BEGIN DATA
1 2
END DATA.

DEFINE !Doit().
!LET !nby1 = 250.
!DO !cnt1=1 !TO !nby1.
CTABLES
  /VLABELS VARIABLES=var1 var2 DISPLAY=DEFAULT
  /TABLE var2 BY var1 [COUNT F40.0]
  /CATEGORIES VARIABLES=var1 var2 ORDER=A KEY=VALUE EMPTY=EXCLUDE
  /TITLES
   TITLE= 'Table 1' 'second line' 'third line'.
!DOEND.
!ENDDEFINE.

!DoIt.
Exec.


BEGIN PROGRAM.
import spss,viewer
spssappObj = viewer.spssapp()
outputdoc = spssappObj.GetDesignatedOutput()
objItems = outputdoc.Items
outputdoc.ClearSelection()
strLangTitle = "Table"
k = 0
for i in range(objItems.Count):
        objItem = objItems.GetItem(i)
        if objItem.SPSSType == 5 :
                objItem.Selected = True
                objPivotTable = objItem.ActivateTable()
                strOldTitle = objPivotTable.TitleText
                if ((strOldTitle.find("Tabelle") >= 0) or (strOldTitle.find("Table") >=
0)):
                        position =  strOldTitle.find(chr(13))
                        k = k + 1
                        strNewTitle = strLangTitle + " " + str(k) + strOldTitle[position:]
                        objPivotTable.TitleText = strNewTitle
                        strLabel = strLangTitle + " " + str(k)
                        objItem.Label = strLabel
                else:
                        k = k + 1
                        strNewTitle = strLangTitle + " " + str(k) + chr(13) + strOldTitle
                        objPivotTable.TitleText = strNewTitle
                        strLabel = strLangTitle + " " + str(k)
                        objItem.Label = strLabel

End Program.


*******************************
la volta statistics
Christian Schmidhauser, Dr.phil.II
Weinbergstrasse 108
Ch-8006 Zürich
Tel: +41 (043) 233 98 01
Fax: +41 (043) 233 98 02
email: mailto:[hidden email]
internet: http://www.lavolta.ch/