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

ds9 reader corrections/improvements #90

Merged
merged 13 commits into from
Dec 10, 2016

Conversation

keflavich
Copy link
Contributor

Mostly minor:

  • warn if a region is skipped when reading
  • read 'box' regions (we accepted only 'rectangle' before)
  • allow for whitespace after commas

is what ds9 actually uses for rectangles), adding a warning if the
region isn't recognized, and allowing for whitespace (which ds9 can
handle)
@cdeil cdeil added this to the 0.2 milestone Nov 30, 2016
@cdeil
Copy link
Member

cdeil commented Nov 30, 2016

@keflavich - Thanks!

Long-term I think an option to "ignore", "warn" or "error" on this would be best, like what the Python unicode encode / decode functions do.

For now: can you please add one test for one case where the warning is raised, asserting that it works?

@keflavich
Copy link
Contributor Author

maybe kwarg should be skipped={'warn','ignore','error'}? What keyword do you prefer?

@cdeil
Copy link
Member

cdeil commented Dec 1, 2016

maybe kwarg should be skipped={'warn','ignore','error'}? What keyword do you prefer?

I like errors for the name of the parse function argument, meaning "how to deal with parsing errors"

That's what Python uses for str.encode and bytes.decode:

In [6]: str.encode?
Docstring:
S.encode(encoding='utf-8', errors='strict') -> bytes

Encode S using the codec registered for encoding. Default encoding
is 'utf-8'. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
'xmlcharrefreplace' as well as any other name registered with
codecs.register_error that can handle UnicodeEncodeErrors.
Type:      method_descriptor

and

In [8]: bytes.decode?
Signature: bytes.decode(self, /, encoding='utf-8', errors='strict')
Docstring:
Decode the bytes using the codec registered for encoding.

encoding
  The encoding with which to decode the bytes.
errors
  The error handling scheme to use for the handling of decoding errors.
  The default is 'strict' meaning that decoding errors raise a
  UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
  as well as any other name registered with codecs.register_error that
  can handle UnicodeDecodeErrors.
Type:      method_descriptor

@cdeil cdeil self-assigned this Dec 1, 2016
Copy link
Member

@cdeil cdeil left a comment

Choose a reason for hiding this comment

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

Is a AstropyUserWarning appropriate for parsing errors? Shouldn't it be a more specific DS9RegionParsingError to make it more obvious and easier to handle in other libraries?

@@ -192,20 +194,33 @@ def ds9_region_list_to_objects(region_list):
else:
raise ValueError("No central coordinate")
else:
# Note: this should effectively never happen, because it would
Copy link
Member

Choose a reason for hiding this comment

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

I think this will happen all the time. People have invalid ds9 files and region_type. They mistype or whatever.

Can you change this code comment from "this should effectively never happen, ..." to something like "For invalid inputs, we do ..."

Is it possible to give the user a line number where the problem in their file occurs?
It will be really common and users will appreciate a helpful message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this error actually can't occur by user error. If a user mistypes / enters an incorrect region type, the previous function, ds9_string_to_region_list, should fail. Only regions that are parsed by ds9_string_to_region_list but do not have a corresponding entry within ds9_region_list_to_objects should reach this point, and that should only happen if someone updating this code forgets to add a new entry in this function.

"""Parse ds9 region string to region objects

Parameters
----------
region_string : str
DS9 region string
warn_skipped : bool
Print a warning if there is a skipped (commented) line?
Can set to ``False`` or ``'raise'`` if you want an exception instead.
Copy link
Member

Choose a reason for hiding this comment

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

Like I said in the other comment, I'd prefer to call it "error".
And as-is, the option is documented as bool, but you also accept a string raise.
That's no good.
Let's make it a multi-choice string option.

@@ -357,6 +385,19 @@ def line_parser(line, coordsys=None):
language_spec[region_type] = itertools.chain((coordinate, coordinate), itertools.cycle((radius,)))

return region_type, parsed_return, parsed_meta, composite, include
else:
# it is not clear when it is OK to warn
Copy link
Member

Choose a reason for hiding this comment

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

Can we resolve / improve the discussion on when to warn in this PR, and then change the comment?
It currently reads as an open issue "it is not clear ..."

Maybe just put whatever behaviour you like best, and establish it via tests?

@keflavich
Copy link
Contributor Author

fyi, I'm pushing failing builds because I can't run tests locally; they all raise:
E OSError: Could not find lib c or load any of its variants [].

@cdeil
Copy link
Member

cdeil commented Dec 5, 2016

E OSError: Could not find lib c or load any of its variants [].

If you can't figure it out, maybe try git clean -fdx and python setup.py test in master and paste the full output in a gist, and I'll try to help by having a look at the log.

@keflavich
Copy link
Contributor Author

I can't figure it out, the problem persists, but I can run the tests directly now with

py.test regions/io/tests/test_ds9_language.py

@keflavich
Copy link
Contributor Author

@cdeil I think this is ready for final review now

@cdeil
Copy link
Member

cdeil commented Dec 9, 2016

@astrofrog or @joleroi - Do you have time for a quick review here?

@cdeil
Copy link
Member

cdeil commented Dec 9, 2016

I think it's good that you introduce a specific DS9RegionParserWarning.
But one motivation for custom warning / error is that people can catch it, right?
So it should be added to __all__ and show up in the docs and be part of our public API, no?

For equivalent errors, should we stick with ValueError as you do here, or introduce DS9RegionParserError, to be equivalent with the warning?

from ..write_ds9 import write_ds9, ds9_objects_to_string

_ASTROPY_MINVERSION = v.LooseVersion('1.1')
_ASTROPY_VERSION = v.LooseVersion(astrov.version)
_ASTROPY_MINVERSION = vers.LooseVersion('1.1')
Copy link
Member

Choose a reason for hiding this comment

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

We are working towards putting this as astropy.regions in the core package.
So there's no need to support old Astropy versions.

At the moment regions has to work with Astropy 1.2, and we can switch to 1.3 as soon as that is out.

So remove this xfail for Astropy < 1.1 here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see any reason to remove the xfail now, as long as regions is still an independent package. Sure, when we merge it into astropy-core, then it makes sense... but that's not now.

@keflavich
Copy link
Contributor Author

I like the idea of adding a DS9RegionParserError as a subclass of ValueError.... I'll do it if I have time.

@cdeil
Copy link
Member

cdeil commented Dec 10, 2016

I'm merging this now.

Thanks, @keflavich !

@cdeil cdeil merged commit 81d6439 into astropy:master Dec 10, 2016
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.

2 participants