-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Xiaomi_Scale_Body_Metrics.py
225 lines (194 loc) · 8.16 KB
/
Xiaomi_Scale_Body_Metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Mi Body Composition Scale 2 Garmin Connect v7.4
from math import floor
import sys
from body_scales import bodyScales
class bodyMetrics:
def __init__(self, weight, height, age, sex, impedance):
self.weight = weight
self.height = height
self.age = age
self.sex = sex
self.impedance = impedance
self.scales = bodyScales(age, height, sex, weight)
# Check for potential out of boundaries
if self.height > 220:
print("Height is too high (limit: >220cm) or scale is sleeping")
sys.stderr.write('Height is over 220cm\n')
exit()
elif weight < 10 or weight > 200:
print("Weight is either too low or too high (limits: <10kg and >200kg)")
sys.stderr.write('Weight is below 10kg or above 200kg\n')
exit()
elif age > 99:
print("Age is too high (limit >99 years)")
sys.stderr.write('Age is above 99 years\n')
exit()
elif impedance > 3000:
print("Impedance is above 3000 Ohm")
sys.stderr.write('Impedance is above 3000 Ohm\n')
exit()
# Set the value to a boundary if it overflows
def checkValueOverflow(self, value, minimum, maximum):
if value < minimum:
return minimum
elif value > maximum:
return maximum
else:
return value
# Get LBM coefficient (with impedance)
def getLBMCoefficient(self):
lbm = (self.height * 9.058 / 100) * (self.height / 100)
lbm += self.weight * 0.32 + 12.226
lbm -= self.impedance * 0.0068
lbm -= self.age * 0.0542
return lbm
# Get BMR
def getBMR(self):
if self.sex == 'female':
bmr = 864.6 + self.weight * 10.2036
bmr -= self.height * 0.39336
bmr -= self.age * 6.204
else:
bmr = 877.8 + self.weight * 14.916
bmr -= self.height * 0.726
bmr -= self.age * 8.976
# Capping
if self.sex == 'female' and bmr > 2996:
bmr = 5000
elif self.sex == 'male' and bmr > 2322:
bmr = 5000
return self.checkValueOverflow(bmr, 500, 10000)
# Get fat percentage
def getFatPercentage(self):
# Set a constant to remove from LBM
if self.sex == 'female' and self.age <= 49:
const = 9.25
elif self.sex == 'female' and self.age > 49:
const = 7.25
else:
const = 0.8
# Calculate body fat percentage
LBM = self.getLBMCoefficient()
if self.sex == 'male' and self.weight < 61:
coefficient = 0.98
elif self.sex == 'female' and self.weight > 60:
coefficient = 0.96
if self.height > 160:
coefficient *= 1.03
elif self.sex == 'female' and self.weight < 50:
coefficient = 1.02
if self.height > 160:
coefficient *= 1.03
else:
coefficient = 1.0
fatPercentage = (1.0 - (((LBM - const) * coefficient) / self.weight)) * 100
# Capping body fat percentage
if fatPercentage > 63:
fatPercentage = 75
return self.checkValueOverflow(fatPercentage, 5, 75)
# Get water percentage
def getWaterPercentage(self):
waterPercentage = (100 - self.getFatPercentage()) * 0.7
if (waterPercentage <= 50):
coefficient = 1.02
else:
coefficient = 0.98
# Capping water percentage
if waterPercentage * coefficient >= 65:
waterPercentage = 75
return self.checkValueOverflow(waterPercentage * coefficient, 35, 75)
# Get bone mass
def getBoneMass(self):
if self.sex == 'female':
base = 0.245691014
else:
base = 0.18016894
boneMass = (base - (self.getLBMCoefficient() * 0.05158)) * -1
if boneMass > 2.2:
boneMass += 0.1
else:
boneMass -= 0.1
# Capping boneMass
if self.sex == 'female' and boneMass > 5.1:
boneMass = 8
elif self.sex == 'male' and boneMass > 5.2:
boneMass = 8
return self.checkValueOverflow(boneMass, 0.5 , 8)
# Get muscle mass
def getMuscleMass(self):
muscleMass = self.weight - ((self.getFatPercentage() * 0.01) * self.weight) - self.getBoneMass()
# Capping muscle mass
if self.sex == 'female' and muscleMass >= 84:
muscleMass = 120
elif self.sex == 'male' and muscleMass >= 93.5:
muscleMass = 120
return self.checkValueOverflow(muscleMass, 10 ,120)
# Get Visceral Fat
def getVisceralFat(self):
if self.sex == 'female':
if self.weight > (13 - (self.height * 0.5)) * -1:
subsubcalc = ((self.height * 1.45) + (self.height * 0.1158) * self.height) - 120
subcalc = self.weight * 500 / subsubcalc
vfal = (subcalc - 6) + (self.age * 0.07)
else:
subcalc = 0.691 + (self.height * -0.0024) + (self.height * -0.0024)
vfal = (((self.height * 0.027) - (subcalc * self.weight)) * -1) + (self.age * 0.07) - self.age
else:
if self.height < self.weight * 1.6:
subcalc = ((self.height * 0.4) - (self.height * (self.height * 0.0826))) * -1
vfal = ((self.weight * 305) / (subcalc + 48)) - 2.9 + (self.age * 0.15)
else:
subcalc = 0.765 + self.height * -0.0015
vfal = (((self.height * 0.143) - (self.weight * subcalc)) * -1) + (self.age * 0.15) - 5.0
return self.checkValueOverflow(vfal, 1 ,50)
# Get BMI
def getBMI(self):
return self.checkValueOverflow(self.weight/((self.height/100)*(self.height/100)), 10, 90)
# Get ideal weight (just doing a reverse BMI, should be something better)
def getIdealWeight(self, orig=True):
# Uses mi fit algorithm (or holtek's one)
if orig and self.sex == 'female':
return (self.height - 70) * 0.6
elif orig and self.sex == 'male':
return (self.height - 80) * 0.7
else:
return self.checkValueOverflow((22*self.height)*self.height/10000, 5.5, 198)
# Get fat mass to ideal (guessing mi fit formula)
def getFatMassToIdeal(self):
mass = (self.weight * (self.getFatPercentage() / 100)) - (self.weight * (self.scales.getFatPercentageScale()[2] / 100))
if mass < 0:
return f"to_gain:{mass*-1:.1f}"
else:
return f"to_lose:{mass:.1f}"
# Get protetin percentage (warn: guessed formula)
def getProteinPercentage(self, orig=True):
# Use original algorithm from mi fit (or legacy guess one)
if orig:
proteinPercentage = (self.getMuscleMass() / self.weight) * 100
proteinPercentage -= self.getWaterPercentage()
else:
proteinPercentage = 100 - (floor(self.getFatPercentage() * 100) / 100)
proteinPercentage -= floor(self.getWaterPercentage() * 100) / 100
proteinPercentage -= floor((self.getBoneMass()/self.weight*100) * 100) / 100
return self.checkValueOverflow(proteinPercentage, 5, 32)
# Get body type (out of nine possible)
def getBodyType(self):
if self.getFatPercentage() > self.scales.getFatPercentageScale()[2]:
factor = 0
elif self.getFatPercentage() < self.scales.getFatPercentageScale()[1]:
factor = 2
else:
factor = 1
if self.getMuscleMass() > self.scales.getMuscleMassScale()[1]:
return 3 + (factor * 3)
elif self.getMuscleMass() < self.scales.getMuscleMassScale()[0]:
return 1 + (factor * 3)
else:
return 2 + (factor * 3)
# Get Metabolic Age
def getMetabolicAge(self):
if self.sex == 'female':
metabolicAge = (self.height * -1.1165) + (self.weight * 1.5784) + (self.age * 0.4615) + (self.impedance * 0.0415) + 83.2548
else:
metabolicAge = (self.height * -0.7471) + (self.weight * 0.9161) + (self.age * 0.4184) + (self.impedance * 0.0517) + 54.2267
return self.checkValueOverflow(metabolicAge, 15, 80)