diff --git a/talks/introduction/example_project/myscript.py b/talks/introduction/example_project/myscript.py old mode 100644 new mode 100755 index 3b768e3f0f9e01019ec53b555ddbcacb3fa1174f..02f2bc91fa5bcad1b387512986fc75f6f0b499b9 --- a/talks/introduction/example_project/myscript.py +++ b/talks/introduction/example_project/myscript.py @@ -6,42 +6,23 @@ # Python script, you should use this line # instead: #!/usr/bin/env python - # In Python, we need to "import" libraries # (called modules) before we can use them. import sys import nibabel as nib -# Python uses indentation instead of braces -# for all of its control structures - if -# while, and for statements, functions and -# classes, and so on and so forth. -# -# The standard convention for indentation -# is four spaces. Please don't use tab -# characters! -def main(): - - # We can get to our command - # line arguments via sys.argv - fpath = sys.argv[1] - - # We can use nibabel to load - # NIFTI images (and other - # neuroimaging data formats) - img = nib.load(fpath) - data = img.get_data() - - # Now we're working with a - # numpy array. - nzmean = data[data != 0].mean() - - print('mean:', nzmean) +# We can get to our command +# line arguments via sys.argv +fpath = sys.argv[1] - sys.exit(0) +# We can use nibabel to load +# NIFTI images (and other +# neuroimaging data formats) +img = nib.load(fpath) +data = img.get_data() +# Now we're working with a +# numpy array. +nzmean = data[data != 0].mean() -# This bit is the Python equivalent of -# "int main()" in a C or C++ program. -if __name__ == '__main__': - main() +print('mean:', nzmean) diff --git a/talks/introduction/pytreat_intro.ipynb b/talks/introduction/pytreat_intro.ipynb index 270b2b264db137fb6bb4101c1dd00a3bd2b0eb42..5eed1ef09bb1faa4a040c571a360f16df964364f 100644 --- a/talks/introduction/pytreat_intro.ipynb +++ b/talks/introduction/pytreat_intro.ipynb @@ -10,13 +10,15 @@ "Program: https://docs.google.com/document/d/10CwLEhUi-YiwfC2F40QCVm6eEVwKiaXkfTKz67xWAfM/edit?usp=sharing\n", "\n", "\n", - "__Get your laptop ready!__\n", + "__We need to do some setting up, so get your laptop ready!__\n", "\n", "__Make sure you have FSL 5.0.10 installed and working!__\n", "\n", + "__End all sentences with an exclamation mark!__\n", + "\n", "__Open this page in your web browser!__\n", "\n", - "https://git.fmrib.ox.ac.uk/fsl/pytreat-2018-practicals/tree/master/talks/introduction/pytreat_intro.ipynb\n", + "https://git.fmrib.ox.ac.uk/fsl/pytreat-2018-practicals/tree/master/talks/introduction/pytreat_intro.md\n", "\n", "\n", "## Overview\n", @@ -120,7 +122,7 @@ "## Running Python scripts\n", "\n", "\n", - "Here's a basic Python script:\n", + "Here's a basic Python script - a _Hello world_ for neuroimaging:\n", "\n", "\n", "> ```\n", @@ -132,56 +134,37 @@ "> # Python script, you should use this line\n", "> # instead: #!/usr/bin/env python\n", ">\n", - ">\n", "> # In Python, we need to \"import\" libraries\n", "> # (called modules) before we can use them.\n", "> import sys\n", "> import nibabel as nib\n", ">\n", - "> # Python uses indentation instead of braces\n", - "> # for all of its control structures - if\n", - "> # while, and for statements, functions and\n", - "> # classes, and so on and so forth.\n", - "> #\n", - "> # The standard convention for indentation\n", - "> # is four spaces. Please don't use tab\n", - "> # characters!\n", - "> def main():\n", - ">\n", - "> # We can get to our command\n", - "> # line arguments via sys.argv\n", - "> fpath = sys.argv[1]\n", - ">\n", - "> # We can use nibabel to load\n", - "> # NIFTI images (and other\n", - "> # neuroimaging data formats)\n", - "> img = nib.load(fpath)\n", - "> data = img.get_data()\n", + "> # We can get to our command\n", + "> # line arguments via sys.argv\n", + "> fpath = sys.argv[1]\n", ">\n", - "> # Now we're working with a\n", - "> # numpy array.\n", - "> nzmean = data[data != 0].mean()\n", + "> # We can use nibabel to load\n", + "> # NIFTI images (and other\n", + "> # neuroimaging data formats)\n", + "> img = nib.load(fpath)\n", + "> data = img.get_data()\n", ">\n", - "> print('mean:', nzmean)\n", + "> # Now we're working with a\n", + "> # numpy array.\n", + "> nzmean = data[data != 0].mean()\n", ">\n", - "> sys.exit(0)\n", - ">\n", - ">\n", - "> # This bit is the Python equivalent of\n", - "> # \"int main()\" in a C or C++ program.\n", - "> if __name__ == '__main__':\n", - "> main()\n", + "> print('mean:', nzmean)\n", "> ```\n", "\n", "\n", "__Exercise__ Save the above code to a file called `script.py`, then run this\n", - "in a terminal (replace `/path/to/some/image.nii.gz` with a path to some image\n", - "on your computer):\n", + "in a terminal (replace `/path/to/some/image/on/your/computer.nii.gz` with a\n", + "path to some image on your computer):\n", "\n", "\n", "> ```\n", "> chmod a+x script.py\n", - "> ./script.py /path/to/some/image.nii.gz\n", + "> ./script.py /path/to/some/image/on/your/computer.nii.gz\n", "> ```\n", "\n", "\n", @@ -205,12 +188,10 @@ "link to it - do this now in a terminal:\n", "\n", "\n", - "> ```\n", - "> # You might need to \"sudo\" this\n", - "> # if your version of FSL needs\n", - "> # admin privileges to modify.\n", - "> ln -s $FSLDIR/fslpython/envs/fslpython/bin/ipython $FSLDIR/bin/fslipython\n", - "> ```\n", + "> You might need to \"sudo\" this if your version of FSL needs admin privileges\n", + "> to modify.\n", + ">\n", + "> ln -s $FSLDIR/fslpython/envs/fslpython/bin/ipython $FSLDIR/bin/fslipython\n", "\n", "\n", "Now if you want to do some interactive work, you can use `fslipython` in a\n", @@ -287,6 +268,20 @@ "rather than experimenting, then go with PyCharm.\n", "\n", "\n", + "Importantly, both PyCharm and Spyder will correctly indent your Python code!\n", + "\n", + "\n", + "> If you are going to stick with Emacs for your Python development, then it\n", + "> should correctly indent Python code by default. But if it isn't, add\n", + "> the following to your `~/.emacs` file:\n", + ">\n", + "> (defun my-python-mode-hook ()\n", + "> (setq indent-tabs-mode nil)\n", + "> (setq python-indent 4) ; for versions prior to 24.3\n", + "> (setq python-indent-offset 4)) ; for versions 24.3 or newer\n", + "> (add-hook 'python-mode-hook 'my-python-mode-hook)\n", + "\n", + "\n", "### Spyder\n", "\n", "\n", @@ -302,15 +297,16 @@ "Spyder can be installed directly into `fslpython`:\n", "\n", "\n", - "> ```\n", - "> # If your FSL installation requires\n", - "> # administrative privileges to modify,\n", - "> # you will need to prefix these\n", - "> # commands with sudo.\n", - "> $FSLDIR/fslpython/bin/conda install -n fslpython -y spyder\n", - "> ln -s $FSLDIR/fslpython/envs/fslpython/bin/spyder $FSLDIR/bin/fslspyder\n", - "> ```\n", + "> If your FSL installation requires administrative privileges to modify, you\n", + "> will need to prefix these commands with sudo.\n", + ">\n", + "> Install Spyder:\n", + ">\n", + "> $FSLDIR/fslpython/bin/conda install -n fslpython -y spyder\n", "\n", + "> Create a link so you can call it easily:\n", + ">\n", + "> ln -s $FSLDIR/fslpython/envs/fslpython/bin/spyder $FSLDIR/bin/fslspyder\n", "\n", "Now to run Spyder, you can just type:\n", "\n", @@ -350,10 +346,10 @@ "### PyCharm\n", "\n", "\n", - "PyCharm is a general-purpose Python development environment. Unlike Spyder, it\n", - "does not have an integrated console or variable explorer. But it has more\n", - "tools to help you write code, and better file management/version control\n", - "integration.\n", + "PyCharm is a general-purpose Python development environment. When compared to\n", + "Spyder, it is less geared towards interactive analysis, but has better code\n", + "editing tools (e.g. autocomplete and refactoring), and better file\n", + "management/version control integration.\n", "\n", "\n", "And it is also easy to install - simply download the Community edition from\n", @@ -426,12 +422,14 @@ "terminal:\n", "\n", "\n", - "> ```\n", - "> # Remember to prefix with sudo if your\n", - "> # FSL install needs admin to modify.\n", - "> $FSLDIR/fslpython/bin/conda install -n fslpython -y jupyter\n", - "> ln -s $FSLDIR/fslpython/envs/fslpython/bin/jupyter $FSLDIR/bin/fsljupyter\n", - "> ```\n", + "\n", + "> Remember to prefix with sudo if your FSL install needs admin to modify.\n", + ">\n", + "> $FSLDIR/fslpython/bin/conda install -n fslpython -y jupyter\n", + "\n", + "> And add a link so you can call it easily:\n", + ">\n", + "> ln -s $FSLDIR/fslpython/envs/fslpython/bin/jupyter $FSLDIR/bin/fsljupyter\n", "\n", "\n", "<a class=\"anchor\" id=\"git\"></a>\n", @@ -439,7 +437,8 @@ "\n", "\n", "All the cool kids these days use [git](https://git-scm.com/) to\n", - "collaboratively work on their Python code.\n", + "collaboratively work on their Python code. The PyTreat is a great opportunity\n", + "to start learning and using it for your own work!\n", "\n", "\n", "Git is different from CVS and SVN in that it is _distributed_. In CVS and SVN,\n", @@ -498,66 +497,53 @@ "1. Add new files to your local repository, or make changes to existing files.\n", "2. Run `git add` on the new/changed files to _stage_ them.\n", "3. Run `git commit` to commit all staged changes to your local repository.\n", - "4. Run `git push` to push those changes to your gitlab repository.\n", + "4. Run `git push` to push those commits to your gitlab repository.\n", "\n", "\n", "When you start working on a new project (or if you have an existing project\n", "that you want to put into git):\n", "\n", "\n", - "__1. Create a project directory__\n", + "__1. Organise all of your project files into their own folder__\n", "\n", - "Or navigate to your existing project directory.\n", "\n", + "This sounds obvious, but just to be sure.\n", "\n", - "__2. Turn the directory into a git repository__\n", - "\n", - "> ```\n", - "> cd path/to/super/cool/project\n", - "> git init\n", - "> ```\n", - "\n", - "\n", - "__3. Add your files to git__\n", - "\n", - "If you want to add all of the files in your project directory into git, type\n", - "this:\n", - "\n", - "> ```\n", - "> git add .\n", - "> ```\n", "\n", + "__2. Create a repository for your project on gitlab__\n", "\n", - "Otherwise, if you only want certain files in git, then `git add` them one by\n", - "one (or with standard bash file patterns).\n", - "\n", - "You should avoid putting large binary files or data files into git - it works\n", - "best with source code. Talk to the FMRIB IT people if you really need to store\n", - "large files in git, as they can help you with this.\n", "\n", + "Log in to gitlab (https://git.fmrib.ox.ac.uk/), then click on the _+_ button\n", + "towards the top right, and select _New project_. Give the project a name and\n", + "choose its visiblity (note that _Public_ means your project will be visible to\n", + "the world).\n", "\n", - "__4. Commit your changes__\n", "\n", + "__3. Turn your project folder into a git repository__\n", "\n", - "> ```\n", - "> git commit -m \"A useful message describing the changes you are committing.\"\n", - "> ```\n", "\n", + "Now, follow the instructions that are listd on your gitlab project home page,\n", + "under __Existing folder__ (repeated here):\n", "\n", - "__5. Create a repository for your project on gitlab__\n", "\n", + "> cd existing_folder\n", + "> git init\n", + "> git remote add origin git@git.fmrib.ox.ac.uk:username/project.git\n", + "> git add .\n", + "> git commit -m \"Initial commit\"\n", + "> git push -u origin master\n", "\n", - "Log in to gitlab (https://git.fmrib.ox.ac.uk/), then click on the _+_ button\n", - "towards the top right, and select _New project_. Give the project a name and\n", - "choose its visiblity (note that _Public_ means your project will be visible to\n", - "the world).\n", "\n", + "The `git add .` line will add _all_ of the files in your project directory\n", + "into git. If you only want certain files in git, then `git add` them one by\n", + "one (or use standard bash file patterns, e.g. `git add *.py`).\n", "\n", - "Now, follow the instructions under __Existing Git repository__ to \"link\" your\n", - "local project repository to the one on gitlab.\n", + "You should avoid putting large binary files or data files into git - it works\n", + "best with plain text. Talk to the FMRIB IT people if you really need to store\n", + "large files in git, as they can help you with this.\n", "\n", "\n", - "__6. Develop your super cool project!__\n", + "__4. Develop your super cool project!__\n", "\n", "\n", "Now you can get to work! Whenever you make changes to your code that you want\n", diff --git a/talks/introduction/pytreat_intro.md b/talks/introduction/pytreat_intro.md index e663f009f7ebb47ec661d6e3cc5aadcf5d60e80e..6600f479dc3ebd3bf48e853945c5387bb99ce100 100644 --- a/talks/introduction/pytreat_intro.md +++ b/talks/introduction/pytreat_intro.md @@ -4,13 +4,15 @@ Program: https://docs.google.com/document/d/10CwLEhUi-YiwfC2F40QCVm6eEVwKiaXkfTKz67xWAfM/edit?usp=sharing -__Get your laptop ready!__ +__We need to do some setting up, so get your laptop ready!__ __Make sure you have FSL 5.0.10 installed and working!__ +__End all sentences with an exclamation mark!__ + __Open this page in your web browser!__ -https://git.fmrib.ox.ac.uk/fsl/pytreat-2018-practicals/tree/master/talks/introduction/pytreat_intro.ipynb +https://git.fmrib.ox.ac.uk/fsl/pytreat-2018-practicals/tree/master/talks/introduction/pytreat_intro.md ## Overview @@ -98,7 +100,7 @@ will work in FSL! ## Running Python scripts -Here's a basic Python script: +Here's a basic Python script - a _Hello world_ for neuroimaging: > ``` @@ -110,56 +112,37 @@ Here's a basic Python script: > # Python script, you should use this line > # instead: #!/usr/bin/env python > -> > # In Python, we need to "import" libraries > # (called modules) before we can use them. > import sys > import nibabel as nib > -> # Python uses indentation instead of braces -> # for all of its control structures - if -> # while, and for statements, functions and -> # classes, and so on and so forth. -> # -> # The standard convention for indentation -> # is four spaces. Please don't use tab -> # characters! -> def main(): -> -> # We can get to our command -> # line arguments via sys.argv -> fpath = sys.argv[1] -> -> # We can use nibabel to load -> # NIFTI images (and other -> # neuroimaging data formats) -> img = nib.load(fpath) -> data = img.get_data() +> # We can get to our command +> # line arguments via sys.argv +> fpath = sys.argv[1] > -> # Now we're working with a -> # numpy array. -> nzmean = data[data != 0].mean() +> # We can use nibabel to load +> # NIFTI images (and other +> # neuroimaging data formats) +> img = nib.load(fpath) +> data = img.get_data() > -> print('mean:', nzmean) +> # Now we're working with a +> # numpy array. +> nzmean = data[data != 0].mean() > -> sys.exit(0) -> -> -> # This bit is the Python equivalent of -> # "int main()" in a C or C++ program. -> if __name__ == '__main__': -> main() +> print('mean:', nzmean) > ``` __Exercise__ Save the above code to a file called `script.py`, then run this -in a terminal (replace `/path/to/some/image.nii.gz` with a path to some image -on your computer): +in a terminal (replace `/path/to/some/image/on/your/computer.nii.gz` with a +path to some image on your computer): > ``` > chmod a+x script.py -> ./script.py /path/to/some/image.nii.gz +> ./script.py /path/to/some/image/on/your/computer.nii.gz > ``` @@ -183,12 +166,10 @@ instead! It is already installed in `fslpython`, but we just need to create a link to it - do this now in a terminal: -> ``` -> # You might need to "sudo" this -> # if your version of FSL needs -> # admin privileges to modify. -> ln -s $FSLDIR/fslpython/envs/fslpython/bin/ipython $FSLDIR/bin/fslipython -> ``` +> You might need to "sudo" this if your version of FSL needs admin privileges +> to modify. +> +> ln -s $FSLDIR/fslpython/envs/fslpython/bin/ipython $FSLDIR/bin/fslipython Now if you want to do some interactive work, you can use `fslipython` in a @@ -265,6 +246,20 @@ work, then you might like Spyder. If you spend most of your time writing code rather than experimenting, then go with PyCharm. +Importantly, both PyCharm and Spyder will correctly indent your Python code! + + +> If you are going to stick with Emacs for your Python development, then it +> should correctly indent Python code by default. But if it isn't, add +> the following to your `~/.emacs` file: +> +> (defun my-python-mode-hook () +> (setq indent-tabs-mode nil) +> (setq python-indent 4) ; for versions prior to 24.3 +> (setq python-indent-offset 4)) ; for versions 24.3 or newer +> (add-hook 'python-mode-hook 'my-python-mode-hook) + + ### Spyder @@ -280,15 +275,16 @@ project management tools, or integration with version control (i.e. `git`). Spyder can be installed directly into `fslpython`: -> ``` -> # If your FSL installation requires -> # administrative privileges to modify, -> # you will need to prefix these -> # commands with sudo. -> $FSLDIR/fslpython/bin/conda install -n fslpython -y spyder -> ln -s $FSLDIR/fslpython/envs/fslpython/bin/spyder $FSLDIR/bin/fslspyder -> ``` +> If your FSL installation requires administrative privileges to modify, you +> will need to prefix these commands with sudo. +> +> Install Spyder: +> +> $FSLDIR/fslpython/bin/conda install -n fslpython -y spyder +> Create a link so you can call it easily: +> +> ln -s $FSLDIR/fslpython/envs/fslpython/bin/spyder $FSLDIR/bin/fslspyder Now to run Spyder, you can just type: @@ -320,10 +316,10 @@ plt.plot([1, 2, 3], [4, 5, 6]) ### PyCharm -PyCharm is a general-purpose Python development environment. Unlike Spyder, it -does not have an integrated console or variable explorer. But it has more -tools to help you write code, and better file management/version control -integration. +PyCharm is a general-purpose Python development environment. When compared to +Spyder, it is less geared towards interactive analysis, but has better code +editing tools (e.g. autocomplete and refactoring), and better file +management/version control integration. And it is also easy to install - simply download the Community edition from @@ -396,12 +392,14 @@ __Exercise__ Install Jupyter into `fslpython` - run these commands in a terminal: -> ``` -> # Remember to prefix with sudo if your -> # FSL install needs admin to modify. -> $FSLDIR/fslpython/bin/conda install -n fslpython -y jupyter -> ln -s $FSLDIR/fslpython/envs/fslpython/bin/jupyter $FSLDIR/bin/fsljupyter -> ``` + +> Remember to prefix with sudo if your FSL install needs admin to modify. +> +> $FSLDIR/fslpython/bin/conda install -n fslpython -y jupyter + +> And add a link so you can call it easily: +> +> ln -s $FSLDIR/fslpython/envs/fslpython/bin/jupyter $FSLDIR/bin/fsljupyter <a class="anchor" id="git"></a> @@ -409,7 +407,8 @@ terminal: All the cool kids these days use [git](https://git-scm.com/) to -collaboratively work on their Python code. +collaboratively work on their Python code. The PyTreat is a great opportunity +to start learning and using it for your own work! Git is different from CVS and SVN in that it is _distributed_. In CVS and SVN, @@ -475,59 +474,46 @@ When you start working on a new project (or if you have an existing project that you want to put into git): -__1. Create a project directory__ +__1. Organise all of your project files into their own folder__ -Or navigate to your existing project directory. +This sounds obvious, but just to be sure. -__2. Turn the directory into a git repository__ - -> ``` -> cd path/to/super/cool/project -> git init -> ``` - - -__3. Add your files to git__ - -If you want to add all of the files in your project directory into git, type -this: - -> ``` -> git add . -> ``` +__2. Create a repository for your project on gitlab__ -Otherwise, if you only want certain files in git, then `git add` them one by -one (or with standard bash file patterns). - -You should avoid putting large binary files or data files into git - it works -best with source code. Talk to the FMRIB IT people if you really need to store -large files in git, as they can help you with this. +Log in to gitlab (https://git.fmrib.ox.ac.uk/), then click on the _+_ button +towards the top right, and select _New project_. Give the project a name and +choose its visiblity (note that _Public_ means your project will be visible to +the world). -__4. Commit your changes__ +__3. Turn your project folder into a git repository__ -> ``` -> git commit -m "A useful message describing the changes you are committing." -> ``` +Now, follow the instructions that are listd on your gitlab project home page, +under __Existing folder__ (repeated here): -__5. Create a repository for your project on gitlab__ +> cd existing_folder +> git init +> git remote add origin git@git.fmrib.ox.ac.uk:username/project.git +> git add . +> git commit -m "Initial commit" +> git push -u origin master -Log in to gitlab (https://git.fmrib.ox.ac.uk/), then click on the _+_ button -towards the top right, and select _New project_. Give the project a name and -choose its visiblity (note that _Public_ means your project will be visible to -the world). +The `git add .` line will add _all_ of the files in your project directory +into git. If you only want certain files in git, then `git add` them one by +one (or use standard bash file patterns, e.g. `git add *.py`). -Now, follow the instructions under __Existing Git repository__ to "link" your -local project repository to the one on gitlab. +You should avoid putting large binary files or data files into git - it works +best with plain text. Talk to the FMRIB IT people if you really need to store +large files in git, as they can help you with this. -__6. Develop your super cool project!__ +__4. Develop your super cool project!__ Now you can get to work! Whenever you make changes to your code that you want