diff --git a/DataAnalyser.py b/DataAnalyser.py index fa89798d5f02cda1269a01b35845e5cdcd38de2e..8deb6390d91424481309e755a0460da700c1d9fe 100644 --- a/DataAnalyser.py +++ b/DataAnalyser.py @@ -4,6 +4,7 @@ import string import pandas as pandas from fastf1.core import Session, Lap, Laps, DataNotLoadedError from bs4 import BeautifulSoup +from fastf1.events import Event from DataHandler import DataHandler @@ -145,7 +146,27 @@ class DataAnalyser(DataHandler): raise KeyError("No weather entry found") - def filterForRainSessions(self, sessions: list[Session]): + def filterForWetEvents(self, events: list[Event]): + """ + Filter out & return only those events from input list that had wet conditions in any notable capacity according + to their respective wikipedia pages. + :param sessions: List of sessions from which to pick out sessions with rain. + :return: List of sessions which had rain falling during the session for any amount of time. + """ + from DataImporter import DataImporter # here due to circular dependency + + wetEvents: list[Event] = [] + importer = DataImporter() + for event in events: + print(f"Filtering {event.year} {event["EventName"]}") + weatherDescription: string = importer.fetchWeather(event) + if self.isWet(weatherDescription): wetEvents.append(event) + + return wetEvents + + # TODO: Rename (other than legacy) + @DeprecationWarning + def filterForRainSessionsLEGACY(self, sessions: list[Session]): """ Filter out & return only those sessions from input list that had rain falling at any point during the session. diff --git a/DataImporter.py b/DataImporter.py index 6220d4c48908b0a93aefced55c6930915a29aa0c..840d6fd57e98d5357b8fadbdd51aac5da889f5e0 100644 --- a/DataImporter.py +++ b/DataImporter.py @@ -100,18 +100,20 @@ class DataImporter(DataHandler, ABC): session.load(laps = laps, telemetry = telemetry, weather = weather, messages = messages) return session - def getRainRacesSince(self, firstYear: int): + def getWetEventsSince(self, firstYear: int): currentYear = datetime.now().year if firstYear > currentYear: raise ValueError("Cannot get race data from the future :)") rainRaces: list[Session] = [] for firstYear in range(firstYear, currentYear): # FIXME: Handle exception after new years, when no events have run in current year yet - wetWeatherRacesInYear: list[Session] = self.getRainRacesIn(firstYear) + wetWeatherRacesInYear: list[Session] = self.getWetEventsIn(firstYear) for wetWeatherRace in wetWeatherRacesInYear: rainRaces.append(wetWeatherRace) return rainRaces + # TODO: Rename (other than legacy) + @DeprecationWarning def getRainRacesIn(self, year: int): events: list[Event] = self.importAllEventsFromYear(year) races: list[Session] = [] @@ -120,10 +122,20 @@ class DataImporter(DataHandler, ABC): try: raceSession: Session = self.importSessionWeather(raceIdentifier) races.append(raceSession) - except ValueError: # Needed as long as weather data for races before 2018 hasn't been supplemented + except ValueError: # Needed as long as weather data for races before 2018 hasn't been supplemented print(f"Weather for {event["EventName"]} {event.year} could not be determined") analyser: DataAnalyser = DataAnalyser() - rainRaces: list[Session] = analyser.filterForRainSessions(races) + rainRaces: list[Session] = analyser.filterForRainSessionsLEGACY(races) + + return rainRaces + + def getWetEventsIn(self, year: int): + print(f"Fetching wet events in {year}") + + events: list[Event] = self.importAllEventsFromYear(year) + analyser: DataAnalyser = DataAnalyser() + rainRaces: list[Session] = analyser.filterForWetEvents(events) + + return rainRaces - return rainRaces \ No newline at end of file diff --git a/main.py b/main.py index 08cf9d17c7fbf62feb692ca3efbef377bfb8fbcf..c47c20c53551f7c3f4c099c7bd9db1bb9f9b69b4 100644 --- a/main.py +++ b/main.py @@ -31,9 +31,9 @@ class Main: #dataHandler = Main.DataHandlingPackage() #dataHandler.plotter.plotBackgroundPaintTest() - self.testNewRainRaceFetching() + #self.testNewRainRaceFetching() - #self.printRainRaces() + self.printWetEvents() #self.overtakeAnalysis(racesToAnalyse) @@ -45,13 +45,14 @@ class Main: isWet: bool = dataHandler.analyser.isWet(w) print(isWet) - def printRainRaces(self): + def printWetEvents(self): dataHandler: Main.DataHandlingPackage = Main.DataHandlingPackage() year: int = 2015 - rainRaces: list[Session] = dataHandler.importer.getRainRacesSince(year) - print(f"Rain races since {max(year, 2018)}") - for rainRace in rainRaces: - print(rainRace) + wetEvents: list[Event] = dataHandler.importer.getWetEventsSince(year) + print(f"Wet races since {year} (according to wikipedia):") + for wetEvent in wetEvents: + print(f"{wetEvent.year} {wetEvent["EventName"]}") + def overtakeAnalysis(self, raceSessionIdentifiers: list[SessionIdentifier]): dataHandler: Main.DataHandlingPackage = self.DataHandlingPackage()