Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Nic
gridsource
Commits
f089d1f8
Commit
f089d1f8
authored
Nov 23, 2020
by
Nic
Browse files
[units] add getunits(), getrow() and getvalue()
parent
dd7a0c02
Changes
2
Hide whitespace changes
Inline
Side-by-side
gridsource/validation.py
View file @
f089d1f8
...
...
@@ -19,6 +19,8 @@ import pkg_resources
import
simplejson
as
json
import
yaml
ureg
=
pint
.
UnitRegistry
()
def
yamlcat
(
*
files
):
"""concat YAML files and returns a StringIO object
...
...
@@ -492,6 +494,35 @@ class ValidatorMixin:
"""called by Base class __init__()"""
self
.
_schemas
=
{}
def
getunits
(
self
,
tabname
,
key
):
""" retrieve from schema the working units. Raise KeyError the the
column is not units-aware
"""
return
self
.
get
(
tabname
,
'schemas'
).
build
()[
'properties'
][
key
][
'units'
]
def
getrow
(
self
,
tabname
,
key
,
search_by
=
None
):
""" retrieve a row from any tabname, eventually sorting by `search_by`
"""
df
=
self
.
get
(
tabname
).
copy
()
if
search_by
:
df
.
set_index
(
search_by
,
inplace
=
True
)
return
df
.
loc
[
key
]
def
getvalue
(
self
,
tabname
,
key
,
column
,
search_by
=
None
,
as_quantity
=
True
):
""" retrive a cell content by intersecting row/column index
if as_quantity is:
* `True`: return a quantity argument *if possible*
* `False`: return magnitude
"""
row
=
self
.
getrow
(
tabname
,
key
,
search_by
)
value
=
row
[
column
]
if
as_quantity
:
units
=
self
.
getunits
(
tabname
,
column
)
return
value
*
ureg
.
parse_expression
(
units
)
else
:
return
value
def
quantify
(
self
,
tabname
,
restrict_to_units
=
False
):
df
=
self
.
_data
[
tabname
].
copy
()
schema
=
self
.
_schemas
[
tabname
]
...
...
tests/test_validation.py
View file @
f089d1f8
...
...
@@ -13,7 +13,7 @@ import pytest
from
gridsource
import
Data
as
IVData
from
gridsource
import
ValidData
as
VData
from
gridsource.validation
import
DataFrameSchematizer
from
gridsource.validation
import
DataFrameSchematizer
,
ureg
def
test_DFS_00_schema_bulding
():
...
...
@@ -514,6 +514,40 @@ def test_IVData_06_units(datadir):
df_imp
=
data_imp
.
convert_to
(
tabname
)
# .pint.to_base_units()
df_si
=
data_SI
.
convert_to
(
tabname
)
# .pint.to_base_units()
assert
pd
.
testing
.
assert_frame_equal
(
df_imp
,
df_si
)
is
None
# -------------------------------------------------------------------------
# getunits
assert
data_SI
.
getunits
(
"pressure"
,
key
=
"test_p_bar"
)
==
"MPa"
assert
data_imp
.
getunits
(
"pressure"
,
key
=
"test_p_bar"
)
==
"psi"
# column length/id has no units:
with
pytest
.
raises
(
KeyError
):
data_imp
.
getunits
(
"length"
,
key
=
"id"
)
# getvalue
l9
=
data_SI
.
getrow
(
"length"
,
key
=
9
,
search_by
=
"id"
)
l9_expected
=
pd
.
Series
(
{
"test_dist_m"
:
2000
,
"test_dist_yd"
:
2000
,
"test_dist_in"
:
2000
,
"test_dist_ft"
:
2000
,
"test_dist_foot"
:
2000
,
"test_string"
:
"a"
,
"groucho"
:
3.2
,
},
name
=
9.0
,
)
assert
pd
.
testing
.
assert_series_equal
(
l9
,
l9_expected
)
is
None
l9_value
=
data_SI
.
getvalue
(
"length"
,
key
=
9
,
search_by
=
"id"
,
column
=
"test_dist_yd"
,
as_quantity
=
False
)
assert
pytest
.
approx
(
l9_value
)
==
2000
l9_quantity_exp
=
2
*
ureg
.
m
# rendering with quantity:
l9_quantity
=
data_SI
.
getvalue
(
"length"
,
key
=
9
,
search_by
=
"id"
,
column
=
"test_dist_yd"
)
diff
=
l9_quantity
-
l9_quantity_exp
assert
abs
(
diff
.
magnitude
)
<
1e-5
def
test_IVData_07_failing_units
(
datadir
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment