<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Numbers of Patients Registered at a GP Practice
21/2/17
Number of patients registred with a particular GP who live in a particular LSOA (/via Carl Baker, HoC Library)
Demo sketch of opening data into an interactive Google Map.
Step1:  Previously, I have created a simple sqlite3 database containing administrative open data from NHS Digital (database generator script).
Connect to local copy of the db.
Step2:  Look up the GP practices on the Isle of Wight... (We could search for the code but I happen to know it...)
Step3:  The folium library makes it easy to generate choropleth maps using the Leaflet javascript library. We can use various map tiles - the default is Google Maps.
Step4:  Generate a choropleth map for selected GP practice, colouring LSOA by number of folk registered to that practice.
<ASSISTANT_TASK:>
Python Code:
#Original data source
#http:§§//www.content.digital.nhs.uk/catalogue/PUB23139
#Get the datafile
!wget -P data http://www.content.digital.nhs.uk/catalogue/PUB23139/gp-reg-patients-LSOA-alt-tall.csv
#Import best ever data handling package
import pandas as pd
#Load downloaded CSV file
df=pd.read_csv('data/gp-reg-patients-LSOA-alt-tall.csv')
#Preview first few lines
df.head()
Explanation: Numbers of Patients Registered at a GP Practice
21/2/17
Number of patients registred with a particular GP who live in a particular LSOA (/via Carl Baker, HoC Library)
Demo sketch of opening data into an interactive Google Map.
End of explanation
import sqlite3
#Use homebrew database of NHS administrative info
con = sqlite3.connect("nhsadmin.sqlite")
Explanation: Previously, I have created a simple sqlite3 database containing administrative open data from NHS Digital (database generator script).
Connect to local copy of the db.
End of explanation
ccode='10L'
#Find 
EPRACCUR='epraccur'
epraccur_iw = pd.read_sql_query('SELECT * FROM {typ} WHERE "Commissioner"="{ccode}"'.format(typ=EPRACCUR,ccode=ccode), con)
epraccur_iw
Explanation: Look up the GP practices on the Isle of Wight... (We could search for the code but I happen to know it...)
End of explanation
import folium
#color brewer palettes: ‘BuGn’, ‘BuPu’, ‘GnBu’, ‘OrRd’, ‘PuBu’, ‘PuBuGn’, ‘PuRd’, ‘RdPu’, ‘YlGn’, ‘YlGnBu’, ‘YlOrBr’, and ‘YlOrRd’.
#Fiona is a powerful library for geo wrangling with various dependencies that can make installation a pain...
#...but I have it installed already so I can use it to trivially find the centre of a set of boundaries in a geojson file
import fiona
#This is a canned demo - I happen to have the Local Authority Code for the Isle of Wight...
#...and copies of LSOA geojson files by LA
# (I could get LA code from the NHS addmin db)
geojson_local='../../IWgeodata/lsoa_by_lad/E06000046.json'
fi=fiona.open(geojson_local)
centre_lat,centre_lon=((fi.bounds[0]+fi.bounds[2])/2,(fi.bounds[1]+fi.bounds[3])/2)
Explanation: The folium library makes it easy to generate choropleth maps using the Leaflet javascript library. We can use various map tiles - the default is Google Maps.
End of explanation
#Add a widget in that lets you select the GP practice by name then fudge the lookup to practice code
#We could also add another widget to select eg Male | Female | All
def generate_map(gpcode):
    gpmap = folium.Map([centre_lon,centre_lat], zoom_start=11)
    gpmap.choropleth(
        geo_path=geojson_local,
        data=df[df['PRACTICE_CODE']==gpcode],
        columns=['LSOA_CODE', 'ALL_PATIENTS'],
        key_on='feature.properties.LSOA11CD',
        fill_color='PuBuGn', fill_opacity=0.7,
        legend_name='Number of people on list in LSOA'
        )
    
    return gpmap
def generate_map_from_gpname(gpname):
    gpcode=epraccur_iw[epraccur_iw['Name']==gpname]['Organisation Code'].iloc[0]
    return generate_map(gpcode)
#iw_gps=epraccur_iw['Organisation Code'].unique().tolist()
iw_gps=epraccur_iw['Name'].unique().tolist()
iw_gps[:3],len(iw_gps)
from ipywidgets import interact
interact(generate_map_from_gpname, gpname=iw_gps);
Explanation: Generate a choropleth map for selected GP practice, colouring LSOA by number of folk registered to that practice.
End of explanation
<END_TASK>