<jupyter_start><jupyter_text>Convert time to minutesFrom: https://stackoverflow.com/questions/48447123/convert-time-hhmmss-to-minutes-in-python/48447482<jupyter_code>#Function to convert time to minutes
def convert_to_minutes(my_time):
  factors = (60, 1, 1/60)

  t1 = sum(i*j for i, j in zip(map(int, my_time.split(':')), factors))
  return t1<jupyter_output><empty_output><jupyter_text>Analysis FunctionReads in a specific year of results, returns a pandas dataframe of finisher data (including minutes elapsed)<jupyter_code>### Finds a specific year of results, removes non-finishers, calculates time in minutes ###
def analyze_year(year):
  #Read in files
  fp = "/content/gdrive/Shared drives/ELIFE/People/Malloy, John/Random/Black Canyon/BC" + str(year) + "_100k.p"
  df = pickle.load(open(fp, "rb"))

  #Drop the "Did not finish" line
  df = df.drop(df.loc[df["State"] == "Did"].index[0])

  #Create a minutes field to sum up every time - contingent upon convert_to_minutes() function
  df["Minutes"] = df["Time"].apply(convert_to_minutes)
  
  return df[df["Minutes"] > 0]
### Finds the percentiles of each finisher using the np.floor function to round all percentiles down ###
def find_percentiles(year):
  #Find specific data for a specific year, filter out non-finishers
  df = analyze_year(year)

  #Find percentiles, round down for each one (using np.floor)
  percentiles = df["Minutes"].apply(lambda x: stats.percentileofscore(df["Minutes"], x))
  df["Percentiles"] = percentiles
  df["Percentiles"] = df["Percentiles"].apply(np.floor)
  
  #print(df)
  return df<jupyter_output><empty_output><jupyter_text>Function testingTesting functions to ensure correct performance<jupyter_code>find_percentiles(2021)<jupyter_output>State Age Gender   GP      Time   Rank      Minutes  Percentiles
0      AZ  31      M    2   8:10:54  92.07   490.900000          0.0
1      AZ  33      M    3   8:16:23  84.48   496.383333          0.0
2      WY  28      M    4   8:34:49  90.15   514.816667          0.0
3      MN  26      M    5   8:36:28  87.77   516.466667          1.0
4      CA  36      M    6   8:36:37  97.15   516.616667          1.0
..    ...  ..    ...  ...       ...    ...          ...          ...
359    UT  43      F   99  19:52:48  69.05  1192.800000         98.0
360    CA  44      M  263  19:54:24   50.3  1194.400000         99.0
361    AZ  46      M  264  19:56:47  58.66  1196.783333         99.0
389    CA  60      F    0  20:09:38  62.78  1209.633333         99.0
409    CA  53      F    0  20:13:41  59.95  1213.683333        100.0

[364 rows x 8 columns]<jupyter_text>Plotting<jupyter_code>#Distribution of minutes across all finishers (normalized based on the number of finishers)
fig = plt.figure()
for year in [2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021]:
  df = analyze_year(year)
  
  plt.scatter(x=np.linspace(0,1,len(df)), y=df["Minutes"], label=str(year) + " BC 100k", alpha=0.5, s=10, marker='x')
  plt.xlabel("Finishing percentile")
  plt.ylabel("Minutes")
# plt.gca().spines("top").set_visible(False)
# plt.gca().spines("right").set_visible(False)
plt.legend()
plt.show()
# Percentile distribution of finishers, average minutes for each percentile on y-axis
fig = plt.figure()
for year in [2020, 2021]:
  df = find_percentiles(year)
  #Find average number of minutes for each percentile (only relevant to this graph)
  df = df.groupby("Percentiles", as_index=False)["Minutes"].mean()
  plt.scatter(x=df["Percentiles"], y=df["Minutes"], alpha=0.5, s=10)
#Histogram of finishers in each percentile
fig = plt.figure(figsize=(10,10))

#Set colors to normalized temperature values 
years = [2014, 2016, 2017, 2018, 2019, 2020, 2021]
temps = [86, 82, 84, 66, 72, 63, 72, 72] #temperatures from timeanddate.com/weather/@5285131
normalized_temps = minmax_scale(list(temps))
colors = dict(zip(years, normalized_temps))
temperatures = dict(zip(years, temps))

for year in years:
  df = find_percentiles(year)
  #df["Minutes"].hist(alpha=0.7, label=year, grid=False, bins=30, stacked=True, density=True)
  kwargs = dict(hist_kws={'alpha':0.5}, kde_kws={'linewidth':3})
  sns.distplot(df["Minutes"], **kwargs, bins=20, label=str(year) + ", Temp=" + str(temperatures[year]), norm_hist=True, color = plt.cm.coolwarm(colors[year]))
  
  #Play around with y-axes values - currently set to invisible
  plt.yticks([])


plt.xlabel("Minutes")
plt.ylabel("Density")
plt.xlim(0, 1500)
plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13435183104920242, 'fraction_numerical': 0.09638283531790609, 'mean_word_length': 3.506072874493927, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '153223', 'license': 'MIT', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1575, 'n_words': 419, 'path': 'BC_analysis.ipynb', 'repo_name': 'jfmalloy1/BlackCanyonUltra', 'size': '4451'}