Skip to content

Commit

Permalink
alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanbhosale committed Nov 18, 2024
1 parent 94c17be commit 1cda479
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 81 deletions.
120 changes: 95 additions & 25 deletions api/utils/post_survey_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,15 @@ def calculate_discrepancy_scores(df: pd.DataFrame, margin_of_error_height: float
color_continuous_scale='Viridis'
)
fig_height.update_layout(
yaxis={'categoryorder':'total ascending'}
yaxis={'categoryorder':'total ascending'},
coloraxis_colorbar=dict(
orientation='h',
yanchor='top',
y=-0.2,
xanchor='center',
x=0.5,
title='Average Height Discrepancy (cm)'
)
)
plots['height_discrepancy_plot'] = fig_height.to_json()

Expand All @@ -174,7 +182,15 @@ def calculate_discrepancy_scores(df: pd.DataFrame, margin_of_error_height: float
color_continuous_scale='Magma'
)
fig_weight.update_layout(
yaxis={'categoryorder':'total ascending'}
yaxis={'categoryorder':'total ascending'},
coloraxis_colorbar=dict(
orientation='h',
yanchor='top',
y=-0.2,
xanchor='center',
x=0.5,
title='Average Weight Discrepancy (kg)'
)
)
plots['weight_discrepancy_plot'] = fig_weight.to_json()

Expand All @@ -191,7 +207,15 @@ def calculate_discrepancy_scores(df: pd.DataFrame, margin_of_error_height: float
color_continuous_scale='RdBu'
)
fig_height_acc.update_layout(
yaxis={'categoryorder':'total ascending'}
yaxis={'categoryorder':'total ascending'},
coloraxis_colorbar=dict(
orientation='h',
yanchor='top',
y=-0.2,
xanchor='center',
x=0.5,
title='Height Measurement Accuracy (%)'
)
)
plots['height_accuracy_plot'] = fig_height_acc.to_json()

Expand All @@ -208,7 +232,15 @@ def calculate_discrepancy_scores(df: pd.DataFrame, margin_of_error_height: float
color_continuous_scale='Plasma'
)
fig_weight_acc.update_layout(
yaxis={'categoryorder':'total ascending'}
yaxis={'categoryorder':'total ascending'},
coloraxis_colorbar=dict(
orientation='h',
yanchor='top',
y=-0.2,
xanchor='center',
x=0.5,
title='Weight Measurement Accuracy (%)'
)
)
plots['weight_accuracy_plot'] = fig_weight_acc.to_json()

Expand Down Expand Up @@ -238,7 +270,17 @@ def calculate_discrepancy_scores(df: pd.DataFrame, margin_of_error_height: float
labels={'L0_name': 'L0 Name', 'Percentage': 'Percentage (%)'},
color_discrete_sequence=px.colors.qualitative.Set2
)
fig_class_wasting.update_layout(barmode='stack', yaxis={'categoryorder':'total ascending'})
fig_class_wasting.update_layout(
barmode='stack',
yaxis={'categoryorder':'total ascending'},
legend=dict(
orientation="h",
yanchor="top",
y=-0.25,
xanchor="center",
x=0.5
)
)
plots['classification_wasting_plot'] = fig_class_wasting.to_json()

# Plot 6: Classification Accuracy - Stunting vs L0 (Stacked Horizontal Bar)
Expand Down Expand Up @@ -267,27 +309,20 @@ def calculate_discrepancy_scores(df: pd.DataFrame, margin_of_error_height: float
labels={'L0_name': 'L0 Name', 'Percentage': 'Percentage (%)'},
color_discrete_sequence=px.colors.qualitative.Set3
)
fig_class_stunting.update_layout(barmode='stack', yaxis={'categoryorder':'total ascending'})
fig_class_stunting.update_layout(
barmode='stack',
yaxis={'categoryorder':'total ascending'},
legend=dict(
orientation="h",
yanchor="top",
y=-0.25,
xanchor="center",
x=0.5
)
)
plots['classification_stunting_plot'] = fig_class_stunting.to_json()

# Plot 7: Composite Discrepancy Score per L0 (Horizontal Bar)
discrepancy_df_sorted = discrepancy_df.sort_values('composite_discrepancy_score', ascending=False)
fig_composite = px.bar(
discrepancy_df_sorted,
x='composite_discrepancy_score',
y='L0_name',
orientation='h',
title='Composite Discrepancy Score per L0',
labels={'L0_name': 'L0 Name', 'composite_discrepancy_score': 'Composite Discrepancy Score'},
color='composite_discrepancy_score',
color_continuous_scale='Cividis'
)
fig_composite.update_layout(
yaxis={'categoryorder':'total ascending'}
)
plots['composite_discrepancy_plot'] = fig_composite.to_json()

# Plot 8: Classification Accuracy - Underweight vs L0 (Stacked Horizontal Bar)
# Plot 7: Classification Accuracy - Underweight vs L0 (Stacked Horizontal Bar)
classification_underweight_df = discrepancy_df[[
'L0_name',
'classification_accuracy_underweight_percent',
Expand All @@ -313,9 +348,44 @@ def calculate_discrepancy_scores(df: pd.DataFrame, margin_of_error_height: float
labels={'L0_name': 'L0 Name', 'Percentage': 'Percentage (%)'},
color_discrete_sequence=px.colors.qualitative.Set1
)
fig_class_underweight.update_layout(barmode='stack', yaxis={'categoryorder':'total ascending'})
fig_class_underweight.update_layout(
barmode='stack',
yaxis={'categoryorder':'total ascending'},
legend=dict(
orientation="h",
yanchor="top",
y=-0.25,
xanchor="center",
x=0.5
)
)
plots['classification_underweight_plot'] = fig_class_underweight.to_json()

# Plot 8: Composite Discrepancy Score per L0 (Horizontal Bar)
discrepancy_df_sorted = discrepancy_df.sort_values('composite_discrepancy_score', ascending=False)
fig_composite = px.bar(
discrepancy_df_sorted,
x='composite_discrepancy_score',
y='L0_name',
orientation='h',
title='Composite Discrepancy Score per L0',
labels={'L0_name': 'L0 Name', 'composite_discrepancy_score': 'Composite Discrepancy Score'},
color='composite_discrepancy_score',
color_continuous_scale='Cividis'
)
fig_composite.update_layout(
yaxis={'categoryorder':'total ascending'},
coloraxis_colorbar=dict(
orientation='h',
yanchor='top',
y=-0.2,
xanchor='center',
x=0.5,
title='Composite Discrepancy Score'
)
)
plots['composite_discrepancy_plot'] = fig_composite.to_json()

return {
'grouped_discrepancy_scores': results,
'plots': plots
Expand Down
125 changes: 69 additions & 56 deletions dashboard/src/utils/post_survey_analysis/functionality.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ def execute_post_survey_analysis(uploaded_file, df):
st.error(f"The following required columns are missing: {', '.join(missing_columns)}")
return

# Inputs for margins of error
margin_of_error_height = st.number_input(
"Margin of Error for Height (cm)",
min_value=0.0,
value=0.0,
step=0.1,
help="Acceptable margin of error for height measurements."
)
margin_of_error_weight = st.number_input(
"Margin of Error for Weight (kg)",
min_value=0.0,
value=0.0,
step=0.1,
help="Acceptable margin of error for weight measurements."
)
# Inputs for margins of error (Side by Side)
col1, col2 = st.columns(2)
with col1:
margin_of_error_height = st.number_input(
"Margin of Error for Height (cm)",
min_value=0.0,
value=0.0,
step=0.1,
help="Acceptable margin of error for height measurements."
)
with col2:
margin_of_error_weight = st.number_input(
"Margin of Error for Weight (kg)",
min_value=0.0,
value=0.0,
step=0.1,
help="Acceptable margin of error for weight measurements."
)

# Prepare data for API
uploaded_file.seek(0)
Expand Down Expand Up @@ -83,6 +86,7 @@ def execute_post_survey_analysis(uploaded_file, df):

# Plot the composite discrepancy scores
st.subheader("Composite Discrepancy Score Plot")

def display_plot(plot_json, caption):
try:
fig = pio.from_json(plot_json)
Expand All @@ -98,47 +102,56 @@ def display_plot(plot_json, caption):
# Display Plots
st.subheader("Discrepancy Plots")

# Plot 1: Height Discrepancy (cm) vs L0
if 'height_discrepancy_plot' in plots:
display_plot(plots['height_discrepancy_plot'], "Average Height Discrepancy (cm) per L0")
else:
st.warning("Height Discrepancy Plot not available.")

# Plot 2: Weight Discrepancy (kg) vs L0
if 'weight_discrepancy_plot' in plots:
display_plot(plots['weight_discrepancy_plot'], "Average Weight Discrepancy (kg) per L0")
else:
st.warning("Weight Discrepancy Plot not available.")

# Plot 3: Height Measurement Accuracy (%) vs L0
if 'height_accuracy_plot' in plots:
display_plot(plots['height_accuracy_plot'], "Height Measurement Accuracy (%) per L0")
else:
st.warning("Height Measurement Accuracy Plot not available.")

# Plot 4: Weight Measurement Accuracy (%) vs L0
if 'weight_accuracy_plot' in plots:
display_plot(plots['weight_accuracy_plot'], "Weight Measurement Accuracy (%) per L0")
else:
st.warning("Weight Measurement Accuracy Plot not available.")

# Plot 5: Classification Accuracy - Wasting vs L0
if 'classification_wasting_plot' in plots:
display_plot(plots['classification_wasting_plot'], "Classification Accuracy - Wasting vs L0")
else:
st.warning("Classification Accuracy - Wasting Plot not available.")

# Plot 6: Classification Accuracy - Stunting vs L0
if 'classification_stunting_plot' in plots:
display_plot(plots['classification_stunting_plot'], "Classification Accuracy - Stunting vs L0")
else:
st.warning("Classification Accuracy - Stunting Plot not available.")

# Plot 7: Classification Accuracy - Underweight vs L0
if 'classification_underweight_plot' in plots:
display_plot(plots['classification_underweight_plot'], "Classification Accuracy - Underweight vs L0")
else:
st.warning("Classification Accuracy - Underweight Plot not available.")
# Arrange the average height and weight discrepancy plots side by side
col3, col4 = st.columns(2)
with col3:
# Plot 1: Average Height Discrepancy (cm) vs L0
if 'height_discrepancy_plot' in plots:
display_plot(plots['height_discrepancy_plot'], "Average Height Discrepancy (cm) per L0")
else:
st.warning("Height Discrepancy Plot not available.")
with col4:
# Plot 2: Average Weight Discrepancy (kg) vs L0
if 'weight_discrepancy_plot' in plots:
display_plot(plots['weight_discrepancy_plot'], "Average Weight Discrepancy (kg) per L0")
else:
st.warning("Weight Discrepancy Plot not available.")

# Arrange the height and weight measurement accuracy plots side by side
col5, col6 = st.columns(2)
with col5:
# Plot 3: Height Measurement Accuracy (%) vs L0
if 'height_accuracy_plot' in plots:
display_plot(plots['height_accuracy_plot'], "Height Measurement Accuracy (%) per L0")
else:
st.warning("Height Measurement Accuracy Plot not available.")
with col6:
# Plot 4: Weight Measurement Accuracy (%) vs L0
if 'weight_accuracy_plot' in plots:
display_plot(plots['weight_accuracy_plot'], "Weight Measurement Accuracy (%) per L0")
else:
st.warning("Weight Measurement Accuracy Plot not available.")

# Arrange the three classification accuracy plots side by side
col7, col8, col9 = st.columns(3)
with col7:
# Plot 5: Classification Accuracy - Wasting vs L0
if 'classification_wasting_plot' in plots:
display_plot(plots['classification_wasting_plot'], "Classification Accuracy - Wasting vs L0")
else:
st.warning("Classification Accuracy - Wasting Plot not available.")
with col8:
# Plot 6: Classification Accuracy - Stunting vs L0
if 'classification_stunting_plot' in plots:
display_plot(plots['classification_stunting_plot'], "Classification Accuracy - Stunting vs L0")
else:
st.warning("Classification Accuracy - Stunting Plot not available.")
with col9:
# Plot 7: Classification Accuracy - Underweight vs L0
if 'classification_underweight_plot' in plots:
display_plot(plots['classification_underweight_plot'], "Classification Accuracy - Underweight vs L0")
else:
st.warning("Classification Accuracy - Underweight Plot not available.")

# Optionally, provide download link for discrepancy scores
st.subheader("Download Discrepancy Scores")
Expand Down

0 comments on commit 1cda479

Please sign in to comment.