Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MLAir
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
esde
machine-learning
MLAir
Commits
e76cd80e
Commit
e76cd80e
authored
5 years ago
by
lukas leufen
Browse files
Options
Downloads
Patches
Plain Diff
new helpers dict_to_xarray and float_round including tests and doc
parent
cbb44040
No related branches found
No related tags found
2 merge requests
!37
include new development
,
!27
Lukas issue032 feat plotting postprocessing
Pipeline
#28712
passed with warnings
5 years ago
Stage: test
Stage: pages
Stage: deploy
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/helpers.py
+39
-4
39 additions, 4 deletions
src/helpers.py
test/test_helpers.py
+52
-0
52 additions, 0 deletions
test/test_helpers.py
with
91 additions
and
4 deletions
src/helpers.py
+
39
−
4
View file @
e76cd80e
...
@@ -5,16 +5,19 @@ __date__ = '2019-10-21'
...
@@ -5,16 +5,19 @@ __date__ = '2019-10-21'
import
logging
import
logging
import
keras
import
keras.backend
as
K
import
math
import
math
from
typing
import
Union
import
numpy
as
np
import
os
import
os
import
time
import
time
import
socket
import
socket
import
datetime
as
dt
import
datetime
as
dt
import
keras
import
keras.backend
as
K
import
numpy
as
np
import
xarray
as
xr
from
typing
import
Union
,
Dict
,
Callable
def
to_list
(
arg
):
def
to_list
(
arg
):
if
not
isinstance
(
arg
,
list
):
if
not
isinstance
(
arg
,
list
):
...
@@ -197,3 +200,35 @@ class PyTestRegex:
...
@@ -197,3 +200,35 @@ class PyTestRegex:
def
__repr__
(
self
)
->
str
:
def
__repr__
(
self
)
->
str
:
return
self
.
_regex
.
pattern
return
self
.
_regex
.
pattern
def
dict_to_xarray
(
d
:
Dict
,
coordinate_name
:
str
)
->
xr
.
DataArray
:
"""
Convert a dictionary of 2D-xarrays to single 3D-xarray. The name of new coordinate axis follows <coordinate_name>.
:param d: dictionary with 2D-xarrays
:param coordinate_name: name of the new created axis (2D -> 3D)
:return: combined xarray
"""
xarray
=
None
for
k
,
v
in
d
.
items
():
if
xarray
is
None
:
xarray
=
v
xarray
.
coords
[
coordinate_name
]
=
k
else
:
tmp_xarray
=
v
tmp_xarray
.
coords
[
coordinate_name
]
=
k
xarray
=
xr
.
concat
([
xarray
,
tmp_xarray
],
coordinate_name
)
return
xarray
def
float_round
(
number
:
float
,
decimals
:
int
=
0
,
round_type
:
Callable
=
math
.
ceil
)
->
float
:
"""
Perform given rounding operation on number with the precision of decimals.
:param number: the number to round
:param decimals: numbers of decimals of the rounding operations (default 0 -> round to next integer value)
:param round_type: the actual rounding operation. Can be any callable function like math.ceil, math.floor or python
built-in round operation.
:return: rounded number with desired precision
"""
multiplier
=
10.
**
decimals
return
round_type
(
number
*
multiplier
)
/
multiplier
This diff is collapsed.
Click to expand it.
test/test_helpers.py
+
52
−
0
View file @
e76cd80e
...
@@ -189,3 +189,55 @@ class TestSetExperimentName:
...
@@ -189,3 +189,55 @@ class TestSetExperimentName:
def
test_set_experiment_from_sys
(
self
):
def
test_set_experiment_from_sys
(
self
):
exp_name
,
_
=
set_experiment_name
(
experiment_date
=
"
2019-11-14
"
)
exp_name
,
_
=
set_experiment_name
(
experiment_date
=
"
2019-11-14
"
)
assert
exp_name
==
"
2019-11-14_network
"
assert
exp_name
==
"
2019-11-14_network
"
class
TestPytestRegex
:
@pytest.fixture
def
regex
(
self
):
return
PyTestRegex
(
"
teststring
"
)
def
test_pytest_regex_init
(
self
,
regex
):
assert
regex
.
_regex
.
pattern
==
"
teststring
"
def
test_pytest_regex_eq
(
self
,
regex
):
assert
regex
==
"
teststringabcd
"
assert
regex
!=
"
teststgabcd
"
def
test_pytest_regex_repr
(
self
,
regex
):
assert
regex
.
__repr__
()
==
"
teststring
"
class
TestDictToXarray
:
def
test_dict_to_xarray
(
self
):
array1
=
xr
.
DataArray
(
np
.
random
.
randn
(
2
,
3
),
dims
=
(
'
x
'
,
'
y
'
),
coords
=
{
'
x
'
:
[
10
,
20
]})
array2
=
xr
.
DataArray
(
np
.
random
.
randn
(
2
,
3
),
dims
=
(
'
x
'
,
'
y
'
),
coords
=
{
'
x
'
:
[
10
,
20
]})
d
=
{
"
number1
"
:
array1
,
"
number2
"
:
array2
}
res
=
dict_to_xarray
(
d
,
"
merge_dim
"
)
assert
type
(
res
)
==
xr
.
DataArray
assert
sorted
(
list
(
res
.
coords
))
==
[
"
merge_dim
"
,
"
x
"
]
assert
res
.
shape
==
(
2
,
2
,
3
)
class
TestFloatRound
:
def
test_float_round_ceil
(
self
):
assert
float_round
(
4.6
)
==
5
assert
float_round
(
239.3992
)
==
240
def
test_float_round_decimals
(
self
):
assert
float_round
(
23.0091
,
2
)
==
23.01
assert
float_round
(
23.1091
,
3
)
==
23.11
def
test_float_round_type
(
self
):
assert
float_round
(
34.9221
,
2
,
math
.
floor
)
==
34.92
assert
float_round
(
34.9221
,
0
,
math
.
floor
)
==
34.
assert
float_round
(
34.9221
,
2
,
round
)
==
34.92
assert
float_round
(
34.9221
,
0
,
round
)
==
35.
def
test_float_round_negative
(
self
):
assert
float_round
(
-
34.9221
,
2
,
math
.
floor
)
==
-
34.93
assert
float_round
(
-
34.9221
,
0
,
math
.
floor
)
==
-
35.
assert
float_round
(
-
34.9221
,
2
)
==
-
34.92
assert
float_round
(
-
34.9221
,
0
)
==
-
34.
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