Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Eisgenerator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Klemm, Carl Philipp
Eisgenerator
Commits
c933b28f
Commit
c933b28f
authored
2 years ago
by
Carl Philipp Klemm
Browse files
Options
Downloads
Patches
Plain Diff
eistype: support writeing to and reading from streams instead of just files
parent
0db20026
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
eisgenerator/eistype.h
+2
-0
2 additions, 0 deletions
eisgenerator/eistype.h
eistype.cpp
+53
-34
53 additions, 34 deletions
eistype.cpp
with
55 additions
and
34 deletions
eisgenerator/eistype.h
+
2
−
0
View file @
c933b28f
...
...
@@ -173,6 +173,7 @@ public:
EisSpectra
(
const
std
::
filesystem
::
path
&
path
){
*
this
=
loadFromDisk
(
path
);}
EisSpectra
(){}
static
EisSpectra
loadFromDisk
(
const
std
::
filesystem
::
path
&
path
);
static
EisSpectra
loadFromStream
(
std
::
istream
&
path
);
void
setLabel
(
size_t
label
,
size_t
maxLabel
);
size_t
getLabel
();
void
setSzLabels
(
std
::
vector
<
size_t
>
label
);
...
...
@@ -182,6 +183,7 @@ public:
bool
isMulticlass
();
std
::
vector
<
fvalue
>
getFvalueLabels
();
bool
saveToDisk
(
const
std
::
filesystem
::
path
&
path
)
const
;
void
saveToStream
(
std
::
ostream
&
stream
)
const
;
};
bool
saveToDisk
(
const
EisSpectra
&
data
,
const
std
::
filesystem
::
path
&
path
);
...
...
This diff is collapsed.
Click to expand it.
eistype.cpp
+
53
−
34
View file @
c933b28f
...
...
@@ -250,35 +250,28 @@ std::vector<fvalue> EisSpectra::getFvalueLabels()
}
}
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
())
void
EisSpectra
::
saveToStream
(
std
::
ostream
&
stream
)
const
{
Log
(
Log
::
ERROR
)
<<
"can not open "
<<
path
<<
" for writing
\n
"
;
return
false
;
}
file
<<
std
::
scientific
;
file
<<
F_MAGIC
<<
", "
<<
std
::
to_string
(
F_VERSION_MAJOR
)
<<
'.'
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'
;
file
<<
'"'
<<
model
<<
'"'
<<
(
!
header
.
empty
()
?
", "
:
""
);
file
<<
header
;
stream
<<
'"'
<<
model
<<
'"'
<<
(
!
header
.
empty
()
?
", "
:
""
);
stream
<<
header
;
if
(
!
labels
.
empty
())
{
if
(
!
labelNames
.
empty
())
{
file
<<
"
\n
labelsNames
\n
"
;
stream
<<
"
\n
labelsNames
\n
"
;
std
::
string
labelLine
;
for
(
const
std
::
string
&
name
:
labelNames
)
labelLine
+=
"
\"
"
+
name
+
"
\"
, "
;
labelLine
.
pop_back
();
labelLine
.
pop_back
();
file
<<
labelLine
;
stream
<<
labelLine
;
}
file
<<
"
\n
labels
\n
"
;
stream
<<
"
\n
labels
\n
"
;
std
::
stringstream
labelSs
;
for
(
double
label
:
labels
)
...
...
@@ -286,59 +279,68 @@ bool EisSpectra::saveToDisk(const std::filesystem::path& path) const
std
::
string
labelLine
=
labelSs
.
str
();
labelLine
.
pop_back
();
labelLine
.
pop_back
();
file
<<
labelLine
;
stream
<<
labelLine
;
}
file
<<
"
\n
omega, real, im
\n
"
;
stream
<<
"
\n
omega, real, im
\n
"
;
for
(
const
eis
::
DataPoint
&
point
:
data
)
file
<<
point
.
omega
<<
", "
<<
point
.
im
.
real
()
<<
", "
<<
point
.
im
.
imag
()
<<
'\n'
;
file
.
close
();
return
true
;
stream
<<
point
.
omega
<<
", "
<<
point
.
im
.
real
()
<<
", "
<<
point
.
im
.
imag
()
<<
'\n'
;
}
EisSpectra
EisSpectra
::
loadFrom
Disk
(
const
std
::
filesystem
::
path
&
path
)
bool
EisSpectra
::
saveTo
Disk
(
const
std
::
filesystem
::
path
&
path
)
const
{
EisSpectra
out
;
std
::
fstream
file
;
file
.
open
(
path
,
std
::
ios_base
::
in
);
file
.
open
(
path
,
std
::
ios_base
::
out
|
std
::
ios_base
::
trunc
);
if
(
!
file
.
is_open
())
throw
file_error
(
"can not open "
+
path
.
string
()
+
" for reading
\n
"
);
{
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
(
file
,
line
);
std
::
getline
(
stream
,
line
);
std
::
vector
<
std
::
string
>
tokens
=
tokenizeBinaryIgnore
(
line
,
','
,
'"'
,
'\\'
);
if
(
tokens
.
size
()
<
2
||
tokens
[
0
]
!=
F_MAGIC
)
{
throw
file_error
(
path
.
string
()
+
" is
not a valid EISGenerator file"
);
throw
file_error
(
"
not a valid EISGenerator file
or stream
"
);
}
else
{
std
::
vector
<
std
::
string
>
versionTokens
=
tokenize
(
tokens
[
1
],
'.'
);
if
(
versionTokens
.
size
()
!=
3
||
std
::
stoi
(
versionTokens
[
0
])
>
F_VERSION_MAJOR
||
std
::
stoi
(
versionTokens
[
1
])
>
F_VERSION_MINOR
)
throw
file_error
(
path
.
string
()
+
" was
saved by a newer version of EISGenerator, can not open"
);
throw
file_error
(
"
saved by a newer version of EISGenerator, can not open"
);
}
std
::
getline
(
file
,
line
);
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
;
while
(
file
.
good
())
while
(
stream
.
good
())
{
std
::
getline
(
file
,
line
);
std
::
getline
(
stream
,
line
);
if
(
line
.
starts_with
(
"labelsNames"
))
{
std
::
getline
(
file
,
line
);
std
::
getline
(
stream
,
line
);
out
.
labelNames
=
tokenizeBinaryIgnore
(
line
,
','
,
'"'
,
'\\'
);
continue
;
}
else
if
(
line
.
starts_with
(
"labels"
))
{
std
::
getline
(
file
,
line
);
std
::
getline
(
stream
,
line
);
std
::
vector
<
std
::
string
>
tokens
=
tokenizeBinaryIgnore
(
line
,
','
,
'"'
,
'\\'
);
for
(
const
std
::
string
&
token
:
tokens
)
out
.
labels
.
push_back
(
std
::
stod
(
token
));
...
...
@@ -350,7 +352,7 @@ EisSpectra EisSpectra::loadFromDisk(const std::filesystem::path& path)
}
tokens
=
tokenize
(
line
,
','
);
if
(
tokens
.
size
()
!=
3
)
throw
file_error
(
"invalid line
in "
+
path
.
string
()
+
"
: "
+
line
);
throw
file_error
(
"invalid line: "
+
line
);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnarrowing"
...
...
@@ -363,6 +365,23 @@ EisSpectra EisSpectra::loadFromDisk(const std::filesystem::path& path)
eis
::
removeDuplicates
(
out
.
data
);
}
file
.
close
();
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
());
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment