eisgenerator 1.0.x
eistype.h
1//SPDX-License-Identifier: LGPL-3.0-or-later
2/* * eisgenerator - a shared library and application to generate EIS spectra
3 * Copyright (C) 2022-2024 Carl Philipp Klemm <carl@uvos.xyz>
4 *
5 * This file is part of eisgenerator.
6 *
7 * eisgenerator is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * eisgenerator is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with eisgenerator. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22#include <complex>
23#include <vector>
24#include <valarray>
25#include <cassert>
26#include <cmath>
27#include <filesystem>
28
32typedef float fvalue;
33
34namespace eis
35{
36
47{
48public:
49 std::complex<fvalue> im;
50 fvalue omega;
51 DataPoint() = default;
52 DataPoint(std::complex<fvalue> imIn, fvalue omegaIn = 0): im(imIn), omega(omegaIn){}
53 bool operator<(const DataPoint& in) const
54 {
55 return omega < in.omega;
56 }
57 bool operator>(const DataPoint& in) const
58 {
59 return omega > in.omega;
60 }
61 bool operator==(const DataPoint& in) const
62 {
63 return im == in.im;
64 }
65 DataPoint operator-(const DataPoint& in) const
66 {
67 DataPoint out(*this);
68 out.im = out.im - in.im;
69 return out;
70 }
71 DataPoint operator+(const DataPoint& in) const
72 {
73 DataPoint out(*this);
74 out.im = out.im + in.im;
75 return out;
76 }
77 DataPoint operator/(fvalue in) const
78 {
79 DataPoint out(*this);
80 out.im = out.im / in;
81 return out;
82 }
83 DataPoint operator*(fvalue in) const
84 {
85 DataPoint out(*this);
86 out.im = out.im * in;
87 return out;
88 }
89 DataPoint operator/=(fvalue in)
90 {
91 im = im / in;
92 return *this;
93 }
99 fvalue complexVectorLength() const
100 {
101 return std::sqrt(std::pow(im.real(), 2) + std::pow(im.imag(), 2));
102 }
103};
104
105
109class Range
110{
111public:
112 fvalue start;
113 fvalue end;
114 size_t count = 0;
115 size_t step = 0;
116 bool log = false;
124 fvalue stepSize() const
125 {
126 fvalue startL = log ? log10(start) : start;
127 fvalue endL = log ? log10(end) : end;
128 return (endL-startL)/(count-1);
129 }
130
136 fvalue stepValue() const
137 {
138 return at(step);
139 }
140
146 fvalue center() const
147 {
148 return (start+end)/2;
149 }
150
156 fvalue at(size_t index) const
157 {
158 assert(index < count || (index == 0 && count == 0));
159 if(count < 2)
160 return start;
161 return log ? pow(10, stepSize()*index+log10(start)) : stepSize()*index+start;
162 }
163
164 fvalue operator[](size_t index) const
165 {
166 return at(index);
167 }
168 Range operator*(fvalue in) const
169 {
170 return Range(start*in, end*in, count, log);
171 }
172 Range operator/(fvalue in) const
173 {
174 return operator*(static_cast<fvalue>(1.0)/in);
175 }
176 Range operator*(int in) const
177 {
178 return operator*(static_cast<fvalue>(in));
179 }
180 Range operator/(int in) const
181 {
182 return operator*(static_cast<fvalue>(1.0)/in);
183 }
184 Range(fvalue startI, fvalue endI, size_t countI, bool logI = false): start(startI), end(endI), count(countI), log(logI){}
185 Range() = default;
186
192 void print(int level) const;
193
199 std::string getString() const;
200
207 bool isSane() const;
208
214 std::vector<fvalue> getRangeVector() const;
215
222 [[nodiscard]] static Range fromString(std::string str, size_t count);
223
230 [[nodiscard]] static std::vector<Range> rangesFromParamString(const std::string& paramStr, size_t count);
231};
232
233class parse_errror: public std::exception
234{
235 std::string whatStr;
236public:
237 parse_errror(const std::string& whatIn): whatStr(whatIn)
238 {}
239 virtual const char* what() const noexcept override
240 {
241 return whatStr.c_str();
242 }
243};
244
245class file_error: public std::exception
246{
247 std::string whatStr;
248public:
249 file_error(const std::string& whatIn): whatStr(whatIn)
250 {}
251 virtual const char* what() const noexcept override
252 {
253 return whatStr.c_str();
254 }
255};
256
258{
259public:
260 static constexpr int F_VERSION_MAJOR = 1;
261 static constexpr int F_VERSION_MINOR = 0;
262 static constexpr int F_VERSION_PATCH = 0;
263 static constexpr char F_MAGIC[] = "EISF";
264
265public:
266 std::vector<DataPoint> data;
267 std::string model;
268 std::string header;
269 std::vector<double> labels;
270 std::vector<std::string> labelNames;
271
272public:
282 EisSpectra(const std::vector<DataPoint>& data, const std::string& model, const std::string& header,
283 std::vector<double> labels = std::vector<double>(),
284 std::vector<std::string> labelNames = std::vector<std::string>());
285
296 EisSpectra(const std::vector<DataPoint>& data, const std::string& model, const std::string& header,
297 std::vector<float> labels, std::vector<std::string> labelNames = std::vector<std::string>());
298
309 EisSpectra(const std::vector<DataPoint>& data, const std::string& model, const std::string& header,
310 std::vector<size_t> labels, std::vector<std::string> labelNames = std::vector<std::string>());
311
322 EisSpectra(const std::vector<DataPoint>& data, const std::string& model, const std::string& header,
323 size_t label, size_t maxLabel, std::vector<std::string> labelNames = std::vector<std::string>());
324
331 EisSpectra(const std::filesystem::path& path){*this = loadFromDisk(path);}
332
333 EisSpectra(){}
334
342 [[nodiscard]] static EisSpectra loadFromDisk(const std::filesystem::path& path);
343
351 [[nodiscard]] static EisSpectra loadFromStream(std::istream& stream);
352
359 void setLabel(size_t label, size_t maxLabel);
360
366 size_t getLabel();
367
373 void setSzLabels(std::vector<size_t> label);
374
380 void setLabels(const std::vector<double>& labelsIn);
381
387 void setLabels(const std::vector<float>& labelsIn);
388
394 std::vector<size_t> getSzLabels() const;
395
402
408 std::vector<fvalue> getFvalueLabels();
409
416 bool saveToDisk(const std::filesystem::path& path) const;
417
423 void saveToStream(std::ostream& stream) const;
424};
425
429[[deprecated]] bool saveToDisk(const EisSpectra& data, const std::filesystem::path& path);
430
434[[deprecated]] [[nodiscard]] EisSpectra loadFromDisk(const std::filesystem::path& path);
435
441std::pair<std::valarray<fvalue>, std::valarray<fvalue>> eisToValarrays(const std::vector<eis::DataPoint>& b);
442
450fvalue eisDistance(const std::vector<eis::DataPoint>& a, const std::vector<eis::DataPoint>& b);
451
452
462fvalue eisNyquistDistance(const std::vector<eis::DataPoint>& a, const std::vector<eis::DataPoint>& b);
463
466}
467
468std::ostream &operator<<(std::ostream &s, eis::DataPoint const& dp);
469
470std::ostream &operator<<(std::ostream &s, eis::Range const& range);
471
472std::ostream &operator<<(std::ostream &s, eis::EisSpectra const& spectra);
473
Basic singular EIS data point.
Definition eistype.h:47
fvalue complexVectorLength() const
calculates the absolute value of the complex impedance
Definition eistype.h:99
std::complex< fvalue > im
Complex impedance.
Definition eistype.h:49
fvalue omega
Frequency of the complex impedance.
Definition eistype.h:50
Definition eistype.h:258
std::vector< size_t > getSzLabels() const
Sets the input values of this model.
bool isMulticlass()
Returns true if there are multiple inputs, false otherwise.
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 >())
Constructs a EisSpectra.
size_t getLabel()
Gets the input value of this model, where it is a single value.
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 >())
Constructs a EisSpectra this function differs from the above only in the datatype of the label.
void setLabels(const std::vector< float > &labelsIn)
Sets the input values of this model.
void saveToStream(std::ostream &stream) const
Saves the spectra in the given stream.
EisSpectra(const std::filesystem::path &path)
Constructs a EisSpectra by loading a EIS file from disk.
Definition eistype.h:331
void setLabels(const std::vector< double > &labelsIn)
Sets the input values of this model.
void setLabel(size_t label, size_t maxLabel)
Constructs a EisSpectra by loading a EIS file from a stream.
void setSzLabels(std::vector< size_t > label)
Sets the input values of this model.
static EisSpectra loadFromStream(std::istream &stream)
Constructs a EisSpectra by loading a EIS file from a stream.
static EisSpectra loadFromDisk(const std::filesystem::path &path)
Constructs a EisSpectra by loading a EIS file from disk.
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 >())
Constructs a EisSpectra this function differs from the above only in the datatype of the label.
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 >())
Constructs a EisSpectra this function differs from the above only in the datatype of the label.
std::vector< fvalue > getFvalueLabels()
Returns the inputs as a vector.
bool saveToDisk(const std::filesystem::path &path) const
Saves the spectra to disk.
A range.
Definition eistype.h:110
fvalue stepValue() const
calculates the value of the current step
Definition eistype.h:136
fvalue stepSize() const
calculates the distance between elements in the range will calculate the log10 of the distance if the...
Definition eistype.h:124
bool isSane() const
checks if the values of this range are sane this checks for some common errors like having a end < be...
static std::vector< Range > rangesFromParamString(const std::string &paramStr, size_t count)
this function creates a vector ranges from the parseable parameter array string
std::vector< fvalue > getRangeVector() const
this function constructs a vector that contains all elements of this range
fvalue end
End of the range.
Definition eistype.h:113
std::string getString() const
gets a machine parseable string encoding this range
void print(int level) const
prints the range to stdout via eis::Log
size_t step
Currently active step.
Definition eistype.h:115
fvalue center() const
calculates the mean of the start and the end values
Definition eistype.h:146
fvalue at(size_t index) const
calculates the value at the given index
Definition eistype.h:156
fvalue start
Start of the range.
Definition eistype.h:112
static Range fromString(std::string str, size_t count)
this function creates a range from the parseable string
size_t count
Number of elements in the range.
Definition eistype.h:114
bool log
True if the elements in the range are to be spaced in log10 increments.
Definition eistype.h:116
Definition eistype.h:246
Definition eistype.h:234
EisSpectra loadFromDisk(const std::filesystem::path &path)
deprecated function use eis::EisSpectra::loadFromDisk instead
bool saveToDisk(const EisSpectra &data, const std::filesystem::path &path)
deprecated function use eis::EisSpectra::saveToDisk instead
fvalue eisDistance(const std::vector< eis::DataPoint > &a, const std::vector< eis::DataPoint > &b)
Returns the mean l2 element wise distance of he given spectra.
std::pair< std::valarray< fvalue >, std::valarray< fvalue > > eisToValarrays(const std::vector< eis::DataPoint > &b)
Returns the a vector of DataPoints as a pair of valarrays.
fvalue eisNyquistDistance(const std::vector< eis::DataPoint > &a, const std::vector< eis::DataPoint > &b)
Returns the mean distance of the points in a to the linearly interpolated nyquist curve of b.
eisgenerator Copyright (C) 2021 Carl Klemm
Definition basicmath.h:26