{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":91249,"databundleVersionId":11294684,"sourceType":"competition"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:46.586121Z","iopub.execute_input":"2025-05-27T06:39:46.586784Z","iopub.status.idle":"2025-05-27T06:39:46.591200Z","shell.execute_reply.started":"2025-05-27T06:39:46.586751Z","shell.execute_reply":"2025-05-27T06:39:46.590166Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Install depedencies","metadata":{}},{"cell_type":"code","source":"!pip install ultralytics\n!pip install plotly","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:46.592306Z","iopub.execute_input":"2025-05-27T06:39:46.592541Z","iopub.status.idle":"2025-05-27T06:39:53.283464Z","shell.execute_reply.started":"2025-05-27T06:39:46.592524Z","shell.execute_reply":"2025-05-27T06:39:53.282352Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!tar xfvz /kaggle/input/ultralytics-for-offline-install/archive.tar.gz\n!pip install --no-index --find-links=./packages ultralytics\n!rm -rf ./packages","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:53.285705Z","iopub.execute_input":"2025-05-27T06:39:53.286334Z","iopub.status.idle":"2025-05-27T06:39:57.170071Z","shell.execute_reply.started":"2025-05-27T06:39:53.286309Z","shell.execute_reply":"2025-05-27T06:39:57.168852Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Declare ","metadata":{}},{"cell_type":"code","source":"import os\nimport random\nimport numpy as np\nimport torch\n\n# Define global constants for dataset directories\nDATA_DIR = '/kaggle/input/byu-locating-bacterial-flagellar-motors-2025'\nTRAIN_CSV = os.path.join(DATA_DIR, 'train_labels.csv')\nTRAIN_DIR = os.path.join(DATA_DIR, 'train')\nTEST_DIR = os.path.join(DATA_DIR, 'test')\nOUTPUT_DIR = './output'\nMODEL_DIR = './models'\n\n# Create output directories if they don't exist\ntry:\n    os.makedirs(OUTPUT_DIR, exist_ok=True)\n    os.makedirs(MODEL_DIR, exist_ok=True)\n    print(f\"Output directories created: {OUTPUT_DIR} and {MODEL_DIR}\")\nexcept OSError as e:\n    print(f\"Error creating directories: {e}\")\n\n# Set device: Use GPU if available; otherwise, fall back to CPU\nDEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nprint(f\"Using device: {DEVICE}\")\n\n# Set random seeds for reproducibility\nRANDOM_SEED = 42\n\n# Set random seeds for Python, NumPy, and PyTorch\nrandom.seed(RANDOM_SEED)\nnp.random.seed(RANDOM_SEED)\ntorch.manual_seed(RANDOM_SEED)\n\n# Set random seed for CUDA if available\nif torch.cuda.is_available():\n    torch.cuda.manual_seed(RANDOM_SEED)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False  # Disable benchmarking for reproducibility\n\nprint(\"Environment setup complete.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:57.171479Z","iopub.execute_input":"2025-05-27T06:39:57.171796Z","iopub.status.idle":"2025-05-27T06:39:57.182836Z","shell.execute_reply.started":"2025-05-27T06:39:57.171771Z","shell.execute_reply":"2025-05-27T06:39:57.182026Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom IPython.display import display\n\n# Load the training labels CSV into a pandas DataFrame\nTRAIN_CSV = os.path.join(DATA_DIR, 'train_labels.csv')\n\ntry:\n    train_labels = pd.read_csv(TRAIN_CSV)\n    print(\"Training dataset loaded successfully.\")\nexcept FileNotFoundError:\n    print(f\"Error: The file {TRAIN_CSV} was not found.\")\nexcept Exception as e:\n    print(f\"An error occurred while loading the file: {e}\")\n\n# Display basic dataset information\nprint(\"\\nTraining dataset shape:\", train_labels.shape)\nprint(\"\\nColumns in the dataset:\")\nprint(train_labels.columns.tolist())\n\n# Display basic statistics for numerical columns\nprint(\"\\nBasic statistics:\")\ndisplay(train_labels.describe())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:57.184709Z","iopub.execute_input":"2025-05-27T06:39:57.184910Z","iopub.status.idle":"2025-05-27T06:39:57.236311Z","shell.execute_reply.started":"2025-05-27T06:39:57.184895Z","shell.execute_reply":"2025-05-27T06:39:57.235697Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Tomogram and Motor Statistics","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\n# Load the training labels CSV into a pandas DataFrame\nTRAIN_CSV = os.path.join(DATA_DIR, 'train_labels.csv')\n\ntry:\n    train_labels = pd.read_csv(TRAIN_CSV)\n    print(\"Training dataset loaded successfully.\")\nexcept FileNotFoundError:\n    print(f\"Error: The file {TRAIN_CSV} was not found.\")\nexcept Exception as e:\n    print(f\"An error occurred while loading the file: {e}\")\n\n# Count unique tomograms in the dataset\nunique_tomo_count = train_labels['tomo_id'].nunique()\nprint(f\"\\nNumber of unique tomograms: {unique_tomo_count}\")\n\n# Compute distribution of motors per tomogram\nmotors_per_tomo = train_labels.groupby('tomo_id')['Number of motors'].first().value_counts().sort_index()\nprint(\"\\nDistribution of motors per tomogram:\")\nprint(motors_per_tomo)\n\n# Visualize the distribution with a bar plot\nplt.figure(figsize=(10, 6))\nmotors_per_tomo.plot(kind='bar', color='skyblue', edgecolor='black')\nplt.title('Distribution of Motors per Tomogram', fontsize=16)\nplt.xlabel('Number of Motors', fontsize=14)\nplt.ylabel('Frequency', fontsize=14)\nplt.xticks(rotation=0, fontsize=12)\nplt.yticks(fontsize=12)\nplt.grid(axis='y', linestyle='--', alpha=0.7)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:57.237186Z","iopub.execute_input":"2025-05-27T06:39:57.237418Z","iopub.status.idle":"2025-05-27T06:39:57.468321Z","shell.execute_reply.started":"2025-05-27T06:39:57.237400Z","shell.execute_reply":"2025-05-27T06:39:57.467440Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":" ### 1. Bar Plot with Seaborn","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Load the training labels CSV into a pandas DataFrame\nTRAIN_CSV = os.path.join(DATA_DIR, 'train_labels.csv')\n\ntry:\n    train_labels = pd.read_csv(TRAIN_CSV)\n    print(\"Training dataset loaded successfully.\")\nexcept FileNotFoundError:\n    print(f\"Error: The file {TRAIN_CSV} was not found.\")\nexcept Exception as e:\n    print(f\"An error occurred while loading the file: {e}\")\n\n# Count unique tomograms in the dataset\nunique_tomo_count = train_labels['tomo_id'].nunique()\nprint(f\"\\nNumber of unique tomograms: {unique_tomo_count}\")\n\n# Compute distribution of motors per tomogram\nmotors_per_tomo = train_labels.groupby('tomo_id')['Number of motors'].first().value_counts().sort_index()\nprint(\"\\nDistribution of motors per tomogram:\")\nprint(motors_per_tomo)\n\n# Visualize the distribution with a bar plot using Seaborn\nplt.figure(figsize=(10, 6))\nsns.barplot(x=motors_per_tomo.index, y=motors_per_tomo.values, palette='viridis')\nplt.title('Distribution of Motors per Tomogram', fontsize=16)\nplt.xlabel('Number of Motors', fontsize=14)\nplt.ylabel('Frequency', fontsize=14)\nplt.xticks(rotation=0, fontsize=12)\nplt.yticks(fontsize=12)\nplt.grid(axis='y', linestyle='--', alpha=0.7)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:57.469344Z","iopub.execute_input":"2025-05-27T06:39:57.469982Z","iopub.status.idle":"2025-05-27T06:39:57.698760Z","shell.execute_reply.started":"2025-05-27T06:39:57.469953Z","shell.execute_reply":"2025-05-27T06:39:57.697956Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 2. Histogram","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Load the training labels CSV into a pandas DataFrame\nTRAIN_CSV = os.path.join(DATA_DIR, 'train_labels.csv')\n\ntry:\n    train_labels = pd.read_csv(TRAIN_CSV)\n    print(\"Training dataset loaded successfully.\")\nexcept FileNotFoundError:\n    print(f\"Error: The file {TRAIN_CSV} was not found.\")\nexcept Exception as e:\n    print(f\"An error occurred while loading the file: {e}\")\n\n# Count unique tomograms in the dataset\nunique_tomo_count = train_labels['tomo_id'].nunique()\nprint(f\"\\nNumber of unique tomograms: {unique_tomo_count}\")\n\n# Compute distribution of motors per tomogram\nmotors_per_tomo = train_labels.groupby('tomo_id')['Number of motors'].first().value_counts().sort_index()\nprint(\"\\nDistribution of motors per tomogram:\")\nprint(motors_per_tomo)\n\n# Visualize the distribution with a histogram\nplt.figure(figsize=(10, 6))\nsns.histplot(motors_per_tomo, bins=10, kde=True, color='skyblue', edgecolor='black')\nplt.title('Distribution of Motors per Tomogram', fontsize=16)\nplt.xlabel('Number of Motors', fontsize=14)\nplt.ylabel('Frequency', fontsize=14)\nplt.xticks(fontsize=12)\nplt.yticks(fontsize=12)\nplt.grid(axis='y', linestyle='--', alpha=0.7)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:57.699710Z","iopub.execute_input":"2025-05-27T06:39:57.700486Z","iopub.status.idle":"2025-05-27T06:39:57.978435Z","shell.execute_reply.started":"2025-05-27T06:39:57.700466Z","shell.execute_reply":"2025-05-27T06:39:57.977758Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 3. Box Plot","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Load the training labels CSV into a pandas DataFrame\nTRAIN_CSV = os.path.join(DATA_DIR, 'train_labels.csv')\n\ntry:\n    train_labels = pd.read_csv(TRAIN_CSV)\n    print(\"Training dataset loaded successfully.\")\nexcept FileNotFoundError:\n    print(f\"Error: The file {TRAIN_CSV} was not found.\")\nexcept Exception as e:\n    print(f\"An error occurred while loading the file: {e}\")\n\n# Count unique tomograms in the dataset\nunique_tomo_count = train_labels['tomo_id'].nunique()\nprint(f\"\\nNumber of unique tomograms: {unique_tomo_count}\")\n\n# Compute distribution of motors per tomogram\nmotors_per_tomo = train_labels.groupby('tomo_id')['Number of motors'].first().value_counts().sort_index()\nprint(\"\\nDistribution of motors per tomogram:\")\nprint(motors_per_tomo)\n\n# Visualize the distribution with a box plot\nplt.figure(figsize=(10, 6))\nsns.boxplot(x=motors_per_tomo.index, y=motors_per_tomo)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:57.979273Z","iopub.execute_input":"2025-05-27T06:39:57.979586Z","iopub.status.idle":"2025-05-27T06:39:58.188374Z","shell.execute_reply.started":"2025-05-27T06:39:57.979561Z","shell.execute_reply":"2025-05-27T06:39:58.187614Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Additional Dataset Insights","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom IPython.display import display\n\n# Load the training labels CSV into a pandas DataFrame\nTRAIN_CSV = os.path.join(DATA_DIR, 'train_labels.csv')\n\ntry:\n    train_labels = pd.read_csv(TRAIN_CSV)\n    print(\"Training dataset loaded successfully.\")\nexcept FileNotFoundError:\n    print(f\"Error: The file {TRAIN_CSV} was not found.\")\nexcept Exception as e:\n    print(f\"An error occurred while loading the file: {e}\")\n\n# Display a few sample rows from the training labels\nprint(\"\\nSample rows from training labels:\")\ndisplay(train_labels.head())\n\n# Check for missing values in each column\nprint(\"\\nMissing values per column:\")\ndisplay(train_labels.isnull().sum())\n\n# Explore the range of tomogram sizes along each axis\nprint(\"\\nTomogram size ranges:\")\nprint(\"Z-axis (slices):\", train_labels['Array shape (axis 0)'].min(), \"to\", train_labels['Array shape (axis 0)'].max())\nprint(\"X-axis (width):\", train_labels['Array shape (axis 1)'].min(), \"to\", train_labels['Array shape (axis 1)'].max())\nprint(\"Y-axis (height):\", train_labels['Array shape (axis 2)'].min(), \"to\", train_labels['Array shape (axis 2)'].max())\n\n# Display voxel spacing distribution\nprint(\"\\nVoxel spacing distribution:\")\nvoxel_spacing_counts = train_labels['Voxel spacing'].value_counts().sort_index()\ndisplay(voxel_spacing_counts)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:58.189323Z","iopub.execute_input":"2025-05-27T06:39:58.189608Z","iopub.status.idle":"2025-05-27T06:39:58.218054Z","shell.execute_reply.started":"2025-05-27T06:39:58.189582Z","shell.execute_reply":"2025-05-27T06:39:58.217431Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Understanding Motor Axis 3D and Data Interpretation","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport plotly.express as px\n\n# Load the training labels CSV into a pandas DataFrame\nTRAIN_CSV = os.path.join(DATA_DIR, 'train_labels.csv')\n\ntry:\n    train_labels = pd.read_csv(TRAIN_CSV)\n    print(\"Training dataset loaded successfully.\")\nexcept FileNotFoundError:\n    print(f\"Error: The file {TRAIN_CSV} was not found.\")\nexcept Exception as e:\n    print(f\"An error occurred while loading the file: {e}\")\n\n# Create a 3D scatter plot\nfig_motor = px.scatter_3d(\n    train_labels, \n    x='Motor axis 0', \n    y='Motor axis 1', \n    z='Motor axis 2',\n    color='Number of motors', \n    color_continuous_scale=\"viridis\",  # Using a vibrant color scheme\n    size_max=8, \n    width=900, \n    height=600, \n    opacity=0.85, \n    template=\"plotly_white\",  # Lighter theme for better contrast\n    title=\"3D Scatter Plot: Motor Axes\"\n)\n\n# Customize the layout\nfig_motor.update_layout(\n    title={\n        'text': \"3D Scatter Plot: Motor Axes\",\n        'y': 0.95,\n        'x': 0.5,\n        'xanchor': 'center',\n        'yanchor': 'top',\n        'font': {'size': 18, 'color': 'black'}\n    },\n    font=dict(size=12, color=\"black\"),\n    legend=dict(font=dict(size=14, color=\"black\")),\n    margin=dict(l=10, r=10, b=10, t=50),\n    scene=dict(\n        xaxis_title='Motor Axis 0',\n        yaxis_title='Motor Axis 1',\n        zaxis_title='Motor Axis 2',\n        xaxis=dict(title_font=dict(size=14, color=\"black\")),\n        yaxis=dict(title_font=dict(size=14, color=\"black\")),\n        zaxis=dict(title_font=dict(size=14, color=\"black\")),\n    )\n)\n\n# Add hover information\nfig_motor.update_traces(\n    hovertemplate=(\n        \"Motor Axis 0: %{x}<br>\" +\n        \"Motor Axis 1: %{y}<br>\" +\n        \"Motor Axis 2: %{z}<br>\" +\n        \"Number of Motors: %{marker.color}<br>\"\n    )\n)\n\nfig_motor.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:58.220577Z","iopub.execute_input":"2025-05-27T06:39:58.220811Z","iopub.status.idle":"2025-05-27T06:39:58.307297Z","shell.execute_reply.started":"2025-05-27T06:39:58.220794Z","shell.execute_reply":"2025-05-27T06:39:58.306610Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport plotly.express as px\n\n# Load the training labels CSV into a pandas DataFrame\nTRAIN_CSV = os.path.join(DATA_DIR, 'train_labels.csv')\n\ntry:\n    train_labels = pd.read_csv(TRAIN_CSV)\n    print(\"Training dataset loaded successfully.\")\n    print(train_labels.head())  # Display the first few rows to check the data\nexcept FileNotFoundError:\n    print(f\"Error: The file {TRAIN_CSV} was not found.\")\nexcept Exception as e:\n    print(f\"An error occurred while loading the file: {e}\")\n\n# Check if the required columns exist\nrequired_columns = ['Array shape (axis 0)', 'Array shape (axis 1)', 'Array shape (axis 2)', 'Number of motors']\nmissing_columns = [col for col in required_columns if col not in train_labels.columns]\nif missing_columns:\n    print(f\"Error: Missing required columns: {missing_columns}\")\nelse:\n    # Create a 3D scatter plot for tomogram shapes\n    fig_shape = px.scatter_3d(\n        train_labels, \n        x='Array shape (axis 0)', \n        y='Array shape (axis 1)', \n        z='Array shape (axis 2)',\n        color='Number of motors', \n        color_continuous_scale=\"magma\",  # More contrast for clarity\n        size_max=8, \n        width=900, \n        height=600, \n        opacity=0.85, \n        template=\"seaborn\",  # New theme for a scientific feel\n        title=\"🧬 3D Scatter Plot: Tomogram Shapes\"\n    )\n\n    # Customize the layout\n    fig_shape.update_layout(\n        title={\n            'text': \"🧬 3D Scatter Plot: Tomogram Shapes\",\n            'y': 0.95,\n            'x': 0.5,\n            'xanchor': 'center',\n            'yanchor': 'top',\n            'font': {'size': 18, 'color': 'black'}\n        },\n        font=dict(size=12, color=\"black\"),\n        legend=dict(font=dict(size=14, color=\"black\")),\n        margin=dict(l=10, r=10, b=10, t=50),\n        scene=dict(\n            xaxis_title='Array Shape (Axis 0)',\n            yaxis_title='Array Shape (Axis 1)',\n            zaxis_title='Array Shape (Axis 2)',\n            xaxis=dict(title_font=dict(size=14, color=\"black\")),\n            yaxis=dict(title_font=dict(size=14, color=\"black\")),\n            zaxis=dict(title_font=dict(size=14, color=\"black\")),\n        )\n    )\n\n    # Add hover information\n    fig_shape.update_traces(\n        hovertemplate=(\n            \"Array Shape (Axis 0): %{x}<br>\" +\n            \"Array Shape (Axis 1): %{y}<br>\" +\n            \"Array Shape (Axis 2): %{z}<br>\" +\n            \"Number of Motors: %{marker.color}<br>\"\n        )\n    )\n\n    fig_shape.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:58.308131Z","iopub.execute_input":"2025-05-27T06:39:58.308423Z","iopub.status.idle":"2025-05-27T06:39:58.392451Z","shell.execute_reply.started":"2025-05-27T06:39:58.308393Z","shell.execute_reply":"2025-05-27T06:39:58.391658Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Descriptive Statistics and Distribution¶","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\n# Load the training labels CSV into a pandas DataFrame\nTRAIN_CSV = os.path.join(DATA_DIR, 'train_labels.csv')\n\ntry:\n    train_labels = pd.read_csv(TRAIN_CSV)\n    print(\"Training dataset loaded successfully.\")\n    print(train_labels.head())  # Display the first few rows to check the data\nexcept FileNotFoundError:\n    print(f\"Error: The file {TRAIN_CSV} was not found.\")\nexcept Exception as e:\n    print(f\"An error occurred while loading the file: {e}\")\n\n# Show descriptive statistics\ndescriptive_stats = train_labels.describe().loc[['mean', 'min', 'max']].T\ndisplay(descriptive_stats)\n\n# Improved histogram design\ntrain_labels.hist(\n    bins=30, \n    figsize=(14, 10), \n    layout=(3, 4), \n    edgecolor=\"black\", \n    color=\"#4CAF50\",  # Greenish color theme\n    grid=False\n)\n\n# Customize the plot\nplt.suptitle(\"Feature Distributions\", fontsize=16, fontweight='bold', color=\"darkblue\")\nplt.tight_layout(rect=[0, 0, 1, 0.96])  # Adjust layout to fit the suptitle\n\n# Adjust subplot titles and labels\nfor ax in plt.gcf().axes:\n    ax.set_title(ax.get_title(), fontsize=12, fontweight='bold', color=\"darkblue\")\n    ax.set_xlabel(ax.get_xlabel(), fontsize=10, color=\"black\")\n    ax.set_ylabel(ax.get_ylabel(), fontsize=10, color=\"black\")\n    ax.tick_params(axis='both', which='major', labelsize=10, colors=\"black\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:39:58.393316Z","iopub.execute_input":"2025-05-27T06:39:58.393633Z","iopub.status.idle":"2025-05-27T06:40:00.239940Z","shell.execute_reply.started":"2025-05-27T06:39:58.393613Z","shell.execute_reply":"2025-05-27T06:40:00.238934Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport plotly.express as px\n\n# Load the training labels CSV into a pandas DataFrame\nTRAIN_CSV = os.path.join(DATA_DIR, 'train_labels.csv')\n\ntry:\n    train_labels = pd.read_csv(TRAIN_CSV)\n    print(\"Training dataset loaded successfully.\")\n    print(train_labels.head())  # Display the first few rows to check the data\nexcept FileNotFoundError:\n    print(f\"Error: The file {TRAIN_CSV} was not found.\")\nexcept Exception as e:\n    print(f\"An error occurred while loading the file: {e}\")\n\n# Show descriptive statistics\ndescriptive_stats = train_labels.describe().loc[['mean', 'min', 'max']].T\ndisplay(descriptive_stats)\n\n# 1. Box Plots with Seaborn\nplt.figure(figsize=(14, 8))\nsns.boxplot(data=train_labels, orient='h', palette='viridis')\nplt.title(\"Box Plots of Features\", fontsize=16, fontweight='bold', color=\"darkblue\")\nplt.xlabel(\"Values\", fontsize=12, color=\"black\")\nplt.ylabel(\"Features\", fontsize=12, color=\"black\")\nplt.xticks(fontsize=10, color=\"black\")\nplt.yticks(fontsize=10, color=\"black\")\nplt.tight_layout()\nplt.show()\n\n# 2. Violin Plots with Seaborn\nplt.figure(figsize=(14, 8))\nsns.violinplot(data=train_labels, orient='h', palette='magma')\nplt.title(\"Violin Plots of Features\", fontsize=16, fontweight='bold', color=\"darkblue\")\nplt.xlabel(\"Values\", fontsize=12, color=\"black\")\nplt.ylabel(\"Features\", fontsize=12, color=\"black\")\nplt.xticks(fontsize=10, color=\"black\")\nplt.yticks(fontsize=10, color=\"black\")\nplt.tight_layout()\nplt.show()\n\n# 3. Density Plots with Seaborn\nplt.figure(figsize=(14, 8))\nsns.kdeplot(data=train_labels, palette='viridis', shade=True)\nplt.title(\"Density Plots of Features\", fontsize=16, fontweight='bold', color=\"darkblue\")\nplt.xlabel(\"Values\", fontsize=12, color=\"black\")\nplt.ylabel(\"Density\", fontsize=12, color=\"black\")\nplt.xticks(fontsize=10, color=\"black\")\nplt.yticks(fontsize=10, color=\"black\")\nplt.tight_layout()\nplt.show()\n\n# 4. Pair Plots with Seaborn\nsns.pairplot(train_labels, diag_kind='kde', palette='viridis')\nplt.suptitle(\"Pair Plots of Features\", fontsize=16, fontweight='bold', color=\"darkblue\")\nplt.tight_layout(rect=[0, 0, 1, 0.96])\nplt.show()\n\n# 5. Interactive Plots with Plotly\nfig = px.scatter_matrix(\n    train_labels,\n    dimensions=train_labels.columns,\n    color='Number of motors',\n    title=\"Interactive Scatter Matrix Plot of Features\",\n    template=\"plotly_white\"\n)\nfig.update_layout(\n    font=dict(size=12, color=\"black\"),\n    legend=dict(font=dict(size=14, color=\"black\")),\n    margin=dict(l=10, r=10, b=10, t=50),\n)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:40:00.240800Z","iopub.execute_input":"2025-05-27T06:40:00.241034Z","iopub.status.idle":"2025-05-27T06:40:21.942084Z","shell.execute_reply.started":"2025-05-27T06:40:00.241015Z","shell.execute_reply":"2025-05-27T06:40:21.941290Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Correlation Matrix","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Load the training labels CSV into a pandas DataFrame\nTRAIN_CSV = os.path.join(DATA_DIR, 'train_labels.csv')\n\ntry:\n    train_labels = pd.read_csv(TRAIN_CSV)\n    print(\"Training dataset loaded successfully.\")\n    print(train_labels.head())  # Display the first few rows to check the data\nexcept FileNotFoundError:\n    print(f\"Error: The file {TRAIN_CSV} was not found.\")\nexcept Exception as e:\n    print(f\"An error occurred while loading the file: {e}\")\n\n# Show descriptive statistics\ndescriptive_stats = train_labels.describe().loc[['mean', 'min', 'max']].T\ndisplay(descriptive_stats)\n\n# Create a correlation heatmap\nplt.figure(figsize=(9, 5), facecolor=\"white\")\nsns.heatmap(\n    data=train_labels.corr(numeric_only=True),\n    cmap=\"coolwarm\",  # Strong contrast for positive/negative correlations\n    vmin=-1, vmax=1,\n    linecolor=\"white\", linewidth=0.6,\n    annot=True,\n    fmt=\".2f\"\n)\nplt.title('Correlation Heatmap', fontsize=14, fontweight='bold', color=\"black\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:40:21.942890Z","iopub.execute_input":"2025-05-27T06:40:21.943174Z","iopub.status.idle":"2025-05-27T06:40:22.390807Z","shell.execute_reply.started":"2025-05-27T06:40:21.943145Z","shell.execute_reply":"2025-05-27T06:40:22.389884Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Visualizing Images from the Dataset","metadata":{}},{"cell_type":"code","source":"import os\nimport glob\nimport cv2\nimport matplotlib.pyplot as plt\n\ndef plotImages(title, directory, n_images=16, img_size=(128, 128)):\n    \"\"\"\n    Display a grid of images from the specified directory.\n    \n    Args:\n        title (str): Title to print before displaying images.\n        directory (str): Glob pattern for image files.\n        n_images (int): Number of images to display.\n        img_size (tuple): Size to resize images for display.\n    \"\"\"\n    print(f\"🖼 {title}\")\n    image_files = glob.glob(directory)\n    \n    if not image_files:\n        print(\"No images found.\")\n        return\n    \n    plt.figure(figsize=(12, 12))\n    plt.subplots_adjust(wspace=0.1, hspace=0.1)\n    \n    for i, file_path in enumerate(image_files[:n_images]):\n        img = cv2.imread(file_path)\n        if img is None:\n            print(f\"Warning: Could not load image at {file_path}\")\n            continue\n        img = cv2.resize(img, img_size)\n        plt.subplot(4, 4, i+1)\n        plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n        plt.axis('off')\n    \n    plt.suptitle(title, fontsize=14, fontweight='bold', color=\"darkred\")\n    plt.show()\n\n# Example usage\n#plotImages(\"Bacterial Flagellar Motors - Train Images\", \"../input/byu-locating-bacterial-flagellar-motors-2025/train/**/*.png\")\nplotImages(\"Bacterial Flagellar Motors - Train Images\", \"../input/byu-locating-bacterial-flagellar-motors-2025/train/***/**\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:40:22.391770Z","iopub.execute_input":"2025-05-27T06:40:22.392048Z","iopub.status.idle":"2025-05-27T06:40:24.990516Z","shell.execute_reply.started":"2025-05-27T06:40:22.392019Z","shell.execute_reply":"2025-05-27T06:40:24.989489Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport random\nimport math\nimport cv2\nimport matplotlib.pyplot as plt\n\ndef visualize_images(path, n_images=12, is_random=True, figsize=(14, 14)):\n    \"\"\"\n    Visualize a set of images from a directory.\n    \n    Args:\n        path (str): Directory path containing images.\n        n_images (int): Number of images to display.\n        is_random (bool): If True, display random images; else, the first n_images.\n        figsize (tuple): Size of the figure.\n    \"\"\"\n    if not os.path.isdir(path):\n        print(f\"Error: The directory {path} does not exist.\")\n        return\n    \n    plt.figure(figsize=figsize)\n    \n    image_names = os.listdir(path)\n    \n    if not image_names:\n        print(f\"Error: No images found in the directory {path}.\")\n        return\n    \n    if is_random:\n        image_names = random.sample(image_names, min(len(image_names), n_images))\n    else:\n        image_names = image_names[:n_images]\n    \n    w = int(math.sqrt(n_images))\n    h = math.ceil(n_images / w)\n    \n    for ind, image_name in enumerate(image_names):\n        img_path = os.path.join(path, image_name)\n        img = cv2.imread(img_path)\n        if img is None:\n            print(f\"Warning: Could not load image at {img_path}\")\n            continue\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        plt.subplot(h, w, ind + 1)\n        plt.imshow(img)\n        plt.xticks([])\n        plt.yticks([])\n    \n    plt.suptitle(\"Sample Tomogram Images\", fontsize=14, fontweight='bold', color=\"darkblue\")\n    plt.tight_layout(rect=[0, 0, 1, 0.96])  # Adjust layout to fit the suptitle\n    plt.show()\n\n# Example usage\nvisualize_images(\"/kaggle/input/byu-locating-bacterial-flagellar-motors-2025/train/tomo_00e463\", n_images=9)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:40:24.991578Z","iopub.execute_input":"2025-05-27T06:40:24.991868Z","iopub.status.idle":"2025-05-27T06:40:27.308845Z","shell.execute_reply.started":"2025-05-27T06:40:24.991851Z","shell.execute_reply":"2025-05-27T06:40:27.307975Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## YOLO","metadata":{}},{"cell_type":"code","source":"%%time\nfrom ultralytics import YOLO\n\n# Load a model\nmodel = YOLO('yolov8n.pt')  # load an official model\n\n# Train the model\nmodel.train(\n    data='coco128.yaml',  # path to your dataset configuration file\n    epochs=100,  # number of epochs\n    imgsz=640,  # image size\n    project='yolo_weights',  # project name (directory where results are saved)\n    name='motor_detector',  # experiment name (subdirectory within the project directory)\n    exist_ok=True  # allow overwriting of existing files\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:40:27.309878Z","iopub.execute_input":"2025-05-27T06:40:27.310251Z","iopub.status.idle":"2025-05-27T06:45:11.324834Z","shell.execute_reply.started":"2025-05-27T06:40:27.310221Z","shell.execute_reply":"2025-05-27T06:45:11.323890Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Verify the Save Location:","metadata":{}},{"cell_type":"code","source":"!ls /kaggle/working/yolo_weights/motor_detector/weights/best.pt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:45:11.326186Z","iopub.execute_input":"2025-05-27T06:45:11.327014Z","iopub.status.idle":"2025-05-27T06:45:11.525364Z","shell.execute_reply.started":"2025-05-27T06:45:11.326961Z","shell.execute_reply":"2025-05-27T06:45:11.524385Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Access the Weights:","metadata":{}},{"cell_type":"code","source":"model = YOLO('/kaggle/working/yolo_weights/motor_detector/weights/best.pt')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:45:11.526696Z","iopub.execute_input":"2025-05-27T06:45:11.526938Z","iopub.status.idle":"2025-05-27T06:45:11.590384Z","shell.execute_reply.started":"2025-05-27T06:45:11.526916Z","shell.execute_reply":"2025-05-27T06:45:11.589524Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Download the Weights:","metadata":{}},{"cell_type":"code","source":"%%time\n# Import YOLO\nfrom ultralytics import YOLO\n\n# Load a model\nmodel = YOLO('yolov8n.pt')  # load an official model\n\n# Train the model\nmodel.train(\n    data='coco128.yaml',  # path to your dataset configuration file\n    epochs=100,  # number of epochs\n    imgsz=640,  # image size\n    project='yolo_weights',  # project name (directory where results are saved)\n    name='motor_detector',  # experiment name (subdirectory within the project directory)\n    exist_ok=True  # allow overwriting of existing files\n)\n\n# Load the best weights\nmodel = YOLO('/kaggle/working/yolo_weights/motor_detector/weights/best.pt')\n\n# Use the model for inference or further training","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:45:11.591631Z","iopub.execute_input":"2025-05-27T06:45:11.591928Z","iopub.status.idle":"2025-05-27T06:49:56.568326Z","shell.execute_reply.started":"2025-05-27T06:45:11.591910Z","shell.execute_reply":"2025-05-27T06:49:56.567521Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Quick Solution: Simple Heuristic\n\n- Load the Test Data: Load the test set tomogram IDs.\n- Predict Motor Locations: Use a simple heuristic to predict motor locations. For example, you can predict a fixed location or use a random location within a reasonable range.\n- Generate Submission CSV: Format the predictions according to the submission format.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\n# Load the test set tomogram IDs\ntest_tomograms = pd.DataFrame({\n    'tomo_id': ['tomo_003acc', 'tomo_00e047', 'tomo_01a877', 'tomo_02b912', 'tomo_03c123']\n})\n\n# Define a simple heuristic to predict motor locations\ndef predict_motor_location(tomogram_id):\n    # Example heuristic: Predict a fixed location for all tomograms\n    fixed_location = [500.0, 250.0, 400.0]\n    \n    # Alternatively, you can use random locations within a reasonable range\n    # random_location = [np.random.uniform(0, 1000), np.random.uniform(0, 1000), np.random.uniform(0, 1000)]\n    \n    return fixed_location\n\n# Generate predictions\npredictions = []\nfor tomo_id in test_tomograms['tomo_id']:\n    motor_location = predict_motor_location(tomo_id)\n    predictions.append([tomo_id] + motor_location)\n\n# Create a DataFrame for the predictions\npredictions_df = pd.DataFrame(predictions, columns=['tomo_id', 'Motor axis 0', 'Motor axis 1', 'Motor axis 2'])\n\n# Save the submission to a CSV file\npredictions_df.to_csv('submission.csv', index=False)\n\nprint(\"Submission CSV generated successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:49:56.569881Z","iopub.execute_input":"2025-05-27T06:49:56.570258Z","iopub.status.idle":"2025-05-27T06:49:56.580423Z","shell.execute_reply.started":"2025-05-27T06:49:56.570209Z","shell.execute_reply":"2025-05-27T06:49:56.579760Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Evaluation","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\n# Example ground truth and predictions\nground_truth = pd.DataFrame({\n    'tomo_id': ['tomo_003acc', 'tomo_00e047', 'tomo_01a877'],\n    'Motor axis 0': [500.0, 300.0, 395.0],\n    'Motor axis 1': [20.0, 320.0, 335.0],\n    'Motor axis 2': [430.0, 800.0, 800.0]\n})\n\npredictions = pd.DataFrame({\n    'tomo_id': ['tomo_003acc', 'tomo_00e047', 'tomo_01a877'],\n    'Motor axis 0': [501.1, -1, 395.2],\n    'Motor axis 1': [22.8, -1, 335.4],\n    'Motor axis 2': [429.8, -1, 798.0]\n})\n\n# Function to calculate Euclidean distance\ndef euclidean_distance(gt, pred):\n    return np.sqrt((gt[0] - pred[0])**2 + (gt[1] - pred[1])**2 + (gt[2] - pred[2])**2)\n\n# Initialize counters\ntp = 0\nfp = 0\nfn = 0\n\n# Evaluate each prediction\nfor index, row in ground_truth.iterrows():\n    gt_location = (row['Motor axis 0'], row['Motor axis 1'], row['Motor axis 2'])\n    pred_location = predictions.loc[predictions['tomo_id'] == row['tomo_id'], ['Motor axis 0', 'Motor axis 1', 'Motor axis 2']].values[0]\n    \n    # Check if the prediction is a no-motor prediction\n    if all(pred == -1 for pred in pred_location):\n        if all(gt == 0 for gt in gt_location):\n            continue  # True Negative (not counted in TP, FP, FN)\n        else:\n            fn += 1  # False Negative\n    else:\n        distance = euclidean_distance(gt_location, pred_location)\n        if distance <= 100:\n            tp += 1  # True Positive\n        else:\n            fp += 1  # False Positive\n\n# Calculate precision and recall\nprecision = tp / (tp + fp) if (tp + fp) > 0 else 0\nrecall = tp / (tp + fn) if (tp + fn) > 0 else 0\n\n# Calculate F2 score\nbeta = 2\nf2_score = (1 + beta**2) * (precision * recall) / ((beta**2 * precision) + recall) if (precision + recall) > 0 else 0\n\nprint(f\"Precision: {precision}\")\nprint(f\"Recall: {recall}\")\nprint(f\"F2 Score: {f2_score}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:49:56.581355Z","iopub.execute_input":"2025-05-27T06:49:56.581645Z","iopub.status.idle":"2025-05-27T06:49:56.609570Z","shell.execute_reply.started":"2025-05-27T06:49:56.581622Z","shell.execute_reply":"2025-05-27T06:49:56.608879Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Submission CSV","metadata":{}},{"cell_type":"code","source":"# Example predictions\npredictions = pd.DataFrame({\n    'tomo_id': ['tomo_003acc', 'tomo_00e047', 'tomo_01a877'],\n    'Motor axis 0': [501.1, -1, 395.2],\n    'Motor axis 1': [22.8, -1, 335.4],\n    'Motor axis 2': [429.8, -1, 798.0]\n})\n\n# Save the submission to a CSV file\npredictions.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T06:49:56.610399Z","iopub.execute_input":"2025-05-27T06:49:56.610676Z","iopub.status.idle":"2025-05-27T06:49:56.629167Z","shell.execute_reply.started":"2025-05-27T06:49:56.610661Z","shell.execute_reply":"2025-05-27T06:49:56.628495Z"}},"outputs":[],"execution_count":null}]}