{
    "cells": [
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "# Dataset loading\n",
                "---\n",
                "Experimenting with loading the PyTorch Lightning dataset and visualising its outputs."
            ]
        },
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Setup"
            ]
        },
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "### Imports"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "from coal_emissions_monitoring.dataset import CoalEmissionsDataModule\n",
                "from coal_emissions_monitoring.data_viz import view_satellite_image"
            ]
        },
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Create the dataset"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "data = CoalEmissionsDataModule(\n",
                "    image_metadata_path=\"/Users/adminuser/GitHub/ccai-ss23-ai-monitoring-tutorial/data/image_metadata.csv\",\n",
                "    campd_facilities_path=\"/Users/adminuser/GitHub/ccai-ss23-ai-monitoring-tutorial/data/facility_attributes.csv\",\n",
                "    campd_emissions_path=\"/Users/adminuser/GitHub/ccai-ss23-ai-monitoring-tutorial/data/daily_emissions_facility_aggregation.csv\",\n",
                "    batch_size=2,\n",
                "    predownload_images=True,\n",
                "    images_dir=\"/Users/adminuser/GitHub/ccai-ss23-ai-monitoring-tutorial/data/images\",\n",
                ")\n",
                "data.setup(stage=\"fit\")"
            ]
        },
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Load some batches"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "for batch in data.train_dataloader():\n",
                "    break\n",
                "print(f\"Keys in batch: {batch.keys()}\")\n",
                "print(f\"Image shape: {batch['image'].shape}\")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "idx = 0\n",
                "print(f\"Target: {batch['target'][idx]}\")\n",
                "print(f\"Facility name: {batch['metadata']['facility_name'][idx]}\")\n",
                "print(f\"Timestamp: {batch['metadata']['ts'][idx]}\")\n",
                "view_satellite_image(batch[\"image\"][idx])"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "((batch[\"image\"][idx] <= 1) | (batch[\"image\"][idx].isnan())).sum() / batch[\"image\"][idx].numel()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": []
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "ccai_ss23",
            "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.10.9"
        },
        "orig_nbformat": 4
    },
    "nbformat": 4,
    "nbformat_minor": 2
}
