Skip to content

feat(analyses): police recorded crime as needs, counted in offences - #237

Merged
billlyzhaoyh merged 3 commits into
mainfrom
feat/crime-need
Dec 1, 2026
Merged

billlyzhaoyh merged 3 commits into
mainfrom
feat/crime-need

Conversation

@billlyzhaoyh

Copy link
Copy Markdown
Contributor

The last of #210's three sources. Police recorded crime becomes a need, counted in offences, as decided on 1 December. There are two needs, recorded_crime and violence_against_the_person, for England and Wales. They are published by community safety partnership and shared out to councils by residents (#218).

Where the figures come from

ONS discontinued its dataset "Recorded crime data by Community Safety Partnership area" in October 2024. The figures moved to "Crime in England and Wales: Police Force Area data tables", published quarterly. The latest edition is the year ending June 2026, and the next is due 28 January 2027.

A new loader, uk_open_data.crime.load_crime():

  • Finds the latest edition through the JSON the ONS website gives for each page, at <page>/data.
  • Reads Table C2: one row per partnership, 314 in all, with one column per kind of offence (23). Every partnership code matches the shipped lookup.
  • Leaves out each force's total and its "Unassigned" offences, about 3% of all offences.
  • Kent: five partnerships' crimes are recorded under "Unassigned Kent", per the note ONS hangs on their rows. Their published figures are partial: Medway shows 96 offences for the year. Those partnerships get missing figures, not the published ones. The loader reads ONS's notes sheet and acts only on notes that say offences are "recorded under an unassigned" group. A note about anything else leaves the figures alone, such as Humberside's note this quarter, which says its overall figures remain accurate.
  • Refuses a table whose offence headings would collide, such as a second total that includes fraud. It also refuses a table missing either of the two offences the needs read.

The needs

need Counts
recorded_crime Offences the police recorded in the latest year, excluding fraud
violence_against_the_person Offences of violence against the person
  • Wording: results say "offences recorded", and that offences "are in" a place, not that they "live in" it. This needed three new NeedWords fields, who, live_in and their. Their defaults keep every existing need's wording unchanged.
  • The caveat says:
    • offences are not people or victims;
    • they are counted where recorded;
    • forces record crime differently;
    • fraud and unassigned offences are left out;
    • partnership figures are shared out by residents.
  • Real figures: 312 of 318 councils get a figure, and the total is 4,971,918 offences, exactly the partnerships' total less Kent's five. The warning names Kent's five partnerships and their six councils.

Small shared fixes

  • Sharing step: the working said shares were rounded "in whole people". It now uses the need's own unit, so "in whole offences", "children" or "households".
  • Header matching: _frames._first_cell_is sets note marks aside, so the low income loader drops its own header matcher.

Review

  • /simplify:

    • the incomplete-partnership rule is now driven by ONS's notes, not by any mark;
    • the loader checks that the two needed offences exist;
    • shared header matching;
    • tidier edition lookup.

    Skipped:

    • keeping a source's figures fetches them once per need, because download_need_figures clears the cache each time. Homelessness, low income and vaccinations already do this, so the fix belongs in the shared step, as a follow-up;
    • a shared module for the ONS website: one caller so far.
  • /code-review: three things to fix, each with a check I ran against the code before the fix to see it fail:

    1. offences "live in" places;
    2. the note rule still matched Humberside's note;
    3. colliding offence columns.

    Also fixed:

    • text and negative figures;
    • the edition name's capital letter;
    • a guard for null JSON;
    • the reason's "every crime";
    • docstrings;
    • the dataset area's year;
    • the docs on Kent's figures;
    • the live check now fetches once, because ONS rate-limits repeated requests.

Tests

These are the seven tests agreed in the plan, in tests/test_crime_api.py and tests/test_crime_needs.py:

  • each partnership has its offences;
  • the latest edition is found;
  • a changed layout names ONS, across eight cases;
  • each need analysis takes both needs;
  • partnership offences are shared out: Dartford 522 and Gravesham 478 of 1,000;
  • Kent's five are left out and named, and naming Medway is refused;
  • a result counts offences, not people.

Existing tests changed:

  • Lists that grew: the need-reach and provision-gap needs lists. Nothing was removed or weakened.
  • The datasets test's kept_figures: it now keys each need's invented figures by the need's own kind of area, using the shipped lookup. Partnership codes aren't council codes.

Checks run

On main 09278d8:

  • ruff check, ruff format --check, ty check src and ast-grep, as CI runs it;
  • pytest: 1,293 passed;
  • mkdocs build --strict.

Live:

  • the loader on ONS's real June 2026 file;
  • real need-reach runs on both needs;
  • the live checks for crime and for the other gov.uk sources that share _frames: low income, fuel poverty, homelessness and vaccinations.

AI tools

Claude Code wrote the change, its tests and this description, and ran the reviews.

A new loader, uk_open_data.crime.load_crime(), reads the latest quarter's
police recorded crime for every community safety partnership in England and
Wales. The source is ONS's "Crime in England and Wales: Police Force Area
data tables", where the figures by partnership moved when ONS discontinued
their own dataset in 2024.
- It finds the latest edition through the JSON the ONS website gives for
  each page.
- It returns one row per partnership, with one column per kind of offence.
- Each force's total and its "Unassigned" offences are left out.
- A partnership whose row ONS marks with a note has missing figures, not
  the published few. ONS marks five Kent partnerships so today, because
  their crimes are recorded under "Unassigned Kent".

Need against reach, provision gap and access and distance can now take two
needs, for England and Wales, as agreed on 1 December:
- recorded_crime: offences excluding fraud;
- violence_against_the_person.
They count offences, so results say "offences recorded", and the caveat
says offences are not people or victims. The figures are for partnerships,
so they are shared out to councils by residents (#218). Most partnerships
are one council. A council whose partnership has no figure is left out and
named.

The working's sentence on sharing now rounds to whole units of what the need
counts, such as offences or children, not "whole people". The datasets test
keeps each need's invented figures by the need's own kind of area.

Signed-off-by: billlyzhaoyh <billlyzhaoyh@users.noreply.github.com>
Github-Issue: #210
After review:
- The loader left out any partnership marked with a note. A note only says
  something applies; ONS's partnership notes also cover small numbers and
  undercounts. It now reads the "Notes - CSP" sheet and leaves out a
  partnership's figures only when its note says its offences are recorded
  as "Unassigned", as note 5 does of five Kent partnerships today. A note
  about anything else, such as Northumbria's robbery undercount in 2025,
  leaves the figures as they are.
- A table that loses total_recorded_crime or violence_against_the_person
  says so, not a KeyError later in a need.
- _frames' header matcher sets marks aside on both sides, so low income and
  crime pass their header's text and drop their own matchers.
- The edition is found with _govuk.listed, and a constant used once is gone.
- In the datasets test, _for_areas adds up councils in one area, and says
  which councils it leaves out.

Signed-off-by: billlyzhaoyh <billlyzhaoyh@users.noreply.github.com>
Github-Issue: #210
From code review. Each of the first three has a test that failed before:
- The analyses said offences "live in" places, as if they were people:
  "97.1% of the offences recorded live in ...". NeedWords now carry who,
  live_in and their, so offences "are in" places, a share label reads
  "offences recorded that are in ...", and access and distance counts each
  offence at the centre of "its" local authority. Words for people,
  households and children are unchanged. need-reach's caveat says a
  count that is not of people is read as more or less reach between places.
- A partnership's figures are taken away only when its note says its
  offences are "recorded under an unassigned" group, as ONS words it for
  Kent. ONS has other notes that mention unassigned offences, such as
  Humberside's, which says its figures remain accurate.
- Offences whose headings would give one column, such as a total that
  includes fraud beside the one that leaves it out, or one with no name,
  are refused rather than one taking the other's place.

Also:
- A figure written as text, such as "1,234 [r]", is read; a negative
  count, which ONS says can happen, is missing.
- The edition's name starts lower case, as a sentence says it, whatever
  case ONS gives it in, and a null description is no longer an
  AttributeError.
- The reason for the needs no longer says every crime is recorded.
- The docstrings that list need sources name ONS.
- The live check fetches once, because ONS limits how often it is asked.
- The dataset's areas carry their year.
- The docs give Kent's partial figures as they are, such as Dartford and
  Gravesham's 11,165.

Signed-off-by: billlyzhaoyh <billlyzhaoyh@users.noreply.github.com>
Github-Issue: #210
@billlyzhaoyh
billlyzhaoyh requested a review from lucharo as a code owner December 1, 2026 16:07
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
PR Preview Action v1.8.2
Preview removed because the pull request was closed.
2026-12-01 16:43 UTC

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.

1 participant