Commit 3abe2dbb authored by Elijah Andrews's avatar Elijah Andrews

Upgraded and made use of file_utils csv reading/writing.

parent 49d7f5d2
import os
import numpy as np
from PyQt5.QtWidgets import QFileDialog, QApplication
def array_to_csv(path, filename, array):
file = open(path + filename, "w")
for entry in array:
def lists_to_csv(file_dir, filename, lists, headers=None, overwrite=False):
if os.path.exists(file_dir + filename) and not overwrite:
print(f"Warning: file already exists. {file_dir + filename}")
os.makedirs(file_dir, exist_ok=True)
file = open(file_dir + filename, "w")
if headers:
line = ""
for h in headers:
line += h + ","
line = line[:-1] + "\n"
file.write(line)
zipped_lists = np.transpose(lists)
for entry in zipped_lists:
line = ""
for part in entry:
line += str(part) + ","
line += "\n"
line = line[:-1] + "\n"
file.write(line)
file.close()
def csv_to_lists(file_dir, filename, has_headers=False):
file = open(file_dir + filename, "r")
lines = file.readlines()
start_idx = 1 if has_headers else 0
zipped_lists = []
for line in lines[start_idx:]:
line = line.strip(',\n')
part_arr = []
for part in line.split(","):
part_arr.append(float(part))
zipped_lists.append(part_arr)
return np.transpose(zipped_lists)
def select_file(start_path, create_window=True):
if create_window:
window = QApplication([])
......
......@@ -324,13 +324,7 @@ def select_data_series(use_all_dirs=True, num_series=None, use_defaults=True, ve
def plot_prediction_file(prediction_file, ax, normalize=False, x_star=None, theta_star=None, label="Numerical", c='k'):
x_min, x_max = ax.get_xlim() # Keep track of original x limits.
predicted_xs = []
predicted_theta_js = []
f = open(prediction_file)
for line in f.readlines():
split = line.strip().split(",")
predicted_xs.append(float(split[0]))
predicted_theta_js.append(float(split[1]))
predicted_xs, predicted_theta_js = file.csv_to_lists("", prediction_file, has_headers=True)
if x_star is None or theta_star is None:
x_star, theta_star = sorted(zip(predicted_xs, predicted_theta_js), key=lambda k: k[1])[-1]
if normalize:
......
......@@ -94,4 +94,4 @@ for Y in Ys:
file_path = f"{out_dir}/W{W:.2f}H{H:.2f}Y{Y:.2f}_bem_slot_prediction_{n}_{density_ratio}_{w_thresh}" \
f"{alph[i]}.csv"
i += 1
file.array_to_csv("", file_path, arr)
file.lists_to_csv("", file_path, [xs, thetas], headers=["x", "theta"])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment