def pullPitScoutingData():
    homeDir = os.path.expanduser('~')

    pyrebase_config = {
        "apiKey": sensitiveInfo.firebase_api_key(),
        "authDomain": "mri2019.firebaseapp.com",
        "databaseURL": "https://mri2019.firebaseio.com",
        "storageBucket": "mri2019.appspot.com",
    }

    firebase = pyrebase.initialize_app(pyrebase_config)
    database = firebase.database()

    pit_file = os.path.join(homeDir, 'MRI-2019Server/config/pitscouting.xlsx')
    wb = openpyxl.load_workbook(pit_file)
    data = wb['Form Responses 1']

    rows = [
        row for row in data.iter_rows(
            min_row=2, min_col=1, max_col=8, values_only=True)
    ]
    for row in rows:
        if not row[1]:
            break
        teamNumber = row[1]
        pitscouting = {'drivetrain': row[2], 'length': row[6], 'width': row[7]}
        database.child("teams").child(teamNumber).child('pitscouting').set(
            pitscouting)
        print(f'{teamNumber} pitscouting uploaded to Firebase')
    print("All pitscouting data pulled")
예제 #2
0
def get_sykes_data():
    homeDir = os.path.expanduser('~')

    pyrebase_config = {
        "apiKey": sensitiveInfo.firebase_api_key(),
        "authDomain": "mri2019.firebaseapp.com",
        "databaseURL": "https://mri2019.firebaseio.com",
        "storageBucket": "mri2019.appspot.com",
    }

    firebase = pyrebase.initialize_app(pyrebase_config)
    database = firebase.database()

    sykes_file = os.path.join(homeDir, 'MRI-2019Server/config/sykes.xlsx')
    wb = openpyxl.load_workbook(sykes_file)
    pred_contrib = wb['Sheet1']

    print("loaded")

    rows = [row for row in pred_contrib.iter_rows(min_row=2, max_col=5, values_only=True)]

    for row in rows:
        if not row[0]:
            break
        teamNumber = row[0]
        sykesData = {
            'teamName': row[1],
            'elo': row[2],
            'opr': row[4]
            }

        database.child("teams").child(teamNumber).child("sykes").set(sykesData)
        print(f'{teamNumber} Sykes uploaded to Firebase')
    print('All Sykes data pulled')
예제 #3
0
def calculate_timd(compressed_timd, timd_name, test=False):
    decompressed_timd = decompression.decompress_timd(compressed_timd)
    if decompressed_timd['header']['isNoShow']:
        pass
    else:
        decompressed_timd['calculated'] = calculate_statistics(
            decompressed_timd)
        decompressed_timd['climb'] = calculate_climb(decompressed_timd)

    if not test:
        print(f'{timd_name} decompressed')

        homeDir = os.path.expanduser('~')

        pyrebase_config = {
            "apiKey": sensitiveInfo.firebase_api_key(),
            "authDomain": "mri2019.firebaseapp.com",
            "databaseURL": "https://mri2019.firebaseio.com",
            "storageBucket": "mri2019.appspot.com",
        }

        firebase = pyrebase.initialize_app(pyrebase_config)
        database = firebase.database()

        # Save data in local cache
        if not os.path.exists(
                os.path.join(homeDir, 'MRI-2019Server/cache/TIMDs')):
            os.makedirs(os.path.join(homeDir, 'MRI-2019Server/cache/TIMDs'))

        with open(
                os.path.join(homeDir,
                             f'MRI-2019Server/cache/TIMDs/{timd_name}.json'),
                'w') as file:
            json.dump(decompressed_timd, file)
        print(f'{timd_name} cached')

        database.child("TIMDs").child(timd_name).set(decompressed_timd)
        database.child("decompedTIMDs").child(timd_name).set(compressed_timd)
        print(f'{timd_name} uploaded to Firebase\n')

    return decompressed_timd
예제 #4
0
def calculate_team(team_number, last_timd, is_json=False):
    if is_json is not False:
        team = is_json
        timds = team['timds']

    else:
        try:
            team = get_team(team_number)
            timds = get_timds(team_number)
        # TODO Fix in master
        except IndexError:
            team = {'teamNumber': last_timd['team_number']}
            timds = [last_timd]

        team['timds'] = sorted(timds,
                               key=lambda timd: timd['header']['matchNumber'])

    num_matches = len(timds)
    num_no_shows = len([timd for timd in timds if timd['header']['isNoShow']])

    timds = [timd for timd in timds if not timd['header']['isNoShow']]

    l3m_timds = sorted(timds,
                       key=lambda timd: timd['header']['matchNumber'])[-3:]

    team_abilities = {}
    team_abilities['groundCargoPickup'] = True if len(
        cycles.filter_timeline_actions(timds,
                                       actionType='intake',
                                       actionPiece='cargo',
                                       actionPlace='ground')) > 0 else False
    team_abilities['groundHatchPickup'] = True if len(
        cycles.filter_timeline_actions(timds,
                                       actionType='intake',
                                       actionPiece='hatch',
                                       actionPlace='ground')) > 0 else False
    team_abilities['climbHab2'] = True if len(
        cycles.filter_timeline_actions(
            timds, actionType='climb', actualClimb='level2')) > 0 else False
    team_abilities['climbHab3'] = True if len(
        cycles.filter_timeline_actions(
            timds, actionType='climb', actualClimb='level3')) > 0 else False
    team_abilities['placeLevel2'] = True if len(
        cycles.filter_timeline_actions(
            timds, actionType='place', placeLevel='level2')) > 0 else False
    team_abilities['placeLevel3'] = True if len(
        cycles.filter_timeline_actions(
            timds, actionType='place', placeLevel='level3')) > 0 else False
    team_abilities['startLevel2'] = True if len(
        [timd for timd in timds
         if timd['header']['startLevel'] == 'hab2']) > 0 else False
    team_abilities['placeHatch'] = True if len(
        cycles.filter_timeline_actions(
            timds, actionType='place', actionPiece='hatch')) > 0 else False
    team_abilities['placeCargo'] = True if len(
        cycles.filter_timeline_actions(
            timds, actionType='place', actionPiece='cargo')) > 0 else False
    team['team_abilities'] = team_abilities

    totals = {
        'cargoPlaced':
        len(
            cycles.filter_timeline_actions(timds,
                                           actionType='place',
                                           actionPiece='cargo')),
        'hatchesPlaced':
        len(
            cycles.filter_timeline_actions(timds,
                                           actionType='place',
                                           actionPiece='hatch')),
        'cyclesLevel1':
        len(
            cycles.filter_timeline_actions(timds,
                                           actionType='place',
                                           placeLevel='level1')),
        'cyclesLevel2':
        len(
            cycles.filter_timeline_actions(timds,
                                           actionType='place',
                                           placeLevel='level2')),
        'cyclesLevel3':
        len(
            cycles.filter_timeline_actions(timds,
                                           actionType='place',
                                           placeLevel='level3')),
        'cyclesRocket':
        len(
            cycles.filter_timeline_actions(timds,
                                           actionType='place',
                                           actionPlace='rocket')),
        'cyclesCargoShip':
        len(
            cycles.filter_timeline_actions(timds,
                                           actionType='place',
                                           actionPlace='cargoShip')),
        'timeDefending':
        sum([timd['calculated']['timeDefending'] for timd in timds]),
        'timeIncap':
        sum([timd['calculated']['timeIncap'] for timd in timds])
    }

    for average_data_field, timd_data_field in TOTAL_AVERAGE_DATA_FIELDS.items(
    ):
        totals[average_data_field] = stats.avg(
            [timd['calculated'].get(timd_data_field) for timd in timds])
    team['totals'] = totals

    l3ms = {}
    for l3m_average_data_field, timd_data_field in L3M_AVERAGE_DATA_FIELDS.items(
    ):
        l3ms[l3m_average_data_field] = stats.avg(
            [timd['calculated'].get(timd_data_field) for timd in l3m_timds])
    for success_data_field, filters in PERCENT_SUCCESS_DATA_FIELDS.items():
        l3ms['l3ms' + success_data_field] = stats.percent_success_place(
            l3m_timds, **filters)
    team['l3ms'] = l3ms

    p75s = {}
    for p75_data_field, timd_data_field in P75_DATA_FIELDS.items():
        p75s[p75_data_field] = stats.p75(
            [timd['calculated'].get(timd_data_field) for timd in timds])
    team['p75s'] = p75s

    SDs = {}
    for SD_data_field, timd_data_field in SD_DATA_FIELDS.items():
        SDs[SD_data_field] = stats.SD(
            [timd['calculated'].get(timd_data_field) for timd in timds])
    team['SDs'] = SDs

    maxes = {}
    for max_data_field, timd_data_field in MAX_DATA_FIELDS.items():
        maxes[max_data_field] = stats.maximum(
            [timd['calculated'].get(timd_data_field) for timd in timds])
    team['maxes'] = maxes

    percentages = {}
    for success_data_field, filters in PERCENT_SUCCESS_DATA_FIELDS.items():
        percentages[success_data_field] = stats.percent_success_place(
            timds, **filters)

    percentages['hatchPercentageOfCycles'] = sum([
        timd['calculated']['hatchesScored'] for timd in timds
    ]) / sum([timd['calculated']['totalCycles'] for timd in timds]) if sum(
        [timd['calculated']['totalCycles'] for timd in timds]) != 0 else 0
    percentages['hatchPercentageOfCycles'] = round(
        100 * (1 - percentages['hatchPercentageOfCycles']))
    percentages['cargoPercentageOfCycles'] = sum([
        timd['calculated']['cargoScored'] for timd in timds
    ]) / sum([timd['calculated']['totalCycles'] for timd in timds]) if sum(
        [timd['calculated']['totalCycles'] for timd in timds]) != 0 else 0
    percentages['cargoPercentageOfCycles'] = round(
        100 * (1 - percentages['cargoPercentageOfCycles']))

    percentages['percentMatchesStartHab1'] = len([
        timd for timd in timds if timd['header']['startLevel'] == 'hab1'
    ]) / len(timds) if len(timds) is not 0 else 0
    percentages['percentMatchesStartHab1'] = round(
        100 * (percentages['percentMatchesStartHab1']))
    percentages['percentMatchesStartHab2'] = len([
        timd for timd in timds if timd['header']['startLevel'] == 'hab2'
    ]) / len(timds) if len(timds) is not 0 else 0
    percentages['percentMatchesStartHab2'] = round(
        100 * (percentages['percentMatchesStartHab2']))
    percentages['leftHab'] = round(
        100 * (len([timd for timd in timds if timd['header']['leftHab']]) /
               len(timds))) if len(timds) is not 0 else 0

    percentages['hab3ClimbSuccessRate'] = round(
        100 * (len(
            cycles.filter_timeline_actions(
                timds, actionType='climb', actualClimb='level3')) / len(
                    cycles.filter_timeline_actions(
                        timds, actionType='climb', attemptedClimb='level3')))
    ) if len(
        cycles.filter_timeline_actions(
            timds, actionType='climb', attemptedClimb='level3')) != 0 else None
    percentages['hab2ClimbSuccessRate'] = round(
        100 * (len(
            cycles.filter_timeline_actions(
                timds, actionType='climb', actualClimb='level2')) / len(
                    cycles.filter_timeline_actions(
                        timds, actionType='climb', attemptedClimb='level2')))
    ) if len(
        cycles.filter_timeline_actions(
            timds, actionType='climb', attemptedClimb='level2')) != 0 else None
    percentages['percentMatchesClimbHab3'] = round(
        100 * (len(
            cycles.filter_timeline_actions(
                timds, actionType='climb', actualClimb='level3')) /
               len(timds))) if len(timds) is not 0 else 0
    percentages['percentMatchesClimbHab2'] = round(
        100 * (len(
            cycles.filter_timeline_actions(
                timds, actionType='climb', actualClimb='level2')) /
               len(timds))) if len(timds) is not 0 else 0
    percentages['percentMatchesClimbHab1'] = round(
        100 * (len(
            cycles.filter_timeline_actions(
                timds, actionType='climb', actualClimb='level1')) /
               len(timds))) if len(timds) is not 0 else 0

    percentages['percentOfTotalTeleopDefending'] = round(
        100 * (team['totals']['timeDefending'] /
               (len(timds) * 135))) if len(timds) is not 0 else 0

    percentages['percentOfTimeIncap'] = round(
        100 * (team['totals']['timeIncap'] /
               (len(timds) * 150))) if len(timds) is not 0 else 0

    percentages['percentOfMatchesNoShow'] = round(
        100 * (num_no_shows / num_matches + num_no_shows))

    team['percentages'] = percentages

    cycle_times = {}
    for average_cycle_data_field, filters in AVERAGE_CYCLE_TIME_DATA_FIELDS.items(
    ):
        cycle_times[average_cycle_data_field] = cycles.cycle_time_calculations(
            cycles.create_cycle_list(timds, 'place', 'intake'), stats.avg,
            **filters)
    for l3m_average_cycle_data_field, filters in L3M_AVERAGE_CYCLE_TIME_DATA_FIELDS.items(
    ):
        cycle_times[
            l3m_average_cycle_data_field] = cycles.cycle_time_calculations(
                cycles.create_cycle_list(l3m_timds, 'place', 'intake'),
                stats.avg, **filters)
    for p75_cycle_data_field, filters in P75_CYCLE_TIME_DATA_FIELDS.items():
        cycle_times[p75_cycle_data_field] = cycles.cycle_time_calculations(
            cycles.create_cycle_list(timds, 'place', 'intake'), stats.p75,
            **filters)
    for SD_cycle_data_field, filters in SD_CYCLE_TIME_DATA_FIELDS.items():
        cycle_times[SD_cycle_data_field] = cycles.cycle_time_calculations(
            cycles.create_cycle_list(timds, 'place', 'intake'), stats.SD,
            **filters)

    team['cycle_times'] = cycle_times

    team['rankings'] = calculations.calculateRankings.calculate_rankings(
        int(team_number), team)

    print(f'{team_number} calculated')

    homeDir = os.path.expanduser('~')

    pyrebase_config = {
        "apiKey": sensitiveInfo.firebase_api_key(),
        "authDomain": "mri2019.firebaseapp.com",
        "databaseURL": "https://mri2019.firebaseio.com",
        "storageBucket": "mri2019.appspot.com",
    }

    if is_json is False:
        firebase = pyrebase.initialize_app(pyrebase_config)
        database = firebase.database()

        # team['pitscouting'] = dict(database.child("teams").child(team_number).child('pitscouting').get().val())
        # team['sykes'] = dict(database.child("teams").child(team_number).child('sykes').get().val())

        # Save data in local cache
        if not os.path.exists(
                os.path.join(homeDir, 'MRI-2019Server/cache/teams')):
            os.makedirs(os.path.join(homeDir, 'MRI-2019Server/cache/teams'))

        with open(
                os.path.join(homeDir,
                             f'MRI-2019Server/cache/teams/{team_number}.json'),
                'w') as file:
            json.dump(team, file)
        print(f'{team_number} cached')

        database.child("teams").child(team_number).set(team)
        print(f'{team_number} uploaded to Firebase\n')

    return team
import tbapy
import os
import json
import pyrebase

# File with functions which return info such as API keys and passwords
import sensitiveInfo

homeDir = os.path.expanduser('~')

# Firebase setup
pyrebase_config = {
    "apiKey": sensitiveInfo.firebase_api_key(),
    "authDomain": "mri2019.firebaseapp.com",
    "databaseURL": "https://mri2019.firebaseio.com",
    "storageBucket": "mri2019.appspot.com",
}
firebase = pyrebase.initialize_app(pyrebase_config)
database = firebase.database()

# Setup for tbapy
tba = tbapy.TBA(sensitiveInfo.tba_api_key())
event = "2019mnri"

# Get a list of all qualifying matches at an event
try:
    matches = [
        match for match in tba.event_matches(event, simple=True)
        if match['comp_level'] == 'qm'
    ]
    print(matches[0])
예제 #6
0
def calculate_team(team_number, last_timd, is_json=False):
    if is_json is not False:
        team = is_json
        timds = team['timds']

    else:
        try:
            team = get_team(team_number)
            timds = get_timds(team_number)
        except IndexError:
            team = {'teamNumber': last_timd['team_number']}
            timds = [last_timd]

        team['timds'] = sorted(timds, key=lambda timd: timd['header']['matchNumber'])

    num_matches = len(timds)
    num_no_shows = len([timd for timd in timds if timd['header']['noShow']])

    timds = [timd for timd in timds if not timd['header']['noShow']]

    l3m_timds = sorted(timds, key=lambda timd: timd['header']['matchNumber'])[-3:]

    totals = {'cellsScoredHighTeleop': stats.total_filter_values(stats.filter_timeline_actions(timds, actionType='shoot', actionTime='teleop'), 'outerPort', 'innerPort'),
              'cellsScoredLowTeleop': stats.total_filter_values(stats.filter_timeline_actions(timds, actionType='shoot', actionTime='teleop'), 'lowerGoal'),
              'timeDefending': sum([timd['calculated']['timeDefending'] for timd in timds]),
              'timeIncap': sum([timd['calculated']['timeIncap'] for timd in timds])}

    for average_data_field, timd_data_field in TOTAL_AVERAGE_DATA_FIELDS.items():
        totals[average_data_field] = stats.avg([timd['calculated'].get(timd_data_field) for timd in timds])

    totals['cellsScored'] = totals['avgCellsScoredTele'] + totals['avgCellsScoredAuto']
    team['totals'] = totals

    team_abilities = {}
    team_abilities['shootCellsHigh'] = True if totals['cellsScoredHighTeleop'] > 0  else False
    team_abilities['shootCellsLow'] = True if totals['cellsScoredLowTeleop'] > 0 else False
    team_abilities['climb'] = True if len(stats.filter_timeline_actions(timds, actionType='climb', climbHeight='hanging')) > 0 else False
    team_abilities['wheelSpunInMatch'] = True if len(stats.filter_timeline_actions(timds, actionType='wheel')) > 0 else False
    team_abilities['moveOffLineAuto'] = True if len([timd for timd in timds if timd['header']['leftLine']]) > 0 else False
    team['team_abilities'] = team_abilities

    l3ms = {}
    for l3m_average_data_field, timd_data_field in L3M_AVERAGE_DATA_FIELDS.items():
        l3ms[l3m_average_data_field] = stats.avg([timd['calculated'].get(timd_data_field) for timd in l3m_timds])
    team['l3ms'] = l3ms

    p75s = {}
    for p75_data_field, timd_data_field in P75_DATA_FIELDS.items():
        p75s[p75_data_field] = stats.p75([timd['calculated'].get(timd_data_field) for timd in timds])
    team['p75s'] = p75s

    SDs = {}
    for SD_data_field, timd_data_field in SD_DATA_FIELDS.items():
        SDs[SD_data_field] = stats.SD([timd['calculated'].get(timd_data_field) for timd in timds])
    team['SDs'] = SDs

    maxes = {}
    for max_data_field, timd_data_field in MAX_DATA_FIELDS.items():
        maxes[max_data_field] = stats.maximum([timd['calculated'].get(timd_data_field) for timd in timds])
    team['maxes'] = maxes

    percentages = {}
    for success_data_field, filters in PERCENT_SUCCESS_DATA_FIELDS.items():
        percentages[success_data_field] = stats.percent_success_shooting(timds, 'teleop', 'shoot', *filters)

    percentages['leftInitLine'] = round(100 * (len([timd for timd in timds if timd['header']['leftLine']]) / len(timds))) if len(timds) is not 0 else 0

    percentages['climbSuccessRate'] = round(100 * (len(stats.filter_timeline_actions(timds, actionType='climb', climbHeight='hanging')) / len(stats.filter_timeline_actions(timds, actionType='climb', climbHeight='fell')))) if len(stats.filter_timeline_actions(timds, actionType='climb', climbHeight='fell')) != 0 else None
    percentages['levelClimbPercentage'] = round(100 * (len(stats.filter_timeline_actions(timds, actionType='climb', levelClimb=True)) / len(stats.filter_timeline_actions(timds, actionType='climb', climbHeight='hanging')))) if len(stats.filter_timeline_actions(timds, actionType='climb', climbHeight='hanging')) != 0 else None
    percentages['parkPercentage'] = round(100 * (len(stats.filter_timeline_actions(timds, actionType='climb', climbHeight='parked')) / len(timds))) if len(timds) is not 0 else 0
    percentages['climbPercentage'] = round(100 * (len(stats.filter_timeline_actions(timds, actionType='climb', climbHeight='hanging')) / len(timds))) if len(timds) is not 0 else 0

    percentages['percentOfTotalTeleopDefending'] = round(100 * (team['totals']['timeDefending'] / (len(timds) * 135))) if len(timds) is not 0 else 0

    percentages['percentOfTimeIncap'] = round(100 * (team['totals']['timeIncap'] / (len(timds) * 150))) if len(timds) is not 0 else 0

    percentages['percentOfMatchesNoShow'] = round(100 * (num_no_shows / num_matches + num_no_shows))

    team['percentages'] = percentages

    # team['rankings'] = calculations.calculateRankings.calculate_rankings(int(team_number), team)

    print(f'{team_number} calculated')

    homeDir = os.path.expanduser('~')

    pyrebase_config = {
        "apiKey": sensitiveInfo.firebase_api_key(),
        "authDomain": "mndu2-2020.firebaseapp.com",
        "databaseURL": "https://mndu2-2020.firebaseio.com",
        "storageBucket": "mndu2-2020.appspot.com"
    }

    if is_json is False:
        firebase = pyrebase.initialize_app(pyrebase_config)
        database = firebase.database()

        # Save data in local cache
        if not os.path.exists(os.path.join(homeDir, 'MNDU2-2020Server/cache/teams')):
            os.makedirs(os.path.join(homeDir, 'MNDU2-2020Server/cache/teams'))

        with open(os.path.join(homeDir, f'MNDU2-2020Server/cache/teams/{team_number}.json'), 'w') as file:
            json.dump(team, file)
        print(f'{team_number} cached')

        database.child("teams").child(team_number).set(team)
        print(f'{team_number} uploaded to Firebase\n')

    return team