Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions set/colab/setup.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# MindMutant Colab Setup\n",
"\n",
"Run this notebook to set up the MindMutant environment in Google Colab.\n",
"This script handles:\n",
"1. Google Drive mounting for data persistence\n",
"2. Repository cloning\n",
"3. Swapping to Colab-optimized source code (`src-colab`)\n",
"4. Dependency installation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 1. Mount Google Drive\n",
"from google.colab import drive\n",
"drive.mount('/content/drive')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 2. Clone Repository\n",
"import os\n",
"\n",
"if not os.path.exists('MindMutant'):\n",
" !git clone https://github.com/bonsai/MindMutant.git\n",
" %cd MindMutant\n",
"else:\n",
" %cd MindMutant\n",
" !git pull"
Comment on lines +37 to +42

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current logic for cloning the repository is not idempotent. If this cell is run a second time, the current working directory will likely be inside the repository, causing os.path.exists('MindMutant') to be False and the script to erroneously attempt to clone the repository inside itself. Using absolute paths makes the script more robust and ensures it behaves correctly on subsequent runs.

repo_path = '/content/MindMutant'
if not os.path.exists(repo_path):
    !git clone https://github.com/bonsai/MindMutant.git $repo_path

%cd $repo_path
!git pull

]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 3. Setup Data Persistence\n",
"import os\n",
"\n",
"# Define Drive data path\n",
"drive_data_path = '/content/drive/MyDrive/MindMutant/data'\n",
"\n",
"# Create directory in Drive if it doesn't exist\n",
"if not os.path.exists(drive_data_path):\n",
" os.makedirs(drive_data_path)\n",
" print(f\"Created {drive_data_path}\")\n",
"\n",
"# Link local data directory to Drive\n",
"# We remove the local 'data' folder (from git) and replace it with a symlink to Drive\n",
"if os.path.exists('data'):\n",
" if not os.path.islink('data'):\n",
" !rm -rf data\n",
" print(\"Removed default data folder\")\n",
"\n",
"if not os.path.exists('data'):\n",
" !ln -s \"$drive_data_path\" data\n",
" print(f\"Linked data directory to {drive_data_path}\")\n",
Comment on lines +69 to +71

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use python var for symlink target

The !ln -s "$drive_data_path" data line runs in a shell where $drive_data_path is not defined (it’s a Python variable, not an environment variable), so the command expands to an empty string and fails to create the symlink. In Colab this means the data directory is never linked to Drive, so any generated data stays in the ephemeral runtime and is lost on restart. Use os.symlink(drive_data_path, 'data') or !ln -s "{drive_data_path}" data to interpolate the Python variable into the shell command.

Useful? React with 👍 / 👎.

"else:\n",
" print(\"Data directory already linked\")"
Comment on lines +64 to +73

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current logic for creating the data symlink has a bug when data is a broken symlink. In that case, os.path.exists('data') returns False, and the script proceeds to !ln -s ..., which fails because the broken symlink file still exists. The logic should be made more robust to handle this case correctly.

if os.path.islink('data'):
    # It's a link. If it's broken, os.path.exists is False.
    if os.path.exists('data'):
        print("Data directory already linked")
    else:
        print("Recreating broken data symlink...")
        !rm -f data
        !ln -s "$drive_data_path" data
        print(f"Linked data directory to {drive_data_path}")
else:
    if os.path.lexists('data'): # It's a file or directory
        !rm -rf data
        print("Removed default data folder")
    !ln -s "$drive_data_path" data
    print(f"Linked data directory to {drive_data_path}")

]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 4. Switch to Colab-Optimized Code\n",
"# We replace the standard 'src' directory with 'src-colab' to use the optimized version\n",
"if os.path.exists('src-colab'):\n",
" if os.path.exists('src'):\n",
" !rm -rf src\n",
" !mv src-colab src\n",
" print(\"Switched to Colab-optimized source code (src-colab -> src)\")\n",
"else:\n",
" print(\"src-colab not found (already switched?)\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 5. Install Dependencies\n",
"!pip install -r set/colab/requirements.txt"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The pip install command can produce a lot of output. Using the -q (quiet) flag can make the notebook execution cleaner and easier to read by suppressing installation messages.

!pip install -q -r set/colab/requirements.txt

]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 6. Download Spacy Model\n",
"!python -m spacy download ja_core_news_md"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 7. Verify Setup\n",
"import spacy\n",
"from src.deap.evolution import Evolution\n",
"\n",
"nlp = spacy.load(\"ja_core_news_md\")\n",
"print(\"✅ Spacy model loaded successfully!\")\n",
"print(\"✅ MindMutant modules importable!\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}