ehsk commited on
Commit
93db903
·
1 Parent(s): 0d7a985

Upload gwf_app.py

Browse files
Files changed (1) hide show
  1. gwf_app.py +93 -0
gwf_app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import numpy as np
4
+ import pandas as pd
5
+ import plotly.express as px
6
+ import streamlit as st
7
+
8
+ st.set_page_config(layout="wide")
9
+ DATA_FILE = "data/gwf_2017-2021_specter2_base.json"
10
+ THEMES = {"cluster": "fall", "year": "mint", "source": "phase"}
11
+
12
+
13
+ def load_df(data_file: os.PathLike):
14
+ df = pd.read_json(data_file, orient="records")
15
+ df["x"] = df["point2d"].apply(lambda x: x[0])
16
+ df["y"] = df["point2d"].apply(lambda x: x[1])
17
+ df["year"] = df["year"].replace("", 0)
18
+ df["year"] = df["year"].astype(int)
19
+ if "publication_type" in df.columns:
20
+ df["type"] = df["publication_type"]
21
+ df = df.drop(columns=["point2d", "publication_type"])
22
+ else:
23
+ df = df.drop(columns=["point2d"])
24
+ return df
25
+
26
+
27
+ @st.cache_data
28
+ def load_dataframe():
29
+ return load_df(DATA_FILE)
30
+
31
+
32
+ DF = load_dataframe()
33
+ DF["opacity"] = 0.04
34
+ min_year, max_year = DF[DF["year"] > 0]["year"].min(), DF[DF["year"] > 0]["year"].max()
35
+
36
+ with st.sidebar:
37
+ start_year, end_year = st.select_slider(
38
+ "Publication year",
39
+ options=[str(y) for y in range(min_year, max_year + 1)],
40
+ value=(str(min_year), str(max_year)),
41
+ )
42
+ src = st.text_input("Source")
43
+
44
+ author_names = st.text_input("Author names (separated by comma)")
45
+
46
+ title = st.text_input("Title")
47
+
48
+ start_year = int(start_year)
49
+ end_year = int(end_year)
50
+ df_mask = (DF["year"] >= start_year) & (DF["year"] <= end_year)
51
+
52
+ if src:
53
+ df_mask = df_mask & DF.source.apply(lambda x: src.lower() in x.lower())
54
+
55
+ if author_names:
56
+ authors = [a.strip() for a in author_names.split(",")]
57
+ author_mask = DF.authors.apply(
58
+ lambda row: all(any(re.match(rf".*{a}.*", x, re.IGNORECASE) for x in row) for a in authors)
59
+ )
60
+ df_mask = df_mask & author_mask
61
+
62
+ if title:
63
+ df_mask = df_mask & DF.title.apply(lambda x: title.lower() in x.lower())
64
+
65
+ DF.loc[df_mask, "opacity"] = 1.0
66
+ st.write(f"Number of points: {DF[df_mask].shape[0]}")
67
+
68
+ color = st.selectbox("Color", ("cluster", "year", "source"))
69
+
70
+
71
+ fig = px.scatter(
72
+ DF,
73
+ x="x",
74
+ y="y",
75
+ opacity=DF["opacity"],
76
+ color=color,
77
+ width=1000,
78
+ height=800,
79
+ hover_data=["title", "authors", "year", "source", "type"],
80
+ color_continuous_scale=THEMES[color],
81
+ )
82
+ fig.update_layout(
83
+ # margin=dict(l=10, r=10, t=10, b=10),
84
+ showlegend=False,
85
+ font=dict(
86
+ family="Times New Roman",
87
+ size=30,
88
+ ),
89
+ )
90
+ fig.update_xaxes(title="")
91
+ fig.update_yaxes(title="")
92
+
93
+ st.plotly_chart(fig, use_container_width=True)