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
rapid-mix
RapidLib
Commits
fac8881e
Commit
fac8881e
authored
Sep 22, 2017
by
mzed
Browse files
calculating costs in label groups or between label groups
parent
6f4ab7a3
Changes
3
Hide whitespace changes
Inline
Side-by-side
RapidAPI/RapidAPI/main.cpp
View file @
fac8881e
...
...
@@ -249,6 +249,29 @@ int main(int argc, const char * argv[]) {
assert
(
myDTW
.
getMaxLength
(
"second series"
)
==
4
);
assert
(
myDTW
.
getMinLength
(
"second series"
)
==
4
);
//costs inside of a series
tempSeries
=
{};
tempSeries
.
input
.
push_back
(
{
1.
,
5.1
}
);
tempSeries
.
input
.
push_back
(
{
2.
,
4.1
}
);
tempSeries
.
input
.
push_back
(
{
3.
,
3.1
}
);
tempSeries
.
input
.
push_back
(
{
4.
,
2.1
}
);
tempSeries
.
input
.
push_back
(
{
5.
,
1.1
}
);
tempSeries
.
label
=
"first series"
;
seriesVector
.
push_back
(
tempSeries
);
tempSeries
=
{};
tempSeries
.
input
.
push_back
(
{
1.3
,
5.1
}
);
tempSeries
.
input
.
push_back
(
{
2.3
,
4.1
}
);
tempSeries
.
input
.
push_back
(
{
3.3
,
3.1
}
);
tempSeries
.
input
.
push_back
(
{
4.3
,
2.1
}
);
tempSeries
.
input
.
push_back
(
{
5.3
,
1.1
}
);
tempSeries
.
label
=
"first series"
;
seriesVector
.
push_back
(
tempSeries
);
myDTW
.
train
(
seriesVector
);
std
::
cout
<<
"mincost "
<<
myDTW
.
calculateCosts
(
"first series"
).
min
<<
std
::
endl
;
std
::
cout
<<
"maxcost "
<<
myDTW
.
calculateCosts
(
"first series"
).
max
<<
std
::
endl
;
/*
fastDTW fastDtw;
std::cout << "fast one-two cost " << fastDtw.getCost(seriesOne, seriesTwo, 1) << std::endl;
...
...
src/seriesClassification.cpp
View file @
fac8881e
...
...
@@ -12,6 +12,8 @@
#include "emscripten/seriesClassificationEmbindings.h"
#endif
#define SEARCH_RADIUS 1
seriesClassification
::
seriesClassification
()
{};
seriesClassification
::~
seriesClassification
()
{};
...
...
@@ -32,7 +34,7 @@ bool seriesClassification::train(const std::vector<trainingSeries> &seriesSet) {
maxLength
=
newLength
;
}
//Per Label
std
::
map
<
std
::
string
,
lengths
>::
iterator
it
=
lengthsPerLabel
.
find
(
allTrainingSeries
[
i
].
label
);
std
::
map
<
std
::
string
,
minMax
<
int
>
>::
iterator
it
=
lengthsPerLabel
.
find
(
allTrainingSeries
[
i
].
label
);
if
(
it
!=
lengthsPerLabel
.
end
())
{
int
newLength
=
int
(
allTrainingSeries
[
i
].
input
.
size
());
if
(
newLength
<
it
->
second
.
min
)
{
...
...
@@ -42,7 +44,7 @@ bool seriesClassification::train(const std::vector<trainingSeries> &seriesSet) {
it
->
second
.
max
=
newLength
;
}
}
else
{
lengths
tempLengths
;
minMax
<
int
>
tempLengths
;
tempLengths
.
min
=
tempLengths
.
max
=
int
(
allTrainingSeries
[
i
].
input
.
size
());
lengthsPerLabel
[
allTrainingSeries
[
i
].
label
]
=
tempLengths
;
}
...
...
@@ -60,14 +62,13 @@ void seriesClassification::reset() {
std
::
string
seriesClassification
::
run
(
const
std
::
vector
<
std
::
vector
<
double
>>
&
inputSeries
)
{
fastDTW
fastDtw
;
int
searchRadius
=
1
;
//TODO: Define this properly, elsewhere?
int
closestSeries
=
0
;
allCosts
.
clear
();
double
lowestCost
=
fastDtw
.
getCost
(
inputSeries
,
allTrainingSeries
[
0
].
input
,
searchRadius
);
double
lowestCost
=
fastDtw
.
getCost
(
inputSeries
,
allTrainingSeries
[
0
].
input
,
SEARCH_RADIUS
);
allCosts
.
push_back
(
lowestCost
);
for
(
int
i
=
1
;
i
<
allTrainingSeries
.
size
();
++
i
)
{
double
currentCost
=
fastDtw
.
getCost
(
inputSeries
,
allTrainingSeries
[
i
].
input
,
searchRadius
);
double
currentCost
=
fastDtw
.
getCost
(
inputSeries
,
allTrainingSeries
[
i
].
input
,
SEARCH_RADIUS
);
allCosts
.
push_back
(
currentCost
);
if
(
currentCost
<
lowestCost
)
{
lowestCost
=
currentCost
;
...
...
@@ -88,7 +89,7 @@ int seriesClassification::getMinLength() {
int
seriesClassification
::
getMinLength
(
std
::
string
label
)
{
int
labelMinLength
=
-
1
;
std
::
map
<
std
::
string
,
lengths
>::
iterator
it
=
lengthsPerLabel
.
find
(
label
);
std
::
map
<
std
::
string
,
minMax
<
int
>
>::
iterator
it
=
lengthsPerLabel
.
find
(
label
);
if
(
it
!=
lengthsPerLabel
.
end
())
{
labelMinLength
=
it
->
second
.
min
;
}
...
...
@@ -101,13 +102,77 @@ int seriesClassification::getMaxLength() {
int
seriesClassification
::
getMaxLength
(
std
::
string
label
)
{
int
labelMaxLength
=
-
1
;
std
::
map
<
std
::
string
,
lengths
>::
iterator
it
=
lengthsPerLabel
.
find
(
label
);
std
::
map
<
std
::
string
,
minMax
<
int
>
>::
iterator
it
=
lengthsPerLabel
.
find
(
label
);
if
(
it
!=
lengthsPerLabel
.
end
())
{
labelMaxLength
=
it
->
second
.
max
;
}
return
labelMaxLength
;
}
seriesClassification
::
minMax
<
double
>
seriesClassification
::
calculateCosts
(
std
::
string
label
)
{
minMax
<
double
>
calculatedMinMax
;
calculatedMinMax
.
min
=
std
::
numeric_limits
<
double
>::
max
();
calculatedMinMax
.
max
=
std
::
numeric_limits
<
double
>::
min
();
fastDTW
fastDTW
;
int
numSeries
=
0
;
for
(
int
i
=
0
;
i
<
(
allTrainingSeries
.
size
()
-
1
);
++
i
)
{
//these loops are a little different than the two-label case
if
(
allTrainingSeries
[
i
].
label
==
label
)
{
for
(
int
j
=
(
i
+
1
);
j
<
allTrainingSeries
.
size
();
++
j
)
{
if
(
allTrainingSeries
[
j
].
label
==
label
)
{
numSeries
++
;
double
currentCost
=
fastDTW
.
getCost
(
allTrainingSeries
[
i
].
input
,
allTrainingSeries
[
j
].
input
,
SEARCH_RADIUS
);
if
(
numSeries
==
1
)
{
calculatedMinMax
.
min
=
calculatedMinMax
.
max
=
currentCost
;
//first match is both min and max
}
else
{
if
(
currentCost
<
calculatedMinMax
.
min
)
{
calculatedMinMax
.
min
=
currentCost
;
}
if
(
currentCost
>
calculatedMinMax
.
max
)
{
calculatedMinMax
.
max
=
currentCost
;
}
}
}
}
}
}
if
(
numSeries
==
0
)
{
calculatedMinMax
.
min
=
calculatedMinMax
.
max
=
0
;
}
return
calculatedMinMax
;
}
seriesClassification
::
minMax
<
double
>
seriesClassification
::
calculateCosts
(
std
::
string
label1
,
std
::
string
label2
)
{
minMax
<
double
>
calculatedMinMax
;
calculatedMinMax
.
min
=
std
::
numeric_limits
<
double
>::
max
();
calculatedMinMax
.
max
=
std
::
numeric_limits
<
double
>::
min
();
fastDTW
fastDTW
;
int
numSeries
=
0
;
for
(
int
i
=
0
;
i
<
(
allTrainingSeries
.
size
());
++
i
)
{
if
(
allTrainingSeries
[
i
].
label
==
label1
)
{
for
(
int
j
=
0
;
j
<
allTrainingSeries
.
size
();
++
j
)
{
if
(
allTrainingSeries
[
j
].
label
==
label2
)
{
numSeries
++
;
double
currentCost
=
fastDTW
.
getCost
(
allTrainingSeries
[
i
].
input
,
allTrainingSeries
[
j
].
input
,
SEARCH_RADIUS
);
if
(
numSeries
==
1
)
{
calculatedMinMax
.
min
=
calculatedMinMax
.
max
=
currentCost
;
//first match is both min and max
}
else
{
if
(
currentCost
<
calculatedMinMax
.
min
)
{
calculatedMinMax
.
min
=
currentCost
;
}
if
(
currentCost
>
calculatedMinMax
.
max
)
{
calculatedMinMax
.
max
=
currentCost
;
}
}
}
}
}
}
return
calculatedMinMax
;
}
//
//std::vector<double> seriesClassification::getCosts(const std::vector<trainingExample> &trainingSet) {
// run(trainingSet);
...
...
src/seriesClassification.h
View file @
fac8881e
...
...
@@ -31,17 +31,22 @@ public:
int
getMaxLength
();
int
getMaxLength
(
std
::
string
label
);
template
<
typename
T
>
struct
minMax
{
T
min
;
T
max
;
};
minMax
<
double
>
calculateCosts
(
std
::
string
label
);
minMax
<
double
>
calculateCosts
(
std
::
string
label1
,
std
::
string
label2
);
private:
std
::
vector
<
trainingSeries
>
allTrainingSeries
;
std
::
vector
<
double
>
allCosts
;
int
maxLength
;
int
minLength
;
struct
lengths
{
int
min
;
int
max
;
};
std
::
map
<
std
::
string
,
lengths
>
lengthsPerLabel
;
std
::
map
<
std
::
string
,
minMax
<
int
>
>
lengthsPerLabel
;
};
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment