def getChart2(): try: data = fishData.stream() dataDict = {} for data1 in data: data1 = data1.to_dict() month = int(data1['date'].split("-")[1]) weight = data1['totalWeight'] if month in dataDict: dataDict[month] = dataDict[month] + weight else: dataDict[month] = weight plt.bar(list(dataDict.keys()), dataDict.values(), color='g') plt.xlabel('Month') plt.ylabel('Weight of catch caught') a = 'temp' + ''.join( [str(random.randint(0, 999)).zfill(3) for _ in range(2)]) + '.png' imageTempPath = BASE_PATH + a plt.title('Month vs Weight of catch caught') plt.savefig(imageTempPath) storage.child('charts/{}'.format(a)).put(imageTempPath) chartUrl = storage.child('charts/{}'.format(a)).get_url(None) print(chartUrl) return jsonify({'chartUrl': chartUrl}) except Exception as e: print(e) return f"An Error Occured", 500
def addProducts(category_id, sub_category_id, product_name, product_desc, product_price, business_id, img): _, product = firestore_client.collection('businesses').document( business_id).collection('categories').document(category_id).collection( 'subcategories').document(sub_category_id).collection( 'products').add({ 'name': product_name, 'description': product_desc, 'price': product_price }) storage.child(business_id + '/' + category_id + '/' + sub_category_id + '/' + product.id + '/' + 'hh.jpg').put(img[0]) firestore_client.collection('businesses').document(business_id).collection( 'categories').document(category_id).collection( 'subcategories').document(sub_category_id).collection( 'products').document(product.id).update({ 'image': storage.child(business_id + '/' + category_id + '/' + sub_category_id + '/' + product.id + '/' + 'hh.jpg').get_url(None) }) # links = storage.child(business_id+'/'+category_id+'/'+sub_category_id+'/'+product.id+'/'+'hh.jpg').get_url(None) # print(links) return True
def uploadFile(file, prev_ref): prev = str(prev_ref).encode() # print("prev --- ", prev) # print("string type --- ", type(prev) == str) # print("not empty --- ", prev != '') # print("has valid filename --- ", allowed_photo(prev)) filename = uniqueValue() + secure_filename(file.filename) filepath = os.path.join(current_dir, filename) file.save(filepath) storage = firebase.storage() if prev and type(prev) == str and prev != '' and allowed_photo(prev): try: storage.delete(prev) except Exception: print("could not delete prev --- ", prev) # pass storage.child(filename).put(filepath) link = 'https://firebasestorage.googleapis.com/v0/b/' + config[ 'storageBucket'] + '/o/' + filename + '?alt=media&token=None' os.remove(filepath) return link
def send_to_db(image_path, Longitude=77, Latitude=13): name = time.time() name *= 1000 name = int(name) storage.child(str(name)).put(image_path) url = storage.child(str(name)).get_url(2) db = firestore.client() ref = db.collection('accidents') ref.add({ "URL": url, "Longitude": Longitude, "Latitude": Latitude, "is_dismissed": False, "is_reported": False, "timestamp": name }) send_to_whatsapp( url, "https://www.google.com/maps/dir/?api=1&destination=" + str(Latitude) + "," + str(Longitude))
def getChart1(): try: data = fishData.stream() dataDict = {} for data1 in data: data1 = data1.to_dict() month = int(data1['date'].split("-")[1]) count = 0 for i in data1['catches']: count += int(i['quantity']) if month in dataDict: dataDict[month] = dataDict[month] + count else: dataDict[month] = count plt.bar(list(dataDict.keys()), dataDict.values(), color='g') plt.xlabel('Months') plt.ylabel('No of Catches') plt.title('Month Vs No of catches caught') a = 'temp' + ''.join( [str(random.randint(0, 999)).zfill(3) for _ in range(2)]) + '.png' imageTempPath = BASE_PATH + a plt.savefig(imageTempPath) storage.child('charts/{}'.format(a)).put(imageTempPath) chartUrl = storage.child('charts/{}'.format(a)).get_url(None) print(chartUrl) return jsonify({ 'chartUrl': chartUrl, 'average': res, 'moneySaved': res * 2 }) except Exception as e: return f"An Error Occured", 500
def updateCatch(): try: data = request.json print(data) print(data['image']) convert(data['image'], data['imageFileName']) imagePath = BASE_PATH + data['imageFileName'] storage.child('fishes/{}'.format(data['imageFileName'])).put(imagePath) fish_url = storage.child('fishes/{}'.format( data['imageFileName'])).get_url(None) # The url of the image sent from app print(fish_url) request_url = 'http://35.240.219.8:8080/register?key=' + fish_url print(request_url) try: predictedData = requests.get(request_url).json() except Exception as e: print(e) print(predictedData) temp = int(''.join( [str(random.randint(0, 999)).zfill(3) for _ in range(2)])) data2 = { 'date': data['date'], 'description': data['description'], 'image': fish_url, 'hours': float(data['hours']), 'latitude': float(data['latitude']), 'longitude': float(data['longitude']), 'name': data['name'] + " " + str(temp), 'weight': float(data['weight']), 'number': data['number'], 'catchId': temp, 'catches': predictedData['fishData'], 'boundedImageUrl': predictedData['boundedUrl'] } ref = userCatches.document(str(data['number'])) ref.update({u'catches': firestore.ArrayUnion([data2])}) print(data2) data1 = { 'latitude': float(data2['latitude']), 'longitude': float(data2['longitude']), 'totalWeight': float(data2['weight']), 'date': data2['date'], 'catchId': data2['catchId'], 'hours': data2['hours'], 'catches': predictedData['fishData'], 'boundedImageUrl': predictedData['boundedUrl'] } print(data1) fishData.document(str(data1['catchId'])).set(data1) return jsonify({"success": True}) except Exception as e: print(e) return jsonify({"success": False}), 400
def getChart4(): print(request.args) try: number = str(request.args['number']) catchDetails = userCatches.document(number).get().to_dict()["catches"] # print(catchDetails) resDict = dict() monthDict = dict() for i in catchDetails: hours = float(i['hours']) weight = float(i['weight']) month = int(i['date'].split("-")[1]) print(month) CPUE = weight / hours print(CPUE) if month in resDict: resDict[month] = resDict[month] + CPUE else: resDict[month] = CPUE if month in monthDict: monthDict[month] += 1 else: monthDict[month] = 1 print(resDict, monthDict) ans = {} total = 0 for i in resDict.keys(): ans[i] = resDict[i] / monthDict[i] total += ans[i] # This is the average weight/hour. # Money saved is random. res = total / len(ans) print(str(ans.keys()) + " " + str(ans.values()) + "hi") plt.bar(list(ans.keys()), ans.values(), color='g') plt.title("Month Vs CPUE - For Fisherman") plt.xlabel('Months') plt.ylabel('CPUE(weight of catch/hour)') a = 'temp' + ''.join( [str(random.randint(0, 999)).zfill(3) for _ in range(2)]) + '.png' imageTempPath = BASE_PATH + a plt.savefig(imageTempPath) storage.child('charts/{}'.format(a)).put(imageTempPath) chartUrl = storage.child('charts/{}'.format(a)).get_url(None) print(chartUrl) return jsonify({ 'chartUrl': chartUrl, 'average': res, 'moneySaved': res * 2 }) except Exception as e: return f"An Error Occured", 500
def imageUpload(request): # try: with open('./secrets.json') as json_file: json_data = json.load(json_file) firebaseConfig = json_data['FIREBASE_CONFIG'] firebase = pyrebase.initialize_app(firebaseConfig) storage = firebase.storage() image_file = request.FILES['image'] storage.child(request.user.profile.nickname).put(image_file) msg = {'status': 'true', 'message': '이미지가 성공적으로 저장되었습니다.'} return JsonResponse(msg, status=200)
def put_image(dirname): filenames = os.listdir(dirname) # folder_num = 1 for i, filename in enumerate(filenames): # print(i, filename) # if i % 2000 == 0: # folder_num += 1 if i < 82800: continue # directory = 'img' + str(folder_num) # if not os.path.exists(directory): # os.makedirs(directory) full_name = os.path.join(dirname, filename) storage_file_name = os.path.join("images/", filename) print(full_name) storage.child(storage_file_name).put(full_name) print(i, "Image Uploaded")
def put_image(dirname): filenames = os.listdir(dirname) # folder_num = 1 for i, filename in enumerate(filenames): # print(i, filename) # if i % 2000 == 0: # folder_num += 1 # if i < 82800: # continue # directory = 'img' + str(folder_num) # if not os.path.exists(directory): # os.makedirs(directory) print(filename) full_name = os.path.join(dirname, filename) name = filename.split('.') if len(name[0]) == 0: continue print(full_name) storage.child(name[0]).put(full_name) print(i, "Image Uploaded")
def send_to_db(image_path, Longitude=77, Latitude=13): name = time.time() name *= 1000 name = int(name) storage.child(str(name)).put(image_path) url = storage.child(str(name)).get_url(2) db = firestore.client() ref = db.collection('accidents') ref.add({ "URL": url, "Longitude": Longitude, "Latitude": Latitude, "is_dismissed": False, "is_reported": False, "timestamp": name })
def getChart3(): data = fishData.stream() dataDict = {} monthCountDict = {} try: for data1 in data: data1 = data1.to_dict() print(data1) month = int(data1['date'].split("-")[1]) weight = data1['totalWeight'] hours = data1['hours'] CPUE = weight / hours if month in monthCountDict: monthCountDict[month] += 1 else: monthCountDict[month] = 1 if month in dataDict: dataDict[month] = dataDict[month] + CPUE else: dataDict[month] = CPUE ans = {} for i in dataDict.keys(): ans[i] = dataDict[i] / monthCountDict[i] plt.bar(list(ans.keys()), ans.values(), color='g') plt.xlabel('Months') plt.ylabel('CPUE(weight of catch/hour)') a = 'temp' + ''.join( [str(random.randint(0, 999)).zfill(3) for _ in range(2)]) + '.png' imageTempPath = BASE_PATH + a plt.savefig(imageTempPath) plt.title("Month Vs CPUE") storage.child('charts/{}'.format(a)).put(imageTempPath) chartUrl = storage.child('charts/{}'.format(a)).get_url(None) print(chartUrl) return jsonify({ 'chartUrl': chartUrl, }) except Exception as e: return f"An Error Occured", 500
def get_Song(songName): ref = db.collection('songs').document(songName).get() filePath = ref.to_dict()["filepath"] storage.child(filePath).download("intermediate.mid") original_score = converter.parse("intermediate.mid").chordify() subset = original_score[0:35] notes, durations = process_Chords(subset, notes_dict, durations_dict) midi_stream = stream.Stream() midi_stream.append(subset) for i in range(200): subset_notes = notes[len(notes) - 29:len(notes) - 1] subset_durations = durations[len(durations) - 29:len(durations) - 1] feed_notes = np.array(subset_notes).reshape((1, len(subset_notes))) feed_duration = np.array(subset_durations).reshape( (1, len(subset_durations))) prediction = model.predict([feed_notes, feed_duration]) notes.append(np.argmax(prediction[0])) durations.append(np.argmax(prediction[1])) prediction_note = notes_dict_reverse[np.argmax(prediction[0])] prediction_duration = durations_dict_reverse[np.argmax(prediction[1])] print(process_output(prediction_note, prediction_duration)) midi_stream.append(process_output(prediction_note, prediction_duration)) midi_stream = midi_stream.chordify() midi_stream.write('mid', "output.mid") storage.child("gen_" + filePath).put("output.mid") return songName
def viewPatient(request, pid): curr_u = retrieveCurrU(request) u_name = database.child('user').child(curr_u).child('details').child('name').get().val() idtoken = request.session['uid'] patient = database.child('patient').child(pid).get().val() screening = database.child('patient').child(pid).child('screening').get().val() screen_list = [] if screening is not None: for p in screening: temp_class = database.child('patient').child(pid).child('screening').child(p).get().val() # Convert classes if temp_class['classes'] == 4: str_class = "No prediction yet" elif temp_class['classes'] == 1: str_class = "Predicted as Class I" elif temp_class['classes'] == 2: str_class = "Predicted as Class II" else: str_class = "Predicted as Class III" # Convert date pf = float(p) date = datetime.fromtimestamp(pf).strftime('%H:%M %d-%m-%Y') p_data = { "date": date, "datetime": p, "classes": str_class, "class1": temp_class['class1'], "class2": temp_class['class2'], "class3": temp_class['class3'], "xray_url": storage.child(patient_path+pid+"/"+p+".jpg").get_url(idtoken) } screen_list.append(p_data) data = { "name": u_name, "pname": patient["pname"], "pid": pid, "age": patient["age"], "gender": patient["gender"], "pic": patient["pic"], "screening": screen_list } return render(request, "profileP.html", data)
def classifier(request, pid=None, dt=None): idtoken = request.session['uid'] # Open patient image xray_url = storage.child(patient_path+pid+"/"+dt+".jpg").get_url(idtoken) img = Image.open(urlopen(xray_url)) img_tensor = T.ToTensor()(img) img_fastai = I(img_tensor) # Prediction classifier = load_learner('./') if classifier: pred_class, pred_idx, output = classifier.predict(img_fastai) else: print("no model") class1 = round(output.numpy()[0], 4) class2 = round(output.numpy()[1], 4) class3 = round(output.numpy()[2], 4) classes = 4 if class1 > class2: if class1 > class3: classes = 1 else: if class2 > class1: if class2 > class3: classes = 2 else: classes = 3 updated = { "classes": classes, "class1": str(class1), "class2": str(class2), "class3": str(class3) } database.child("patient").child(pid).child("screening").child(dt).update(updated) print("before redirect") return redirect('/patient_list/patient_profile/'+pid)
def getProductsInfo(business_id, category_id, sub_category_id): product_list = [] for k in firestore_client.collection('businesses').document( business_id).collection('categories').document( category_id).collection('subcategories').document( sub_category_id).collection('products').get(): product_category_id = k.id product_category_name = k.to_dict()['name'] product_category_description = k.to_dict()['description'] product_category_price = k.to_dict()['price'] product_image = storage.child(business_id + '/' + category_id + '/' + sub_category_id + '/' + product_category_id + '/' + 'hh.jpg').get_url(None) product_list.append({ 'name': product_category_name, 'id': product_category_id, 'description': product_category_description, 'price': product_category_price, 'image': product_image }) return product_list
def doctor(pid, did): import datetime import pandas as pd import numpy as np import firebase_admin from firebase_admin import credentials from firebase_admin import firestore from firebase_admin import storage import pyrebase from datetime import date, timedelta import urllib.request, json import time from matplotlib import pyplot as plt import matplotlib.dates as mdates import os import csv from IPython.display import display from Model import trainData import random # from google.cloud import storage from matplotlib.patches import Ellipse import seaborn as sns import matplotlib.patches as mpatches # signal processing from scipy import signal from scipy.ndimage import label from scipy.stats import zscore from scipy.interpolate import interp1d from scipy.integrate import trapz # misc import warnings # generate pdf from reportlab.pdfgen import canvas from reportlab.lib.colors import Color, lightblue, black # In[97]: if not firebase_admin._apps: cred = credentials.Certificate("serene-firebase-adminsdk.json") app = firebase_admin.initialize_app( cred, { 'storageBucket': 'serene-2dfd6.appspot.com', }, name='[DEFAULT]') else: app = firebase_admin.get_app() db = firestore.client() # In[98]: today = datetime.datetime.now() timestamp = today.strftime("%Y-%m-%d") bucket = storage.bucket(app=app) # ## Get data from storage and get list of dates (2 weeks) # In[99]: # get a a list of date between start and end date userID = pid doctorID = did duration = 15 # two weeks dates = [] for x in range(0, duration): today = date.today() # revert to original # yesterday = today - datetime.timedelta(days=1) start_date = (today - timedelta(days=duration - x)).isoformat() dates.append(start_date) # In[100]: df = pd.DataFrame() notAvailableDates = [] # loop through the storage and get the data sleep = [] for x in range(0, len(dates)): # Sleep blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-sleep.json") # download the file u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) sleepMinutes = data['summary']["totalMinutesAsleep"] except: notAvailableDates.append(dates[x]) pass # Activity (Steps) blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-activity.json") # download the file u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) steps = data['summary']["steps"] except: notAvailableDates.append(dates[x]) pass # heartrate heart_rate = {} blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-heartrate.json") u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) df_heartrate = pd.DataFrame( data['activities-heart-intraday']['dataset']) df_heartrate.time.apply(str) df_heartrate['time'] = pd.to_datetime(df_heartrate['time']) df_heartrate['hour'] = df_heartrate['time'].apply( lambda time: time.strftime('%H')) df_heartrate.drop(['time'], axis=1, inplace=True) heart_rate = df_heartrate.groupby(["hour"], as_index=False).mean() heart_rate['sleepMin'] = sleepMinutes heart_rate['TotalSteps'] = steps heart_rate['date'] = dates[x] heart_rate = heart_rate.astype({"hour": int}) except: notAvailableDates.append(dates[x]) pass # append dataframe df = df.append(heart_rate, ignore_index=True) # In[101]: notAvailableDates notSyncedDates = pd.DataFrame() notSyncedDates['date'] = notAvailableDates # In[102]: notSyncedDates = notSyncedDates.drop_duplicates() # In[103]: notSyncedDates # ### Get user location # In[104]: # get location from database loc_df = pd.DataFrame() locID = [] locations = db.collection(u'PatientLocations').where( u'patientID', u'==', userID).stream() for location in locations: loc1 = location.to_dict() locID.append(location.id) loc_df = loc_df.append(pd.DataFrame(loc1, index=[0]), ignore_index=True) loc_df['id'] = locID # In[105]: loc_df.drop(['anxietyLevel', 'lat', 'lng', 'patientID'], axis=1, inplace=True) # In[106]: loc_df.time.apply(str) loc_df['time'] = pd.to_datetime(loc_df['time']) loc_df['date'] = pd.to_datetime(loc_df['time'], format='%Y:%M:%D').dt.date loc_df['hour'] = loc_df['time'].apply(lambda time: time.strftime('%H')) loc_df.drop(['time'], axis=1, inplace=True) loc_df.hour = loc_df.hour.astype(int) loc_df.date = loc_df.date.astype(str) df.date = df.date.astype(str) # In[107]: dfinal = pd.merge(left=df, right=loc_df, how='left', left_on=['hour', 'date'], right_on=['hour', 'date']).ffill() # ### Test data into model # In[108]: # test model train_df = dfinal.rename(columns={'value': 'Heartrate'}) # In[109]: Labeled_df = pd.DataFrame() Labeled_df = trainData(train_df) # In[110]: Labeled_df.drop(['lon'], axis=1, inplace=True) # In[111]: Labeled_df['name'].fillna("Not given", inplace=True) Labeled_df['id'].fillna("Not given", inplace=True) Labeled_df['anxiety_assigned'].fillna('Not given', inplace=True) # In[112]: # Update firebase with the user anxiety level for row in Labeled_df.itertuples(): if row.id != 'Not given': if row.Label == 'Low' or row.Label == 'LowA': anxietyLevel = '1' elif row.Label == 'Meduim': anxietyLevel = '2' else: anxietyLevel = '3' if row.anxiety_assigned == False or row.anxiety_assigned == 'Not given': doc_ref = db.collection(u'PatientLocations').document(row.id) doc_ref.update({ u'anxietyLevel': anxietyLevel, u'anxiety_assigned': True }) # ### Show the places with highest anxiety level # In[113]: # Show the highest level df_high = pd.DataFrame() df_high = Labeled_df[Labeled_df.Label == 'High'] # # Get patient information # In[114]: docDf = pd.DataFrame() doc_ref = db.collection(u'Patient').document(userID) doc = doc_ref.get().to_dict() docDf = docDf.append(pd.DataFrame(doc, index=[0]), ignore_index=True) # In[115]: age1 = docDf['age'].values name1 = docDf['name'].values emp1 = docDf['employmentStatus'].values mar1 = docDf['maritalStatus'].values income1 = docDf['monthlyIncome'].values chronicD1 = docDf['chronicDiseases'].values smoke1 = docDf['smokeCigarettes'].values gad1 = docDf['GAD-7ScaleScore'].values gender1 = docDf['gender'].values age = age1[0] name = name1[0] emp = emp1[0] mar = mar1[0] income = income1[0] chronicD = chronicD1[0] smoke = smoke1[0] gad = gad1[0] gender = gender1[0] # ## Storage intiliazation # In[116]: firebaseConfig = { "apiKey": "AIzaSyBoxoXwFm9TuFysjQYag0GB1NEPyBINlTU", "authDomain": "serene-2dfd6.firebaseapp.com", "databaseURL": "https://serene-2dfd6.firebaseio.com", "projectId": "serene-2dfd6", "storageBucket": "serene-2dfd6.appspot.com", "messagingSenderId": "461213981433", "appId": "1:461213981433:web:62428e3664182b3e58e028", "measurementId": "G-J66VP2Y3CR" } firebase = pyrebase.initialize_app(firebaseConfig) storage = firebase.storage() # # HR # In[117]: sns.set(rc={'axes.facecolor': '#fcfeff'}) # In[118]: # Take the highest heartrate in a day dfhr = pd.DataFrame() hr = [] hrr = 0 rowCount = 1 for x in range(0, len(dates)): rowCount = 0 for row in df.itertuples(): if (row.date == dates[x]): if (row.value > rowCount): rowCount = row.value hr.append(rowCount) dfhr['date'] = dates dfhr['hr'] = hr # In[119]: plt.figure(figsize=(20, 7)) plt.plot(dfhr['date'], dfhr['hr'], color="#a03e3e", linewidth=4) plt.xlabel("Date", fontsize=16) plt.ylabel("Amplitude (bpm)") plt.tick_params(axis='x', rotation=70) plt.tight_layout() plt.savefig('hr.png', dpi=None) # plt.show() # plt.draw() # # Steps # In[120]: dfstep = pd.DataFrame() avgSteps = [] totalsteps = 0 rowCount = 1 for x in range(0, len(dates)): for row in Labeled_df.itertuples(): if (row.date == dates[x]): rowCount += 1 totalsteps += row.TotalSteps avgSteps.append(totalsteps / rowCount) dfstep['date'] = dates dfstep['Steps'] = avgSteps # In[121]: # Plot Steps plt.figure(figsize=(20, 7)) # plt.fill_between(dfstep['date'], dfstep['Steps'], color="#ffd6b0", linewidth=1) plt.plot(dfstep['date'], dfstep['Steps'], color="#ff5900", linewidth=4, marker='o', markerfacecolor='#ffd6b0') plt.xlabel("Date", fontsize=16) plt.ylabel("Total steps") plt.tick_params(axis='x', rotation=70) plt.tight_layout() plt.savefig('steps.png', dpi=None) # plt.show() # plt.draw() # # Sleep # In[122]: dfsleep = pd.DataFrame() sleeps = [] totalsleep = 0 rowCount = 1 for x in range(0, len(dates)): for row in df.itertuples(): if (row.date == dates[x]): totalsleep = row.sleepMin sleeps.append(totalsleep / 60) dfsleep['date'] = dates dfsleep['sleep'] = sleeps # In[123]: figs = dfsleep.plot.bar(x='date', y='sleep', rot=70, width=0.25, color='#3629a6', capstyle='round').get_figure() figs.set_size_inches(20, 10) plt.xlabel('Date') plt.ylabel('Sleep (hr)') # plt.show() # plt.draw() plt.tight_layout() figs.savefig('sleep.png', dpi=None) # # AL # In[124]: # Change Label values to num, to represent them in a barchart nums = [] for row in Labeled_df.itertuples(): if row.Label == 'Low' or row.Label == 'LowA': nums.append(1) elif row.Label == 'Meduim': nums.append(2) else: nums.append(3) Labeled_df['numLabel'] = nums # In[125]: # Get anxiety level by day and store it in a new data frame plot_df = pd.DataFrame() avgAnxiety = [] totalAnxiety = 0 rowCount = 1 for x in range(0, len(dates)): totalAnxiety = 0 rowCount = 1 for row in Labeled_df.itertuples(): if (row.date == dates[x]): rowCount += 1 totalAnxiety += row.numLabel avgAnxiety.append(totalAnxiety / rowCount) plot_df['date'] = dates plot_df['Anxiety'] = avgAnxiety # In[126]: fig, ax = plt.subplots() # Draw the stem and circle c1 = '#9dd6f5' c2 = '#4ba0d1' c3 = '#23495f' bar_width = 0.25 for t, y in zip(plot_df["date"], plot_df["Anxiety"]): c = "" if (y <= 1): c = c1 elif (1 < y <= 2): c = c2 elif (y > 2): c = c3 plt.bar([t, t], [0, y], bar_width, color=c) colors = [[c1, c1], [c2, c2], [c3, c3]] categories = ['Low', 'Meduim', 'High'] # create dict legend_dict = dict(zip(categories, colors)) # create patches patchList = [] for key in legend_dict: data_key = mpatches.Patch(facecolor=legend_dict[key][0], edgecolor=legend_dict[key][1], label=key) patchList.append(data_key) ax.legend(handles=patchList, ncol=len(categories), fontsize=12) plt.tick_params(axis='x', rotation=70) # Start the graph at 0 ax.set_ylim(0, 3) fig.set_size_inches(15.5, 10) plt.tight_layout() plt.xlabel('Date') ax.yaxis.set_label_coords(-0.02, 0.48) # volo insert # plt.show() fig.savefig('AL.png', dpi=None) # # Location Analysis # In[127]: # get location from database new_loc = pd.DataFrame() locID = [] locations = db.collection(u'PatientLocations').where( u'patientID', u'==', userID).stream() for location in locations: loc = location.to_dict() locID.append(location.id) new_loc = new_loc.append(pd.DataFrame(loc, index=[0]), ignore_index=True) new_loc['id'] = locID # In[128]: new_loc.time.apply(str) new_loc['time'] = pd.to_datetime(new_loc['time']) new_loc['date'] = pd.to_datetime(new_loc['time'], format='%Y:%M:%D').dt.date new_loc.drop(['time'], axis=1, inplace=True) new_loc.date = new_loc.date.astype(str) # In[129]: new_loc = new_loc[(new_loc.date >= dates[0]) & (new_loc.date <= dates[len(dates) - 1])] # In[130]: names = [] Name = "" for row in new_loc.itertuples(): Name = row.nearestLoc names.append(Name) # In[131]: new_name = pd.DataFrame() new_name['name'] = names # In[132]: new_name = new_name.drop_duplicates() new_name.dropna() # In[133]: fnames = [] fName = "" for row in new_name.itertuples(): fName = row.name fnames.append(fName) # In[134]: analysis = pd.DataFrame() count = 0 i = 0 # label = "" locationName = "" near = '' nearLocs = [] counts = [] # labels = [] locationNames = [] for x in range(0, len(fnames)): count = 0 locName = fnames[i] for row in new_loc.itertuples(): if (locName == row.nearestLoc): if (row.anxietyLevel == '3'): count += 1 # label = row.anxietyLevel locationName = row.name near = row.nearestLoc i += 1 counts.append(count) # labels.append(label) locationNames.append(locationName) nearLocs.append(near) analysis['Location'] = locationNames analysis['Frequency'] = counts # analysis ['Anxiety Level'] = labels analysis['Nearest Location'] = nearLocs # In[135]: # newA = analysis.drop(analysis[analysis['Number of occurrences'] == 0].index, inplace=True) # In[136]: import six import arabic_reshaper from bidi.algorithm import get_display from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from reportlab.lib import colors # In[137]: def render_mpl_table(data, col_width=5.0, row_height=0.625, font_size=14, header_color='#23495f', row_colors=['#e1eff7', 'w'], edge_color='#23495f', bbox=[0, 0, 1, 1], header_columns=0, ax=None, **kwargs): if ax is None: size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array( [col_width, row_height]) fig, ax = plt.subplots(figsize=size) ax.axis('off') mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, cellLoc='center', **kwargs) mpl_table.auto_set_font_size(False) mpl_table.set_fontsize(font_size) for k, cell in six.iteritems(mpl_table._cells): cell.set_edgecolor(edge_color) if k[0] == 0 or k[1] < header_columns: cell.set_text_props(weight='bold', color='w') cell.set_facecolor(header_color) else: cell.set_facecolor(row_colors[k[0] % len(row_colors)]) cell.alignment = 'center' fig.savefig('Location.png', dpi=100) return ax # In[138]: if (len(analysis) > 0): for ind, row in analysis.iterrows(): analysis.loc[ind, 'Nearest Location'] = get_display( arabic_reshaper.reshape(analysis.loc[ind, 'Nearest Location'])) # In[139]: if (len(analysis) > 0): render_mpl_table(analysis, header_columns=0, col_width=4) # # improvement # In[153]: # get yesterday improvement today_al = float("{:.2f}".format(plot_df['Anxiety'].mean())) improvement = -1 # In[165]: try: doc_ref = db.collection(u'Patient').document(userID) doc = doc_ref.get().to_dict() prev = float("{:.2f}".format(doc['anxiety_level'])) # calculate the improvement improvement = float("{:.2f}".format(((prev - today_al) / 3) * 100)) except: improvement = 404 # ## Generate doctor report pdf and store it in database # # In[143]: pdf = canvas.Canvas('Doctor.pdf') pdf.setTitle('Patient report') pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 400, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#23495f')) pdf.drawString(100, 570, "Patient Information") pdf.setFont("Helvetica-Bold", 15) pdf.drawString(150, 540, "Name: ") pdf.drawString(150, 520, "Age: ") pdf.drawString(150, 500, "Employment Status: ") pdf.drawString(150, 480, "Martial Status: ") pdf.drawString(150, 460, "Monthly Income: ") pdf.drawString(150, 440, "Chronic Diseases: ") pdf.drawString(150, 420, "Cigarette Smoker: ") pdf.drawString(150, 410, "Gender: ") pdf.setFont("Helvetica", 15) pdf.setFillColor(black) pdf.drawString(210, 540, name) pdf.drawString(210, 520, age) pdf.drawString(310, 500, emp) pdf.drawString(260, 480, mar) pdf.drawString(290, 460, income) pdf.drawString(290, 440, chronicD) pdf.drawString(290, 420, smoke) pdf.drawString(220, 410, gender) pdf.setFillColor(colors.HexColor('#bfbfbf')) pdf.roundRect(370, 560, 125, 30, 4, fill=1, stroke=0) pdf.setFillColorRGB(1, 1, 1) pdf.drawString(375, 570, "GAD-7 Score = ") pdf.setFont("Helvetica-Bold", 15) pdf.drawString(480, 570, gad) pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 160, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Helvetica-Bold", 16) pdf.setFillColor(colors.HexColor('#23495f')) pdf.drawString( 115, 330, "Report Duration From: (" + dates[0] + " To: " + dates[len(dates) - 1] + ")") pdf.setFont("Helvetica-Bold", 14) pdf.drawString(250, 300, "Improvements: ") pdf.setFont("Helvetica-Bold", 20) if (improvement == 404): pdf.drawString(290, 260, "--") else: pdf.drawString(280, 260, str(improvement) + '%') pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#808080')) pdf.drawString(100, 650, "Anxiety Level") pdf.drawImage("AL.png", 57, 400, width=485, height=200) pdf.drawString(100, 330, "Heart Rate") pdf.drawImage("hr.png", 57, 100, width=485, height=200) pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#808080')) pdf.drawString(100, 650, "Activity Rate") pdf.drawImage("steps.png", 57, 400, width=485, height=200) pdf.drawString(100, 350, "Sleep Analysis") pdf.drawImage("sleep.png", 57, 100, width=485, height=200) pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#808080')) pdf.drawString(100, 650, "Locations With Highest Level of Anxiety") if (len(analysis) > 0): pdf.drawImage("Location.png", 30, 200, width=570, height=100) else: pdf.setFont("Helvetica", 15) pdf.setFillColor(colors.HexColor('#23495f')) t = pdf.beginText(130, 550) text = [ name + " condition was stable through this period,", "no locations with high anxiety level were detected." ] for line in text: t.textLine(line) pdf.drawText(t) if (len(notSyncedDates) != 0): pdf.setFont("Helvetica", 12) pdf.setFillColor(colors.HexColor('#d40027')) pdf.drawString( 75, 100, "Note: Below dates are missing, because they were not synced correctly:" ) i = 70 for row in notSyncedDates.itertuples(): pdf.drawString(85, i, '- ' + row.date) i = i - 20 pdf.save() # In[144]: # new method doct = storage.child(userID + "/DoctorReport/doctorReport").put('Doctor.pdf') # In[145]: linkDF = pd.DataFrame() linkDF = linkDF.append(pd.DataFrame(doct, index=[0]), ignore_index=True) # In[146]: token1 = linkDF['downloadTokens'].values token = token1[0] link = storage.child(userID + "/DoctorReport/doctorReport").get_url(token) # In[149]: from datetime import datetime # In[150]: date = datetime.now() # In[161]: # store the data random_id = random.randint(0, 1000) ID = "Doctor" + userID + str(random_id) doc_rec = db.collection(u'DoctorReports').document(str(ID)) doc_rec.set({ u'doctorId': doctorID, u'emailsent': True, u'patientId': userID, u'reportTime': date, u'reportUrl': link, u'improvement': improvement }) doc_rec = db.collection(u'Patient').document(userID) doc_rec.update({u'anxiety_level': today_al}) # In[152]: os.remove("hr.png") os.remove("sleep.png") os.remove("AL.png") os.remove("steps.png") os.remove("Doctor.pdf") if (len(analysis) > 0): os.remove("Location.png") return {"Doctor": "Yaaay"}
def addFiletoFirebase(filename): storage.child('json_keys/' + filename).put('json_keys/' + filename)
"apiKey": "AIzaSyCE_8CVn1Zio1f-EXpEgZtp-8aoyxpteVk", "authDomain": "bestcamera-7e901.firebaseapp.com", "databaseURL": "https://bestcamera-7e901.firebaseio.com/", "projectId": "bestcamera-7e901", "storageBucket": "bestcamera-7e901.appspot.com", "messagingSenderId": "1087271452402", "appId": "1:1087271452402:web:9c34a3417a21237ebe5a6b", "measurementId": "G-3J20J96NZX" } firebase = pyrebase.initialize_app(config) storage = firebase.storage() add = random.randrange(9999999) faceURL = "imagine/dumi" + str(add) + ".jpg" path_local = "./saved/face/img.jpg" imgurl = storage.child(faceURL).put(path_local) db = firebase.database() camera = cv2.VideoCapture(-1) _, frame = camera.read() camera.release() time.sleep(1) while True: camera = cv2.VideoCapture(-1) _, frame = camera.read() camera.release() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades +
from firebase_admin import storage from pyrebase.pyrebase import Firebase from PIL import Image config = { "apiKey": "AIzaSyCbwjD-zqfeO2-5hsHIhF6jHoFTnzFuGQg", "authDomain": "project-1085473150640.firebaseapp.com", "serviceAccount": "/Users/goncalocardoso/PycharmProjects/OpenCV/tese-3ed7d-firebase-adminsdk-4xm5b-3176ad9835.json", "databaseURL": "https://tese-3ed7d.firebaseio.com/", "storageBucket": "tese-3ed7d.appspot.com" } firebase = Firebase(config) storage = firebase.storage() storage.child("images").download("downloaded.jpg") #image = Image.open(r"downloaded.jpg") #crop = image.crop((655, 1841, 835, 2034)) #crop.save('myphoto.jpg', 'JPEG') image = cv2.imread("downloaded.jpg") #image = cv2.imread("/Users/goncalocardoso/Desktop/black.png") hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # define range of blue color in HSV lower_blue = np.array([100, 50, 50]) upper_blue = np.array([130, 255, 255]) lower_green, upper_green = np.array([36, 25, 25]), np.array([70, 255, 255]) # Threshold the HSV image to get only blue colors #mask = cv2.inRange (hsv, np.array([36,25,25]), np.array([70, 255,255])) # mask_blue = cv2.inRange (hsv, lower_blue,upper_blue )
#fileName = 'C:\\Projects\\RubberDuck\\speech-emotion-recognition-ravdess-data\\num25\\audiofile.wav' #bucket = storage.bucket() #blob = bucket.blob(fileName) #blob.upload_from_filename(fileName) config = { "apiKey": "AIzaSyBqEF41l5P_hMo3lkupd8YiJhwPpT3ew4o", "authDomain": "rubberduck-65f9c.firebaseapp.com", "databaseURL": "https://rubberduck-65f9c-default-rtdb.firebaseio.com", "storageBucket": "rubberduck-65f9c.appspot.com" } firebase = Firebase(config) storage = firebase.storage() storage.child("audiofile.wav").download( 'C:\\Projects\\RubberDuck\\speech-emotion-recognition-ravdess-data\\num25\\audiofile.wav' ) #make data usable ownFeature = [load_own()] #ownUsable = ownFeature.reshape(1, -1) #print (loadedModel) #print (ownFeature) #load model modelName = 'C:\\Projects\\RubberDuck\\rubberduckfinal\\rubberduckalg.sav' loadedModel = pickle.load(open(modelName, 'rb')) ownSound = loadedModel.predict(ownFeature) print(ownSound) db = firebase_admin.initialize_app(
def hello(id, du): #!/usr/bin/env python # coding: utf-8 # In[1]: import datetime import pandas as pd import numpy as np import firebase_admin from firebase_admin import credentials from firebase_admin import firestore from firebase_admin import storage import pyrebase from datetime import date, timedelta import urllib.request, json import time #get_ipython().run_line_magic('matplotlib', 'inline') from matplotlib import pyplot as plt import matplotlib.dates as mdates import os import csv from IPython.display import display from Model import trainData import random #from google.cloud import storage from matplotlib.patches import Ellipse import seaborn as sns # signal processing from scipy import signal from scipy.ndimage import label from scipy.stats import zscore from scipy.interpolate import interp1d from scipy.integrate import trapz # misc import warnings #generate pdf from reportlab.pdfgen import canvas from reportlab.lib.colors import Color, lightblue, black, HexColor # In[2]: cred = credentials.Certificate( "/Users/raghadaziz/Desktop/GP2/SereneReports/SereneReport/serene-firebase-adminsdk.json" ) app = firebase_admin.initialize_app( cred, { 'storageBucket': 'serene-2dfd6.appspot.com', }, name='[DEFAULT]') db = firestore.client() # In[3]: duration = du userID = id #"UqTdL3T7MteuQHBe1aNfSE9u0Na2" # In[4]: today = datetime.datetime.now() timestamp = today.strftime("%Y-%m-%d %H:%M:%S") bucket = storage.bucket(app=app) # ## Get data from storage and get list of dates # In[5]: dates = [] for x in range(0, duration): today = date.today() yesterday = today - datetime.timedelta(days=1) start_date = (yesterday - timedelta(days=duration - x)).isoformat() dates.append(start_date) # In[6]: df = pd.DataFrame() # loop through the storage and get the data sleep = [] for x in range(0, len(dates)): #Sleep blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-sleep.json") # download the file u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) sleepMinutes = data['summary']["totalMinutesAsleep"] except: pass #Activity (Steps) blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-activity.json") # download the file u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) steps = data['summary']["steps"] except: pass #heartrate blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-heartrate.json") u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) df_heartrate = pd.DataFrame( data['activities-heart-intraday']['dataset']) df_heartrate.time.apply(str) df_heartrate['time'] = pd.to_datetime(df_heartrate['time']) df_heartrate['hour'] = df_heartrate['time'].apply( lambda time: time.strftime('%H')) df_heartrate.drop(['time'], axis=1, inplace=True) heart_rate = df_heartrate.groupby(["hour"], as_index=False).mean() heart_rate['sleepMin'] = sleepMinutes heart_rate['TotalSteps'] = steps heart_rate['date'] = dates[x] heart_rate = heart_rate.astype({"hour": int}) except: pass # append dataframe df = df.append(heart_rate, ignore_index=True) # In[7]: df # ### Get user location # In[8]: # get location from database loc_df = pd.DataFrame() locID = [] locations = db.collection(u'PatientLocations').where( u'patientID', u'==', userID).stream() for location in locations: loc = location.to_dict() locID.append(location.id) loc_df = loc_df.append(pd.DataFrame(loc, index=[0]), ignore_index=True) loc_df['id'] = locID # In[9]: loc_df.drop(['anxietyLevel', 'lat', 'lng', 'patientID'], axis=1, inplace=True) # In[10]: loc_df.time.apply(str) loc_df['time'] = pd.to_datetime(loc_df['time']) loc_df['date'] = pd.to_datetime(loc_df['time'], format='%Y:%M:%D').dt.date loc_df['hour'] = loc_df['time'].apply(lambda time: time.strftime('%H')) loc_df.drop(['time'], axis=1, inplace=True) loc_df.hour = loc_df.hour.astype(int) loc_df.date = loc_df.date.astype(str) df.date = df.date.astype(str) # In[11]: dfinal = pd.merge(left=df, right=loc_df, how='left', left_on=['hour', 'date'], right_on=['hour', 'date']).ffill() # ### Test data into model # In[12]: #test model train_df = dfinal.rename(columns={'value': 'Heartrate'}) # In[13]: Labeled_df = pd.DataFrame() Labeled_df = trainData(train_df) # In[14]: Labeled_df.drop(['lon'], axis=1, inplace=True) # In[15]: # Replace missing values because it doesn't exist Labeled_df['name'].fillna("Not given", inplace=True) Labeled_df['id'].fillna("Not given", inplace=True) # In[16]: # Update firebase with the user anxiety level for row in Labeled_df.itertuples(): if row.id != 'Not given': if row.Label == 'Low' or row.Label == 'LowA': anxietyLevel = 1 elif row.Label == 'Meduim': anxietyLevel = 2 else: anxietyLevel = 3 doc_ref = db.collection(u'PatientLocations').document(row.id) doc_ref.update({u'anxietyLevel': anxietyLevel}) # ### Show the places with highest anxiety level # In[17]: # Show the highest level df_high = pd.DataFrame() df_high = Labeled_df[Labeled_df.Label == 'High'] # In[18]: df_high.head(5) # # Improvements # # Recommendation # In[19]: docDf = pd.DataFrame() doc_ref = db.collection(u'Patient').document(userID) doc = doc_ref.get().to_dict() docDf = docDf.append(pd.DataFrame(doc, index=[0]), ignore_index=True) # In[20]: age1 = docDf['age'].values name1 = docDf['name'].values emp1 = docDf['employmentStatus'].values mar1 = docDf['maritalStatus'].values income1 = docDf['monthlyIncome'].values chronicD1 = docDf['chronicDiseases'].values smoke1 = docDf['smokeCigarettes'].values gad1 = docDf['GAD-7ScaleScore'].values age = age1[0] name = name1[0] emp = emp1[0] mar = mar1[0] income = income1[0] chronicD = chronicD1[0] smoke = smoke1[0] gad = gad1[0] compareAge = int(age) # In[21]: sleepMin = Labeled_df['sleepMin'].mean() totalSteps = Labeled_df['TotalSteps'].mean() sleepRecomendation = False stepsRecomendation = False recomendedSteps = 'No recomendation' if sleepMin < 360: sleepRecomendation = True if compareAge < 20 and compareAge > 11: if totalSteps < 6000: stepsRecomendation = True recomendedSteps = '6000' if compareAge < 66 and compareAge > 19: if totalSteps < 3000: stepsRecomendation = True recomendedSteps = '3000' sleepMin = sleepMin / 60 float("{:.2f}".format(sleepMin)) float("{:.2f}".format(totalSteps)) # In[22]: # store recomendation in database ID = random.randint(1500000, 10000000) doc_rec = db.collection(u'LastGeneratePatientReport').document(str(ID)) doc_rec.set({ u'steps': totalSteps, u'patientID': userID, u'sleepMin': sleepMin, u'sleepRecomendation': sleepRecomendation, u'stepsRecomendation': stepsRecomendation, u'recommended_steps': recomendedSteps }) # ## Storage intilization # In[113]: firebaseConfig = { "apiKey": "AIzaSyBoxoXwFm9TuFysjQYag0GB1NEPyBINlTU", "authDomain": "serene-2dfd6.firebaseapp.com", "databaseURL": "https://serene-2dfd6.firebaseio.com", "projectId": "serene-2dfd6", "storageBucket": "serene-2dfd6.appspot.com", "messagingSenderId": "461213981433", "appId": "1:461213981433:web:62428e3664182b3e58e028", "measurementId": "G-J66VP2Y3CR" } firebase = pyrebase.initialize_app(firebaseConfig) storage = firebase.storage() # # AL # In[114]: # Change Label values to num, to represent them in a barchart nums = [] for row in Labeled_df.itertuples(): if row.Label == 'Low' or row.Label == 'LowA': nums.append(1) elif row.Label == 'Meduim': nums.append(2) else: nums.append(3) Labeled_df['numLabel'] = nums # In[115]: # Get anxiety level by day and store it in a new data frame plot_df = pd.DataFrame() avgAnxiety = [] totalAnxiety = 0 rowCount = 1 for x in range(0, len(dates)): for row in Labeled_df.itertuples(): if (row.date == dates[x]): rowCount += 1 totalAnxiety += row.numLabel avgAnxiety.append(totalAnxiety / rowCount) plot_df['date'] = dates plot_df['Anxiety'] = avgAnxiety # ## To generate graphs for Android application # In[116]: #divide dataframe into 15 rows (2 weeks) df1 = pd.DataFrame() df2 = pd.DataFrame() df3 = pd.DataFrame() df4 = pd.DataFrame() df5 = pd.DataFrame() df6 = pd.DataFrame() df7 = pd.DataFrame() df8 = pd.DataFrame() df9 = pd.DataFrame() df10 = pd.DataFrame() df11 = pd.DataFrame() df12 = pd.DataFrame() dfarray = [] count = 0 if (len(plot_df) > 15): df1 = plot_df[:15] df2 = plot_df[15:] dfarray.append(df1) dfarray.append(df2) if (len(df2) > 15): count = (df2.last_valid_index() - (len(df2) - 15)) df3 = df2[count:] dfarray.append(df3) if (len(df3) > 15): count = (df3.last_valid_index() - (len(df3) - 15)) df4 = df3[count:] dfarray.append(df4) if (len(df4) > 15): count = (df4.last_valid_index() - (len(df4) - 15)) df5 = df4[count:] dfarray.append(df5) if (len(df5) > 15): count = (df5.last_valid_index() - (len(df5) - 15)) df6 = df5[count:] dfarray.append(df6) if (len(df6) > 15): count = (df6.last_valid_index() - (len(df6) - 15)) df7 = df6[count:] dfarray.append(df7) if (len(df7) > 15): count = (df7.last_valid_index() - (len(df7) - 15)) df8 = df7[count:] dfarray.append(df8) if (len(df8) > 15): count = (df8.last_valid_index() - (len(df8) - 15)) df9 = df8[count:] dfarray.append(df9) if (len(df9) > 15): count = (df9.last_valid_index() - (len(df9) - 15)) df10 = df9[count:] dfarray.append(df10) if (len(df10) > 15): count = (df10.last_valid_index() - (len(df10) - 15)) df11 = df10[count:] dfarray.append(df11) if (len(df11) > 15): count = ( df11.last_valid_index() - (len(df11) - 15)) df12 = df11[count:] dfarray.append(df12) # In[117]: # Plot AL if (len(plot_df) < 15): fig, ax = plt.subplots() # Draw the stem and circle ax.stem(plot_df.date, plot_df.Anxiety, basefmt=' ') plt.tick_params(axis='x', rotation=70) # Start the graph at 0 ax.set_ylim(0, 3) ax.set_title('Anxiety level (Throughout week)') plt.xlabel('Date') plt.ylabel('Low Meduim High', fontsize=12) ax.yaxis.set_label_coords(-0.1, 0.47) (markers, stemlines, baseline) = plt.stem(plot_df.date, plot_df.Anxiety) plt.setp(stemlines, linestyle="-", color="#4ba0d1", linewidth=2) plt.setp(markers, marker='o', markersize=5, markeredgecolor="#4ba0d1", markeredgewidth=1) plt.setp(baseline, linestyle="-", color="#4ba0d1", linewidth=0) conv = str(x) fig.savefig('AL.png', dpi=100) imagePath = 'AL.png' storage.child(userID + "/lastGeneratedPatientReport/AL.png").put('AL.png') os.remove('AL.png') else: for x in range(0, len(dfarray)): fig, ax = plt.subplots() # Draw the stem and circle ax.stem(dfarray[x].date, dfarray[x].Anxiety, basefmt=' ') plt.tick_params(axis='x', rotation=70) # Start the graph at 0 ax.set_ylim(0, 3) ax.set_title('Anxiety level (Throughout week)') plt.xlabel('Date') plt.ylabel('Low Meduim High', fontsize=12) ax.yaxis.set_label_coords(-0.1, 0.47) (markers, stemlines, baseline) = plt.stem(dfarray[x].date, dfarray[x].Anxiety) plt.setp(stemlines, linestyle="-", color="#4ba0d1", linewidth=2) plt.setp(markers, marker='o', markersize=5, markeredgecolor="#4ba0d1", markeredgewidth=1) plt.setp(baseline, linestyle="-", color="#4ba0d1", linewidth=0) conv = str(x) fig.savefig('ALP' + str(x) + '.png', dpi=100) imagePath = 'ALP' + str(x) + '.png' storage.child(userID + "/lastGeneratedPatientReport/ALP" + str(x) + '.png').put('ALP' + str(x) + '.png') os.remove('ALP' + str(x) + '.png') # ## To generate graphs for PDF report # In[108]: df1 = pd.DataFrame() df2 = pd.DataFrame() dfarray = [] count = 0 if (len(plot_df) > 90): df1 = plot_df[:90] df2 = plot_df[90:] dfarray.append(df1) dfarray.append(df2) # In[111]: # Plot AL if (len(plot_df) <= 90): fig, ax = plt.subplots() # Draw the stem and circle ax.stem(plot_df.date, plot_df.Anxiety, basefmt=' ') plt.tick_params(axis='x', rotation=70) # Start the graph at 0 ax.set_ylim(0, 3) ax.set_title('Anxiety level (Throughout week)') plt.xlabel('Date') plt.ylabel('Low Meduim High', fontsize=12) ax.yaxis.set_label_coords(-0.1, 0.47) (markers, stemlines, baseline) = plt.stem(plot_df.date, plot_df.Anxiety) plt.setp(stemlines, linestyle="-", color="#4ba0d1", linewidth=2) plt.setp(markers, marker='o', markersize=5, markeredgecolor="#4ba0d1", markeredgewidth=1) plt.setp(baseline, linestyle="-", color="#4ba0d1", linewidth=0) conv = str(x) fig.savefig('ALpdf.png', dpi=100) else: for x in range(0, len(dfarray)): fig, ax = plt.subplots() # Draw the stem and circle ax.stem(dfarray[x].date, dfarray[x].Anxiety, basefmt=' ') plt.tick_params(axis='x', rotation=70) # Start the graph at 0 ax.set_ylim(0, 3) ax.set_title('Anxiety level (Throughout week)') plt.xlabel('Date') plt.ylabel('Low Meduim High', fontsize=12) ax.yaxis.set_label_coords(-0.1, 0.47) (markers, stemlines, baseline) = plt.stem(dfarray[x].date, dfarray[x].Anxiety) plt.setp(stemlines, linestyle="-", color="#4ba0d1", linewidth=2) plt.setp(markers, marker='o', markersize=5, markeredgecolor="#4ba0d1", markeredgewidth=1) plt.setp(baseline, linestyle="-", color="#4ba0d1", linewidth=0) fig.savefig('AL' + str(x) + 'pdf.png', dpi=100) # # Location Analysis # In[41]: loc = pd.DataFrame() loc = Labeled_df[Labeled_df.name != 'Not given'] # In[42]: loc.drop(['Heartrate', 'sleepMin', 'TotalSteps', 'id'], axis=1, inplace=True) # In[43]: names = [] Name = "" for row in loc.itertuples(): Name = row.name names.append(Name) # In[44]: new_name = pd.DataFrame() new_name['name'] = names # In[45]: new_name = new_name.drop_duplicates() # In[46]: new_name # In[47]: fnames = [] fName = "" for row in new_name.itertuples(): fName = row.name fnames.append(fName) # In[61]: analysis = pd.DataFrame() count = 0 i = 0 label = "" locationName = "" counts = [] labels = [] locationNames = [] for x in range(0, len(fnames)): count = 0 locName = fnames[i] for row in loc.itertuples(): if (locName == row.name): if (row.Label == 'High'): count += 1 label = row.Label locationName = row.name i += 1 counts.append(count) labels.append(label) locationNames.append(locationName) analysis['Location'] = locationNames analysis['Frequency'] = counts analysis['Anxiety Level'] = labels # In[62]: analysis # In[63]: newA = analysis.drop(analysis[analysis['Frequency'] == 0].index, inplace=True) # In[64]: analysis # In[65]: import six # In[66]: def render_mpl_table(data, col_width=5.0, row_height=0.625, font_size=14, header_color='#23495f', row_colors=['#e1eff7', 'w'], edge_color='#23495f', bbox=[0, 0, 1, 1], header_columns=0, ax=None, **kwargs): if ax is None: size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array( [col_width, row_height]) fig, ax = plt.subplots(figsize=size) ax.axis('off') mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, cellLoc='center', **kwargs) mpl_table.auto_set_font_size(False) mpl_table.set_fontsize(font_size) for k, cell in six.iteritems(mpl_table._cells): cell.set_edgecolor(edge_color) if k[0] == 0 or k[1] < header_columns: cell.set_text_props(weight='bold', color='w') cell.set_facecolor(header_color) else: cell.set_facecolor(row_colors[k[0] % len(row_colors)]) cell.alignment = 'center' fig.savefig('Location.png', dpi=100) return ax # In[67]: if (len(analysis) > 0): render_mpl_table(analysis, header_columns=0, col_width=4) # # Genertate patient report and save it in storage # In[71]: pdf = canvas.Canvas('Patient.pdf') pdf.setTitle('Patient report') #sleepRecomendation #recomendedSteps pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFillColor(HexColor('#e1eff7')) pdf.roundRect(57, 400, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Helvetica-Bold", 16) pdf.setFillColor(HexColor('#23495f')) pdf.drawString( 115, 570, "Report Duration From: " + dates[0] + " To: " + dates[len(dates) - 1]) pdf.setFont("Helvetica-Bold", 15) pdf.drawString(250, 540, "Improvments: ") pdf.drawString(200, 500, "Highest day of anxiety level: ") pdf.setFillColor(HexColor('#e1eff7')) pdf.roundRect(57, 160, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Helvetica-Bold", 16) pdf.setFillColor(HexColor('#23495f')) pdf.drawString(130, 330, "Recommendations: ") pdf.drawString(150, 300, "Sleep Recomendation: ") pdf.drawString(150, 260, "Steps Recomendation: ") pdf.setFont("Helvetica", 16) pdf.setFillColor(black) if (sleepRecomendation == True): pdf.drawString(180, 280, "we reccomend you to sleep from 7-9 hours") else: pdf.drawString(180, 280, "keep up the good work") if (stepsRecomendation == True): pdf.drawString(180, 240, "we reccomend you to walk at least " + recomendedSteps) else: pdf.drawString(180, 240, "keep up the good work") pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(HexColor('#808080')) pdf.drawString(100, 650, "Anxiety Level") if (len(plot_df) <= 90): pdf.drawImage("ALpdf.png", 57, 400, width=485, height=200) pdf.drawString(100, 350, "Location Analysis") if (len(analysis) > 0): pdf.drawImage("Location.png", 57, 100, width=485, height=200) else: pdf.setFont("Helvetica", 15) pdf.setFillColor(HexColor('#23495f')) t = pdf.beginText(130, 250) text = [ name + " condition was stable through this period,", "no locations with high anxiety level were detected." ] for line in text: t.textLine(line) pdf.drawText(t) pdf.showPage() else: j = 400 for x in range(0, len(dfarray)): pdf.drawImage('AL' + str(x) + 'pdf.png', 57, j, width=485, height=200) j = j - 300 pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(HexColor('#808080')) pdf.drawString(100, 650, "Location Analysis") if (len(analysis) > 0): pdf.drawImage("Location.png", 57, 400, width=485, height=200) else: pdf.setFont("Helvetica", 15) pdf.setFillColor(HexColor('#23495f')) t = pdf.beginText(130, 550) text = [ name + " condition was stable through this period,", "no locations with high anxiety level were detected." ] for line in text: t.textLine(line) pdf.drawText(t) pdf.save() # In[ ]: #new method doct = storage.child(userID + "/lastGeneratedPatientReport/patientReport").put( 'Patient.pdf') # In[73]: os.remove('Patient.pdf') if (len(plot_df) <= 90): os.remove('ALpdf.png') else: for x in range(0, len(dfarray)): os.remove('AL' + str(x) + 'pdf.png') # In[ ]: return "HI"
def reportP(pid, dates, is_true): import datetime import pandas as pd import numpy as np import firebase_admin from firebase_admin import credentials from firebase_admin import firestore from firebase_admin import storage import pyrebase from datetime import date, timedelta import urllib.request, json import time from matplotlib import pyplot as plt import matplotlib.dates as mdates import os import csv from IPython.display import display from Model import trainData import random from matplotlib.patches import Ellipse import matplotlib.patches as mpatches import seaborn as sns # signal processing from scipy import signal from scipy.ndimage import label from scipy.stats import zscore from scipy.interpolate import interp1d from scipy.integrate import trapz # misc import warnings #generate pdf from reportlab.pdfgen import canvas from reportlab.lib.colors import Color, lightblue, black # In[2]: if not firebase_admin._apps: cred = credentials.Certificate("serene-firebase-adminsdk.json") app = firebase_admin.initialize_app( cred, { 'storageBucket': 'serene-2dfd6.appspot.com', }, name='[DEFAULT]') else: app = firebase_admin.get_app() db = firestore.client() # In[3]: userID = pid GoogleCalendar = is_true # In[4]: today = datetime.datetime.now() timestamp = today.strftime("%Y-%m-%d %H:%M:%S") bucket = storage.bucket(app=app) # ## Get data from storage and get list of dates # In[5]: df = pd.DataFrame() notAvailableDates = [] # loop through the storage and get the data sleep = [] for x in range(0, len(dates)): #Sleep blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-sleep.json") #download the file u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) sleepMinutes = data['summary']["totalMinutesAsleep"] except: notAvailableDates.append(dates[x]) pass #Activity (Steps) blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-activity.json") #download the file u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) steps = data['summary']["steps"] except: notAvailableDates.append(dates[x]) pass #heartrate blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-heartrate.json") u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) df_heartrate = pd.DataFrame( data['activities-heart-intraday']['dataset']) df_heartrate.time.apply(str) df_heartrate['time'] = pd.to_datetime(df_heartrate['time']) df_heartrate['hour'] = df_heartrate['time'].apply( lambda time: time.strftime('%H')) df_heartrate.drop(['time'], axis=1, inplace=True) heart_rate = df_heartrate.groupby(["hour"], as_index=False).mean() heart_rate['sleepMin'] = sleepMinutes heart_rate['TotalSteps'] = steps heart_rate['date'] = dates[x] heart_rate = heart_rate.astype({"hour": int}) except: notAvailableDates.append(dates[x]) pass #append dataframe df = df.append(heart_rate, ignore_index=True) notAvailableDates notSyncedDates = pd.DataFrame() notSyncedDates['date'] = notAvailableDates # In[16]: notSyncedDates = notSyncedDates.drop_duplicates() # ### Get user location # In[17]: # get location from database loc_df = pd.DataFrame() locID = [] locations = db.collection(u'PatientLocations').where( u'patientID', u'==', userID).stream() for location in locations: loc = location.to_dict() locID.append(location.id) loc_df = loc_df.append(pd.DataFrame(loc, index=[0]), ignore_index=True) loc_df['id'] = locID # In[18]: loc_df.drop(['anxietyLevel', 'lat', 'lng', 'patientID'], axis=1, inplace=True) # In[19]: loc_df.time.apply(str) loc_df['time'] = pd.to_datetime(loc_df['time']) loc_df['date'] = pd.to_datetime(loc_df['time'], format='%Y:%M:%D').dt.date loc_df['hour'] = loc_df['time'].apply(lambda time: time.strftime('%H')) loc_df.drop(['time'], axis=1, inplace=True) loc_df.hour = loc_df.hour.astype(int) loc_df.date = loc_df.date.astype(str) df.date = df.date.astype(str) # In[20]: dfinal = pd.merge(left=df, right=loc_df, how='left', left_on=['hour', 'date'], right_on=['hour', 'date']).ffill() # ### Test data into model # In[23]: #test model train_df = dfinal.rename(columns={'value': 'Heartrate'}) # In[24]: Labeled_df = pd.DataFrame() Labeled_df = trainData(train_df) # In[25]: Labeled_df.drop(['lon'], axis=1, inplace=True) # In[26]: # Replace missing values because it doesn't exist Labeled_df['name'].fillna("Not given", inplace=True) Labeled_df['id'].fillna("Not given", inplace=True) #Labeled_df['anxiety_assigned'].fillna('Not given', inplace = True) # In[27]: # In[28]: # Update firebase with the user anxiety level for row in Labeled_df.itertuples(): if row.id != 'Not given': if row.Label == 'Low' or row.Label == 'LowA': anxietyLevel = '1' elif row.Label == 'Meduim': anxietyLevel = '2' else: anxietyLevel = '3' if ((row.anxiety_assigned == False) or (row.anxiety_assigned == 'Not given')): doc_ref = db.collection(u'PatientLocations').document(row.id) doc_ref.update({ u'anxietyLevel': anxietyLevel, u'anxiety_assigned': True }) # ### Show the places with highest anxiety level # In[29]: # Show the highest level df_high = pd.DataFrame() df_high = Labeled_df[Labeled_df.Label == 'High'] # # Recommendation # In[30]: docDf = pd.DataFrame() doc_ref = db.collection(u'Patient').document(userID) doc = doc_ref.get().to_dict() docDf = docDf.append(pd.DataFrame(doc, index=[0]), ignore_index=True) # In[31]: age1 = docDf['age'].values name1 = docDf['name'].values emp1 = docDf['employmentStatus'].values mar1 = docDf['maritalStatus'].values income1 = docDf['monthlyIncome'].values chronicD1 = docDf['chronicDiseases'].values smoke1 = docDf['smokeCigarettes'].values gad1 = docDf['GAD-7ScaleScore'].values gender1 = docDf['gender'].values age = age1[0] name = name1[0] emp = emp1[0] mar = mar1[0] income = income1[0] chronicD = chronicD1[0] smoke = smoke1[0] gad = gad1[0] gender = gender1[0] compareAge = int(age) # In[32]: sleepMin = Labeled_df['sleepMin'].mean() totalSteps = Labeled_df['TotalSteps'].astype(float).mean() sleepRecomendation = False stepsRecomendation = False recomendedSteps = 'No recomendation' if sleepMin < 360: sleepRecomendation = True if compareAge < 20 and compareAge > 11: if totalSteps < 6000: stepsRecomendation = True recomendedSteps = '6000' if compareAge < 66 and compareAge > 19: if totalSteps < 3000: stepsRecomendation = True recomendedSteps = '3000' sleepMin = sleepMin / 60 sleepMin = float("{:.1f}".format(sleepMin)) totalSteps = int(totalSteps) # ## Storage intilization # In[33]: firebaseConfig = { "apiKey": "AIzaSyBoxoXwFm9TuFysjQYag0GB1NEPyBINlTU", "authDomain": "serene-2dfd6.firebaseapp.com", "databaseURL": "https://serene-2dfd6.firebaseio.com", "projectId": "serene-2dfd6", "storageBucket": "serene-2dfd6.appspot.com", "messagingSenderId": "461213981433", "appId": "1:461213981433:web:62428e3664182b3e58e028", "measurementId": "G-J66VP2Y3CR" } firebase = pyrebase.initialize_app(firebaseConfig) storage = firebase.storage() # # AL # In[34]: sns.set(rc={'axes.facecolor': '#fcfeff'}) # In[35]: # Change Label values to num, to represent them in a barchart nums = [] for row in Labeled_df.itertuples(): if row.Label == 'Low' or row.Label == 'LowA': nums.append(1) elif row.Label == 'Meduim': nums.append(2) else: nums.append(3) Labeled_df['numLabel'] = nums # In[36]: # Get anxiety level by day and store it in a new data frame plot_df = pd.DataFrame() avgAnxiety = [] totalAnxiety = 0 rowCount = 1 for x in range(0, len(dates)): totalAnxiety = 0 rowCount = 1 for row in Labeled_df.itertuples(): if (row.date == dates[x]): rowCount += 1 totalAnxiety += row.numLabel avgAnxiety.append(totalAnxiety / rowCount) plot_df['date'] = dates plot_df['Anxiety'] = avgAnxiety # In[37]: #divide dataframe into 15 rows (2 weeks) max is 3 months df1 = pd.DataFrame() df2 = pd.DataFrame() df3 = pd.DataFrame() df4 = pd.DataFrame() df5 = pd.DataFrame() df6 = pd.DataFrame() dfarray = [] count = 0 if (len(plot_df) > 15): df1 = plot_df[:15] df2 = plot_df[15:] dfarray.append(df1) dfarray.append(df2) if (len(df2) > 15): count = (df2.last_valid_index() - (len(df2) - 15)) df3 = df2[count:] dfarray.append(df3) if (len(df3) > 15): count = (df3.last_valid_index() - (len(df3) - 15)) df4 = df3[count:] dfarray.append(df4) if (len(df4) > 15): count = (df4.last_valid_index() - (len(df4) - 15)) df5 = df4[count:] dfarray.append(df5) if (len(df5) > 15): count = (df5.last_valid_index() - (len(df5) - 15)) df6 = df5[count:] dfarray.append(df6) # In[38]: # Plot AL if (len(plot_df) <= 15): fig, ax = plt.subplots() c1 = '#9dd6f5' c2 = '#4ba0d1' c3 = '#23495f' bar_width = 0.25 for t, y in zip(plot_df["date"], plot_df["Anxiety"]): c = "" if (y <= 1): c = c1 elif (1 < y <= 2): c = c2 elif (y > 2): c = c3 plt.bar([t, t], [0, y], bar_width, color=c) colors = [[c1, c1], [c2, c2], [c3, c3]] categories = ['Low', 'Meduim', 'High'] #create dict legend_dict = dict(zip(categories, colors)) #create patches patchList = [] for key in legend_dict: data_key = mpatches.Patch(facecolor=legend_dict[key][0], edgecolor=legend_dict[key][1], label=key) patchList.append(data_key) ax.legend(handles=patchList, ncol=len(categories), fontsize=12) plt.tick_params(axis='x', rotation=70) # Start the graph at 0 ax.set_ylim(0, 3) #fig.set_size_inches(15.5, 10) plt.tight_layout() plt.xlabel('Date') ax.yaxis.set_label_coords(-0.02, 0.48) fig.savefig('AL0.png', dpi=None) imagePath = 'AL0.png' link = storage.child( userID + "/lastGeneratedPatientReport/AL0.png").put('AL0.png') os.remove('AL0.png') else: links = [] for x in range(0, len(dfarray)): fig, ax = plt.subplots() c1 = '#9dd6f5' c2 = '#4ba0d1' c3 = '#23495f' bar_width = 0.25 for t, y in zip(dfarray[x]["date"], dfarray[x]["Anxiety"]): c = "" if (y <= 1): c = c1 elif (1 < y <= 2): c = c2 elif (y > 2): c = c3 plt.bar([t, t], [0, y], bar_width, color=c) colors = [[c1, c1], [c2, c2], [c3, c3]] categories = ['Low', 'Meduim', 'High'] #create dict legend_dict = dict(zip(categories, colors)) #create patches patchList = [] for key in legend_dict: data_key = mpatches.Patch(facecolor=legend_dict[key][0], edgecolor=legend_dict[key][1], label=key) patchList.append(data_key) ax.legend(handles=patchList, ncol=len(categories), fontsize=12) plt.tick_params(axis='x', rotation=70) # Start the graph at 0 ax.set_ylim(0, 3) #fig.set_size_inches(15.5, 10) plt.tight_layout() plt.xlabel('Date') ax.yaxis.set_label_coords(-0.02, 0.48) fig.savefig('AL' + str(x) + '.png', dpi=None) imagePath = 'AL' + str(x) + '.png' link = storage.child(userID + "/lastGeneratedPatientReport/AL" + str(x) + '.png').put('AL' + str(x) + '.png') links.append(link) os.remove('AL' + str(x) + '.png') # ### save file to database first # In[39]: if (len(plot_df) <= 15): linkDF = pd.DataFrame() linkDF = linkDF.append(pd.DataFrame(link, index=[0]), ignore_index=True) token1 = linkDF['downloadTokens'].values token = token1[0] AlLink = storage.child( userID + "/lastGeneratedPatientReport/AL0.png").get_url(token) doc_rec = db.collection(u'LastGeneratePatientReport').document( 'report' + userID) doc_rec.set({ u'average_steps': totalSteps, u'patient_id': userID, u'average_sleep_hours': sleepMin, u'sleepRecomendation': sleepRecomendation, u'stepsRecomendation': stepsRecomendation, u'recommended_steps': recomendedSteps, u'number_of_AL_graphs': 1, u'AL_graph_0': AlLink, u'recommended_sleep_hours': '7-9' }) else: firebase_links = [] for x in range(0, len(links)): linkDF = pd.DataFrame() linkDF = linkDF.append(pd.DataFrame(link, index=[0]), ignore_index=True) token1 = linkDF['downloadTokens'].values token = token1[0] AlLink = storage.child(userID + "/lastGeneratedPatientReport/AL" + str(x) + '.png').get_url(token) firebase_links.append(AlLink) doc_rec = db.collection(u'LastGeneratePatientReport').document( 'report' + userID) doc_rec.set({ u'average_steps': totalSteps, u'patient_id': userID, u'average_sleep_hours': sleepMin, u'sleepRecomendation': sleepRecomendation, u'stepsRecomendation': stepsRecomendation, u'recommended_steps': recomendedSteps, u'number_of_AL_graphs': len(links), u'recommended_sleep_hours': '7-9' }) for x in range(0, len(links)): doc_rec = db.collection(u'LastGeneratePatientReport').document( 'report' + userID) doc_rec.update({u'AL_graph_' + str(x): firebase_links[x]}) # ## To generate graphs for PDF report # In[40]: fig, ax = plt.subplots() c1 = '#9dd6f5' c2 = '#4ba0d1' c3 = '#23495f' bar_width = 0.25 for t, y in zip(plot_df["date"], plot_df["Anxiety"]): c = "" if (y <= 1): c = c1 elif (1 < y <= 2): c = c2 elif (y > 2): c = c3 plt.bar([t, t], [0, y], bar_width, color=c) colors = [[c1, c1], [c2, c2], [c3, c3]] categories = ['Low', 'Meduim', 'High'] #create dict legend_dict = dict(zip(categories, colors)) #create patches patchList = [] for key in legend_dict: data_key = mpatches.Patch(facecolor=legend_dict[key][0], edgecolor=legend_dict[key][1], label=key) patchList.append(data_key) ax.legend(handles=patchList, ncol=len(categories), fontsize=12) plt.tick_params(axis='x', rotation=70) # Start the graph at 0 ax.set_ylim(0, 3) fig.set_size_inches(15.5, 10) plt.tight_layout() plt.xlabel('Date') ax.yaxis.set_label_coords(-0.02, 0.48) fig.savefig('ALpdf.png', dpi=None) # # Location Analysis # In[41]: # get location from database new_loc = pd.DataFrame() locID = [] locations = db.collection(u'PatientLocations').where( u'patientID', u'==', userID).stream() for location in locations: loc = location.to_dict() locID.append(location.id) new_loc = new_loc.append(pd.DataFrame(loc, index=[0]), ignore_index=True) new_loc['id'] = locID # In[42]: new_loc.time.apply(str) new_loc['time'] = pd.to_datetime(new_loc['time']) new_loc['date'] = pd.to_datetime(new_loc['time'], format='%Y:%M:%D').dt.date new_loc.drop(['time'], axis=1, inplace=True) new_loc.date = new_loc.date.astype(str) # In[43]: new_loc = new_loc[(new_loc.date >= dates[0]) & (new_loc.date <= dates[len(dates) - 1])] # In[44]: names = [] Name = "" for row in new_loc.itertuples(): Name = row.nearestLoc names.append(Name) # In[45]: new_name = pd.DataFrame() new_name['name'] = names # In[46]: new_name = new_name.drop_duplicates() new_name.dropna() # In[47]: fnames = [] fName = "" for row in new_name.itertuples(): fName = row.name fnames.append(fName) # In[48]: analysis_EN = pd.DataFrame() analysis_AR = pd.DataFrame() count = 0 i = 0 #label = "" locationName = "" near = '' nearLocs = [] counts = [] labels = [] locationNames = [] for x in range(0, len(fnames)): count = 0 locName = fnames[i] for row in new_loc.itertuples(): if (locName == row.nearestLoc): if (row.anxietyLevel == '3'): count += 1 label = row.anxietyLevel locationName = row.name near = row.nearestLoc i += 1 counts.append(count) #labels.append(label) locationNames.append(locationName) nearLocs.append(near) analysis_EN['Location'] = locationNames analysis_EN['Number of occurrences'] = counts #analysis_EN ['Anxiety Level'] = labels analysis_EN['Nearest Location'] = nearLocs analysis_AR['الموقع'] = locationNames analysis_AR['عدد مرات الزيارة'] = counts #analysis_AR ['مستوى القلق'] = labels analysis_AR['أقرب موقع'] = nearLocs # In[49]: newEn = analysis_EN.drop( analysis_EN[analysis_EN['Number of occurrences'] == 0].index, inplace=True) newAr = analysis_AR.drop( analysis_AR[analysis_AR['عدد مرات الزيارة'] == 0].index, inplace=True) # In[50]: #analysis_EN ['Anxiety Level'] = 'High' #analysis_AR ['مستوى القلق'] = 'مرتفع' # In[51]: import six import arabic_reshaper from googletrans import Translator from bidi.algorithm import get_display from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont # In[52]: def render_mpl_table(data, col_width=5.0, row_height=0.625, font_size=14, tran='', header_color='#23495f', row_colors=['#e1eff7', 'w'], edge_color='#23495f', bbox=[0, 0, 1, 1], header_columns=0, ax=None, **kwargs): if ax is None: size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array( [col_width, row_height]) fig, ax = plt.subplots(figsize=size) ax.axis('off') mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, cellLoc='center', **kwargs) mpl_table.auto_set_font_size(False) mpl_table.set_fontsize(font_size) for k, cell in six.iteritems(mpl_table._cells): cell.set_edgecolor(edge_color) if k[0] == 0 or k[1] < header_columns: cell.set_text_props(weight='bold', color='w') cell.set_facecolor(header_color) else: cell.set_facecolor(row_colors[k[0] % len(row_colors)]) cell.alignment = 'center' fig.savefig(tran + '.png', dpi=100) return ax # In[53]: if (len(analysis_EN) > 0): for ind, row in analysis_EN.iterrows(): analysis_EN.loc[ind, 'Nearest Location'] = get_display( arabic_reshaper.reshape(analysis_EN.loc[ind, 'Nearest Location'])) if (len(analysis_AR) > 0): for ind, row in analysis_AR.iterrows(): analysis_AR.loc[ind, 'أقرب موقع'] = get_display( arabic_reshaper.reshape(analysis_AR.loc[ind, 'أقرب موقع'])) # analysis_AR.loc[ind,'مستوى القلق']=get_display(arabic_reshaper.reshape(analysis_AR.loc[ind,'مستوى القلق'])) analysis_AR = analysis_AR.rename( columns={'الموقع': get_display(arabic_reshaper.reshape('الموقع'))}) analysis_AR = analysis_AR.rename( columns={ 'التكرار': get_display(arabic_reshaper.reshape('التكرار')) }) #analysis_AR = analysis_AR.rename(columns={'مستوى القلق': get_display(arabic_reshaper.reshape('مستوى القلق')) }) analysis_AR = analysis_AR.rename( columns={ 'أقرب موقع': get_display(arabic_reshaper.reshape('أقرب موقع')) }) # In[54]: if (len(analysis_EN) > 0): render_mpl_table(analysis_EN, header_columns=0, col_width=6, tran='Location-EN') if (len(analysis_AR) > 0): render_mpl_table(analysis_AR, header_columns=0, col_width=6, tran='Location-AR') # # Events with highest level of anxiety # In[59]: from dateutil.parser import parse # In[60]: if GoogleCalendar == 'true': events_df = pd.DataFrame() finalEvents_EN = pd.DataFrame() finalEvents_AR = pd.DataFrame() eventID = [] events = db.collection(u'PatientEvents').where(u'patientID', u'==', userID).stream() for event in events: ev = event.to_dict() eventID.append(event.id) events_df = events_df.append(pd.DataFrame(ev, index=[0]), ignore_index=True) events_df['idEvent'] = eventID # In[61]: if len(events_df) > 0: newDates = [] for row in events_df.itertuples(): newDate = parse(row.date) newDates.append(newDate) events_df['newDate'] = newDates events_df = events_df.rename(columns={'name': 'eventName'}) events_df.newDate.apply(str) events_df['date'] = pd.to_datetime(events_df['newDate'], format='%Y:%M:%D').dt.date events_df['hour'] = events_df['newDate'].apply( lambda time: time.strftime('%H')) events_df.hour = events_df.hour.astype(int) events_df.date = events_df.date.astype(str) Test = Labeled_df merge_df = pd.merge(left=Test, right=events_df, how='left', left_on=['hour', 'date'], right_on=['hour', 'date']).ffill() merge_df['eventName'].fillna("Not given", inplace=True) merge_df = merge_df[merge_df.eventName != 'Not given'] #finalEvents_EN = pd.DataFrame() #finalEvents_AR = pd.DataFrame() ev_name = '' evNames = [] #evLabels = [] evDate = [] for row in merge_df.itertuples(): if row.eventName != ev_name: if row.Label == 'High': ev_name = row.eventName #ev_label = row.Label ev_date = row.date if (ev_name != ''): evNames.append(ev_name) #evLabels.append(ev_label) evDate.append(ev_date) finalEvents_EN['Event Name'] = evNames #finalEvents_EN['Anxiety Level'] = evLabels finalEvents_EN['Date'] = evDate finalEvents_AR['اسم الحدث'] = evNames #finalEvents_AR['مستوى القلق'] = evLabels finalEvents_AR['تاريخ الحدث'] = evDate finalEvents_EN = finalEvents_EN.drop_duplicates() finalEvents_AR = finalEvents_AR.drop_duplicates() Final_duplicated = pd.DataFrame() eventId = '' eventIds = [] event_Labels = [] event_names = [] hours = [] for row in merge_df.itertuples(): if eventId != row.idEvent: eventId = row.idEvent if eventId != '': eventIds.append(eventId) event_Labels.append(row.Label) event_names.append(row.eventName) hours.append(row.hour) Final_duplicated['id'] = eventIds Final_duplicated['Label'] = event_Labels Final_duplicated['name'] = event_names Final_duplicated['hour'] = hours Final_duplicated = Final_duplicated.drop_duplicates(subset='id') # Update firebase with the user anxiety level for row in Final_duplicated.itertuples(): if row.Label == 'High': doc_ref = db.collection(u'PatientEvents').document(row.id) doc_ref.update({u'anxietyLevel': '3'}) else: doc_ref = db.collection(u'PatientEvents').document(row.id) doc_ref.delete() checkEvents_df = pd.DataFrame() cEventsID = [] checkEvents = db.collection(u'PatientEvents').where( u'patientID', u'==', userID).stream() for event in checkEvents: cEv = event.to_dict() cEventsID.append(event.id) checkEvents_df = checkEvents_df.append(pd.DataFrame(cEv, index=[0]), ignore_index=True) checkEvents_df['id'] = cEventsID checkEvents_df.fillna("Not given", inplace=True) if len(checkEvents_df) > 0: checkEvents_df = checkEvents_df[checkEvents_df.anxietyLevel == 'Not given'] for row in checkEvents_df.itertuples(): doc_ref = db.collection(u'PatientEvents').document(row.id) doc_ref.delete() # In[62]: if (len(finalEvents_EN) > 0): finalEvents_EN = finalEvents_EN.drop_duplicates() finalEvents_AR = finalEvents_AR.drop_duplicates() # In[63]: if (len(finalEvents_EN) > 0): for ind, row in finalEvents_EN.iterrows(): try: finalEvents_EN.loc[ind, 'Event Name'] = get_display( arabic_reshaper.reshape( finalEvents_EN.loc[ind, 'Event Name'])) except: pass if (len(finalEvents_AR) > 0): #finalEvents_AR['مستوى القلق'] = 'مرتفع' for ind, row in finalEvents_AR.iterrows(): try: finalEvents_AR.loc[ind, 'اسم الحدث'] = get_display( arabic_reshaper.reshape( finalEvents_AR.loc[ind, 'اسم الحدث'])) #finalEvents_AR.loc[ind,'مستوى القلق']=get_display(arabic_reshaper.reshape(finalEvents_AR.loc[ind,'مستوى القلق'])) except: pass finalEvents_AR = finalEvents_AR.rename(columns={ 'اسم الحدث': get_display(arabic_reshaper.reshape('اسم الحدث')) }) #finalEvents_AR = finalEvents_AR.rename(columns={'مستوى القلق': get_display(arabic_reshaper.reshape('مستوى القلق')) }) finalEvents_AR = finalEvents_AR.rename( columns={ 'تاريخ الحدث': get_display(arabic_reshaper.reshape('تاريخ الحدث')) }) # In[64]: if (len(finalEvents_EN) > 0): render_mpl_table(finalEvents_EN, header_columns=0, col_width=4, tran='Events-EN') if (len(finalEvents_AR) > 0): render_mpl_table(finalEvents_AR, header_columns=0, col_width=4, tran='Events-AR') # In[ ]: # # Genertate patient report and save it in storage # In[65]: from reportlab.lib import colors # In[66]: pdfmetrics.registerFont(TTFont('Arabic', 'traditional-arabic/tradbdo.TTF')) # In[67]: pdf = canvas.Canvas('Patient-EN.pdf') pdf.setTitle('Patient report') pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 620, 485, 50, 4, fill=1, stroke=0) pdf.setFont("Helvetica-Bold", 16) pdf.setFillColor(colors.HexColor('#23495f')) pdf.drawString( 115, 638, "Report Duration From: " + dates[0] + " To: " + dates[len(dates) - 1]) pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 400, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#23495f')) pdf.drawString(100, 570, "Patient Information") pdf.setFont("Helvetica-Bold", 15) pdf.drawString(150, 550, "Name: ") pdf.drawString(150, 530, "Age: ") pdf.drawString(150, 510, "Employment Status: ") pdf.drawString(150, 490, "Martial Status: ") pdf.drawString(150, 470, "Monthly Income: ") pdf.drawString(150, 450, "Chronic Diseases: ") pdf.drawString(150, 430, "Cigarette Smoker: ") pdf.drawString(150, 410, "Gender: ") pdf.setFont("Helvetica", 15) pdf.setFillColor(black) pdf.drawString(210, 550, name) pdf.drawString(210, 530, age) pdf.drawString(310, 510, emp) pdf.drawString(260, 490, mar) pdf.drawString(290, 470, income) pdf.drawString(290, 450, chronicD) pdf.drawString(290, 430, smoke) pdf.drawString(220, 410, gender) pdf.setFillColor(colors.HexColor('#bfbfbf')) pdf.roundRect(370, 560, 125, 30, 4, fill=1, stroke=0) pdf.setFillColorRGB(1, 1, 1) pdf.drawString(375, 570, "GAD-7 Score = ") pdf.setFont("Helvetica-Bold", 15) pdf.drawString(480, 570, gad) pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 160, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Helvetica-Bold", 16) pdf.setFillColor(colors.HexColor('#23495f')) pdf.drawString(80, 330, "Recommendations: ") pdf.drawString(100, 300, "Sleep Recomendation: ") pdf.drawString(100, 220, "Steps Recomendation: ") pdf.setFont("Helvetica", 11) pdf.setFillColor(black) if (sleepRecomendation == True): t = pdf.beginText(100, 280) text = [ "It is recommended to sleep at least 7-9 hours per day to get lower chance of anxiety.", "Getting a good night’s sleep can improve your mental well-being and help you to better", "manage your anxiety." ] for line in text: t.textLine(line) pdf.drawText(t) else: t = pdf.beginText(100, 280) text = [ "Good News! It seems you're getting enough sleep!", "Getting a good night’s sleep can improve your mental well-being and help you to better", "manage your anxiety." ] for line in text: t.textLine(line) pdf.drawText(t) if (stepsRecomendation == True): t = pdf.beginText(100, 200) text = [ "It is recommended to walk at least " + recomendedSteps + " steps per day to get lower chance of anxiety.", "Keep in mind that active people have lower rates of anxiety than sedentary people." ] for line in text: t.textLine(line) pdf.drawText(t) else: t = pdf.beginText(100, 200) text = [ "Great Work! You're considered an active person based on your average steps per day.", "Keep in mind that active people have lower rates of anxiety than sedentary people." ] for line in text: t.textLine(line) pdf.drawText(t) pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#808080')) pdf.drawString(100, 650, "Anxiety Level") pdf.drawImage("ALpdf.png", 57, 400, width=485, height=200) pdf.drawString(100, 320, "Locations With Highest Level of Anxiety") if (len(analysis_EN) > 0): pdf.drawImage("Location-EN.png", 30, 200, width=570, height=100) else: pdf.setFont("Helvetica", 15) pdf.setFillColor(colors.HexColor('#23495f')) t = pdf.beginText(130, 250) text = [ name + " condition was stable through this period,", "no locations with high anxiety level were detected." ] for line in text: t.textLine(line) pdf.drawText(t) pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#808080')) pdf.drawString(100, 650, "Events With Highest Level of Anxiety") if (GoogleCalendar == 'true' and len(finalEvents_EN) > 0): pdf.drawImage("Events-EN.png", 30, 500, width=550, height=100) elif (GoogleCalendar == 'true' and len(finalEvents_EN) < 0): pdf.setFont("Helvetica", 15) pdf.setFillColor(colors.HexColor('#23495f')) t = pdf.beginText(130, 550) text = ["No events found associated with high anxiety level"] for line in text: t.textLine(line) pdf.drawText(t) else: pdf.setFont("Helvetica", 15) pdf.setFillColor(colors.HexColor('#23495f')) t = pdf.beginText(130, 550) text = [ name + " have not synced Google Calendar events,", "with thier account." ] for line in text: t.textLine(line) pdf.drawText(t) if (len(notSyncedDates) != 0): pdf.setFont("Helvetica", 12) pdf.setFillColor(colors.HexColor('#d40027')) pdf.drawString( 75, 95, "Note: Below dates are missing, because they were not synced correctly:" ) i = 70 for row in notSyncedDates.itertuples(): pdf.drawString(85, i, '- ' + row.date) i = i - 20 pdf.save() # In[68]: def translate(text): reshaped_text = arabic_reshaper.reshape(text) bidi_text = get_display(reshaped_text) return bidi_text # In[69]: def from_en_to_ar(text): translator = Translator() result = translator.translate(text, dest='ar') return result.text # In[70]: pdf = canvas.Canvas('Patient-AR.pdf') pdf.setTitle('تقرير المريض') pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 620, 485, 50, 4, fill=1, stroke=0) pdf.setFont("Arabic", 22) pdf.setFillColor(colors.HexColor('#23495f')) reportDuration = translate(u'مدة التقرير من') to = translate(u'إلى') pdf.drawString( 125, 638, dates[len(dates) - 1] + ' ' + to + ' ' + dates[0] + ' ' + reportDuration) pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 400, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Arabic", 22) pdf.setFillColor(colors.HexColor('#23495f')) patient = translate(u'معلومات المريض') pdf.drawString(400, 570, patient) na = translate(u'الاسم:') ag = translate(u'العمر:') Es = translate(u'الحالة الوظيفية:') Ms = translate(u'الحالة الإجتماعية:') Mi = translate(u'الدخل الشهري:') Cd = translate(u'الأمراض المزمنة:') Cs = translate(u'مدخن:') ge = translate(u'الجنس:') pdf.setFont("Arabic", 18) pdf.drawString(445, 550, na) pdf.drawString(446, 530, ag) pdf.drawString(400, 510, Es) pdf.drawString(390, 490, Ms) pdf.drawString(400, 470, Mi) pdf.drawString(395, 450, Cd) pdf.drawString(445, 430, Cs) pdf.drawString(446, 410, ge) pdf.setFont("Arabic", 15) pdf.setFillColor(black) pdf.drawString(400, 550, name) pdf.drawString(420, 530, age) pdf.drawString(350, 510, translate(from_en_to_ar(emp))) pdf.drawString(355, 490, translate(from_en_to_ar(mar))) pdf.drawString(360, 470, income) pdf.drawString(360, 450, translate(from_en_to_ar(chronicD))) pdf.drawString(420, 430, translate(from_en_to_ar(smoke))) pdf.drawString(420, 410, translate(from_en_to_ar(gender))) pdf.setFillColor(colors.HexColor('#bfbfbf')) pdf.roundRect(100, 560, 120, 30, 4, fill=1, stroke=0) ga = translate(u'مقياس GAD-7 =') pdf.setFillColorRGB(1, 1, 1) pdf.drawString(120, 570, ga) pdf.setFont("Helvetica-Bold", 15) pdf.drawString(105, 570, gad) pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 160, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Arabic", 22) pdf.setFillColor(colors.HexColor('#23495f')) rec = translate(u' التوصيات:') stepRec = translate(u'توصيات النشاط:') sleepRec = translate(u'توصيات النوم:') pdf.drawString(420, 330, rec) pdf.drawString(380, 300, sleepRec) pdf.drawString(360, 220, stepRec) pdf.setFont("Arabic", 15) pdf.setFillColor(black) if (sleepRecomendation == True): text = translate( u'يوصى بالنوم لمدة تتراوح بين ٧-٩ ساعات في اليوم للحصول على أقل معدل قلق.' ) text2 = translate( u'يمكن أن يحسن النوم الجيد ليلاً من صحتك العقلية ويساعدك على إدارة قلقك بشكل أفضل.' ) pdf.drawString(120, 280, text) pdf.drawString(80, 260, text2) else: text = translate(u'أخبار جيدة! يبدو أنك تحصل على قسط كافٍ من اليوم.') text2 = translate( u'يمكن أن يحسن النوم الجيد ليلاً من صحتك العقلية ويساعدك على إدارة قلقك بشكل أفضل.' ) pdf.drawString(230, 280, text) pdf.drawString(80, 260, text2) if (stepsRecomendation == True): btwnText = translate(u'خطوة في اليوم للحصول على أقل معدل قلق. ') text = translate(u'يوصى بالمشي على الأقل') text2 = translate( u'تذكر أن الأشخاص الأكثر نشاطاً لديهم معدلات قلق أقل من أولئك الغير نشطين.' ) pdf.drawString(130, 200, btwnText + recomendedSteps + text) pdf.drawString(120, 180, text2) else: text = translate( u'عمل رائع! بناءاً على معدل خطواتك في اليوم الواحد، أنت تعتبر من الأشخاص النشطين.' ) text2 = translate( u'تذكر أن الأشخاص الأكثر نشاطاً لديهم معدلات قلق أقل من أولئك الغير نشطين.' ) pdf.drawString(100, 200, text) pdf.drawString(130, 180, text2) pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Arabic", 28) pdf.setFillColor(colors.HexColor('#808080')) AL = translate(u'مستوى القلق') pdf.drawString(400, 650, AL) loc = translate(u'تحليل المواقع ذات مستوى قلق مرتفع ') pdf.drawImage("ALpdf.png", 57, 400, width=485, height=200) pdf.drawString(210, 320, loc) if (len(analysis_AR) > 0): pdf.drawImage("Location-AR.png", 10, 200, width=570, height=100) else: pdf.setFont("Arabic", 18) pdf.setFillColor(colors.HexColor('#23495f')) text = translate( u'كانت الحالة مستقرة خلال هذه الفترة, لم يتم الكشف عن أي مواقع ذات مستوى قلق مرتفع.' ) pdf.drawString(50, 250, text) pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Arabic", 28) pdf.setFillColor(colors.HexColor('#808080')) EV = translate(u'تحليل الأحداث ذات مستوى قلق مرتفع ') pdf.drawString(200, 650, EV) if (GoogleCalendar == 'true' and len(finalEvents_AR) > 0): pdf.drawImage("Events-AR.png", 20, 500, width=550, height=100) elif (GoogleCalendar == 'true' and len(finalEvents_EN) < 0): pdf.setFont("Arabic", 18) pdf.setFillColor(colors.HexColor('#23495f')) text = translate(u' لم يتم الكشف أي أحداث ذات مستوى قلق مرتفع.') pdf.drawString(250, 550, text) else: pdf.setFont("Arabic", 18) pdf.setFillColor(colors.HexColor('#23495f')) text = translate( u'المستخدم لم يقم بمزامنة أحداث تقويم قوقل بحساب سيرين.') pdf.drawString(210, 550, text) if (len(notSyncedDates) != 0): pdf.setFont("Arabic", 15) pdf.setFillColor(colors.HexColor('#d40027')) note = translate( u'.ملاحظة: التواريخ المذكورة أدناه غير متضمنة في التحليل لعدم مزامنة البيانات' ) pdf.drawString(200, 95, note) i = 70 for row in notSyncedDates.itertuples(): pdf.setFont("Helvetica", 12) pdf.drawString(450, i, row.date + ' -') i = i - 20 pdf.save() # In[71]: #new method doct = storage.child(userID + "/lastGeneratedPatientReport/patientReport-EN").put( 'Patient-EN.pdf') doct = storage.child(userID + "/lastGeneratedPatientReport/patientReport-AR").put( 'Patient-AR.pdf') # In[72]: os.remove('Patient-EN.pdf') os.remove('Patient-AR.pdf') if (len(plot_df) <= 90): os.remove('ALpdf.png') else: for x in range(0, len(dfarray)): os.remove('AL' + str(x) + 'pdf.png') if (GoogleCalendar == 'true' and len(finalEvents_AR) > 0): os.remove('Events-AR.png') os.remove('Events-EN.png') if (len(analysis_AR) > 0): os.remove('Location-AR.png') os.remove('Location-EN.png') to_json = plot_df[['date', 'Anxiety']].to_json() return to_json
def reportD(pid): from datetime import datetime, timedelta import pandas as pd import numpy as np import firebase_admin from firebase_admin import credentials from firebase_admin import firestore from firebase_admin import storage import pyrebase import math from datetime import date, timedelta import urllib.request, json import time from matplotlib import pyplot as plt import matplotlib.dates as mdates import os import csv from IPython.display import display from Model import trainData import random #from google.cloud import storage from matplotlib.patches import Ellipse import matplotlib.patches as mpatches import seaborn as sns # signal processing from scipy import signal from scipy.ndimage import label from scipy.stats import zscore from scipy.interpolate import interp1d from scipy.integrate import trapz # misc import warnings #generate pdf from reportlab.pdfgen import canvas from reportlab.lib.colors import Color, lightblue, black # In[35]: if not firebase_admin._apps: cred = credentials.Certificate("serene-firebase-adminsdk.json") app = firebase_admin.initialize_app(cred , { 'storageBucket': 'serene-2dfd6.appspot.com', }, name='[DEFAULT]') else: app = firebase_admin.get_app() db = firestore.client() # In[36]: duration = 7 userID = pid # In[37]: today = datetime.now() timestamp = today.strftime("%Y-%m-%d %H:%M:%S") bucket = storage.bucket(app=app) # ## Get data from storage and get list of dates # In[6]: dates =[] for x in range(0 ,duration): today=date.today() #yesterday = today - datetime.timedelta(days=1) start_date = (today-timedelta(days=duration-x)).isoformat() dates.append(start_date) # In[7]: df= pd.DataFrame() # loop through the storage and get the data sleep =[] for x in range(0 ,len(dates)): #Sleep blob = bucket.blob(userID+"/fitbitData/"+dates[x]+"/"+dates[x]+"-sleep.json") # download the file u = blob.generate_signed_url(timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) sleepMinutes = data['summary']["totalMinutesAsleep"] except: pass #Activity (Steps) blob = bucket.blob(userID+"/fitbitData/"+dates[x]+"/"+dates[x]+"-activity.json") # download the file u = blob.generate_signed_url(timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) steps = data['summary']["steps"] except: pass #heartrate blob = bucket.blob(userID+"/fitbitData/"+dates[x]+"/"+dates[x]+"-heartrate.json") u = blob.generate_signed_url(timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) df_heartrate = pd.DataFrame(data['activities-heart-intraday']['dataset']) df_heartrate.time.apply(str) df_heartrate['time'] = pd.to_datetime(df_heartrate['time']) df_heartrate['hour'] = df_heartrate['time'].apply(lambda time: time.strftime('%H')) df_heartrate.drop(['time'],axis=1, inplace = True) heart_rate = df_heartrate.groupby(["hour"], as_index=False).mean() heart_rate['sleepMin'] = sleepMinutes heart_rate['TotalSteps'] = steps heart_rate['date'] = dates[x] heart_rate = heart_rate.astype({"hour": int}) except: pass # append dataframe try: df = df.append(heart_rate, ignore_index = True) except: pass # ### Get user location # In[8]: # get location from database loc_df = pd.DataFrame() locID = [] locations = db.collection(u'PatientLocations').where(u'patientID', u'==', userID ).stream() for location in locations: loc = location.to_dict() locID.append(location.id) loc_df = loc_df.append(pd.DataFrame(loc,index=[0]),ignore_index=True) loc_df['id'] = locID # In[9]: loc_df.drop(['anxietyLevel', 'lat','lng', 'patientID' ], axis=1, inplace = True) # In[10]: loc_df.time.apply(str) loc_df['time'] = pd.to_datetime(loc_df['time']) loc_df['date'] = pd.to_datetime(loc_df['time'], format='%Y:%M:%D').dt.date loc_df['hour'] = loc_df['time'].apply(lambda time: time.strftime('%H')) loc_df.drop(['time'], axis=1, inplace = True) loc_df.hour = loc_df.hour.astype(int) loc_df.date = loc_df.date.astype(str) df.date = df.date.astype(str) # In[11]: dfinal = pd.merge(left=df, right = loc_df, how = 'left', left_on=['hour','date'], right_on=['hour','date']).ffill() # ### Test data into model # In[12]: #test model train_df = dfinal.rename(columns={'value': 'Heartrate'}) # In[13]: Labeled_df = pd.DataFrame() Labeled_df = trainData(train_df) # In[14]: # Labeled_df.drop(['lon'],axis=1, inplace = True) # In[15]: # Replace missing values because it doesn't exist Labeled_df['name'].fillna("Not given", inplace=True) Labeled_df['id'].fillna("Not given", inplace=True) # ## Storage intilization # In[16]: firebaseConfig = { "apiKey": "AIzaSyBoxoXwFm9TuFysjQYag0GB1NEPyBINlTU", "authDomain": "serene-2dfd6.firebaseapp.com", "databaseURL": "https://serene-2dfd6.firebaseio.com", "projectId": "serene-2dfd6", "storageBucket": "serene-2dfd6.appspot.com", "messagingSenderId": "461213981433", "appId": "1:461213981433:web:62428e3664182b3e58e028", "measurementId": "G-J66VP2Y3CR" } firebase = pyrebase.initialize_app(firebaseConfig) storage = firebase.storage() # # AL # In[17]: sns.set( rc={'axes.facecolor': '#fcfeff'}) # In[18]: # i added this code here because it will result an error down because of the datetime yesterday = today - timedelta(days=1) timestamp = yesterday.strftime("%Y-%m-%d") from datetime import datetime # In[19]: # Change Label values to num, to represent them in a barchart nums=[] for row in Labeled_df.itertuples(): if row.Label == 'Low' or row.Label == 'LowA': nums.append(1) elif row.Label == 'Meduim': nums.append(2) else: nums.append(3) Labeled_df['numLabel'] = nums # In[20]: # Get anxiety level by day and store it in a new data frame plot_df = pd.DataFrame() avgAnxiety = [] days = [] totalAnxiety = 0 rowCount = 1 for x in range(0 ,len(dates)): totalAnxiety = 0 rowCount = 1 for row in Labeled_df.itertuples(): if (row.date == dates[x]): rowCount += 1 totalAnxiety += row.numLabel day = (datetime.strptime(dates[x], "%Y-%m-%d")).strftime("%a") days.append(day) avgAnxiety.append(totalAnxiety/rowCount) plot_df['date'] = dates plot_df['Anxiety'] = avgAnxiety plot_df['day'] = days # In[42]: # In[21]: fig, ax = plt.subplots() c1 = '#9dd6f5' c2 = '#4ba0d1' c3 = '#23495f' bar_width = 0.25 for t, y in zip(plot_df["day"], plot_df["Anxiety"]): c="" if(y <= 1): c = c1 elif (1 < y <= 2): c = c2 elif ( y > 2): c = c3 plt.bar([t,t], [0,y],bar_width, color=c) colors = [[c1,c1],[c2,c2],[c3,c3]] categories = ['Low','Meduim','High'] #create dict legend_dict=dict(zip(categories,colors)) #create patches patchList = [] for key in legend_dict: data_key = mpatches.Patch(facecolor=legend_dict[key][0], edgecolor=legend_dict[key][1], label=key) patchList.append(data_key) ax.legend(handles=patchList,ncol=len(categories), fontsize=12) plt.tick_params(axis='x') # Start the graph at 0 ax.set_ylim(0, 3) #fig.set_size_inches(15.5, 10) plt.tight_layout() #plt.xlabel('Date') ax.yaxis.set_label_coords(-0.02, 0.48) fig.savefig('AL.png', dpi = None) imagePath = "AL.png" link = storage.child(userID+"/dailyReport/AL.png").put('AL.png') os.remove("AL.png") # In[22]: linkDF = pd.DataFrame() linkDF = linkDF.append(pd.DataFrame(link,index=[0]),ignore_index=True) token1 = linkDF['downloadTokens'].values token = token1[0] AlLink = storage.child(userID+"/dailyReport/AL.png").get_url(token) # # Improvement # In[77]: # get yesterday improvement #today = datetime.datetime.now() #yesterday = today - datetime.timedelta(days=1) #timestamp = yesterday.strftime("%Y-%m-%d") al = float(plot_df[plot_df.date == timestamp].Anxiety) today_al = float("{:.2f}".format(al)) yesterday = plot_df.iloc[5]['Anxiety'] today = plot_df.iloc[6]['Anxiety'] imp = ((yesterday - today)/3)*100 # get before yesterday to calculate the improvement try: doc_ref = db.collection(u'DailyReport').document('daily'+userID) doc = doc_ref.get().to_dict() prev = float(doc['anxiety_level']) # calculate the improvement #1. get today Al improvement= ((prev - today_al)/3)*100 doc_rec = db.collection(u'DailyReport').document('daily'+userID) doc_rec.set({ u'AL_graph': AlLink, u'improvement': float("{:.2f}".format(improvement)),#float("{:.2f}".format(improvement)) u'anxiety_level' : float(today_al), u'date': str(datetime.now().strftime("%Y-%m-%d")) }) yesterday = plot_df.iloc[6]['Anxiety'] today = plot_df.iloc[5]['Anxiety'] except: doc_rec = db.collection(u'DailyReport').document('daily'+userID) doc_rec.set({ u'AL_graph': AlLink, u'improvement': 404 , u'anxiety_level' : float(today_al), u'date': str(datetime.now().strftime("%Y-%m-%d")) }) # In[ ]: json_df = plot_df[['Anxiety','date']].to_json() return json_df
def updateCatch(): try: data = request.json print(data) print(data['image']) convert(data['image'], data['imageFileName']) imagePath = BASE_PATH + data['imageFileName'] storage.child('fishes/{}'.format(data['imageFileName'])).put(imagePath) fish_url = storage.child('fishes/{}'.format( data['imageFileName'])).get_url(None) print(fish_url) data2 = { 'date': data['date'], 'description': data['description'], 'image': fish_url, 'hours': float(data['hours']), 'latitude': float(data['latitude']), 'longitude': float(data['longitude']), 'name': data['name'], 'weight': float(data['weight']), 'number': data['number'], 'catchId': int(''.join( [str(random.randint(0, 999)).zfill(3) for _ in range(2)])) } ref = userCatches.document(str(data['number'])) ref.update({u'catches': firestore.ArrayUnion([data2])}) print(data2) data1 = { 'latitude': float(data2['latitude']), 'longitude': float(data2['longitude']), 'totalWeight': float(data2['weight']), 'date': data2['date'], 'catchId': data2['catchId'], 'hours': data2['hours'], 'catches': [{ 'cost': 200, 'fishType': 'Shark', 'weight': 50, 'quantity': 10 }, { 'cost': 200, 'fishType': 'Whale', 'weight': 50, 'quantity': 10 }, { 'cost': 200, 'fishType': 'Dolphin', 'weight': 50, 'quantity': 10 }, { 'cost': 200, 'fishType': 'Bluefish', 'weight': 50, 'quantity': 10 }] } print(data1) fishData.document(str(data1['catchId'])).set(data1) return jsonify({"success": True}) except Exception as e: return jsonify({"success": False}), 400
auth = firebase.auth() email = "*****@*****.**" password = "******" camera = PiCamera() while True: try: if GPIO.input(10) == GPIO.HIGH: print("food pushed") now = datetime.now() dt = now.strftime("%d%m%Y%H:%M:%S") food = dt + ".jpg" camera.capture(food) print(food + " saved") storage.child(food).put(food) user = auth.sign_in_with_email_and_password(email, password) url = storage.child("fried.jpeg").get_url(user['idToken']) print(url) image = {'img': url, 'addedOn': str(date.today())} db.collection('images').add(image) print("Image sent") os.remove(food) print("File Removed") sleep(2) elif GPIO.input(11) == GPIO.HIGH: print("barcode pushed") now = datetime.now() dt = now.strftime("%d%m%Y%H:%M:%S") barcode = dt + ".jpg" camera.capture(barcode)
config = { "apiKey": "AIzaSyDuQJ6RZE6dWs7vToVDCJIRvEWDmMy9GKs", "authDomain": "pmisha-misha.firebaseapp.com", "databaseURL": "https://misha-misha.firebaseio.com", "projectId": "misha-misha", "storageBucket": "misha-misha.appspot.com", "serviceAccount": "ServiceAccountKey.json" } firebase = pyrebase.initialize_app(config) storage = firebase.storage() path_on_cloud = "images/uploadedFile1" path_local = "downloaded.jpeg" storage.child(path_on_cloud).download(path_local) #PUSHING NUMBER val = 2 doc_ref = db.collection(u'data').document(u'TF') doc_ref.set({ u'TF' : val, })