add transform documentation

This commit is contained in:
WorldTeacher
2024-06-03 13:57:57 +02:00
parent be50b91feb
commit 6bb89b0e94

View File

@@ -19,11 +19,19 @@ def createDateList(start_date, end_date):
class Transform:
"""Takes the raw json data and creates a dictionary containing a dictionary per sensor. The inner dictionary contains the date as x and the number of activations as y.
"""
def __init__(self, data_source):
self.data_source = data_source
self.data = None
def load_data(self):
def load_data(self)->"Transform":
"""loads the data from the data source and stores it in the data attribute
Returns:
self: The instance of the class
"""
with open(self.data_source, "r") as file:
self.data = json.load(file)
return self
@@ -59,31 +67,21 @@ class Transform:
if addMissing:
tmp[name]["x"].append(date)
tmp[name]["y"].append(0)
else:
on_state = len(sensor_data[date]["on"])
off_state = len(sensor_data[date]["off"])
activations = (on_state + off_state) / 2
if on_state != off_state:
# print("Error: On and off states are not equal", name, date)
tmp[name]["x"].append(date)
tmp[name]["y"].append(activations)
else:
# add the date to the tmp dictionary as x and the number of activations, divided by two as y
tmp[name]["x"].append(date)
tmp[name]["y"].append(activations if activations > 1 else 0)
# print(tmp)
# if split:
# #remove all values that are not in the range of the start and end date
# for i in range(len(tmp[name]["x"])):
# if tmp[name]["x"][i] <= start_date or tmp[name]["x"][i] >= end_date:
# print("Removing value", tmp[name]["x"][i], tmp[name]["y"][i])
# tmp[name]["x"][i] = None
# tmp[name]["y"][i] = None
# tmp[name]["x"] = [x for x in tmp[name]["x"] if x]
# tmp[name]["y"] = [y for y in tmp[name]["y"] if y]
# add the date to the tmp dictionary as x and the number of activations as y
tmp[name]["x"].append(date)
tmp[name]["y"].append(activations)
# if on_state != off_state:
# tmp[name]["x"].append(date)
# tmp[name]["y"].append(activations)
# else:
# # add the date to the tmp dictionary as x and the number of activations, divided by two as y
# tmp[name]["x"].append(date)
# tmp[name]["y"].append(activations if activations > 1 else 0)
temp.update(tmp)
return temp