There is definitely something wrong with matplotlib/numpy. Consider the following >from numpy import * >mydata=memmap('map.dat',dtype=float64,mode='w+',shape=56566500) > del mydata I can now remove the file map.dat with (from the command line) $rm map.dat However If I plot mydata before the line > del mydata I can't get rid of the file until I exit python!! Does matplotlib keep a reference to the data? How can I remove this reference? Mathew
Here is sample code demonstrating the problem I'm having with pyhd5/numpy/matplotlib import h5py import numpy import re import sys import os import gtk import pdb import gc from matplotlib.figure import Figure from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas def doplot(widget,box1): id=h5py.h5f.open("MLS-Aura_L1BRADD_v02-23-c01_2009d001.h5") data_id = h5py.h5d.open(id,"R1A:118.B22D:PT.S0.DACS-4") dataspaceID = h5py.h5d.DatasetID.get_space(data_id) noDims = h5py.h5s.SpaceID.get_simple_extent_ndims(dataspaceID) dims = h5py.h5s.SpaceID.get_simple_extent_dims(dataspaceID) start=tuple([0]*noDims) h5py.h5s.SpaceID.select_hyperslab(dataspaceID,start,dims,None,None, h5py.h5s.SELECT_SET) memory_space_id = h5py.h5s.create_simple(dims) data=numpy.memmap('tmpdat',dtype=numpy.float64,mode='w+',shape=dims) h5py.h5d.DatasetID.read(data_id,memory_space_id,dataspaceID,data) del data data=numpy.memmap('tmpdat',dtype=numpy.float64,mode='r',shape=dims) fig = Figure(figsize=(5,5), dpi=100) ax = fig.add_subplot(111) plot_data=data[0,0:,0] mif=numpy.arange(plot_data.shape[0]) #if the next line is commented out, all is good ax.plot(plot_data,mif) canvas = FigureCanvas(fig) box1.pack_start(canvas, True, True, 0) canvas.show() id.close() del data del fig del ax del plot_data del canvas gc.collect() #the following line fails os.remove('tmpdat') def delete_event(widget, event, data=None): return False window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.connect("destroy", lambda x: gtk.main_quit()) box1 = gtk.HBox(False, 0) window.add(box1) button = gtk.Button("Hello World") box1.pack_start(button, True, True, 0) #window.add(box1) button.show() button.connect("clicked", doplot, box1) box1.show() window.set_default_size(500,400) window.show() gtk.main()
Yes, for efficiency reasons matplotlib will use a reference to original data whenever possible, rather than copying it. Also, when using the pylab/pyplot API, matplotlib figures also stay around until they are explicitly deleted. You may need an explicit call to "clf('all')" to remove the figures, and thus references to the memmap'd file. However, my advice would be to just not use memmap. I can't imagine matplotlib performs very well if the data can't all fit in memory, so you might as well just load it all into memory anyway. Mike Mathew Yeates wrote: > There is definitely something wrong with matplotlib/numpy. Consider > the following > >from numpy import * > >mydata=memmap('map.dat',dtype=float64,mode='w+',shape=56566500) > > del mydata > > I can now remove the file map.dat with (from the command line) $rm map.dat > > However > If I plot mydata before the line > > del mydata > > > I can't get rid of the file until I exit python!! > Does matplotlib keep a reference to the data? How can I remove this > reference? > > Mathew > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day > trial. Simplify your report design, integration and deployment - and focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > ------------------------------------------------------------------------ > > _______________________________________________ > Matplotlib-devel mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-devel > -- Michael Droettboom Science Software Branch Operations and Engineering Division Space Telescope Science Institute Operated by AURA for NASA