Name | pychronicles JSON |
Version |
0.1.6
JSON |
| download |
home_page | None |
Summary | A package for chronicle recognition. |
upload_time | 2024-08-14 14:22:51 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.7 |
license | GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. |
keywords |
data analysis
pattern
timed sequence
temporal data
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
---
title: README
author: Thomas Guyet
---
[![PyPI version](https://badge.fury.io/py/pychronicles.svg)](https://badge.fury.io/py/pychronicles)
[![Downloads](https://static.pepy.tech/badge/pychronicles)](https://pepy.tech/project/pychronicles)
[![GitHub license](https://img.shields.io/static/v1?label=Licence&message=LGPL-3&color=green)](https://www.gnu.org/licenses/lgpl-3.0.txt)
# PyChronicles package
A chronicle is a specification of the 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 occurrences of pairs of events must occurs within a given temporal interval.
It can be used to recognize complex behaviors in sequence the temporal events.
PyChronicle is now compatible with Pandas and makes chronicle features [available on dataframes](#dataframe-accessor) representing timed sequences.
This library proposes a Python class to define a timed sequence, a chronicle, to read/save them in a standard CRS format. The main useful functionnality for chronicle is their efficient matching in a timed sequence, or pandas dataframes (including an accessor).
The Abstracter class enables to create a chronicle from a collection of sequences.
For more details about the usage of this package, have a look at the [documentation](https://tguyet.gitlabpages.inria.fr/pychronicles/). And for more (formal) information about the temporal model of chronicle, see our [book](https://link.springer.com/book/10.1007/978-3-031-33693-5) and :
~~~bibtex
@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](https://pypi.org/project/hydronaut) is available in [Python Package Index](https://pypi.org/) and can be installed using any standard Python package manager.
~~~sh
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:
~~~sh
git clone --recursive https://gitlab.inria.fr/tguyet/pychronicles
cd pychronicles
pip install -r requirements.txt
pip install --upgrade .
~~~
You can also use pip to install the repo from sources. For instance, run the following commands:
~~~sh
git clone --recursive https://gitlab.inria.fr/tguyet/pychronicles
cd pychronicles
pip install .
~~~
In case you want to visualize chronicles, you will need to install manually the `networkx` and `matplotlib`. By default, they are not installed and the plotting functions is disabled.
~~~sh
pip install networkx matplotlib
~~~
## Basic Usage Example
```python
import pychonicles
# create a timed sequence
seq = [('a',1),('c',2),('b',3),('a',8)]
ts = TimedSequence(np.array([e[2] for e in seq], dtype='float'), [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 matches 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. To make it more efficient, it is possible to compile a cythonized version.
### Timed Sequences
A timed sequence is be created from different manners:
* a pair of lists containing the events and the dates.
* a simple list of items (`str`, `int` or `None`) with implicit timestamps : `['a', 'b', ..., None, 'c', None, 'd']`. In this case, `None` means that there is no event at the corresponding time instant.
* a list of explicitly timestamped items (`str` or `int`) : `[ (1,'a'), (23,'b'), (30,'c'), (45, 'd')]`
The dates can be represented with dates (only with `np.datetime64` type) or floats. In case the dates are coded as `int`, they will be converted in `float`.
The type of events must be `str` or `int`.
_Usage Example_:
```python
>>> 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 the this package is the chronicle class that represents a chronicle temporal model which also offers efficient matching functionalities.
The chronicles handles two models of time (that must be consistent with the model of time in timed sequences):
* discrete timestamps using floats
* continuous timestamps using `datetime64` format. In this case the temporal constraints of a chronicle must be defined using `np.timedelta64` values.
A chronicle can not combine constraints of the two different kinds. The first defined constraint defines the model of time of the chronicle.
<br>
The package implements efficient algorithms to recognize it. It benefits from numpy functionalities to increase their efficiency. There are three different ways to _recognize_ a chronicle in a sequence of a events:
* the absence/presence recognition (`c.match(seq)`): its result is a boolean stating whether the chronicle occur at least once in the sequence, this is the most efficient algorithm
* the occurrence enumeration (`c.recognize(seq)`): its result is a list of occurrences of the chronicle in a sequence. Contrary to the first function, it looks for all possible combination of events. Thus it is less efficient, but more informative.
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.
```python
>>> 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:
```python
>>> c.draw()
```
### Fuzzy chronicles
The `FuzzyChronicle` class represents a class for approximated recognition of a chronicle. The chronicle is defined in the same way of a chronicle.
The temporal model is enriched with the modeling of a fuzzyness of temporal constraints (`lbda` parameter)
In addition to the matching function presented above, it proposes a `cmp` function that finds occurrences of a chronicle with a degree of matching (parameter `threshold`).
More details about fuzzy chroniclesis available in the [article](https://hal.archives-ouvertes.fr/hal-03698361) (in French):
```bibtex
@inproceedings{fchronicles,
title = {{\'E}num{\'e}ration des occurrences d'une chronique},
author = {Guyet, Thomas and Besnard, Philippe and Ben Salha, Nasreddine and Samet, Ahmed 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 include an new accessor for pandas dataframe.
This accessor enables, denoted `tpattern` to use chronicle functionnalities directly with the pandas packages. 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 an 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 sequence. The index of the dataframe models the time ... it can be integer, float or date index.
A column of the dataframe acn be defined to specify the identifier of the sequence. In the following example, the column name `id` denotes this identifier. Intuitivelly, 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 gather all the information about the events. The columns describes each event (an 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. The index of the dataframe is defined with dates in this case.
The event names of the basic sequence feed the column `label`, and we added two columns:
* a column `str_val` with the string values (twice the label)
* a column `num_val` with random numbers
* a column `id` that represents the identifier of a sequence.
```python
#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
)
```
<br>
Then, the definition of the event of a chronicle are no more the name of an event, but the description of a event of interest throught its description features.
When used with pandas dataframe, 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`".
At the time, our framework handles string and number attributes. The specification of an event uses the classical operators defined in the [Pandas documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.query.html).
## Matching / enumerate occurrences of a chronicle
Let us now use a dataframe defined as explained in the previous section with chronicle.
For that, we first need to define a chronicle. It works exactly as before but, the definition of chronicle's events are queries.
For instance, the following example illustrates the definition a simple chronicle that uses queries as event:
```python
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](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.query.html).
This chronicle is semantically sound for being used in 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 throught the Pandas dataframe:
* `df.tpattern.match(c)` will return a boolean indicating whether the chronicle occurs in the sequence or not
* `df.tpattern.recognize(c)` will return the list of occurrences of `c` in the sequence
* `df.tpattern.project(c)` will return the list of delays for the temporal constraints of the chronicle `c`
Note that the `tpattern` keyword gives access to the features of our package.
<br>
Using chronicles becomes very easy with Pandas dataframe and it also gains in expressivity thanks to the use of queries as event.
We keep good computational efficiency of the recognition algorithms thanks to the use of the rewritting principle developed with semantic chronicles (see reference below).
```bibtex
@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 proceed with a dataframe containing several sequences, you can use the following idioms based on `apply`:
```python
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 dataframe containing a boolean value for each sequence id.
The same applies with `recognition` function.
## Use of MTL formula
For situation recognitinon, there are strong connection between chronicles and metric temporal logics such as MTL or TPTL. See the [article](https://hal.archives-ouvertes.fr/hal-03777471):
~~~bibtex
@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 formulae. This means that our framework enables to express a MTL formulae using query as events.
The `tpattern` accessor provides a function `match_mtl` that is dedicated to the recognition of a MTL formulae in the timed sequence. This function returns a boolean value. There is not equivalent to the enumeration of 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 combines events defined in the same way as for chronicle events.
```python
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](https://github.com/mvcisback/py-metric-temporal-logic).
## Abstraction of a sequence
Finally, we also provide a function that enables to abstract a collection of sequences into a chronicle.
Contrary to the recognition functions that can handle several columns, the abstraction requires to define a column that correspond to the event name. In addition, it requires to define the attribute name that correspond to the identifier of the sequences.
Then, the function, named `abstract` takes two arguments and outputs a single chronicle that occurs in all the sequences.
The following example extracts a chronicle from the dataframe introduced above. It focuses event on the label attribute.
```python
chro = df.tpattern.abstract('label', 'id')
```
# Additional features
## Export / import of chronicles
It is possible to specify chronicles using the CRS format. The following code illustrate 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
* better cythonize the recognition
* 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:** 08/2024
Raw data
{
"_id": null,
"home_page": null,
"name": "pychronicles",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "Thomas Guyet <thomas.guyet@inria.fr>",
"keywords": "data analysis, pattern, timed sequence, temporal data",
"author": null,
"author_email": "Thomas Guyet <thomas.guyet@inria.fr>",
"download_url": "https://files.pythonhosted.org/packages/30/22/22f8f36ed58ae9d1813c619e197553e193f799b0a13e238f8fba70a7660b/pychronicles-0.1.6.tar.gz",
"platform": null,
"description": "---\ntitle: README\nauthor: Thomas Guyet\n---\n\n[![PyPI version](https://badge.fury.io/py/pychronicles.svg)](https://badge.fury.io/py/pychronicles)\n[![Downloads](https://static.pepy.tech/badge/pychronicles)](https://pepy.tech/project/pychronicles)\n[![GitHub license](https://img.shields.io/static/v1?label=Licence&message=LGPL-3&color=green)](https://www.gnu.org/licenses/lgpl-3.0.txt)\n\n\n# PyChronicles package\n\nA chronicle is a specification of the 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 occurrences of pairs of events must occurs within a given temporal interval.\nIt can be used to recognize complex behaviors in sequence the temporal events.\nPyChronicle is now compatible with Pandas and makes chronicle features [available on dataframes](#dataframe-accessor) representing timed sequences.\n\nThis library proposes a Python class to define a timed sequence, a chronicle, to read/save them in a standard CRS format. The main useful functionnality for chronicle is their efficient matching in a timed sequence, or pandas dataframes (including an accessor).\nThe Abstracter class enables to create a chronicle from a collection of sequences.\n\nFor more details about the usage of this package, have a look at the [documentation](https://tguyet.gitlabpages.inria.fr/pychronicles/). And for more (formal) information about the temporal model of chronicle, see our [book](https://link.springer.com/book/10.1007/978-3-031-33693-5) and :\n~~~bibtex\n@book{chroniclesbook,\n title = {Chronicles: Formalization of a Temporal Model},\n author = {Besnard, Philippe and Guyet, Thomas},\n publisher={Springer Nature},\n pages = {121},\n year = {2023}\n}\n~~~\n\n## Package installation\n\nThe [PyChronicles package](https://pypi.org/project/hydronaut) is available in [Python Package Index](https://pypi.org/) and can be installed using any standard Python package manager.\n~~~sh\npip install pychronicles\n~~~\n\nIt can also be installed from source. For example, to install it with pip, either locally or in a virtual environment, run the following commands:\n\n~~~sh\ngit clone --recursive https://gitlab.inria.fr/tguyet/pychronicles\ncd pychronicles\npip install -r requirements.txt\npip install --upgrade .\n~~~\n\nYou can also use pip to install the repo from sources. For instance, run the following commands: \n~~~sh\ngit clone --recursive https://gitlab.inria.fr/tguyet/pychronicles\ncd pychronicles\npip install .\n~~~\n\nIn case you want to visualize chronicles, you will need to install manually the `networkx` and `matplotlib`. By default, they are not installed and the plotting functions is disabled.\n~~~sh\npip install networkx matplotlib\n~~~\n\n## Basic Usage Example\n\n```python\nimport pychonicles\n\n# create a timed sequence\nseq = [('a',1),('c',2),('b',3),('a',8)]\nts = TimedSequence(np.array([e[2] for e in seq], dtype='float'), [e[0] for e in seq])\n\n#create a chronicle\nc=Chronicle()\nc.add_event(0,'a')\nc.add_event(1,'b')\nc.add_constraint(0,1, (0.4,2.3))\n\nreco=c.match(ts)\nprint(f\"Does the chronicle matches the sequence? [{reco}]\")\n\nreco=c.recognize(ts)\nprint(f\"What are the occurrences of the chronicle in the sequence? [{reco}]\")\n```\n\n\n## Efficient chronicle recognition\n\nThe chronicle recognition package is a pure python package implemented using numpy features. To make it more efficient, it is possible to compile a cythonized version.\n\n### Timed Sequences\n\nA timed sequence is be created from different manners:\n* a pair of lists containing the events and the dates.\n* a simple list of items (`str`, `int` or `None`) with implicit timestamps : `['a', 'b', ..., None, 'c', None, 'd']`. In this case, `None` means that there is no event at the corresponding time instant.\n* a list of explicitly timestamped items (`str` or `int`) : `[ (1,'a'), (23,'b'), (30,'c'), (45, 'd')]`\n\nThe dates can be represented with dates (only with `np.datetime64` type) or floats. In case the dates are coded as `int`, they will be converted in `float`.\n\nThe type of events must be `str` or `int`.\n\n_Usage Example_:\n```python\n>>> 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)]\n>>> dates = np.array([np.datetime64('1970-01-01') + np.timedelta64(e[1],'D') for e in seq], dtype='datetime64')\n>>> data = np.array([e[0] for e in seq])\n>>> ts = TimedSequence(dates, data)\n```\n\n### Chronicles\n\nThe core of the this package is the chronicle class that represents a chronicle temporal model which also offers efficient matching functionalities.\n\n\nThe chronicles handles two models of time (that must be consistent with the model of time in timed sequences):\n* discrete timestamps using floats\n* continuous timestamps using `datetime64` format. In this case the temporal constraints of a chronicle must be defined using `np.timedelta64` values.\n\nA chronicle can not combine constraints of the two different kinds. The first defined constraint defines the model of time of the chronicle. \n\n<br>\n\nThe package implements efficient algorithms to recognize it. It benefits from numpy functionalities to increase their efficiency. There are three different ways to _recognize_ a chronicle in a sequence of a events:\n\n* the absence/presence recognition (`c.match(seq)`): its result is a boolean stating whether the chronicle occur at least once in the sequence, this is the most efficient algorithm\n* the occurrence enumeration (`c.recognize(seq)`): its result is a list of occurrences of the chronicle in a sequence. Contrary to the first function, it looks for all possible combination of events. Thus it is less efficient, but more informative.\n\nNote 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.\n\n```python\n>>> c=Chronicle()\n>>> c.add_event(0,'a')\n>>> c.add_event(1,'b')\n>>> c.add_event(2,'c')\n>>> c.add_constraint(0,1, (1.0,2.0))\n>>> c.add_constraint(0,2, (2.0,5.0))\n>>> c.add_constraint(1,2, (0.0,2.0))\n>>> print(c)\nC1 {{[a],[b],[c]}}\n0,1: (1.0, 2.0)\n0,2: (2.0, 5.0)\n1,2: (0.0, 2.0)\n\n>>> c.minimize()\n>>> print(c)\nC1 {{[a],[b],[c]}}\n0,1: (1.0, 2.0)\n0,2: (2.0, 4.0)\n1,2: (0.0, 2.0)\n```\n\nYou can also depict a chronicle as a matplotlib graph:\n\n```python\n>>> c.draw()\n```\n\n\n### Fuzzy chronicles\n\nThe `FuzzyChronicle` class represents a class for approximated recognition of a chronicle. The chronicle is defined in the same way of a chronicle. \nThe temporal model is enriched with the modeling of a fuzzyness of temporal constraints (`lbda` parameter)\n\nIn addition to the matching function presented above, it proposes a `cmp` function that finds occurrences of a chronicle with a degree of matching (parameter `threshold`).\n\nMore details about fuzzy chroniclesis available in the [article](https://hal.archives-ouvertes.fr/hal-03698361) (in French):\n```bibtex\n@inproceedings{fchronicles,\n title = {{\\'E}num{\\'e}ration des occurrences d'une chronique},\n author = {Guyet, Thomas and Besnard, Philippe and Ben Salha, Nasreddine and Samet, Ahmed and Lachiche, Nicolas},\n booktitle = {Actes de la conf\u00e9rence Extraction et Gestion des Connaissances},\n publisher = {{\\'E}ditions RNTI},\n pages = {253--260},\n year = {2020}\n}\n```\n\n# Dataframe accessor\n\nThe `pychronicles` package include an new accessor for pandas dataframe. \nThis accessor enables, denoted `tpattern` to use chronicle functionnalities directly with the pandas packages. In this case, you do not even need to manage your timed sequences by hand ... everything can be done directly with pandas.\n\nThe pandas accessor enables:\n\n* to match or enumerate occurrences of a chronicle\n* to match an Metric Temporal Logic (MTL) formula\n* to abstract a collection of sequences \n* to extract the delays of matching occurrences of a chronicle\n\n\n## Spirit of the accessor\n\nThe spirit of our accessor is to use a pandas dataframe to encode a sequence or a collection of sequence. The index of the dataframe models the time ... it can be integer, float or date index. \n\nA column of the dataframe acn be defined to specify the identifier of the sequence. In the following example, the column name `id` denotes this identifier. Intuitivelly, it can be used to make `groupby` operations, to process a collection of timed sequences. \n\nThe 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 gather all the information about the events. The columns describes each event (an can contain `None`). \n\n\nThe example below illustrates the representation of a collection of sequences with a Pandas dataframe. \nThe basic sequence is repeated three times to create a collection of three timed sequences. The index of the dataframe is defined with dates in this case. \nThe event names of the basic sequence feed the column `label`, and we added two columns:\n\n\n* a column `str_val` with the string values (twice the label)\n* a column `num_val` with random numbers\n* a column `id` that represents the identifier of a sequence.\n\n```python\n#create a basic sequence\nseq = [('a',1),('c',2),('b',3),('a',8),('a',10),('b',12),('a',15),('c',17)]\ndf = pd.DataFrame({\n \"label\": [e[0] for e in seq]*3,\n \"str_val\": [e[0]*2 for e in seq]*3,\n \"num_val\": np.random.randint(10,size=3*len(seq)),\n 'id': [1]*len(seq)+[2]*len(seq)+[3]*len(seq)\n },\n index = [np.datetime64('1980-01-01') + np.timedelta64(e[1],'D') for e in seq ]*3\n)\n```\n\n<br>\n\nThen, the definition of the event of a chronicle are no more the name of an event, but the description of a event of interest throught its description features. \nWhen used with pandas dataframe, the events of a chronicle are queries on the dataframe.\nIn the example above, an event could be defined by \"`label=='a' & num_val>3`\".\n\nAt the time, our framework handles string and number attributes. The specification of an event uses the classical operators defined in the [Pandas documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.query.html). \n\n## Matching / enumerate occurrences of a chronicle\n\nLet us now use a dataframe defined as explained in the previous section with chronicle. \n\nFor that, we first need to define a chronicle. It works exactly as before but, the definition of chronicle's events are queries.\n\nFor instance, the following example illustrates the definition a simple chronicle that uses queries as event:\n```python\nc=Chronicle()\nc.add_event(0,'label==\"a\"')\nc.add_event(1,'label==\"b\" & num_val>5')\nc.add_constraint(0,1, (np.timedelta64(4,'D'),np.timedelta64(10,'D')))\n``` \n\nFor more details about the syntax of queries, please refer to the [Pandas documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.query.html). \n\nThis chronicle is semantically sound for being used in with the dataframe defined in the example of the previous section. \n\n### Dataframe representing a sequence\n\nIn 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]`).\n\nThe recognition functionalities are now accessible throught the Pandas dataframe:\n\n* `df.tpattern.match(c)` will return a boolean indicating whether the chronicle occurs in the sequence or not\n* `df.tpattern.recognize(c)` will return the list of occurrences of `c` in the sequence\n* `df.tpattern.project(c)` will return the list of delays for the temporal constraints of the chronicle `c`\n\nNote that the `tpattern` keyword gives access to the features of our package.\n\n<br>\n\nUsing chronicles becomes very easy with Pandas dataframe and it also gains in expressivity thanks to the use of queries as event. \nWe keep good computational efficiency of the recognition algorithms thanks to the use of the rewritting principle developed with semantic chronicles (see reference below). \n\n```bibtex\n@inproceedings{semantic_chronicles,\n title = {An extension of chronicles temporal model with taxonomies -- Application to epidemiological studies},\n author = {Bakalara, Johanne and Guyet, Thomas and Dameron, Olivier and Happe, Andr{\\'e} and Oger, Emmanuel},\n booktitle = {Proceedings of the 14th International Conference on Health Informatics (HEALTHINF)},\n pages = {1--10},\n year = {2021}\n}\n```\n\n### Dataframe representing a collection of sequences\n\nTo proceed with a dataframe containing several sequences, you can use the following idioms based on `apply`:\n\n```python\nreco=df.groupby('id').apply(lambda d: d.tpattern.match(c))\n```\n\nIn this case, the attribute `id` is used to select the events for each sequence, and then the `match` algorithm is applied to this selection.\nThe result is a dataframe containing a boolean value for each sequence id.\n\nThe same applies with `recognition` function.\n\n## Use of MTL formula\n\nFor situation recognitinon, there are strong connection between chronicles and metric temporal logics such as MTL or TPTL. See the [article](https://hal.archives-ouvertes.fr/hal-03777471):\n~~~bibtex\n@inproceedings{chronicle_templogics,\n title = {Logical forms of chronicles},\n author = {Guyet, Thomas and Markey, Nicolas},\n booktitle = {Proceedings of the 29th International Symposium on Temporal Representation and Reasoning (TIME)},\n pages = {1--15},\n year = {2022}\n}\n~~~\n\nTo illustrate that the principle of semantic chronicles can be broader, we extended it to the case of MTL formulae. This means that our framework enables to express a MTL formulae using query as events. \n\n\nThe `tpattern` accessor provides a function `match_mtl` that is dedicated to the recognition of a MTL formulae in the timed sequence. This function returns a boolean value. There is not equivalent to the enumeration of occurrences of a chronicle.\n\nThe 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 combines events defined in the same way as for chronicle events.\n\n```python\nquery = ' F(label==\"a\" & F[2.9,5]( label==\"b\" & num_val>5 ))'\ndf.tpattern.match_mtl(query)\n```\n\nThis feature is based on the [Python implementation of MTL](https://github.com/mvcisback/py-metric-temporal-logic). \n\n## Abstraction of a sequence\n\nFinally, we also provide a function that enables to abstract a collection of sequences into a chronicle. \n\nContrary to the recognition functions that can handle several columns, the abstraction requires to define a column that correspond to the event name. In addition, it requires to define the attribute name that correspond to the identifier of the sequences. \nThen, the function, named `abstract` takes two arguments and outputs a single chronicle that occurs in all the sequences.\n\n\nThe following example extracts a chronicle from the dataframe introduced above. It focuses event on the label attribute.\n```python\nchro = df.tpattern.abstract('label', 'id')\n```\n\n# Additional features\n\n## Export / import of chronicles\n\nIt is possible to specify chronicles using the CRS format. The following code illustrate the syntax for specifying a chronicle in this format.\n\n```\nchronicle C27_sub_0[]()\n{\n event(Event_Type1[], t006)\n\tevent(Event_Type1[], t004)\n event(Event_Type2[], t002)\n event(Event_Type3[], t001)\n\n t004-t006 in [17,25]\n t006-t002 in [-16,-10]\n t002-t001 in [14,29]\n t004-t001 in [27,35]\n}\n```\n\n## Perspectives\n\n* implementation of chronicle mining algorithms\n* graphical chronicle editor\n* better cythonize the recognition\n* optimized matching of several chronicles at the same time\n\n\nIf 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.\n\n\n# Authorship\n\n* **Author:** Thomas Guyet\n* **Institution:** Inria/AIstroSight\n* **date:** 08/2024\n\n",
"bugtrack_url": null,
"license": "GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, \"this License\" refers to version 3 of the GNU Lesser General Public License, and the \"GNU GPL\" refers to version 3 of the GNU General Public License. \"The Library\" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An \"Application\" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A \"Combined Work\" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the \"Linked Version\". The \"Minimal Corresponding Source\" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The \"Corresponding Application Code\" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License \"or any later version\" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. ",
"summary": "A package for chronicle recognition.",
"version": "0.1.6",
"project_urls": {
"documentation": "https://tguyet.gitlabpages.inria.fr/pychronicles/",
"homepage": "https://gitlab.inria.fr/tguyet/pychronicles",
"repository": "https://gitlab.inria.fr/tguyet/pychronicles"
},
"split_keywords": [
"data analysis",
" pattern",
" timed sequence",
" temporal data"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "302222f8f36ed58ae9d1813c619e197553e193f799b0a13e238f8fba70a7660b",
"md5": "e472dc11404389dc128f5e7353599fef",
"sha256": "1bed55885d5b7a05f0692b2fb8444812da8c265ab3a40f014f542c6705354edd"
},
"downloads": -1,
"filename": "pychronicles-0.1.6.tar.gz",
"has_sig": false,
"md5_digest": "e472dc11404389dc128f5e7353599fef",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 41174,
"upload_time": "2024-08-14T14:22:51",
"upload_time_iso_8601": "2024-08-14T14:22:51.498297Z",
"url": "https://files.pythonhosted.org/packages/30/22/22f8f36ed58ae9d1813c619e197553e193f799b0a13e238f8fba70a7660b/pychronicles-0.1.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-14 14:22:51",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "pychronicles"
}