Changes between Version 7 and Version 8 of MaskedArray

Show
Ignore:
Timestamp:
01/13/06 21:26:14 (7 years ago)
Author:
sasha
Comment:

Added Ufuncs and Masked Arrays section

Legend:

Unmodified
Added
Removed
Modified
  • MaskedArray

    v7 v8  
    1010 
    1111describe migration from Numeric.MA to numpy.ma here 
     12---- 
     13= Ufuncs and Masked Arrays = 
     14 
     15In changeset:1835, Travis added {{{__array_wrap__}}} hook to the {{{MaskedArray}}} class. This was done in an attempt 
     16to fix mixed arithmetics. Unfortunately, there is not enought information within the   {{{__array_wrap__}}} hook to correctly  
     17generate the mask: 
     18{{{ 
     19>>> from numpy import * 
     20>>> print ma.array([1])/ma.array([0]) 
     21[--] 
     22>>> print array([1])/ma.array([0]) 
     23[0] 
     24}}} 
     25In order to fix this situation, more information has to be passed to the  {{{__array_wrap__}}} hook by the ufunc. Sasha proposes 
     26to change the  {{{__array_wrap__}}} signature form  
     27{{{ 
     28def __array_wrap__(self, arr) 
     29}}} 
     30to 
     31{{{ 
     32def __array_wrap__(self, arr, context) 
     33}}} 
     34and make ufuncs pass a tuple context=(func, args, i), where func is the ufunc itself, args is the tuple of ufunc arguments and 
     35i is the index of self in the args tuple.  Strictly speaking, i is not necessary, but it is available in ufunc and may prove to be helpful 
     36in the future. 
     37 
     38Once ufuncs can handle the case of mixed arguments to binary operations, it is tempting to get rid of ma wrapers to ufuncs alltogether 
     39and implement ma logic entirely in  {{{__array_wrap__}}} and  {{{__array__}}} hooks.  Unfortunately,  {{{__array__}}} hook suffers from the 
     40same problem: before passing data to ufunc ma array heeds to replace masked values with something safe for the given operation. In 
     41order to do this more information in needed than passed to {{{__array__}}} hook.  Sasha proposes to make a similar change to  {{{__array__}}} 
     42hook as for  {{{__array_wrap__}}} hook above. 
     43 
     44The new signature will be 
     45{{{ 
     46def __array__(self, dtype=None, context=None) 
     47}}} 
     48The ufuncs will pass to {{{__array__}}} a tuple context=(func, args, i), where func is the ufunc itself, args is the tuple of ufunc arguments and 
     49i is the index of self in the args tuple.  For backward compatibility ufuncs will allow a two-argument {{{__array__}}} and classes that will take 
     50advantage of context will define {{{__array__}}} with a default value for context so that it can be called with one or two arguments as well.  
     51 
    1252 
    1353----