Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Provide numpy-like constructors #23

Open
wants to merge 1 commit into from

2 participants

@llvilanova

This should close bug fperez/datarray#19.

@llvilanova llvilanova Provide two constructors, following numpy's semantics.
This provides numpy-like constructors:
     - array: create a new array from existing contents
     - DataArra: create a new array with given shape

This is meant to close bug http://github.com/fperez/datarray/issues/#issue/19
17a17e4
@fperez
Owner

I tried to merge your code, but unfortunately it breaks the test suite very badly... We do have a couple of minor failures right now in trunk, but after applying your commit I get:

((98c5238...))maqroll[tests]> python -c 'import datarray;datarray.test()'
Running unit tests for datarray
NumPy version 1.3.0
NumPy is installed in /usr/lib/python2.6/dist-packages/numpy
Python version 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3]
nose version 0.11.1
.E..........F.EEEF.EFEFE......EEEEEEEEEEEEEEEEEEEEEEE..EEEEE
======================================================================
ERROR: Failure: ValueError (Dimension mismatch between ticks and data at index 0)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.6/nose/loader.py", line 224, in generate
    for test in g():
  File "/home/fperez/usr/opt/lib/python2.6/site-packages/datarray/testing/tests/test_utils.py", line 22, in test_assert_datarray_equal
    y = DataArray([1, 2], [('a', ['a', 'b'])])
  File "/home/fperez/usr/opt/lib/python2.6/site-packages/datarray/datarray.py", line 621, in __new__
    _init_axes(res, None, labels)
  File "/home/fperez/usr/opt/lib/python2.6/site-packages/datarray/datarray.py", line 452, in _init_axes
    axes.append(Axis(label, i, dest, ticks=ticks))
  File "/home/fperez/usr/opt/lib/python2.6/site-packages/datarray/datarray.py", line 82, in __init__
    self._tick_dict = self._validate_ticks(ticks)
  File "/home/fperez/usr/opt/lib/python2.6/site-packages/datarray/datarray.py", line 148, in _validate_ticks
    raise ValueError(e)
ValueError: Dimension mismatch between ticks and data at index 0


Lots more output, snipped...


======================================================================
FAIL: datarray.tests.test_data_array.test_1d(False,)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.6/nose/case.py", line 183, in runTest
    self.test(*self.arg)
AssertionError

----------------------------------------------------------------------
Ran 60 tests in 0.026s

FAILED (errors=35, failures=4)

Could you ensure that the test suite passes, at least at the level it passes in current trunk, and ping us again?

Thanks,

f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 6, 2010
  1. @llvilanova

    Provide two constructors, following numpy's semantics.

    llvilanova authored
    This provides numpy-like constructors:
         - array: create a new array from existing contents
         - DataArra: create a new array with given shape
    
    This is meant to close bug http://github.com/fperez/datarray/issues/#issue/19
This page is out of date. Refresh to see the latest.
Showing with 48 additions and 38 deletions.
  1. +48 −38 datarray/datarray.py
View
86 datarray/datarray.py
@@ -425,6 +425,34 @@ def _names_to_numbers(axes, ax_ids):
return proc_ids
+def _init_axes (dest, source, labels):
+ # XXX if an entry of labels is a tuple, it is interpreted
+ # as a (label, ticks) tuple
+ if labels is None:
+ if hasattr(source,'axes'):
+ _set_axes(dest, source.axes)
+ return
+ labels = []
+ elif len(labels) > dest.ndim:
+ raise NamedAxisError('labels list should have length <= array ndim')
+
+ labels = list(labels) + [None]*(dest.ndim - len(labels))
+ axes = []
+ for i, label_spec in enumerate(labels):
+ if type(label_spec) == type(()):
+ if len(label_spec) != 2:
+ raise ValueError(
+ 'if the label specification is a tuple, it must be ' \
+ 'of the form (label, ticks)'
+ )
+ label, ticks = label_spec
+ else:
+ label = label_spec
+ ticks = None
+ axes.append(Axis(label, i, dest, ticks=ticks))
+
+ _set_axes(dest, axes)
+ _validate_axes(axes)
def _validate_axes(axes):
"""
@@ -565,51 +593,33 @@ def runs_op(*args, **kwargs):
runs_op.func_name = opname
runs_op.func_doc = super_op.__doc__
return runs_op
-
-
-class DataArray(np.ndarray):
+def array (data, labels=None, dtype=None, copy=True, order=None, ndmin=True):
+ # XXX accepting argument 'subok' does not make sense in here
+ res = np.array(data, dtype=dtype, copy=False, order=order,
+ ndmin=ndmin).view(type=DataArray)
+
+ _init_axes(res, data, labels)
+ if copy:
+ # TODO: still raises ValueError when 'resize' is called
+ new_res = res.copy()
+ del res
+ return new_res
+
+ return res
+
+class DataArray(np.ndarray):
# XXX- we need to figure out where in the numpy C code .T is defined!
@property
def T(self):
return self.transpose()
- def __new__(cls, data, labels=None, dtype=None, copy=False):
- # XXX if an entry of labels is a tuple, it is interpreted
- # as a (label, ticks) tuple
- # Ensure the output is an array of the proper type
- arr = np.array(data, dtype=dtype, copy=copy).view(cls)
- if labels is None:
- if hasattr(data,'axes'):
- _set_axes(arr, data.axes)
- return arr
- labels = []
- elif len(labels) > arr.ndim:
- raise NamedAxisError('labels list should have length <= array ndim')
-
- labels = list(labels) + [None]*(arr.ndim - len(labels))
- axes = []
- for i, label_spec in enumerate(labels):
- if type(label_spec) == type(()):
- if len(label_spec) != 2:
- raise ValueError(
- 'if the label specification is a tuple, it must be ' \
- 'of the form (label, ticks)'
- )
- label, ticks = label_spec
- else:
- label = label_spec
- ticks = None
- axes.append(Axis(label, i, arr, ticks=ticks))
-
- _set_axes(arr, axes)
-
- # validate the axes
- _validate_axes(axes)
-
-
- return arr
+ def __new__(cls, shape, labels=None, dtype=float, buffer=None, offset=0, strides=None, order=None):
+ res = np.ndarray.__new__(cls, shape, dtype=dtype, buffer=buffer,
+ offset=offset, strides=strides, order=order)
+ _init_axes(res, None, labels)
+ return res
@property
def aix(self):
Something went wrong with that request. Please try again.