def plot_for_all_files(play_by_play_directory):
    for file in os.listdir(play_by_play_directory):
        try:
            id = get_game_id(play_by_play_directory + '/' + file)
            id = id[2:12]
            eventid = airball_list.get(id[2:])[0][0]
            period = get_period(play_by_play_directory + '/' + file, eventid)
            time_remaining = get_time_remaining_at_eventid(
                play_by_play_directory + '/' + file, eventid)
            events_after_5 = get_events_after_5_mins.output_list_for_5_mins_after_airball(
                file)
            events_before = get_all_events_before_airball.output_list_for_before_airball(
                file)
            date = file[1:11]
            plot_after_airball(date, events_after_5, period, time_remaining)
            plot_before_airball(date, events_before, period, time_remaining)
            print('Plotted events before and after airball for file: ' + file)
        except:
            continue
def get_events_after_5_mins(filename, airball_list, game_ids,
                            play_by_play_directory, list_of_lists):
    count = 0
    temp_game_id = get_game_id(play_by_play_directory + '/' + filename, )[2:12]
    if temp_game_id in game_ids:
        event_ids = []
        for set in airball_list.get(temp_game_id[2:]):
            event_ids.append([set[0], set[2]])

        for arr in event_ids:
            period = get_period(play_by_play_directory + '/' + filename,
                                arr[0])
            time_remaining = get_time_remaining_at_eventid(
                play_by_play_directory + '/' + filename, arr[0])

            try:
                edited_time = time_remaining[3:]
                begin_time = int(edited_time[0:2])
                if begin_time > 5:
                    end_time = begin_time - 5
                    final_time = '00:0' + str(end_time) + ':' + edited_time[3:]
                    list_of_plays = get_plays_within_times(
                        play_by_play_directory + '/' + filename, arr[1],
                        time_remaining, final_time, period, [])
                    list_of_lists.append(list_of_plays)
                    #print(list_of_plays)
                    count += 1
                else:
                    list_of_plays = get_plays_within_times(
                        play_by_play_directory + '/' + filename, arr[1],
                        time_remaining, "00:00:01", period, [])
                    list_of_lists.append(list_of_plays)
                    #print(list_of_plays)
                    count += 1
            except:
                continue
        event_ids.clear()
        #print(data)
        plt.figure(figsize=(20,15))
        plt.title(data['label'][0] + ': Events before Airball at Period ' + period + ' with ' + time_remaining + ' Time Remaining', fontsize=20)
        plt.xlabel('events', fontsize=15)
        plt.ylabel('# of occurrences', fontsize=15)
        plt.bar(data["x"], data["y"])
        #plt.savefig(plots_directory + '/' + data['label'][0] +  '_' +  date + '_Events_Before_Airball.png')
        plt.close()
        data = data.fromkeys(data, [])

count = 0
for file in os.listdir(play_by_play_directory):
    try:
        print(file)
        id = get_game_id(play_by_play_directory + '/' + file)
        id = id[2:12]
        eventid = airball_list.get(id[2:])[0][0]
        period = get_period(play_by_play_directory + '/' + file, eventid)
        time_remaining = get_time_remaining_at_eventid(play_by_play_directory + '/' + file, eventid)
        events_after_5 = get_events_after_5_mins.output_list_for_5_mins_after_airball(file)
        events_before = get_all_events_before_airball.output_list_for_before_airball(file)
        date = file[1:11]
        print('date', date)
        print(events_after_5)
        plot_after_airball(date, events_after_5, period, time_remaining)
        plot_before_airball(date, events_before, period, time_remaining)
    except:
        continue


Exemple #4
0
def getPlayerEventsInRange(eventID, playerID, minuteRange, eventsDictionary):
    eventsBefore = []
    eventsAfter = []
    subTimes = []
    hasExecuted = False
    
    timeRemaining = get_time_remaining_at_eventid(play_by_play_directory + '/' + file, eventID)
    secondsRemaining = int(timeRemaining[3:5]) * 60 + int(timeRemaining[6:])
    quarter = get_period(play_by_play_directory + '/' + file, eventID)

    #If the airball took place within the time range specified above, append all events before and after into their respective lists
    if secondsRemaining > minuteRange * 60 and secondsRemaining < (720 - minuteRange * 60):
        hasExecuted = True
        with open(play_by_play_directory + '/' + file) as f:
            csv_reader = csv.reader(f, delimiter=',')
            for row in csv_reader:                               
                if row[0] != 'game_id':
                    eventTimeRemaining = row[16]
                    eventSecondsRemaining = (int(eventTimeRemaining[3:5]) * 60 + int(eventTimeRemaining[6:]))
                    
                    #If the event is in the correct time range before the air ball, append the event to the list
                    if (eventSecondsRemaining <= (secondsRemaining + (minuteRange * 60))) and (eventSecondsRemaining >= secondsRemaining) and row[13] == quarter and row[19] < eventID:
                        if row[21] == "shot":
                            if row[22] == playerID:
                                eventsBefore.append('assist')
                            elif row[31] == playerID:
                                eventsBefore.append(row[21])
                        else:
                            if row[31] == playerID:
                                if row[21] == "foul":
                                    eventsBefore.append(row[21])
                                    #eventsBefore.append(row[34][:6]) #Type of foul
                                else:
                                    eventsBefore.append(row[21]) #Type of event that occurs
                    
                    #If the event is in the correct time range after the air ball, append the event to the list
                    if (eventSecondsRemaining >= (secondsRemaining - (minuteRange * 60))) and (eventSecondsRemaining <= secondsRemaining) and row[13] == quarter and row[19] > eventID:
                        if row[21] == "sub":
                            if row[27] == playerID:
                                subTimes.append(secondsRemaining - eventSecondsRemaining)
                        if row[21] == "shot":
                            if row[22] == playerID:
                                eventsAfter.append('assist')
                            elif row[31] == playerID:
                                eventsAfter.append(row[21])
                        else:
                            if row[31] == playerID:
                                if row[21] == "foul":
                                    eventsAfter.append(row[21])
                                    #eventsAfter.append(row[34][:6]) #Type of foul
                                else:
                                    eventsAfter.append(row[21]) #Type of event that occurs

        #This code runs every air ball and determines if an event happens before and / or after an air ball and puts the results into a dictionary
        if len(eventsBefore) == 0:
            eventsDictionary["nothing"][0] = eventsDictionary["nothing"][0] + 1
        if len(eventsAfter) == 0:
            eventsDictionary["nothing"][1] = eventsDictionary["nothing"][1] + 1
        
        eventsOccurred = []
        #print("\nEvents Before: " + str(eventsBefore))
        for event in eventsBefore:
            if (event in eventsDictionary.keys()) and (event not in eventsOccurred):
                eventsDictionary[event][0] = eventsDictionary[event][0] + 1
                eventsOccurred.append(event)
                #print("Amount of events before: " + str(len(eventsOccurred)))

        eventsOccurred = []
        #print("\nEvents After: " + str(eventsAfter))
        for event in eventsAfter:
            if (event in eventsDictionary.keys()) and (event not in eventsOccurred):
                eventsDictionary[event][1] = eventsDictionary[event][1] + 1
                eventsOccurred.append(event)
                #print("Amount of events after: " + str(len(eventsOccurred)) + "\n")
                
        eventsBefore = []
        eventsAfter = []

        return hasExecuted