Exemplo n.º 1
0
def map_(func, log):
    '''
        Maps the log according to a given lambda function.
        domain and target of the function need to be of the same type (either trace or event) otherwise, the map behaves unexpected

        Parameters
        ----------
        func
        log

        Returns
        -------

        '''
    if isinstance(log, log_inst.EventLog):
        return log_inst.EventLog(list(map(func, log)),
                                 attributes=log.attributes,
                                 classifiers=log.classifiers,
                                 omni_present=log.omni_present,
                                 extensions=log.extensions,
                                 properties=log.properties)
    elif isinstance(log, log_inst.EventStream):
        return log_inst.EventStream(list(map(func, log)),
                                    attributes=log.attributes,
                                    classifiers=log.classifiers,
                                    omni_present=log.omni_present,
                                    extensions=log.extensions,
                                    properties=log.properties)
    else:
        warnings.warn(
            'input log object not of appropriate type, map() not applied')
        return log
Exemplo n.º 2
0
def filter_(func, log):
    '''
    Filters the log according to a given lambda function.

    Parameters
    ----------
    func
    log

    Returns
    -------

    '''
    if isinstance(log, log_inst.EventLog):
        return log_inst.EventLog(list(filter(func, log)),
                                 attributes=log.attributes,
                                 classifiers=log.classifiers,
                                 omni_present=log.omni_present,
                                 extensions=log.extensions,
                                 properties=log.properties)
    elif isinstance(log, log_inst.EventStream):
        return log_inst.EventStream(list(filter(func, log)),
                                    attributes=log.attributes,
                                    classifiers=log.classifiers,
                                    omni_present=log.omni_present,
                                    extensions=log.extensions,
                                    properties=log.properties)
    else:
        warnings.warn(
            'input log object not of appropriate type, filter() not applied')
        return log
Exemplo n.º 3
0
def apply(log, parameters=None):
    """
      Converts the event log to an event stream

      Parameters
      ----------
      log: :class:`pm4py.log.log.EventLog`
          An Event log
      include_case_attributes:
          Default is True
      case_attribute_prefix:
          Default is 'case:'
      enable_deepcopy
          Enables deepcopy (avoid references between input and output objects)

      Returns
          -------
      log : :class:`pm4py.log.log.EventLog`
          An Event stream
      """
    if parameters is None:
        parameters = {}

    stream_post_processing = exec_utils.get_param_value(
        Parameters.STREAM_POST_PROCESSING, parameters, False)
    case_pref = exec_utils.get_param_value(Parameters.CASE_ATTRIBUTE_PREFIX,
                                           parameters, 'case:')
    enable_deepcopy = exec_utils.get_param_value(Parameters.DEEP_COPY,
                                                 parameters, True)
    include_case_attributes = exec_utils.get_param_value(
        Parameters.INCLUDE_CASE_ATTRIBUTES, parameters, True)
    compress = exec_utils.get_param_value(Parameters.COMPRESS, parameters,
                                          True)

    if pkgutil.find_loader("pandas"):
        import pandas
        if isinstance(log, pandas.DataFrame):
            extensions = __detect_extensions(log)
            list_events = pandas_utils.to_dict_records(log)
            if stream_post_processing:
                list_events = __postprocess_stream(list_events)
            if compress:
                list_events = __compress(list_events)
            for i in range(len(list_events)):
                list_events[i] = Event(list_events[i])
            log = log_instance.EventStream(list_events,
                                           attributes={'origin': 'csv'})
            for ex in extensions:
                log.extensions[ex.name] = {
                    xes_constants.KEY_PREFIX: ex.prefix,
                    xes_constants.KEY_URI: ex.uri
                }
    if isinstance(log, EventLog):
        return __transform_event_log_to_event_stream(
            log,
            include_case_attributes=include_case_attributes,
            case_attribute_prefix=case_pref,
            enable_deepcopy=enable_deepcopy)
    return log
Exemplo n.º 4
0
def sort_log(
        log: log_inst.EventLog,
        key,
        reverse: bool = False
) -> Union[log_inst.EventLog, log_inst.EventStream]:
    """
    Sorts the event log according to a given key.

    Parameters
    ----------
    log
        event log object; either EventLog or EventStream
    key
        sorting key
    reverse
        indicates whether sorting should be reversed or not

    Returns
    -------
        sorted event log if object provided is correct; original log if not correct
    """
    if type(log) not in [pd.DataFrame, EventLog, EventStream]:
        raise Exception(
            "the method can be applied only to a traditional event log!")

    if isinstance(log, log_inst.EventLog):
        return log_inst.EventLog(sorted(log, key=key, reverse=reverse),
                                 attributes=log.attributes,
                                 classifiers=log.classifiers,
                                 omni_present=log.omni_present,
                                 extensions=log.extensions,
                                 properties=log.properties)
    elif isinstance(log, log_inst.EventStream):
        return log_inst.EventStream(sorted(log, key=key, reverse=reverse),
                                    attributes=log.attributes,
                                    classifiers=log.classifiers,
                                    omni_present=log.omni_present,
                                    extensions=log.extensions,
                                    properties=log.properties)
    else:
        warnings.warn(
            'input log object not of appropriate type, sorted() not applied')
        return log
Exemplo n.º 5
0
def sort_(func, log, reverse=False):
    if isinstance(log, log_inst.EventLog):
        return log_inst.EventLog(sorted(log, key=func, reverse=reverse),
                                 attributes=log.attributes,
                                 classifiers=log.classifiers,
                                 omni_present=log.omni_present,
                                 extensions=log.extensions,
                                 properties=log.properties)
    elif isinstance(log, log_inst.EventStream):
        return log_inst.EventStream(sorted(log, key=func, reverse=reverse),
                                    attributes=log.attributes,
                                    classifiers=log.classifiers,
                                    omni_present=log.omni_present,
                                    extensions=log.extensions,
                                    properties=log.properties)
    else:
        warnings.warn(
            'input log object not of appropriate type, map() not applied')
        return log
Exemplo n.º 6
0
def __transform_event_log_to_event_stream(
        log,
        include_case_attributes=True,
        case_attribute_prefix=pmutil.CASE_ATTRIBUTE_PREFIX,
        enable_deepcopy=False):
    """
      Converts the event log to an event stream

      Parameters
      ----------
      log: :class:`pm4py.log.log.EventLog`
          An Event log
      include_case_attributes:
          Default is True
      case_attribute_prefix:
          Default is 'case:'
      enable_deepcopy
          Enables deepcopy (avoid references between input and output objects)

      Returns
          -------
      log : :class:`pm4py.log.log.EventLog`
          An Event stream
      """
    events = []
    for trace in log:
        for event in trace:
            new_event = deepcopy(event) if enable_deepcopy else event
            if include_case_attributes:
                for key, value in trace.attributes.items():
                    new_event[case_attribute_prefix + key] = value
            # fix 14/02/2019: since the XES standard does not force to specify a case ID, when event log->event stream
            # conversion is done, the possibility to get back the original event log is lost
            if pmutil.CASE_ATTRIBUTE_GLUE not in new_event:
                new_event[pmutil.CASE_ATTRIBUTE_GLUE] = str(hash(trace))
            events.append(new_event)
    return log_instance.EventStream(events,
                                    attributes=log.attributes,
                                    classifiers=log.classifiers,
                                    omni_present=log.omni_present,
                                    extensions=log.extensions,
                                    properties=log.properties)
Exemplo n.º 7
0
def filter_log(
        f: Callable[[Any], bool], log: log_inst.EventLog
) -> Union[log_inst.EventLog, log_inst.EventStream]:
    """
    Filters the log according to a given (lambda) function.

    Parameters
    ----------
    f
        function that specifies the filter criterion, may be a lambda
    log
        event log; either EventLog or EventStream Object

    Returns
    -------
    log
        filtered event log if object provided is correct; original log if not correct

    """
    if type(log) not in [pd.DataFrame, EventLog, EventStream]:
        raise Exception(
            "the method can be applied only to a traditional event log!")

    if isinstance(log, log_inst.EventLog):
        return log_inst.EventLog(list(filter(f, log)),
                                 attributes=log.attributes,
                                 classifiers=log.classifiers,
                                 omni_present=log.omni_present,
                                 extensions=log.extensions,
                                 properties=log.properties)
    elif isinstance(log, log_inst.EventStream):
        return log_inst.EventStream(list(filter(f, log)),
                                    attributes=log.attributes,
                                    classifiers=log.classifiers,
                                    omni_present=log.omni_present,
                                    extensions=log.extensions,
                                    properties=log.properties)
    else:
        warnings.warn(
            'input log object not of appropriate type, filter() not applied')
        return log