-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_publications.py
More file actions
104 lines (92 loc) · 2.78 KB
/
Copy pathcount_publications.py
File metadata and controls
104 lines (92 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""this code generates an interactive graph showing both the
cumulative number of publications in the covid publications datebase
as well as the number added each month. It reads the.json directly from
the database"""
import json
from pathlib import Path
import pandas as pd
import plotly.graph_objects as go
# get data
data_path = Path(__file__).parent / "Swedish_COVID19_publications_data.json"
with open(data_path) as f:
txt = json.load(f)
df = pd.json_normalize(txt["publications"])
df = df[["type", "published"]]
# for now, assume dates with day as 00 are 1st day
df.replace("-00", "-01", regex=True, inplace=True)
# make the cumulative sum of papers published
df.published = pd.to_datetime(df["published"], format="%Y-%m-%d").dt.date
currdate = pd.to_datetime("today").date()
df = df[df["published"] < currdate]
df.sort_values(by="published", inplace=True)
df1 = df["published"].value_counts().sort_index().reset_index()
df1["cumulativecount"] = df1["count"].cumsum()
# find number of papers published in each month
df["year"] = pd.DatetimeIndex(df["published"]).year
df["month"] = pd.DatetimeIndex(df["published"]).month
df["index"] = pd.to_datetime(df[["year", "month"]].assign(day=1))
df2 = df.groupby(["index"]).size().reset_index(name="Number Added")
# make the graph
# bar chart
trace2 = go.Bar(
x=df2["index"],
y=df2["Number Added"],
name="New publications each month",
marker_color="rgb(222,44,108)",
hovertemplate="Month: %{x|%B %Y}" + "<br>New publications: %{y}<extra></extra>",
)
# line graph
trace1 = go.Scatter(
mode="lines",
x=df1["published"],
y=df1["cumulativecount"],
name="Cumulative Total",
marker_color="rgb(46,104,165)",
line_width=5,
hovertemplate="Date: %{x}" + "<br>Total Publications: %{y}<extra></extra>",
)
# combine traces
data = [trace2, trace1]
# figure layout
fig = go.Figure(data=data)
fig.update_layout(
plot_bgcolor="white",
font=dict(size=12),
margin=dict(r=0, t=50),
autosize=True,
legend=dict(
orientation="h",
yanchor="bottom",
y=1,
xanchor="left",
x=-0.1,
),
# width = 1000,
# height = 500
)
# modify x-axis
fig.update_xaxes(
title="<b>Date</b>",
showgrid=True,
linecolor="black",
tickangle=45,
# dtick="M1",
# set start point of x-axis
tick0="2020-01-01",
)
# modify y-axis
fig.update_yaxes(
title="<b>Number of Publications</b>",
showgrid=True,
gridcolor="grey",
linecolor="black",
# change range to envelope the appropriate range
range=[0, max(df1["cumulativecount"] + 50)],
)
# Below produces a .html file
# fig.write_html('Count_by_today.html', include_plotlyjs=False, full_html=False)
# Below for testing
fig.show()
fig.write_json("COVID_publication_count.json")
# Below for dynamic use
# print(fig.to_json())