features.build¶
Feature computation pipeline: convert normalised ActivityWatch events
into bucketed, schema-validated FeatureRow instances and optionally
write them to Parquet.
Pipeline overview¶
The feature build pipeline operates in three modes:
- Batch (
build_features_from_aw_events) -- converts a sorted sequence of normalisedEventobjects (plus optionalAWInputEventobjects) into per-bucketFeatureRowinstances. - Dummy (
generate_dummy_features) -- produces synthetic feature rows for a given date, useful for testing and development. - File (
build_features_for_date) -- fetches events from a running ActivityWatch server (or generates dummy features when no server is available) and writes them to the Parquet partition layout.
The batch pipeline follows this data flow:
Events ──► bucket by time ──► dominant app per bucket ──► context features
│
Input events ──► bucket ──► aggregate (keys/clicks/mouse) ─┘
│
┌──────────────────────────────────┘
▼
session detection ──► dynamics tracker ──► FeatureRow
build_features_from_aw_events¶
Core function that converts normalised events into per-bucket feature rows. Events are grouped into fixed-width time buckets (default 60 s). For each bucket, the dominant application (longest total duration) determines the context columns (app ID, category, title hash, flags).
| Parameter | Type | Default | Description |
|---|---|---|---|
events |
Sequence[Event] |
-- | Sorted normalised events |
user_id |
str |
"default-user" |
Pseudonymous user identifier |
device_id |
str \| None |
None |
Optional device identifier |
input_events |
Sequence[AWInputEvent] \| None |
None |
Input watcher events for keyboard/mouse features |
bucket_seconds |
int |
DEFAULT_BUCKET_SECONDS |
Width of each time bucket |
session_start |
datetime \| None |
None |
Fixed session start (online mode); None triggers auto-detection |
idle_gap_seconds |
float |
DEFAULT_IDLE_GAP_SECONDS |
Minimum gap that splits sessions (batch mode) |
schema_version |
str |
"v3" |
Feature schema to emit ("v1", "v2", or "v3") |
When input_events is None, all keyboard/mouse feature columns
(keys_per_min, clicks_per_min, etc.) are set to None.
When session_start is None (batch mode), sessions are detected
automatically from idle gaps via
features.sessions.detect_session_boundaries.
In online mode, the caller passes the known session start to avoid
resetting the session each poll cycle.
Sub-modules invoked per bucket:
| Sub-module | Columns produced |
|---|---|
features.windows |
app_switch_count_last_5m, app_switch_count_last_15m, app_entropy_5m, app_entropy_15m, top2_app_concentration_15m |
features.sessions |
session_id, session_length_so_far |
features.domain |
domain_category |
features.text |
window_title_bucket, title_repeat_count_session, keyed title-sketch features (v3) |
features.dynamics |
Rolling means and deltas (7 columns) |
| (inline) | app_dwell_time_seconds |
app_dwell_time_seconds is computed directly in
build_features_from_aw_events (not via a sub-module). It tracks how
long the current dominant app has been foreground continuously across
consecutive buckets. When the dominant app changes the counter resets;
when it stays the same the previous dwell is accumulated.
app_entropy_5m and app_entropy_15m are Shannon entropy values of the
app duration distribution over the last 5 and 15 minutes respectively.
They are computed by features.windows.app_entropy_in_window. A single
focused app yields entropy 0; uniform usage across N apps yields
log2(N). These features capture how "scattered" the user's app usage
is within a time window.
top2_app_concentration_15m is the combined time share of the two
most-used apps over the last 15 minutes. It is computed by
features.windows.top2_app_concentration_in_window. A value of 1.0
means at most two apps were used; lower values indicate more fragmented
usage across three or more apps.
from taskclf.features.build import build_features_from_aw_events
rows = build_features_from_aw_events(
events,
user_id="user-001",
input_events=input_events,
)
generate_dummy_features¶
Creates n_rows synthetic FeatureRow instances spanning hours 9--17
of the given date. Cycles through 10 dummy applications to produce
realistic variety. Useful for pipeline testing without real
ActivityWatch data.
| Parameter | Type | Default | Description |
|---|---|---|---|
date |
date |
-- | Calendar date to generate buckets for |
n_rows |
int |
DEFAULT_DUMMY_ROWS |
Number of rows to generate |
user_id |
str |
"dummy-user-001" |
User identifier |
device_id |
str \| None |
None |
Optional device identifier |
build_features_for_date¶
Builds feature rows for a given date and writes them to Parquet using the partitioned layout:
When aw_host is provided (and synthetic is False), events are
fetched live from a running ActivityWatch server via the REST API.
Both aw-watcher-window and aw-watcher-input buckets are queried
automatically. Without aw_host (or with synthetic=True), dummy
features are generated for testing.
| Parameter | Type | Default | Description |
|---|---|---|---|
date |
date |
-- | Calendar date to build features for |
data_dir |
Path |
-- | Root of processed data |
aw_host |
str \| None |
None |
AW server URL; None falls back to dummy generation |
title_salt |
str \| None |
None |
Optional process override for title hashing; defaults to local .title_secret |
user_id |
str |
"default-user" |
Pseudonymous user identifier |
device_id |
str \| None |
None |
Optional device identifier |
synthetic |
bool |
False |
Force dummy feature generation |
schema_version |
str |
"v3" |
Output schema version |
When aw_host is set and title_salt is omitted, the builder resolves
the per-install local secret from UserConfig(data_dir).title_secret.
New builds default to features_v3/.
See also¶
core.schema--FeatureSchemaV3is the current default contract;v1andv2remain supportedcore.types--FeatureRowandEventmodelsadapters.activitywatch-- event normalisation upstream of this pipeline
taskclf.features.build
¶
Feature computation pipeline: build bucketed feature rows and write to parquet.
generate_dummy_features(date, n_rows=DEFAULT_DUMMY_ROWS, *, user_id='dummy-user-001', device_id=None, schema_version=LATEST_FEATURE_SCHEMA_VERSION)
¶
Create n_rows synthetic FeatureRow instances spanning date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date
|
date
|
The calendar date to generate buckets for (hours 9-17). |
required |
n_rows
|
int
|
Number of rows to generate. |
DEFAULT_DUMMY_ROWS
|
user_id
|
str
|
User identifier for all generated rows. |
'dummy-user-001'
|
device_id
|
str | None
|
Optional device identifier. |
None
|
Returns:
| Type | Description |
|---|---|
list[FeatureRowBase]
|
Validated |
Source code in src/taskclf/features/build.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
build_features_from_aw_events(events, *, user_id='default-user', device_id=None, input_events=None, bucket_seconds=DEFAULT_BUCKET_SECONDS, session_start=None, idle_gap_seconds=DEFAULT_IDLE_GAP_SECONDS, schema_version=LATEST_FEATURE_SCHEMA_VERSION)
¶
Convert normalised events into per-bucket :class:FeatureRow instances.
Events are grouped into fixed-width time buckets. For each bucket the dominant application (longest total duration) is selected and its metadata (app ID, title hash, flags) is used to populate the context columns.
When input_events from aw-watcher-input are provided, keyboard
and mouse features (keys_per_min, clicks_per_min,
scroll_events_per_min, mouse_distance) are computed by
aggregating the 5-second input samples that fall within each bucket.
Without input events those fields remain None.
Session detection is performed automatically via idle-gap analysis
(see :func:~taskclf.features.sessions.detect_session_boundaries).
In online mode the caller may pass a known session_start to
avoid resetting the session each poll cycle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
Sequence[Event]
|
Sorted, normalised events satisfying the
:class: |
required |
user_id
|
str
|
Random UUID identifying the user (not PII). |
'default-user'
|
device_id
|
str | None
|
Optional device identifier. |
None
|
input_events
|
Sequence[AWInputEvent] | None
|
Optional sorted input events from
|
None
|
bucket_seconds
|
int
|
Width of each time bucket in seconds (default 60). |
DEFAULT_BUCKET_SECONDS
|
session_start
|
datetime | None
|
If provided, used as the session start for every
bucket (online mode). When |
None
|
idle_gap_seconds
|
float
|
Minimum gap in seconds that splits sessions
(only used when session_start is |
DEFAULT_IDLE_GAP_SECONDS
|
Returns:
| Type | Description |
|---|---|
list[FeatureRowBase]
|
Validated |
Source code in src/taskclf/features/build.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | |
build_features_for_date(date, data_dir, *, aw_host=None, title_salt=None, user_id='default-user', device_id=None, synthetic=False, schema_version=LATEST_FEATURE_SCHEMA_VERSION)
¶
Build feature rows for date, validate, and write to parquet.
When aw_host is provided (and synthetic is False), events
are fetched live from a running ActivityWatch server. Otherwise
dummy/synthetic rows are generated for testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date
|
date
|
Calendar date to build features for. |
required |
data_dir
|
Path
|
Root of processed data (e.g. |
required |
aw_host
|
str | None
|
Base URL of a running AW server
(e.g. |
None
|
title_salt
|
str | None
|
Optional process override for title hashing. When omitted
and aw_host is set, the local |
None
|
user_id
|
str
|
Pseudonymous user identifier. |
'default-user'
|
device_id
|
str | None
|
Optional device identifier. |
None
|
synthetic
|
bool
|
Force dummy feature generation even if aw_host is set. |
False
|
Returns:
| Type | Description |
|---|---|
Path
|
Path of the written parquet file. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If generated data fails the selected feature-schema validation. |