CoolFace
Apppublic

MrFrank99/processmining

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
process_mining.py169 linesDownload Raw Back to root
1'''2#---------------------3#import libraries4#---------------------5 6 7#---------------------8#disable util9import os10os.environ["PM4PY_NO_PSUTIL"] = "1"  # Disable psutil usage11import pm4py  # Now import pm4py safely12#---------------------13 14import pandas as pd15import numpy as np16from datetime import datetime, timedelta17import random18 19#from pm4py.objects.conversion.log import converter as log_converter20#from pm4py.objects.log.util import dataframe_utils21from pm4py.algo.discovery.heuristics import algorithm as heuristics_miner22from pm4py.algo.discovery.dfg import algorithm as dfg_discovery23from pm4py.visualization.heuristics_net import visualizer as hn_visualizer24from pm4py.visualization.dfg import visualizer as dfg_visualization25#import pm4py26 27import matplotlib.pyplot as plt28import seaborn as sns29 30 31#---------------------32#create functions33#---------------------34 35# Function to generate incrementally increasing timestamps36def generate_timestamps(num_events, start_time):37  np.random.seed(42)38  timestamps = [start_time]39  for _ in range(1, num_events):40      # Add a random time increment between 10 and 45 minutes41      increment = timedelta(minutes=random.randint(10, 45))42      next_timestamp = timestamps[-1] + increment43      timestamps.append(next_timestamp)44  return timestamps45 46#---------------------47 48# Function to generate the process log49def generate_patient_process_log(num_patients=100, prop_direct_to_doctor=0.1, prop_from_triage_to_doctor=0.2, start_time_param=datetime.now()):50    np.random.seed(42)51    random.seed(42)  # Ensure reproducibility of random results52    data = []53    events_standard_pathway = ["Arrival", "Nurse Triage", "Nurse Consultation", "End"]54    events_direct_to_doctor = ["Arrival", "Doctor Consultation", "End"]55    events_triage_to_doctor = ["Arrival", "Nurse Triage", "Doctor Consultation", "End"]56 57    for patient_id in range(1, num_patients + 1):58        # Randomly determine the patient's pathway59        rand_value = random.random()60        if rand_value < prop_direct_to_doctor:61            # Pathway: Arrival -> Doctor Consultation -> End62            events = events_direct_to_doctor63        elif rand_value < (prop_direct_to_doctor + prop_from_triage_to_doctor):64            # Pathway: Arrival -> Nurse Triage -> Doctor Consultation -> End65            events = events_triage_to_doctor66        else:67            # Pathway: Arrival -> Nurse Triage -> Nurse Consultation -> End68            events = events_standard_pathway69 70        # Generate timestamps for the patient's pathway71        if patient_id == 1:72            # Use the specified start time for the first patient73            start_time = start_time_param74        else:75            # Use the end time of the last activity for the next patient76            start_time = timestamps[-1] + timedelta(minutes=random.randint(5, 15))77 78        timestamps = generate_timestamps(len(events), start_time)79 80        # Create process log entries81        for i, event in enumerate(events):82            data.append({83                "case_id": f"Patient_{patient_id}",84                "activity": event,85                "timestamp": timestamps[i]86            })87 88    # Convert the data to a pandas DataFrame89    df = pd.DataFrame(data)90    return df91 92#---------------------93 94# Function to generate incrementally increasing timestamps95def generate_timestamps_days(num_events, start_time):96    timestamps = [start_time]97    for _ in range(1, num_events):98        # Add a random time increment between 10 and 45 minutes99        increment = timedelta(minutes=random.randint(5, 15))100        next_timestamp = timestamps[-1] + (increment*(24*60))101        timestamps.append(next_timestamp)102    return timestamps103 104#---------------------105 106# Function to generate the process log107def generate_patient_process_with_follow_ups_log(num_patients=100, prop_direct_to_doctor=0.1, prop_from_triage_to_doctor=0.2):108 109    data = []110    events_standard_pathway = ["Appointment 1", "Follow up 1", "Follow up 2", "End"]111    events_direct_to_doctor = ["Appointment 1", "Urgent appointment", "End"]112    events_triage_to_doctor = ["Appointment 1", "Follow up 1", "Urgent appointment", "End"]113 114    start_time = datetime.now()115 116    for patient_id in range(1, num_patients+1):117        # Randomly determine the patient's pathway118        rand_value = random.random()119        if rand_value < prop_direct_to_doctor:120            # Pathway: Arrival -> Doctor Consultation -> End121            events = events_direct_to_doctor122        elif rand_value < (prop_direct_to_doctor + prop_from_triage_to_doctor):123            # Pathway: Arrival -> Nurse Triage -> Doctor Consultation -> End124            events = events_triage_to_doctor125        else:126            # Pathway: Arrival -> Nurse Triage -> Nurse Consultation -> End127            events = events_standard_pathway128 129        # Generate timestamps for the patient's pathway130        timestamps = generate_timestamps_days(len(events), start_time)131 132        # Create process log entries133        for i, event in enumerate(events):134            data.append({135                "case_id": f"Patient_{patient_id}",136                "activity": event,137                "timestamp": timestamps[i]138            })139 140#---------------------141 142def change_datetime_format(df, datetime_column):143    # Ensure the column is of datetime type144    df[datetime_column] = pd.to_datetime(df[datetime_column])145 146    # Format the datetime column to MM/DD/YYYY HH:MM:SS147    df[datetime_column] = df[datetime_column].dt.strftime('%m/%d/%Y %H:%M:%S')148 149    return df150 151#---------------------152 153 154 155#---------------------156 157 158 159#---------------------160 161 162 163#---------------------164 165 166 167#---------------------168 169'''