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

Print line feature #282

Merged
merged 3 commits into from
Apr 20, 2021
Merged
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
34 changes: 23 additions & 11 deletions Scripts/datahandling/resultdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@ def __init__(self, results_directory_path):
if not os.path.exists(results_directory_path):
os.makedirs(results_directory_path)
self.path = results_directory_path
self._list_buffer = {}
self._line_buffer = {}
self._df_buffer = {}
self._xlsx_buffer = {}

def flush(self):
"""Save to files and empty buffers."""
for filename in self._list_buffer:
with open(os.path.join(self.path, "{}.txt".format(filename)), 'w') as f:
for row in self._list_buffer[filename]:
f.write(row)
self._list_buffer = {}
for filename in self._line_buffer:
self._line_buffer[filename].close()
self._line_buffer = {}
for filename in self._df_buffer:
self._df_buffer[filename].to_csv(
os.path.join(self.path, filename),
Expand Down Expand Up @@ -56,6 +54,21 @@ def print_data(self, data, filename, colname):
df.index.union(data.index), copy=False)
self._df_buffer[filename][colname] = data

def print_line(self, line, filename):
"""Write text to line in file (closed when flushing).

Parameters
----------
line : str
Row of text
filename : str
Name of file where text is pushed (can contain other text)
"""
if filename not in self._line_buffer:
self._line_buffer[filename] = open(
os.path.join(self.path, "{}.txt".format(filename)), 'w')
self._line_buffer[filename].write(line + "\n")

def print_matrix(self, data, filename, sheetname):
"""Save 2-d matrix data to buffer (printed to file when flushing).

Expand Down Expand Up @@ -90,11 +103,10 @@ def print_matrix(self, data, filename, sheetname):
ws.cell(row=i+2, column=1).value = data.index[i]
for j in xrange(0, data.shape[1]):
ws.cell(row=i+2, column=j+2).value = data.iloc[i, j]
# Create list file
if filename not in self._list_buffer:
self._list_buffer[filename] = []
# Create text file
sheetname = sheetname.replace("_", "\t")
for j in data.columns:
for i in data.index:
self._list_buffer[filename].append(
"{}\t{}\t{}\t{}\n".format(i, j, sheetname, str(data[j][i])))
self.print_line(
"{}\t{}\t{}\t{}".format(i, j, sheetname, str(data[j][i])),
filename)