Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into issue_35
Browse files Browse the repository at this point in the history
  • Loading branch information
jura-gresko committed Mar 21, 2020
2 parents e9d956e + f732e26 commit d40632b
Show file tree
Hide file tree
Showing 40 changed files with 2,873 additions and 230 deletions.
15 changes: 8 additions & 7 deletions examples/distance_examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ include_directories(
${PROJECT_SOURCE_DIR}/../../3rdparty/jpeglib
)

include_directories(${Boost_INCLUDE_DIRS})
include_directories(${Boost_INCLUDE_DIRS})

file(COPY "assets" DESTINATION ".")

file(GLOB EXAMPLE_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)

find_package(JPEG)
#Run through each source
foreach(exampleSrc ${EXAMPLE_SRCS})

Expand Down Expand Up @@ -47,12 +47,13 @@ foreach(exampleSrc ${EXAMPLE_SRCS})
endforeach(exampleSrc)

if(UNIX)
FIND_PACKAGE(JPEG REQUIRED)
target_link_libraries(earth_mover_distance_example ${JPEG_LIBRARY})
target_link_libraries(earth_mover_distance_2_example ${JPEG_LIBRARY})
target_link_libraries(earth_mover_distance_example JPEG::JPEG)
target_link_libraries(earth_mover_distance_2_example JPEG::JPEG)
# target_link_libraries(earth_mover_distance_example /usr/local/lib/libjpeg.a)
# target_link_libraries(earth_mover_distance_2_example /usr/local/lib/libjpeg.a)
endif(UNIX)

if(CMAKE_SYSTEM_NAME MATCHES Windows)
target_link_libraries(earth_mover_distance_example ${PROJECT_SOURCE_DIR}/../../3rdparty/jpeglib/Release/jpeg.lib)
target_link_libraries(earth_mover_distance_2_example ${PROJECT_SOURCE_DIR}/../../3rdparty/jpeglib/Release/jpeg.lib)
target_link_libraries(earth_mover_distance_2_example ${PROJECT_SOURCE_DIR}/../../3rdparty/jpeglib/Release/jpeg.lib)
endif()
62 changes: 62 additions & 0 deletions examples/distance_examples/cramervon_mises_distance_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (c) 2019 Panda Team
*/

#include <vector>
#include <iostream>
#include <chrono>
#include "../../modules/distance.hpp"
#include "assets/test_data.cpp"


int main()
{
/******************** examples for Cramer-von Nises Distance **************************/
// example for picture
std::cout << "Cramer-von Nises distance example have started" << std::endl;
std::cout << "" << std::endl;

/*** here are some data records ***/
std::vector<double> samples_1 = { 0, 1, 2, 3, 3, 2, 1, 0, 2, 2 };
std::vector<double> samples_2 = { 0, 0, 2, 3, 3, 2, 1, 0, 2, 2 };

metric::CramervonNises<std::vector<double>, double> distance_1;

auto t1 = std::chrono::steady_clock::now();
auto result = distance_1(samples_1, samples_2);
auto t2 = std::chrono::steady_clock::now();
std::cout << "result: " << result
<< " (Time = " << double(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count()) / 1000000
<< " s)" << std::endl;
std::cout << "" << std::endl;

//

metric::CramervonNises<std::vector<double>, double> distance_2(0.0001);

t1 = std::chrono::steady_clock::now();
result = distance_2(samples_1, samples_2);
t2 = std::chrono::steady_clock::now();
std::cout << "result: " << result
<< " (Time = " << double(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count()) / 1000000
<< " s)" << std::endl;
std::cout << "" << std::endl;

//

metric::CramervonNises<std::vector<double>, double> distance_3(0.1);

t1 = std::chrono::steady_clock::now();
result = distance_3(samples_1, samples_2);
t2 = std::chrono::steady_clock::now();
std::cout << "result: " << result
<< " (Time = " << double(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count()) / 1000000
<< " s)" << std::endl;
std::cout << "" << std::endl;

return 0;
}
2 changes: 1 addition & 1 deletion examples/distance_examples/entropy_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ int main() {
std::cout << "Variation of Information, normalized Variation of Information:" << std::endl;

std::cout << "VOI = " << metric::variationOfInformation(v1, v2) << std::endl;
std::cout << "VOI (Manhatten) = " << metric::variationOfInformation<double, metric::Manhatten<double>>(v1, v2) << std::endl;
std::cout << "VOI (Manhatten) = " << metric::variationOfInformation<std::vector<std::vector<double>>, metric::Manhatten<double>>(v1, v2) << std::endl;
std::cout << "VOI norm = " << metric::variationOfInformation_normalized(v1, v2) << std::endl;

// functor
Expand Down
40 changes: 40 additions & 0 deletions examples/distance_examples/kolmogorov_smirnov_distance_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (c) 2019 Panda Team
*/

#include <vector>
#include <iostream>
#include <chrono>
#include "../../modules/distance.hpp"
#include "assets/test_data.cpp"


int main()
{
/******************** examples for Kolmogorov-Smirnov Distance **************************/
// example for picture
std::cout << "Kolmogorov-Smirnov distance example have started" << std::endl;
std::cout << "" << std::endl;

/*** here are some data records ***/
std::vector<double> samples_1 = { 0, 1, 2, 3, 3, 2, 1, 0, 2, 2 };
std::vector<double> samples_2 = { 0, 0, 2, 3, 3, 2, 1, 0, 2, 2 };
//std::vector<double> samples_1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//std::vector<double> samples_2 = { 1, 2, 3, 4, 5 };
//
metric::KolmogorovSmirnov<std::vector<double>, double> distance;

auto t1 = std::chrono::steady_clock::now();
auto result1 = distance(samples_1, samples_2);
auto t2 = std::chrono::steady_clock::now();
std::cout << "result: " << result1
<< " (Time = " << double(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count()) / 1000000
<< " s)" << std::endl;
std::cout << "" << std::endl;

return 0;
}
76 changes: 76 additions & 0 deletions examples/distance_examples/random_emd_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (c) 2019 Panda Team
*/

#include <vector>
#include <iostream>
#include <chrono>
#include "../../modules/distance.hpp"
#include "assets/test_data.cpp"


int main()
{
/******************** examples for Earth Mover’s Distance **************************/
// example for picture
std::cout << "Earth Mover's distance example have started" << std::endl;
std::cout << "" << std::endl;

/*** here are some data records ***/
std::vector<double> samples_1 = { 0, 1, 2, 3, 3, 2, 1, 0, 2, 2 };
std::vector<double> samples_2 = { 0, 0, 2, 3, 3, 2, 1, 0, 2, 2 };

metric::RandomEMD<std::vector<double>, double> distance_1;


//
// typedef int emd_Type;

// auto cost_mat = metric::EMD_details::ground_distance_matrix_of_2dgrid<emd_Type>(3, 3);
// auto maxCost = metric::EMD_details::max_in_distance_matrix(cost_mat);

// metric::EMD<emd_Type> distance_orig(cost_mat, maxCost);

//print_matrix(cost_mat);




auto t1 = std::chrono::steady_clock::now();
auto result = distance_1(samples_1, samples_2);
auto t2 = std::chrono::steady_clock::now();
std::cout << "result: " << result
<< " (Time = " << double(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count()) / 1000000
<< " s)" << std::endl;
std::cout << "" << std::endl;

//

metric::RandomEMD<std::vector<double>, double> distance_2(0.0001);

t1 = std::chrono::steady_clock::now();
result = distance_2(samples_1, samples_2);
t2 = std::chrono::steady_clock::now();
std::cout << "result: " << result
<< " (Time = " << double(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count()) / 1000000
<< " s)" << std::endl;
std::cout << "" << std::endl;

//

metric::RandomEMD<std::vector<double>, double> distance_3(0.1);

t1 = std::chrono::steady_clock::now();
result = distance_3(samples_1, samples_2);
t2 = std::chrono::steady_clock::now();
std::cout << "result: " << result
<< " (Time = " << double(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count()) / 1000000
<< " s)" << std::endl;
std::cout << "" << std::endl;

return 0;
}
6 changes: 3 additions & 3 deletions examples/space_examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ project(space_examples)
file(GLOB EXAMPLE_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)


include_directories(${Boost_INCLUDE_DIRS})
include_directories(${Boost_INCLUDE_DIRS})

# for Visual Studio
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")
endif(MSVC)
endif(MSVC)

#Run through each source
foreach(exampleSrc ${EXAMPLE_SRCS})
Expand All @@ -22,7 +22,7 @@ foreach(exampleSrc ${EXAMPLE_SRCS})
get_filename_component(exampleName ${exampleSrc} NAME_WE)

#Add compile target
add_executable(${exampleName} ${exampleSrc})
add_executable(${exampleName} ${exampleSrc})


#link to Boost libraries AND your targets and dependencies
Expand Down
102 changes: 102 additions & 0 deletions examples/space_examples/knn_graph_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (c) 2019 Panda Team
*/

#include <iostream>
#include "../../modules/utils/graph.hpp"
#include "../../modules/distance.hpp"


int main()
{
std::cout << "Graph space example have started" << std::endl;
std::cout << "" << std::endl;

size_t neighbors_num = 3;

std::vector<std::vector<double>> table = {
{ 0, 1 },
{ 1, 1 },
{ 2, 2 },
{ 3, 3 },
{ 4, 3 },
{ 5, 3 },
{ 4, 6 },
{ 5, 1 },
{ 4, 1 },
{ 3, 2 },
{ 0, 3 },
{ 1, 3 },
{ 2, 3 },
{ 6, 6 },
{ 0, 2 },
{ 0, 9 },
{ 0, 4 },
{ 0, 5 },
{ 0, 6 },
{ 0, 7 },
{ 0, 8 },
};

auto g = metric::KNNGraph<std::vector<double>, metric::Euclidian<double>>(table, neighbors_num, 2.5 * neighbors_num);

std::cout << "graph:" << std::endl;
std::cout << g.get_matrix() << std::endl;

//

std::vector<double> query = { 7, 5 };

std::cout << std::endl;
std::cout << "gnn search:" << std::endl;
auto found = g.gnnn_search(query, 3);

for (size_t i = 0; i < found.size(); i++)
{
std::cout << found[i] << " -> ";
print_vector(g.get_node_data(found[i]));
std::cout << std::endl;
}
std::cout << std::endl;

//

auto other_g = metric::KNNGraph(g);

std::cout << "other graph:" << std::endl;
std::cout << "gnn search:" << std::endl;
found = other_g.gnnn_search(query, 3);

for (size_t i = 0; i < found.size(); i++)
{
std::cout << found[i] << " -> ";
print_vector(other_g.get_node_data(found[i]));
std::cout << std::endl;
}
std::cout << std::endl;

//

// metric::Tree<std::vector<double>, metric::Euclidian<double>> tree(table);
//
// auto graph_from_tree = metric::KNNGraph(tree, neighbors_num, 2.5 * neighbors_num);
//
// std::cout << "tree graph:" << std::endl;
// std::cout << "gnn search:" << std::endl;
//found = graph_from_tree.gnnn_search(query, 3);
//
//for (size_t i = 0; i < found.size(); i++)
//{
// std::cout << found[i] << " -> ";
// print_vector(graph_from_tree.get_node_data(found[i]));
// std::cout << std::endl;
//}
// std::cout << std::endl;


return 0;
}
3 changes: 3 additions & 0 deletions examples/space_examples/simple_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ int main()

cTree.print();

std::cout << std::endl;
std::cout << std::endl;

//

/*** batch insert ***/
Expand Down
4 changes: 4 additions & 0 deletions modules/distance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Copyright (c) PANDA Team
#include "distance/k-structured/kohonen_distance.hpp"

#include "distance/k-related/L1.hpp"

#include "distance/k-random/VOI.hpp"
#include "distance/k-random/KolmogorovSmirnov.hpp"
#include "distance/k-random/RandomEMD.hpp"
#include "distance/k-random/CramervonNises.hpp"

#endif //_METRIC_DISTANCE_HPP
Loading

0 comments on commit d40632b

Please sign in to comment.