Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
ArchiveExtractor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
DG
ArchiveExtractor
Commits
d0fd0633
Commit
d0fd0633
authored
1 year ago
by
BRONES Romain
Browse files
Options
Downloads
Patches
Plain Diff
Improve boolean casting
* Now usable for GetNearest
parent
951bce18
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ArchiveExtractor.py
+55
-2
55 additions, 2 deletions
ArchiveExtractor.py
with
55 additions
and
2 deletions
ArchiveExtractor.py
+
55
−
2
View file @
d0fd0633
...
...
@@ -42,6 +42,13 @@ _DBDFMT2 = "%d-%m-%Y %H:%M:%S"
# NOTE: it is faster than using pandas.to_datetime()
_ArrayTimeStampToDatetime
=
np
.
vectorize
(
datetime
.
datetime
.
fromtimestamp
)
# Vectorized bool map dictionnary
_ArrayStr2Bool
=
np
.
vectorize
({
"
true
"
:
True
,
'
t
'
:
True
,
"
false
"
:
False
,
'
f
'
:
False
,
}.
get
)
def
_check_initialized
():
"""
Check if the module is initialized.
...
...
@@ -243,6 +250,48 @@ def _cmd_with_retry(dp, cmd, arg, retry=2):
break
return
cmdreturn
def
_cast_bool
(
value
):
"""
Cast a value, or array of values, to boolean.
Try to assess the input data type. If string, then try to find true or false word inside.
Parameters:
-----------
value: string, integer, or array of such
value to convert.
Return:
boolean:
value or array of boolean.
"""
# Force to array
value
=
np
.
asarray
(
value
)
# cast back to single value
def
castback
(
v
):
if
v
.
shape
==
():
return
v
.
item
()
return
v
# Simply try to cast to bool first
try
:
value
=
value
.
astype
(
"
bool
"
)
logger
.
debug
(
"
Direct conversion to boolean
"
)
return
castback
(
value
)
except
ValueError
:
# Keep trying to cast
pass
logger
.
debug
(
"
Try to convert to boolean
"
)
value
=
np
.
char
.
strip
(
np
.
char
.
lower
(
value
))
value
=
_ArrayStr2Bool
(
value
)
return
castback
(
value
)
##########################################################################
### Module private variables ###
##########################################################################
...
...
@@ -547,8 +596,12 @@ def _extract_scalar(attribute, method, date1, date2, db, dtype):
logger
.
error
(
"
Could not extract this chunk. Check the device extractor
"
)
return
None
# Transform by datatype
if
dtype
is
bool
:
_value
=
_cast_bool
(
_value
)
# Fabricate return pandas.Series
d
=
pd
.
Series
(
index
=
[
datetime
.
datetime
.
fromtimestamp
(
int
(
_date
)
/
1000
),],
data
=
[
float
(
_value
)
,],
name
=
attribute
)
d
=
pd
.
Series
(
index
=
[
datetime
.
datetime
.
fromtimestamp
(
int
(
_date
)
/
1000
),],
data
=
[
_value
,],
name
=
attribute
)
return
d
...
...
@@ -579,7 +632,7 @@ def _extract_scalar(attribute, method, date1, date2, db, dtype):
# Transform to datetime - value arrays
if
dtype
is
bool
:
_value
=
np
.
asarray
([{
"
true
"
:
True
,
"
false
"
:
False
}[
_a
.
lower
()]
for
_a
in
_value
]
)
_value
=
_cast_bool
(
_value
)
else
:
_value
=
np
.
asarray
(
_value
,
dtype
=
dtype
)
...
...
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