Пример #1
0
def read_config():
    try:
        logger.info("Process Start")
        config = cp.ConfigParser()
        logger.info("Reading Config")
        config_data = config.read("config.ini")
        if len(config_data) != 1:
            directory_in_str = ''
            load_data = ''
            directory = ''
            raise ValueError
        else:
            directory_in_str = config['BASIC']['yaml_directory']
            load_data = config['BASIC']['load_data']
            generate_stats = config['BASIC']['generate_stats']
            logger.info("Data Directory - %s", directory_in_str)
            directory = os.fsencode(directory_in_str)
    except ValueError:
        logger.error("Config File Not found" + traceback.format_exc())
        sys.exit(-1)
    except KeyError:
        logger.error("Config File Not in the expected format" +
                     traceback.format_exc())
        sys.exit(-1)
    return directory_in_str, load_data, generate_stats, directory
Пример #2
0
def setup_mongo_client():
    try:
        logger.info("Inside setup_mongo_client Method")
        client = MongoClient()
        client = MongoClient('localhost', 27017)
    except:
        logger.error("Exception occurred while setting up Mongo client" +
                     traceback.format_exc())
    return client
Пример #3
0
def get_individual_field_stats(player,field_stats_cursor):
    try:
        raw_field_stats_list=get_individual_field_raw_stats(player,field_stats_cursor)
        total_catches = sum([item for item in raw_field_stats_list])
        #print("Player                            - ", player)
        #print("Total Catches                     - ", total_catches)
        overall_fielding_player_stats=[total_catches]
    except:
        logger.error("Exception Inside get_individual_field_stats Method" + traceback.format_exc())
        sys.exit(-1)
    return overall_fielding_player_stats
Пример #4
0
def load_all_data_from_db(database, table):
    try:
        client = setup_mongo_client()
        db = client[database]
        if (db):
            collection = db[table]
        if (collection):
            cursor = collection.find({})
    except:
        logger.error("Input directory is empty. Read data from database" +
                     traceback.format_exc())
        sys.exit(-1)
    return cursor
Пример #5
0
def get_individual_bat_raw_stats(player,bat_stats_cursor):
    bat_raw_stats=[]
    try:
        for item in bat_stats_cursor:
            for i,item2 in enumerate(item['stats']):
                if player in item2.keys():
                    #print(item2[player])
                    item2[player].append(i+1)
                    bat_raw_stats.append(item2[player])
        #print(player,bat_raw_stats)
        #print(bat_raw_stats)
        bat_stats_cursor.rewind()
    except:
        logger.error("Exception Inside get_individual_bat_raw_stats Method" + traceback.format_exc())
        sys.exit(-1)
    return bat_raw_stats
Пример #6
0
def insert_docs_to_tables(database, table, doc):
    client = setup_mongo_client()
    try:
        db = client[database]
    except:
        logger.error("Exception occurred while connecting to database" +
                     traceback.format_exc())
    if (db):
        try:
            collection = db[table]
        except:
            logger.error("Exception occurred while connecting to the table" +
                         traceback.format_exc())
    #print (json.loads(json.dumps(doc)))
    post_id = collection.insert_one(
        json.loads(json.dumps(doc), object_hook=remove_dots))
    return post_id
Пример #7
0
def get_list_all_fielders(field_stats_cursor):
    try:
        fielder_list = []
        fielder_list_unique = []
        for item in field_stats_cursor:
            for player in item['stats']:
                fielder_list.append(player.keys())

        for item in fielder_list:
            for key in item:
                fielder_list_unique.append(key)
        fielder_list_unique = list(set(fielder_list_unique))
        field_stats_cursor.rewind()
    except:
        logger.error("Exception Inside get_list_all_batsmen Method" + traceback.format_exc())
        sys.exit(-1)
    return fielder_list_unique
Пример #8
0
def get_list_all_batsmen(bat_stats_cursor):
    try:
        batsmen_list = []
        batsmen_list_unique = []
        for item in bat_stats_cursor:
            for player in item['stats']:
                #print(player)
                batsmen_list.append(player.keys())
        # player_list=list(player_list.keys())
        # print(player_list)
        for item in batsmen_list:
            for key in item:
                batsmen_list_unique.append(key)
        batsmen_list_unique = list(set(batsmen_list_unique))
        bat_stats_cursor.rewind()
    except:
        logger.error("Exception Inside get_list_all_batsmen Method" + traceback.format_exc())
        sys.exit(-1)
    return batsmen_list_unique
Пример #9
0
def get_individual_field_raw_stats(player,field_stats_cursor):
    field_raw_stats=[]
    try:
        for item in field_stats_cursor:
            for item2 in item['stats']:
                if player in item2.keys():
                    #print(item2[player])
                    field_raw_stats.append(item2[player])
        #print(bat_raw_stats)
        if not field_raw_stats:
            raise ValueError
        field_stats_cursor.rewind()
    except ValueError:
        return [0]
        field_stats_cursor.rewind()
    except:
        logger.error("Exception Inside get_individual_field_raw_stats Method" + traceback.format_exc())
        sys.exit(-1)
    return field_raw_stats
Пример #10
0
def get_individual_bowl_raw_stats(player,bowl_stats_cursor,wickets_stats_cursor):
    bowl_raw_stats=[]
    try:
        for item in bowl_stats_cursor:
            for i,item2 in enumerate(item['stats']):
                if player in item2.keys():
                    item2[player].append(i + 1)
                    bowl_raw_stats.append(item2[player])
        #print(bowl_raw_stats)
        if not bowl_raw_stats:
            raise ValueError
        bowl_stats_cursor.rewind()
    except ValueError:
        bowl_stats_cursor.rewind()
        return [[0,0,0,0,0,0,0,0,0,0,0]]
    except:
        bowl_stats_cursor.rewind()
        logger.error("Exception Inside get_individual_bowl_raw_stats Method" + traceback.format_exc())
        sys.exit(-1)
    return bowl_raw_stats
Пример #11
0
def get_consolidated_match_Stats(score_batsmen, score_bowler, wickets_bowler,
                                 fielding_details, score_batsmen_pp,
                                 score_batsmen_middle, score_batsmen_death):
    logger.info("Inside get_consolidated_match_Stats Method")
    #Function to summarise the stats
    try:
        batsmen_score_total = {}
        batsmen_score_pp = {}
        batsmen_score_middle = {}
        batsmen_score_death = {}
        bowler_score_total = {}
        wickets_bowler_total = {}
        fielding_details_total = {}
        #Runs Scored/Conceded,Balls Faced/bowled,Str Rate/Eco Rate,No of Dots played/faced,No of 4s,No of 6s,% of Dots,"
        # "% of balls in Boundaries,% of runs in Boundaries
        for key, value in score_batsmen.items():
            try:
                batsmen_score_total[key] = [
                    sum(value),
                    len(value),
                    "%.1f" % round((sum(value) * 100 / len(value)), 1),
                    value.count(0),
                    value.count(4),
                    value.count(6),
                    round(value.count(0) * 100 / len(value), 1),
                    round((value.count(4) + value.count(6)) * 100 / len(value),
                          1),
                    round((value.count(4) * 4 + value.count(6) * 6) * 100 /
                          sum(value), 1)
                ]
            except ZeroDivisionError:
                batsmen_score_total[key] = [
                    sum(value),
                    len(value),
                    "%.1f" % round((sum(value) * 100 / len(value)), 1),
                    value.count(0),
                    value.count(4),
                    value.count(6),
                    round(value.count(0) * 100 / len(value), 1),
                    round((value.count(4) + value.count(6)) * 100 / len(value),
                          1), 0
                ]  #Zero division error while caclulating % of boundary in score
                # print (batsmen_score_total)
        for key, value in score_batsmen_pp.items():
            try:
                batsmen_score_pp[key] = [
                    sum(value),
                    len(value),
                    "%.1f" % round((sum(value) * 100 / len(value)), 1),
                    value.count(0),
                    value.count(4),
                    value.count(6),
                    round(value.count(0) * 100 / len(value), 1),
                    round((value.count(4) + value.count(6)) * 100 / len(value),
                          1),
                    round((value.count(4) * 4 + value.count(6) * 6) * 100 /
                          sum(value), 1)
                ]
            except ZeroDivisionError:
                batsmen_score_pp[key] = [
                    sum(value),
                    len(value),
                    "%.1f" % round((sum(value) * 100 / len(value)), 1),
                    value.count(0),
                    value.count(4),
                    value.count(6),
                    round(value.count(0) * 100 / len(value), 1),
                    round((value.count(4) + value.count(6)) * 100 / len(value),
                          1), 0
                ]
        #
        for key, value in score_batsmen_middle.items():
            try:
                batsmen_score_middle[key] = [
                    sum(value),
                    len(value),
                    "%.1f" % round((sum(value) * 100 / len(value)), 1),
                    value.count(0),
                    value.count(4),
                    value.count(6),
                    round(value.count(0) * 100 / len(value), 1),
                    round((value.count(4) + value.count(6)) * 100 / len(value),
                          1),
                    round((value.count(4) * 4 + value.count(6) * 6) * 100 /
                          sum(value), 1)
                ]
            except ZeroDivisionError:
                batsmen_score_middle[key] = [
                    sum(value),
                    len(value),
                    "%.1f" % round((sum(value) * 100 / len(value)), 1),
                    value.count(0),
                    value.count(4),
                    value.count(6),
                    round(value.count(0) * 100 / len(value), 1),
                    round((value.count(4) + value.count(6)) * 100 / len(value),
                          1), 0
                ]
        #
        for key, value in score_batsmen_death.items():
            try:
                batsmen_score_death[key] = [
                    sum(value),
                    len(value),
                    "%.1f" % round((sum(value) * 100 / len(value)), 1),
                    value.count(0),
                    value.count(4),
                    value.count(6),
                    round(value.count(0) * 100 / len(value), 1),
                    round((value.count(4) + value.count(6)) * 100 / len(value),
                          1),
                    round((value.count(4) * 4 + value.count(6) * 6) * 100 /
                          sum(value), 1)
                ]
            except ZeroDivisionError:
                batsmen_score_death[key] = [
                    sum(value),
                    len(value),
                    "%.1f" % round((sum(value) * 100 / len(value)), 1),
                    value.count(0),
                    value.count(4),
                    value.count(6),
                    round(value.count(0) * 100 / len(value), 1),
                    round((value.count(4) + value.count(6)) * 100 / len(value),
                          1), 0
                ]
        #
        for key, value in score_bowler.items():
            try:
                bowler_score_total[key] = [
                    sum(value),
                    len(value),
                    "%.1f" % round((sum(value) * 6 / len(value)), 1),
                    value.count(0),
                    value.count(4),
                    value.count(6),
                    round(value.count(0) * 100 / len(value), 1),
                    round((value.count(4) + value.count(6)) * 100 / len(value),
                          1),
                    round((value.count(4) * 4 + value.count(6) * 6) * 100 /
                          sum(value), 1), 0
                ]
            except ZeroDivisionError:
                bowler_score_total[key] = [
                    sum(value),
                    len(value),
                    "%.1f" % round((sum(value) * 6 / len(value)), 1),
                    value.count(0),
                    value.count(4),
                    value.count(6),
                    round(value.count(0) * 100 / len(value), 1),
                    round((value.count(4) + value.count(6)) * 100 / len(value),
                          1), 0, 0
                ]  #Zero division error while caclulating % of boundary balls
        # print(bowler_score_total)
        for key, value in wickets_bowler.items():
            wickets_bowler_total[key] = [
                len(value), len(score_bowler[key]) / len(value)
            ]  # No of Wickets, Strike Rate
            bowler_score_total[key][9] = len(value)
        # print(wickets_bowler_total)
        for key, value in fielding_details.items():
            fielding_details_total[key] = int(len(value) / 2)  # No of Catches
        # print(fielding_details_total)
        # stats_list=[batsmen_score_total,bowler_score_total,wickets_bowler_total,fielding_details_total]
    except:
        print("exception Occurred")
        logger.error("Error Inside get_consolidated_match_Stats Method" +
                     traceback.format_exc())
    return batsmen_score_total,bowler_score_total,wickets_bowler_total,fielding_details_total,batsmen_score_pp\
        ,batsmen_score_middle,batsmen_score_death
Пример #12
0
def get_score_from_yaml(innings, filename):
    logger.info("Inside get_score_from_yaml Method")
    #This function is called per innings. Twice per match.
    #Return values are used by get_consolidated_match_Stats to summarise the details
    match_info_df = pd.DataFrame()
    #source = os.listdir("C:\\Users\\Siddi\\PycharmProjects\\IPL_Prediction\\Data\\Yaml")
    #destination = "C:\\Users\\Siddi\\PycharmProjects\\IPL_Prediction\\Data\\Errors"
    filename = directory_in_str + filename
    with open(filename, 'r') as stream:
        try:
            #Initialize dictionaries to store the required stats.
            score_batsmen = {}
            score_batsmen_pp = {}
            score_batsmen_middle = {}
            score_batsmen_death = {}
            score_bowler = {}
            wickets_bowler = {}
            fielding_details = {}
            exception_case = {}
            json_obj = json.dumps(yaml.load(stream),
                                  default=json_serial,
                                  indent=4)
            #Separates the info details from yaml and obtains only the ball by ball inns details
            # Contains the details of both the innings.
            fir_innings_info = json_normalize(json.loads(json_obj)["innings"])
            fir_innings_info = pd.DataFrame(fir_innings_info)
            if (innings == 1):  #Get first innings ball by ball details
                fir_inns_del = OrderedDict()
                fir_inns_del = fir_innings_info.iloc[0, 0]
            else:  #Gets second innings ball by ball details
                fir_inns_del = OrderedDict()
                fir_inns_del = fir_innings_info.iloc[1, 2]
            #Details that are fetched
            #print("Runs Scored/Conceded,Balls Faced,Str Rate,No of Dots,No of 4s,No of 6s,% of Dots,"
            # "% of balls in Boundaries,% of runs in Boundaries")
            for i in fir_inns_del:
                if (type(i) == str):
                    exception_case["Absent_Hurt"] = i
                else:
                    for key, value in i.items():
                        if value[
                                "batsman"] in score_batsmen:  #value["batsman"] contains the name of the batsman
                            #If the name is present in dict already as a key then:
                            score_batsmen[value["batsman"]].append(
                                value["runs"]["batsman"])
                            if (float(key) >= 0.0 and float(key) < 6):
                                score_batsmen_pp[value["batsman"]].append(
                                    value["runs"]["batsman"])
                            elif (float(key) >= 6.0 and float(key) < 16):
                                score_batsmen_middle[value["batsman"]].append(
                                    value["runs"]["batsman"])
                            else:
                                score_batsmen_death[value["batsman"]].append(
                                    value["runs"]["batsman"])
                            if "wicket" in value:
                                if value["wicket"]["kind"]!="run out" and value["wicket"]["kind"] != "retired hurt" \
                                        and value["wicket"]["kind"] != "obstructing the field":
                                    #Credit the bowler for wicket only if the mode of dismissal is none of the above
                                    if value[
                                            "bowler"] in wickets_bowler:  #If bowler name is present as a key in dict
                                        wickets_bowler[value["bowler"]].append(
                                            value["wicket"]["player_out"])
                                    else:  #Create the key with bowler name
                                        wickets_bowler[value["bowler"]] = [
                                            value["runs"]["total"]
                                        ]
                                    if (value["wicket"]["kind"] != "run out"
                                            and
                                            value["wicket"]["kind"] != "bowled"
                                            and
                                            value["wicket"]["kind"] != "lbw"
                                            and value["wicket"]["kind"] !=
                                            "caught and bowled"
                                            and value["wicket"]["kind"] !=
                                            "retired hurt"
                                            and value["wicket"]["kind"] !=
                                            "obstructing the field"
                                            and value["wicket"]["kind"] !=
                                            "hit wicket"):
                                        #Credit the fielder if mode of dismissal is none of the above
                                        if value["wicket"]["fielders"][
                                                0] in fielding_details:
                                            fielding_details[
                                                value["wicket"]["fielders"]
                                                [0]].append(value["wicket"]
                                                            ["player_out"])
                                            fielding_details[
                                                value["wicket"]["fielders"]
                                                [0]].append(value["bowler"])
                                        else:
                                            fielding_details[value["wicket"][
                                                "fielders"][0]] = [
                                                    value["runs"]["total"]
                                                ]
                                            fielding_details[
                                                value["wicket"]["fielders"]
                                                [0]].append(value["bowler"])
                        else:
                            score_batsmen[value["batsman"]] = [
                                value["runs"]["batsman"]
                            ]
                            if (float(key) > 0 and float(key) < 6):
                                score_batsmen_pp[value["batsman"]] = [
                                    value["runs"]["batsman"]
                                ]
                                score_batsmen_middle[value["batsman"]] = [0]
                                score_batsmen_death[value["batsman"]] = [0]
                            elif (float(key) > 6 and float(key) < 16):
                                score_batsmen_middle[value["batsman"]] = [
                                    value["runs"]["batsman"]
                                ]
                                score_batsmen_death[value["batsman"]] = [0]
                            else:
                                score_batsmen_death[value["batsman"]] = [
                                    value["runs"]["batsman"]
                                ]
                        if value[
                                "bowler"] in score_bowler:  #Runs conceded by the bowler
                            score_bowler[value["bowler"]].append(
                                value["runs"]["total"])
                        else:
                            score_bowler[value["bowler"]] = [
                                value["runs"]["total"]
                            ]
        except yaml.YAMLError as exc:
            logger.error("yaml error Inside get_score_from_yaml Method")
        except:
            #shutil.copy(file, destination)
            logger.error(
                " error Inside get_score_from_yaml Method - %s" +
                traceback.format_exc(), filename)
            #print("Exception occured")
            #print(filename)
            pass
    return score_batsmen,score_bowler,wickets_bowler,fielding_details,exception_case,score_batsmen_pp,score_batsmen_middle ,\
            score_batsmen_death