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
4f2a8d4c
Commit
4f2a8d4c
authored
2 years ago
by
Martin Lange
Browse files
Options
Downloads
Patches
Plain Diff
store units as pint object in info
parent
0e87f9ec
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!237
Optimize units etc.
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/finam/adapters/time_integration.py
+1
-1
1 addition, 1 deletion
src/finam/adapters/time_integration.py
src/finam/data/tools.py
+13
-11
13 additions, 11 deletions
src/finam/data/tools.py
tests/data/test_tools.py
+0
-5
0 additions, 5 deletions
tests/data/test_tools.py
with
14 additions
and
17 deletions
src/finam/adapters/time_integration.py
+
1
−
1
View file @
4f2a8d4c
...
...
@@ -291,7 +291,7 @@ class SumOverTime(TimeIntegrationAdapter):
in_info
=
self
.
exchange_info
(
up_info
)
units
=
tools
.
UNITS
.
Unit
(
in_info
.
units
)
units
=
in_info
.
units
if
self
.
_per_time
:
units
*=
tools
.
UNITS
.
Unit
(
"
s
"
)
out_info
=
in_info
.
copy_with
(
units
=
(
1.0
*
units
).
to_reduced_units
().
units
)
...
...
This diff is collapsed.
Click to expand it.
src/finam/data/tools.py
+
13
−
11
View file @
4f2a8d4c
...
...
@@ -17,7 +17,6 @@ from .grid_tools import Grid, GridBase
pint
.
application_registry
.
default_format
=
"
cf
"
UNITS
=
pint
.
application_registry
_UNIT_CACHE
=
{}
_UNIT_PAIRS_CACHE
=
{}
...
...
@@ -51,7 +50,7 @@ def prepare(data, info, time_entries=1, force_copy=False):
FinamDataError
If the data doesn
'
t match its info.
"""
units
=
_get_pint_units
(
info
.
units
)
units
=
info
.
units
if
isinstance
(
data
,
pint
.
Quantity
):
if
not
compatible_units
(
data
.
units
,
units
):
raise
FinamDataError
(
...
...
@@ -355,7 +354,7 @@ def check(
if
not
compatible_units
(
info
.
units
,
xdata
):
raise
FinamDataError
(
f
"
check: given data has incompatible units.
"
f
"
Got
{
get_units
(
xdata
)
}
, expected
{
UNITS
.
Unit
(
info
.
units
)
}
.
"
f
"
Got
{
get_units
(
xdata
)
}
, expected
{
info
.
units
}
.
"
)
...
...
@@ -434,15 +433,13 @@ def _get_pint_units(var):
if
var
is
None
:
raise
FinamDataError
(
"
Can
'
t extract units from
'
None
'
.
"
)
if
isinstance
(
var
,
pint
.
Unit
):
return
var
if
isinstance
(
var
,
pint
.
Quantity
):
return
var
.
units
or
UNITS
.
dimensionless
units
=
_UNIT_CACHE
.
get
(
var
)
if
units
is
None
:
units
=
UNITS
.
Unit
(
var
)
_UNIT_CACHE
[
var
]
=
units
return
units
return
UNITS
.
Unit
(
var
)
def
compatible_units
(
unit1
,
unit2
):
...
...
@@ -508,7 +505,6 @@ def _cache_units(unit1, unit2):
def
clear_units_cache
():
"""
Clears the units cache
"""
_UNIT_CACHE
.
clear
()
_UNIT_PAIRS_CACHE
.
clear
()
...
...
@@ -557,7 +553,10 @@ class Info:
self
.
grid
=
grid
self
.
meta
=
meta
or
{}
self
.
meta
.
update
(
meta_kwargs
)
self
.
meta
.
setdefault
(
"
units
"
,
""
)
units
=
self
.
meta
.
get
(
"
units
"
,
""
)
units
=
None
if
units
is
None
else
UNITS
.
Unit
(
units
)
self
.
meta
[
"
units
"
]
=
units
def
copy
(
self
):
"""
Copies the info object
"""
...
...
@@ -581,6 +580,9 @@ class Info:
elif
k
==
"
grid
"
:
if
v
is
not
None
or
use_none
:
other
.
grid
=
v
elif
k
==
"
units
"
:
if
v
is
not
None
or
use_none
:
other
.
meta
[
k
]
=
v
if
v
is
None
else
UNITS
.
Unit
(
v
)
else
:
if
v
is
not
None
or
use_none
:
other
.
meta
[
k
]
=
v
...
...
This diff is collapsed.
Click to expand it.
tests/data/test_tools.py
+
0
−
5
View file @
4f2a8d4c
...
...
@@ -274,12 +274,8 @@ class TestDataTools(unittest.TestCase):
def
test_cache_units
(
self
):
finam
.
data
.
tools
.
clear_units_cache
()
self
.
assertEqual
({},
finam
.
data
.
tools
.
_UNIT_CACHE
)
self
.
assertEqual
({},
finam
.
data
.
tools
.
_UNIT_PAIRS_CACHE
)
u
=
finam
.
data
.
tools
.
_get_pint_units
(
"
m
"
)
self
.
assertEqual
({
"
m
"
:
u
},
finam
.
data
.
tools
.
_UNIT_CACHE
)
eqiv
=
finam
.
data
.
tools
.
equivalent_units
(
"
mm
"
,
"
L/m^2
"
)
self
.
assertTrue
(
eqiv
)
self
.
assertEqual
(
...
...
@@ -289,5 +285,4 @@ class TestDataTools(unittest.TestCase):
finam
.
data
.
tools
.
clear_units_cache
()
self
.
assertEqual
({},
finam
.
data
.
tools
.
_UNIT_CACHE
)
self
.
assertEqual
({},
finam
.
data
.
tools
.
_UNIT_PAIRS_CACHE
)
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