Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
finam
Manage
Activity
Members
Labels
Plan
Issues
32
Issue boards
Milestones
Code
Merge requests
4
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FINAM
finam
Commits
97b17e91
Commit
97b17e91
authored
2 years ago
by
Martin Lange
Browse files
Options
Downloads
Patches
Plain Diff
cache units compatibility and equivalence
parent
4bd14d02
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!229
Cache units, compatibility and equivalence
Pipeline
#133703
passed with stages
in 5 minutes and 32 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/finam/data/tools.py
+29
-4
29 additions, 4 deletions
src/finam/data/tools.py
with
29 additions
and
4 deletions
src/finam/data/tools.py
+
29
−
4
View file @
97b17e91
...
...
@@ -25,6 +25,8 @@ from .grid_tools import Grid, GridBase
pint_xarray
.
unit_registry
.
default_format
=
"
cf
"
UNITS
=
pint_xarray
.
unit_registry
_UNITS_CACHE
=
{}
def
_gen_dims
(
ndim
,
info
):
"""
...
...
@@ -533,10 +535,14 @@ def compatible_units(unit1, unit2):
Returns
-------
bool
U
i
nt compatibility.
Un
i
t compatibility.
"""
unit1
,
unit2
=
_get_pint_units
(
unit1
),
_get_pint_units
(
unit2
)
return
unit1
.
is_compatible_with
(
unit2
)
comp_equiv
=
_UNITS_CACHE
.
get
((
unit1
,
unit2
))
if
comp_equiv
is
None
:
comp_equiv
=
_cache_units
(
unit1
,
unit2
)
return
comp_equiv
[
0
]
def
equivalent_units
(
unit1
,
unit2
):
...
...
@@ -556,10 +562,29 @@ def equivalent_units(unit1, unit2):
Unit equivalence.
"""
unit1
,
unit2
=
_get_pint_units
(
unit1
),
_get_pint_units
(
unit2
)
comp_equiv
=
_UNITS_CACHE
.
get
((
unit1
,
unit2
))
if
comp_equiv
is
None
:
comp_equiv
=
_cache_units
(
unit1
,
unit2
)
return
comp_equiv
[
1
]
def
_cache_units
(
unit1
,
unit2
):
equiv
=
False
compat
=
False
try
:
return
np
.
isclose
((
1.0
*
unit1
).
to
(
unit2
).
magnitude
,
1.0
)
equiv
=
np
.
isclose
((
1.0
*
unit1
).
to
(
unit2
).
magnitude
,
1.0
)
compat
=
True
except
pint
.
errors
.
DimensionalityError
:
return
False
pass
_UNITS_CACHE
[(
unit1
,
unit2
)]
=
compat
,
equiv
return
compat
,
equiv
def
clear_units_cache
():
"""
Clears the units cache
"""
_UNITS_CACHE
.
clear
()
def
assert_type
(
cls
,
slot
,
obj
,
types
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment