spmviz.data.SequenceDatabase#
- class spmviz.data.SequenceDatabase(sequences, times=None, ids=None, attributes=None, labels=None, name=None)[source]#
Bases:
objectA 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
ofarray_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.- attributes
pandas.DataFrame,optional One row per sequence (group, outcome, …).
- labels
mapping,optional Display names of the items, used by every table and figure.
- name
str,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.
- 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:
- df
pandas.DataFrame - case, activity
str Columns of the case (sequence) identifier and of the item.
- timestamp
str,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.
- attributes
listofstr,optional Case-level columns copied (first value per case) into
attributes.- sortbool
Order the events of a case by
timestamp(stable). WithFalsethe row order is kept and the timestamps are only attached.
- df
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 (
EventLogor 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=Nonesplits into characters, otherwise onsep.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=Trueruns of the same state become one element (the distinct successive states view).
- property is_simple#
True when every element holds a single item.
- property lengths#
Number of elements of every sequence.
- 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,)))
- 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.