{ "cells": [ { "cell_type": "markdown", "id": "2cde7464-b19a-488b-8fca-c735549692aa", "metadata": {}, "source": [ "Fieldtrip: KIT to fieldtrip\n", "===========================\n", "\n", "Lead author: Osama Abdullah osama.abdullah@nyu.edu\n", "\n", "This notebook is to be run in MATLAB, it facilitates the importing of the raw KIT files, laser scan files and MRI anatomical data into fieldtrip for data processing purposes.\n", "The `oddball` experiment data is used as an example." ] }, { "cell_type": "markdown", "id": "d8c3e0d9-7c10-48d7-9d50-fcb86365f3d1", "metadata": {}, "source": [ "Specify the as `BOX_DIR` an environment variable pointing towards the `BOX://MEG/Data` folder and set the variable in your script." ] }, { "cell_type": "code", "execution_count": 2, "id": "initial_id", "metadata": { "ExecuteTime": { "start_time": "2024-06-03T06:25:22.578648Z" }, "jupyter": { "is_executing": true } }, "outputs": [], "source": [ "% MRI-MEG KIT coregisteration\n", "clear all\n", "close all\n", "clc" ] }, { "cell_type": "markdown", "id": "f72a6184-00f7-43a9-9794-4caaa2785037", "metadata": {}, "source": [ "Ensure the environment variable is properly set to the MEG Data folder" ] }, { "cell_type": "code", "execution_count": 2, "id": "a68c517b-38ae-4da9-aeb8-4fbd870476f4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Users\\hz3752\\Box\\MEG\\Data\n" ] } ], "source": [ "BOX_DIR = getenv('BOX_DIR');\n", "disp(BOX_DIR)" ] }, { "cell_type": "markdown", "id": "bb25794c-0bad-49bd-9277-57b4b0b77081", "metadata": {}, "source": [ "Specify the needed files, a T1w MRI scan, MEG data .con file, laser surface scan of the head .txt, stylus points from laser scan .txt, atleast two HPI KIT coils .mrk files" ] }, { "cell_type": "code", "execution_count": 4, "id": "1acb1428-ec35-4171-8db1-eb6125063dee", "metadata": { "jupyter": { "is_executing": true } }, "outputs": [], "source": [ "% It is important that you use T1.mgz instead of orig.mgz as T1.mgz is normalized to [255,255,255] dimension\n", "mrifile = fullfile([BOX_DIR,'oddball\\sub-03\\anat\\sub-003\\sub-003\\mri\\T1.mgz']);\n", "confile = fullfile([BOX_DIR,'oddball\\sub-03\\meg-kit\\sub-03-raw-kit.con']); \n", "laser_surf = fullfile([BOX_DIR,'oddball\\sub-03\\meg-kit\\sub-03-basic-surface.txt']);\n", "laser_points = [BOX_DIR, 'oddball\\sub-03\\meg-kit\\sub-03-stylus.txt'];\n", "mrkfile1 = [BOX_DIR,'oddball\\sub-03\\meg-kit\\240524-1.mrk'];\n", "mrkfile2 = [BOX_DIR, 'oddball\\sub-03\\meg-kit\\240524-2.mrk'];" ] }, { "cell_type": "code", "execution_count": null, "id": "3a755801-654a-4a2f-97bd-3c1dfe62a38f", "metadata": { "jupyter": { "is_executing": true } }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "6f4195b7-e467-4e9b-a87a-fc613ad36b71", "metadata": { "jupyter": { "is_executing": true } }, "outputs": [], "source": [ "%% Read Laser headshape (points and fudicials)\n", "lasershape = read_head_shape_laser(laser_surf,laser_points);\n", "lasershape = ft_convert_units(lasershape, 'cm');\n", "laser2ctf = ft_headcoordinates(lasershape.fid.pos(1,:),lasershape.fid.pos(4,:),lasershape.fid.pos(5,:),'ctf');\n", "lasershape = ft_transform_geometry(laser2ctf, lasershape)\n", "ft_determine_coordsys(lasershape, 'interactive', 'no')\n" ] }, { "cell_type": "code", "execution_count": null, "id": "25e455ea-f1d8-4db4-bf62-1f5e135ee65d", "metadata": { "jupyter": { "is_executing": true } }, "outputs": [], "source": [ "% Deface the laser mesh under a certain plan (change the 140) Define the configuration for ft_defacemesh\n", "planecut = 140;\n", "cfg = [];\n", "cfg.method = 'plane'; % Use a plane for exclusion\n", "cfg.translate = [0 planecut 0]; % A point on the plane (adjust z_value as needed)\n", "cfg.rotate = [0 0 0]; % Rotation vector, modify if the plane is not axis-aligned\n", "cfg.selection = 'outside'; % Remove points below the plane\n", "% Apply ft_defacemesh to remove points below the plane\n", "mesh = ft_defacemesh(cfg, lasershape);\n", "% Plot the resulting mesh to check the results\n", "ft_plot_mesh(mesh);\n", "lasershape = mesh\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1323030a-c011-4da7-bc31-9e309b19b5a4", "metadata": { "jupyter": { "is_executing": true } }, "outputs": [], "source": [ "%% read mri and mri-headshape\n", "mri = ft_read_mri(mrifile); % read mri file\n", "mri = ft_convert_units(mri, 'cm'); %make sure units cm\n", "% mri = ft_determine_coordsys(mri, 'interactive', 'no');\n", "cfg = [];\n", "cfg.method = 'interactive';\n", "cfg.coordsys = 'ctf'; %use CTF coordinates (pos x toward nose, +y to left)\n", "mri_init = ft_volumerealign(cfg,mri)\n", "ft_determine_coordsys(mri_init, 'interactive', 'no'); % sanity check, should be CTF\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a637489b-3316-43f1-a53c-4538498c3777", "metadata": { "jupyter": { "is_executing": true } }, "outputs": [], "source": [ "%% Align MEG Dewar to Laser scan Head model\n", "% now we want to align the 3 markers in the *.con file with the 3 markers\n", "% in the lasershape, where 1:5 markers match to the 4:9 lasershape\n", "% fiducials\n", "mrk1 = ft_read_headshape(mrkfile1);\n", "mrk1 = ft_convert_units(mrk1, lasershape.unit);\n", "mrk2 = ft_read_headshape(mrkfile2);\n", "mrk2 = ft_convert_units(mrk2, lasershape.unit);\n", "mrka = mrk1;\n", "mrka.fid.pos = (mrk1.fid.pos+mrk2.fid.pos)/2;\n", "p1 = mrka.fid.pos(1:5,:);\n", "p2 = lasershape.fid.pos;\n", "t1 = ft_headcoordinates(p1(1,:), p1(2,:), p1(3,:), 'ctf');%J\n", "t2 = ft_headcoordinates(p2(6,:), p2(4,:), p2(5,:), 'ctf');%J\n", "% t1 = ft_headcoordinates(p1(1,:), p1(2,:), p1(3,:), 'ctf');\n", "% t2 = ft_headcoordinates(p2(1,:), p2(4,:), p2(5,:), 'ctf');\n", "transform_mrk2laser = t2\\t1;\n", "% p1t = ft_warp_apply(transform_mrk2laser, p1)\n", "grad = ft_read_sens(confile,'senstype','meg');\n", "grad = ft_transform_geometry(transform_mrk2laser, grad);\n" ] }, { "cell_type": "code", "execution_count": null, "id": "444dde3e-1b71-4dd2-b782-2336ae7dab16", "metadata": { "jupyter": { "is_executing": true } }, "outputs": [], "source": [ "%% align MRI and Laser\n", "cfg = []\n", "cfg.method = 'headshape';\n", "cfg.headshape = lasershape;\n", "cfg.headshape.interactive = 'no'\n", "cfg.headshape.icp = 'yes'\n", "mri_aligned = ft_volumerealign(cfg,mri_init)\n", "% ft_determine_coordsys(mri_aligned,'interactive', 'no')\n", "%% segmentation MRI\n", "cfg = [];\n", "cfg.output = {'brain', 'skull', 'scalp'};\n", "segmentedmri = ft_volumesegment(cfg, mri_aligned);\n", "save segmentedmri segmentedmri\n", "cfg = [];\n", "cfg.method='singleshell';\n", "mriskullmodel = ft_prepare_headmodel(cfg, segmentedmri);\n", "cfg = [];\n", "cfg.tissue = {'brain', 'skull', 'scalp'};\n", "cfg.numvertices = [3000 2000 1000];\n", "mesh = ft_prepare_mesh(cfg, segmentedmri);\n", "% ft_plot_mesh(mesh(3), 'facecolor', 'none'); % scalp\n", "%% \n", "cfg = [];\n", "% cfg.elec = structure, see FT_READ_SENS\n", " cfg.grad = grad;%structure, see FT_READ_SENS\n", "% cfg.opto = structure, see FT_READ_SENS\n", " cfg.headshape = mesh(3)%structure, see FT_READ_HEADSHAPE\n", " cfg.headmodel = mriskullmodel% structure, see FT_PREPARE_HEADMODEL and FT_READ_HEADMODEL\n", "% cfg.sourcemodel = structure, see FT_PREPARE_SOURCEMODEL\n", "% cfg.dipole = structure, see FT_DIPOLEFITTING\n", " cfg.mri = mri_aligned;\n", " cfg.mesh = lasershape;\n", " cfg.axes = 'yes'\n", "ft_geometryplot(cfg)" ] } ], "metadata": { "kernelspec": { "display_name": "MATLAB Kernel", "language": "matlab", "name": "jupyter_matlab_kernel" }, "language_info": { "file_extension": ".m", "mimetype": "text/x-matlab", "name": "matlab" } }, "nbformat": 4, "nbformat_minor": 5 }