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

Improved error message for scale units in FITS. #2023

Merged
merged 1 commit into from
Feb 12, 2014
Merged

Improved error message for scale units in FITS. #2023

merged 1 commit into from
Feb 12, 2014

Conversation

krngrvr09
Copy link

Fix for issue #1979.

Improved the error message and added a test case.
Please review the changes.

@embray embray mentioned this pull request Jan 30, 2014
table=Table.read(infile, format='ipac')
table.write('table.fits',format='fits', overwrite=True)
out = table.vstack([t1, t2, t4], join_type='outer')
assert exc.__class__.__name__ == 'UnitScaleError'
Copy link
Member

Choose a reason for hiding this comment

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

The indentation is inconsistent here.

@astrofrog
Copy link
Member

@krngrvr09 - it looks like the tests are failing. You can run the test suite locally bby doing:

python setup.py test

do you get it to pass on your computer?

"The column ' {0} ' could not be stored in FITS format "
"because it has a scale ' ({1}) ' that "
"is not recognized by the FITS standard. Either scale "
"the data or change the units.".format(col.name, str(scale)))
Copy link
Contributor

Choose a reason for hiding this comment

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

This message looks great. Only one minor nitpick: I don't think we need the extra spaces inside the single quotes, i.e.: '{0}' is fine, and I don't think we need the extra single quotes around {1}, i.e.: ({1}) is fine.

Copy link
Member

Choose a reason for hiding this comment

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

I usually don't make the quotes part of the format string at all and just use {0!r}; then Python will use the appropriate quotes around the string.

Copy link
Contributor

Choose a reason for hiding this comment

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

The problem with {0!r} is that you can get u prefixes on Python 2. Not terrible, but a little user unfriendly.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, true. That's why I also sometimes do repr(foo).lstrip('bu') but that's probably almost uglier :) It's better practice in general in case the string you're inserting contains quotes itself but that isn't really an issue here I guess.

@krngrvr09
Copy link
Author

@astrofrog Yes, the tests were failing on my system too. I have fixed them.
@mdboom I agree, the if-statement, is necessary. I have added that too.

Hopefully everything seems good now.
If there are more changes to be made, let me know. Thank you.

try:
col.unit = col_unit.to_string(format='fits')
except UnitScaleError:
raise UnitScaleError(
Copy link
Member

Choose a reason for hiding this comment

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

Just a couple of minor esthetic changes. You only use col_unit once, so I think you could just leave:

col.unit = input[col.name].unit.to_string(format='fits')

since you don't gain anything from defining the intermediate variable col_unit.

For scale, it does make sense to define it because it does help the readability in the exception, but you can move the

scale = ...

line inside the except clause, because it's only needed then. That is:

except:
    scale = ...
    raise UnitScaleError(...)

Copy link
Member

Choose a reason for hiding this comment

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

That had actually been my suggestion in the first place; earlier the exception message I suggested used the unit name in the message, so it was used twice. But now I agree it's not needed more than once.

@astrofrog
Copy link
Member

@krngrvr09 - this is almost ready, I left a few comments above.

@krngrvr09
Copy link
Author

@astrofrog I have deleted the line out=table.vstack.... And also made the required changes in test_connect.py and made the changes in connect.py.

c = ['x', 'y', 'z']
t = Table([a, b, c], names=('a', 'b', 'c'), meta={'name': 'first table'})
t['a'].unit='percent'
t.format='ipac'
Copy link
Member

Choose a reason for hiding this comment

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

Just saw this line, which is not needed (t.format='ipac') - can you remove it?

@krngrvr09
Copy link
Author

@astrofrog Done.

@astrofrog
Copy link
Member

@krngrvr09 - this is looking good. It looks like there is a conflict in the changes compared to the latest version of Astropy, so you will need to rebase on the latest master of the main repository. Have you done this before? There are instructions provided here:

http://docs.astropy.org/en/stable/development/workflow/development_workflow.html#rebase-on-trunk

but if you have any issues it might be easier to chat about it live on IRC. I am not available right now, but you could go on during the week and ask if you have any issues. In any case, if you do mess up, then don't open a new pull request, because there is always a way we can recover.

-------
is_fits : bool
Returns `True` if the given file is a FITS file.
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this docstring has lost its indentation here. Each of these lines should have 4 spaces at the beginning.

@krngrvr09
Copy link
Author

@keflavich I have made the required changes. I hope, everything is now, Ok. But still if there are any other changes, to be made. Please suggest.

@astrofrog I have been able to do the rebase. Apologies for the noise commits. Is there anything else, I have to do?

@taldcroft
Copy link
Member

@krngrvr09 - please look at the file diffs and eliminate any diffs that are not specifically related to the patch you are applying. There are still a number of diffs that are just noise and which incorrectly change the formatting.

At this point the number of commits has gone over 50 during your learning process, so I think it's time for you to start over from scratch from the current master and apply your patch cleanly in a few commits. It's not too hard when you look at the file diffs.

@taldcroft
Copy link
Member

@krngrvr09 - by "start over" I mean:

git branch -m fits fits-backup
git checkout master
git pull upstream master # where upstream is the main astropy/astropy repo
git checkout -b fits

Now based on the file diffs you currently see here, edit the appropriate files to replicate those diffs. Run tests locally to make sure everything works. Then:

git diff master

Make sure that the only changes are ones related to your fix, and make sure that all the diffs you see in this branch currently on github are in your new patch. No extra whitespace or formatting changes.

Finally, commit your changes and do a force push to github:

git commit -am "Improve error message for scale units in FITS"
git push -f origin fits

BTW, you could also squash all the 50 commits into 1. You can google on "git squash" to find some tutorials on how to do this. This will also require a force push to github.

@krngrvr09
Copy link
Author

@taldcroft Thank you very much for your help. Honestly, I think, what we just did was very cool!

@mdboom
Copy link
Contributor

mdboom commented Feb 12, 2014

Look good to me!

taldcroft added a commit that referenced this pull request Feb 12, 2014
Improved error message for scale units in FITS.
@taldcroft taldcroft merged commit ba9b2a4 into astropy:master Feb 12, 2014
@taldcroft
Copy link
Member

@krngrvr09 - merged, thanks!

@embray embray added this to the v0.3.1 milestone Feb 12, 2014
@embray embray added the units label Feb 12, 2014
@embray embray added Bug and removed Bug labels Feb 12, 2014
@krngrvr09
Copy link
Author

Thanks to the AstroPy community. I learned a lot!

taldcroft added a commit that referenced this pull request Feb 25, 2014
Improved error message for scale units in FITS.
Conflicts:

	astropy/io/fits/connect.py
	astropy/io/fits/tests/test_connect.py
@krngrvr09 krngrvr09 deleted the fits branch March 12, 2014 20:49
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.

None yet

6 participants