Skip to content

Read ASCII tables in chunks - #6458

Merged
taldcroft merged 22 commits into
astropy:masterfrom
taldcroft:ascii-chunk
Nov 21, 2017
Merged

taldcroft merged 22 commits into
astropy:masterfrom
taldcroft:ascii-chunk

Conversation

@taldcroft

@taldcroft taldcroft commented Aug 20, 2017 •

Copy link
Copy Markdown
Member

Fixes #3334

This revives an old effort to be able to read large ASCII table files in smaller chunks. This implementation deals strictly with the fast (C/Cython-based) routines, since that would be the preferred way to read most large ASCII tables. As such it is a bit intricate. However, the good news is that all the formats supported by the fast reader, except for commented_header, are supported for chunked reading.

This implementation allows for reading a big file in chunks and then returning a single stacked table, OR getting the table chunks one at a time via a generator. The latter mode could be useful for scan-processing a large table where you don't actually need to keep everything in memory.

To do:

  • Fix a Py3 test failure that looks like a 4-byte unicode vs. bytes problem
  • Docs
  • More tests
  • Squash intermediate commits that include an earlier idea which didn't pan out
  • Change the table.vstack of the chunked tables into something more efficient. vstack has a lot of general purpose checking and machinery that isn't needed here. LEAVE this for now. Doing this in a memory efficient way is actually a bit tricky because Column cannot be resized in place.

For this PR punted on getting commented_header to work, but suggest a work-around in the docs. The way this was implemented in the fast reader is special-case from the rest that is a bit orthogonal to what I'm doing here so I am not immediately sure of a solution.

@astropy-bot

astropy-bot Bot commented Aug 20, 2017 •

Copy link
Copy Markdown

Hi there @taldcroft 👋 - thanks for the pull request! I'm just a friendly 🤖 that checks for issues related to the changelog and making sure that this pull request is milestoned and labeled correctly. This is mainly intended for the maintainers, so if you are not a maintainer you can ignore this, and a maintainer will let you know if any action is required on your part 😃.

Everything looks good from my point of view! 👍

If there are any issues with this message, please report them here.

@taldcroft

Copy link
Copy Markdown
Member Author

WIP for memory optimization, but this is not so easy so punt for now. This is for future reference.

    out = None
    for tbl in _read_in_chunks_generator(table, chunk_size, **kwargs):
        if out is None:
            out = tbl
            continue

        if out.colnames != tbl.colnames:
            raise ValueError('column name mismatch in chunked reading')

        for out_col, tbl_col in zip(out.itercols(), tbl.itercols()):
            try:
                dtype_str = common_dtype([out_col, tbl_col])
            except MergeConflictError:
                raise  # TO DO: handle this
            else:
                if dtype_str != out_col.dtype.str:
                    # Promote column and change output table reference
                    out_col = out_col.astype(dtype_str)
                    tbl[out_col.name] = out_col
                out_col.insert(len(out_col), tbl_col)

@bsipocz

bsipocz commented Aug 28, 2017

Copy link
Copy Markdown
Member

@taldcroft - No need to address Fix one range(..) to be six.moves.range(), as this will go into 3.0, won't it?

@taldcroft

Copy link
Copy Markdown
Member Author

@bsipocz - Good point, WOOHOO for Py3 only!

@pllim

pllim commented Sep 21, 2017 •

Copy link
Copy Markdown
Member

I can reproduce failures on my Windows but they are not exactly the same ones as Appveyor:
testlog.txt

There are also a couple of ResourceWarning about unclosed sockets (not captured in log) but probably unrelated?

p.s. To run the tests, I rebased your branch here on top of master locally first before install.

@taldcroft

Copy link
Copy Markdown
Member Author

@astrofrog @hamogu - this is ready for final review. The changes in ui.py (+ docs and tests) may be the place to focus.

@pllim

pllim commented Sep 28, 2017

Copy link
Copy Markdown
Member

One test still fails on Appveyor (which is an improvement over the two that I found a week ago).

@taldcroft

taldcroft commented Sep 28, 2017 •

Copy link
Copy Markdown
Member Author

@pllim - argh, it was passing appveyor before the last commit.

[EDIT] - well I thought it was, but maybe not? Sigh.

@taldcroft

Copy link
Copy Markdown
Member Author

At least I now have a functional Windows dev environment on my mac.

@pllim

pllim commented Sep 28, 2017

Copy link
Copy Markdown
Member

It passed! There is a conflict though.

Comment thread docs/io/ascii/read.rst

The ``chunk_size`` should generally be set to the largest value that is
reasonable given available system memory. There is overhead associated
with processing each chunk, so the fewer chunks the better.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll have time to look at the code next week, but a small comments on the docs:

  • Say "If you file has a commented header and you do want to read it in chunks, you might want to uncomment the header using a text editing program and then use the read-in-chunks feature on the modified table".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done (but suggest using the no_header format + names arg instead).

@astrofrog astrofrog left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me but it seems that @saimn found a real bug above.

@taldcroft

Copy link
Copy Markdown
Member Author

Ah yes, the inplace resize for non-masked columns is no good in the case of a string that changes size. This should be an easy fix.

@taldcroft
taldcroft merged commit e65baad into astropy:master Nov 21, 2017
@taldcroft
taldcroft deleted the ascii-chunk branch November 21, 2017 17:34
@bsipocz

bsipocz commented Jan 30, 2018

Copy link
Copy Markdown
Member

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants