def get_avg_hr(patient_id): """Return average heart rate based on all stored heart rate of the specified patient Parameters ---------- patient_id: str or int user input indicating the patient to be inspected Returns ------- out: json obj Contains calculated average heart rate. May instead contain error message if user input is invalid. """ from request_validation import validid from data_process import avg_hr try: validid(patient_id, database) patient_id = str(patient_id) result = avg_hr(database[patient_id]["HR_list"]) out = jsonify({ "avg_HR": result, }) return out except IndexError: out = jsonify({"Error": "Patient does not exist"}) return out except ValueError: out = jsonify({"Error": "Patient does not have any record"}) return out except NameError: out = jsonify({"Error": "Invalid ID input"}) return out
def get_hr(patient_id): """Return all stored heart rate of specified patient Parameters ---------- patient_id: str or int user input indicating the patient to be inspected Returns ------- out: json obj Contains list of stored heart rate of patient in integer. May instead contain error message if user input is invalid. """ from request_validation import validid try: validid(patient_id, database) patient_id = str(patient_id) out = jsonify({ "HR_list": database[str(patient_id)]["HR_list"], }) return out except IndexError: out = jsonify({"Error": "Patient does not exist"}) return out except ValueError: out = jsonify({"Error": "Patient does not have any record"}) return out except NameError: out = jsonify({"Error": "Invalid ID input"}) return out
def avg_interval(): """Returns average heart rate based on user specified interval Interval goes from specified time point to the latest time point Returns ------- out: json obj Contains calculated average heart rate. May instead contain error message if user input is invalid. """ from request_validation import validtime from request_validation import validid from data_process import get_intv_avg info = request.get_json() try: validid(info["patient_id"], database) key = str(info["patient_id"]) try: validtime(info, database) avg = get_intv_avg(database[key], info["heart_rate_average_since"]) out = jsonify({ "avg_HR": avg, "since": info["heart_rate_average_since"] }) return out except IndexError: out = jsonify({ "Error": "Specified time is larger " "than all timestamps of this patient", }) return out except NameError: out = jsonify({"NameError": "Invalid time entry"}) return out except ValueError: out = jsonify({"ValueError": "Patient does not have any record"}) return out except IndexError: out = jsonify({"IndexError": "Patient does not exist"}) return out except ValueError: out = jsonify({"ValueError": "Patient does not have any record"}) return out except NameError: out = jsonify({"Error": "Invalid ID input"}) return out
def test_validid(data): from request_validation import validid result = validid(1, data) assert result == 1 with pytest.raises(NameError): validid('s', data) with pytest.raises(IndexError): validid(2, data) with pytest.raises(ValueError): validid(5, data)
def get_stat(patient_id): """Return whether patient's heart rate shows tachycardia Parameters ---------- patient_id: str or int user input indicating the patient to be inspected Returns ------- out: json obj Contains status of patient. May instead contain error message if user input is invalid. """ from request_validation import validid from data_process import tachycardic try: validid(patient_id, database) patient_id = str(patient_id) state = tachycardic(database[patient_id]["HR_list"][-1], database[patient_id]["user_age"]) out = jsonify({ "state": state, "Timestamp": database[patient_id]["Timestamp"][-1], }) return out except IndexError: out = jsonify({"Error": "Patient does not exist"}) return out except ValueError: out = jsonify({"Error": "Patient does not have any record"}) return out except NameError: out = jsonify({"Error": "Invalid ID input"}) return out