Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

78 save array to text csv separator as parameter + License text to davis-one-header + more standart plotly Colorscales #113

Merged
merged 22 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 74 additions & 2 deletions Tests/ArrayCoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,75 @@
using std::string;
using std::vector;

TEST(ArrayCore, save_to_disk_2d) {
//! 2-dimensional array
int rows = 10;
int cols = 5;
int** vals2d = new int* [rows];
for (int i = 0; i < rows; ++i) {
vals2d[i] = new int[cols];
for (int j = 0; j < cols; ++j) {
vals2d[i][j] = i * cols + j;
}
}
bool result = dv::save(vals2d, rows, cols, "./data/test_saving_save_to_disk_2d.csv");
EXPECT_EQ(result, true);
}

TEST(ArrayCore, save_to_disk_pseudo_2d) {
//! 1-dimensional array that simulates a 2-dimensional
int rows = 10;
int cols = 5;
int* vals = new int[rows * cols];
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
vals[i * cols + j] = i * cols + j;
}
}
dv::configSaveToDisk conf;
conf.separatorOfCols = "*";
conf.separatorOfRows = "____";
bool result = dv::save(vals, rows, cols, "./data/test_saving_save_to_disk_pseudo_2d.csv", conf);
EXPECT_EQ(result, true);
}

TEST(ArrayCore, save_to_disk_1d) {
//! 1-dimensional array
int size = 10;
int* vals = new int[size];
for (int i = 0; i < size; ++i) {
vals[i] = i;
}
bool result = dv::save(vals, size, "./data/test_saving_save_to_disk_1d.csv");
EXPECT_EQ(result, true);
}

TEST(ArrayCore, save_to_disk_container1D) {
//! 1-dimensional container
vector<double> vec;
for (size_t i = 0; i < 10; ++i) {
vec.emplace_back(i * 1e-10);
}
bool result = dv::save(vec, "./data/test_saving_save_to_disk_container1D.csv");
EXPECT_EQ(result, true);
}

TEST(ArrayCore, save_to_disk_container2D) {
//! 2-dimensional container
int rows = 10;
int cols = 5;
vector<vector<int>> arr2;
for (int i = 0; i < rows; ++i) {
vector<int> vec(cols);
for (int j = 0; j < cols; ++j) {
vec[j] = i * cols + j;
}
arr2.emplace_back(vec);
}
bool result = dv::save(arr2, "./data/test_saving_save_to_disk_container2D.csv");
EXPECT_EQ(result, true);
}

TEST(ArrayCore, universal_1d_conteiner) {
EXPECT_EQ(dvs::isPlotlyScriptExists(), true);
std::list<double> vec = {5, 34};
Expand Down Expand Up @@ -42,7 +111,7 @@ TEST(ArrayCore, configurator) {
config.heatmap.xLabel = "Столбцы";
config.heatmap.yLabel = "Строки";
config.heatmap.title = "Тестовая матрица";
config.heatmap.colorSc = dv::COLORSCALE_GLAMOUR;
config.heatmap.colorSc = dv::COLORSCALE_YlGnBu;
bool result1 = dv::show(values, "HeatMap", config);
config.typeVisual = dv::VISUALTYPE_SURFACE;
config.surf.title = "This is Surface!!!";
Expand Down Expand Up @@ -129,7 +198,10 @@ TEST(ArrayCore, readAndShowMatrixFromFile) {
vector<vector<double>> values;
bool readRes = dvs::readMatrix(values, "./data/2023_07_19-12_59_31_379_Baumer2_text.csv", ';');
EXPECT_EQ(readRes, true);
bool result = dv::show(values, "readAndShowMatrixFromFile");
auto config = dv::Config();
config.heatmap.xLabel = "english";
config.heatmap.yLabel = "русский";
bool result = dv::show(values, "readAndShowMatrixFromFile", config);
EXPECT_EQ(result, true);
}

Expand Down
101 changes: 92 additions & 9 deletions array_core/array_core.h
Original file line number Diff line number Diff line change
@@ -1,47 +1,70 @@
#ifndef ARRAY_CORE_ARRAY_CORE_H_
#define ARRAY_CORE_ARRAY_CORE_H_
//#START_GRAB_TO_INCLUDES_LIST
#include <iostream>
#include <vector>
//#STOP_GRAB_TO_INCLUDES_LIST
#include "plotly_maker/plotly_maker.h"
#include "common_utils/common_utils.h"
#include "common_utils/common_constants.h"
#include "configurator.h"


namespace dv {
//#START_GRAB_TO_DV_NAMESPACE

using std::vector;
using std::string;


//! 2-dimensional array
template <typename T>
bool show(T** data, uint64_t arrRows, uint64_t arrCols,
const string& htmlPageName = dvs::kAppName, const Config& configuration = Config());

template <typename T>
bool save(T** data, uint64_t arrRows, uint64_t arrCols, const string& filename,
const configSaveToDisk& configuration = configSaveToDisk());

//! 1-dimensional array that simulates a 2-dimensional one (element access [i*cols+j])
template <typename T>
bool show(const T* data, uint64_t arrRows, uint64_t arrCols,
const string& htmlPageName = dvs::kAppName, const Config& configuration = Config());

template <typename T>
bool save(const T* data, uint64_t arrRows, uint64_t arrCols, const string& filename,
const configSaveToDisk& configuration = configSaveToDisk());

//! 1-dimensional array
template <typename T>
bool show(const T* data, uint64_t count, const string& htmlPageName = dvs::kAppName, const Config& configuration = Config());

template <typename T>
bool save(const T* data, uint64_t count, const string& filename, const configSaveToDisk& configuration = configSaveToDisk());

//! 1-dimensional container
template<typename C, //https://devblogs.microsoft.com/oldnewthing/20190619-00/?p=102599
typename T = std::decay_t<decltype(*begin(std::declval<C>()))>,
typename = std::enable_if_t<std::is_convertible_v<T, double>> >
bool show(C const& container, const string& htmlPageName = dvs::kAppName, const Config& configuration = Config());

//! 2-dimensional container
template<typename C,
typename T = std::decay_t<decltype(*begin(std::declval<C>()))>,
typename E = std::decay_t<decltype(*begin(std::declval<T>()))>,
typename = std::enable_if_t<std::is_convertible_v<E, double>> >
typename = std::enable_if_t<std::is_convertible_v<T, double>> >
bool save(C const& container, const string& filename, const configSaveToDisk& configuration = configSaveToDisk());

//! 2-dimensional container
template<typename C,
typename E = std::decay_t<decltype(*begin(std::declval<C>()))>,
typename T = std::decay_t<decltype(*begin(std::declval<E>()))>,
typename = std::enable_if_t<std::is_convertible_v<T, double>> >
bool show(C const& container_of_containers, const string& htmlPageName = dvs::kAppName, const Config& configuration = Config());

template<typename C,
typename E = std::decay_t<decltype(*begin(std::declval<C>()))>,
typename T = std::decay_t<decltype(*begin(std::declval<E>()))>,
typename = std::enable_if_t<std::is_convertible_v<T, double>> >
bool save(C const& container_of_containers, const string& filename, const configSaveToDisk& configuration = configSaveToDisk());

// ***********************************
// template functions implementations:
// ***********************************
Expand All @@ -63,6 +86,18 @@ bool show(T** data, uint64_t arrRows, uint64_t arrCols, const string& htmlPageNa
return res;
}

template <typename T>
bool save(T** data, uint64_t arrRows, uint64_t arrCols, const std::string& filename, const configSaveToDisk& configuration) {
vector<vector<T>> vecVec;
vecVec.reserve(arrRows);
for (uint64_t i = 0; i < arrRows; ++i) {
vector<T> row(&data[i][0], &data[i][0] + arrCols);
vecVec.emplace_back(row);
}
bool res = dvs::saveVecVec<T>(vecVec, filename, configuration);
return res;
}

template <typename T>
bool show(const T* data, uint64_t arrRows, uint64_t arrCols, const string& htmlPageName, const Config& configuration) {
vector<vector<double>> vecVecDbl;
Expand All @@ -80,6 +115,19 @@ bool show(const T* data, uint64_t arrRows, uint64_t arrCols, const string& htmlP
return res;
}

template <typename T>
bool save(const T* data, uint64_t arrRows, uint64_t arrCols, const string& filename,
const configSaveToDisk& configuration) {
vector<vector<T>> vecVec;
vecVec.reserve(arrRows);
for (uint64_t i = 0; i < arrRows; ++i) {
vector<T> row(&data[i * arrCols], &data[i * arrCols] + arrCols);
vecVec.emplace_back(row);
}
bool res = dvs::saveVecVec<T>(vecVec, filename, configuration);
return res;
}

template <typename T>
bool show(const T* data, uint64_t count, const string& htmlPageName, const Config& configuration) {
vector<double> dblRow(data, data + count);
Expand All @@ -90,10 +138,17 @@ bool show(const T* data, uint64_t count, const string& htmlPageName, const Confi
return res;
}

template <typename T>
bool save(const T* data, uint64_t count, const string& filename, const configSaveToDisk& configuration) {
vector<T> row(data, data + count);
bool res = dvs::saveVec<T>(row, filename, configuration);
return res;
}

template<typename C, typename T, typename>
bool show(C const& container, const string& htmlPageName, const Config& configuration) {
vector<double> dblRow(container.size());
int i = 0;
uint64_t i = 0;
for (auto v : container) {
dblRow[i] = v;
++i;
Expand All @@ -105,20 +160,31 @@ bool show(C const& container, const string& htmlPageName, const Config& configur
return res;
}

template<typename C, typename T, typename E, typename >
template<typename C, typename T, typename>
bool save(C const& container, const string& filename, const configSaveToDisk& configuration) {
vector<T> row(container.size());
uint64_t i = 0;
for (auto v : container) {
row[i] = v;
++i;
}
bool res = dvs::saveVec<T>(row, filename, configuration);
return res;
}

template<typename C, typename E, typename T, typename >
bool show(C const& container_of_containers, const string& htmlPageName, const Config& configuration) {
vector<vector<double>> vecVecDbl;
vecVecDbl.reserve(container_of_containers.size());
for (auto row : container_of_containers) {
vector<double> dblRow(row.size());
int i = 0;
uint64_t i = 0;
for (auto v : row) {
dblRow[i] = v;
++i;
}
vecVecDbl.emplace_back(dblRow);
}

bool res = false;
if (configuration.typeVisual == VISUALTYPE_AUTO ||
configuration.typeVisual == VISUALTYPE_HEATMAP) {
Expand All @@ -128,7 +194,24 @@ bool show(C const& container_of_containers, const string& htmlPageName, const Co
return res;
}

template<typename C, typename E, typename T, typename >
bool save(C const& container_of_containers, const string& filename, const configSaveToDisk& configuration) {
vector<vector<T>> vecVec;
vecVec.reserve(container_of_containers.size());
for (auto row : container_of_containers) {
vector<T> rowTemp(row.size());
uint64_t i = 0;
for (auto v : row) {
rowTemp[i] = v;
++i;
}
vecVec.emplace_back(rowTemp);
}
bool res = dvs::saveVecVec<T>(vecVec, filename, configuration);
return res;
}

//#STOP_GRAB_TO_DV_NAMESPACE
} // end namespace dvs
} // end namespace dv

#endif //ARRAY_CORE_ARRAY_CORE_H_
17 changes: 16 additions & 1 deletion array_core/configurator.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ enum config_colorscales {
COLORSCALE_SUNNY,
COLORSCALE_GLAMOUR,
COLORSCALE_THERMAL,
COLORSCALE_GRAYSCALE
COLORSCALE_GRAYSCALE,
COLORSCALE_YlGnBu,
COLORSCALE_JET,
COLORSCALE_HOT,
COLORSCALE_ELECTRIC,
COLORSCALE_PORTLAND
};


Expand Down Expand Up @@ -66,6 +71,16 @@ struct Config {
config_visualizationTypes typeVisual;
};

struct configSaveToDisk {
configSaveToDisk():
separatorOfRows("\n"),
separatorOfCols(";") {}
std::string separatorOfRows;
std::string separatorOfCols;
};



//#STOP_GRAB_TO_DV_NAMESPACE
}// end namespace dv

Expand Down
2 changes: 1 addition & 1 deletion common_utils/common_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "common_utils.h"
#include "common_constants.h"

//#START_GRAB_TO_INCLUDES_LIST
#include <fstream>
#include <iostream>
Expand Down Expand Up @@ -232,5 +231,6 @@ bool make_string(const string& src,
//std::cout<<"\n\n"<<reserve_size<<"<-->"<<out.size();
return true;
}

//#STOP_GRAB_TO_DVS_NAMESPACE
}; // namespace dvs
Loading
Loading