{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "07f5c7b4-73ff-ade9-7ba9-a74a9f97c939"
      },
      "source": [
        "This is beginning exploration of the training data. Let's begin by grabbing a list of files."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b80233d2-fc39-6429-90b9-ba6fafd9d01e"
      },
      "outputs": [],
      "source": [
        "# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages\n",
        "# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats\n",
        "# For example, here's several helpful packages to load in \n",
        "\n",
        "library(ggplot2) # Data visualization\n",
        "library(readr) # CSV file I/O, e.g. the read_csv function\n",
        "library(stringr) # string manipulation\n",
        "library(raster) # read and manipulate raster images\n",
        "\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "# system(\"ls ../input\")  # It turns out that this command does not work here.\n",
        "\n",
        "# Any results you write to the current directory are saved as output.\n",
        "\n",
        "## Get list of data files\n",
        "files <- list.files(path=\"../input/\", full.names=T, recursive=FALSE)\n",
        "files\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "a3c3ee6b-6320-21c2-c23c-121640738e82"
      },
      "source": [
        "So what exactly is in train_wkt_v2.csv?"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "33f05b75-d61b-0081-89c3-f9f8947b6c73"
      },
      "outputs": [],
      "source": [
        "## Read training data\n",
        "train <- read.csv(\"../input/train_wkt_v2.csv\", stringsAsFactors = FALSE)\n",
        "str(train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "739f7669-1710-442a-ded1-fe3eb076529c"
      },
      "outputs": [],
      "source": [
        "## Take a look at the class representation in each\n",
        "# For now, I use count of comas to represent the object count. This is not right, but it is a start.\n",
        "train$itemcnt <- str_count(train$MultipolygonWKT, \",\")\n",
        "xtabs(itemcnt~ImageId+ClassType, train)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "c091523b-c00f-51d1-ed64-ad98d7c76fe6"
      },
      "source": [
        "Let's make a nice graph of this table"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5e19b8bb-d795-db51-8c84-c4bc7adbec72"
      },
      "outputs": [],
      "source": [
        "#Plot the Data\n",
        "g <- ggplot(train, aes(factor(ClassType), ImageId)) + geom_point(aes(size = itemcnt), colour = \"green\") + theme_bw() + xlab(\"ClassType\") + ylab(\"ImageId\")\n",
        "g + scale_size_continuous(range=c(0,10))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "16c9bcae-3280-ef22-189e-cbbfc1aa4fd9"
      },
      "source": [
        "**Let's look at one of the images**\n",
        "We can start with RGB layers. This looks like a typical satellite view. What are all the dark spots? We can also look at the individual layers."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a45b6d1b-3704-5f18-c828-d63a3ef79dec"
      },
      "outputs": [],
      "source": [
        "img <- raster(\"../input/three_band/6120_2_4.tif\")\n",
        "class(img)\n",
        "plot(img)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6c215c15-760a-b26b-cb36-5b53fb513f9e"
      },
      "outputs": [],
      "source": [
        "imgr <- stack(\"../input/three_band/6120_2_4.tif\")\n",
        "class(imgr)\n",
        "plotRGB(imgr, stretch = \"lin\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a6a74157-fecc-b37d-d3db-42817877c583"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b3ac6cbc-c345-4d7b-0c7f-fe4545eb30f3"
      },
      "outputs": [],
      "source": [
        "imgs <- stack(\"../input/three_band/6120_2_4.tif\")\n",
        "class(imgs)\n",
        "plotRGB(imgs, stretch = \"lin\")\n",
        "plot(imgs)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "e2458cee-a4de-1d7f-be31-9f16c903883f"
      },
      "source": [
        "**Let's look at the 16 band images**\n",
        "These are quite interesting. Several of the layers show detail that is not visible in the RGB layers."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "62dec016-d01e-16b5-ace1-94047bb55910"
      },
      "outputs": [],
      "source": [
        "files16 <- list.files(path=\"../input/sixteen_band/\", pattern=\"6120_\", full.names=T, recursive=FALSE)\n",
        "files16\n",
        "\n",
        "for(i in files16){\n",
        "    thisfile <- i\n",
        "    thisfile\n",
        "    img <- stack(thisfile)\n",
        "    plot(img)\n",
        "}\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7f09880b-70f4-60e5-fe95-965bfb75ba44"
      },
      "outputs": [],
      "source": ""
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "R",
      "language": "R",
      "name": "ir"
    },
    "language_info": {
      "codemirror_mode": "r",
      "file_extension": ".r",
      "mimetype": "text/x-r-source",
      "name": "R",
      "pygments_lexer": "r",
      "version": "3.3.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}