{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "3bb7a04d-1915-428a-a931-85d77d40c379", "metadata": {}, "outputs": [], "source": [ "from __future__ import print_function\n", "\n", "from itertools import product\n", "import os\n", "import shutil\n", "from time import time\n", "import warnings\n", "import argparse\n", "\n", "import bdpy\n", "from bdpy.dataform import Features, save_array\n", "from bdpy.distcomp import DistComp\n", "from bdpy.ml import ModelTraining\n", "from bdpy.util import makedir_ifnot\n", "from fastl2lir import FastL2LiR\n", "import numpy as np\n", "import yaml\n", "import torch\n", "import h5py" ] }, { "cell_type": "code", "execution_count": 2, "id": "fe5a2900-ef2e-4b03-91a5-02861fd5ea7a", "metadata": {}, "outputs": [], "source": [ "def featdec_fastl2lir_train(\n", " fmri_data_files,\n", " features_dir,\n", " output_dir='./feature_decoders',\n", " rois_list=None, num_voxel=None, label_key=None,\n", " features_list=None, feature_index_file=None,\n", " alpha=100, chunk_axis=1\n", "):\n", " '''Feature decoder training.\n", "\n", " Input:\n", "\n", " - fmri_data_files\n", " - features_dir\n", "\n", " Output:\n", "\n", " - output_dir\n", "\n", " Parameters:\n", "\n", " TBA\n", "\n", " Note:\n", "\n", " If Y.ndim >= 3, Y is divided into chunks along `chunk_axis`.\n", " Note that Y[0] should be sample dimension.\n", " '''\n", "\n", " analysis_basename = 'train' + '-' + conf['__filename__']\n", "\n", " features_list = features_list[::-1] # Start training from deep layers\n", "\n", " # Print info -------------------------------------------------------------\n", " print('Subjects: %s' % list(fmri_data_files.keys()))\n", " print('ROIs: %s' % list(rois_list.keys()))\n", " print('Target features: %s' % features_dir)\n", " print('Layers: %s' % features_list)\n", " print('')\n", "\n", " # Load data --------------------------------------------------------------\n", " print('----------------------------------------')\n", " print('Loading data')\n", "\n", " # data_brain = {sbj: bdpy.BData(dat_file[0])\n", " # for sbj, dat_file in fmri_data_files.items()}\n", "\n", " data_brain_path = f'betas_all_subj01.hdf5'\n", " print(fmri_data_files)\n", " s = 1\n", " f = h5py.File(f'{fmri_data_files[\"nsd-01\"][0]}/betas_all_subj0{s}_fp32_renorm.hdf5', 'r')\n", " betas = f['betas'][:]\n", " \n", " if feature_index_file is not None:\n", " data_features = Features(os.path.join(features_dir), feature_index=feature_index_file)\n", " else:\n", " data_features = Features(os.path.join(features_dir))\n", "\n", " \n", " # Initialize directories -------------------------------------------------\n", " makedir_ifnot(output_dir)\n", " makedir_ifnot('tmp')\n", "\n", " # Save feature index -----------------------------------------------------\n", " if feature_index_file is not None:\n", " feature_index_save_file = os.path.join(output_dir, 'feature_index.mat')\n", " shutil.copy(feature_index_file, feature_index_save_file)\n", " print('Saved %s' % feature_index_save_file)\n", "\n", " # Analysis loop ----------------------------------------------------------\n", " print('----------------------------------------')\n", " print('Analysis loop')\n", "\n", " # for a, b, c in product(features_list, fmri_data_files, rois_list):\n", " # print(a,b,c)\n", " \n", " for feat, sbj, roi in product(features_list, fmri_data_files, rois_list):\n", " print('--------------------')\n", " print('Feature: %s' % feat)\n", " print('Subject: %s' % sbj)\n", " print('ROI: %s' % roi)\n", " print('Num voxels: %d' % num_voxel[roi])\n", "\n", " # Setup\n", " # -----\n", " analysis_id = analysis_basename + '-' + sbj + '-' + roi + '-' + feat\n", " results_dir = os.path.join(output_dir, feat, sbj, roi, 'model')\n", " makedir_ifnot(results_dir)\n", "\n", " # Check whether the analysis has been done or not.\n", " info_file = os.path.join(results_dir, 'info.yaml')\n", " if os.path.exists(info_file):\n", " with open(info_file, 'r') as f:\n", " info = yaml.safe_load(f)\n", " while info is None:\n", " warnings.warn('Failed to load info from %s. Retrying...'\n", " % info_file)\n", " with open(info_file, 'r') as f:\n", " info = yaml.safe_load(f)\n", " if '_status' in info and 'computation_status' in info['_status']:\n", " if info['_status']['computation_status'] == 'done':\n", " print('%s is already done and skipped' % analysis_id)\n", " continue\n", "\n", " # Preparing data\n", " # --------------\n", " print('Preparing data')\n", "\n", " start_time = time()\n", "\n", " # Brain data\n", " # x = data_brain[sbj].select(rois_list[roi]) # Brain data\n", " # x_labels = data_brain[sbj].get_label(label_key) # Labels\n", "\n", " # # Target features and image labels (file names)\n", " # y_labels = np.unique(x_labels)\n", " # y = data_features.get(feat, label=y_labels) # Target DNN features\n", "\n", " # # Use x that has a label included in y\n", " # x = np.vstack([_x for _x, xl in zip(x, x_labels) if xl in y_labels])\n", " # x_labels = [xl for xl in x_labels if xl in y_labels]\n", "\n", " x = betas\n", " \n", " yf = h5py.File(os.path.join(features_dir, feat, f'{s}.h5'), 'r')\n", " y = yf['dataset'][:]\n", "\n", " print(y.shape)\n", " print(x.shape)\n", " print('Elapsed time (data preparation): %f' % (time() - start_time))\n", "\n", " # Calculate normalization parameters\n", " # ----------------------------------\n", "\n", " # Normalize X (fMRI data)\n", " x_mean = np.mean(x, axis=0)[np.newaxis, :] # np.newaxis was added to match Matlab outputs\n", " x_norm = np.std(x, axis=0, ddof=1)[np.newaxis, :]\n", "\n", " # Normalize Y (DNN features)\n", " y_mean = np.mean(y, axis=0)[np.newaxis, :]\n", " y_norm = np.std(y, axis=0, ddof=1)[np.newaxis, :]\n", "\n", " # Y index to sort Y by X (matching samples)\n", " # -----------------------------------------\n", " y_index = np.array([np.where(np.array(y_labels) == xl) for xl in x_labels]).flatten()\n", "\n", " # Save normalization parameters\n", " # -----------------------------\n", " print('Saving normalization parameters.')\n", " norm_param = {'x_mean': x_mean, 'y_mean': y_mean,\n", " 'x_norm': x_norm, 'y_norm': y_norm}\n", " save_targets = [u'x_mean', u'y_mean', u'x_norm', u'y_norm']\n", " for sv in save_targets:\n", " save_file = os.path.join(results_dir, sv + '.mat')\n", " if not os.path.exists(save_file):\n", " try:\n", " save_array(save_file, norm_param[sv], key=sv, dtype=np.float32, sparse=False)\n", " print('Saved %s' % save_file)\n", " except Exception:\n", " warnings.warn('Failed to save %s. Possibly double running.' % save_file)\n", "\n", " # Preparing learning\n", " # ------------------\n", " model = FastL2LiR()\n", " model_param = {'alpha': alpha,\n", " 'n_feat': num_voxel[roi],\n", " 'dtype': np.float32}\n", "\n", " # Distributed computation setup\n", " # -----------------------------\n", " makedir_ifnot('./tmp')\n", " distcomp_db = os.path.join('./tmp', analysis_basename + '.db')\n", " distcomp = DistComp(backend='sqlite3', db_path=distcomp_db)\n", "\n", " # Model training\n", " # --------------\n", " print('Model training')\n", " start_time = time()\n", "\n", " train = ModelTraining(model, x, y)\n", " train.id = analysis_basename + '-' + sbj + '-' + roi + '-' + feat\n", " train.model_parameters = model_param\n", "\n", " train.X_normalize = {'mean': x_mean,\n", " 'std': x_norm}\n", " train.Y_normalize = {'mean': y_mean,\n", " 'std': y_norm}\n", " train.Y_sort = {'index': y_index}\n", "\n", " train.dtype = np.float32\n", " train.chunk_axis = chunk_axis\n", " train.save_format = 'bdmodel'\n", " train.save_path = results_dir\n", " train.distcomp = distcomp\n", "\n", " train.run()\n", "\n", " print('Total elapsed time (model training): %f' % (time() - start_time))\n", "\n", " print('%s finished.' % analysis_basename)\n", "\n", " return output_dir\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "7715c7bb-6d3e-41f1-a533-b1197c6c794f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/weka/proj-medarc/shared/mindeyev2_dataset//wds/subj01/train/{0..39}.tar\n", "/weka/proj-medarc/shared/mindeyev2_dataset//wds/subj01/new_test/0.tar\n", "Loaded test dl for subj1!\n", "\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3d0e33de2d814568af0b258cc47e6942", "version_major": 2, "version_minor": 0 }, "text/plain": [ "0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "83d924e11e524bee878541b23a36396e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import webdataset as wds\n", "import random\n", "from tqdm.auto import tqdm\n", "\n", "def my_split_by_node(urls): return urls\n", "data_path = '/weka/proj-medarc/shared/mindeyev2_dataset/'\n", "subj_list = [1]\n", "num_sessions = 40\n", "multi_subject = False\n", "batch_size = 1\n", "data_type = torch.float16\n", "subj = 1\n", "\n", "train_data = {}\n", "train_dl = {}\n", "num_voxels = {}\n", "voxels = {}\n", "\n", "train_url = f\"{data_path}/wds/subj0{subj}/train/\" + \"{0..\" + f\"{num_sessions-1}\" + \"}.tar\"\n", "print(train_url)\n", "\n", "train_data = wds.WebDataset(train_url,resampled=False,nodesplitter=my_split_by_node)\\\n", " .decode(\"torch\")\\\n", " .rename(behav=\"behav.npy\", past_behav=\"past_behav.npy\", future_behav=\"future_behav.npy\", olds_behav=\"olds_behav.npy\")\\\n", " .to_tuple(*[\"behav\", \"past_behav\", \"future_behav\", \"olds_behav\"])\n", "train_dl = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=False, drop_last=False, pin_memory=True)\n", "\n", "# f = h5py.File(f'{data_path}/betas_all_subj0{s}_fp32_renorm.hdf5', 'r')\n", "# betas = f['betas'][:]\n", "# betas = torch.Tensor(betas).to(\"cpu\").to(data_type)\n", "# num_voxels_list.append(betas[0].shape[-1])\n", "# num_voxels[f'subj0{s}'] = betas[0].shape[-1]\n", "# voxels[f'subj0{s}'] = betas\n", "# print(f\"num_voxels for subj0{s}: {num_voxels[f'subj0{s}']}\")\n", "\n", "\n", "num_test=3000\n", "test_url = f\"{data_path}/wds/subj0{subj}/new_test/\" + \"0.tar\"\n", "print(test_url)\n", "test_data = wds.WebDataset(test_url,resampled=False,nodesplitter=my_split_by_node)\\\n", " .decode(\"torch\")\\\n", " .rename(behav=\"behav.npy\", past_behav=\"past_behav.npy\", future_behav=\"future_behav.npy\", olds_behav=\"olds_behav.npy\")\\\n", " .to_tuple(*[\"behav\", \"past_behav\", \"future_behav\", \"olds_behav\"])\n", "test_dl = torch.utils.data.DataLoader(test_data, batch_size=1, shuffle=False, drop_last=True, pin_memory=True)\n", "print(f\"Loaded test dl for subj{subj}!\\n\")\n", "\n", "all_indexes_train = []\n", "all_betas_train = []\n", "\n", "for behav0, past_behav0, future_behav0, old_behav0 in tqdm(train_dl):\n", " img_idx = behav0[:,0,0].cpu().long().numpy()[0]\n", " betas_idx = behav0[:,0,5].cpu().long().numpy()[0]\n", " all_indexes_train.append(img_idx)\n", " all_betas_train.append(betas_idx)\n", "\n", "all_indexes_test = []\n", "all_betas_test = []\n", "\n", "for behav0, past_behav0, future_behav0, old_behav0 in tqdm(test_dl):\n", " img_idx = behav0[:,0,0].cpu().long().numpy()[0]\n", " betas_idx = behav0[:,0,5].cpu().long().numpy()[0]\n", " all_indexes_test.append(img_idx)\n", " all_betas_test.append(betas_idx)" ] }, { "cell_type": "code", "execution_count": 5, "id": "d2d1c060-6892-4d23-b131-969c35425087", "metadata": {}, "outputs": [], "source": [ "all_indexes_to_compute = set(all_indexes_train + list(set(all_indexes_test)))\n", "all_indexes_to_compute = list(all_indexes_to_compute)" ] }, { "cell_type": "code", "execution_count": 6, "id": "f3eb5455-bc9c-4b80-84bd-5f69ff6c93e5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10000" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(all_indexes_to_compute)" ] }, { "cell_type": "code", "execution_count": 7, "id": "58eea517-0188-4270-a21f-df2a166194fd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loading_betas\n", "betas_ loaded\n" ] } ], "source": [ "import utils\n", "f = h5py.File(f'{data_path}/betas_all_subj0{subj}_fp32_renorm.hdf5', 'r')\n", "print(\"loading_betas\")\n", "betas = f['betas'][:]\n", "betas = torch.from_numpy(betas).to(\"cpu\")\n", "print(\"betas_ loaded\")\n", "x_train, valid_nsd_ids_train, x_test, test_nsd_ids = utils.load_nsd(subject=subj, betas=betas, data_path=data_path)" ] }, { "cell_type": "code", "execution_count": 9, "id": "9b492d47-ac12-47d2-a1c5-718923340385", "metadata": {}, "outputs": [], "source": [ "# Define the path to your configuration YAML file\n", "conf_file = 'config/nsd-37ses_func1pt8mm_betas_fithrf_GLMdenoise_RR_testshared1000_trainnoave_testave_fastl2lir_a100_vgg19_allunits.yaml' # Replace with your actual config file path\n", "\n", "# Load the configuration\n", "with open(conf_file, 'r') as f:\n", " conf = yaml.safe_load(f)\n", "\n", "conf.update({\n", " '__filename__': os.path.splitext(os.path.basename(conf_file))[0]\n", "})\n", "\n", "if 'analysis name' in conf:\n", " feature_decoders_dir = os.path.join(conf['feature decoder dir'], conf['analysis name'], conf['network'])\n", "else:\n", " feature_decoders_dir = os.path.join(conf['feature decoder dir'], conf['network'])\n", "\n", "if 'feature index file' in conf:\n", " feature_index_file = os.path.join(\n", " conf['training feature dir'][0],\n", " conf['network'],\n", " conf['feature index file']\n", " )\n", "else:\n", " feature_index_file = None" ] }, { "cell_type": "code", "execution_count": 10, "id": "ec23d0bd-f4f8-412d-8890-558c064ceb46", "metadata": {}, "outputs": [], "source": [ "features_dir=os.path.join(\n", " conf['training feature dir'][0],\n", " conf['network']\n", " )\n", "yf = h5py.File(os.path.join(features_dir, 'features[28]', f'{1}.h5'), 'r')\n", "features = yf['dataset']\n", "# features = torch.from_numpy(features).to(\"cpu\")" ] }, { "cell_type": "code", "execution_count": 11, "id": "f9ce9d84-6f77-408c-9257-fe7b4ef015c2", "metadata": {}, "outputs": [], "source": [ "y_train = torch.zeros((len(x_train),)+ features.shape[1:], dtype = torch.float16)" ] }, { "cell_type": "code", "execution_count": 12, "id": "365a2465-f7c8-4c47-8a03-8b64392578a5", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4aa9bfdb5941443789f1574a42e659f1", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/27000 [00:00 21\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mElapsed training time for \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[43mmodel_name\u001b[49m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtime\u001b[38;5;241m.\u001b[39mstrftime(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mH:\u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mM:\u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124mS\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;250m \u001b[39mtime\u001b[38;5;241m.\u001b[39mgmtime(time\u001b[38;5;241m.\u001b[39mtime()\u001b[38;5;250m \u001b[39m\u001b[38;5;241m-\u001b[39m\u001b[38;5;250m \u001b[39mstart))\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mNameError\u001b[0m: name 'model_name' is not defined" ] } ], "source": [ "weight_decay = 60000\n", "start = time.time()\n", "# ridge_weights = np.zeros((np.prod(y_train.shape[1:]), x_train.shape[-1])).astype(np.float16)\n", "# ridge_biases = np.zeros((np.prod(y_train.shape[1:]))).astype(np.float16)\n", "print(f\"Training Ridge CLIP Image model with alpha={weight_decay}\")\n", "model = Ridge(\n", " alpha=weight_decay,\n", " max_iter=50000,\n", " random_state=42,\n", ")\n", "\n", "model.fit(x_train, y_train.reshape(len(y_train), -1))\n", "ridge_weights = model.coef_.astype(np.float16)\n", "ridge_biases = model.intercept_.astype(np.float16)\n", "datadict = {\"coef\" : ridge_weights, \"intercept\" : ridge_biases}\n", "# Save the regression weights\n", "with open(f'{outdir}/ridge_image_weights.pkl', 'wb') as f:\n", " pickle.dump(datadict, f)\n", " \n", "\n", "print(f\"Elapsed training time for : {time.strftime('%H:%M:%S', time.gmtime(time.time() - start))}\")" ] }, { "cell_type": "code", "execution_count": 1, "id": "cb7f7c46-2991-4235-bcbd-0920e95c8479", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'x' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mx\u001b[49m\u001b[38;5;241m.\u001b[39mshape\n", "\u001b[0;31mNameError\u001b[0m: name 'x' is not defined" ] } ], "source": [ "x.shape" ] }, { "cell_type": "code", "execution_count": 16, "id": "e4b7e52c-e032-4b27-b041-47e337cdda92", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/admin/home-ckadirt/mindeye/lib/python3.11/site-packages/numpy/core/_methods.py:152: RuntimeWarning: overflow encountered in reduce\n", " arrmean = umr_sum(arr, axis, dtype, keepdims=True, where=where)\n", "/admin/home-ckadirt/mindeye/lib/python3.11/site-packages/numpy/core/_methods.py:187: RuntimeWarning: overflow encountered in reduce\n", " ret = umr_sum(x, axis, dtype, out, keepdims=keepdims, where=where)\n" ] } ], "source": [ "# Normalize X (fMRI data)\n", "x = x_train.numpy()\n", "y = y_train.numpy()\n", "x_mean = np.mean(x, axis=0)[np.newaxis, :] # np.newaxis was added to match Matlab outputs\n", "x_norm = np.std(x, axis=0, ddof=1)[np.newaxis, :]\n", "\n", "# Normalize Y (DNN features)\n", "y_mean = np.mean(y, axis=0)[np.newaxis, :]\n", "y_norm = np.std(y, axis=0, ddof=1)[np.newaxis, :]\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a83f254a-af28-48a7-90de-8f339d4f74e6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Saving normalization parameters.\n", "Model training\n", "Normalizing X\n", "Normalizing Y\n", "Training: rr_vgg19_features-1---chunk00000000\n" ] } ], "source": [ "# Y index to sort Y by X (matching samples)\n", "# -----------------------------------------\n", "# y_index = np.array([np.where(np.array(y_labels) == xl) for xl in x_labels]).flatten()\n", "\n", "# Save normalization parameters\n", "# -----------------------------\\\n", "results_dir = '.'\n", "alpha = 100\n", "num_voxel = x_train.shape[-1]\n", "analysis_basename = 'rr_vgg19_features'\n", "\n", "print('Saving normalization parameters.')\n", "norm_param = {'x_mean': x_mean, 'y_mean': y_mean,\n", " 'x_norm': x_norm, 'y_norm': y_norm}\n", "save_targets = [u'x_mean', u'y_mean', u'x_norm', u'y_norm']\n", "for sv in save_targets:\n", " save_file = os.path.join(results_dir, sv + '.mat')\n", " if not os.path.exists(save_file):\n", " try:\n", " save_array(save_file, norm_param[sv], key=sv, dtype=np.float32, sparse=False)\n", " print('Saved %s' % save_file)\n", " except Exception:\n", " warnings.warn('Failed to save %s. Possibly double running.' % save_file)\n", "\n", "# Preparing learning\n", "# ------------------\n", "model = FastL2LiR()\n", "model_param = {'alpha': alpha,\n", " 'n_feat': num_voxel,\n", " 'dtype': np.float16}\n", "\n", "# Distributed computation setup\n", "# -----------------------------\n", "makedir_ifnot('./tmp')\n", "distcomp_db = os.path.join('./tmp', analysis_basename + '.db')\n", "distcomp = DistComp(backend='sqlite3', db_path=distcomp_db)\n", "\n", "# Model training\n", "# --------------\n", "print('Model training')\n", "start_time = time.time()\n", "\n", "train = ModelTraining(model, x, y)\n", "train.id = analysis_basename + '-' + str(subj) + '-' + '-' \n", "train.model_parameters = model_param\n", "\n", "train.X_normalize = {'mean': x_mean,\n", " 'std': x_norm}\n", "train.Y_normalize = {'mean': y_mean,\n", " 'std': y_norm}\n", "# train.Y_sort = {'index': y_index}\n", "\n", "train.dtype = np.float16\n", "train.save_format = 'bdmodel'\n", "train.save_path = results_dir\n", "train.distcomp = distcomp\n", "\n", "train.run()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8859a81f-25eb-4f6b-a02f-fbbcc95df5c6", "metadata": {}, "outputs": [], "source": [ "train.model.b" ] }, { "cell_type": "code", "execution_count": null, "id": "6c35d459-1a88-4b0a-8822-90fc89d4fdad", "metadata": {}, "outputs": [], "source": [ "f'{outdir}/ridge_image_weights.pkl'" ] }, { "cell_type": "code", "execution_count": null, "id": "8af3fa7d-845e-4231-a594-3fadfd5bfdd8", "metadata": {}, "outputs": [], "source": [ "\n", "\n", "# Start training\n", "output_directory = featdec_fastl2lir_train(\n", " fmri_data_files=conf['training fmri'],\n", " features_dir=os.path.join(\n", " conf['training feature dir'][0],\n", " conf['network']\n", " ),\n", " output_dir=feature_decoders_dir,\n", " rois_list=conf['rois'],\n", " num_voxel=conf['rois voxel num'],\n", " label_key=conf['label key'],\n", " features_list=conf['layers'],\n", " feature_index_file=feature_index_file,\n", " alpha=conf['alpha'],\n", " chunk_axis=conf['chunk axis']\n", ")\n", "\n", "print(f\"Feature decoders trained and saved to: {output_directory}\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6101d9b4-ba46-4f95-967b-372a5844efb5", "metadata": {}, "outputs": [], "source": [ "features_dir=os.path.join(\n", " conf['training feature dir'][0],\n", " conf['network']\n", " )\n", "yf = h5py.File(os.path.join(features_dir, 'features[2]', f'{1}.h5'), 'r')\n", "features = yf['dataset']" ] }, { "cell_type": "code", "execution_count": null, "id": "b4726cee-484d-4046-8923-6ef4513a575a", "metadata": {}, "outputs": [], "source": [ "features[1]" ] } ], "metadata": { "kernelspec": { "display_name": "mindeye", "language": "python", "name": "mindeye" }, "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.11.10" } }, "nbformat": 4, "nbformat_minor": 5 }