def getaverageage(): fhir = FHIR() patients = fhir.get_all_patients() ages = [] for patient in patients: ages.append(patient.age()) return str(sum(ages) / len(ages))
def surname(text): fhir = FHIR() patients = fhir.get_all_patients() number_of_patients = 0 for i in patients: number_of_patients = number_of_patients + 1 for index in range(0, number_of_patients): patient = patients[index] if text == patient.name.family: return index return -1
def unique_identifier(text): fhir = FHIR() patients = fhir.get_all_patients() number_of_patients = 0 for i in patients: number_of_patients = number_of_patients + 1 for index in range(0, number_of_patients): patient = patients[index] if text == patient.uuid: return index return -1
def graph_by_states(): fhir = FHIR() patients = fhir.get_all_patients() states = {} for patient in patients: for addressArr in patient.addresses: states.update({addressArr.state: states.get(addressArr.state, 0) + 1}) plt.bar(range(len(states)), list(states.values()), align='center') plt.xticks(range(len(states)), list(states.keys()), rotation='vertical') plt.show()
def __init__(self, page=None, patient_uuid=None): fhir = FHIR() self.uuid_presented = False self.patients = fhir.get_all_patients() if patient_uuid is not None: self.uuid_presented = True self.specific_patient = fhir.get_patient(patient_uuid) if page is not None: try: assert patient_uuid is None self.patients = fhir.get_patient_page(page) except AssertionError: sys.exit( "Error: patients page and patient uuid cannot be initialised at the same time" )
def graph_languages_spoken(): fhir = FHIR() patients = fhir.get_all_patients() languages = {} for patient in patients: for language in patient.communications.languages: languages.update({language: languages.get(language, 0) + 1}) plt.bar(range(len(languages)), list(languages.values()), align='center') plt.xticks(range(len(languages)), list(languages.keys()), rotation='vertical') fig1 = plt.gcf() plt.draw() number = random.randint(1, 1000) fig1.savefig('plot'+str(number)+'.png', dpi=100) return number
def graph_by_cities(): fhir = FHIR() patients = fhir.get_all_patients() cities = {} for patient in patients: for addressArr in patient.addresses: cities.update({addressArr.city: cities.get(addressArr.city, 0) + 1}) plt.bar(range(len(cities)), list(cities.values()), align='center') plt.xticks(range(len(cities)), list(cities.keys()), rotation='vertical') # fig = plt.figure() # fig.savefig('plot.png') plt.show() # need to think of what can we graph from the observation data # def graph_by_diseases(): # fhir = FHIR() # patients = fhir.get_all_patients() # problems = {} # count = 0 # for patient in patients: # count = count + 1 # if count == 4: # break # print("YOOOO THIS IS THR FKING ID" + patient.uuid) # try: # observations = fhir.get_patient_observations(patient.uuid) # except: # continue # for observation in observations: # print("observation 1") # components = observation.components # for component in components: # print(component.display) # print(component.value) # problems.update({component.display: problems.get(component.display, 0) + 1}) # plt.bar(range(len(problems)), list(problems.values()), align = 'center') # plt.xticks(range(len(problems)),list(problems.keys()), rotation = 'vertical' ) # plt.show(); # def graph_by_diseases(): # def graph_by_certain_age(age):
def graph_marital_status(): fhir = FHIR() patients = fhir.get_all_patients() marital_status = {} for patient in patients: if str(patient.marital_status) in marital_status: marital_status[str(patient.marital_status)] += 1 else: marital_status[str(patient.marital_status)] = 1 plt.bar(range(len(marital_status)), list(marital_status.values()), align='center') plt.xticks(range(len(marital_status)), list(marital_status.keys())) fig1 = plt.gcf() plt.draw() number = random.randint(1, 1000) fig1.savefig('plot'+str(number)+'.png', dpi=100) return number
class Main: def __init__(self): self.fhir = FHIR('https://localhost:5001/api/', verify_ssl=False) self.patients = self.fhir.get_all_patients() def print_patients_in_range(self, limit, lower, upper, sender_name, sender_ref, app_url): i = 0 patients_list = [] for patient in self.patients: dict = {} if not i < limit: # print(patients_list) return patients_list id = patient.uuid name = patient.full_name() age = patient.age() if age >= lower and age < upper: telecoms = patient.telecoms phone = telecoms[len(telecoms) - 1].number # print(phone) msg = "Hi " + name + ",\n" + "Are you a carer? If so, we've built an app just for you! You can download our app for carers here: " + app_url + "\nUse this reference code to set up your app: " + sender_ref + "\nTake Care,\n" + sender_name # print(msg) # print(id) # print(name) # print(age) # print() dict["phone"] = phone dict["msg"] = msg patients_list.append(dict) i += 1 # main = Main() # list = main.print_patients_in_range(3, 55, 65, "Paul", "UCL-FHIR-Hack", "appdownload.com") # patient = fhir.get_patient('8f789d0b-3145-4cf2-8504-13159edaa747') # name = patient.full_name() # print(patient) # list = fhir.get_all_patients()
def graph_by_age_groups(): fhir = FHIR() patients = fhir.get_all_patients() ageGroups = {'<10': 0, '10-20': 0, '20-30':0, '30-40':0, '40-50':0, '50-60':0, '60-70': 0, '70-80':0, '80-90':0, '90+':0} for patient in patients: birth = patient.birth_date patientAge = calculate_age(birth) if(patientAge < 10): ageGroups['<10'] += 1 elif( 10 <= patientAge < 20): ageGroups['10-20'] += 1 elif( 20 <=patientAge < 30): ageGroups['20-30'] += 1 elif( 30 <=patientAge < 40): ageGroups['30-40'] += 1 elif( 40 <=patientAge< 50): ageGroups['40-50'] += 1 elif( 50 <=patientAge < 60): ageGroups['50-60'] += 1 elif( 60 <patientAge < 70 ): ageGroups['60-70'] += 1 elif( 70 <patientAge < 80 ): ageGroups['70-80'] += 1 elif( 80 <patientAge < 90 ): ageGroups['80-90'] += 1 elif( 90 <=patientAge ): ageGroups['90+'] += 1 plt.bar(range(len(ageGroups)), list(ageGroups.values()), align='center') plt.xticks(range(len(ageGroups)), list(ageGroups.keys()), rotation='vertical') fig1 = plt.gcf() plt.draw() number = random.randint(1, 1000) fig1.savefig('plot'+str(number)+'.png', dpi=100) return number
def observationstats(): fhir = FHIR() patients = fhir.get_all_patients() observations = [] for patient in patients: observations.extend(fhir.get_patient_observations(patient.uuid)) total_obsv = len(observations) observation_types = [observation.type for observation in observations] most_frequent_observation_type = max(set(observation_types), key=observation_types.count) observation_components = [] for observation in observations: observation_components.extend(observation.components) total_obsv_components = len(observation_components) observation_component_types = [ observation_component.display for observation_component in observation_components ] most_frequent_observation_component_type = max(set(observation_types), key=observation_types.count) obsvstats_dictionary = { "totalObsv": total_obsv, "mfObservationType": most_frequent_observation_type, "totalObsvComp": total_obsv_components, "mfObsvCompType": most_frequent_observation_component_type } response = jsonify(obsvstats_dictionary) return response
def __init__(self): fhir = FHIR(endpoint='https://fhir.compositegrid.com:5001/api/') self.patients = fhir.get_all_patients()
class GraphManager: """Create a FHIR object to retrieve patient data """ def __init__(self): self.FHIR = FHIR() def get_patients(self, page_number: int = 0) -> List[Patient]: if page_number == 0: return self.FHIR.get_all_patients() else: return self.FHIR.get_patient_page(page_number) def characteristic_is_observation_type(self, characteristic: str) -> bool: non_observation_types = ['language', 'marital status', 'city', 'country', 'state', 'family name', 'age'] if non_observation_types.__contains__(characteristic): return False return True def get_latest_obeservation_with_characteristic(self, patient: Patient, characteristic: str): observations = self.FHIR.get_patient_observations_page(patient.uuid, 3) latest_observation_value = 0 latest_time = datetime(1,1,1) found_first_observation = False for observation in observations: for observation_component in observation.components: if observation_component.display == characteristic: if found_first_observation == False: found_first_observation = True latest_time = observation.effective_datetime latest_observation_value = observation_component.value elif observation.effective_datetime > latest_time: latest_time = observation.effective_datetime latest_observation_value = observation_component.value return latest_observation_value def get_data(self, characteristic: str, page_number: int = 0): characteristic_values = {} patients = self.get_patients(page_number) if self.characteristic_is_observation_type(characteristic) == True: for patient in patients: data_point = self.get_latest_obeservation_with_characteristic(patient, characteristic) if str(data_point) in characteristic_values: characteristic_values[str(data_point)] += 1 else: characteristic_values[str(data_point)] = 1 else: for patient in patients: if characteristic == 'language': for language in patient.communications.languages: characteristic_values.update({language: characteristic_values.get(language, 0) + 1}) if characteristic == 'marital status': if str(patient.marital_status) in characteristic_values: characteristic_values[str(patient.marital_status)] += 1 else: characteristic_values[str(patient.marital_status)] = 1 if characteristic == 'city': if patient.addresses[0].city in characteristic_values: characteristic_values[str(patient.addresses[0].city)] += 1 else: characteristic_values[str(patient.addresses[0].city)] = 1 if characteristic == 'country': if patient.addresses[0].country in characteristic_values: characteristic_values[str(patient.addresses[0].country)] += 1 else: characteristic_values[str(patient.addresses[0].country)] = 1 if characteristic == 'state': if patient.addresses[0].state in characteristic_values: characteristic_values[str(patient.addresses[0].state)] += 1 else: characteristic_values[str(patient.addresses[0].state)] = 1 if characteristic == 'family name': if patient.name.family in characteristic_values: characteristic_values[str(patient.name.family)] += 1 else: characteristic_values[str(patient.name.family)] = 1 if characteristic == 'age': if floor(patient.age()) in characteristic_values: characteristic_values[str(floor(patient.age()))] += 1 else: characteristic_values[str(floor(patient.age()))] = 1 return collections.OrderedDict(sorted(characteristic_values.items())) def graph_single_characteristic(self, characteristic: str, page_number: int = 0, graph_type: str = 'x_y_plot'): data = self.get_data(characteristic, page_number) plt.title('Values of the ' + str(characteristic) + ' in the first ' + str(page_number) + ' pages in the patients record') if graph_type == 'bar chart': plt.bar(range(len(data)), list(data.values()), align='center') plt.xticks(range(len(data)), list(data.keys()), rotation='vertical') elif graph_type == 'pie chart': plt.pie(data.values(), labels=data.keys(), autopct='%1.1f%%', shadow=True, startangle=90) plt.axis('equal') elif graph_type == 'x_y_plot': plt.plot(list(data.keys()), list(data.values())) def graph_multiple_characteristics(self, characteristics: List[str], page_number: int = 0, graph_type: str = 'x_y_plot'): for characteristic in characteristics: plt.figure() self.graph_single_characteristic(characteristic, page_number, graph_type) def show_figures(self): plt.show()
class SyntheticDataGenerator(): def __init__(self): self.fhir = FHIR() self.patients = self.fhir.get_all_patients() self.num_examples = len(self.patients) self.num_features = 11 self.patient_record_columns = [ "UUID", "Name", "Gender", "Birthdate", "MaritalStatus", "MultipleBirth", "CoreRace", "CoreEthnicity", "Birthsex", "disabilityAdjLifeYears", "QualAdjLifeYears" ] def getPatientsDF(self): patients = self.patients patient_data = np.zeros((self.num_examples, self.num_features), dtype=object) for i in range(self.num_examples): patient = patients[i] patient_data[i][0] = patient.uuid patient_data[i][1] = patient.full_name() patient_data[i][2] = patient.gender patient_data[i][3] = patient.birth_date patient_data[i][4] = patient.marital_status.marital_status patient_data[i][5] = patient.multiple_birth patient_data[i][6] = patient.get_extension("us-core-race") patient_data[i][7] = patient.get_extension("us-core-ethnicity") patient_data[i][8] = patient.get_extension("us-core-birthsex") patient_data[i][9] = patient.get_extension( "disability-adjusted-life-years") patient_data[i][10] = patient.get_extension( "quality-adjusted-life-years") return pd.DataFrame(patient_data, columns=self.patient_record_columns) def createObservations(self): # patient_observations = [] # for uuid in patients_df["UUID"]: # patient_observations.append(fhir.get_patient_observations(uuid)) with open("patient_obs.pkl", "rb") as f: patient_observations = pickle.load(f) components = [] for obs in patient_observations: for ob in obs: for component in ob.components: components.append(component.display) components_arr = np.unique(components) for i in range(len(components_arr)): components_arr[i] = components_arr[i].replace("\u200b", "") components_arr = np.unique(components_arr) components_dict = {} for i in range(components_arr.size): components_dict[components_arr[i]] = i patient_observations_arr = np.empty( (self.num_examples, components_arr.size)) patient_observations_arr[:] = np.nan number_obs_arr = np.zeros(patient_observations_arr.shape, dtype=int) for obs in patient_observations: for ob in obs: for component in ob.components: components.append(component.display) for i in range(len(patient_observations)): for j in range(len(patient_observations[i])): for k in range(len(patient_observations[i][j].components)): ob = patient_observations[i][j].components[k] index = (i, components_dict[ob.display.replace("\u200b", "")]) if np.isnan(patient_observations_arr[index]): patient_observations_arr[index] = ob.value else: patient_observations_arr[index] += ob.value number_obs_arr[index] += 1 patient_mean_obs = patient_observations_arr / number_obs_arr patient_observations_df = pd.DataFrame(patient_mean_obs, columns=components_arr) return patient_observations_df def generateDataset(self, patient_observations_df): patients_df = self.getPatientsDF() collengths = [] for column in patient_observations_df.columns: if len(patient_observations_df[column].dropna()) < 100: collengths.append(column) patient_observations_df.drop(collengths, axis=1, inplace=True) full_patient_df = pd.concat([patients_df, patient_observations_df], axis=1) full_patient_df["Age"] = (date.today() - full_patient_df["Birthdate"]).dt.days full_patient_df["Gender"] = (full_patient_df["Gender"] == "male") full_patient_df["MaritalStatus"] = ( full_patient_df["MaritalStatus"] == "M") full_patient_df["Birthsex"] = (full_patient_df["Birthsex"] == "M") full_patient_df.rename( { "Gender": "is_male", "MaritalStatus": "is_married", "Birthsex": "birthsex_is_male" }, axis=1, inplace=True) full_patient_df["is_male"] = full_patient_df["is_male"].astype(int) full_patient_df["is_married"] = full_patient_df["is_married"].astype( int) full_patient_df["MultipleBirth"] = full_patient_df[ "MultipleBirth"].astype(int) full_patient_df["birthsex_is_male"] = full_patient_df[ "birthsex_is_male"].astype(int) patient_dataset = full_patient_df.copy() cols_to_drop = [ "Name", "is_male", "Birthdate", "is_married", "CoreRace", "UUID", "CoreEthnicity", "MultipleBirth", "Body mass index (BMI) [Percentile] Per age and gender", "Weight-for-length Per age and sex", "RBC Auto (Bld) [#/Vol]" ] patient_dataset["disabilityAdjLifeYears"] = patient_dataset[ "disabilityAdjLifeYears"].astype(float) patient_dataset["QualAdjLifeYears"] = patient_dataset[ "QualAdjLifeYears"].astype(float) patient_dataset.drop(cols_to_drop, axis=1, inplace=True) return patient_dataset def generateSyntheticData(self, number_of_datapoints): patient_dataset = self.generateDataset(self.createObservations()) covariance_data = patient_dataset.cov() + 45 * np.identity(48) covariance_data.fillna(0, inplace=True) patient_cholesky = np.linalg.cholesky(covariance_data) samples = np.random.multivariate_normal(patient_dataset.mean().values, covariance_data, size=number_of_datapoints * 2) synthetic_df = pd.DataFrame(samples) synthetic_df[0] = (synthetic_df[0] >= 1) synthetic_df = synthetic_df.astype(int) synthetic_df = synthetic_df[(synthetic_df[1] >= 0) & (synthetic_df[2] >= 0)] synthetic_df[synthetic_df < 0] = np.nan synthetic_df.columns = patient_dataset.columns synthetic_df["Age"] = synthetic_df["Age"] // 365 return synthetic_df.head(number_of_datapoints)
def setVariables(searchField, searchCriteria): searchF = searchField searchC = searchCriteria fhir = FHIR() global patients patients = fhir.get_all_patients()
def index(): fhir = FHIR() patients = fhir.get_all_patients() return render_template('patients.html', patients=patients)
def demographicGroups(): fhir = FHIR() patients = fhir.get_all_patients() statistics = { 'age': { '0 to 9': 0, '10 to 19': 0, '20 to 29': 0, '30 to 39': 0, '40 to 49': 0, '50 to 59': 0, '60 to 69': 0, '70 to 79': 0, '80 to 100': 0, '> 100': 0 }, 'marital': { 'Never Married': 0, "Single": 0, 'Married': 0 }, 'gender': { 'male': 0, 'female': 0 }, 'location': {}, 'race': {}, 'language': {} } for patient in patients: age = abs(datetime.today().year - patient.birth_date.year) marital = str(patient.marital_status) gender = str(patient.gender) locations = [str(i.state) for i in patient.addresses] race = str(patient.get_extension('us-core-race')) languages = [str(i) for i in patient.communications.languages] if age <= 9: statistics['age']['0 to 9'] += 1 elif age >= 10 and age <= 19: statistics['age']['10 to 19'] += 1 elif age >= 20 and age <= 29: statistics['age']['20 to 29'] += 1 elif age >= 30 and age <= 39: statistics['age']['30 to 39'] += 1 elif age >= 40 and age <= 49: statistics['age']['40 to 49'] += 1 elif age >= 50 and age <= 59: statistics['age']['50 to 59'] += 1 elif age >= 60 and age <= 69: statistics['age']['60 to 69'] += 1 elif age >= 70 and age <= 79: statistics['age']['70 to 79'] += 1 elif age >= 80 and age <= 100: statistics['age']['80 to 100'] += 1 else: statistics['age']['> 100'] += 1 statistics['marital'][marital] += 1 statistics['gender'][gender] += 1 for i in locations: if statistics['location'].get(i) == None: statistics['location'][i] = 1 else: statistics['location'][i] += 1 if statistics['race'].get(race) == None: statistics['race'][race] = 1 else: statistics['race'][race] += 1 for i in languages: if statistics['language'].get(i) == None: statistics['language'][i] = 1 else: statistics['language'][i] += 1 statistics['age'] = statistics['age'].values() statistics['marital'] = statistics['marital'].values() statistics['gender'] = statistics['gender'].values() return render_template("demographic.html", stats=statistics)
def __init__(self): self.pdf = canvas.Canvas("Record.pdf") self.pdf.setTitle("Patient Record") fhir = FHIR() self.patients_lists = fhir.get_all_patients()
def save_doc(text, dropdown): # Full list of patients fhir = FHIR() patients = fhir.get_all_patients() document = Document() new_line = " " # Find number of patients number_of_patients = 0 for i in patients: number_of_patients = number_of_patients + 1 # Index of Patient if dropdown == "Forename": result = forename(text) elif dropdown == "Surname": result = surname(text) else: result = unique_identifier(text) patient = patients[result] # Address of Patient address = patient.addresses[0] communications = patient.communications # Date Today today = date.today() # Marital Status marital_status_patient = patient.marital_status.marital_status # Age age = today.year - patient.birth_date.year - ( (today.month, today.day) < (patient.birth_date.month, patient.birth_date.day)) # Records of patients patient_data = {"Unique Identifier": patient.uuid.upper(), "Surname": patient.name.family, "Forename": patient.name.given, "Gender": patient.gender.capitalize(), "Birthday": str(patient.birth_date.day) + "." + str(patient.birth_date.month) + "." \ + str(patient.birth_date.year), "Age": str(age), "Marital Status": str(marital_status_patient), "Address": address.full_address, "Languages Spoken": communications.languages} # Records of a patient observations = fhir.get_patient_observations( patient_data["Unique Identifier"].lower()) # Number of observations for 1 Patient count = 0 for i in observations: count = count + 1 # NHS Image LOGO = "1280px-NHS-Logo.svg.png" logo = document.add_picture(LOGO, width=Inches(1.50)) document.add_heading(patient.name.full_name, 1) document.add_heading(new_line, 1) # Adding general information about a patient in a word document for data in patient_data: p = document.add_paragraph() p.add_run(data + ": ").bold = True p.add_run(patient_data[data]) document.add_heading(new_line, 1) # Creating a table table = document.add_table(rows=1, cols=8) hdr_cells = table.rows[0].cells hdr_cells[0].text = 'Observation(s) ' hdr_cells[1].text = 'Number' hdr_cells[2].text = 'Type' hdr_cells[3].text = 'Status ' hdr_cells[5].text = 'Issued Date' hdr_cells[6].text = 'Code' hdr_cells[7].text = 'Result' # Adding data into the table for i in range(0, count): row_cells = table.add_row().cells row_cells[0].text = str(i + 1) row_cells[1].text = observations[i].uuid.upper() row_cells[2].text = observations[i].type.capitalize() row_cells[3].text = observations[i].status.capitalize() row_cells[5].text = str(observations[i].issued_datetime) for component in observations[i].components: row_cells[6].text = component.code row_cells[7].text = component.display + ": " + component.quantity() # Save the document document.save( str(patient.name.given[0]) + "." + str(patient.name.family) + "." + str(age) + '.docx')
import matplotlib.pyplot as plt from fhir_parser import FHIR import math fhir = FHIR() patients = fhir.get_all_patients() def getLanguageData(): languages = {} for patient in patients: for language in patient.communications.languages: languages.update({language: languages.get(language, 0) + 1}) print(languages) return languages def getMaritalStatus(): maritalStatus = {} for patient in patients: maritalStatus.update({ str(patient.marital_status): maritalStatus.get(str(patient.marital_status), 0) + 1 }) return maritalStatus def getAge(): ages = {} for patient in patients: # print(math.floor(patient.age()))
def getSpecificPatient(uuid): fhir = FHIR() patients = fhir.get_all_patients() specific_patient = fhir.get_patient(uuid) return specific_patient