Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SaQC
Manage
Activity
Members
Labels
Plan
Issues
36
Issue boards
Milestones
Wiki
Code
Merge requests
8
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
c02b50ac
Commit
c02b50ac
authored
4 years ago
by
Bert Palm
🎇
Browse files
Options
Downloads
Patches
Plain Diff
added reindex to History, and added tests for that; fixed old bug in test History
parent
a09fd07f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!231
new Flagger and Stuff
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
saqc/flagger/history.py
+28
-6
28 additions, 6 deletions
saqc/flagger/history.py
test/flagger/test_history.py
+39
-0
39 additions, 0 deletions
test/flagger/test_history.py
with
67 additions
and
6 deletions
saqc/flagger/history.py
+
28
−
6
View file @
c02b50ac
...
...
@@ -4,6 +4,7 @@ from __future__ import annotations
from
typing
import
Tuple
,
Type
import
pandas
as
pd
import
numpy
as
np
from
saqc.common
import
*
class
History
:
...
...
@@ -149,7 +150,9 @@ class History:
-------
History
"""
# internal detail:
# Note:
# all following code must handle a passed empty series
# ensure continuous increasing columns
assert
0
<=
pos
<=
len
(
self
)
...
...
@@ -191,11 +194,8 @@ class History:
"""
s
=
self
.
_validate_value
(
value
)
if
s
.
empty
:
raise
ValueError
(
'
Cannot append empty pd.Series
'
)
if
not
self
.
empty
and
not
s
.
index
.
equals
(
self
.
index
):
raise
ValueError
(
"
Index must be equal to FH
'
s index
"
)
if
len
(
self
)
>
0
and
not
s
.
index
.
equals
(
self
.
index
):
raise
ValueError
(
"
Index must be equal to FlagHistory index
"
)
self
.
_insert
(
value
,
pos
=
len
(
self
),
force
=
force
)
return
self
...
...
@@ -279,6 +279,28 @@ class History:
"""
return
self
.
_constructor
(
hist
=
self
,
copy
=
deep
)
def
reindex
(
self
,
index
:
pd
.
Index
,
fill_value_last
:
float
=
UNFLAGGED
)
->
History
:
"""
Reindex the History. Be careful this alters the past.
Parameters
----------
index : pd.Index
the index to reindex to.
fill_value_last : float, default 0
value to fill nan
'
s (UNTOUCHED) in the last column. Defaults to 0 (UNFLAGGED).
Returns
-------
History
"""
self
.
hist
=
self
.
hist
.
reindex
(
index
=
index
,
copy
=
False
,
fill_value
=
np
.
nan
)
self
.
mask
=
self
.
mask
.
reindex
(
index
=
index
,
copy
=
False
,
fill_value
=
False
)
# Note: all following code must handle empty frames
self
.
hist
.
iloc
[:,
-
1
:]
=
self
.
hist
.
iloc
[:,
-
1
:].
fillna
(
fill_value_last
)
self
.
mask
.
iloc
[:,
-
1
:]
=
True
return
self
def
__copy__
(
self
,
deep
:
bool
=
True
):
return
self
.
copy
(
deep
=
deep
)
...
...
This diff is collapsed.
Click to expand it.
test/flagger/test_history.py
+
39
−
0
View file @
c02b50ac
...
...
@@ -102,7 +102,11 @@ def check_invariants(hist):
# or the entire row is True
if
not
hist
.
empty
:
idxmax
=
hist
.
mask
.
idxmax
(
axis
=
1
)
print
(
f
'
idxmax:
{
idxmax
}
'
)
for
row
,
col
in
idxmax
.
items
():
# this is contra intuitive, it gets the positional (for iloc)
row
=
idxmax
.
index
.
get_loc
(
row
)
assert
all
(
hist
.
mask
.
iloc
[
row
,
:
col
]
==
False
)
assert
all
(
hist
.
mask
.
iloc
[
row
,
col
:]
==
True
)
...
...
@@ -183,6 +187,41 @@ def test_copy(data):
assert
shallow
.
mask
is
hist
.
mask
@pytest.mark.parametrize
(
'
data
'
,
data
+
[
None
])
def
test_reindex_trivial_cases
(
data
):
df
=
pd
.
DataFrame
(
data
,
dtype
=
float
)
orig
=
History
(
hist
=
df
)
# checks
for
index
in
[
df
.
index
,
pd
.
Index
([])]:
hist
=
orig
.
copy
()
ref
=
hist
.
reindex
(
index
)
assert
ref
is
hist
# check if working inplace
check_invariants
(
hist
)
@pytest.mark.parametrize
(
'
data
'
,
data
+
[
None
])
def
test_reindex_missing_indicees
(
data
):
df
=
pd
.
DataFrame
(
data
,
dtype
=
float
)
hist
=
History
(
hist
=
df
)
index
=
df
.
index
[
1
:
-
1
]
# checks
ref
=
hist
.
reindex
(
index
)
assert
ref
is
hist
# check if working inplace
check_invariants
(
hist
)
@pytest.mark.parametrize
(
'
data
'
,
data
+
[
None
])
def
test_reindex_extra_indicees
(
data
):
df
=
pd
.
DataFrame
(
data
,
dtype
=
float
)
hist
=
History
(
hist
=
df
)
index
=
df
.
index
.
append
(
pd
.
Index
(
range
(
len
(
df
.
index
),
len
(
df
.
index
)
+
5
)))
# checks
ref
=
hist
.
reindex
(
index
)
assert
ref
is
hist
# check if working inplace
check_invariants
(
hist
)
@pytest.fixture
(
scope
=
'
module
'
)
def
__hist
():
# this FH is filled by
...
...
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