Select to view content in your preferred language

How to delete a graph via arcpy

1292
4
07-19-2012 08:00 AM
RichardWatson1
Deactivated User
After getting the graphs to work, I am interested in cycling through (via a loop of some sort) to create multiple graphs.

My problem is that it appears that the 'graph' is stored in not-easily-referenced memory.

MakeGraph_management is pretty straight forward:

arcpy.MakeGraph_management(graph_grf, graph, "temp")

I can't replace "temp" with a variable (I mean, I can, but its useless) since the sting value is what names the output of MakeGraph.

arcpy.env.overwriteOutput = True  Does noting to help me.

arcpy.Delete_management('temp') looks like it *should* work as the help file indicates "Graph" as a valid input data type, but then I try that I get an error like:
ERROR 000732: Input Data Element: Dataset temp does not exist or is not supported
Failed to execute (Delete).


I suppose I could change the name that gets passed to make graph, and never delete anything but that seems sloppy and I plan on iterating through hundreds if not thousands of inputs... I'd imagine eventually I will run out of memory.

I'm using 10.1, service pack 2 (build 3200) if that matters.  I'm not interested in using other python packages to graph due to organizational constraints.

Any ideas?  Thanks.
Tags (2)
0 Kudos
4 Replies
NickJones
Emerging Contributor
Did you find a way around this.  I'm trying to batch a script that exports a graph at the end, and I run out of memory after like 15 iterations, just as you said.

Thanks,

Nick Jones
0 Kudos
NickJones
Emerging Contributor
ESRI helped me get around this by deleting the variable at the end.  It's been running a lot longer than I've been able to get it to run.  So I think it's working.  This is what I did:


graphExport = "tempGraphDelete"

arcpy.MakeGraph_management(graphCopy, "SERIES=line:vertical DATA={0} X=Distance Y=Invert SORT=ASC;SERIES=line:vertical DATA={0} X=Distance Y=Crown \
SORT=ASC;SERIES=line:vertical DATA={0} X=Distance Y=GrElev SORT=ASC;GRAPH=general TITLE={1} SUBTITLE={2}: ;LEGEND=general;AXIS=left \
TITLE=Elevation in Feet;AXIS=right TITLE=Invert;AXIS=bottom TITLE=Distance in Feet;AXIS=top".format(ClosedConduitsVertexPoints, titleVariable, subtitleVariable), graphExport)

del graphExport
0 Kudos
RichardWatson1
Deactivated User
Thanks for the thought, but try re-runing your script with the same inputs during the same session (try it in pythonwin or idle in the same session), you should error out similar to my first post.


As I understand it, using your example:

del graphExport
will delete the variable holding the string value of "tempGraphDelete" however it will not delete the graph datatype/object that is named "tempGraphDelete" 

From the outside, the use seems to mimic arcpy.MakeFeatureLayer_management, where you can store the resulting memory layer's name in a variable but that variable is not the memory object (except arcpy.Delete_management works on those objects). 

The one saving grace (for me, for right now) is that if you implement this as an Arctoolbox script it appears that you can at least rerun the same inputs without erring out (this is not the case in Pythonwin) so I'm wondering if the toolbox acts as some sort of self contained object/environment and those in-memory objects are destroyed once the script complete?  I tried testing this by referencing variables that I didn't explicitly delete from my script in the ArcGIS "Python" window and the Python window returned "is not defined" errors. 

I'm hoping that means I won't run out of memory as long as I invoke this as a toolbox?

ESRI helped me get around this by deleting the variable at the end.  It's been running a lot longer than I've been able to get it to run.  So I think it's working.  This is what I did:


graphExport = "tempGraphDelete"

arcpy.MakeGraph_management(graphCopy, "SERIES=line:vertical DATA={0} X=Distance Y=Invert SORT=ASC;SERIES=line:vertical DATA={0} X=Distance Y=Crown \
SORT=ASC;SERIES=line:vertical DATA={0} X=Distance Y=GrElev SORT=ASC;GRAPH=general TITLE={1} SUBTITLE={2}: ;LEGEND=general;AXIS=left \
TITLE=Elevation in Feet;AXIS=right TITLE=Invert;AXIS=bottom TITLE=Distance in Feet;AXIS=top".format(ClosedConduitsVertexPoints, titleVariable, subtitleVariable), graphExport)

del graphExport
0 Kudos
EdwardConrad
Deactivated User

Came across this post and I'm having the same issue as Richard Watson. I have a Python Toolbox that can create a graph fine on the first try, but subsequent runs of the tool results in an error. Closing ArcMap deletes the temporary graph & the tool will run correctly once, but I'm unable to delete the temporary graph using Arcpy.  Has anyone found a solution? I've tried several approaches to delete the temp graph file with no luck.

def execute(self, parameters, messages):

    # Attempt to setup Current Workspace

    folder = parameters[0].valueAsText

    arcpy.env.workspace = folder

    referenceFiles = os.path.join(os.path.dirname(__file__), "Reference Files - Please don't alter\This Tool"

    graph_Template = os.path.join(referenceFiles, "ownership.grf")

(1st approach)

   if arcpy.Exists("temp_graph"):

      arcpy.Delete_management("temp_graph")

      arcpy.MakeGraph_management(graph_Template, [...], "temp_graph")
      arcpy.SaveGraph_management("temp_graph", [...]

   else:

      arcpy.MakeGraph_management(graph_Template, [...], "temp_graph")
      arcpy.SaveGraph_management("temp_graph", [...])

  

(2nd approach)

      arcpy.MakeGraph_management(graph_Template, [...], "temp_graph")
      arcpy.SaveGraph_management("temp_graph", [...])

     arcpy.Delete_management("in_memory")

Here's my question posted on stack exchange: 

arcgis 10.4 - How do you delete temporary graph files using Arcpy? - Geographic Information Systems ... 

0 Kudos