Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix format conversion in the fast writer with plain format strings ('.2f') #4517

Merged
merged 3 commits into from
Mar 1, 2016

Conversation

saimn
Copy link
Contributor

@saimn saimn commented Jan 21, 2016

Plain format strings ('.2f') were not supported by the fast writer, whereas it is by the basic writer: http://docs.astropy.org/en/latest/table/construct_table.html#format-specifier

With the fast writer I get this error:

astropy/io/ascii/cparser.pyx in astropy.io.ascii.cparser.FastWriter.write (astropy/io/ascii/cparser.c:18549)()

astropy/io/ascii/cparser.pyx in astropy.io.ascii.cparser.auto_format_func (astropy/io/ascii/cparser.c:20888)()

ValueError: Unable to parse format string .1f

The basic writer use astropy.table.pprint._auto_format_func to try the different formatting syntaxes. And the fast writer was reimplementing this in cparser.pyx (auto_format_func). The comment on these function is not so clear about why the function is duplicated (_Mimics pprint.auto_format_func for non-numpy values.), and the function does less than astropy.table.pprint._auto_format_func so this commit just use this function. But maybe there is a good reason for this duplication ?

cc @taldcroft

@saimn
Copy link
Contributor Author

saimn commented Jan 21, 2016

Hum, the documentation build failed, it seems to be due to ../CHANGES.rst:350: WARNING: py:obj reference target not found: fits2bitmap.

@bsipocz
Copy link
Member

bsipocz commented Jan 21, 2016

Thanks @saimn. #4518 should fix it.

@saimn
Copy link
Contributor Author

saimn commented Feb 8, 2016

Taking another look at this, auto_format_func was introduced in d4cbc70 (cc @mdmueller )

The fact that it is defined in cparser.pyx may provide some speedup (?), but duplicating the code is problematic because it may have a different behavior as pprint._auto_format_func (which is the case in this issue). So, if speed matters here, another option could be to replace pprint._auto_format_func with auto_format_func from cparser.pyx (with a fix for plain format strings) ?

@saimn
Copy link
Contributor Author

saimn commented Feb 17, 2016

ping @taldcroft ?

@taldcroft
Copy link
Member

@saimn - sorry for the delay, I'll try to have a look soon.

@taldcroft
Copy link
Member

@saimn - I believe that speed was a driver in writing the duplicate code, although just offhand it doesn't seem like much of that code would really Cythonize effectively so I'm not sure.

If you look at http://astropy.readthedocs.org/en/latest/io/ascii/fast_ascii_io.html#writing and the referenced notebook, you can see some performance testing that was done. You could try running this notebook using current master and then with your branch to see what changes (if anything) in the plots.

@saimn
Copy link
Contributor Author

saimn commented Feb 24, 2016

@taldcroft : results are here https://gist.github.com/saimn/f143251187f7e95fe57f
I don't really see a difference. At the end of each notebook I added a basic timeit for each function, the python looks actually a bit faster but this should negligible when writing a real table. And the format functions are stored in a cache in pprint._format_funcs so auto_format_func is not called so often.

@taldcroft taldcroft added this to the v1.0.9 milestone Feb 24, 2016
@taldcroft taldcroft self-assigned this Feb 24, 2016
@taldcroft
Copy link
Member

@saimn - awesome! I love patches that remove unnecessary code and improve things along the way!

All this needs now is a bug fix changelog entry for 1.0.9. (If you could try cherry picking this PR onto the 1.0.x branch to make sure there is no conflict that would be good).

Plain format strings ('.2f') were not supported by the fast writer, whereas it
is by the basic writer:
http://docs.astropy.org/en/latest/table/construct_table.html#format-specifier

The basic writer use `astropy.table.pprint._auto_format_func` to try the
different formatting syntaxes. And the fast writer was reimplementing this in
`cparser.pyx` (`auto_format_func`). The comment on these function is not so
clear about why the function is duplicated (*Mimics pprint._auto_format_func for
non-numpy values.*), and the function does less than
`astropy.table.pprint._auto_format_func` so this commit just use this function.
@saimn
Copy link
Contributor Author

saimn commented Feb 24, 2016

@taldcroft : done, and rebased on master to fix changelog conflicts. It also applies cleanly on 1.0.x and tests pass.

@saimn
Copy link
Contributor Author

saimn commented Feb 25, 2016

Nothing bad, the coverage build failed because of some error with coveralls.io:

Submitting coverage to coveralls.io...
Coverage submitted! 
Failure to submit data. Response [524]: <!DOCTYPE html> ...

taldcroft added a commit that referenced this pull request Mar 1, 2016
Fix format conversion in the fast writer with plain format strings ('.2f')
@taldcroft taldcroft merged commit 107e02a into astropy:master Mar 1, 2016
@taldcroft
Copy link
Member

Thanks @saimn !

eteq pushed a commit that referenced this pull request Mar 1, 2016
Fix format conversion in the fast writer with plain format strings ('.2f')
eteq pushed a commit to eteq/astropy that referenced this pull request Mar 1, 2016
Fix format conversion in the fast writer with plain format strings ('.2f')
@eteq
Copy link
Member

eteq commented Mar 2, 2016

@saimn @taldcroft - drat. It looks like this fix doesn't work in py 2.6, revealed when I packported this to 1.0.x and 1.1.x: https://travis-ci.org/astropy/astropy/jobs/112989010 and https://travis-ci.org/astropy/astropy/jobs/112988557

Any thoughts on how to fix this? Or should we instead re-assign this to v1.2 and just accept it in the 1.1.x and 1.0.x branches?

@taldcroft
Copy link
Member

@eteq - I would guess that if you change {:0.1f} to {0:0.1f} in the test then it will pass in Py2.6.

@saimn saimn deleted the fast_reader_fmt branch March 2, 2016 08:17
@saimn
Copy link
Contributor Author

saimn commented Mar 2, 2016

@eteq - Sorry about that, I forgot about the need for py2.6 compatibility for old versions. @taldcroft is right, replacing the format string with {0:0.1f} should fix it. I can make a PR for this later today if it helps.

saimn added a commit to saimn/astropy that referenced this pull request Mar 2, 2016
astrofrog added a commit that referenced this pull request Mar 2, 2016
Fix Python 2.6 compatibility for #4517
eteq pushed a commit that referenced this pull request Mar 3, 2016
Fix Python 2.6 compatibility for #4517
eteq pushed a commit that referenced this pull request Mar 3, 2016
Fix Python 2.6 compatibility for #4517
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.

5 participants