Python for medical physicistssbcrowe.net/wp-content/uploads/2015/09/python_tutorial_1.pdf · PYTHON...
Transcript of Python for medical physicistssbcrowe.net/wp-content/uploads/2015/09/python_tutorial_1.pdf · PYTHON...
PYTHON FORMEDICAL PHYSICISTS
Radiation Oncology Medical Physics
Cancer Care Services, Royal Brisbane & Women’s Hospital
TUTORIAL 1:INTRODUCTION
Thursday 1st October, 2015
AGENDA
1. Reference list
2. Introduction to Python
3. Getting started with Python
4. Programming basics
5. Python syntax
6. Data structures
7. Flow control
8. Comments
REFERENCES
Online resources:
The Python Tutorial (https://docs.python.org/3.5/tutorial/index.html)
Codecademy (https://www.codecademy.com/en/tracks/python)
Textbooks (available on N: drive):
Python for Scientists: A Curated Collection of Chapters from the O’Reilly Data and Programming Library, by O’Reilly(http://www.oreilly.com/programming/free/python-for-scientists.csp)
A Primer on Scientific Programming with Python, by Langtangen
ABOUT PYTHON
Python is an interpreted language – the Python interpreter executes statements entered by the user or parsed from a script
Python is a popular choice for scientists, with many packages developed for numerical analysis and scientific computation
INSTALLING PYTHON
Recommend installing the free Anaconda distribution, including science, math, and analysis packages available from continuum.io
available for Windows, Mac and Linux operating systems
There are currently 2 active branches Python 2.X – legacy
Python 3.X – recommended!
INSTALLING ANACONDA
PACKAGES
Packages extend functionality
Anaconda comes with 195 packages that you may find useful, including: Spyder, a development environment
numpy, for processing numerical data
scipy, containing scientific functions
matplotlib, for data visualisation
There are useful packages not included in Anaconda, including: pydicom, for reading, modifying and writing DICOM data
(pydicom.org)
These can be installed using pips ($\>pips install pydicom)
RUNNING PYTHON
The Python interpreter can be run from the command line or terminal
On a Windows PC, you can run “Anaconda Command Prompt” to start a session with the appropriate environmental paths
Running python this way starts the interpreter in interactive mode, such that commands are prompted for via the terminal
PYTHON SCRIPTS
Instructions can be passed to the Python interpreter interactively, via the terminal
In this way the Python interpreter can be used like a calculator, or like Matlab
Instructions for the Python interpreter can also be provided in a script
Tools exist for just-in-time compilation (done at run time) to help performance (PyPy, Cython)
RUNNING PYTHON
Integrated development environments (IDEs) often provide an interactive terminal, in addition to an editor suited to writing scripts
Common features include: navigation tools
syntax highlighting
Spyder comes packaged with Anaconda
It uses the iPythonenvironment, which comes with tab completion
Alternatives: PyCharm – IDE featuring
code analysis, refactoring and testing tools (developed by JetBrains)
Python Tools for Visual Studio – supporting Python in VS2015
SPYDER
Editor
IPythonconsole
Execute current code
BASICS OF PROGRAMMING
Programming styles can be referred to as ‘paradigms’
We’ll be discussing structured programming, which features the following flows of control: sequential execution of statements
conditional execution of statements (if, else, elif)
iterative execution of statements (for, while)
In a later tutorial, we will discuss object-oriented programming
PSEUDO CODE
Pseudocode is description of an algorithm or computer programs that is designed for human reading
Pseudocode is especially useful if you aren’t familiar with the syntax of a programming language, as you can translate it later
Facilitates top-down design: breaking problem down into smaller problems, that can be individually solved
Example of pseudocode:
for every pixel in a gamma evaluation image,add 1 to number of total pointsif pixel value > 1, add 1 to number of failed points
if number of failed points / number of total points < 0.9print “Plan Failed”
otherwiseprint “Plan Passed”
PYTHON SYNTAX
The syntax of a programming language is the set of rules that defines how the code should be structured
Examples of syntactical rules include quotation marks should be used when typing a string
parentheses should be paired
keywords should not be used as identifiers
A couple of notes on Python: Python uses whitespace to structure code, unlike the C family, Java,
etc. which use curly braces: {,}
Variables in Python are dynamically typed(Python: num = 3; C: int num = 3)
DATA TYPES
Type Description Syntax
int integer 1
float floating point number 1.0
complex complex number 2.0+1.0j
str character string “string”
bytes immutable sequence of bytes bytes([1,10,100])
bytearray mutable sequence of bytes bytearray([1,10,100])
list mutable list of objects [1.0, 2.0, 3.0]
tuple immutable list of objects (1.0, 2.0, 3.0)
set mutable list of unique objects {1.0, 2.0, 3.0}
frozen set immutable list of unique objects frozenset([1.0, 2.0, 3.0])
dict dictionary of key and value pairs {1: “first”, 2: “second”}
OPERATORS
Augmented assignment is possible: num = 2
num += 3
num == 5 returns true
Division is typed 5 / 2 = 2
5.0 / 2.0 = 2.5
5.0 // 2.0 = 2
Sign Operator
= assignment
+ addition
- subtraction & negation
* multiplication
/ division
// floor division
% modulo (i.e. remainder)
** exponentiation
BOOLEAN OPERATORS
Sign Operator
== equality
!= inequality
<, <= less than (or equal to)
>, >= greater than (or equal to)
and logical and
or logical or
is identity
not boolean negation
^ logical xor for booleans(bitwise operator for ints)
Equality is not identity: [1,2,3] == [1,2,3] returns true
[1,2,3] is [1,2,3] returns false
METHODS
Methods are functions attached to objects
Methods encapsulate a block of statements that are executed when the method is called Methods can accept arguments (objects passed to the method)
Methods can return an object
In Python, the syntax for calling a method is:result = method(argument1, argument2)
In Python, the syntax for calling a method is:def method(argument):
...return result
WHY METHODS?
Abstracting (encapsulating) code that is repeated in your application into distinct methods comes with multiple benefits: reduced redundancy
increased readability
improved refactoring (it becomes simpler for the author to make changes to the code)
decreased interdependence between software modules
supports recursion
DATA STRUCTURES
Lists represent a sequence of objects
Lists can contain mixed types, e.g. ([1, 2.3, “a”])
Lists are mutable and Tuples are immutable (meaning that the contents cannot be changed without a new tuple being created)
Method Description
append(x) add x to list
extend(L) add list L to list
insert(i,x) add x at position I
remove(x) remove (first) x
pop() remove, return last item
clear() remove all items
index(x) get index of (first) x
sort() sort the list
reverse() reverse the list
copy() copy the list
DATA STRUCTURES
A set is similar to a list, but does not allow duplicate elements
Sets are unordered
A frozenset is an immutable version of set
A dictionary, also known as an associative array, is a list indexed by keys (as opposed to indices).
For example:
dct = {‘Ichi’: 1, ‘Ni’: 2}
dct[‘Ichi’] == 1# returns true
‘Ichi’ in dct# returns true
FLOW CONTROL
The if statement is used to conditionally execute a block of code
It can be combined with else and elif (else if)
Example:
if chi_reduced > 1:print “model does not capture the data”
elif chi_reduced == 1:print “model fits the data”
else:print “model over-fits the data”
FLOW CONTROL
The for statement is used to iterate over a sequence:
names = [‘alice’, ‘bob’]for name in names:
print(arr[index])
It can be combined with the range() function to allow iteration over a sequence of numbers:
for index in range(5):print(arr[index])
The while statement is used to iterate conditionally
Example:
def fib(n):a, b = 0, 1while a < n:print(a, end=‘ ‘)a, b = b, a+b
print()
DOCUMENTATION
Documentation is an important part of developing software – and one component is the annotation of code with in-line comments
Single line comments can be added using a hash character (#)
Mutli-line comments can be added using three quotation marks as a delimiter (""")
Example:
#this is a comment
num = 3 # assign 3 to num
""" This is a multi-linecomment that spans 2 lines"""
DOCUMENTATION
A docstring is a specifically formatted comment that is retained by the interpreter when encountered
These comments can be accessed using the “help” (or the __doc__ attribute for an object)
These are created within Python as the first statement following a definition of function, method, module or class
For example:
def my_method(self):"""The method's docstring"""
EXAMPLE: SEVEN29 .MCC FILES
# import functionality
import sys
from StringIO import StringIO
import numpy as np
# take list of files as an argument for the script
files = sys.argv[1:]
# iterate over files
for file in files:
f = open(file,'r')
o = StringIO()
# for every line in the file that contains a #
for lines in f:
if '#' in lines:
o.write(lines)
# continued …
# close the .mcc file
f.close()
o.seek(0)
# load lines in numpy array
seven = np.loadtxt(o)
# fill a 27x27 matrix with detector readings
out = np.zeros((27,27))
for i in range(0,27):
out[:,i] = seven[i*27:i*27+27,1]
# write to file
fname = file[:-3]+'txt‘
np.savetxt(fname, out, fmt='%.4f', delimiter='\t')
o.close()
NEXT TUTORIAL:USING PACKAGES, JUPYTER NOTEBOOKS,
AND CODING EXAMPLES