Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
KI-WaVo
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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Landesamt für Umwelt
KI-WaVo
Commits
d2c31376
Commit
d2c31376
authored
7 months ago
by
Michel Spils
Browse files
Options
Downloads
Patches
Plain Diff
zrx export better filename
parent
cb2bc705
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/utils/db_tools.py
+46
-32
46 additions, 32 deletions
src/utils/db_tools.py
with
46 additions
and
32 deletions
src/utils/db_tools.py
+
46
−
32
View file @
d2c31376
...
@@ -5,6 +5,7 @@ A collection of classes and functions for interacting with the oracle based Wavo
...
@@ -5,6 +5,7 @@ A collection of classes and functions for interacting with the oracle based Wavo
import
logging
import
logging
from
datetime
import
datetime
from
datetime
import
datetime
from
pathlib
import
Path
from
pathlib
import
Path
from
re
import
I
from
typing
import
List
from
typing
import
List
import
warnings
import
warnings
...
@@ -28,7 +29,8 @@ from utils.orm_classes import (
...
@@ -28,7 +29,8 @@ from utils.orm_classes import (
PegelForecasts
,
PegelForecasts
,
Log
,
Log
,
Sensor
,
Sensor
,
InputForecastsMeta
InputForecastsMeta
,
Modell
)
)
# pylint: disable=unsupported-assignment-operation
# pylint: disable=unsupported-assignment-operation
...
@@ -164,7 +166,7 @@ class OracleWaVoConnection:
...
@@ -164,7 +166,7 @@ class OracleWaVoConnection:
y
=
pred_single_db
(
model
,
df_input
)
y
=
pred_single_db
(
model
,
df_input
)
finally
:
finally
:
self
.
insert_forecast
(
self
.
insert_forecast
(
y
,
gauge_config
[
"
gauge
"
],
gauge_config
[
"
model_folder
"
].
name
,
end_time
,
member
,
created
y
,
gauge_config
,
end_time
,
member
,
created
)
)
def
load_input_db
(
self
,
in_size
,
end_time
,
gauge_config
,
created
)
->
pd
.
DataFrame
:
def
load_input_db
(
self
,
in_size
,
end_time
,
gauge_config
,
created
)
->
pd
.
DataFrame
:
...
@@ -261,8 +263,7 @@ class OracleWaVoConnection:
...
@@ -261,8 +263,7 @@ class OracleWaVoConnection:
try
:
try
:
self
.
insert_forecast
(
self
.
insert_forecast
(
y
,
y
,
gauge_config
[
"
gauge
"
],
gauge_config
,
gauge_config
[
"
model_folder
"
].
name
,
end_time
,
end_time
,
member
,
member
,
created
,
created
,
...
@@ -409,8 +410,7 @@ class OracleWaVoConnection:
...
@@ -409,8 +410,7 @@ class OracleWaVoConnection:
def
insert_forecast
(
def
insert_forecast
(
self
,
self
,
forecast
:
torch
.
Tensor
,
forecast
:
torch
.
Tensor
,
sensor_name
:
str
,
gauge_config
:
dict
,
model_name
:
str
,
end_time
:
pd
.
Timestamp
,
end_time
:
pd
.
Timestamp
,
member
:
int
,
member
:
int
,
created
:
pd
.
Timestamp
=
None
,
created
:
pd
.
Timestamp
=
None
,
...
@@ -421,40 +421,22 @@ class OracleWaVoConnection:
...
@@ -421,40 +421,22 @@ class OracleWaVoConnection:
Args:
Args:
forecast (Tensor): The forecast data to be inserted.
forecast (Tensor): The forecast data to be inserted.
gauge_config (dict): The configuration for the gauge.
end_time (pd.Timestamp): The timestamp of the forecast.
member (int): The member identifier.
member (int): The member identifier.
created (pd.Timestamp, optional): The timestamp of the forecast creation.
Returns:
Returns:
None
None
"""
"""
sensor_name
=
gauge_config
[
"
gauge
"
]
model_name
=
gauge_config
[
"
model_folder
"
].
name
# Turn tensor into dict to
# Turn tensor into dict to
if
forecast
.
isnan
().
any
():
if
forecast
.
isnan
().
any
():
fcst_values
=
{
f
"
h
{
i
}
"
:
None
for
i
in
range
(
1
,
49
)}
fcst_values
=
{
f
"
h
{
i
}
"
:
None
for
i
in
range
(
1
,
49
)}
else
:
else
:
fcst_values
=
{
f
"
h
{
i
}
"
:
forecast
[
i
-
1
].
item
()
for
i
in
range
(
1
,
49
)}
fcst_values
=
{
f
"
h
{
i
}
"
:
forecast
[
i
-
1
].
item
()
for
i
in
range
(
1
,
49
)}
self
.
_export_zrxp
(
forecast
,
gauge_config
,
end_time
,
member
,
sensor_name
,
model_name
,
created
)
if
self
.
main_config
.
get
(
"
export_zrxp
"
):
target_file
=
self
.
main_config
[
"
zrxp_out_folder
"
]
/
f
"
{
end_time
.
strftime
(
"
%Y%m%d%H
"
)
}
_
{
sensor_name
}
_
{
model_name
}
_
{
member
}
.zrx
"
#2023 11 19 03
df_zrxp
=
pd
.
DataFrame
(
forecast
,
columns
=
[
"
value
"
])
df_zrxp
[
"
timestamp
"
]
=
end_time
df_zrxp
[
"
forecast
"
]
=
pd
.
date_range
(
start
=
end_time
,
periods
=
49
,
freq
=
"
1h
"
)[
1
:]
df_zrxp
[
"
member
"
]
=
member
df_zrxp
=
df_zrxp
[[
'
timestamp
'
,
'
forecast
'
,
'
member
'
,
'
value
'
]]
with
open
(
target_file
,
'
w
'
,
encoding
=
"
utf-8
"
)
as
file
:
file
.
write
(
'
#REXCHANGEWISKI.
'
+
model_name
.
split
(
"
_
"
)[
0
]
+
'
.W.KNN|*|
\n
'
)
file
.
write
(
'
#RINVAL-777|*|
\n
'
)
file
.
write
(
'
#LAYOUT(timestamp,forecast, member,value)|*|
\n
'
)
df_zrxp
.
to_csv
(
path_or_buf
=
target_file
,
header
=
False
,
index
=
False
,
mode
=
'
a
'
,
sep
=
'
'
,
date_format
=
'
%Y%m%d%H%M
'
)
stmt
=
select
(
PegelForecasts
).
where
(
stmt
=
select
(
PegelForecasts
).
where
(
PegelForecasts
.
tstamp
==
bindparam
(
"
tstamp
"
),
PegelForecasts
.
tstamp
==
bindparam
(
"
tstamp
"
),
...
@@ -502,6 +484,38 @@ class OracleWaVoConnection:
...
@@ -502,6 +484,38 @@ class OracleWaVoConnection:
session
.
commit
()
session
.
commit
()
def
_export_zrxp
(
self
,
forecast
,
gauge_config
,
end_time
,
member
,
sensor_name
,
model_name
,
created
):
if
self
.
main_config
.
get
(
"
export_zrxp
"
)
and
member
==
0
:
with
Session
(
self
.
engine
)
as
session
:
stmt
=
select
(
Modell
).
where
(
Modell
.
modelldatei
==
str
(
gauge_config
[
"
model_folder
"
].
resolve
()))
model_obj
=
session
.
scalar
(
stmt
)
if
model_obj
is
None
:
logging
.
error
(
"
Model %s not found in database, skipping zrxp export
"
,
gauge_config
[
"
model_folder
"
])
return
target_file
=
self
.
main_config
[
"
zrxp_out_folder
"
]
/
f
"
{
end_time
.
strftime
(
"
%Y%m%d%H
"
)
}
_
{
sensor_name
.
replace
(
"
,
"
,
"
_
"
)
}
_
{
model_name
}
.zrx
"
#2023 11 19 03
df_zrxp
=
pd
.
DataFrame
(
forecast
,
columns
=
[
"
value
"
])
df_zrxp
[
"
timestamp
"
]
=
end_time
df_zrxp
[
"
forecast
"
]
=
pd
.
date_range
(
start
=
end_time
,
periods
=
49
,
freq
=
"
1h
"
)[
1
:]
df_zrxp
[
"
member
"
]
=
member
df_zrxp
=
df_zrxp
[[
'
timestamp
'
,
'
forecast
'
,
'
member
'
,
'
value
'
]]
with
open
(
target_file
,
'
w
'
,
encoding
=
"
utf-8
"
)
as
file
:
file
.
write
(
'
#REXCHANGEWISKI.
'
+
model_obj
.
modellname
+
'
.W.KNN|*|
\n
'
)
file
.
write
(
'
#RINVAL-777|*|
\n
'
)
file
.
write
(
'
#LAYOUT(timestamp,forecast, member,value)|*|
\n
'
)
df_zrxp
.
to_csv
(
path_or_buf
=
target_file
,
header
=
False
,
index
=
False
,
mode
=
'
a
'
,
sep
=
'
'
,
date_format
=
'
%Y%m%d%H%M
'
)
def
maybe_update_tables
(
self
)
->
None
:
def
maybe_update_tables
(
self
)
->
None
:
"""
"""
Updates the database tables if necessary based on the configuration settings.
Updates the database tables if necessary based on the configuration settings.
...
...
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