[Numpy-discussion] Best way to construct/slice 3-dimensional ndarray from multiple 2d ndarrays?

Keith Hughitt keith.hughitt@gmail....
Wed Aug 17 12:43:55 CDT 2011


The 2d arrays are read in using another library (PyFITS), so I probably
won't be able to control that too much, otherwise that sounds like exactly
what I need.

I'm actually overriding the indexing operation so that the user gets back an
ndarray subclass when they do "cube[0]":

    def __getitem__(self, key):
        """Overiding indexing operation"""
        if isinstance(key, int):
            data = np.ndarray.__getitem__(self, key)
            header = self._headers[key]
            for cls in BaseMap.__subclasses__():
                if cls.is_datasource_for(header):
                    return cls(data, header)
            raise UnrecognizedDataSouceError
        else:
            return np.ndarray.__getitem__(self, key)

Which relates to the second part of the question I had about how the ndarray
is handled when an instance of a ndarray subclass is created.

Thanks for the suggestions!

Keith

On Wed, Aug 17, 2011 at 1:00 PM, Olivier Delalleau <shish@keba.be> wrote:

> Right now you allocate new memory only when creating your 3d array. When
> you do "x = cube[0]" this creates a view that does not allocate more memory.
>
> If your 2d arrays were created independently, I don't think you can avoid
> this.
> If you have some control on the way your original 2D arrays are created,
> you can first initialize the 3d array with correct shape (or an upper bound
> on the number of 2d arrays), then use views on this 3d array ("x_i =
> cube[i]") to fill your 2D arrays in the same memory space.
>
> I can't help with your second question, sorry.
>
> -=- Olivier
>
> 2011/8/17 Keith Hughitt <keith.hughitt@gmail.com>
>
>> Hi all,
>>
>> I have a method which builds a single 3d ndarray from several
>> equal-dimension 2d ndarrays, and another method which extracts the original
>> 2d ndarrays back out from the 3d one.
>>
>> The way I'm doing this right now is pretty simple, e.g.:
>>
>> cube = np.asarray([arr1, arr2,...])
>> ...
>> x = cube[0]
>>
>> I believe the way this is currently handled, is to use new memory
>> locations first for the 3d array, and then later for the 2d slices.
>>
>> Does anyone know if there is a better way to handle this? Ideally, I would
>> like to reuse the same memory locations instead of copying it anew each
>> time.
>>
>> Also, when subclassing ndarray and calling obj = data.view(cls) for an
>> ndarray "data", does this copy the data into the new object by value or
>> reference? The method which extracts the 2d slice actually returns a
>> subclass of ndarray created using the extracted data, so this is why I ask.
>>
>> Any insight or suggestions would be appreciated.
>>
>> Thanks!
>> Keith
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110817/b32dae4c/attachment.html 


More information about the NumPy-Discussion mailing list