<jupyter_start><jupyter_text>Crashes by WeekLet's start by looking at the number of incidents per month:<jupyter_code>query = (
    "SELECT Collision_Date "
    "FROM Collision AS C "
    "WHERE Collision_Date IS NOT NULL "
    "AND Collision_Date <= '2015-12-31' "  # 2016 is incomplete
    "AND Motorcycle_Collision == 1 "
)

df = run_query(query)
# Number of accidents
len(df)
DATE_COL = "Collision_Date"
CRASH_COL = "Crashes"

df["DOY"]     = df[DATE_COL].dt.dayofyear
df["DOW"]     = df[DATE_COL].dt.dayofweek
df["Day"]     = df[DATE_COL].dt.day
df["Week"]    = df[DATE_COL].dt.week
df["Month"]   = df[DATE_COL].dt.month
df["Year"]    = df[DATE_COL].dt.year
df[CRASH_COL] = 1
# Convert to a timeseries
ts = df[[DATE_COL]]
ts.index= ts[DATE_COL]
ax = ts.resample('W-MON').count()[DATE_COL].plot(
    kind="line",
    title='Crashes in California',
    figsize=(12,6),
    linewidth=1,
    color=LINE_COLOR,
)

# Set Y range and grid
#ax.set_ylim([5000, 13000])
ax.grid()

# Set axis labels
plt.title('Motorcycle Crashes per Week in California', y=1.03, size=28)
FONTSIZE="xx-large"
plt.xlabel("")
plt.ylabel("Crashes / Week", fontsize=FONTSIZE)

# Set the year between the tick marks using minor ticks
# Pandas uses a obscure Axis labeling system, so to put the ticks between the
# major ticks, we interpolate it using np.interp().
y = ax.get_xticks()
x = [2001] + list(range(2002, 2018, 2))  # Step of 2 means every other year
minor_x = np.interp(np.arange(2001.5, 2016.5, 2), x, y)
ax.set_xticks(minor_x, minor=True)
ax.set_xticklabels(np.arange(2001, 2016, 2), minor=True, size=14)
ax.set_xticks([])  # Unset major ticks

# Add shaded bands for every other year
for year in range(2002, 2017, 2):
    ax.axvspan(pd.to_datetime(str(year)), pd.to_datetime(str(year+1)), color="black", alpha=0.05)

for ext in ("png", "svg"):
    plt.savefig("/tmp/motorcycle_accidents_per_week_in_california.{ext}".format(ext=ext), bbox_inches="tight")

plt.show()<jupyter_output><empty_output><jupyter_text>Grouped by Day of the Year<jupyter_code>from datetime import datetime

def annotate_year(df, ax, month, day, text, xytext, adjust=(0, 0), arrowstyle="->"):
    """ Draw an annotation on the Day of Year plot. """
    # Use 2015 because it is a non-leapyear, and most years are non-leap year
    doy = datetime(year=2015, month=month, day=day).timetuple().tm_yday
    y_pos = df[CRASH_COL][month][day]

    ax.annotate(
        text,
        (doy+adjust[0], y_pos+adjust[1]),
        xytext=xytext,
        textcoords='offset points',
        arrowprops=dict(
            arrowstyle=arrowstyle,
            connectionstyle="arc3",
        ),
        size=16,
    )
# Get the start locations of each month
def month_starts(df):
    """ Get the start and midpoints of each month. """
    # Month starts
    majors = []
    for x, (month, day) in enumerate(df.index):
        if day == 1:
            majors.append(x)
        if month == 12 and day == 31:
            majors.append(x)

    # Midpoints
    minors = []
    for i in range(len(majors)-1):
        end = majors[i+1]
        start = majors[i]
        x = start + (end-start)/2.
        minors.append(x)

    return (majors, minors)
# Calculate crashes per day
mean_crashes = df.groupby(["Month", "Day"]).count()
mean_crashes[CRASH_COL] /= 15  # Average instead of sum of years
mean_crashes[CRASH_COL][2][29] = mean_crashes[CRASH_COL][2][29] * 15/3.  # Only 3 leap years!

ax = mean_crashes[CRASH_COL].plot(
    kind="line",
    linewidth=1.8,
    figsize=(12,6),
    color=LINE_COLOR,
)

# Set Y Range and grid
#ax.set_ylim([700, 1600])
ax.set_xlim([-2, 367])  # Avoid hiding the start/end points
ax.set_ylim([10, 55])  # Avoid hiding the start/end points
ax.grid()

# Set axis labels
plt.title('Mean Motorcycle Crashes by Date (2001–2015)', y=1.03, size=28)
FONTSIZE="xx-large"
plt.xlabel('')
plt.ylabel("Crashes", fontsize=FONTSIZE)

# Fix the X tick labels
(major_x, minor_x) = month_starts(mean_crashes)
labels = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
ax.set_xticks(major_x[:-1])
ax.set_xticklabels([])
ax.set_xticks(minor_x, minor=True)
ax.set_xticklabels(labels, minor=True, size=11)

# Shade every other month
for i in range(0, len(major_x)-1, 2):
    start = major_x[i+1]
    end = major_x[i+2]
    ax.axvspan(start, end, color="black", alpha=0.03)

# Federal Holidays
annotate_year(mean_crashes, ax, 1, 1, "New Years", (+5, -40), (-2, 0))
annotate_year(mean_crashes, ax, 1, 18, "MLK", (-17, 25))
annotate_year(mean_crashes, ax, 2, 18, "Washington's Birthday", (-79.5, -22), (0, -4.5), arrowstyle="-[")
annotate_year(mean_crashes, ax, 5, 28, "Memorial Day", (-49.5, -30), (0, -1), arrowstyle="-[")
annotate_year(mean_crashes, ax, 7, 3, "3rd of July", (-75, +20))
annotate_year(mean_crashes, ax, 7, 4, "4th of July", (+7, +40))
annotate_year(mean_crashes, ax, 9, 4, "Labor Day", (-37, -35), (0, -2), arrowstyle="-[")
annotate_year(mean_crashes, ax, 10, 11, "Columbus Day", (-52, +30), (0, +7), arrowstyle="-[")
annotate_year(mean_crashes, ax, 11, 11, "Veterans Day", (-80, -25))
annotate_year(mean_crashes, ax, 11, 25, "Thanksgiving", (-47.5, -32), (0, -3), arrowstyle="-[")
annotate_year(mean_crashes, ax, 12, 25, "Christmas", (-90, -0))

# Other Holidays
annotate_year(mean_crashes, ax, 10, 31, "Halloween", (+15, +35))
annotate_year(mean_crashes, ax, 2, 14, "Valentine's Day", (-55, 50))
annotate_year(mean_crashes, ax, 3, 17, "St. Patrick's Day", (-40, 60))

for ext in ("png", "svg"):
    plt.savefig("/tmp/mean_motorcycle_accidents_by_date.{ext}".format(ext=ext), bbox_inches="tight")

plt.show()<jupyter_output><empty_output><jupyter_text>Weekends<jupyter_code>DOW_COL = "DOW"

winter_str = "November to April"
summer_str = "May to October"

df["Season"] = [summer_str if 5 <= month <= 10 else winter_str for month in df["Month"]]
def make_violin_plot(df, colors, season, ax):

    # Set plot size
    df_temp = df[df["Season"] == season]
    gr = df_temp.groupby(["Collision_Date", "DOW", "Season"]).count()
    gr = gr.reset_index()

    ax = sns.violinplot(x="DOW", y="Crashes", data=gr, palette=colors, linewidth=2, inner="quartile", cut=0, scale="count", ax=ax)
    plt.xticks(np.arange(7), ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], size=14)
    ax.yaxis.label.set_visible(False)
    ax.xaxis.label.set_visible(False)
    ax.set_ylim([-3, 103])
f, axs = plt.subplots(2, sharex=True, sharey=True, figsize=(12, 8))
f.tight_layout(rect=[0, 0.03, 1, 0.95])  # Used to keep the suptitle() above the plot
f.subplots_adjust(hspace=0)

FONTSIZE="xx-large"
f.text(-0.02, 0.5, "Crashes", va="center", rotation="vertical", fontsize=FONTSIZE)
f.text(0.045, 0.88, "November–April", fontsize=22)
f.text(0.045, 0.46, "May–October", fontsize=22)
f.suptitle("Motorcycle Crashes by Day of the Week", fontsize=28)

# Winter plot
axs[0] = make_violin_plot(df, cm.winter(np.linspace(0.2, 1, 7)), winter_str, axs[0])
# Summer plot
axs[1] = make_violin_plot(df, cm.inferno(np.linspace(0.3, 0.9, 7)), summer_str, axs[1])

for ext in ("png", "svg"):
    f.savefig("/tmp/motorcycle_accidents_by_day_of_the_week_and_season.{ext}".format(ext=ext), bbox_inches="tight")<jupyter_output><empty_output><jupyter_text>Exploring the Leap Day Bump<jupyter_code>query = (
    "SELECT * "
    "FROM Collision AS C "
    "WHERE Collision_Date IS NOT NULL "
    "AND Collision_Date <= '2015-12-31' "  # 2016 is incomplete
    "AND Motorcycle_Collision == 1 "
    "AND ("
        "Collision_Date == '2004-02-29' OR "
        "Collision_Date == '2008-02-29' OR "
        "Collision_Date == '2012-02-29' "
    ")"
)

leap_day_df = run_query(query)
DATE_COL = "Collision_Date"
CRASH_COL = "Crashes"

leap_day_df["DOY"]     = leap_day_df[DATE_COL].dt.dayofyear
leap_day_df["DOW"]     = leap_day_df[DATE_COL].dt.dayofweek
leap_day_df["Day"]     = leap_day_df[DATE_COL].dt.day
leap_day_df["Week"]    = leap_day_df[DATE_COL].dt.week
leap_day_df["Month"]   = leap_day_df[DATE_COL].dt.month
leap_day_df["Year"]    = leap_day_df[DATE_COL].dt.year
leap_day_df[CRASH_COL] = 1
leap_day_df.groupby(["Year"])[CRASH_COL].count()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1709787649499634, 'fraction_numerical': 0.045765194044422745, 'mean_word_length': 4.412813738441216, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '499248', 'license': 'MIT', 'n_tokens_mistral': 3518, 'n_tokens_neox': 3217, 'n_words': 729, 'path': 'files/switrs-motorcycle-accidents-by-date/SWITRS Crash Dates With Motorcycles.ipynb', 'repo_name': 'agude/agude.github.io', 'size': '8194'}