Skip to content

Commit 8832164

Browse files
Add task for plots.
1 parent bb3736c commit 8832164

File tree

8 files changed

+5597
-22
lines changed

8 files changed

+5597
-22
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ years to observe trends and patterns.
3434

3535
The cleaning part was done in a way in which one can easily adapt the code such that one
3636
can add other variables of the survey to the survey to the different dictionaries in the
37-
parampeters.py file.
37+
parampeters.py file, this is why it does not directly follow exactly the same structure
38+
as in class where we used specific variables.
3839

3940
Therefore some parts of the cleaning where split up in two parts such that the code
4041
remains flexible. Since you told us in the assignment that it is fine for readability

crime_increase_perception_commune

31.7 KB
Binary file not shown.

crime_increase_perception_nation

28.8 KB
Binary file not shown.
31.8 KB
Binary file not shown.

src/project_mbb/data_management/clean_enusc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ def _fill_missing(enusc_mapped):
5555
replacements = {99: pd.NA, 77: "Other", 88: "Don't know", 85: "Doesn't apply"}
5656
enusc_filling = enusc_mapped.copy()
5757
for column in categories:
58+
enusc_filling[column] = enusc_filling[column].astype(pd.Int8Dtype())
5859
enusc_filling[column] = enusc_filling[column].apply(
5960
lambda x: replacements.get(x, x) if x in replacements else x
6061
)
6162

6263
if enusc_mapped[column].dtype != "object":
6364
enusc_filling[column] = (
6465
enusc_filling[column]
65-
# .astype(pd.Int8Dtype())
66+
# .astype(pd.Int8Dtype()) esto se puede borrar
6667
.astype(pd.CategoricalDtype())
6768
)
6869
enusc_filling[column] = enusc_filling[column].cat.rename_categories(
@@ -94,5 +95,4 @@ def _set_data_types_not_mapped_var(enusc_filled):
9495
enusc_dtypes[val] = enusc_dtypes[val].astype(pd.CategoricalDtype())
9596
for ent in strings:
9697
enusc_dtypes[ent] = enusc_dtypes[ent].astype(str)
97-
enusc_dtypes = enusc_dtypes
9898
return enusc_dtypes

src/project_mbb/final/plot.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pandas as pd
2+
import plotly.graph_objects as go
3+
4+
from project_mbb.config import BLD
5+
6+
products = [
7+
BLD / "final" / "perception_plot_commune.png",
8+
BLD / "final" / "perception_plot_nation.png",
9+
BLD / "final" / "perception_plot_neighborhood.png",
10+
]
11+
12+
13+
def plot_crime_perception(enusc_clean):
14+
crime_columns = {
15+
"crime_increase_perception_nation": "Crime Perception - Nation",
16+
"crime_increase_perception_commune": "Crime Perception - Commune",
17+
"crime_increase_perception_neighborhood": "Crime Perception - Neighbourhood",
18+
}
19+
20+
category_labels = ["Decreased", "Stayed the same", "Increased", "Don't know"]
21+
for (col, title), i in zip(crime_columns.items(), products, strict=False):
22+
enusc_model = pd.DataFrame()
23+
enusc_model[col] = enusc_clean[col].cat.codes
24+
category_counts = (
25+
enusc_model[col].value_counts().reindex([0, 3, 2, 1], fill_value=0)
26+
)
27+
28+
fig = go.Figure()
29+
30+
fig.add_trace(
31+
go.Bar(
32+
x=category_labels,
33+
y=category_counts.values,
34+
marker={"color": ["red", "blue", "green", "gray"]},
35+
text=category_counts.values,
36+
textposition="auto",
37+
)
38+
)
39+
40+
fig.update_layout(
41+
title=title,
42+
xaxis_title="Perception Category",
43+
yaxis_title="Count",
44+
template="plotly_white",
45+
)
46+
47+
# Show figure
48+
fig.write_image(i)

src/project_mbb/final/task_plot.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pandas as pd
2+
3+
from project_mbb.config import BLD
4+
from project_mbb.final.plot import plot_crime_perception
5+
6+
products = [
7+
BLD / "final" / "perception_plot_commune.png",
8+
BLD / "final" / "perception_plot_nation.png",
9+
BLD / "final" / "perception_plot_neighborhood.png",
10+
]
11+
12+
13+
def task_plot_perception(
14+
enusc_clean=BLD / "data" / "enusc_clean.pkl", produces=products
15+
):
16+
enusc_clean = pd.read_pickle(enusc_clean)
17+
plot_crime_perception(enusc_clean)

0 commit comments

Comments
 (0)