Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assume you are taking over a project for your colleague Bob. Bob was injured taking a selfie and will be on medical leave for a

Assume you are taking over a project for your colleague "Bob." Bob was injured taking a selfie and will be on medical leave for a few weeks. You have been assigned to take over Bob's "BMI4kids" project, which is to build an Object Oriented Python package for use by health informatics team. You have to figure out

a) How does the existing code work?b) How can the existing code be extended to add new functionality?

Requirements

You must complete the following:

  1. Get the existing code working before you attempt to modify it. Make a backup copy. (Bob did this for you.)

  2. Add new code to BMI class to calculate age and sex based BMI percentiles. Follow the directions in the notes.txt file located in the Project_7 folder. These notes will guide you along the development path letting you refactor the code to both preserve the existing behavior while adding new behavior.

  3. Add comments explaining what's going on in the code and why. Add a clarifying comment to something you find confusing. Use the Python document style comments, # line comments, etc. Don't go nuts with the comments either. Too many just adds noise. Explain whats going on without repeating what can be learned by reading the code. Explain how to use the code, any known limitations, etc.

  4. Add 4 new positive test cases the test.py file. Positive means these tests should work, the code should return a valid BMI value back to you. There are 2 tests in the test.py file already, plus two additional test cases listed below to get you started. Add 4 more of your own to choosing that will exercise the BMI input checking mechanism.

standard(72, 180, months=228, sex='F') # should return percentile: P85

standard(72, 180, months=228, sex='M') # should percentile: P75

  1. Add 4 new negative test cases to the test.py file. Negative means the BMI class will detect an error and raise an Exception. Add assert statements in the BMI class __init__() method to check for invalid input data. When encountered, the BMI class raises an Exception stating what is wrong. The test.py detects and reacts to the error. Use a try:/except: structure to trap and print the error. Check for weights, heights, or months that are less than zero.

Hints

Use Anaconda Spyder as your Python IDE to make your life easier. Assume also that Bob's manager has left you a zip file containing the following items:

  • Some Python code that "works"

  • Two cross reference data files pulled off of the CDC website

  • A notes.txt file containing a log

notes.txt :

GOAL: ==================== Create a utility package containing common calculations for use by the Health Informatics department. First pass will include BMI calculations for both adults and for children aged 2-20 years old. Plan: ==================== There is a BMI module from somewhere that has a good start at a utility module. It needs work to include the BMI4kids update. 1) Refactor existing code: - create a "BMI4Kids/" directory - create a "BMI4Kids/Utils/" sub-directory - copy the BMI_utils.py file from zip file into "BMI4Kids/Utils/" - copy test code (see below) into "BMI4Kids/test.py" file - set Spyder working dorectory to "BMI4Kids/" in upper right of Spyder window - run test.py to make sure the BMI functions work as-is 2) Extend existing code to include BMI percentile property: - copy xref files from zip file into "BMI4Kids/Utils/" - create an "BMI4Kids/Utils/__init__.py" file, and add code below to initialize the utilities packages. this code will preload the data files containing the age-sex-percentile cross references. this code needs to execute only once at module load time so the BMI class has everything it needs when a new BMI instance gets created. - change "BMI4Kids/Utils/BMI_utils.py". see notes in the listing below labeled with "##" comments. - change "BMI4Kids/test.py" dump_bmi() function to print out sex, months, and percentile. - add new tests to "BMI4Kids/test.py" and run to make sure new code works. use a try/except structure to trap any failed assert statements. try: # statement except Exception as e: print( ">>> Error: " + str(e) + ", ignoring it and continuing ...") Source Code: ==================== BMI4Kids/test.py (complete) ---------------- #!/usr/bin/env python3 import Utils import Utils.BMI_utils as b def dump_bmi(q): print("height: %.2fm (%.2fin)"%(q.meters, q.inches)) print("weight: %.2fkg (%.2flbs)"%(q.kilos, q.pounds)) print("bmi: ", q.bmi) print("category: ", q.category) print(" ") print("top of test.py ") print("patient #1, height & weight in standard") q = b.standard(74, 195) dump_bmi(q) print("patient #2, height & weight in metric") q = b.metric(1.88, 88) dump_bmi(q) ### end test.py ### BMI4Kids/Utils/__init__.py (complete) -------------------------- import os import pandas as pd from . import BMI_utils as b def _load_file(filename): filename = os.path.dirname(__file__) + os.sep + filename print("loading file=[" + filename + "]") df = pd.read_csv(filename) df.set_index("Months", inplace=True) print("done") return(df) b.BMI._f_df = _load_file("xref_female.csv") b.BMI._m_df = _load_file("xref_male.csv") ### end __init__.py ### BMI4Kids/Utils/BMI_utils.py (This is not complete! See lines that start with ## for info on how to make it complete.) --------------------------- import pandas as pd class BMI: # Class variables _f_df = None # female bmi percentile data frame _m_df = None # male bmi percentile data frame # Methods def __init__(self, meters, kilos, months, sex): # Check input values to make sure they are reasonable assert sex is None or sex == "F" or sex == "M", "unknown value=["+sex+"] for sex" ## ## 1) add more tests here to check for weight/height/months <= 0 >>> ## # Save arguments as instance variables. These are visible only to # this particular inistance. self._meters = meters self._kilos = kilos self._months = months self._sex = sex ### Properties @property def bmi(self): return round(self._kilos / self._meters ** 2, 4) @property def category(self): ## ## 2) need to add an if conditional here similar to the if statement in the ## percentiles method. category is only valid for BMI values that have ## no month value (None) or and age value > 20 years old. ## b = self.bmi if b < 18.5: return "Underweight" elif b < 25.0: return "Healthy Weight" elif b < 30.0: return "Overweight" else: return "Obese" @property def percentile(self): # if no age or sex is given, use category if self._months is None or self._sex is None: return None # choose dataframe based on instance's sex. df is indexed on "Months" # df[0] corresponds to 24 months old. percentiles is a series copied # from df at given months. index = self._months - 24 if self._sex == 'F': percentiles = BMI._f_df.iloc[index] else: percentiles = BMI._m_df.iloc[index] # search for BMI in series percentiles that is bigger than BMI for # this instance. b = self.bmi result = "" for p in range(len(percentiles)): if b <= percentiles[p]: result = percentiles.index[p] break # if this instance's BMI is larger than anything in the row, return # ">" with biggest available category if result == "": result = ">" + percentiles.index[len(percentiles)-1] return result ## ## 3) need to add new properties that return sex and months ## @property def meters(self): return self._meters @property def kilos(self): return self._kilos @property def inches(self): return self._meters / 0.0254 @property def pounds(self): return self._kilos * 2.20462 ## 4) both constructors need to have optional parameters for months and sex. ## they both need default values set to None (ex: if no value is passed in ## for months, the parameter is assigned a value of None.) Both month and ## sex must passed to the BMI() class. The metric() function is completed, ## but standard() needs updating. ## def metric(meters, kilos, months=None, sex=None): b = BMI(meters, kilos, months, sex) return b def standard(inches, pounds): meters = inches * 0.0254 kilos = pounds / 2.20462 return BMI(meters, kilos) ### end BMI_utils.py ### ### end notes.txt ###

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions