Skip to content

Commit 3913817

Browse files
authored
Fix incorrect Likelihood of Stop chart if some races have no stops. (#362)
1 parent f6e96f7 commit 3913817

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

frontend/src/Components/Charts/SearchRate/SearchRate.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@ function SearchRate(props) {
9494
DEMOGRAPHICS_COLORS.other,
9595
];
9696
const data = {
97-
labels: ['Black', 'Hispanic', 'Asian', 'Native American', 'Other'],
97+
labels: res.data.stop_percentages_races || [
98+
'Black',
99+
'Hispanic',
100+
'Asian',
101+
'Native American',
102+
'Other',
103+
],
98104
datasets: [
99105
{
100106
axis: 'y',

nc/tests/api/test_stop_search_rate.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ def test_agency_stop_counts_by_year(self, client, durham, year_2020):
393393
# Stop rate ratio should be 0.6 for black drivers, or black drivers are
394394
# 60% more likely to be pulled over than white drivers
395395
assert data["stop_percentages"] == [0.62]
396+
assert data["stop_percentages_races"] == ["Black"]
396397
table_data = data["table_data"]
397398
# Two rows for white and black drivers
398399
assert len(table_data) == 2
@@ -438,6 +439,7 @@ def test_more_and_less_likelihood_of_stops(self, client, durham, year_2020):
438439
assert response.status_code == 200
439440
data = response.json()
440441
assert data["stop_percentages"] == [1.69, -0.35]
442+
assert data["stop_percentages_races"] == ["Black", "Asian"]
441443
table_data = data["table_data"]
442444
# Three rows for white, black, and asian drivers
443445
assert len(table_data) == 3
@@ -490,3 +492,4 @@ def test_no_acs_data_empty_stop_percentages(self, client, durham, year_2020):
490492
# When no ACS data exists, stop_percentages should be empty
491493
assert data["stop_percentages"] == []
492494
assert data["table_data"] == []
495+
assert "stop_percentages_races" not in data

nc/views/likelihood.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,11 @@ def get(self, request, agency_id):
159159
"stop_percentages": stop_percentages,
160160
"table_data": table_data.to_dict(orient="records"),
161161
}
162+
163+
if stop_percentages and len(stop_percentages) < 5:
164+
# We have stop percentages, but not for all races. Add the list of
165+
# races with stop percentages to the response, so we'll know which
166+
# ones to display in the chart on the frontend
167+
data["stop_percentages_races"] = chart_df["race"]
168+
162169
return Response(data=data, status=200)

0 commit comments

Comments
 (0)