Skip to content

Commit

Permalink
♻️ add more modules
Browse files Browse the repository at this point in the history
  • Loading branch information
FR-SON committed Jan 11, 2024
1 parent 24a0741 commit d3912bb
Show file tree
Hide file tree
Showing 10 changed files with 352 additions and 130 deletions.
5 changes: 3 additions & 2 deletions tracex/extraction/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""App configuration for extraction app."""
from django.apps import AppConfig
from extraction.logic import orchestrator


class ExtractionConfig(AppConfig):
Expand All @@ -9,6 +10,6 @@ class ExtractionConfig(AppConfig):
name = "extraction"

def ready(self):
from extraction.logic import orchestrator
orchestrator = orchestrator.Orchestrator()
orchestrator_instance = orchestrator.Orchestrator()
orchestrator.Orchestrator._instance = orchestrator_instance
print("Orchestrator ready")
188 changes: 90 additions & 98 deletions tracex/extraction/content/outputs/single_trace_event_type.xes
Original file line number Diff line number Diff line change
@@ -1,176 +1,168 @@
<?xml version="1.0" encoding="utf-8" ?>
<log xes.version="1849-2016" xes.features="nested-attributes" xmlns="http://www.xes-standard.org/">
<extension name="Concept" prefix="concept" uri="http://www.xes-standard.org/concept.xesext" />
<extension name="Time" prefix="time" uri="http://www.xes-standard.org/time.xesext" />
<extension name="Concept" prefix="concept" uri="http://www.xes-standard.org/concept.xesext" />
<string key="origin" value="csv" />
<trace>
<string key="concept:name" value="1" />
<event>
<string key="event_information" value="Experiencing mild symptoms" />
<date key="time:timestamp" value="2021-07-15T00:00:00" />
<date key="time:endDate" value="2021-07-15T00:00:00" />
<string key="event_information" value="Starting to experience symptoms: persistent cough/fatigue" />
<date key="time:timestamp" value="2022-07-15T00:00:00" />
<date key="time:endDate" value="2022-07-15T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Symptom Onset" />
<string key="attribute_location" value="Home" />
</event>
<event>
<string key="event_information" value="Isolating at home" />
<date key="time:timestamp" value="2021-07-15T00:00:00" />
<date key="time:endDate" value="2021-07-15T00:00:00" />
<string key="event_information" value="Taking extended leave from work" />
<date key="time:timestamp" value="2022-07-16T00:00:00" />
<date key="time:endDate" value="2022-07-16T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Lifestyle Change" />
<string key="attribute_location" value="Home" />
</event>
<event>
<string key="event_information" value="Informing school administration" />
<date key="time:timestamp" value="2021-07-15T00:00:00" />
<date key="time:endDate" value="2021-07-15T00:00:00" />
<string key="event_information" value="Contacting family doctor" />
<date key="time:timestamp" value="2022-07-19T00:00:00" />
<date key="time:endDate" value="2022-07-19T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Other" />
<string key="attribute_location" value="Home" />
<string key="concept:name" value="Doctor visit" />
<string key="attribute_location" value="Doctors" />
</event>
<event>
<string key="event_information" value="Symptoms worsening" />
<date key="time:timestamp" value="2021-07-19T00:00:00" />
<date key="time:endDate" value="2021-07-19T00:00:00" />
<string key="event_information" value="Going to doctor's clinic for examination" />
<date key="time:timestamp" value="2022-07-20T00:00:00" />
<date key="time:endDate" value="2022-07-20T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Symptom Onset" />
<string key="attribute_location" value="Home" />
<string key="concept:name" value="Doctor visit" />
<string key="attribute_location" value="Doctors" />
</event>
<event>
<string key="event_information" value="Developing high fever" />
<date key="time:timestamp" value="2021-07-19T00:00:00" />
<date key="time:endDate" value="2021-07-19T00:00:00" />
<string key="event_information" value="Confirming Covid-19 diagnosis" />
<date key="time:timestamp" value="2022-07-20T00:00:00" />
<date key="time:endDate" value="2022-07-20T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Symptom Onset" />
<string key="attribute_location" value="Home" />
<string key="concept:name" value="Diagnosis" />
<string key="attribute_location" value="Doctors" />
</event>
<event>
<string key="event_information" value="Experiencing difficulty breathing" />
<date key="time:timestamp" value="2021-07-19T00:00:00" />
<date key="time:endDate" value="2021-07-19T00:00:00" />
<string key="event_information" value="Isolating at home to prevent spreading" />
<date key="time:timestamp" value="2022-07-20T00:00:00" />
<date key="time:endDate" value="2022-07-20T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Symptom Onset" />
<string key="attribute_location" value="Home" />
</event>
<event>
<string key="event_information" value="Monitoring symptoms at home" />
<date key="time:timestamp" value="2021-07-19T00:00:00" />
<date key="time:endDate" value="2021-07-24T00:00:00" />
<string key="time:duration" value="5 days 00:00:00" />
<string key="concept:name" value="Other" />
<string key="concept:name" value="Lifestyle Change" />
<string key="attribute_location" value="Home" />
</event>
<event>
<string key="event_information" value="Seeking medical help" />
<date key="time:timestamp" value="2021-07-24T00:00:00" />
<date key="time:endDate" value="2021-07-24T00:00:00" />
<string key="event_information" value="Prescribed medications for symptom relief" />
<date key="time:timestamp" value="2022-07-20T00:00:00" />
<date key="time:endDate" value="2022-07-20T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Doctor visit" />
<string key="concept:name" value="Medication" />
<string key="attribute_location" value="Doctors" />
</event>
<event>
<string key="event_information" value="Visiting local hospital" />
<date key="time:timestamp" value="2021-07-24T00:00:00" />
<date key="time:endDate" value="2021-07-24T00:00:00" />
<string key="event_information" value="Monitoring condition closely" />
<date key="time:timestamp" value="2022-07-20T00:00:00" />
<date key="time:endDate" value="2022-07-20T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Doctor visit" />
<string key="attribute_location" value="Hospital" />
<string key="concept:name" value="Treatment" />
<string key="attribute_location" value="Home" />
</event>
<event>
<string key="event_information" value="Undergoing tests" />
<date key="time:timestamp" value="2021-07-24T00:00:00" />
<date key="time:endDate" value="2021-07-24T00:00:00" />
<string key="event_information" value="Informing school about positive test result" />
<date key="time:timestamp" value="2022-07-20T00:00:00" />
<date key="time:endDate" value="2022-07-20T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Other" />
<string key="attribute_location" value="Hospital" />
</event>
<event>
<string key="event_information" value="Admitted to hospital" />
<date key="time:timestamp" value="2021-07-24T00:00:00" />
<date key="time:endDate" value="2021-07-31T00:00:00" />
<string key="time:duration" value="7 days 00:00:00" />
<string key="concept:name" value="Hospital admission" />
<string key="attribute_location" value="Hospital" />
<string key="concept:name" value="Doctor visit" />
<string key="attribute_location" value="Doctors" />
</event>
<event>
<string key="event_information" value="Receiving treatments" />
<date key="time:timestamp" value="2021-07-24T00:00:00" />
<date key="time:endDate" value="2021-07-31T00:00:00" />
<string key="time:duration" value="7 days 00:00:00" />
<string key="event_information" value="Receiving support from school administration" />
<date key="time:timestamp" value="2022-07-20T00:00:00" />
<date key="time:endDate" value="2022-07-20T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Treatment" />
<string key="attribute_location" value="Hospital" />
<string key="attribute_location" value="Home" />
</event>
<event>
<string key="event_information" value="Showing signs of improvement" />
<date key="time:timestamp" value="2021-07-31T00:00:00" />
<date key="time:endDate" value="2021-07-31T00:00:00" />
<string key="event_information" value="Symptoms persisting" />
<date key="time:timestamp" value="2022-07-20T00:00:00" />
<date key="time:endDate" value="2022-07-20T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Other" />
<string key="attribute_location" value="Hospital" />
<string key="concept:name" value="Symptom Onset" />
<string key="attribute_location" value="Home" />
</event>
<event>
<string key="event_information" value="Discharged from hospital" />
<date key="time:timestamp" value="2021-08-01T00:00:00" />
<date key="time:endDate" value="2021-08-01T00:00:00" />
<string key="event_information" value="Difficulty participating in family activities" />
<date key="time:timestamp" value="2022-07-20T00:00:00" />
<date key="time:endDate" value="2022-07-20T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Hospital discharge" />
<string key="attribute_location" value="Hospital" />
<string key="concept:name" value="Lifestyle Change" />
<string key="attribute_location" value="Home" />
</event>
<event>
<string key="event_information" value="Continuing self-isolating at home" />
<date key="time:timestamp" value="2021-08-01T00:00:00" />
<date key="time:endDate" value="2021-08-01T00:00:00" />
<string key="event_information" value="Receiving extra care from wife" />
<date key="time:timestamp" value="2022-07-20T00:00:00" />
<date key="time:endDate" value="2022-07-20T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Lifestyle Change" />
<string key="concept:name" value="Treatment" />
<string key="attribute_location" value="Home" />
</event>
<event>
<string key="event_information" value="Monitoring symptoms closely" />
<date key="time:timestamp" value="2021-08-01T00:00:00" />
<date key="time:endDate" value="2021-08-01T00:00:00" />
<string key="event_information" value="Children being supportive" />
<date key="time:timestamp" value="2022-07-20T00:00:00" />
<date key="time:endDate" value="2022-07-20T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Other" />
<string key="concept:name" value="Feelings" />
<string key="attribute_location" value="Home" />
</event>
<event>
<string key="event_information" value="Receiving Covid-19 vaccine" />
<date key="time:timestamp" value="2022-02-15T00:00:00" />
<date key="time:endDate" value="2022-02-15T00:00:00" />
<string key="event_information" value="Receiving first dose of Covid-19 vaccine" />
<date key="time:timestamp" value="2022-08-25T00:00:00" />
<date key="time:endDate" value="2022-08-25T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Treatment" />
<string key="attribute_location" value="Doctors" />
</event>
<event>
<string key="event_information" value="Taking necessary precautions" />
<date key="time:timestamp" value="2022-02-15T00:00:00" />
<date key="time:endDate" value="2022-02-15T00:00:00" />
<string key="event_information" value="Receiving second dose of Covid-19 vaccine" />
<date key="time:timestamp" value="2022-09-22T00:00:00" />
<date key="time:endDate" value="2022-09-22T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Lifestyle Change" />
<string key="attribute_location" value="Home" />
<string key="concept:name" value="Treatment" />
<string key="attribute_location" value="Doctors" />
</event>
<event>
<string key="event_information" value="Receiving second vaccine dose" />
<date key="time:timestamp" value="2022-03-08T00:00:00" />
<date key="time:endDate" value="2022-03-08T00:00:00" />
<string key="event_information" value="Completing vaccination process" />
<date key="time:timestamp" value="2022-09-22T00:00:00" />
<date key="time:endDate" value="2022-09-22T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Treatment" />
<string key="attribute_location" value="Doctors" />
</event>
<event>
<string key="event_information" value="Support from family/friends/and colleagues" />
<date key="time:timestamp" value="2021-07-15T00:00:00" />
<date key="time:endDate" value="2021-07-15T00:00:00" />
<string key="event_information" value="Alleviating concerns about future infections" />
<date key="time:timestamp" value="2022-09-22T00:00:00" />
<date key="time:endDate" value="2022-09-22T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Other" />
<string key="concept:name" value="Treatment" />
<string key="attribute_location" value="Doctors" />
</event>
<event>
<string key="event_information" value="Navigating through infection" />
<date key="time:timestamp" value="2022-09-22T00:00:00" />
<date key="time:endDate" value="2022-09-22T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Treatment" />
<string key="attribute_location" value="Home" />
</event>
<event>
<string key="event_information" value="Importance of health and resilience" />
<date key="time:timestamp" value="2021-07-15T00:00:00" />
<date key="time:endDate" value="2021-07-15T00:00:00" />
<string key="event_information" value="Taking proactive step towards protection" />
<date key="time:timestamp" value="2022-09-22T00:00:00" />
<date key="time:endDate" value="2022-09-22T00:00:00" />
<string key="time:duration" value="0 days 00:00:00" />
<string key="concept:name" value="Other" />
<string key="concept:name" value="Lifestyle Change" />
<string key="attribute_location" value="Home" />
</event>
</trace>
Expand Down
42 changes: 22 additions & 20 deletions tracex/extraction/logic/module.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import os
from abc import ABC
from pandas import DataFrame
from abc import ABC, abstractmethod


class Module(ABC):
"""
This abstract base class defines a common interface for all concrete modules.
"""

def __init__(self, name, description):
"""
Initializes a module with the following parameters.
name: The name of the module.
description: A description of what the module does.
patient_journey: The patient journey most modules operate on.
result: The result that the module provides.
"""
self.name = name
self.description = description
self.patient_journey = None
self.result = None

def __str__(self):
return self.name

def __repr__(self):
return self.__str__()

def execute(self) -> DataFrame:
pass


class ActivityLabeler(Module):
def execute(self):
pass
@abstractmethod
def execute(self, _input, patient_journey=None):
"""
Executes the logic of the module. Override this to define your own module.
Every module receives the patient journey as parameter which is set to the instance variable of each module.
This method should always save a dataframe in the "result" instance variable for internal processing.
"""
self.patient_journey = patient_journey


class PreProcessor(Module):
pass


class TimeExtractor(Module):
pass


class LocationExtractor(Module):
pass

Expand Down
3 changes: 3 additions & 0 deletions tracex/extraction/logic/modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from .module_patient_journey_generator import PatientJourneyGenerator
from .module_activity_labeler import ActivityLabeler
from .module_time_extractor import TimeExtractor
from .module_location_extractor import LocationExtractor
48 changes: 48 additions & 0 deletions tracex/extraction/logic/modules/module_activity_labeler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from ..module import Module
from .. import utils as u
from .. import prompts as p

from pandas import DataFrame


class ActivityLabeler(Module):
# Remove this, only for test purposes
def __init__(self, name, description):
super().__init__(name, description)
print("ActivityLabeler module is ready")

def execute(self, _input, patient_journey=None):
super().execute(_input, patient_journey)
self.result = self.extract_activities()

# TODO: Convert to dataframes
def extract_activities(self):
"""Converts the input text to activity_labels."""
messages = [
{"role": "system", "content": p.TXT_TO_BULLETPOINTS_CONTEXT},
{
"role": "user",
"content": p.TXT_TO_BULLETPOINTS_PROMPT + self.patient_journey,
},
{"role": "assistant", "content": p.TXT_TO_BULLETPOINTS_ANSWER},
]
activity_labels = u.query_gpt(messages)
activity_labels = self._remove_commas(activity_labels)
activity_labels = self._add_ending_commas(activity_labels)
with open((u.output_path / "intermediates/1_bulletpoints.txt"), "w") as f:
f.write(activity_labels)
return activity_labels

@staticmethod
def _remove_commas(activity_labels):
"""Removes commas from within the activity_labels."""
activity_labels = activity_labels.replace(", ", "/")
activity_labels = activity_labels.replace(",", "/")
return activity_labels

@staticmethod
def _add_ending_commas(activity_labels):
"""Adds commas at the end of each line."""
activity_labels = activity_labels.replace("\n", ",\n")
activity_labels = activity_labels + ","
return activity_labels
Loading

0 comments on commit d3912bb

Please sign in to comment.