1

I am running a python code that simulates and predicts conditions inside a rapid compression machine (such as temperature, pressure, chemical species, etc.). The code is set up so that it calculates the time evolution of all these parameters (which are saved in numpy arrays) and then outputs them to a function, which creates and saves plots of these parameters against time.

These arrays are very large, and the function creates about 14 plots using matplotlib. The function will get to about the 7th plot (sometimes more, sometimes less), when I get an error reading "python.exe has stopped working". Not sure if it's a memory issue because the plots are so big or what's going on.

I have tried both (as you'll see in my sample code) plt.figure.clf() and plt.close(). I've even tried doing a time.sleep inbetween plots. The following is an example snippet of my function that does the plotting (not the real code, just an example necessary to illustrate my problem).

I am using Windows, Python 2.7, and Matplotlib 1.4.2

def graph_parameters(time, a, b, c, d, e):  #a,b,c,d,e are my parameters

    delay = 10

    plt.figure(1)
    plt.figure(facecolor="white")
    plt.plot(time, a)
    plt.ylabel('a')
    plt.xlabel('Time(s)')
    plt.savefig('fig1.png')
    plt.figure(1).clf()
    plt.close('all')
    time.sleep(delay)

    plt.figure(2)
    plt.figure(facecolor="white")
    plt.plot(time, b)
    plt.ylabel('b')
    plt.xlabel('Time(s)')
    plt.savefig('fig2.png')
    plt.figure(2).clf()
    plt.close('all')
    time.sleep(delay) 

etc.

Thanks in advance.

hitzg
  • 12,133
  • 52
  • 54
David W
  • 111
  • 2
  • 6

1 Answers1

1

I'm not completely sure, but I think it was a memory issue. I found a way (from another stackoverflow entry) to delete variables I don't need. Per my example above, if I only want to keep 'time_' and parameters 'a_','b_','c_','d_' and 'e_', I run the following at the end of my main code:

graph_array = np.array([time_, a_, b_, c_, d_, e_])

#Delete variables that aren't going to be plotted in order to free up memory
attributes = sys.modules[__name__]

for name in dir():
   if name[0]!='_' and np.all( name!= np.array(['graph_array', 'attributes','np', 'gc', 'graph_parameters']) ):
     delattr(attributes, name)

gc.collect()

graph_parameters(*graph_array)

Basically what the for loop does is keep private and magical function names and names of specified variables, and deletes all other names. I got this idea from the answer to the following link. How do I clear all variables in the middle of a Python script?

I watched my task manager as the code ran, and there was a significant drop in memory usage (~1,600,000 K to ~100,000 K) by Python right before plots began being saved, indicating that this method did free up memory. Also, no crash.

Community
  • 1
  • 1
David W
  • 111
  • 2
  • 6