{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# OxHyper-Minerals dataset preview notebook\n", "**Author: Vít Růžička, 2024**\n", "\n", "This notebook offers simple previews of data which is released as the **OxHyper datasets**. For more details please check my main web at: https://previtus.github.io/ ... There are in total 3 OxHyper datasets, please follow these links:\n", "- OxHyperSyntheticCH4: https://huggingface.co/datasets/previtus/OxHyperSyntheticCH4\n", "- OxHyperRealCH4: https://huggingface.co/datasets/previtus/OxHyperRealCH4\n", "- OxHyperMinerals: https://huggingface.co/datasets/previtus/OxHyperMinerals_Train\n", "- ... additionally the STARCOP dataset is at https://huggingface.co/datasets/previtus/STARCOP_allbands_Train1\n", "\n", "*Note about this notebook: We simply download products for one of the events to allow easy inspection of the data (for downloading the whole dataset we recommend using git clone over the whole repository).*" ] }, { "cell_type": "markdown", "metadata": { "id": "KG1WkD7ToOj9" }, "source": [ "## 1 Preparation\n", "\n", "Setup these libraries (on the Google Colab or on your local machine)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "gYG2iJ8Dl5l8", "outputId": "199c168f-b1b6-4b0c-dd57-591d447c6375" }, "outputs": [], "source": [ "# Install these libraries\n", "# !pip install --upgrade huggingface_hub\n", "# !pip install datasets\n", "# !pip install rasterio\n", "#!pip install ipywidgets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Login to Huggingface using your token\n", "from huggingface_hub import login\n", "login()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from datasets import load_dataset\n", "from huggingface_hub import hf_hub_download, snapshot_download\n", "import rasterio as rio\n", "import numpy as np\n", "import os\n", "import pylab as plt\n", "from mpl_toolkits.axes_grid1 import make_axes_locatable\n", "\n", "def load_bands(folder_path, band_names):\n", " data = []\n", " for band_name in band_names:\n", " with rio.open(os.path.join(folder_path,band_name)) as src:\n", " data.append(src.read(1))\n", " return np.asarray(data)\n", "\n", "def colorbar_next_to(im, ax, size='5%',pad=0.05):\n", " divider = make_axes_locatable(ax)\n", " cax = divider.append_axes('right', size=size, pad=pad)\n", " plt.gcf().colorbar(im, cax=cax, orientation='vertical')" ] }, { "cell_type": "markdown", "metadata": { "id": "9h7KwrQ9pOuS" }, "source": [ "## 2 Dataset previews\n", "\n", "First choose one of the datasets for inspection:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# dataset_name = \"previtus/OxHyperSyntheticCH4\"\n", "# dataset_name = \"previtus/OxHyperRealCH4\"\n", "dataset_name = \"previtus/OxHyperMinerals_Train\" # or OxHyperMinerals_TestVal\n", "\n", "subset = \"train\" # train, val or test" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "i4e4j27A71P-", "outputId": "f357e74f-c195-4244-9769-9005d11b7a06" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Found cached dataset csv (/home/vitek/.cache/huggingface/datasets/previtus___csv/previtus--OxHyperMinerals_Train-59fd4dde5eedbe20/0.0.0/eea64c71ca8b46dd3f537ed218fc9bf495d5707789152eb2764f5c78fa66d59d)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Loaded dataset previtus/OxHyperMinerals_Train (using subset train and csv= train_minerals.csv )\n", "Dataset({\n", " features: ['event_id', 'mode', 'granule_source', 'tile_source', 'id', 'qplume'],\n", " num_rows: 796\n", "})\n" ] } ], "source": [ "if dataset_name == \"previtus/OxHyperRealCH4\":\n", " csv_name = \"real_train_v3.csv\"\n", " if subset == \"val\": csv_name = \"real_val_v3.csv\"\n", " if subset == \"test\": csv_name = \"real_test_v3.csv\"\n", "\n", "elif dataset_name == \"previtus/OxHyperSyntheticCH4\":\n", " csv_name = \"train_filtered_v2.csv\"\n", " if subset == \"val\": csv_name = \"val_filtered_v2.csv\"\n", " if subset == \"test\": csv_name = \"test_filtered_v2.csv\"\n", "\n", "elif \"OxHyperMinerals\" in dataset_name:\n", " csv_name = \"train_minerals.csv\"\n", " if subset == \"val\": csv_name = \"val_minerals.csv\"\n", " if subset == \"test\": csv_name = \"test_minerals.csv\"\n", "\n", "dataset = load_dataset(dataset_name, split=subset, data_files={subset: csv_name})\n", "\n", "print(\"Loaded dataset\", dataset_name, \"(using subset\", subset, \"and csv=\", csv_name, \")\")\n", "print(dataset)\n", "\n", "# Setup which products will be used:\n", "rgb_bands = [\"B_EMIT_641nm.tif\", \"B_EMIT_551nm.tif\", \"B_EMIT_462nm.tif\"]\n", "mf_bands = [\"B_magic30_tile.tif\"]\n", "\n", "if dataset_name == \"previtus/OxHyperSyntheticCH4\":\n", " gt_names = [\"labelbinary.tif\"]\n", "\n", "if dataset_name == \"previtus/OxHyperRealCH4\":\n", " gt_names = [\"label_v3_binary.tif\"]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 478, "referenced_widgets": [ "b81be55f7c7e4b67a771116f66e31287", "756866b141324cb8b3af5ea92b6f7819", "368d56e362f24cb9ad9557cdb7004011", "d37d3bc80c3b45a69f6ecbdab04cc5ab", "753b5f7716ad4f849687fff3f5d8c463", "096656183cbc4bbc82873e8604fdc615", "e37098c302b74a67bab46c68011b906d", "717a204307fd4e119add3aa0d68d5f91", "7367791fb1904ebe8ebbfe053a6aa00d", "5b0141f4063c45f4a1da91f189b27ec2", "fee71dbfd64e439f90861bb0c5722b53" ] }, "id": "G27_i3_Tt9jg", "outputId": "616b2ef4-d982-4942-a378-3c4624bcc7e9" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2222204x006_2224105x025_01\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8d1d5a03d2a24e2c936489de999d56fe", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Fetching 10 files: 0%| | 0/10 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "local_path = \"local_\"+dataset_name\n", "for example_i, example in enumerate(dataset):\n", " if example_i < 1: continue # can be used to skip to later ones\n", " folder_path = example[\"id\"]\n", " print(folder_path)\n", " full_path = os.path.join(local_path, folder_path)\n", "\n", " # Download event folder for this data sample:\n", " if \"OxHyperMinerals\" in dataset_name:\n", " snapshot_download(repo_id=dataset_name, repo_type=\"dataset\", allow_patterns=[folder_path+\"/C\", folder_path+\"/*.tif\", folder_path+\"/*.hdr\"],\n", " local_dir=local_path)\n", " rgb = []\n", " rgb_bands = [36, 22, 10]\n", " with rio.open(os.path.join(full_path,\"C\")) as src:\n", " for band_i in rgb_bands:\n", " rgb.append(src.read(band_i))\n", " rgb = np.asarray(rgb)\n", " \n", " with rio.open(os.path.join(full_path,\"minerals3ghk.tif\")) as src:\n", " minerals = src.read()\n", " \n", " print(\"example data loaded: rgb\", rgb.shape, \"minerals:\", minerals.shape)\n", " # Visualize the event:\n", " fig, axes = plt.subplots(1, 2, figsize=(12,4), tight_layout=True)\n", " \n", " ax = axes[0]\n", " rgb_viz = np.clip(rgb / 30., 0, 2)\n", " ax.imshow(np.transpose(rgb_viz, (1, 2, 0)))\n", " ax.set_title(\"RGB\")\n", " \n", " ax = axes[1]\n", " ax.imshow(np.transpose(255*minerals, (1, 2, 0)))\n", " ax.set_title(\"Minerals label\")\n", "\n", " else:\n", " snapshot_download(repo_id=dataset_name, repo_type=\"dataset\", allow_patterns=[folder_path+\"/*.tif\", folder_path+\"/*.hdr\"],\n", " local_dir=local_path)\n", "\n", " # Load data:\n", " rgb = load_bands(full_path, rgb_bands)\n", " mf = load_bands(full_path, mf_bands)\n", " gt = load_bands(full_path, gt_names)\n", "\n", " print(\"example data loaded: rgb\", rgb.shape, \"mf:\", mf.shape, \"gt:\", gt.shape)\n", " # Visualize the event:\n", " fig, axes = plt.subplots(1, 3, figsize=(12,4), tight_layout=True)\n", " \n", " ax = axes[0]\n", " rgb_viz = np.clip(rgb / 30., 0, 2)\n", " ax.imshow(np.transpose(rgb_viz, (1, 2, 0)))\n", " ax.set_title(\"RGB\")\n", " \n", " ax = axes[1]\n", " mf_viz = np.clip(mf[0] / 1750., 0, 2)\n", " im = ax.imshow(1750*mf_viz, cmap=\"magma\")\n", " colorbar_next_to(im, ax)\n", " ax.set_title(\"MF\")\n", " \n", " ax = axes[2]\n", " ax.imshow(gt[0], vmin=0, vmax=1)\n", " ax.set_title(\"Ground truth label\")\n", "\n", " plt.show()\n", " \n", " break # comment for more examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (Etc)" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [], "source": [ "# you might want to clean you HF cache afterwards:\n", "# !ls ~/.cache/huggingface/hub" ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.4" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "096656183cbc4bbc82873e8604fdc615": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "368d56e362f24cb9ad9557cdb7004011": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "FloatProgressModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "FloatProgressModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "ProgressView", "bar_style": "success", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_717a204307fd4e119add3aa0d68d5f91", "max": 129, "min": 0, "orientation": "horizontal", "style": "IPY_MODEL_7367791fb1904ebe8ebbfe053a6aa00d", "value": 129 } }, "5b0141f4063c45f4a1da91f189b27ec2": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "717a204307fd4e119add3aa0d68d5f91": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "7367791fb1904ebe8ebbfe053a6aa00d": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "ProgressStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "ProgressStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "bar_color": null, "description_width": "" } }, "753b5f7716ad4f849687fff3f5d8c463": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "756866b141324cb8b3af5ea92b6f7819": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "HTMLModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_096656183cbc4bbc82873e8604fdc615", "placeholder": "​", "style": "IPY_MODEL_e37098c302b74a67bab46c68011b906d", "value": "Fetching 129 files: 100%" } }, "b81be55f7c7e4b67a771116f66e31287": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "HBoxModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HBoxView", "box_style": "", "children": [ "IPY_MODEL_756866b141324cb8b3af5ea92b6f7819", "IPY_MODEL_368d56e362f24cb9ad9557cdb7004011", "IPY_MODEL_d37d3bc80c3b45a69f6ecbdab04cc5ab" ], "layout": "IPY_MODEL_753b5f7716ad4f849687fff3f5d8c463" } }, "d37d3bc80c3b45a69f6ecbdab04cc5ab": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "HTMLModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_5b0141f4063c45f4a1da91f189b27ec2", "placeholder": "​", "style": "IPY_MODEL_fee71dbfd64e439f90861bb0c5722b53", "value": " 129/129 [00:00<00:00, 796.38it/s]" } }, "e37098c302b74a67bab46c68011b906d": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "DescriptionStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "fee71dbfd64e439f90861bb0c5722b53": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "DescriptionStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } } } } }, "nbformat": 4, "nbformat_minor": 4 }