if __name__ == '__main__':
    np.random.seed(0)
    nb_points = 16
    subset_size = 20000
    filename_template = 'data_emanuele/%s_%s_tracks.trk'
    subjects = ['100206', '100307']
    label_y = {'plausible': 1, 'unplausible': 0}

    X = []
    y = []
    groups = []
    for subject in subjects:
        for label in label_y.keys():
            filename = filename_template % (subject, label)
            streamlines, header, lengths, idxs = load_streamlines(filename,
                                                                  idxs=subset_size,
                                                                  verbose=True)

            print("Resampling streamlines to %s points" % nb_points)
            streamlines = set_number_of_points(streamlines, nb_points)
            print("Embedding streamlines into vectors")
            # tmp = embed_flattened(streamlines)
            # tmp = embed_flattened_plus_flipped(streamlines)
            # tmp = embed_ordered(streamlines)
            # tmp = embed_flattened_plus_length(streamlines)
            # tmp = embed_flattened_plus_flipped_plus_length(streamlines)
            tmp = embed_flattened_plus_flipped_plus_length_plus_curvature(streamlines)
            X.append(tmp)
            y.append([label_y[label]] * len(tmp))
            groups.append([subject] * len(tmp))
if __name__ == '__main__':

    from nilab.load_trk import load_streamlines
    from dipy.tracking.streamline import set_number_of_points
    from dipy.tracking.distances import bundles_distances_mdf
    from time import time

    trk_fn = 'sub-100206_var-FNAL_tract.trk'
    nb_points = 32
    nb_prototypes = 100
    streamline_distance = bundles_distances_mdf

    streamlines, header, lengths, idxs = load_streamlines(trk_fn,
                                                          idxs=None,
                                                          apply_affine=True,
                                                          container='list',
                                                          verbose=True)
    print("Resampling to %s points" % nb_points)
    streamlines = set_number_of_points(streamlines, nb_points)
    # streamlines = np.array(set_number_of_points(streamlines, nb_points),
    #                        dtype=np.object)

    print("embed_flattened():")
    t0 = time()
    X_flattened = embed_flattened(streamlines)
    print("%s sec." % (time() - t0))

    print("embed_flattened_plus_flipped():")
    t0 = time()
    X_flattened_plus_flipped = embed_flattened_plus_flipped(streamlines)
N_streamlines1 = 50000  #00
N_streamlines2 = 50000  #00
N_points = 32
k = 1000
lap_flag = False
MWFBM_flag = False
MATLAB_flag = True
# %% Load Trk
print("laod stramlines...")

filename1 = "data1/1M_len20-250mm_coff0001_step1_seedimage_30deg_SD_STREAM.trk"
filename2 = "data2/1M_len20-250mm_coff0001_step1_seedimage_30deg_SD_STREAM.trk"

track1, header1, lengths1, indices1 = load_streamlines(filename1,
                                                       container="array",
                                                       verbose=True,
                                                       idxs=N_streamlines1,
                                                       apply_affine=True)
track2, header2, lengths2, indices2 = load_streamlines(filename2,
                                                       container="array",
                                                       verbose=True,
                                                       idxs=N_streamlines2,
                                                       apply_affine=True)

# %% Resampling
print("setting the same number of points for both the tracts...")

track1 = np.array(set_number_of_points(track1, N_points))
track2 = np.array(set_number_of_points(track2, N_points))

# %% Nearest Neightbours