Conditionals#

What do we need to know to detect spikes using python?#

  • Present data in code (individual voltage values, manipulate them and store the results) - variables

  • Compare variables (voltage to threshold) - boolean operations

  • Perform different actions based on the value of a variable (only keep the position if the voltage exceeds the threshold) - if-else statements

  • Present and access data in a time series of voltage values - lists

  • Perform an action for each element in a sequence of values (inspect voltage values one-by-one) - for loops

  • Separate data and logic so we can use the same code for new recordings - functions

  • Apply this to multi data files

  • Plot and save the results

Comparing variables#

Until now, we know how to add, subtract, multiply, and divide variables.

However, to solve our spike detection problem, we need to compare values: We need to dermine whether the voltage exceeds the threshold!

This is done using comparison operators:

  • > greater than

  • < less than

  • == equal (not to be confused with = which assigns values to variables)

  • >= greater or equal

  • <= less or equal

voltage = -40
threshold = 10

print(voltage < threshold, voltage > threshold, voltage == threshold)
True False False

The result of a comparison is not a number, but True or False.

We call the resulting variables boolean variables after George Boole.

Clicker question “comparisons”(Click me!)

Let’s define two variables: a = 10; b = 20. Which of these statements evaluate to True?

a == 10
a > b
b <= 20
b < 20
b = 3

Combining comparisons#

We can combine different comparisons with and or or, like so: a > 10 and a < 20.

  • and returns True only if both comparisons are True

  • or returns True if at least one of the two comparisons is True.

Perform different actions based on the value of a variable#

Yay - we can now figure out whether our voltage exceeds the threshold!

But that alone is pretty useless - we want to perform specific computations based on the outcome of this comparison, for instance, if the voltage value exceeds the threshold, then we want to print it’s time point.

This can be done using so-called “if-else” statements. The basic logic of these statements is this: if EXPRESSION is True, then we do one thing, else we do something else:

first_name = 'Anton'

if first_name == 'Anton':
    print('The first name is "Anton".')
else:
    print('The first name is NOT "Anton".')
The first name is "Anton".

A couple of new things:

  • if and else keywords. if is followed by the expression whos True/False value determines what is done.

  • : - marks the end of a special statement like if or else.

  • Indentation marks code blocks that are executed after each statement, based on the True/False value of the expression after if.

What’s going on?#

  1. We create a variable with name first_name and value Anton.

  2. Python checks whether the value of the variable first_name equals (==) Anton (not to be confused with the = used for assigning values to variables).

  3. It does evalute to True.

print(first_name)
print(first_name == 'Anton')
Anton
True
  1. Since the condition for the if statement is True, the indented code block after if is executed. If the condition had evaluated to False the indented code block after else would have been executed.

How about this?

first_name = 'Benton'
if first_name == 'Anton':
    print('First name starts with "A".')
else:
    print('No!')
No!
  1. We create a variable with name first_name and value Benton.

  2. Python checks whether the value of the variable first_name equals (==) Anton

  3. It does not - it evaluates to False:

print(first_name)
print(first_name == 'Anton')
Benton
False
  1. Since the condition for the if statement is False, the code block after if is skipped.

  2. Python moves to the else block and executes the indented code block.

Multiple conditions can be checked by using elif statements (short for “else if”). Note, that the order of comparisons matters - python will only execute the first matching statement and skip the rest.

name = 'Benton'
if name == 'Anton':
    print('Name starts with "A".')
elif name == 'Benton':
    print('Name starts with "B".')
elif name == 'Canton':
    print('Name starts with "C".')
else:
    print('No!')
Name starts with "B".

Lastly, the else statement is optional. You can use if without the else:

today = "Monday"
if today == "Monday":
    print("I hate Mondays!")
I hate Mondays!

Clicker question “if statements”(Click me!)

What will be printed?

size = 10

if size > 20:
	print('too big!')
elif size < 20:
	print('not too big!)
elif size == 10:
	print('perfect')
else:
	print('no result')

What do we need to know to detect spikes using python?#

  • Present data in code (individual voltage values, manipulate them and store the results) - variables

  • Compare variables (voltage to threshold) - boolean values

  • Perform different actions based on the value of a variable (only keep the position if the voltage exceeds the threshold) - if-else statements

  • Present and access data in a time series of voltage values - lists

  • Perform an action for each element in a sequence of values (inspect voltage values one-by-one) - for loops

  • Separate data and logic so we can use the same code for new recordings - functions

  • Apply this to multi data files

  • Plot and save the results

The core computation of the spike detection problem is solved!! (You will put the individual steps together during the exercise.)

Now we need to learn how to process the whole time series of voltage values! We could manually read the numbers from the text file and enter different values of the voltage variable but this would be tedious.

So far all variables held single values - numbers or strings. How do we present sequences of numbers, like our time series of voltage values? And how do we work through a sequence and apply the spike detection to each individual value in the sequence?