You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

csv_parser.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. import os
  3. import csv
  4. script_path = '/root/health-calculator'
  5. weight_age = script_path + '/wtageinf.csv'
  6. height_age = script_path + '/lenageinf.csv'
  7. head_age = script_path + '/hcageinf.csv'
  8. def average(num1, num2):
  9. return float((float(num1) + float(num2)) / 2)
  10. def get_measurement(csvfile, gender, age):
  11. age = float(age)
  12. if gender == "boys":
  13. gender = '1'
  14. else:
  15. gender = '2'
  16. # lines[i][6] = band -3
  17. # lines[i][7] = band -2
  18. # lines[i][11] = band 2
  19. # lines[i][12] = band 3
  20. with open(csvfile, newline='') as f:
  21. lines = list(csv.reader(f, delimiter=',', quotechar='"'))
  22. i = 0
  23. while i < len(lines):
  24. # gender
  25. if lines[i][0] == gender:
  26. if float(lines[i][1]) - 0.5 == age:
  27. return (average(lines[i-1][6], lines[i+1][6]),
  28. average(lines[i-1][7], lines[i+1][7]),
  29. average(lines[i-1][11], lines[i+1][11]),
  30. average(lines[i-1][12], lines[i+1][12]))
  31. elif float(lines[i][1]) == age:
  32. return lines[i][6], lines[i][7], lines[i][11], lines[i][12]
  33. i += 1
  34. def is_height_age_normal(gender, height, age):
  35. m = get_measurement(height_age, gender, age)
  36. print(m)
  37. if height > float(m[3]):
  38. return 'Sangat tinggi'
  39. elif height > float(m[2]):
  40. return 'Tinggi'
  41. elif height < float(m[0]):
  42. return 'Pendek'
  43. elif height < float(m[1]):
  44. return 'Agak pendek'
  45. return 'Tinggi normal'
  46. def is_weight_age_normal(gender, weight, age):
  47. m = get_measurement(weight_age, gender, age)
  48. if weight > float(m[3]):
  49. return 'Gizi sangat berlebih'
  50. elif weight > float(m[2]):
  51. return 'Gizi berlebih'
  52. elif weight < float(m[0]):
  53. return 'Gizi buruk'
  54. elif weight < float(m[1]):
  55. return 'Gizi kurang'
  56. return 'Gizi normal'
  57. def is_head_age_normal(gender, head, age):
  58. if age > 36:
  59. return 'Pengukuran kepala hanya sampai 36 bulan'
  60. m = get_measurement(head_age, gender, age)
  61. if head > float(m[2]):
  62. return 'Makrosefali'
  63. elif head < float(m[1]):
  64. return 'Mikrosefali'
  65. return 'Ukuran kepala normal'
  66. if __name__ == "__main__":
  67. print(is_weight_age_normal('boys', 100, 48))
  68. print(is_height_age_normal('boys', 91, 48))