Skip to content

Instantly share code, notes, and snippets.

View StephenFordham's full-sized avatar

Stephen Fordham StephenFordham

View GitHub Profile
@StephenFordham
StephenFordham / flask_login.py
Created November 6, 2021 19:49
flask_login
@app.route('/login', methods=['POST', 'GET'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user is None:
flash('Please register to gain access', category='danger')
return redirect(url_for('register'))
@StephenFordham
StephenFordham / plotting_animated_bar.py
Created June 23, 2021 14:15
plotting_animated_bar
fig = px.bar(plot_df, x='Provider County', y="Observed Prescribing Rate per 100 Visits", color="Provider County",
animation_frame="Year", animation_group="Provider County", range_y=[3, 70],
title='Antibiotic Prescribing rates in US counties')
fig.show()
keys = [i for i in range(65)]
all_dataframes = {}
for i, county in zip(keys, df['Provider County'].unique()):
df2 = df[df['Provider County'] == county].sort_values('Year', ascending=True)
all_dataframes[i] = df2
master_df = []
for i in all_dataframes:
master_df.append(all_dataframes[i])
import pandas as pd
import plotly.express as px
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
df = pd.read_csv('potentially-avoidable-antibiotic-prescribing-rates.csv')
@StephenFordham
StephenFordham / optimised_resistance_scoring_plotting.py
Created April 13, 2021 12:19
optimised_resistance_scoring_plotting
fig, ax = plt.subplots(figsize=(14, 16))
res_geo.plot(ax=ax, figsize=(14, 16), column=res_geo['resistance_scores'], cmap='cool', k=6,
missing_kwds=dict(color="lightgrey",label='No Data/<10 isolates'),
scheme='FisherJenks',
edgecolor = 'black',
legend=True,
linewidth = 0.1,
legend_kwds=dict(loc='lower left', frameon=False, title='Resistance Scores'))
@StephenFordham
StephenFordham / plotting_resistance_scores.py
Created April 13, 2021 12:13
plotting_resistance_scores
res_geo.plot(figsize=(12, 16), column=res_geo['resistance_scores'], cmap='OrRd',
edgecolor = 'black',
linewidth = 0.1)
@StephenFordham
StephenFordham / merging_dataframe.py
Created April 13, 2021 11:54
merging_dataframe
data = gdf[gdf['country'].isin(res['country'].to_list())]
res_df = res.merge(data, on='country')
others = gdf[~gdf['country'].isin(res['country'].to_list())]
others['resistance_scores'] = np.NaN
res_geo = pd.concat([res_df, others])
@StephenFordham
StephenFordham / country_data_cleaning.py
Created April 13, 2021 11:23
country_data_cleaning
data_countries = res['country'].to_list()
all_countries = gdf['country'].to_list()
same_countries = []
different_countries = []
for country in data_countries:
if country not in all_countries:
different_countries.append(country)
@StephenFordham
StephenFordham / representative_countries_2.py
Created April 13, 2021 11:11
representative_countries
res = pd.DataFrame(res.groupby('country').mean()['resistance_scores'].sort_values(ascending=False))
res = res[res.index.isin(representative_countires)].reset_index()
@StephenFordham
StephenFordham / antibiotic_resistance_mapping.py
Created April 13, 2021 10:46
antibiotic_resistance_mapping
res = pd.read_csv('resistance_score__Country__res_0-3_vir_0-5.csv')
res['resistance_scores'] = res['annotation'].apply(lambda x: int(x[0]))
res.drop('annotation', axis=1, inplace=True)
res.columns = ['country', 'resistance_scores']
res.groupby('country').mean()['resistance_scores'].sort_values(ascending=False)