count-ts-window-selector 0.2.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.
@@ -0,0 +1,231 @@
1
+ Metadata-Version: 2.3
2
+ Name: count-ts-window-selector
3
+ Version: 0.2.0
4
+ Summary: Select Poisson-compatible time windows for higher-event-rate count time series.
5
+ Author: Rajiv Sambasivan
6
+ Author-email: Rajiv Sambasivan <rajiv.sambasivan@r2-ds.com>
7
+ Requires-Dist: scipy>=1.11.0
8
+ Requires-Python: >=3.10
9
+ Description-Content-Type: text/markdown
10
+
11
+ # count-ts-window-selector
12
+
13
+ A Python package for selecting Poisson-compatible observation windows in higher-event-rate count time series.
14
+
15
+ ## Overview
16
+
17
+ This package implements the workflow described in the project proposal:
18
+
19
+ 1. Validate series density and zero inflation before any Fano analysis.
20
+ 2. Filter candidate window sizes to those that retain at least 30 non-overlapping windows.
21
+ 3. Aggregate counts over each candidate window.
22
+ 4. Compute the Fano factor and chi-square-based confidence interval around 1.
23
+ 5. Return only windows whose Fano factor remains inside the configured confidence band.
24
+
25
+ The package intentionally targets higher-event-rate count series and explicitly rejects sparse, zero-heavy, or hurdle-style data before Fano-factor evaluation.
26
+
27
+ ## Installation
28
+
29
+ With uv:
30
+
31
+ ```bash
32
+ uv sync
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ ```python
38
+ from count_ts_window_selector import WindowSelectionConfig, select_time_window
39
+
40
+ counts = [
41
+ 12, 10, 9, 11, 13, 8, 14, 11, 10, 9,
42
+ 12, 10, 11, 13, 9, 10, 8, 12, 13, 11,
43
+ 10, 11, 8, 12, 10, 9, 13, 11, 12, 10,
44
+ ]
45
+
46
+ config = WindowSelectionConfig(
47
+ min_event_rate=5.0,
48
+ max_zero_window_proportion=0.1,
49
+ min_windows=30,
50
+ confidence_level=0.90,
51
+ )
52
+ results = select_time_window(counts, [2, 3, 5, 10, 15], config=config)
53
+ print(results)
54
+ ```
55
+
56
+ ## Horizon-scoped analysis
57
+
58
+ Timestamp data can be evaluated inside a bounded macro-horizon before applying
59
+ the micro-window Fano analysis:
60
+
61
+ ```python
62
+ from count_ts_window_selector import WindowSelectionConfig, evaluate_horizon
63
+
64
+ result = evaluate_horizon(
65
+ "data/london-fire-brigade_timestamp_events.csv",
66
+ [1, 2, 3],
67
+ start_date="2024-01-02",
68
+ horizon_window="7 days",
69
+ base_resolution="1d",
70
+ boundary_policy="error",
71
+ config=WindowSelectionConfig(min_windows=2),
72
+ )
73
+
74
+ print(result["suitability_status"])
75
+ print(result["accepted_windows"])
76
+ ```
77
+
78
+ ## Single-stage full-range analysis
79
+
80
+ Set `horizon_window=None` to evaluate the complete aligned dataset range. The
81
+ result reports the evaluation mode, retains every accepted candidate, and
82
+ deterministically ranks them for a single selected window:
83
+
84
+ ```python
85
+ from count_ts_window_selector import (
86
+ build_arrival_intensity_series,
87
+ plot_arrival_intensity,
88
+ save_tseda_arrival_intensity_series,
89
+ )
90
+
91
+ full_range = evaluate_horizon(
92
+ "data/london-fire-brigade_timestamp_events.csv",
93
+ [1, 2, 3, 4, 5, 7],
94
+ horizon_window=None,
95
+ base_resolution="1d",
96
+ config=WindowSelectionConfig(min_event_rate=1.0, min_windows=2),
97
+ )
98
+
99
+ selected = full_range["selected_window"]
100
+ if selected is not None:
101
+ intensity = build_arrival_intensity_series(
102
+ "data/london-fire-brigade_timestamp_events.csv",
103
+ window_size=selected["window_size"],
104
+ base_resolution="1d",
105
+ )
106
+ plot_arrival_intensity(intensity)
107
+ export = save_tseda_arrival_intensity_series(
108
+ "data/london-fire-brigade_timestamp_events.csv",
109
+ window_size=selected["window_size"],
110
+ output_path="data/london_single_window_teseda.csv",
111
+ base_resolution="1d",
112
+ rate_unit="day",
113
+ )
114
+ ```
115
+
116
+ `selected_window` is ranked by dispersion deviation from 1, confidence-band
117
+ width, and window length. If no full-range candidate is accepted and
118
+ overdispersion is observed, diagnostics recommend the bounded two-stage
119
+ workflow rather than treating the failure as an invalid input.
120
+
121
+ The selected-window TSEDA CSV is saved with `window_midpoint` and
122
+ `arrival_intensity` columns. The midpoint represents the temporal center of
123
+ each complete, non-overlapping selected window.
124
+
125
+ The same export supports two-stage output: supply the selected macro-horizon's
126
+ `start_date` and `horizon_window` along with its selected micro-window. Omit
127
+ `horizon_window` for a full-range single-stage export.
128
+
129
+ ```python
130
+ two_stage_export = save_tseda_arrival_intensity_series(
131
+ "data/london-fire-brigade_timestamp_events.csv",
132
+ window_size=1,
133
+ start_date="2024-01-02",
134
+ horizon_window="7 days",
135
+ output_path="data/london_two_stage_teseda.csv",
136
+ base_resolution="1d",
137
+ rate_unit="day",
138
+ )
139
+ ```
140
+
141
+ The extracted interval is half-open: `[start_date, start_date + horizon_window)`.
142
+ Incomplete requested horizons raise `HorizonBoundaryError` by default. Complete
143
+ base-resolution intervals are constructed explicitly, including zero-count
144
+ intervals.
145
+
146
+ Candidate horizon lengths can be compared by stepping through the available
147
+ dataset:
148
+
149
+ ```python
150
+ from count_ts_window_selector import WindowSelectionConfig, tune_horizon_windows
151
+
152
+ summaries = tune_horizon_windows(
153
+ "data/london-fire-brigade_timestamp_events.csv",
154
+ candidate_horizons=["7 days", "14 days", "21 days"],
155
+ candidate_windows=[1, 2, 3],
156
+ step_size="1 day",
157
+ base_resolution="1d",
158
+ config=WindowSelectionConfig(min_windows=2),
159
+ )
160
+ ```
161
+
162
+ Each summary reports attempted, suitability-valid, and suitability-failed
163
+ horizons alongside the Poisson acceptance rate. The acceptance rate is
164
+ conditional on suitability, and is `None` when no suitable horizon exists.
165
+
166
+ Apply a separate, explicit recommendation rule to the comparison table:
167
+
168
+ ```python
169
+ from count_ts_window_selector import (
170
+ HorizonRecommendationConfig,
171
+ recommend_optimal_horizon,
172
+ )
173
+
174
+ recommendation = recommend_optimal_horizon(
175
+ summaries,
176
+ config=HorizonRecommendationConfig(
177
+ min_valid_horizon_proportion=0.80,
178
+ min_valid_horizons=10,
179
+ acceptance_rate_tolerance=0.02,
180
+ prefer_shorter_horizon=True,
181
+ ),
182
+ )
183
+ print(recommendation["recommended_horizon"])
184
+ print(recommendation["candidate_audit"])
185
+ ```
186
+
187
+ Tuning only compares horizons; recommendation is deliberately a separate
188
+ stateless operation. If no candidate passes the reliability gates, the
189
+ recommended horizon is `None` and the per-candidate audit identifies the
190
+ failed observed-versus-required thresholds.
191
+
192
+ ## Daily tabular export
193
+
194
+ ```python
195
+ from count_ts_window_selector import build_daily_window_table
196
+
197
+ summary = build_daily_window_table(
198
+ data_file="data/london-fire-brigade_timestamp_events.csv",
199
+ window_size_hours=2,
200
+ )
201
+ print(summary["output_file"])
202
+ print(summary["first_full_day"], summary["last_full_day"])
203
+ print(summary["omitted_prefix"])
204
+ print(summary["omitted_suffix"])
205
+ ```
206
+
207
+ This writes a day-by-day table with one row per full calendar day and one column per selected window within the day. Partial leading or trailing days are omitted and reported in the metadata.
208
+
209
+ ## TSEDA-ready export
210
+
211
+ ```python
212
+ from count_ts_window_selector import teseda_export
213
+
214
+ summary = teseda_export(
215
+ data_file="data/london-fire-brigade_timestamp_events.csv",
216
+ window_size_hours=2,
217
+ )
218
+ print(summary["output_file"])
219
+ print(summary["sampling_period"]) # 2 hours
220
+ print(summary["intensity_unit"]) # arrivals per hour
221
+ ```
222
+
223
+ This writes a two-column series in the form `window_start,arrival_intensity`, where the intensity is the count in each window divided by the selected window width. The sampling period is equal to the chosen window width and the normalized intensity is reported in per-hour units when the window is measured in hours.
224
+
225
+ ## CLI
226
+
227
+ ```bash
228
+ uv run count-ts-window-selector 12 10 9 11 13 8 14 11 10 9 --candidate-window 5 --candidate-window 10 --candidate-window 15
229
+ ```
230
+
231
+ The package is intended for non-hurdle, Poisson-like count processes. Sparse or zero-heavy data should be addressed with hurdle or zero-inflated models instead.
@@ -0,0 +1,221 @@
1
+ # count-ts-window-selector
2
+
3
+ A Python package for selecting Poisson-compatible observation windows in higher-event-rate count time series.
4
+
5
+ ## Overview
6
+
7
+ This package implements the workflow described in the project proposal:
8
+
9
+ 1. Validate series density and zero inflation before any Fano analysis.
10
+ 2. Filter candidate window sizes to those that retain at least 30 non-overlapping windows.
11
+ 3. Aggregate counts over each candidate window.
12
+ 4. Compute the Fano factor and chi-square-based confidence interval around 1.
13
+ 5. Return only windows whose Fano factor remains inside the configured confidence band.
14
+
15
+ The package intentionally targets higher-event-rate count series and explicitly rejects sparse, zero-heavy, or hurdle-style data before Fano-factor evaluation.
16
+
17
+ ## Installation
18
+
19
+ With uv:
20
+
21
+ ```bash
22
+ uv sync
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```python
28
+ from count_ts_window_selector import WindowSelectionConfig, select_time_window
29
+
30
+ counts = [
31
+ 12, 10, 9, 11, 13, 8, 14, 11, 10, 9,
32
+ 12, 10, 11, 13, 9, 10, 8, 12, 13, 11,
33
+ 10, 11, 8, 12, 10, 9, 13, 11, 12, 10,
34
+ ]
35
+
36
+ config = WindowSelectionConfig(
37
+ min_event_rate=5.0,
38
+ max_zero_window_proportion=0.1,
39
+ min_windows=30,
40
+ confidence_level=0.90,
41
+ )
42
+ results = select_time_window(counts, [2, 3, 5, 10, 15], config=config)
43
+ print(results)
44
+ ```
45
+
46
+ ## Horizon-scoped analysis
47
+
48
+ Timestamp data can be evaluated inside a bounded macro-horizon before applying
49
+ the micro-window Fano analysis:
50
+
51
+ ```python
52
+ from count_ts_window_selector import WindowSelectionConfig, evaluate_horizon
53
+
54
+ result = evaluate_horizon(
55
+ "data/london-fire-brigade_timestamp_events.csv",
56
+ [1, 2, 3],
57
+ start_date="2024-01-02",
58
+ horizon_window="7 days",
59
+ base_resolution="1d",
60
+ boundary_policy="error",
61
+ config=WindowSelectionConfig(min_windows=2),
62
+ )
63
+
64
+ print(result["suitability_status"])
65
+ print(result["accepted_windows"])
66
+ ```
67
+
68
+ ## Single-stage full-range analysis
69
+
70
+ Set `horizon_window=None` to evaluate the complete aligned dataset range. The
71
+ result reports the evaluation mode, retains every accepted candidate, and
72
+ deterministically ranks them for a single selected window:
73
+
74
+ ```python
75
+ from count_ts_window_selector import (
76
+ build_arrival_intensity_series,
77
+ plot_arrival_intensity,
78
+ save_tseda_arrival_intensity_series,
79
+ )
80
+
81
+ full_range = evaluate_horizon(
82
+ "data/london-fire-brigade_timestamp_events.csv",
83
+ [1, 2, 3, 4, 5, 7],
84
+ horizon_window=None,
85
+ base_resolution="1d",
86
+ config=WindowSelectionConfig(min_event_rate=1.0, min_windows=2),
87
+ )
88
+
89
+ selected = full_range["selected_window"]
90
+ if selected is not None:
91
+ intensity = build_arrival_intensity_series(
92
+ "data/london-fire-brigade_timestamp_events.csv",
93
+ window_size=selected["window_size"],
94
+ base_resolution="1d",
95
+ )
96
+ plot_arrival_intensity(intensity)
97
+ export = save_tseda_arrival_intensity_series(
98
+ "data/london-fire-brigade_timestamp_events.csv",
99
+ window_size=selected["window_size"],
100
+ output_path="data/london_single_window_teseda.csv",
101
+ base_resolution="1d",
102
+ rate_unit="day",
103
+ )
104
+ ```
105
+
106
+ `selected_window` is ranked by dispersion deviation from 1, confidence-band
107
+ width, and window length. If no full-range candidate is accepted and
108
+ overdispersion is observed, diagnostics recommend the bounded two-stage
109
+ workflow rather than treating the failure as an invalid input.
110
+
111
+ The selected-window TSEDA CSV is saved with `window_midpoint` and
112
+ `arrival_intensity` columns. The midpoint represents the temporal center of
113
+ each complete, non-overlapping selected window.
114
+
115
+ The same export supports two-stage output: supply the selected macro-horizon's
116
+ `start_date` and `horizon_window` along with its selected micro-window. Omit
117
+ `horizon_window` for a full-range single-stage export.
118
+
119
+ ```python
120
+ two_stage_export = save_tseda_arrival_intensity_series(
121
+ "data/london-fire-brigade_timestamp_events.csv",
122
+ window_size=1,
123
+ start_date="2024-01-02",
124
+ horizon_window="7 days",
125
+ output_path="data/london_two_stage_teseda.csv",
126
+ base_resolution="1d",
127
+ rate_unit="day",
128
+ )
129
+ ```
130
+
131
+ The extracted interval is half-open: `[start_date, start_date + horizon_window)`.
132
+ Incomplete requested horizons raise `HorizonBoundaryError` by default. Complete
133
+ base-resolution intervals are constructed explicitly, including zero-count
134
+ intervals.
135
+
136
+ Candidate horizon lengths can be compared by stepping through the available
137
+ dataset:
138
+
139
+ ```python
140
+ from count_ts_window_selector import WindowSelectionConfig, tune_horizon_windows
141
+
142
+ summaries = tune_horizon_windows(
143
+ "data/london-fire-brigade_timestamp_events.csv",
144
+ candidate_horizons=["7 days", "14 days", "21 days"],
145
+ candidate_windows=[1, 2, 3],
146
+ step_size="1 day",
147
+ base_resolution="1d",
148
+ config=WindowSelectionConfig(min_windows=2),
149
+ )
150
+ ```
151
+
152
+ Each summary reports attempted, suitability-valid, and suitability-failed
153
+ horizons alongside the Poisson acceptance rate. The acceptance rate is
154
+ conditional on suitability, and is `None` when no suitable horizon exists.
155
+
156
+ Apply a separate, explicit recommendation rule to the comparison table:
157
+
158
+ ```python
159
+ from count_ts_window_selector import (
160
+ HorizonRecommendationConfig,
161
+ recommend_optimal_horizon,
162
+ )
163
+
164
+ recommendation = recommend_optimal_horizon(
165
+ summaries,
166
+ config=HorizonRecommendationConfig(
167
+ min_valid_horizon_proportion=0.80,
168
+ min_valid_horizons=10,
169
+ acceptance_rate_tolerance=0.02,
170
+ prefer_shorter_horizon=True,
171
+ ),
172
+ )
173
+ print(recommendation["recommended_horizon"])
174
+ print(recommendation["candidate_audit"])
175
+ ```
176
+
177
+ Tuning only compares horizons; recommendation is deliberately a separate
178
+ stateless operation. If no candidate passes the reliability gates, the
179
+ recommended horizon is `None` and the per-candidate audit identifies the
180
+ failed observed-versus-required thresholds.
181
+
182
+ ## Daily tabular export
183
+
184
+ ```python
185
+ from count_ts_window_selector import build_daily_window_table
186
+
187
+ summary = build_daily_window_table(
188
+ data_file="data/london-fire-brigade_timestamp_events.csv",
189
+ window_size_hours=2,
190
+ )
191
+ print(summary["output_file"])
192
+ print(summary["first_full_day"], summary["last_full_day"])
193
+ print(summary["omitted_prefix"])
194
+ print(summary["omitted_suffix"])
195
+ ```
196
+
197
+ This writes a day-by-day table with one row per full calendar day and one column per selected window within the day. Partial leading or trailing days are omitted and reported in the metadata.
198
+
199
+ ## TSEDA-ready export
200
+
201
+ ```python
202
+ from count_ts_window_selector import teseda_export
203
+
204
+ summary = teseda_export(
205
+ data_file="data/london-fire-brigade_timestamp_events.csv",
206
+ window_size_hours=2,
207
+ )
208
+ print(summary["output_file"])
209
+ print(summary["sampling_period"]) # 2 hours
210
+ print(summary["intensity_unit"]) # arrivals per hour
211
+ ```
212
+
213
+ This writes a two-column series in the form `window_start,arrival_intensity`, where the intensity is the count in each window divided by the selected window width. The sampling period is equal to the chosen window width and the normalized intensity is reported in per-hour units when the window is measured in hours.
214
+
215
+ ## CLI
216
+
217
+ ```bash
218
+ uv run count-ts-window-selector 12 10 9 11 13 8 14 11 10 9 --candidate-window 5 --candidate-window 10 --candidate-window 15
219
+ ```
220
+
221
+ The package is intended for non-hurdle, Poisson-like count processes. Sparse or zero-heavy data should be addressed with hurdle or zero-inflated models instead.
@@ -0,0 +1,26 @@
1
+ [project]
2
+ name = "count-ts-window-selector"
3
+ version = "0.2.0"
4
+ description = "Select Poisson-compatible time windows for higher-event-rate count time series."
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+ dependencies = ["scipy>=1.11.0"]
8
+
9
+ [[project.authors]]
10
+ name = "Rajiv Sambasivan"
11
+ email = "rajiv.sambasivan@r2-ds.com"
12
+
13
+ [project.scripts]
14
+ count-ts-window-selector = "count_ts_window_selector:main"
15
+
16
+ [build-system]
17
+ requires = ["uv_build>=0.12.0,<0.13.0"]
18
+ build-backend = "uv_build"
19
+
20
+ [dependency-groups]
21
+ dev = [
22
+ "ipykernel>=6.29.0",
23
+ "matplotlib>=3.8.0",
24
+ "nbclient>=0.10.0",
25
+ "pytest>=9.2.0",
26
+ ]
@@ -0,0 +1,27 @@
1
+ [project]
2
+ name = "count-ts-window-selector"
3
+ version = "0.2.0"
4
+ description = "Select Poisson-compatible time windows for higher-event-rate count time series."
5
+ readme = "README.md"
6
+ authors = [
7
+ { name = "Rajiv Sambasivan", email = "rajiv.sambasivan@r2-ds.com" }
8
+ ]
9
+ requires-python = ">=3.10"
10
+ dependencies = [
11
+ "scipy>=1.11.0",
12
+ ]
13
+
14
+ [project.scripts]
15
+ count-ts-window-selector = "count_ts_window_selector:main"
16
+
17
+ [build-system]
18
+ requires = ["uv_build>=0.12.0,<0.13.0"]
19
+ build-backend = "uv_build"
20
+
21
+ [dependency-groups]
22
+ dev = [
23
+ "ipykernel>=6.29.0",
24
+ "matplotlib>=3.8.0",
25
+ "nbclient>=0.10.0",
26
+ "pytest>=9.2.0",
27
+ ]