#MAC Installation SynthSR (Apple M1 Chip)

#STEP 1: Download most recent development version of FreeSurfer for macOS. Https://surfer.nmr.mgh.harvard.edu/pub/dist/freesurfer/dev/. The .pkg file should automatically install in <Applications>. Place licence file in development folder.

#STEP 2: Open terminal and set default shell to zsh
chsh -s /bin/zsh

#STEP 3: Setup freesurfer environment by running the following in terminal.
export FREESURFER_HOME=/Applications/freesurfer/dev
source $FREESURFER_HOME/SetUpFreeSurfer.sh

# if running dev version, code returned following install should read as below:
-------- freesurfer-darwin-macOS-dev-20221204-0049b69 --------
Setting up environment for FreeSurfer/FS-FAST (and FSL)
FREESURFER_HOME   /Applications/freesurfer/dev
FSFAST_HOME       /Applications/freesurfer/dev/fsfast
FSF_OUTPUT_FORMAT nii.gz
SUBJECTS_DIR      /Applications/freesurfer/dev/subjects
MNI_DIR           /Applications/freesurfer/dev/mni
FSL_DIR           /usr/local/fsl
annabellesorby-adams@Annabelles-M1-Pro-2 ~ % 

#STEP 4: Uninstall the existing tensorflow
sudo -H /Applications/freesurfer/dev/python/bin/python3 -m pip uninstall tensorflow
# you will be prompted to enter passord and confirm deletion of existing tensorflow package (Y/n). Enter <y>

#STEP 5: Install new tensorflow

#STEP 5.1 Download HomeBrew via the following link: https://brew.sh/.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

#STEP 5.2: Install xcode
xcode-select --install
#You will be prompted if xcode is already downloaded. To check for available updates run:
softwareupdate --list

#STEP 5.3: Download conda MiniForge via the following GitHublink: https://github.com/conda-forge/miniforge.  
#Ensure you install the OS X arm64 (Apple Silicon) version. Place file on desktop and change permissions.
cd desktop
chmod +x MiniForge3<version on desktop>
./MiniForge<version on desktop>
# run enter to complete installation. Click <yes> to agree to license

#STEP 5.4: Create new virtual environment
cd desktop
conda create --name env_tf python=3.9

#STEP 5.5: Install tensorflow
conda install -c apple tensorflow-deps -y
python -m pip install tensorflow-macos
pip install tensorflow-metal

#STEP 6: Run SynthSR