Python class with and without classes
Programming has to be practiced, it can not be learned from looking at slides alone. Here we give you some simple tasks to implement in Python, which add up to a mini-simulation of synaptic transmission in the end. We structured this exercise such that you can start with simple tasks and add complexity with each additional concept you learn in our lectures.
Task 0. Get ready to create Python programs
a.
You need a Python with some of the basic packages
b.
You need a good editor that is compatible with Python! Many people in the world are using VS Code. You can use what you want but don’t come to us with tears in your eyes if you use something else.
- How to install the VS code editor
- How to configure the editor for your needs
- Working with VS code
- Collection of general tips
Arrival of an action potential at the presynaptic terminal leads to the release of vesicles. Neurotransmitters drift through the synaptic cleft and induce input currents when arriving at the postsynaptic bouton on the dendrite. We will simulate this process in Python:
Task 1. Variables, Functions, Plotting
The postsynaptic current may be described by a function
- Plot this function over time for different parameters
, and annotate the axes properly. - Define a Python function PSP with default parameters
, which computes and returns .
Task 2. Loops, Conditional Execution, Flow Control
Transport of neurotransmitter from the release site
particles are released at time .- There are
steps from to . - In each time step
, each particle moves either one step forward or one step backward, with equal probability. - Particles at
can only move forward with probability . - Particles arriving at
are absorbed.
For each particle, compute the time it takes until it arrives at
Task 3. Some more plots \& functions
The postsynaptic current is proportional to the amount of neurotransmitter arriving at the postsynaptic terminal.
- Plot
and into the same graph and adjust the parameters , by hand until you obtain a good fit, use a legend to distinguish the two curves. - Quantify the fit error by computing the mean quadratic distance
between the two curves. - Define a Python function mqdis for computing
.
Task 4. Some more flow control
Fitting by hand is stupid, we have computers for that! Devise a procedure that minimizes
One idea is to start with two initial, positive parameters
- Compute two new parameters by randomly increasing or decreasing
and by a small percentage. - Keep the new parameters if they provide a better fit, otherwise continue with the old parameters.
Implement this fitting procedure in Python and find the parameters
Task 5. Modules
Put your functions mqdis and psp into one module, and add a third function which contains your code for simulating the random walk. Put all the plotting stuff of Tasks #1 to #3 into a test code section which is only executed when the module is executed as a script.
Modify your code for Task #4 such that it uses the functions imported from your new module.
Task 6. Towards classes
Construct an upper-class which you can use to study random walks, and populate this class with internal variables and methods. Here are some ideas how you might do it:
- The __init__-method sets the parameter
for a specific random walk. - Method sim(n) simulates
(additional) particles and stores (adds) their durations (to the existing data). - Method avg() returns the average drift time.
- Method fit(f, a_init, b_init) performs a fit of an arbitrary function f depending on two parameters to the drift time statistics collected so far.
The source code is Open Source and can be found on GitHub.