Skip to content
Browse files

Added la.add(); Sphinx doc work.

  • Loading branch information...
1 parent 4ce1cc6 commit ec646b591a6889316bdc668c610865dd91d753cb @kwgoodman committed
Showing with 531 additions and 348 deletions.
  1. +1 −104 LICENSE
  2. +2 −2 README.rst
  3. +37 −31 RELEASE.rst
  4. +29 −8 doc/source/functions.rst
  5. +2 −0 doc/source/index.rst
  6. +1 −170 doc/source/intro.rst
  7. +1 −0 doc/source/license.rst
  8. +1 −0 doc/source/release.rst
  9. +40 −27 la/LICENSE
  10. +1 −1 la/__init__.py
  11. +13 −3 la/deflarry.py
  12. +196 −0 la/flarry.py
  13. +207 −2 la/tests/flarry_test.py
View
105 LICENSE
@@ -1,104 +1 @@
-larry is distributed under a Simplified BSD license. Parts of NumPy, Scipy and
-numpydoc, which all have BSD licenses, are included in larry.
-
----------------------------------------------------------------------------
-la license:
-
-Copyright (c) 2008, 2009, 2010, Archipel Asset Management AB.
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions are met:
-
- * Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
-
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGE.
-
----------------------------------------------------------------------------
-NumPy license:
-
-Copyright (c) 2005-2009, NumPy Developers.
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions are
-met:
-
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-
- * Redistributions in binary form must reproduce the above
- copyright notice, this list of conditions and the following
- disclaimer in the documentation and/or other materials provided
- with the distribution.
-
- * Neither the name of the NumPy Developers nor the names of any
- contributors may be used to endorse or promote products derived
- from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
----------------------------------------------------------------------------
-SciPy license:
-
-Copyright (c) 2001, 2002 Enthought, Inc.
-All rights reserved.
-
-Copyright (c) 2003-2009 SciPy Developers.
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions are met:
-
- a. Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- b. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- c. Neither the name of the Enthought nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
-ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
-DAMAGE.
-
----------------------------------------------------------------------------
-numpydoc license:
-
-The numpydoc license is in la/doc/sphinxext/LICENSE.txt
-
+The license file is one level down from this file: la/LICENSE.
View
4 README.rst
@@ -90,9 +90,9 @@ After you have installed ``la``, run the suite of unit tests:
>>> import la
>>> la.test()
<snip>
- Ran 2958 tests in 1.351s
+ Ran 2978 tests in 1.351s
OK
- <nose.result.TextTestResult run=2958 errors=0 failures=0>
+ <nose.result.TextTestResult run=2978 errors=0 failures=0>
The ``la`` package contains a C extension module. The functions in the module
speed up common alignment operations such as adding two unaligned larrys. If
View
68 RELEASE.rst
@@ -3,18 +3,27 @@
Release Notes
=============
+These are the major changes made in each release. For detailed changes see
+http://github.com/kwgoodman/la
+
la 0.4 (celery)
===============
*Release date: Not yet released, in development*
-New larry methods
------------------
+The focus of this release was binary operations between unaligned larrys with
+user control of the join method (five available) and the fill method. A
+general binary function, la.binaryop(), was added as were the convenience
+functions add, subtract, multiply, divide. Supporting functions such as
+la.align(), which aligns two larrys, were also added.
+
+**New larry methods**
+
- ismissing: A bool larry with element-wise marking of missing values
- take: A copy of the specified elements of a larry along an axis
-New functions
--------------
+**New functions**
+
- rand: Random samples from a uniform distribution
- randn: Random samples from a Gaussian distribution
- missing_marker: Return missing value marker for the given larry
@@ -26,21 +35,22 @@ New functions
- align: Align two larrys using one of five join methods
- info: la package information such as version number and HDF5 availability
- binaryop: Binary operation on two larrys with given function and join method
+- la.add: Sum of two larrys using given join method and fill method
+
+**Enhancements**
-Enhancements
-------------
- listmap now has option to ignore unmappable elements instead of KeyError
- listmap.pyx now has option to ignore unmappable elements instead of KeyError
- larry.morph() is much faster as are methods, such as merge, that use it
-Breakage from la 0.3
---------------------
+**Breakage from la 0.3**
+
- Development moved from launchpad to github
-- func.py and afunc.py renamed flarry.py and farray.py to match new flabel.py
+- func.py and afunc.py renamed flarry.py and farray.py to match new flabel.py.
Broke: "from la.func import stack"; Did not break: "from la import stack"
-Bug fixes
----------
+**Bug fixes**
+
- #590270 Index with 1d array bug: lar[1darray,:] worked; lar[1darray] crashed
@@ -49,34 +59,34 @@ la 0.3 (banana)
*Release date: 2010-06-04*
-New larry methods
------------------
+**New larry methods**
+
- astype: Copy of larry cast to specified type
- geometric_mean: new method based on existing array function
-New functions
--------------
+**New functions**
+
- la.util.resample.cross_validation: k-fold cross validation index iterator
- la.util.resample.bootstrap: bootstrap index iterator
- la.util.misc.listmap: O(n) version of map(list1.index, list2)
- la/src/clistmap.pyx: Cython version of listmap with python fallback
-Enhancements
-------------
+**Enhancements**
+
- Major performance boost in most larry methods!
- You can now use an optional dtype when creating larrys
- You can now optionally skip the integrity test when creating a new larry
- Add ability to compare (==, >, !=, etc) larrys with lists and tuples
- Documentation and unit tests
-Breakage from la 0.2
---------------------
+**Breakage from la 0.2**
+
- lastrank and lastrank_decay methods combined into one method: lastrank
- Given shape (n,m) input, lastrank now returns shape (n,) instead of (n,1)
- geometric_mean now reduces input in the same way as lastrank (see above)
-Bug fixes
----------
+**Bug fixes**
+
- #571813 Three larry methods crashed on 1d input
- #571737 skiprows missing from parameters section of the fromcsv doc string
- #571899 label indexing fails when larry is 3d and index is a tuple of len 2
@@ -87,18 +97,14 @@ Bug fixes
- #583596 assert_larry_equal crashed when comparing float larry to str larry
- #585694 cumsum and cumprod crashed on dtype=int
-Details
--------
-For further details see the change log in la/ChangeLog.
-
la 0.2 (avocado)
================
*Release date: 2010-04-27*
-New larry methods
------------------
+**New larry methods**
+
- lix : Index into a larry using labels or index numbers or both
- swapaxes : Swap the two specified axes
- sortaxis : Sort data (and label) according to label along specified axis
@@ -108,16 +114,16 @@ New larry methods
- insertaxis : Insert a new axis at the specified position
- invert : Element by element inverting of True to False and False to True
-Enhancements
-------------
+**Enhancements**
+
- All larry methods can now take nd input arrays (some previously 2d only)
- Added ability to save larrys with datetime.date labels to HDF5
- New function (panel) to convert larry of shape (n, m, k) to shape (m*k, n)
- Expanded documentation
- Over 280 new unit tests; testing easier with new assert_larry_equal function
-Bug fixes
----------
+**Bug fixes**
+
- #517912: larry([]) == larry([]) raised IndexError
- #518096: larry.fromdict failed due to missing import
- #518106: la.larry.fromdict({}) failed
View
37 doc/source/functions.rst
@@ -29,7 +29,7 @@ The alignment functions help you align one of more larrys.
------------
-.. autofunction:: la.binaryop
+.. autofunction:: la.align_raw
------------
@@ -40,6 +40,34 @@ The alignment functions help you align one of more larrys.
.. autofunction:: la.intersection
+Binary
+------
+
+The binary functions combine two larrys into one.
+
+------------
+
+.. autofunction:: la.binaryop
+
+------------
+
+.. autofunction:: la.add
+
+
+Random
+------
+
+Functions that return larrys containing random samples.
+
+------------
+
+.. autofunction:: la.rand
+
+------------
+
+.. autofunction:: la.randn
+
+
Misc
----
@@ -57,11 +85,4 @@ Miscellaneous functions that operate on larrys.
.. autofunction:: la.cov
-------------
-
-.. autofunction:: la.rand
-
-------------
-
-.. autofunction:: la.randn
View
2 doc/source/index.rst
@@ -12,6 +12,8 @@ Meet larry, he's a labeled array:
archive
reference
functions
+ release
+ license
Indices and tables
==================
View
171 doc/source/intro.rst
@@ -7,173 +7,4 @@ larry is a labeled Numpy array. In this part of the manual I'll try to give
you a sense of what larry can do and then talk about the license and
installation.
-Who's larry?
-============
-
-The main class of the la package is a labeled array, larry. A larry consists
-of data and labels. The data is stored as a NumPy array and the labels as a
-list of lists (one list per dimension).
-
-Here's larry in schematic form:
-::
- date1 date2 date3
- 'AAPL' 209.19 207.87 210.11
- y = 'IBM' 129.03 130.39 130.55
- 'DELL' 14.82 15.11 14.94
-
-The larry above is stored internally as a `Numpy <http://www.numpy.org>`_
-array and a list of lists:
-::
- y.label = [['AAPL', 'IBM', 'DELL'], [date1, date2, date3]]
- y.x = np.array([[209.19, 207.87, 210.11],
- [129.03, 130.39, 130.55],
- [ 14.82, 15.11, 14.94]])
-
-A larry can have any number of dimensions except zero. Here, for example, is
-one way to create a one-dimensional larry:
-::
- >>> import la
- >>> y = la.larry([1, 2, 3])
-
-In the statement above the list is converted to a Numpy array and the labels
-default to ``range(n)``, where *n* in this case is 3.
-
-larry has built-in methods such as **movingsum, ranking, merge, shuffle,
-zscore, demean, lag** as well as typical Numpy methods like **sum, max, std,
-sign, clip**. NaNs are treated as missing data.
-
-Alignment by label is automatic when you add (or subtract, multiply, divide)
-two larrys.
-
-You can archive larrys in HDF5 format using **save** and **load** or using a
-dictionary-like interface:
-::
- >>> io = la.IO('/tmp/dataset.hdf5')
- >>> io['y'] = y # <--- save
- >>> z = io['y'] # <--- load
- >>> del io['y'] # <--- delete from archive
-
-For the most part larry acts like a Numpy array. And, whenever you want,
-you have direct access to the Numpy array that holds your data. For
-example if you have a function, *myfunc*, that works on Numpy arrays and
-doesn't change the shape or ordering of the array, then you can use it on a
-larry, *y*, like this:
-::
- y.x = myfunc(y.x)
-
-larry adds the convenience of labels, provides many built-in methods, and
-let's you use your existing array functions.
-
-License
-=======
-
-The ``la`` package is distributed under a Simplified BSD license. Parts of
-NumPy, Scipy, and numpydoc, which all have BSD licenses, are included in
-``la``. See the LICENSE file, which is distributed with the ``la`` package,
-for details.
-
-Installation
-============
-
-The ``la`` package requires Python and Numpy. Numpy 1.4.1 or newer is
-recommended for its improved NaN handling. Also some of the unit tests in the
-``la`` package require Numpy 1.4 or newer and many require
-`nose <http://somethingaboutorange.com/mrl/projects/nose>`_.
-
-To save and load larrys in HDF5 format, you need
-`h5py <http://h5py.alfven.org>`_ with HDF5 1.8.
-
-To install ``la``:
-::
- $ python setup.py build
- $ sudo python setup.py install
-
-Or, if you wish to specify where ``la`` is installed, for example inside
-``/usr/local``:
-::
- $ python setup.py build
- $ sudo python setup.py install --prefix=/usr/local
-
-After you have installed ``la``, run the suite of unit tests:
-::
- >>> import la
- >>> la.test()
- <snip>
- Ran 2958 tests in 1.351s
- OK
- <nose.result.TextTestResult run=2958 errors=0 failures=0>
-
-The ``la`` package contains a C extension module. The functions in the module
-speed up common alignment operations such as adding two unaligned larrys. If
-the module doesn't compile when you build ``la`` then there's an automatic
-fallback to a python version of the functions. To see whether you are using
-the C version or the Python versions:
-::
- >>> la.info()
- la version 0.4.0
- la file /usr/local/lib/python2.6/dist-packages/la/__init__.pyc
- HDF5 archiving Available
- listmap Faster C version
- listmap_fill Faster C version
-
-Since ``la`` can run in a pure python mode, you can use ``la`` by just saving
-it and making sure that python can find it.
-
-URLs
-====
-
-=============== ========================================================
- docs http://larry.sourceforge.net
- download http://pypi.python.org/pypi/la
- code http://github.com/kwgoodman/la
- list http://groups.google.ca/group/pystatsmodels
- devel list https://launchpad.net/~larry-discuss
-=============== ========================================================
-
-``la`` at a glance
-==================
-
-**la package**
-
-====================================== ====================================
-package name ``la``
-web site http://larry.sourceforge.net
-license Simplified BSD
-programming languages Python, Cython
-required dependencies Python, NumPy
-optional dependencies h5py, Scipy, nose, C-compiler
-year started (open source) 2008 (2010)
-====================================== ====================================
-
-**Data object**
-
-====================================== ====================================
-data object (main class) larry
-number of dimensions supported nd > 0d
-data container Numpy array
-direct access to data container yes
-data types homogenous: float, int, str, object
-label container list of lists
-direct access to label container yes
-label types heterogenous, hashable
-label constraints unique along any one axis, hashable
-missing values NaN (float), partial: '' (str),
- None (object)
-binary operations on two data objects intersection of labels
-IO HDF5, partial support for CSV
-====================================== ====================================
-
-**Similar to Numpy**
-
-====================================== ====================================
-Numpy array ``la`` larry
-====================================== ====================================
-``arr = np.array([[1, 2], [3, 4]])`` ``lar = la.larry([[1, 2], [3, 4]])`` Note ``(arr == lar.x).all()`` is True
-``np.nansum(arr)`` ``lar.sum()``
-``arr.shape``, ``arr.dtype``, ``lar.shape``, ``lar.dtype``
-``arr.ndim``, ``arr.T`` ``lar.ndim``, ``lar.T``
-``arr.astype(float)`` ``lar.astype(float)``
-``arr1 + arr2`` ``lar1 + lar2``
-``arr[:,0]`` ``lar[:,0]``
-====================================== ====================================
-
+.. include:: ../../README.rst
View
1 doc/source/license.rst
@@ -0,0 +1 @@
+.. include:: ../../la/LICENSE
View
1 doc/source/release.rst
@@ -0,0 +1 @@
+.. include:: ../../RELEASE.rst
View
67 la/LICENSE
@@ -1,18 +1,28 @@
-larry is distributed under a Simplified BSD license. Parts of NumPy, Scipy and
-numpydoc, which all have BSD licenses, are included in larry.
-
-Numpy
- la/deflarry.py(astype): Docstring
- la/deflarry.py(take): Docstring
- la/flarry.py(rand): Docstring
- la/flarry.py(randn): Docstring
-Scipy
- la/util/scipy.py: Functions and docstrings
-numpydoc
- la/doc/shinxext/: Entire directory
-
----------------------------------------------------------------------------
-la license:
+=======
+License
+=======
+
+The ``la`` package is distributed under a Simplified BSD license. Parts of
+NumPy, Scipy and numpydoc, which all have BSD licenses, are included in larry.
+
+*Numpy*
+
+- la/deflarry.py(astype): Docstring
+- la/deflarry.py(take): Docstring
+- la/flarry.py(rand): Docstring
+- la/flarry.py(randn): Docstring
+
+*Scipy*
+
+- la/util/scipy.py: Functions and docstrings
+
+*numpydoc*
+
+- la/doc/shinxext/: Entire directory
+
+
+la license
+----------
Copyright (c) 2008, 2009, 2010, Archipel Asset Management AB.
All rights reserved.
@@ -39,8 +49,9 @@ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------
-NumPy license:
+
+NumPy license
+-------------
Copyright (c) 2005-2009, NumPy Developers.
All rights reserved.
@@ -50,16 +61,16 @@ modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
+ notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
- copyright notice, this list of conditions and the following
- disclaimer in the documentation and/or other materials provided
- with the distribution.
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
* Neither the name of the NumPy Developers nor the names of any
- contributors may be used to endorse or promote products derived
- from this software without specific prior written permission.
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@@ -73,8 +84,9 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------
-SciPy license:
+
+SciPy license
+-------------
Copyright (c) 2001, 2002 Enthought, Inc.
All rights reserved.
@@ -107,8 +119,9 @@ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
----------------------------------------------------------------------------
-numpydoc license:
+
+numpydoc license
+----------------
The numpydoc license is in la/doc/sphinxext/LICENSE.txt
View
2 la/__init__.py
@@ -13,7 +13,7 @@
from numpy import nan, inf
from la.flarry import (union, intersection, stack, panel, cov, rand, randn,
- align, binaryop)
+ align, align_raw, binaryop, add)
from la.util.report import info
from la.version import __version__
from la.util import testing
View
16 la/deflarry.py
@@ -641,7 +641,15 @@ def invert(self):
__array_priority__ = 10
def __add__(self, other):
- """Sum a larry with another larry, Numpy array, or scalar.
+ """
+ Sum a larry with another larry, Numpy array, or scalar.
+
+ If two larrys are added then the larrys are joined with an inner join
+ (i.e., the intersection of the labels).
+
+ See Also
+ --------
+ la.add: Sum of two larrys using given join method and fill method.
Examples
--------
@@ -719,7 +727,8 @@ def __rsub__(self, other):
return -self.__sub__(other)
def __div__(self, other):
- """Divide a larry with a another larry, Numpy array, or scalar.
+ """
+ Divide a larry with a another larry, Numpy array, or scalar.
Examples
--------
@@ -807,7 +816,8 @@ def __mul__(self, other):
__rmul__ = __mul__
def __and__(self, other):
- """Logical and a larry with a another larry, Numpy array, or scalar.
+ """
+ Logical and a larry with a another larry, Numpy array, or scalar.
Notes
-----
View
196 la/flarry.py
@@ -341,6 +341,91 @@ def align_raw(lar1, lar2, join='inner', cast=True):
label.append(list3)
return x1, x2, label, x1isview, x2isview
+
+def union(axis, *args):
+ """
+ Union of labels along specified axis.
+
+ Parameters
+ ----------
+ axis : int
+ The axis along which to take the union of the labels.
+ args : larrys
+ The larrys (separated by commas) over which the union is taken.
+
+ Returns
+ -------
+ out : list
+ A list containing the union of the labels.
+
+ See Also
+ --------
+ la.intersection : Intersection of labels along specified axis.
+
+ Examples
+ --------
+ >>> import la
+ >>> y1 = larry([[1, 2], [3, 4]], [['a', 'b'], ['c', 'd']])
+ >>> y2 = larry([[1, 2], [3, 4]], [['e', 'b'], ['f', 'd']])
+ >>> la.union(0, y1, y2)
+ ['a', 'b', 'e']
+ >>> la.union(1, y1, y2)
+ ['c', 'd', 'f']
+
+ """
+ rc = frozenset([])
+ for arg in args:
+ if isinstance(arg, larry):
+ rc = frozenset(arg.label[axis]) | rc
+ else:
+ raise TypeError, 'One or more input is not a larry'
+ rc = list(rc)
+ rc.sort()
+ return rc
+
+def intersection(axis, *args):
+ """
+ Intersection of labels along specified axis.
+
+ Parameters
+ ----------
+ axis : int
+ The axis along which to take the intersection of the labels.
+ args : larrys
+ The larrys (separated by commas) over which the intersection is taken.
+
+ Returns
+ -------
+ out : list
+ A list containing the intersection of the labels.
+
+ See Also
+ --------
+ la.union : Union of labels along specified axis.
+
+ Examples
+ --------
+ >>> import la
+ >>> y1 = larry([[1, 2], [3, 4]], [['a', 'b'], ['c', 'd']])
+ >>> y2 = larry([[1, 2], [3, 4]], [['e', 'b'], ['f', 'd']])
+ >>> la.intersection(0, y1, y2)
+ ['b']
+ >>> la.intersection(1, y1, y2)
+ ['d']
+
+ """
+ rc = frozenset(args[0].label[axis])
+ for i in xrange(1, len(args)):
+ arg = args[i]
+ if isinstance(arg, larry):
+ rc = frozenset(arg.label[axis]) & rc
+ else:
+ raise TypeError, 'One or more input is not a larry'
+ rc = list(rc)
+ rc.sort()
+ return rc
+
+# Binary-- -----------------------------------------------------------------
def binaryop(func, lar1, lar2, join='inner', cast=True, missone='ignore',
misstwo='ignore', **kwargs):
@@ -477,6 +562,117 @@ def binaryop(func, lar1, lar2, join='inner', cast=True, missone='ignore',
return larry(x, label, integrity=False)
+def add(lar1, lar2, join='inner', cast=True, missone='ignore',
+ misstwo='ignore'):
+ """
+ Sum of two larrys using given join method and fill method.
+
+ Parameters
+ ----------
+ lar1 : larry
+ The larry on the left-hand side of the sum. Must have the same number
+ of dimensions as `lar2`.
+ lar2 : larry
+ The larry on the right-hand side of the sum. Must have the same number
+ of dimensions as `lar1`.
+ join : {'inner', 'outer', 'left', 'right', list}, optional
+ The method used to join the two larrys. The default join method along
+ all axes is 'inner', i.e., the intersection of the labels. If `join`
+ is a list of strings then the length of the list should be the number
+ of dimensions of the two larrys. The first element in the list is the
+ join method for axis=0, the second element is the join method for
+ axis=1, and so on.
+ cast : bool, optional
+ Only float, str, and object dtypes have missing value markers (la.nan,
+ '', and None, respectively). Other dtypes, such as int and bool, do
+ not have a missing value marker. If `cast` is set to True (default)
+ then int and bool dtypes, for example, will be cast to float if any
+ new rows, columns, etc are created. If cast is set to False, then a
+ TypeError will be raised for int and bool dtype input if the join
+ introduces new rows, columns, etc. An inner join will never introduce
+ new rows, columns, etc.
+ missone : {scalar, 'ignore'}, optional
+ By default ('ignore') no special treatment of missing values is made.
+ If, however, `missone` is set to something other than 'ignore', such
+ as 0, then all elements that are missing in one larry but not missing
+ in the other larry are replaced by `missone`. For example, if an
+ element is in one larry but missing in the other larry then you may
+ want to set the missing value to zero when summing two larrys.
+ misstwo : {scalar, 'ignore'}, optional
+ By default ('ignore') no special treatment of missing values is made.
+ If, however, `misstwo` is set to something other than 'ignore', such
+ as 0, then all elements that are missing in both larrys are replaced
+ by `misstwo`.
+
+ Returns
+ -------
+ y : larry
+ The sum of the two larrys.
+
+ See Also
+ --------
+ la.larry.__add__: Sum a larry with another larry, Numpy array, or scalar.
+ la.binaryop: Binary operation on two larrys using given function.
+
+ Notes
+ -----
+ This is a convenience function that calls la.binaryop with `func` set
+ to numpy.add.
+
+ Examples
+ --------
+ Create two larrys:
+
+ >>> from la import nan
+ >>> lar1 = larry([1, 2, nan], [['a', 'b', 'c']])
+ >>> lar2 = larry([1, nan, nan], [['a', 'b', 'dd']])
+
+ The default is an inner join (note that lar1 and lar2 have two labels in
+ common):
+
+ >>> la.add(lar1, lar2)
+ label_0
+ a
+ b
+ x
+ array([ 2., NaN])
+
+ If one data element is missing in one larry but not in the other, then you
+ can replace the missing value with `missone` (here 0):
+
+ >>> la.add(lar1, lar2, missone=0)
+ label_0
+ a
+ b
+ x
+ array([ 2., 2.])
+
+ An outer join:
+
+ >>> la.add(lar1, lar2, join='outer')
+ label_0
+ a
+ b
+ c
+ dd
+ x
+ array([ 2., NaN, NaN, NaN])
+
+ An outer join with single and double missing values replaced by zero:
+
+ >>> la.add(lar1, lar2, join='outer', missone=0, misstwo=0)
+ label_0
+ a
+ b
+ c
+ dd
+ x
+ array([ 2., 2., 0., 0.])
+
+ """
+ return binaryop(np.add, lar1, lar2, join=join, cast=cast, missone=missone,
+ misstwo=misstwo)
+
def union(axis, *args):
"""
Union of labels along specified axis.
View
209 la/tests/flarry_test.py
@@ -7,7 +7,7 @@
nan = np.nan
from la import larry
-from la import union, intersection, panel, stack, cov, align, binaryop
+from la import (union, intersection, panel, stack, cov, align, binaryop, add)
from la.util.testing import assert_larry_equal as ale
@@ -620,6 +620,210 @@ def test_binaryop_22(self):
msg = "binaryop failed"
ale(actual, desired, msg, original=y1)
ale(actual, desired, msg, original=y2)
+
+class Test_add(unittest.TestCase):
+ "Test la.add()"
+
+ def test_add_01(self):
+ "add test #01"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2)
+ desired = larry([2, 4])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_02(self):
+ "add test #02"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='inner')
+ desired = larry([2, 4])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_03(self):
+ "add test #03"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='inner', missone=0)
+ desired = larry([2, 4])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_04(self):
+ "add test #04"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='inner', missone=0, misstwo=0)
+ desired = larry([2, 4])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_05(self):
+ "add test #05"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, misstwo=0)
+ desired = larry([2, 4])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_06(self):
+ "add test #06"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='outer')
+ desired = larry([2, 4, nan])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_07(self):
+ "add test #07"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='outer', missone=0)
+ desired = larry([2, 4, 3], dtype=float)
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_08(self):
+ "add test #08"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='outer', missone=0, misstwo=0)
+ desired = larry([2, 4, 3], dtype=float)
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_09(self):
+ "add test #09"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='outer', misstwo=0)
+ desired = larry([2, 4, nan])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_10(self):
+ "add test #10"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='left')
+ desired = larry([2, 4])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_11(self):
+ "add test #11"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='left', missone=0)
+ desired = larry([2, 4])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_12(self):
+ "add test #12"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='left', missone=0, misstwo=0)
+ desired = larry([2, 4])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_13(self):
+ "add test #13"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='right')
+ desired = larry([2, 4, nan])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_14(self):
+ "add test #14"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='right', missone=0)
+ desired = larry([2, 4, 3], dtype=float)
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_15(self):
+ "add test #15"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='right', missone=0, misstwo=0)
+ desired = larry([2, 4, 3], dtype=float)
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_16(self):
+ "add test #16"
+ y1 = larry([1, 2])
+ y2 = larry([1, 2, 3])
+ actual = add(y1, y2, join='right', misstwo=0)
+ desired = larry([2, 4, nan])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_17(self):
+ "add test #17"
+ y1 = larry([1, nan, nan, 1])
+ y2 = larry([1, 1, nan, 1], [[0, 1, 2, 'a']])
+ actual = add(y1, y2)
+ desired = larry([2, nan, nan], [[0, 1, 2]])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_18(self):
+ "add test #18"
+ y1 = larry([1, nan, nan, 1])
+ y2 = larry([1, 1, nan, 1], [[0, 1, 2, 'a']])
+ actual = add(y1, y2, join='outer')
+ desired = larry([2, nan, nan, nan, nan], [[0, 1, 2, 3, 'a']])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_19(self):
+ "add test #19"
+ y1 = larry([1, nan, nan, 1])
+ y2 = larry([1, 1, nan, 1], [[0, 1, 2, 'a']])
+ actual = add(y1, y2, join='outer', missone=0)
+ desired = larry([2, 1, nan, 1, 1], [[0, 1, 2, 3, 'a']])
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
+ def test_add_20(self):
+ "add test #20"
+ y1 = larry([1, nan, nan, 1])
+ y2 = larry([1, 1, nan, 1], [[0, 1, 2, 'a']])
+ actual = add(y1, y2, join='outer', missone=0, misstwo=0)
+ desired = larry([2, 1, 0, 1, 1], [[0, 1, 2, 3, 'a']], dtype=float)
+ msg = "add failed"
+ ale(actual, desired, msg, original=y1)
+ ale(actual, desired, msg, original=y2)
+
def suite():
s = []
@@ -627,7 +831,8 @@ def suite():
s.append(u(Test_func))
s.append(u(Test_align_1d))
s.append(u(Test_align_2d))
- s.append(u(Test_binaryop))
+ s.append(u(Test_binaryop))
+ s.append(u(Test_add))
return unittest.TestSuite(s)
def run():

0 comments on commit ec646b5

Please sign in to comment.
Something went wrong with that request. Please try again.