{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Warp (old) result volumes to different spaces \n", "This is useful for visualisation. We used to analyse our data in the individual animals' functional spaces, but switched to warping everything to template space. This causes issues with existing visualisation scripts and notebooks. This workflow let's you easily warp existing data to the new space.\n", "\n", "It assumes that: \n", "- `${BasePath}/Results` is a folder or symlink with result files \n", "- `${{BasePath}/Reference` contains a copy of the `manual-masks` refernces and warps from `NHP-BIDS`" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "cd ..; BasePath=$(pwd); cd Notebooks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Configure \n", "Here, you should provide the information needed to do the warps " ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "subj=eddy # subject; BIDS convention, do not capitalize\n", "src_fld=${BasePath}/Results/Eddy-ctcheckerboard # source folder with results that need warping\n", "trg_fld=${BasePath}/Results/Eddy-ctcheckerboard-nmt # target folder where warped files will go\n", "srcspace='func' # the original space the results are in\n", "trgspace='nmt' # the results we want them warped to \n", "# it is only tested for func-to-nmt, mileage may vary for other warp-paths due slight nomenclature differences" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This information should be automatically populated based on the information above." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "warp_fld=${BasePath}/Reference/sub-${subj}/warps # folder where the warps are \n", "warp=${warp_fld}/sub-${subj}_${srcspace}2${trgspace}_res-1x1x1.mat # warp file for flirt \n", "ref=${warp_fld}/sub-${subj}_${srcspace}2${trgspace}_res-1x1x1.nii.gz # reference volume" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We first make a copy of the source folder to create the target folder. Doing it this way ensures, we won't touch the original data and we can then do the warping with a very simple command using the `find` function to identify all nifti files. The cell is configured to only copy when the target folder does not exist" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "# copy the results folder so we can easily overwrite\n", "if ! [ -d \"${trg_fld}\" ]; then\n", " cp -r ${src_fld} ${trg_fld};\n", "else\n", " echo ' >> Target folder already exist. Are you sure this is what you want?'\n", " echo 'Will not automatically overwrite, but make sure you do not apply warps multiple times!'\n", "fi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Perform the warps \n", "The command below will warp all nifti files with the specified warp. Let's unpack the command a bit: \n", "`find` looks inside `${trg_fld}` for all files ending in `.nii.gz`. The `-execdir` flag means the function will then move focus to the folder where the found file is located and excute `flirt` to apply the warp `${warp}` to the found file with `${ref}` as the reference volume grid. It will overwrite the found file with the warped version of itself. `\"{}\"` are placeholders for the result of `find`.\n", "\n", "This will of course take a while to finish. You can speed it up by restricting the search (e.g., to *zstat*.nii.gz) but keep in mind that in that case there will still be unwarped files in the target folder. Probably to wait a a bit and do the entire folder. " ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "# warp all the nifti files in the results folder\n", "find \"${trg_fld}\" -name *.nii.gz -execdir \\\n", " flirt -applyxfm -init ${warp} -in \"{}\" -ref ${ref} -out \"{}\" \\;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Bash", "language": "bash", "name": "bash" }, "language_info": { "codemirror_mode": "shell", "file_extension": ".sh", "mimetype": "text/x-sh", "name": "bash" } }, "nbformat": 4, "nbformat_minor": 4 }