From 9b0c6256f2110b0332d6d3b08e0fbf2a4e1bad98 Mon Sep 17 00:00:00 2001 From: Walker Arce Date: Tue, 9 Aug 2022 21:34:01 -0500 Subject: [PATCH] Fix bug where loaded sessions persist event history --- config.yml | 2 +- output_view_ui.py | 9 +++- patient_data_fields.py | 97 ++++++++++++++++++++++++++++-------------- session_manager_ui.py | 19 +++++++-- ui_params.py | 2 +- 5 files changed, 89 insertions(+), 40 deletions(-) diff --git a/config.yml b/config.yml index b358498..6e02116 100644 --- a/config.yml +++ b/config.yml @@ -10,7 +10,7 @@ enable-review: false enable-woodway: false first-time: true fps: 8 -logs-dir: C:\cometrics\1.2.1\logs +logs-dir: C:\cometrics\1.3.2\logs patient-concerns: concerns.yml phases: - Assessment diff --git a/output_view_ui.py b/output_view_ui.py index e9251ca..54b229d 100644 --- a/output_view_ui.py +++ b/output_view_ui.py @@ -185,6 +185,10 @@ def start_session(self, recording_path=None): self.ble_view.start_session() if self.woodway_view: self.woodway_view.start_session() + if self.video_view: + self.video_view.clear_event_treeview() + if self.key_view: + self.key_view.clear_sh_treeview() def enable_video_slider(self): if self.video_view.player: @@ -1416,10 +1420,11 @@ def populate_event_treeview(self): def clear_event_treeview(self): clear_treeview(self.event_treeview) + self.event_treeview_parents = [] + self.event_history = [] def populate_event_treeview_review(self): self.reviewing = True - self.clear_event_treeview() if self.event_history: for i in range(0, len(self.event_history)): bind = self.event_history[i] @@ -2058,6 +2063,8 @@ def init_background_vars(self, keystroke_file): def clear_sh_treeview(self): clear_treeview(self.sh_treeview) + self.sh_treeview_parents = [] + self.event_history = [] def add_session_event(self, events): for event in events: diff --git a/patient_data_fields.py b/patient_data_fields.py index de8b6dd..daffa4e 100644 --- a/patient_data_fields.py +++ b/patient_data_fields.py @@ -193,12 +193,23 @@ def __init__(self, parent, x, y, height, width, patient_file, prim_session_numbe self.start_label = Label(self.patient_frames[frame_select], text="Session Start Time: " + session_time, anchor=NW, font=field_font) self.start_label.place(x=5, y=patient_y, anchor=NW) + info_count += 1 + info_count += 1 + if not info_count % field_count and frame_count != 1: + frame_select += 1 + patient_y = 30 + else: + patient_y += field_offset / 2 self.patient_vars[PatientDataVar.PATIENT_NAME].set(self.patient.name) if self.patient.medical_record_number: self.patient_vars[PatientDataVar.MRN].set(self.patient.medical_record_number) self.patient_vars[PatientDataVar.SESS_NUM].set(prim_session_number) + load_session_button = Button(self.patient_frames[frame_select], text="Load Existing Session", font=field_font, + command=self.check_load_session) + load_session_button.place(x=(width / 2), y=patient_y, anchor=N, width=width * 0.88) + self.current_patient_field = 0 self.patient_frames[self.current_patient_field].place(x=self.x, y=self.y) self.patient_entries[0].focus() @@ -224,7 +235,7 @@ def __init__(self, parent, x, y, height, width, patient_file, prim_session_numbe values=(bind[1],), tags=(treeview_bind_tags[i % 2])) - self.patient_vars[PatientDataVar.SESS_NUM].trace('w', self.check_load_session) + # self.patient_vars[PatientDataVar.SESS_NUM].trace('w', self.check_load_session) if debug: self.patient_vars[PatientDataVar.SESS_LOC].set("Debug") @@ -241,25 +252,42 @@ def show_dur_key(self, i): def hide_dur_key(self, i): self.bind_treeview.item(str(len(self.freq_bindings) + i), tags=treeview_bind_tags[(len(self.freq_bindings) + i) % 2]) - def check_load_session(self, *args): + def check_prim_sessions(self, session_number): + for prim_file in self.caller.prim_files: + with open(prim_file, 'r') as f: + json_file = json.load(f) + if str(session_number) == json_file['Session Number']: + return prim_file + return None + + def check_reli_sessions(self, session_number): + for reli_file in self.caller.reli_files: + with open(reli_file, 'r') as f: + json_file = json.load(f) + if str(session_number) == json_file['Session Number']: + return reli_file + return None + + def check_load_session(self): session_number = self.patient_vars[PatientDataVar.SESS_NUM].get() - if bool(re.match('^[0-9]+$', session_number)): - session_number = int(session_number) - 1 + if session_number: if self.patient_vars[PatientDataVar.PRIM_DATA].get() == "Primary": - if session_number < len(self.caller.prim_files): - print(f"INFO: Loading primary data session {self.patient_vars[PatientDataVar.SESS_NUM].get()}") - self.session_file = session_file = self.caller.prim_files[session_number] - session_video_file = os.path.join(pathlib.Path(session_file).parent, pathlib.Path(session_file).stem + ".mp4") - if os.path.exists(session_video_file): - self.caller.ovu.video_view.video_file = session_video_file - with open(session_file, 'r') as f: - self.session_json = session_json = json.load(f) - session_json_events = session_json["Event History"] - self.caller.ovu.key_view.clear_sh_treeview() - self.caller.ovu.key_view.add_session_event(session_json_events) - self.caller.ovu.video_view.add_event_history(session_json_events) - self.caller.ovu.video_view.populate_event_treeview_review() - self.session_loaded = True + self.session_file = session_file = self.check_prim_sessions(session_number) + if session_file: + if os.path.exists(session_file): + print(f"INFO: Loading primary data session {self.patient_vars[PatientDataVar.SESS_NUM].get()}") + session_video_file = os.path.join(pathlib.Path(session_file).parent, pathlib.Path(session_file).stem + ".mp4") + if os.path.exists(session_video_file): + self.caller.ovu.video_view.video_file = session_video_file + with open(session_file, 'r') as f: + self.session_json = session_json = json.load(f) + session_json_events = session_json["Event History"] + self.caller.ovu.key_view.clear_sh_treeview() + self.caller.ovu.video_view.clear_event_treeview() + self.caller.ovu.key_view.add_session_event(session_json_events) + self.caller.ovu.video_view.add_event_history(session_json_events) + self.caller.ovu.video_view.populate_event_treeview_review() + self.session_loaded = True else: self.caller.ovu.key_view.clear_sh_treeview() self.caller.ovu.video_view.clear_event_treeview() @@ -269,21 +297,24 @@ def check_load_session(self, *args): else: self.session_loaded = False elif self.patient_vars[PatientDataVar.PRIM_DATA].get() == "Reliability": - if session_number < len(self.caller.reli_files): - print(f"INFO: Loading reliability data session {self.patient_vars[PatientDataVar.SESS_NUM].get()}") - self.caller.ovu.key_view.clear_sh_treeview() - self.session_file = session_file = self.caller.reli_files[session_number] - session_video_file = os.path.join(pathlib.Path(session_file).parent, - pathlib.Path(session_file).stem + ".mp4") - if os.path.exists(session_video_file): - self.caller.ovu.video_view.video_file = session_video_file - with open(session_file, 'r') as f: - self.session_json = session_json = json.load(f) - session_json_events = session_json["Event History"] - self.caller.ovu.key_view.add_session_event(session_json_events) - self.caller.ovu.video_view.add_event_history(session_json_events) - self.caller.ovu.video_view.populate_event_treeview_review() - self.session_loaded = True + self.session_file = session_file = self.check_reli_sessions(session_number) + if session_file: + if os.path.exists(session_file): + print(f"INFO: Loading reliability data session {self.patient_vars[PatientDataVar.SESS_NUM].get()}") + self.caller.ovu.key_view.clear_sh_treeview() + session_video_file = os.path.join(pathlib.Path(session_file).parent, + pathlib.Path(session_file).stem + ".mp4") + if os.path.exists(session_video_file): + self.caller.ovu.video_view.video_file = session_video_file + with open(session_file, 'r') as f: + self.session_json = session_json = json.load(f) + session_json_events = session_json["Event History"] + self.caller.ovu.key_view.clear_sh_treeview() + self.caller.ovu.video_view.clear_event_treeview() + self.caller.ovu.key_view.add_session_event(session_json_events) + self.caller.ovu.video_view.add_event_history(session_json_events) + self.caller.ovu.video_view.populate_event_treeview_review() + self.session_loaded = True else: self.caller.ovu.key_view.clear_sh_treeview() self.caller.ovu.video_view.clear_event_treeview() diff --git a/session_manager_ui.py b/session_manager_ui.py index 5f1fd1c..76bb3be 100644 --- a/session_manager_ui.py +++ b/session_manager_ui.py @@ -314,7 +314,7 @@ def save_session(self): x = { "Session Date": self.session_date, "Session Start Time": self.session_time, - "Session Start Timestamp": EmpaticaE4.get_unix_timestamp(self.now), + "Session Start Timestamp": self.session_start_timestamp, "Session End Timestamp": EmpaticaE4.get_unix_timestamp(), "Session Time": self.stf.session_time, "Pause Time": self.stf.break_time, @@ -399,7 +399,20 @@ def start_session(self): "Woodway view is not present when it should be!") print("ERROR: Something went wrong with starting session, Woodway view is not present when it should be") return - + session_fields = self.pdf.get_session_fields() + reli = '_R' if session_fields["Primary Data"] == "Reliability" else '' + output_session_file = path.join(self.session_dir, + self.config.get_data_folders()[1], + session_fields["Primary Data"], + f"{session_fields['Session Number']}" + f"{session_fields['Assessment Name'][:2]}" + f"{session_fields['Condition Name'][:2]}" + f"{self.session_file_date}{reli}.json") + if os.path.exists(output_session_file): + messagebox.showerror("Session Exists", f"The selected session already exists!\n{output_session_file}") + print(f"ERROR: The selected session already exists {output_session_file}") + return + self.session_start_timestamp = EmpaticaE4.get_unix_timestamp() self.now = now = datetime.datetime.today() self.session_date = now.strftime("%B %d, %Y") self.session_file_date = now.strftime("%B")[:3] + now.strftime("%d") + now.strftime("%Y") @@ -409,8 +422,6 @@ def start_session(self): self.pdf.lock_session_fields() self.stf.lock_session_fields() # Start the session - session_fields = self.pdf.get_session_fields() - reli = '_R' if session_fields["Primary Data"] == "Reliability" else '' self.ovu.start_session(recording_path=path.join(self.session_dir, self.config.get_data_folders()[1], session_fields["Primary Data"], diff --git a/ui_params.py b/ui_params.py index c4f2319..0da2b81 100644 --- a/ui_params.py +++ b/ui_params.py @@ -1,4 +1,4 @@ -cometrics_version = "1.3.1" +cometrics_version = "1.3.2" ui_title = f"cometrics v{cometrics_version}" cometrics_data_root = fr'C:\cometrics'