Parser
This is the parser module. You can use it to parse and clean CSGO demos. To see what parsed demo looks like, and what the JSON keys indicate, please visit Example Parser JSON.
awpy.parser.cleaning
Data cleaning functions
- awpy.parser.cleaning.associate_entities(game_names: list[Optional[str]] | None = None, entity_names: list[str] | None = None, metric='lcss') dict [source]
A function to return a dict of associated entities. Accepts
- Parameters:
game_names (list, optional) – A list of names generated by the demofile. Defaults to []
entity_names (list, optional) – A list of names: Defaults to []
metric (string, optional) – A string indicating distance metric, one of lcss, hamming, levenshtein, jaro, difflib. Defaults to ‘lcss’
- Returns:
A dictionary where the keys are entries in game_names, values are the matched entity names.
- awpy.parser.cleaning.replace_entities(df: DataFrame, col_name: str, entity_dict: dict) DataFrame [source]
A function to replace values in a Pandas df column given an entity dict, as created in associate_entities()
- Parameters:
df (DataFrame) – A Pandas DataFrame
col_name (string) – A column in the Pandas DataFrame
entity_dict (dict) – A dictionary as created in the associate_entities() function
- Returns:
A dataframe with replaced names.
awpy.parser.demoparser
This module defines the DemoParser class that handles the core functionality of parsing and cleaning a csgo demo file
Typical usage example: from awpy.parser import DemoParser
# Create parser object # Set log=True above if you want to produce a logfile for the parser demo_parser = DemoParser(
demofile = “og-vs-natus-vincere-m1-dust2.dem”, demo_id = “OG-NaVi-BLAST2020”, parse_rate=128, trade_time=5, buy_style=”hltv”
)
# Parse the demofile, output results to dictionary data = demo_parser.parse() https://github.com/pnxenopoulos/awpy/blob/main/examples/00_Parsing_a_CSGO_Demofile.ipynb
- class awpy.parser.demoparser.DemoParser(demofile: str = '', outpath: str | None = None, demo_id: str | None = None, log: bool = False, parse_rate: int = 128, parse_frames: bool = True, parse_kill_frames: bool = False, trade_time: int = 5, dmg_rolled: bool = False, buy_style: str = 'hltv', json_indentation: bool = False)[source]
Bases:
object
DemoParser can parse, load and clean data from a CSGO demofile. Can be instantiated without a specified demofile.
- demofile
A string denoting the path to the demo file, which ends in .dem Defaults to ‘’
- Type:
string
- outpath
Path where to save the outputfile to. Default is current directory
- Type:
string
- demo_id
A unique demo name/game id. Default is inferred from demofile name
- Type:
string
- output_file
The output file name. Default is ‘demoid’+”.json”
- Type:
str
- log
A boolean indicating if the log should print to stdout. Default is False
- Type:
bool
- parse_rate
One of 128, 64, 32, 16, 8, 4, 2, or 1. The lower the value, the more frames are collected. Indicates spacing between parsed demo frames in ticks. Default is 128.
- Type:
int, optional
- parse_frames
Flag if you want to parse frames (trajectory data) or not. Default is True
- Type:
bool
- parse_kill_frames
Flag if you want to parse frames on kills. Default is False
- Type:
bool
- trade_time
Length of the window for a trade (in seconds). Default is 5.
- Type:
int, optional
- dmg_rolled
Boolean if you want damages rolled up (since multiple damages for a player can happen in 1 tick from the same weapon.) Default is False
- Type:
bool
- buy_style
Buy style string, one of “hltv” or “csgo” Default is “hltv”
- Type:
string
- json_indentation
Whether the json file should be pretty printed with indentation (larger, more readable) or not (smaller, less human readable) Default is False
- Type:
bool
- json
Dictionary containing the parsed json file
- Type:
dict
- Raises:
ValueError – Raises a ValueError if the Golang version is lower than 1.17
- clean_rounds(remove_no_frames: bool = True, remove_warmups: bool = True, remove_knifes: bool = True, remove_bad_timings: bool = True, remove_excess_players: bool = True, remove_excess_kills: bool = True, remove_bad_endings: bool = True, remove_bad_scoring: bool = True, return_type: str = 'json', save_to_json: bool = True) Game | dict[str, Any] [source]
Cleans a parsed demofile JSON.
- Parameters:
remove_no_frames (bool, optional) – Remove rounds where there are no frames. Default to True.
remove_warmups (bool, optional) – Remove warmup rounds. Defaults to True.
remove_knifes (bool, optional) – Remove knife rounds. Defaults to True.
remove_bad_timings (bool, optional) – Remove bad timings. Defaults to True.
remove_excess_players (bool, optional) – Remove rounds with more than 5 players. Defaults to True.
remove_excess_kills (bool, optional) – Remove rounds with more than 10 kills. Defaults to True.
remove_bad_endings (bool, optional) – Remove rounds with bad round end reasons. Defaults to True.
remove_bad_scoring (bool, optional) – Remove rounds where the scoring is off (like scores going below the previous round’s). Defaults to False.
return_type (str, optional) – Return JSON or DataFrame. Defaults to “json”.
save_to_json (bool, optional) – Whether to write the JSON to a file. Defaults to True.
- Raises:
AttributeError – Raises an AttributeError if the .json attribute is None
ValueError – Raises a ValueError if the return type is neither ‘json’ nor ‘df’
- Returns:
A dictionary of the cleaned demo.
- Return type:
dict
- parse(return_type: str = 'json', clean: bool = True) Game | dict[str, Any] [source]
Wrapper for parse_demo() and read_json(). Use to parse a demo.
- Parameters:
return_type (string, optional) – Either “json” or “df”. Default is “json”
clean (bool, optional) – True to run clean_rounds, otherwise, uncleaned data is returned. Defaults to True.
- Returns:
A dictionary of output (which is parsed to a JSON file in the working directory)
- Raises:
ValueError – Raises a ValueError if the return_type is not “json” or “df”
AttributeError – Raises an AttributeError if the .json attribute is None
- parse_demo() None [source]
Parse a demofile using the Go script parse_demo.go – this function needs the .demofile to be set in the class, and the file needs to exist.
- Returns:
Outputs a JSON file to current working directory.
- Raises:
ValueError – Raises a ValueError if the Golang version is lower than 1.17
FileNotFoundError – Raises a FileNotFoundError if the demofile path does not exist.
- parse_json_to_df() dict[str, Any] [source]
Returns JSON into dictionary where keys correspond to data frames
- Returns:
A dictionary of output
- Raises:
AttributeError – Raises an AttributeError if the .json attribute is None
- read_json(json_path: str)[source]
Reads the JSON file given a JSON path. Can be used to read in already processed demofiles.
- Parameters:
json_path (string) – Path to JSON file
- Returns:
JSON in Python dictionary form
- Raises:
FileNotFoundError – Raises a FileNotFoundError if the JSON path doesn’t exist
- remove_bad_scoring() None [source]
Removes rounds where the scoring is bad.
We loop through the rounds: If the round ahead has equal or less score, we do not add the current round. If the round ahead has +1 score, we add the current round
- Raises:
AttributeError – Raises an AttributeError if the .json attribute is None
- remove_end_round(bad_endings: list[str] | None = None) None [source]
Removes rounds with bad end reason.
- Parameters:
bad_endings (list, optional) – List of bad round end reasons. Defaults to [“Draw”, “Unknown”, “”].
- Raises:
AttributeError – Raises an AttributeError if the .json attribute is None
- remove_excess_kill_rounds() None [source]
Removes rounds with more than 10 kills.
- Raises:
AttributeError – Raises an AttributeError if the .json attribute is None
- remove_excess_players() None [source]
Removes rounds where there are more than 5 players on a side.
- Raises:
AttributeError – Raises an AttributeError if the .json attribute is None
- remove_knife_rounds() None [source]
Removes knife rounds.
- Raises:
AttributeError – Raises an AttributeError if the .json attribute is None
- remove_rounds_with_no_frames() None [source]
Removes rounds with no frames
- Raises:
AttributeError – Raises an AttributeError if the .json attribute is None
- remove_time_rounds() None [source]
Remove rounds with odd round timings.
- Raises:
AttributeError – Raises an AttributeError if the .json attribute is None
- remove_warmups() None [source]
Removes warmup rounds.
- Raises:
AttributeError – Raises an AttributeError if the .json attribute is None
- renumber_rounds() None [source]
Renumbers the rounds.
- Raises:
AttributeError – Raises an AttributeError if the .json attribute has a “gameRounds” key.