Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Program: https://docs.google.com/document/d/11LQgxC-LZPG_TXS3MP9tYXNWABAQdTI00iavk-tKttU/edit\n",
"__We need to do some setting up, so get your laptop ready!__\n",
"__Make sure you have FSL 6.0.3 installed and working!__\n",
"__End all sentences with an exclamation mark!__\n",
"\n",
"https://git.fmrib.ox.ac.uk/fsl/pytreat-practicals-2020/tree/master/talks/introduction/pytreat_intro.md\n",
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"\n",
"\n",
"## Overview\n",
"\n",
"\n",
"* [Python in a nutshell](#python-in-a-nutshell)\n",
"* [`fslpython`](#fslpython)\n",
"* [Running Python scripts](#running-python-scripts)\n",
"* [Interactive Python: IPython](#interactive-python-ipython)\n",
"* [Python editors](#python-editors)\n",
"* [Python in your browser: Jupyter Notebook](#python-in-your-browser-jupyter-notebook)\n",
"* [Git](#git)\n",
"* [The PyTreat practicals](#the-pytreat-practicals)\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"python-in-a-nutshell\"></a>\n",
"## Python in a nutshell\n",
"\n",
"\n",
"### Pros\n",
"\n",
"\n",
"* _Flexible_ Feel free to use functions, classes, objects, modules and\n",
" packages. Or don't - it's up to you!\n",
"\n",
"* _Fast_ If you do things right (in other words, if you use `numpy`)\n",
"\n",
"* _Dynamically typed_ No need to declare your variables, or specify their\n",
" types."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = 'Great, I am _so_ sick of writing \"char *a;\"!'\n",
"print(a)\n",
"\n",
"a = 12345\n",
"print('a is now an number!', a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* _Intuitive syntax_ How do I run some code for each of the elements in my\n",
" list?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = [1, 2, 3, 4, 5]\n",
"\n",
"for elem in a:\n",
" print(elem)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Cons\n",
"\n",
"* _Dynamically typed_ Easier to make mistakes, harder to catch them\n",
"\n",
"* _No compiler_ See above\n",
"\n",
"* _Slow_ if you don't do things the right way\n",
"\n",
"* _Python 2 is not the same as Python 3_ But there's an easy solution: Forget\n",
" that Python 2 exists.\n",
"\n",
"* _Hard to manage different versions of python_ But we have a solution for\n",
" you: `fslpython`.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"fslpython\"></a>\n",
"## `fslpython`\n",
"\n",
"\n",
"FSL 5.0.10 and newer comes with its own version of Python, bundled with nearly\n",
"all of the scientific libraries that you are likely to need.\n",
"\n",
"\n",
"So if you use `fslpython` for all of your development, you can be sure that it\n",
"will work in FSL!\n",
"\n",
"\n",
"> `fslpython` is based on _conda_ and, in FSL 6.0.3, is Python version\n",
"> 3.7.3. You can read more about conda [here](https://conda.io/docs/).\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"running-python-scripts\"></a>\n",
"## Running Python scripts\n",
"\n",
"\n",
"Here's a basic Python script - a _Hello world_ for neuroimaging:\n",
"\n",
"\n",
"> ```\n",
"> #!/usr/bin/env fslpython\n",
">\n",
"> # That first line up there ensures that your\n",
"> # script will be executed in the fslpython\n",
"> # environment. If you are writing a general\n",
"> # Python script, you should use this line\n",
"> # instead: #!/usr/bin/env python\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",
"> # We can get to our command\n",
"> # line arguments via sys.argv\n",
"> fpath = sys.argv[1]\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",
"> # Now we're working with a\n",
"> # numpy array.\n",
"> nzmean = data[data != 0].mean()\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/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/on/your/computer.nii.gz\n",
"> ```\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"interactive-python-ipython\"></a>\n",
"## Interactive Python: IPython\n",
"\n",
"\n",
"Python is an [_interpreted\n",
"language_](https://en.wikipedia.org/wiki/Interpreted_language), like MATLAB. So\n",
"you can either write your code into a file, and then run that file, or you can\n",
"type code directly into a Python interpreter.\n",
"\n",
"\n",
"Python has a standard interpreter built-in - run `fslpython` in a terminal,\n",
"and see what happens (use CTRL+D to exit).\n",
"\n",
"\n",
"__But__ there is another interpreter called [IPython](https://ipython.org/)\n",
"which is vastly superior to the standard Python interpreter. Use IPython\n",
"instead! It is already installed in `fslpython`, so if you want to do some\n",
"interactive work, you can use `fslipython` in a terminal.\n",
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"\n",
"\n",
"__Exercise__ Do it now! Start `fslipython`, then copy/paste this code into the\n",
"prompt!\n",
"\n",
"\n",
"> ```\n",
"> # this line is not python - it is\n",
"> # specific to ipython/jupyter notebook\n",
"> %matplotlib\n",
">\n",
"> import numpy as np\n",
">\n",
"> x = np.concatenate(([0.25, 0.75], np.arange(0.1, 1.0, 0.1)))\n",
"> y = np.concatenate(([0.75, 0.75], -np.sin(np.linspace(np.pi / 4, 3 * np.pi / 4, 9))))\n",
">\n",
"> import matplotlib.pyplot as plt\n",
">\n",
"> fig = plt.figure()\n",
"> ax = fig.add_subplot(111)\n",
">\n",
"> ax.scatter(x, y)\n",
"> ```\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"python-editors\"></a>\n",
"## Python editors\n",
"\n",
"\n",
"> Summary:\n",
"> - Make your tab key insert four spaces. Don't use tab characters in Python\n",
"> code.\n",
">\n",
"> - Use [Spyder](https://pythonhosted.org/spyder/) if you want a MATLAB-like\n",
"> envionment (focus on analysis, rather than coding).\n",
">\n",
"> - Use [PyCharm](https://www.jetbrains.com/pycharm/) if you want an IDE-like\n",
"> environment (focus on coding, rather than analysis).\n",
">\n",
"> - Use [Atom](https://atom.io/) or [VS Code](https://code.visualstudio.com/)\n",
"> if you like using the latest and greatest.\n",
">\n",
"> - If you like your existing editor, use it. But you will be better off if\n",
"> you can integrate it with `fslpython`, [pylint](https://www.pylint.org/)\n",
"> and [pyflakes](https://github.com/PyCQA/pyflakes).\n",
"\n",
"\n",
"You can use any text editor that you want to edit Python files. But the one\n",
"golden rule that you must follow, no matter what editor you use:\n",
"\n",
"\n",
"__Configure your tab key to insert four spaces. Don't use tab characters in\n",
"Python code!__\n",
"\n",
"\n",
"This is the [standard\n",
"convention](https://www.python.org/dev/peps/pep-0008/#indentation) for Python\n",
"code. If you deviate from this convention, and somebody else needs to work\n",
"with your code, they will be angry at you!\n",
"\n",
"\n",
"Now, with that out of the way, there are several good Python editors available\n",
"to you. If you are getting started with Python, we recommend\n",
"[PyCharm](https://www.jetbrains.com/pycharm/) or\n",
"[Spyder](https://pythonhosted.org/spyder/).\n",
"\n",
"\n",
"If you are used to MATLAB, and you do a lot of experimenting or interactive\n",
"work, then you might like Spyder. If you spend most of your time writing code\n",
"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",
"Spyder is a MATLAB-like environment for Python. It has a code editor and an\n",
"interactive IPython prompt. You can inspect variables that are in your\n",
"workspace, plot data, and so on and so forth.\n",
"\n",
"\n",
"Beyond that, Spyder is fairly simple - it does not have much in the way of\n",
"project management tools, or integration with version control (i.e. `git`).\n",
"\n",
"\n",
"Spyder can be installed directly into `fslpython`:\n",
"\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",
"> Create a link so you can call it easily:\n",
">\n",
"> ln -s $FSLDIR/fslpython/envs/fslpython/bin/spyder $FSLDIR/bin/fslspyder\n",
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"\n",
"Now to run Spyder, you can just type:\n",
"\n",
"\n",
"> ```\n",
"> fslspyder &\n",
"> ```\n",
"\n",
"\n",
"Now you need to make sure that Spyder is using the `fslpython` environment to\n",
"run your code.\n",
"\n",
"\n",
"1. Go to _python_ (the menu) > _Preferences_ > Python Interpreter\n",
"2. Make sure that _Use the following Python interpreter_ is selected\n",
"3. Make sure that the path is `$FSLDIR/fslpython/envs/fslpython/bin/python`\n",
" (for your specific value of `$FSLDIR`).\n",
"\n",
"\n",
"Type the following into the console to test that everything is working:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"plt.plot([1, 2, 3], [4, 5, 6])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### PyCharm\n",
"\n",
"\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",
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
"\n",
"\n",
"And it is also easy to install - simply download the Community edition from\n",
"the [home page](https://www.jetbrains.com/pycharm/). Then, if you are on a\n",
"mac, double-click the `.dmg` file, and drag PyCharm into your `/Applications/`\n",
"folder. Then double-click on PyCharm to start it.\n",
"\n",
"\n",
"Once you have chosen a theme, you will be asked if you would like to create a\n",
"_Launcher script_ - do this, because you will then be able to open files from\n",
"the terminal by typing `charm [filename]`.\n",
"\n",
"\n",
"Now you will be presented with a _Welcome to PyCharm_ window. __Before doing\n",
"anything else__, click on the _Configure_ button down in the bottom right, and\n",
"choose _Preferences_. Then in the _Project Interpreter_ section:\n",
"\n",
"\n",
"1. Click on the gear button and choose _Add local..._\n",
"2. Choose _Existing environment_\n",
"3. Click on the _..._ button, and navigate to\n",
" `$FSLDIR/fslpython/envs/fslpython/bin/python`.\n",
"\n",
"\n",
"### Other options\n",
"\n",
"\n",
"Emacs is capable of being used as a fully-fledged Python IDE, if you have the\n",
"time and patience to configure it.\n",
"\n",
"If you are the fashionable sort, try one of these:\n",
"\n",
"- [Visual Studio Code](https://code.visualstudio.com/)\n",
"- [Atom](https://atom.io/)\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"python-in-your-browser-jupyter-notebook\"></a>\n",
"## Python in your browser: Jupyter Notebook\n",
"\n",
"\n",
"It is possible to do your Python-based development and experimentation inside\n",
"your web browser, thanks to the [Jupyter project](https://jupyter.org/).\n",
"\n",
"\n",
"Jupyter works in the following way:\n",
"\n",
"1. You start a Juptyer web server on your computer. This web server provides\n",
" the environment in which your Python code is executed.\n",
"\n",
"2. You open https://localhost:8888 (or similar) in a web browser - this opens\n",
" a connection to the (locally running) web server.\n",
"\n",
"3. You start a \"Notebook\" (Jupyter's version of a file), and start typing.\n",
" You can put text, LaTeX, and of course Python code into a notebook.\n",
"\n",
"4. All of the code that you write gets sent to the web server and\n",
" executed. Then the results get sent back to the web browser, and displayed\n",
" in your notebook - magic!\n",
"\n",
"\n",
"All of the PyTreat practicals are written in a Jupyter notebook. Some of the\n",
"talks are too - you're looking at a Jupyter Notebook right now!\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"git\"></a>\n",
"## Git\n",
"\n",
"\n",
"All the cool kids these days use [git](https://git-scm.com/) to\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",
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
"\n",
"\n",
"Git is different from CVS and SVN in that it is _distributed_. In CVS and SVN,\n",
"there is only one central repository. You check out a copy of the source from\n",
"the repository, make some changes, and then commit those changes back to the\n",
"central repository.\n",
"\n",
"\n",
"In git, there are multiple repositories. There is usually one repository which\n",
"acts as the central one, but you will _clone_ (or _fork_) that central\n",
"repository to create your own full copy of the repository.\n",
"\n",
"\n",
"Then, you can make changes and commit them in your own repository. And at any\n",
"point, you can push your changes back to the central repository.\n",
"\n",
"\n",
"### WIN's gitlab server\n",
"\n",
"\n",
"https://git.fmrib.ox.ac.uk/\n",
"\n",
"\n",
"If you have a FMRIB account, then you also have a Gitlab account. Gitlab is a\n",
"git server that you can use to store your \"central\" git repository. Gitlab is\n",
"very similar to https://www.github.com, but it is managed by WIN, and your\n",
"code is not publicly visible (although it can be if you want). Gitlab backs\n",
"up your code automatically, and has a nice web interface.\n",
"\n",
"You can have up to 10 projects on your gitlab account - talk to the FMRIB IT\n",
"people if you need more.\n",
"\n",
"\n",
"### Using git and gitlab\n",
"\n",
"\n",
"> We need to go through a couple of intiial configuration steps before\n",
"> proceeding. You only need to do this once (for each computer that you use).\n",
">\n",
"> First, run these commands to configure git on your system.\n",
">\n",
"> ```\n",
"> git config --global user.name \"Your name\"\n",
"> git config --global user.email \"Your email address\"\n",
"> ```\n",
">\n",
"> Now you need to create a SSH key pair, so your computer can talk to the\n",
"> gitlab server without you having to log in. Don't be scared - there are\n",
"> detailed instructions on doing this at\n",
"> https://git.fmrib.ox.ac.uk/help/ssh/README.md - follow the instructions\n",
"> under the section entitled __Generating a new SSH key pair__.\n",
"\n",
"\n",
"Working with git and gitlab generally involves these steps:\n",
"\n",
"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 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. Organise all of your project files into their own folder__\n",
"__2. Create a repository for your project on gitlab__\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",
"__3. Turn your project folder into a git repository__\n",
"Now, follow the instructions that are listd on your gitlab project home page,\n",
"under __Existing folder__ (repeated here):\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",
"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",
"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",
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
"\n",
"\n",
"Now you can get to work! Whenever you make changes to your code that you want\n",
"saved, follow the `git add`, `git commit`, `git push` steps described above.\n",
"\n",
"\n",
"For example, let's say we have added a new file called `cool_module.py`. To\n",
"get this into git, we would do the following:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"git add cool_module.py\n",
"git commit -m \"Added cool module. It's super cool\"\n",
"git push origin master"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"the-pytreat-practicals\"></a>\n",
"## The PyTreat practicals\n",
"\n",
"\n",
"All of the practicals for PyTreat are hosted in a git repository at:\n",
"\n",
"\n",
"https://git.fmrib.ox.ac.uk/fsl/pytreat-practicals-2020\n",
"\n",
"\n",
"So let's go get them!\n",
"\n",
"\n",
"> ```\n",
"> git clone https://git.fmrib.ox.ac.uk/fsl/pytreat-practicals-2020.git\n",
"> cd pytreat-practicals-2020\n",
"> fslpython -m notebook\n",
"> ```"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}