import matplotlib.patches as mpatches

path_input = '/home/brainlab/Desktop/Rudas/Data/Ising/HCP/data/hcp_mean/J_ij.csv'
path_output = '/home/brainlab/Desktop/Rudas/Data/Ising/HCP/data/hcp_mean/results/'

# Ising Parameters
temperature_parameters = (
    0.05, 5, 5
)  # Temperature parameters (initial tempeture, final tempeture, number of steps)
no_simulations = 150  # Number of simulation after thermalization
thermalize_time = 0.3  #

J = to_normalize(np.loadtxt(path_input, delimiter=','))

ts = np.linspace(temperature_parameters[0],
                 temperature_parameters[1],
                 num=temperature_parameters[2])
colors = ['red', 'green', 'black', 'blue', 'purple']

f = plt.figure(figsize=(18, 10))  # plot the calculated values

simulated_fc, critical_temperature, E, M, S, H = generalized_ising(
    J,
    temperature_parameters=temperature_parameters,
    no_simulations=no_simulations,
    thermalize_time=thermalize_time)

makedir(path_output)
save_results(temperature_parameters, J, E, M, S, H, simulated_fc,
             critical_temperature, path_output)
Exemple #2
0
n_time_points = 4000 # Number of simulation after thermalization
thermalize_time = 0.2  #

main_path = '/home/brainlab/Desktop/Popiel/Ising_HCP/'

parcels = ['Aud', 'CinguloOperc', 'CinguloParietal', 'DMN', 'Dorsal', 'FrontoParietal', 'Retrosplenial', 'SMhand',
           'SMmouth', 'Ventral', 'Visual']


for parcel in parcels:
    print('Running', parcel)
    parcel_path = main_path + parcel + '/'
    #results_path = sub_path + 'results/'
    save_path = parcel_path  + '/results/'

    makedir(save_path)
    if not file_exists(save_path + 'phi.csv'):
        Jij = to_normalize(load_matrix(parcel_path + 'Jij_avg.csv'))

        start_time = time.time()

        simulated_fc, critical_temperature, E, M, S, H, spin_mean, tc = generalized_ising(Jij,
                                                                                          temperature_parameters=temperature_parameters,
                                                                                          n_time_points=n_time_points,
                                                                                          thermalize_time=thermalize_time,
                                                                                          phi_variables=True,
                                                                                          return_tc=True)

        print('It took ',time.time()-start_time, 'seconds to fit the generalized Ising model')

        makedir(save_path)
Exemple #3
0
    parcels = [
        'Aud', 'CinguloOperc', 'CinguloParietal', 'DMN', 'Dorsal',
        'FrontoParietal', 'Retrosplenial', 'SMhand', 'SMmouth', 'Ventral',
        'Visual'
    ]

    for parcel in parcels:
        print('Running', parcel)
        parcel_path = main_path + parcel + '/'
        #results_path = sub_path + 'results/'
        save_path = parcel_path + '/results/avg/'

        save_path_phi = main_path + parcel + '/phi/'

        makedir(save_path_phi)

        makedir(save_path)

        Jij = to_normalize(load_matrix(parcel_path + 'Jij_avg.csv'))

        start_time = time.time()

        simulated_fc, critical_temperature, E, M, S, H, spin_mean, tc = generalized_ising(
            Jij,
            temperature_parameters=temperature_parameters,
            n_time_points=n_time_points,
            thermalize_time=thermalize_time,
            phi_variables=True,
            return_tc=True)
Exemple #4
0
def phi_sim_save(size,
                 output_directory,
                 count,
                 temperature_parameters=(-1, 5, 50),
                 no_simulations=500,
                 thermalize_time=0.3):

    import numpy as np
    from projects.generalize_ising_model.core import generalized_ising
    import time
    from projects.generalize_ising_model.tools.utils import to_save_results, makedir, to_generate_randon_graph, save_graph
    from projects.generalize_ising_model.phi_project.utils import to_estimate_tpm_from_ising_model, to_calculate_mean_phi, \
        to_save_phi

    makedir(output_directory + '/phi/')
    makedir(output_directory + '/ising/')

    size = int(size)
    output_path_phi = output_directory + '/' + 'phi/' + str(count) + '/'
    output_path_ising = output_directory + '/' + 'ising/' + str(count) + '/'

    if makedir(output_path_ising) and makedir(output_path_phi):

        J = save_graph(
            output_path_phi + 'Jij_' + str(count) + '.csv',
            to_generate_randon_graph(size, isolate=False, weighted=True))

        # Ising Parameters
        temperature_parameters = temperature_parameters  # Temperature parameters (initial tempeture, final tempeture, number of steps)
        no_simulations = no_simulations  # Number of simulation after thermalization
        thermalize_time = thermalize_time  #

        start_time = time.time()
        print('Fitting Generalized Ising model for a ', size, ' by', size,
              ' random matrix.')
        simulated_fc, critical_temperature, E, M, S, H, spin_mean = generalized_ising(
            J,
            temperature_parameters=temperature_parameters,
            no_simulations=no_simulations,
            thermalize_time=thermalize_time,
            temperature_distribution='log')

        to_save_results(temperature_parameters,
                        J,
                        E,
                        M,
                        S,
                        H,
                        simulated_fc,
                        critical_temperature,
                        output_path_ising,
                        temperature_distribution='log')
        print('It took ',
              time.time() - start_time,
              'seconds to fit the generalized ising model')

        print('Computing Phi for random ' + str(size) + ' by ' + str(size) +
              ' matrix')

        ts = np.logspace(temperature_parameters[0],
                         np.log10(temperature_parameters[1]),
                         temperature_parameters[2])

        phi_temperature, phi_sum, phi_sus = [], [], []
        cont = 0

        start_time = time.time()

        for t in ts:
            # print('Temperature: ' + str(t))
            tpm, fpm = to_estimate_tpm_from_ising_model(J, t)
            phi_, phiSum, phiSus = to_calculate_mean_phi(
                fpm, spin_mean[:, cont], t)
            phi_temperature.append(phi_)
            phi_sum.append(phiSum)
            phi_sus.append(phiSus)

            cont += 1

        to_save_phi(ts, phi_temperature, phi_sum, phi_sus, S,
                    critical_temperature, size, output_path_phi)

        print('It takes ',
              time.time() - start_time, 'seconds to compute phi for a ', size,
              'by', size, ' random matrix.')
    else:
        print(str(count) + ' is already done!')
Exemple #5
0
    print('Extracting groups from: ' + network_name)
    for num in n_groups:
        print('Number of groups: ' + str(num))
        network_centroids = network['Centroid (MNI)'].tolist()

        centroid_list_splited = [[np.float(value) for value in centroid.split(' ')] for centroid in network_centroids]
        network_centroids_nd = np.asarray(centroid_list_splited)

        if num <= network_centroids_nd.shape[0]:
            kmeans = KMeans(n_clusters=num)
            kmeans.fit(network_centroids_nd)

            y_kmeans = kmeans.predict(network_centroids_nd)

            indexs = network['ParcelID'].tolist()

            new_img_data = np.zeros(data_img.shape)

            for index, group in zip(indexs, y_kmeans):
                temporal_img_data = np.zeros(data_img.shape)
                temporal_img_data[np.where(data_img == index)] = 1

                temporal_img_data = ndim.binary_fill_holes(temporal_img_data)
                temporal_img_data = ndim.binary_dilation(temporal_img_data)
                temporal_img_data = ndim.binary_fill_holes(temporal_img_data)

                new_img_data[temporal_img_data] = group + 1

            makedir(output_path + network_name + '/')
            nib.save(nib.Nifti1Image(new_img_data.astype(np.float32), affine_img), output_path + network_name + '/' + network_name + '_parcellation_' + str(num) + '.nii')