Skip to content

Give example for Table.read with memmap=True #8573

Closed

Description

@cdeil

In #6821 @astrofrog added the memmap=True option to Table.read for FITS files. The PR shows an example where this gives huge performance improvements in execution time and memory use.

Could someone please add an example to http://docs.astropy.org/en/stable/table/index.html where memmap=True for FITS read does give an advantage?
And if possible also add a short explanation why this works? I thought Table data is column-wise in-memory (as explained at http://docs.astropy.org/en/stable/table/implementation_details.html), but FITS BINTABLE stores row by row on disk. So I don't understand how memmap=True can work with Table at all.

@saimn - this issue / question from me is triggered by your #7404 (comment) where you say that Table.read with memmap=True does manage to load FITS without having to copy the whole table into memory.

Looking for a small standalone example I can play with with %time and %memit to understand what is going on ...

Activity

  1. saimn commented on Apr 12, 2019

    @saimn
    Contributor

    You're right about columns, so when accessing a column all the data is loaded in memory (unless I'm missing another way to do it ? maybe @astrofrog knows). But thanks to memmap you can load only a subset of the rows. I made quickly a notebook to illustrate:
    https://github.com/saimn/misc-astro/blob/master/astropy/Table_memmap.ipynb

  2. cdeil commented on Apr 12, 2019

    @cdeil
    MemberAuthor

    @saimn - Thank you!

    Now I understand, it makes sense.

    I get the same results as you in the notebook (for me on macOS) - you could add a mention to pip install ipython_memory_usage (https://github.com/ianozsvald/ipython_memory_usage) if someone else wants to try it out.

    In Gammapy we are always working with columns, so what we do now to never memmap (which came out of the discussions / advice in #7440) is the right approach (cc @adonath)

  3. saimn commented on Apr 12, 2019

    @saimn
    Contributor

    I get the same results as you in the notebook (for me on macOS) - you could add a mention to pip install ipython_memory_usage (https://github.com/ianozsvald/ipython_memory_usage) if someone else wants to try it out.

    Indeed, I added a note about this.

    In Gammapy we are always working with columns, so what we do now to never memmap (which came out of the discussions / advice in #7440) is the right approach (cc @adonath)

    Yes, but actually with Table.read it is False by default.

  4. cdeil commented on Apr 12, 2019

    @cdeil
    MemberAuthor

    Yes, but actually with Table.read it is False by default.

    Yes, but we sometimes read multiple FITS tables from a given file, and then do it roughly like this:

    with fits.open("data.fits", memmap=False) as hdulist:
        t1 = Table.read(hdulist[1])
        t2 = Table.read(hdulist[2])

    It should be a bit faster than opening the file twice, but probably we could change to this for simplicity and performance would be basically the same, if file open/close is inexpensive compared to table read.

    t1 = Table.read("data.fits", hdu=1)
    t2 = Table.read("data.fits", hdu=2)
  5. saimn commented on Apr 12, 2019

    @saimn
    Contributor

    Okay, the performance depend on your files, if headers have a lot of keywords or not. In general the first solution is faster because you don't need to parse again the primary and first extension to read the second one.

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