nisplus 0.1.0__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.
nisplus-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,222 @@
1
+ Metadata-Version: 2.4
2
+ Name: nisplus
3
+ Version: 0.1.0
4
+ Summary: Neural Information Squeezer Plus core models and utilities.
5
+ Requires-Python: >=3.11
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: numpy
8
+ Requires-Dist: torch
9
+ Provides-Extra: dev
10
+ Requires-Dist: pytest; extra == "dev"
11
+
12
+ # NIS2026 / nisplus
13
+
14
+ `nisplus` is the reusable core library extracted from the NIS2026 research codebase for neural effective information and causal emergence experiments.
15
+
16
+ It provides:
17
+
18
+ - NIS+ PyTorch models for micro-to-macro representation learning.
19
+ - Training losses for stage-1 prediction/reconstruction and stage-2 EI reweighting.
20
+ - Effective-information helper utilities.
21
+ - Synthetic SIR and Kuramoto data generators used by the research experiments.
22
+
23
+ The repository still contains experiment scripts, notebooks, result artifacts, and local data assets, but the importable package lives under `src/nisplus/`.
24
+
25
+ ## Installation
26
+
27
+ From the repository root:
28
+
29
+ ```bash
30
+ python -m pip install -e ".[dev]"
31
+ ```
32
+
33
+ Verify the install:
34
+
35
+ ```bash
36
+ python -c "import nisplus; print(nisplus.NISPlusNN)"
37
+ pytest -q
38
+ ```
39
+
40
+ Core runtime dependencies are `torch` and `numpy`. The `dev` extra installs `pytest`.
41
+
42
+ ## Quick Start
43
+
44
+ ```python
45
+ import torch
46
+
47
+ from nisplus import NISPlusNN, stage1_loss
48
+
49
+ model = NISPlusNN(x_dim=4, h_dim=2, noise_dim=2)
50
+ x_t = torch.randn(32, 4)
51
+ x_tp1 = torch.randn(32, 4)
52
+
53
+ out = model(x_t, x_tp1)
54
+ loss, metrics = stage1_loss(out, x_t, x_tp1)
55
+ loss.backward()
56
+
57
+ print(out["h_t"].shape)
58
+ print(metrics)
59
+ ```
60
+
61
+ For a runnable walkthrough, open:
62
+
63
+ ```text
64
+ exp/nisplus_toy_example.ipynb
65
+ ```
66
+
67
+ ## Core API
68
+
69
+ Top-level imports:
70
+
71
+ ```python
72
+ from nisplus import (
73
+ NISPlusNN,
74
+ NISPlusMicroDynamics,
75
+ stage1_loss,
76
+ stage2_loss,
77
+ multi_step_prediction_loss,
78
+ compute_weights,
79
+ recompute_weights,
80
+ estimate_dei,
81
+ estimate_dei_micro,
82
+ compute_causal_emergence,
83
+ )
84
+ ```
85
+
86
+ Model modules:
87
+
88
+ - `NISPlusNN`: encoder, decoder, forward latent dynamics, and backward latent dynamics.
89
+ - `NISPlusMicroDynamics`: direct micro-dynamics baseline for causal-emergence comparisons.
90
+
91
+ Useful model methods:
92
+
93
+ - `model.encode(x)`: map micro-state `x` to latent macro-state `h`.
94
+ - `model.decode(h, noise=None)`: decode latent state back to micro-state.
95
+ - `model.latent_forward(h)`: predict the next latent state.
96
+ - `model.latent_backward(h_next)`: approximate inverse latent dynamics.
97
+ - `model.multi_step_predict(x_t, n_steps, n_samples)`: roll forward in latent space and decode predictions.
98
+
99
+ ## Data Utilities
100
+
101
+ SIR toy data:
102
+
103
+ ```python
104
+ from nisplus.data.sir import SIRDataset, generate_sir_dataset, get_sir_dataloaders
105
+
106
+ x_t, x_tp1, y_t, y_tp1 = generate_sir_dataset(
107
+ n_trajectories=100,
108
+ n_steps=3,
109
+ sigma=0.03,
110
+ seed=42,
111
+ )
112
+ dataset = SIRDataset(x_t, x_tp1, y_t, y_tp1)
113
+ ```
114
+
115
+ Kuramoto toy data:
116
+
117
+ ```python
118
+ from nisplus.data.kuramoto import generate_kuramoto_dataset
119
+
120
+ x_t, x_tp1, y_t, y_tp1, meta = generate_kuramoto_dataset(
121
+ n_trajectories=100,
122
+ n_steps=4,
123
+ group_sizes=(5, 5),
124
+ seed=42,
125
+ )
126
+ ```
127
+
128
+ YRD air-quality data and loaders remain repository-local under `data/` and are not part of the first-stage `nisplus` package.
129
+
130
+ ## Training Pattern
131
+
132
+ Stage 1 trains prediction and reconstruction:
133
+
134
+ ```python
135
+ optimizer.zero_grad()
136
+ out = model(x_t, x_tp1)
137
+ loss, metrics = stage1_loss(out, x_t, x_tp1)
138
+ loss.backward()
139
+ optimizer.step()
140
+ ```
141
+
142
+ Stage 2 adds EI-style density-ratio weighting and backward latent consistency:
143
+
144
+ ```python
145
+ from nisplus import compute_weights, stage2_loss
146
+
147
+ with torch.no_grad():
148
+ h_all = model.encode(all_x_t)
149
+ weights = compute_weights(h_all, bandwidth=0.05, max_weight=10.0)
150
+
151
+ out = model(x_batch, x_next_batch)
152
+ loss, metrics = stage2_loss(out, x_batch, x_next_batch, weights, indices)
153
+ ```
154
+
155
+ `indices` should index each batch sample into the full training-set weight vector. The bundled Dataset classes return `idx` for this purpose.
156
+
157
+ ## Effective Information Helpers
158
+
159
+ Use `compute_weights` for latent density-ratio weights and `estimate_dei` / `estimate_dei_micro` for dimension-averaged EI estimates:
160
+
161
+ ```python
162
+ from nisplus import compute_causal_emergence, estimate_dei, estimate_dei_micro
163
+
164
+ dei_macro, macro_info = estimate_dei(model, h_t, h_tp1, h_pred, weights)
165
+ dei_micro, micro_info = estimate_dei_micro(micro_model, x_t, x_tp1, x_pred)
166
+ delta_j = compute_causal_emergence(dei_macro, dei_micro)
167
+ ```
168
+
169
+ EI estimation uses Jacobian calculations and can be slower than ordinary training loss evaluation.
170
+
171
+ ## Repository Layout
172
+
173
+ - `src/nisplus/`: importable NIS+ core library.
174
+ - `src/nisplus/data/`: bundled synthetic SIR and Kuramoto generators.
175
+ - `tests/`: smoke tests for the packaged API.
176
+ - `exp/nisplus_toy_example.ipynb`: minimal tutorial notebook.
177
+ - `exp/sir/`: SIR experiment scripts, notebooks, state, and runs.
178
+ - `exp/kuramoto/`: Kuramoto experiment scripts, notebooks, state, and runs.
179
+ - `exp/yrd_air/`: YRD air-quality experiment scripts and state.
180
+ - `data/`: local data assets and non-core data loaders.
181
+ - `results/`: generated result artifacts.
182
+ - `docs/`: paper drafts, logs, and references.
183
+
184
+ ## Experiment Entrypoints
185
+
186
+ Install the package in editable mode before running experiment scripts:
187
+
188
+ ```bash
189
+ python -m pip install -e ".[dev]"
190
+ ```
191
+
192
+ Main scripts:
193
+
194
+ ```bash
195
+ python exp/sir/train.py
196
+ python exp/sir/ood_search.py
197
+ python exp/kuramoto/train.py
198
+ python exp/kuramoto/scale_search.py
199
+ ```
200
+
201
+ Small SIR smoke run:
202
+
203
+ ```bash
204
+ python exp/sir/train.py \
205
+ --n_trajectories 8 \
206
+ --n_steps 2 \
207
+ --total_epochs 1 \
208
+ --stage2_start 1 \
209
+ --batch_size 4 \
210
+ --eval_interval 1 \
211
+ --ei_eval_interval 999 \
212
+ --output_dir /tmp/nisplus_sir_smoke \
213
+ --device cpu
214
+ ```
215
+
216
+ ## Testing
217
+
218
+ ```bash
219
+ pytest -q
220
+ ```
221
+
222
+ The smoke tests cover package imports, model forward shapes, loss backward passes, toy data generation, and EI helper execution.
@@ -0,0 +1,211 @@
1
+ # NIS2026 / nisplus
2
+
3
+ `nisplus` is the reusable core library extracted from the NIS2026 research codebase for neural effective information and causal emergence experiments.
4
+
5
+ It provides:
6
+
7
+ - NIS+ PyTorch models for micro-to-macro representation learning.
8
+ - Training losses for stage-1 prediction/reconstruction and stage-2 EI reweighting.
9
+ - Effective-information helper utilities.
10
+ - Synthetic SIR and Kuramoto data generators used by the research experiments.
11
+
12
+ The repository still contains experiment scripts, notebooks, result artifacts, and local data assets, but the importable package lives under `src/nisplus/`.
13
+
14
+ ## Installation
15
+
16
+ From the repository root:
17
+
18
+ ```bash
19
+ python -m pip install -e ".[dev]"
20
+ ```
21
+
22
+ Verify the install:
23
+
24
+ ```bash
25
+ python -c "import nisplus; print(nisplus.NISPlusNN)"
26
+ pytest -q
27
+ ```
28
+
29
+ Core runtime dependencies are `torch` and `numpy`. The `dev` extra installs `pytest`.
30
+
31
+ ## Quick Start
32
+
33
+ ```python
34
+ import torch
35
+
36
+ from nisplus import NISPlusNN, stage1_loss
37
+
38
+ model = NISPlusNN(x_dim=4, h_dim=2, noise_dim=2)
39
+ x_t = torch.randn(32, 4)
40
+ x_tp1 = torch.randn(32, 4)
41
+
42
+ out = model(x_t, x_tp1)
43
+ loss, metrics = stage1_loss(out, x_t, x_tp1)
44
+ loss.backward()
45
+
46
+ print(out["h_t"].shape)
47
+ print(metrics)
48
+ ```
49
+
50
+ For a runnable walkthrough, open:
51
+
52
+ ```text
53
+ exp/nisplus_toy_example.ipynb
54
+ ```
55
+
56
+ ## Core API
57
+
58
+ Top-level imports:
59
+
60
+ ```python
61
+ from nisplus import (
62
+ NISPlusNN,
63
+ NISPlusMicroDynamics,
64
+ stage1_loss,
65
+ stage2_loss,
66
+ multi_step_prediction_loss,
67
+ compute_weights,
68
+ recompute_weights,
69
+ estimate_dei,
70
+ estimate_dei_micro,
71
+ compute_causal_emergence,
72
+ )
73
+ ```
74
+
75
+ Model modules:
76
+
77
+ - `NISPlusNN`: encoder, decoder, forward latent dynamics, and backward latent dynamics.
78
+ - `NISPlusMicroDynamics`: direct micro-dynamics baseline for causal-emergence comparisons.
79
+
80
+ Useful model methods:
81
+
82
+ - `model.encode(x)`: map micro-state `x` to latent macro-state `h`.
83
+ - `model.decode(h, noise=None)`: decode latent state back to micro-state.
84
+ - `model.latent_forward(h)`: predict the next latent state.
85
+ - `model.latent_backward(h_next)`: approximate inverse latent dynamics.
86
+ - `model.multi_step_predict(x_t, n_steps, n_samples)`: roll forward in latent space and decode predictions.
87
+
88
+ ## Data Utilities
89
+
90
+ SIR toy data:
91
+
92
+ ```python
93
+ from nisplus.data.sir import SIRDataset, generate_sir_dataset, get_sir_dataloaders
94
+
95
+ x_t, x_tp1, y_t, y_tp1 = generate_sir_dataset(
96
+ n_trajectories=100,
97
+ n_steps=3,
98
+ sigma=0.03,
99
+ seed=42,
100
+ )
101
+ dataset = SIRDataset(x_t, x_tp1, y_t, y_tp1)
102
+ ```
103
+
104
+ Kuramoto toy data:
105
+
106
+ ```python
107
+ from nisplus.data.kuramoto import generate_kuramoto_dataset
108
+
109
+ x_t, x_tp1, y_t, y_tp1, meta = generate_kuramoto_dataset(
110
+ n_trajectories=100,
111
+ n_steps=4,
112
+ group_sizes=(5, 5),
113
+ seed=42,
114
+ )
115
+ ```
116
+
117
+ YRD air-quality data and loaders remain repository-local under `data/` and are not part of the first-stage `nisplus` package.
118
+
119
+ ## Training Pattern
120
+
121
+ Stage 1 trains prediction and reconstruction:
122
+
123
+ ```python
124
+ optimizer.zero_grad()
125
+ out = model(x_t, x_tp1)
126
+ loss, metrics = stage1_loss(out, x_t, x_tp1)
127
+ loss.backward()
128
+ optimizer.step()
129
+ ```
130
+
131
+ Stage 2 adds EI-style density-ratio weighting and backward latent consistency:
132
+
133
+ ```python
134
+ from nisplus import compute_weights, stage2_loss
135
+
136
+ with torch.no_grad():
137
+ h_all = model.encode(all_x_t)
138
+ weights = compute_weights(h_all, bandwidth=0.05, max_weight=10.0)
139
+
140
+ out = model(x_batch, x_next_batch)
141
+ loss, metrics = stage2_loss(out, x_batch, x_next_batch, weights, indices)
142
+ ```
143
+
144
+ `indices` should index each batch sample into the full training-set weight vector. The bundled Dataset classes return `idx` for this purpose.
145
+
146
+ ## Effective Information Helpers
147
+
148
+ Use `compute_weights` for latent density-ratio weights and `estimate_dei` / `estimate_dei_micro` for dimension-averaged EI estimates:
149
+
150
+ ```python
151
+ from nisplus import compute_causal_emergence, estimate_dei, estimate_dei_micro
152
+
153
+ dei_macro, macro_info = estimate_dei(model, h_t, h_tp1, h_pred, weights)
154
+ dei_micro, micro_info = estimate_dei_micro(micro_model, x_t, x_tp1, x_pred)
155
+ delta_j = compute_causal_emergence(dei_macro, dei_micro)
156
+ ```
157
+
158
+ EI estimation uses Jacobian calculations and can be slower than ordinary training loss evaluation.
159
+
160
+ ## Repository Layout
161
+
162
+ - `src/nisplus/`: importable NIS+ core library.
163
+ - `src/nisplus/data/`: bundled synthetic SIR and Kuramoto generators.
164
+ - `tests/`: smoke tests for the packaged API.
165
+ - `exp/nisplus_toy_example.ipynb`: minimal tutorial notebook.
166
+ - `exp/sir/`: SIR experiment scripts, notebooks, state, and runs.
167
+ - `exp/kuramoto/`: Kuramoto experiment scripts, notebooks, state, and runs.
168
+ - `exp/yrd_air/`: YRD air-quality experiment scripts and state.
169
+ - `data/`: local data assets and non-core data loaders.
170
+ - `results/`: generated result artifacts.
171
+ - `docs/`: paper drafts, logs, and references.
172
+
173
+ ## Experiment Entrypoints
174
+
175
+ Install the package in editable mode before running experiment scripts:
176
+
177
+ ```bash
178
+ python -m pip install -e ".[dev]"
179
+ ```
180
+
181
+ Main scripts:
182
+
183
+ ```bash
184
+ python exp/sir/train.py
185
+ python exp/sir/ood_search.py
186
+ python exp/kuramoto/train.py
187
+ python exp/kuramoto/scale_search.py
188
+ ```
189
+
190
+ Small SIR smoke run:
191
+
192
+ ```bash
193
+ python exp/sir/train.py \
194
+ --n_trajectories 8 \
195
+ --n_steps 2 \
196
+ --total_epochs 1 \
197
+ --stage2_start 1 \
198
+ --batch_size 4 \
199
+ --eval_interval 1 \
200
+ --ei_eval_interval 999 \
201
+ --output_dir /tmp/nisplus_sir_smoke \
202
+ --device cpu
203
+ ```
204
+
205
+ ## Testing
206
+
207
+ ```bash
208
+ pytest -q
209
+ ```
210
+
211
+ The smoke tests cover package imports, model forward shapes, loss backward passes, toy data generation, and EI helper execution.
@@ -0,0 +1,25 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "nisplus"
7
+ version = "0.1.0"
8
+ description = "Neural Information Squeezer Plus core models and utilities."
9
+ readme = "README.md"
10
+ requires-python = ">=3.11"
11
+ dependencies = [
12
+ "numpy",
13
+ "torch",
14
+ ]
15
+
16
+ [project.optional-dependencies]
17
+ dev = [
18
+ "pytest",
19
+ ]
20
+
21
+ [tool.setuptools.packages.find]
22
+ where = ["src"]
23
+
24
+ [tool.pytest.ini_options]
25
+ testpaths = ["tests"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,24 @@
1
+ """Core public API for NIS+."""
2
+
3
+ from .ei import (
4
+ compute_causal_emergence,
5
+ compute_weights,
6
+ estimate_dei,
7
+ estimate_dei_micro,
8
+ recompute_weights,
9
+ )
10
+ from .losses import multi_step_prediction_loss, stage1_loss, stage2_loss
11
+ from .models import NISPlusMicroDynamics, NISPlusNN
12
+
13
+ __all__ = [
14
+ "NISPlusNN",
15
+ "NISPlusMicroDynamics",
16
+ "stage1_loss",
17
+ "stage2_loss",
18
+ "multi_step_prediction_loss",
19
+ "compute_weights",
20
+ "recompute_weights",
21
+ "estimate_dei",
22
+ "estimate_dei_micro",
23
+ "compute_causal_emergence",
24
+ ]
@@ -0,0 +1,41 @@
1
+ """Synthetic data utilities shipped with NIS+."""
2
+
3
+ from .kuramoto import (
4
+ KuramotoDataset,
5
+ angles_to_micro,
6
+ build_two_community_coupling,
7
+ community_order_parameters,
8
+ generate_kuramoto_dataset,
9
+ generate_kuramoto_dataset_multistep,
10
+ get_kuramoto_dataloaders,
11
+ global_order_parameter,
12
+ micro_to_community_macro,
13
+ simulate_kuramoto_trajectory,
14
+ )
15
+ from .sir import (
16
+ SIRDataset,
17
+ generate_sir_dataset,
18
+ get_sir_dataloaders,
19
+ macro_to_micro,
20
+ simulate_sir_trajectory,
21
+ sir_derivatives,
22
+ )
23
+
24
+ __all__ = [
25
+ "SIRDataset",
26
+ "sir_derivatives",
27
+ "simulate_sir_trajectory",
28
+ "macro_to_micro",
29
+ "generate_sir_dataset",
30
+ "get_sir_dataloaders",
31
+ "KuramotoDataset",
32
+ "angles_to_micro",
33
+ "build_two_community_coupling",
34
+ "community_order_parameters",
35
+ "global_order_parameter",
36
+ "micro_to_community_macro",
37
+ "simulate_kuramoto_trajectory",
38
+ "generate_kuramoto_dataset",
39
+ "generate_kuramoto_dataset_multistep",
40
+ "get_kuramoto_dataloaders",
41
+ ]