mpags as1 an introduction to scientific computing with
play

MPAGS AS1 An introduction to scientific computing with - PowerPoint PPT Presentation

MPAGS AS1 An introduction to scientific computing with http://stevenbamford.com/python_mpags_2018/ Steven Bamford Course prerequisites To make the most of this course, you should have: Some programming experience (in any language)


  1. MPAGS AS1 An introduction to scientific computing with http://stevenbamford.com/python_mpags_2018/ Steven Bamford

  2. Course prerequisites • To make the most of this course, you should have: • Some programming experience (in any language) • Access to a computer with Python installed • Preferably a laptop so you can try things out during the sessions • You will need (at least) numpy, matplotlib and scipy modules installed • See your sys. admin. and the course webpage for installation help • Anaconda recommended • Ideally you should also have: • Some current or upcoming need of a scripting language • A piece of real or toy analysis on which you can try out using Python

  3. Course aims • To give you… • experience of using a modern scripting language (specifically Python) • practical advice about scientific programming • knowledge of the main scientific modules for Python • the ability to do basic data analysis tasks in Python (e.g. data manipulation, plotting, …) • knowledge of some specific tools for scientific computing (e.g. signal processing, optimisation, …) • an overview of Python's full capabilities • Not to… • teach programming in general • cover every aspect of Python

  4. Course structure • Five sessions of ≤ 2 hours each: • Thursday 3:00 – 5:00 pm • ~1.5 hour lecture • Optionally some time working on exercises with support • Lecture notes will go online before each session • Caveats, pros and cons • Questions: • During lectures/exercises • communication with remote locations tricky • tweet me @thebamf • Email to arrange a meeting (in my office or over skype) • steven.bamford@nottingham.ac.uk • Formative assessment by coursework • Development of a working Python program related to your studies

  5. Coursework • A Python program relevant to your research • put course material into practice • opportunity to become familiar with Python • get feedback on your coding • requirement to qualify for credits • Your program should… • be written as an importable module (.py file) or Jupyter notebook (.ipynb) • do something meaningful: analyse real data or perform a simulation • use at least two user functions • use at least one of the modules introduced in Sessions 3 – 5 • produce at least one informative plot • comprise >~ 50 lines of actual code (excluding comments and imports)

  6. Coursework • Submit as source code (.py/.ipynb file) and pdf/png files of the output plot(s) • Email me (steven.bamford@nottingham.ac.uk) with subject “MPAGS coursework” • Link to a GitHub repository, etc. or attach code • if private add me (bamford) as a collaborator • make sure any requirements are clear • Three stages: • One (optional) • README describing what you intend your code to do • Rough outline of the code (classes, functions, snippets, comments, pseudocode) • If submitted by 9 Nov , I will provide feedback • Two (optional) • Roughly working version of your code, although may be incomplete, have bugs, although try to make it reasonable and easy to understand! • If submitted by 23 Nov , I will provide feedback • Three (for MPAGS credits) • Full (ideally) working version of your code (doesn’t have to be amazing!) • 14 Dec deadline

  7. Outline • Session1 : Introduction to Python • Why Python is (mostly) awesome • Introduction to the language: • start-up, syntax, constructs, functions, classes, getting help • Python 2.x versus 3.x • Good programming practice versus 'thinking aloud' • Session 2 : Numerical Python and plotting • Numpy • Using arrays wisely • Plotting: • matplotlib and others • IPython notebooks • Session 3 : Scientific Python • Scipy • optimisation, interpolation, statistics, filtering, integration, … • Other tools • scikit, astroML, GNU Scientific Library, R, Theano, …

  8. Outline • Session 4 : Python for specialists • Python for observers • Astropy • Handling FITS files • PyRAF – scripting IRAF with Python • Python for theorists • Symbolic algebra (sympy and Sage) • Bayesian inference and Deep Learning in Python • MCMC with emcee • ANNs with keras • Session 5 : Extreme Python • Efficiently storing and processing large amounts of data • PyTables, Pandas • Multiprocessing • Timing and testing • Wrapping external libraries and creating the fastest code • Cython • Numba and NumbaPro • Web applications • Django and others

  9. An introduction to scientific programming with Session 1 : Introduction to Python

  10. Why use a scripting language? • Modern scripting languages: • Python, IDL, R, Perl, Ruby, … • High-level • Interactive interpreter • Ease of use • Speed of development • Encourages scripting, rather than one-off analysis • Permanent record • Repeatability

  11. Why not? • If you want fastest possible performance • at the expense of everything else • You need highly parallel code • Need low-level control • Unless you are working on a supercomputer or developing operating systems components, these probably don't apply to you • Even then, Python could be useful in places (glue, tests, etc.)

  12. Why Python is awesome • Designed to be easy to learn and use – clear syntax • Well documented • Powerful, flexible, fully-featured programming language • Multi-paradigm • 'Batteries included' • Comprehensive scientific tools • Fast, efficient • Interpreter, introspection • Runs everywhere, completely free • You probably already have it

  13. Why learn Python? • Less stress • Get more science done • Widely used and growing popularity • Throughout academia and industry • NASA, AstraZeneca, Google, Industrial Light & Magic, Philips,… • Web services, engineering, science, air traffic control, quantitative finance, games, education, data management, … • Python programmers in demand • Easy introduction to general programming concepts Why not? • Existing code for your project in another language, but still…

  14. Starting the interpreter • IPython (interactive Python) / $ ipython qtconsole • $ ipython • Spyder • integrated development environment

  15. Jupyter (IPython) notebooks • Mathematica/Maple-style notebooks • Store code and output together in one file • Blend interactive prompt and scripts • Good for demonstrations / trying things out • Keep reproducable record of interactive analyses • To start, in terminal: jupyter notebook • Opens notebook interface in web browser • If you put them online (especially on GitHub) can easily display with nbviewer.ipython.org jupyter nbconvert can be used to convert to python/html/slides, etc. •

  16. Basics >>> 2+2 4 >>> # This is a comment ... 2+2 4 >>> 2+2.0 # and a comment on the same line as code 4.0 >>> (50-5*6)/4 5 >>> width = 20 # assignment, no type declaration >>> height = 5*9 >>> width * height 900 >>> x = y = z = 0 # zero x, y and z >>> y 0 >>> n Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined

  17. Scripts 2+2 # This is a comment 2+2 2+2.0 # and a comment on the same line as code (50-5*6)/4 width = 20 # assignment, no type declaration height = 5*9 width * height x = y = z = 0 # zero x, y and z print(y) • Write code in a text editor (ideally one Python aware!) • Save in a file and execute… from command line: $ python test.py from the IPython prompt: In [1]: %run test.py • Save and use interactively in future sessions ( >>> import test ) • Create executable files ( $ ./test.py )

  18. Strings >>> 'spam and eggs' 'spam and eggs' >>> 'doesn\'t' "doesn't" >>> "doesn't" "doesn't" >>> '"Yes," he said.' '"Yes," he said.' >>> hello = 'Greetings!' >>> hello 'Greetings!' >>> print(hello) Greetings! >>> print(hello + ' How do you do?') Greetings! How do you do? >>> print(hello, 'How do you do?') ('Greetings!', 'How do you do?') OR Greetings! How do you do? >>> howdo = 'How do you do?' >>> print(hello+' '+howdo) Greetings! How do you do?

  19. Numbers >>> 10 + 3 >>> 10.0 + 3.0 13 13.0 >>> 10 - 3 >>> 10.0 - 3.0 7 7.0 >>> 10 * 3 >>> 10.0 * 3 30 30.0 >>> 10 / 3 >>> 10.0 / 3 3 OR 3.3333333333333335 3.3333333333333335 >>> 10 // 3 >>> 10.0 // 3 3 3.0 >>> 10 % 3 >>> 10.0 % 3.0 1 1.0 >>> 10**3 >>> 10.0**3 1000 1000.0 >>> 10 + 3 * 5 # *,/ then +,- 25 >>> 4.2 + 3.14 >>> (10 + 3) * 5 7.3399999999999999 65 >>> 4.2 * 3.14 >>> -1**2 # Note: -(1**2) 13.188000000000001 -1

  20. Numbers Augmented assignment: >>> a = 20 >>> a += 8 >>> a Functions: 28 >>> abs(-5.2) >>> a /= 8.0 5.2 >>> a >>> sqrt(25) 3.5 5.0 Comparisons: >>> 5 * 2 == 4 + 6 True >>> 0.12 * 2 == 0.1 + 0.14 False >>> a = 0.12 * 2; b = 0.1 + 0.14 >>> eps = 0.0001 >>> a - eps < b < a + eps True

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend