{ "cells": [ { "cell_type": "markdown", "id": "0009d44f-be72-4bb2-9ca6-487a2be24ce1", "metadata": { "tags": [] }, "source": [ "# Generate pm4py log using ASP Generator\n", "\n", "ASP log generator uses the decl model which converts the model into abudction logic programming and pass to the clingo.\n", "Clingo generates the output which is turned into the pm4py log or can be created a .xes file.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "1eb21bf2-bf41-46b2-96e3-806047cc8cb1", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python 3.11.4\r\n" ] } ], "source": [ "!python3 --version" ] }, { "cell_type": "code", "execution_count": 2, "id": "2938753f-7b7c-4095-b918-1a667621e50d", "metadata": { "tags": [] }, "outputs": [], "source": [ "import os\n", "\n", "from Declare4Py.ProcessMiningTasks.ASPLogGeneration.asp_generator import AspGenerator\n", "from Declare4Py.ProcessModels.DeclareModel import DeclareModel" ] }, { "cell_type": "code", "execution_count": 3, "id": "0b9d5123-dc4f-4a75-bdd1-d4235d636772", "metadata": { "tags": [] }, "outputs": [], "source": [ "decl_model_1 = \"decl_files/Response.decl\"\n", "decl_model_2 = \"decl_files/MikeModel.decl\"\n", "decl_model_3 = \"decl_files/reference10.decl\"\n", "decl_model_4 = \"diagonisis_simple.decl\"\n", "decl_folder = \"decl_files\"\n", "\n", "decl_filename = decl_model_4.split(\".\")[0]\n", "output_file = pathlib.Path(\".\", f\"{decl_filename}.xes\")\n", "decl_file = pathlib.Path(\".\", decl_folder, decl_model_4)" ] }, { "cell_type": "code", "execution_count": 4, "id": "4b2d84b6-6a5a-4b3c-a0bb-335981eb6374", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'activities': ['ER Triage',\n", " 'ER Registration',\n", " 'ER Sepsis Triage',\n", " 'Leucocytes',\n", " 'CRP',\n", " 'LacticAcid',\n", " 'IV Antibiotics',\n", " 'Admission NC',\n", " 'IV Liquid',\n", " 'Release A',\n", " 'Release B',\n", " 'Return ER',\n", " 'Admission IC'],\n", " 'formula': '',\n", " 'parsed_formula': None,\n", " 'parameters': [],\n", " 'backend': 'lydia',\n", " 'attribute_type': [],\n", " 'payload': [],\n", " 'serialized_constraints': ['Chain Response[Admission NC, Release B] | | |',\n", " 'Chain Response[Admission NC, Release A] | | |',\n", " 'Chain Precedence[IV Liquid, Admission NC] | | |',\n", " 'Chain Response[ER Registration, ER Triage] | | |'],\n", " 'constraints': [{'template': \"\",\n", " 'activities': ['Admission NC', 'Release B'],\n", " 'condition': ['', '', '']},\n", " {'template': \"\",\n", " 'activities': ['Admission NC', 'Release A'],\n", " 'condition': ['', '', '']},\n", " {'template': \"\",\n", " 'activities': ['IV Liquid', 'Admission NC'],\n", " 'condition': ['', '', '']},\n", " {'template': \"\",\n", " 'activities': ['ER Registration', 'ER Triage'],\n", " 'condition': ['', '', '']}],\n", " 'parsed_model': ,\n", " 'declare_model_lines': ['activity ER Triage\\n',\n", " 'bind ER Triage: org:group, Diagnose, Age\\n',\n", " 'activity ER Registration\\n',\n", " 'bind ER Registration: InfectionSuspected, org:group, DiagnosticBlood, DisfuncOrg, SIRSCritTachypnea, Hypotensie, SIRSCritHeartRate, Infusion, DiagnosticArtAstrup, Age, DiagnosticIC, DiagnosticSputum, DiagnosticLiquor, DiagnosticOther, SIRSCriteria2OrMore, DiagnosticXthorax, SIRSCritTemperature, DiagnosticUrinaryCulture, SIRSCritLeucos, Oligurie, DiagnosticLacticAcid, Diagnose, Hypoxie, DiagnosticUrinarySediment, DiagnosticECG\\n',\n", " 'activity ER Sepsis Triage\\n',\n", " 'bind ER Sepsis Triage: org:group, Diagnose, Age\\n',\n", " 'activity Leucocytes\\n',\n", " 'bind Leucocytes: Leucocytes, org:group, Diagnose, Age\\n',\n", " 'activity CRP\\n',\n", " 'bind CRP: CRP, org:group, Diagnose, Age\\n',\n", " 'activity LacticAcid\\n',\n", " 'bind LacticAcid: org:group, LacticAcid\\n',\n", " 'activity IV Antibiotics\\n',\n", " 'bind IV Antibiotics: org:group\\n',\n", " 'activity Admission NC\\n',\n", " 'bind Admission NC: org:group\\n',\n", " 'activity IV Liquid\\n',\n", " 'bind IV Liquid: org:group, Diagnose, Age\\n',\n", " 'activity Release A\\n',\n", " 'bind Release A: org:group\\n',\n", " 'activity Release B\\n',\n", " 'bind Release B: org:group\\n',\n", " 'activity Return ER\\n',\n", " 'bind Return ER: org:group\\n',\n", " 'activity Admission IC\\n',\n", " 'bind Admission IC: org:group\\n',\n", " 'CRP: float between 5.0 and 573.0\\n',\n", " 'InfectionSuspected: true, false\\n',\n", " 'org:group: A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, ?\\n',\n", " 'DiagnosticBlood: true, false\\n',\n", " 'DisfuncOrg: true, false\\n',\n", " 'SIRSCritTachypnea: true, false\\n',\n", " 'Hypotensie: true, false\\n',\n", " 'SIRSCritHeartRate: true, false\\n',\n", " 'Infusion: true, false\\n',\n", " 'Leucocytes: float between 0.2 and 381.3\\n',\n", " 'DiagnosticArtAstrup: true, false\\n',\n", " 'LacticAcid: float between 0.2 and 14.9\\n',\n", " 'Age: integer between 20 and 90\\n',\n", " 'DiagnosticIC: true, false\\n',\n", " 'DiagnosticSputum: false, true\\n',\n", " 'DiagnosticLiquor: false, true\\n',\n", " 'DiagnosticOther: false, true\\n',\n", " 'SIRSCriteria2OrMore: true, false\\n',\n", " 'DiagnosticXthorax: true, false\\n',\n", " 'SIRSCritTemperature: true, false\\n',\n", " 'DiagnosticUrinaryCulture: true, false\\n',\n", " 'SIRSCritLeucos: false, true\\n',\n", " 'Oligurie: false, true\\n',\n", " 'DiagnosticLacticAcid: true, false\\n',\n", " 'Diagnose: YA, YB, YC, YD, QA, QB, QC, QD, QE, IA, IB, IC, ID, IE, AA, AB, AC, AD, AE, ZA, ZB, ZC, ZD, RA, RB, RC, RD, JA, JB, JC, JD, JE, BA, BB, A, BC, B, BD, C, BE, D, E, F, G, H, I, J, K, L, M, N, SA, O, SB, P, SC, Q, SD, R, S, T, U, V, KA, W, KB, X, KC, Y, KD, Z, KE, CA, CB, CC, CD, CE, TA, TB, TC, TD, LA, LB, LC, LD, LE, DA, DB, DC, DD, DE, UA, UB, UC, UD, MA, MB, MC, MD, ME, EA, EB, EC, ED, EE, VA, VB, VC, VD, NA, NB, NC, ND, FA, FB, FC, FD, FE, WA, WB, WC, WD, OA, OB, OC, OD, OE, GA, GB, GC, GD, GE, XA, XB, XC, XD, PA, PB, PC, PD, PE, HA, HB, HC, HD, HE\\n',\n", " 'Hypoxie: false, true\\n',\n", " 'DiagnosticUrinarySediment: true, false\\n',\n", " 'DiagnosticECG: true, false\\n',\n", " 'Chain Response[Admission NC, Release B] | | |\\n',\n", " 'Chain Response[Admission NC, Release A] | | |\\n',\n", " 'Chain Precedence[IV Liquid, Admission NC] | | |\\n',\n", " 'Chain Response[ER Registration, ER Triage] | | |']}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create Declare model by reading declare model from a file.\n", "\n", "model: DeclareModel = DeclareModel().parse_from_file(decl_file)\n", "model.__dict__" ] }, { "cell_type": "code", "execution_count": 5, "id": "f4b87ba4-2740-4006-b40f-b0ae95bab202", "metadata": {}, "outputs": [], "source": [ "#general Setting\n", "\n", "# Number of traces that should be generated\n", "num_of_traces = 20\n", "\n", "# Minimum and maximum number of events a trace can contain\n", "(num_min_events, num_max_events) = (2, 5)\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "b9dc4474-319b-40e0-95a7-c8ac36a18f2b", "metadata": {}, "outputs": [], "source": [ "# Initializing ASP generator with default distributor which is uniform.\n", "\n", "asp = AspGenerator(\n", " model,\n", " num_of_traces,\n", " num_min_events,\n", " num_max_events\n", ")" ] }, { "cell_type": "code", "execution_count": 7, "id": "ca9ef1f5", "metadata": {}, "outputs": [], "source": [ "asp.set_distribution(\n", " distributor_type=\"uniform\", # \"uniform\", \"gaussian\", \"custom\"\n", ")\n", "\n", "# or\n", "\n", "# asp.set_distribution(\n", "# distributor_type=\"gaussian\", # \"uniform\", \"gaussian\", \"custom\"\n", "# loc=2,\n", "# scale= 4\n", "# )\n", "\n", "# asp.set_distribution(\n", "# distributor_type=\"custom\", # \"uniform\", \"gaussian\", \"custom\"\n", "# custom_probabilities=[0.3, 0.5, 0.1, 0.4]\n", "# )\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "974a92ad-6497-433f-99e4-2f0d79a6e150", "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Generate the traces and parse the result produced by clingo\n", "asp.run() # Run accets 1 optional value whether to create file for the ASP generated from given declare model\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "a03b7b40-e718-4783-a8e4-36528bd4d01d", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "bca3e735f5434ebfab6ea1fe6eacd282", "version_major": 2, "version_minor": 0 }, "text/plain": [ "exporting log, completed traces :: 0%| | 0/20 [00:00