PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

Similar documents
ARTIFICIAL INTELLIGENCE AND PYTHON

Webgurukul Programming Language Course

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Introduction to Python Part 1. Brian Gregor Research Computing Services Information Services & Technology

Python Training. Complete Practical & Real-time Trainings. A Unit of SequelGate Innovative Technologies Pvt. Ltd.

Beyond Blocks: Python Session #1

Introduction to Python Part 2

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala

PYTHON NUMPY TUTORIAL CIS 581

ENGR 102 Engineering Lab I - Computation

Course Introduction and Python Basics

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Introduction to Python

Advanced Algorithms and Computational Models (module A)

Introduction to Python

And Parallelism. Parallelism in Prolog. OR Parallelism

Pace University. Fundamental Concepts of CS121 1

APT Session 2: Python

Session 1: Introduction to Python from the Matlab perspective. October 9th, 2017 Sandra Diaz

Table of Contents EVALUATION COPY

Introduction to Machine Learning. Useful tools: Python, NumPy, scikit-learn


Babu Madhav Institute of Information Technology, UTU 2015

CS Advanced Unix Tools & Scripting

Ch.1 Introduction. Why Machine Learning (ML)? manual designing of rules requires knowing how humans do it.

Large-Scale Networks

Basic Python Revision Notes With help from Nitish Mittal

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Scientific Python. 1 of 10 23/11/ :00

Webinar Series. Introduction To Python For Data Analysis March 19, With Interactive Brokers

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Scientific Computing: Lecture 1

Here n is a variable name. The value of that variable is 176.

Numerical Methods. Centre for Mathematical Sciences Lund University. Spring 2015

Python. Department of Computer Science And Engineering. European University Cyprus

DSC 201: Data Analysis & Visualization

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Introduction to Python. Dmytro Karpenko Research Infrastructure Services Group, Department for Research Computing, USIT, UiO

Introduction to Python (All the Basic Stuff)

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

STA141C: Big Data & High Performance Statistical Computing

Lecture 0. Introduction to Python. Randall Romero Aguilar, PhD II Semestre 2018 Last updated: July 12, 2018

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

Introduction to Python Part 1

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

C++ for Python Programmers

Python Workshop. January 18, Chaitanya Talnikar. Saket Choudhary

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Introduction to Computer Vision Laboratories

Lecture 1: Introduction to Python

Key Differences Between Python and Java

Python Programming, bridging course 2011

CIS192 Python Programming. Robert Rand. August 27, 2015

Part IV. More on Python. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

STATS 507 Data Analysis in Python. Lecture 0: Introduction and Administrivia

Introduction to programming with Python

Table of Contents. Preface... xxi

The Python interpreter

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Senthil Kumaran S

Python Programming. Hans-Petter Halvorsen.

Accelerating Information Technology Innovation

Python for Scientists

FIT9133 Module 1: Introduction to Algorithms and Python Programming Basics

Data type built into Python. Dictionaries are sometimes found in other languages as associative memories or associative arrays.

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

Shell / Python Tutorial. CS279 Autumn 2017 Rishi Bedi

What Version Number to Install

Introduction to Python. Didzis Gosko

Python for Earth Scientists

DSC 201: Data Analysis & Visualization

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Python Basics. Lecture and Lab 5 Day Course. Python Basics

Computer Components. Software{ User Programs. Operating System. Hardware

Certified Data Science with Python Professional VS-1442

CIS192: Python Programming

Accelerating Information Technology Innovation

tutorial : modeling synaptic plasticity

Hacettepe University Computer Engineering Department. Programming in. BBM103 Introduction to Programming Lab 1 Week 4. Fall 2018

MLCV 182: Practical session 1 Ron Shapira Weber Computer Science, Ben-Gurion University

python 01 September 16, 2016

Visualize ComplexCities

A. Python Crash Course

Introduction to Mathematical and Scientific Programming TUTORIAL WEEK 1 (MATH 1MP3) Winter 2019

Intelligente Datenanalyse Intelligent Data Analysis

Episode 1 Using the Interpreter

DaMPL. Language Reference Manual. Henrique Grando

Introduction to Programming Using Java (98-388)

LECTURE 1. Getting Started with Python

Collections. Lists, Tuples, Sets, Dictionaries

Introduction to Python

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Introduction to Python, Cplex and Gurobi

CS150 Sample Final Solution

CS150 Sample Final. Name: Section: A / B

STSCI Python Introduction. Class URL

Transcription:

PYTHON FOR MEDICAL PHYSICISTS Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

TUTORIAL 1: INTRODUCTION Thursday 1 st 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 conda or pip

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 ipython environment, 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 Execute current code Editor IPython console

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 points if pixel value > 1, add 1 to number of failed points if number of failed points / number of total points < 0.9 print Plan Failed otherwise print 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 Sign Operator = assignment + addition - subtraction & negation * multiplication / division // floor division % modulo (i.e. remainder) ** exponentiation 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

BOOLEAN OPERATORS Sign Operator == equality!= inequality <, <= less than (or equal to) >, >= greater than (or equal to) and or is not ^ logical and logical or identity 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 append(x) extend(l) insert(i,x) remove(x) pop() clear() index(x) sort() reverse() copy() Description add x to list add list L to list add x at position I remove (first) x remove, return last item remove all items get index of (first) x sort the list reverse the list 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: The while statement is used to iterate conditionally Example: def fib(n): a, b = 0, 1 while a < n: print(a, end= ) a, b = b, a+b print() for index in range(5): print(arr[index])

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-line comment 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) # 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() # continued

NEXT TUTORIAL: USING PACKAGES, JUPYTER NOTEBOOKS, AND CODING EXAMPLES