{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "dab18dbb", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy.misc import face\n", "from scipy.ndimage import zoom\n", "from scipy.special import logsumexp\n", "import torch\n", "import matplotlib.pyplot as plt\n", "import pickle\n", "import scipy.io\n", "import cv2\n", "import os\n", "import pandas as pd\n", "from scipy.stats import pearsonr, spearmanr" ] }, { "cell_type": "code", "execution_count": null, "id": "b48a6642", "metadata": {}, "outputs": [], "source": [ "def load_images_from_folder(folder):\n", " images = []\n", " img_name = []\n", " for filename in os.listdir(folder):\n", " img = cv2.imread(os.path.join(folder,filename))\n", " if img is not None:\n", " images.append(img)\n", " img_name.append(filename)\n", " return images, img_name" ] }, { "cell_type": "code", "execution_count": null, "id": "a79f3e8f", "metadata": {}, "outputs": [], "source": [ "imgs, img_name = load_images_from_folder('stimuli')" ] }, { "cell_type": "code", "execution_count": null, "id": "c2d3ff2c", "metadata": {}, "outputs": [], "source": [ "img_name" ] }, { "cell_type": "code", "execution_count": null, "id": "d00d5fcb", "metadata": {}, "outputs": [], "source": [ "len(scipy.io.loadmat(fn)['Results']['Disp'])" ] }, { "cell_type": "code", "execution_count": null, "id": "6957bb28", "metadata": {}, "outputs": [], "source": [ "fn = '/home/pranjul/DeepGaze/Bachelorarbeit_Christine_Huschens/results/S04/FA_Block1.mat'" ] }, { "cell_type": "code", "execution_count": null, "id": "6af13d03", "metadata": { "scrolled": true }, "outputs": [], "source": [ "len(scipy.io.loadmat(fn)['Results']['FixData'][0][0][0][63][0])" ] }, { "cell_type": "code", "execution_count": null, "id": "67e9c2eb", "metadata": {}, "outputs": [], "source": [ "fn = '/home/pranjul/DeepGaze/Bachelorarbeit_Christine_Huschens/results/S04/FA_Block1.mat'\n", "\n", "for i in range(64):\n", " gavx, gavy, sttime, entime = [], [], [], []\n", " \n", " for j in range(len(scipy.io.loadmat(fn)['Results']['FixData'][0][0][0][i][0])):\n", " gavx.append(scipy.io.loadmat(fn)['Results']['FixData'][0][0][0][i][0][j][18][0][0])\n", " gavy.append(scipy.io.loadmat(fn)['Results']['FixData'][0][0][0][i][0][j][19][0][0])\n", " sttime.append(scipy.io.loadmat(fn)['Results']['FixData'][0][0][0][i][0][j][4][0][0])\n", " entime.append(scipy.io.loadmat(fn)['Results']['FixData'][0][0][0][i][0][j][5][0][0])\n", "\n", " fixendtimes = scipy.io.loadmat(fn)['Results']['FixEndTimes'][0][0][0][i][0]\n", " stim_image_name = scipy.io.loadmat(fn)['Results']['ImPath'][0][0][0][i][0].split('\\\\')[-1]\n", " stim_folder_name = scipy.io.loadmat(fn)['Results']['ImPath'][0][0][0][i][0].split('\\\\')[-2]\n", "\n", " break\n", "\n", "gavx = np.array(gavx)\n", "gavy = np.array(gavy)\n", "sttime = np.array(sttime)\n", "entime = np.array(entime)\n", "\n", "res_width = scipy.io.loadmat(fn)['Results']['Disp'][0][0][0][0][2][0][0][0][0][0]\n", "res_height = scipy.io.loadmat(fn)['Results']['Disp'][0][0][0][0][2][0][0][1][0][0]\n", "ImWidth = scipy.io.loadmat(fn)['Results']['ImWidth'][0][0][0][0]\n", "ImHeight = scipy.io.loadmat(fn)['Results']['ImHeight'][0][0][0][0]\n", "\n", "FixX = np.round(gavx) - (res_width/2) + (ImWidth/2)\n", "FixY = np.round(gavy) - (res_height/2) + (ImHeight/2)\n", "FixDur = entime - sttime\n", "FixOnset = fixendtimes - FixDur + FixDur*.999\n", "\n", "exclude_ind = np.unique([np.where(FixDur < 100)[0][0], np.where(FixOnset < 0)[0][0]])\n", "FixX = np.delete(FixX, exclude_ind)\n", "FixY = np.delete(FixY, exclude_ind)\n", "FixDur = np.delete(FixDur, exclude_ind)\n", "FixOnset = np.delete(FixOnset, exclude_ind)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "87e18819", "metadata": {}, "outputs": [], "source": [ "image = cv2.imread('/home/pranjul/DeepGaze/Bachelorarbeit_Christine_Huschens/stimuli/' + stim_folder_name + '/' + stim_image_name)\n", "image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n", "image = cv2.resize(image, (ImWidth, ImHeight))\n", "plt.imshow(image)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e07be79d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "17977642", "metadata": {}, "outputs": [], "source": [ "image.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "aad511e9", "metadata": {}, "outputs": [], "source": [ "scipy.io.loadmat(fn)['Results']['ImPath'][0][0][0][i][0].split('\\\\')[-2]" ] }, { "cell_type": "code", "execution_count": null, "id": "6396d916", "metadata": {}, "outputs": [], "source": [ "\n", "#fixation_history_x = fix_X[i]/3\n", "#print(fixation_history_x)\n", "#fixation_history_y = fix_Y[i]/3\n", "#radius_history = radius[i]/5\n", "\n", "#print(fixation_history_x, fixation_history_y, radius_history)\n", "\n", "# Create a 2D matrix filled with zeros of size (600, 800)\n", "matrix_size = (1200, 1200)\n", "matrix = np.zeros(matrix_size, dtype=int)\n", "\n", "# Call the function to add circles to the matrix\n", "result_matrix = add_circles(matrix, FixY, FixX, FixDur/5)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6f396faf", "metadata": {}, "outputs": [], "source": [ "plt.imshow(result_matrix)" ] }, { "cell_type": "code", "execution_count": null, "id": "7c35bf1b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "2b9b0899", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "e52c9c1a", "metadata": {}, "outputs": [], "source": [ "FixX(iFix) = round(FixData(iFix).gavx - Results.Disp.Resolution.width./2 + Results.ImWidth/2); \n", "FixY(iFix) = round(FixData(iFix).gavy - Results.Disp.Resolution.height./2 + Results.ImHeight/2);\n", "FixDur(iFix) = FixData(iFix).entime - FixData(iFix).sttime;\n", "FixOnset(iFix) = Results.FixEndTimes{iIm}(iFix) - FixDur(iFix);\n", "% Bug fix to correct error in fixation onsets\n", "FixOnset(iFix) = FixOnset(iFix) + FixDur(iFix)*.999;" ] }, { "cell_type": "code", "execution_count": null, "id": "8e8787a9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "412324a5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "d2ebb782", "metadata": {}, "outputs": [], "source": [ "def load_fix_from_folder(folder):\n", " fix_X = []\n", " fix_Y = []\n", " radius = []\n", " img_name = []\n", " for filename in os.listdir(folder):\n", " fix_X.append(scipy.io.loadmat(os.path.join(folder,filename))['currImData'][:,4])\n", " fix_Y.append(scipy.io.loadmat(os.path.join(folder,filename))['currImData'][:,5])\n", " radius.append(scipy.io.loadmat(os.path.join(folder,filename))['currImData'][:,6])\n", " img_name.append(str(scipy.io.loadmat(os.path.join(folder,filename))['currImName'][0][0]) + '.jpg')\n", " #print(filename)\n", " #print(img_name)\n", " return fix_X, fix_Y, radius, img_name" ] }, { "cell_type": "code", "execution_count": null, "id": "78ddfb7d", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "def create_folder(folder_path):\n", " try:\n", " os.mkdir(folder_path)\n", " print(f\"Folder '{folder_path}' created successfully.\")\n", " except FileExistsError:\n", " print(f\"Folder '{folder_path}' already exists.\")\n", " except Exception as e:\n", " print(f\"An error occurred: {e}\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "585be960", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "def folder_exists(folder_path):\n", " return os.path.exists(folder_path) and os.path.isdir(folder_path)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "25fa0752", "metadata": {}, "outputs": [], "source": [ "def add_circles(matrix, x_list, y_list, r_list):\n", " for x, y, r in zip(x_list, y_list, r_list):\n", " x, y, r = int(x), int(y), int(r)\n", " for i in range(max(0, y - r), min(matrix.shape[0], y + r + 1)):\n", " for j in range(max(0, x - r), min(matrix.shape[1], x + r + 1)):\n", " if (i - y) ** 2 + (j - x) ** 2 <= r ** 2:\n", " matrix[i][j] += 1\n", " return matrix" ] }, { "cell_type": "code", "execution_count": null, "id": "bd2da5f7", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from scipy.misc import face\n", "from scipy.ndimage import zoom\n", "from scipy.special import logsumexp\n", "import torch\n", "\n", "import deepgaze_pytorch\n", "\n", "DEVICE = 'cuda'\n", "\n", "# you can use DeepGazeI or DeepGazeIIE\n", "model = deepgaze_pytorch.DeepGazeIII(pretrained=True).to(DEVICE)\n", "\n", "#image = face()\n", "\n", "n_f = '/home/pranjul/DeepGaze/Bachelorarbeit_Christine_Huschens_(1)/results/S'\n", "\n", "for q in range(30, 41):\n", " \n", " x = []\n", " \n", " # Replace 'path/to/your/folder' with the folder path you want to check\n", " folder_path = n_f + str(q)\n", " \n", " if folder_exists(folder_path):\n", " \n", " #fix_X, fix_Y, radius, img_name = load_fix_from_folder('S_fix/S'+ str(q) +'_fix')\n", "\n", " # Replace 'path/to/your/folder' with the desired folder path\n", " folder_path = '/home/pranjul/DeepGaze/Bachelorarbeit_Christine_Huschens/DG3_HG_heatmaps_c/S'+ str(q) +'_fix_c'\n", " create_folder(folder_path)\n", "\n", " for file in sorted(os.listdir(n_f + str(q))):\n", " if 'Block' in file:\n", " print(file) \n", "\n", " fn = n_f + str(q) + '/' + file\n", "\n", " for i in range(64):\n", " gavx, gavy, sttime, entime = [], [], [], []\n", "\n", " for j in range(len(scipy.io.loadmat(fn)['Results']['FixData'][0][0][0][i][0])):\n", " gavx.append(scipy.io.loadmat(fn)['Results']['FixData'][0][0][0][i][0][j][18][0][0])\n", " gavy.append(scipy.io.loadmat(fn)['Results']['FixData'][0][0][0][i][0][j][19][0][0])\n", " sttime.append(scipy.io.loadmat(fn)['Results']['FixData'][0][0][0][i][0][j][4][0][0])\n", " entime.append(scipy.io.loadmat(fn)['Results']['FixData'][0][0][0][i][0][j][5][0][0])\n", "\n", " fixendtimes = scipy.io.loadmat(fn)['Results']['FixEndTimes'][0][0][0][i][0]\n", " stim_image_name = scipy.io.loadmat(fn)['Results']['ImPath'][0][0][0][i][0].split('\\\\')[-1]\n", " stim_folder_name = scipy.io.loadmat(fn)['Results']['ImPath'][0][0][0][i][0].split('\\\\')[-2]\n", "\n", " #break\n", "\n", " gavx = np.array(gavx)\n", " gavy = np.array(gavy)\n", " sttime = np.array(sttime)\n", " entime = np.array(entime)\n", "\n", " res_width = scipy.io.loadmat(fn)['Results']['Disp'][0][0][0][0][2][0][0][0][0][0]\n", " res_height = scipy.io.loadmat(fn)['Results']['Disp'][0][0][0][0][2][0][0][1][0][0]\n", " ImWidth = scipy.io.loadmat(fn)['Results']['ImWidth'][0][0][0][0]\n", " ImHeight = scipy.io.loadmat(fn)['Results']['ImHeight'][0][0][0][0]\n", "\n", " FixX = np.round(gavx) - (res_width/2) + (ImWidth/2)\n", " FixY = np.round(gavy) - (res_height/2) + (ImHeight/2)\n", " FixDur = entime - sttime\n", " FixOnset = fixendtimes - FixDur + FixDur\n", " \n", " #print(FixDur)\n", " #print(FixOnset)\n", " dur_indices = np.where(FixDur < 100)[0]\n", " onset_indices = np.where(FixOnset < 0)[0]\n", "\n", " exclude_ind = np.unique(np.concatenate([dur_indices, onset_indices]))\n", " \n", " print(exclude_ind)\n", " FixX = np.delete(FixX, exclude_ind)\n", " FixY = np.delete(FixY, exclude_ind)\n", " FixDur = np.delete(FixDur, exclude_ind)\n", " FixOnset = np.delete(FixOnset, exclude_ind) \n", " \n", " image = cv2.imread('/home/pranjul/DeepGaze/Bachelorarbeit_Christine_Huschens/stimuli/' + stim_folder_name + '/' + stim_image_name)\n", " create_folder(os.path.join(folder_path, stim_folder_name))\n", " \n", " #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n", " image = cv2.resize(image, (ImWidth, ImHeight)) \n", " \n", " if image is not None and len(FixX) > 3 and len(FixY > 3):\n", "\n", " # location of previous scanpath fixations in x and y (pixel coordinates), starting with the initial fixation on the image.\n", " #fixation_history_x = np.array([1024//2, 300, 500, 200, 200, 700])\n", " #fixation_history_y = np.array([768//2, 300, 100, 300, 100, 500])\n", "\n", " #print(img_name[i])\n", "\n", " fixation_history_x = FixX\n", " #print(fixation_history_x)\n", " fixation_history_y = FixY\n", " #radius_history = radius[i]/5\n", "\n", " #print(fixation_history_x, fixation_history_y, radius_history)\n", "\n", " # Create a 2D matrix filled with zeros of size (600, 800)\n", " matrix_size = (ImWidth, ImHeight)\n", " matrix = np.zeros(matrix_size, dtype=int)\n", "\n", " # Call the function to add circles to the matrix\n", " result_matrix = add_circles(matrix, FixX, FixY, FixDur/5)\n", "\n", " #plt.imshow(result_matrix)\n", " #plt.plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " #plt.axis('on')\n", " #plt.colorbar(fraction=0.046, pad=0.04) # Adjust fraction and pad values as needed\n", " #plt.tight_layout()\n", "\n", " # load precomputed centerbias log density (from MIT1003) over a 1024x1024 image\n", " # you can download the centerbias from https://github.com/matthias-k/DeepGaze/releases/download/v1.0.0/centerbias_mit1003.npy\n", " # alternatively, you can use a uniform centerbias via `centerbias_template = np.zeros((1024, 1024))`.\n", " centerbias_template = np.load('centerbias_mit1003.npy')\n", "\n", " # rescale to match image size\n", " centerbias = zoom(centerbias_template, (image.shape[0]/centerbias_template.shape[0], image.shape[1]/centerbias_template.shape[1]), order=0, mode='nearest')\n", " # renormalize log density\n", " centerbias -= logsumexp(centerbias)\n", "\n", " image_tensor = torch.tensor([image.transpose(2, 0, 1)]).to(DEVICE)\n", " centerbias_tensor = torch.tensor([centerbias]).to(DEVICE)\n", " x_hist_tensor = torch.tensor([fixation_history_x[model.included_fixations]]).to(DEVICE)\n", " y_hist_tensor = torch.tensor([fixation_history_y[model.included_fixations]]).to(DEVICE)\n", "\n", " log_density_prediction = model(image_tensor, centerbias_tensor, x_hist_tensor, y_hist_tensor)\n", "\n", " # Scale factor\n", " #scale_factor = 3\n", "\n", " # Calculate the new width and height\n", " #new_width = image.shape[1] * scale_factor\n", " #new_height = image.shape[0] * scale_factor\n", "\n", " # Resize the image using cv2.resize()\n", " #image = cv2.resize(image, (new_width, new_height))\n", "\n", " image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n", "\n", " x.append((log_density_prediction.detach().cpu().numpy()[0, 0], str(stim_folder_name), str(stim_image_name.split('.')[0]),\n", " 'S' + str(q), result_matrix))\n", "\n", " f, axs = plt.subplots(nrows=1, ncols=3, figsize=(16, 9))\n", " axs[0].imshow(image)\n", " axs[0].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " axs[0].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='white', zorder=100)\n", " axs[0].set_axis_off()\n", " axs[1].matshow(log_density_prediction.detach().cpu().numpy()[0, 0]) # first image in batch, first (and only) channel\n", " axs[1].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " axs[1].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='white', zorder=100)\n", " axs[1].set_axis_off()\n", " axs[2].matshow(result_matrix)\n", " axs[2].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " axs[2].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='white', zorder=100)\n", " axs[2].set_axis_off()\n", " plt.tight_layout()\n", " plt.savefig(os.path.join(folder_path, stim_folder_name, stim_image_name.split('.')[0] + '.png'))\n", " #plt.show()\n", " plt.close()\n", " #break\n", "\n", " # Open a file in binary write mode\n", " with open(folder_path + '/' + 'S' + str(q) + '.pkl', 'wb') as file:\n", " pickle.dump(x, file)\n", "\n", " #break\n", " #break" ] }, { "cell_type": "code", "execution_count": null, "id": "d083b6bf", "metadata": {}, "outputs": [], "source": [ "np.shape(x)" ] }, { "cell_type": "code", "execution_count": null, "id": "3b1a8ba5", "metadata": {}, "outputs": [], "source": [ "32*8" ] }, { "cell_type": "code", "execution_count": 2, "id": "970201dc", "metadata": {}, "outputs": [], "source": [ "x_loaded = {}" ] }, { "cell_type": "code", "execution_count": 7, "id": "072a0b13", "metadata": {}, "outputs": [], "source": [ "for q in range(10, 41):\n", "\n", " # Replace 'path/to/your/folder' with the folder path you want to check\n", " folder_path = '/raid/pranjul/DG3_HG_heatmaps_c/S'+ str(q) +'_fix_c'\n", " \n", " if folder_exists(folder_path):\n", " # Open a file in binary write mode\n", " with open('/raid/pranjul/DG3_HG_heatmaps_c/S'+ str(q) +'_fix_c/' + 'S'+ str(q) + '.pkl', 'rb') as file:\n", " x_loaded[q] = pickle.load(file)\n", "\n", "#x_loaded = [x.tolist() for x in x_loaded]" ] }, { "cell_type": "code", "execution_count": 8, "id": "eef8a427", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "37" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(x_loaded)" ] }, { "cell_type": "code", "execution_count": null, "id": "532e15cd", "metadata": {}, "outputs": [], "source": [ "x_loaded" ] }, { "cell_type": "code", "execution_count": null, "id": "a4e0f88c", "metadata": { "scrolled": true }, "outputs": [], "source": [ "x_loaded[4]" ] }, { "cell_type": "code", "execution_count": null, "id": "4d0b7c18", "metadata": {}, "outputs": [], "source": [ "plt.imshow(x_loaded[4][0][0])" ] }, { "cell_type": "code", "execution_count": null, "id": "7253f70f", "metadata": {}, "outputs": [], "source": [ "plt.imshow(x_loaded[4][0][4])" ] }, { "cell_type": "code", "execution_count": 10, "id": "48886c2d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9472" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "256*37" ] }, { "cell_type": "code", "execution_count": null, "id": "fb4e8b68", "metadata": {}, "outputs": [], "source": [ "for i in range(2):\n", " print(f\"Iter:{i+1}\")\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "9743923c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " dg3 stim_folder \\\n", "0 [[-21.89091177095421, -21.89091177095421, -21.... pareidolia \n", "1 [[-23.752590900618024, -23.752590900618024, -2... pareidolia_inv \n", "2 [[-22.717763718089778, -22.717763718089778, -2... faces \n", "3 [[-23.13883206422637, -23.13883206422637, -23.... objects \n", "4 [[-20.465820021512595, -20.465820021512595, -2... pareidolia_inv \n", "... ... ... \n", "9204 [[-22.03320704181483, -22.03320704181483, -22.... objects_inv \n", "9205 [[-19.347162311089896, -19.347162311089896, -1... pareidolia_inv \n", "9206 [[-21.903870228837253, -21.903870228837253, -2... objects \n", "9207 [[-19.344542363086855, -19.344542363086855, -1... faces_inv \n", "9208 [[-24.540417387594943, -24.540417387594943, -2... pareidolia_inv \n", "\n", " stim_name sub hg \n", "0 64 S04 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... \n", "1 56_inv S04 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... \n", "2 face31 S04 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... \n", "3 16_match S04 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... \n", "4 46_inv S04 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... \n", "... ... ... ... \n", "9204 74_match_inv S40 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... \n", "9205 15_inv S40 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... \n", "9206 39_match S40 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... \n", "9207 face12_inv S40 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... \n", "9208 06_inv S40 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... \n", "\n", "[9209 rows x 5 columns]\n" ] } ], "source": [ "import pandas as pd\n", "\n", "# Assuming x_loaded is a dictionary\n", "data_frames = []\n", "\n", "for key, value in x_loaded.items():\n", " df = pd.DataFrame(value, columns=['dg3', 'stim_folder', 'stim_name', 'sub', 'hg'])\n", " data_frames.append(df)\n", "\n", "# Concatenate DataFrames\n", "df = pd.concat(data_frames, ignore_index=True)\n", "\n", "# Display the resulting DataFrame\n", "print(df)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "4d51aabe", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 11, "id": "9fc92e53", "metadata": {}, "outputs": [], "source": [ "df_agg_hg = df.groupby(['stim_folder', 'stim_name'])['hg'].apply(lambda x: np.mean(x.tolist(), axis=0)).reset_index()\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "2e9d4054", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
stim_folderstim_namehg
0facesface01[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
1facesface02[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
2facesface03[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
3facesface04[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
4facesface05[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
............
251pareidolia_inv75_inv[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
252pareidolia_inv78_inv[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
253pareidolia_inv80_inv[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
254pareidolia_inv81_inv[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
255pareidolia_inv83_inv[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
\n", "

256 rows × 3 columns

\n", "
" ], "text/plain": [ " stim_folder stim_name \\\n", "0 faces face01 \n", "1 faces face02 \n", "2 faces face03 \n", "3 faces face04 \n", "4 faces face05 \n", ".. ... ... \n", "251 pareidolia_inv 75_inv \n", "252 pareidolia_inv 78_inv \n", "253 pareidolia_inv 80_inv \n", "254 pareidolia_inv 81_inv \n", "255 pareidolia_inv 83_inv \n", "\n", " hg \n", "0 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "1 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "2 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "3 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "4 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", ".. ... \n", "251 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "252 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "253 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "254 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "255 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "\n", "[256 rows x 3 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_agg_hg" ] }, { "cell_type": "code", "execution_count": 14, "id": "1f15b7de", "metadata": {}, "outputs": [], "source": [ "\n", "import base64\n", "# Define a function to serialize the 2D arrays\n", "def serialize_array(arr):\n", " return base64.b64encode(pickle.dumps(arr)).decode('utf-8')\n", "\n", "# Apply the serialization function to the column\n", "df_agg_hg['hg'] = df_agg_hg['hg'].apply(serialize_array)\n", "\n", "# Save the DataFrame to a CSV file\n", "df_agg_hg.to_csv('/raid/pranjul/agg_hg_37_subs_c.csv', index=False)" ] }, { "cell_type": "code", "execution_count": 15, "id": "709b8028", "metadata": {}, "outputs": [], "source": [ "\n", "\n", "# Load the DataFrame from the CSV file\n", "loaded_df_csv = pd.read_csv('/raid/pranjul/agg_hg_37_subs_c.csv')\n", "\n", "# Define a function to deserialize the 2D arrays\n", "def deserialize_array(serialized_arr):\n", " return pickle.loads(base64.b64decode(serialized_arr.encode('utf-8')))\n", "\n", "# Apply the deserialization function to the column\n", "loaded_df_csv['hg'] = loaded_df_csv['hg'].apply(deserialize_array)\n", "\n", "# Now, loaded_df contains the original DataFrame with 2D arrays in 'Array_Column'" ] }, { "cell_type": "code", "execution_count": 16, "id": "44d66ffb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
stim_folderstim_namehg
0facesface01[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
1facesface02[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
2facesface03[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
3facesface04[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
4facesface05[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
............
251pareidolia_inv75_inv[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
252pareidolia_inv78_inv[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
253pareidolia_inv80_inv[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
254pareidolia_inv81_inv[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
255pareidolia_inv83_inv[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
\n", "

256 rows × 3 columns

\n", "
" ], "text/plain": [ " stim_folder stim_name \\\n", "0 faces face01 \n", "1 faces face02 \n", "2 faces face03 \n", "3 faces face04 \n", "4 faces face05 \n", ".. ... ... \n", "251 pareidolia_inv 75_inv \n", "252 pareidolia_inv 78_inv \n", "253 pareidolia_inv 80_inv \n", "254 pareidolia_inv 81_inv \n", "255 pareidolia_inv 83_inv \n", "\n", " hg \n", "0 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "1 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "2 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "3 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "4 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", ".. ... \n", "251 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "252 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "253 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "254 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "255 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,... \n", "\n", "[256 rows x 3 columns]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loaded_df_csv" ] }, { "cell_type": "code", "execution_count": null, "id": "cad6488c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "90dcd74f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3334d903", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "d8eea8a4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7a5b6082", "metadata": {}, "outputs": [], "source": [ "sp_corr = []\n", "for i in range(len(df['dg3'])):\n", " sp_corr.append(spearmanr(df['dg3'][i].flatten(), df['hg'][i].flatten())[0])\n", " #break\n" ] }, { "cell_type": "code", "execution_count": null, "id": "76a4cfab", "metadata": {}, "outputs": [], "source": [ "import pickle\n", "\n", "# Save the list to a file\n", "with open('/raid/pranjul/sp_corr_dg3_37_subs_c.pkl', 'wb') as file:\n", " pickle.dump(sp_corr, file)" ] }, { "cell_type": "code", "execution_count": null, "id": "19b45d8a", "metadata": {}, "outputs": [], "source": [ "# Load the list from the file\n", "with open('/raid/pranjul/sp_corr_dg3_37_subs_c.pkl', 'rb') as file:\n", " loaded_list = pickle.load(file)\n", "\n", "print(loaded_list)" ] }, { "cell_type": "code", "execution_count": null, "id": "9969e070", "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "id": "98174519", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b86e879c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a5aaf588", "metadata": {}, "outputs": [], "source": [ "# Add the Spearman correlation values to the DataFrame\n", "df['sp_corr'] = sp_corr" ] }, { "cell_type": "code", "execution_count": null, "id": "3393ccd0", "metadata": {}, "outputs": [], "source": [ "df = df.drop(columns=['dg3', 'hg'])" ] }, { "cell_type": "code", "execution_count": null, "id": "74823bb5", "metadata": {}, "outputs": [], "source": [ "len(df['sp_corr'])" ] }, { "cell_type": "code", "execution_count": null, "id": "41901f26", "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "id": "f742b79b", "metadata": {}, "outputs": [], "source": [ "result_df" ] }, { "cell_type": "code", "execution_count": null, "id": "f060d3ba", "metadata": {}, "outputs": [], "source": [ "# Define the custom order\n", "custom_order = ['faces', 'faces_inv', 'objects', 'objects_inv', 'pareidolia', 'pareidolia_inv', 'pareidolia_art', 'pareidolia_art_inv']\n", "\n", "# Convert the 'stim_folder' column to a Categorical data type with the custom order\n", "result_df['stim_folder'] = pd.Categorical(result_df['stim_folder'], categories=custom_order, ordered=True)\n", "\n", "# Sort the DataFrame based on the custom order\n", "df_sorted = result_df.sort_values(by='stim_folder')\n", "\n", "# Print the sorted DataFrame\n", "print(df_sorted)" ] }, { "cell_type": "code", "execution_count": null, "id": "5a8fb762", "metadata": {}, "outputs": [], "source": [ "result_df" ] }, { "cell_type": "code", "execution_count": null, "id": "c6544090", "metadata": {}, "outputs": [], "source": [ "df['stim_folder']" ] }, { "cell_type": "code", "execution_count": null, "id": "588a1915", "metadata": {}, "outputs": [], "source": [ "df['stim_folder'].value_counts()" ] }, { "cell_type": "code", "execution_count": null, "id": "994c83f0", "metadata": { "scrolled": true }, "outputs": [], "source": [ "result_df['stim_folder'].value_counts()" ] }, { "cell_type": "code", "execution_count": null, "id": "c9ec7617", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7543ea0a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "86af740e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "2ca5da07", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "# Assuming df is your original DataFrame\n", "# If you don't have a DataFrame, you can create a sample one\n", "# df = pd.DataFrame({'stim_folder': ['objects']*32 + ['faces']*32 + ['pareidolia']*31 + ['pareidolia_art_inv']*31 + ['pareidolia_inv']*31 + ['pareidolia_art']*31 + ['objects_inv']*31 + ['faces_inv']*30})\n", "\n", "# Get the unique values and their counts\n", "value_counts = df['stim_folder'].value_counts()\n", "\n", "# Find the minimum count\n", "min_count = value_counts.min()\n", "\n", "# Identify rows to be removed for each unique value\n", "rows_to_remove = []\n", "\n", "for stim_folder, count in value_counts.items():\n", " if count > min_count:\n", " indices = df[df['stim_folder'] == stim_folder].sample(n=count - min_count).index\n", " rows_to_remove.extend(indices)\n", "\n", "# Remove the identified rows\n", "result_df = df.drop(rows_to_remove)\n", "\n", "# Display the result DataFrame\n", "print(result_df)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "76acbad9", "metadata": {}, "outputs": [], "source": [ "min_count" ] }, { "cell_type": "code", "execution_count": null, "id": "95468f6f", "metadata": { "scrolled": true }, "outputs": [], "source": [ "len(rows_to_remove)" ] }, { "cell_type": "code", "execution_count": null, "id": "f1716d59", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "4811d462", "metadata": {}, "outputs": [], "source": [ "from itertools import combinations\n", "\n", "# 'Object_and_face_identification'\n", "# Given array\n", "task_array = ['faces', 'faces_inv', 'objects', 'objects_inv', 'pareidolia', 'pareidolia_inv', 'pareidolia_art', 'pareidolia_art_inv']\n", "\n", "# Generate all unique combinations of two tasks\n", "combinations_list = list(combinations(task_array, 2))\n", "\n", "# Display the result\n", "print(combinations_list)" ] }, { "cell_type": "code", "execution_count": null, "id": "fd3bbdaa", "metadata": {}, "outputs": [], "source": [ "len(df['sp_corr'])" ] }, { "cell_type": "code", "execution_count": null, "id": "345e9b3f", "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "id": "65c5864f", "metadata": {}, "outputs": [], "source": [ "df[\"stim_folder\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "c2ffd235", "metadata": {}, "outputs": [], "source": [ "df[\"sp_corr\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "9163a8b5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "1af78875", "metadata": {}, "outputs": [], "source": [ "combinations_list" ] }, { "cell_type": "code", "execution_count": null, "id": "0bddc8f1", "metadata": {}, "outputs": [], "source": [ "result_df" ] }, { "cell_type": "code", "execution_count": null, "id": "fb842369", "metadata": {}, "outputs": [], "source": [ "df_sorted" ] }, { "cell_type": "code", "execution_count": null, "id": "d16e1945", "metadata": {}, "outputs": [], "source": [ "result_df" ] }, { "cell_type": "code", "execution_count": null, "id": "1eabdc8b", "metadata": { "scrolled": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "import seaborn as sns\n", "from statannotations.Annotator import Annotator\n", "\n", "x = \"stim_folder\"\n", "y = \"sp_corr\"\n", "\n", "# Filter the DataFrame based on the 'Time' condition\n", "subset_df = df_sorted\n", "\n", "# Increase the figure size and font size\n", "plt.figure(figsize=(6, 4))\n", "sns.set(style=\"white\", rc={\"axes.edgecolor\": \"black\", \"grid.color\": \"black\", \"grid.linestyle\": \":\"}, font_scale=1.5)\n", "\n", "# Create a palette with distinct colors\n", "palette = sns.color_palette(\"Set3\", n_colors=len(subset_df[x].unique())) # You can choose any other colormap\n", "\n", "# Create the barplot with the distinct color palette\n", "ax = sns.barplot(data=subset_df, x=x, y=y, palette=palette, capsize=0.1, errwidth=1.5) # Added capsize and errwidth for error bars\n", "#ax = sns.boxplot(data=subset_df, x=x, y=y, palette=palette) # Added capsize and errwidth for error bars\n", "\n", "'''\n", "# Add statistical annotations\n", "annot = Annotator(ax, combinations_list, data=subset_df, x=x, y=y)\n", "annot.new_plot(ax, combinations_list,\n", " data=subset_df, x=x, y=y)\n", "annot.configure(test='Kruskal', text_format='star', loc='outside',\n", " comparisons_correction=\"fdr_bh\",\n", " hide_non_significant=True,\n", " pvalue_thresholds=[[1e-3, '∗∗∗'], [1e-2, \"∗∗\"], [0.05, \"∗\"]], verbose=2)\n", "\n", "#annot.apply_test()\n", "\n", "# Adjust the position of stars between lines\n", "#annot.line_offset_to_group = 0.5\n", "#annot.line_offset = 0.3 # Adjust this value to move the stars closer to the lines\n", "#annot.text_offset = 0.05 # Adjust this value to move the stars closer to the lines\n", "#ax, test_results = annot.annotate(line_offset = 0.01)\n", "#ax, test_results = annot.annotate()\n", "\n", "ax, test_results = annot.apply_test().annotate(line_offset=0.001)\n", "'''\n", "# Remove top and right plot edges\n", "sns.despine()\n", "\n", "# Define explicit legend handles\n", "legend_labels = ['faces', 'faces_inv', 'objects', 'objects_inv', 'pareidolia', 'pareidolia_inv', 'pareidolia_art', 'pareidolia_art_inv']\n", "legend_handles = [plt.Rectangle((0, 0), 1, 1, color=color, label=label) for color, label in zip(palette, legend_labels)]\n", "\n", "# Create legend without edges and without error bars, and change the title to \"CNN\"\n", "legend = ax.legend(handles=legend_handles, title=\"\", title_fontsize='16', loc='upper left', bbox_to_anchor=(1, 1), frameon=False, fontsize=16)\n", "\n", "\n", "\n", "# Remove x-axis ticks and labels\n", "#ax.set_xticks([])\n", "#ax.set_xticklabels([])\n", "\n", "# Set y-axis limits from 0 to 1\n", "ax.set_ylim(0.25, 0.35)\n", "ax.set_yticks(np.linspace(0.25, 0.35, num=6))\n", "\n", "# Show the tick marks on the left side of the y-axis\n", "#ax.tick_params(axis='y', direction='out', length=5) # Adjust 'length' as needed\n", "ax.tick_params(tick1On=True)\n", "\n", "# Set font size for axis labels and title\n", "ax.set_ylabel('DG3 & HG correlation [Spearman\\'s r]', fontsize=16)\n", "#ax.set_ylabel('')\n", "ax.set_xlabel('2 (orientation) x 4 (stimuli condition)', fontsize=16)\n", "\n", "#ax.set_title('255ms:lay13-15', fontsize=18)\n", "\n", "# Set font size for tick labels\n", "ax.tick_params(axis='both', labelsize=16)\n", "\n", "# Uncomment the following lines to show y-axis ticks and tick labels\n", "# ax.set_yticks(np.linspace(0, 1.0, num=11)) # Uncomment if not already set\n", "# ax.set_yticklabels(['0.0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1.0'])\n", "\n", "#plt.savefig('fig_5_3.png', dpi= 600, bbox_inches='tight')\n", "#plt.savefig('fig_5_3.png', dpi= 600)\n", "\n", "\n", "# Show the plot\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1999e662", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "import seaborn as sns\n", "from statannotations.Annotator import Annotator\n", "\n", "x = \"stim_folder\"\n", "y = \"sp_corr\"\n", "\n", "# Filter the DataFrame based on the 'Time' condition\n", "subset_df = result_df\n", "\n", "# Increase the figure size and font size\n", "plt.figure(figsize=(6, 4))\n", "sns.set(style=\"white\", rc={\"axes.edgecolor\": \"black\", \"grid.color\": \"black\", \"grid.linestyle\": \":\"}, font_scale=1.5)\n", "\n", "# Create a palette with distinct colors\n", "palette = sns.color_palette(\"Set3\", n_colors=len(subset_df[x].unique())) # You can choose any other colormap\n", "\n", "# Create the barplot with the distinct color palette\n", "ax = sns.barplot(data=subset_df, x=x, y=y, palette=palette, capsize=0.1, errwidth=1.5) # Added capsize and errwidth for error bars\n", "# ax = sns.boxplot(data=subset_df, x=x, y=y, palette=palette) # Added capsize and errwidth for error bars\n", "\n", "\n", "# Add statistical annotations\n", "annot = Annotator(ax, combinations_list, data=subset_df, x=x, y=y)\n", "annot.new_plot(ax, combinations_list,\n", " data=subset_df, x=x, y=y)\n", "annot.configure(test='Kruskal', text_format='star', loc='outside',\n", " comparisons_correction=\"fdr_bh\",\n", " hide_non_significant=True,\n", " pvalue_thresholds=[[1e-3, '∗∗∗'], [1e-2, \"∗∗\"], [0.05, \"∗\"]], verbose=2, line_height = .05)\n", "\n", "#annot.apply_test()\n", "\n", "# Adjust the position of stars between lines\n", "#annot.line_offset_to_group = 0.5\n", "#annot.line_offset = 0.3 # Adjust this value to move the stars closer to the lines\n", "#annot.text_offset = 0.05 # Adjust this value to move the stars closer to the lines\n", "#ax, test_results = annot.annotate(line_offset = 0.01)\n", "#ax, test_results = annot.annotate()\n", "\n", "ax, test_results = annot.apply_test().annotate(line_offset=0.001)\n", "\n", "# Remove top and right plot edges\n", "sns.despine()\n", "\n", "# Define explicit legend handles\n", "legend_labels = ['faces', 'faces_inv', 'objects', 'objects_inv', 'pareidolia', 'pareidolia_inv', 'pareidolia_art', 'pareidolia_art_inv']\n", "legend_handles = [plt.Rectangle((0, 0), 1, 1, color=color, label=label) for color, label in zip(palette, legend_labels)]\n", "\n", "# Create legend without edges and without error bars, and change the title to \"CNN\"\n", "legend = ax.legend(handles=legend_handles, title=\"\", title_fontsize='16', loc='upper left', bbox_to_anchor=(1, 1), frameon=False, fontsize=16)\n", "\n", "\n", "\n", "# Remove x-axis ticks and labels\n", "#ax.set_xticks([])\n", "#ax.set_xticklabels([])\n", "\n", "# Set y-axis limits from 0 to 1\n", "#ax.set_ylim(0.25, 0.35)\n", "#ax.set_yticks(np.linspace(0.25, 0.35, num=6))\n", "\n", "# Show the tick marks on the left side of the y-axis\n", "#ax.tick_params(axis='y', direction='out', length=5) # Adjust 'length' as needed\n", "ax.tick_params(tick1On=True)\n", "\n", "# Set font size for axis labels and title\n", "ax.set_ylabel('DG3 & HG correlation [Spearman\\'s r]', fontsize=16)\n", "#ax.set_ylabel('')\n", "#ax.set_xlabel('2 (orientation) x 4 (stimuli condition)', fontsize=16)\n", "ax.set_xticklabels(ax.get_xticklabels(), rotation=90)\n", "\n", "\n", "#ax.set_title('255ms:lay13-15', fontsize=18)\n", "\n", "# Set font size for tick labels\n", "ax.tick_params(axis='both', labelsize=16)\n", "\n", "# Uncomment the following lines to show y-axis ticks and tick labels\n", "# ax.set_yticks(np.linspace(0, 1.0, num=11)) # Uncomment if not already set\n", "#ax.set_yticklabels(['0.0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1.0'])\n", "\n", "#plt.savefig('fig_5_3.png', dpi= 600, bbox_inches='tight')\n", "#plt.savefig('fig_5_3.png', dpi= 600)\n", "\n", "\n", "# Show the plot\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b2dc4e82", "metadata": {}, "outputs": [], "source": [ "print(\"Length of x:\", len(subset_df[x]))\n", "print(\"Length of y:\", len(subset_df[y]))" ] }, { "cell_type": "code", "execution_count": null, "id": "68b2c5b2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "bf6f386d", "metadata": {}, "outputs": [], "source": [ "loaded_df_csv" ] }, { "cell_type": "code", "execution_count": null, "id": "c57e732a", "metadata": {}, "outputs": [], "source": [ "df_agg_hg = loaded_df_csv" ] }, { "cell_type": "code", "execution_count": null, "id": "aa645e29", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy.misc import face\n", "from scipy.ndimage import zoom\n", "from scipy.special import logsumexp\n", "import torch\n", "import matplotlib.pyplot as plt\n", "\n", "import deepgaze_pytorch\n", "\n", "DEVICE = 'cuda'\n", "\n", "# you can use DeepGazeI or DeepGazeIIE\n", "model = deepgaze_pytorch.DeepGazeIIE(pretrained=True).to(DEVICE)\n", "\n", "# image = face()\n", "\n", "x = []\n", "\n", "\n", "for i in range(len(df_agg_hg)):\n", " \n", " stim_folder_name = df_agg_hg['stim_folder'][i]\n", " stim_image_name = df_agg_hg['stim_name'][i]\n", " \n", " folder_path = '/home/pranjul/DeepGaze/Bachelorarbeit_Christine_Huschens/DG2E_HG_heatmaps_c/'\n", " \n", " image = cv2.imread('/home/pranjul/DeepGaze/Bachelorarbeit_Christine_Huschens/stimuli/' + stim_folder_name + '/' + stim_image_name + '.tif')\n", " #create_folder(os.path.join(folder_path, stim_folder_name))\n", "\n", " #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n", " image = cv2.resize(image, (1200, 1200)) \n", " \n", " # load precomputed centerbias log density (from MIT1003) over a 1024x1024 image\n", " # you can download the centerbias from https://github.com/matthias-k/DeepGaze/releases/download/v1.0.0/centerbias_mit1003.npy\n", " # alternatively, you can use a uniform centerbias via `centerbias_template = np.zeros((1024, 1024))`.\n", " centerbias_template = np.load('centerbias_mit1003.npy')\n", " # centerbias_template = np.zeros((1024, 1024))\n", " # rescale to match image size\n", " centerbias = zoom(centerbias_template, (image.shape[0]/centerbias_template.shape[0], image.shape[1]/centerbias_template.shape[1]), order=0, mode='nearest')\n", " # renormalize log density\n", " centerbias -= logsumexp(centerbias)\n", "\n", " image_tensor = torch.tensor([image.transpose(2, 0, 1)]).to(DEVICE)\n", " centerbias_tensor = torch.tensor([centerbias]).to(DEVICE)\n", "\n", " log_density_prediction = model(image_tensor, centerbias_tensor)\n", " \n", " a = log_density_prediction.detach().cpu().numpy()[0,0]\n", " \n", " x.append(a)\n", " \n", " '''\n", " f, axs = plt.subplots(nrows=1, ncols=2, figsize=(16, 9))\n", " axs[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n", " # axs[0].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " # axs[0].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", " axs[0].set_axis_off()\n", " axs[1].matshow(log_density_prediction.detach().cpu().numpy()[0, 0]) # first image in batch, first (and only) channel\n", " # axs[1].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " # axs[1].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", " axs[1].set_axis_off()\n", " # plt.savefig(os.path.join('DG2_heatmaps', '{0}.jpg'.format(i)))\n", " \n", " \n", " f, axs = plt.subplots(nrows=1, ncols=3, figsize=(16, 9))\n", " axs[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n", " #axs[0].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " #axs[0].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='white', zorder=100)\n", " axs[0].set_axis_off()\n", " axs[1].matshow(log_density_prediction.detach().cpu().numpy()[0, 0]) # first image in batch, first (and only) channel\n", " #axs[1].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " #axs[1].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='white', zorder=100)\n", " axs[1].set_axis_off()\n", " axs[2].matshow(df_agg_hg['hg'][i])\n", " #axs[2].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " #axs[2].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='white', zorder=100)\n", " axs[2].set_axis_off()\n", " plt.tight_layout()\n", " plt.savefig(os.path.join(folder_path, stim_folder_name, stim_image_name + '.png'))\n", " #plt.show()\n", " plt.close()\n", " '''\n", " \n", " #break" ] }, { "cell_type": "code", "execution_count": null, "id": "61eaa85b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "91bef121", "metadata": {}, "outputs": [], "source": [ "len(x)" ] }, { "cell_type": "code", "execution_count": null, "id": "6bc2fa05", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "24f5fd26", "metadata": {}, "outputs": [], "source": [ "# Add the Spearman correlation values to the DataFrame\n", "df_agg_hg['dg2'] = x" ] }, { "cell_type": "code", "execution_count": null, "id": "6d007260", "metadata": {}, "outputs": [], "source": [ "df_agg_hg" ] }, { "cell_type": "code", "execution_count": null, "id": "7fecd938", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "fda8ef1e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "c030811c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "f707fe40", "metadata": {}, "outputs": [], "source": [ "\n", "\n", "# Define a function to serialize the 2D arrays\n", "def serialize_array(arr):\n", " return base64.b64encode(pickle.dumps(arr)).decode('utf-8')\n", "\n", "# Apply the serialization function to the column\n", "df_agg_hg['hg'] = df_agg_hg['hg'].apply(serialize_array)\n", "\n", "# Apply the serialization function to the column\n", "df_agg_hg['dg2'] = df_agg_hg['dg2'].apply(serialize_array)\n", "\n", "# Save the DataFrame to a CSV file\n", "df_agg_hg.to_csv('/raid/pranjul/agg_hg_dg2_26_subs_c.csv', index=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "412a8aa4", "metadata": {}, "outputs": [], "source": [ "import base64\n", "\n", "# Load the DataFrame from the CSV file\n", "loaded_df_csv = pd.read_csv('/raid/pranjul/agg_hg_dg2_26_subs_c.csv')\n", "\n", "# Define a function to deserialize the 2D arrays\n", "def deserialize_array(serialized_arr):\n", " return pickle.loads(base64.b64decode(serialized_arr.encode('utf-8')))\n", "\n", "# Apply the deserialization function to the column\n", "loaded_df_csv['hg'] = loaded_df_csv['hg'].apply(deserialize_array)\n", "\n", "# Apply the deserialization function to the column\n", "loaded_df_csv['dg2'] = loaded_df_csv['dg2'].apply(deserialize_array)\n", "\n", "# Now, loaded_df contains the original DataFrame with 2D arrays in 'Array_Column'" ] }, { "cell_type": "code", "execution_count": null, "id": "51936049", "metadata": {}, "outputs": [], "source": [ "loaded_df_csv" ] }, { "cell_type": "code", "execution_count": null, "id": "c9ff6f39", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "42e4c616", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "2db1eb6d", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "# Create two sample dataframes\n", "data1 = {'SharedColumn': [1, 2, 3, 4, 5],\n", " 'Data1_Column': ['A', 'B', 'C', 'D', 'E']}\n", "df1 = pd.DataFrame(data1)\n", "\n", "data2 = {'SharedColumn': [1, 1, 2, 2, 3],\n", " 'Data2_Column': [10, 20, 30, 40, 50]}\n", "df2 = pd.DataFrame(data2)\n", "\n", "# Merge dataframes based on the shared column\n", "merged_df = pd.merge(df1, df2, on='SharedColumn', how='outer')\n", "\n", "# Divide the dataframes based on unique values in the 'SharedColumn'\n", "unique_values_df1 = merged_df[merged_df['Data2_Column'].isnull()].drop('Data2_Column', axis=1)\n", "repeated_values_df2 = merged_df[merged_df['Data1_Column'].notnull()].drop('Data1_Column', axis=1)\n", "\n", "# Display the results\n", "print(\"Unique Values in df1:\")\n", "print(unique_values_df1)\n", "\n", "print(\"\\nRepeated Values in df2:\")\n", "print(repeated_values_df2)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "3fbe8c57", "metadata": {}, "outputs": [], "source": [ "df1" ] }, { "cell_type": "code", "execution_count": null, "id": "3964aa7b", "metadata": {}, "outputs": [], "source": [ "df2" ] }, { "cell_type": "code", "execution_count": null, "id": "72ad2dbf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "fba47112", "metadata": {}, "outputs": [], "source": [ "pd.merge(loaded_df_csv, df_sh, on='stim_folder', how='outer')" ] }, { "cell_type": "code", "execution_count": null, "id": "6873e2b2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b568d804", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "0ec6401a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "5fbccdc1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "e79d4b83", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "05bf148f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3492dab1", "metadata": {}, "outputs": [], "source": [ "sp_corr_dg2 = []\n", "for i in range(len(loaded_df_csv)):\n", " sp_corr_dg2.append(spearmanr(loaded_df_csv['dg2'][i].flatten(), loaded_df_csv['hg'][i].flatten())[0])\n", " #break" ] }, { "cell_type": "code", "execution_count": null, "id": "1cb4d2a9", "metadata": {}, "outputs": [], "source": [ "# Add the Spearman correlation values to the DataFrame\n", "loaded_df_csv['sp_corr_dg2'] = sp_corr_dg2" ] }, { "cell_type": "code", "execution_count": null, "id": "e9809da0", "metadata": {}, "outputs": [], "source": [ "loaded_df_csv" ] }, { "cell_type": "code", "execution_count": null, "id": "0d679042", "metadata": {}, "outputs": [], "source": [ "loaded_df_csv = loaded_df_csv.drop(columns=['dg2', 'hg'])" ] }, { "cell_type": "code", "execution_count": null, "id": "db0c138a", "metadata": {}, "outputs": [], "source": [ "loaded_df_csv" ] }, { "cell_type": "code", "execution_count": null, "id": "feb185b0", "metadata": {}, "outputs": [], "source": [ "loaded_df_csv['stim_folder'].value_counts()" ] }, { "cell_type": "code", "execution_count": null, "id": "36efceb1", "metadata": {}, "outputs": [], "source": [ "# Define the custom order\n", "custom_order = ['faces', 'faces_inv', 'objects', 'objects_inv', 'pareidolia', 'pareidolia_inv', 'pareidolia_art', 'pareidolia_art_inv']\n", "\n", "# Convert the 'stim_folder' column to a Categorical data type with the custom order\n", "loaded_df_csv['stim_folder'] = pd.Categorical(loaded_df_csv['stim_folder'], categories=custom_order, ordered=True)\n", "\n", "# Sort the DataFrame based on the custom order\n", "df_sorted = loaded_df_csv.sort_values(by='stim_folder')\n", "\n", "# Print the sorted DataFrame\n", "print(df_sorted)" ] }, { "cell_type": "code", "execution_count": null, "id": "3d9bcec6", "metadata": {}, "outputs": [], "source": [ "df_sorted" ] }, { "cell_type": "code", "execution_count": null, "id": "02efe074", "metadata": {}, "outputs": [], "source": [ "df_sorted_sh = pd.merge(df_sorted, df_sh, on='stim_folder', how='outer')" ] }, { "cell_type": "code", "execution_count": null, "id": "293790d5", "metadata": {}, "outputs": [], "source": [ "df_sorted_sh" ] }, { "cell_type": "code", "execution_count": null, "id": "7cea2334", "metadata": {}, "outputs": [], "source": [ "# Divide Column1 by Column2\n", "df_sorted_sh['sh_corrected'] = df_sorted_sh['sp_corr_dg2'] / df_sorted_sh['sp_corr_splt_hlf']" ] }, { "cell_type": "code", "execution_count": null, "id": "af406de8", "metadata": {}, "outputs": [], "source": [ "df_sorted_sh" ] }, { "cell_type": "code", "execution_count": null, "id": "5f2aa57c", "metadata": { "scrolled": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "import seaborn as sns\n", "from statannotations.Annotator import Annotator\n", "\n", "x = \"stim_folder\"\n", "y = \"sh_corrected\"\n", "\n", "# Filter the DataFrame based on the 'Time' condition\n", "subset_df = df_sorted_sh\n", "\n", "# Increase the figure size and font size\n", "plt.figure(figsize=(6, 4))\n", "sns.set(style=\"white\", rc={\"axes.edgecolor\": \"black\", \"grid.color\": \"black\", \"grid.linestyle\": \":\"}, font_scale=1.5)\n", "\n", "# Create a palette with distinct colors\n", "palette = sns.color_palette(\"Set3\", n_colors=len(subset_df[x].unique())) # You can choose any other colormap\n", "\n", "# Create the barplot with the distinct color palette\n", "ax = sns.barplot(data=subset_df, x=x, y=y, palette=palette, capsize=0.1, errwidth=1.5) # Added capsize and errwidth for error bars\n", "# ax = sns.boxplot(data=subset_df, x=x, y=y, palette=palette) # Added capsize and errwidth for error bars\n", "\n", "\n", "# Add statistical annotations\n", "annot = Annotator(ax, combinations_list, data=subset_df, x=x, y=y)\n", "annot.new_plot(ax, combinations_list,\n", " data=subset_df, x=x, y=y)\n", "annot.configure(test='Kruskal', text_format='star', loc='outside',\n", " comparisons_correction=\"fdr_bh\",\n", " hide_non_significant=True,\n", " pvalue_thresholds=[[1e-3, '∗∗∗'], [1e-2, \"∗∗\"], [0.05, \"∗\"]], verbose=2, line_height = .05)\n", "\n", "#annot.apply_test()\n", "\n", "# Adjust the position of stars between lines\n", "#annot.line_offset_to_group = 0.5\n", "#annot.line_offset = 0.3 # Adjust this value to move the stars closer to the lines\n", "#annot.text_offset = 0.05 # Adjust this value to move the stars closer to the lines\n", "#ax, test_results = annot.annotate(line_offset = 0.01)\n", "#ax, test_results = annot.annotate()\n", "\n", "ax, test_results = annot.apply_test().annotate(line_offset=0.2)\n", "\n", "# Remove top and right plot edges\n", "sns.despine()\n", "\n", "# Define explicit legend handles\n", "legend_labels = ['faces', 'faces_inv', 'objects', 'objects_inv', 'pareidolia', 'pareidolia_inv', 'pareidolia_art', 'pareidolia_art_inv']\n", "legend_handles = [plt.Rectangle((0, 0), 1, 1, color=color, label=label) for color, label in zip(palette, legend_labels)]\n", "\n", "# Create legend without edges and without error bars, and change the title to \"CNN\"\n", "legend = ax.legend(handles=legend_handles, title=\"\", title_fontsize='16', loc='upper left', bbox_to_anchor=(1, 1), frameon=False, fontsize=16)\n", "\n", "\n", "\n", "# Remove x-axis ticks and labels\n", "#ax.set_xticks([])\n", "#ax.set_xticklabels([])\n", "\n", "# Set y-axis limits from 0 to 1\n", "ax.set_ylim(0.9, 1.1)\n", "ax.set_yticks(np.linspace(0.9, 1.1, num=11))\n", "\n", "# Show the tick marks on the left side of the y-axis\n", "#ax.tick_params(axis='y', direction='out', length=5) # Adjust 'length' as needed\n", "ax.tick_params(tick1On=True)\n", "\n", "# Set font size for axis labels and title\n", "ax.set_ylabel('S-H corrected DG2E & HG correlation \\n [Spearman\\'s r]', fontsize=16)\n", "#ax.set_ylabel('')\n", "#ax.set_xlabel('2 (orientation) x 4 (stimuli condition)', fontsize=16)\n", "ax.set_xticklabels(ax.get_xticklabels(), rotation=90)\n", "\n", "\n", "#ax.set_title('255ms:lay13-15', fontsize=18)\n", "\n", "# Set font size for tick labels\n", "ax.tick_params(axis='both', labelsize=16)\n", "\n", "# Uncomment the following lines to show y-axis ticks and tick labels\n", "# ax.set_yticks(np.linspace(0, 1.0, num=11)) # Uncomment if not already set\n", "#ax.set_yticklabels(['0.0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1.0'])\n", "\n", "#plt.savefig('fig_5_3.png', dpi= 600, bbox_inches='tight')\n", "#plt.savefig('fig_5_3.png', dpi= 600)\n", "\n", "\n", "# Show the plot\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "24057961", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "5877b510", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a9a4da8a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "edfab771", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "8f454bd9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b7559c80", "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Get all unique values in the 'sub' column\n", "unique_subs = df['sub'].unique()\n", "unique_subs" ] }, { "cell_type": "code", "execution_count": null, "id": "cc285653", "metadata": {}, "outputs": [], "source": [ "# Randomly shuffle the unique values\n", "np.random.shuffle(unique_subs)\n", "unique_subs" ] }, { "cell_type": "code", "execution_count": null, "id": "486cf66c", "metadata": {}, "outputs": [], "source": [ "# Calculate the index to split at (half of the unique values)\n", "split_index = len(unique_subs) // 2\n", "split_index" ] }, { "cell_type": "code", "execution_count": null, "id": "162006ff", "metadata": { "scrolled": false }, "outputs": [], "source": [ "# Select the first half of unique values\n", "selected_subs_df1 = unique_subs[:split_index]\n", "selected_subs_df1" ] }, { "cell_type": "code", "execution_count": null, "id": "e9ad53ad", "metadata": {}, "outputs": [], "source": [ "# Split the DataFrame into two based on the selected unique values\n", "df1 = df[df['sub'].isin(selected_subs_df1)]\n", "df2 = df[~df['sub'].isin(selected_subs_df1)]" ] }, { "cell_type": "code", "execution_count": null, "id": "783a8dd0", "metadata": {}, "outputs": [], "source": [ "df1" ] }, { "cell_type": "code", "execution_count": null, "id": "40436fc4", "metadata": {}, "outputs": [], "source": [ "df2" ] }, { "cell_type": "code", "execution_count": null, "id": "9d683c68", "metadata": {}, "outputs": [], "source": [ "df1_hg = df1.groupby(['stim_folder', 'stim_name'])['hg'].apply(lambda x: np.mean(x.tolist(), axis=0)).reset_index()\n", "\n", "# Rename the column 'old_col_name' to 'new_col_name'\n", "df1_hg = df1_hg.rename(columns={'hg': 'hg_1'})\n", "df1_hg" ] }, { "cell_type": "code", "execution_count": null, "id": "6c8fa6bb", "metadata": {}, "outputs": [], "source": [ "df2_hg = df2.groupby(['stim_folder', 'stim_name'])['hg'].apply(lambda x: np.mean(x.tolist(), axis=0)).reset_index()\n", "\n", "# Rename the column 'old_col_name' to 'new_col_name'\n", "df2_hg = df2_hg.rename(columns={'hg': 'hg_2'})\n", "df2_hg" ] }, { "cell_type": "code", "execution_count": null, "id": "0b460ad5", "metadata": {}, "outputs": [], "source": [ "# Merge the DataFrames based on 'stim_folder' and 'stim_name'\n", "merged_df = pd.merge(df1_hg, df2_hg, on=['stim_folder', 'stim_name'], how='inner')\n", "\n", "# Display the resulting merged DataFrame\n", "print(merged_df)" ] }, { "cell_type": "code", "execution_count": null, "id": "3be7535f", "metadata": {}, "outputs": [], "source": [ "plt.imshow(merged_df['hg_1'][1])" ] }, { "cell_type": "code", "execution_count": null, "id": "48eb82a8", "metadata": {}, "outputs": [], "source": [ "plt.imshow(merged_df['hg_2'][1])" ] }, { "cell_type": "code", "execution_count": null, "id": "778da74b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "955721a6", "metadata": {}, "outputs": [], "source": [ "sp_corr_splt_hlf = []\n", "for i in range(len(merged_df)):\n", " sp_corr_splt_hlf.append(spearmanr(merged_df['hg_1'][i].flatten(), merged_df['hg_2'][i].flatten())[0])\n", " #break" ] }, { "cell_type": "code", "execution_count": null, "id": "3cf2d0f9", "metadata": {}, "outputs": [], "source": [ "# Add the Spearman correlation values to the DataFrame\n", "merged_df['sp_corr_splt_hlf'] = sp_corr_splt_hlf" ] }, { "cell_type": "code", "execution_count": null, "id": "8e0ead15", "metadata": {}, "outputs": [], "source": [ "merged_df = merged_df.drop(columns=['hg_1', 'hg_2'])" ] }, { "cell_type": "code", "execution_count": null, "id": "0a2c8570", "metadata": {}, "outputs": [], "source": [ "merged_df" ] }, { "cell_type": "code", "execution_count": null, "id": "d302dc86", "metadata": {}, "outputs": [], "source": [ "merged_df = merged_df.groupby('stim_folder')['sp_corr_splt_hlf'].mean().reset_index()\n", "merged_df" ] }, { "cell_type": "code", "execution_count": null, "id": "9dbab573", "metadata": {}, "outputs": [], "source": [ "# Define the custom order\n", "custom_order = ['faces', 'faces_inv', 'objects', 'objects_inv', 'pareidolia', 'pareidolia_inv', 'pareidolia_art', 'pareidolia_art_inv']\n", "\n", "# Convert the 'stim_folder' column to a Categorical data type with the custom order\n", "merged_df['stim_folder'] = pd.Categorical(merged_df['stim_folder'], categories=custom_order, ordered=True)\n", "\n", "# Sort the DataFrame based on the custom order\n", "merged_df_sorted = merged_df.sort_values(by='stim_folder')\n", "\n", "# Print the sorted DataFrame\n", "print(merged_df_sorted)" ] }, { "cell_type": "code", "execution_count": null, "id": "150d3228", "metadata": {}, "outputs": [], "source": [ "# Extract the 'sp_corr_splt_hlf' column and save it to a NumPy array\n", "sp_corr_splt_hlf_array = merged_df_sorted['sp_corr_splt_hlf'].to_numpy()" ] }, { "cell_type": "code", "execution_count": null, "id": "01253db8", "metadata": {}, "outputs": [], "source": [ "sp_corr_splt_hlf_array" ] }, { "cell_type": "code", "execution_count": null, "id": "30d27acc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "f2d9d05e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "d751aabb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "2bcf36b3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "e2ce5789", "metadata": {}, "outputs": [], "source": [ "# Number of times to perform the bootstrap sampling\n", "num_iterations = 50\n", "sp_corr_splt_hlf_array = []\n", "\n", "for _ in range(num_iterations):\n", "\n", " # Get all unique values in the 'sub' column\n", " unique_subs = df['sub'].unique()\n", " #print(unique_subs)\n", "\n", " # Randomly shuffle the unique values\n", " np.random.shuffle(unique_subs)\n", " print(unique_subs)\n", "\n", " # Calculate the index to split at (half of the unique values)\n", " split_index = len(unique_subs) // 2\n", " #print(split_index)\n", "\n", " # Select the first half of unique values\n", " selected_subs_df1 = unique_subs[:split_index]\n", " #selected_subs_df1\n", "\n", " # Split the DataFrame into two based on the selected unique values\n", " df1 = df[df['sub'].isin(selected_subs_df1)]\n", " df2 = df[~df['sub'].isin(selected_subs_df1)]\n", "\n", " df1_hg = df1.groupby(['stim_folder', 'stim_name'])['hg'].apply(lambda x: np.mean(x.tolist(), axis=0)).reset_index()\n", "\n", " # Rename the column 'old_col_name' to 'new_col_name'\n", " df1_hg = df1_hg.rename(columns={'hg': 'hg_1'})\n", " #df1_hg\n", "\n", " df2_hg = df2.groupby(['stim_folder', 'stim_name'])['hg'].apply(lambda x: np.mean(x.tolist(), axis=0)).reset_index()\n", "\n", " # Rename the column 'old_col_name' to 'new_col_name'\n", " df2_hg = df2_hg.rename(columns={'hg': 'hg_2'})\n", " #df2_hg\n", "\n", " # Merge the DataFrames based on 'stim_folder' and 'stim_name'\n", " merged_df = pd.merge(df1_hg, df2_hg, on=['stim_folder', 'stim_name'], how='inner')\n", "\n", " # Display the resulting merged DataFrame\n", " #print(merged_df)\n", "\n", " sp_corr_splt_hlf = []\n", " for i in range(len(merged_df)):\n", " sp_corr_splt_hlf.append(spearmanr(merged_df['hg_1'][i].flatten(), merged_df['hg_2'][i].flatten())[0])\n", " #break\n", "\n", " # Add the Spearman correlation values to the DataFrame\n", " merged_df['sp_corr_splt_hlf'] = sp_corr_splt_hlf\n", "\n", " merged_df = merged_df.drop(columns=['hg_1', 'hg_2'])\n", "\n", " merged_df = merged_df.groupby('stim_folder')['sp_corr_splt_hlf'].mean().reset_index()\n", "\n", " # Define the custom order\n", " custom_order = ['faces', 'faces_inv', 'objects', 'objects_inv', 'pareidolia', 'pareidolia_inv', 'pareidolia_art', 'pareidolia_art_inv']\n", "\n", " # Convert the 'stim_folder' column to a Categorical data type with the custom order\n", " merged_df['stim_folder'] = pd.Categorical(merged_df['stim_folder'], categories=custom_order, ordered=True)\n", "\n", " # Sort the DataFrame based on the custom order\n", " merged_df_sorted = merged_df.sort_values(by='stim_folder')\n", "\n", " # Print the sorted DataFrame\n", " #print(merged_df_sorted)\n", "\n", " # Extract the 'sp_corr_splt_hlf' column and save it to a NumPy array\n", " sp_corr_splt_hlf_array.append(merged_df_sorted['sp_corr_splt_hlf'].to_numpy())\n", "\n", "print(sp_corr_splt_hlf_array)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "3fa9422a", "metadata": {}, "outputs": [], "source": [ "np.mean(sp_corr_splt_hlf_array, axis=0)" ] }, { "cell_type": "code", "execution_count": null, "id": "10718a78", "metadata": {}, "outputs": [], "source": [ "len(sp_corr_splt_hlf_array)" ] }, { "cell_type": "code", "execution_count": null, "id": "1ac85240", "metadata": {}, "outputs": [], "source": [ "from tabulate import tabulate\n", "\n", "mapping = ['faces', 'faces_inv', 'objects', 'objects_inv', 'pareidolia', 'pareidolia_inv', 'pareidolia_art', 'pareidolia_art_inv']\n", "values = [0.64045339, 0.63962363, 0.65247639, 0.64685355, 0.69212828, 0.68226111, 0.63446804, 0.64820666]\n", "\n", "array_result = np.array(values)\n", "\n", "table = list(zip(mapping, array_result))\n", "headers = [\"Conditions\", \"S-H rho\"]\n", "\n", "print(tabulate(table, headers=headers))" ] }, { "cell_type": "code", "execution_count": null, "id": "3b4c3fe0", "metadata": {}, "outputs": [], "source": [ "# Given data\n", "mapping = ['faces', 'faces_inv', 'objects', 'objects_inv', 'pareidolia', 'pareidolia_inv', 'pareidolia_art', 'pareidolia_art_inv']\n", "values = [0.64045339, 0.63962363, 0.65247639, 0.64685355, 0.69212828, 0.68226111, 0.63446804, 0.64820666]\n", "\n", "# Create a DataFrame\n", "df_sh = pd.DataFrame({'stim_folder': mapping, 'sp_corr_splt_hlf': values})\n", "\n", "# Print the DataFrame\n", "print(df_sh)" ] }, { "cell_type": "code", "execution_count": null, "id": "04fd3c07", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "10ac43f4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a60ccce0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "873461b1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "dbda9f4b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7ae9f2d9", "metadata": {}, "outputs": [], "source": [ "\n", "correlation_coef_objects_iter_img = []\n", "\n", "for keys in y_objects:\n", " mmm = []\n", " print(keys)\n", " for i in range(len(y_objects_subs)):\n", " if y_objects_subs[i][0][0] == keys:\n", " mmm.append(y_objects_subs[i][1])\n", " #print(y_faces_subs[i][1])\n", "\n", " # Number of times to perform the bootstrap sampling\n", " num_iterations = 50\n", " correlation_coef_objects_iter = []\n", "\n", " for _ in range(num_iterations):\n", "\n", " # Define your dataset\n", " dataset = list(range(len(mmm)))\n", "\n", " # Perform bootstrap sampling without replacement until no dataset is left\n", " bootstrap_samples = []\n", " correlation_coef_objects = []\n", "\n", " while dataset:\n", " bootstrap_sample = random.sample(dataset, len(dataset))\n", " bootstrap_samples.append(bootstrap_sample)\n", " dataset = [x for x in dataset if x not in bootstrap_sample]\n", "\n", " # Print the bootstrap samples\n", " #for i, sample in enumerate(bootstrap_samples):\n", " # print(f\"Bootstrap Sample {k + 1}: {sample}\")\n", " \n", " temp1 = []\n", " temp2 = []\n", "\n", " for i in bootstrap_samples[0][:int(len(bootstrap_samples[0])/2)]:\n", " temp1.append(mmm[i])\n", "\n", " for i in bootstrap_samples[0][int(len(bootstrap_samples[0])/2):]:\n", " temp2.append(mmm[i])\n", "\n", " #print(temp1)\n", " temp1 = np.mean(temp1, axis=0)\n", " temp2 = np.mean(temp2, axis=0)\n", " \n", " #plt.matshow(mmm[sample[i]])\n", " #plt.matshow(mmm[sample[i+1]])\n", " correlation_coef_objects.append(spearmanr(temp1.flatten(),\n", " temp2.flatten())[0])\n", "\n", " correlation_coef_objects_iter.append(np.mean(correlation_coef_objects))\n", " \n", " #break\n", " \n", "\n", " correlation_coef_objects_iter_img.append(np.mean(correlation_coef_objects_iter))\n", " \n", " #break\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "f59ec104", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "27c5282e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "55fccdef", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "856344fb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "68144351", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "031db151", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "6a111361", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3fcfb699", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "9a181318", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "abbe38bb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "eba2327a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "084d5689", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "36cc322a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "5841029d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "973779fb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "1594f5f5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "2d1b2423", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "893d2965", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b54a54cd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "92934490", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "520e6c25", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "6d8f569b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "08b42deb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "c30b8314", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 5 }