Skip to content

Commit

Permalink
added helper library
Browse files Browse the repository at this point in the history
  • Loading branch information
linozen committed Aug 19, 2021
1 parent 188b871 commit 51744f6
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
Empty file added explorer/lib/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions explorer/lib/download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import base64
from io import BytesIO
import pandas as pd


def get_csv_download_link(df, name):
"""Generates a link allowing the data in a given panda dataframe to be downloaded
in: dataframe
out: href string
"""
csv = df.to_csv(index=False)
b64 = base64.b64encode(
csv.encode()
).decode() # some strings <-> bytes conversions necessary here
href = f'<a href="data:file/csv;base64,{b64}" download="ioi_{name}.csv">Download as CSV file</a>'
return href


def to_excel(df):
output = BytesIO()
writer = pd.ExcelWriter(output, engine="xlsxwriter")
df.to_excel(writer, sheet_name="data")
writer.save()
processed_data = output.getvalue()
return processed_data


def get_excel_download_link(df, name):
"""Generates a link allowing the data in a given panda dataframe to be downloaded
in: dataframe
out: href string
"""
val = to_excel(df)
b64 = base64.b64encode(val)
return f'<a href="data:application/octet-stream;base64,{b64.decode()}" download="ioi_{name}.xlsx">Download as Excel file</a>'
78 changes: 78 additions & 0 deletions explorer/lib/figures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px


def generate_pie_chart(
df,
values,
names,
hover_name,
color,
color_discrete_sequence,
color_discrete_map,
):
fig = px.pie(
df,
values=values,
names=names,
hover_name=hover_name,
color=color,
color_discrete_sequence=color_discrete_sequence,
color_discrete_map=color_discrete_map,
width=800,
)
fig.update_traces(textinfo="percent+value")
return fig


def generate_histogram(df, x, y, nbins, color, color_discrete_map, labels):
fig = px.histogram(
df,
x=x,
y=y,
nbins=nbins,
color=color,
color_discrete_map=color_discrete_map,
labels=labels,
)
return fig


def generate_stacked_bar_chart(data):
fig = go.Figure(data=data)
fig.update_layout(width=800, height=800, barmode="stack")
return fig


def generate_ranking_plot(df, input_col, options, scoring):
input_col_score = pd.Series(index=options)
for i in range(1, 7):
input_col_counts = df[f"{input_col}[{i}]"].value_counts()
scores = input_col_counts.multiply(scoring[i])
input_col_score = input_col_score.add(scores, fill_value=0)
input_col_score = input_col_score.sort_values(ascending=False)
if i == 1:
ranked_first = df[f"{input_col}[1]"].value_counts()
ranked_first_clean = pd.DataFrame(
{"institution": ranked_first.index, "ranked_first": ranked_first.values}
)
input_col_df = pd.DataFrame(
{
"institution": input_col_score.index,
"score": input_col_score.values,
}
)
input_col_df = input_col_df.merge(
ranked_first_clean, on="institution", how="left"
).fillna(0)
input_col_df = input_col_df.sort_values(["score", "ranked_first"])
fig = px.bar(
input_col_df.sort_values(by="score"),
y="institution",
x="score",
color="ranked_first",
color_continuous_scale="viridis",
orientation="h",
)
return fig

0 comments on commit 51744f6

Please sign in to comment.