I have some trouble with an app I'm making, I receive data from a chip connected to 3 sensors then I save that data into a csv file (one csv file per day). It correctly saves the data for all the duration (albeit sometimes there is inexplicably some loss of a few seconds). But somehow when I display it in my graphs tab, either it shows correctly with no problems or, like this time, even though there is data at the recorded time, it displays it like there is none or shows the wrong values at the wrong time or it stays constant despite having increased the values...
Here the pH and temperature value should be going up starting 8:40 and not be continuous, the frequency of the points on the graph is every 10 seconds
the csv to show the values are not static outside of the DO
same thing here
here there should be values for every 30 minutes from the start until 9:00
csv shows again there are values recorded
and here it stops despite the csv having values, also those displayed on the graphs are wrong as per shown with the following extract of csv
the correct values in the csv
and here it shows there are values until 23:59:59
The error is probably linked to def get_today_csv and how I’m trying to format the hours and date onto the graphs:
class GraphMenu(Menu):
def __init__(self, master,
title: str, func: Callable,
_type: Literal["4H", "Total", "24H"]):
super().__init__(master=master, title=title)
self.extra = lambda: self.create_graphs()
self.get_data = func # Fonction pour obtenir les données du graphique
self.graphs: list[FigureCanvasTkAgg] = []
self._type = _type
self.displayed_graph: ctk.StringVar = self.master.displayed_graph
self.displayed_graph.trace_add("write", self.change_displayed_graph)
self.create_widgets()
def create_widgets(self) -> None:
…
def create_graphs(self, instant_reload: bool = False) -> None:
if not instant_reload:
self.graph_frame.place_forget()
self.loading_frame.place(relx=0, rely=0, relwidth=1, relheight=1)
self.update()
self.data = self.get_data()
self.graph_frame.place(
relx=0,
rely=0.125 if self._type == "4H" else 0.075,
relwidth=1,
relheight=0.8 if self._type == "4H" else 0.85
)
self.graph_do.initialize_data(data={"time": self.data["time"], "values": self.data["values"]["DO"]})
self.graph_ph.initialize_data(data={"time": self.data["time"], "values": self.data["values"]["pH"]})
self.graph_temp.initialize_data(data={"time": self.data["time"], "values": self.data["values"]["Temperature"]})
self.update()
def get_estimated_time(self) -> float: return 1.5
def get_today_data(self, graph_type: Literal["4H", "24H", "Total"],
selected_csv: str = None, data = None) -> dict[Literal["time", "values"], dict[Literal["DO", "pH", "Temperature"], list[float]]]:
timestamps_sorting: list[Literal[0, 1]]
data: dict[str, Union[list[datetime], dict[str, list[float]]]]
if graph_type == "4H":
if self.upper_edge.get() == "" or self.lower_edge.get() == "" :
return
self.upper_edge: IntVar
self.lower_edge: IntVar
if selected_csv is None:
selected_csv = self.master.csv_filename.get()
day_carry = 0
if graph_type == "4H":
current_time = dt.time(self.upper_edge.get()%24, 0, 0)
if self.upper_edge.get() == 24: day_carry = 1
else: current_time = dt.time(0, 0, 0)
selected_csv_date = datetime.strptime(selected_csv, "%Y-%m-%d").date()
now = datetime.combine(
date=selected_csv_date + timedelta(days=day_carry),
time=current_time
) # Creer un objet now pour si on veut changer de csv, met a jour l'affichage et la date
now_str = now.strftime("%Y/%m/%d-%H:%M:%S") # Pour avoir la date en date heure
lower_hour_time = dt.time((self.lower_edge.get()%24 if graph_type == "4H" else 0), 0, 0) # Pour lire la variable
lower_hour = datetime.combine(
date=selected_csv_date,
time=lower_hour_time
)
lower_hour_str = lower_hour.strftime("%Y/%m/%d-%H:%M:%S")
day_data = self.master.load_data(self.master.csv_filenames[selected_csv]) # Va chercher la data du csv actuel
day_data_values = list(day_data.values())[0]
timestamps: list[datetime] = [datetime.combine(now, datetime.strptime(t, "%H:%M:%S").time()) for t in day_data_values["Timestamp"]]
now += timedelta(days=1 if graph_type != "4H" else 0)
now_str = now.strftime("%Y/%m/%d-%H:%M:%S")
timestamps = pd.DatetimeIndex([t for t in timestamps if lower_hour <= t <= now])
dates = pd.date_range(start=lower_hour_str, end=now_str, freq="s")
timestamps_sorting = [] # Masque
i = 0
j = 0
if len(timestamps) > len(dates):
while j < len(dates) and i < len(timestamps):
recorded_timestamp = timestamps[j]
check_timestamp = dates[i]
if recorded_timestamp == check_timestamp:
timestamps_sorting.append(1)
i += 1
else:
timestamps_sorting.append(0)
j += 1
else:
while i < len(dates) and j < len(timestamps):
recorded_timestamp = timestamps[j]
check_timestamp = dates[i]
if recorded_timestamp == check_timestamp:
timestamps_sorting.append(1)
j += 1
else:
timestamps_sorting.append(0)
i += 1
if j == len(timestamps):
timestamps_sorting.extend([0] * (len(dates) - i))
if data is None:
data = {
"time": [],
"values": {
"DO": [],
"pH": [],
"Temperature": []
}
}
if graph_type == "Total":
timestamps_sorting = timestamps_sorting[1:]
lacking_data_offset = 0
# Combine time and value appending into a single loop with conditional assignment
for i, timestamp_sorting in enumerate(timestamps_sorting):
data["time"].append(dates[i])
if timestamp_sorting:
data["values"]["DO"].append(day_data_values["DO"][i - lacking_data_offset])
data["values"]["pH"].append(day_data_values["pH"][i - lacking_data_offset])
data["values"]["Temperature"].append(day_data_values["Temperature"][i - lacking_data_offset])
else:
data["values"]["DO"].append(-1)
data["values"]["pH"].append(-1)
data["values"]["Temperature"].append(-1)
lacking_data_offset += not timestamp_sorting # Increment lacking_data_offset only when there's missing data
return data