tpattern 0.1.13__tar.gz → 0.1.15__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. {tpattern-0.1.13/tpattern.egg-info → tpattern-0.1.15}/PKG-INFO +115 -26
  2. {tpattern-0.1.13 → tpattern-0.1.15}/README.md +114 -25
  3. {tpattern-0.1.13 → tpattern-0.1.15}/pyproject.toml +1 -1
  4. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/__init__.py +1 -1
  5. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/significance.py +19 -0
  6. {tpattern-0.1.13 → tpattern-0.1.15/tpattern.egg-info}/PKG-INFO +115 -26
  7. {tpattern-0.1.13 → tpattern-0.1.15}/LICENSE +0 -0
  8. {tpattern-0.1.13 → tpattern-0.1.15}/setup.cfg +0 -0
  9. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_calibrated_pins.py +0 -0
  10. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_contrast.py +0 -0
  11. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_dedup.py +0 -0
  12. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_guided.py +0 -0
  13. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_null_compare.py +0 -0
  14. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_outputs.py +0 -0
  15. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_read_table_validation.py +0 -0
  16. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_reference.py +0 -0
  17. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_report_dendrogram.py +0 -0
  18. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_reproduction.py +0 -0
  19. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_sofcoder.py +0 -0
  20. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_starter.py +0 -0
  21. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_support_reporting.py +0 -0
  22. {tpattern-0.1.13 → tpattern-0.1.15}/tests/test_synthetic.py +0 -0
  23. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/advisor.py +0 -0
  24. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/ci.py +0 -0
  25. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/contrast.py +0 -0
  26. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/detect.py +0 -0
  27. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/diagnostics.py +0 -0
  28. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/guided.py +0 -0
  29. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/io.py +0 -0
  30. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/methods.py +0 -0
  31. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/pattern.py +0 -0
  32. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/randomise.py +0 -0
  33. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/reference.py +0 -0
  34. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/report.py +0 -0
  35. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/starter.py +0 -0
  36. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/synthetic.py +0 -0
  37. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern/viz.py +0 -0
  38. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern.egg-info/SOURCES.txt +0 -0
  39. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern.egg-info/dependency_links.txt +0 -0
  40. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern.egg-info/requires.txt +0 -0
  41. {tpattern-0.1.13 → tpattern-0.1.15}/tpattern.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tpattern
3
- Version: 0.1.13
3
+ Version: 0.1.15
4
4
  Summary: Open, auditable temporal & sequential pattern analysis (T-patterns) for sport and behaviour
5
5
  Author-email: Andrew Callaway <acallaway@bournemouth.ac.uk>
6
6
  License-Expression: MIT
@@ -36,20 +36,49 @@ Dynamic: license-file
36
36
  # tpattern — open, auditable analysis of recurring patterns in event sequences
37
37
 
38
38
  [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ajcallaway/TPattern/blob/main/colab/tpattern_guided_analysis.ipynb)
39
- [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.21397543.svg)](https://doi.org/10.5281/zenodo.21397543)
39
+ [![DOI](https://img.shields.io/badge/DOI-10.5281%2Fzenodo.21397543-1682D4.svg)](https://doi.org/10.5281/zenodo.21397543)
40
+ [![R interface](https://img.shields.io/badge/R-interface-276DC3?logo=r&logoColor=white)](#using-tpattern-from-r)
40
41
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
41
42
 
42
43
  `tpattern` detects recurring temporal patterns (T-patterns; Magnusson, 2000) in
43
44
  **any sequence of timed, labelled events** — behaviour, sport, clinical or
44
45
  physiological logs, interaction data — validates them properly against chance, and
45
46
  produces clean, publication-ready output (tables, figures, pattern dendrograms). It
46
- is a free, transparent, reproducible alternative to the THEME software, and the
47
- analysis companion to the [OpenTag.Studio](https://opentag.studio) tagging tool.
47
+ implements the published T-pattern detection algorithm (Magnusson, 2000) as free,
48
+ auditable code — the same critical-interval method the THEME software applies (see
49
+ [The method it implements](#the-method-it-implements)) — and is the analysis
50
+ companion to the [OpenTag.Studio](https://opentag.studio) tagging tool.
48
51
 
49
- ## Two ways to use it
52
+ ## Three ways to use it
50
53
 
51
- - 🚀 **Google Colab — no coding.** [**Open the guided notebook in Colab**](https://colab.research.google.com/github/ajcallaway/TPattern/blob/main/colab/tpattern_guided_analysis.ipynb): upload your events, follow the guided steps, download a report with a plain-English verdict for every pattern and a paste-ready Methods paragraph. See [`colab/`](colab/).
52
- - 🐍 **Python library.** `pip install` and script it — see [Quickstart](#quickstart) and [`examples/`](examples/).
54
+ ### 🚀 In your browser — Google Colab, no install, no coding
55
+
56
+ Open a guided notebook, upload your events CSV, and it walks you through four steps —
57
+ **upload → inspect → the recommended settings appear → run** — then hands back a full
58
+ report inline and as a downloadable zip: a colour-coded results table with a
59
+ plain-English verdict for every pattern (🟢 robust · 🟡 borderline · ⚪ likely chance),
60
+ the pattern dendrograms, the code to restyle them, and a paste-ready Methods paragraph.
61
+ No data of your own yet? Point it at [`data/worldcup_goals.csv`](data/worldcup_goals.csv)
62
+ (choose **ms** for the time unit).
63
+
64
+ Three notebooks (all in [`colab/`](colab/)):
65
+
66
+ - [**Guided analysis**](https://colab.research.google.com/github/ajcallaway/TPattern/blob/main/colab/tpattern_guided_analysis.ipynb) — **recommended.** Installs the latest `tpattern` and opens the guided panel. This is the one the badge above opens.
67
+ - **Guided analysis (offline)** — the package is bundled inside the notebook (a snapshot), so it runs where you can't install from GitHub. Open `colab/tpattern_guided_analysis_offline.ipynb` via **File → Upload notebook**.
68
+ - **Worked example** — the companion to the tutorial paper. Runs the load → inspect → recommend → calibrate → report walk-through on the shipped World Cup goal sequences, cell by cell, so you can follow the whole analysis and then swap in your own data (`colab/tpattern_worked_example.ipynb`).
69
+
70
+ ### 🐍 As a Python library
71
+
72
+ `pip install tpattern` and script it — see [Getting started](#getting-started-in-a-minute), [Quickstart](#quickstart) and [`examples/`](examples/).
73
+
74
+ ### 📊 From R
75
+
76
+ R users (common in psychology and behavioural science) can drive the **same
77
+ engine** via [`reticulate`](https://rstudio.github.io/reticulate/) — the algorithm
78
+ stays in Python, so results match exactly. Install with
79
+ `remotes::install_github("ajcallaway/TPattern", subdir = "r")`, then run the whole
80
+ analysis in one call: `tpattern_analysis("events.csv")`. See
81
+ [Using tpattern from R](#using-tpattern-from-r).
53
82
 
54
83
  Bring your own data (one row per event — see [`SCHEMA.md`](SCHEMA.md)) or try the
55
84
  included [`data/`](data/) example.
@@ -108,24 +137,47 @@ print(explain("fdr_q")) # what an output column means
108
137
  - **Clean figures** — dendrograms, temporal signatures, forest plots — that share one
109
138
  customisable style (`tpattern.viz.STYLE`).
110
139
 
111
- ## Why it exists
112
-
113
- T-pattern analysis finds behaviourally meaningful structure that event counts and
114
- simple transition tables miss. It is most established through Magnusson's THEME
115
- software, the reference implementation. `tpattern` is an open, complementary
116
- implementation that reproduces THEME's detection and adds a reproducible chance
117
- model and clear reporting:
118
-
119
- - **reproduces** THEME's core detection (critical intervals, NX/T baseline,
120
- completeness competition) — validated to the pattern for published datasets;
121
- - **adds a chance model** — every pattern is tested against a surrogate null, with
122
- p-values **corrected for repeated testing** (false-discovery rate and
123
- family-wise);
124
- - **handles concurrency explicitly** — events sharing a timestamp are treated as
140
+ ## The method it implements
141
+
142
+ T-pattern analysis (Magnusson, 1996, 2000, 2019) finds behaviourally meaningful
143
+ structure that event counts and simple transition tables miss: event types that
144
+ recur in the same order, separated by statistically consistent time intervals, more
145
+ often than chance predicts. `tpattern` implements the algorithm exactly as published,
146
+ in auditable code:
147
+
148
+ - **Critical-interval detection.** For an ordered pair of event types (A, B) it
149
+ searches for the time window after A within which B follows more often than B's
150
+ overall rate would predict. Significance is the **upper-tail binomial** probability
151
+ of the observed number of A→B matches, evaluated against that baseline rate (B's
152
+ occurrences spread over the observation length — the *NX/T* baseline), thresholded
153
+ at α (default 0.005). This is the critical-interval test set out in Magnusson (2000)
154
+ and the THEME manual.
155
+ - **Free search, one-to-one matching, distinct tokens.** A free search selects the
156
+ largest significant interval; occurrences are matched one-to-one by the greedy
157
+ procedure the algorithm specifies; a pattern must be built from distinct event
158
+ tokens.
159
+ - **Bottom-up hierarchy with completeness competition.** Patterns are grown level by
160
+ level, longer patterns competing with their sub-patterns so that only the most
161
+ complete survive.
162
+
163
+ On top of the published detection, `tpattern` adds what the method has historically
164
+ lacked:
165
+
166
+ - **a proper chance model** — every detected pattern is tested against a surrogate
167
+ null, with p-values **corrected for repeated testing** (Benjamini–Hochberg FDR for
168
+ screening, Holm family-wise for confirmatory claims), stratified by level;
169
+ - **explicit concurrency handling** — events sharing a timestamp are reported as
125
170
  co-occurrence rather than sequence;
126
- - **reports clearly** — tidy tables, effect sizes with confidence intervals,
171
+ - **clear reporting** — tidy tables, effect sizes with confidence intervals,
127
172
  purposeful figures, and clean **dendrograms** of the detected patterns.
128
173
 
174
+ The reference software for this method is THEME (PatternVision Ltd), with **SOF-CODER**
175
+ (Jonsson et al., 2006) as its coding front end. `tpattern` reads that ecosystem
176
+ directly — `read_sofcoder` ingests SOF-CODER/THEME (`DATANAME, T, Events`) files — so
177
+ existing THEME datasets can be re-analysed with the surrogate calibration, and
178
+ [OpenTag.Studio](https://opentag.studio) can play, for `tpattern`, the role SOF-CODER
179
+ plays for THEME.
180
+
129
181
  ## Installation
130
182
 
131
183
  ```bash
@@ -191,6 +243,32 @@ Config()).detect()` returns them directly.
191
243
  the groups (Fisher's exact test, odds ratio + 95% CI) and feeds `forest_plot` — or run
192
244
  `new_analysis(template="two_groups")` for a ready-made script.
193
245
 
246
+ ## Using tpattern from R
247
+
248
+ R users (common in psychology and behavioural science) can drive the **same
249
+ engine** from R — the algorithm stays in Python, called through
250
+ [`reticulate`](https://rstudio.github.io/reticulate/), so results match this
251
+ package and the paper exactly. The R interface lives in [`r/`](r/):
252
+
253
+ ```r
254
+ # install.packages("remotes")
255
+ remotes::install_github("ajcallaway/TPattern", subdir = "r")
256
+
257
+ library(tpattern)
258
+ install_tpattern() # once: sets up a Python environment R can use
259
+
260
+ # the whole analysis in one call — prints what the advisor recommends,
261
+ # calibrates every pattern, writes a report, returns the table:
262
+ out <- tpattern_analysis("my_events.csv", time_unit = "ms")
263
+ out$table
264
+ ```
265
+
266
+ Or step by step: `read_events()` → `advise()` (the advisor, pulled through in
267
+ full) → `calibrate_patterns()` → `results_table()` / `tpattern_report()`, with
268
+ `explain()` for any setting and `new_analysis()` to write a commented starter
269
+ script. See [`r/README.md`](r/README.md). (CRAN release to follow, then
270
+ `install.packages("tpattern")`.)
271
+
194
272
  ## The workflow
195
273
 
196
274
  ```
@@ -252,9 +330,9 @@ pip install -e .[test]
252
330
  pytest -q
253
331
  ```
254
332
 
255
- Both the synthetic ground-truth tests and the THEME reproduction tests run
256
- everywhere — the latter check the detector still reproduces THEME's published
257
- occurrence counts to the exact N.
333
+ Both the synthetic ground-truth tests and the reproduction tests run everywhere —
334
+ the latter check the detector still reproduces the occurrence counts published for
335
+ the reference datasets to the exact N.
258
336
 
259
337
  ## Citation
260
338
 
@@ -268,12 +346,23 @@ https://doi.org/10.5281/zenodo.21397543
268
346
  This DOI is permanent and always resolves to the current release. To pin a specific
269
347
  version instead, use that release's own DOI, listed on the Zenodo record.
270
348
 
271
- And the method:
349
+ And the method it implements:
272
350
 
273
351
  Magnusson, M. S. (2000). Discovering hidden time patterns in behavior: T-patterns
274
352
  and their detection. *Behavior Research Methods, Instruments, & Computers*, 32(1),
275
353
  93–110. https://doi.org/10.3758/BF03200792
276
354
 
355
+ The reference software for the method, and its coding front end:
356
+
357
+ Magnusson, M. S. (2019). T-pattern detection and analysis (TPA) with THEME: A mixed
358
+ methods approach. *Frontiers in Psychology*, 10, 2663.
359
+ https://doi.org/10.3389/fpsyg.2019.02663
360
+
361
+ Jonsson, G. K., Anguera, M. T., Blanco-Villaseñor, Á., Losada, J. L.,
362
+ Hernández-Mendo, A., Ardá, T., Camerino, O., & Castellano, J. (2006). Hidden
363
+ patterns of play interaction in soccer using SOF-CODER. *Behavior Research Methods*,
364
+ 38(3), 372–381. https://doi.org/10.3758/BF03192790
365
+
277
366
  ## License
278
367
 
279
368
  MIT — see [`LICENSE`](LICENSE).
@@ -1,20 +1,49 @@
1
1
  # tpattern — open, auditable analysis of recurring patterns in event sequences
2
2
 
3
3
  [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ajcallaway/TPattern/blob/main/colab/tpattern_guided_analysis.ipynb)
4
- [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.21397543.svg)](https://doi.org/10.5281/zenodo.21397543)
4
+ [![DOI](https://img.shields.io/badge/DOI-10.5281%2Fzenodo.21397543-1682D4.svg)](https://doi.org/10.5281/zenodo.21397543)
5
+ [![R interface](https://img.shields.io/badge/R-interface-276DC3?logo=r&logoColor=white)](#using-tpattern-from-r)
5
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
6
7
 
7
8
  `tpattern` detects recurring temporal patterns (T-patterns; Magnusson, 2000) in
8
9
  **any sequence of timed, labelled events** — behaviour, sport, clinical or
9
10
  physiological logs, interaction data — validates them properly against chance, and
10
11
  produces clean, publication-ready output (tables, figures, pattern dendrograms). It
11
- is a free, transparent, reproducible alternative to the THEME software, and the
12
- analysis companion to the [OpenTag.Studio](https://opentag.studio) tagging tool.
12
+ implements the published T-pattern detection algorithm (Magnusson, 2000) as free,
13
+ auditable code — the same critical-interval method the THEME software applies (see
14
+ [The method it implements](#the-method-it-implements)) — and is the analysis
15
+ companion to the [OpenTag.Studio](https://opentag.studio) tagging tool.
13
16
 
14
- ## Two ways to use it
17
+ ## Three ways to use it
15
18
 
16
- - 🚀 **Google Colab — no coding.** [**Open the guided notebook in Colab**](https://colab.research.google.com/github/ajcallaway/TPattern/blob/main/colab/tpattern_guided_analysis.ipynb): upload your events, follow the guided steps, download a report with a plain-English verdict for every pattern and a paste-ready Methods paragraph. See [`colab/`](colab/).
17
- - 🐍 **Python library.** `pip install` and script it — see [Quickstart](#quickstart) and [`examples/`](examples/).
19
+ ### 🚀 In your browser — Google Colab, no install, no coding
20
+
21
+ Open a guided notebook, upload your events CSV, and it walks you through four steps —
22
+ **upload → inspect → the recommended settings appear → run** — then hands back a full
23
+ report inline and as a downloadable zip: a colour-coded results table with a
24
+ plain-English verdict for every pattern (🟢 robust · 🟡 borderline · ⚪ likely chance),
25
+ the pattern dendrograms, the code to restyle them, and a paste-ready Methods paragraph.
26
+ No data of your own yet? Point it at [`data/worldcup_goals.csv`](data/worldcup_goals.csv)
27
+ (choose **ms** for the time unit).
28
+
29
+ Three notebooks (all in [`colab/`](colab/)):
30
+
31
+ - [**Guided analysis**](https://colab.research.google.com/github/ajcallaway/TPattern/blob/main/colab/tpattern_guided_analysis.ipynb) — **recommended.** Installs the latest `tpattern` and opens the guided panel. This is the one the badge above opens.
32
+ - **Guided analysis (offline)** — the package is bundled inside the notebook (a snapshot), so it runs where you can't install from GitHub. Open `colab/tpattern_guided_analysis_offline.ipynb` via **File → Upload notebook**.
33
+ - **Worked example** — the companion to the tutorial paper. Runs the load → inspect → recommend → calibrate → report walk-through on the shipped World Cup goal sequences, cell by cell, so you can follow the whole analysis and then swap in your own data (`colab/tpattern_worked_example.ipynb`).
34
+
35
+ ### 🐍 As a Python library
36
+
37
+ `pip install tpattern` and script it — see [Getting started](#getting-started-in-a-minute), [Quickstart](#quickstart) and [`examples/`](examples/).
38
+
39
+ ### 📊 From R
40
+
41
+ R users (common in psychology and behavioural science) can drive the **same
42
+ engine** via [`reticulate`](https://rstudio.github.io/reticulate/) — the algorithm
43
+ stays in Python, so results match exactly. Install with
44
+ `remotes::install_github("ajcallaway/TPattern", subdir = "r")`, then run the whole
45
+ analysis in one call: `tpattern_analysis("events.csv")`. See
46
+ [Using tpattern from R](#using-tpattern-from-r).
18
47
 
19
48
  Bring your own data (one row per event — see [`SCHEMA.md`](SCHEMA.md)) or try the
20
49
  included [`data/`](data/) example.
@@ -73,24 +102,47 @@ print(explain("fdr_q")) # what an output column means
73
102
  - **Clean figures** — dendrograms, temporal signatures, forest plots — that share one
74
103
  customisable style (`tpattern.viz.STYLE`).
75
104
 
76
- ## Why it exists
77
-
78
- T-pattern analysis finds behaviourally meaningful structure that event counts and
79
- simple transition tables miss. It is most established through Magnusson's THEME
80
- software, the reference implementation. `tpattern` is an open, complementary
81
- implementation that reproduces THEME's detection and adds a reproducible chance
82
- model and clear reporting:
83
-
84
- - **reproduces** THEME's core detection (critical intervals, NX/T baseline,
85
- completeness competition) — validated to the pattern for published datasets;
86
- - **adds a chance model** — every pattern is tested against a surrogate null, with
87
- p-values **corrected for repeated testing** (false-discovery rate and
88
- family-wise);
89
- - **handles concurrency explicitly** — events sharing a timestamp are treated as
105
+ ## The method it implements
106
+
107
+ T-pattern analysis (Magnusson, 1996, 2000, 2019) finds behaviourally meaningful
108
+ structure that event counts and simple transition tables miss: event types that
109
+ recur in the same order, separated by statistically consistent time intervals, more
110
+ often than chance predicts. `tpattern` implements the algorithm exactly as published,
111
+ in auditable code:
112
+
113
+ - **Critical-interval detection.** For an ordered pair of event types (A, B) it
114
+ searches for the time window after A within which B follows more often than B's
115
+ overall rate would predict. Significance is the **upper-tail binomial** probability
116
+ of the observed number of A→B matches, evaluated against that baseline rate (B's
117
+ occurrences spread over the observation length — the *NX/T* baseline), thresholded
118
+ at α (default 0.005). This is the critical-interval test set out in Magnusson (2000)
119
+ and the THEME manual.
120
+ - **Free search, one-to-one matching, distinct tokens.** A free search selects the
121
+ largest significant interval; occurrences are matched one-to-one by the greedy
122
+ procedure the algorithm specifies; a pattern must be built from distinct event
123
+ tokens.
124
+ - **Bottom-up hierarchy with completeness competition.** Patterns are grown level by
125
+ level, longer patterns competing with their sub-patterns so that only the most
126
+ complete survive.
127
+
128
+ On top of the published detection, `tpattern` adds what the method has historically
129
+ lacked:
130
+
131
+ - **a proper chance model** — every detected pattern is tested against a surrogate
132
+ null, with p-values **corrected for repeated testing** (Benjamini–Hochberg FDR for
133
+ screening, Holm family-wise for confirmatory claims), stratified by level;
134
+ - **explicit concurrency handling** — events sharing a timestamp are reported as
90
135
  co-occurrence rather than sequence;
91
- - **reports clearly** — tidy tables, effect sizes with confidence intervals,
136
+ - **clear reporting** — tidy tables, effect sizes with confidence intervals,
92
137
  purposeful figures, and clean **dendrograms** of the detected patterns.
93
138
 
139
+ The reference software for this method is THEME (PatternVision Ltd), with **SOF-CODER**
140
+ (Jonsson et al., 2006) as its coding front end. `tpattern` reads that ecosystem
141
+ directly — `read_sofcoder` ingests SOF-CODER/THEME (`DATANAME, T, Events`) files — so
142
+ existing THEME datasets can be re-analysed with the surrogate calibration, and
143
+ [OpenTag.Studio](https://opentag.studio) can play, for `tpattern`, the role SOF-CODER
144
+ plays for THEME.
145
+
94
146
  ## Installation
95
147
 
96
148
  ```bash
@@ -156,6 +208,32 @@ Config()).detect()` returns them directly.
156
208
  the groups (Fisher's exact test, odds ratio + 95% CI) and feeds `forest_plot` — or run
157
209
  `new_analysis(template="two_groups")` for a ready-made script.
158
210
 
211
+ ## Using tpattern from R
212
+
213
+ R users (common in psychology and behavioural science) can drive the **same
214
+ engine** from R — the algorithm stays in Python, called through
215
+ [`reticulate`](https://rstudio.github.io/reticulate/), so results match this
216
+ package and the paper exactly. The R interface lives in [`r/`](r/):
217
+
218
+ ```r
219
+ # install.packages("remotes")
220
+ remotes::install_github("ajcallaway/TPattern", subdir = "r")
221
+
222
+ library(tpattern)
223
+ install_tpattern() # once: sets up a Python environment R can use
224
+
225
+ # the whole analysis in one call — prints what the advisor recommends,
226
+ # calibrates every pattern, writes a report, returns the table:
227
+ out <- tpattern_analysis("my_events.csv", time_unit = "ms")
228
+ out$table
229
+ ```
230
+
231
+ Or step by step: `read_events()` → `advise()` (the advisor, pulled through in
232
+ full) → `calibrate_patterns()` → `results_table()` / `tpattern_report()`, with
233
+ `explain()` for any setting and `new_analysis()` to write a commented starter
234
+ script. See [`r/README.md`](r/README.md). (CRAN release to follow, then
235
+ `install.packages("tpattern")`.)
236
+
159
237
  ## The workflow
160
238
 
161
239
  ```
@@ -217,9 +295,9 @@ pip install -e .[test]
217
295
  pytest -q
218
296
  ```
219
297
 
220
- Both the synthetic ground-truth tests and the THEME reproduction tests run
221
- everywhere — the latter check the detector still reproduces THEME's published
222
- occurrence counts to the exact N.
298
+ Both the synthetic ground-truth tests and the reproduction tests run everywhere —
299
+ the latter check the detector still reproduces the occurrence counts published for
300
+ the reference datasets to the exact N.
223
301
 
224
302
  ## Citation
225
303
 
@@ -233,12 +311,23 @@ https://doi.org/10.5281/zenodo.21397543
233
311
  This DOI is permanent and always resolves to the current release. To pin a specific
234
312
  version instead, use that release's own DOI, listed on the Zenodo record.
235
313
 
236
- And the method:
314
+ And the method it implements:
237
315
 
238
316
  Magnusson, M. S. (2000). Discovering hidden time patterns in behavior: T-patterns
239
317
  and their detection. *Behavior Research Methods, Instruments, & Computers*, 32(1),
240
318
  93–110. https://doi.org/10.3758/BF03200792
241
319
 
320
+ The reference software for the method, and its coding front end:
321
+
322
+ Magnusson, M. S. (2019). T-pattern detection and analysis (TPA) with THEME: A mixed
323
+ methods approach. *Frontiers in Psychology*, 10, 2663.
324
+ https://doi.org/10.3389/fpsyg.2019.02663
325
+
326
+ Jonsson, G. K., Anguera, M. T., Blanco-Villaseñor, Á., Losada, J. L.,
327
+ Hernández-Mendo, A., Ardá, T., Camerino, O., & Castellano, J. (2006). Hidden
328
+ patterns of play interaction in soccer using SOF-CODER. *Behavior Research Methods*,
329
+ 38(3), 372–381. https://doi.org/10.3758/BF03192790
330
+
242
331
  ## License
243
332
 
244
333
  MIT — see [`LICENSE`](LICENSE).
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "tpattern"
7
- version = "0.1.13"
7
+ version = "0.1.15"
8
8
  description = "Open, auditable temporal & sequential pattern analysis (T-patterns) for sport and behaviour"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -6,7 +6,7 @@ behavior: T-patterns and their detection. Behavior Research Methods,
6
6
  Instruments, & Computers, 32(1), 93-110.
7
7
  """
8
8
 
9
- __version__ = "0.1.13" # defined before submodule imports (methods.py reads it)
9
+ __version__ = "0.1.15" # defined before submodule imports (methods.py reads it)
10
10
 
11
11
  from .io import (Observation, read_observation, read_sample, read_table,
12
12
  read_sofcoder, DataFormatError, check_reference, CHECKS)
@@ -66,6 +66,7 @@ class Calibrated:
66
66
  p_emp_strength: float = 1.0 # empirical p of the analytic strength
67
67
  fwer_keep: bool = False # Holm on p_emp
68
68
  fdr_q: float = 1.0 # Benjamini-Hochberg q on p_emp
69
+ fdr_q_by: float = 1.0 # Benjamini-Yekutieli q (dependence-robust FDR)
69
70
 
70
71
 
71
72
  @dataclass
@@ -81,6 +82,8 @@ class CalibrationResult:
81
82
  def kept(self, method: str = "fdr") -> list[Calibrated]:
82
83
  if method == "fwer":
83
84
  return [c for c in self.real if c.fwer_keep]
85
+ if method in ("fdr_by", "by"):
86
+ return [c for c in self.real if c.fdr_q_by <= self.q_target]
84
87
  return [c for c in self.real if c.fdr_q <= self.q_target]
85
88
 
86
89
 
@@ -100,6 +103,20 @@ def _benjamini_hochberg(items, get_p):
100
103
  return {id(items[i]): q[i] for i in range(m)}
101
104
 
102
105
 
106
+ def _benjamini_yekutieli(items, get_p):
107
+ """Benjamini-Yekutieli q-values: the BH q scaled by the harmonic factor
108
+ c(m) = sum_{i=1..m} 1/i, which gives false-discovery-rate control under
109
+ arbitrary dependence among the p-values (Benjamini & Yekutieli, 2001).
110
+ Surrogate p-values for patterns that share events are dependent, so this is
111
+ the dependence-robust companion to the BH q; it is more conservative."""
112
+ m = len(items)
113
+ if m == 0:
114
+ return {}
115
+ cm = sum(1.0 / i for i in range(1, m + 1))
116
+ bh = _benjamini_hochberg(items, get_p)
117
+ return {k: min(1.0, v * cm) for k, v in bh.items()}
118
+
119
+
103
120
  def _holm_keep(items, get_p, alpha):
104
121
  """Holm–Bonferroni step-down; return set of ids kept at family alpha."""
105
122
  m = len(items)
@@ -217,9 +234,11 @@ def calibrate(observations, config: Config | None = None, *,
217
234
  grp = [c for c in real if c.level == lv]
218
235
  keep = _holm_keep(grp, lambda c: c.p_emp, alpha)
219
236
  qmap = _benjamini_hochberg(grp, lambda c: c.p_emp)
237
+ bymap = _benjamini_yekutieli(grp, lambda c: c.p_emp)
220
238
  for c in grp:
221
239
  c.fwer_keep = id(c) in keep
222
240
  c.fdr_q = qmap[id(c)]
241
+ c.fdr_q_by = bymap[id(c)]
223
242
 
224
243
  return CalibrationResult(real=real, surrogate_counts=surrogate_counts,
225
244
  B=B, alpha=alpha, q_target=q_target, null=null,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tpattern
3
- Version: 0.1.13
3
+ Version: 0.1.15
4
4
  Summary: Open, auditable temporal & sequential pattern analysis (T-patterns) for sport and behaviour
5
5
  Author-email: Andrew Callaway <acallaway@bournemouth.ac.uk>
6
6
  License-Expression: MIT
@@ -36,20 +36,49 @@ Dynamic: license-file
36
36
  # tpattern — open, auditable analysis of recurring patterns in event sequences
37
37
 
38
38
  [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ajcallaway/TPattern/blob/main/colab/tpattern_guided_analysis.ipynb)
39
- [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.21397543.svg)](https://doi.org/10.5281/zenodo.21397543)
39
+ [![DOI](https://img.shields.io/badge/DOI-10.5281%2Fzenodo.21397543-1682D4.svg)](https://doi.org/10.5281/zenodo.21397543)
40
+ [![R interface](https://img.shields.io/badge/R-interface-276DC3?logo=r&logoColor=white)](#using-tpattern-from-r)
40
41
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
41
42
 
42
43
  `tpattern` detects recurring temporal patterns (T-patterns; Magnusson, 2000) in
43
44
  **any sequence of timed, labelled events** — behaviour, sport, clinical or
44
45
  physiological logs, interaction data — validates them properly against chance, and
45
46
  produces clean, publication-ready output (tables, figures, pattern dendrograms). It
46
- is a free, transparent, reproducible alternative to the THEME software, and the
47
- analysis companion to the [OpenTag.Studio](https://opentag.studio) tagging tool.
47
+ implements the published T-pattern detection algorithm (Magnusson, 2000) as free,
48
+ auditable code — the same critical-interval method the THEME software applies (see
49
+ [The method it implements](#the-method-it-implements)) — and is the analysis
50
+ companion to the [OpenTag.Studio](https://opentag.studio) tagging tool.
48
51
 
49
- ## Two ways to use it
52
+ ## Three ways to use it
50
53
 
51
- - 🚀 **Google Colab — no coding.** [**Open the guided notebook in Colab**](https://colab.research.google.com/github/ajcallaway/TPattern/blob/main/colab/tpattern_guided_analysis.ipynb): upload your events, follow the guided steps, download a report with a plain-English verdict for every pattern and a paste-ready Methods paragraph. See [`colab/`](colab/).
52
- - 🐍 **Python library.** `pip install` and script it — see [Quickstart](#quickstart) and [`examples/`](examples/).
54
+ ### 🚀 In your browser — Google Colab, no install, no coding
55
+
56
+ Open a guided notebook, upload your events CSV, and it walks you through four steps —
57
+ **upload → inspect → the recommended settings appear → run** — then hands back a full
58
+ report inline and as a downloadable zip: a colour-coded results table with a
59
+ plain-English verdict for every pattern (🟢 robust · 🟡 borderline · ⚪ likely chance),
60
+ the pattern dendrograms, the code to restyle them, and a paste-ready Methods paragraph.
61
+ No data of your own yet? Point it at [`data/worldcup_goals.csv`](data/worldcup_goals.csv)
62
+ (choose **ms** for the time unit).
63
+
64
+ Three notebooks (all in [`colab/`](colab/)):
65
+
66
+ - [**Guided analysis**](https://colab.research.google.com/github/ajcallaway/TPattern/blob/main/colab/tpattern_guided_analysis.ipynb) — **recommended.** Installs the latest `tpattern` and opens the guided panel. This is the one the badge above opens.
67
+ - **Guided analysis (offline)** — the package is bundled inside the notebook (a snapshot), so it runs where you can't install from GitHub. Open `colab/tpattern_guided_analysis_offline.ipynb` via **File → Upload notebook**.
68
+ - **Worked example** — the companion to the tutorial paper. Runs the load → inspect → recommend → calibrate → report walk-through on the shipped World Cup goal sequences, cell by cell, so you can follow the whole analysis and then swap in your own data (`colab/tpattern_worked_example.ipynb`).
69
+
70
+ ### 🐍 As a Python library
71
+
72
+ `pip install tpattern` and script it — see [Getting started](#getting-started-in-a-minute), [Quickstart](#quickstart) and [`examples/`](examples/).
73
+
74
+ ### 📊 From R
75
+
76
+ R users (common in psychology and behavioural science) can drive the **same
77
+ engine** via [`reticulate`](https://rstudio.github.io/reticulate/) — the algorithm
78
+ stays in Python, so results match exactly. Install with
79
+ `remotes::install_github("ajcallaway/TPattern", subdir = "r")`, then run the whole
80
+ analysis in one call: `tpattern_analysis("events.csv")`. See
81
+ [Using tpattern from R](#using-tpattern-from-r).
53
82
 
54
83
  Bring your own data (one row per event — see [`SCHEMA.md`](SCHEMA.md)) or try the
55
84
  included [`data/`](data/) example.
@@ -108,24 +137,47 @@ print(explain("fdr_q")) # what an output column means
108
137
  - **Clean figures** — dendrograms, temporal signatures, forest plots — that share one
109
138
  customisable style (`tpattern.viz.STYLE`).
110
139
 
111
- ## Why it exists
112
-
113
- T-pattern analysis finds behaviourally meaningful structure that event counts and
114
- simple transition tables miss. It is most established through Magnusson's THEME
115
- software, the reference implementation. `tpattern` is an open, complementary
116
- implementation that reproduces THEME's detection and adds a reproducible chance
117
- model and clear reporting:
118
-
119
- - **reproduces** THEME's core detection (critical intervals, NX/T baseline,
120
- completeness competition) — validated to the pattern for published datasets;
121
- - **adds a chance model** — every pattern is tested against a surrogate null, with
122
- p-values **corrected for repeated testing** (false-discovery rate and
123
- family-wise);
124
- - **handles concurrency explicitly** — events sharing a timestamp are treated as
140
+ ## The method it implements
141
+
142
+ T-pattern analysis (Magnusson, 1996, 2000, 2019) finds behaviourally meaningful
143
+ structure that event counts and simple transition tables miss: event types that
144
+ recur in the same order, separated by statistically consistent time intervals, more
145
+ often than chance predicts. `tpattern` implements the algorithm exactly as published,
146
+ in auditable code:
147
+
148
+ - **Critical-interval detection.** For an ordered pair of event types (A, B) it
149
+ searches for the time window after A within which B follows more often than B's
150
+ overall rate would predict. Significance is the **upper-tail binomial** probability
151
+ of the observed number of A→B matches, evaluated against that baseline rate (B's
152
+ occurrences spread over the observation length — the *NX/T* baseline), thresholded
153
+ at α (default 0.005). This is the critical-interval test set out in Magnusson (2000)
154
+ and the THEME manual.
155
+ - **Free search, one-to-one matching, distinct tokens.** A free search selects the
156
+ largest significant interval; occurrences are matched one-to-one by the greedy
157
+ procedure the algorithm specifies; a pattern must be built from distinct event
158
+ tokens.
159
+ - **Bottom-up hierarchy with completeness competition.** Patterns are grown level by
160
+ level, longer patterns competing with their sub-patterns so that only the most
161
+ complete survive.
162
+
163
+ On top of the published detection, `tpattern` adds what the method has historically
164
+ lacked:
165
+
166
+ - **a proper chance model** — every detected pattern is tested against a surrogate
167
+ null, with p-values **corrected for repeated testing** (Benjamini–Hochberg FDR for
168
+ screening, Holm family-wise for confirmatory claims), stratified by level;
169
+ - **explicit concurrency handling** — events sharing a timestamp are reported as
125
170
  co-occurrence rather than sequence;
126
- - **reports clearly** — tidy tables, effect sizes with confidence intervals,
171
+ - **clear reporting** — tidy tables, effect sizes with confidence intervals,
127
172
  purposeful figures, and clean **dendrograms** of the detected patterns.
128
173
 
174
+ The reference software for this method is THEME (PatternVision Ltd), with **SOF-CODER**
175
+ (Jonsson et al., 2006) as its coding front end. `tpattern` reads that ecosystem
176
+ directly — `read_sofcoder` ingests SOF-CODER/THEME (`DATANAME, T, Events`) files — so
177
+ existing THEME datasets can be re-analysed with the surrogate calibration, and
178
+ [OpenTag.Studio](https://opentag.studio) can play, for `tpattern`, the role SOF-CODER
179
+ plays for THEME.
180
+
129
181
  ## Installation
130
182
 
131
183
  ```bash
@@ -191,6 +243,32 @@ Config()).detect()` returns them directly.
191
243
  the groups (Fisher's exact test, odds ratio + 95% CI) and feeds `forest_plot` — or run
192
244
  `new_analysis(template="two_groups")` for a ready-made script.
193
245
 
246
+ ## Using tpattern from R
247
+
248
+ R users (common in psychology and behavioural science) can drive the **same
249
+ engine** from R — the algorithm stays in Python, called through
250
+ [`reticulate`](https://rstudio.github.io/reticulate/), so results match this
251
+ package and the paper exactly. The R interface lives in [`r/`](r/):
252
+
253
+ ```r
254
+ # install.packages("remotes")
255
+ remotes::install_github("ajcallaway/TPattern", subdir = "r")
256
+
257
+ library(tpattern)
258
+ install_tpattern() # once: sets up a Python environment R can use
259
+
260
+ # the whole analysis in one call — prints what the advisor recommends,
261
+ # calibrates every pattern, writes a report, returns the table:
262
+ out <- tpattern_analysis("my_events.csv", time_unit = "ms")
263
+ out$table
264
+ ```
265
+
266
+ Or step by step: `read_events()` → `advise()` (the advisor, pulled through in
267
+ full) → `calibrate_patterns()` → `results_table()` / `tpattern_report()`, with
268
+ `explain()` for any setting and `new_analysis()` to write a commented starter
269
+ script. See [`r/README.md`](r/README.md). (CRAN release to follow, then
270
+ `install.packages("tpattern")`.)
271
+
194
272
  ## The workflow
195
273
 
196
274
  ```
@@ -252,9 +330,9 @@ pip install -e .[test]
252
330
  pytest -q
253
331
  ```
254
332
 
255
- Both the synthetic ground-truth tests and the THEME reproduction tests run
256
- everywhere — the latter check the detector still reproduces THEME's published
257
- occurrence counts to the exact N.
333
+ Both the synthetic ground-truth tests and the reproduction tests run everywhere —
334
+ the latter check the detector still reproduces the occurrence counts published for
335
+ the reference datasets to the exact N.
258
336
 
259
337
  ## Citation
260
338
 
@@ -268,12 +346,23 @@ https://doi.org/10.5281/zenodo.21397543
268
346
  This DOI is permanent and always resolves to the current release. To pin a specific
269
347
  version instead, use that release's own DOI, listed on the Zenodo record.
270
348
 
271
- And the method:
349
+ And the method it implements:
272
350
 
273
351
  Magnusson, M. S. (2000). Discovering hidden time patterns in behavior: T-patterns
274
352
  and their detection. *Behavior Research Methods, Instruments, & Computers*, 32(1),
275
353
  93–110. https://doi.org/10.3758/BF03200792
276
354
 
355
+ The reference software for the method, and its coding front end:
356
+
357
+ Magnusson, M. S. (2019). T-pattern detection and analysis (TPA) with THEME: A mixed
358
+ methods approach. *Frontiers in Psychology*, 10, 2663.
359
+ https://doi.org/10.3389/fpsyg.2019.02663
360
+
361
+ Jonsson, G. K., Anguera, M. T., Blanco-Villaseñor, Á., Losada, J. L.,
362
+ Hernández-Mendo, A., Ardá, T., Camerino, O., & Castellano, J. (2006). Hidden
363
+ patterns of play interaction in soccer using SOF-CODER. *Behavior Research Methods*,
364
+ 38(3), 372–381. https://doi.org/10.3758/BF03192790
365
+
277
366
  ## License
278
367
 
279
368
  MIT — see [`LICENSE`](LICENSE).
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes