GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: automatically convert recarray dtype to np.record by ahaldane · Pull Request #5943 · numpy/numpy

Conversation

@ahaldane

@ahaldane ahaldane commented Jun 5, 2015

Copy link
Copy Markdown
Member

As discussed in #3581, this PR automatically converts the dtype of np.recarrays to np.record.

To get unit tests to pass I also had to add a setattr to the MaskedArray class: MaskedArrays did not support assignment to dtype attribute, as demonstrated in the following examples:

>>> a = np.zeros(4, dtype='f4,i4')
>>> m = np.ma.array(a)
>>> m.dtype = np.dtype('f8')
>>> m   # Exception
>>> a.view(dtype='f8', type=np.ma.MaskedArray) #Exception

MaskedArray.setattr now catches assignments to dtype and updates the mask accordingly.

Possible issues with this PR, if anyone has any ideas:

  1. Viewing as a.view(np.recarray) now changes the dtype. It's not totally clear to me this won't break anything. (Example: It broke maskedarrays above, but due to a maskedaray bug).

  2. Another example of the last point: Views are not reversible.

>>> a = np.zeros(4, 'f4,i4')
>>> b = a.view(np.recarray).view(np.ndarray)
>>> b.dtype
dtype((numpy.record, [('f0', '<f4'), ('f1', '<i4')]))
  1. Note (not really a problem): Attempt to create record arrays of non-structured type does not set the dtype to np.record (since it is not possible to do so). This is not new to this PR.
>>> a = np.zeros(5, 'f8')
>>> np.rec.array(a)
array([ 0.,  0.,  0.,  0.], 
      dtype=float64).view(numpy.recarray)

Benefits:

  • Goal is to improve results in record array is made up of numpy.void, not numpy.record #3581

I still need to finish writing unit tests

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ahaldane
ahaldane force-pushed the record_finalize branch 4 times, most recently from bf2597f to 742801f Compare June 5, 2015 18:10
@ahaldane

ahaldane commented Jun 5, 2015

Copy link
Copy Markdown
Member Author

ugh there are still two test failures, deep in MaskedArray code. I'll work on this later.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@charris

charris commented Jun 5, 2015

Copy link
Copy Markdown
Member

My sympathies ;)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhvk

mhvk commented Jun 5, 2015

Copy link
Copy Markdown
Contributor

@ahaldane - I hadn't thought about the reversibility. Since that cannot possibly be guaranteed anyway, I think it is not a major problem, but perhaps it is good in the documentation to mention how one would get a regular structured array out (i.e., reverse the dtype change). Is it just rec.view(dtype=np.dtype(np.void, rec.dtype), np.ndarray)?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahaldane
ahaldane force-pushed the record_finalize branch from 742801f to dc5161c Compare June 8, 2015 00:28
@ahaldane

ahaldane commented Jun 8, 2015

Copy link
Copy Markdown
Member Author

Update, here's what's new

  1. Updated docs for how to reverse the view. @mhvk That makes sense. And I think the reverse view needs to be
rec.view(rec.dtype.fields or rec.dtype, np.ndarray)

to take into account non-structured (non-void) record arrays. IE, both of the following record arrays

rec = np.rec.array(ones(4, dtype='f4,i4')) # structured, dtype is np.void
rec = np.rec.array(ones(4, dtype='f8'))    # unstructured, dtype is np.float64
  1. While working out the view above, I noticed that non-void record arrays can be created/represented using rec.array despite not having np.record dtype, so I modified recarray.__repr__ accordingly.

  2. I removed the recarray.view method. The only case it differed from ndarray.view was for non-structured dtype, in which case it returned an ndarray instead of a (non-structured) recarray. But this breaks chained views:

>>> rec.view('f8').view('f4,i4')

The user probably wants the final result to be a record array, but the middle view currently converts to ndarray. There are already a fair number of places non-structured recarrays turn up (see above), so no reason to hide them.

  1. Hopefully clarified the docs for the dtype((base_dtype, new_dtype)) form of dtype specification.

As for the MaskedArray ssues, they pass tests now but I think there may still be something wrong. I still need to think more about it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhvk

mhvk commented Jun 8, 2015

Copy link
Copy Markdown
Contributor

@ahaldane - I had a quick look through the latest PR and it seems OK (but as I don't use recarray much myself, someone else should definitely look as well).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@charris charris added 00 - Bug component: numpy._core component: numpy.lib labels Jun 13, 2015
@ahaldane
ahaldane force-pushed the record_finalize branch from dc5161c to 73bb3e1 Compare June 13, 2015 17:27
@ahaldane

ahaldane commented Jun 13, 2015

Copy link
Copy Markdown
Member Author

Updated with extra tweaks to the MaskedArray part. (I think the recarray part is done).

I feel better about the MaskedArray changes now. As a reminder, this needed to happen to pass unit tests related to masked record arrays.

A problem was that the masked singleton's shape was being overwritten if I wasn't careful (another example of #5806). I think my changes are safe wrt this now. As an extra precaution I also tweaked MaskedArray.anom by quitting early if the result is masked, to avoid any extra manipulation of masked.

I also removed code that updates the mask's shape after the dtype is updated. I am not sure what its purpose was, and removing it doesn't affect any unit tests. Maskedarray doesn't support dtype changes that change the shape anyway. Maybe I have to think more about it, but right now it seems OK to me, and is easily changed back.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Jun 14, 2015
Numpy 1.10 issues for io.fits astropy/astropy#3854
Closed
BUG Ensure masked object arrays can always return single items. #5962
Merged
@charris charris added this to the 1.10.0 release milestone Jun 17, 2015
mhvk
mhvk reviewed Jun 18, 2015
View reviewed changes
Comment thread numpy/core/records.py Outdated

@mhvk mhvk Jun 18, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahaldane - In principle, at this point self.dtype is obj.dtype -- I think it would read better to just write self.dtype = obj.dtype, and expand your comment. e.g.,

    # invoke __setattr__, which for void dtypes will do
    # self.dtype = sb.dtype((record, obj.dtype))
    self.dtype = obj.dtype

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ahaldane ahaldane Jun 19, 2015

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expanded the comment, but I still have to use self.dtype since the condition is "self.dtype is not obj.dtype" ;)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ahaldane
ahaldane force-pushed the record_finalize branch from 73bb3e1 to cfd3dac Compare June 19, 2015 14:33
@ahaldane

ahaldane commented Jun 19, 2015

Copy link
Copy Markdown
Member Author

Updated with better comment + expanded tests, plus added the shape checks mentioned above back in. When adding the new tests I finally figured out why the shape manipultion was there - it raises a ValueError if the dtype change cannot apply to the mask.

Eg, when changing from 'f4,f4' to 'f8', the mask would change from [('f0', '?'), ('f1', '?')] to bool, but it is unclear how to construct the new mask values from the old mask - should you OR the old mask? Currently MA just disallows that dtype change. 'f4,f4' to 'f4' on the other hand has no such problem.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahaldane
ahaldane force-pushed the record_finalize branch from cfd3dac to f8b5308 Compare June 19, 2015 15:00
mhvk
mhvk reviewed Jun 19, 2015
View reviewed changes
Comment thread numpy/core/records.py Outdated

@mhvk mhvk Jun 19, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahaldane - it should be:

class myarray(np.ndarray):
    def __array_finalize__(self, obj):
        print(self, obj)
        print(type(self), type(obj), type(self) is type(obj))
        if obj is not None:
            print(self.dtype, obj.dtype, self.dtype is obj.dtype)

# Now the three ways we can get to __array_finalize__
myarray((4,), buffer=np.arange(4.))
# [ 0.  1.  2.  3.] None
# <class '__main__.myarray'> <class 'NoneType'>
# -->  myarray([ 0.,  1.,  2.,  3.])
mya[:2]
# [ 0.  1.] [ 0.  1.  2.  3.]
# <class '__main__.myarray'> <class '__main__.myarray'>
# float64 float64 True
# --> myarray([ 0.,  1.])
np.arange(4.).view(myarray)
# [ 0.  1.  2.  3.] [ 0.  1.  2.  3.]
# <class '__main__.myarray'> <class 'numpy.ndarray'> False
# float64 float64 True
# --> Out[15]: myarray([ 0.,  1.,  2.,  3.])

I think this is just a problem with your if statement; it should be

if obj is not None and type(self) is not type(obj):
    self.dtype = obj.dtype

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ahaldane ahaldane Jun 19, 2015

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm being too clever, but I think we want the dtype to be set to np.record in the case of an explicit constructor. I still think the clause I have is the right one.

class TestClass(np.ndarray):
    def __array_finalize__(self, obj):
        print(obj is not None and type(self) is not type(obj), 
              type(obj) is not type(self))

a = np.arange(10)

t1 = a.view(TestClass)  # view fron ndarray
t2 = TestClass(4)       # explicit constructor
t3 = t1[:2]             # slice
t4 = t1.view(TestClass) # view from same class

This prints out (left is your if, right is my if)

(True, True)
(False, True)
(False, False)
(False, False)

I think the right is what I want. Is that correct?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mhvk mhvk Jun 19, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, if you need it to be set also for explicit construction then, yes, your case is the right one. Since this was not obvious to me, and therefore may not be obvious to a future person looking at the code, maybe write the comment as:

# Both when constructing a new instance (obj=None) and when viewing
# from another class -- obj.view(type(self)) -- we invoke __setattr__,
# which for void dtypes will do self.dtype = sb.dtype((record, self.dtype)).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mhvk

mhvk commented Jun 19, 2015

Copy link
Copy Markdown
Contributor

@ahaldane - nice catch on the mask change with incompatible dtype. While oring the mask seems OK for the example you give, it does seem safest to just disallow it, at least for now!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahaldane
ahaldane force-pushed the record_finalize branch from f8b5308 to 867ed71 Compare June 19, 2015 18:58
@ahaldane

ahaldane commented Jun 19, 2015

Copy link
Copy Markdown
Member Author

@mhvk the detailed comment is a good idea. Done.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhvk

mhvk commented Jun 19, 2015

Copy link
Copy Markdown
Contributor

Great, looks all OK to me!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahaldane

ahaldane commented Jun 19, 2015

Copy link
Copy Markdown
Member Author

Don't merge, I found a problem:

>>> r = np.rec.array(np.ones(4, dtype='f4,f4'))
>>> r.view('f8').dtype
dtype('float64')

will fix soon.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahaldane

ahaldane commented Jun 19, 2015

Copy link
Copy Markdown
Member Author

Oh never mind, that is a scalar recarray. It is working as expected, I just confused myself.

But it did make me notice, perhaps a better if clause in array_finalize (as we were discussing above) is

if self.dtype.type is not record

I think that's clearer, right?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@charris

charris commented Jun 19, 2015

Copy link
Copy Markdown
Member

Agree.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahaldane
ahaldane force-pushed the record_finalize branch from 867ed71 to 698a8d2 Compare June 19, 2015 20:42
@ahaldane
BUG: automatically convert recarray dtype to np.record
a93b862
Viewing an ndarray as a np.recarray now automatically converts
the dtype to np.record.

This commit also fixes assignment to MaskedArray's dtype attribute,
fixes the repr of recarrays with non-structured dtype, and removes
recarray.view so that viewing a recarray as a non-structured dtype
no longer converts to ndarray type.

Fixes numpy#3581
@ahaldane
ahaldane force-pushed the record_finalize branch from 698a8d2 to a93b862 Compare June 19, 2015 20:43
@mhvk

mhvk commented Jun 19, 2015

Copy link
Copy Markdown
Contributor

Yes, better, now the test is much more self-explanatory.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

charris added a commit that referenced this pull request Jun 19, 2015
@charris
Merge pull request #5943 from ahaldane/record_finalize
e42bea5
BUG: automatically convert recarray dtype to np.record
@charris
charris merged commit e42bea5 into numpy:master Jun 19, 2015
@charris

charris commented Jun 19, 2015

Copy link
Copy Markdown
Member

@ahaldane @mhvk Thanks, merged. I hope it is correct ;)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahaldane

ahaldane commented Jun 19, 2015

Copy link
Copy Markdown
Member Author

Thanks, and thanks @mhvk for helpful comments & feedback.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ahaldane added a commit to ahaldane/numpy that referenced this pull request Oct 18, 2015
@ahaldane
BUG: recarrays viewed as subarrays don't convert to np.record type
3a816a4
Record array views were updated in numpy#5943 to return np.record dtype
where possible, but forgot about the case of sub-arrays.

That's fixed here, so accessing subarray fields by attribute or index
works sensibly, as well as viewing a record array as a subarray dtype,
and printing subarrays.

This also happens to fix numpy#6459, since it affects the same lines.

Fixes numpy#6497 numpy#6459
@ahaldane ahaldane mentioned this pull request Oct 18, 2015
BUG: recarrays viewed as subarrays don't convert to np.record type #6500
Merged
charris pushed a commit to charris/numpy that referenced this pull request Oct 18, 2015
@ahaldane @charris
BUG: recarrays viewed as subarrays don't convert to np.record type
f90fbb9
Record array views were updated in numpy#5943 to return np.record dtype
where possible, but forgot about the case of sub-arrays.

That's fixed here, so accessing subarray fields by attribute or index
works sensibly, as well as viewing a record array as a subarray dtype,
and printing subarrays.

This also happens to fix numpy#6459, since it affects the same lines.

Fixes numpy#6497 numpy#6459
@charris charris mentioned this pull request Oct 18, 2015
backport 6500: BUG: recarrays viewed as subarrays don't convert to np.record type #6504
Merged
jaimefrio pushed a commit to jaimefrio/numpy that referenced this pull request Mar 22, 2016
@ahaldane @jaimefrio
BUG: recarrays viewed as subarrays don't convert to np.record type
e5c4cce
Record array views were updated in numpy#5943 to return np.record dtype
where possible, but forgot about the case of sub-arrays.

That's fixed here, so accessing subarray fields by attribute or index
works sensibly, as well as viewing a record array as a subarray dtype,
and printing subarrays.

This also happens to fix numpy#6459, since it affects the same lines.

Fixes numpy#6497 numpy#6459
@eendebakpt eendebakpt mentioned this pull request May 7, 2025
DEP: Deprecate setting the strides and dtype of a numpy array #28901
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

00 - Bug component: numpy._core component: numpy.lib

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@ahaldane @charris @mhvk
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.