Skip to content

Commit

Permalink
Print line feature (#282)
Browse files Browse the repository at this point in the history
* Create print_line method

* Align columns better with values padded up to 12 digits
Restrict number of significant digits to 7, larger numbers as exponents

* Revert "Align columns better with values padded up to 12 digits"

This reverts commit 5fd912f.
  • Loading branch information
Jens West authored Apr 20, 2021
1 parent 46a0550 commit 76d054d
Showing 1 changed file with 23 additions and 11 deletions.
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)

0 comments on commit 76d054d

Please sign in to comment.