<div>Similar to what Matthew said, I often find that it's cleaner to make a seperate class with a "data" (or somesuch) property that lazily loads the numpy array.</div>
<div> </div>
<div>For example, something like:</div>
<div> </div>
<div>class DataFormat(object):</div>
<div>    def __init__(self, filename):</div>
<div>        self.filename = filename</div>
<div>        for key, value in self._read_header().iteritems():</div>
<div>            setattr(self, key, value)</div>
<div> </div>
<div>    @property</div>
<div>    def data(self):</div>
<div>        try:</div>
<div>            return self._data</div>
<div>        except AttributeError:</div>
<div>            self._data = self._read_data()</div>
<div>            return self._data</div>
<div> </div>
<div>Hope that helps,</div>
<div>-Joe<br><br></div>
<div class="gmail_quote">On Tue, Jul 26, 2011 at 4:15 PM, Matthew Brett <span dir="ltr"><<a href="mailto:matthew.brett@gmail.com">matthew.brett@gmail.com</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">Hi,<br>
<div class="im"><br>On Tue, Jul 26, 2011 at 5:11 PM, Craig Yoshioka <<a href="mailto:craigyk@me.com">craigyk@me.com</a>> wrote:<br>> I want to subclass ndarray to create a class for image and volume data, and when referencing a file I'd like to have it load the data only when accessed.  That way the class can be used to quickly set and manipulate header values, and won't load data unless necessary.  What is the best way to do this?  Are there any hooks I can use to load the data when an array's values are first accessed or manipulated?  I tried some trickery with __array_interface__ but couldn't get it to work very well.  Should I just use a memmapped array, and give up on a purely 'lazy' approach?<br>
<br></div>What kind of images are you loading?   We do lazy loading in nibabel,<br>for medical image type formats:<br><br><a href="http://nipy.sourceforge.net/nibabel/" target="_blank">http://nipy.sourceforge.net/nibabel/</a><br>
<br>- but our images _have_ arrays and headers, rather than (appearing to<br>be) arrays.  Thus something like:<br><br>import nibabel as nib<br><br>img = nib.load('my_image.img')<br># data not loaded at this point<br>
data = img.get_data()<br># data loaded now.  Maybe memmapped if the format allows<br><br>If you think you might have similar needs, I'd be very happy to help<br>you get going in nibabel...<br><br>Best,<br><font color="#888888"><br>
Matthew<br></font>
<div>
<div></div>
<div class="h5">_______________________________________________<br>NumPy-Discussion mailing list<br><a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br><a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</div></div></blockquote></div><br>