Skip to content

Commit

Permalink
WIP dashboard : year shortcuts, widgets, comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrotsmnrd committed Aug 22, 2024
1 parent 467d15f commit 62b692f
Showing 1 changed file with 119 additions and 30 deletions.
149 changes: 119 additions & 30 deletions web_api/dashboard/main_dashboard.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

import pandas as pd
import panel as pn
import param
Expand All @@ -15,18 +17,28 @@

groups = {"year": "int"}


datasets_metrics = {
extraction_tools_metrics = {
"RTransparent": ["is_data_pred", "is_code_pred", "score", "eigenfactor_score"]
}

dims_aggregations = {
"is_data_pred": ["percent", "count_true", "count"],
"is_data_pred": ["percent", "count_true"],
"is_code_pred": ["percent", "count_true"],
"score": ["mean"],
"eigenfactor_score": ["mean"],
}

metrics_titles = {
"percent_is_data_pred": "Data Sharing (%)",
"percent_is_code_pred": "Code Sharing (%)",
"count_true_is_data_pred": "Data Sharing",
"count_true_is_code_pred": "Code Sharing",
"mean_score": "Mean Score",
"mean_eigenfactor_score": "Mean Eigenfactor Score",
}

metrics_by_title = {v: k for k, v in metrics_titles.items()}


aggregation_formulas = {
"percent": lambda x: x.mean() * 100,
Expand All @@ -42,9 +54,9 @@ class MainDashboard(param.Parameterized):
"""

# High-level parameters.
dataset = param.Selector(default="", objects=[], label="Dataset")
extraction_tool = param.Selector(default="", objects=[], label="Extraction tool")

metrics = param.ListSelector(default=[], objects=[], label="Metrics")
metrics = param.Selector(default=[], objects=[], label="Metrics")

splitting_var = param.Selector(
default="year",
Expand All @@ -53,7 +65,9 @@ class MainDashboard(param.Parameterized):
)

# Filters
filter_pubdate = param.Range(step=1, label="Publication date")
filter_pubdate = param.Range( # (2000, 2024), bounds=(2000, 2024),
step=1, label="Publication date"
)

filter_journal = param.Selector(
default="All journals (including empty)",
Expand All @@ -73,24 +87,45 @@ def __init__(self, datasets, **params):

# By default, take the first dataset.
# Currently, there's only RTransparent
self.param.dataset.objects = list(self.datasets.keys())
self.dataset = self.param.dataset.objects[0]
self.param.extraction_tool.objects = list(self.datasets.keys())
self.extraction_tool = self.param.extraction_tool.objects[0]

@pn.depends("extraction_tool", watch=True)
def did_change_extraction_tool(self):
print("DID_CHANGE_EXTRACTION_TOOL")

new_extraction_tools_metrics_metrics = extraction_tools_metrics[
self.extraction_tool
]

new_metrics = []
for m in new_extraction_tools_metrics_metrics:
for agg in dims_aggregations[m]:
new_metrics.append(metrics_titles[f"{agg}_{m}"])

self.param.metrics.objects = new_metrics
self.metrics = self.param.metrics.objects[0]

# self.param.metrics.objects = extraction_tools_metrics[self.extraction_tool]
# self.metrics = self.param.metrics.objects[0]

@pn.depends("dataset", watch=True)
def did_change_dataset(self):
self.metrics = datasets_metrics[self.dataset]
self.raw_data = self.datasets[self.dataset]
self.raw_data = self.datasets[self.extraction_tool]
print(self.raw_data)
# breakpoint()

# Hardcoded for RTransparent for the moment, update to more generic later

self.param.filter_pubdate.bounds = (
self.raw_data.year.min(),
self.raw_data.year.max(),
# self.raw_data.year.max(),
# Use current year instead, so the "Past X years" buttons work
datetime.now().year,
)
self.param.filter_pubdate.default = (
self.raw_data.year.min(),
self.raw_data.year.max(),
)
self.filter_pubdate = (self.raw_data.year.min(), self.raw_data.year.max())

self.param.filter_selected_journals.objects = self.raw_data.journal.unique()
# As default, takes the journals with the biggest number of occurences
Expand Down Expand Up @@ -125,24 +160,34 @@ def filtered_grouped_data(self):

return result

@pn.depends("dataset", "splitting_var", "filter_pubdate")
@pn.depends("extraction_tool", "splitting_var", "filter_pubdate", "metrics")
def get_echart_plot(self):
print("GET_ECHART_PLOT")

if self.filter_pubdate is None:
# The filters are not yet initialized
# Let's return an empty plot
return pn.pane.ECharts({}, height=640, width=840, renderer="svg")

df = self.filtered_grouped_data()

raw_metric = metrics_by_title[self.metrics]

xAxis = df[self.splitting_var].tolist()
series = [
{
"id": serie,
"name": serie,
"id": self.metrics,
"name": self.metrics,
"type": "line",
"data": df[serie].tolist(),
"data": df[raw_metric].tolist(),
}
for serie in ["percent_is_data_pred", "percent_is_code_pred"]
]

title = f"{self.metrics} by {self.splitting_var} ({int(self.filter_pubdate[0])}-{int(self.filter_pubdate[1])})"

echarts_config = {
"title": {
"text": "Percentage of Publications Following Open Science Practices Over Time",
"text": title,
},
"tooltip": {
"show": True,
Expand All @@ -152,8 +197,9 @@ def get_echart_plot(self):
# {{a1}} : {{c1}} """,
},
"legend": {
#'data':['Sales']
"data": ["is_data_pred", "is_code_pred"],
"data": [
{"name": self.metrics, "icon": "path://M 0 0 H 20 V 20 H 0 Z"},
],
"orient": "vertical",
"right": 10,
"top": 20,
Expand Down Expand Up @@ -183,21 +229,28 @@ def get_echart_plot(self):

@pn.depends("filter_pubdate.bounds")
def get_pubdate_filter(self):
print("GET_PUBDATE_FILTER")

# It's the slider that controls the filter_pubdate param
pubdate_slider = pn.widgets.RangeSlider.from_param(self.param.filter_pubdate)

# The text inputs only reflect and update the value of the slider's bounds
start_pubdate_input = pn.widgets.TextInput(
value=str(int(self.param.filter_pubdate.bounds[0])), width=80
)
end_pubdate_input = pn.widgets.TextInput(
value=str(int(self.param.filter_pubdate.bounds[1])), width=80
)

pubdate_slider = pn.widgets.RangeSlider.from_param(self.param.filter_pubdate)

# When the slider's value change, update the TextInputs
def update_pubdate_text_inputs(event):
start_pubdate_input.value = str(pubdate_slider.value[0])
end_pubdate_input.value = str(pubdate_slider.value[1])

pubdate_slider.param.watch(update_pubdate_text_inputs, "value")

# When the TextInputs' value change, update the slider,
# which updated the filter_pubdate param
def update_pubdate_slider(event):
pubdate_slider.value = (
int(start_pubdate_input.value or self.param.filter_pubdate.bounds[0]),
Expand All @@ -207,18 +260,50 @@ def update_pubdate_slider(event):
start_pubdate_input.param.watch(update_pubdate_slider, "value")
end_pubdate_input.param.watch(update_pubdate_slider, "value")

return pn.Column(pn.Row(start_pubdate_input, end_pubdate_input), pubdate_slider)
last_year_button = pn.widgets.Button(
name="Last year", width=80, button_type="light", button_style="solid"
)
past_5years_button = pn.widgets.Button(
name="Past 5 years", width=80, button_type="light", button_style="solid"
)
past_10years_button = pn.widgets.Button(
name="Past 10 years", width=80, button_type="light", button_style="solid"
)

def did_click_shortcut_button(event):
print(event)
if event.obj.name == "Last year":
pubdate_slider.value = (datetime.now().year, datetime.now().year)
elif event.obj.name == "Past 5 years":
pubdate_slider.value = (datetime.now().year - 5, datetime.now().year)
elif event.obj.name == "Past 10 years":
pubdate_slider.value = (datetime.now().year - 10, datetime.now().year)

last_year_button.on_click(did_click_shortcut_button)
past_5years_button.on_click(did_click_shortcut_button)
past_10years_button.on_click(did_click_shortcut_button)
pubdate_shortcuts = pn.Row(
last_year_button, past_5years_button, past_10years_button
)

return pn.Column(
pn.Row(start_pubdate_input, end_pubdate_input),
pubdate_slider,
pubdate_shortcuts,
)

@pn.depends("dataset", "filter_journal")
@pn.depends("extraction_tool", "filter_journal")
def get_sidebar(self):
print("GET_SIDEBAR")

items = [
pn.pane.Markdown("## Filters"),
pn.pane.Markdown("### Applied Filters"),
pn.pane.Markdown("(todo)"),
pn.layout.Divider(),
pn.pane.Markdown("### Publication Details"),
# pn.pane.Markdown("#### Publication Date"),
self.get_pubdate_filter,
self.get_pubdate_filter(),
pn.layout.Divider(),
pn.widgets.Select.from_param(self.param.filter_journal),
]
Expand All @@ -234,18 +319,22 @@ def get_sidebar(self):

return sidebar

@pn.depends("dataset")
@pn.depends("extraction_tool")
def get_top_bar(self):
print("GET_TOP_BAR")

return pn.Row(
pn.widgets.Select.from_param(self.param.dataset),
pn.widgets.CheckBoxGroup.from_param(self.param.metrics),
pn.widgets.Select.from_param(self.param.extraction_tool),
pn.widgets.Select.from_param(self.param.metrics),
pn.widgets.Select.from_param(self.param.splitting_var),
)

@pn.depends(
"dataset", "filter_journal", "filter_selected_journals", "splitting_var"
"extraction_tool", "filter_journal", "filter_selected_journals", "splitting_var"
)
def get_dashboard(self):
print("GET_DASHBOARD")

# Layout the dashboard
dashboard = pn.Column(
"# Data and code transparency",
Expand Down

0 comments on commit 62b692f

Please sign in to comment.