Skip to content

Commit dc89a81

Browse files
sadhana-rebrahimebrahim
authored andcommitted
Add ability to delete sessions (#414)
1 parent ac1bbcf commit dc89a81

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/openlifu/db/database.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,31 @@ def write_session(self, subject:Subject, session:Session, on_conflict=OnConflict
219219

220220
self.logger.info(f"Added session with ID {session_id} for subject {subject.id} to the database.")
221221

222+
def delete_session(self, subject_id: str, session_id: str, on_conflict: OnConflictOpts = OnConflictOpts.ERROR):
223+
224+
# Check if the session ID exists in the database for this subject
225+
session_ids = self.get_session_ids(subject_id)
226+
227+
if session_id not in session_ids:
228+
if on_conflict == OnConflictOpts.ERROR:
229+
raise ValueError(f"Session ID {session_id} does not exist in the database.")
230+
elif on_conflict == OnConflictOpts.SKIP:
231+
self.logger.info(f"Cannot delete session ID {session_id} as it does not exist in the database.")
232+
return
233+
else:
234+
raise ValueError("Invalid 'on_conflict' option.")
235+
236+
# Delete the session file
237+
session_filename = self.get_session_filename(subject_id, session_id)
238+
if Path.is_file(session_filename):
239+
shutil.rmtree(session_filename.parent)
240+
241+
if session_id in session_ids:
242+
session_ids.remove(session_id)
243+
self.write_session_ids(subject_id, session_ids)
244+
245+
self.logger.info(f"Removed Session with ID {session_id} from the database.")
246+
222247
def write_run(self, run:Run, session:Session = None, protocol:Protocol = None, on_conflict=OnConflictOpts.ERROR):
223248
"""Write a run with a snapshot of session and a snapshot of protocol if provided
224249

tests/test_database.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,35 @@ def test_write_session_with_transducer_tracking_results(example_database: Databa
327327
session.transducer_tracking_results = [example_transducer_tracking_result]
328328
example_database.write_session(example_subject, session, on_conflict = OnConflictOpts.OVERWRITE)
329329

330+
def test_delete_session(example_database: Database, example_subject: Subject):
331+
# Write a session
332+
session = Session(name="bleh", id='a_session',subject_id=example_subject.id)
333+
334+
session_id = session.id
335+
subject_id = example_subject.id
336+
337+
# Can add a new session, and it loads back in correctly.
338+
example_database.write_session(example_subject, session)
339+
340+
assert session.id in example_database.get_session_ids(example_subject.id)
341+
342+
# Session is deleted
343+
example_database.delete_session(subject_id, session_id)
344+
assert session.id not in example_database.get_session_ids(subject_id)
345+
with pytest.raises(FileNotFoundError):
346+
example_database.load_session(example_subject, session_id)
347+
348+
# Error option
349+
with pytest.raises(ValueError, match="does not exist in the database"):
350+
example_database.delete_session(subject_id, "non_existent_protocol", on_conflict=OnConflictOpts.ERROR)
351+
352+
# Skip option
353+
example_database.delete_session(subject_id, "non_existent_protocol", on_conflict=OnConflictOpts.SKIP)
354+
355+
# Invalid option
356+
with pytest.raises(ValueError, match="Invalid"):
357+
example_database.delete_session(subject_id, "non_existent_protocol", on_conflict=OnConflictOpts.OVERWRITE)
358+
330359
def test_write_run(example_database: Database, tmp_path:Path):
331360
subject_id = "example_subject"
332361
session_id = "example_session"

0 commit comments

Comments
 (0)