Skip to content

perf: read a distribution straight into a DataFrame - #16

Merged
simontaurus merged 1 commit into
mainfrom
feat/frame-first-dataset
Nov 3, 2026
Merged

simontaurus merged 1 commit into
mainfrom
feat/frame-first-dataset

Conversation

@simontaurus

Copy link
Copy Markdown
Contributor

Closes #15.

Loading a cycling dataset for the dashboard took minutes. The cause is
structural: the dataset was read into one pydantic model per cell and
immediately flattened back into a DataFrame, and only the DataFrame is
plotted.

materialize_df

Reads a distribution straight into a frame, without constructing rows.
materialize_data is unchanged and still returns the typed rows, and both now
share one distribution loop (_read_distribution), so the retry, the warning
per unreadable distribution and the chained error behave identically.

CSV goes through _pint_csv.read_csv. JSON is read column by column: a
serialized value is {"value": x, "unit": "Item:OSW..."}, so when a column
agrees on one unit the magnitudes are taken directly and the unit is attached
once.

to_df vectorized

A column almost always holds one unit. When that unit is the target, the stored
magnitudes already are the answer, so the column is read without pint at all.
A column that mixes units keeps the per-value to_pint().to(unit) conversion,
covered by a test with a kelvin and a Celsius value in one column.

This also speeds up serialize_data("csv"), which uploads use.

Measured, 20 000 rows

before after
materialize_df (csv) n/a 0.25 s
materialize_data (csv) 11.07 s unchanged
materialize_df (json) n/a 1.38 s
to_df 4.19 s 0.57 s

For reference, pd.read_csv of the same data is 0.02 s.

Tests: 234 pass, 16 new. The two failures in test_controller.py need a live
PostgREST and fail the same way on main.

- materialize_df skips building a model per cell, which is what made a
  large dataset take minutes; materialize_data keeps the typed rows
- to_df reads a column's magnitudes directly when its values agree on
  one unit, and converts per value only when they differ (#15)
- both read paths share one distribution loop

20k rows: materialize_df 0.25 s against 11.07 s for the rows (csv),
to_df 4.19 s to 0.57 s
@simontaurus
simontaurus merged commit a047db5 into main Nov 3, 2026
9 checks passed
@simontaurus
simontaurus deleted the feat/frame-first-dataset branch November 3, 2026 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vectorize DatasetControllerMixin.to_df unit conversion

1 participant