### Executing the Ray Task Example (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/as_completed.ipynb Calls the `foo` function to start the Ray tasks and begin monitoring their execution. ```python foo() ``` -------------------------------- ### Getting Help on tqdm (IPython) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/as_completed.ipynb An IPython command to display the help documentation for the `tqdm` library, typically used in an interactive environment like a Jupyter notebook or IPython console. ```ipython tqdm? ``` -------------------------------- ### Initializing ConfigParser Object (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/configparser.ipynb Creates an instance of the `ConfigParser` class. This object will hold the configuration data and provide methods for manipulation. ```python config = configparser.ConfigParser() ``` -------------------------------- ### Importing configparser Module (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/configparser.ipynb Imports the necessary `configparser` module to work with configuration files. This is the first step before creating or reading config objects. ```python import configparser ``` -------------------------------- ### Accessing game start data (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_testing.ipynb Accesses the 'start' field from the parsed `game` object, which likely contains information about the game's start settings like stage, players, etc. ```python game['start'] ``` -------------------------------- ### Reading Configuration from a File (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/configparser.ipynb Opens an existing configuration file and reads its content into the `config` object. This loads configuration settings from a file. ```python with open('/home/vlad/Repos/libmelee/melee/GALE01.ini') as f: config.read(f) ``` -------------------------------- ### Viewing Configuration File Content (Shell) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/configparser.ipynb Uses the `cat` command to display the content of the generated configuration file '/tmp/test.cfg' in the terminal. ```shell cat /tmp/test.cfg ``` -------------------------------- ### Importing Required Libraries (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/as_completed.ipynb Imports necessary libraries for time manipulation, Ray parallel processing, concurrent futures, asyncio, and progress bars (tqdm), along with typing hints. ```python import time import ray import concurrent import asyncio from tqdm import tqdm from typing import List ``` -------------------------------- ### Creating and Monitoring Ray Tasks (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/as_completed.ipynb Defines a function `foo` that launches 10 Ray remote tasks using the `work` function and then calls the `monitor` function to track their progress with a log interval of 2 seconds. ```python def foo(): futures = [work.remote(i).future() for i in range(10)] monitor(futures, log_interval=2) ``` -------------------------------- ### Listing Options in a Section (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/configparser.ipynb Retrieves a list of all option names within the 'Gecko' section of the configuration object. This is useful for inspecting available settings. ```python config.options('Gecko') ``` -------------------------------- ### Writing ConfigParser to a File (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/configparser.ipynb Opens a file named '/tmp/test.cfg' in write mode and writes the current configuration object's content to it. This persists the configuration to disk. ```python with open('/tmp/test.cfg', 'w') as f: config.write(f) ``` -------------------------------- ### Enabling IPython Debugger (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Uses the IPython magic command `%pdb` to enable the Python debugger, which will automatically start on errors. ```python %pdb ``` -------------------------------- ### Enabling IPython Debugger (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Uses the IPython magic command `%pdb` to enable the Python debugger, which will automatically start on errors. ```python %pdb ``` -------------------------------- ### Importing Slippi Parsing Library - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/invulnerable.ipynb Imports the parse_libmelee module from the slippi_db library, which provides functions for parsing Slippi replay files (.slp). This is a prerequisite for loading and analyzing replay data. ```python from slippi_db import parse_libmelee ``` -------------------------------- ### Setting an Option in a Section (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/configparser.ipynb Sets the value of the 'foo' option within the 'Core' section to 'bar'. This is how key-value pairs are stored in the configuration. ```python config.set('Core', 'foo', 'bar') ``` -------------------------------- ### Enabling Python Debugger - IPython Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/invulnerable.ipynb This is an IPython magic command that enables the Python debugger (pdb). When executed, it configures the environment to automatically enter the debugger upon encountering an unhandled exception. ```python %pdb ``` -------------------------------- ### Reloading Test Module and Getting Singles Info (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Reloads the `test_peppi` module again and calls `get_singles_info` with 'prod' to retrieve singles match metadata. ```python reload(test_peppi) singles_metas = test_peppi.get_singles_info('prod') ``` -------------------------------- ### Getting Count of Singles Metadata (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Calculates and displays the number of items in the `singles_metas` list. ```python len(singles_metas) ``` -------------------------------- ### Adding a Section to ConfigParser (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/configparser.ipynb Adds a new section named 'Core' to the configuration object. Sections are used to group configuration options. ```python config.add_section('Core') ``` -------------------------------- ### Sorting Training Data by Start Time - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/extract_vs_phillip_replays.ipynb Sorts the `training_phil_df` DataFrame in place based on the 'startAt' column, arranging the replays chronologically. ```python training_phil_df.sort_values('startAt', inplace=True) ``` -------------------------------- ### Access Batch Data (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/rewards.ipynb Demonstrates accessing specific data within a fetched batch. This example accesses the `rewards` attribute from the game state of the first item in the 14th batch (index 13). Requires the `batches` list to be populated. ```python batches[13][0].game.rewards ``` -------------------------------- ### Accessing game metadata (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_testing.ipynb Accesses the 'metadata' field from the parsed `game` object, which typically contains information about the game setup, players, and stage. ```python game['metadata'] ``` -------------------------------- ### Retrieving Result from a Future (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/as_completed.ipynb Retrieves the result from a future object named `ft`. This call will block until the future has completed and its result is available. ```python ft.result() ``` -------------------------------- ### Defining Ray Remote Task and Progress Monitor (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/as_completed.ipynb Defines a Ray remote function `work` that simulates work by sleeping for a given number of seconds. Also defines a `monitor` function that takes a list of futures and logs progress periodically, showing completion percentage, rate, and estimated time remaining. ```python @ray.remote def work(secs): time.sleep(secs) return secs def monitor( futures: List[concurrent.futures.Future], log_interval: int = 5, ): total_items = len(futures) start_time = time.perf_counter() last_log = start_time finished_items = 0 for _ in concurrent.futures.as_completed(futures): finished_items += 1 current_time = time.perf_counter() if current_time - last_log > log_interval: run_time = current_time - start_time items_per_second = finished_items / run_time num_items_remaining = total_items - finished_items estimated_time_remaining = num_items_remaining / items_per_second progress_percent = finished_items / total_items print( f'{finished_items}/{total_items} = {100 * progress_percent:.1f}% ' f'rate={items_per_second:.1f} ' f'eta={estimated_time_remaining:.0f}' ) last_log = current_time ``` -------------------------------- ### Getting Count of Local Games Loaded (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Calculates and displays the number of game objects successfully loaded from the local directory. ```python len(local_games) ``` -------------------------------- ### Defining Game Analysis Function (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Defines a function `analyze_game` that takes a game object, extracts player ports from the start and first frame data, and prints them. ```python def analyze_game(game): start_ports = [p['port'] for p in game['start']['players']] frame_ports = sorted(game['frames'][0]['ports']) print(start_ports, frame_ports) ``` -------------------------------- ### Selecting Last 5 Replays Per Matchup - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/extract_vs_phillip_replays.ipynb Groups the `training_phil_df` by the player's character ('char') and opponent's character ('opponent_char'), then selects the last 5 rows within each group. This creates `matchup_df` containing recent examples for each matchup. ```python matchup_df = training_phil_df.groupby(['char', 'opponent_char']).tail(5).reset_index() len(matchup_df) ``` -------------------------------- ### Loading Slippi Replay File - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/invulnerable.ipynb Loads a specific Slippi replay file (.slp) located at 'peppi_test/21_35_46 [NOAH] Ice Climbers + [TITP] Captain Falcon (FD).slp' using the get_slp function from the parse_libmelee module. The parsed game data is stored in the 'game' variable. ```python game = parse_libmelee.get_slp('peppi_test/21_35_46 [NOAH] Ice Climbers + [TITP] Captain Falcon (FD).slp') ``` -------------------------------- ### Grouping Raw File Paths by Player (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/make_test_dataset.ipynb Iterates through the set of all unique raw file paths. If a path starts with 'Players/', it extracts the player name and adds the raw path to a list associated with that player in the `player_raws` dictionary. Finally, it prints a comma-separated list of the unique player names found. ```python player_raws = {} for raw in all_raw: if not raw.startswith('Players/'): continue player = raw.split('/')[1] player_raws.setdefault(player, []).append(raw) print(','.join(sorted(player_raws.keys()))) ``` -------------------------------- ### Selecting Elements from PyArrow Array Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/pyarrow_testing.ipynb Uses the 'take' method to select elements from a PyArrow array based on a list of indices. This example selects the first element (index 0) three times. ```python sa.take([0, 0, 0]) ``` -------------------------------- ### Accessing game frame and port data (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_testing.ipynb Extracts the 'frames' data from the parsed game, then accesses the 'ports' field within frames, and finally gets a list of port names from the first frame's port data. ```python frames = game['frames'] ports = frames.field('ports') port_names = list(ports[0]) ``` -------------------------------- ### Compacting Raw File Paths (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/make_test_dataset.ipynb Defines a cached function `compact_raw` that simplifies raw file paths, specifically handling paths starting with 'Players/' or 'Phillip/'. It then applies this function to the 'raw' column of the DataFrame to create a new 'compact_raw' column. ```python @functools.cache def compact_raw(raw: str) -> str: if raw.startswith('Players/'): return raw.split('/')[1] if raw.startswith('Phillip/'): return 'Phillip' return raw df['compact_raw'] = df['raw'].map(compact_raw) ``` -------------------------------- ### Getting Size of In-Memory Parquet Data - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parquet.ipynb Retrieves the byte content from the in-memory stream `f` after the Parquet data has been written and calculates its length, effectively getting the size of the generated Parquet data. ```Python len(f.getvalue()) ``` -------------------------------- ### Filtering and Copying Replays for Toy Dataset (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/create_dummy_dataset.ipynb Filters the loaded `metadata` to find ranked games, sorts them by size, selects the smallest `dataset_size` games, creates the output directory structure, copies the corresponding parsed replay files from `data_dir` to the output directory, and saves the selected metadata subset to a new `meta.json` file in the output directory. ```python dataset_size = 1 ranked_metas = [meta for meta in metadata if meta['raw'].startswith('Ranked')] sorted_metas = sorted(ranked_metas, key=lambda meta: meta['pq_size']) toy_metadata = sorted_metas[:dataset_size] output_data_dir = os.path.join(output_path, 'games') os.makedirs(output_data_dir, exist_ok=True) for meta in toy_metadata: name = meta['slp_md5'] src = os.path.join(data_dir, name) dst = os.path.join(output_data_dir, name) shutil.copy(src, dst) output_meta_path = os.path.join(output_path, 'meta.json') with open(output_meta_path, 'w') as f: json.dump(toy_metadata, f) ``` -------------------------------- ### Getting Keys for Specific Failure Reason Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parse_failures.ipynb Accesses the `reason_to_key` dictionary using a specific string key: ": 61 is not a valid EventType". It retrieves and assigns the list of keys associated with this particular failure reason. ```python event_type_61_keys = reason_to_key[": 61 is not a valid EventType"] ``` -------------------------------- ### Importing Libraries (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/create_dummy_dataset.ipynb Imports standard Python libraries `json`, `os`, and `shutil` necessary for handling JSON data, interacting with the operating system (paths, directories), and performing file operations (copying). ```python import json import os import shutil ``` -------------------------------- ### Split Data Paths (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/rewards.ipynb Splits a directory of Slippi replay files into training and testing sets based on file paths. Requires a directory containing `.slp` files. ```python train_paths, test_paths = data.train_test_split('/home/victor/slippi/AllCompressed/') ``` -------------------------------- ### Downloading Specific SLP Files Locally (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Sets a local directory path and downloads the first document from the `keys` list (which are SLP keys) to that directory using `upload_lib.download_slp_locally`. ```python dir = 'data/test/start' for key in keys[:1]: upload_lib.download_slp_locally('test', key, 'data/test/start/') ``` -------------------------------- ### Determining Game Winner by Stock Count (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Initializes winner to None, gets stock counts from the last frame, identifies players with 0 stocks (losers), and if there's exactly one player with > 0 stocks among the non-losers, declares them the winner. ```python winner = None last_frame = game['frames'][-1] stock_counts = {} for index, player in last_frame['ports'].items(): stock_counts[int(index)] = player['leader']['post']['stocks'].as_py() losers = [p for p, s in stock_counts.items() if s == 0] if losers: winners = [p for p, s in stock_counts.items() if s > 0] if len(winners) == 1: winner = winners[0] print(winner) ``` -------------------------------- ### Connecting to Production Meta Database (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Establishes a connection to the 'meta' database in the 'prod' environment using `upload_lib.get_db` and fetches all documents. ```python prod_meta = upload_lib.get_db('prod', 'meta') meta_docs = list(prod_meta.find()) ``` -------------------------------- ### Connecting to Test Databases (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Establishes connections to the 'slp' and 'meta' databases in the 'test' environment using `upload_lib.get_db` and fetches all documents from the 'slp' collection. ```python test_slp = upload_lib.get_db('test', 'slp') test_meta = upload_lib.get_db('test', 'meta') test_slp = list(test_slp.find()) ``` -------------------------------- ### Connecting to Production Databases (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Establishes connections to the 'raw' and 'slp' databases in the 'prod' environment using `upload_lib.get_db` and fetches all documents from the 'slp' collection. ```python prod_raw = upload_lib.get_db('prod', 'raw') prod_slp = upload_lib.get_db('prod', 'slp') prod_slp = list(prod_slp.find()) ``` -------------------------------- ### Loading Replay Metadata (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/create_dummy_dataset.ipynb Reads the JSON file located at `meta_path` and parses its content into the `metadata` variable. This step makes the replay information available for filtering and selection. ```python with open(meta_path) as f: metadata = json.load(f) ``` -------------------------------- ### Getting DataFrame length in Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/pyarrow_example.ipynb Calculates and returns the number of rows in the pandas DataFrame 'df'. This provides a quick overview of the dataset size. ```python len(df) ``` -------------------------------- ### Create Data Source (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/rewards.ipynb Configures and creates a data source object from the training file paths. Specifies parameters like allowed characters/opponents, batch size, unroll length, and the controller embedding function. ```python train_data = data.make_source( filenames=train_paths, allowed_characters='all', allowed_opponents='all', in_parallel=False, max_action_repeat=0, embed_controller=embed.embed_controller_discrete, batch_size=1, unroll_length=200, ) ``` -------------------------------- ### Loading Games from Directory (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Initializes an empty list, iterates through files in the specified directory, and attempts to load each file as a game object using `peppi_py.game`, appending successful loads to the list. ```python games = [] path = 'data/test/start/' for name in os.listdir(path): games.append(peppi_py.game(os.path.join(path, name))) ``` -------------------------------- ### Creating SLP Key to Raw Key Mapping (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Creates a dictionary mapping the 'key' of each document in `test_slp` to its corresponding 'raw_key'. ```python slp_to_raw = {d['key']: d['raw_key'] for d in test_slp} ``` -------------------------------- ### Loading Specific Game File (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Sets a file path and loads the specified `.slp` file into a `game` object using `peppi_py.game`. ```python path = 'data/test/18_29_53 Kirby + Falco.slp' game = peppi_py.game(path) ``` -------------------------------- ### Preparing File List for Test Dataset Copy (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/make_test_dataset.ipynb Iterates through the 'raw' and 'name' columns of the `for_test_dataset` DataFrame and builds a dictionary `files_to_copy` where keys are raw zip file paths and values are lists of the specific filenames from those zips that are included in the test dataset. ```python files_to_copy = {} for raw, filename in zip(for_test_dataset['raw'], for_test_dataset['name']): files_to_copy.setdefault(raw, []).append(filename) ``` -------------------------------- ### Importing Libraries for Data Processing Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_panic.ipynb Imports the necessary Python libraries: `pickle` for loading serialized data, `os` for interacting with the operating system (like path manipulation), and `pandas` for data analysis and manipulation. ```python import pickle import os import pandas as pd ``` -------------------------------- ### Importing Libraries for Data Analysis - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/local_parse_analysis.ipynb Imports necessary libraries for data processing and analysis, including pandas for data manipulation, pickle for loading data, os for path handling, and custom modules from `slippi_db`. ```python import collections import pickle import pandas as pd import os import peppi_py import tree import json import io import math import tqdm.notebook import functools from slippi_db import utils, preprocessing, parse_peppi ``` -------------------------------- ### Using TensorFlow's tf.scan (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/scan_test.ipynb Demonstrates the use of `tf.scan` to apply a cumulative operation (`tf.add`) over the elements of the `xs` array. It starts with an initial value of 4 (as a NumPy int64) and processes the array in reverse order. ```python tf.scan(tf.add, xs, np.int64(4), reverse=True) ``` -------------------------------- ### Filtering Invalid Entries by Reason Prefix Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_panic.ipynb Further filters the `invalid` DataFrame to select rows where the 'reason' column value is a string that starts with the substring 'called'. This isolates entries related to specific types of errors or events. ```python called = invalid[invalid['reason'].map(lambda s: s.startswith('called'))] ``` -------------------------------- ### Fetch Data Batches (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/rewards.ipynb Fetches the next 20 batches of data from the configured `train_data` source. Each batch contains processed game state information. Requires the `train_data` source to be initialized. ```python batches = [next(train_data) for _ in range(20)] ``` -------------------------------- ### Import slippi-ai Modules (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/rewards.ipynb Imports the `data` and `embed` modules from the `slippi_ai` library, which are necessary for data loading and embedding controller inputs. ```python from slippi_ai import data, embed ``` -------------------------------- ### Getting Number of Melee Actions - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/embed_test.ipynb Calculates the length of `melee.Action`. This likely retrieves the total count of distinct actions defined within the `melee` library's enumeration or structure representing in-game actions. ```python len(melee.Action) ``` -------------------------------- ### Defining Output Path (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/create_dummy_dataset.ipynb Specifies the directory path (`output_path`) where the generated toy dataset, including copied replay files and filtered metadata, will be stored. ```python output_path = '../data/toy_dataset' ``` -------------------------------- ### Loading Pickled Data - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/extract_vs_phillip_replays.ipynb Constructs the path to a pickled data file (`parsed.pkl`) within the root directory. Opens the file in binary read mode and reads its entire content into the `data_bytes` variable. ```python parsed_path = os.path.join(root, 'parsed.pkl') with open(parsed_path, 'rb') as f: data_bytes = f.read() len(data_bytes) ``` -------------------------------- ### Importing Libraries for Data Processing (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/make_test_dataset.ipynb Imports standard and custom libraries required for loading, processing, and manipulating Slippi replay data, including file handling, data structures, and utilities from `slippi_db`. ```python import collections import pickle import pandas as pd import os import peppi_py import tree import json import io import math import tqdm.notebook import functools import zipfile from slippi_db import utils, preprocessing, parse_peppi ``` -------------------------------- ### Defining Input Data Paths (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/create_dummy_dataset.ipynb Sets the base path for replay data (`data_path`), the specific directory for parsed replays (`data_dir`), and the path to the metadata JSON file (`meta_path`). These variables are crucial for locating the source data. ```python data_path = '/Users/vlad/SSBM/Replays/' data_dir = os.path.join(data_path, 'Parsed') meta_path = os.path.join(data_path, 'meta.json') ``` -------------------------------- ### Importing Libraries (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Imports standard libraries like `os` and `numpy`, along with custom modules `slippi_db` and `peppi_py` for database interaction and replay parsing. ```python import os import numpy as np from slippi_db import upload_lib, preprocessing import peppi_py ``` -------------------------------- ### Loading Local Slippi Games with Error Handling (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Iterates through files in the user's local Slippi directory, attempts to load each as a game using `peppi_py.game`, skips files causing `OSError`, appends successful loads, and stops after loading 20 games. ```python local_games = [] path = os.path.expanduser('~/Slippi') for name in os.listdir(path): try: game = peppi_py.game(os.path.join(path, name)) except OSError: continue local_games.append(game) if len(local_games) == 20: break ``` -------------------------------- ### Importing Libraries for Slippi Data Processing - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parquet.ipynb Imports necessary libraries: `pyarrow` for data handling, `slippi_db.parse_libmelee` for parsing Slippi files, `pyarrow.parquet` for Parquet format, and `io` for in-memory file operations. ```Python import pyarrow as pa from slippi_db import parse_libmelee import pyarrow.parquet as pq import io ``` -------------------------------- ### Initializing Interactive Tables - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/local_parse_analysis.ipynb Initializes the `itables` library for displaying pandas DataFrames as interactive tables in a notebook environment. ```python from itables import init_notebook_mode init_notebook_mode(all_interactive=True) ``` -------------------------------- ### Importing Libraries (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/compression_analysis.ipynb Imports standard libraries like os, time, pandas, and custom modules from slippi_db for database interaction and dataset creation. ```python import os import time import pandas as pd from slippi_db import make_compression_datasets from slippi_db import upload_lib ``` -------------------------------- ### Importing Parsing and Serialization Libraries (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/compression_analysis.ipynb Imports standard libraries pickle, zlib, os, and custom modules parse_libmelee, parse_peppi, and InvalidGameError, array_to_nest from slippi_db and slippi_ai.types for processing and serializing game data. ```python import pickle, zlib, os from slippi_db import parse_libmelee, parse_peppi from slippi_ai.types import InvalidGameError, array_to_nest ``` -------------------------------- ### Downloading SLP Files Locally (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/compression_analysis.ipynb Creates the local data directory if it doesn't exist and iterates through the retrieved SLP file information to download each file to the specified local directory using upload_lib.download_slp_locally. ```python os.makedirs(slp_dir, exist_ok=True) for info in slp_infos: upload_lib.download_slp_locally(env, info['key'], slp_dir) ``` -------------------------------- ### Importing os module (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_testing.ipynb Imports the standard Python `os` module, providing a way of using operating system dependent functionality. ```python import os ``` -------------------------------- ### Writing Failure Reasons to File (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Formats the sorted failure reasons into lines of text (reason, count) and writes them to a file named 'data/reasons.txt'. ```python lines = [k + ', ' + str(v) + '\n' for k, v in reasons] with open('data/reasons.txt', 'w') as f: f.writelines(lines) ``` -------------------------------- ### Organizing Files for Copying - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/extract_vs_phillip_replays.ipynb Iterates through the 'raw' directory and 'filename' columns of the `matchup_df`. It creates a dictionary `files_to_copy` where keys are the 'raw' directory paths and values are lists of filenames within that directory that need to be copied. ```python files_to_copy = {} for raw, filename in zip(matchup_df['raw'], matchup_df['filename']): files_to_copy.setdefault(raw, []).append(filename) ``` -------------------------------- ### Initializing Itables for Interactive DataFrames (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/make_test_dataset.ipynb Configures the `itables` library to make pandas DataFrames interactive when displayed in a notebook environment, enabling features like sorting and filtering. ```python from itables import init_notebook_mode init_notebook_mode(all_interactive=True) ``` -------------------------------- ### Mapping Meta Documents by Key Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parse_failures.ipynb Creates a dictionary where each key is the value of the 'key' field from a meta document in the `prod_meta` list. The corresponding value in the dictionary is the entire meta document. ```python key_to_meta = {meta['key']: meta for meta in prod_meta} ``` -------------------------------- ### Selecting Samples for Test Dataset (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/make_test_dataset.ipynb Groups the `training_with_raw` DataFrame by the 'raw' column and selects the last 50 entries from each group using `tail(50)`. The index is reset, and the size of this resulting `for_test_dataset` DataFrame is stored and printed. ```python for_test_dataset = training_with_raw.groupby('raw').tail(50).reset_index() size = len(for_test_dataset) size ``` -------------------------------- ### Defining and Creating Output Directory Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_panic.ipynb Sets the `out` variable to the desired path for the output directory ('data/panic'). It then creates this directory using `os.makedirs()`, ensuring that parent directories are also created if needed and that no error is raised if the directory already exists (`exist_ok=True`). ```python out = 'data/panic' os.makedirs(out, exist_ok=True) ``` -------------------------------- ### Importing Enet Library - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/enet_test.ipynb Imports the enet library, which is a thin wrapper around the ENet library for creating peer-to-peer network applications. ```python import enet ``` -------------------------------- ### Fetching Meta Documents from Production DB Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parse_failures.ipynb Connects to the 'meta' collection in the 'prod' database using `upload_lib.get_db`. It fetches all documents from this collection and converts the database cursor into a Python list. ```python prod_meta = list(upload_lib.get_db('prod', 'meta').find()) ``` -------------------------------- ### Fetching Meta Documents from Production DB (META constant) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parse_failures.ipynb Connects to the meta documents collection in the 'prod' database, identified by the `upload_lib.META` constant. It retrieves all documents from this collection and stores them as a list. ```python metas = list(upload_lib.get_db('prod', upload_lib.META).find()) ``` -------------------------------- ### Creating a Python Dictionary Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/scan_test.ipynb Creates a standard Python dictionary using the `dict()` constructor. It initializes the dictionary with one key-value pair from a literal dictionary `{'b': 1}` and adds another key-value pair `a=2` using keyword arguments. ```python dict({'b': 1}, a=2) ``` -------------------------------- ### Copying Selected Files to New Zip Archive (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/make_test_dataset.ipynb Defines the path for the destination zip file for the test dataset. If it exists, it's removed. It then prepares a list of tuples, each containing a source raw zip path and the list of filenames to copy from it. Finally, it uses a utility function `utils.copy_multi_zip_files` to perform the copying operation into the destination zip. ```python dest_zip = os.path.join(root, f'TestDataset-{size}.zip') if os.path.exists(dest_zip): os.remove(dest_zip) sources_and_files = [ (os.path.join(root, 'Raw', raw), filenames) for raw, filenames in files_to_copy.items() ] utils.copy_multi_zip_files(sources_and_files, dest_zip) ``` -------------------------------- ### Loading Pickled Data Bytes - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/local_parse_analysis.ipynb Constructs the full path to the pickled data file and reads its content into a byte string. It then prints the size of the loaded data in bytes. ```python parsed_path = os.path.join(root, 'parsed.pkl') with open(parsed_path, 'rb') as f: data_bytes = f.read() len(data_bytes) ``` -------------------------------- ### Mapping Raw and SLP Documents by Key Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parse_failures.ipynb Creates two separate dictionaries. `key_to_raw` maps the 'key' field from raw documents to the document itself, and `key_to_slp` does the same for SLP documents. ```python key_to_raw = {raw['key']: raw for raw in prod_raw} key_to_slp = {slp['key']: slp for slp in prod_sl ``` -------------------------------- ### Initializing Interactive Tables (itables) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/raw_analysis.ipynb Initializes the `itables` library for displaying pandas DataFrames interactively, typically in a notebook environment. Setting `all_interactive=True` makes all DataFrames interactive by default. ```Python from itables import init_notebook_mode init_notebook_mode(all_interactive=True) ``` -------------------------------- ### Listing Local SLP Files (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/compression_analysis.ipynb Creates a list of full file paths for all files found within the previously defined local SLP data directory. ```python paths = [os.path.join(slp_dir, f) for f in os.listdir(slp_dir)] ``` -------------------------------- ### Initializing Interactive Tables - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/extract_vs_phillip_replays.ipynb Initializes the `itables` library for displaying pandas DataFrames as interactive tables within a Jupyter notebook environment. Sets `all_interactive=True` for default interactivity. ```python from itables import init_notebook_mode init_notebook_mode(all_interactive=True) ``` -------------------------------- ### Importing and Reloading Test Module (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Imports the `test_peppi` module from `slippi_db` and immediately reloads it using `importlib.reload`. ```python from slippi_db import test_peppi from importlib import reload reload(test_peppi) ``` -------------------------------- ### Importing Libraries for Slippi Data Processing - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/extract_vs_phillip_replays.ipynb Imports necessary Python libraries and custom modules from `slippi_ai` and `slippi_db` for data manipulation, parsing, and analysis of Slippi replay files. Includes standard libraries like pandas and os, and specialized ones like melee and peppi_py. ```python import collections import pickle import pandas as pd import os import peppi_py import tree import json import io import math import tqdm.notebook import functools import typing as tp import itertools import melee from slippi_ai import nametags from slippi_db import utils, preprocessing, parse_peppi from slippi_db.scripts.make_local_dataset import check_replay ``` -------------------------------- ### Importing peppi_py library (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_testing.ipynb Imports the `peppi_py` library, which is likely used for parsing or interacting with `.slp` files (Slippi replay files). ```python import peppi_py ``` -------------------------------- ### Defining Local Data Directory (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/compression_analysis.ipynb Constructs the path for a local directory where downloaded SLP files will be stored, based on the environment name. ```python slp_dir = f'data/{env}' ``` -------------------------------- ### Mapping Meta Documents by Key (from metas list) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parse_failures.ipynb Creates a dictionary where each key is the value of the 'key' field from a meta document in the `metas` list. The corresponding value in the dictionary is the entire meta document. ```python key_to_meta = {meta['key']: meta for meta in metas} ``` -------------------------------- ### Project Dependencies List - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/requirements-b9.txt Specifies the Python packages and versions required to run the project. Includes standard packages, packages from Git repositories, and specific versions for compatibility. ```Python pandas==1.4.4 dm-sonnet git+https://github.com/vladfi1/libmelee.git@dev gdown wandb git+https://github.com/mbr/simplekv.git boto3 pymongo dnspython fancyflags pyarrow py7zr # for nvcr's tensorflow 2.3 numpy==1.18.5 tensorflow_probability==0.11 ``` -------------------------------- ### Project Dependencies - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/requirements.txt Specifies the Python packages required to run the project, including specific versions or repository links. ```Python pandas dm-sonnet git+https://github.com/vladfi1/libmelee.git@v0.38.1 gdown tensorflow_probability[tf] wandb dnspython fancyflags pyarrow git+https://github.com/vladfi1/py7zr.git@dev parameterized ``` -------------------------------- ### Mapping Individual Files to Raw Zip Files (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/make_test_dataset.ipynb Iterates through the `raw_to_files` mapping and builds a reverse mapping `file_to_raw`, where each individual filename is mapped back to the raw zip file it was found in. This handles cases where a file might appear in multiple raw dumps. ```python file_to_raw = {} for raw, files in raw_to_files.items(): for f in files: # File could be in two different dumps, one for each player file_to_raw[f] = raw ``` -------------------------------- ### Querying SLP Database Info (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/compression_analysis.ipynb Connects to the SLP database for the specified environment, retrieves all document metadata, and calculates the total original and compressed sizes of the stored SLP files. ```python slp_db = upload_lib.get_db(env, upload_lib.SLP) slp_infos = list(slp_db.find({})) slp_compressed_size = sum(info['stored_size'] for info in slp_infos) slp_size = sum(info['original_size'] for info in slp_infos) ``` -------------------------------- ### Compiling Selected Replays into Zip Archive - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/extract_vs_phillip_replays.ipynb Defines the destination path for the zip archive (`dest_zip`). Removes the zip file if it already exists. Constructs a list of source directory paths and the corresponding files to copy. Uses a utility function `utils.copy_multi_zip_files` to copy the specified files from their source directories into the destination zip archive. ```python dest_zip = os.path.join(dataset_root, 'PhillipMatchupCompilation.zip') if os.path.exists(dest_zip): os.remove(dest_zip) sources_and_files = [ (os.path.join(dataset_root, 'Raw', raw), filenames) for raw, filenames in files_to_copy.items() ] utils.copy_multi_zip_files(sources_and_files, dest_zip) ``` -------------------------------- ### Fetching SLP Documents from Production DB Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parse_failures.ipynb Connects to the 'slp' collection in the 'prod' database using `upload_lib.get_db`. It then fetches all documents from this collection and converts the resulting cursor into a Python list. ```python prod_slp = upload_lib.get_db('prod', 'slp') prod_slp = list(prod_slp.find()) ``` -------------------------------- ### Loading Parsed Data from Pickle File (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/make_test_dataset.ipynb Reads the byte content of a pickle file located at `parsed_path`, which presumably contains pre-processed Slippi replay data. It then prints the size of the loaded data in bytes. ```python parsed_path = os.path.join(root, 'parsed.pkl') with open(parsed_path, 'rb') as f: data_bytes = f.read() len(data_bytes) ``` -------------------------------- ### Defining test file paths (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_testing.ipynb Sets variables `test_file` and `buggy_file` to string paths pointing to sample `.slp` files, likely used for testing or analysis. ```python test_file = '../data/test/Game_20211113T124748.slp' buggy_file = '../data/test/18_29_53 Kirby + Falco.slp' ``` -------------------------------- ### Mapping Parsed Documents by Key Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parse_failures.ipynb Creates a dictionary where each key is the value of the 'key' field from a parsed document in the `prod_pq` list. The corresponding value in the dictionary is the entire parsed document. ```python key_to_parse = {doc['key']: doc for doc in prod_pq} ``` -------------------------------- ### Initializing In-Memory Byte Stream - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parquet.ipynb Creates an in-memory binary stream using `io.BytesIO`, which can be used like a file object for reading and writing bytes. ```Python f = io.BytesIO() ``` -------------------------------- ### Asserting Consistent File Parsing (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Calls the `preprocessing.assert_same_parse` function on a specific file path to verify that parsing the file multiple times yields consistent results. ```python path = 'data/test/start/18_29_53 Kirby + Falco.slp' preprocessing.assert_same_parse(path) ``` -------------------------------- ### Importing Libraries for Data Processing Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/raw_analysis.ipynb Imports the pandas library for data manipulation and the `upload_lib` module from `slippi_db` for database interaction. These are essential prerequisites for fetching and processing the data. ```Python import pandas as pd from slippi_db import upload_lib ``` -------------------------------- ### Mapping SLP Keys to Raw Keys (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Extracts the 'key' from documents in `non_parse`, maps these keys to their corresponding 'raw_key' using the `slp_to_raw` dictionary, and prints the set of unique raw keys. ```python keys = [d['key'] for d in non_parse] raw_keys = set([slp_to_raw[k] for k in keys]) print(raw_keys) ``` -------------------------------- ### Loading Parsed Data from Pickle File Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_panic.ipynb Opens a pickle file named 'parsed.pkl' located in the `root` directory in binary read mode (`'rb'`) and loads its content into the `db` variable using `pickle.load()`. This assumes the file contains a serialized Python object. ```python with open(os.path.join(root, 'parsed.pkl'), 'rb') as f: db = pickle.load(f) ``` -------------------------------- ### Parsing Slippi game (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_testing.ipynb Parses the specified `test_file` using `peppi_py.game`, loading the game data into the `game` variable for further processing. ```python game = peppi_py.game(test_file) ``` -------------------------------- ### Verifying Compression Consistency (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/compression_analysis.ipynb Checks that all retrieved SLP file entries have the same compression type and prints the identified compression method. ```python compression = slp_infos[0]['compression'] for info in slp_infos: assert info['compression'] == compression print(compression) ``` -------------------------------- ### Creating Size Comparison DataFrame (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/compression_analysis.ipynb Creates a Pandas DataFrame from the calculated dataset sizes, including columns for the dataset name, its size, and its size relative to the 'uncompressed' dataset size. ```python names, sizes_ = zip(*sizes.items()) df = pd.DataFrame({'name': names, 'size': sizes_}) df['relative_size'] = df['size'] / sizes['uncompressed'] ``` -------------------------------- ### Parsing Slippi File with slippi_db - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parquet.ipynb Uses the `parse_libmelee.get_slp` function to parse a specific Slippi replay file (`.slp`) and stores the resulting game data in the `game` variable. ```Python game = parse_libmelee.get_slp('peppi_test/21_35_46 [NOAH] Ice Climbers + [TITP] Captain Falcon (FD).slp') ``` -------------------------------- ### Parsing Local SLP Files (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/compression_analysis.ipynb Iterates through the list of local SLP file paths, attempts to parse each file using parse_libmelee.get_slp (commented out parse_peppi), collects the parsed data arrays, and records any errors encountered during parsing. The %%time magic command indicates timing the execution. ```python %%time pa_arrays = [] errors = [] for path in paths: print(path) try: # pa_arrays.append(parse_peppi.get_slp(path)) pa_arrays.append(parse_libmelee.get_slp(path)) except InvalidGameError as e: errors.append((path, e)) len(errors) ``` -------------------------------- ### Importing slippi_db upload_lib Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parse_failures.ipynb Imports the `upload_lib` module from the `slippi_db` library. This module provides utilities for interacting with the Slippi database, particularly for handling data uploads and retrieval. ```python from slippi_db import upload_lib ``` -------------------------------- ### Fetching Raw Documents from Production DB Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parse_failures.ipynb Connects to the raw documents collection in the 'prod' database, identified by the `upload_lib.RAW` constant. It retrieves all documents from this collection and stores them as a list. ```python prod_raw = list(upload_lib.get_db('prod', upload_lib.RAW).find()) ``` -------------------------------- ### Mapping Raw Zip Files to Contained Files (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/make_test_dataset.ipynb Iterates through the unique raw file paths in the DataFrame, checks if they are zip files and exist, and uses the `get_files_in_zip` function to build a dictionary mapping each raw zip file path to a set of the filenames it contains. A progress bar is used to track the process. ```python raw_to_files = {} for raw in tqdm.notebook.tqdm(set(df['raw'])): if not raw.endswith('.zip'): continue if not raw_exists(raw): continue raw_to_files[raw] = get_files_in_zip(os.path.join(root, 'Raw', raw)) ``` -------------------------------- ### Creating PyArrow Table from Game Data - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parquet.ipynb Constructs a PyArrow `Table` object from the parsed game data, placing the data into a single column named 'root'. ```Python table = pa.Table.from_arrays([game], names=['root']) ``` -------------------------------- ### Accessing player 0 data and defining helper (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_testing.ipynb Navigates through the nested data structure to access data for player 0 ('p0'), their 'leader' state, 'post' frame data, and 'position', defining a lambda function `get_post` to easily retrieve post-frame fields as NumPy arrays. ```python p0 = ports.field('0') leader = p0.field('leader') post = leader.field('post') position = post.field('position') get_post = lambda key: post.field(key).to_numpy() ``` -------------------------------- ### Importing Slippi AI Embedding Modules - Python Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/embed_test.ipynb Imports the `networks` and `embed` modules from the `slippi_ai` library. These modules are expected to contain definitions and utilities for handling game state and controller input embeddings within the AI project. ```python from slippi_ai import networks, embed ``` -------------------------------- ### Defining Raw Data Path and Asserting Existence Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/peppi_panic.ipynb Constructs the full path to a raw zip file ('Summit-004.zip' within the 'Raw' subdirectory of the `root` path) and stores it in the `raw` variable. An `assert` statement checks if this file actually exists, raising an error if it doesn't. ```python raw = os.path.join(root, 'Raw/Summit-004.zip') assert os.path.exists(raw) ``` -------------------------------- ### Analyzing Loaded Local Games (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/db_analysis.ipynb Iterates through the list of `local_games` and calls the `analyze_game` function on each game object. ```python for g in local_games: analyze_game(g) ``` -------------------------------- ### Grouping Bogus Keys by Failure Reason Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/parse_failures.ipynb Initializes a `collections.defaultdict` with a list factory. It then iterates through the `bogus` keys, looks up the parsed document for each key, and appends the key to the list associated with its 'reason' field. ```python import collections reason_to_key = collections.defaultdict(list) for key in bogus: reason_to_key[key_to_parse[key]['reason']].append(key) ``` -------------------------------- ### Printing Size Ratios Relative to SLP (Python) Source: https://github.com/vladfi1/slippi-ai/blob/main/notebooks/compression_analysis.ipynb Iterates through the sizes dictionary, sorted by size, and prints each dataset name along with its size ratio relative to the original SLP file size. ```python for name in sorted(sizes, key=lambda n: sizes[n]): print(name, '%.3f' % (sizes['slp'] / sizes[name])) ```