Skip to content
Snippets Groups Projects
Commit ad9db0e9 authored by Lennard Geese's avatar Lennard Geese
Browse files

Improve import function to support any session type and support specifying...

Improve import function to support any session type and support specifying what data should be imported
parent 6349d572
No related branches found
No related tags found
No related merge requests found
......@@ -29,6 +29,30 @@ class DataAnalyser(DataHandler):
overtakes.append(self.countOvertakesInLap(lapNumber, race))
return overtakes
def countOvertakesInLap(self, lapNumber: int, race: Session):
orderToSort: list[(Lap, int)] = self.prepareWeightedOrderFromLap(lapNumber, race)
overtakes: int = 0
i: int = 0
while i < len(orderToSort) - 1: # do not change to for-loop, seems to not like resetting the index
weightedDriverAhead: list[(Lap, int)] = orderToSort[i]
weightedDriverBehind: list[(Lap, int)] = orderToSort[i + 1]
if weightedDriverAhead[1] > weightedDriverBehind[1]:
temp: int = orderToSort[i]
orderToSort[i] = orderToSort[i + 1]
orderToSort[i + 1] = temp
i = -1 # reset to first index; -1 because loop will set it to 0
if not ( # don't count overtake if driver nonexistent or if one of them is on an in-lap
weightedDriverAhead[0] is None
or weightedDriverBehind[0] is None
or self.isAnInLap(weightedDriverAhead[0])
or self.isAnInLap(weightedDriverBehind[0])
): overtakes += 1
i += 1
return overtakes
def prepareWeightedOrderFromLap(self, lapNumber: int, race: Session):
'''Prepare a list from specific lap & race, that can be sorted via bubble sort to determine the number of
overtakes that occured in that lap.
......@@ -58,7 +82,7 @@ class DataAnalyser(DataHandler):
try:
if lapNumber == 1:
startPosition = self.getGridPositionFor(driver, race)
startPosition = self.getGridPositionForDriver(driver, race)
else: startPosition = int(driversPreviousLap['Position'].iloc[0])
endPosition = int(driversCurrentLap['Position'].iloc[0])
......@@ -74,31 +98,7 @@ class DataAnalyser(DataHandler):
return weightedOrder
def countOvertakesInLap(self, lapNumber: int, race: Session):
orderToSort: list[(Lap, int)] = self.prepareWeightedOrderFromLap(lapNumber, race)
overtakes: int = 0
i: int = 0
while i < len(orderToSort) - 1: # do not change to for-loop, seems to not like resetting the index
weightedDriverAhead: list[(Lap, int)] = orderToSort[i]
weightedDriverBehind: list[(Lap, int)] = orderToSort[i + 1]
if weightedDriverAhead[1] > weightedDriverBehind[1]:
temp: int = orderToSort[i]
orderToSort[i] = orderToSort[i + 1]
orderToSort[i + 1] = temp
i = -1 # reset to first index; -1 because loop will set it to 0
if not ( # don't count overtake if driver nonexistent or if one of them is on an in-lap
weightedDriverAhead[0] is None
or weightedDriverBehind[0] is None
or self.isAnInLap(weightedDriverAhead[0])
or self.isAnInLap(weightedDriverBehind[0])
): overtakes += 1
i += 1
return overtakes
def getGridPositionFor(self, driverNumber: int, race: Session):
def getGridPositionForDriver(self, driverNumber: int, race: Session):
sessionResults = race.results
gridPosition: int = int(sessionResults['GridPosition'].loc[driverNumber])
return gridPosition
......
......@@ -22,16 +22,19 @@ class DataImporter(DataHandler, ABC):
return races
def importRaceWeather(self):
x = 0
def importRaceSessions(self, races):
def importRaceSessions(self, races: list[Session], laps = True, telemetry = False, weather = True, messages = False):
sessions: list[Session] = []
for race in races:
sessions.append(self.importRaceSession(race))
for raceSession in races:
sessions.append(self.importSession(raceSession, "R", laps, weather, messages, telemetry))
return sessions
def importRaceSession(self, race):
season, raceIndex = race
session = fastf1.get_session(season, raceIndex, 'R')
session.load(laps=True, weather=True, messages=False, telemetry=False)
def importSession(self, session: Session, sessionType: str, laps = True, telemetry = False, weather = True, messages = False):
season, raceIndex = session
session = fastf1.get_session(season, raceIndex, sessionType)
session.load(laps = laps, telemetry = telemetry, weather = weather, messages = messages)
return session
\ No newline at end of file
......@@ -43,7 +43,7 @@ class Main:
# Import
raceSession = dataHandler.importer.importRaceSession(race_index)
raceSession = dataHandler.importer.importSession(race_index, "R")
raceName: str = f"{raceSession.event['EventName']} {raceSession.event.year}"
print("Import done")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment