Skip to content

How many times does MaskedColumn need to be finalized? #6721

Closed

Description

@taldcroft

I find this perplexing. I print a print statement in BaseColumn.__array_finalize__:

In [2]: c = Column([1,2], format='%d')
Finished array finalize (with copy attrs)

In [3]: c[:]
Finished array finalize (with copy attrs)
Out[3]: 
<Column dtype='int64' format='%d' length=2>
1
2

In [4]: c = MaskedColumn([1,2], format='%d')
Finished array finalize (with copy attrs)
Finished array finalize (with copy attrs)

In [5]: c[:]
Finished array finalize (with copy attrs)
Finished array finalize (with copy attrs)
Finished array finalize (with copy attrs)
Out[5]: Finished array finalize (with copy attrs)
Finished array finalize (with copy attrs)

<MaskedColumn dtype='int64' format='%d' length=2>
1
2

This came up in working on #6720.

Activity

  1. mhvk commented on Oct 13, 2017

    @mhvk
    Contributor

    Just trying to work out what happens...

    1. Initialization (2 times): relatively easy: get one call from the creation of a BaseColumn instance (specifically, the self_data.view(cls) call), and then another in the creation of a MaskedArray instance. I think this is unavoidable in the current designs of BaseColumn and MaskedArray.
    2. Slicing (3 times): the main problem is that .data returns a masked_BaseColumn - i.e., the masked array still knows that it is holding Column data, when really this should be a plain MaskedArray. This accounts for the 2 extraneous instances.
    3. repr (2 times): again, the problem is that .data returns a masked_BaseColumn.

    I can resolve 2 & 3 by temporarily setting self._baseclass = np.ndarray in self.data -- see https://github.com/astropy/astropy/compare/master...mhvk:table-masked-column-array-finalize?expand=1 -- I also tried doing this in __new__, but that causes problems with properties not always propagating (might work with an adjusted shim, not sure).

    More generally, the problems seem to arise from both Column and MaskedArray essentially being container classes, and MaskedArray effectively insisting it is the "outer container". Possibly, things would be easier by rewriting with a different sub-class order, i.e., MaskedColumn(MaskedArray, Column). Sadly, that is a bit more involved than I have time for right now...

  2. geekypathak21 commented on May 30, 2019

    @geekypathak21
    Contributor

    @mhvk can you please elaborate a bit more about. How should I rewrite MaskedColumn()? To resolve this.

  3. mhvk commented on May 30, 2019

    @mhvk
    Contributor

    @himanshupathak21061998 - not sure what to add to my comment above. I'd just start with making the same changes as I did and then checking it indeed reduces the number of times __array_finalize__ gets called.

    Note that for this issue ypu will need to understand how ndarray subclasses and MaskedArray work; without that, it may be tricky, though numpy has decent documentation about subclasses.

  4. geekypathak21 commented on May 30, 2019

    @geekypathak21
    Contributor

    Ok, I will try to work on this. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions