{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Geographical pronunciation statistics" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "import pandas\n", "import MySQLdb\n", "import numpy\n", "import json\n", "\n", "db = MySQLdb.connect(user='root', passwd='Nmmxhjgt1@', db='stimmen', charset='utf8')\n", "\n", "%matplotlib notebook\n", "from matplotlib import pyplot\n", "import folium\n", "from IPython.display import display\n", "from shapely.geometry import Polygon, MultiPolygon, shape, Point, box, mapping\n", "from jupyter_progressbar import ProgressBar\n", "from collections import defaultdict\n", "from ipy_table import make_table\n", "from html import escape\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from matplotlib.colors import LogNorm\n", "from sklearn import mixture\n", "from skimage.measure import find_contours\n", "from collections import Counter\n", "from random import shuffle" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Borders of Frysian municipalities\n", "\n", "with open('Friesland_AL8.GeoJson') as f:\n", " gemeentes = json.load(f)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "coords = [feature['geometry'] for feature in gemeentes['features']]\n", "coords_folium = [[[[c__[::-1] for c__ in c_] for c_ in c] for c in coords_['coordinates']] for coords_ in coords]\n", "shapes = [shape(coords_).simplify(tolerance=0.001) for coords_ in coords]\n", "gemeente_names = [feature['properties']['name'] for feature in gemeentes['features']]\n", "\n", "def get_gemeente(point):\n", " for i, shape in enumerate(shapes):\n", " if shape.contains(point):\n", " return i\n", " return -1" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "# Answers to how participants state a word should be pronounces.\n", "\n", "answers = pandas.read_sql('''\n", "SELECT prediction_quiz_id, user_lat, user_lng, question_text, answer_text\n", "FROM core_surveyresult as survey\n", "INNER JOIN core_predictionquizresult as result ON survey.id = result.survey_result_id\n", "INNER JOIN core_predictionquizresultquestionanswer as answer\n", " ON result.id = answer.prediction_quiz_id\n", "''', db)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "zero_latlng_questions = {\n", " q\n", " for q, row in answers.groupby('question_text').agg('std').iterrows()\n", " if row['user_lat'] == 0 and row['user_lng'] == 0\n", "}\n", "answers_filtered = answers[answers['question_text'].map(lambda x: x not in zero_latlng_questions)]" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/herbert/.virtualenvs/stimmenfryslan/lib/python3.6/site-packages/ipykernel_launcher.py:10: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", " # Remove the CWD from sys.path while we load stuff.\n" ] } ], "source": [ "# Takes approximately 2 minutes\n", "\n", "gemeente_map = {\n", " (lng, lat): get_gemeente(Point(lng, lat))\n", " for lng, lat in set(zip(answers_filtered['user_lng'], answers_filtered['user_lat']))\n", "}\n", "\n", "answers_filtered['gemeente'] = [\n", " gemeente_map[(lng, lat)]\n", " for lat, lng in zip(answers_filtered['user_lat'], answers_filtered['user_lng'])\n", "]" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/herbert/.virtualenvs/stimmenfryslan/lib/python3.6/site-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", " \n" ] } ], "source": [ "answers_filtered['question_text_url'] = answers_filtered['question_text'].map(\n", " lambda x: x.replace('\"', '').replace('*', ''))" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "def reverse(rd_multipolygon):\n", " if len(rd_multipolygon) == 2 and tuple(map(type, rd_multipolygon)) == (float, float):\n", " return rd_multipolygon[::-1]\n", " return [\n", " reverse(element)\n", " for element in rd_multipolygon\n", " ]" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "def get_palette(n, no_black=True, no_white=True):\n", " with open('glasbey/{}_colors.txt'.format(n + no_black + no_white)) as f:\n", " return [\n", " '#%02x%02x%02x' % tuple(int(c) for c in line.replace('\\n', '').split(','))\n", " for line in f\n", " if not no_black or line != '0,0,0\\n'\n", " if not no_white or line != '255,255,255\\n'\n", " ]" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "56dab238a8864488a66a23b26dfa4ce3", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HBox(children=(FloatProgress(value=0.0, max=1.0), HTML(value='0s passed', placeholder='0…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "cdfs = [\n", " np.array([\n", " box(xmin, ymin, xmax_, ymax).intersection(shape).area / shape.area\n", " for xmax_ in numpy.linspace(xmin, xmax, 10001)\n", " ][1:])\n", " for shape in ProgressBar(shapes)\n", " for xmin, ymin, xmax, ymax in [shape.bounds]\n", "]" ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "scrolled": false }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6a7c2ca1bb1042ff8cfd4b6efbb14c2a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HBox(children=(FloatProgress(value=0.0, max=1.0), HTML(value='0s passed', placeholder='0…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "