Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ENH added arange(), zeros(), ones()

  • Loading branch information...
commit c79c3c2ac56b65b07bd444ea7fb78bba0f3dbded 1 parent 721010e
Steven Troxler authored
Showing with 344 additions and 6 deletions.
  1. +2 −2 README.rst
  2. +1 −0  RELEASE.rst
  3. +277 −3 la/flarry.py
  4. +64 −1 la/tests/flarry_test.py
View
4 README.rst
@@ -118,9 +118,9 @@ After you have installed ``la``, run the suite of unit tests::
>>> import la
>>> la.test()
<snip>
- Ran 3047 tests in 1.408s
+ Ran 3059 tests in 1.408s
OK
- <nose.result.TextTestResult run=3047 errors=0 failures=0>
+ <nose.result.TextTestResult run=3059 errors=0 failures=0>
The ``la`` package contains C extensions that speed up common alignment
operations such as adding two unaligned larrys. If the C extensions don't
View
1  RELEASE.rst
@@ -16,6 +16,7 @@ la 0.6
- la.isaligned() returns True if two larrys are aligned along specified axis
- la.sortby() sorts a larry by a row or column specified by its label
- la.align_axis() aligns multiple larrys along (possibly) different axes
+- la.arange(), la.ones(), and la.zeros() provide quick ways to get test data
**Enhancements**
View
280 la/flarry.py
@@ -7,9 +7,10 @@
from la.farray import covMissing
from la.missing import missing_marker, ismissing
-__all__ = ['align', 'align_axis', 'align_raw', 'isaligned', 'union',
- 'intersection', 'binaryop', 'add', 'sortby', 'subtract', 'multiply',
- 'divide', 'unique', 'stack', 'panel', 'cov', 'rand', 'randn']
+__all__ = ['align', 'align_axis', 'align_raw', 'arange', 'ones', 'zeros',
+ 'isaligned', 'union', 'intersection', 'binaryop', 'add', 'sortby',
+ 'subtract', 'multiply', 'divide', 'unique', 'stack', 'panel', 'cov',
+ 'rand', 'randn']
# Alignment -----------------------------------------------------------------
@@ -649,6 +650,279 @@ def intersection(axis, *args):
rc = list(rc)
rc.sort()
return rc
+# Instantiation shortcuts---------------------------------------------------
+
+def arange(*args, **kwargs):
+ """
+ Array of sequential integers, filled where later axes have smaller spacing.
+
+ Parameters
+ ----------
+ args : `n` ints, optional
+ The dimensions of the returned larry, should be all positive. these
+ may be omitted if you pass in a label as a keyword argument.
+ For convenience, if `arg[0]` is a list, then it will be
+ assumed to refer to `label` (see below).
+ kwargs : keyword arguments, optional
+ If 'label' is supplied, the output will be thus labeled and the shape
+ will be determined by looking at the given labels (any dimension
+ arguments in `args` will be ignored in this case). Other kwargs are as
+ for np.arange, except that 'stop' is not allowed since it is determined
+ by the dimensions of the larry.
+
+ Returns
+ -------
+ lar : larry
+ a ``(d1, ..., dn)``-shaped larry of consecutive integers (or spaced
+ `step` apart if such a keyword argument is used), labeled either by
+ integers or by the labels supplied.
+
+ Examples
+ --------
+ A basic, 1d arange using the 'dtype' argument:
+
+ >>> la.arange(5, dtype='f4')
+ label_0
+ 0
+ 1
+ 2
+ 3
+ 4
+ x
+ array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=np.float32)
+
+ A multi-dimensional arange:
+ >>> la.arange(2,3,3)
+ label_0
+ 0
+ 1
+ label_1
+ 0
+ 1
+ 2
+ label_2
+ 0
+ 1
+ 2
+ x
+ array([[[ 0, 1, 2],
+ [ 3, 4, 5],
+ [ 6, 7, 8]],
+
+ [[ 9, 10, 11],
+ [12, 13, 14],
+ [15, 16, 17]]])
+
+ Using the 'label' keword:
+
+ >>> la.arange(label=[['a', 'b']])
+ label_0
+ a
+ b
+ x
+ array([0, 1])
+
+ """
+ # set label and size
+ if 'label' in kwargs:
+ label = list(kwargs['label'])
+ shape = [len(lab) for lab in label]
+ del kwargs['label']
+ else:
+ if isinstance(args[0], list):
+ label = list(args[0])
+ shape = [len(lab) for lab in label]
+ else:
+ shape = args
+ label = [range(i) for i in shape]
+ # start and step
+ start, step = 0, 1
+ if 'start' in kwargs:
+ start = kwargs['start']
+ del kwargs['start']
+ if 'step' in kwargs:
+ step = kwargs['step']
+ del kwargs['step']
+ # make the data, adjust, and put together
+ total = np.product(np.array(shape))
+ data = np.arange(total, **kwargs).reshape(shape)
+ data *= step; data += start
+ return larry(data, label)
+
+def ones(shape=None, label=None, **kwargs):
+ """
+ Array of sequential integers, filled where later axes have smaller spacing.
+
+ Parameters
+ ----------
+ shape : {int, tuple}, optional
+ If shape is given, then a label must be supplied. If both
+ are supplied, then `shape` is ignored. If `shape` is an int, output
+ will be one-dimensional.
+ label : list, optional
+ List of lists, a label for the larry produced. For convenience, if no
+ keywords are supplied but the first argument is a list, then
+ that argument will be assumed to be `label` rather than `shape`.
+ kwargs : keyword arguments, optional
+ If 'label' is supplied, the output will be thus labeled and the shape
+ will be determined by looking at the given labels (if shape is
+ supplied, it will be overridden
+ arguments in `args` will be ignored in this case). Other kwargs are as
+ for np.arange.
+
+ Returns
+ -------
+ lar : larry
+ a ``(d1, ..., dn)``-shaped larry of ones, labeled either by
+ integers or by the labels supplied.
+
+ Examples
+ --------
+ A basic, 1d arange using the 'dtype' argument:
+
+ >>> la.ones(5, dtype='i4')
+ label_0
+ 0
+ 1
+ 2
+ 3
+ 4
+ x
+ array([1, 1, 1, 1, 1], dtype=np.int32)
+
+ A multi-dimensional arange:
+ >>> la.arange(2,3,3)
+ label_0
+ 0
+ 1
+ label_1
+ 0
+ 1
+ 2
+ label_2
+ 0
+ 1
+ 2
+ x
+ array([[[ 1., 1., 1.],
+ [ 1., 1., 1.],
+ [ 1., 1., 1.]],
+
+ [[ 1., 1., 1.],
+ [ 1., 1., 1.],
+ [ 1., 1., 1.]]])
+
+ Using the 'label' argument:
+
+ >>> la.arange(label=[['a', 'b']])
+ label_0
+ a
+ b
+ x
+ array([1., 1.])
+
+ """
+ if shape is not None:
+ if isinstance(shape, list):
+ label = shape
+ if isinstance(shape, int):
+ shape = (shape,)
+ if label is not None:
+ shape = [len(lab) for lab in label]
+ else:
+ if shape is None:
+ raise ValueError("Either `label` or `shape` must be supplied.")
+ label = [range(i) for i in shape]
+ data = np.ones(shape, **kwargs)
+ return larry(data, label)
+
+def zeros(shape=None, label=None, **kwargs):
+ """
+ Array of sequential integers, filled where later axes have smaller spacing.
+
+ Parameters
+ ----------
+ shape : {int, tuple}, optional
+ If shape is given, then a label must be supplied. If both
+ are supplied, then `shape` is ignored. If `shape` is an int, output
+ will be one-dimensional.
+ label : list, optional
+ List of lists, a label for the larry produced. For convenience, if no
+ keywords are supplied but the first argument is a list, then
+ that argument will be assumed to be `label` rather than `shape`.
+ kwargs : keyword arguments, optional
+ If 'label' is supplied, the output will be thus labeled and the shape
+ will be determined by looking at the given labels (if shape is
+ supplied, it will be overridden
+ arguments in `args` will be ignored in this case). Other kwargs are as
+ for np.arange.
+
+ Returns
+ -------
+ lar : larry
+ a ``(d1, ..., dn)``-shaped larry of zeros, labeled either by
+ integers or by the labels supplied.
+
+ Examples
+ --------
+ A basic, 1d arange using the 'dtype' argument:
+
+ >>> la.ones(5, dtype='i4')
+ label_0
+ 0
+ 1
+ 2
+ 3
+ 4
+ x
+ array([0, 0, 0, 0, 0], dtype=np.int32)
+
+ A multi-dimensional arange:
+ >>> la.arange(2,3,3)
+ label_0
+ 0
+ 1
+ label_1
+ 0
+ 1
+ 2
+ label_2
+ 0
+ 1
+ 2
+ x
+ array([[[ 0., 0., 0.],
+ [ 0., 0., 0.],
+ [ 0., 0., 0.]],
+
+ [[ 0., 0., 0.],
+ [ 0., 0., 0.],
+ [ 0., 0., 0.]]])
+
+ Using the 'label' argument:
+
+ >>> la.arange(label=[['a', 'b']])
+ label_0
+ a
+ b
+ x
+ array([0., 0.])
+
+ """
+ if shape is not None:
+ if isinstance(shape, list):
+ label = shape
+ if isinstance(shape, int):
+ shape = (shape,)
+ if label is not None:
+ shape = [len(lab) for lab in label]
+ else:
+ if shape is None:
+ raise ValueError("Either `label` or `shape` must be supplied.")
+ label = [range(i) for i in shape]
+ data = np.zeros(shape, **kwargs)
+ return larry(data, label)
+
# Binary-- -----------------------------------------------------------------
View
65 la/tests/flarry_test.py
@@ -10,7 +10,7 @@
from la import larry, rand
from la import (union, intersection, panel, stack, cov, align, isaligned,
binaryop, add, subtract, multiply, divide, unique, sortby,
- align_axis)
+ align_axis, arange, ones, zeros)
from la.util.testing import assert_larry_equal as ale
@@ -661,6 +661,69 @@ def test_align_axis_9(self):
ale(a2, d2, msg % '2nd', original=y2)
ale(a3, d3, msg % '3rd', original=y3)
+class Test_quick_inst(unittest.TestCase):
+ "Test quick larry-creation functions."
+
+ def test_arange_1(self):
+ a = arange(5)
+ d = larry([0, 1, 2, 3, 4])
+ ale(a, d, "arange failed.")
+
+ def test_arange_2(self):
+ a = arange(label=[['a', 'b', 'c', 'd']])
+ d = larry([0, 1, 2, 3], [['a', 'b', 'c', 'd']])
+ ale(a, d, "arange failed.")
+
+ def test_arange_3(self):
+ a = arange(2, 2, dtype='f8')
+ d = larry(np.array([0, 1, 2, 3], dtype='f8').reshape(2,2))
+ ale(a, d, "arange failed.")
+
+ def test_arange_4(self):
+ a = arange([['a', 'b'], ['c', 'd']])
+ d = larry([[0, 1], [2, 3]], [['a', 'b'], ['c', 'd']])
+ ale(a, d, "arange failed.")
+
+ def test_ones_1(self):
+ a = ones(5)
+ d = larry([1., 1., 1., 1., 1.])
+ ale(a, d, "ones failed.")
+
+ def test_ones_2(self):
+ a = ones(label=[['a', 'b', 'c', 'd']])
+ d = larry([1., 1., 1., 1.], [['a', 'b', 'c', 'd']])
+ ale(a, d, "ones failed.")
+
+ def test_ones_3(self):
+ a = ones((2, 2), dtype='i8')
+ d = larry(np.array([1., 1., 1., 1.], dtype='i8').reshape(2,2))
+ ale(a, d, "ones failed.")
+
+ def test_ones_4(self):
+ a = ones([['a', 'b'], ['c', 'd']])
+ d = larry([[1., 1.], [1., 1.]], [['a', 'b'], ['c', 'd']])
+ ale(a, d, "ones failed.")
+
+ def test_zeros_1(self):
+ a = zeros(5)
+ d = larry([0., 0., 0., 0., 0.])
+ ale(a, d, "zeros failed.")
+
+ def test_zeros_2(self):
+ a = zeros(label=[['a', 'b', 'c', 'd']])
+ d = larry([0., 0., 0., 0.], [['a', 'b', 'c', 'd']])
+ ale(a, d, "zeros failed.")
+
+ def test_zeros_3(self):
+ a = zeros((2, 2), dtype='i8')
+ d = larry(np.array([0., 0., 0., 0.], dtype='i8').reshape(2,2))
+ ale(a, d, "zeros failed.")
+
+ def test_zeros_4(self):
+ a = zeros([['a', 'b'], ['c', 'd']])
+ d = larry([[0., 0.], [0., 0.]], [['a', 'b'], ['c', 'd']])
+ ale(a, d, "zeros failed.")
+
class Test_binaryop(unittest.TestCase):
"Test la.binaryop()"
Please sign in to comment.
Something went wrong with that request. Please try again.