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
955426d7
Commit
955426d7
authored
4 years ago
by
leufen1
Browse files
Options
Downloads
Patches
Plain Diff
added a numpy filter version (should be again faster)
parent
202d9baa
Branches
Branches containing commit
Tags
Tags containing commit
5 merge requests
!319
add all changes of dev into release v1.4.0 branch
,
!318
Resolve "release v1.4.0"
,
!317
enabled window_lead_time=1
,
!295
Resolve "data handler FIR filter"
,
!259
Draft: Resolve "WRF-Datahandler should inherit from SingleStationDatahandler"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mlair/helpers/filter.py
+25
-10
25 additions, 10 deletions
mlair/helpers/filter.py
with
25 additions
and
10 deletions
mlair/helpers/filter.py
+
25
−
10
View file @
955426d7
...
@@ -11,7 +11,7 @@ from matplotlib import pyplot as plt
...
@@ -11,7 +11,7 @@ from matplotlib import pyplot as plt
from
scipy
import
signal
from
scipy
import
signal
import
xarray
as
xr
import
xarray
as
xr
from
mlair.helpers
import
to_list
,
TimeTrackingWrapper
from
mlair.helpers
import
to_list
,
TimeTrackingWrapper
,
TimeTracking
class
FIRFilter
:
class
FIRFilter
:
...
@@ -258,14 +258,19 @@ class ClimateFIRFilter:
...
@@ -258,14 +258,19 @@ class ClimateFIRFilter:
# combine historical data / observation [t0-length,t0] and climatological statistics [t0+1,t0+length]
# combine historical data / observation [t0-length,t0] and climatological statistics [t0+1,t0+length]
history
=
self
.
_shift_data
(
data
,
range
(
-
length
,
1
),
time_dim
,
var_dim
,
new_dim
)
history
=
self
.
_shift_data
(
data
,
range
(
-
length
,
1
),
time_dim
,
var_dim
,
new_dim
)
future
=
self
.
_shift_data
(
apriori
,
range
(
1
,
length
+
1
),
time_dim
,
var_dim
,
new_dim
)
future
=
self
.
_shift_data
(
apriori
,
range
(
1
,
length
+
1
),
time_dim
,
var_dim
,
new_dim
)
filter_input_data
=
history
.
combine_first
(
future
)
filter_input_data
=
xr
.
concat
([
history
.
dropna
(
time_dim
),
future
],
dim
=
new_dim
,
join
=
"
left
"
)
# filter_input_data = history.combine_first(future)
time_axis
=
filter_input_data
.
coords
[
"
datetime
"
]
time_axis
=
filter_input_data
.
coords
[
"
datetime
"
]
# apply vectorized fir filter along the tmp dimension
# apply vectorized fir filter along the tmp dimension
filt
=
xr
.
apply_ufunc
(
fir_filter_vectorized
,
filter_input_data
,
time_axis
,
kwargs
=
{
"
fs
"
:
fs
,
"
cutoff_high
"
:
cutoff_high
,
"
order
"
:
order
,
input_core_dims
=
[[
new_dim
],
[]],
output_core_dims
=
[[
new_dim
]],
vectorize
=
True
,
"
causal
"
:
False
,
"
padlen
"
:
int
(
min
(
padlen_factor
,
1
)
*
length
)}
kwargs
=
{
"
fs
"
:
fs
,
"
cutoff_high
"
:
cutoff_high
,
"
order
"
:
order
,
with
TimeTracking
():
"
causal
"
:
False
,
"
padlen
"
:
int
(
min
(
padlen_factor
,
1
)
*
length
)})
filt
=
fir_filter_numpy_vectorized
(
filter_input_data
,
var_dim
,
kwargs
)
# with TimeTracking():
# filt = xr.apply_ufunc(fir_filter_vectorized, filter_input_data, time_axis,
# input_core_dims=[[new_dim], []], output_core_dims=[[new_dim]], vectorize=True,
# kwargs=kwargs)
# plot
# plot
if
self
.
plot_path
is
not
None
:
if
self
.
plot_path
is
not
None
:
...
@@ -383,13 +388,23 @@ def fir_filter(data, fs, order=5, cutoff_low=None, cutoff_high=None, window="ham
...
@@ -383,13 +388,23 @@ def fir_filter(data, fs, order=5, cutoff_low=None, cutoff_high=None, window="ham
return
filtered
,
h
return
filtered
,
h
def
fir_filter_vectorized
(
data
,
time_stamp
,
fs
,
order
=
5
,
cutoff_low
=
None
,
cutoff_high
=
None
,
window
=
"
hamming
"
,
h
=
None
,
def
fir_filter_numpy_vectorized
(
filter_input_data
,
var_dim
,
kwargs
):
filt_np
=
xr
.
DataArray
(
np
.
nan
,
coords
=
filter_input_data
.
coords
)
for
var
in
filter_input_data
.
coords
[
var_dim
]:
a
=
np
.
apply_along_axis
(
fir_filter_vectorized
,
2
,
filter_input_data
.
sel
({
var_dim
:
var
}).
values
,
**
kwargs
)
filt_np
.
loc
[{
var_dim
:
var
}]
=
a
return
filt_np
def
fir_filter_vectorized
(
data
,
time_stamp
=
None
,
fs
=
1
,
order
=
5
,
cutoff_low
=
None
,
cutoff_high
=
None
,
window
=
"
hamming
"
,
h
=
None
,
causal
=
True
,
causal
=
True
,
padlen
=
None
):
padlen
=
None
):
"""
Expects numpy array.
"""
"""
Expects numpy array.
"""
pd_date
=
pd
.
to_datetime
(
time_stamp
)
if
time_stamp
is
not
None
:
if
pd_date
.
day
==
1
and
pd_date
.
month
in
[
1
,
7
]:
pd_date
=
pd
.
to_datetime
(
time_stamp
)
logging
.
info
(
time_stamp
)
if
pd_date
.
day
==
1
and
pd_date
.
month
in
[
1
,
7
]:
logging
.
info
(
time_stamp
)
sel
=
~
np
.
isnan
(
data
)
sel
=
~
np
.
isnan
(
data
)
res
=
np
.
empty_like
(
data
)
res
=
np
.
empty_like
(
data
)
if
h
is
None
:
if
h
is
None
:
...
...
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