Python Lab 11: Police Stops in Aurora
Due Tuesday, November 30th, 11:45PM
Introduction
To get started, open IDLE and then create a New File via the File menu.
We suggest you immediately save this file in the directory you created to manage all your
Python Labs this semester (e.g., CSCI101/PythonLabs). Save this file as
Lab11-aurora_police.py.
This assignment
explores a data file (in CSV format)
from Aurora, Colorado on
citizens that were stopped by law enforcement between Dec 2011 and Dec 2016. You should
first write a function that reads the contents of the csv file and then
two other
functions to count the total number of police stops (by race and by sex).
Download the following file and unzip it to obtain the CSV file that you
need to test your functions:
aurora_police.csv
Assignment
Step 1: Reading a CSV file
Write a function called read_csv that takes the path to a CSV
file and returns the contents of that file. We suggest you use
the CSV module that we discussed in Week 10 (see
Python Video #17: CSV Files).
Put the CSV file in the same directory/folder as your Python code.
You can assume the CSV file exists in the same directory/folder
where your Python code is.
Extra credit (0.5 points) if your function prints
“OUTPUT File does not exist” when appropriate (if the
provided path leads to a file that does not exist). See
optional zyBooks Section 21.3 for details.
Also, extra credit (0.5 points) if your function prints “OUTPUT Invalid file extension” when appropriate
(if provided path leads to a file that does not have the .csv file extension).
In both of these
invalid file selections, have your function return None.
Example Function Call
>>> rows =
read_csv("aurora_police.csv")
>>> rows[0]
['raw_row_number','date',
'time', 'location', 'lat', 'lng', 'district', 'subject_age',
'subject_race', 'subject_sex', 'type', 'violation',
'citation_issued', 'outcome', 'raw_ethnicity', 'raw_race']
Best practice: Since the open function uses the system default
encoding, which may or may not be appropriate for the contents of
a file, it is always a good idea to specify the encoding when you open
a file. In this lab, you MUST specify the
encoding when you open the file or your program will error. See below and other examples.
open('some_csv', 'r',
encoding='utf-8')
Step 2: Processing your CSV data
Write a function called stops_by_race that prints the number of police stops
by race. The first input
to the function should be the content retrieved
from your read_csv function. The second input to the function
should be the subject_race, such as “white”,
“asian” or “ALL”. If ALL is specified as the second input,
the function should return the total count of all stops
in the file. Create another function called stops_by_sex which
returns the number of police stops by sex (including the "ALL" option).
Example Function Calls
>>> rows = read_csv("aurora_police.csv")
>>> stops_by_race(rows, 'ALL')
172929
>>> stops_by_race(rows, ‘white’)
113807
>>> stops_by_sex(rows, ‘male’)
104322
Step 3: Data Comparison
Once you are able to obtain the number of stops for each race, review demographic data from census.gov or Wikipedia for Aurora, Colorado and compare the
racial makeup of the city to the results of your stops_by_race
function. Were the percentages what you expected? Write
a short paragraph describing your thoughts/analysis from this data comparison in a separate document from your Python code.
Extra Credit (0-6 points): Change your data to another city of interest and
compare the results. Additional data files can be found here:
Stanford
Open Policing. To receive the full 3 points, you must include details of your findings in the
same document as Step 3. You are welcome to do a 3rd city as well, for another
3 points (6 points max).
Summary
1) Your program must have three
functions:
- Function “read_csv” must read a csv file and return the
file contents. Possible extra credit is specified above.
- Function “stops_by_race” should take in the data obtained
by read_csv and return the number of police stops for a
specific race or ALL races.
- Function “stops_by_sex” should take in the data obtained
by read_csv and return the number of police stops for a
specific gender or ALL genders. Note: the Aurora data for
gender in this file is male, female, and NA; hopefully they are
more inclusive in their next data set!
You will submit this program to the "Python Lab 11 - Police Stops: Program Code" Gradescope assignment,
where it will be autograded as usual by Gradescope. You should submit a single python file containing only
your function definitions, with the same function names as specified above. If a function name does
not match the name above, you will not receive credit for that function.
2) A separate pdf file should contain a brief summary of your
findings from your data comparison. We encourage you to analyze your results as
a scientist should, i.e., with an open mind and without bias.
Any racist comments are unacceptable. All additional extra credit summaries should be included in
this pdf as well, as separate paragraphs. Submit your pdf to to the "Python Lab 11 - Police Stops: Thoughts/Analysis"
Gradescope assignment.
Comments
All Python files should include a header with your name,
section, assignment info, references (i.e., who did you collaborate
with on this assignment?; what resource did you use?), and approximate
time taken to do the assignment. Be sure to cite any
allowed external references used to complete the assignment.
Any code without this header will lose 1 point. Here's an
example:
# Tracy Camp
# CSCI 101 – Section A
# Python Lab 11
# References: TA Carter for help with function calls
# Time: 75 minutes
Submission
Once you are satisfied with your solution to this lab, you need to submit your solutions to
Gradescope. In Gradescope, go to Assignments > Python Lab 11 - Police Stops: Program Code and upload Lab11-aurora_police.py.
Upload a pdf of your analysis/comments to Python Lab 11 - Police Stops: Thoughts/Analysis.
Note: this lab is worth 10 points. To receive credit, your code must execute in Python 3, and you must submit each file
to the proper Gradescope assignment.
Whenever you submit something to Gradescope, we strongly recommend you always double check
what you submitted actually got submitted correctly (e.g., did the file upload correctly?
did you submit the correct file? etc.) If your submission is incorrect, it's on you.
|