def get_Functional_connectivity_windowed(inputfile, outputfile):
    print("\nCalculating Functional Connectivity:")
    print("inputfile: {0}".format(inputfile))
    print("outputfile: {0}".format(outputfile))
    with open(inputfile, 'rb') as f:
        data, fs = pickle.load(f)  #un-pickles files
    data_array = np.array(data)
    fs = float(fs)
    moving_window = (
        1 / 5)  #window moved over 1/5th of a second when this value = (1/5)
    window_size = 1  #1 = 1s econds
    totalSecs = np.floor(np.size(data_array, 0) / fs / moving_window)
    totalSecs = int(totalSecs)
    alphatheta = np.zeros((np.size(data_array, 1), np.size(data_array,
                                                           1), totalSecs))
    beta = np.zeros((np.size(data_array, 1), np.size(data_array,
                                                     1), totalSecs))
    broadband = np.zeros((np.size(data_array, 1), np.size(data_array,
                                                          1), totalSecs))
    highgamma = np.zeros((np.size(data_array, 1), np.size(data_array,
                                                          1), totalSecs))
    lowgamma = np.zeros((np.size(data_array, 1), np.size(data_array,
                                                         1), totalSecs))
    startInd_non_round = 0
    for w in range(0, totalSecs):
        printProgressBar(w + 1,
                         totalSecs,
                         prefix="Progress:",
                         suffix=". {0}/{1}. {2} sec. Mov Win: {3} sec.".format(
                             w + 1, totalSecs,
                             np.round(totalSecs * moving_window, 2),
                             moving_window))
        startInd_non_round = startInd_non_round + fs * moving_window
        startInd_round = int(np.round(startInd_non_round))
        endInd = int((startInd_round + fs * window_size) -
                     1)  #calculating over 1 second windows
        window = data_array[startInd_round:endInd, :]
        broad = broadband_conn(window, int(fs), avgref=True)
        adj_alphatheta, adj_beta, adj_lowgamma, adj_highgamma = multiband_conn(
            window, int(fs), avgref=True)
        alphatheta[:, :, w] = adj_alphatheta
        beta[:, :, w] = adj_beta
        broadband[:, :, w] = broad
        highgamma[:, :, w] = adj_highgamma
        lowgamma[:, :, w] = adj_lowgamma
    print("Saving to: {0}".format(outputfile))
    electrode_row_and_column_names = data.columns.values
    order_of_matrices_in_pickle_file = pd.DataFrame(
        ["broadband", "alphatheta", "beta", "lowgamma", "highgamma"],
        columns=["Order of matrices in pickle file"])
    with open(outputfile, 'wb') as f:
        pickle.dump([
            broadband, alphatheta, beta, lowgamma, highgamma,
            electrode_row_and_column_names, order_of_matrices_in_pickle_file
        ], f)
    #np.savez(outputfile, broadband=broadband, alphatheta=alphatheta, beta=beta, lowgamma=lowgamma, highgamma=highgamma)
    print("...done\n\n")
def get_Functional_connectivity(inputfile, outputfile):
    print("\nCalculating Functional Connectivity:")
    print("inputfile: {0}".format(inputfile))
    print("outputfile: {0}".format(outputfile))
    with open(inputfile, 'rb') as f:
        data, fs = pickle.load(f)  #un-pickles files
    data_array = np.array(data)
    fs = float(fs)
    totalSecs = np.floor(np.size(data_array, 0) / fs)
    totalSecs = int(totalSecs)
    alphatheta = np.zeros((np.size(data_array, 1), np.size(data_array,
                                                           1), totalSecs))
    beta = np.zeros((np.size(data_array, 1), np.size(data_array,
                                                     1), totalSecs))
    broadband = np.zeros((np.size(data_array, 1), np.size(data_array,
                                                          1), totalSecs))
    highgamma = np.zeros((np.size(data_array, 1), np.size(data_array,
                                                          1), totalSecs))
    lowgamma = np.zeros((np.size(data_array, 1), np.size(data_array,
                                                         1), totalSecs))
    for t in range(0, totalSecs):
        printProgressBar(
            t + 1,
            totalSecs,
            prefix="Progress:",
            suffix=
            "done. Calculating {0} of {1} functional connectivity matrices".
            format(t + 1, totalSecs))
        startInd = int(t * fs)
        endInd = int(((t + 1) * fs) - 1)  #calculating over 1 second windows
        window = data_array[startInd:endInd, :]
        broad = broadband_conn(window, int(fs), avgref=True)
        adj_alphatheta, adj_beta, adj_lowgamma, adj_highgamma = multiband_conn(
            window, int(fs), avgref=True)
        alphatheta[:, :, t] = adj_alphatheta
        beta[:, :, t] = adj_beta
        broadband[:, :, t] = broad
        highgamma[:, :, t] = adj_highgamma
        lowgamma[:, :, t] = adj_lowgamma
    print("Saving to: {0}".format(outputfile))
    electrode_row_and_column_names = data.columns.values
    order_of_matrices_in_pickle_file = pd.DataFrame(
        ["broadband", "alphatheta", "beta", "lowgamma", "highgamma"],
        columns=["Order of matrices in pickle file"])
    with open(outputfile, 'wb') as f:
        pickle.dump([
            broadband, alphatheta, beta, lowgamma, highgamma,
            electrode_row_and_column_names, order_of_matrices_in_pickle_file
        ], f)
    #np.savez(outputfile, broadband=broadband, alphatheta=alphatheta, beta=beta, lowgamma=lowgamma, highgamma=highgamma)
    print("...done\n\n")
def get_Functional_connectivity(inputfile,outputfile,windowlength):
    print("\nCalculating Functional Connectivity:")
    print("inputfile: {0}".format(inputfile))
    print("outputfile: {0}".format(outputfile))
    with open(inputfile, 'rb') as f: data, fs = pickle.load(f)#un-pickles files
    data_array = np.array(data)
    fs = float(fs)
    totalSecs = np.floor(np.size(data_array,0)/fs)
    totalSecs = int(totalSecs)
    alphatheta = np.zeros((np.size(data_array,1),np.size(data_array,1),totalSecs))
    beta = np.zeros((np.size(data_array,1),np.size(data_array,1),totalSecs))
    broadband = np.zeros((np.size(data_array,1),np.size(data_array,1),totalSecs))
    highgamma = np.zeros((np.size(data_array,1),np.size(data_array,1),totalSecs))
    lowgamma = np.zeros((np.size(data_array,1),np.size(data_array,1),totalSecs))
    
    windows = []
    for t in range(0,totalSecs,windowlength):
        #printProgressBar(t+1, totalSecs, prefix = "Progress:", suffix = "done. Calculating {0} of {1} functional connectivity matrices".format(t+1,totalSecs ) )
        startInd = int(t*fs)
        endInd = int(((t+windowlength)*fs) - 1) # calculating over windowlength
        windows.append((data_array[startInd:endInd,:],int(fs)))

    if __name__ == 'functional_connectivity':
        print('begin multiprocessing')
        try:
            mp.set_start_method('spawn')
        except RuntimeError:
            pass
        num_workers = mp.cpu_count()
        print('num cores = ' + str(num_workers))
        try:
            with Pool(processes=num_workers) as pool:
                broad = pool.starmap(broadband_conn,windows)
                print('broadband calculations complete')
                processed_bands = pool.starmap(multiband_conn,windows)
                print('other band calculations complete')
        except:
            print('error encountered, continuing with multiprocessing disabled...')
            try:
                broad = [broadband_conn(x[0],x[1]) for x in windows]
                print('broadband calculations complete')
                processed_bands = [multiband_conn(x[0],x[1]) for x in windows]
                print('other band calculations complete')
            except ValueError:
                print('Encountered ValueError: input data most likely contains NaNs. Skipping...\n')
                # calculations did not finish
                return True

    for t in range(0,len(range(0,totalSecs,windowlength))):
        alphatheta[:,:,t] = processed_bands[t][0]
        beta[:,:,t] = processed_bands[t][1]
        highgamma[:,:,t] = processed_bands[t][3]
        lowgamma[:,:,t] = processed_bands[t][2]
        #printProgressBar(t+1, totalSecs, prefix = "Progress:", suffix = "done. Calculating {0} of {1} functional connectivity matrices".format(t+1,totalSecs ) )
        #startInd = int(t*fs)
        #endInd = int(((t+1)*fs) - 1) #calculating over 1 second windows
        #window = data_array[startInd:endInd,:]
        #broad = broadband_conn(window,int(fs),avgref=True)
        broadband[:,:,t] = broad[t]
        
    print("Saving to: {0}".format(outputfile))
    electrode_row_and_column_names = data.columns.values
    order_of_matrices_in_pickle_file = pd.DataFrame(["broadband", "alphatheta", "beta", "lowgamma" , "highgamma" ], columns=["Order of matrices in pickle file"])
    with open(outputfile, 'wb') as f: pickle.dump([broadband, alphatheta, beta, lowgamma, highgamma, electrode_row_and_column_names, order_of_matrices_in_pickle_file], f)
    #np.savez(outputfile, broadband=broadband, alphatheta=alphatheta, beta=beta, lowgamma=lowgamma, highgamma=highgamma)
    print("...done\n\n")
    return False