def load_probs(fn_probs, interval_min, interval_max, weekdays, pickup, n_keep=-1): probs = dict() probs[DS] = list() probs[PR] = list() dd = defaultdict(float) with io.open(fn_probs, "rb") as fin: reader = csv.DictReader(fin) for i, row in enumerate(reader): if i == 0: continue row = common.clean_dict(row) int_within = interval_min <= row["tau"] <= interval_max wd_eq = row["day"] == weekdays pickup_eq = row["pickup"] == pickup if int_within and wd_eq and pickup_eq: dd[row["dropoff"]] += row["probability"] if n_keep > len(dd.values()): n_keep = len(dd.values()) probs[DS] = map(int, dd.keys())[:n_keep] probs[PR] = dd.values()[:n_keep] return probs
def _make_listitem(self, label, label2='', iconImage='', thumbnail='', path='', **options): li = xbmcgui.ListItem(label, label2=label2, iconImage=iconImage, thumbnailImage=thumbnail, path=path) cleaned_info = clean_dict(options.get('info')) if cleaned_info: li.setInfo('video', cleaned_info) if options.get('is_playable'): li.setProperty('IsPlayable', 'true') if options.get('context_menu'): li.addContextMenuItems(options['context_menu']) #endpoint = options['context_menu'].get('add_to_playlist') #if endpoint: #keys = ['label', 'label2', 'icon', 'thumbnail', 'path', 'info'] # need the url for calling add_to_playlist this is thwat gets added to the context menu # need the current url for the item, it will be encoded in teh url for calling add_to_playlist # also other info in [keys] will be added to teh add_to_playlist url's qs so we can recreate the listitem # perhaps try pickling the listitem? #current_url = options.get('url') #context_menu_url = self.url_for(endpoint, url=options.get('url'), label=label, label2=label2, iconImage=iconImage, thumbnail=thumbnail) #li.addContextMenuItems([('Add to Playlist', 'XBMC.RunPlugin(%s)' % context_menu_url)]) #return li return options['url'], li, options.get('is_folder', True)
def load_cumulative_probs(fn_probs, interval_min, interval_max, weekdays): dropoff_probs = defaultdict(float) pickup_probs = defaultdict(float) with io.open(fn_probs, "rb") as fin: reader = csv.DictReader(fin) next(reader) for row in reader: row = common.clean_dict(row) int_within = interval_min <= row["tau"] <= interval_max wd_eq = row["day"] in weekdays if int_within and wd_eq: dropoff_probs[row["dropoff"]] += row["probability"] pickup_probs[row["pickup"]] += row["probability"] return pickup_probs, dropoff_probs
def create_demands_file(stations, fn_raw, fn_demands, kd, fl): pbar = ProgressBar( widgets=["Creating Demands File: ", Bar(), Percentage(), "|", ETA()], maxval=fl + 1).start() cur_date = (0, 0, 0) fout = None writer = None counter = 0 rows = list() with io.open(fn_raw, "rb") as fin: reader = csv.DictReader(fin) for i, row in enumerate(reader): if i == 0: continue nrow = [None] * len(fn_demands_fields) row = common.clean_dict(row) str_time = row["pickup_datetime"] t = time.strptime(str_time, common.date_format) cdate = (t.tm_wday + 1, t.tm_yday // 7, t.tm_year) if cdate != cur_date: cur_date = cdate fname = day_dem_template.format(*cur_date) fout = io.open(fname, "wb") writer = csv.writer(fout, delimiter=" ") writer.writerow(fn_demands_fields) writer.writerow([counter]) writer.writerows(rows) rows = list() fout.close() counter = 0 p_l = [row["pickup_longitude"], row["pickup_latitude"]] d_l = [row["dropoff_longitude"], row["dropoff_latitude"]] _, sts = kd.query(np.array([p_l, d_l])) nrow[0] = epoch_seconds(row["pickup_datetime"]) nrow[1] = sts[0] nrow[2] = epoch_seconds(row["dropoff_datetime"]) nrow[3] = row["dropoff_longitude"] nrow[4] = row["dropoff_latitude"] nrow[5] = sts[1] nrow[6] = row["pickup_longitude"] nrow[7] = row["pickup_latitude"] rows.append(nrow) counter += 1 pbar.update(i) pbar.finish()
def extract_frequencies(fn_raw, stations, kd, fl): num_pd = OrderedDict() num_ti = OrderedDict() num_tau = defaultdict(int) num_tau_occ = defaultdict(set) counter = 0 pbar = ProgressBar( widgets=["Extracting Frequencies: ", Bar(), Percentage(), "|", ETA()], maxval=fl + 1).start() with io.open(fn_raw, "rb") as fin: reader = csv.DictReader(fin) for i, row in enumerate(reader): if i == 0: continue try: row = common.clean_dict(row) p_time, p_day = percent_time(row["pickup_datetime"]) p_l = [row["pickup_longitude"], row["pickup_latitude"]] d_l = [row["dropoff_longitude"], row["dropoff_latitude"]] _, sts = kd.query(np.array([p_l, d_l])) tau = int(4 * 24 * p_time) ti = (tau, p_day) if not ti in num_pd.keys(): num_pd[ti] = dict() if not ti in num_ti: num_ti[ti] = 0 if not (sts[0], sts[1]) in num_pd[ti]: num_pd[ti][(sts[0], sts[1])] = 0 counter += 1 num_pd[ti][(sts[0], sts[1])] += row["passenger_count"] num_ti[ti] += row["passenger_count"] num_tau[tau] += row["passenger_count"] num_tau_occ[tau].add(p_day) except ValueError: pass pbar.update(i + 1) pbar.finish() return num_pd, num_ti, num_tau, num_tau_occ, counter
def filter_data_for_day(fn_huge, fn_filtered, wds, max_wds): print "Filtering file based on weekday..." current_day = None day_count = defaultdict(int) with io.open(fn_huge, "rb") as fin: with io.open(fn_filtered, "wb") as fout: reader = csv.DictReader(fin) writer = csv.DictWriter(fout, fieldnames=common.fn_raw_fields) writer.writeheader() for i, row in enumerate(reader): if i == 0: continue row = common.clean_dict(row) str_time = row["pickup_datetime"] t = time.strptime(str_time, common.date_format) if t.tm_wday in wds: if current_day != t.tm_yday: current_day = t.tm_yday if day_count[t.tm_wday] <= max_wds: day_count[t.tm_wday] += 1 if sum(day_count.values()) > len(wds) * max_wds: return if day_count[t.tm_yday] <= max_wds: writer.writerow(row)