run_weighter.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # pandas and numpy for data manipulation
  2. import pandas as pd
  3. import numpy as np
  4. # fbprophet for additive models
  5. import fbprophet
  6. # gspread for Google Sheets access
  7. import gspread
  8. # slacker for interacting with Slack
  9. from slacker import Slacker
  10. # oauth2client for authorizing access to Google Sheets
  11. from oauth2client.service_account import ServiceAccountCredentials
  12. # os for deleting images
  13. import os
  14. # matplotlib for plotting
  15. import matplotlib.pyplot as plt
  16. import matplotlib.patches as mpatches
  17. import matplotlib
  18. # import weighter
  19. from weighter import Weighter
  20. if __name__ == "__main__":
  21. # google sheets access
  22. scope = ['https://spreadsheets.google.com/feeds']
  23. # Use local stored credentials in json file
  24. # make sure to first share the sheet with the email in the json file
  25. credentials = ServiceAccountCredentials.from_json_keyfile_name('C:/Users/Will Koehrsen/Desktop/weighter-2038ffb4e5a6.json', scope)
  26. # Authorize access
  27. gc = gspread.authorize(credentials);
  28. # Slack api key is stored as text file
  29. with open('C:/Users/Will Koehrsen/Desktop/slack_api.txt', 'r') as f:
  30. slack_api_key = f.read()
  31. slack = Slacker(slack_api_key)
  32. # Open the sheet, need to share the sheet with email specified in json file
  33. gsheet = gc.open('Auto Weight Challenge').sheet1
  34. # List of lists with each row in the sheet as a list
  35. weight_lists = gsheet.get_all_values()
  36. # Headers are the first list
  37. # Pop returns the element (list in this case) and removes it from the list
  38. headers = weight_lists.pop(0)
  39. # Convert list of lists to a dataframe with specified column header
  40. weights = pd.DataFrame(weight_lists, columns=headers)
  41. # Record column should be a boolean
  42. weights['Record'] = weights['Record'].astype(bool)
  43. # Name column is a string
  44. weights['Name'] = weights['Name'].astype(str)
  45. # Convert dates to datetime, then set as index, then set the time zone
  46. weights['Date'] = pd.to_datetime(weights['Date'], unit='s')
  47. weights = weights.set_index('Date', drop = True).tz_localize(tz='US/Eastern')
  48. # Drop any extra entries
  49. weights = weights.drop('NaT')
  50. # If there are new entries create the weighter object
  51. if len(weights) > np.count_nonzero(weights['Record']):
  52. # Initialize with dataframe of weights, google sheet, and slack object
  53. weighter = Weighter(weights, gsheet, slack)
  54. weighter.process_entries()
  55. print('Success')