Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit ae696919 authored by Samuël Weber's avatar Samuël Weber
Browse files

add scatterplot per species

parent 9dbb1f71
......@@ -554,11 +554,13 @@ def update_box_graph(datatable, temporality, plots_options, groupby_var, species
@app.callback(Output('scatter-graph', 'figure'),
[Input('datatable', 'derived_virtual_data'),
Input('tab-2-col', 'value')],
Input('tab-2-col', 'value'),
Input('scatter_groupby', 'value'),
],
[State('specie-dropdown', 'value'),
State('source-dropdown', 'value'),
State('options_check', 'value')])
def update_scatter_graph(datatable, tabselected, species, sources, options):
def update_scatter_graph(datatable, tabselected, groupby, species, sources, options):
"""Update the scatter graph
:datatable: TODO
......@@ -592,8 +594,8 @@ def update_scatter_graph(datatable, tabselected, species, sources, options):
if tabselected != "2":
return figure
if datatable is None or len(datatable) == 0 or len(species+sources) != 2:
figure["layout"]["title"] = "Please select only 2 species/sources"
if datatable is None or len(datatable) == 0:
figure["layout"]["title"] = "Please select at least 2 species/sources or 2 stations"
return figure
dfdt = pd.DataFrame(datatable)
......@@ -602,43 +604,66 @@ def update_scatter_graph(datatable, tabselected, species, sources, options):
dfdt["Labels"] = np.nan
stations = list(dfdt["Station"].unique())
if groupby == "site":
if len(species+sources) != 2:
figure["layout"]["title"] = "Please select 2 species"
return figure
elif groupby == "specie":
if len(stations) != 2:
figure["layout"]["title"] = "Please select 2 stations"
return figure
# species = set(species) - set(BASE_VAR_SP) - set(notNumeric)
# sources = set(sources) - set(BASE_VAR_SRC)
variables = species + sources
dfdt = dfdt[variables+["Station", "Date"]].dropna()
if groupby == "specie":
df = dfdt[["Date", "Station"] + variables]
df = df.pivot_table(index=["Date"], columns=["Station"], values=variables)\
.reset_index()\
.melt(id_vars=["Date"])\
.dropna()
df.columns = ["Date", "Specie", "Station", "value"]
dfdt = df.pivot_table(index=["Date", "Specie"], columns=["Station"]).reset_index()
dfdt.columns = ["Date", "Specie"] + list(dfdt.columns.get_level_values("Station")[-2:])
else:
dfdt = dfdt[variables+["Station", "Date"]].dropna()
x, y = stations if groupby == "specie" else variables
color = "Specie" if groupby == "specie" else "Station"
orders = variables if groupby == "specie" else stations
# Plot part ==============================================================
figure = px.scatter(
dfdt, x=variables[0], y=variables[1],
dfdt, x=x, y=y, #variables[0], y=variables[1],
opacity=0.5,
color="Station",
color=color, #color="Station",
hover_data=["Date"],
category_orders={"Station": stations},
category_orders={color: orders}, #category_orders={"Station": stations},
trendline="ols",
marginal_x="histogram",
marginal_y="histogram",
# labels={"Station": ''}
)
figure["layout"]["xaxis"]["title"] = "{var} ({unit})".format(
var=variables[0],
xtitle = "{var} ({unit})".format(
var=x,
unit=' or '.join(
utilities.get_units(
[variables[0]],
[variables[0]] if variables[0] in sources else []
)
utilities.get_units( [s for s in variables], sources=len(sources)>0 )
)
)
figure["layout"]["yaxis"]["title"] = "{var} ({unit})".format(
var=variables[1],
ytitle = "{var} ({unit})".format(
var=y,
unit=' or '.join(
utilities.get_units(
[variables[1]],
[variables[1]] if variables[1] in sources else []
)
utilities.get_units( [s for s in variables], sources=len(sources)>0 )
)
)
figure["layout"]["xaxis"]["title"] = xtitle
figure["layout"]["yaxis"]["title"] = ytitle
return figure
......
......@@ -567,11 +567,13 @@ def update_box_graph(datatable, temporality, plots_options, groupby_var, species
@app.callback(Output('scatter-graph', 'figure'),
[Input('datatable', 'derived_virtual_data'),
Input('tab-2-col', 'value')],
Input('tab-2-col', 'value'),
Input('scatter_groupby', 'value'),
],
[State('specie-dropdown', 'value'),
State('source-dropdown', 'value'),
State('options_check', 'value')])
def update_scatter_graph(datatable, tabselected, species, sources, options):
def update_scatter_graph(datatable, tabselected, groupby, species, sources, options):
"""Update the scatter graph
:datatable: TODO
......@@ -605,8 +607,8 @@ def update_scatter_graph(datatable, tabselected, species, sources, options):
if tabselected != "2":
return figure
if datatable is None or len(datatable) == 0 or len(species+sources) != 2:
figure["layout"]["title"] = "Please select only 2 species/sources"
if datatable is None or len(datatable) == 0:
figure["layout"]["title"] = "Please select at least 2 species/sources or 2 stations"
return figure
dfdt = pd.DataFrame(datatable)
......@@ -615,43 +617,66 @@ def update_scatter_graph(datatable, tabselected, species, sources, options):
dfdt["Labels"] = np.nan
stations = list(dfdt["Station"].unique())
if groupby == "site":
if len(species+sources) != 2:
figure["layout"]["title"] = "Please select 2 species"
return figure
elif groupby == "specie":
if len(stations) != 2:
figure["layout"]["title"] = "Please select 2 stations"
return figure
# species = set(species) - set(BASE_VAR_SP) - set(notNumeric)
# sources = set(sources) - set(BASE_VAR_SRC)
variables = species + sources
dfdt = dfdt[variables+["Station", "Date"]].dropna()
if groupby == "specie":
df = dfdt[["Date", "Station"] + variables]
df = df.pivot_table(index=["Date"], columns=["Station"], values=variables)\
.reset_index()\
.melt(id_vars=["Date"])\
.dropna()
df.columns = ["Date", "Specie", "Station", "value"]
dfdt = df.pivot_table(index=["Date", "Specie"], columns=["Station"]).reset_index()
dfdt.columns = ["Date", "Specie"] + list(dfdt.columns.get_level_values("Station")[-2:])
else:
dfdt = dfdt[variables+["Station", "Date"]].dropna()
x, y = stations if groupby == "specie" else variables
color = "Specie" if groupby == "specie" else "Station"
orders = variables if groupby == "specie" else stations
# Plot part ==============================================================
figure = px.scatter(
dfdt, x=variables[0], y=variables[1],
dfdt, x=x, y=y, #variables[0], y=variables[1],
opacity=0.5,
color="Station",
color=color, #color="Station",
hover_data=["Date"],
category_orders={"Station": stations},
category_orders={color: orders}, #category_orders={"Station": stations},
trendline="ols",
marginal_x="histogram",
marginal_y="histogram",
# labels={"Station": ''}
)
figure["layout"]["xaxis"]["title"] = "{var} ({unit})".format(
var=variables[0],
xtitle = "{var} ({unit})".format(
var=x,
unit=' or '.join(
utilities.get_units(
[variables[0]],
[variables[0]] if variables[0] in sources else []
)
utilities.get_units( [s for s in variables], sources=len(sources)>0 )
)
)
figure["layout"]["yaxis"]["title"] = "{var} ({unit})".format(
var=variables[1],
ytitle = "{var} ({unit})".format(
var=y,
unit=' or '.join(
utilities.get_units(
[variables[1]],
[variables[1]] if variables[1] in sources else []
)
utilities.get_units( [s for s in variables], sources=len(sources)>0 )
)
)
figure["layout"]["xaxis"]["title"] = xtitle
figure["layout"]["yaxis"]["title"] = ytitle
return figure
......
......@@ -549,10 +549,26 @@ on the UGA gitlab for an example on how to use this app.
html.Div(id="select-2-specie"),
html.Div(
children=[
html.Div(
[
html.Label('Group by'),
dcc.RadioItems(
id='scatter_groupby',
className="options-items",
options=[
{'label': 'Site', 'value': 'site'},
{'label': 'Specie', 'value': 'specie'},
],
value="site",
)
]
),
dcc.Graph(
id="scatter-graph",
figure={'data': [], 'layout': {'title': 'Scatter plot'}}
)
]
)
]
)
......
......@@ -351,11 +351,11 @@ def datetime2fractionaldate(date):
result = year + (date- boy).total_seconds() / (eoy - boy).total_seconds()
return result
def get_units(species, sources):
def get_units(species, sources=False):
"""Get units if the given species
:species: list, list of species to get units
:sources: list, list of sources to get units
:sources: bool, either included the PMF sources unit (ie. µg/m3)
:returns: list, the units list
"""
conn = sqlite3.connect(settings.BDDPM) # BDDPM must be defined in local_settings.py
......@@ -366,7 +366,7 @@ def get_units(species, sources):
units = units.values
units = units.reshape(len(units,))
units = units.tolist()
if len(sources) > 0:
if sources:
units.append("µg/m3")
units = list(set(units))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment