A Brief Introduction to Using Python for Computational

A Brief Introduction to Using Python for Computational Neuroscience Brian Gregor Research Computing Services Information Services & Technology

Outline § § RCS & SCC intro Running Python vs. other languages Walk-through of a Python program

RCS Team and Expertise § Our Team § § § Scientific Programmers Systems Administrators Graphics/Visualization Specialists Account/Project Managers Special Initiatives (Grants) § Consulting Focus: § § § Bioinformatics Data Analysis / Statistics Molecular modeling Geographic Information Systems Scientific / Engineering Simulation Visualization § Maintains and administers the Shared Computing Cluster § CONTACT US: help@scc. bu. edu

BU’s Shared Computing Cluster § Available for use by faculty, staff, and students at BU (and collaborators). § High-speed networking connection to BU campuses § Located in Holyoke, MA § 100% powered by renewable energy § >18, 000 CPU cores § Dozens of GPUs § ~5 PB of storage

Running Python: Installing it yourself § There are many ways to install Python on your laptop/PC/etc. § Official Python: https: //www. python. org/downloads/ § Anaconda: https: //www. anaconda. com/download/ § Python X-Y: https: //python-xy. github. io/ § Enthought: https: //www. enthought. com/product/enthought-pythondistribution/

BU’s most popular personal option: Anaconda § https: //www. anaconda. com/download/ § Anaconda is a packaged set of programs including the Python language, a huge number of libraries, and several tools. § A ~630 MB download! § These include the Spyder development environment and Jupyter notebooks. § Anaconda can be used on the SCC, with some caveats.

Using Python on the SCC § Use the module software to get access to the various python versions. § module avail modulename will show available versions Module Name Description Notes python Python with many (!) extra libraries installed Versions 2. 7. 13 and 3. 6. 2 are the usual versions used. anaconda 2 Anaconda set up with Python 2. 7. x anaconda 3 Anaconda set up with Python 3. 6. x python 2 -intel Intel compiled and optimized Python Sometimes much faster than 2. 7. x standard Python. python 3 -intel Intel compiled and optimized Python Sometimes much faster than 3. 6. x standard Python. § Access a module: module load python/2. 7. 13

Python 2 vs. 3 § Python 2: released in 2000, Python 3 released in 2008 § Python 2 is in “maintenance mode” – no new features are expected § Py 3 is not completely compatible with Py 2 § For learning Python these differences are almost negligible § Which one to learn? § If your research group / advisor / boss have a preference use the same one. § If you have a compelling reason to focus on one vs the other § Ex. Using Python inside of other programs. § Otherwise just choose Py 3. This is where the language development is happening!

What is Python? § Python… § …is a general purpose interpreted programming language. § …is a language that supports multiple approaches to software design, principally structured and object-oriented programming. § …provides automatic memory management and garbage collection § …is extensible § …is dynamically typed.

Python Popularity § Economist, Jul 19, 2018: Python has brought computer programming to a vast new audience Google Trends Interest over time

Compiled Languages (ex. C++ or Fortran)

Interpreted Languages Source code files prog. py math. py Python interpreter (ex. Python or R) bytecode conversion Python interpreter: follows bytecode instructions python prog. py § Clearly, a lot less work is done to get a program to start running compared with compiled languages! § Bytecodes are an internal representation of the text program that can be efficiently run by the Python interpreter. § The interpreter itself is written in C and is a compiled program.

Comparison Interpreted Compiled § Faster development § Easier debugging § Longer development § Debugging can stop anywhere, swap in new code, more control over state of program § (almost always) takes less code to get things done § Slower programs § Sometimes as fast as compiled, rarely faster § Less control over program behavior § Edit / compile / test cycle is longer! § Harder to debug § Usually requires a special compilation § (almost always) takes more code to get things done § Faster § Compiled code runs directly on CPU § Can communicate directly with hardware § More control over program behavior

Working in Python § The Jupyter notebook system is very popular. § Offers a web-based system to create documents containing code, data, and visualizations.

Working in Python § The Spyder development environment is a more traditional software development system. § Analogous to Visual Studio or Xcode. § Text-based software development with integrated debugging, testing, and project management. § Other dev enviros: § Eclipse, Py. Charm, Visual Studio, Emacs

The Spyder IDE Variable and file explorer editor Python console

Some Python computations with the leaky integrate -and-fire neuron model § The LIF model is a simple spiking neuron model. § Based on an resistor in parallel with a capacitor circuit. Here we’ll use a constant current. § Voltage spikes are added when a firing time is reached and the output is set to 0 during a rest time. https: //neuronaldynamics. epfl. ch/online/Ch 1. S 3. html

LIF § For our Python example, we’ll look at some noisy data from one voltage spike. § A least-squares fit will be performed to compute the values for Rm and Cm from the model based on the known input current and measured potential.

The demo problem § Least Squares: “A mathematical procedure for finding the best-fitting curve to a given set of points by minimizing the sum of the squares of the offsets ("the residuals") of the points from the curve. ” (Mathworld) Some noisy data spike RC behavior § Should we fit to the entire data set? § Does it matter? § What does the fitting algorithm compute? resting

LIF solution for discrete time § The Python demo code: § Reads in the noise data file § Implements the discrete solution to the LIF above § Calls a library to perform the least-squares fit to estimate the membrane resistance and capacitance § Plots the results.
- Slides: 20