Skip to content

Commit 2b3412f

Browse files
committed
Removing examples of string interpolation. Fixing link warnings. Adding Python syntax primer
1 parent c554bf0 commit 2b3412f

5 files changed

Lines changed: 223 additions & 11 deletions

File tree

episodes/01-opening-and-checking-an-image.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ for i in range(nchannels):
189189
# because range() counts from 0 but subplot() assumes you're counting from 1.
190190
plt.subplot(1, nchannels, i+1)
191191
plt.imshow(image[:, :, i])
192-
plt.title('Channel %s' % i) # add a plot title
192+
plt.title('Channel ' + str(i)) # add a plot title
193193
plt.axis(False) # we just want to show the image, so turn off the axis labels
194194

195195
plt.show()
@@ -227,7 +227,7 @@ Starting with displaying a single histogram for one channel:
227227
channel_idx = 0
228228
channel = image[:, :, channel_idx]
229229
plt.hist(channel.flatten(), bins=255)
230-
plt.title('Channel %s' % channel_idx)
230+
plt.title('Channel ' + str(channel_idx))
231231
plt.show()
232232
```
233233

@@ -243,7 +243,7 @@ for i in range(nchannels):
243243

244244
plt.subplot(1, nchannels, i+1)
245245
plt.hist(channel.flatten(), bins=255)
246-
plt.title('Channel %s' % i) # add a plot title
246+
plt.title('Channel ' + str(i)) # add a plot title
247247

248248
plt.show()
249249
```

episodes/02-applying-filters.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ from skimage.morphology import square
4141
kernel = square(3) # create a 3x3 square kernel
4242
```
4343

44-
There are other shapes of kernel that can be used, and are documented
45-
[here](https://scikit-image.org/docs/stable/api/skimage.morphology.html).
44+
There are other shapes of kernel that can be used, and are documented in the
45+
[morphology section](https://scikit-image.org/docs/stable/api/skimage.morphology.html)
46+
of the skimage docs.
4647

4748
Note that as of skimage 0.25.0, the `square` function has been deprecated in
4849
favour of a new function, [footprint_rectangle](https://scikit-image.org/docs/stable/api/skimage.morphology.html#skimage.morphology.footprint_rectangle):
@@ -120,8 +121,9 @@ images, this may be difficult especially if the two are not entirely distinct fr
120121
other, or if the background is not a uniform shade. In cases like this, a rolling ball
121122
algorithm can be applied. The rolling ball estimates the background intensity of an
122123
image by using the pixel values to translate the image into a height map, and then
123-
rolling a ball of a given radius across it. Additional information on how it works
124-
can be found [here](https://www.researchgate.net/figure/Schematic-diagram-of-background-subtraction-by-the-rolling-ball-method-the-histogram_fig3_319985119).
124+
rolling a ball of a given radius across it. The rolling ball algorithm has been
125+
published and a [figure](https://www.researchgate.net/figure/Schematic-diagram-of-background-subtraction-by-the-rolling-ball-method-the-histogram_fig3_319985119)
126+
from the publication describes how it works.
125127

126128
::::::::::::::::::::::::::::::::::::: challenge
127129
## Exercise 7: Rolling ball background intensity

episodes/04-measurements.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ for i in labels[:6]: # just showing the first 6
6868
plt.subplot(2, 3, i + 1) # subplot counts from 1 rather than 0
6969
plt.imshow(nucleus)
7070
pixels = [p for p in nucleus.flatten() if p] # remove the background pixels
71-
print('Feature %i:' % i, numpy.mean(pixels))
71+
print('Feature ' + str(i) + ':', numpy.mean(pixels))
7272
```
7373

7474
![](fig/4_1_mean_intensities.png){alt='Mean intensities'}

instructors/instructor-notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Instructor Notes
33
---
44

5-
# Building the course
5+
## Building the course
66

77
Set up R:
88

learners/reference.md

Lines changed: 212 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,216 @@
22
title: Reference
33
---
44

5-
## Glossary
5+
## Python syntax primer
66

7-
This is a placeholder file. Please add content here.
7+
As this is a course on image analysis, some prior knowledge of Python syntax is assumed.
8+
This reference provides a summary of the language constructs used in the examples and
9+
exercises of this course.
10+
11+
### Variables
12+
13+
This is how you can store data to retrieve later:
14+
15+
```python
16+
# assigning the integer 1 to the variable `a`, and the decimal 0.4 to
17+
# the variable `b`
18+
a = 1
19+
b = 0.4
20+
21+
# we can perform arithmetic on numbers
22+
c = a + 4
23+
24+
# even reassign a variable based on its previous value
25+
b = b * 2
26+
27+
# strings/text can be specified with single quotes or double quotes
28+
some_string = 'a b c d e'
29+
30+
# we can still use the `+` operator on strings, except instead this
31+
# will concatenate them together
32+
some_other_string = some_string + 'f g h i'
33+
34+
# Python also has the boolean values True and False
35+
one_is_greater_than_zero = True
36+
one_is_an_even_number = False
37+
```
38+
39+
### Functions
40+
41+
These may be built into base Python, the standard library, extra installed packages, or ones that you've made
42+
yourself. A function is always run with brackets.
43+
44+
```python
45+
# show something on the console
46+
x = 2
47+
print(x)
48+
49+
# you don't have to create a variable first - you can just print a value directly
50+
print('abc')
51+
print(2)
52+
53+
# multiple arguments are separated with commas
54+
print(1, 2, 3)
55+
56+
# some functions are written to take named arguments - check the function's documentation
57+
# to see what arguments you can give it
58+
print(1, 2, 3, sep='-')
59+
60+
# we still need brackets to run the function, even if they're empty
61+
credits()
62+
63+
# the number 1 and the string '1' are not the same - Python has function for converting
64+
# or 'casting' values to certain data types
65+
string_1 = str(1) # str -> cast to string
66+
integer_4 = int(4.1) # int -> cast to integer
67+
float_3_point_0 = float(3) # float -> cast to decimal
68+
```
69+
70+
### Importing packages
71+
72+
Python comes with a lot of extra functionality that it not loaded by default. Some of these may be pre-installed (i.e. they are
73+
part of the Python [standard library](https://docs.python.org/3/library/index.html)), or they may need to be installed with `pip`.
74+
75+
```python
76+
# load the entire package - access its functions with dot notation
77+
import skimage
78+
image = skimage.io.imread('some_image.tif')
79+
80+
# some components inside packages are not functions, but regular
81+
# strings, numbers, etc. - we still access them with dot notation but
82+
# do not need `()` brackets to use them
83+
import sys
84+
print(sys.executable)
85+
86+
# import just the bits we need
87+
from skimage.io import imread
88+
image = imread('some_image.tif')
89+
90+
# rename the package as you're importing it
91+
import matplotlib.pyplot as plt
92+
plt.imshow(image)
93+
```
94+
95+
### Lists
96+
97+
Python can use these to store many pieces of information together:
98+
99+
```python
100+
# use square brackets to define a list, and separate the items with commas
101+
some_list = ['a', 'b', 'c', 'd', 'e']
102+
103+
# access individual items with square brackets - note that Python counts from 0
104+
print(some_list[0])
105+
print(some_list[3])
106+
107+
# access a subset (slice) of a list with square brackets and colons - this will
108+
# start at position 1 and go up to, but not including, position 3
109+
print(some_list[1:3])
110+
111+
# negative numbers will count from the end of the list rather than the start
112+
print(some_list[2:-1])
113+
114+
# leaving out the first number is equivalent to starting at position 0 (i.e. the
115+
# start), and leaving out the second number is equivalent to ending at position -1
116+
# (i.e. the end)
117+
print(some_list[:3])
118+
print(some_list[1:])
119+
120+
# leaving out both numbers will just slice the whole list
121+
print(some_list[:])
122+
123+
# slicing and indexing also works with strings
124+
some_string = 'abcdefgh'
125+
print(some_string[2:5])
126+
127+
# images are designed to be indexed in multiple dimensions at once - do this
128+
# by specifying multiple indexes/slices, separated by commas. Here we load an
129+
# image and access all of dimension 1, positions 100-200 of dimension 2, and
130+
# just position 2 of dimension 3
131+
from skimage.io import imread
132+
image = imread('some_image.tif')
133+
print(image[:, 100:200, 2])
134+
```
135+
136+
### Dictionaries
137+
138+
These store many named pieces of information together as key-value pairs:
139+
140+
```python
141+
# use curly brackets to define a dictionary. Separate the key from the value
142+
# with a colon, and separate key-value pairs from each other with commas
143+
chromosome_counts = {'human': 46, 'wheat': 42, 'fruit fly': 8, 'zebrafish': 25}
144+
145+
# access individual values with square brackets
146+
print(chromosome_counts['fruit fly'])
147+
```
148+
149+
### Loops
150+
151+
This is how we can perform some actions on many pieces of information, one at a time:
152+
153+
```python
154+
# first we need something to loop over
155+
colours = ['orange', 'red', 'blue', 'green', 'yellow', 'purple']
156+
157+
# go through each item the `colours` list, one at a time. The item
158+
# that we're currently processing will be called `colour`
159+
for colour in colours:
160+
# the code inside the loop is indented with spaces or tabs - it doesn't
161+
# matter which one or how many, as long as you're consistent
162+
print('doing something with: ' + colour)
163+
164+
# we're no longer indented, so we're now outside the loop
165+
print('done')
166+
167+
# this will do the same thing as the loop above, but with much more
168+
# typing and repetition
169+
print('doing something with: ' + 'orange')
170+
print('doing something with: ' + 'red')
171+
print('doing something with: ' + 'blue')
172+
print('doing something with: ' + 'green')
173+
print('doing something with: ' + 'yellow')
174+
print('doing something with: ' + 'purple')
175+
```
176+
177+
### Conditional statements
178+
179+
Python can execute blocks of code only when certain conditions are met:
180+
181+
```python
182+
# use the keyword `if`, followed by a boolean expression. Common mathematical
183+
# operators can be used in Python, including ==, !=, >, <, >=, <=
184+
if 1 < 2:
185+
# 1 is less than 2, so this indented code will run
186+
print('1 is less than 2')
187+
188+
if 1 + 2 == 5:
189+
# 1 + 2 is not 5, so this will not run
190+
print('1 + 2 is 5')
191+
192+
# we can use `else` to provide some code to fall back on if the condition
193+
# is not met
194+
if 1 + 2 == 5:
195+
print('1 + 2 is 5')
196+
else:
197+
print('1 + 2 is not 5')
198+
199+
# use `elif` to specify multiple possible courses of action - only the first one to
200+
# be satisfied will be executed
201+
202+
# let's check how large each number in a list is
203+
values = [5, 0, 2, 150, -1]
204+
205+
for val in values:
206+
if val > 100:
207+
print(str(val) + ' is greater than 100')
208+
209+
elif val > 4:
210+
print(str(val) + 'is greater than 4')
211+
212+
elif val >= 0:
213+
print(str(val) + ' is greater than or equal to 0')
214+
215+
else:
216+
print(str(val) + ' is less than 0')
217+
```

0 commit comments

Comments
 (0)