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

Quantities with structured units #11775

Merged
merged 57 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
1101e28
Basic start of a StructuredUnit.
mhvk May 28, 2018
13ee02b
Basic start of StructuredQuantity.
mhvk May 28, 2018
766155c
Allow initialization with unit tuples.
mhvk May 29, 2018
bf5a03d
Implement quantity unit conversion.
mhvk May 29, 2018
b065087
More setup, combining some in a test base class.
mhvk May 31, 2018
86d3ee2
Move actual conversion to unit; add value and to_value.
mhvk May 31, 2018
17edc08
Remove pointless isinstance check.
mhvk May 31, 2018
55bb52a
Add docstrings.
mhvk Jun 8, 2018
b2175b4
Add si, cgs, decompose, is_equivalent.
mhvk Jun 8, 2018
78e6c08
Be a bit more careful with dtype and the recursive replacement.
mhvk Jun 9, 2018
cb6476f
Rework so that StructuredUnit parts can be StructuredUnit.
mhvk Jun 10, 2018
f867750
Allow list of tuples for conversion.
mhvk Jun 11, 2018
f47728b
Pre-construct converters properly.
mhvk Jun 11, 2018
57fd702
Write parser, introducing COMMA to signify structured units.
mhvk Jun 11, 2018
e458f55
New parser/lexer that allows Structured Units
mhvk Jan 10, 2021
c5a3d0a
Define to_string, __str__ and __repr__; fix Quantity in getitem.
mhvk Jul 14, 2018
3994bd4
Allow initialization with full-string units.
mhvk Jul 15, 2018
9addda8
Introduce StructuredUnit multiplication and division.
mhvk Jul 15, 2018
674e100
Fail on inconsistent units.
mhvk Jul 16, 2018
7715577
Stricter checking of inputs for StructuredUnit.
mhvk Jul 16, 2018
fd71c72
Add a bit more documentation and commentary.
mhvk Jul 16, 2018
ef94e71
define (in)equality
mhvk Aug 21, 2018
1d377a5
Define __setitem__.
mhvk Aug 21, 2018
1b7aaba
Small corrections, some additional tests.
mhvk Jan 10, 2021
6c1f9cf
Add test for conversion to own unit.
mhvk Jan 10, 2021
41c4d18
Implement unit "shift" operations
mhvk Apr 3, 2021
6ad9b2b
Support empty_like, zeros_like, and ones_like.
mhvk Apr 3, 2021
13ecc8b
Support all erfa functions dealing with pv vectors.
mhvk Apr 3, 2021
7f43689
Ensure names are ignored in equivalency tests
mhvk May 10, 2021
49887af
Add support for ldn, though it needs erfa bug fix
mhvk May 10, 2021
91a502f
Add .si and .cgs properties to StructuredQuantity
mhvk May 10, 2021
e155cec
Make StructuredUnit its own class, rather than a subclass of np.void.
mhvk May 10, 2021
bcbda52
Add support for astrom functions (without time input)
mhvk May 11, 2021
6e90646
Add tests with function units (magnitude)
mhvk May 15, 2021
420826d
Move __eq__, __ne__ to Quantity, as it is better.
mhvk May 14, 2021
aa0013d
Remove need of ilshift by generalizing Quantity.__ilshift__
mhvk May 15, 2021
45111f1
Use more of base Quantity.to,to_value and their docstrings
mhvk May 16, 2021
afd2052
Move all StructuredQuantity functionality in Quantity itself.
mhvk May 22, 2021
5c5f3ac
Remove _create_array in favour of extending _recursively_get_dtype.
mhvk May 22, 2021
0b4fa7d
Simplify __getitem__ and __setitem__, using more common code.
mhvk May 22, 2021
b474796
Make StructuredUnit also indexable by number.
mhvk May 22, 2021
2e4a0d9
Add basic documentation.
mhvk May 22, 2021
08793a6
In erfa, can now import StructuredUnit on top.
mhvk May 23, 2021
16664df
Correct implementation of _normalize_tuples
mhvk May 23, 2021
1782e64
Tests with structured MaskedQuantity.
mhvk May 23, 2021
086a941
Fix format string for oldest python.
mhvk May 23, 2021
73bfbe7
Changelog entry.
mhvk May 23, 2021
aaf9f5e
Rename .names attribute to .field_names.
mhvk May 23, 2021
9f0decd
Allow passing in StructuredUnit for names.
mhvk May 23, 2021
40fb562
Adjust stringency of some ERFA test to pass on 32 bit
mhvk May 23, 2021
1fe39df
Added comments and adjusted doc page a little following comments.
mhvk May 23, 2021
1e90a2b
Ensure structured SpecificTypeQuantity is possible.
mhvk May 24, 2021
b1a151a
Fix _repr_latex_
mhvk May 25, 2021
cd30915
Make .to(other) return the result for a value with all ones.
mhvk May 25, 2021
ad28c92
Ensure tests pass on pyerfa-dev
mhvk May 29, 2021
5055b82
Clarify various comments following review. [skip ci]
mhvk Jun 6, 2021
a8ef5af
Extend documentation following review.
mhvk Jul 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions astropy/units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .function.core import *
from .function.logarithmic import *

from .structured import *
from .decorators import *

del bases
Expand Down
4 changes: 4 additions & 0 deletions astropy/units/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,10 @@ def __call__(self, s="", represents=None, format=None, namespace=None,
elif isinstance(s, (int, float, np.floating, np.integer)):
return CompositeUnit(s, [], [], _error_check=False)

elif isinstance(s, tuple):
from .structured import StructuredUnit
return StructuredUnit(s)

elif s is None:
raise TypeError("None is not a valid Unit")

Expand Down
57 changes: 56 additions & 1 deletion astropy/units/format/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Generic(Base):
_show_scale = True

_tokens = (
'COMMA',
'DOUBLE_STAR',
'STAR',
'PERIOD',
Expand Down Expand Up @@ -107,6 +108,7 @@ def _lexer(cls):
def _make_lexer(cls):
tokens = cls._tokens

t_COMMA = r'\,'
t_STAR = r'\*'
t_PERIOD = r'\.'
t_SOLIDUS = r'/'
Expand Down Expand Up @@ -178,7 +180,60 @@ def _make_parser(cls):

def p_main(p):
'''
main : product_of_units
main : unit
| structured_unit
| structured_subunit
'''
if isinstance(p[1], tuple):
# Unpack possible StructuredUnit inside a tuple, ie.,
# ignore any set of very outer parentheses.
p[0] = p[1][0]
else:
p[0] = p[1]

def p_structured_subunit(p):
'''
structured_subunit : OPEN_PAREN structured_unit CLOSE_PAREN
'''
# We hide a structured unit enclosed by parentheses inside
# a tuple, so that we can easily distinguish units like
# "(au, au/day), yr" from "au, au/day, yr".
p[0] = (p[2],)

def p_structured_unit(p):
'''
structured_unit : subunit COMMA
| subunit COMMA subunit
'''
from ..structured import StructuredUnit
inputs = (p[1],) if len(p) == 3 else (p[1], p[3])
mhvk marked this conversation as resolved.
Show resolved Hide resolved
units = ()
for subunit in inputs:
if isinstance(subunit, tuple):
# Structured unit that should be its own entry in the
# new StructuredUnit (was enclosed in parentheses).
units += subunit
elif isinstance(subunit, StructuredUnit):
# Structured unit whose entries should be
# individiually added to the new StructuredUnit.
units += subunit.values()
else:
# Regular unit to be added to the StructuredUnit.
units += (subunit,)

p[0] = StructuredUnit(units)

def p_subunit(p):
'''
subunit : unit
| structured_unit
| structured_subunit
'''
p[0] = p[1]

def p_unit(p):
'''
unit : product_of_units
| factor product_of_units
| factor product product_of_units
| division_product_of_units
Expand Down
4 changes: 2 additions & 2 deletions astropy/units/format/generic_lextab.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

# generic_lextab.py. This file automatically created by PLY (version 3.11). Don't edit!
_tabversion = '3.10'
_lextokens = set(('CARET', 'CLOSE_PAREN', 'DOUBLE_STAR', 'FUNCNAME', 'OPEN_PAREN', 'PERIOD', 'SIGN', 'SOLIDUS', 'STAR', 'UFLOAT', 'UINT', 'UNIT'))
_lextokens = set(('CARET', 'CLOSE_PAREN', 'COMMA', 'DOUBLE_STAR', 'FUNCNAME', 'OPEN_PAREN', 'PERIOD', 'SIGN', 'SOLIDUS', 'STAR', 'UFLOAT', 'UINT', 'UNIT'))
_lexreflags = 32
_lexliterals = ''
_lexstateinfo = {'INITIAL': 'inclusive'}
_lexstatere = {'INITIAL': [("(?P<t_UFLOAT>((\\d+\\.?\\d*)|(\\.\\d+))([eE][+-]?\\d+)?)|(?P<t_UINT>\\d+)|(?P<t_SIGN>[+-](?=\\d))|(?P<t_FUNCNAME>((sqrt)|(ln)|(exp)|(log)|(mag)|(dB)|(dex))(?=\\ *\\())|(?P<t_UNIT>%|([YZEPTGMkhdcmuµnpfazy]?'((?!\\d)\\w)+')|((?!\\d)\\w)+)|(?P<t_DOUBLE_STAR>\\*\\*)|(?P<t_STAR>\\*)|(?P<t_PERIOD>\\.)|(?P<t_CARET>\\^)|(?P<t_OPEN_PAREN>\\()|(?P<t_CLOSE_PAREN>\\))|(?P<t_SOLIDUS>/)", [None, ('t_UFLOAT', 'UFLOAT'), None, None, None, None, ('t_UINT', 'UINT'), ('t_SIGN', 'SIGN'), ('t_FUNCNAME', 'FUNCNAME'), None, None, None, None, None, None, None, None, ('t_UNIT', 'UNIT'), None, None, None, (None, 'DOUBLE_STAR'), (None, 'STAR'), (None, 'PERIOD'), (None, 'CARET'), (None, 'OPEN_PAREN'), (None, 'CLOSE_PAREN'), (None, 'SOLIDUS')])]}
_lexstatere = {'INITIAL': [("(?P<t_UFLOAT>((\\d+\\.?\\d*)|(\\.\\d+))([eE][+-]?\\d+)?)|(?P<t_UINT>\\d+)|(?P<t_SIGN>[+-](?=\\d))|(?P<t_FUNCNAME>((sqrt)|(ln)|(exp)|(log)|(mag)|(dB)|(dex))(?=\\ *\\())|(?P<t_UNIT>%|([YZEPTGMkhdcmuµnpfazy]?'((?!\\d)\\w)+')|((?!\\d)\\w)+)|(?P<t_DOUBLE_STAR>\\*\\*)|(?P<t_COMMA>\\,)|(?P<t_STAR>\\*)|(?P<t_PERIOD>\\.)|(?P<t_CARET>\\^)|(?P<t_OPEN_PAREN>\\()|(?P<t_CLOSE_PAREN>\\))|(?P<t_SOLIDUS>/)", [None, ('t_UFLOAT', 'UFLOAT'), None, None, None, None, ('t_UINT', 'UINT'), ('t_SIGN', 'SIGN'), ('t_FUNCNAME', 'FUNCNAME'), None, None, None, None, None, None, None, None, ('t_UNIT', 'UNIT'), None, None, None, (None, 'DOUBLE_STAR'), (None, 'COMMA'), (None, 'STAR'), (None, 'PERIOD'), (None, 'CARET'), (None, 'OPEN_PAREN'), (None, 'CLOSE_PAREN'), (None, 'SOLIDUS')])]}
_lexstateignore = {'INITIAL': ' '}
_lexstateerrorf = {'INITIAL': 't_error'}
_lexstateeoff = {}
Loading