as
ticker
import
matplotlib.units
as
units
class
ProxyDelegate
:
def
__init__
(
self
,
fn_name
,
proxy_type
):
self
.
proxy_type
=
proxy_type
self
.
fn_name
=
fn_name
def
__get__
(
self
,
obj
,
objtype
=
None
):
return
self
.
proxy_type
(
self
.
fn_name
,
obj
)
class
TaggedValueMeta
(
type
):
def
__init__
(
self
,
name
,
bases
,
dict
):
for
fn_name
in
self
.
_proxies
:
if
not
hasattr
(
self
,
fn_name
):
setattr
(
self
,
fn_name
,
ProxyDelegate
(
fn_name
,
self
.
_proxies
[
fn_name
]))
class
PassThroughProxy
:
def
__init__
(
self
,
fn_name
,
obj
):
self
.
fn_name
=
fn_name
self
.
target
=
obj
.
proxy_target
def
__call__
(
self
,
*
args
):
fn
=
getattr
(
self
.
target
,
self
.
fn_name
)
ret
=
fn
(
*
args
)
return
ret
class
ConvertArgsProxy
(
PassThroughProxy
):
def
__init__
(
self
,
fn_name
,
obj
):
super
()
.
__init__
(
fn_name
,
obj
)
self
.
unit
=
obj
.
unit
def
__call__
(
self
,
*
args
):
converted_args
=
[]
for
a
in
args
:
try
:
converted_args
.
append
(
a
.
convert_to
(
self
.
unit
))
except
AttributeError
:
converted_args
.
append
(
TaggedValue
(
a
,
self
.
unit
))
converted_args
=
tuple
([
c
.
get_value
()
for
c
in
converted_args
])
return
super
()
.
__call__
(
*
converted_args
)
class
ConvertReturnProxy
(
PassThroughProxy
):
def
__init__
(
self
,
fn_name
,
obj
):
super
()
.
__init__
(
fn_name
,
obj
)
self
.
unit
=
obj
.
unit
def
__call__
(
self
,
*
args
):
ret
=
super
()
.
__call__
(
*
args
)
return
(
NotImplemented
if
ret
is
NotImplemented
else
TaggedValue
(
ret
,
self
.
unit
))
class
ConvertAllProxy
(
PassThroughProxy
):
def
__init__
(
self
,
fn_name
,
obj
):
super
()
.
__init__
(
fn_name
,
obj
)
self
.
unit
=
obj
.
unit
def
__call__
(
self
,
*
args
):
converted_args
=
[]
arg_units
=
[
self
.
unit
]
for
a
in
args
:
if
hasattr
(
a
,
'get_unit'
)
and
not
hasattr
(
a
,
'convert_to'
):
return
NotImplemented
if
hasattr
(
a
,
'convert_to'
):
try
:
a
=
a
.
convert_to
(
self
.
unit
)
except
Exception
:
pass
arg_units
.
append
(
a
.
get_unit
())
converted_args
.
append
(
a
.
get_value
())
else
:
converted_args
.
append
(
a
)
if
hasattr
(
a
,
'get_unit'
):
arg_units
.
append
(
a
.
get_unit
())
else
:
arg_units
.
append
(
None
)
converted_args
=
tuple
(
converted_args
)
ret
=
super
()
.
__call__
(
*
converted_args
)
if
ret
is
NotImplemented
:
return
NotImplemented
ret_unit
=
unit_resolver
(
self
.
fn_name
,
arg_units
)
if
ret_unit
is
NotImplemented
:
return
NotImplemented
return
TaggedValue
(
ret
,
ret_unit
)
class
TaggedValue
(
metaclass
=
):
_proxies
=
{
'__add__'
:
ConvertAllProxy
,
'__sub__'
:
ConvertAllProxy
,
'__mul__'
:
ConvertAllProxy
,
'__rmul__'
:
ConvertAllProxy
,
'__cmp__'
:
ConvertAllProxy
,
'__lt__'
:
ConvertAllProxy
,
'__gt__'
:
ConvertAllProxy
,
'__len__'
:
PassThroughProxy
}
def
__new__
(
cls
,
value
,
unit
):
value_class
=
type
(
value
)
try
:
subcls
=
type
(
f
'TaggedValue_of_
{
value_class
.
__name__
}
'
,
(
cls
,
value_class
),
{})
return
object
.
__new__
(
subcls
)
except
TypeError
:
return
object
.
__new__
(
cls
)
def
__init__
(
self
,
value
,
unit
):
self
.
value
=
value
self
.
unit
=
unit
self
.
proxy_target
=
self
.
value
def
__copy__
(
self
):
return
TaggedValue
(
self
.
value
,
self
.
unit
)
def
__getattribute__
(
self
,
name
):
if
name
.
startswith
(
'__'
):
return
object
.
__getattribute__
(
self
,
name
)
variable
=
object
.
__getattribute__
(
self
,
'value'
)
if
hasattr
(
variable
,
name
)
and
name
not
in
self
.
__class__
.
__dict__
:
return
getattr
(
variable
,
name
)
return
object
.
__getattribute__
(
self
,
name
)
def
__array__
(
self
,
dtype
=
object
,
copy
=
False
):
return
(
self
.
value
,
dtype
)
def
__array_wrap__
(
self
,
array
,
context
=
None
,
return_scalar
=
False
):
return
TaggedValue
(
array
,
self
.
unit
)
def
__repr__
(
self
):
return
f
'TaggedValue(
{
self
.
value
!r}
,
{
self
.
unit
!r}
)'
def
__str__
(
self
):
return
f
"
{
self
.
value
}
in
{
self
.
unit
}
"
def
__len__
(
self
):
return
len
(
self
.
value
)
if
parse_version
(
)
>=
parse_version
(
'1.20'
):
def
__getitem__
(
self
,
key
):
return
TaggedValue
(
self
.
value
[
key
],
self
.
unit
)
def
__iter__
(
self
):
return
(
TaggedValue
(
inner
,
self
.
unit
)
for
inner
in
self
.
value
)
def
get_compressed_copy
(
self
,
mask
):
new_value
=
(
self
.
value
,
mask
=
mask
)
.
compressed
()
return
TaggedValue
(
new_value
,
self
.
unit
)
def
convert_to
(
self
,
unit
):
if
unit
==
self
.
unit
or
not
unit
:
return
self
try
:
new_value
=
self
.
unit
.
convert_value_to
(
self
.
value
,
unit
)
except
AttributeError
:
new_value
=
self
return
TaggedValue
(
new_value
,
unit
)
def
get_value
(
self
):
return
self
.
value
def
get_unit
(
self
):
return
self
.
unit
class
BasicUnit
:
__array_priority__
=
(
0
)
.
__array_priority__
+
1
def
__init__
(
self
,
name
,
fullname
=
None
):
self
.
name
=
name
if
fullname
is
None
:
fullname
=
name
self
.
fullname
=
fullname
self
.
conversions
=
dict
()
def
__repr__
(
self
):
return
f
'BasicUnit(
{
self
.
name
}
)'
def
__str__
(
self
):
return
self
.
fullname
def
__call__
(
self
,
value
):
return
TaggedValue
(
value
,
self
)
def
__mul__
(
self
,
rhs
):
value
=
rhs
unit
=
self
if
hasattr
(
rhs
,
'get_unit'
):
value
=
rhs
.
get_value
()
unit
=
rhs
.
get_unit
()
unit
=
unit_resolver
(
'__mul__'
,
(
self
,
unit
))
if
unit
is
NotImplemented
:
return
NotImplemented
return
TaggedValue
(
value
,
unit
)
def
__rmul__
(
self
,
lhs
):
return
self
*
lhs
def
__array_wrap__
(
self
,
array
,
context
=
None
,
return_scalar
=
False
):
return
TaggedValue
(
array
,
self
)
def
__array__
(
self
,
t
=
None
,
context
=
None
,
copy
=
False
):
ret
=
(
1
)
if
t
is
not
None
:
return
ret
.
astype
(
t
)
else
:
return
ret
def
add_conversion_factor
(
self
,
unit
,
factor
):
def
convert
(
x
):
return
x
*
factor
self
.
conversions
[
unit
]
=
convert
def
add_conversion_fn
(
self
,
unit
,
fn
):
self
.
conversions
[
unit
]
=
fn
def
get_conversion_fn
(
self
,
unit
):
return
self
.
conversions
[
unit
]
def
convert_value_to
(
self
,
value
,
unit
):
conversion_fn
=
self
.
conversions
[
unit
]
ret
=
conversion_fn
(
value
)
return
ret
def
get_unit
(
self
):
return
self
class
UnitResolver
:
def
addition_rule
(
self
,
):
for
unit_1
,
unit_2
in
(
):
if
unit_1
!=
unit_2
:
return
NotImplemented
return
[
0
]
def
multiplication_rule
(
self
,
):
non_null
=
[
u
for
u
in
if
u
]
if
len
(
non_null
)
>
1
:
return
NotImplemented
return
non_null
[
0
]
op_dict
=
{
'__mul__'
:
multiplication_rule
,
'__rmul__'
:
multiplication_rule
,
'__add__'
:
addition_rule
,
'__radd__'
:
addition_rule
,
'__sub__'
:
addition_rule
,
'__rsub__'
:
addition_rule
}
def
__call__
(
self
,
operation
,
):
if
operation
not
in
self
.
op_dict
:
return
NotImplemented
return
self
.
op_dict
[
operation
](
self
,
)
unit_resolver
=
UnitResolver
()
cm
=
BasicUnit
(
'cm'
,
'centimeters'
)
inch
=
BasicUnit
(
'inch'
,
'inches'
)
inch
.
add_conversion_factor
(
cm
,
2.54
)
cm
.
add_conversion_factor
(
inch
,
1
/
2.54
)
radians
=
BasicUnit
(
'rad'
,
'radians'
)
degrees
=
BasicUnit
(
'deg'
,
'degrees'
)
radians
.
add_conversion_factor
(
degrees
,
180.0
/
)
degrees
.
add_conversion_factor
(
radians
,
/
180.0
)
secs
=
BasicUnit
(
's'
,
'seconds'
)
hertz
=
BasicUnit
(
'Hz'
,
'Hertz'
)
minutes
=
BasicUnit
(
'min'
,
'minutes'
)
secs
.
add_conversion_fn
(
hertz
,
lambda
x
:
1.
/
x
)
secs
.
add_conversion_factor
(
minutes
,
1
/
60.0
)
def
rad_fn
(
x
,
pos
=
None
):
if
x
>=
0
:
n
=
int
((
x
/
)
*
2.0
+
0.25
)
else
:
n
=
int
((
x
/
)
*
2.0
-
0.25
)
if
n
==
0
:
return
'0'
elif
n
==
1
:
return
r
'$\pi/2$'
elif
n
==
2
:
return
r
'$\pi$'
elif
n
==
-
1
:
return
r
'$-\pi/2$'
elif
n
==
-
2
:
return
r
'$-\pi$'
elif
n
%
2
==
0
:
return
fr
'$
{
n
//
2
}
\pi$'
else
:
return
fr
'$
{
n
}
\pi/2$'
class
BasicUnitConverter
(
):
@staticmethod
def
axisinfo
(
unit
,
axis
):
"""Return AxisInfo instance for x and unit."""
if
unit
==
radians
:
return
(
majloc
=
(
base
=
/
2
),
majfmt
=
(
rad_fn
),
label
=
unit
.
fullname
,
)
elif
unit
==
degrees
:
return
(
majloc
=
(),
majfmt
=
(
r
'$
%i
^\circ$'
),
label
=
unit
.
fullname
,
)
elif
unit
is
not
None
:
if
hasattr
(
unit
,
'fullname'
):
return
(
label
=
unit
.
fullname
)
elif
hasattr
(
unit
,
'unit'
):
return
(
label
=
unit
.
unit
.
fullname
)
return
None
@staticmethod
def
convert
(
val
,
unit
,
axis
):
if
(
val
):
if
isinstance
(
val
,
):
val
=
val
.
astype
(
float
)
.
filled
(
)
out
=
(
len
(
val
))
for
i
,
thisval
in
enumerate
(
val
):
if
(
thisval
):
out
[
i
]
=
else
:
try
:
out
[
i
]
=
thisval
.
convert_to
(
unit
)
.
get_value
()
except
AttributeError
:
out
[
i
]
=
thisval
return
out
if
(
val
):
return
else
:
return
val
.
convert_to
(
unit
)
.
get_value
()
@staticmethod
def
default_units
(
x
,
axis
):
"""Return the default unit for x or None."""
if
(
x
):
for
thisx
in
x
:
return
thisx
.
unit
return
x
.
unit
def
cos
(
x
):
if
(
x
):
return
[
(
val
.
convert_to
(
radians
)
.
get_value
())
for
val
in
x
]
else
:
return
(
x
.
convert_to
(
radians
)
.
get_value
())
[
BasicUnit
]
=
[
TaggedValue
]
=
()