Skip to content

Commit

Permalink
feat(pm4py): merging hotfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fit-alessandro-berti committed Sep 26, 2023
2 parents 873fa16 + f024067 commit 1029e51
Show file tree
Hide file tree
Showing 13 changed files with 742 additions and 93 deletions.
5 changes: 5 additions & 0 deletions pm4py/objects/ocel/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
OCEL_OBJECTS_KEY = "ocel:objects"
OCEL_ID_KEY = "ocel:id"
OCEL_OMAP_KEY = "ocel:omap"
OCEL_TYPED_OMAP_KEY = "ocel:typedOmap"
OCEL_VMAP_KEY = "ocel:vmap"
OCEL_OVMAP_KEY = "ocel:ovmap"
OCEL_O2O_KEY = "ocel:o2o"
OCEL_OBJCHANGES_KEY = "ocel:objectChanges"
OCEL_EVTYPES_KEY = "ocel:eventTypes"
OCEL_OBJTYPES_KEY = "ocel:objectTypes"
OCEL_GLOBAL_LOG = "ocel:global-log"
OCEL_GLOBAL_LOG_ATTRIBUTE_NAMES = "ocel:attribute-names"
OCEL_GLOBAL_LOG_OBJECT_TYPES = "ocel:object-types"
Expand Down
3 changes: 2 additions & 1 deletion pm4py/objects/ocel/exporter/jsonocel/exporter.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from enum import Enum
from typing import Optional, Dict, Any

from pm4py.objects.ocel.exporter.jsonocel.variants import classic
from pm4py.objects.ocel.exporter.jsonocel.variants import classic, ocel20
from pm4py.objects.ocel.obj import OCEL
from pm4py.util import exec_utils


class Variants(Enum):
CLASSIC = classic
OCEL20 = ocel20


def apply(ocel: OCEL, target_path: str, variant=Variants.CLASSIC, parameters: Optional[Dict[Any, Any]] = None):
Expand Down
2 changes: 1 addition & 1 deletion pm4py/objects/ocel/exporter/jsonocel/variants/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from pm4py.objects.ocel.exporter.jsonocel.variants import classic
from pm4py.objects.ocel.exporter.jsonocel.variants import classic, ocel20
74 changes: 42 additions & 32 deletions pm4py/objects/ocel/exporter/jsonocel/variants/classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,13 @@ class Parameters(Enum):
ENCODING = "encoding"


def apply(ocel: OCEL, target_path: str, parameters: Optional[Dict[Any, Any]] = None):
"""
Exports an object-centric event log in a JSONOCEL file, using the classic JSON dump
Parameters
------------------
ocel
Object-centric event log
target_path
Destination path
parameters
Parameters of the algorithm, including:
- Parameters.EVENT_ID => the event ID column
- Parameters.OBJECT_ID => the object ID column
- Parameters.OBJECT_TYPE => the object type column
"""
def get_base_json_object(ocel: OCEL, parameters: Optional[Dict[Any, Any]] = None):
if parameters is None:
parameters = {}

event_id = exec_utils.get_param_value(Parameters.EVENT_ID, parameters, ocel.event_id_column)
object_id = exec_utils.get_param_value(Parameters.OBJECT_ID, parameters, ocel.object_id_column)
object_type = exec_utils.get_param_value(Parameters.OBJECT_TYPE, parameters, ocel.object_type_column)
encoding = exec_utils.get_param_value(Parameters.ENCODING, parameters, pm4_constants.DEFAULT_ENCODING)

ocel = ocel_consistency.apply(ocel, parameters=parameters)

all_object_types = list(ocel.objects[object_type].unique())
all_attribute_names = attributes_names.get_attribute_names(ocel, parameters=parameters)
Expand All @@ -56,16 +38,16 @@ def apply(ocel: OCEL, target_path: str, parameters: Optional[Dict[Any, Any]] = N

events_items, objects_items = clean_dataframes.get_dataframes_from_ocel(ocel, parameters=parameters)

result = {}
result[constants.OCEL_GLOBAL_EVENT] = global_event_items
result[constants.OCEL_GLOBAL_OBJECT] = global_object_items
result[constants.OCEL_GLOBAL_LOG] = {}
result[constants.OCEL_GLOBAL_LOG][constants.OCEL_GLOBAL_LOG_OBJECT_TYPES] = all_object_types
result[constants.OCEL_GLOBAL_LOG][constants.OCEL_GLOBAL_LOG_ATTRIBUTE_NAMES] = all_attribute_names
result[constants.OCEL_GLOBAL_LOG][constants.OCEL_GLOBAL_LOG_VERSION] = constants.CURRENT_VERSION
result[constants.OCEL_GLOBAL_LOG][constants.OCEL_GLOBAL_LOG_ORDERING] = constants.DEFAULT_ORDERING
result[constants.OCEL_EVENTS_KEY] = {}
result[constants.OCEL_OBJECTS_KEY] = {}
base_object = {}
base_object[constants.OCEL_GLOBAL_EVENT] = global_event_items
base_object[constants.OCEL_GLOBAL_OBJECT] = global_object_items
base_object[constants.OCEL_GLOBAL_LOG] = {}
base_object[constants.OCEL_GLOBAL_LOG][constants.OCEL_GLOBAL_LOG_OBJECT_TYPES] = all_object_types
base_object[constants.OCEL_GLOBAL_LOG][constants.OCEL_GLOBAL_LOG_ATTRIBUTE_NAMES] = all_attribute_names
base_object[constants.OCEL_GLOBAL_LOG][constants.OCEL_GLOBAL_LOG_VERSION] = constants.CURRENT_VERSION
base_object[constants.OCEL_GLOBAL_LOG][constants.OCEL_GLOBAL_LOG_ORDERING] = constants.DEFAULT_ORDERING
base_object[constants.OCEL_EVENTS_KEY] = {}
base_object[constants.OCEL_OBJECTS_KEY] = {}

events_items = events_items.to_dict("records")
i = 0
Expand All @@ -78,7 +60,7 @@ def apply(ocel: OCEL, target_path: str, parameters: Optional[Dict[Any, Any]] = N
event = {k: v for k, v in event.items() if k.startswith(constants.OCEL_PREFIX)}
event[constants.OCEL_VMAP_KEY] = vmap
event[constants.OCEL_OMAP_KEY] = rel_objs[eid]
result[constants.OCEL_EVENTS_KEY][eid] = event
base_object[constants.OCEL_EVENTS_KEY][eid] = event
i = i + 1
del events_items

Expand All @@ -90,8 +72,36 @@ def apply(ocel: OCEL, target_path: str, parameters: Optional[Dict[Any, Any]] = N
del object[object_id]
ovmap = {k: v for k, v in object.items() if pd.notnull(v) and not k.startswith(constants.OCEL_PREFIX)}
object = {object_type: object[object_type], constants.OCEL_OVMAP_KEY: ovmap}
result[constants.OCEL_OBJECTS_KEY][oid] = object
base_object[constants.OCEL_OBJECTS_KEY][oid] = object
i = i + 1
del objects_items

json.dump(result, open(target_path, "w", encoding=encoding), indent=2)
return base_object


def apply(ocel: OCEL, target_path: str, parameters: Optional[Dict[Any, Any]] = None):
"""
Exports an object-centric event log in a JSONOCEL file, using the classic JSON dump
Parameters
------------------
ocel
Object-centric event log
target_path
Destination path
parameters
Parameters of the algorithm, including:
- Parameters.EVENT_ID => the event ID column
- Parameters.OBJECT_ID => the object ID column
- Parameters.OBJECT_TYPE => the object type column
"""
if parameters is None:
parameters = {}

encoding = exec_utils.get_param_value(Parameters.ENCODING, parameters, pm4_constants.DEFAULT_ENCODING)

ocel = ocel_consistency.apply(ocel, parameters=parameters)

base_object = get_base_json_object(ocel, parameters=parameters)

json.dump(base_object, open(target_path, "w", encoding=encoding), indent=2)
117 changes: 117 additions & 0 deletions pm4py/objects/ocel/exporter/jsonocel/variants/ocel20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import json
from enum import Enum
from typing import Optional, Dict, Any

import pandas as pd

from pm4py.objects.ocel import constants
from pm4py.objects.ocel.obj import OCEL
from pm4py.util import exec_utils, constants as pm4_constants
from pm4py.objects.ocel.util import ocel_consistency
from pm4py.objects.ocel.exporter.jsonocel.variants import classic
from pm4py.objects.ocel.util import attributes_per_type


class Parameters(Enum):
EVENT_ID = constants.PARAM_EVENT_ID
OBJECT_ID = constants.PARAM_OBJECT_ID
OBJECT_TYPE = constants.PARAM_OBJECT_TYPE
EVENT_ACTIVITY = constants.PARAM_EVENT_ACTIVITY
EVENT_TIMESTAMP = constants.PARAM_EVENT_TIMESTAMP
ENCODING = "encoding"


def apply(ocel: OCEL, target_path: str, parameters: Optional[Dict[Any, Any]] = None):
"""
Exports an object-centric event log (OCEL 2.0) in a JSONOCEL 2.0 file, using the classic JSON dump
Parameters
------------------
ocel
Object-centric event log
target_path
Destination path
parameters
Parameters of the algorithm, including:
- Parameters.EVENT_ID => the event ID column
- Parameters.OBJECT_ID => the object ID column
- Parameters.OBJECT_TYPE => the object type column
"""
if parameters is None:
parameters = {}

event_id = exec_utils.get_param_value(Parameters.EVENT_ID, parameters, ocel.event_id_column)
object_id = exec_utils.get_param_value(Parameters.OBJECT_ID, parameters, ocel.object_id_column)
object_type = exec_utils.get_param_value(Parameters.OBJECT_TYPE, parameters, ocel.object_type_column)
event_activity = exec_utils.get_param_value(Parameters.EVENT_ACTIVITY, parameters, ocel.event_activity)
event_timestamp = exec_utils.get_param_value(Parameters.EVENT_TIMESTAMP, parameters, ocel.event_timestamp)

encoding = exec_utils.get_param_value(Parameters.ENCODING, parameters, pm4_constants.DEFAULT_ENCODING)

ocel = ocel_consistency.apply(ocel, parameters=parameters)

base_object = classic.get_base_json_object(ocel, parameters=parameters)

ets, ots = attributes_per_type.get(ocel, parameters=parameters)

base_object[constants.OCEL_EVTYPES_KEY] = {}
for et in ets:
base_object[constants.OCEL_EVTYPES_KEY][et] = {}
et_atts = ets[et]
for k, v in et_atts.items():
this_type = "string"
if "date" in v or "time" in v:
this_type = "date"
elif "float" in v or "double" in v:
this_type = "float"
base_object[constants.OCEL_EVTYPES_KEY][et][k] = this_type

base_object[constants.OCEL_OBJTYPES_KEY] = {}
for ot in ots:
base_object[constants.OCEL_OBJTYPES_KEY][ot] = {}
ot_atts = ots[ot]
for k, v in ot_atts.items():
this_type = "string"
if "date" in v or "time" in v:
this_type = "date"
elif "float" in v or "double" in v:
this_type = "float"
base_object[constants.OCEL_OBJTYPES_KEY][ot][k] = this_type

base_object[constants.OCEL_OBJCHANGES_KEY] = []
if len(ocel.object_changes) > 0:
object_changes = ocel.object_changes.to_dict("records")
for i in range(len(object_changes)):
object_changes[i][event_timestamp] = object_changes[i][event_timestamp].isoformat()

base_object[constants.OCEL_OBJCHANGES_KEY] = object_changes

e2o_list = ocel.relations[[event_id, object_id, constants.DEFAULT_QUALIFIER]].to_dict("records")
eids = set()

for elem in e2o_list:
eid = elem[event_id]
oid = elem[object_id]
qualifier = elem[constants.DEFAULT_QUALIFIER]

if eid not in eids:
base_object[constants.OCEL_EVENTS_KEY][eid][constants.OCEL_TYPED_OMAP_KEY] = []
eids.add(eid)

base_object[constants.OCEL_EVENTS_KEY][eid][constants.OCEL_TYPED_OMAP_KEY].append({object_id: oid, constants.DEFAULT_QUALIFIER: qualifier})

o2o_list = ocel.o2o.to_dict("records")
oids = set()

for elem in o2o_list:
oid = elem[object_id]
oid2 = elem[object_id+"_2"]
qualifier = elem[constants.DEFAULT_QUALIFIER]

if oid not in oids:
base_object[constants.OCEL_OBJECTS_KEY][oid][constants.OCEL_O2O_KEY] = []
oids.add(oid)

base_object[constants.OCEL_OBJECTS_KEY][oid][constants.OCEL_O2O_KEY].append({object_id: oid2, constants.DEFAULT_QUALIFIER: qualifier})

json.dump(base_object, open(target_path, "w", encoding=encoding), indent=2)
19 changes: 3 additions & 16 deletions pm4py/objects/ocel/exporter/xmlocel/variants/ocel20.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
from lxml import etree

from pm4py.objects.ocel import constants
from pm4py.objects.ocel.exporter.util import clean_dataframes
from pm4py.objects.ocel.obj import OCEL
from pm4py.objects.ocel.util import attributes_names
from pm4py.objects.ocel.util import related_objects
from pm4py.util import exec_utils, constants as pm4_constants
from pm4py.objects.ocel.util import ocel_consistency
from pm4py.objects.ocel.util import attributes_per_type


class Parameters(Enum):
Expand Down Expand Up @@ -42,22 +40,9 @@ def apply(ocel: OCEL, target_path: str, parameters: Optional[Dict[Any, Any]] = N

ocel = ocel_consistency.apply(ocel, parameters=parameters)

ets = {k: {x: str(v[x].dtype) for x in v.dropna(axis="columns", how="all").columns if not x.startswith("ocel:")} for k, v in ocel.events.groupby(event_activity_column)}
ots = {k: {x: str(v[x].dtype) for x in v.dropna(axis="columns", how="all").columns if not x.startswith("ocel:")} for k, v in ocel.objects.groupby(object_type_column)}
ots2 = {k: {x: str(v[x].dtype) for x in v.dropna(axis="columns", how="all").columns if not x.startswith("ocel:")} for k, v in ocel.object_changes.groupby(object_type_column)}

for k in ots2:
if k not in ots:
ots[k] = ots2[k]
else:
for x in ots2[k]:
if x not in ots[k]:
ots[k][x] = ots2[k][x]

objects0 = ocel.objects.to_dict("records")
events0 = ocel.events.to_dict("records")
object_changes0 = ocel.object_changes.to_dict("records")
o2o_list = ocel.o2o.to_dict("records")
o2o_dict = ocel.o2o.groupby(object_id_column).agg(list).to_dict("tight")
o2o_dict = {o2o_dict["index"][i]: o2o_dict["data"][i] for i in range(len(o2o_dict["index"]))}

Expand Down Expand Up @@ -100,6 +85,8 @@ def apply(ocel: OCEL, target_path: str, parameters: Optional[Dict[Any, Any]] = N
object_changes2[oid][chng_field].append((chng_value, chng_time.isoformat()))
object_changes3[oid].append((chng_field, chng_value, chng_time.isoformat()))

ets, ots = attributes_per_type.get(ocel, parameters=parameters)

root = etree.Element("log")
object_types = etree.SubElement(root, "object-types")
for ot in ots:
Expand Down
Loading

0 comments on commit 1029e51

Please sign in to comment.