Skip to content

Parse plural forms of units from strings #1520

Closed

Description

@astrofrog

I know that we decided to always use singular units in Astropy, but I'm wondering whether we should allow the unit parser to be a little more relaxed - for instance I am reading some files that have units in microns and this is causing the parser to fail:

In [5]: Unit('microns')
ERROR: ValueError: 'microns' did not parse as unit format 'generic': At col 0, 'microns' is not a valid unit in string 'microns' [astropy.units.core]

Do we want to consider relaxing the parser (maybe optionally) for such plural forms?

Activity

  1. ghost assigned on Oct 3, 2013
  2. astrofrog commented on Oct 3, 2013

    @astrofrog
    MemberAuthor

    Similarly, do we want to allow capitalized forms if they are unambiguous?

    In [6]: Unit('HZ')
    ERROR: ValueError: 'HZ' did not parse as unit format 'generic': At col 0, 'HZ' is not a valid unit in string 'HZ' [astropy.units.core]
    
  3. astrofrog commented on Oct 3, 2013

    @astrofrog
    MemberAuthor

    Another example of unit read in from an existing file

    ValueError: 'ergs/cm^2/s' did not parse as unit format 'generic': At col 0, 'ergs' is not a valid unit in string 'ergs/cm^2/s'
    

    By the way, I'm not saying that we should support all of these, but just raising the question as to whether we should have a non-pedantic mode that recognizes e.g. plural forms and doesn't care about capitalization?

  4. mdboom commented on Oct 3, 2013

    @mdboom
    Contributor

    I'm really wary of deviating from the unit standards, of which there are already too many, and I don't want to create another one. Interoperability is really important -- it may sound pedantic and rigid, but any time you extend a format to include extra things, people will be surprised when it doesn't interoperate with other tools. I think the only reason I would do this is if a large, important database provided data in this way and they were unwilling to change. Otherwise, I think we need to stand our ground.

    None of the existing unit formats take pluralized names, and all are case sensitive.

  5. mhvk commented on Oct 3, 2013

    @mhvk
    Contributor

    I can see a bit of a case for the full names of units ("microns", "meters", "Newtons"), but not for the abbreviations that are defined for it (i.e., no "ergs", "dynes", "bas" [but maybe "barns"]). I agree with @mdboom with being wary of accepting anything like this by default, but perhaps a format='lax' option would be a useful addition. Overall, still -0.

  6. mdboom commented on Oct 8, 2013

    @mdboom
    Contributor

    I'd like to push this off to 0.4. Agreed?

  7. astrofrog commented on Oct 8, 2013

    @astrofrog
    MemberAuthor

    @mdboom - definitely

  8. embray commented on Oct 11, 2013

    @embray
    Member

    I could see a case for "strict=False" mode for parsing units, either implemented as a module level configuration variable or some option on the Unit parser. Obviously "strict=True" should be the default.

  9. mdboom commented on Dec 5, 2013

    @mdboom
    Contributor

    I'm going back through some old issues here. I'm still really against making this change, however, what about making something like this work...?

    In [5]: Unit('microns')
    ERROR: ValueError: 'microns' did not parse as a unit.  Did you mean 'micron'?
    
  10. pllim commented on Dec 5, 2013

    @pllim
    Member

    Just for the record, STScI has a bunch of spectra files with unit set to angstroms. I worked around it by having my own little function to recognize that string and set the unit to u.AA. I do agree with @mdboom's solution, because if people want to use non-standard unit string, they should handle it appropriately themselves.

  11. embray commented on Dec 5, 2013

    @embray
    Member

    I like this solution.

  12. astrofrog commented on Dec 5, 2013

    @astrofrog
    MemberAuthor

    +1 to suggestions

  13. mdboom commented on Dec 30, 2013

    @mdboom
    Contributor

    Closed by #1870.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions