thouis / numpy-trac-migration

numpy Trac to github issues migration
2 stars 3 forks source link

Nested sequences with different lengths issues (migrated from Trac #297) #904

Closed thouis closed 11 years ago

thouis commented 11 years ago

Original ticket http://projects.scipy.org/numpy/ticket/297 Reported 2006-09-27 by atmention:FrancescAlted, assigned to unknown.

!NumPy is not detecting string nested sequence with different lengths:

>>> numpy.array(['aaa', ['bbb', 'ccc']])
array([aaa, ['b],
      dtype='|S3')

numarray does:

>>> numarray.strings.array(['aaa', ['bbb', 'ccc']])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/site-packages/numarray/strings.py", line 1117, in array
    padc=padc, kind=kind)
  File "/usr/lib/python2.4/site-packages/numarray/strings.py", line 977, in fromlist
    shape, itemsize = _slistShape(slist, itemsize=itemsize, shape=shape)
  File "/usr/lib/python2.4/site-packages/numarray/strings.py", line 912, in _slistShape
    shape_items = _slistShape0(slist)
  File "/usr/lib/python2.4/site-packages/numarray/strings.py", line 887, in _slistShape0
    raise ValueError("Nested sequences with different lengths.")
ValueError: Nested sequences with different lengths.

and I think this last message is quite adequate for the issue.

Furthermore, I'd say that a message like numarray is better for numerical nested sequences with different lengths.

NumPy:

>>> numpy.array([1, [2, 3]])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: an integer is required

numarray:

>>> numarray.array([1, [2, 3]])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/site-packages/numarray/numarraycore.py", line 417, in array
    return fromlist(sequence,type,shape)
  File "/usr/lib/python2.4/site-packages/numarray/numarraycore.py", line 267, in fromlist
    arr.fromlist(seq)
ValueError: Nested sequences with different lengths.
thouis commented 11 years ago

Comment in Trac by atmention:FrancescAlted, 2006-09-27

As an aside, I'd say that the error raised should be a !ValueError (i.e. a la numarray) and not a !TypeError, but this is subject to interpretation, of course.

thouis commented 11 years ago

Comment in Trac by atmention:teoliphant, 2006-09-27

I'll look into it. The strings problem we can hopefully fix. The other error is raised from a different location than the sequence conversion code. It may be harder to "fix"

thouis commented 11 years ago

Comment in Trac by atmention:teoliphant, 2006-09-27

I've changed string and unicode getitems so that sequences are not allowed (unless they are strings or unicode sequences. Previously str(obj) and unicode(obj) were being called on the object being set. If this was a sequence, then the stringified sequence was used without difficulty.

The array from sequence code basically looks like

get shape and size from sequence (by looking at the first element in each depth)

for k in range(shape[0]):

new[k] = obj[k]

#

So, a 1-d loop is used and the setitem is used of new with the getitem of obj to construct the result. If there is a mismatch in the level of nesting this is not caught until the attempt at setting doesn't work.

All error messages have been changed for the problem trying to set an array element with a sequence to "setting an array element with a sequence". It is still a type-error. For strings and unicode arrays, strings and unicode "sequences" are of course allowed.