mirror of
https://github.com/janishutz/BiogasControllerApp.git
synced 2025-11-25 05:44:23 +00:00
Config, Lots of docs, Format
Added a config validator and documented code that was previously undocumented, for the plot_generator scripts, documented them.
This commit is contained in:
@@ -6,53 +6,85 @@ n = int(input("Sensor number to be printed: "))
|
||||
|
||||
file = ""
|
||||
|
||||
|
||||
def generate_plot():
|
||||
reader = csv.reader(file, delimiter=',')
|
||||
# Read data using the CSV library
|
||||
reader = csv.reader(file, delimiter=",")
|
||||
|
||||
# Create a list from the data
|
||||
data = list(reader)
|
||||
data.sort(key=lambda imp: float(imp[2]))
|
||||
lenght = len(data)
|
||||
|
||||
# Sort the list using a lambda sort descriptor
|
||||
# A lambda function is an anonymous function (= an unnamed function),
|
||||
# which makes it convenient. A sort descriptor is a function that
|
||||
# (usually, but not here) returns a value indicating which of two values
|
||||
# come before or after in the ordering.
|
||||
# Here, instead we simply return a floating point value for each data point
|
||||
data.sort(key=lambda data_point: float(data_point[2]))
|
||||
|
||||
# Store the x and y coordinates in two arrays
|
||||
x = []
|
||||
y = []
|
||||
|
||||
for _ in range(lenght):
|
||||
extract = data.pop(0)
|
||||
sensor = int(extract.pop(0))
|
||||
for _ in range(len(data)):
|
||||
# Extract the data point
|
||||
data_point = data.pop(0)
|
||||
sensor = int(data_point.pop(0))
|
||||
if sensor == n:
|
||||
ye = extract.pop(0)
|
||||
xe = extract.pop(0)
|
||||
y.append(float(ye))
|
||||
x.append(float(xe))
|
||||
y.append(float(data_point.pop(0)))
|
||||
x.append(float(data_point.pop(0)))
|
||||
|
||||
# Use Numpy's polyfit function to fit a 2nd degree polynomial to the points using quadratic regression
|
||||
# This function returns an array with the coefficients
|
||||
fit = np.polyfit(x, y, 2)
|
||||
|
||||
# The formula to output to the plot
|
||||
formula = f"F(U) = {round(float(fit[0]), 4)}U^2+{round(float(fit[1]), 4)}U+{round(float(fit[2]), 4)}"
|
||||
|
||||
fit_fn = np.poly1d(fit)
|
||||
# Create a fit function from the previously determined coefficients
|
||||
fit_fn = np.poly1d(fit) # Returns a function that takes a list of x-coordinate as argument
|
||||
|
||||
# Plot the line on the graph
|
||||
plt.plot(x, fit_fn(x), color="BLUE", label="T(U)")
|
||||
|
||||
# Scatter Plot the data points that we have
|
||||
plt.scatter(x, y, color="MAGENTA", marker="o", label="Data")
|
||||
|
||||
# Label the graph
|
||||
plt.ylabel("Temperature")
|
||||
plt.xlabel("Voltage")
|
||||
title = 'Sensor MCP9701A #{}'.format(n)
|
||||
plt.title(title)
|
||||
plt.title("Sensor MCP9701A #{}".format(n))
|
||||
|
||||
# Scale the axis appropriately
|
||||
plt.axis((0.6, 2.0, 15.0, 70.0))
|
||||
|
||||
# Print a legend and set the graph to be annotated
|
||||
plt.legend(loc="lower right")
|
||||
plt.annotate(formula, xy=(0.85, 60))
|
||||
|
||||
# Enable the background grid
|
||||
plt.grid(True)
|
||||
|
||||
# Finally, show the graph
|
||||
plt.show()
|
||||
|
||||
# Get user input whether to save the plot or not
|
||||
saveit = input("Do you wish to save the plot? (y/N) ").lower()
|
||||
|
||||
if saveit == "y":
|
||||
plt.savefig("Sensor"+str(n)+".png")
|
||||
plt.savefig("Sensor"+str(n)+".pdf", format="pdf")
|
||||
plt.savefig("Sensor"+str(n)+".svg", format="svg")
|
||||
# Save the plot as Sensor[Number] (e.g. Sensor9) as png, pdf and svg
|
||||
plt.savefig("Sensor" + str(n) + ".png")
|
||||
plt.savefig("Sensor" + str(n) + ".pdf", format="pdf")
|
||||
plt.savefig("Sensor" + str(n) + ".svg", format="svg")
|
||||
print("==> Images saved")
|
||||
else:
|
||||
print("==> Images discarded")
|
||||
|
||||
|
||||
# Since we have defined a function above as a function, this here is executed first
|
||||
filename = input("Please enter a file path to the csv file to be plotted: ")
|
||||
|
||||
# Try to open the file
|
||||
try:
|
||||
file = open(filename, "r")
|
||||
generate_plot()
|
||||
|
||||
@@ -4,29 +4,36 @@ import matplotlib.pyplot as plt
|
||||
import csv
|
||||
import os
|
||||
|
||||
# Get user input for various data
|
||||
path = input("Path to csv-file to be plotted: ")
|
||||
print("For the below, it is recommended to enter data in this format: yyyy-mm-dd-hh-mm")
|
||||
date = input("Date & time at which the measurement was taken (approx.): ")
|
||||
group = input("Group-name: ")
|
||||
saveit = input("Should the graph be saved? (y/n) ").lower()
|
||||
|
||||
imp = open(path, "r")
|
||||
reader = csv.reader(imp, delimiter=',')
|
||||
rohdaten = list(reader)
|
||||
lenght = len(rohdaten)
|
||||
reader = csv.reader(imp, delimiter=",")
|
||||
data = list(reader)
|
||||
x = []
|
||||
y = []
|
||||
for i in range(lenght):
|
||||
extract = rohdaten.pop(0)
|
||||
for i in range(len(data)):
|
||||
# Extract the data
|
||||
extract = data.pop(0)
|
||||
x.append(float(extract.pop(0)))
|
||||
y.append(float(extract.pop(0)))
|
||||
|
||||
# Set up plot
|
||||
plt.plot(x, y, color="MAGENTA")
|
||||
plt.xlabel("Time")
|
||||
plt.ylabel("Voltage")
|
||||
title = f"GC - Biogasanlage {date}"
|
||||
plt.title(title)
|
||||
|
||||
plt.title(f"GC - Biogasanlage {date}")
|
||||
plt.grid(True)
|
||||
if saveit == "y":
|
||||
|
||||
# Check if user wants to save the image
|
||||
if saveit == "n":
|
||||
print("didn't save images")
|
||||
else:
|
||||
pos = 0
|
||||
for letter in path[::-1]:
|
||||
if letter == "/":
|
||||
@@ -40,11 +47,7 @@ if saveit == "y":
|
||||
os.mkdir(save_path)
|
||||
except FileExistsError:
|
||||
pass
|
||||
plt.savefig(save_path)
|
||||
os.rename(f"{save_path}/.png", f"{save_path}/GC-{date}-{group}.png")
|
||||
print(f"saved images to {save_path}")
|
||||
else:
|
||||
print("didn't save images")
|
||||
plt.savefig(f"{save_path}/GC-{date}-{group}.png")
|
||||
|
||||
print(f"Saved images to {save_path}")
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user