def create_list(): raw_dates = get_date_list() files = [] for i in range(len(raw_dates)): filename = "nadac/NADAC " + raw_dates[i] files.append(filename) drugs = builder(files[0]) for i in range(len(files)-1): update(files[i+1], drugs) print "done with %s" % files[i+1] return new_fda(drugs)
def draw_graph(drug_dict): colors = [(114/255, 158/255, 206/255), (255/255, 158/255, 74/255), (103/255, 191/255, 92/255), (237/255, 102/255, 93/255), (173/255, 139/255, 201/255), (168/255, 120/255, 110/255), (237/255, 151/255, 202/255), (162/255, 162/255, 162/255), (205/255, 204/255, 93/255), (109/255, 204/255, 218/255)] xlist = get_date_list() x = [] for date in xlist: date = datetime.datetime.strptime(date,"%Y%m%d") x.append(date) y_dict = {} for drug in drug_dict: drug_name = drug_dict[drug].name y_dict[drug_name] = [] for k in sorted(drug_dict[drug].prices): y_dict[drug_name].append(drug_dict[drug].prices[k]) dates = mdates.date2num(x) fig = plt.figure() graph = fig.add_subplot(111) count = 0 for y in y_dict: graph.plot(dates, y_dict[y], lw=2.5, color=colors[count], label=y) first, last = get_first_last(y_dict[y]) plt.annotate('$%0.2f' % first, xy=(0.1, first), xytext=(0, 0), xycoords=('axes fraction', 'data'), textcoords='offset points', color=colors[count]) plt.annotate('$%0.2f' % last, xy=(.9, last), xytext=(1, 0), xycoords=('axes fraction', 'data'), textcoords='offset points', color=colors[count]) count += 1 leg = graph.legend(loc=2) conv = np.vectorize(mdates.strpdate2num('%Y%m%d')) graph.axvline(conv('20130101'), color='lightgrey', zorder=0) graph.axvline(conv('20140101'), color='lightgrey', zorder=0) graph.axvline(conv('20150101'), color='lightgrey', zorder=0) graph.axvline(conv('20160101'), color='lightgrey', zorder=0) graph.xaxis.set_major_formatter(mdates.DateFormatter('%Y%m%d')) graph.xaxis.set_major_locator(mdates.MonthLocator()) graph.xaxis.set_minor_locator(mdates.DayLocator()) plt.gcf().autofmt_xdate() fig.autofmt_xdate() for color, text in zip(colors,leg.get_texts()): text.set_color(color) plt.show()
def write(drugs): fieldnames = ["ID", "Name", "Scientific Name", "Unit", "OTC", "Brand/Generic", "Vendor", "Package", "Description"] dates = get_date_list() for i in range(len(dates)): fieldnames.append(dates[i]) with open("FullPrices.csv","a") as prices_csv: writer = csv.writer(prices_csv, delimiter='|') writer.writerow(fieldnames) for entry in drugs: drug = drugs[entry] drugrow = [drug.id, drug.name, drug.scientific_name, drug.unit, drug.otc, drug.b_or_g, drug.vendor, drug.package, drug.desc] for i in range(len(dates)): try: drugrow.append(drug.prices[dates[i]]) except KeyError: drugrow.append(None) writer.writerow(drugrow) prices_csv.close() print "Done"