def getMixtures(dates, network, max_n_links=None, return_tts=False, max_nb_mixture=4,num_threads=1): tic("Running with {0} jobs.".format(num_threads),"getMixtures") ttob_seqs = (ttob_seq for date in dates for ttob_seq in getDayTTOBservations(date, network)) # Get travel times for each link all_ttobs = (ttob for ttob_seq in ttob_seqs for ttob in ttob_seq) tic("starting groupby...","getMixtures") all_ttobs_by_lid = sorted([(lid, list(vals)) for (lid, vals) in groupby(all_ttobs, lambda ttob:ttob.linkId)], key=lambda z:-len(z[1])) tic("groupby done, {0} links".format(len(all_ttobs_by_lid)),"getMixtures") if max_n_links: all_ttobs_by_lid = all_ttobs_by_lid[:max_n_links] tts_by_link = [(lid, np.array([tto.tt for tto in vals])) for (lid, vals) in all_ttobs_by_lid] tic("vectorization done","getMixtures") if num_threads != 1: tic("Running with {0} jobs.".format(num_threads),"getMixtures") learned_mixtures = Parallel(n_jobs=num_threads,verbose=10)(delayed(getMixtures_inner)((lid, tts, max_nb_mixture)) for (lid, tts) in tts_by_link) else: learned_mixtures = [(lid, learnMixtureAuto(tts, max_nb_mixture)) for (lid, tts) in tts_by_link] if return_tts: return (dict(learned_mixtures), dict(tts_by_link)) else: return dict(learned_mixtures)
def getMixtures_inner(args): (lid,tts,max_nb_mixture) = args return (lid, learnMixtureAuto(tts, max_nb_mixture))