Spaces:
Running
Running
Create 4 Sunburst.py
Browse files- pages/4 Sunburst.py +110 -0
pages/4 Sunburst.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#===import module===
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
import numpy as np
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
#===config===
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="Coconut",
|
| 11 |
+
page_icon="🥥",
|
| 12 |
+
layout="wide"
|
| 13 |
+
)
|
| 14 |
+
st.header("Data visualization")
|
| 15 |
+
st.subheader('Put your CSV file and choose a visualization')
|
| 16 |
+
|
| 17 |
+
#===clear cache===
|
| 18 |
+
def reset_all():
|
| 19 |
+
st.cache_data.clear()
|
| 20 |
+
|
| 21 |
+
#===check type===
|
| 22 |
+
@st.cache_data(ttl=3600)
|
| 23 |
+
def get_ext(extype):
|
| 24 |
+
extype = uploaded_file.name
|
| 25 |
+
return extype
|
| 26 |
+
|
| 27 |
+
@st.cache_data(ttl=3600)
|
| 28 |
+
def upload(extype):
|
| 29 |
+
papers = pd.read_csv(uploaded_file)
|
| 30 |
+
return papers
|
| 31 |
+
|
| 32 |
+
@st.cache_data(ttl=3600)
|
| 33 |
+
def conv_txt(extype):
|
| 34 |
+
col_dict = {'TI': 'Title',
|
| 35 |
+
'SO': 'Source title',
|
| 36 |
+
'DT': 'Document Type',
|
| 37 |
+
'DE': 'Author Keywords',
|
| 38 |
+
'ID': 'Keywords Plus',
|
| 39 |
+
'AB': 'Abstract',
|
| 40 |
+
'TC': 'Cited by',
|
| 41 |
+
'PY': 'Year',}
|
| 42 |
+
papers = pd.read_csv(uploaded_file, sep='\t', lineterminator='\r')
|
| 43 |
+
papers.rename(columns=col_dict, inplace=True)
|
| 44 |
+
return papers
|
| 45 |
+
|
| 46 |
+
#===Read data===
|
| 47 |
+
uploaded_file = st.file_uploader("Choose a file", type=['csv', 'txt'], on_change=reset_all)
|
| 48 |
+
|
| 49 |
+
if uploaded_file is not None:
|
| 50 |
+
extype = get_ext(uploaded_file)
|
| 51 |
+
if extype.endswith('.csv'):
|
| 52 |
+
papers = upload(extype)
|
| 53 |
+
|
| 54 |
+
elif extype.endswith('.txt'):
|
| 55 |
+
papers = conv_txt(extype)
|
| 56 |
+
|
| 57 |
+
@st.cache_data(ttl=3600)
|
| 58 |
+
def get_minmax(extype):
|
| 59 |
+
extype = extype
|
| 60 |
+
MIN = int(papers['Year'].min())
|
| 61 |
+
MAX = int(papers['Year'].max())
|
| 62 |
+
GAP = MAX - MIN
|
| 63 |
+
return papers, MIN, MAX, GAP
|
| 64 |
+
|
| 65 |
+
tab1, tab2 = st.tabs(["📈 Generate visualization", "📓 Recommended Reading"])
|
| 66 |
+
|
| 67 |
+
with tab1:
|
| 68 |
+
#===sunburst===
|
| 69 |
+
papers, MIN, MAX, GAP = get_minmax(extype)
|
| 70 |
+
|
| 71 |
+
if (GAP != 0):
|
| 72 |
+
YEAR = st.slider('Year', min_value=MIN, max_value=MAX, value=(MIN, MAX), on_change=reset_all)
|
| 73 |
+
else:
|
| 74 |
+
st.write('You only have data in ', (MAX))
|
| 75 |
+
YEAR = (MIN, MAX)
|
| 76 |
+
|
| 77 |
+
@st.cache_data(ttl=3600)
|
| 78 |
+
def listyear(extype):
|
| 79 |
+
global papers
|
| 80 |
+
years = list(range(YEAR[0],YEAR[1]+1))
|
| 81 |
+
papers = papers.loc[papers['Year'].isin(years)]
|
| 82 |
+
return years, papers
|
| 83 |
+
|
| 84 |
+
@st.cache_data(ttl=3600)
|
| 85 |
+
def vis_sunbrust(extype):
|
| 86 |
+
papers['Cited by'] = papers['Cited by'].fillna(0)
|
| 87 |
+
vis = pd.DataFrame()
|
| 88 |
+
vis[['doctype','source','citby','year']] = papers[['Document Type','Source title','Cited by','Year']]
|
| 89 |
+
viz=vis.groupby(['doctype', 'source', 'year'])['citby'].agg(['sum','count']).reset_index()
|
| 90 |
+
viz.rename(columns={'sum': 'cited by', 'count': 'total docs'}, inplace=True)
|
| 91 |
+
|
| 92 |
+
fig = px.sunburst(viz, path=['doctype', 'source', 'year'], values='total docs',
|
| 93 |
+
color='cited by',
|
| 94 |
+
color_continuous_scale='RdBu',
|
| 95 |
+
color_continuous_midpoint=np.average(viz['cited by'], weights=viz['total docs']))
|
| 96 |
+
fig.update_layout(height=800, width=1200)
|
| 97 |
+
return fig
|
| 98 |
+
|
| 99 |
+
years, papers = listyear(extype)
|
| 100 |
+
|
| 101 |
+
if {'Document Type','Source title','Cited by','Year'}.issubset(papers.columns):
|
| 102 |
+
fig = vis_sunbrust(extype)
|
| 103 |
+
st.plotly_chart(fig, height=800, width=1200) #use_container_width=True)
|
| 104 |
+
|
| 105 |
+
else:
|
| 106 |
+
st.error('We require these columns: Document Type, Source title, Cited by, Year', icon="🚨")
|
| 107 |
+
|
| 108 |
+
with tab2:
|
| 109 |
+
st.markdown('**numpy.average — NumPy v1.24 Manual. (n.d.). Numpy.Average — NumPy v1.24 Manual.** https://numpy.org/doc/stable/reference/generated/numpy.average.html')
|
| 110 |
+
st.markdown('**Sunburst. (n.d.). Sunburst Charts in Python.** https://plotly.com/python/sunburst-charts/')
|