-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.py
More file actions
246 lines (193 loc) · 8.18 KB
/
Copy pathutility.py
File metadata and controls
246 lines (193 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
''' ====================================================================
Utility script for myclimbs.
Functions:
readClimbs() - Function to read CSV climbing log
plotDistanceBySession() - Generates a graph showing distance by
session
plotVSumsBySession() - Generates a graph showing VSums by session
===================================================================='''
import re
import csv
import session
import climb
import exceptions
import math
import matplotlib.pyplot as plt
import numpy as np
''' A function used to read and parse a climbing log csv (template)
for myclimbs.
In:
dateRange (year)
climbTrackerFilePath - The path to the csv file
Out:
A list of sessions.
'''
def readClimbs(dateRange, climbTrackerFilePath):
sessions = []
thisClimb = ()
thisSession = ()
thisSessionDate = ""
lastSessionDate = ""
with open(climbTrackerFilePath) as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=',')
for row in csv_reader:
if dateRange in row["Date"]:
thisSessionDate = row["Date"]
# If session ended last iteration, add it and create a new one
if thisSessionDate != lastSessionDate:
if lastSessionDate != "":
sessions.append(thisSession)
# Append session for first case and when date changes
thisSession = session.Session(thisSessionDate)
# Construct climb object based on type
if row["Type"] == "B":
thisClimb = climb.Boulder(row["Grade"], row["# RP Tries"],
row["Type"], row["Length"],
row["Laps"])
thisSession.addBoulder(thisClimb)
# TODO need to finish winter implementation, ran out of LOCs
if row["Type"] == "W":
pass
# Construct Winter Climb
else:
thisClimb = climb.Climb(row["Grade"], row["# RP Tries"],
row["Type"], row["Length"],
row["Laps"])
thisSession.addClimb(thisClimb)
lastSessionDate = row["Date"]
# Add the last session
# try:
sessions.append(thisSession)
# except NoClimbsInPeriodError:
# print("There aren't any climbs/sessions logged in ", dateRange)
return sessions
''' Function to plot distance by session date.
In:
sessions - list of session type'''
def plotDistanceBySession(sessions):
dates = []
distances = []
maxSessionDistance = 0
fig = plt.figure(dpi=150) # 150 DPI equal "good enough" size
# The next line is very important. Without it, axis labels don't show.
ax = fig.add_subplot(111)
for session in sessions:
dates.append(session.getSessionDate())
distances.append(session.getSessionDistance())
if session.getSessionDistance() > maxSessionDistance:
maxSessionDistance = session.getSessionDistance()
# Have to get data ordered (by dates) from first of year to EOY
dates.reverse()
distances.reverse()
ax.set_title('Total Vertical Distance Climbed by Session Date')
ax.set_ylabel('Session Distances (Feet)')
ax.set_yticks(np.arange(0, maxSessionDistance, 40))
ax.set_xlabel('Session Dates')
ax.set_xticklabels(dates, rotation=90)
ax.bar(dates, distances)
fig.tight_layout()
plt.show()
''' Function to plot VSums by session date.
In:
sessions - list of session type'''
def plotVSumsBySession(sessions):
dates = []
vsums = []
maxSessionVSum = 0
fig = plt.figure(dpi=150) # 150 DPI equal "good enough" size
# The next line is very important. Without it, axis labels don't show.
ax = fig.add_subplot(111)
for session in sessions:
if "B" in session.getSessionType():
if session.getSessionVSum() > 0:
dates.append(session.getSessionDate())
vsums.append(session.getSessionVSum())
if session.getSessionVSum() > maxSessionVSum:
maxSessionVSum += session.getSessionVSum()
# Have to get data ordered (by dates) from first of year to EOY
dates.reverse()
vsums.reverse()
ax.set_title('VSums by Session Date')
ax.set_ylabel('VSum')
ax.set_yticks(np.arange(0, maxSessionVSum, 5))
ax.set_xlabel('Session Date')
ax.set_xticklabels(dates, rotation=90)
ax.bar(dates, vsums)
fig.tight_layout()
plt.show()
''' Function to plot grade pyramid for bouldering and summer roped climbs.
In:
sessions - list of session type
'''
def plotClimbPyramids(sessions):
boulderGradeCount = {}
summerRopedGradeCount = {}
for session in sessions:
sessionGradeCount = session.getSessionGradeCount()
# print(session.getSessionType())
# if session.
for grade, gradecount in sessionGradeCount.items():
# If this is a bouldering session
# if session.getSessionVSum() > 0:
if "V" in grade and "x" in grade:
# Strip x out of grade and build dict
grade = grade.replace("x", "")
if grade in boulderGradeCount.keys():
boulderGradeCount[grade] += gradecount
else:
boulderGradeCount[grade] = gradecount
# TODO need to finish winter implementation, ran out of LOCs
# If this is a summer roped session
else:
if "5." in grade and "x" in grade:
# Strip x out of grade and build dict
grade = grade.replace("x", "")
if grade in summerRopedGradeCount.keys():
summerRopedGradeCount[grade] += gradecount
else:
summerRopedGradeCount[grade] = gradecount
# Call the plotting function
plotPyramid(boulderGradeCount, "Bouldering Pyramid")
plotPyramid(summerRopedGradeCount, "Summer Roped Pyramid")
''' A generic pyramid plot function used for all session types
Input:
gradeCount dict
title for graph
Output:
pyramid bar chart
'''
def plotPyramid(gradeCount, title):
maxCount = max(gradeCount.values())
gradeCountSorted = {}
fig = plt.figure(dpi=150) # 150 DPI equal "good enough" size
# The next line is very important. Without it, axis labels don't show.
ax = fig.add_subplot(111)
# gradeCountSorted = OrderedDict(sorted(gradeCount.items()))
# This is a super hacky way of performing a natural sort without natsort
gradeCountSortedKeyList = natural_sort(gradeCount.keys())
for key in gradeCountSortedKeyList:
gradeCountSorted[key] = gradeCount[key]
# Dynamically generate y tick spacing base on maxCount
ytickStep = int(math.ceil(maxCount / 10))
# Generate plot
ax.set_title(title)
ax.set_ylabel('Count')
ax.set_yticks(np.arange(0, maxCount, ytickStep))
ax.set_xlabel('Grade')
ax.set_xticklabels(list(gradeCountSorted.keys()))
ax.bar(list(gradeCountSorted.keys()), list(gradeCountSorted.values()),
align='center')
fig.tight_layout()
plt.show()
''' A function to sort string in natural order.
This is used to handle tricky problem of sorting Yosemite climbing
grades.
From:
https://stackoverflow.com/questions/4836710/is-there-a-built-in- +
function-for-string-natural-sort
Since we could not use third party utils like natsort.
'''
def natural_sort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)