Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
jlopez
mandrill
Commits
dab7bdf8
Commit
dab7bdf8
authored
May 23, 2019
by
jlopez
Browse files
Bug refactor and use Rcpp
parent
14e06dd6
Changes
4
Hide whitespace changes
Inline
Side-by-side
.gitignore
View file @
dab7bdf8
...
...
@@ -9,3 +9,4 @@
.Rproj.user/
.Rproj.user
exstract.cpp
0 → 100644
View file @
dab7bdf8
#include <Rcpp.h>
#include <vector>
#include <fstream>
#include <map>
#include <time.h>
using
namespace
std
;
using
namespace
Rcpp
;
std
::
map
<
std
::
string
,
std
::
string
>
months
{
{
"Jan"
,
"01"
}
,
{
"Feb"
,
"02"
},
{
"Mar"
,
"03"
},
{
"Apr"
,
"04"
},
{
"May"
,
"05"
},
{
"Jun"
,
"06"
},
{
"Jul"
,
"07"
},
{
"Aug"
,
"08"
},
{
"Sep"
,
"09"
},
{
"Oct"
,
"10"
},
{
"Nov"
,
"11"
},
{
"Dec"
,
"12"
}
};
//[[Rcpp::plugins(cpp11)]]
vector
<
string
>
split
(
string
s
,
string
delimiter
)
{
size_t
pos_start
=
0
,
pos_end
,
delim_len
=
delimiter
.
length
();
string
token
;
vector
<
string
>
res
;
while
((
pos_end
=
s
.
find
(
delimiter
,
pos_start
))
!=
string
::
npos
)
{
token
=
s
.
substr
(
pos_start
,
pos_end
-
pos_start
);
pos_start
=
pos_end
+
delim_len
;
res
.
push_back
(
token
);
}
res
.
push_back
(
s
.
substr
(
pos_start
));
return
res
;
}
std
::
string
dec2hex
(
int
i
)
{
stringstream
ss
;
ss
<<
hex
<<
uppercase
<<
i
;
return
ss
.
str
();
}
std
::
string
bitwNot
(
string
value
)
{
std
::
string
result
=
""
;
for
(
int
i
=
0
;
i
<
value
.
size
();
i
++
)
{
if
(
value
[
i
]
==
'0'
)
{
result
+=
"1"
;
}
else
{
result
+=
"0"
;
}
}
return
result
;
}
bool
is_number
(
const
std
::
string
&
s
)
{
return
!
s
.
empty
()
&&
std
::
all_of
(
s
.
begin
(),
s
.
end
(),
::
isdigit
);
}
std
::
string
decToBinary
(
int
n
)
{
if
(
n
<
0
){
// check if negative and alter the number
n
=
256
+
n
;
}
std
::
string
result
=
""
;
while
(
n
>
0
){
result
=
std
::
string
(
1
,
(
char
)
(
n
%
2
+
48
))
+
result
;
n
=
n
/
2
;
}
return
result
;
}
time_t
dateToint
(
std
::
string
v
)
{
struct
tm
tm
;
time_t
t
;
strptime
(
v
.
c_str
(),
"%Y-%m-%d %H:%M:%S"
,
&
tm
);
tm
.
tm_isdst
=
-
1
;
t
=
mktime
(
&
tm
);
return
t
;
}
std
::
string
intTodate
(
time_t
t
)
{
struct
tm
*
timeinfo
;
timeinfo
=
localtime
(
&
t
);
std
::
string
year
=
std
::
to_string
(
timeinfo
->
tm_year
+
1900
);
std
::
string
month
=
std
::
to_string
(
timeinfo
->
tm_mon
+
1
);
month
=
(
month
.
size
()
==
1
)
?
"0"
+
month
:
month
;
std
::
string
day
=
std
::
to_string
(
timeinfo
->
tm_mday
);
day
=
(
day
.
size
()
==
1
)
?
"0"
+
day
:
day
;
std
::
string
hour
=
std
::
to_string
(
timeinfo
->
tm_hour
);
hour
=
(
hour
.
size
()
==
1
)
?
"0"
+
hour
:
hour
;
std
::
string
min
=
std
::
to_string
(
timeinfo
->
tm_min
);
min
=
(
min
.
size
()
==
1
)
?
"0"
+
min
:
min
;
std
::
string
sec
=
std
::
to_string
(
timeinfo
->
tm_sec
);
sec
=
(
sec
.
size
()
==
1
)
?
"0"
+
sec
:
sec
;
std
::
string
result
=
year
+
"-"
+
month
+
"-"
+
day
+
" "
+
hour
+
":"
+
min
+
":"
+
sec
;
return
result
;
}
// [[Rcpp::export]]
void
CPPparseAcceleration
(
std
::
string
data_path
,
std
::
string
output_path
,
StringVector
mandrill_uuid
,
std
::
string
timeS
,
std
::
string
timeE
)
{
std
::
ofstream
output
(
output_path
);
output
<<
"
\"
date
\"
;
\"
uuid
\"
;
\"
x
\"
;
\"
y
\"
;
\"
z
\"
"
<<
endl
;
std
::
ifstream
input
(
data_path
);
for
(
std
::
string
line
;
getline
(
input
,
line
);
)
{
vector
<
string
>
vline
=
split
(
line
,
"_"
);
int
size
=
vline
.
size
();
bool
exact
=
true
;
if
(
size
>=
23
)
{
exact
=
true
;
}
else
if
(
size
==
1
)
{
vline
=
split
(
line
,
","
);
size
=
vline
.
size
();
if
(
size
>=
23
)
{
exact
=
true
;
}
}
if
(
exact
)
{
//Parse date
std
::
string
sdate
=
vline
[
0
];
std
::
vector
<
string
>
dline
=
split
(
line
,
" "
);
if
(
dline
.
size
()
<
5
)
{
continue
;
}
std
::
string
month
=
months
[
dline
[
1
]];
std
::
string
realDate
=
dline
[
3
]
+
"-"
+
month
+
"-"
+
dline
[
2
]
+
" "
+
dline
[
4
];
//Parse uuid
std
::
string
uuid
=
vline
[
3
];
bool
find
=
false
;
for
(
int
i
=
0
;
i
<
mandrill_uuid
.
size
();
i
++
){
if
(
mandrill_uuid
[
i
]
==
uuid
)
{
find
=
true
;
}
}
int
sizeAcceleration
=
std
::
stoi
(
vline
[
19
]);
if
(
sizeAcceleration
<=
100
)
{
find
=
false
;
}
if
(
find
)
{
//Parse acceleration
time_t
t
=
dateToint
(
realDate
);
time_t
ts
=
dateToint
(
timeS
);
time_t
te
=
dateToint
(
timeE
);
if
(
t
>=
ts
)
{
if
(
t
<=
te
)
{
time_t
t2
=
t
;
int
index_element
=
21
;
int
group
=
sizeAcceleration
/
3
;
for
(
int
i
=
21
;
i
<
(
group
+
21
);
i
++
){
std
::
string
sx
=
vline
[
index_element
];
std
::
string
sy
=
vline
[
index_element
+
1
];
std
::
string
sz
=
vline
[
index_element
+
2
];
index_element
+=
3
;
if
(
is_number
(
sx
)
&&
is_number
(
sy
)
&&
is_number
(
sz
))
{
int
ix
=
std
::
stoi
(
sx
);
int
iy
=
std
::
stoi
(
sy
);
int
iz
=
std
::
stoi
(
sz
);
if
(
ix
==
0
&&
iy
==
0
&&
iz
==
0
)
{
// nothing to do
}
else
{
if
(
t2
>=
ts
)
{
if
(
t2
<=
te
)
{
std
::
string
sacceleration
=
std
::
to_string
(
ix
)
+
";"
+
std
::
to_string
(
iy
)
+
";"
+
std
::
to_string
(
iz
);
std
::
string
newdate
=
intTodate
(
t2
);
std
::
string
result
=
newdate
+
";
\"
"
+
uuid
+
"
\"
;"
+
sacceleration
;
output
<<
result
<<
endl
;
}
else
{
return
;
}
}
}
t2
+=
1
;
}
}
}
else
{
return
;
}
}
}
}
}
}
// [[Rcpp::export]]
void
CPPparseMouvement
(
std
::
string
data_path
,
std
::
string
output_path
,
StringVector
mandrill_uuid
,
std
::
string
timeS
,
std
::
string
timeE
)
{
std
::
ofstream
output
(
output_path
);
output
<<
"
\"
date
\"
;
\"
uuid
\"
;
\"
mouvement
\"
"
<<
endl
;
std
::
ifstream
input
(
data_path
);
for
(
std
::
string
line
;
getline
(
input
,
line
);
)
{
vector
<
string
>
vline
=
split
(
line
,
"_"
);
int
size
=
vline
.
size
();
bool
exact
=
true
;
if
(
size
>=
23
)
{
exact
=
true
;
}
else
if
(
size
==
1
)
{
vline
=
split
(
line
,
","
);
size
=
vline
.
size
();
if
(
size
>=
23
)
{
exact
=
true
;
}
}
if
(
exact
)
{
//Parse date
std
::
string
sdate
=
vline
[
0
];
std
::
vector
<
string
>
dline
=
split
(
line
,
" "
);
if
(
dline
.
size
()
<
5
)
{
continue
;
}
std
::
string
month
=
months
[
dline
[
1
]];
std
::
string
realDate
=
dline
[
3
]
+
"-"
+
month
+
"-"
+
dline
[
2
]
+
" "
+
dline
[
4
];
//Parse uuid
std
::
string
uuid
=
vline
[
3
];
bool
find
=
false
;
for
(
int
i
=
0
;
i
<
mandrill_uuid
.
size
();
i
++
){
if
(
mandrill_uuid
[
i
]
==
uuid
)
{
find
=
true
;
}
}
int
sizeMouvement
=
std
::
stoi
(
vline
[
19
]);
if
(
sizeMouvement
<=
100
)
{
find
=
false
;
}
if
(
find
)
{
//Parse mouvement
int
mouvement
=
0
;
time_t
t
=
dateToint
(
realDate
);
time_t
ts
=
dateToint
(
timeS
);
time_t
te
=
dateToint
(
timeE
);
if
(
t
>=
ts
)
{
if
(
t
<=
te
)
{
time_t
t2
=
t
;
for
(
int
i
=
22
;
i
<
(
sizeMouvement
+
22
);
i
++
){
if
(
is_number
(
vline
[
i
]))
{
int
value
=
std
::
stoi
(
vline
[
i
]);
mouvement
=
value
;
if
(
t2
>=
ts
)
{
if
(
t2
<=
te
)
{
std
::
string
smouv
=
std
::
to_string
(
mouvement
);
std
::
string
newdate
=
intTodate
(
t2
);
std
::
string
result
=
newdate
+
";
\"
"
+
uuid
+
"
\"
;"
+
smouv
;
output
<<
result
<<
endl
;
}
else
{
return
;
}
}
t2
+=
1
;
}
}
}
else
{
return
;
}
}
}
}
}
}
// [[Rcpp::export]]
void
CPPparseTemperature
(
std
::
string
data_path
,
std
::
string
output_path
,
StringVector
mandrill_uuid
,
std
::
string
timeS
,
std
::
string
timeE
)
{
std
::
ofstream
output
(
output_path
);
output
<<
"
\"
date
\"
;
\"
uuid
\"
;
\"
temperature
\"
"
<<
endl
;
std
::
ifstream
input
(
data_path
);
for
(
std
::
string
line
;
getline
(
input
,
line
);
)
{
//std::cout << line << std::endl;
vector
<
string
>
vline
=
split
(
line
,
"_"
);
int
size
=
vline
.
size
();
bool
exact
=
true
;
if
(
size
>=
24
&&
size
<=
26
)
{
if
(
vline
[
16
]
!=
"undefined"
)
{
exact
=
true
;
}
}
else
if
(
size
==
1
)
{
vline
=
split
(
line
,
","
);
size
=
vline
.
size
();
if
(
size
>=
124
)
{
if
(
vline
[
16
]
!=
"undefined"
)
{
exact
=
true
;
}
}
}
if
(
exact
)
{
//Parse date
std
::
string
sdate
=
vline
[
0
];
std
::
vector
<
string
>
dline
=
split
(
line
,
" "
);
if
(
dline
.
size
()
<
5
)
{
continue
;
}
std
::
string
month
=
months
[
dline
[
1
]];
std
::
string
realDate
=
dline
[
3
]
+
"-"
+
month
+
"-"
+
dline
[
2
]
+
" "
+
dline
[
4
];
//Parse uuid
std
::
string
uuid
=
vline
[
3
];
bool
find
=
false
;
for
(
int
i
=
0
;
i
<
mandrill_uuid
.
size
();
i
++
){
if
(
mandrill_uuid
[
i
]
==
uuid
)
{
find
=
true
;
}
}
if
(
find
)
{
//Parse temperature
int
df1
=
std
::
stoi
(
vline
[
22
]);
int
df2
=
std
::
stoi
(
vline
[
23
]);
std
::
string
hdf1
=
dec2hex
(
df1
);
std
::
string
hdf2
=
dec2hex
(
df2
);
std
::
string
C0
=
hdf2
;
if
(
df2
<
16
)
{
C0
=
"0"
+
C0
;
}
std
::
string
HC
=
hdf1
+
C0
;
unsigned
long
dh1
=
std
::
stoul
(
HC
,
nullptr
,
16
);
unsigned
long
dh2
=
dh1
;
std:
string
dh
=
dec2hex
(
dh2
);
int
sizeDH
=
dh
.
size
();
if
(
sizeDH
<
4
)
{
dh
=
std
::
string
(
4
-
sizeDH
,
'0'
)
+
dh
;
}
std
::
string
vh1
=
dh
.
substr
(
0
,
2
);
std
::
string
vh2
=
dh
.
substr
(
2
,
3
);
std
::
string
hb1
=
decToBinary
(
std
::
stoi
(
vh1
,
nullptr
,
16
));
std
::
string
hb2
=
decToBinary
(
std
::
stoi
(
vh2
,
nullptr
,
16
));
unsigned
long
sizeHB1
=
hb1
.
size
();
unsigned
long
sizeHB2
=
hb2
.
size
();
if
(
sizeHB1
<
8
)
{
hb1
=
std
::
string
(
8
-
sizeHB1
,
'0'
)
+
hb1
;
}
if
(
sizeHB2
<
8
)
{
hb2
=
std
::
string
(
8
-
sizeHB2
,
'0'
)
+
hb2
;
}
std
::
string
tmpbv
=
hb1
+
hb2
;
std
::
string
bv
=
tmpbv
.
substr
(
4
,
15
);
std
::
string
bv2
=
bitwNot
(
bv
);
int
sizeBV
=
bv2
.
size
();
std
::
string
bv3
=
bv2
.
substr
(
sizeBV
-
11
-
1
,
sizeBV
-
1
);
double
temperature
=
0.0
;
if
(
bv3
[
0
]
==
'0'
)
{
temperature
=
0.0
;
}
else
{
temperature
=
(
double
)
dh1
/
10.0
;
}
if
(
temperature
>
0
)
{
std
::
string
stemp
=
std
::
to_string
(
temperature
);
std
::
string
result
=
realDate
+
";
\"
"
+
uuid
+
"
\"
;"
+
stemp
.
substr
(
0
,
stemp
.
find
(
"."
)
+
2
);
time_t
t
=
dateToint
(
realDate
);
time_t
ts
=
dateToint
(
timeS
);
time_t
te
=
dateToint
(
timeE
);
if
(
t
>=
ts
)
{
if
(
t
<=
te
)
{
output
<<
result
<<
endl
;
}
else
{
return
;
}
}
}
}
}
}
}
main.R
View file @
dab7bdf8
library
(
dplyr
)
library
(
broman
)
library
(
randomcoloR
)
library
(
openxlsx
)
library
(
Rcpp
)
source
(
"tools.R"
)
sourceCpp
(
"exstract.cpp"
)
#--------------------------------------------------
# GLOBAL VARIABLE
#--------------------------------------------------
root_path
<-
"./data/2018-11-26au02"
# Le range de départ de la période à extraire
timeS
<-
"2018-11-26 12:00:00"
...
...
@@ -15,38 +24,40 @@ timeE <- "2018-11-27 11:59:59"
# timeE <- ""
# Les uuid des mandrill ici 1 mandrill
mandrill_uuid
<-
c
(
"32-38-31-39-58-36-8e-0c"
)
#
mandrill_uuid <- c("32-38-31-39-58-36-8e-0c")
# ici avec 2 mandrills
#mandrill_uuid <- c("32-38-31-39-58-36-8e-0c", "32-38-31-39-62-36-8f-0c")
mandrill_uuid
<-
c
(
"32-38-31-39-58-36-8e-0c"
)
# Si le vecteur est vide cela signifie qu'on prend tout les mandrills
#mandrill_uuid <- c()
# Si le vecteur est vide cela signifie qu'on ne prend aucun mandrills
# Les chemins des 3 fichiers
path_temperature
<-
"./2018-11-26au02/logLoRaTemp.csv"
path_acceleration
<-
"./2018-11-26au02/logLoRaAcc.csv"
path_mouvement
<-
"./2018-11-26au02/logLoRaMov.csv"
#--------------------------------------------------
# Temperature mandrill
#--------------------------------------------------
path_temperature
<-
paste0
(
root_path
,
"/logLoRaTemp.csv"
)
preTraitementTemperature
(
root_path
,
path_temperature
,
mandrill_uuid
,
timeS
,
timeE
)
mandrill_temperature
<-
readCSVTemperature
(
paste0
(
root_path
,
"/temperature.csv"
))
data_t
emperature
<-
readLines
(
path_temperature
)
extT
emperature
<-
extractExtTemperature
(
paste0
(
root_path
,
"/temperature_ext.xlsx"
),
timeS
,
timeE
)
mandrill_temperature
<-
extractTemperature
(
data_temperature
,
mandrill_uuid
,
timeS
,
timeE
)
uuid
<-
unique
(
mandrill_temperature
$
uuid
)
minT
<-
min
(
mandrill_temperature
$
temperature
,
extTemperature
$
temperature
)
maxT
<-
max
(
mandrill_temperature
$
temperature
,
extTemperature
$
temperature
)
mandrillT
<-
filter
(
mandrill_temperature
,
uuid
==
uuid
[
1
])
uuid
<-
unique
(
mandrill_temperature
$
uuid
)
color_mandrill
<-
distinctColorPalette
(
length
(
uuid
))
color_mandrill
<-
distinctColorPalette
(
length
(
uuid
)
+1
)
if
(
length
(
uuid
)
==
1
)
{
color_mandrill
[
1
]
<-
"blue"
}
plot
(
mandrillT
$
date
,
mandrillT
$
temperature
,
type
=
"l"
,
col
=
color_mandrill
[
1
])
mandrill
<-
filter
(
mandrill_temperature
,
uuid
==
uuid
[
1
])
plot
(
mandrill
$
date
,
mandrill
$
temperature
,
type
=
"l"
,
col
=
"blue"
,
ylim
=
c
(
minT
,
maxT
),
main
=
"Temperature"
)
if
(
length
(
uuid
)
>
1
)
{
for
(
index
in
c
(
2
:
length
(
uuid
)))
{
...
...
@@ -55,93 +66,50 @@ if(length(uuid) > 1) {
}
}
lines
(
extTemperature
$
date
,
extTemperature
$
temperature
,
col
=
"red"
)
#--------------------------------------------------
# Mouvement mandrill
# Mouvement
& Acceleration
mandrill
#--------------------------------------------------
d
at
a
_mouvement
<-
readLines
(
path_mouvement
)
m
oyenne
_mouvement
<-
FALSE
p
at
h
_mouvement
<-
paste0
(
root_path
,
"/logLoRaMov.csv"
)
preTraitementMouvement
(
root_path
,
path_mouvement
,
mandrill_uuid
,
timeS
,
timeE
)
m
andrill
_mouvement
<-
readCSVMouvement
(
paste0
(
root_path
,
"/mouvement.csv"
))
mandrill_mouvement
<-
extractMouvement
(
data_mouvement
,
mandrill_uuid
,
timeS
,
timeE
,
moyenne_mouvement
)
path_acceleration
<-
paste0
(
root_path
,
"/logLoRaAcc.csv"
)
preTraitementAcceleration
(
root_path
,
path_acceleration
,
mandrill_uuid
,
timeS
,
timeE
)
mandrill_acceleration
<-
readCSVAcceleration
(
paste0
(
root_path
,
"/acceleration.csv"
))
uuid
<-
unique
(
mandrill_mouvement
$
uuid
)
mandrill
<-
filter
(
mandrill_mouvement
,
uuid
==
uuid
[
1
])
color_mandrill
<-
distinctColorPalette
(
length
(
uuid
))
if
(
length
(
uuid
)
==
1
)
{
color_mandrill
[
1
]
<-
"blue"
}
plot
(
mandrill
$
date
,
mandrill
$
mouvement
,
type
=
"l"
,
col
=
color_mandrill
[
1
])
if
(
length
(
uuid
)
>
1
)
{
for
(
index
in
c
(
2
:
length
(
uuid
)))
{
mandrill
<-
filter
(
mandrill_mouvement
,
uuid
==
uuid
[
index
])
lines
(
mandrill
$
date
,
mandrill
$
mouvement
,
col
=
color_mandrill
[
index
])
}
}
#--------------------------------------------------
# Acceleration mandrill
#--------------------------------------------------
data_acceleration
<-
readLines
(
path_acceleration
)
minMA
<-
0
m
oyenne_acceleration
<-
FALSE
m
axMA
<-
255
mandrill_acceleration
<-
extractAcceleration
(
data_acceleration
,
mandrill_uuid
,
timeS
,
timeE
,
moyenne_acceleration
)
uuid
<-
unique
(
mandrill_acceleration
$
uuid
)
mandrill
<-
filter
(
mandrill_acceleration
,
uuid
==
uuid
[
1
])
color_mandrill
<-
distinctColorPalette
(
length
(
uuid
))
color_mandrill
<-
distinctColorPalette
(
length
(
uuid
)
+1
)
# Position X
plot
(
mandrill
$
date
,
mandrill
$
x
,
type
=
"l"
,
col
=
"blue"
)
if
(
length
(
uuid
)
>
1
)
{
for
(
index
in
c
(
2
:
length
(
uuid
)))
{
print
(
uuid
[
index
])
mandrill
<-
filter
(
mandrill_acceleration
,
uuid
==
uuid
[
index
])
lines
(
mandrill
$
date
,
mandrill
$
x
,
col
=
color_mandrill
[
index
])
print
(
nrow
(
mandrill
))
}
}
color_mandrill
<-
distinctColorPalette
(
length
(
uuid
))
mandrillM
<-
filter
(
mandrill_mouvement
,
uuid
==
uuid
[
1
])
mandrillA
<-
filter
(
mandrill_acceleration
,
uuid
==
uuid
[
1
])
par
(
mfrow
=
c
(
2
,
1
))
plot
(
mandrillM
$
date
,
mandrillM
$
mouvement
,
type
=
"l"
,
col
=
"purple"
,
ylim
=
c
(
minMA
,
maxMA
),
main
=
"Mouvement"
)
plot
(
mandrillA
$
date
,
mandrillA
$
x
,
type
=
"l"
,
col
=
"blue"
,
ylim
=
c
(
minMA
,
maxMA
),
main
=
"Acceleration X"
)
# Position Y