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;
125 fvalue stepSize() const
126 {
127 fvalue startL = log ? log10(start) : start;
128 fvalue endL = log ? log10(end) : end;
129 return (endL-startL)/(count-1);
130 }
131
137 fvalue stepValue() const
138 {
139 return at(step);
140 }
141
147 fvalue center() const
148 {
149 return (start+end)/2;
150 }
151
157 fvalue at(size_t index) const
158 {
159 assert(index < count || (index == 0 && count == 0));
160 if(count < 2)
161 return start;
162 return log ? pow(10, stepSize()*index+log10(start)) : stepSize()*index+start;
163 }
164
165 fvalue operator[](size_t index) const
166 {
167 return at(index);
168 }
169 Range operator*(fvalue in) const
170 {
171 return Range(start*in, end*in, count, log);
172 }
173 Range operator/(fvalue in) const
174 {
175 return operator*(static_cast<fvalue>(1.0)/in);
176 }
177 Range operator*(int in) const
178 {
179 return operator*(static_cast<fvalue>(in));
180 }
181 Range operator/(int in) const
182 {
183 return operator*(static_cast<fvalue>(1.0)/in);
184 }
185 Range(fvalue startI, fvalue endI, size_t countI, bool logI = false): start(startI), end(endI), count(countI), log(logI){}
186 Range() = default;
187
193 void print(int level) const;
194
200 std::string getString() const;
201
209 bool isSane() const;
210
216 std::vector<fvalue> getRangeVector() const;
217
224 [[nodiscard]] static Range fromString(std::string str, size_t count);
225
232 [[nodiscard]] static std::vector<Range> rangesFromParamString(const std::string& paramStr, size_t count);
233};
234
235class parse_errror: public std::exception
236{
237 std::string whatStr;
238public:
239 parse_errror(const std::string& whatIn): whatStr(whatIn)
240 {}
241 virtual const char* what() const noexcept override
242 {
243 return whatStr.c_str();
244 }
245};
246
247class file_error: public std::exception
248{
249 std::string whatStr;
250public:
251 file_error(const std::string& whatIn): whatStr(whatIn)
252 {}
253 virtual const char* what() const noexcept override
254 {
255 return whatStr.c_str();
256 }
257};
258
260{
261public:
262 static constexpr int F_VERSION_MAJOR = 1;
263 static constexpr int F_VERSION_MINOR = 0;
264 static constexpr int F_VERSION_PATCH = 0;
265 static constexpr char F_MAGIC[] = "EISF";
266
267public:
268 std::vector<DataPoint> data;
269 std::string model;
270 std::string header;
271 std::vector<double> labels;
272 std::vector<std::string> labelNames;
273
274public:
284 EisSpectra(const std::vector<DataPoint>& data, const std::string& model, const std::string& header,
285 std::vector<double> labels = std::vector<double>(),
286 std::vector<std::string> labelNames = std::vector<std::string>());
287
299 EisSpectra(const std::vector<DataPoint>& data, const std::string& model, const std::string& header,
300 std::vector<float> labels, std::vector<std::string> labelNames = std::vector<std::string>());
301
313 EisSpectra(const std::vector<DataPoint>& data, const std::string& model, const std::string& header,
314 std::vector<size_t> labels, std::vector<std::string> labelNames = std::vector<std::string>());
315
327 EisSpectra(const std::vector<DataPoint>& data, const std::string& model, const std::string& header,
328 size_t label, size_t maxLabel, std::vector<std::string> labelNames = std::vector<std::string>());
329
336 EisSpectra(const std::filesystem::path& path){*this = loadFromDisk(path);}
337
338 EisSpectra(){}
339
349 [[nodiscard]] static EisSpectra loadFromDisk(const std::filesystem::path& path);
350
360 [[nodiscard]] static EisSpectra loadFromStream(std::istream& stream);
361
368 void setLabel(size_t label, size_t maxLabel);
369
375 size_t getLabel();
376
382 void setSzLabels(std::vector<size_t> label);
383
389 void setLabels(const std::vector<double>& labelsIn);
390
396 void setLabels(const std::vector<float>& labelsIn);
397
403 std::vector<size_t> getSzLabels() const;
404
411
417 std::vector<fvalue> getFvalueLabels();
418
425 bool saveToDisk(const std::filesystem::path& path) const;
426
432 void saveToStream(std::ostream& stream) const;
433};
434
438[[deprecated]] bool saveToDisk(const EisSpectra& data, const std::filesystem::path& path);
439
443[[deprecated]] [[nodiscard]] EisSpectra loadFromDisk(const std::filesystem::path& path);
444
451std::pair<std::valarray<fvalue>, std::valarray<fvalue>> eisToValarrays(const std::vector<eis::DataPoint>& data);
452
462fvalue eisDistance(const std::vector<eis::DataPoint>& a, const std::vector<eis::DataPoint>& b);
463
464
476fvalue eisNyquistDistance(const std::vector<eis::DataPoint>& a, const std::vector<eis::DataPoint>& b);
477
480}
481
482std::ostream &operator<<(std::ostream &s, eis::DataPoint const& dp);
483
484std::ostream &operator<<(std::ostream &s, eis::Range const& range);
485
486std::ostream &operator<<(std::ostream &s, eis::EisSpectra const& spectra);
487
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:260
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 an 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 an EisSpectra.
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 an EIS file from disk.
Definition eistype.h:336
void setLabels(const std::vector< double > &labelsIn)
Sets the input values of this model.
void setLabel(size_t label, size_t maxLabel)
Sets all input values up to a maximum given by maxLabel to the value given by label.
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 an EisSpectra.
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 an EisSpectra.
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:137
fvalue stepSize() const
Calculates the distance between elements in the range.
Definition eistype.h:125
bool isSane() const
Checks if the values of this range are sane/.
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:147
fvalue at(size_t index) const
Calculates the value at the given index.
Definition eistype.h:157
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:248
Definition eistype.h:236
std::pair< std::valarray< fvalue >, std::valarray< fvalue > > eisToValarrays(const std::vector< eis::DataPoint > &data)
Returns the a vector of DataPoints as a pair of valarrays.
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 the given spectra.
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