title: README author: Thomas Guyet
PyChronicles package
A chronicle is a specification of complex temporal behaviors as a graph of temporal constraints. More specifically, a chronicle is a multiset of events and a set of temporal constraints specifying that the occurrences of pairs of events must occur within given temporal intervals. It can be used to recognize complex behaviors in sequences of temporal events. PyChronicles is now compatible with Pandas and makes chronicle features available on dataframes representing timed sequences.
This library provides Python classes to define timed sequences and chronicles, and to read/save chronicles in the standard CRS format. The main functionality for chronicles is their efficient matching in timed sequences, or in pandas dataframes (through an accessor).
The Abstracter class makes it possible to create a chronicle from a collection of sequences.
For more details about the usage of this package, have a look at the documentation. For more (formal) information about the temporal model of chronicles, see our book:
@book{chroniclesbook,
title = {Chronicles: Formalization of a Temporal Model},
author = {Besnard, Philippe and Guyet, Thomas},
publisher={Springer Nature},
pages = {121},
year = {2023}
}
Package installation
The PyChronicles package is available in the Python Package Index and can be installed using any standard Python package manager.
pip install pychronicles
It can also be installed from source. For example, to install it with pip, either locally or in a virtual environment, run the following commands:
git clone --recursive https://gitlab.inria.fr/tguyet/pychronicles
cd pychronicles
pip install -r requirements.txt
pip install --upgrade .
Alternatively, you can install the repository from sources with pip only:
git clone --recursive https://gitlab.inria.fr/tguyet/pychronicles
cd pychronicles
pip install .
In case you want to visualize chronicles, you need to install networkx and matplotlib manually. By default, they are not installed and the plotting function is disabled.
pip install networkx matplotlib
Basic Usage Example
import numpy as np
from pychronicles import TimedSequence, Chronicle
# create a timed sequence
seq = [('a',1),('c',2),('b',3),('a',8)]
ts = TimedSequence(np.array([e[1] for e in seq], dtype='float'), np.array([e[0] for e in seq]))
# create a chronicle
c=Chronicle()
c.add_event(0,'a')
c.add_event(1,'b')
c.add_constraint(0,1, (0.4,2.3))
reco=c.match(ts)
print(f"Does the chronicle match the sequence? [{reco}]")
reco=c.recognize(ts)
print(f"What are the occurrences of the chronicle in the sequence? [{reco}]")
Efficient chronicle recognition
The chronicle recognition package is a pure Python package implemented using numpy features.
Timed Sequences
A timed sequence is created from a pair of arrays: a numpy array containing the dates, and an array containing the events (with the same length).
The dates can be represented with dates (only with the np.datetime64 type) or with floats. Integer dates must be converted into floats (the pandas accessor does it automatically).
The type of the events must be str or int.
Usage Example:
>>> seq = [('a',1),('c',2),('b',3),('a',8),('a',10),('b',12),('a',15),('c',17), ('b',20),('c',23),('c',25),('b',26),('c',28),('b',30)]
>>> dates = np.array([np.datetime64('1970-01-01') + np.timedelta64(e[1],'D') for e in seq], dtype='datetime64')
>>> data = np.array([e[0] for e in seq])
>>> ts = TimedSequence(dates, data)
Chronicles
The core of this package is the Chronicle class, which represents the chronicle temporal model and offers efficient matching functionalities.
Chronicles handle two models of time (which must be consistent with the model of time of the timed sequences):
- numerical timestamps using floats,
- dates using the
datetime64format. In this case, the temporal constraints of a chronicle must be defined usingnp.timedelta64values.
A chronicle cannot combine constraints of the two different kinds. The first defined constraint defines the model of time of the chronicle.
The package implements efficient algorithms to recognize chronicles. There are three different ways to recognize a chronicle in a sequence of events:
- the absence/presence recognition (
c.match(seq)): its result is a boolean stating whether the chronicle occurs at least once in the sequence. This is the most efficient algorithm, as it stops as soon as an occurrence is found. - the occurrence enumeration (
c.recognize(seq)): its result is the list of the occurrences of the chronicle in the sequence. Contrary to the first function, it looks for all the possible combinations of events. Thus, it is less efficient, but more informative. - the lazy occurrence enumeration (
c.iter_recognize(seq)): it returns an iterator over the same occurrences, which are computed on demand. This is useful to process the occurrences one at a time, or to stop the enumeration early.
In an occurrence, two identical events of a chronicle cannot match the same event of the sequence.
Note that a chronicle is somehow similar to a simple temporal network, and the set of constraints may be inconsistent or redundant. It is possible to minimize the temporal constraints of a chronicle using the corresponding function.
>>> c=Chronicle()
>>> c.add_event(0,'a')
>>> c.add_event(1,'b')
>>> c.add_event(2,'c')
>>> c.add_constraint(0,1, (1.0,2.0))
>>> c.add_constraint(0,2, (2.0,5.0))
>>> c.add_constraint(1,2, (0.0,2.0))
>>> print(c)
C1 {{[a],[b],[c]}}
0,1: (1.0, 2.0)
0,2: (2.0, 5.0)
1,2: (0.0, 2.0)
>>> c.minimize()
>>> print(c)
C1 {{[a],[b],[c]}}
0,1: (1.0, 2.0)
0,2: (2.0, 4.0)
1,2: (0.0, 2.0)
You can also depict a chronicle as a matplotlib graph:
>>> c.draw()
Fuzzy chronicles
The FuzzyChronicle class enables the approximate recognition of a chronicle. A fuzzy chronicle is defined in the same way as a chronicle.
The temporal model is enriched with the modeling of the fuzziness of the temporal constraints (lbda parameter).
In addition to the matching functions presented above, it provides a cmp function that finds all the occurrences of a chronicle whose degree of matching is above a threshold (parameter threshold), together with their similarity. The iter_cmp function is its lazy version.
More details about fuzzy chronicles are available in the article (in French):
@inproceedings{fchronicles,
title = {{\'E}num{\'e}ration des occurrences d'une chronique},
author = {Guyet, Thomas and Besnard, Philippe and Samet, Ahmed and Ben Salha, Nasreddine and Lachiche, Nicolas},
booktitle = {Actes de la conférence Extraction et Gestion des Connaissances},
publisher = {{\'E}ditions RNTI},
pages = {253--260},
year = {2020}
}
Dataframe accessor
The pychronicles package includes a new accessor for pandas dataframes.
This accessor, denoted tpattern, makes it possible to use chronicle functionalities directly with pandas. In this case, you do not even need to manage your timed sequences by hand: everything can be done directly with pandas.
The pandas accessor enables:
- to match or enumerate occurrences of a chronicle
- to match a Metric Temporal Logic (MTL) formula
- to abstract a collection of sequences
- to extract the delays of matching occurrences of a chronicle
Spirit of the accessor
The spirit of our accessor is to use a pandas dataframe to encode a sequence or a collection of sequences. The index of the dataframe models the time: it can be an integer, float or date index.
A column of the dataframe can be defined to specify the identifier of the sequences. In the following example, the column named id denotes this identifier. Intuitively, it can be used to make groupby operations, to process a collection of timed sequences.
The important feature of our package is that any other column can be added to the dataframe. The idea behind this modeling is that the dataframe gathers all the information about the events. The columns describe each event (and can contain None).
The example below illustrates the representation of a collection of sequences with a Pandas dataframe.
The basic sequence is repeated three times to create a collection of three timed sequences. In this case, the index of the dataframe is defined with dates.
The event names of the basic sequence feed the column label, and we added three columns:
- a column
str_valwith the string values (twice the label) - a column
num_valwith random numbers - a column
idthat represents the identifier of a sequence.
# create a basic sequence
seq = [('a',1),('c',2),('b',3),('a',8),('a',10),('b',12),('a',15),('c',17)]
df = pd.DataFrame({
"label": [e[0] for e in seq]*3,
"str_val": [e[0]*2 for e in seq]*3,
"num_val": np.random.randint(10,size=3*len(seq)),
'id': [1]*len(seq)+[2]*len(seq)+[3]*len(seq)
},
index = [np.datetime64('1980-01-01') + np.timedelta64(e[1],'D') for e in seq ]*3
)
Then, the events of a chronicle are no longer event names, but descriptions of the events of interest through their features.
When used with pandas dataframes, the events of a chronicle are queries on the dataframe.
In the example above, an event could be defined by "label=='a' & num_val>3".
For the time being, our framework handles string and number attributes. The specification of an event uses the classical operators defined in the Pandas documentation.
Matching / enumerating the occurrences of a chronicle
Let us now use a dataframe defined as explained in the previous section with a chronicle.
For that, we first need to define a chronicle. It works exactly as before, but the chronicle's events are defined by queries.
For instance, the following example illustrates the definition of a simple chronicle that uses queries as events:
c=Chronicle()
c.add_event(0,'label=="a"')
c.add_event(1,'label=="b" & num_val>5')
c.add_constraint(0,1, (np.timedelta64(4,'D'),np.timedelta64(10,'D')))
For more details about the syntax of queries, please refer to the Pandas documentation.
This chronicle is semantically sound to be used with the dataframe defined in the example of the previous section.
Dataframe representing a sequence
In this section, we consider that the dataframe represents a unique sequence (for instance, we selected the first sequence of the previous dataframe with df[df.id==1]).
The recognition functionalities are now accessible through the Pandas dataframe:
df.tpattern.match(c)returns a boolean indicating whether the chronicle occurs in the sequence or notdf.tpattern.recognize(c)returns the list of the occurrences ofcin the sequence (df.tpattern.iter_recognize(c)is its lazy version)df.tpattern.project(c)returns a dataframe with the delays of the temporal constraints of the chroniclecin each of its occurrences
Note that the tpattern keyword gives access to the features of our package.
Using chronicles becomes very easy with Pandas dataframes, and they also gain in expressivity thanks to the use of queries as events. We keep a good computational efficiency of the recognition algorithms thanks to the rewriting principle developed with semantic chronicles (see the reference below).
@inproceedings{semantic_chronicles,
title = {An extension of chronicles temporal model with taxonomies -- Application to epidemiological studies},
author = {Bakalara, Johanne and Guyet, Thomas and Dameron, Olivier and Happe, Andr{\'e} and Oger, Emmanuel},
booktitle = {Proceedings of the 14th International Conference on Health Informatics (HEALTHINF)},
pages = {1--10},
year = {2021}
}
Dataframe representing a collection of sequences
To process a dataframe containing several sequences, you can use the following idiom based on apply:
reco=df.groupby('id').apply(lambda d: d.tpattern.match(c))
In this case, the attribute id is used to select the events for each sequence, and then the match algorithm is applied to this selection.
The result is a pandas series containing a boolean value for each sequence id.
The same applies to the recognize function.
Use of MTL formulas
For situation recognition, there are strong connections between chronicles and metric temporal logics such as MTL or TPTL. See the article:
@inproceedings{chronicle_templogics,
title = {Logical forms of chronicles},
author = {Guyet, Thomas and Markey, Nicolas},
booktitle = {Proceedings of the 29th International Symposium on Temporal Representation and Reasoning (TIME)},
pages = {1--15},
year = {2022}
}
To illustrate that the principle of semantic chronicles can be broader, we extended it to the case of MTL formulas. This means that our framework makes it possible to express MTL formulas using queries as events.
The tpattern accessor provides a function match_mtl that is dedicated to the recognition of an MTL formula in the timed sequence (only for dataframes indexed by floats or integers). This function returns a boolean value. There is no equivalent to the enumeration of the occurrences of a chronicle.
The syntax of an MTL formula is based on the two operators F (eventually) and G (globally). These operators can be specified with temporal constraints (in brackets). They combine events defined in the same way as chronicle events.
query = ' F(label=="a" & F[2.9,5]( label=="b" & num_val>5 ))'
df.tpattern.match_mtl(query)
This feature is based on the Python implementation of MTL.
Abstraction of a sequence
Finally, we also provide a function that abstracts a collection of sequences into a chronicle.
Contrary to the recognition functions that can handle several columns, the abstraction requires a column that corresponds to the event names. In addition, it requires the name of the attribute that corresponds to the identifier of the sequences.
Then, the function, named abstract, takes these two arguments and outputs a single chronicle that occurs in all the sequences.
The following example extracts a chronicle from the dataframe introduced above. Its events are based on the label attribute.
chro = df.tpattern.abstract('label', 'id')
Additional features
Export / import of chronicles
It is possible to specify chronicles using the CRS format (see the functions load and to_crs of the pychronicles.utils module). The following code illustrates the syntax for specifying a chronicle in this format.
chronicle C27_sub_0[]()
{
event(Event_Type1[], t006)
event(Event_Type1[], t004)
event(Event_Type2[], t002)
event(Event_Type3[], t001)
t004-t006 in [17,25]
t006-t002 in [-16,-10]
t002-t001 in [14,29]
t004-t001 in [27,35]
}
Perspectives
- implementation of chronicle mining algorithms
- graphical chronicle editor
- compile the recognition (e.g. with Cython)
- optimized matching of several chronicles at the same time
If you think that this package can be improved, do not hesitate to contact the author to request additional features. He will do his best to maintain the code and improve it continuously.
Authorship
- Author: Thomas Guyet
- Institution: Inria/AIstroSight
- date: 10/2026
Metadata
Release files for pychronicles 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pychronicles-0.2.0.tar.gz | 60.7 kB | Without provenance | Details for pychronicles-0.2.0.tar.gz |
Release files / pychronicles-0.2.0.tar.gz
| Download URL | pychronicles-0.2.0.tar.gz |
|---|---|
| Size | 60.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6d5f47e329093b590864f2a0d1adb2d5c3b9c831c1be50f7998ce15c68b232c2
|
|
BLAKE2b-256 checksum How to use checksums |
fd59dd406df5cafcdf6982fb05ca3c64ea4d660bbcbf51ff644e2ff2e85ce77f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Security for pychronicles 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.