From 00e90f87584f777f184eacfc04c3719ceac142bf Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm <philipp@uvos.xyz> Date: Tue, 8 Oct 2024 15:28:53 +0200 Subject: [PATCH] Port to use libkisstype --- CMakeLists.txt | 16 +- basicmath.cpp | 1 - compcache.h | 4 +- eisgenerator/basicmath.h | 2 +- eisgenerator/componant/componant.h | 3 +- eisgenerator/eistype.h | 297 ----------------------------- eisgenerator/model.h | 2 +- eisgenerator/normalize.h | 3 +- eisgenerator/spectra.h | 206 -------------------- eistype.cpp | 167 ---------------- linearregession.h | 2 +- main.cpp | 7 +- model.cpp | 1 - normalize.cpp | 3 +- spectra.cpp | 285 --------------------------- test.cpp | 9 +- translators.cpp | 2 +- 17 files changed, 27 insertions(+), 983 deletions(-) delete mode 100644 eisgenerator/eistype.h delete mode 100644 eisgenerator/spectra.h delete mode 100644 eistype.cpp delete mode 100644 spectra.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2efc2fd..71c6ccb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,8 @@ add_compile_definitions(VERSION_MINOR=${CMAKE_PROJECT_VERSION_MINOR}) add_compile_definitions(VERSION_PATCH=${CMAKE_PROJECT_VERSION_PATCH}) find_package(Doxygen) +find_package(PkgConfig REQUIRED) +pkg_search_module(TYPE REQUIRED libkisstype) set(SRC_FILES componant/componant.cpp @@ -31,8 +33,6 @@ set(SRC_FILES basicmath.cpp normalize.cpp basicmath.cpp - eistype.cpp - spectra.cpp strops.cpp translators.cpp randomgen.cpp @@ -55,8 +55,6 @@ set(API_HEADERS_CPP ${API_HEADERS_CPP_DIR}/model.h ${API_HEADERS_CPP_DIR}/log.h ${API_HEADERS_CPP_DIR}/basicmath.h - ${API_HEADERS_CPP_DIR}/eistype.h - ${API_HEADERS_CPP_DIR}/spectra.h ${API_HEADERS_CPP_DIR}/normalize.h ${API_HEADERS_CPP_DIR}/translators.h ) @@ -112,9 +110,9 @@ if(PROFILE_ENABLED) endif(PROFILE_ENABLED) add_library(${PROJECT_NAME}_obj OBJECT ${SRC_FILES} ${API_HEADERS_CPP}) -target_link_libraries(${PROJECT_NAME}_obj ${LIBS}) +target_link_libraries(${PROJECT_NAME}_obj ${TYPE_LIBRARIES}) target_sources(${PROJECT_NAME}_obj INTERFACE FILE_SET header_set TYPE HEADERS BASE_DIRS ${API_HEADERS_CPP_DIR} FILES ${API_HEADERS_CPP}) -target_include_directories(${PROJECT_NAME}_obj PUBLIC eisgenerator .) +target_include_directories(${PROJECT_NAME}_obj PUBLIC eisgenerator . ${TYPE_INCLUDE_DIRS}) set_target_properties(${PROJECT_NAME}_obj PROPERTIES POSITION_INDEPENDENT_CODE 1) set_target_properties(${PROJECT_NAME}_obj PROPERTIES COMPILE_FLAGS ${COMMON_COMPILE_FLAGS} LINK_FLAGS ${COMMON_LINK_FLAGS}) add_library(${PROJECT_NAME} SHARED $<TARGET_OBJECTS:${PROJECT_NAME}_obj>) @@ -138,9 +136,12 @@ install(FILES kissplotcsv DESTINATION bin PERMISSIONS WORLD_EXECUTE WORLD_READ OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE ) + +set(LIBS_TEST -L. -l${EISGEN_LINK_FLAG} -l${TYPE_LIBRARIES}) + link_directories(${CMAKE_CURRENT_BINARY_DIR}) set(SRC_FILES_TEST_APP test.cpp) -set(LIBS_TEST -L. -l${EISGEN_LINK_FLAG}) +message(${LIBS_TEST}) add_executable(${PROJECT_NAME}_test ${SRC_FILES_TEST_APP}) add_dependencies(${PROJECT_NAME}_test ${PROJECT_NAME}) target_link_libraries(${PROJECT_NAME}_test ${LIBS_TEST}) @@ -150,7 +151,6 @@ install(TARGETS ${PROJECT_NAME}_test DESTINATION bin) link_directories(${CMAKE_CURRENT_BINARY_DIR}) set(SRC_FILES_TEST_APP main.cpp) -set(LIBS_TEST -L. -l${EISGEN_LINK_FLAG}) add_executable(${PROJECT_NAME}_export ${SRC_FILES_TEST_APP}) add_dependencies(${PROJECT_NAME}_export ${PROJECT_NAME}) target_link_libraries(${PROJECT_NAME}_export ${LIBS_TEST}) diff --git a/basicmath.cpp b/basicmath.cpp index 63f6d93..2026381 100644 --- a/basicmath.cpp +++ b/basicmath.cpp @@ -25,7 +25,6 @@ #include <limits> #include <stdexcept> -#include "eistype.h" #include "log.h" #include "linearregession.h" diff --git a/compcache.h b/compcache.h index 99cb2c0..3f32ad7 100644 --- a/compcache.h +++ b/compcache.h @@ -21,8 +21,8 @@ #include <vector> #include <map> #include <complex> - -#include "eistype.h" +#include <kisstype/type.h> +#include <filesystem> namespace eis { diff --git a/eisgenerator/basicmath.h b/eisgenerator/basicmath.h index 23792e2..803493a 100644 --- a/eisgenerator/basicmath.h +++ b/eisgenerator/basicmath.h @@ -20,7 +20,7 @@ #pragma once #include <vector> -#include "eistype.h" +#include <kisstype/type.h> namespace eis { diff --git a/eisgenerator/componant/componant.h b/eisgenerator/componant/componant.h index 991b849..44b36c3 100644 --- a/eisgenerator/componant/componant.h +++ b/eisgenerator/componant/componant.h @@ -23,8 +23,7 @@ #include <iostream> #include <vector> #include <string> - -#include "../eistype.h" +#include <kisstype/type.h> namespace eis { diff --git a/eisgenerator/eistype.h b/eisgenerator/eistype.h deleted file mode 100644 index ce58c77..0000000 --- a/eisgenerator/eistype.h +++ /dev/null @@ -1,297 +0,0 @@ -//SPDX-License-Identifier: LGPL-3.0-or-later -/* * eisgenerator - a shared library and application to generate EIS spectra - * Copyright (C) 2022-2024 Carl Philipp Klemm <carl@uvos.xyz> - * - * This file is part of eisgenerator. - * - * eisgenerator is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * eisgenerator is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with eisgenerator. If not, see <http://www.gnu.org/licenses/>. - */ - -#pragma once -#include <complex> -#include <vector> -#include <valarray> -#include <cassert> -#include <cmath> -#include <filesystem> - -/** - * @brief Datatype for all calculations - */ -typedef float fvalue; - -namespace eis -{ - -/** -* Types used by eisgenerator -* @defgroup TYPES Types -* @{ -*/ - -/** -* @brief Basic singular EIS data point -*/ -class DataPoint -{ -public: - std::complex<fvalue> im; /**< Complex impedance */ - fvalue omega; /**< Frequency of the complex impedance */ - DataPoint() = default; - DataPoint(std::complex<fvalue> imIn, fvalue omegaIn = 0): im(imIn), omega(omegaIn){} - bool operator<(const DataPoint& in) const - { - return omega < in.omega; - } - bool operator>(const DataPoint& in) const - { - return omega > in.omega; - } - bool operator==(const DataPoint& in) const - { - return im == in.im; - } - DataPoint operator-(const DataPoint& in) const - { - DataPoint out(*this); - out.im = out.im - in.im; - return out; - } - DataPoint operator+(const DataPoint& in) const - { - DataPoint out(*this); - out.im = out.im + in.im; - return out; - } - DataPoint operator/(const DataPoint& in) const - { - DataPoint out(*this); - out.im = out.im / in.im; - return out; - } - DataPoint operator*(const DataPoint& in) const - { - DataPoint out(*this); - out.im = out.im * in.im; - return out; - } - DataPoint operator/(fvalue in) const - { - DataPoint out(*this); - out.im = out.im / in; - return out; - } - DataPoint operator*(fvalue in) const - { - DataPoint out(*this); - out.im = out.im * in; - return out; - } - DataPoint operator/=(fvalue in) - { - im = im / in; - return *this; - } - /** - * @brief Calculates the absolute value of the complex impedance. - * - * @return The absolute value of the complex impedance. - */ - fvalue complexVectorLength() const - { - return std::sqrt(std::pow(im.real(), 2) + std::pow(im.imag(), 2)); - } -}; - - -/** -* @brief A range -*/ -class Range -{ -public: - fvalue start; /**< Start of the range*/ - fvalue end; /**< End of the range*/ - size_t count = 0; /**< Number of elements in the range*/ - size_t step = 0; /**< Currently active step*/ - bool log = false; /**< True if the elements in the range are to be spaced in log10 increments*/ - - /** - * @brief Calculates the distance between elements in the range. - * - * Will calculate the log10 of the distance if the range is logarithmic. - * - * @return The linear or log distance between steps of this range. - */ - fvalue stepSize() const - { - fvalue startL = log ? log10(start) : start; - fvalue endL = log ? log10(end) : end; - return (endL-startL)/(count-1); - } - - /** - * @brief Calculates the value of the current step. - * - * @return The value of the current step. - */ - fvalue stepValue() const - { - return at(step); - } - - /** - * @brief Calculates the mean of the start and the end values. - * - * @return The mean of the start and the end values. - */ - fvalue center() const - { - return (start+end)/2; - } - - /** - * @brief Calculates the value at the given index. - * - * @return The value at the given index. - */ - fvalue at(size_t index) const - { - assert(index < count || (index == 0 && count == 0)); - if(count < 2) - return start; - return log ? pow(10, stepSize()*index+log10(start)) : stepSize()*index+start; - } - - fvalue operator[](size_t index) const - { - return at(index); - } - Range operator*(fvalue in) const - { - return Range(start*in, end*in, count, log); - } - Range operator/(fvalue in) const - { - return operator*(static_cast<fvalue>(1.0)/in); - } - Range operator*(int in) const - { - return operator*(static_cast<fvalue>(in)); - } - Range operator/(int in) const - { - return operator*(static_cast<fvalue>(1.0)/in); - } - Range(fvalue startI, fvalue endI, size_t countI, bool logI = false): start(startI), end(endI), count(countI), log(logI){} - Range() = default; - - /** - * @brief Prints the range to stdout via eis::Log. - * - * @param level The eis::Log::Level to print at. - */ - void print(int level) const; - - /** - * @brief Gets a machine parseable string encoding this range. - * - * @return The string. - */ - std::string getString() const; - - /** - * @brief Checks if the values of this range are sane/ - * - * This checks for some common errors like having a end < being. - * - * @return true if sane, false if not. - */ - bool isSane() const; - - /** - * @brief This function constructs a vector that contains all elements of this range. - * - * @return The vector with the elements of this range. - */ - std::vector<fvalue> getRangeVector() const; - - /** - * @brief This function creates a range from the parseable string. - * - * @throw std::invalid_argument when the string syntax is incorrect. - * @return The range that was parsed from the string. - */ - [[nodiscard]] static Range fromString(std::string str, size_t count); - - /** - * @brief This function creates a vector ranges from the parseable parameter array string. - * - * @throw std::invalid_argument when the string syntax is incorrect - * @return The vector of ranges that was parsed from the string. - */ - [[nodiscard]] static std::vector<Range> rangesFromParamString(const std::string& paramStr, size_t count); -}; - -class parse_errror: public std::exception -{ - std::string whatStr; -public: - parse_errror(const std::string& whatIn): whatStr(whatIn) - {} - virtual const char* what() const noexcept override - { - return whatStr.c_str(); - } -}; - -class file_error: public std::exception -{ - std::string whatStr; -public: - file_error(const std::string& whatIn): whatStr(whatIn) - {} - virtual const char* what() const noexcept override - { - return whatStr.c_str(); - } -}; - -/** -* @brief Returns the a vector of DataPoints as a pair of valarrays. -* -* @param data the data to transform to valarrays -* @return A pair of valarrays, first the real part and second the imaginary part. -*/ -std::pair<std::valarray<fvalue>, std::valarray<fvalue>> eisToValarrays(const std::vector<eis::DataPoint>& data); - -/** @} */ - - -struct VersionFixed -{ - int major; - int minor; - int patch; -}; - -const VersionFixed& getVersion(); - -} - -std::ostream &operator<<(std::ostream &s, eis::DataPoint const& dp); - -std::ostream &operator<<(std::ostream &s, eis::Range const& range); - -#include "spectra.h" diff --git a/eisgenerator/model.h b/eisgenerator/model.h index 7246919..65cd099 100644 --- a/eisgenerator/model.h +++ b/eisgenerator/model.h @@ -25,8 +25,8 @@ #include <string> #include <vector> #include <functional> +#include <kisstype/type.h> -#include "eistype.h" #include "componant/componant.h" namespace eis diff --git a/eisgenerator/normalize.h b/eisgenerator/normalize.h index e1a5281..cc6fbd2 100644 --- a/eisgenerator/normalize.h +++ b/eisgenerator/normalize.h @@ -21,11 +21,10 @@ #pragma once #include <vector> #include <string> +#include <kisstype/type.h> -#include "eistype.h" #include "model.h" - namespace eis { diff --git a/eisgenerator/spectra.h b/eisgenerator/spectra.h deleted file mode 100644 index 2d7a430..0000000 --- a/eisgenerator/spectra.h +++ /dev/null @@ -1,206 +0,0 @@ -#pragma once -/** - * @addtogroup TYPES - * @{ - */ - -#include <string> -#include <vector> - -#include "eistype.h" - -namespace eis -{ - -class EisSpectra -{ -public: - static constexpr int F_VERSION_MAJOR = 1; - static constexpr int F_VERSION_MINOR = 1; - static constexpr int F_VERSION_PATCH = 0; - static constexpr char F_MAGIC[] = "EISF"; - -public: - std::vector<DataPoint> data; - std::string model; - std::string headerDescription; - std::string header; - std::vector<double> labels; - std::vector<std::string> labelNames; - -public: - /** - * @brief Constructs an EisSpectra. - * - * @param data Vector of the data points of the spectra. - * @param model Model description string for this spectra. - * @param header A free-form text that will be included in any save of this spectra. - * @param labels An optional vector of giving the values of the inputs of the model to get this spectra. - * @param labelNames An optional vector of names describing every input of the model. - */ - EisSpectra(const std::vector<DataPoint>& data, const std::string& model, const std::string& header, - std::vector<double> labels = std::vector<double>(), - std::vector<std::string> labelNames = std::vector<std::string>()); - - /** - * @brief Constructs an EisSpectra. - * - * This function differs from the above only in the datatype of the label. - * - * @param data Vector of the data points of the spectra. - * @param model Model description string for this spectra. - * @param header A free-form text that will be included in any save of this spectra. - * @param labels An optional vector of giving the values of the inputs of the model to get this spectra. - * @param labelNames An optional vector of names describing every input of the model. - */ - EisSpectra(const std::vector<DataPoint>& data, const std::string& model, const std::string& header, - std::vector<float> labels, std::vector<std::string> labelNames = std::vector<std::string>()); - - /** - * @brief Constructs an EisSpectra. - * - * This function differs from the above only in the datatype of the label. - * - * @param data Vector of the data points of the spectra. - * @param model Model description string for this spectra. - * @param header A free-from text that will be included in any save of this spectra. - * @param labels An optional vector of giving the values of the inputs of the model to get this spectra. - * @param labelNames An optional vector of names describing every input of the model. - */ - EisSpectra(const std::vector<DataPoint>& data, const std::string& model, const std::string& header, - std::vector<size_t> labels, std::vector<std::string> labelNames = std::vector<std::string>()); - - /** - * @brief Constructs an EisSpectra. - * - * This function differs from the above only in the datatype of the label. - * - * @param data Vector of the data points of the spectra. - * @param model Model description string for this spectra. - * @param header A free-form text that will be included in any save of this spectra. - * @param label A value corresponding to all inputs of the model. - * @param maxLabel The number of inputs of the model. - */ - EisSpectra(const std::vector<DataPoint>& data, const std::string& model, const std::string& header, - size_t label, size_t maxLabel, std::vector<std::string> labelNames = std::vector<std::string>()); - - /** - * @brief Constructs a EisSpectra by loading an EIS file from disk. - * - * @throw eis::file_error if there is an error loading the file - * @param path The path to the file. - */ - EisSpectra(const std::filesystem::path& path){*this = loadFromDisk(path);} - - EisSpectra(){} - - /** - * @brief Constructs a EisSpectra by loading a EIS file from disk. - * - * This function has the attribute [[nodiscard]] - * - * @throw eis::file_error if there is an error loading the file - * @param path The path to the file. - * @return The EisSpectra parsed from the file. - */ - [[nodiscard]] static EisSpectra loadFromDisk(const std::filesystem::path& path); - - /** - * @brief Constructs a EisSpectra by loading a EIS file from a stream. - * - * This function has the attribute [[nodiscard]] - * - * @throw eis::file_error if there is an error loading the file - * @param stream The stream that contains the EIS file. - * @return The EisSpectra parsed from the stream. - */ - [[nodiscard]] static EisSpectra loadFromStream(std::istream& stream); - - /** - * @brief Sets all input values up to a maximum given by maxLabel to the value given by label. - * - * @param label The value to apply to all inputs. - * @param maxLabel The maximum number of values to set. - */ - void setLabel(size_t label, size_t maxLabel); - - /** - * @brief Gets the input value of this model, where it is a single value. - * - * @return The input value, - */ - size_t getLabel(); - - /** - * @brief Sets the input values of this model. - * - * @param label The input values. - */ - void setSzLabels(std::vector<size_t> label); - - /** - * @brief Sets the input values of this model. - * - * @param label The input values. - */ - void setLabels(const std::vector<double>& labelsIn); - - /** - * @brief Sets the input values of this model. - * - * @param label The input values. - */ - void setLabels(const std::vector<float>& labelsIn); - - /** - * @brief Sets the input values of this model. - * - * @param label The input values. - */ - std::vector<size_t> getSzLabels() const; - - /** - * @brief Returns true if there are multiple inputs, false otherwise. - * - * @return true if there are multiple inputs, false otherwise. - */ - bool isMulticlass(); - - /** - * @brief Returns the inputs as a vector. - * - * @return The inputs as a vector. - */ - std::vector<fvalue> getFvalueLabels(); - - /** - * @brief Saves the spectra to disk. - * - * @param path A path to the file on disk where the spectra shall be saved. - * @return true on success, false on failure. - */ - bool saveToDisk(const std::filesystem::path& path) const; - - /** - * @brief Saves the spectra in the given stream. - * - * @param stream A std::ostream into which the spectra will be saved. - */ - void saveToStream(std::ostream& stream) const; -}; - -/** - * @brief Deprecated function use eis::EisSpectra::saveToDisk instead. - */ -[[deprecated]] bool saveToDisk(const EisSpectra& data, const std::filesystem::path& path); - -/** - * @brief Deprecated function use eis::EisSpectra::loadFromDisk instead. - */ -[[deprecated]] [[nodiscard]] EisSpectra loadFromDisk(const std::filesystem::path& path); - -} - -std::ostream &operator<<(std::ostream &s, eis::EisSpectra const& spectra); - -/** @} */ diff --git a/eistype.cpp b/eistype.cpp deleted file mode 100644 index 638821c..0000000 --- a/eistype.cpp +++ /dev/null @@ -1,167 +0,0 @@ -//SPDX-License-Identifier: LGPL-3.0-or-later -// -// eisgenerator - a shared libary and application to generate EIS spectra -// Copyright (C) 2022-2024 Carl Philipp Klemm <carl@uvos.xyz> -// -// This file is part of eisgenerator. -// -// eisgenerator is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// eisgenerator is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with eisgenerator. If not, see <http://www.gnu.org/licenses/>. -// - -#include "eistype.h" -#include <sstream> -#include <string> -#include <vector> - -#include "strops.h" -#include "log.h" - -using namespace eis; - -bool eis::saveToDisk(const EisSpectra& data, const std::filesystem::path& path) -{ - Log(Log::INFO)<<__func__<<" is deprecated"; - return data.saveToDisk(path); -} - -EisSpectra eis::loadFromDisk(const std::filesystem::path& path) -{ - Log(Log::INFO)<<__func__<<" is deprecated"; - return EisSpectra(path); -} - -std::pair<std::valarray<fvalue>, std::valarray<fvalue>> eis::eisToValarrays(const std::vector<eis::DataPoint>& data) -{ - std::valarray<fvalue> real(data.size()); - std::valarray<fvalue> imag(data.size()); - - for(size_t i = 0; i < data.size(); ++i) - { - real[i] = data[i].im.real(); - imag[i] = data[i].im.imag(); - } - - return {real, imag}; -} - -void eis::Range::print(int level) const -{ - Log(static_cast<Log::Level>(level))<<"Range "<<start<<'-'<<end<<' '<<count<<" steps"<<(log ? " Log" : ""); -} - -std::vector<fvalue> eis::Range::getRangeVector() const -{ - std::vector<fvalue> out(count, 0); - for(size_t i = 0; i < count; ++i) - out[i] = at(i); - return out; -} - -eis::Range eis::Range::fromString(std::string str, size_t count) -{ - bool log = false; - std::vector<std::string> tokens = tokenize(str, '~'); - eis::Range out; - - if(str.back() == 'l' || str.back() == 'L') - { - log = true; - str.pop_back(); - } - - try - { - if(tokens.size() == 1) - { - out = Range(std::stod(tokens[0]), std::stod(tokens[0]), 1, log); - } - else - { - out = Range(std::stod(tokens[0]), std::stod(tokens[1]), count, log); - if(tokens.size() > 2) - throw std::invalid_argument(""); - } - - } - catch(const std::invalid_argument& ia) - { - throw std::invalid_argument("invalid parameter \""+ str + "\""); - } - - return out; -} - -std::vector<Range> eis::Range::rangesFromParamString(const std::string& paramStr, size_t count) -{ - std::vector<std::string> tokens = tokenize(paramStr, ','); - - std::vector<Range> ranges(tokens.size()); - for(size_t i = 0; i < tokens.size(); ++i) - { - std::string& token = tokens[i]; - try - { - ranges[i] = fromString(token, count); - } - catch(const std::invalid_argument& ia) - { - throw std::invalid_argument("invalid parameter string \"{"+ paramStr + "}\""); - } - } - return ranges; -} - -std::string eis::Range::getString() const -{ - std::stringstream ss; - - ss<<start; - if(count > 1) - { - ss<<'~'<<end; - if(log) - ss<<'L'; - } - - return ss.str(); -} - -bool eis::Range::isSane() const -{ - if(log && (start == 0 || end == 0)) - return false; - if(end < start) - return false; - return true; -} - - -std::ostream &operator<<(std::ostream &s, Range const& range) -{ - s<<range.getString(); - return s; -} - -static VersionFixed version = {VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH}; - -const VersionFixed& getVersion() -{ - return version; -} - -std::ostream &operator<<(std::ostream &s, DataPoint const& dp) -{ - s<<dp.im; - return s; -} diff --git a/linearregession.h b/linearregession.h index 435dea7..1711f08 100644 --- a/linearregession.h +++ b/linearregession.h @@ -21,7 +21,7 @@ #pragma once #include <vector> -#include "eisgenerator/eistype.h" +#include <kisstype/type.h> class LinearRegessionCalculator { diff --git a/main.cpp b/main.cpp index 4fcfd79..a724bfd 100644 --- a/main.cpp +++ b/main.cpp @@ -25,6 +25,7 @@ #include <cmath> #include <cassert> #include <filesystem> +#include <kisstype/spectra.h> #include "basicmath.h" #include "model.h" @@ -94,7 +95,7 @@ static void runSweep(const Config& config, eis::Model& model) if(config.noise > 0) eis::noise(results, config.noise, false); - eis::EisSpectra spectra(results, model.getModelStrWithParam(), "exported by eisgenerator_export"); + eis::Spectra spectra(results, model.getModelStrWithParam(), "exported by eisgenerator_export"); spectra.saveToStream(std::cout); @@ -103,7 +104,7 @@ static void runSweep(const Config& config, eis::Model& model) if(!config.saveFileName.empty()) { eis::Log(eis::Log::INFO)<<"Saveing to "<<config.saveFileName; - eis::EisSpectra(results, model.getModelStrWithParam(0), "").saveToDisk(config.saveFileName); + eis::Spectra(results, model.getModelStrWithParam(0), "").saveToDisk(config.saveFileName); } } @@ -171,7 +172,7 @@ static void runParamSweep(const Config& config, eis::Model& model) } } - eis::EisSpectra(data, model.getModelStrWithParam(i), "").saveToDisk(config.saveFileName+"/"+std::to_string(i)+".csv"); + eis::Spectra(data, model.getModelStrWithParam(i), "").saveToDisk(config.saveFileName+"/"+std::to_string(i)+".csv"); } eis::Log(eis::Log::INFO, false)<<'.'; } diff --git a/model.cpp b/model.cpp index eea3e4f..eb61a2a 100644 --- a/model.cpp +++ b/model.cpp @@ -34,7 +34,6 @@ #include "componant/componant.h" #include "componant/resistor.h" -#include "eistype.h" #include "strops.h" #include "componant/paralellseriel.h" #include "log.h" diff --git a/normalize.cpp b/normalize.cpp index 3a47036..64532fe 100644 --- a/normalize.cpp +++ b/normalize.cpp @@ -24,7 +24,8 @@ #include <cmath> #include <complex> #include <limits> -#include "eistype.h" +#include <kisstype/type.h> + #include "log.h" #include "basicmath.h" diff --git a/spectra.cpp b/spectra.cpp deleted file mode 100644 index 9ded602..0000000 --- a/spectra.cpp +++ /dev/null @@ -1,285 +0,0 @@ -#include <fstream> - -#include "spectra.h" -#include "eistype.h" -#include "strops.h" -#include "log.h" -#include "basicmath.h" - -namespace eis -{ - -EisSpectra::EisSpectra(const std::vector<DataPoint>& dataIn, const std::string& modelIn, - const std::string& headerIn, std::vector<double> labelsIn, std::vector<std::string> labelNamesIn): -data(dataIn), model(modelIn), header(headerIn), labels(labelsIn), labelNames(labelNamesIn) -{ - -} - -EisSpectra::EisSpectra(const std::vector<DataPoint>& dataIn, const std::string& modelIn, - const std::string& headerIn, std::vector<float> labelsIn, std::vector<std::string> labelNamesIn): -data(dataIn), model(modelIn), header(headerIn), labelNames(labelNamesIn) -{ - setLabels(labelsIn); -} - -EisSpectra::EisSpectra(const std::vector<DataPoint>& dataIn, const std::string& modelIn, const std::string& headerIn, - std::vector<size_t> labelsIn, std::vector<std::string> labelNamesIn): -data(dataIn), model(modelIn), header(headerIn), labelNames(labelNamesIn) -{ - setSzLabels(labelsIn); -} - -EisSpectra::EisSpectra(const std::vector<DataPoint>& dataIn, const std::string& modelIn, const std::string& headerIn, - size_t label, size_t maxLabel, std::vector<std::string> labelNamesIn): -data(dataIn), model(modelIn), header(headerIn), labelNames(labelNamesIn) -{ - setLabel(label, maxLabel); -} - -void EisSpectra::setLabel(size_t label, size_t maxLabel) -{ - labels.assign(maxLabel, 0); - labels[label] = 1; -} - -void EisSpectra::setSzLabels(std::vector<size_t> labelsIn) -{ - labels.assign(labelsIn.size(), 0); - for(size_t i = 0; i < labelsIn.size(); ++i) - labels[i] = static_cast<double>(labelsIn[i]); -} - -std::vector<size_t> EisSpectra::getSzLabels() const -{ - std::vector<size_t> out(labels.size()); - for(size_t i = 0; i < labels.size(); ++i) - out[i] = static_cast<size_t>(labels[i]); - return out; -} - -size_t EisSpectra::getLabel() -{ - for(size_t i = 0; i < labels.size(); ++i) - if(labels[i] != 0) - return i; - return 0; -} - -bool EisSpectra::isMulticlass() -{ - bool foundFirst = false; - for(size_t i = 0; i < labels.size(); ++i) - { - if(labels[i] != 0) - { - if(foundFirst) - return true; - else - foundFirst = true; - } - } - return false; -} - -void EisSpectra::setLabels(const std::vector<float>& labelsIn) -{ - labels.assign(labelsIn.size(), 0); - for(size_t i = 0; i < labels.size(); ++i) - labels[i] = labelsIn[i]; -} - -void EisSpectra::setLabels(const std::vector<double>& labelsIn) -{ - labels = labelsIn; -} - -std::vector<fvalue> EisSpectra::getFvalueLabels() -{ - if constexpr(std::is_same<fvalue, double>::value) - { - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wstrict-aliasing" - return *reinterpret_cast<std::vector<fvalue>*>(&labels); - #pragma GCC diagnostic pop - } - else - { - std::vector<fvalue> out(labels.size()); - for(size_t i = 0; i < labels.size(); ++i) - out[i] = static_cast<fvalue>(labels[i]); - return out; - } -} - -void EisSpectra::saveToStream(std::ostream& stream) const -{ - stream<<std::scientific; - stream<<F_MAGIC<<", "<<std::to_string(F_VERSION_MAJOR)<<'.' - <<std::to_string(F_VERSION_MINOR)<<'.'<<std::to_string(F_VERSION_PATCH)<<'\n'; - - stream<<'"'<<(model.empty() ? "None" : model)<<'"'<<'\n'<<(headerDescription.empty() ? "None" : headerDescription)<<'\n'<<(header.empty() ? "None" : header); - - if(!labels.empty()) - { - if(!labelNames.empty()) - { - stream<<"\nlabelsNames\n"; - std::string labelLine; - for(const std::string& name : labelNames) - labelLine += "\"" + name + "\", "; - labelLine.pop_back(); - labelLine.pop_back(); - stream<<labelLine; - } - stream<<"\nlabels\n"; - - std::stringstream labelSs; - for(double label : labels) - labelSs<<label<<", "; - std::string labelLine = labelSs.str(); - labelLine.pop_back(); - labelLine.pop_back(); - stream<<labelLine; - } - - stream<<"\nomega, real, im\n"; - - for(const eis::DataPoint& point : data) - stream<<point.omega<<", "<<point.im.real()<<", "<<point.im.imag()<<'\n'; -} - -bool EisSpectra::saveToDisk(const std::filesystem::path& path) const -{ - std::fstream file; - file.open(path, std::ios_base::out | std::ios_base::trunc); - if(!file.is_open()) - { - Log(Log::ERROR)<<"can not open "<<path<<" for writing\n"; - return false; - } - - saveToStream(file); - - file.close(); - return true; -} - -EisSpectra EisSpectra::loadFromStream(std::istream& stream) -{ - EisSpectra out; - std::string line; - std::getline(stream, line); - std::vector<std::string> tokens = tokenizeBinaryIgnore(line, ',', '"', '\\'); - VersionFixed fileVersion; - - if(tokens.size() != 2 || tokens[0] != F_MAGIC) - { - throw file_error("not a valid EISGenerator file or stream"); - } - else - { - std::vector<std::string> versionTokens = tokenize(tokens[1], '.'); - if(versionTokens.size() != 3) - throw file_error("could not load file version from file"); - fileVersion.major = std::stoi(versionTokens[0]); - fileVersion.minor = std::stoi(versionTokens[1]); - fileVersion.patch = std::stoi(versionTokens[2]); - if(fileVersion.major > F_VERSION_MAJOR || fileVersion.minor > F_VERSION_MINOR) - throw file_error("saved by a newer version of EISGenerator, can not open"); - } - - if(fileVersion.minor == F_VERSION_MINOR) - { - std::getline(stream, line); - stripQuotes(line); - out.model = line == "None" ? "" : line; - std::getline(stream, line); - stripQuotes(line); - out.headerDescription = line == "None" ? "" : line; - std::getline(stream, line); - stripQuotes(line); - out.header = line == "None" ? "" : line; - } - else - { - std::getline(stream, line); - tokens = tokenizeBinaryIgnore(line, ',', '"', '\\'); - stripQuotes(tokens[0]); - out.model = tokens[0]; - line.erase(line.begin(), line.begin()+tokens.size()); - out.header = line; - } - - out.model.erase(std::remove(out.model.begin(), out.model.end(), '\0'), out.model.end()); - out.header.erase(std::remove(out.header.begin(), out.header.end(), '\0'), out.header.end()); - - while(stream.good()) - { - std::getline(stream, line); - if(line.starts_with("labelsNames")) - { - std::getline(stream, line); - out.labelNames = tokenizeBinaryIgnore(line, ',', '"', '\\'); - for(std::string& label : out.labelNames) - { - label = stripWhitespace(label); - stripQuotes(label); - } - continue; - } - else if(line.starts_with("labels")) - { - std::getline(stream, line); - std::vector<std::string> tokens = tokenizeBinaryIgnore(line, ',', '"', '\\'); - for(const std::string& token : tokens) - out.labels.push_back(std::stod(token)); - continue; - } - else if(line.empty() || line[0] == '#' || line.starts_with("omega")) - { - continue; - } - tokens = tokenize(line, ','); - if(tokens.size() != 3) - throw file_error("invalid line: " + line); - - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wnarrowing" - if constexpr (std::is_same<fvalue, double>::value) - out.data.push_back(DataPoint({std::stod(tokens[1]), std::stod(tokens[2])}, std::stod(tokens[0]))); - else - out.data.push_back(DataPoint({std::stof(tokens[1]), std::stof(tokens[2])}, std::stof(tokens[0]))); - #pragma GCC diagnostic pop - - eis::removeDuplicates(out.data); - } - - return out; -} - -EisSpectra EisSpectra::loadFromDisk(const std::filesystem::path& path) -{ - std::fstream file; - file.open(path, std::ios_base::in); - if(!file.is_open()) - throw file_error("can not open " + path.string() + " for reading\n"); - - try - { - EisSpectra out = loadFromStream(file); - return out; - } - catch(const file_error& err) - { - throw file_error(path.string() + std::string(": ") + err.what()); - } -} - -} - -std::ostream &operator<<(std::ostream &s, eis::EisSpectra const& spectra) -{ - spectra.saveToStream(s); - return s; -} diff --git a/test.cpp b/test.cpp index f7a4b6b..6489ebc 100644 --- a/test.cpp +++ b/test.cpp @@ -25,8 +25,9 @@ #include <chrono> #include <sstream> #include <cstring> +#include <kisstype/type.h> +#include <kisstype/spectra.h> -#include "eistype.h" #include "model.h" #include "log.h" #include "normalize.h" @@ -365,7 +366,7 @@ static bool testEisNyquistDistance() const std::filesystem::path filePath("./relaxis_rp-rp_0.csv"); try { - eis::EisSpectra spectra(filePath); + eis::Spectra spectra(filePath); std::vector<fvalue> omega(spectra.data.size()); for(size_t i = 0; i < spectra.data.size(); ++i) @@ -402,7 +403,7 @@ static bool testLoadDeduplication() const std::filesystem::path filePath("./relaxis_rp-rp_0.csv"); try { - eis::EisSpectra spectra(filePath); + eis::Spectra spectra(filePath); if(eis::fvalueEq(spectra.data[spectra.data.size()-1].omega, spectra.data[spectra.data.size()-2].omega+2)) { @@ -461,7 +462,7 @@ static bool testLoader(const char* file, const size_t expectedLength, const size { std::stringstream ss(file); eis::Log(eis::Log::INFO)<<__func__<<" STRLEN "<<strlen(file); - eis::EisSpectra spectra = eis::EisSpectra::loadFromStream(ss); + eis::Spectra spectra = eis::Spectra::loadFromStream(ss); if(spectra.data.size() != expectedLength) { eis::Log(eis::Log::ERROR)<<__func__<<" Spectra has length "<<spectra.data.size()<<" expected "<<expectedLength; diff --git a/translators.cpp b/translators.cpp index 8cfee08..c0ebfdb 100644 --- a/translators.cpp +++ b/translators.cpp @@ -26,9 +26,9 @@ #include <map> #include <algorithm> #include <sstream> +#include <kisstype/type.h> #include "strops.h" -#include "eistype.h" #include "log.h" #include "componant/componant.h" -- GitLab