{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":64733,"databundleVersionId":7762802,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Published on March 02, 2024. By Marília Prata, mpwolke","metadata":{}},{"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)\nimport seaborn as sns\nfrom matplotlib import pyplot as plt\n\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\n\nimport plotly.graph_objects as go\nimport plotly.offline as py\nimport plotly.express as px\nfrom plotly.offline import iplot\n\n#Ignore warnings\nimport warnings\nwarnings.filterwarnings('ignore')\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\nfor 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","execution":{"iopub.status.busy":"2024-03-02T17:57:56.901546Z","iopub.execute_input":"2024-03-02T17:57:56.901976Z"},"_kg_hide-output":true,"_kg_hide-input":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](https://www.kaggle.com/competitions/64733/images/header)https://www.kaggle.com/competitions/geolifeclef-2024","metadata":{}},{"cell_type":"markdown","source":"#Competition Citation:\n\n@misc{geolifeclef-2024,\n\n    author = {Alexis Joly, Awsaf, César Leblanc, ChrisBotella, HCL-Jevster, inversion, Martin Görner, Maxime Ryckewaert, Maximilien Servajean, picekl, tlarcher},\n    \n    title = {GeoLifeCLEF 2024 @ LifeCLEF & CVPR-FGVC},\n    \n    publisher = {Kaggle},\n    year = {2024},\n    url = {https://kaggle.com/competitions/geolifeclef-2024}","metadata":{}},{"cell_type":"markdown","source":"#Running the 1st snippet started 14:57 till 15:07 After freezing the page a couple of times.","metadata":{}},{"cell_type":"markdown","source":"#Data downloading - Rasters\n\n\"All PA-related data are already provided through Kagle; however, all the original rasters are provided on the Seafile. To download the full dataset, we recommend downloading each group separately using zipped archives.\"\n\nhttps://www.kaggle.com/competitions/geolifeclef-2024/data\n\n\"Suppose that you want to download all rasters available and the metadata for presence-only occurences and save the results in a folder output. The command line should be :\"\n\nhttps://www.kaggle.com/code/maximiliense/download-script-usage","metadata":{}},{"cell_type":"code","source":"python download.py --data output --raster --presence-only --all-variables","metadata":{"execution":{"iopub.status.busy":"2024-03-02T17:52:52.601011Z","iopub.execute_input":"2024-03-02T17:52:52.601368Z","iopub.status.idle":"2024-03-02T17:52:52.635595Z","shell.execute_reply.started":"2024-03-02T17:52:52.601338Z","shell.execute_reply":"2024-03-02T17:52:52.634209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#How to fix the sintaxe above and below?","metadata":{}},{"cell_type":"markdown","source":"#\"Now, if you are only interested in some variables (e.g. elevation and landcover), you could replace -all-variables by --elevation --landcover.\"\n\nhttps://www.kaggle.com/code/maximiliense/download-script-usage","metadata":{}},{"cell_type":"code","source":"python download.py --data output --raster --presence-only --climate","metadata":{"execution":{"iopub.status.busy":"2024-03-02T17:53:08.529464Z","iopub.execute_input":"2024-03-02T17:53:08.529810Z","iopub.status.idle":"2024-03-02T17:53:08.535940Z","shell.execute_reply.started":"2024-03-02T17:53:08.529786Z","shell.execute_reply":"2024-03-02T17:53:08.534888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#How to use the download script https://github.com/plantnet/GeoLifeCLEF","metadata":{}},{"cell_type":"code","source":"#By Maximilien Servajean https://github.com/plantnet/GeoLifeCLEF/blob/master/download.py\n\n\"\"\"\nFile: download.py\nAuthor: maximiliense\nDate: 2024-02-08\nDescription: This script downloads files from the GeoLifeCLEF 2024 dataset.\n\"\"\"\nimport re\nimport os\nimport argparse\nimport requests\n\nfrom tqdm import tqdm\n\nvariables = [\n    'climate',\n    'elevation',\n    'humanfootprint',\n    'landcover',\n    'soilgrids',\n    'satellitepatches',\n    'satellitetimeseries'\n]\n\nrasters = {\n    'climate': 'EnvironmentalRasters/Climate.zip',\n    'elevation': 'EnvironmentalRasters/Elevation.zip',\n    'humanfootprint': 'EnvironmentalRasters/HumanFootprint.zip',\n    'landcover': 'EnvironmentalRasters/LandCover.zip',\n    'soilgrids': 'EnvironmentalRasters/Soilgrids.zip'\n}\n\npresence_only = {\n    'climate': {\n        'not_cube': [\n            'EnvironmentalRasters/Climate/GLC24-PO-train-bioclimatic-average.csv',\n            'EnvironmentalRasters/Climate/GLC24-PO-train-bioclimatic-monthly.csv'],\n        'cube': [\n            'EnvironmentalRasters/Climate/GLC24-PO-train-bioclimatic-average.csv',\n            'EnvironmentalRasters/Climate/Climatic_Monthly_2000-2019_cubes/GLC24-PO-train-bioclimatic-monthly.zip'\n        ]\n    },\n    'elevation': {\n        'not_cube': ['EnvironmentalRasters/Elevation/GLC24-PO-train-elevation.csv']\n        },\n    'humanfootprint': {\n        'not_cube': ['EnvironmentalRasters/HumanFootprint/GLC24-PO-train-human-footprint.csv']\n        },\n    'landcover': {\n        'not_cube': ['EnvironmentalRasters/LandCover/GLC24-PO-train-landcover.csv']},\n    'soilgrids': {\n        'not_cube': ['EnvironmentalRasters/Soilgrids/GLC24-PO-train-soilgrids.csv']\n        },\n    'satellitepatches': {\n        'not_cube': [\n            'SatellitePatches/PO-Train-SatellitePatches-NIR.zip',\n            'SatellitePatches/PO-Train-SatellitePatches-RGB.zip'\n        ]\n    },\n    'satellitetimeseries': {\n        'not_cube': [\n            'SatelliteTimeSeries/GLC24-PO-train-landsat-time-series-swir2.csv',\n            'SatelliteTimeSeries/GLC24-PO-train-landsat-time-series-swir1.csv',\n            'SatelliteTimeSeries/GLC24-PO-train-landsat-time-series-red.csv',\n            'SatelliteTimeSeries/GLC24-PO-train-landsat-time-series-nir.csv',\n            'SatelliteTimeSeries/GLC24-PO-train-landsat-time-series-green.csv',\n            'SatelliteTimeSeries/GLC24-PO-train-landsat-time-series-blue.csv',\n        ],\n        'cube': [\n            'SatelliteTimeSeries/cubes/GLC24-PO-train-landsat-time-series.zip'\n        ]\n    }\n}\n\npresence_absence = {\n    'climate': {\n        'not_cube': [\n            'EnvironmentalRasters/Climate/GLC24-PA-test-bioclimatic-average.csv',\n            'EnvironmentalRasters/Climate/GLC24-PA-train-bioclimatic-average.csv',\n            'EnvironmentalRasters/Climate/GLC24-PA-test-bioclimatic-monthly.csv',\n            'EnvironmentalRasters/Climate/GLC24-PA-train-bioclimatic-monthly.csv'],\n        'cube': [\n            'EnvironmentalRasters/Climate/GLC24-PA-test-bioclimatic-average.csv',\n            'EnvironmentalRasters/Climate/GLC24-PA-train-bioclimatic-average.csv',\n            'EnvironmentalRasters/Climate/Climatic_Monthly_2000-2019_cubes/GLC24-PA-test-bioclimatic-monthly.zip',\n            'EnvironmentalRasters/Climate/Climatic_Monthly_2000-2019_cubes/GLC24-PA-train-bioclimatic-monthly.zip',\n        ]\n    },\n    'elevation': {\n        'not_cube': [\n            'EnvironmentalRasters/Elevation/GLC24-PA-train-elevation.csv',\n            'EnvironmentalRasters/Elevation/GLC24-PA-test-elevation.csv']\n        },\n    'humanfootprint': {\n        'not_cube': [\n            'EnvironmentalRasters/HumanFootprint/GLC24-PA-train-human-footprint.csv',\n            'EnvironmentalRasters/HumanFootprint/GLC24-PA-test-human-footprint.csv'\n            ]\n        },\n    'landcover': {\n        'not_cube': [\n            'EnvironmentalRasters/LandCover/GLC24-PA-train-landcover.csv',\n            'EnvironmentalRasters/LandCover/GLC24-PA-test-landcover.csv'\n            ]\n    },\n    'soilgrids': {\n        'not_cube': [\n            'EnvironmentalRasters/Soilgrids/GLC24-PA-train-soilgrids.csv',\n            'EnvironmentalRasters/Soilgrids/GLC24-PA-test-soilgrids.csv'\n        ]\n    },\n    'satellitepatches': {\n        'not_cube': [\n            'SatellitePatches/PA-Train-SatellitePatches-NIR.zip',\n            'SatellitePatches/PA-Train-SatellitePatches-RGB.zip',\n            'SatellitePatches/PA-Test-SatellitePatches-NIR.zip',\n            'SatellitePatches/PA-Test-SatellitePatches-RGB.zip'\n        ]\n    },\n    'satellitetimeseries': {\n        'not_cube': [\n            'SatelliteTimeSeries/GLC24-PA-train-landsat-time-series-swir2.csv',\n            'SatelliteTimeSeries/GLC24-PA-train-landsat-time-series-swir1.csv',\n            'SatelliteTimeSeries/GLC24-PA-train-landsat-time-series-red.csv',\n            'SatelliteTimeSeries/GLC24-PA-train-landsat-time-series-nir.csv',\n            'SatelliteTimeSeries/GLC24-PA-train-landsat-time-series-green.csv',\n            'SatelliteTimeSeries/GLC24-PA-train-landsat-time-series-blue.csv',\n            'SatelliteTimeSeries/GLC24-PA-test-landsat-time-series-swir2.csv',\n            'SatelliteTimeSeries/GLC24-PA-test-landsat-time-series-swir1.csv',\n            'SatelliteTimeSeries/GLC24-PA-test-landsat-time-series-red.csv',\n            'SatelliteTimeSeries/GLC24-PA-test-landsat-time-series-nir.csv',\n            'SatelliteTimeSeries/GLC24-PA-test-landsat-time-series-green.csv',\n            'SatelliteTimeSeries/GLC24-PA-test-landsat-time-series-blue.csv'\n        ],\n        'cube': [\n            'SatelliteTimeSeries/cubes/GLC24-PA-train-landsat-time-series.zip',\n            'SatelliteTimeSeries/cubes/GLC24-PA-test-landsat-time-series.zip'\n        ]\n    }\n}\n\nmetadata = {\n    'po': [\n        'PresenceOnlyOccurrences/GLC24-PO-metadata-train.csv'\n        ],\n    'pa': [\n        'PresenceAbsenceSurveys/GLC24-PA-metadata-test.csv',\n        'PresenceAbsenceSurveys/GLC24-PA-metadata-train.csv'\n    ]\n}\nrepository = 'https://lab.plantnet.org/seafile/d/bdb829337aa44a9489f6'\nurl_struct = f'{repository}/files/?p=/{{}}'\n\ndef find_url(file):\n    \"\"\"Given the folder and the file, return the url to download the file\n\n    Args:\n        category (str): folder containing the file\n        file (str): file to download\n\n    Raises:\n        requests.exceptions.HTTPError: exception raised when the url is not found\n\n    Returns:\n        str: the url to direct download the file\n    \"\"\"\n    response = requests.get(url_struct.format(file), timeout=60)\n    url_key = \"rawPath\"\n    pattern = f\"{url_key}: '([^']+)'\"\n    # Find the first match\n    match = re.search(pattern, response.text)\n\n    if match:\n        return match.group(1).replace('\\\\u002D', '-') + '?raw=1'\n\n    raise requests.exceptions.HTTPError(f'Failed to find url for {file}')\n\n\ndef check_if_file_complete(path, response):\n    \"\"\"Given a path and a response from a request returns True or False\n    if the file needs to be re-downloaded...\n\n    Args:\n        path (str): the location of the file on disk\n        response (requests.models.Response): the response to the file request\n\n    Returns:\n        bool: True if the file needs to be downloaded, False otherwise.\n    \"\"\"\n    # check if file exists\n    if os.path.exists(path):\n        # get file size from response\n        file_size_server = int(response.headers.get('Content-Length', 0))\n        file_size_downloaded = os.path.getsize(path)\n        if file_size_server != file_size_downloaded:\n            return True\n        else:\n            print(f'{path} already downloaded and complete.')\n            return False\n    else:\n        return True\n\n\ndef download_file(url, filename):\n    \"\"\"Download a file given an url.\n\n    Args:\n        url (str): the url of the file to download\n        filename (str): the location where to write the file\n    \"\"\"\n    # Send a HTTP request to the URL of the zipfile\n    response = requests.get(url, timeout=60, stream=True)\n    # Make sure the request was successful\n    assert response.status_code == 200, 'Failed to download file'\n\n    # Download file only if needs to be\n    if check_if_file_complete(filename, response):\n        total_size_in_bytes = int(response.headers.get('content-length', 0))\n        block_size = 1024  # 1 Kilobyte\n\n        # Create dest directory if required\n        if not os.path.exists(os.path.dirname(filename)):\n            print(f'mkdir {os.path.dirname(filename)}')\n            os.makedirs(os.path.dirname(filename))\n\n        progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True)\n\n        with open(filename, 'wb') as file:\n            for data in response.iter_content(block_size):\n                progress_bar.update(len(data))\n                file.write(data)\n\n        progress_bar.close()\n\n        if total_size_in_bytes not in (0, progress_bar.n):\n            raise requests.exceptions.RequestException('Error.. size do not match...')\n\n\ndef process_download(file, data):\n    \"\"\"Download the file in category cat into the folder data. The function\n    handles the exception that can rise from the sub-methods.\n\n    Args:\n        cat (str): the folder containing the file\n        file (str): the file itself\n        data (str): the folder to save the data in\n    \"\"\"\n    try:\n        u = find_url(file)\n        print(f'Downloading {u} ({file})')\n        download_file(u, f'{data}/{file}')\n    except requests.exceptions.HTTPError:\n        print(f'Failed to find url for {file}')\n    except (AssertionError, requests.exceptions.RequestException):\n        print(f'Failed to download file {file}\\n\\t{u}')\n\ndef process_option(struct, cube, variable, output):\n    if variable in struct:\n        if cube and 'cube' in struct[variable]:\n            l = struct[variable]['cube']\n        else:\n            l = struct[variable]['not_cube']\n        for f in l:\n            process_download(f, output)\n    \n    \nif __name__ == \"__main__\":\n    # Create the parser\n    parser = argparse.ArgumentParser(\n        description=('GeoLifeCLEF 2024 file downloader. '\n                     'The script permits to download individual '\n                     'files or all files at once.'))\n\n    parser.add_argument(\n        '--data', default='data', help='Destination for the downloads (default: data)')\n\n    group = parser.add_argument_group('Data type')\n    group.add_argument('--raster', action='store_true', help='Download full rasters')\n    group.add_argument('--presence-only', action='store_true',\n                       help='Download PO metadata and pre-extracted data if option set to true')\n    group.add_argument('--presence-absence', action='store_true',\n                       help='Download PA metadata and pre-extracted data if option set to true')\n    group.add_argument('--pre-extracted', action='store_true',\n                       help='Download pre-extracted data of PO and/or PA depending on the options set')\n\n    group.add_argument('--cube', action='store_true',\n                       help='Download cube instead of csv when available')\n\n    group = parser.add_argument_group('Variables')\n    for v in variables:\n        group.add_argument(f'--{v}', action='store_true',\n                           help=f'Download {v} raster or pre-extracted data')\n    group.add_argument('--all-variables', action='store_true',\n                       help='Select all variables')\n\n    # Parse the arguments\n    args = parser.parse_args()\n\n    # Create output folder\n    if not os.path.exists(args.data):\n        print(f'mkdir {args.data}')\n        os.mkdir(args.data)\n\n    # download metadata\n    if args.presence_only:\n        process_download(metadata['po'][0], args.data)\n\n    if args.presence_absence:\n        for f in metadata['pa']:\n            process_download(f, args.data)\n\n    for v in variables:\n        # process raster\n        if args.all_variables or getattr(args, v):\n            if args.raster:\n                if v in rasters:\n                    process_download(rasters[v], args.data)\n            if args.pre_extracted and args.presence_only:\n                process_option(presence_only, args.cube, v, args.data)\n            # process presence absence\n            if args.pre_extracted and args.presence_absence:\n                process_option(presence_absence, args.cube, v, args.data)","metadata":{"execution":{"iopub.status.busy":"2024-03-02T17:54:27.275354Z","iopub.execute_input":"2024-03-02T17:54:27.275727Z","iopub.status.idle":"2024-03-02T17:54:27.367117Z","shell.execute_reply.started":"2024-03-02T17:54:27.275700Z","shell.execute_reply":"2024-03-02T17:54:27.365703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Above, I pressed Ctrl-D and Nothing happened","metadata":{}},{"cell_type":"markdown","source":"#ModuleNotFoundError: No module named 'data'","metadata":{}},{"cell_type":"code","source":"from data.GLC23PatchesProviders import MultipleRasterPatchProvider, RasterPatchProvider, JpegPatchProvider\nfrom data.GLC23Datasets import PatchesDataset","metadata":{"execution":{"iopub.status.busy":"2024-03-02T18:10:41.730370Z","iopub.execute_input":"2024-03-02T18:10:41.730859Z","iopub.status.idle":"2024-03-02T18:10:42.038880Z","shell.execute_reply.started":"2024-03-02T18:10:41.730828Z","shell.execute_reply":"2024-03-02T18:10:42.037785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline \n#--no-import-all  unrecognized arguments\n#%pylab inline is deprecated\n\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2024-03-02T18:26:05.681310Z","iopub.execute_input":"2024-03-02T18:26:05.681755Z","iopub.status.idle":"2024-03-02T18:26:05.687687Z","shell.execute_reply.started":"2024-03-02T18:26:05.681721Z","shell.execute_reply":"2024-03-02T18:26:05.686644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Clone your code:","metadata":{}},{"cell_type":"code","source":"!rm -rf GLC\n!git clone https://github.com/maximiliense/GLC","metadata":{"execution":{"iopub.status.busy":"2024-03-02T18:26:31.996991Z","iopub.execute_input":"2024-03-02T18:26:31.997325Z","iopub.status.idle":"2024-03-02T18:26:37.754384Z","shell.execute_reply.started":"2024-03-02T18:26:31.997301Z","shell.execute_reply":"2024-03-02T18:26:37.753259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Change this path to adapt to where you downloaded the data\nDATA_PATH = Path(\"../input/geolifeclef-2024/\")","metadata":{"execution":{"iopub.status.busy":"2024-03-02T18:28:29.319063Z","iopub.execute_input":"2024-03-02T18:28:29.319459Z","iopub.status.idle":"2024-03-02T18:28:29.325054Z","shell.execute_reply.started":"2024-03-02T18:28:29.319427Z","shell.execute_reply":"2024-03-02T18:28:29.323675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#This folder is the path root where the data was downloaded and extracted:","metadata":{}},{"cell_type":"code","source":"ls -L $DATA_PATH","metadata":{"execution":{"iopub.status.busy":"2024-03-02T18:28:52.085717Z","iopub.execute_input":"2024-03-02T18:28:52.086089Z","iopub.status.idle":"2024-03-02T18:28:52.414093Z","shell.execute_reply.started":"2024-03-02T18:28:52.086060Z","shell.execute_reply":"2024-03-02T18:28:52.413031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#TimeSeries-Cubes\n\nThe TimeSeries-Cubes subfolder contains 4 CSV files:","metadata":{}},{"cell_type":"code","source":"ls $DATA_PATH/TimeSeries-Cubes","metadata":{"execution":{"iopub.status.busy":"2024-03-02T18:34:50.311417Z","iopub.execute_input":"2024-03-02T18:34:50.311762Z","iopub.status.idle":"2024-03-02T18:34:50.625420Z","shell.execute_reply.started":"2024-03-02T18:34:50.311728Z","shell.execute_reply":"2024-03-02T18:34:50.624400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(DATA_PATH / \"GLC24_P0_metadata_train.csv\", sep=\",\")#, index_col=\"surveyId\")\n\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-02T18:45:30.866717Z","iopub.execute_input":"2024-03-02T18:45:30.867126Z","iopub.status.idle":"2024-03-02T18:45:34.605439Z","shell.execute_reply.started":"2024-03-02T18:45:30.867091Z","shell.execute_reply":"2024-03-02T18:45:34.604613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"taxonRank\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-02T18:52:49.295882Z","iopub.execute_input":"2024-03-02T18:52:49.296218Z","iopub.status.idle":"2024-03-02T18:52:49.564864Z","shell.execute_reply.started":"2024-03-02T18:52:49.296194Z","shell.execute_reply":"2024-03-02T18:52:49.563872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Trying to plot a map with GLC.plotting. And not making it.","metadata":{}},{"cell_type":"code","source":"!pip install GLC","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:01:57.845888Z","iopub.execute_input":"2024-03-02T19:01:57.846267Z","iopub.status.idle":"2024-03-02T19:02:09.610177Z","shell.execute_reply.started":"2024-03-02T19:01:57.846239Z","shell.execute_reply":"2024-03-02T19:02:09.608638Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install GLC.plotting","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:03:32.851525Z","iopub.execute_input":"2024-03-02T19:03:32.851942Z","iopub.status.idle":"2024-03-02T19:03:34.173473Z","shell.execute_reply.started":"2024-03-02T19:03:32.851910Z","shell.execute_reply":"2024-03-02T19:03:34.172658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Titouan Lorieul https://www.kaggle.com/code/tlorieul/geolifeclef2022-data-loading-and-visualization\n\nfrom GLC.plotting import plot_map\n\n\ndef plot_observations_distribution(ax, df, test=None, **kwargs):\n    default_kwargs = {\n        \"zorder\": 1,\n        \"alpha\": 0.1,\n        \"s\": 0.5\n    }\n    default_kwargs.update(kwargs)\n    kwargs = default_kwargs\n    \n    ax.scatter(df.lon, df.lat, color=\"blue\", **kwargs)\n    \n    if dftest is not None:\n        ax.scatter(df.lon, df.lat, color=\"red\", **kwargs)\n\n\nfig = plt.figure(figsize=(10, 5.5))\nax = plot_map(taxonRank=\"SUBSPECIES\")\nplot_observations_distribution(ax, df)\nax.set_title(\"Distribution Subspecies\")","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:06:02.432497Z","iopub.execute_input":"2024-03-02T19:06:02.432852Z","iopub.status.idle":"2024-03-02T19:06:02.461890Z","shell.execute_reply.started":"2024-03-02T19:06:02.432825Z","shell.execute_reply":"2024-03-02T19:06:02.460325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/geolifeclef-2024/GLC24_PA_metadata_train.csv\", sep=\",\")\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:11:30.451030Z","iopub.execute_input":"2024-03-02T19:11:30.452226Z","iopub.status.idle":"2024-03-02T19:11:31.276410Z","shell.execute_reply.started":"2024-03-02T19:11:30.452193Z","shell.execute_reply":"2024-03-02T19:11:31.275287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Sınıflara ait örnek sayılarının grafikle gösterimi \nfig, ax=plt.subplots(1,2,figsize=(30,30))\nplt.xticks(rotation=45, fontsize=8)\nsns.countplot(x='region', data=train, ax=ax[0])\ntrain['region'].value_counts().plot.pie(autopct=\"%1.1f%%\", ax=ax[1])\nplt.title('European Flora by Regions', fontsize=16);","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-03-02T20:17:53.913614Z","iopub.execute_input":"2024-03-02T20:17:53.913967Z","iopub.status.idle":"2024-03-02T20:17:55.520265Z","shell.execute_reply.started":"2024-03-02T20:17:53.913940Z","shell.execute_reply":"2024-03-02T20:17:55.519507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import geopandas as gpd","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:11:36.916041Z","iopub.execute_input":"2024-03-02T19:11:36.916395Z","iopub.status.idle":"2024-03-02T19:11:37.162743Z","shell.execute_reply.started":"2024-03-02T19:11:36.916367Z","shell.execute_reply":"2024-03-02T19:11:37.161748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"geom = gpd.points_from_xy(x = train['lon'], y=train['lat'], crs=4326)\ntrain_geo = gpd.GeoDataFrame(train, geometry=geom)","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:11:48.545386Z","iopub.execute_input":"2024-03-02T19:11:48.546742Z","iopub.status.idle":"2024-03-02T19:12:32.499818Z","shell.execute_reply.started":"2024-03-02T19:11:48.546685Z","shell.execute_reply":"2024-03-02T19:12:32.498856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"region\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:14:36.442575Z","iopub.execute_input":"2024-03-02T19:14:36.442925Z","iopub.status.idle":"2024-03-02T19:14:36.524615Z","shell.execute_reply.started":"2024-03-02T19:14:36.442896Z","shell.execute_reply":"2024-03-02T19:14:36.523775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"speciesId\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:27:22.818801Z","iopub.execute_input":"2024-03-02T19:27:22.819170Z","iopub.status.idle":"2024-03-02T19:27:22.859843Z","shell.execute_reply.started":"2024-03-02T19:27:22.819143Z","shell.execute_reply":"2024-03-02T19:27:22.859062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"country\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:14:12.976477Z","iopub.execute_input":"2024-03-02T19:14:12.976836Z","iopub.status.idle":"2024-03-02T19:14:13.058871Z","shell.execute_reply.started":"2024-03-02T19:14:12.976809Z","shell.execute_reply":"2024-03-02T19:14:13.057591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_geo_fr = train_geo[train_geo['country']=='France']","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:25:32.484095Z","iopub.execute_input":"2024-03-02T19:25:32.484479Z","iopub.status.idle":"2024-03-02T19:25:32.632814Z","shell.execute_reply.started":"2024-03-02T19:25:32.484450Z","shell.execute_reply":"2024-03-02T19:25:32.631892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tff = train_geo_fr[train_geo_fr['speciesId']==254.0]\ntff.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:38:00.807216Z","iopub.execute_input":"2024-03-02T19:38:00.807592Z","iopub.status.idle":"2024-03-02T19:38:00.826184Z","shell.execute_reply.started":"2024-03-02T19:38:00.807543Z","shell.execute_reply":"2024-03-02T19:38:00.824922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tff['region'].value_counts(dropna=False).plot(kind='barh', color=['green', 'red', 'purple', 'orange'], title='Count by 254.0 SpeciesId (France)');","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:41:37.491065Z","iopub.execute_input":"2024-03-02T19:41:37.491471Z","iopub.status.idle":"2024-03-02T19:41:37.690839Z","shell.execute_reply.started":"2024-03-02T19:41:37.491433Z","shell.execute_reply":"2024-03-02T19:41:37.689956Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Sınıflara ait örnek sayılarının grafikle gösterimi \nfig, ax=plt.subplots(1,2,figsize=(20,20))\nplt.xticks(rotation=45, fontsize=8)\nsns.countplot(x='region', data=tff, ax=ax[0])\ntff['region'].value_counts().plot.pie(autopct=\"%1.1f%%\", ax=ax[1])\nplt.title('France Specie 254.0 by Regions', fontsize=16);","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-03-02T20:19:44.753696Z","iopub.execute_input":"2024-03-02T20:19:44.754071Z","iopub.status.idle":"2024-03-02T20:19:45.222527Z","shell.execute_reply.started":"2024-03-02T20:19:44.754043Z","shell.execute_reply":"2024-03-02T20:19:45.221361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tff[\"region\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-02T19:39:42.351779Z","iopub.execute_input":"2024-03-02T19:39:42.352336Z","iopub.status.idle":"2024-03-02T19:39:42.359939Z","shell.execute_reply.started":"2024-03-02T19:39:42.352303Z","shell.execute_reply":"2024-03-02T19:39:42.358898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Above we have 282, sample below on the original was 12000 which is far too much","metadata":{}},{"cell_type":"markdown","source":"#Map by Region (Species 254.0 on France)","metadata":{}},{"cell_type":"code","source":"region_params = dict(column=\"region\", tooltip=True, popup=True, cmap=['green', 'red', 'purple', 'orange'], marker_kwds=dict(radius=3.5, fill=True), style_kwds=dict(stroke=False))\ntff.sample(300).explore(**region_params)","metadata":{"execution":{"iopub.status.busy":"2024-03-02T20:26:29.234292Z","iopub.execute_input":"2024-03-02T20:26:29.234716Z","iopub.status.idle":"2024-03-02T20:26:33.545254Z","shell.execute_reply.started":"2024-03-02T20:26:29.234689Z","shell.execute_reply":"2024-03-02T20:26:33.544329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#checking Species 4499.0 on Denmark","metadata":{}},{"cell_type":"code","source":"train_geo_den = train_geo[train_geo['country']=='Denmark']","metadata":{"execution":{"iopub.status.busy":"2024-03-02T20:32:28.570631Z","iopub.execute_input":"2024-03-02T20:32:28.571262Z","iopub.status.idle":"2024-03-02T20:32:28.824070Z","shell.execute_reply.started":"2024-03-02T20:32:28.571230Z","shell.execute_reply":"2024-03-02T20:32:28.823028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ffnn = train_geo_den[train_geo_den['speciesId']==4499.0]\nffnn.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-02T20:34:22.124334Z","iopub.execute_input":"2024-03-02T20:34:22.124933Z","iopub.status.idle":"2024-03-02T20:34:22.151354Z","shell.execute_reply.started":"2024-03-02T20:34:22.124899Z","shell.execute_reply":"2024-03-02T20:34:22.149927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Sınıflara ait örnek sayılarının grafikle gösterimi \nfig, ax=plt.subplots(1,2,figsize=(20,20))\nplt.xticks(rotation=45, fontsize=8)\nsns.countplot(x='region', data=ffnn, ax=ax[0])\ntff['region'].value_counts().plot.pie(autopct=\"%1.1f%%\", ax=ax[1])\nplt.title('Denmark Specie 4499.0 by Regions', fontsize=16);","metadata":{"execution":{"iopub.status.busy":"2024-03-02T20:35:50.666768Z","iopub.execute_input":"2024-03-02T20:35:50.667146Z","iopub.status.idle":"2024-03-02T20:35:51.158230Z","shell.execute_reply.started":"2024-03-02T20:35:50.667117Z","shell.execute_reply":"2024-03-02T20:35:51.156963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ffnn[\"region\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-02T20:36:45.769739Z","iopub.execute_input":"2024-03-02T20:36:45.770181Z","iopub.status.idle":"2024-03-02T20:36:45.780731Z","shell.execute_reply.started":"2024-03-02T20:36:45.770147Z","shell.execute_reply":"2024-03-02T20:36:45.779352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#On Denmark we have occurences on Continental and Atlantic regions only.","metadata":{}},{"cell_type":"code","source":"region_params = dict(column=\"region\", tooltip=True, popup=True, cmap=['purple', 'orange'], marker_kwds=dict(radius=3.5, fill=True), style_kwds=dict(stroke=False))\nffnn.sample(1200).explore(**region_params)","metadata":{"execution":{"iopub.status.busy":"2024-03-02T20:40:18.921263Z","iopub.execute_input":"2024-03-02T20:40:18.921718Z","iopub.status.idle":"2024-03-02T20:40:19.177948Z","shell.execute_reply.started":"2024-03-02T20:40:18.921683Z","shell.execute_reply":"2024-03-02T20:40:19.176687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#My intention was to plot the Rasters, however I wasn't able to unzip/load them.\n\nI will wait till any other Kaggler make it.","metadata":{}},{"cell_type":"markdown","source":"#Acknowledgements:\n\nMaximilien Servajean https://www.kaggle.com/code/maximiliense/download-script-usage\n\nhttps://github.com/plantnet/GeoLifeCLEF/blob/master/download.py\n\nTitouan Lorieul https://www.kaggle.com/code/tlorieul/geolifeclef2022-data-loading-and-visualization\n\nOfir Mazor https://www.kaggle.com/code/ofirmazor/quick-maps-exploration-france","metadata":{}}]}