spmviz.data.SequenceDatabase#

class spmviz.data.SequenceDatabase(sequences, times=None, ids=None, attributes=None, labels=None, name=None)[source]#

Bases: object

A database of sequences of itemsets with optional timestamps and attributes.

Parameters:
sequencesiterable

Each sequence is an iterable of elements; an element is an item or an iterable of items (an itemset). A string is split into its characters.

timesiterable of array_like, optional

One timestamp per element (numbers or datetimes; datetimes are stored as seconds since the Unix epoch).

idssequence, optional

Identifiers of the sequences (case ids); default 0..n-1.

attributespandas.DataFrame, optional

One row per sequence (group, outcome, …).

labelsmapping, optional

Display names of the items, used by every table and figure.

namestr, optional

Examples

>>> db = SequenceDatabase(["abc", "acd", "bcd"])
>>> db.n_sequences, db.alphabet
(3, ['a', 'b', 'c', 'd'])
>>> db.support("a c")
2
property alphabet#

Sorted list of the distinct items.

contains(pattern, constraints=None, **kw)[source]#

Boolean array: which sequences contain the pattern.

describe()[source]#

stats() as a one-column table (handy in notebooks).

filter(min_length=None, max_length=None, contains=None, **constraints)[source]#

Sequences with length in a range and/or containing a pattern.

classmethod from_event_log(df, case='case', activity='activity', timestamp=None, group_simultaneous=False, attributes=None, labels=None, name=None, sort=True)[source]#

Build sequences from a long event table (one row per event).

Parameters:
dfpandas.DataFrame
case, activitystr

Columns of the case (sequence) identifier and of the item.

timestampstr, optional

Column used to order the events of a case (stable sort) and stored as the element timestamps. Without it the row order is kept.

group_simultaneousbool

Events of a case with equal timestamps form one itemset.

attributeslist of str, optional

Case-level columns copied (first value per case) into attributes.

sortbool

Order the events of a case by timestamp (stable). With False the row order is kept and the timestamps are only attached.

Examples

>>> log = pd.DataFrame({"case": [1, 1, 2], "activity": ["a", "b", "a"]})
>>> SequenceDatabase.from_event_log(log).n_sequences
2
classmethod from_pm4py(log, case='case:concept:name', activity='concept:name', timestamp='time:timestamp', **kw)[source]#

Convert a pm4py event log (EventLog or the pm4py pandas format).

classmethod from_spmf(path, **kw)[source]#

Read a sequence database in SPMF format (see spmviz.io.read_spmf()).

classmethod from_strings(strings, sep=None, **kw)[source]#

Each string is a sequence; sep=None splits into characters, otherwise on sep.

Examples

>>> SequenceDatabase.from_strings(["wake shower coffee"], sep=" ")[0]
(('wake',), ('shower',), ('coffee',))
classmethod from_wide(df, columns=None, collapse=False, **kw)[source]#

State sequences stored in columns (one row per sequence, as in TraMineR).

Missing values end a sequence. With collapse=True runs of the same state become one element (the distinct successive states view).

property is_simple#

True when every element holds a single item.

item_frequency()[source]#

Number of occurrences of each item.

item_support()[source]#

Number of sequences containing each item, sorted decreasingly.

label(item)[source]#

Display name of an item.

property lengths#

Number of elements of every sequence.

map_items(mapping)[source]#

Rename items with a dict or a function (e.g. to merge similar events).

pattern(p)[source]#

Resolve a pattern written with the items, their string form or their labels.

Examples

>>> db = SequenceDatabase([[1, 2], [2, 3]], labels={1: "login", 2: "search", 3: "buy"})
>>> db.pattern("login -> search"), db.pattern("2 3")
(((1,), (2,)), ((2,), (3,)))
sample(n, random_state=None)[source]#

Random sample of n sequences without replacement.

property sequences#

The sequences as tuples of itemsets.

split(by)[source]#

Split by an attribute column (or an array of group labels) into a dict of databases.

stats()[source]#

Summary statistics (the columns of the benchmark tables in the literature).

subset(idx)[source]#

A new database with the selected sequences (ids, times and attributes follow).

support(pattern, constraints=None, **kw)[source]#

Number of sequences that contain the pattern.

to_event_log(case='case', activity='activity', timestamp='timestamp')[source]#

Long event table: one row per item (elements keep a shared position/timestamp).

to_lists()[source]#

Plain Python lists: items for simple databases, lists of itemsets otherwise.