Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,39 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- `tc.simulate_hierarchy(..., factors="shared")`: one set of module series
`f` for every subject (the noise `z` stays private), so nodes of one module
are correlated across subjects, by `e_i e_j sqrt(g_a g_b)` for nodes `i`,
`j` of subjects `a`, `b`. `truth["factors"]` holds each subject's module
series. The default (`"private"`) output is unchanged.
- Subject weights per feature: `tc.wisfc(subject_weights=W)` accepts `W` of
shape (subjects, subjects, features), the weight of each subject in each
subject's reference at each feature; in `tc.timecorr` and `tc.features` as
`cfun=functools.partial(tc.wisfc, subject_weights=W)`, also with
`causal=True` and in streamed steps. A feature of a subject where no
subject has weight has no reference: its entries with the other features
are the correlations of the subject's series there with the references of
those features, its diagonal entry and its entries with other such
features are 0, and one `UserWarning` counts the (subject, feature) pairs.
Matrix weights are unchanged.
- `tc.distinct_subject_weights(labels)`: the weights per feature that leave
out of each subject's reference the subjects holding one of its own series
(labels of shape (subjects, features)), for subjects that can hold the same
series; with two or more subjects and no repeated label the result is
`tc.isfc`. New section in the
"Several subjects" tutorial.

### Fixed

- Docstrings of `tc.wcorr`, `tc.isfc`, `tc.wisfc` and `tc.autofc` still said
that causal rows are unchanged "to 1e-9" when later samples are replaced or
removed; since 0.4.0 they are unchanged bit for bit (removal: with the
built-in kernel functions). No change in behavior.

## [0.4.0] - 2026-11-24

### Added
Expand Down
3 changes: 2 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ python setup.py sdist bdist_wheel
- **`timecorr/helpers.py`**:
- Kernel functions (`gaussian_weights`, `laplace_weights`, `eye_weights`, `mexican_hat_weights`, `t_weights`, `uniform_weights`, `boxcar_weights`) and `one_sided`
- The estimator `wcorr` and correlation functions (`isfc`, `wisfc`, `autofc`), each with keyword-only `causal=False, min_samples=None`; the causal branch is `_causal_setup` / `_causal_block` (see "Causal mode" under Key Concepts)
- Subject weights of `wisfc`: a matrix (S, S), or per feature (S, S, K) (`_subject_weight_matrix` checks the shape; `_subject_reference` gives a subject's reference and the mask of its features without reference, `_node_weighted_mean` the mean per feature, a column of zeros where no subject has weight; `_no_reference_entries` sets the entries of such a feature before the symmetrization, in `wisfc` and in the streamed blocks of `summaries._unit_row_blocks`: the defined orientation `C[k, i]` for both, 0 between two such features; `_no_reference_message` is the warning, issued by `wisfc` and by the streamed step in `higher_order`); `distinct_subject_weights(labels)` (exported) builds the per-feature weights that leave out subjects holding one of a subject's own series (`tests/test_node_subject_weights.py`)
- Frozen hypertools models of causal mode: `_model_reduce(..., return_model=True)` gives a `_FittedModel` (the model, its padding and its frozen constant columns); `_COMPONENT_MODELS` names the models with `components_`; `_FitUnusable` is raised for a fit a refit cannot use
- Combine functions (`null_combine`, `corrmean_combine`, `mean_combine`, `tstat_combine`)
- `reduce` (dimensionality reduction), `format_data` (input formatting via pydata-wrangler), `mat2vec`/`vec2mat`, `smooth`, `plot_weights`
Expand All @@ -66,7 +67,7 @@ python setup.py sdist bdist_wheel
- **`timecorr/datasets.py`** (used as `tc.datasets`; its functions are not top-level names): `fetch(url, path, sha256=None, member=None)` (cached download through uniquely named `.part` files next to the target and an atomic replace, so concurrent calls for one target are safe; a relative path is under the cache root `cache_dir()`: `$TIMECORR_DATA`, default `~/timecorr_data`; a cached file is checked against `sha256` on every call), `story_listening(n_nodes=100, return_nodes=False)` (HTFA node weights of the story-listening data, `pieman/pieman_posterior_K700.mat`; intact participants, nodes varying in every participant, farthest-point selection; `n_nodes` outside 1..679, the nodes that vary in every participant, raises `ValueError`; `return_nodes=True` adds a dict with `centers`, `widths`, `index`, `varying`, `total`) and `industry_returns(start, end)` (log returns of the 49 Industry Portfolios, `french/49_Industry_Portfolios_daily_CSV.zip`; `attrs` has the file's first line, `"header"`, and the count of missing returns, `"missing"`). `tests/test_datasets.py` holds the loading code the tutorial notebooks had at commit f9db4af and requires equal arrays. Only runtime dependencies.
- **`timecorr/video.py`**: `render_video(draw, n_frames, path, fps=30, crf=23, hold=0, n_jobs=None, chunk_size=25, retries=0)` (`draw(frames, paths)` is called once per run of at most `chunk_size` consecutive frames in joblib worker processes, 3 or the usable cores by default; PNG frames in a temporary folder; `hold` appends copies of the last frame; with `retries`, workers are stopped after an error and new ones draw the frames whose files do not exist, again in runs of consecutive frames; the video is encoded under a temporary name next to the target and replaces it only on success; encoded with imageio-ffmpeg's ffmpeg, `-c:v libx264 -preset slow -crf <crf> -pix_fmt yuv420p -movflags +faststart`, byte for byte what the notebooks' own command gave: `tests/test_video.py`), `show_video(path, width=960)` (an HTML `<video>` tag with a relative, percent-encoded and escaped link inside a Sphinx source tree and for a URL; `IPython.display.Video(embed=True)` elsewhere) and `media_dir(name="tutorial_media")` (`../_static/tutorial_media` when the parent of the working directory holds `conf.py` and `_static`, otherwise `name`). All three are exported from `timecorr`.
- **`timecorr/tutorial.mplstyle`**: the plot style of the tutorials (`plt.style.use('timecorr.tutorial')`; the seven rcParams the notebooks set, the Okabe-Ito color cycle included; `package_data` in `setup.py`; needs matplotlib >= 3.7; `tests/test_tutorial_style.py`). Importing timecorr does not apply it.
- **`timecorr/simulate.py`**: simulated data with known dynamic correlations (`simulate_data`, `simulate_multi_order`) and helpers for structure embedded at a chosen order (`min_features`, `carrier_reducer`, `carrier_columns`, `nested_template`), and `simulate_nested` (nodes in modules in supergroups; returns `(data, truth)` with the order-2 signal `truth["h"]`), and `simulate_hierarchy` (nested groups of any depth `len(taus)`, one signal per group per level; returns `(data, truth)` with `truth["groups"]`, `truth["signals"]`, `truth["top"]` and the node signs `truth["signs"]`; `signs="private"` (default) or `"shared"`).
- **`timecorr/simulate.py`**: simulated data with known dynamic correlations (`simulate_data`, `simulate_multi_order`) and helpers for structure embedded at a chosen order (`min_features`, `carrier_reducer`, `carrier_columns`, `nested_template`), and `simulate_nested` (nodes in modules in supergroups; returns `(data, truth)` with the order-2 signal `truth["h"]`), and `simulate_hierarchy` (nested groups of any depth `len(taus)`, one signal per group per level; returns `(data, truth)` with `truth["groups"]`, `truth["signals"]`, `truth["top"]` and the node signs `truth["signs"]`; `signs="private"` (default) or `"shared"`; `factors="private"` (default) or `"shared"`: the module series `f` drawn per subject or once for all, returned in `truth["factors"]`).

### Key Concepts

Expand Down
31 changes: 31 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Dynamic correlations
isfc
wisfc
autofc
distinct_subject_weights

``timecorr`` builds the kernel, restricts it (``include_timepoints``,
``include_zero``, ``exclude_timepoints``, and ``events``: see Events below), applies the correlation function
Expand All @@ -45,6 +46,30 @@ results differ from version 0.2.0 and from Eq. 4 of Owen, Chang & Manning
isfc = tc.timecorr(subjects, cfun=tc.isfc) # list of 4 (100, 15) arrays
group = tc.timecorr(subjects, cfun=tc.isfc, combine=tc.corrmean_combine) # (100, 15)

``wisfc`` takes the weight of every subject in every subject's reference: a
subjects-by-subjects matrix or, with one weight per feature as well, an array
of shape (subjects, subjects, features). ``distinct_subject_weights`` builds
such an array from labels of what each subject holds at each feature, for
subjects that can hold the same series (for example subjects drawn with
replacement from a pool): a subject is left out of another's reference at
the features where it holds one of that subject's series, so no series is
correlated with a mean that contains it. In ``timecorr`` and ``features``
the weights are bound with ``functools.partial`` (also with ``causal=True``);
they are for data with as many features as the labels have columns, so in
``features`` they fit the orders whose input has that width.

.. code-block:: python

import functools

labels = [["a", "b", "c", "d", "e"], # what each of the 4 subjects holds
["a", "f", "g", "h", "i"], # at each of the 5 features
["j", "k", "l", "m", "n"],
["o", "b", "p", "q", "r"]]
W = tc.distinct_subject_weights(labels) # (4, 4, 5)
cfun = functools.partial(tc.wisfc, subject_weights=W)
distinct = tc.timecorr(subjects, cfun=cfun) # list of 4 (100, 15) arrays

Higher-order features
---------------------

Expand Down Expand Up @@ -581,6 +606,12 @@ Simulation
return_corrs=True, set_random_seed=0)
# truth["corrs"]: (200, 10) correlations among the 4 block products

``simulate_hierarchy`` draws each subject's module series (``f``) and noise
separately by default, so its subjects share coupling signals and no time
series; with ``factors="shared"`` every subject has the same module series
(``truth["factors"]``), and nodes of one module are correlated across
subjects.

Decoders
--------

Expand Down
6 changes: 6 additions & 0 deletions docs/generated/timecorr.distinct_subject_weights.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
timecorr.distinct\_subject\_weights
===================================

.. currentmodule:: timecorr

.. autofunction:: distinct_subject_weights
141 changes: 102 additions & 39 deletions docs/tutorial/subjects.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
"id": "subjects-02",
"metadata": {
"execution": {
"iopub.execute_input": "2026-11-22T05:08:39.553395Z",
"iopub.status.busy": "2026-11-22T05:08:39.553239Z",
"iopub.status.idle": "2026-11-22T05:08:39.558749Z",
"shell.execute_reply": "2026-11-22T05:08:39.557950Z"
"iopub.execute_input": "2026-11-24T15:06:07.576027Z",
"iopub.status.busy": "2026-11-24T15:06:07.575890Z",
"iopub.status.idle": "2026-11-24T15:06:07.582879Z",
"shell.execute_reply": "2026-11-24T15:06:07.582024Z"
}
},
"outputs": [],
Expand All @@ -47,10 +47,10 @@
"id": "subjects-03",
"metadata": {
"execution": {
"iopub.execute_input": "2026-11-22T05:08:39.560530Z",
"iopub.status.busy": "2026-11-22T05:08:39.560401Z",
"iopub.status.idle": "2026-11-22T05:08:43.210236Z",
"shell.execute_reply": "2026-11-22T05:08:43.209162Z"
"iopub.execute_input": "2026-11-24T15:06:07.596179Z",
"iopub.status.busy": "2026-11-24T15:06:07.595066Z",
"iopub.status.idle": "2026-11-24T15:06:20.191315Z",
"shell.execute_reply": "2026-11-24T15:06:20.190651Z"
}
},
"outputs": [],
Expand Down Expand Up @@ -82,10 +82,10 @@
"id": "subjects-05",
"metadata": {
"execution": {
"iopub.execute_input": "2026-11-22T05:08:43.213191Z",
"iopub.status.busy": "2026-11-22T05:08:43.212996Z",
"iopub.status.idle": "2026-11-22T05:08:43.236791Z",
"shell.execute_reply": "2026-11-22T05:08:43.236125Z"
"iopub.execute_input": "2026-11-24T15:06:20.207981Z",
"iopub.status.busy": "2026-11-24T15:06:20.206284Z",
"iopub.status.idle": "2026-11-24T15:06:20.275684Z",
"shell.execute_reply": "2026-11-24T15:06:20.266685Z"
}
},
"outputs": [
Expand Down Expand Up @@ -129,10 +129,10 @@
"id": "subjects-07",
"metadata": {
"execution": {
"iopub.execute_input": "2026-11-22T05:08:43.240070Z",
"iopub.status.busy": "2026-11-22T05:08:43.239930Z",
"iopub.status.idle": "2026-11-22T05:08:43.246941Z",
"shell.execute_reply": "2026-11-22T05:08:43.245907Z"
"iopub.execute_input": "2026-11-24T15:06:20.284270Z",
"iopub.status.busy": "2026-11-24T15:06:20.283861Z",
"iopub.status.idle": "2026-11-24T15:06:20.316546Z",
"shell.execute_reply": "2026-11-24T15:06:20.309719Z"
}
},
"outputs": [
Expand Down Expand Up @@ -173,10 +173,10 @@
"id": "subjects-09",
"metadata": {
"execution": {
"iopub.execute_input": "2026-11-22T05:08:43.249459Z",
"iopub.status.busy": "2026-11-22T05:08:43.249333Z",
"iopub.status.idle": "2026-11-22T05:08:43.384583Z",
"shell.execute_reply": "2026-11-22T05:08:43.383869Z"
"iopub.execute_input": "2026-11-24T15:06:20.332675Z",
"iopub.status.busy": "2026-11-24T15:06:20.332103Z",
"iopub.status.idle": "2026-11-24T15:06:20.549934Z",
"shell.execute_reply": "2026-11-24T15:06:20.533243Z"
}
},
"outputs": [
Expand All @@ -186,9 +186,21 @@
"text": [
"independent activity autofc score 0.97, mean |r| 0.25\n",
"independent activity isfc score 0.05, mean |r| 0.05\n",
"independent activity wisfc score 0.04, mean |r| 0.05\n",
"independent activity wisfc score 0.04, mean |r| 0.05\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"shared activity autofc score 0.86, mean |r| 0.21\n",
"shared activity isfc score 0.86, mean |r| 0.23\n",
"shared activity isfc score 0.86, mean |r| 0.23\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"shared activity wisfc score 0.86, mean |r| 0.23\n"
]
}
Expand Down Expand Up @@ -220,10 +232,10 @@
"id": "subjects-11",
"metadata": {
"execution": {
"iopub.execute_input": "2026-11-22T05:08:43.387504Z",
"iopub.status.busy": "2026-11-22T05:08:43.387377Z",
"iopub.status.idle": "2026-11-22T05:08:43.506625Z",
"shell.execute_reply": "2026-11-22T05:08:43.505677Z"
"iopub.execute_input": "2026-11-24T15:06:20.560828Z",
"iopub.status.busy": "2026-11-24T15:06:20.555921Z",
"iopub.status.idle": "2026-11-24T15:06:20.734779Z",
"shell.execute_reply": "2026-11-24T15:06:20.734011Z"
}
},
"outputs": [
Expand Down Expand Up @@ -273,10 +285,10 @@
"id": "subjects-13",
"metadata": {
"execution": {
"iopub.execute_input": "2026-11-22T05:08:43.508359Z",
"iopub.status.busy": "2026-11-22T05:08:43.508270Z",
"iopub.status.idle": "2026-11-22T05:08:43.574380Z",
"shell.execute_reply": "2026-11-22T05:08:43.573578Z"
"iopub.execute_input": "2026-11-24T15:06:20.743960Z",
"iopub.status.busy": "2026-11-24T15:06:20.741852Z",
"iopub.status.idle": "2026-11-24T15:06:20.881889Z",
"shell.execute_reply": "2026-11-24T15:06:20.864162Z"
}
},
"outputs": [
Expand Down Expand Up @@ -324,10 +336,10 @@
"id": "subjects-15",
"metadata": {
"execution": {
"iopub.execute_input": "2026-11-22T05:08:43.576242Z",
"iopub.status.busy": "2026-11-22T05:08:43.576113Z",
"iopub.status.idle": "2026-11-22T05:08:43.668613Z",
"shell.execute_reply": "2026-11-22T05:08:43.667582Z"
"iopub.execute_input": "2026-11-24T15:06:20.897993Z",
"iopub.status.busy": "2026-11-24T15:06:20.897888Z",
"iopub.status.idle": "2026-11-24T15:06:21.027775Z",
"shell.execute_reply": "2026-11-24T15:06:21.025771Z"
}
},
"outputs": [
Expand Down Expand Up @@ -371,6 +383,57 @@
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "subjects-distinct-md",
"metadata": {},
"source": [
"## 4. Subjects that hold the same series\n",
"\n",
"When subjects are drawn from a pool, two of them can hold the same series, and `tc.isfc` then correlates that series with a mean that contains it. `tc.distinct_subject_weights` takes a label for what each subject holds at each feature and returns weights for `tc.wisfc` that leave those subjects out, feature by feature."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "subjects-distinct-code",
"metadata": {
"execution": {
"iopub.execute_input": "2026-11-24T15:06:21.038679Z",
"iopub.status.busy": "2026-11-24T15:06:21.038586Z",
"iopub.status.idle": "2026-11-24T15:06:21.118106Z",
"shell.execute_reply": "2026-11-24T15:06:21.092619Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"weights: (4, 4, 8) ; subjects in the reference of subject 0 at feature 0: [2 3]\n",
"mean correlation of the repeated series with its reference: isfc 0.59, distinct subjects 0.00\n",
"subject 2 holds neither copy, largest change: 2.220446049250313e-16\n"
]
}
],
"source": [
"import functools\n",
"\n",
"four = [x.copy() for x in groups['independent activity'][0][:4]]\n",
"four[1][:, 0] = four[0][:, 0] # subjects 0 and 1 hold the same series at feature 0\n",
"labels = np.arange(4 * K).reshape(4, K) # one label per subject and feature\n",
"labels[1, 0] = labels[0, 0]\n",
"\n",
"W = tc.distinct_subject_weights(labels)\n",
"print('weights:', W.shape, '; subjects in the reference of subject 0 at feature 0:', np.flatnonzero(W[0, :, 0]))\n",
"plain = tc.timecorr(four, cfun=tc.isfc, **kernel)\n",
"distinct = tc.timecorr(four, cfun=functools.partial(tc.wisfc, subject_weights=W), **kernel)\n",
"# column 0 of the vectorized output: feature 0 of subject 0 with its reference at feature 0\n",
"print(f'mean correlation of the repeated series with its reference: isfc {plain[0][:, 0].mean():.2f}, '\n",
" f'distinct subjects {distinct[0][:, 0].mean():.2f}')\n",
"print('subject 2 holds neither copy, largest change:', np.abs(plain[2] - distinct[2]).max())"
]
},
{
"cell_type": "markdown",
"id": "subjects-16",
Expand All @@ -389,14 +452,14 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"id": "b620860f",
"metadata": {
"execution": {
"iopub.execute_input": "2026-11-22T05:08:43.671264Z",
"iopub.status.busy": "2026-11-22T05:08:43.671106Z",
"iopub.status.idle": "2026-11-22T05:08:43.699574Z",
"shell.execute_reply": "2026-11-22T05:08:43.697903Z"
"iopub.execute_input": "2026-11-24T15:06:21.144681Z",
"iopub.status.busy": "2026-11-24T15:06:21.136538Z",
"iopub.status.idle": "2026-11-24T15:06:21.230993Z",
"shell.execute_reply": "2026-11-24T15:06:21.220145Z"
},
"tags": [
"nbsphinx-thumbnail"
Expand Down
Loading
Loading