Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SaQC
Manage
Activity
Members
Labels
Plan
Issues
35
Issue boards
Milestones
Wiki
Code
Merge requests
7
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
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
rdm-software
SaQC
Commits
fb03a2b9
Commit
fb03a2b9
authored
4 years ago
by
Juliane Geller
Browse files
Options
Downloads
Patches
Plain Diff
flag drift with scaling function
parent
3612c19f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
3 merge requests
!193
Release 1.4
,
!188
Release 1.4
,
!138
WIP: Detect and reset offset
Pipeline
#9792
passed with stage
in 6 minutes and 12 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
saqc/funcs/functions.py
+47
-2
47 additions, 2 deletions
saqc/funcs/functions.py
test/funcs/test_functions.py
+9
-2
9 additions, 2 deletions
test/funcs/test_functions.py
with
56 additions
and
4 deletions
saqc/funcs/functions.py
+
47
−
2
View file @
fb03a2b9
...
...
@@ -4,6 +4,7 @@
from
functools
import
partial
from
inspect
import
signature
import
dios
import
numpy
as
np
import
pandas
as
pd
import
scipy
...
...
@@ -13,6 +14,7 @@ import itertools
import
collections
import
numba
from
mlxtend.evaluate
import
permutation_test
from
scipy
import
stats
from
scipy.cluster.hierarchy
import
linkage
,
fcluster
...
...
@@ -955,7 +957,7 @@ def flagDriftFromNorm(data, field, flagger, fields, segment_freq, norm_spread, n
for
segment
in
segments
:
if
segment
[
1
].
shape
[
0
]
<=
1
:
continue
drifters
=
detectDeviants
(
data
,
metric
,
norm_spread
,
norm_frac
,
linkage_method
,
'
variables
'
)
drifters
=
detectDeviants
(
segment
[
1
]
,
metric
,
norm_spread
,
norm_frac
,
linkage_method
,
'
variables
'
)
for
var
in
drifters
:
flagger
=
flagger
.
setFlags
(
fields
[
var
],
loc
=
segment
[
1
].
index
,
**
kwargs
)
...
...
@@ -969,7 +971,7 @@ def flagDriftFromReference(data, field, flagger, fields, segment_freq, thresh,
"""
The function flags value courses that deviate from a reference course by a margin exceeding a certain threshold.
The deviation is meas
s
ured by the distance function passed to parameter metric.
The deviation is measured by the distance function passed to parameter metric.
Parameters
----------
...
...
@@ -1027,3 +1029,46 @@ def flagDriftFromReference(data, field, flagger, fields, segment_freq, thresh,
return
data
,
flagger
def
flagDriftScale
(
data
,
field
,
flagger
,
fields_scale1
,
fields_scale2
,
segment_freq
,
norm_spread
,
norm_frac
=
0.5
,
metric
=
lambda
x
,
y
:
scipy
.
spatial
.
distance
.
pdist
(
np
.
array
([
x
,
y
]),
metric
=
'
cityblock
'
)
/
len
(
x
),
linkage_method
=
'
single
'
,
**
kwargs
):
fields
=
fields_scale1
+
fields_scale2
data_to_flag
=
data
[
fields
].
to_df
()
data_to_flag
.
dropna
(
inplace
=
True
)
convert_slope
=
[]
convert_intercept
=
[]
for
field1
in
fields_scale1
:
for
field2
in
fields_scale2
:
slope
,
intercept
,
r_value
,
p_value
,
std_err
=
stats
.
linregress
(
data_to_flag
[
field1
],
data_to_flag
[
field2
])
convert_slope
.
append
(
slope
)
convert_intercept
.
append
(
intercept
)
factor_slope
=
np
.
median
(
convert_slope
)
factor_intercept
=
np
.
median
(
convert_intercept
)
dat
=
dios
.
DictOfSeries
()
for
field1
in
fields_scale1
:
dat
[
field1
]
=
factor_intercept
+
factor_slope
*
data_to_flag
[
field1
]
for
field2
in
fields_scale2
:
dat
[
field2
]
=
data_to_flag
[
field2
]
dat_to_flag
=
dat
[
fields
].
to_df
()
segments
=
dat_to_flag
.
groupby
(
pd
.
Grouper
(
freq
=
segment_freq
))
for
segment
in
segments
:
if
segment
[
1
].
shape
[
0
]
<=
1
:
continue
drifters
=
detectDeviants
(
segment
[
1
],
metric
,
norm_spread
,
norm_frac
,
linkage_method
,
'
variables
'
)
for
var
in
drifters
:
flagger
=
flagger
.
setFlags
(
fields
[
var
],
loc
=
segment
[
1
].
index
,
**
kwargs
)
return
data
,
flagger
\ No newline at end of file
This diff is collapsed.
Click to expand it.
test/funcs/test_functions.py
+
9
−
2
View file @
fb03a2b9
...
...
@@ -32,7 +32,7 @@ def test_flagRange(data, field, flagger):
assert
(
flagged
==
expected
).
all
()
@pytest.mark.parametrize
(
"
flagger
"
,
TESTFLAGGER
)
'''
@pytest.mark.parametrize(
"
flagger
"
, TESTFLAGGER)
@pytest.mark.parametrize(
"
method
"
, [
'
wavelet
'
,
'
dtw
'
])
@pytest.mark.parametrize(
"
pattern
"
, [pytest.lazy_fixture(
"
course_pattern_1
"
),
pytest.lazy_fixture(
"
course_pattern_2
"
),] ,)
...
...
@@ -53,7 +53,7 @@ def test_flagPattern(course_test, flagger, method, pattern):
flagger = flagger.initFlags(test_data)
data, flagger = flagPattern(test_data,
"
data
"
, flagger, reference_field=
"
pattern_data
"
, partition_freq=
"
days
"
, method=method)
assert flagger.isFlagged(
"
data
"
)[dict_pattern[
"
pattern_2
"
]].all()
'''
...
...
@@ -226,11 +226,18 @@ def test_flagDriftFromNormal(dat, flagger):
data
=
dat
(
periods
=
200
,
peak_level
=
5
,
name
=
'
d1
'
)[
0
]
data
[
'
d2
'
]
=
dat
(
periods
=
200
,
peak_level
=
10
,
name
=
'
d2
'
)[
0
][
'
d2
'
]
data
[
'
d3
'
]
=
dat
(
periods
=
200
,
peak_level
=
100
,
name
=
'
d3
'
)[
0
][
'
d3
'
]
data
[
'
d4
'
]
=
3
+
4
*
data
[
'
d1
'
]
data
[
'
d5
'
]
=
3
+
4
*
data
[
'
d1
'
]
flagger
=
flagger
.
initFlags
(
data
)
data_norm
,
flagger_norm
=
flagDriftFromNorm
(
data
,
'
dummy
'
,
flagger
,
[
'
d1
'
,
'
d2
'
,
'
d3
'
],
segment_freq
=
"
200min
"
,
norm_spread
=
5
)
data_ref
,
flagger_ref
=
flagDriftFromReference
(
data
,
'
d1
'
,
flagger
,
[
'
d1
'
,
'
d2
'
,
'
d3
'
],
segment_freq
=
"
3D
"
,
thresh
=
20
)
data_scale
,
flagger_scale
=
flagDriftScale
(
data
,
'
dummy
'
,
flagger
,
[
'
d1
'
,
'
d3
'
],
[
'
d4
'
,
'
d5
'
],
segment_freq
=
"
3D
"
,
thresh
=
20
,
norm_spread
=
5
)
assert
flagger_norm
.
isFlagged
()[
'
d3
'
].
all
()
assert
flagger_ref
.
isFlagged
()[
'
d3
'
].
all
()
assert
flagger_scale
.
isFlagged
()[
'
d3
'
].
all
()
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