Spaces:
Sleeping
Sleeping
Download figure.py from osel-dbcms/Justify: direct link, hf CLI and curl.
- Browser
- Download file 7.08 kB
-
https://huggingface.co/spaces/osel-dbcms/Justify/resolve/main/figure.py
- Command line
-
hf download hf://spaces/osel-dbcms/Justify/figure.py
-
curl -L -o figure.py https://huggingface.co/spaces/osel-dbcms/Justify/resolve/main/figure.py
7.08 kB
| import plotly.graph_objects as go | |
| import pandas as pd | |
| import numpy as np | |
| def make_log_ticks(y_min, y_max): | |
| min_exp = int(np.floor(np.log10(y_min))) | |
| max_exp = int(np.ceil(np.log10(y_max))) | |
| n_decades = max_exp - min_exp | |
| # Use more ticks for narrow ranges, | |
| # fewer ticks when many orders of magnitude are shown | |
| if n_decades <= 2: | |
| multipliers = [1, 2, 5] | |
| else: | |
| multipliers = [1] | |
| ticks = [] | |
| for exp in range(min_exp, max_exp + 1): | |
| decade = 10.0 ** exp | |
| for mult in multipliers: | |
| value = mult * decade | |
| if y_min <= value <= y_max: | |
| ticks.append(value) | |
| # Make sure ratio = 1 is included when visible | |
| if y_min <= 1.0 <= y_max and 1.0 not in ticks: | |
| ticks.append(1.0) | |
| ticks.sort() | |
| return ticks | |
| def build_figure(dataframe: pd.DataFrame, mc_option="absolute") -> go.Figure: | |
| fig = go.Figure() | |
| if mc_option == "absolute": | |
| # Condition 1 | |
| fig.add_bar( | |
| y=dataframe["Condition 1"], | |
| x=dataframe["CASRN"], | |
| name="Condition 1", | |
| orientation="v", | |
| error_y=dict( | |
| type="data", | |
| symmetric=False, | |
| array=dataframe["Cond1_err_plus"], | |
| arrayminus=dataframe["Cond1_err_minus"], | |
| visible=True, | |
| ), | |
| marker=dict( | |
| color="lightcoral", | |
| line=dict( | |
| color="red", | |
| width=1 | |
| ) | |
| ) | |
| ) | |
| # Condition 2 | |
| fig.add_bar( | |
| y=dataframe["Condition 2"], | |
| x=dataframe["CASRN"], | |
| name="Condition 2", | |
| orientation="v", | |
| error_y=dict( | |
| type="data", | |
| symmetric=False, | |
| array=dataframe["Cond2_err_plus"], | |
| arrayminus=dataframe["Cond2_err_minus"], | |
| visible=True, | |
| ), | |
| marker=dict( | |
| color="lightskyblue", | |
| line=dict( | |
| color="blue", | |
| width=1 | |
| ) | |
| ), | |
| ) | |
| # Upper extent including error bars | |
| y1_upper = ( | |
| dataframe["Condition 1"].to_numpy(dtype=float) | |
| + dataframe["Cond1_err_plus"].to_numpy(dtype=float) | |
| ) | |
| y2_upper = ( | |
| dataframe["Condition 2"].to_numpy(dtype=float) | |
| + dataframe["Cond2_err_plus"].to_numpy(dtype=float) | |
| ) | |
| # Determine y-axis lower limit from smallest displayed median | |
| y1 = dataframe["Condition 1"].to_numpy(dtype=float) | |
| y2 = dataframe["Condition 2"].to_numpy(dtype=float) | |
| all_values = np.concatenate([y1, y2]) | |
| positive_values = all_values[ | |
| np.isfinite(all_values) & | |
| (all_values > 0) | |
| ] | |
| if len(positive_values) > 0: | |
| y_min = np.nanmin(positive_values) / 2.0 | |
| else: | |
| y_min = 1e-6 | |
| ticks = make_log_ticks(y_min, 1.0) | |
| fig.update_layout( | |
| barmode="group", | |
| legend=dict( | |
| orientation="h", | |
| x=0.5, | |
| xanchor="center", | |
| y=1.08, | |
| font=dict(size=18) | |
| ), | |
| yaxis=dict( | |
| title=dict( | |
| text="Fraction released", | |
| font=dict(size=18) | |
| ), | |
| type="log", | |
| range=[ | |
| np.log10(y_min), | |
| np.log10(1.0) | |
| ], | |
| tickmode="array", | |
| tickvals=ticks, | |
| ticktext=[ | |
| f"{x:g}" | |
| for x in ticks | |
| ], | |
| gridcolor="lightgray", | |
| gridwidth=1, | |
| ), | |
| ) | |
| elif mc_option == "ratio": | |
| fig.add_bar( | |
| y=dataframe["Ratio"], | |
| x=dataframe["CASRN"], | |
| name="Condition 1 / Condition 2", | |
| orientation="v", | |
| error_y=dict( | |
| type="data", | |
| symmetric=False, | |
| array=dataframe["Ratio_err_plus"], | |
| arrayminus=dataframe["Ratio_err_minus"], | |
| visible=True, | |
| ), | |
| marker=dict( | |
| color="lightskyblue", | |
| line=dict( | |
| color="blue", | |
| width=1 | |
| ) | |
| ), | |
| ) | |
| # Reference line: ratio = 1 | |
| fig.add_hline( | |
| y=1.0, | |
| line_width=2, | |
| line_dash="dash", | |
| line_color="black", | |
| ) | |
| # Include the complete IQR when determining axis limits | |
| ratio_lower = ( | |
| dataframe["Ratio"].to_numpy(dtype=float) | |
| - dataframe["Ratio_err_minus"].to_numpy(dtype=float) | |
| ) | |
| ratio_upper = ( | |
| dataframe["Ratio"].to_numpy(dtype=float) | |
| + dataframe["Ratio_err_plus"].to_numpy(dtype=float) | |
| ) | |
| positive_lower = ratio_lower[ | |
| np.isfinite(ratio_lower) | |
| & (ratio_lower > 0) | |
| ] | |
| positive_upper = ratio_upper[ | |
| np.isfinite(ratio_upper) | |
| & (ratio_upper > 0) | |
| ] | |
| if len(positive_lower) > 0: | |
| y_min = np.nanmin(positive_lower) / 2.0 | |
| else: | |
| y_min = 0.1 | |
| if len(positive_upper) > 0: | |
| y_max = np.nanmax(positive_upper) * 2.0 | |
| else: | |
| y_max = 10.0 | |
| # Always leave some room around ratio = 1 | |
| y_min = min(y_min, 0.5) | |
| y_max = max(y_max, 2.0) | |
| # Generate appropriate tick locations | |
| ticks = make_log_ticks( | |
| y_min, | |
| y_max | |
| ) | |
| fig.update_layout( | |
| barmode="group", | |
| showlegend=False, | |
| yaxis=dict( | |
| title=dict( | |
| text="Fraction released ratio (C1 / C2)", | |
| font=dict(size=18) | |
| ), | |
| type="log", | |
| range=[ | |
| np.log10(y_min), | |
| np.log10(y_max) | |
| ], | |
| tickmode="array", | |
| tickvals=ticks, | |
| ticktext=[ | |
| f"{x:g}" | |
| for x in ticks | |
| ], | |
| gridcolor="lightgray", | |
| gridwidth=1, | |
| ), | |
| ) | |
| # ---------------------------- | |
| # Common layout | |
| # ---------------------------- | |
| fig.update_layout( | |
| xaxis=dict( | |
| title=dict( | |
| text="Representative boundary chemicals (CAS)", | |
| standoff=0, | |
| font=dict(size=18) | |
| ) | |
| ), | |
| margin=dict( | |
| l=50, | |
| r=50, | |
| t=20, | |
| b=20 | |
| ), | |
| autosize=True, | |
| height=600, | |
| ) | |
| fig.update_xaxes( | |
| tickfont=dict(size=14) | |
| ) | |
| fig.update_yaxes( | |
| tickfont=dict(size=14) | |
| ) | |
| return fig |