Changeset 1681 for branches/alma


Ignore:
Timestamp:
01/29/10 18:14:51 (15 years ago)
Author:
Takeshi Nakazato
Message:

New Development: No

JIRA Issue: Yes CAS-1823

Ready to Release: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Allow operation of scantable with numpy.ndarray.

Internal process of four operations (add, sub, mul, div) looks very similar
on Python level, so that I have defined _operation() method to unify their
internal process. Currently add, sub, mul, div just
call _operation with slightly different parameters.


File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/alma/python/scantable.py

    r1680 r1681  
    18831883        varlist = vars()
    18841884        s = None
    1885         if isinstance(factor, list):
    1886             if isinstance(factor[0], float) or isinstance(factor[0], int):
    1887                 s = scantable( self._math._arrayop( self.copy(), factor, "MUL", tsys ) )
    1888             elif isinstance(factor[0], list):
     1885        import numpy
     1886        if isinstance(factor, list) or isinstance(factor, numpy.ndarray):
     1887            if isinstance(factor[0], list) or isinstance(factor[0], numpy.ndarray):
    18891888                from asapmath import _array2dOp
    18901889                s = _array2dOp( self.copy(), factor, "MUL", tsys )
    18911890            else:
    1892                 asaplog.push( 'Unexpected type of scaling factor.' )
    1893                 print_log( 'ERROR' )
     1891                s = scantable( self._math._arrayop( self.copy(), factor, "MUL", tsys ) )
    18941892        else:
    18951893            s = scantable(self._math._unaryop(self.copy(), factor, "MUL", tsys))
     
    20352033
    20362034    def __add__(self, other):
    2037         varlist = vars()
    2038         s = None
    2039         if isinstance(other, scantable):
    2040             s = scantable(self._math._binaryop(self.copy(), other, "ADD"))
    2041         elif isinstance(other, float) or isinstance(other, int):
    2042             s = scantable(self._math._unaryop(self.copy(), other, "ADD", False))
    2043         elif isinstance(other, list):
    2044             if isinstance(other[0], float) or isinstance(other[0], int):
    2045                 s = scantable(self._math._arrayop(self.copy(), other, "ADD", False))
    2046             elif isinstance(other[0], list):
    2047                 from asapmath import _array2dOp
    2048                 s = _array2dOp( self.copy(), other, "ADD", False )
    2049             else:
    2050                 raise TypeError("Other input is not a scantable or float value or float list")
    2051         else:
    2052             raise TypeError("Other input is not a scantable or float value or float list")
    2053         s._add_history("operator +", varlist)
    2054         print_log()
    2055         return s
     2035        """
     2036        implicit on all axes and on Tsys
     2037        """
     2038        return self._operation( other, "ADD" )
    20562039
    20572040    def __sub__(self, other):
     
    20592042        implicit on all axes and on Tsys
    20602043        """
    2061         varlist = vars()
    2062         s = None
    2063         if isinstance(other, scantable):
    2064             s = scantable(self._math._binaryop(self.copy(), other, "SUB"))
    2065         elif isinstance(other, float) or isinstance(other, int):
    2066             s = scantable(self._math._unaryop(self.copy(), other, "SUB", False))
    2067         elif isinstance(other, list):
    2068             if isinstance(other[0], float) or isinstance(other[0], int):
    2069                 s = scantable(self._math._arrayop(self.copy(), other, "SUB", False))
    2070             elif isinstance(other[0], list):
    2071                 from asapmath import _array2dOp
    2072                 s = _array2dOp( self.copy(), other, "SUB", False )
    2073             else:
    2074                 raise TypeError("Other input is not a scantable or float value or float list")
    2075         else:
    2076             raise TypeError("Other input is not a scantable or float value or float list")
    2077         s._add_history("operator -", varlist)
    2078         print_log()
    2079         return s
     2044        return self._operation( other, 'SUB' )
    20802045
    20812046    def __mul__(self, other):
     
    20832048        implicit on all axes and on Tsys
    20842049        """
    2085         varlist = vars()
    2086         s = None
    2087         if isinstance(other, scantable):
    2088             s = scantable(self._math._binaryop(self.copy(), other, "MUL"))
    2089         elif isinstance(other, float) or isinstance(other, int):
    2090             s = scantable(self._math._unaryop(self.copy(), other, "MUL", False))
    2091         elif isinstance(other, list):
    2092             if isinstance(other[0], float) or isinstance(other[0], int):
    2093                 s = scantable(self._math._arrayop(self.copy(), other, "MUL", False))
    2094             elif isinstance(other[0], list):
    2095                 from asapmath import _array2dOp
    2096                 s = _array2dOp( self.copy(), other, "MUL", False )
    2097             else:
    2098                 raise TypeError("Other input is not a scantable or float value or float list")
    2099         else:
    2100             raise TypeError("Other input is not a scantable or float value or float list")
    2101         s._add_history("operator *", varlist)
    2102         print_log()
    2103         return s
     2050        return self._operation( other, 'MUL' )
    21042051
    21052052    def __div__(self, other):
     
    21072054        implicit on all axes and on Tsys
    21082055        """
    2109         varlist = vars()
    2110         s = None
    2111         if isinstance(other, scantable):
    2112             s = scantable(self._math._binaryop(self.copy(), other, "DIV"))
    2113         elif isinstance(other, float) or isinstance(other, int):
    2114             if float(other) == 0.0:
    2115                 raise ZeroDivisionError("Dividing by zero is not recommended")
    2116             s = scantable(self._math._unaryop(self.copy(), other, "DIV", False))
    2117         elif isinstance(other, list):
    2118             if isinstance(other[0], float) or isinstance(other[0], int):
    2119                 s = scantable(self._math._arrayop(self.copy(), other, "DIV", False))
    2120             elif isinstance(other[0], list):
    2121                 from asapmath import _array2dOp
    2122                 s = _array2dOp( self.copy(), other, "DIV", False )
    2123             else:
    2124                 raise TypeError("Other input is not a scantable or float value or float list")
    2125         else:
    2126             raise TypeError("Other input is not a scantable or float value or float list")
    2127         s._add_history("operator /", varlist)
    2128         print_log()
    2129         return s
     2056        return self._operation( other, 'DIV' )
    21302057
    21312058    def get_fit(self, row=0):
     
    23022229        for i in range(len(self)):
    23032230            yield self[i]
     2231
     2232    def _operation(self, other, opmode):
     2233        varlist = vars()
     2234        s = None
     2235        import numpy
     2236        if isinstance(other, scantable):
     2237            s = scantable(self._math._binaryop(self.copy(), other, opmode))
     2238        elif isinstance(other, float) or isinstance(other, int):
     2239            if opmode == 'DIV' and float(other) == 0.0:
     2240                raise ZeroDivisionError("Dividing by zero is not recommended")
     2241            s = scantable(self._math._unaryop(self.copy(), other, opmode, False))
     2242        elif isinstance(other, list) or isinstance(other, numpy.ndarray):
     2243            if isinstance(other[0], list) or isinstance(other[0], numpy.ndarray):
     2244                from asapmath import _array2dOp
     2245                s = _array2dOp( self.copy(), other, opmode, False )
     2246            else:
     2247                s = scantable(self._math._arrayop(self.copy(), other, opmode, False))
     2248        else:
     2249            raise TypeError("Other input is not a scantable or float value or float list")
     2250        opdic = {}
     2251        opdic['ADD'] = '+'
     2252        opdic['SUB'] = '-'
     2253        opdic['MUL'] = '*'
     2254        opdic['DIV'] = '/'
     2255        s._add_history("operator %s" % opdic[opmode], varlist)
     2256        print_log()
     2257        return s
     2258
     2259       
Note: See TracChangeset for help on using the changeset viewer.