Spaces:
Sleeping
Sleeping
Fixed hovertext
Browse files
FFMI.py
CHANGED
@@ -25,13 +25,19 @@ def calculate_ffmi(fat_free_mass_kg, height_m):
|
|
25 |
return ffmi
|
26 |
|
27 |
def plot_ffmi_curves(height_m, gender):
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
# Define body fat percentages (Y-axis)
|
32 |
-
body_fat_percentage_range = np.linspace(1, 50, 100, endpoint=True) # body fat percentage
|
33 |
if gender == "Male":
|
34 |
-
# Define FFMI ranges and their descriptions for mans
|
35 |
ffmi_ranges = [
|
36 |
{"min": 0, "max": 18, "description": "Below average", "color": "blue"},
|
37 |
{"min": 18, "max": 20, "description": "Average", "color": "green"},
|
@@ -42,7 +48,6 @@ def plot_ffmi_curves(height_m, gender):
|
|
42 |
{"min": 28, "max": float("inf"), "description": "FAKEE", "color": "black"}
|
43 |
]
|
44 |
else:
|
45 |
-
# Define FFMI ranges and their descriptions for womens
|
46 |
ffmi_ranges = [
|
47 |
{"min": 0, "max": 15, "description": "Below average", "color": "blue"},
|
48 |
{"min": 15, "max": 17, "description": "Average", "color": "green"},
|
@@ -53,27 +58,23 @@ def plot_ffmi_curves(height_m, gender):
|
|
53 |
{"min": 25, "max": float("inf"), "description": "FAKEE", "color": "black"}
|
54 |
]
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
# Create plot
|
59 |
fig = go.Figure()
|
60 |
|
61 |
-
# Calculate and plot curves for each FFMI range
|
62 |
for ffmi_range in ffmi_ranges:
|
63 |
weight_kg_values = []
|
64 |
body_fat_values = []
|
|
|
65 |
|
66 |
for weight in weight_kg_range:
|
67 |
for body_fat_percentage in body_fat_percentage_range:
|
68 |
fat_free_mass = calculate_fat_free_mass(weight, body_fat_percentage)
|
69 |
ffmi = calculate_ffmi(fat_free_mass, height_m)
|
70 |
|
71 |
-
# Check if the calculated FFMI falls within the current FFMI range
|
72 |
if ffmi_range["min"] <= ffmi < ffmi_range["max"]:
|
73 |
weight_kg_values.append(weight)
|
74 |
body_fat_values.append(body_fat_percentage)
|
|
|
75 |
|
76 |
-
# Add curve for the current FFMI range
|
77 |
fig.add_trace(go.Scatter(
|
78 |
x=weight_kg_values,
|
79 |
y=body_fat_values,
|
@@ -82,10 +83,14 @@ def plot_ffmi_curves(height_m, gender):
|
|
82 |
marker=dict(
|
83 |
color=ffmi_range.get("marker_color", ffmi_range["color"]),
|
84 |
size=5, opacity=0.6,
|
85 |
-
line=dict(color=ffmi_range.get("line_color", ffmi_range["color"]))
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
-
# Update layout
|
89 |
fig.update_layout(
|
90 |
title='FFMI Curves for Different Body Fat Percentages and Weight',
|
91 |
xaxis_title='Weight (kg)',
|
@@ -95,8 +100,4 @@ def plot_ffmi_curves(height_m, gender):
|
|
95 |
template='plotly_dark'
|
96 |
)
|
97 |
|
98 |
-
# Show the interactive plot
|
99 |
return fig
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
25 |
return ffmi
|
26 |
|
27 |
def plot_ffmi_curves(height_m, gender):
|
28 |
+
"""
|
29 |
+
Generate a plot of FFMI (Fat-Free Mass Index) curves based on weight
|
30 |
+
and body fat percentage for a specified gender.
|
31 |
+
|
32 |
+
:param height_m: Height of the individual in meters [m].
|
33 |
+
:param gender: Gender of the individual ('Male' or 'Female'), which determines
|
34 |
+
the FFMI ranges used for categorization.
|
35 |
+
:return: A Plotly Figure object containing the scatter plot of FFMI curves.
|
36 |
+
"""
|
37 |
+
weight_kg_range = np.linspace(45, 160, 100, endpoint=True)
|
38 |
+
body_fat_percentage_range = np.linspace(1, 50, 100, endpoint=True)
|
39 |
|
|
|
|
|
40 |
if gender == "Male":
|
|
|
41 |
ffmi_ranges = [
|
42 |
{"min": 0, "max": 18, "description": "Below average", "color": "blue"},
|
43 |
{"min": 18, "max": 20, "description": "Average", "color": "green"},
|
|
|
48 |
{"min": 28, "max": float("inf"), "description": "FAKEE", "color": "black"}
|
49 |
]
|
50 |
else:
|
|
|
51 |
ffmi_ranges = [
|
52 |
{"min": 0, "max": 15, "description": "Below average", "color": "blue"},
|
53 |
{"min": 15, "max": 17, "description": "Average", "color": "green"},
|
|
|
58 |
{"min": 25, "max": float("inf"), "description": "FAKEE", "color": "black"}
|
59 |
]
|
60 |
|
|
|
|
|
|
|
61 |
fig = go.Figure()
|
62 |
|
|
|
63 |
for ffmi_range in ffmi_ranges:
|
64 |
weight_kg_values = []
|
65 |
body_fat_values = []
|
66 |
+
ffmi_values = []
|
67 |
|
68 |
for weight in weight_kg_range:
|
69 |
for body_fat_percentage in body_fat_percentage_range:
|
70 |
fat_free_mass = calculate_fat_free_mass(weight, body_fat_percentage)
|
71 |
ffmi = calculate_ffmi(fat_free_mass, height_m)
|
72 |
|
|
|
73 |
if ffmi_range["min"] <= ffmi < ffmi_range["max"]:
|
74 |
weight_kg_values.append(weight)
|
75 |
body_fat_values.append(body_fat_percentage)
|
76 |
+
ffmi_values.append(ffmi)
|
77 |
|
|
|
78 |
fig.add_trace(go.Scatter(
|
79 |
x=weight_kg_values,
|
80 |
y=body_fat_values,
|
|
|
83 |
marker=dict(
|
84 |
color=ffmi_range.get("marker_color", ffmi_range["color"]),
|
85 |
size=5, opacity=0.6,
|
86 |
+
line=dict(color=ffmi_range.get("line_color", ffmi_range["color"]))
|
87 |
+
),
|
88 |
+
hovertext=[f'Weight: {weight_kg:.2f}<br>BodyFat: {body_fat:.2f}<br>FFMI: {ffmi:.2f}' for ffmi,weight_kg, body_fat in
|
89 |
+
|
90 |
+
zip(ffmi_values, weight_kg_values, body_fat_values)],
|
91 |
+
hoverinfo='text'
|
92 |
+
))
|
93 |
|
|
|
94 |
fig.update_layout(
|
95 |
title='FFMI Curves for Different Body Fat Percentages and Weight',
|
96 |
xaxis_title='Weight (kg)',
|
|
|
100 |
template='plotly_dark'
|
101 |
)
|
102 |
|
|
|
103 |
return fig
|
|
|
|
|
|
app.py
CHANGED
@@ -12,7 +12,6 @@ def main():
|
|
12 |
height_m = st.number_input("Enter your height in meters:", value=1.75, min_value=1.10, max_value=2.3, step=0.01)
|
13 |
with col2:
|
14 |
gender = st.selectbox("Select your gender", ["Male", "Female"])
|
15 |
-
|
16 |
# Plot FFMI curves
|
17 |
if st.button("Visualize FFMI Curves"):
|
18 |
try:
|
|
|
12 |
height_m = st.number_input("Enter your height in meters:", value=1.75, min_value=1.10, max_value=2.3, step=0.01)
|
13 |
with col2:
|
14 |
gender = st.selectbox("Select your gender", ["Male", "Female"])
|
|
|
15 |
# Plot FFMI curves
|
16 |
if st.button("Visualize FFMI Curves"):
|
17 |
try:
|