Wiki source code of 02. Installing PyNN - Linux

Version 59.1 by adavison on 2022/05/10 14:26

Hide last authors
adavison 59.1 1 (% class="box successmessage" %)
2 (((
3 Video: [[https:~~/~~/www.youtube.com/watch?v=Ui8_I9T0UcU>>https://www.youtube.com/watch?v=Ui8_I9T0UcU]]
4 )))
5
adavison 4.1 6 == Learning objectives ==
7
annedevismes 44.1 8 In this tutorial, you will learn how to install PyNN, together with the Brian 2, NEST and NEURON simulators, on Linux.
adavison 4.1 9
adavison 6.1 10 (% class="box infomessage" %)
11 (((
12 Note: There will be a separate tutorial for each environment.
13 )))
14
adavison 4.1 15 == Audience ==
16
annedevismes 38.1 17 This tutorial is intended for people with at least a basic knowledge of neuroscience (high-school level or above) and basic familiarity with the Python programming language. It should also be helpful for people who already have advanced knowledge of neuroscience and neural simulation, who simply wish to learn how to use PyNN and how it differs from other simulation tools they know.
adavison 4.1 18
19 == Prerequisites ==
20
shailesh 24.1 21 To follow this tutorial, you will need a computer with Linux and a good network connection. You will need to know how to open the terminal application for your operating system.
adavison 4.1 22
23 == Format ==
24
shailesh 27.1 25 These tutorials will be screencasts, in which the presenter runs commands in a terminal, and the viewer is expected to follow along. The intended duration is 10-15 minutes.
adavison 4.1 26
27 == Script ==
adavison 5.1 28
shailesh 11.1 29 (% class="box successmessage" %)
30 (((
31 **Slide** showing tutorial title, PyNN logo, link to PyNN service page.
32 )))
33
34 Hello, my name is X.
35
shailesh 12.1 36 This video is one of a series of tutorials for PyNN, which is Python software for modelling and simulating spiking neural networks.
adavison 5.1 37
shailesh 12.1 38 For a list of the other tutorials in this series, you can visit ebrains.eu/service/pynn, that's p-y-n-n.
39
40 (% class="box successmessage" %)
41 (((
42 **Slide** listing learning objectives
43 )))
44
shailesh 46.1 45 In this tutorial, I will guide you through setting up PyNN, together with the Brian 2, NEST, and NEURON simulators, on a Linux  environment. Note that we have a dedicated version of this tutorial for other environments, such as Mac OS, Windows, and EBRAINS Jupyter Lab.
shailesh 12.1 46
shailesh 18.1 47 (% class="box successmessage" %)
48 (((
49 **Slide** listing prerequisites
50 )))
adavison 5.1 51
shailesh 54.1 52 I will demonstrate the installation on a computer with Ubuntu 20.04 OS installed. The steps are likely to remain very similar for other versions of Ubuntu OS and are also not expected to vary significantly for other Linux distributions. In the latter case, you can find information on the Internet about how to carry out the equivalent of the tasks demonstrated here. Also, this tutorial focuses only on Python 3, because Python 2 is no longer supported. It is recommended to use Python version 3.6 or higher. I will be using Python 3.8.10 in this tutorial, because it is the default version provided with Ubuntu 20.04.
shailesh 18.1 53
shailesh 22.18 54 (% class="box errormessage" %)
55 (((
annedevismes 38.1 56 **Note**
57 Having multiple versions of Python on your system can produce issues while installing NEST. The method shown below will install NEST for the default version of Python provided by your Ubuntu OS. For example, for Ubuntu 18.04, this might be Python 3.6.9, and for Ubuntu 20.04, it will likely be 3.8.10. If you wish to associate the NEST installation with a different Python version installed on your system, please refer to the NEST installation instructions to do so on their website.
shailesh 22.18 58 )))
59
shailesh 21.4 60 (% class="box infomessage" %)
61 (((
shailesh 21.8 62 **Screencast** - terminal
shailesh 21.4 63 )))
64
annedevismes 40.1 65 In this tutorial, we will make use of virtual environments. This allows multiple Python projects to coexist on the same computer, even when they might have different, and even conflicting, requirements. It helps isolate projects, thereby preventing unrequested changes in others, when any one of them is updated.
adavison 5.1 66
annedevismes 40.1 67 Let's begin by creating a directory for our project.
shailesh 21.8 68
shailesh 21.4 69 (% class="box infomessage" %)
shailesh 21.2 70 (((
shailesh 21.8 71 **Screencast** - terminal
72 \\(% style="color:#000000" %)cd ~~
shailesh 21.5 73 mkdir pynn_project
74 cd pynn_project
shailesh 21.2 75 )))
adavison 5.1 76
shailesh 56.1 77 Next, we will create a virtual environment within this directory. Python 3 provides support for creating virtual environments. To use this, we first install the package named 'python3-venv':
shailesh 21.8 78
79 (% class="box infomessage" %)
80 (((
81 **Screencast** - terminal
shailesh 55.1 82
83 (% style="color:#000000" %)
84 sudo apt-get install python3-venv
shailesh 56.1 85 )))
86
shailesh 58.1 87 And once this is installed, we can create a new virtual environment by typing 'python3', '-m venv' to indicate the name module of the module we just installed, followed by the name we wish to assign to our virtual environment. Here, we have set this to 'pynn_env'.
shailesh 56.1 88
89 (% class="box infomessage" %)
90 (((
91 **Screencast** - terminal
92
93 (% style="color:#000000" %)
shailesh 55.1 94 python3 -m venv pynn_env
shailesh 21.8 95 )))
96
shailesh 27.10 97 (% class="box warningmessage" %)
98 (((
annedevismes 38.1 99 **Note**
100
101
annedevismes 40.1 102 Note that this command is 'python3' and not simply 'python'. This is because Ubuntu 20, by default, understands only the former. You can find on the Internet various ways to have 'python' also refer to 'python3', but for the purposes of this tutorial, we keep things simple and try to work with the bare minimum changes to the system.
shailesh 27.10 103 )))
104
annedevismes 40.1 105 This will create a sub-directory named 'pynn_env' within our project directory, with several files and sub-directories. Let's take a look at the 'site-packages' directory.
shailesh 21.8 106
107 (% class="box infomessage" %)
108 (((
109 **Screencast** - file explorer
shailesh 37.1 110 \\(% style="color:#000000" %)<< show directory contents; especially lib/python3.8/site-packages >>
shailesh 21.8 111 )))
112
annedevismes 40.1 113 As you see here, only a limited number of basic packages have currently been installed in this virtual environment. In the steps ahead, we will install various other packages, which you will be able to see here.
shailesh 21.8 114
annedevismes 40.1 115 To enter this virtual environment, and thereby use its resources in isolation from other projects on your computer, we have to "activate" it. This is achieved by running the command:
shailesh 21.11 116
117 (% class="box infomessage" %)
118 (((
119 **Screencast** - terminal
120 \\(% style="color:#000000" %)source pynn_env/bin/activate
121 )))
122
annedevismes 38.1 123 Notice how this changes the command prompt to show the name of your virtual environment. In our case, we have named it 'pynn_env', and this is now reflected as a prefix to the command prompt. This confirms that we are now in our new virtual environment.
shailesh 22.1 124
shailesh 27.4 125 (% class="box warningmessage" %)
126 (((
annedevismes 38.1 127 **Note**
128
129
annedevismes 40.1 130 You might be required to run the above command every time you open a new terminal window. Check that the terminal command prompt indicates the name of your virtual environment to confirm that you have indeed activated it.
shailesh 27.4 131 )))
132
shailesh 48.1 133 Before we proceed, let us run the following commands to ensure that our environment is setup as required:
134
135
136 (% class="box infomessage" %)
137 (((
138 **Screencast** - terminal
shailesh 49.1 139 \\(% style="color:#000000" %)pip install ~-~-upgrade pip
shailesh 48.1 140 sudo apt install make
141 )))
142
annedevismes 42.1 143 Now that we have our project's virtual environment setup, we are ready to install PyNN and other simulators. In general, it is advisable to install the various simulators (especially NEURON and NEST) prior to installing PyNN, because PyNN will then auto compile NEURON's NMODL fles and NEST's extensions during installation. In this tutorial, we will adopt this approach and begin by installing the simulators. For the purposes of this tutorial, we will demonstrate the installation of Brian2, NEST, and NEURON simulators.
shailesh 21.2 144
shailesh 27.18 145 (% class="box errormessage" %)
146 (((
147 **Note:**
annedevismes 38.1 148 \\If you have previously installed NEURON or NEST on your system and are installing PyNN now, you will have to compile NEURON's NMODL fles and NEST's extensions manually. For more instructions on this, take a look at:
shailesh 27.18 149 [[(% style="color:#000000" %)http:~~/~~/neuralensemble.org/docs/PyNN/installation.html>>http://neuralensemble.org/docs/PyNN/installation.html]]
150 )))
151
annedevismes 38.1 152 We start here with the installation of Brian 2. Brian 2 can be installed simply by using the pip command.
shailesh 21.4 153
shailesh 21.7 154 (% class="box infomessage" %)
155 (((
shailesh 21.10 156 **Screencast** - terminal
shailesh 22.4 157 \\(% style="color:#000000" %)pip install brian2
shailesh 21.7 158 )))
159
annedevismes 40.1 160 This will install Brian 2, along with all its dependencies such as 'cython', 'numpy', and so on. We can now go back to our virtual environment's 'site-packages' directory to see how it is populated with all these packages.
shailesh 21.7 161
shailesh 22.6 162 (% class="box infomessage" %)
163 (((
164 **Screencast** - file explorer
shailesh 37.1 165 \\(% style="color:#000000" %)<< show directory contents lib/python3.8/site-packages >>
shailesh 22.6 166 )))
167
shailesh 22.17 168 To confirm that we have properly installed Brian 2 on our computer, we can test as follows:
shailesh 22.4 169
shailesh 22.2 170 (% class="box infomessage" %)
171 (((
shailesh 22.4 172 **Screencast** - terminal
shailesh 27.10 173 \\(% style="color:#000000" %)python
shailesh 22.4 174 \\import brian2
shailesh 22.7 175 \\exit()
shailesh 22.4 176 )))
177
shailesh 27.10 178 (% class="box warningmessage" %)
179 (((
annedevismes 40.1 180 **Note**
181 \\You might remember that, earlier in this tutorial, we had to use the term 'python3' to run Python on our system. But here, as in the rest of this tutorial, we will simply write 'python'. This is possible because, once we have activated our virtual environment, this environment understands that both 'python' and 'python3' are equivalent.
shailesh 27.10 182 )))
183
annedevismes 38.1 184 If there are no error messages here, and the import is successful, we have completed the installation of Brian 2.
shailesh 22.5 185
annedevismes 40.1 186 We will now move on to install the NEST simulator. Unlike Brian 2, NEST is not a Python package and therefore, it cannot be installed via the 'pip' command.
shailesh 22.7 187
annedevismes 43.1 188 At the time of creating this tutorial, the latest version of NEST is v3.1. This is currently supported by PyNN v0.10, and it is likely that other versions of NEST are potentially incompatible with this version of PyNN. The installation is done by first adding the PPA repository for NEST and updating apt, followed by the installation of NEST itself.
shailesh 22.7 189
190 (% class="box infomessage" %)
191 (((
192 **Screencast** - terminal
193 \\(% style="color:#000000" %)sudo add-apt-repository ppa:nest-simulator/nest
194 sudo apt-get update
195 \\sudo apt-get install nest
196 )))
197
shailesh 27.3 198 This installs the NEST module along with PyNEST, which is a Python interface for controlling the NEST kernel. This allows us to use NEST via Python. To confirm that we have properly installed NEST on our computer, we can test as follows:
shailesh 22.7 199
shailesh 22.20 200 (% class="box infomessage" %)
201 (((
shailesh 22.21 202 **Screencast** - terminal
shailesh 27.2 203 \\(% style="color:#000000" %)nest
shailesh 45.1 204
205 (% style="color:#000000" %)exit
shailesh 27.2 206 )))
207
annedevismes 43.1 208 This will display the NEST banner, which mentions the version amongst other info. Here, as we can see, we have now installed NEST v3.1 on our system. Next, let's verify that this is indeed accessible via Python.
shailesh 27.2 209
210 (% class="box infomessage" %)
211 (((
212 **Screencast** - terminal
shailesh 27.10 213 \\(% style="color:#000000" %)python
shailesh 22.21 214 \\import nest
215 \\exit()
shailesh 22.20 216 )))
217
shailesh 27.5 218 (% class="box warningmessage" %)
219 (((
annedevismes 38.1 220 **Note**
annedevismes 40.1 221 \\I find that I receive a "no module named nest" error when trying this right after installing NEST. But it succeeds after a restart. So, if you do observe an error, close all programs, restart your computer, and try again. This time, it should execute as expected.
shailesh 27.5 222 )))
223
annedevismes 38.1 224 If there are no error messages here, and the import is successful, we have completed installing NEST simulator and are able to load it via Python.
shailesh 22.20 225
shailesh 53.1 226 We next move on to the third simulator, NEURON. Similar to Brian2, the installation for NEURON can be easily done via the 'pip' command. Do note that this method of installation does not auto-enable MPI support, which would be required for running simulations in parallel. Do visit the NEURON website if you wish to install on clusters or HPC machines.
shailesh 22.20 227
shailesh 27.8 228 (% class="box infomessage" %)
229 (((
230 **Screencast** - terminal
231 \\(% style="color:#000000" %)pip install neuron
232 )))
shailesh 22.20 233
annedevismes 40.1 234 This installs the NEURON simulator on our system. To confirm that we have properly installed NEURON, we can test as follows:
shailesh 27.8 235
236 (% class="box infomessage" %)
237 (((
238 **Screencast** - terminal
shailesh 37.2 239 \\(% style="color:#000000" %)nrniv
shailesh 37.3 240 \\quit()
shailesh 27.8 241 )))
242
annedevismes 40.1 243 This will display the NEURON banner, which mentions the version amongst other info. Here, as we can see, we have now installed NEURON v8.0.0 on our system. Next, let's verify that this is indeed accessible via Python.
shailesh 27.8 244
245 (% class="box infomessage" %)
246 (((
247 **Screencast** - terminal
shailesh 27.10 248 \\(% style="color:#000000" %)python
shailesh 27.8 249 \\from neuron import h
250 \\exit()
251 )))
252
annedevismes 38.1 253 If there are no error messages here, and the import is successful, we have completed installing NEURON simulator and are able to load it via Python.
shailesh 27.8 254
annedevismes 40.1 255 Now that we have installed all the simulators we intend to use, we move on to installing PyNN itself. Because PyNN is a Python package, we can install it easily using the 'pip' command:
shailesh 27.8 256
shailesh 27.18 257 (% class="box infomessage" %)
258 (((
259 **Screencast** - terminal
shailesh 35.1 260 \\(% style="color:#000000" %)pip install PyNN
shailesh 27.18 261 )))
262
shailesh 32.2 263 (% class="box warningmessage" %)
264 (((
annedevismes 38.1 265 **Note**
annedevismes 40.1 266 \\Note that PyNN project is spelt with a captial P, small y, and two captital N. The pip command is case-insensitive, so you may write it differently here. But the Python module, as we will see later, is case-sensitive and is spelt starting with a small P. This is in line with general Python convention, whereby package names start with small letters. Thus, while importing PyNN module via Python, it needs to be written as 'pyNN'.
shailesh 32.2 267 )))
268
annedevismes 38.1 269 To verify that PyNN has been successfully installed on our system and that it is indeed able to communicate with the other simulators that we installed earlier, we can try running:
shailesh 27.18 270
shailesh 27.20 271 (% class="box infomessage" %)
272 (((
273 **Screencast** - terminal
shailesh 27.18 274
shailesh 27.20 275 (% style="color:#000000" %)python
276 \\import pyNN.brian2 as sim
277
278 (% style="color:#000000" %)sim.setup()
279
280 (% style="color:#000000" %)sim.end()
281 )))
282
annedevismes 44.1 283 This confirms that PyNN has been properly setup and also that it is able to employ Brian 2. To verify that PyNN is also able to communicate with NEST and NEURON simulators, we can do the following:
shailesh 27.20 284
285 (% class="box infomessage" %)
286 (((
287 **Screencast** - terminal
288
289 (% style="color:#000000" %)python
290 \\import pyNN.nest as sim
291
292 (% style="color:#000000" %)sim.setup()
293
294 (% style="color:#000000" %)sim.end()
295
296
297
298 (% style="color:#000000" %)import pyNN.neuron as sim
299
300 (% style="color:#000000" %)sim.setup()
301
302 (% style="color:#000000" %)sim.end()
303 )))
304
annedevismes 38.1 305 This confirms that all the required software packages have been successfully completed and are functioning as expected.
shailesh 27.20 306
shailesh 47.1 307 As a last step, we will install a Python package named 'matplotlib' that would come in handy in the tutorials ahead.
308
309 (% class="box infomessage" %)
310 (((
311 **Screencast** - terminal
312 \\(% style="color:#000000" %)pip install matplotlib
313 )))
314
shailesh 21.1 315 (% class="box successmessage" %)
316 (((
317 **Slide** recap of learning objectives
318 )))
adavison 5.1 319
annedevismes 40.1 320 That is the end of this tutorial, in which I've demonstrated how to install PyNN and other required simulators in a Linux system. You are now ready to start modeling! To learn about model development in PyNN, take a look at our next tutorial. Also, we will be releasing a series of tutorials, throughout the rest of 2021 and 2022, to introduce more advanced features of PyNN, so keep an eye on the EBRAINS website.
shailesh 20.1 321
shailesh 21.1 322 (% class="box successmessage" %)
323 (((
shailesh 27.12 324 **Slide** useful links
325 \\[[(% style="color:#000000" %)https:~~/~~/realpython.com/python-virtual-environments-a-primer/>>https://realpython.com/python-virtual-environments-a-primer/]](%%)
326 [[(% style="color:#000000" %)https:~~/~~/briansimulator.org/install/>>https://briansimulator.org/install/]](%%)
shailesh 27.13 327 [[(% style="color:#000000" %)https:~~/~~/nest-simulator.readthedocs.io/en/v3.1/installation/index.html>>https://nest-simulator.readthedocs.io/en/v3.1/installation/index.html]](%%)
shailesh 27.14 328 [[(% style="color:#000000" %)https:~~/~~/neuron.yale.edu/neuron/>>https://neuron.yale.edu/neuron/]]
shailesh 27.12 329 )))
330
shailesh 27.16 331 We have listed here some links that might be of interest to users who wish to find more details about the various softwares employed in this tutorial.
shailesh 27.12 332
333 (% class="box successmessage" %)
334 (((
shailesh 21.1 335 **Slide** acknowledgements, contact information
336 )))
shailesh 20.1 337
shailesh 21.1 338 (% class="wikigeneratedid" %)
annedevismes 38.1 339 PyNN has been developed by many different people, with financial support from several organisations. I'd like to mention in particular the CNRS and the European Commission, through the FACETS, BrainScaleS, and Human Brain Project grants.
shailesh 20.1 340
shailesh 21.1 341 (% class="wikigeneratedid" %)
annedevismes 38.1 342 For more information, visit neuralensemble.org/PyNN. If you have questions you can contact us through the PyNN Github project, the NeuralEnsemble forum, EBRAINS support, or the EBRAINS Community.