Skip to content

Commit

Permalink
added test mode trace testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tkv29 committed Jun 6, 2024
1 parent a76c85f commit 1184b24
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 29 deletions.
3 changes: 2 additions & 1 deletion tracex_project/extraction/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ def form_valid(self, form):
self.request.session["selected_modules"] = selected_modules

if self.request.session.get("is_comparing") is True:
orchestrator.save_results_to_db()
if not TEST_MODE:
orchestrator.save_results_to_db()

return redirect(reverse_lazy("testing_comparison"))

Expand Down
129 changes: 107 additions & 22 deletions tracex_project/trace_comparator/comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,99 @@ def compare_traces(
has no match as 'unexpected'. Finally, determine activities from the pipeline that are correctly
matched but in the wrong order.
"""
pipeline_activities: pd.Series = pipeline_df["activity"]
ground_truth_activities: pd.Series = ground_truth_df["activity"]

(
mapping_pipeline_to_ground_truth,
mapping_ground_truth_to_pipeline,
) = find_activity_mapping(view, pipeline_activities, ground_truth_activities)
missing_activities: List[str] = find_unmapped_activities(
ground_truth_activities, mapping_ground_truth_to_pipeline
)
unexpected_activities: List[str] = find_unmapped_activities(
pipeline_activities, mapping_pipeline_to_ground_truth
)
wrong_orders: List[Tuple[str, str]] = find_wrong_orders(
pipeline_activities, mapping_ground_truth_to_pipeline
)
if c.TEST_MODE:
simulate_progress(view)

mapping_pipeline_to_ground_truth = [
0,
-1,
-1,
5,
4,
5,
7,
-1,
8,
7,
-1,
-1,
-1,
-1,
15,
15,
]
mapping_ground_truth_to_pipeline = [
0,
-1,
-1,
5,
4,
3,
5,
6,
8,
8,
-1,
-1,
14,
14,
-1,
14,
14,
]
missing_activities = [
"isolating myself",
"informing family",
"experiencing slow recovery",
"returning to work with precautions",
"remainding optimistic and adhering to safety guidelines",
]
unexpected_activities = [
"taking immediate action",
"isolating and informing family",
"receiving doctor's recommendations for treatment",
"receiving support from children",
"slowly recovering from infection",
"returning to work with precautions",
"improvement in Covid-19 situation",
"receiving first dose of vaccine",
]
wrong_orders = [
("getting tested for Covid-19", "worsening symptoms and consulting doctor"),
("getting tested for Covid-19", "facing financial difficulties"),
(
"worsening symptoms and consulting doctor",
"facing financial difficulties",
),
]
matching_percent_ground_truth_to_pipeline = 62
matching_percent_pipeline_to_ground_truth = 72


else:
pipeline_activities: pd.Series = pipeline_df["activity"]
ground_truth_activities: pd.Series = ground_truth_df["activity"]

(
mapping_pipeline_to_ground_truth,
mapping_ground_truth_to_pipeline,
) = find_activity_mapping(view, pipeline_activities, ground_truth_activities)
missing_activities: List[str] = find_unmapped_activities(
ground_truth_activities, mapping_ground_truth_to_pipeline
)
unexpected_activities: List[str] = find_unmapped_activities(
pipeline_activities, mapping_pipeline_to_ground_truth
)
wrong_orders: List[Tuple[str, str]] = find_wrong_orders(
pipeline_activities, mapping_ground_truth_to_pipeline
)

matching_percent_pipeline_to_ground_truth: int = find_matching_percentage(
pipeline_activities, mapping_pipeline_to_ground_truth
)
matching_percent_ground_truth_to_pipeline: int = find_matching_percentage(
ground_truth_activities, mapping_ground_truth_to_pipeline
)
matching_percent_pipeline_to_ground_truth: int = find_matching_percentage(
pipeline_activities, mapping_pipeline_to_ground_truth
)
matching_percent_ground_truth_to_pipeline: int = find_matching_percentage(
ground_truth_activities, mapping_ground_truth_to_pipeline
)

results: dict = {
"mapping_pipeline_to_ground_truth": mapping_pipeline_to_ground_truth,
Expand All @@ -57,6 +127,21 @@ def compare_traces(

return results

def simulate_progress(view):
count = 0
while count <= 50:
count += 5
time.sleep(1)
update_progress(
view, count, 100, "Mapping Pipeline Activites to Ground Truth Activites"
)

while count <= 100:
count += 5
time.sleep(1)
update_progress(
view, count, 100, "Mapping Ground Truth Activites to Pipeline Activites"
)

def find_activity_mapping(
view, pipeline_activities: pd.Series, ground_truth_activities: pd.Series
Expand Down
20 changes: 14 additions & 6 deletions tracex_project/trace_comparator/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from trace_comparator.comparator import compare_traces
from trace_comparator.forms import PatientJourneySelectForm
from tracex.logic.utils import DataFrameUtilities, Conversion

from tracex.logic.constants import TEST_MODE

class TraceComparisonMixin(View):
"""Mixin providing functionality that is used in multiple views in the trace comparator app."""
Expand Down Expand Up @@ -74,7 +74,10 @@ class TraceTestingComparisonView(TemplateView, TraceComparisonMixin):
def get_context_data(self, **kwargs):
"""Prepare the latest trace of the selected Patient Journey that is available in the database for display."""
context = super().get_context_data(**kwargs)
patient_journey_name: str = self.request.session.get("patient_journey_name")
if TEST_MODE:
patient_journey_name = "journey_comparison_1"
else:
patient_journey_name: str = self.request.session.get("patient_journey_name")
patient_journey: str = PatientJourney.manager.get(
name=patient_journey_name
).patient_journey
Expand Down Expand Up @@ -115,10 +118,15 @@ def get(self, request, *args, **kwargs):

def post(self, request):
"""Compare the newest trace of a Patient Journey against the ground truth and update session with results."""
patient_journey_name: str = self.request.session.get("patient_journey_name")
ground_truth_df, pipeline_df = self.get_first_and_last_trace(
patient_journey_name
)
if not TEST_MODE:
patient_journey_name: str = self.request.session.get("patient_journey_name")
ground_truth_df, pipeline_df = self.get_first_and_last_trace(
patient_journey_name
)

else:
pipeline_df = []
ground_truth_df = []

try:
comparison_result_dict: dict = compare_traces(
Expand Down

0 comments on commit 1184b24

Please sign in to comment.