Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
Solves issues with temperature column name KeyError
Browse files Browse the repository at this point in the history
  • Loading branch information
fullonic committed Feb 3, 2020
1 parent c124bd7 commit ab1ce9c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 26 deletions.
5 changes: 3 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ FrontEnd
- [ ] Delete volume from pump control settings
- [ ] Add setting to change default saved DF file name
- [ ] Add setting to change default saved plot file name
- [ ] Add possibility to save plot as png
- [ ] Improve plot
-------------------------------
- [ ] Change "process please" name when "abrir grafic" is selected
- [ ] Only allow one checkbox selected at the time
16 changes: 5 additions & 11 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,7 @@ def process_excel_files(
def landing():
"""Endpoint dispatcher to redirect user to the proper route."""
# Check if user is authenticated
if not (session.get("auth", False)):
flash(
f"{greeting()}, primer cal iniciar la sessió abans d’utilitzar aquesta aplicació",
"info",
)
return redirect(url_for("excel_files"))
else:
flash(f"Hey {greeting()}, benvingut {session['username']}", "info")
return redirect(url_for("login"))
return redirect(url_for("excel_files"))


@app.route("/excel_files", methods=["POST", "GET"])
Expand Down Expand Up @@ -227,8 +219,10 @@ def excel_files():
wait = int(request.form.get("wait"))
close = int(request.form.get("close"))
plot = True if request.form.get("plot") else False # if generate or no loop plots
ignore_loops = [int(loop) for loop in request.form["ignore_loops"].split(",")]
print(f"{ignore_loops=}")
try:
ignore_loops = [int(loop) for loop in request.form["ignore_loops"].split(",")]
except ValueError: # If user didn't insert any value
ignore_loops = None

# Show preview plot if user wants
if request.form.get("experiment_plot"):
Expand Down
25 changes: 19 additions & 6 deletions scripts/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,21 @@ def __init__(
self.ignore_loops = ignore_loops or []

def format_file(self, original_file):
"""For OLSystem output file into a pandas DF."""
txt_file = FileFormater(original_file)
txt_file.to_dataframe()
df = txt_file.df
for col in df.columns[1:]:
df[col] = df[col].astype(str)
self.df = df

# Solves issues with temperature symbol °C or ?C. Outputs always °C
columns_name = list(self.df.columns)
if "SDWA0003000061 , CH 1 temp [?C]" in self.df.columns:
idx = columns_name.index("SDWA0003000061 , CH 1 temp [?C]")
columns_name.remove("SDWA0003000061 , CH 1 temp [?C]")
columns_name.insert(idx, "SDWA0003000061 , CH 1 temp [°C]")

self.df.columns = columns_name
self.original_file = txt_file
self.experiment_plot()

Expand Down Expand Up @@ -255,6 +263,7 @@ def _close_df(self, start: datetime, end: datetime) -> pd.DataFrame:

@progress_bar
def create_plot(self, format_="html"):
"""Proxy for Plot object."""
print("Creating Plots", end="\n")
step = 100 / len(self.df_close_list)
for i, df_close in enumerate(self.df_close_list):
Expand All @@ -272,13 +281,16 @@ def create_plot(self, format_="html"):
yield round(step * k)

def save(self, df_loop, name):
"""Save data frame into a excel file."""
return df_loop.to_excel(f"{self.original_file.folder_dst}/df_loop_{name}.xlsx")


class Plot:
"""Generate all necessary kind of application plots."""

def __init__(
self, data, x_axis, y_axis, title, *, dst=None, fname="dataframe", output="html",
):
): # noqa
self.data = data
self.x_axis = x_axis
self.y_axis = y_axis
Expand All @@ -288,6 +300,7 @@ def __init__(
self.dst = dst

def create(self):
"""Create a plot for each close loop data."""
x = self.data[self.x_axis]
y = self.data[self.y_axis]
fig = go.Figure()
Expand All @@ -304,10 +317,11 @@ def create(self):
fig.write_html(f"{self.dst}/{self.fname}.{self.output}")

def simple_plot(self, markers=[]):
"""Plot all information from document before any kind of data manipulation."""
from plotly.subplots import make_subplots

x = self.data[self.x_axis][:100]
y = self.data[self.y_axis][:100]
x = self.data[self.x_axis]
y = self.data[self.y_axis]
# fig = go.Figure()
# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])
Expand All @@ -318,8 +332,7 @@ def simple_plot(self, markers=[]):
secondary_y=False,
)
temp = [
string_to_float(t)
for t in list(self.data["SDWA0003000061 , CH 1 temp [?C]"])[:100]
string_to_float(t) for t in list(self.data["SDWA0003000061 , CH 1 temp [°C]"])
]

fig.add_trace(
Expand Down
2 changes: 1 addition & 1 deletion scripts/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def generate_resume(self, control):
"min O2 [mgO2/L]": O2.min,
"avg O2 [mgO2/L]": O2.avg,
"avg temp [°C]": temp_mean(
df_close["SDWA0003000061 , CH 1 temp [?C]"]
df_close["SDWA0003000061 , CH 1 temp [°C]"]
),
"O2 after blank": O2_HR - control,
}
Expand Down
23 changes: 17 additions & 6 deletions tests/test_file_convertion.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import time

import sys
sys.path.append("/home/somnium/Desktop/Projects/resPI/")
# import queue
# from threading import Thread

from scripts import Plot, ExperimentCycle, ControlFile, Control, ResumeDataFrame, FileFormater
from scripts.utils import string_to_float

# now = time.perf_counter()
C1 = "/home/somnium/Desktop/ANGULA/RealData/D2/blanc16.txt"
C2 = "/home/somnium/Desktop/ANGULA/RealData/D2/blanc16.txt"
# file_path = "/home/somnium/Desktop/ANGULA/RealData/D2/blanc16.txt"
file_path = "/home/somnium/Desktop/ANGULA/RealData/D2/nitota.txt"
C1 = "/home/somnium/Desktop/ANGULA/RealData/D3/control1.txt"
C2 = "/home/somnium/Desktop/ANGULA/RealData/D3/control2.txt"
file_path = "/home/somnium/Desktop/ANGULA/RealData/D3/Angula.txt"
dst = "/home/somnium/Desktop/ANGULA/RealData"
flush, wait, close = 3, 10, 40
#
Expand All @@ -19,7 +19,7 @@
C_Total = Control(C)
C_Total.get_bank()
control = C_Total.calculate_blank()
experiment = ExperimentCycle(flush, wait, close, file_path, ignore_loops=[1,2,3,4,5])
experiment = ExperimentCycle(flush, wait, close, file_path)
lst = experiment.df_close_list
len(lst)

Expand All @@ -33,3 +33,14 @@

# f = FileFormater(C1)
# f.to_dataframe()


lst = ['Date &Time [DD-MM-YYYY HH:MM:SS]', 'Time stamp code',
'Barometric pressure [hPa]', 'SDWA0003000061 , CH 1 phase [r.U.]',
'SDWA0003000061 , CH 1 temp [°C]',
'SDWA0003000061 , CH 1 O2 [mg/L]']

if "SDWA0003000061 , CH 1 temp [?C]" in lst:
idx = lst.index("SDWA0003000061 , CH 1 temp [?C]")
lst.remove("SDWA0003000061 , CH 1 temp [?C]")
lst.insert(idx, "SDWA0003000061 , CH 1 temp [°C]")

0 comments on commit ab1ce9c

Please sign in to comment.