Skip to content

Use proper precision when format not specified - #11145

Merged
mhvk merged 1 commit into
astropy:masterfrom
jmmaljaars:11131-correct-print-format
Jan 6, 2021
Merged

mhvk merged 1 commit into
astropy:masterfrom
jmmaljaars:11131-correct-print-format

Conversation

@jmmaljaars

Copy link
Copy Markdown
Contributor

Description

The precision of a Quantity was not properly printed in case the precision was specified and format was empty. This should be fixed by defining format_spec a bit earlier, and using this when parsing the "unadorned" string.

Fixes #11131

@github-actions github-actions Bot added the units label Dec 7, 2020
@pllim pllim added the Bug label Dec 7, 2020
@pllim pllim added this to the v4.2.1 milestone Dec 7, 2020
@pllim
pllim requested a review from mhvk December 7, 2020 14:55
@pllim pllim modified the milestones: v4.2.1, v4.0.5 Dec 7, 2020
@pllim

pllim commented Dec 7, 2020

Copy link
Copy Markdown
Member

Thanks! Would need a test and a change log.

@mhvk or anyone who reviews, feel free to adjust milestone if I set it wrongly.

@jmmaljaars

jmmaljaars commented Dec 7, 2020 •

Copy link
Copy Markdown
Contributor Author

I reverted it to Draft, since it doesn't properly handle the case where self.value is a numpy array.

Comment thread astropy/units/quantity.py Outdated
}

# Set the precision, if set, otherwise use numpy default
pops = np.get_printoptions()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, it makes sense that this doesn't work with arrays. I fear we'll need to explicitly set one of the numpy formatting arguments (maybe just precision itself? but a different default?), since those will be used if one just expands {self.value}. I remember the machinery changed a little while ago, so probably good to start by looking what it presently allows.

@jmmaljaars jmmaljaars Dec 8, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should be fixed - while being backwards compatible - by using np.array2string in the conditional

        if format not in formats:
            raise ValueError(f"Unknown format '{format}'")
        elif format is None:
            if precision is None:
                # Use default formatting settings
                return f'{self.value}{self._unitstr:s}'
            else:
                # np.array2string properly formats arrays as well as scalars
                return f'{np.array2string(self.value, precision=precision, floatmode="fixed")}{self._unitstr:s}'

The CI/Python3.8 job is failing for an unknown reason - looks like a connection error. Locally, it works perfectly fine. In any case, I will mark this PR as ready for review

@jmmaljaars
jmmaljaars marked this pull request as ready for review December 8, 2020 13:49
@pllim

pllim commented Dec 8, 2020

Copy link
Copy Markdown
Member

Failure does look transient. I restarted the CI.

@jmmaljaars

jmmaljaars commented Dec 9, 2020 •

Copy link
Copy Markdown
Contributor Author

Does this PR need a changelog entry?

@pllim

pllim commented Dec 9, 2020

Copy link
Copy Markdown
Member

Does this PR need a changelog entry?

This PR is marked as a bug fix, so yes, please add a change log entry. Thanks!

@jmmaljaars
jmmaljaars force-pushed the 11131-correct-print-format branch from 995bd17 to 1bacfaa Compare January 4, 2021 07:43
@jmmaljaars

Copy link
Copy Markdown
Contributor Author

Done! (sorry that it took a while...)

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, thanks so much! Two comments left, both nitpicky...

Comment thread astropy/units/quantity.py Outdated
return f'{self.value}{self._unitstr:s}'
else:
# np.array2string properly formats arrays as well as scalars
return f'{np.array2string(self.value, precision=precision, floatmode="fixed")}{self._unitstr:s}'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Total nitpick, but to me this reads clearer as

return np.array2string(self.value, precision=precision, floatmode="fixed") + self._unitstr

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, should be fixed in acb7cc3

Comment thread CHANGES.rst Outdated
astropy.units
^^^^^^^^^^^^^

- Use proper precision when printing a ``Quantity`` in case precision format not

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe "when creating a string from a Quantity"? It affects to_string() and __str__.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should be fixed (and migrated to milestone 4.0.5) in acb7cc3

@mhvk

mhvk commented Jan 4, 2021

Copy link
Copy Markdown
Contributor

p.s. Need to move the changelog entry to 4.0.5 to match the milestone...

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, looks good now, but I did manage to find one more nitpick. Sorry! When you change that, could you also squash the commits? (If not sure how, see https://docs.astropy.org/en/latest/development/workflow/development_workflow.html#how-to-squash -- am also happy to do it for you).

Comment thread CHANGES.rst Outdated
- Ensure ``keepdims`` works for taking ``mean``, ``std``, and ``var`` of
``Quantity``. [#11198]

- Use proper precision when creating a string from a ``Quantity`` in case precision format is not

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry to have another nitpick, but I think this suggestion of mine doesn't actually convey to a reader what changed. Maybe

  - For ``Quantity.to_string()``, ensure that the precision argument is also
    used when the format is not latex. [#11145]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 179bf32 , also squashed this bug fix into one commit.

@jmmaljaars
jmmaljaars force-pushed the 11131-correct-print-format branch from acb7cc3 to 179bf32 Compare January 6, 2021 06:22

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! Merging...

@mhvk
mhvk merged commit 98e5213 into astropy:master Jan 6, 2021
astrofrog pushed a commit that referenced this pull request Mar 16, 2021
Use proper precision when format not specified
pllim pushed a commit to pllim/astropy that referenced this pull request Mar 24, 2021
…format

Use proper precision when format not specified
pllim added a commit to pllim/astropy that referenced this pull request Mar 24, 2021
eteq added a commit that referenced this pull request Mar 25, 2021
Manual backport of #11145 (Use proper precision when format not specified)
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.

quantity.to_string(precision=<int>) does not produce a string of the quantity with <int> decimals of precision

3 participants