Skip to content

Shortcut for Quantity q.to(unit).value? #6125

Closed

Description

@taldcroft

First, is there a shorter way to do q.to(unit).value where q is a Quantity.

If not, could we make one? For instance as?

q = 1 * u.m
q_km = q.as(u.km)

Activity

  1. mhvk commented on May 28, 2017

    @mhvk
    Contributor

    There is a mechanism to access values in a given unit by a corresponding attribute; this is turned off by default but used in Angle:

    q = 1.*u.m
    q._include_easy_conversion_members = True
    q.km
    # 0.001
    

    We had a (very) long discussion about this, and I don't think I'd want to include this by default (try to do tab-completion on q after doing the above example... len(dir(q)) gives 628...), since in typical usage one would (hopefully) never want to convert to straight arrays, especially once speed considerations have largely disappeared (which they will have with numpy 1.13, with long-delayed introduction of __array_ufunc__; see my PR #2583). Though one option would be to make the default configurable or allow enabling within a context.

    What might be a better option is to expose the getter for the .value property, and define it such that one can give a unit. This could then even be used within the .to method. It would be something like:

    def get_value(unit=None, equivalencies=[]):
        value = self.view(np.ndarray)
        if unit is not None:
            new_val = self.unit.to(unit, value,
                                   equivalencies=equivalencies)
        if self.shape:
            return value
        else:
            return value.item()
    
    value = property(get_value)
    
    def to(self, unit, equivalencies=[]):
        return self._new_view(self.get_value(unit, equivalencies), unit)
    

    I particularly like that this would just reorganize code, not introduce more. Would this be a solution?

    A final alternative, depending on what exactly you want this for, is the function decorator that was discussed some time ago, to help with general code that uses arrays internally, which, like quantity_input, allows you to define what units input should be in, but returns array views of quantities converted to that unit instead of just checking that the input has a consistent unit.

  2. mhvk commented on May 28, 2017

    @mhvk
    Contributor

    p.s. for the property getter solution, of course we're free to call the getter anything we want. I'd use in if it weren't a reserved keyword; perhaps to_value(unit) or value_in(unit) is clearer than get_value(unit).

  3. taldcroft commented on May 29, 2017

    @taldcroft
    MemberAuthor

    Sounds good, thanks! 👍 for value_in as that will be close in the tab-completion list.

  4. mhvk commented on May 30, 2017

    @mhvk
    Contributor

    @taldcroft - with the many replacements that suddenly became possible (see #6137), it is quite clear that this is useful to have, despite my initial doubts; a few things will even become faster! And, of course, with this the decorator should be even simpler to implement.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions