diff --git a/DataAnalyser.py b/DataAnalyser.py index 641614522dd13d78c9e9ba49ed07dc7f60e65be2..7439a00cda7e577a2e01acbf770cbf07a6d39bc3 100644 --- a/DataAnalyser.py +++ b/DataAnalyser.py @@ -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 diff --git a/DataImporter.py b/DataImporter.py index a21d59e0714e4fd920062bafcbf251c27f9cc0db..5d4d413985268fa05a806bde3c9d104808e162df 100644 --- a/DataImporter.py +++ b/DataImporter.py @@ -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 diff --git a/main.py b/main.py index 9b334e5b44df6e3c8ce0c650afb59903537df8fb..ad9b05c64586ca0a7fac52d682d7fc27a105060c 100644 --- a/main.py +++ b/main.py @@ -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")