def createAllFeaturesAndTargets():
    filepath1 = dru.findPathForFile("AUGv2_trajectories-0830am-0900am.txt")
    filepath2 = dru.findPathForFile("trajectories-peachtree.txt")
    for testnum in [1, 1.1, 2]:
        print("Creating features and targets for testnum:", testnum,
              "and filepath:")
        print(filepath1)
        createFeaturesOnly(filepath1, testnum)
        print("Done with testnum:", testnum)
    for testnum in [3, 4]:
        print("Creating features and targets for testnum:", testnum,
              "and filepath:")
        print(filepath2)
        #manually move features for lankershim (not hard)
        createFeaturesOnly(filepath2, testnum)
        print("Done with testnum:", testnum)
def newCreateAllFeaturesAndTargets(testtypes,
                                   filename_lank="trajectories-lankershim.txt",
                                   filename_peach="trajectories-peachtree.txt",
                                   save=True,
                                   byIntersection=True):
    filepath1 = dru.findPathForFile(filename_lank)
    filepath2 = dru.findPathForFile(filename_peach)
    if byIntersection:
        save_path = os.path.join(c.PATH_TO_RESULTS, "ByIntersection")
    else:
        save_path = os.path.join(c.PATH_TO_RESULTS, "General")
    check_make_paths([save_path])
    for testnum in testtypes:
        check_make_paths([os.path.join(save_path, testnum)])
        allFeatures_normal, allFeatures_LSTM = getAllPossibleFeatures(
            filepath1,
            filepath2,
            lanetype=bool(int(testnum[0])),
            history=bool(int(testnum[1])),
            histFs=[5, 10, 20, 30],
            traffic=bool(int(testnum[2])),
            numFramesToFind=20)
        if not save: continue
        if not byIntersection:
            #AppendToEnd(intersectionID, features)
            #save new
            continue
        for intersectionID in allFeatures_LSTM.keys():
            #save features
            check_make_paths(
                [os.path.join(save_path, testnum, str(intersectionID))])
            print(allFeatures_LSTM[intersectionID].shape)
            np.savetxt(
                os.path.join(save_path, testnum, str(intersectionID),
                             "featuresAndTargets"),
                allFeatures_normal[intersectionID])
            np.save(
                os.path.join(save_path, testnum, str(intersectionID),
                             "LSTM_Formatted_featuresAndTargets"),
                allFeatures_LSTM[intersectionID])
    return allFeatures_normal, allFeatures_LSTM
def doGoalStuff(filepath, isCompressed, goalFile, fake=True, vels=False):
    print(filepath)
    filename = os.path.basename(filepath)
    featurepath = dutil.findFeaturePath(filename)
    print("constructing driver data...")
    driver_data = dd.data(filepath, isCompressed, featurepath)
    print("constructing goal dict...")
    goal_dict = loadFeatureGoals(featurepath)
    #will be a dict that maps dest_num: (avg_x, avg_y), goalNum
    #key = (dest, destLane)
    #d[key]=(x,y),n -- d[key][0]=(x,y) -- d[key][1] = goalNums
    numGoals = len(list(goal_dict.keys())) - 1
    print("constructing vidDIct")
    if 'format_wrong' in goalFile:
        wrongPath = dutil.findPathForFile(goalFile)
        outfile = goalFile[0:(len(goalFile) -
                              len('_format_wrong.txt'))] + '.txt'
        outpath = dutil.findPathForFile(outfile)
        print("fixing format")
        fixFormat(wrongPath, outpath, numGoals, driver_data)
        if not vels:
            vidDict = readGoalFile(outpath, numGoals, driver_data, False)
        else:
            vidDict, vidToVels = readGoalAndVelFile(outpath, numGoals,
                                                    driver_data, False)
    else:
        if not vels:
            vidDict = readGoalFile(dutil.findPathForFile(goalFile), numGoals,
                                   driver_data, fake)
        else:
            vidDict, vidToVels = readGoalAndVelFile(
                dutil.findPathForFile(goalFile), numGoals, driver_data, fake)

    #pick a random vehicle
    #vid = driver_data.getRandVid()
    #while not vid in vidDict.keys():
    #    vid = driver_data.getRandVid()
    stopAt = 900
    vids = [2, 5, 9, 14, 24]
    '''stopAt = 899
    print(vid)
    fidFirst = list(vidDict[vid].keys())[0]
    fidLast = 925#list(vidDict[vid].keys())[len(list(vidDict[vid].keys()))-1]
    print(fidFirst, fidLast)
    fids = [fidLast]
    vidsToFids = {vid:fids}
    for vid in vidsToFids.keys():'''
    #for vid in vids:
    for vid in vidDict.keys():
        if not driver_data.hasVID(vid):
            continue
        stopAt = min(driver_data.lastFrameForVID(vid),
                     list(vidDict[vid].keys())[-1])
        frames = []
        ActX = []
        ActY = []
        PredX = []
        PredY = []
        #for fid in vidsToFids[vid]:
        for fid in vidDict[vid].keys():
            if not fid in (driver_data.getFrameDict()).keys():
                continue
            if vid not in (driver_data.getFrameDict())[fid]:
                continue
            if not vels:
                processJustGoals(vid, fid, driver_data, vidDict, goal_dict,
                                 stopAt)
            else:
                aX, aY, pX, pY = processVelsAndGoals(vid, fid, driver_data,
                                                     vidDict, goal_dict,
                                                     stopAt, vidToVels)
                ActX.append(aX)
                ActY.append(aY)
                PredX.append(pX)
                PredY.append(pY)
                frames.append(fid)
        plt.figure()
        sns.set(style="darkgrid", font='Source Sans Pro')
        sns.set_context("talk")
        x = frames
        XAline, = plt.plot(x, ActX, label='Actual X Vel')
        YAline, = plt.plot(x, ActY, label='Actual Y Vel')
        XPline, = plt.plot(x, PredX, '--', label='Pred X Vel')
        YPline, = plt.plot(x, PredY, '--', label='Pred Y Vel')
        title = "Pred and Actual Vel Comparison for Vehicle " + str(vid)
        plt.title(title)
        plt.legend(loc=0)
        fileout = 'TrajComparisonVidX&Y' + str(vid) + '.png'
        plt.savefig(plotFolder + fileout)
def main(arguments):
    mode = argument_utils.expand_mode(arguments.mode)
    if mode == "combine":
        print("Combining trajectory files")
        peachtree = doStuffForPeachtree()
        lankershim = combineLankershimFiles()
        return peachtree, lankershim

    elif mode == "augment":
        print("Augmenting raw trajectoris")
        for filename in arguments.filenames:
            print("Augmenting file:", filename)
            du.augOrigData(dru.findPathForFile(filename))

    elif mode == "featurize":
        print("Featurizing augmented data")
        filename_lank = "AUGv2_trajectories-lankershim.txt"
        filename_peach = "AUGv2_trajectories-peachtree.txt"
        testtypes = arguments.test_nums.split(",")

        print("Test types to featurize for:", testtypes)
        if arguments.featurize_type == "s":
            print("saving featurzied general data")
            newCreateAllFeaturesAndTargets(testtypes,
                                           filename_lank,
                                           filename_peach,
                                           save=True,
                                           byIntersection=False)
        elif arguments.featurize_type == "i":
            print("saving feautrized data by intersection")
            newCreateAllFeaturesAndTargets(testtypes,
                                           filename_lank,
                                           filename_peach,
                                           save=True)
        elif arguments.featurize_type == "n":
            print("not saving")
            newCreateAllFeaturesAndTargets(testtypes,
                                           filename_lank,
                                           filename_peach,
                                           save=False)
        else:
            print(
                "invalid featurization option,", arguments.featurize_type,
                "options are s (save general), i (save by intersection), n (dont save - not recommended)"
            )
        print("Done featurizing")

    elif mode == "train":
        model_arg = arguments.models
        models = argument_utils.model_choices[model_arg]
        testtypes = arguments.test_nums.split(",")
        str_inters = arguments.test_intersections.split(",")
        saving = True  # TODO arg?

        for test_inters in str_inters:
            list_test = [int(i) for i in test_inters]
            train_inters = sorted(
                list(set([1, 2, 3, 4, 5, 6, 7, 8, 9]) - set(list_test)))
            print("test inters:", list_test)
            print("train inters:", train_inters)
            intersections = ([int(i) for i in test_inters],
                             [int(i) for i in train_inters])
            """ Deprecated
            if arguments.mode == "tr":
                if not saving:
                    print("indicated to train without saving, which is useless, will save")
                print("training and saving")
                saving = True
                new_train(models, testtypes, intersections, saving)
            elif arguments.mode == "te":
                print("testing only")
                graphs = input("Save graphs?")
                new_test(models, testtypes, intersections, saving, graphs)
            elif arguments.mode == "t":
            """
            print("training and testing")
            graphs = False  # TODO arg?
            new_train_and_test(models, testtypes, intersections, saving,
                               graphs)

    elif mode == "evaluate":
        print("evaluating.")
        model_arg = arguments.models
        models = argument_utils.model_choices[model_arg]
        testtypes = arguments.test_nums.split(",")
        teston = arguments.test_intersections
        doEvalThings(models, testtypes, teston, arguments)

    elif mode == "experimental":
        print("doing crazy things probably")
        #argv[2] == models (svm or dnn or lstms or nns (dnn and lstms) or all)
        #argv[3] == testtypes - "," separated 000,001,010,011,100
        #argv[4] == "," separated test intersections, assume train on all except test
        #argv[5] == optional - subset, number of training examples - UNUSED
        #argv[6] == s (save), 0 (dont save) - UNUSED, save all
        model_arg = arguments.models
        models = argument_utils.model_choices[model_arg]
        testtypes = arguments.test_nums.split(",")
        str_inters = arguments.test_intersections.split(",")
        saving = True  # TODO arg?

        for test_inters in str_inters:
            list_test = [int(i) for i in test_inters]
            train_inters = sorted(
                list(set([1, 2, 3, 4, 5, 6, 7, 8, 9]) - set(list_test)))
            print("test inters:", list_test)
            print("train inters:", train_inters)
            intersections = ([int(i) for i in test_inters],
                             [int(i) for i in train_inters])
            new_train_and_test(models,
                               testtypes,
                               intersections,
                               saving,
                               False,
                               exper=True)
    else:
        print(
            "invalid argument:", arguments.mode,
            "options are a - augment, f - featurize, tr - train, te - test, t - train and test"
        )
def combineLankershimFiles(overlapStartFrame=10201, maxVid1=1438):
    file1 = 'trajectories-0830am-0845am.txt'
    file2 = 'trajectories-0845am-0900am.txt'
    return futil.combineTrajFiles(dru.findPathForFile(file1),
                                  dru.findPathForFile(file2))
def run():
    filepath = dru.findPathForFile("AUGv2_trajectories-0830am-0900am.txt")
    testID = "Test1"
    test_folder = c.PATH_TO_RESULTS + str(testID)
    doLSTM(filepath, testID, test_folder, numEpochs=2)