{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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)\nfrom IPython.core.display import display, HTML\nimport ipywidgets as widgets\nfrom IPython.display import display,clear_output\nfrom ipywidgets import Output, Button\nfrom ipywidgets import TwoByTwoLayout\n# Utils widgets\nfrom ipywidgets import Button, Layout, jslink, IntText, IntSlider, Box, VBox\n\nfrom scipy.stats import pearsonr\nfrom plotly.subplots import make_subplots\nimport plotly.graph_objects as go\nimport plotly.io as pio\npio.templates\nfrom PIL import Image\nfrom IPython.display import Image as img\n\nimport random\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-07T17:36:05.614249Z","iopub.execute_input":"2022-02-07T17:36:05.614618Z","iopub.status.idle":"2022-02-07T17:36:05.623367Z","shell.execute_reply.started":"2022-02-07T17:36:05.614587Z","shell.execute_reply":"2022-02-07T17:36:05.622239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Introduction\n\nI built the notebook to randomly pick pictures of the species you're interested.\n\nFor example, there is one picture showing the **fin_whale** below.\n\nYou may choose the species you want to see and click the **Click me** button.\n\n**It is required to run the code from your own notebook, so you may need to fork this one.**\n\nThis is an ongoing work and I'll iterate it in the following versions. Stay tuned!","metadata":{}},{"cell_type":"code","source":"# load dataset\ndata = pd.read_csv(\"../input/happy-whale-and-dolphin/train.csv\")\n\n# unique individual id\nunique_individual_id = data['individual_id'].unique()\n\n# choose species\n# randomly pick one picture\n# display it\ndef choose_species(num_pics,species,width,height,same_individual):\n    if same_individual == True:\n        dataset = data.loc[data['species'] == 'common_dolphin']\n        unique_individual_id = dataset['individual_id'].unique()\n        choose_id = random.choice(unique_individual_id)\n        try:\n            dataset = dataset.loc[dataset['individual_id'] == choose_id].sample(num_pics)\n            pics_id = list(dataset['image'])\n            individual_id = list(dataset['individual_id'])\n            for i in range(num_pics):\n                im = Image.open(\"../input/happy-whale-and-dolphin/train_images/\" + str(pics_id[i]))\n                resized_im = im.resize((width, height))\n                print(\"individual_id: \" + individual_id[i])\n                display(resized_im)\n        except ValueError:\n            print(\"This individual doesn't have enough pics to show.\")\n            print(\"Please set a lower number of pictures.\")\n    else:\n        dataset = data.loc[data['species'] == 'common_dolphin'].sample(num_pics)\n        pics_id = list(dataset['image'])\n        individual_id = list(dataset['individual_id'])\n        for i in range(num_pics):\n            im = Image.open(\"../input/happy-whale-and-dolphin/train_images/\" + str(pics_id[i]))\n            resized_im = im.resize((width, height))\n            print(\"individual_id: \" + individual_id[i])\n            display(resized_im)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-07T17:36:05.626066Z","iopub.execute_input":"2022-02-07T17:36:05.627232Z","iopub.status.idle":"2022-02-07T17:36:05.722638Z","shell.execute_reply.started":"2022-02-07T17:36:05.627181Z","shell.execute_reply":"2022-02-07T17:36:05.721936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Example\nHere we can see two random pictures of one same fin_whale individual, either of the pic has width of 196 and height of 160. ","metadata":{}},{"cell_type":"code","source":"choose_species(num_pics = 2,\n               species = \"fin_whale\",\n               width = 196,\n               height = 160,\n               same_individual = True)","metadata":{"execution":{"iopub.status.busy":"2022-02-07T17:36:05.724263Z","iopub.execute_input":"2022-02-07T17:36:05.725252Z","iopub.status.idle":"2022-02-07T17:36:05.872337Z","shell.execute_reply.started":"2022-02-07T17:36:05.725203Z","shell.execute_reply":"2022-02-07T17:36:05.871287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Choose the pics you are interested to see\n\n**It is required to run the code from your own notebook, so you may need to fork this one.**","metadata":{}},{"cell_type":"code","source":"# create dropdown menus to get input\nunique_species = data['species'].unique()\noutput = Output()\nstart = Button(description=\"Click me\")\nstart.style.button_color = 'lightblue'\n\n\nnum_widget = widgets.Dropdown(\n    options=[1,2,3,4,5],\n    value=list(list([1,2,3,4,5]))[0],\n    description='Nr. of pics',\n    disabled=False,\n)\n\nspecies_widget = widgets.Dropdown(\n    options=list(unique_species),\n    value=list(list(unique_species))[10],\n    description='Species',\n    disabled=False,\n)\n\nsame_widget = widgets.Checkbox(\n    value=False,\n    description='Same individual',\n    disabled=False,\n    indent=False\n)\n\nwidth_widgets = widgets.IntSlider(\n    value=144,\n    min=0,\n    max=360,\n    step=5,\n    description='width',\n    disabled=False,\n    continuous_update=False,\n    orientation='horizontal',\n    readout=True,\n    readout_format='d',\n)\n\nheight_widgets = widgets.IntSlider(\n    value=128,\n    min=0,\n    max=360,\n    step=5,\n    description='height',\n    disabled=False,\n    continuous_update=False,\n    orientation='horizontal',\n    readout=True,\n    readout_format='d',\n)\n\ndef click_start(b):\n    with output:\n        clear_output()\n        print(\"The species you choose is : \" + species_widget.value)\n        choose_species(num_widget.value,\n                      species_widget.value,\n                      width_widgets.value,\n                      height_widgets.value,\n                      same_widget.value)\n        print(\"Click the button to get another one!\")\n\nstart.on_click(click_start)\n\ndisplay(num_widget,\n        species_widget,\n        same_widget,\n        width_widgets,\n        height_widgets,\n        start,\n        output)\n\n\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-07T17:36:05.874034Z","iopub.execute_input":"2022-02-07T17:36:05.874373Z","iopub.status.idle":"2022-02-07T17:36:05.955233Z","shell.execute_reply.started":"2022-02-07T17:36:05.874327Z","shell.execute_reply":"2022-02-07T17:36:05.954382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Can you guess the species of this photo?\n\nLet's play something for fun.\n\nThe program will randomly pick a picture to display and you can choose which species is the photo coming from.","metadata":{}},{"cell_type":"code","source":"# choose one individual by random\nspecies = data['species'].unique()\n# list of wrong options\nwrong_species = list(random.sample(list(species), 3))\nsample = data.sample(1).iloc[0]\nrandom_img = sample['image']\nspecies_sample = sample['species']\npic_route = \"../input/happy-whale-and-dolphin/train_images/\" + str(random_img)\nim = Image.open(pic_route)\nresized_im_guess = im.resize((144, 144))\nprint(\"Do you know which species it is?\")\n\n# display widgets\ncheck = Button(description=\"Check!\")\ncheck.style.button_color = 'lightblue'\n\nspecies_choice = widgets.RadioButtons(\n    options=[*wrong_species, species_sample],\n    description='Species is ',\n    disabled=False\n)\n\noutput_2 = Output()\n\n\n\n\ndef click_check(b):\n    with output_2:\n        clear_output()\n        if species_choice.value == species_sample:\n            print(\"Bingo! You are right!\")\n        else:\n            print(\"Hmmm... the species is \" + str(species_sample) + \" actually.\")\n\n        \ncheck.on_click(click_check)\n\ndisplay(resized_im_guess,\n        species_choice,\n        check,\n        output_2)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-07T17:36:05.957378Z","iopub.execute_input":"2022-02-07T17:36:05.957633Z","iopub.status.idle":"2022-02-07T17:36:06.155589Z","shell.execute_reply.started":"2022-02-07T17:36:05.957602Z","shell.execute_reply":"2022-02-07T17:36:06.154589Z"},"trusted":true},"execution_count":null,"outputs":[]}]}