{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"b878500d-ac91-574f-fb85-af99a66ff166"},"source":"## Reading WKT and plotting with Pyplot"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b9bc9c0c-0fa1-da59-73cf-ed39517994ef"},"outputs":[],"source":"import pandas as pd\nimport numpy as np\nfrom shapely.wkt import loads\nfrom matplotlib.patches import Polygon\nimport matplotlib.pyplot as plt\n\ndf = pd.read_csv('../input/train_wkt.csv')\ndf.head()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"91fb5cc9-d588-26a6-04e5-86ab1b99bfe3"},"outputs":[],"source":"df['ImageId'].unique()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c233d5d6-68a0-2ae8-5757-d58291245163"},"outputs":[],"source":"# Use just first image\npolygonsList = {}\nimage = df[df.ImageId == '6010_4_2']\nfor cType in image.ClassType.unique():\n    polygonsList[cType] = loads(image[image.ClassType == cType].MultipolygonWKT.values[0])"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"1fcab23c-506c-736d-86a4-32644a7226ce"},"outputs":[],"source":"# plot using matplotlib\nfig, ax = plt.subplots(figsize=(8, 8))\n\n# colors are going to be grayscale for now\nfor p in polygonsList:\n    for polygon in polygonsList[p]:\n        mpl_poly = Polygon(np.array(polygon.exterior), color=str(p/10.), lw=0, alpha=0.4)\n        ax.add_patch(mpl_poly)\n\nax.relim()\nax.autoscale()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"7a4b1b5a-43bf-911f-fbe3-5ad5e8e937d7"},"outputs":[],"source":"# number of objects on the image by type\n'''\n1. Buildings\n2. Misc. Manmade structures \n3. Road \n4. Track - poor/dirt/cart track, footpath/trail\n5. Trees - woodland, hedgerows, groups of trees, standalone trees\n6. Crops - contour ploughing/cropland, grain (wheat) crops, row (potatoes, turnips) crops\n7. Waterway \n8. Standing water\n9. Vehicle Large - large vehicle (e.g. lorry, truck,bus), logistics vehicle\n10. Vehicle Small - small vehicle (car, van), motorbike\n'''\nfor p in polygonsList:\n    print(\"Type: {:4d}, objects: {}\".format(p,len(polygonsList[p].geoms)))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"1d36b810-b721-779a-5e5e-2859096178d4"},"outputs":[],"source":"# average area of objects by type\nfor p in polygonsList:\n    areas = []\n    for i in polygonsList[p]:\n        areas.append(i.area)\n    print(\"Type: {:4d}, mean.area: {:4.2e}\".format(p,np.mean(areas)))"},{"cell_type":"markdown","metadata":{"_cell_guid":"e245177a-5548-bdbb-2c52-f552a7f3093b"},"source":"## Exploring available dataset"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ea7c4f5d-76ab-3b89-2eb4-b42c713d55ee"},"outputs":[],"source":"# number of images in available kernel dataset?\ndf.ImageId.unique()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"79374cf5-b772-754b-b6ce-8227bdafacb4"},"outputs":[],"source":"# convert to shapely, get geometries and pivot\ndf['polygons'] = df.apply(lambda row: loads(row.MultipolygonWKT),axis=1)\ndf['nPolygons'] = df.apply(lambda row: len(row['polygons'].geoms),axis=1)\n\npvt = df.pivot(index='ImageId', columns='ClassType', values='nPolygons')\npvt"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"a965f287-e53c-0cd4-591e-bbe428277701"},"outputs":[],"source":"fig, ax = plt.subplots(figsize=(8, 4))\nax.set_aspect('equal')\nplt.imshow(pvt.T, interpolation='nearest', cmap=plt.cm.Blues)\nplt.title('Number of objects by type')\nplt.ylabel('Class Type')\nplt.xlabel('Train Image')\nplt.colorbar()\nplt.show()"},{"cell_type":"markdown","metadata":{"_cell_guid":"4e4cbeae-b332-2173-a14d-65bbbd48905e"},"source":"Numbers wise trees are leading by far, being the only major object type on some of the images. They are followed by buildings and other man structures, which are actually not present on every image, as well as vehicles."}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","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.5.2"}},"nbformat":4,"nbformat_minor":0}