def calculate_intensity_points(self, hr, time_taken): """Calculates the intensity points for the given hr and time.""" max_hr_percent = dec((hr / self.max_hr), 2) * 100 mins = dec(time_taken.total_seconds() / 60, 2) points = dec(self.database.get_points(max_hr_percent)) total_points = points * mins return dec(total_points, 3)
def get_laps(activity_id): global calls calls += 1 for lap in client.get_activity_laps(activity_id): distance = str(lap.distance).split()[0] lap.distance_miles = dec(convert_distance(dec(distance), 'metre', 'mile')) lap.distance_km = dec(convert_distance(dec(distance), 'metre', 'km')) yield lap
def get_activities(afterdate, beforedate): global calls calls += 1 for activity in client.get_activities(after=afterdate, before=beforedate): distance = str(activity.distance).split()[0] activity.distance_miles = dec(convert_distance(dec(distance), 'metre', 'mile')) activity.distance_km = dec(convert_distance(dec(distance), 'metre', 'km')) activity.gear_name = activity_gear(activity.gear_id) yield activity
def get_laps(self, activity_id): for lap in self._client.get_activity_laps(activity_id): distance = str(lap.distance).split()[0] lap.distance_miles = dec( convert_distance(dec(distance), 'metre', 'mile')) lap.distance_km = dec( convert_distance(dec(distance), 'metre', 'km')) lap.pace_mile = calculate_pace(lap.moving_time, lap.distance_miles, 'mile') lap.pace_km = calculate_pace(lap.moving_time, lap.distance_km, 'km') lap.speed_mile = calculate_speed(lap.distance_miles, lap.moving_time) lap.speed_km = calculate_speed(lap.distance_km, lap.moving_time) yield lap
def _calculate_paces(self): """Returns the training paces for the current vdot score.""" if self.vdot_score == dec('0.00', 2): return vdot_diff = self._vdot_dec() self._calculate_training_paces(vdot_diff) self._calculate_race_times(vdot_diff) return None
def get_settings(self): """Gets the current settings from the database.""" name, dob, vdot, hr, units = (self.database.get_current_settings()) self.username = name self.dob = dob self.vdot = dec(vdot, 2) self.max_hr = hr self.units = units
def get_activities(self, before=None, after=None): for activity in self._client.get_activities(after=after, before=before): distance = str(activity.distance).split()[0] activity.distance_miles = dec( convert_distance(dec(distance), 'metre', 'mile')) activity.distance_km = dec( convert_distance(dec(distance), 'metre', 'km')) activity.pace_mile = calculate_pace(activity.moving_time, activity.distance_miles, 'mile') activity.pace_km = calculate_pace(activity.moving_time, activity.distance_km, 'km') activity.speed_mile = calculate_speed(activity.distance_miles, activity.moving_time) activity.speed_km = calculate_speed(activity.distance_km, activity.moving_time) yield activity
def calculate_vdot(self, distance, time): """Calculates the vdot for a person based on the finish time of the distance.""" time_seconds = convert_to_time(time).total_seconds() vdot, max_time, min_time = self.db.vdot_range(distance, time_seconds) vdot_distance_diff = max_time - min_time finish_distance_diff = min_time - time_seconds if vdot_distance_diff == 0: self.vdot_score = int(vdot) else: self.vdot_score = int(vdot) + dec(finish_distance_diff / vdot_distance_diff, 2, 'ROUND_FLOOR') self._calculate_paces()
def _calculate_training_paces(self, vdot_diff): """Calculates the paces based on the vdot score.""" data = list() km_range, mile_range = self._pace_ranges() ranges = len(km_range) for key in mile_range[0]._fields: if key in ('xVDOT', 'xUnit'): continue if ranges == 1: km_pace = getattr(km_range[0], key) mile_pace = getattr(km_range[0], key) else: pace_diff = getattr(mile_range[0], key) - getattr(mile_range[0], key) pace_add = int(dec(dec(pace_diff) * vdot_diff, 0)) mile_pace = getattr(mile_range[0], key) - pace_add pace_diff = getattr(km_range[0], key) - getattr(km_range[0], key) pace_add = int(dec(dec(pace_diff) * vdot_diff, 0)) km_pace = getattr(km_range[0], key) - pace_add data.append(training_pace(key[1:], mile_pace, km_pace)) self.training_paces = data
def create_step(self, workout_type, run_target, run_duration_type, run_duration, run_duration_unit): """Creates the step for a workout.""" # Create targets if run_target == 'pace': run_pace = int(self.get_paces_att(workout_type, 'Pace')) target_value_low = calculate_metres_per_sec( datetime.timedelta(seconds=run_pace), self.units) target_value_high = calculate_metres_per_sec( datetime.timedelta(seconds=run_pace - pace_add), self.units) targets = self.get_pace_targets(target_value_high, target_value_low) elif run_target == 'hr': target_value_low = dec( (dec(self.get_paces_att(workout_type, 'HRZoneLow'), 2) * dec(self.max_hr, 0)) + 100, 0, 'ROUND_FLOOR') target_value_high = dec( (dec(self.get_paces_att(workout_type, 'HRZoneHigh'), 2) * dec(self.max_hr, 0)) + 100, 0, 'ROUND_CEILING') targets = self.get_hr_targets(target_value_high, target_value_low) else: # run_target == 'none': targets = self.get_open_targets() # Create duration if run_duration_type == 'distance': if run_duration_unit != 'metre': run_duration = convert_distance(run_duration, run_duration_unit, 'metre') durations = self.get_distance_durations(run_duration) elif run_duration_type == 'time': durations = self.get_time_durations(run_duration) else: # run_duration_type == 'none': durations = self.get_open_durations() return WorkOutStep('Run', durations, targets, 0)
def _calculate_race_times(self, vdot_diff): """Calculates the race times based on the vdot score.""" data = list() vdot_score = int(self.vdot_score) vdot_df = [x for x in self.VDOT_racetimes if x.xVDOT >= vdot_score] for head in vdot_df[0]._fields: if head == 'xVDOT': continue if len(vdot_df) == 1: pace_add = 0 else: pace_diff = getattr(vdot_df[0], head) - getattr(vdot_df[1], head) pace_add = int(dec(dec(pace_diff) * vdot_diff, 0)) data.append(race_pace(head[1:], getattr(vdot_df[0], head) - pace_add)) race_distances = [x for x in self.db.get_distances() if x.Name in [y.Distance for y in data]] self.race_times = [race_time(x[0].Distance, x[0].Time, calculate_pace(datetime.timedelta(seconds=x[0].Time), x[1].Miles, 'Mile' ).total_seconds(), calculate_pace(datetime.timedelta(seconds=x[0].Time), x[1].KM, 'KM' ).total_seconds()) for x in tuple(zip(sorted(data, key=lambda x: x.Distance), sorted(race_distances, key=lambda x: x.Name)))] print(self.race_times)
def create_standard_step(self, run): """Creates a standard workout for a distance or length of time""" steps = [] workout_type, run_target, run_duration, splits, intensity = run if run_target == '': run_target = self.determine_target(workout_type) run_duration_type, run_duration_value, run_duration_unit = self.format_duration( run_duration) # Create mile/km splits if greater than 1 if run_duration_type == 'distance' and splits == 1 and run_duration_unit != 'metre': run_duration_rounded = int( dec(run_duration_value, 0, 'ROUND_FLOOR')) run_duration_remainder = dec(run_duration_value.remainder_near(1), 1, 'ROUND_UP').copy_abs() step = self.create_step(workout_type, run_target, run_duration_type, 1, run_duration_unit) steps.append(step) if run_duration_rounded > 1: # Only create a repeat if there are more than one. repeat_step = self.create_repeat_step(step.id, run_duration_rounded) steps.append(repeat_step) if run_duration_remainder > 0: remainder_step = self.create_step(workout_type, run_target, run_duration_type, run_duration_remainder, run_duration_unit) steps.append(remainder_step) else: step = self.create_step(workout_type, run_target, run_duration_type, run_duration_value, run_duration_unit) steps.append(step) self.steps += steps return steps[0].id
def add_calendar_labels(self, day, summary=None): """Adds the calendar day labels and summary .""" if summary: total_time = time_to_string( datetime.timedelta(seconds=summary.TotalTime)) total_dist = dec(summary.TotalDistance, 2) label_text = '<b>Distance</b><br>{total_dist}<br><b>Time</b><br>{total_time}'.format( total_time=total_time, total_dist=total_dist) elif 'Weekly' in day: label_text = '' else: label_text = day[-2:].lstrip('0') self.labels[day] = QtWidgets.QLabel(self.verticalLayoutWidget) self.labels[day].setObjectName(_fromUtf8("lbl" + day)) self.labels[day].setText(_translate("MainWindow", label_text, None))
def format_duration(self, duration): """Formats the duration of workout.""" if type(duration) == list: # List is for distance based duration_type = 'distance' duration_value = dec(duration[0], 2) duration_unit = duration[1] if duration_unit != 'metre' and duration_unit != self.units: duration_value = convert_distance(duration_value, duration_unit, self.units) elif duration: duration_type = 'time' duration_value = convert_to_time(duration).total_seconds() duration_unit = None else: duration_type = 'none' duration_value = None duration_unit = None return duration_type, duration_value, duration_unit
print(easy_pace / race_pace) print('CHECKING JD FORMULA\n') my_time = datetime.timedelta(hours=1, minutes=39, seconds=10) time_45 = datetime.timedelta(hours=1, minutes=40, seconds=20) time_46 = datetime.timedelta(hours=1, minutes=38, seconds=27) between_times = time_45 - time_46 my_time_diff = my_time - time_46 print(between_times, my_time_diff) print(1 - (my_time_diff / between_times)) print(between_times * 0.61) print(divmod(race_pace.seconds, 60)) print(' '.join(['{}'] * 3)) score = dec('0.26', 2) print(int(score)) print(between_times.total_seconds()) print(dec(dec(between_times.total_seconds()) * score, 0)) print(list(range(12.5)))
def _vdot_dec(self): """Returns the decimal of the current vdot score.""" return self.vdot_score - dec(self.vdot_score, 0, 'ROUND_FLOOR')
from settings.converters import dec, convert_distance, convert_to_date, convert_to_time from settings.settings import Settings import os import datetime settings = Settings() threshold_time = convert_to_time('00:18:00') threshold_pace = convert_to_time('00:07:00') rest_time = convert_to_time('00:08:00') rest_pace = convert_to_time('00:09:00') rest_distance = dec(rest_time.total_seconds() / rest_pace.total_seconds(), 2) distance = dec(threshold_time.total_seconds() / threshold_pace.total_seconds(), 2) print(distance) print(rest_distance) intervals = (1500 * 3) + (1200 * 3) #+ (800) metres = convert_distance(intervals, 'metre', 'mile') miles = dec('1.5', 1) miles += dec('1.5', 1) print(distance + miles + rest_distance) settings.WORKOUT_PLANS = 'template\\plans\\' settings.WORKOUT_TEMPLATES = 'template\\workout templates.json'
if gear_id not in shoes.keys(): shoes[gear_id] = strava_connection.get_gear(gear_id).name return shoes[gear_id] with open('strava_activities.csv', 'w', newline='') as open_file: for a in strava_connection.get_activities( after=datetime.datetime(2019, 6, 7)): a.start_date_local = a.start_date_local.replace(second=0) ref = settings.database.connection.execute( 'SELECT DiaryID, DiaryDate FROM Diary WHERE DiaryDate = ?', (a.start_date_local, )).fetchone() if ref: cursor = settings.database.connection.cursor() cursor.execute('UPDATE Diary SET StravaID = ? WHERE DiaryID = ?;', (a.id, ref[0])) settings.database.connection.commit() elif a.start_date_local > datetime.datetime(2019, 6, 7): settings.database.add_diary_entry( (None, a.start_date_local, a.elapsed_time.total_seconds(), 0, str(dec(a.distance_miles, 2)), str(dec(a.distance_km, 2)), str(a.speed_mile), str(a.speed_km), a.pace_mile.total_seconds(), a.pace_km.total_seconds(), a.average_heartrate, 0, None, None, None, None, a.id, None, None, shoes.setdefault(a.gear_id, default_shoe(a.gear_id)), False)) #writer.writerow([a.start_date_local, a.elapsed_time, a.workout_type, a.distance_miles, a.distance_km, # a.speed_mile, a.speed_km, a.pace_mile, a.pace_km, a.average_heartrate, a.gear_id, # shoes.setdefault(a.gear_id, default_shoe(a.gear_id)), a.id, a.description])
from collections import OrderedDict from settings.converters import convert_to_time, calculate_pace, time_to_string, convert_distance, dec distances = { '1,500': [dec('1.5', 1), convert_distance(dec('1.5', 1), 'km', 'mile')], 'Mile': [convert_distance(dec('1', 0), 'mile', 'km'), dec('1', 0)], '3,000': [dec('3', 0), convert_distance(dec('3', 0), 'km', 'mile')], '2mile': [convert_distance(dec('2', 0), 'mile', 'km'), dec('2', 0)], '5,000': [dec('5', 0), convert_distance(dec('5', 0), 'km', 'mile')], '10K': [dec('10', 0), convert_distance(dec('10', 0), 'km', 'mile')], '15K': [dec('15', 0), convert_distance(dec('15', 0), 'km', 'mile')], '10Mile': [convert_distance(dec('10', 2), 'mile', 'km'), dec('10', 0)], 'HalfMarathon': [convert_distance(dec('13.11', 2), 'mile', 'km'), dec('13.11', 2)], 'Marathon': [convert_distance(dec('26.22', 2), 'mile', 'km'), dec('26.22', 2)] } def get_vdot_race_paces(file): """Extracts the race paces from the VDOT Races file.""" vdot = OrderedDict() headers = [] with open(file, 'r') as VDOT_file: for line in VDOT_file.readlines(): cells = line.split() # Ignore last column as this is repeated. if cells[0] == 'VDOT': headers += cells[1:] else: cell_times = [x for x in cells[1:]] vdot[int(cells[0])] = dict(zip(headers, cell_times)) return vdot
#settings.update_settings('Name', 'Paul Lucas') #settings.update_settings('DateOfBirth', datetime.datetime(year=1987, month=6, day=26)) print('\nTraining') for k, m, km in VDOT_values.training_paces: print(k.ljust(15), datetime.timedelta(seconds=m), datetime.timedelta(seconds=km)) print('\nRaces') print(VDOT_values.race_times) #print(VDOT_values.VDOT_paces) print(str(VDOT_values.vdot_score)) print(VDOT_values.vdot_score - dec(VDOT_values.vdot_score, 0, 'ROUND_FLOOR')) VDOT_values.save_vdot() settings.get_zones() print('SETTINGS') for r in db.connection.execute(""" SELECT COALESCE(VDOTHistory.VDOT, 0) AS VDOT, MaxHR Units, DateOfBirth FROM Settings LEFT JOIN VDOTHistory ON Settings.VDOTHistoryID = VDOTHistory.VDOTHistoryID; """):