├── .gitattributes ├── .gitignore ├── LICENSE ├── README.rst ├── binder └── environment.yml ├── book ├── _config.yml ├── _toc.yml ├── faq.ipynb ├── help.ipynb ├── img │ ├── 10_errors_in_science.png │ ├── 10_tdd.png │ ├── 18_temp_pic.png │ ├── cmd.png │ ├── download_options.png │ ├── install_options.png │ ├── lauch_terminal.gif │ ├── launch_binder.png │ ├── logo.png │ ├── logo.xcf │ ├── logo_acinn_small.jpg │ ├── logo_uob_small.jpg │ ├── miniforge.png │ ├── numpy │ │ ├── image0013830.gif │ │ ├── image0020619.gif │ │ └── image004de9e.gif │ ├── open_editor.gif │ ├── term.png │ ├── unix-tree.png │ └── welcome_img.jpg ├── midterm.md ├── references.ipynb ├── syllabus.md ├── week_01 │ ├── 01-Installation.ipynb │ ├── 01a-Details-Windows.ipynb │ ├── 01b-Details-Linux.ipynb │ ├── 02-Terminal.ipynb │ ├── 02a-Windows.ipynb │ ├── 02b-Linux.ipynb │ ├── 03-Installation-uni.ipynb │ ├── 04-Getting-Started.ipynb │ └── Assignment-01.ipynb ├── week_02 │ ├── 01-install-packages.md │ ├── 02-ipython-spyder.ipynb │ ├── 03-Language-Fundamentals.ipynb │ ├── Assignment-02.ipynb │ └── my_python_script.py ├── week_03 │ ├── 01-Import-Scopes.ipynb │ ├── 02-Strings-Paths.ipynb │ ├── 03-Tips-and-Tricks.ipynb │ ├── 04-Good-practices.ipynb │ ├── Assignment-03.ipynb │ └── greetings.py ├── week_04 │ ├── 01-Why-Numpy.ipynb │ ├── 02-Numbers.ipynb │ └── Assignment-04.ipynb ├── week_05 │ ├── 01-Numpy-ndarrays.ipynb │ ├── 02-Scientific-Python.ipynb │ └── Assignment-05.ipynb ├── week_06 │ ├── 01-Exceptions.ipynb │ ├── 02-Testing.ipynb │ └── Assignment-06.ipynb ├── week_07 │ ├── 01-Package-structure.ipynb │ └── 02-ClimVis.ipynb ├── week_08 │ └── 01-Documentation.ipynb ├── week_09 │ ├── 01-OOP-Part-1.ipynb │ └── Assignment-07.ipynb ├── week_10 │ ├── 01-OOP-Part-2.ipynb │ ├── Assignment-08.ipynb │ └── ascii_art.py ├── week_11 │ └── 01-OOP-Part-3.ipynb └── welcome.ipynb ├── build.sh ├── data ├── innsbruck_temp.json ├── monthly_temp.npz └── rgi_intersects.json ├── linkcheck.sh └── push.sh /.gitattributes: -------------------------------------------------------------------------------- 1 | *.ipynb filter=nbstripout 2 | *.ipynb diff=ipynb 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Mine 2 | html/index.html 3 | .idea 4 | book/_build/ 5 | .DS_Store 6 | 7 | # Byte-compiled / optimized / DLL files 8 | __pycache__/ 9 | *.py[cod] 10 | *$py.class 11 | 12 | # C extensions 13 | *.so 14 | 15 | # Distribution / packaging 16 | .Python 17 | env/cd 18 | build/ 19 | develop-eggs/ 20 | dist/ 21 | downloads/ 22 | eggs/ 23 | .eggs/ 24 | lib/ 25 | lib64/ 26 | parts/ 27 | sdist/ 28 | var/ 29 | wheels/ 30 | *.egg-info/enodo 31 | .installed.cfg 32 | *.egg 33 | 34 | # PyInstaller 35 | # Usually these files are written by a python script from a template 36 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 37 | *.manifest 38 | *.spec 39 | 40 | # Installer logs 41 | pip-log.txt 42 | pip-delete-this-directory.txt 43 | 44 | # Unit test / coverage reports 45 | htmlcov/ 46 | .tox/ 47 | .coverage 48 | .coverage.* 49 | .cache 50 | nosetests.xml 51 | coverage.xml 52 | *.cover 53 | .hypothesis/ 54 | 55 | # Translations 56 | *.mo 57 | *.pot 58 | 59 | # Django stuff: 60 | *.log 61 | local_settings.py 62 | 63 | # Flask stuff: 64 | instance/ 65 | .webassets-cache 66 | 67 | # Scrapy stuff: 68 | .scrapy 69 | 70 | # Sphinx documentation 71 | docs/_build/ 72 | 73 | # PyBuilder 74 | target/ 75 | 76 | # Jupyter Notebook 77 | .ipynb_checkpoints 78 | 79 | # pyenv 80 | .python-version 81 | 82 | # celery beat schedule file 83 | celerybeat-schedule 84 | 85 | # SageMath parsed files 86 | *.sage.py 87 | 88 | # dotenv 89 | .env 90 | 91 | # virtualenv 92 | .venv 93 | venv/ 94 | ENV/ 95 | 96 | # Spyder project settings 97 | .spyderproject 98 | .spyproject 99 | 100 | # Rope project settings 101 | .ropeproject 102 | 103 | # mkdocs documentation 104 | /site 105 | 106 | # mypy 107 | .mypy_cache/ 108 | 109 | # Specific 110 | book/week_05/ERA-Int-MonthlyAvg-4D-T.nc 111 | .virtual_documents 112 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | .. -*- rst -*- -*- restructuredtext -*- 2 | .. This file should be written using restructured text conventions 3 | 4 | ====================== 5 | Scientific Programming 6 | ====================== 7 | 8 | Lectures notes of the scientific programming course at the University of Innsbruck. 9 | 10 | Website: http://fabienmaussion.info/scientific_programming 11 | 12 | License 13 | ------- 14 | 15 | .. image:: https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by.svg 16 | :target: https://creativecommons.org/licenses/by/4.0/ 17 | :alt: Creative Commons License 18 | 19 | These lecture notes and exercises are licensed under a `Creative Commons Attribution 4.0 International License `_. 20 | -------------------------------------------------------------------------------- /binder/environment.yml: -------------------------------------------------------------------------------- 1 | name: scipro 2 | channels: 3 | - conda-forge 4 | dependencies: 5 | - numpy 6 | - scipy 7 | - matplotlib 8 | -------------------------------------------------------------------------------- /book/_config.yml: -------------------------------------------------------------------------------- 1 | ####################################################################################### 2 | # A default configuration that will be loaded for all jupyter books 3 | # See the documentation for help and more options: 4 | # https://jupyterbook.org/customize/config.html 5 | 6 | ####################################################################################### 7 | # Book settings 8 | title: Scientific Programming # The title of the book. Will be placed in the left navbar. 9 | author: Fabien Maussion # The author of the book 10 | copyright: "2018-2024" # Copyright year to be placed in the footer 11 | logo: img/logo.png # A path to the book logo 12 | repository: 13 | url: https://github.com/fmaussion/scientific_programming 14 | path_to_book: book 15 | branch: master 16 | parse: 17 | myst_enable_extensions: 18 | # don't forget to list any other extensions you want enabled, 19 | # including those that are enabled by default! 20 | - dollarmath 21 | - linkify 22 | - substitution 23 | - colon_fence 24 | - html_image 25 | launch_buttons: 26 | notebook_interface: "jupyterlab" 27 | binderhub_url: "https://mybinder.org" 28 | html: 29 | analytics: 30 | google_analytics_id: G-5BGML8VW02 31 | plausible_analytics_domain: fabienmaussion.info 32 | use_repository_button: true 33 | use_issues_button: true 34 | use_edit_page_button: true 35 | toc_title: "On this page" 36 | extra_footer: | 37 |

38 | 39 | 40 | 41 | These lecture notes and exercises are licensed under a Creative Commons Attribution 4.0 International (CC BY 4.0) license. 42 |
43 | © Copyright 2018-2024. 44 |

45 | sphinx: 46 | config: 47 | html_show_copyright: false 48 | html_last_updated_fmt: '%b %d, %Y' 49 | html_js_files: 50 | - ['https://plausible.oggm.org/js/script.js', {'defer': 'defer', 'data-domain': 'fabienmaussion.info'}] 51 | extra_extensions: 52 | - sphinx_exercise 53 | execute: 54 | execute_notebooks: auto 55 | allow_errors: true 56 | only_build_toc_files: true 57 | -------------------------------------------------------------------------------- /book/_toc.yml: -------------------------------------------------------------------------------- 1 | # Table of contents 2 | # Learn more at https://jupyterbook.org/customize/toc.html 3 | 4 | format: jb-book 5 | root: welcome 6 | parts: 7 | - caption: Introduction 8 | chapters: 9 | - file: syllabus 10 | - file: faq 11 | - file: references 12 | - file: help 13 | - caption: Week 01 - Terminal & Python primer 14 | chapters: 15 | - file: week_01/01-Installation 16 | sections: 17 | - file: week_01/01a-Details-Windows 18 | - file: week_01/01b-Details-Linux 19 | - file: week_01/02-Terminal 20 | sections: 21 | - file: week_01/02a-Windows 22 | - file: week_01/02b-Linux 23 | - file: week_01/03-Installation-uni 24 | - file: week_01/04-Getting-Started 25 | - file: week_01/Assignment-01 26 | - caption: Week 02 - Python language fundamentals 27 | chapters: 28 | - file: week_02/01-install-packages 29 | - file: week_02/02-ipython-spyder 30 | - file: week_02/03-Language-Fundamentals 31 | - file: week_02/Assignment-02 32 | - caption: Week 03 - Variables scopes, modules, strings, good practices 33 | chapters: 34 | - file: week_03/01-Import-Scopes 35 | - file: week_03/02-Strings-Paths 36 | - file: week_03/03-Tips-and-Tricks 37 | - file: week_03/04-Good-practices 38 | - file: week_03/Assignment-03 39 | - caption: Week 04 - Floating point arithmetics, Numpy basics 40 | chapters: 41 | - file: week_04/01-Why-Numpy 42 | - file: week_04/02-Numbers 43 | - file: week_04/Assignment-04 44 | - caption: Week 05 - Scientific Python 45 | chapters: 46 | - file: week_05/01-Numpy-ndarrays 47 | - file: week_05/02-Scientific-Python 48 | - file: week_05/Assignment-05 49 | - caption: Midterm exam 50 | chapters: 51 | - file: midterm 52 | - caption: Week 06 - Testing 53 | chapters: 54 | - file: week_06/01-Exceptions 55 | - file: week_06/02-Testing 56 | - file: week_06/Assignment-06 57 | - caption: Week 07 - Python Packages and ClimVis project 58 | chapters: 59 | - file: week_07/01-Package-structure 60 | - file: week_07/02-ClimVis 61 | - caption: Week 08 - Code documentation 62 | chapters: 63 | - file: week_08/01-Documentation 64 | - caption: Week 09 - Object Oriented Programming I 65 | chapters: 66 | - file: week_09/01-OOP-Part-1 67 | - file: week_09/Assignment-07 68 | - caption: Week 10 - Object Oriented Programming II 69 | chapters: 70 | - file: week_10/01-OOP-Part-2 71 | - file: week_10/Assignment-08 72 | - caption: Week 11 - Object Oriented Programming III 73 | chapters: 74 | - file: week_11/01-OOP-Part-3 -------------------------------------------------------------------------------- /book/faq.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Frequently Asked Questions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## What is \"Scientific Programming\"?" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "**Scientific programming targets to solve scientific problems with the help of computers**. \n", 22 | "\n", 23 | "It is sometimes used as synonym for [computational science](https://en.wikipedia.org/wiki/Computational_science), but in my opinion these are not entirely the same. \"Scientific programming\" is not really a discipline, and therefore cannot be taught.\n", 24 | "\n", 25 | "What are we doing here then? Well, we are going to learn **programming** first, and then **programming as a tool to do science**. We are going to apply our new skills to scientific problems, but not only. Within the time given to us, we won't be able to learn *everything* about programming of course. My hope is that at the end of the lecture you'll have sufficient background and tools at your disposition to solve your own problems, and (this is the most important bit) that you'll know *where* to find solutions to the problems you encounter." 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "## Why should I learn programming?" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "As a scientist you are going to either *produce* or *analyze* data, most of the time you'll do both. For a long time, scientists have seen programming as a \"tool\", a menial task to accomplish in order to answer the questions they were asking. Nowadays programming has taken a prominent place in a scientist's work, for several reasons:\n", 40 | "- **the amount of data we have to handle increases together with computational power and our capacity to store it**. I would even argue that the bottleneck in model based scientific discovery isn't the computational effort anymore, but our capacity to comprehend and analyze these huge amounts of data.\n", 41 | "- **the questions we are asking are getting more complex, and so are the tools we are using to answer them**. The times when scientists could make discoveries with the help of a piece of paper and a pen are long gone, at least for most of us in the geosciences. We rely heavily on computer models, and these models are developed by us scientists, not only by programmers.\n", 42 | "- **science faces a credibility crisis**, and part of the mistrust towards scientists comes from the fact that their research happens behind closed doors, using closed source tools and based on protected data. Opening our computers and demonstrating that our code can be trusted is necessary to re-engage confidence in our results.\n", 43 | "- on a more general note: a better understanding of the tools that govern our digital world is a strong asset for many aspects of our everyday life and citizenship.\n", 44 | "\n", 45 | "In simple words, we have to become better programmers to be faster and better at what we do: science." 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "## Why Python? " 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "We will use the [Python programming language](https://www.python.org/) in this course. In case you are wondering why this language and not any other like ````, let me stop you right away: this course is not about \"learning Python\", it is about learning the general concepts of programming: algorithmic, numerics, program structure, object oriented programming, testing, etc. Python is just the tool I chose to use for this purpose.\n", 60 | "\n", 61 | "We could indeed have taken any other language, but there are several advantages in using Python. A quick web search will give you [millions of reasons](https://www.google.co.uk/search?dcr=0&ei=Wo2OWtKHNY7fwAKv1KbIDQ&q=Why+should+I+learn+python), but let me pick some of my favorites here:\n", 62 | "- **Python is a general purpose programming language and, as such, well suited to learn general programming concepts**. It is therefore better suited than, for example, [R](https://www.r-project.org/) which was developed for statistics and has certain particularities regarding object oriented programming in particular.\n", 63 | "- **Python can be used for many purposes**, from data preprocessing to numerical modeling and plotting. Unless you have a very compelling reason to change, you are likely to be able to use Python for all programming tasks you'll have in the near future.\n", 64 | "- **Python is one of the [fastest growing languages for data science](https://stackoverflow.blog/2017/09/06/incredible-growth-python/)**. There is a very active community developing new and exiting packages every day, and joining this community is a good bet on the future.\n", 65 | "- **Python is free and open-source**. No license fee, the code is available for everyone to see.\n", 66 | "\n", 67 | "There are many other reasons to use Python (and some arguments *against* Python as well of course), but I don't think it's relevant to list them here. My argument is following: for a good programmer, switching language is not a very big deal. It's not easy of course, but it's possible - becoming a good programmer is the hard bit, and is a never ending process." 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "## Why did you write these lecture notes? There are plenty of other resources online." 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "That's true, and I used many of these resources myself (see {doc}`references`). However, none of the tutorials is organized to fit *exactly* the 15 week semester of Austrian universities. Therefore, I had to make some choices regarding (i) what to teach and (ii) how to teach it. But you are right: there is no need to reinvent the wheel. As you will see, I will rely heavily on external resources, all openly available and written by better teachers than me. \n", 82 | "\n", 83 | "Following the open source philosophy, my lecture notes are also freely available." 84 | ] 85 | } 86 | ], 87 | "metadata": { 88 | "kernelspec": { 89 | "display_name": "Python 3 (ipykernel)", 90 | "language": "python", 91 | "name": "python3" 92 | }, 93 | "language_info": { 94 | "codemirror_mode": { 95 | "name": "ipython", 96 | "version": 3 97 | }, 98 | "file_extension": ".py", 99 | "mimetype": "text/x-python", 100 | "name": "python", 101 | "nbconvert_exporter": "python", 102 | "pygments_lexer": "ipython3", 103 | "version": "3.10.6" 104 | } 105 | }, 106 | "nbformat": 4, 107 | "nbformat_minor": 4 108 | } 109 | -------------------------------------------------------------------------------- /book/help.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "0", 6 | "metadata": {}, 7 | "source": [ 8 | "# Getting help" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "1", 14 | "metadata": {}, 15 | "source": [ 16 | "Seeking for information online is necessary and helpful for programmers of *any* level. I would even argue that the best programmers are the ones who know how to efficiently find information online.\n", 17 | "\n", 18 | "When encountering an issue, the first question you should ask yourself is: *\"am I the only person likely to be affected by this problem?\"*. **The answer will be no** in 99% of the cases. For these, here is a list of recommendations:\n", 19 | "- [Stack Overflow](https://stackoverflow.com/) used to be THE place for programming questions. Thanks to community based moderation rules, the good questions are more visible than bad ones, and good answers are rewarded. [Take a tour](https://stackoverflow.com/tour) of the site's principles, and look for similar questions before asking your own one. Many of its content has been scrapped by generative AI (see below), and the model that has fueled stack overflow's success over decades is at risk. Still, it remains an excellent resource to date.\n", 20 | "- Learn to ask the right question to your search engine. Naming this the correct way (semantics) is one of the objectives of this lecture, and I hope that in the end you will not only write better code, you will also *speak* the programming language a little better.\n", 21 | "- This class was developped *before* the rise of generative AI models like **ChatGPT** or **Github Copilot**. Today, these tools are omnipresent and are extremely useful for both beginner and expert programmers. I use these tools almost daily in my work. Feel free to use them in your learning journey - be aware however of their pros and cons. They should *assist* you in learning, not replace your own brain.\n", 22 | "\n", 23 | "If every other thing fails (i.e the remaining 1% of the cases), then:\n", 24 | "- Ask a friend\n", 25 | "- Ask your professor\n", 26 | "- Ask a question on Stack Overflow. Before doing so, read what a [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) is and try to stick to these recommendations.\n", 27 | "- If you think you discovered a bug, than report it to the library directly. Almost all the scientific python packages are hosted on GitHub: the \"issues\" tab is where to report bugs. Read the excellent [Craft Minimal Bug Reports](http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports) article from Matt Rocklin before doing so." 28 | ] 29 | } 30 | ], 31 | "metadata": { 32 | "kernelspec": { 33 | "display_name": "Python 3 (ipykernel)", 34 | "language": "python", 35 | "name": "python3" 36 | }, 37 | "language_info": { 38 | "codemirror_mode": { 39 | "name": "ipython", 40 | "version": 3 41 | }, 42 | "file_extension": ".py", 43 | "mimetype": "text/x-python", 44 | "name": "python", 45 | "nbconvert_exporter": "python", 46 | "pygments_lexer": "ipython3", 47 | "version": "3.12.4" 48 | } 49 | }, 50 | "nbformat": 4, 51 | "nbformat_minor": 5 52 | } 53 | -------------------------------------------------------------------------------- /book/img/10_errors_in_science.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/10_errors_in_science.png -------------------------------------------------------------------------------- /book/img/10_tdd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/10_tdd.png -------------------------------------------------------------------------------- /book/img/18_temp_pic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/18_temp_pic.png -------------------------------------------------------------------------------- /book/img/cmd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/cmd.png -------------------------------------------------------------------------------- /book/img/download_options.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/download_options.png -------------------------------------------------------------------------------- /book/img/install_options.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/install_options.png -------------------------------------------------------------------------------- /book/img/lauch_terminal.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/lauch_terminal.gif -------------------------------------------------------------------------------- /book/img/launch_binder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/launch_binder.png -------------------------------------------------------------------------------- /book/img/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/logo.png -------------------------------------------------------------------------------- /book/img/logo.xcf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/logo.xcf -------------------------------------------------------------------------------- /book/img/logo_acinn_small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/logo_acinn_small.jpg -------------------------------------------------------------------------------- /book/img/logo_uob_small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/logo_uob_small.jpg -------------------------------------------------------------------------------- /book/img/miniforge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/miniforge.png -------------------------------------------------------------------------------- /book/img/numpy/image0013830.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/numpy/image0013830.gif -------------------------------------------------------------------------------- /book/img/numpy/image0020619.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/numpy/image0020619.gif -------------------------------------------------------------------------------- /book/img/numpy/image004de9e.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/numpy/image004de9e.gif -------------------------------------------------------------------------------- /book/img/open_editor.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/open_editor.gif -------------------------------------------------------------------------------- /book/img/term.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/term.png -------------------------------------------------------------------------------- /book/img/unix-tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/unix-tree.png -------------------------------------------------------------------------------- /book/img/welcome_img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/book/img/welcome_img.jpg -------------------------------------------------------------------------------- /book/midterm.md: -------------------------------------------------------------------------------- 1 | # Midterm exam 2 | 3 | The midterm exam will take place on Tuesday November 22, from 10H20 to 11H00. 4 | 5 | Bring your laptops! The exam will be hold on OLAT. 6 | -------------------------------------------------------------------------------- /book/references.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# External resources " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Resources used (and linked) in these lecture notes:\n", 15 | "\n", 16 | "\n", 17 | "**Prerequisites:**\n", 18 | "- [Programming for Everybody (Getting Started with Python)](https://www.edx.org/course/programming-for-everybody-getting-started-with-python) from EdX: if you have no programming experience, this is recommended *before* you take this lecture, this will guide you through the first programming steps. If you are able to learn by yourself, the textbook is [available for free](https://www.py4e.com).\n", 19 | "\n", 20 | "**Linux and bash scripting:**\n", 21 | "- [Ryan's Tutorials](https://ryanstutorials.net/) for the linux command line and bash scripting are entertaining and well designed.\n", 22 | "\n", 23 | "**Python tutorials:**\n", 24 | "- the [official tutorial](https://docs.python.org/3/tutorial/index.html), always a good place to start.\n", 25 | "- the [scipy lecture notes](http://scipy-lectures.org), a good overview of the scientific python stack for scientists with previous programming experience.\n", 26 | "\n", 27 | "**Python reference:**\n", 28 | "- the [python documentation](https://docs.python.org) is your best reference for any question related to the language and the standard library.\n", 29 | "\n", 30 | "**Testing**\n", 31 | "- Katy Huff's [scientific python testing tutorial](http://carpentries-incubator.github.io/python-testing/): a one and a half hour introduction to testing practices, from the basics to continuous integration\n", 32 | "\n", 33 | "**Floating point precision errors**\n", 34 | "- Python's [Floating Point Arithmetic](https://docs.python.org/3/tutorial/floatingpoint.html) article is a good read but a bit tough for beginners\n", 35 | "- [What Every Programmer Should Know About Floating-Point Arithmetic](https://floating-point-gui.de/) is a more gentle introduction, but you still might want to use a search engine for more information.\n", 36 | "\n", 37 | "**Numpy**\n", 38 | "- [Why python is slow](https://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/) by Jake Vanderplas\n", 39 | "- [From Python to Numpy](http://www.labri.fr/perso/nrougier/from-python-to-numpy/) by Nicolas P. Rougier: an open-access book on numpy vectorization techniques (rather advanced).\n", 40 | "- [The numpy reference](https://docs.scipy.org/doc/numpy/reference/index.html): the official documentation is always the best place to obtain first hand information.\n", 41 | "\n", 42 | "**Scientific Python**\n", 43 | "- [Python Data Science Handbook](https://jakevdp.github.io/PythonDataScienceHandbook/) by Jake Vanderplas: an open-access textbook on using Python for Scientists\n", 44 | "\n", 45 | "**Python scripts, packages, namespaces and scopes**\n", 46 | "- an [entry level tutorial from RealPython](https://realpython.com/run-python-scripts/) about python scripts\n", 47 | "- [blog post by Sebastian Raschka](https://sebastianraschka.com/Articles/2014_python_scope_and_namespaces.html#scope-resolution-for-variable-names-via-the-legb-rule): A Beginner's Guide to Python's Namespaces, Scope Resolution, and the LEGB Rule\n", 48 | "\n", 49 | "**Object Oriented Programming**\n", 50 | "The web is full of blog posts and basic tutorials about OOP in python. Unfortunately, most of them make a poor job at explaining *why* OOP can be useful and when not. I will try to find better resources, but for now I recommend:\n", 51 | "- [RealPython's beginner tutorial](https://realpython.com/python3-object-oriented-programming/)\n", 52 | "- [Wikipedia page about OOP](https://en.wikipedia.org/wiki/Object-oriented_programming)\n", 53 | "- Wikipedia page about inheritance\n", 54 | "\n", 55 | "**Documentation**\n", 56 | "- [A beginner’s guide to writing documentation](http://www.writethedocs.org/guide/writing/beginners-guide-to-docs/)\n", 57 | "\n", 58 | "**Some youtube videos about tech in general**\n", 59 | "- [A fun but though-provoking talk about machine learning and why we should think before we do things](https://www.youtube.com/watch?v=ajGX7odA87k)\n", 60 | "\n", 61 | "*(this list will be updated when the notes get written further)*" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": null, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [] 70 | } 71 | ], 72 | "metadata": { 73 | "kernelspec": { 74 | "display_name": "Python 3 (ipykernel)", 75 | "language": "python", 76 | "name": "python3" 77 | }, 78 | "language_info": { 79 | "codemirror_mode": { 80 | "name": "ipython", 81 | "version": 3 82 | }, 83 | "file_extension": ".py", 84 | "mimetype": "text/x-python", 85 | "name": "python", 86 | "nbconvert_exporter": "python", 87 | "pygments_lexer": "ipython3", 88 | "version": "3.10.6" 89 | } 90 | }, 91 | "nbformat": 4, 92 | "nbformat_minor": 4 93 | } 94 | -------------------------------------------------------------------------------- /book/syllabus.md: -------------------------------------------------------------------------------- 1 | # Syllabus 2 | 3 | German: *Lehrveranstaltungskonzept*. Please read it carefully at the beginning of the class, and return to it as often as necessary. 4 | 5 | 6 | ## Learning outcomes 7 | 8 | This class aims at teaching **modern programming techniques** for (geo-)scientists. After completing the class, you will: 9 | - be familiar with a modern and open-source programming language (Python) and its use for scientific applications 10 | - be able to program in a structured, extendable and reproducible manner 11 | - be able to read and write python programs of intermediate complexity, as well as python packages 12 | - understand how numbers are handled by computers and be aware of numerical accuracy errors 13 | - be aware of simple performance considerations (vectorization) 14 | - know how to write and run formal tests for your code (with pytest) 15 | - be acquainted with various programming utility tools: IDEs, debugger 16 | - be able to search for, understand, install and take advantage of existing packages and libraries available in the rich scientific Python ecosystem 17 | 18 | 19 | ```{admonition} Non-objectives of this class: 20 | 21 | This class is not about learning data analysis, plotting or numerics. 22 | Nor is it about learning the details of the python packages you will 23 | use for the rest of your studies such as matplotlib, pandas, xarray, etc. 24 | 25 | My objective here is to provide you with a **solid foundational knowledge about core programming concepts**, 26 | in order to to make of you an **independent learner**, 27 | able to expand and deepen your programming skills by yourself. 28 | 29 | Don't worry: you will have ample time to play with fancy libraries in the other classes of the master curriculum. 30 | ``` 31 | 32 | ## Prerequisites 33 | 34 | The targeted audience for this lecture are **students at the master level with previous experience in programming**. No prior knowledge of python is required, but I'll assume that you are familiar with a similar language (Matlab, IDL, R...) and basic programming structures (loops, functions, conditional blocks...). 35 | 36 | Ideally you will have the programming level of a student having completed [my intro to programming class](https://fabienmaussion.info/intro_to_programming) in the BSc. 37 | The first few weeks we will be catching up on the materials from the bachelor class, but quickly enough we will move towards more advanced topics. 38 | 39 | ## Organisation of the class 40 | 41 | The class is taught in English. It is okay to ask questions in German as well! 42 | 43 | This class follows the [flipped classroom](https://en.wikipedia.org/wiki/Flipped_classroom) model. You will acquire new knowledge at home by reading online materials (and watching videos when appropriate). We will then use the time together in class to discuss the materials and write code. 44 | 45 | The semester is 15 weeks long. **Each week will always be organised as such**: 46 | - On Fridays, you will receive instructions for the week to come (which materials to read/watch, and one homework assignment). You study them at home during the week. 47 | - On Mondays (11:00-11:54), we meet to discuss the assignment solutions from the previous week (assignments are mandatory but not graded, see "Grading" below) 48 | - On Tuesdays (10:15-11:45), we meet in class. We will start the hour with a short, individual online assessment, for you to check if you understood the new materials correctly. Based on your own assessment, you pick the topics and questions you would like to discuss in class. We work on the assignments together. 49 | - On Fridays: start again! 50 | 51 | During the semester, you can work either on your own computer (laptop) or on the working stations in the computer room. 52 | 53 | ```{important} 54 | The class grants you 5 ECTS if successfully passed: in theory, this represents 8 to 10 hours work per week (not including holidays). For this course, it means that you will spend at least twice as much time doing homework than sitting in class. 55 | 56 | I strongly recommend to work regularly for the class. Programming is quite different to other disciplines, and "doing nothing for a few months" cannot be replaced by a "no-sleep-48-hours-push" at the end of semester. Programming is a bit like learning how to ski or climb: it is best learned by doing, and you will notice that regular practice will make you better each week. 57 | ``` 58 | 59 | ## Learning checklist 60 | 61 | At the end of each lesson, there is a "learning checklist": go over it at the end of the lecture and see i you can check all the boxes. It's a good way for you to check if you are ready to go on, or if you still need to go back to some reading and learning! 62 | 63 | ## Grading 64 | 65 | 66 | Combined: mid-term exam (20%), end-term exam (50%), participation to practicals (0%), programming project (30%). A positive evaluation of the sum of both exams, the practicals and the project is necessary to pass the class. 67 | 68 | 1. The mid-term exam (open-book exam, 0H45, combinaison of muliple choice, essay and programming type of questions) will take place on **Tuesday 22.11.2022** from 10:15 to 11:00. 69 | 2. The end-term exam (open-book exam, 1H30, combinaison of muliple choice, essay and programming type of questions) will take place on **Tuesday 07.02.2023** from 10:15 to 12:00. 70 | 3. The practicals take place every other Monday. Participation and one group presentation (non graded) is required to pass the class. 71 | 4. The projects start in November (date pending) and will have to be returned before the Christmas holidays. 72 | 73 | A positive evaluation of each of these elements is mandatory to pass the class! 74 | 75 | **Weekly assignments** 76 | 77 | Each week there will be an **assignment** (unless specified otherwise, e.g. during the group projects). These assignments can be worked through alone or in groups. Each week, I will ask **one volunteer group** to present their results to the rest of the class on the following week. 78 | 79 | **Programming project** 80 | 81 | In the middle of the semester, you will be given a programming project to realize over several weeks. Ideally, it will be a project that raises your interest. The grading will be explained once the project starts, but will be combined between an individual contribution (50%) within a group project (50%). 82 | 83 | **Bonus points** 84 | 85 | There will be bonus points for anyone pointing me to typos / mistakes / broken links / incomprehensible or difficult passages in the lecture notes. [Send me an email](https://fabienmaussion.infointro) or [open an issue](https://github.com/fmaussion/intro_to_programming/issues) on GitHub! 86 | 87 | ## Weekly lesson plan 88 | 89 | You will notice that there are 12 weekly units for a 15 weeks long semester: this is expected since (i) some weeks contain holidays, and (ii) I expect some changes to the schedule. 90 | 91 | - Week 01 - Terminal & Python primer 92 | - Welcome! Motivation & syllabus 93 | - Installing python & Getting started with the command line 94 | - For python newcomers: some reading to do 95 | - Week 02 - Python language fundamentals 96 | - Language fundamentals 97 | - Week 03 - Variables scopes, modules, strings, good practices 98 | - Modules, import mechanism, namespaces, scope(s) 99 | - String formatting and file paths 100 | - Good practices, programming style and conventions 101 | - Week 04 - Floating point arithmetics, Numpy basics 102 | - Introduction to numpy: why does numpy exist? 103 | - Number representation in computers 104 | - Week 05 - Scientific Python 105 | - Arrays in numpy: indexing, ufuncs, broadcasting 106 | - The scientific python stack 107 | - Week 06 - Revisions 108 | 109 | **Mid-term exam** 110 | 111 | - Week 07 - Testing 112 | - Errors, Exceptions and Warnings 113 | - Testing your code 114 | - Week 08 - Python Packages and ClimVis project 115 | - Structure of a python package 116 | - Term project: the ClimVis package 117 | - Week 09 - Code documentation 118 | - Documenting your code 119 | - Week 10 - Object Oriented Programming I 120 | - Object Oriented Programming: introduction 121 | - Week 11 - Object Oriented Programming II 122 | - Object Oriented Programming: inheritance 123 | - Week 12 - Object Oriented Programming III 124 | - Object Oriented Programming: why? 125 | - Week 13 - Data workflow 126 | - The scientific data analysis workflow 127 | 128 | **End-term exam** 129 | 130 | ## Previous lectures 131 | 132 | Here is a link to last year's lecture notes if you are curious: https://fabienmaussion.info/scipro_ws2021 133 | -------------------------------------------------------------------------------- /book/week_01/01-Installation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Installing Python on your computer" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | " This chapter contains some instructions about how to install Python on your personal computer.\n", 15 | "\n", 16 | "There are many ways to install Python on your computer (and while most of them work, some of them are less practical than others for scientific applications). Unless you know what you are doing (i.e.: you have been doing this before), please follow these instructions exactly.\n", 17 | "\n", 18 | "\n", 19 | "```{admonition} Important! What to do if you **already** have python installed on your laptop \n", 20 | ":class: warning, dropdown\n", 21 | " \n", 22 | "**If you already have anaconda / conda / miniconda installed**:\n", 23 | "\n", 24 | "You can keep your installation if it works for you. If you want to start from scratch, uninstall anaconda and start again as explained below. \n", 25 | "\n", 26 | "---\n", 27 | "\n", 28 | "**If you don't know what I'm talking about:** \n", 29 | "\n", 30 | "Then you should probably follow the instructions below.\n", 31 | "```" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "```{admonition} Important! For Windows 7 users\n", 39 | ":class: warning, dropdown\n", 40 | "\n", 41 | "Windows 7 is [not supported since January 2020](https://en.wikipedia.org/wiki/Windows_7). It seems that you can't install `mambaforge` on Windows 7. Regardless of python, I strongly recommend to update to a version with security updates. \n", 42 | "```" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "## Install Miniconda " 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "### On Windows \n", 57 | "\n", 58 | "Go to the miniforge download website: https://github.com/conda-forge/miniforge#mambaforge\n", 59 | "\n", 60 | "Download the installers for **Mambaforge** and your operating system (Windows, Mac OS or Linux).\n", 61 | "Unless you have an uncommon architecture (unlikely), you should choose the `x86_64` files.\n", 62 | "\n", 63 | "
\n", 64 | "\n", 65 | "Double-click the `.exe` file.\n", 66 | "\n", 67 | "Follow the instructions on the screen.\n", 68 | "\n", 69 | "If you are unsure about any setting, accept the defaults. You can change them later. At this stage, I recommend:\n", 70 | "\n", 71 | "\n", 72 | "\n", 73 | "### On Mac OS and Linux \n", 74 | "\n", 75 | "For these platforms, no need to download the files yourself. Open a terminal and do:\n", 76 | "\n", 77 | "```none\n", 78 | "curl -L -O \"https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-$(uname)-$(uname -m).sh\"\n", 79 | "bash Mambaforge-$(uname)-$(uname -m).sh\n", 80 | "```\n", 81 | "\n", 82 | "or, if this doesn't work:\n", 83 | "\n", 84 | "```none\n", 85 | "wget \"https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-$(uname)-$(uname -m).sh\"\n", 86 | "bash Mambaforge-$(uname)-$(uname -m).sh\n", 87 | "```\n", 88 | "\n", 89 | "The first command downloads the tool, and the second installs it. Follow the prompts on the installer screens.\n", 90 | "\n", 91 | "If you are unsure about any setting, accept the defaults **except for the last question asking you if you wish to run `conda init`: here, answer \"yes\".**\n", 92 | "\n", 93 | "To make the changes take effect, close and then re-open your terminal window.\n", 94 | "\n", 95 | "### Testing your installation\n", 96 | "\n", 97 | "\n", 98 | "To see if everything worked well, open a terminal (Mac and Linux). On Windows, open the `miniforge prompt` (from the Start menu, search for and open \"miniforge prompt\"):\n", 99 | "\n", 100 | "
\n", 101 | "\n", 102 | "and type in:\n", 103 | "\n", 104 | "```none\n", 105 | "mamba list\n", 106 | "``` \n", 107 | "\n", 108 | "You should see a long list of package names.\n", 109 | "\n", 110 | "If you type:\n", 111 | "\n", 112 | "```none\n", 113 | "python\n", 114 | "```\n", 115 | "\n", 116 | "A new python prompt should appear, with something like:\n", 117 | "\n", 118 | "```none\n", 119 | "Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:15:42) [MSC v.1916 64 bit (AMD64)] on win32\n", 120 | "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", 121 | ">>>\n", 122 | "```\n", 123 | "\n", 124 | "You can type ``exit()`` to get out of the python interpreter.\n", 125 | "\n", 126 | "\n", 127 | "```{admonition} Optional: conda, mamba, conda-forge?\n", 128 | ":class: note, dropdown\n", 129 | "\n", 130 | "Although the instructions above should work in almost all situations and should get you ready for the class,\n", 131 | "I should explain what we have just done in case you are interested or if you google:\n", 132 | "\n", 133 | "We have just installed [miniconda](https://docs.conda.io/en/latest/miniconda.html) which is a minimal installer for the larger project Anaconda. Anaconda is a scientific Python installation which, in my opinion, has too many options and tools that you won't need.\n", 134 | "\n", 135 | "**But** we haven't installed `miniconda` only: we have also set some options per default automatically:\n", 136 | "- we now use [conda-forge](https://conda-forge.org/) as default channels to download the python packages we will use later in the lecture (instead of anaconda channels).\n", 137 | "- we now use [mamba](https://mamba.readthedocs.io) as the default python package install tool (instead of `conda`). `mamba install` behaves exactly like `conda install`, but is significantly faster.\n", 138 | "```" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "## What is this python \"interpreter\"? " 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "Start with reading the definition from the Tech Terms Dictionary.\n", 153 | "\n", 154 | "Put simple, the python interpreter is a program executing python programs or commands. It reads the programs you write and executes the instructions they contain. We will look into interpreters in a little more detail later in the semester, when we will talk about compiled and interpreted languages. " 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "## What is a \"python installation\"?" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "[Explanation for windows users](01a-Details-Windows)\n", 169 | ": the difference between the command prompt, the miniforge prompt, and the python interpreter\n", 170 | "\n", 171 | "[Explanation for linux/mac users](01b-Details-Linux)\n", 172 | ": what did ``mambaforge`` do to your terminal?" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "## Learning checklist\n", 180 | "\n", 181 | "\n", 182 | " \n", 183 | " \n", 184 | " " 185 | ] 186 | } 187 | ], 188 | "metadata": { 189 | "hide_input": false, 190 | "kernelspec": { 191 | "display_name": "Python 3 (ipykernel)", 192 | "language": "python", 193 | "name": "python3" 194 | }, 195 | "language_info": { 196 | "codemirror_mode": { 197 | "name": "ipython", 198 | "version": 3 199 | }, 200 | "file_extension": ".py", 201 | "mimetype": "text/x-python", 202 | "name": "python", 203 | "nbconvert_exporter": "python", 204 | "pygments_lexer": "ipython3", 205 | "version": "3.12.4" 206 | }, 207 | "toc": { 208 | "base_numbering": 2, 209 | "nav_menu": {}, 210 | "number_sections": true, 211 | "sideBar": true, 212 | "skip_h1_title": false, 213 | "title_cell": "Table of Contents", 214 | "title_sidebar": "Contents", 215 | "toc_cell": true, 216 | "toc_position": {}, 217 | "toc_section_display": "block", 218 | "toc_window_display": true 219 | } 220 | }, 221 | "nbformat": 4, 222 | "nbformat_minor": 4 223 | } 224 | -------------------------------------------------------------------------------- /book/week_01/01a-Details-Windows.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# For windows users: the difference between the command prompt, the miniforge prompt, and the python interpreter " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Many of you have opened a prompt (a terminal) for the first time in this class. When I was a child and my parents bought our first computer, opening a terminal was very normal and even needed to play games! \n", 15 | "\n", 16 | "**There is nothing to be afraid of**. You will use the prompt very often, but you won't need it for anything else than starting python, jupyterlab, or any other python tool of your choice.\n", 17 | "\n", 18 | "## The command prompt (or `cmd.exe`)\n", 19 | "\n", 20 | "You will find it by typing cmd in the windows search bar:\n", 21 | "\n", 22 | "
\n", 23 | "\n", 24 | "In the command prompt, you can do many things, but the one thing you'll really need is to **navigate** to the folder of your choice. The name of the currently active folder is always indicated at the start of the command line.\n", 25 | "\n", 26 | "Only a few commands are necessary for this class:\n", 27 | "- `help`: seeing the list of commands available\n", 28 | "- `dir`: list the directories (folders) in the current location\n", 29 | "- `cd FolderName`: navigate to the folder FolderName\n", 30 | "- `cd ..`: navigate one folder up\n", 31 | "- `TAB`: ask the command prompt to complete the name for you. For example, if you are lazy and type two letters of the desired folder only, typing `TAB` might complete it for you\n", 32 | "- `exit`: exit the prompt (you can also simply close the window)\n", 33 | "\n", 34 | "That's it! The reason why you need to be able to navigate to a specific folder will be explained in the next lesson.\n", 35 | "\n", 36 | "## The miniforge prompt\n", 37 | "\n", 38 | "You will find it by typing \"miniforge\" in the windows search bar:\n", 39 | "\n", 40 | "
\n", 41 | "\n", 42 | "You will recognize that you are in a miniforge prompt by the title of the window and the `(base)` text before the location:\n", 43 | "\n", 44 | "
\n", 45 | "\n", 46 | "**The miniforge prompt is exactly like the command prompt, BUT it has python installed in it**. All the commands explained above work in the miniforge prompt as well, but these new commands are now available to you:\n", 47 | "- all the `mamba` commands (`mamba list`, `mamba install`, ...)\n", 48 | "- opening a python interpreter (`python`, and later today also `ipython`, `jupyter-lab`) \n", 49 | "\n", 50 | "**In practice, you will actually never open the command prompt, but always the miniforge prompt. You will use it to navigate to a folder and then start a python interpreter.**\n", 51 | "\n", 52 | "## The python interpreter\n", 53 | "\n", 54 | "The python interpreter can be opened in many different ways, but the way you learn first is to start it from the miniforge prompt. The python interpreter is recognizable by the three \"great than\" sign at the beginning of the command line `>>>`. The python interpreter is where the python commands are valid.\n", 55 | "\n", 56 | "```{admonition} Be aware of the context\n", 57 | ":class: important\n", 58 | "\n", 59 | "Be aware of the interpreter your are using. To summarize:\n", 60 | "- In the command prompt, you can navigate between folders (`cd`, `dir`, etc.)\n", 61 | "- In the miniforge prompt, you can navigate between folders AND you can install python packages (`mamba`) and start python (`python` or `jupyter-lab`)\n", 62 | "- In the python interpreter, you can type python commands and much much more \n", 63 | "```" 64 | ] 65 | } 66 | ], 67 | "metadata": { 68 | "hide_input": false, 69 | "kernelspec": { 70 | "display_name": "Python 3 (ipykernel)", 71 | "language": "python", 72 | "name": "python3" 73 | }, 74 | "language_info": { 75 | "codemirror_mode": { 76 | "name": "ipython", 77 | "version": 3 78 | }, 79 | "file_extension": ".py", 80 | "mimetype": "text/x-python", 81 | "name": "python", 82 | "nbconvert_exporter": "python", 83 | "pygments_lexer": "ipython3", 84 | "version": "3.10.6" 85 | }, 86 | "toc": { 87 | "base_numbering": 2, 88 | "nav_menu": {}, 89 | "number_sections": true, 90 | "sideBar": true, 91 | "skip_h1_title": false, 92 | "title_cell": "Table of Contents", 93 | "title_sidebar": "Contents", 94 | "toc_cell": true, 95 | "toc_position": {}, 96 | "toc_section_display": "block", 97 | "toc_window_display": true 98 | } 99 | }, 100 | "nbformat": 4, 101 | "nbformat_minor": 4 102 | } 103 | -------------------------------------------------------------------------------- /book/week_01/01b-Details-Linux.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# For linux/mac users: what did `mambaforge` do to your terminal?" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "In this short video I explain what just happened to your terminal, if you are interested:" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": { 21 | "tags": [ 22 | "remove-input" 23 | ] 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "from IPython.display import VimeoVideo\n", 28 | "VimeoVideo(755453097, width=900)" 29 | ] 30 | } 31 | ], 32 | "metadata": { 33 | "hide_input": false, 34 | "kernelspec": { 35 | "display_name": "Python 3 (ipykernel)", 36 | "language": "python", 37 | "name": "python3" 38 | }, 39 | "language_info": { 40 | "codemirror_mode": { 41 | "name": "ipython", 42 | "version": 3 43 | }, 44 | "file_extension": ".py", 45 | "mimetype": "text/x-python", 46 | "name": "python", 47 | "nbconvert_exporter": "python", 48 | "pygments_lexer": "ipython3", 49 | "version": "3.10.6" 50 | }, 51 | "toc": { 52 | "base_numbering": 2, 53 | "nav_menu": {}, 54 | "number_sections": true, 55 | "sideBar": true, 56 | "skip_h1_title": false, 57 | "title_cell": "Table of Contents", 58 | "title_sidebar": "Contents", 59 | "toc_cell": true, 60 | "toc_position": {}, 61 | "toc_section_display": "block", 62 | "toc_window_display": true 63 | } 64 | }, 65 | "nbformat": 4, 66 | "nbformat_minor": 4 67 | } 68 | -------------------------------------------------------------------------------- /book/week_01/02-Terminal.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Basic terminal commands and navigation" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "In order to start using python (or any programming language, really) you need to know a few very important things about your computer:\n", 15 | "- where are files located on my system?\n", 16 | "- how to use a terminal to navigate between folders to find the files I want to use?" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## Tutorials\n", 24 | "\n", 25 | "- **For Windows users**: [A windows command prompt primer](./02a-Windows)\n", 26 | "- **For Linux/Mac users** (recommended for windows users as well): [An introduction to the Linux terminal](./02b-Linux)" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "## Learning checklist (all platforms)\n", 34 | "\n", 35 | "\n", 36 | " \n", 37 | " " 38 | ] 39 | } 40 | ], 41 | "metadata": { 42 | "hide_input": false, 43 | "kernelspec": { 44 | "display_name": "Python 3 (ipykernel)", 45 | "language": "python", 46 | "name": "python3" 47 | }, 48 | "language_info": { 49 | "codemirror_mode": { 50 | "name": "ipython", 51 | "version": 3 52 | }, 53 | "file_extension": ".py", 54 | "mimetype": "text/x-python", 55 | "name": "python", 56 | "nbconvert_exporter": "python", 57 | "pygments_lexer": "ipython3", 58 | "version": "3.10.6" 59 | }, 60 | "toc": { 61 | "base_numbering": 2, 62 | "nav_menu": {}, 63 | "number_sections": true, 64 | "sideBar": true, 65 | "skip_h1_title": false, 66 | "title_cell": "Table of Contents", 67 | "title_sidebar": "Contents", 68 | "toc_cell": true, 69 | "toc_position": {}, 70 | "toc_section_display": "block", 71 | "toc_window_display": true 72 | } 73 | }, 74 | "nbformat": 4, 75 | "nbformat_minor": 4 76 | } 77 | -------------------------------------------------------------------------------- /book/week_01/02a-Windows.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# A Windows command prompt primer" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "These two videos are not from me but are quite good as an introduction to the windows command prompt. There are enough to learn the very basics of the prompt and enough to get you started. Watch the following units from the same author if you are curious to know more. " 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": { 21 | "tags": [ 22 | "remove-input" 23 | ] 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "from IPython.display import YouTubeVideo\n", 28 | "YouTubeVideo('MBBWVgE0ewk', width=900)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "metadata": { 35 | "tags": [ 36 | "remove-input" 37 | ] 38 | }, 39 | "outputs": [], 40 | "source": [ 41 | "YouTubeVideo('7ABkcHLdG_A', width=900)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [] 50 | } 51 | ], 52 | "metadata": { 53 | "hide_input": false, 54 | "kernelspec": { 55 | "display_name": "Python 3 (ipykernel)", 56 | "language": "python", 57 | "name": "python3" 58 | }, 59 | "language_info": { 60 | "codemirror_mode": { 61 | "name": "ipython", 62 | "version": 3 63 | }, 64 | "file_extension": ".py", 65 | "mimetype": "text/x-python", 66 | "name": "python", 67 | "nbconvert_exporter": "python", 68 | "pygments_lexer": "ipython3", 69 | "version": "3.10.6" 70 | }, 71 | "toc": { 72 | "base_numbering": 2, 73 | "nav_menu": {}, 74 | "number_sections": true, 75 | "sideBar": true, 76 | "skip_h1_title": false, 77 | "title_cell": "Table of Contents", 78 | "title_sidebar": "Contents", 79 | "toc_cell": true, 80 | "toc_position": {}, 81 | "toc_section_display": "block", 82 | "toc_window_display": true 83 | } 84 | }, 85 | "nbformat": 4, 86 | "nbformat_minor": 4 87 | } 88 | -------------------------------------------------------------------------------- /book/week_01/03-Installation-uni.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Installing Python at the University of Innsbruck" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "```{note}\n", 15 | "Python works very well on the stations in the computer room. It is a nice way for you to not have to carry your laptop with you for each class, and also you will learn some linux on the go! Furthermore, it is also useful for the climate class since all necessary packages are installed and ready to be used.\n", 16 | "\n", 17 | "However, if you prefer not to, you can skip this lesson.\n", 18 | "```" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "In this section we use the powerful linux ``PATH`` variable you've learned about in the [linux tutorial](02b-Linux) to \"install\" the same Python program in your personal Linux account. This is done in a couple of easy steps." 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "## The default python on linux systems" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "Open a terminal and type:\n", 40 | "\n", 41 | "```\n", 42 | "$ python\n", 43 | "```\n", 44 | "\n", 45 | "This should have started the python command line, looking like this:\n", 46 | "\n", 47 | "```\n", 48 | "Python 2.7.5 (default, Apr 2 2020, 13:16:51) \n", 49 | "[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2\n", 50 | "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", 51 | ">>>\n", 52 | "```\n", 53 | "\n", 54 | "The ```>>>``` is a **prompt**, waiting for you to type python commands. Type ``[CTRL+D]`` to exit.\n", 55 | "\n", 56 | "Python is available per default on virtually *any* linux system, for the simple reason that many programs actually *use* python internally. However, as you can see, it runs python version 2 (as of 2020, modern Linux distributions now removed the `python` command and only offer `python3`). We can also run python 3 with a simple:\n", 57 | "\n", 58 | "```\n", 59 | "$ python3\n", 60 | "```\n", 61 | "\n", 62 | "Now, shouldn't this python program be enough? For simple usages like today's examples, yes it would suffice. But as we are going to see, python alone isn't really useful for us scientists: we need so-called **third party packages**, tools developed with and for python, but not available per default. While some of them are so important that they are available as linux packages, it is much easier (and safer) to use our own python installation for installing those. This is the primary purpose of a personalized python installation." 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "## Using a custom (pre-installed) python at the university" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "It turns out I have \"installed\" a more recent python version (and some additional packages) on a shared repository available to everyone with a UIBK account. I wrote \"installed\" with quotation marks because, in linux, the definition of \"installed\" is subjective. Let's say that I've put the python executables somewhere where everybody can see them. Let's try it:\n", 77 | "\n", 78 | "```\n", 79 | "$ /project/c7071047/miniconda3/bin/python\n", 80 | "Python 3.7.3 | packaged by conda-forge | (default, Jul 1 2019, 21:52:21) \n", 81 | "[GCC 7.3.0] :: Anaconda, Inc. on linux\n", 82 | "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", 83 | ">>>\n", 84 | "```\n", 85 | "\n", 86 | "You should have been able to launch a new python interpreter: this version is more recent and provided by ``Anaconda, Inc.`` **This is the python I'd like you to use from now on** (when at the University - for your personal computer see the next section).\n", 87 | "\n", 88 | "```{note}\n", 89 | "The ``/project`` folder is a shared folder. I don't know where the device is located *physically*, but it definitely isn't in the computer room. This is yet another powerful feature of linux systems: shared folders look just like normal folders. Quite practical, huh? Now this may have one simple drawback: launching this python might be a bit slow sometimes (especially if all of you try to open it at the same time). Most of the time this shouldn't be a big problem, though.\n", 90 | "```" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "## Making this change \"permanent\" " 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "Nobody wants to write such a long command to start python. So what we are going to do is to add the ``/project/c7071047/miniconda3/bin/`` folder to our ``PATH``, and in such a way that it is remembered for your later sessions.\n", 105 | "\n", 106 | "We are going to edit a special file in your ``HOME``, ``~/.bashrc``. This file contains a list of commands which are executed automatically each time you open a terminal. How practical! Let's open this file and add the following two lines at the end of it:\n", 107 | "\n", 108 | "```\n", 109 | "# added for the python course:\n", 110 | "export PATH=\"/project/c7071047/miniconda3/bin:$PATH\"\n", 111 | "```\n", 112 | "\n", 113 | "```{warning}\n", 114 | "Careful! This will add a folder to the ``PATH`` variable, which is a fundamental element in linux: make sure that you add these two lines (and exactly these lines: respect the upper- and lower-case as well!) to your ``.bashrc``\n", 115 | "```\n", 116 | "\n", 117 | "What did we just do? We added a folder to the ``PATH`` (remember what this is?) and added it at the *beginning* of it. This is important because linux is going to look for programs in order: if a ``python`` executable is found in the first folder, no need to look for another one (i.e. ignore the default linux program).\n", 118 | "\n", 119 | "```{note}\n", 120 | "For this change to take effect, close your terminal and open a new one! ``~/.bashrc`` is executed only once, at the opening of a new terminal.\n", 121 | "```" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "If everything worked fine, after typing ``python`` in the command line you should be given the most recent version I prepared for you." 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "## Learning summary" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "- python is available per default on almost all linux distributions, for the simple reason that many software packages use python internally \n", 143 | "- on linux, a python \"installation\" is simply a link to a python executable file - on Windows, it's a bit more complicated" 144 | ] 145 | } 146 | ], 147 | "metadata": { 148 | "hide_input": false, 149 | "kernelspec": { 150 | "display_name": "Python 3 (ipykernel)", 151 | "language": "python", 152 | "name": "python3" 153 | }, 154 | "language_info": { 155 | "codemirror_mode": { 156 | "name": "ipython", 157 | "version": 3 158 | }, 159 | "file_extension": ".py", 160 | "mimetype": "text/x-python", 161 | "name": "python", 162 | "nbconvert_exporter": "python", 163 | "pygments_lexer": "ipython3", 164 | "version": "3.10.6" 165 | }, 166 | "toc": { 167 | "base_numbering": 2, 168 | "nav_menu": {}, 169 | "number_sections": true, 170 | "sideBar": true, 171 | "skip_h1_title": false, 172 | "title_cell": "Table of Contents", 173 | "title_sidebar": "Contents", 174 | "toc_cell": true, 175 | "toc_position": {}, 176 | "toc_section_display": "block", 177 | "toc_window_display": true 178 | } 179 | }, 180 | "nbformat": 4, 181 | "nbformat_minor": 4 182 | } 183 | -------------------------------------------------------------------------------- /book/week_01/04-Getting-Started.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Getting started with Python " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "In this chapter we are getting used to the python command line, and we are going to write our first small program.\n", 15 | "\n", 16 | "**Prerequisites**: you have python and ipython installed, as described in {doc}`01-Installation`. Alternatively, you can also use MyBinder for this unit." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## The standard command-line interpreter" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "Let's open a python interpreter and type in some commands:\n", 31 | "\n", 32 | "```python\n", 33 | ">>> a = 2\n", 34 | ">>> b = 3\n", 35 | ">>> print(a+b)\n", 36 | "5\n", 37 | ">>> a*b\n", 38 | "6\n", 39 | ">>> a**3\n", 40 | "```" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "This should look quite familiar to you, whatever language you learned before. We defined variables, and did some arithmetics with them. On line 3 we used the ``print()`` function to display the output of our computations, but as it turns out it is not necessary: the command line shell will print the output anyway. This functionality is called [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop), and it is a feature of modern interpreted languages. We'll come back to this later." 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "## Running scripts" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "Of course, the command-line interpreter is only useful for small tasks or for data exploration. Most of the time, you will want to write **scripts** (a collection of commands executed in order to reach a certain goal) or more complex **programs** (the difference between \"scripts\" and \"programs\" is fuzzy, but for now let's call a \"program\" a tool which involves writing several functions and/or files)." 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "### Running a python script from the terminal " 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "Python scripts are simple text files. Per convention we like to give them a suffix: ``.py``. This suffix is required for python modules but not mandatory for scripts with a shebang. In general it's a good idea to use the ``.py`` suffix though.\n", 76 | "\n", 77 | "Let's create a new file, ``my_python_script.py`` (using a text editor of your choice, for example `gedit` in ubuntu or `wordpad` in Windows), and add these few lines of code to it:\n", 78 | "\n", 79 | "```python\n", 80 | "a = 3\n", 81 | "b = 6\n", 82 | "c = a * b\n", 83 | "print('The product of {} and {} is {}'.format(a, b, c))\n", 84 | "```\n", 85 | "\n", 86 | "Now you should be able to run the script by calling python in a terminal:\n", 87 | "\n", 88 | "```\n", 89 | "$ python my_python_script.py\n", 90 | "```" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "## Take home points" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "- python scripts are text files ending with ``.py``. They can be run from the terminal (``python my_file.py``) or from the ``ipython`` interpreter (``%run my_file.py``)." 105 | ] 106 | } 107 | ], 108 | "metadata": { 109 | "hide_input": false, 110 | "kernelspec": { 111 | "display_name": "Python 3 (ipykernel)", 112 | "language": "python", 113 | "name": "python3" 114 | }, 115 | "language_info": { 116 | "codemirror_mode": { 117 | "name": "ipython", 118 | "version": 3 119 | }, 120 | "file_extension": ".py", 121 | "mimetype": "text/x-python", 122 | "name": "python", 123 | "nbconvert_exporter": "python", 124 | "pygments_lexer": "ipython3", 125 | "version": "3.10.6" 126 | }, 127 | "latex_envs": { 128 | "LaTeX_envs_menu_present": true, 129 | "autoclose": false, 130 | "autocomplete": true, 131 | "bibliofile": "biblio.bib", 132 | "cite_by": "apalike", 133 | "current_citInitial": 1, 134 | "eqLabelWithNumbers": true, 135 | "eqNumInitial": 1, 136 | "hotkeys": { 137 | "equation": "Ctrl-E", 138 | "itemize": "Ctrl-I" 139 | }, 140 | "labels_anchors": false, 141 | "latex_user_defs": false, 142 | "report_style_numbering": false, 143 | "user_envs_cfg": false 144 | }, 145 | "nbTranslate": { 146 | "displayLangs": [ 147 | "*" 148 | ], 149 | "hotkey": "alt-t", 150 | "langInMainMenu": true, 151 | "sourceLang": "en", 152 | "targetLang": "fr", 153 | "useGoogleTranslate": true 154 | }, 155 | "toc": { 156 | "base_numbering": 3, 157 | "nav_menu": {}, 158 | "number_sections": true, 159 | "sideBar": true, 160 | "skip_h1_title": false, 161 | "title_cell": "Table of Contents", 162 | "title_sidebar": "Contents", 163 | "toc_cell": true, 164 | "toc_position": {}, 165 | "toc_section_display": "block", 166 | "toc_window_display": true 167 | } 168 | }, 169 | "nbformat": 4, 170 | "nbformat_minor": 4 171 | } 172 | -------------------------------------------------------------------------------- /book/week_01/Assignment-01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Assignment #01" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "This week's assignment is about getting you more comfortable with linux and able to use python on your computer before doing more complicated things." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## #01-01: install python on your laptop" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "Follow the instructions from {doc}`01-Installation`." 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "## #01-02: linux tutorial" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "Recommended for windows users, mandatory for linux/mac users: follow the [linux tutorial](./02b-Linux) (on MyBinder, your laptop, or at university)." 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "## #01-03: install python on your university account" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "Recommended for all users: follow the instructions from {doc}`03-Installation-uni` to install python on your linux account at the university." 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "## #01-04: a first python script" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "Open a terminal/prompt and navigate to a folder on your laptop where you will put the exercises of this class.\n", 71 | "\n", 72 | "Create a new python file containing the following code:\n", 73 | "\n", 74 | "```python\n", 75 | "import platform\n", 76 | "import sys\n", 77 | "import os\n", 78 | "\n", 79 | "print('Enter your name:')\n", 80 | "username = input()\n", 81 | "print(f\"Hello {username}!\")\n", 82 | "print(f\"You are running python on {platform.system()}, release {platform.release()}.\")\n", 83 | "print(f\"Your python version is {sys.version}, installed at {sys.exec_prefix}\")\n", 84 | "print(f\"Your current working directory is {os.getcwd()}.\", end=\" \")\n", 85 | "print(f\"It has {len(os.listdir())} files and directories in it.\")\n", 86 | "```\n", 87 | "\n", 88 | "**Run the script**. Does everything match your expectations? If not, you might want to get back to all the steps above." 89 | ] 90 | } 91 | ], 92 | "metadata": { 93 | "hide_input": false, 94 | "kernelspec": { 95 | "display_name": "Python 3 (ipykernel)", 96 | "language": "python", 97 | "name": "python3" 98 | }, 99 | "language_info": { 100 | "codemirror_mode": { 101 | "name": "ipython", 102 | "version": 3 103 | }, 104 | "file_extension": ".py", 105 | "mimetype": "text/x-python", 106 | "name": "python", 107 | "nbconvert_exporter": "python", 108 | "pygments_lexer": "ipython3", 109 | "version": "3.10.6" 110 | }, 111 | "toc": { 112 | "base_numbering": 1, 113 | "nav_menu": {}, 114 | "number_sections": false, 115 | "sideBar": true, 116 | "skip_h1_title": false, 117 | "title_cell": "Table of Contents", 118 | "title_sidebar": "Contents", 119 | "toc_cell": false, 120 | "toc_position": {}, 121 | "toc_section_display": "block", 122 | "toc_window_display": false 123 | } 124 | }, 125 | "nbformat": 4, 126 | "nbformat_minor": 4 127 | } 128 | -------------------------------------------------------------------------------- /book/week_02/01-install-packages.md: -------------------------------------------------------------------------------- 1 | # Installing python packages 2 | 3 | This chapter contains instructions about how to install 4 | additional packages useful for scientific programming, such as: 5 | - ipython and spyder for development 6 | - jupyterlab for interactive notebooks (e.g. for the climate class) 7 | - other packages of the scientific ecosystem, such as numpy, xarray, matplotlib... 8 | 9 | ## Prerequisites 10 | 11 | - you have a working python installation based on mambaforge ([Week 01](../week_01/01-Installation.ipynb)) or conda 12 | - you understood the differences between the **windows prompt**, the **miniforge prompt** and the **python 13 | interpreter** 14 | 15 | ```{warning} 16 | 17 | If you still have doubts about all these terms, don't hesitate to revisit the installation instructions in 18 | week [01](../week_01/01-Installation.ipynb). 19 | ``` 20 | 21 | ## Context: the conda (base) environment 22 | 23 | When you open the **miniforge prompt** (a **terminal** in linux/mac), you are opening a windows prompt with new tools available: 24 | for one, `python` is installed and can be run. Similarly, `conda` and `mamba` commands are only available from the miniforge 25 | prompt, and not from the standard prompt. 26 | This is possible thanks to [conda](https://docs.conda.io), which is a package management system for python. 27 | Conda gives you access to a very large number of python packages *for free* (the only thing you'll need is an 28 | internet connection, to connect to the package servers). 29 | 30 | ```{admonition} More details on miniforge and mamba 31 | :class: dropdown, note 32 | 33 | I asked you to insall miniforge instead of "plain" conda for two main reasons: 34 | - miniforge is giving you access to the packages available from the [conda-forge](https://conda-forge.org), which is a more complete and more up-to-date repository for python packages. 35 | - miniforge has `mamba` installed per default. `mamba` is a "drop-in" replacement for `conda`, and is significantly faster. You don't need to know why at this stage: just follow my instructions and you will be fine. 36 | 37 | ``` 38 | 39 | You will recognize that you are using conda thanks to the following indices: 40 | - when opening the miniforge prompt or terminal, a `(base)` text appears in front of the current path. For the user Jane, a 41 | typical miniforge prompt looks like: `(base) C:\Users\Jane>` 42 | - when Jane asks her computer where to find python, conda is indicating the `python.exe` that came with the conda installation. 43 | 44 | You can ask for the location of a specific prompt command with the command `where`. 45 | For Jane, the miniforge prompt gives the following indications about the location of `python`: 46 | 47 | ```none 48 | (base) C:\Users\Jane> where python 49 | C:\Users\Jane\mambaforge\python.exe 50 | C:\Users\Jane\AppData\Local\Microsoft\WindowsApps\python.exe 51 | 52 | (base) C:\Users\Jane> 53 | ``` 54 | 55 | The first `python.exe` on the list is the one that will be used if you type `python` in the prompt. 56 | This is precisely why conda is useful: **it clearly separates your python installation from all other contents 57 | on your computer**. 58 | 59 | 60 | ## Install ipython, jupyterlab and spyder in the (base) environment 61 | 62 | [ipython](https://ipython.org) and [jupyter](https://jupyter.org) are fundamental tools of a scientific python installation. We will use them a LOT during the semester and your studies. I will make a demonstration in class, but first you have to install it! **From the miniforge prompt**, type: 63 | 64 | 65 | ```none 66 | mamba install ipython jupyterlab spyder 67 | ``` 68 | 69 | This will install ipython, jupyterlab and spyder at the same time. To check if it worked, type: 70 | 71 | ```none 72 | ipython 73 | ``` 74 | 75 | Which should display something like: 76 | 77 | ```none 78 | Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:15:42) [MSC v.1916 64 bit (AMD64)] 79 | Type 'copyright', 'credits' or 'license' for more information 80 | IPython 8.1.1 -- An enhanced Interactive Python. Type '?' for help. 81 | 82 | In [1]: 83 | ``` 84 | 85 | The only *visual* difference between the `ipython` and `python` interpreters is that `>>>` has been replaced by `In [1]:`. More on this later. 86 | 87 | Exit `ipython` (remember how? Use `exit()`), and then type: 88 | 89 | ```none 90 | spyder 91 | ``` 92 | 93 | This should open a development environment called spyder. If all of this worked properly, you are good to go. 94 | 95 | 96 | ```{exercise} 97 | Open a miniforge prompt and ask windows where to find your current python installation. Compare yours with Jane's. 98 | What about the location of `ipython`? And of `jupyterlab`? 99 | ``` 100 | 101 | ## Install other packages and managing environments 102 | 103 | These instructions are useful for later in the class (when I'll ask you to install more packages) or for the climate class (where you'll have to install packages right away). 104 | 105 | ### Create an environment called "scipro" 106 | 107 | `(base)` is the name of the base (default) environment for conda. Installing further packages in `(base)` is fine, 108 | but I recommend against it. I recommend to keep `(base)` as simple as possible, with few or no packages installed, 109 | and use named environments for further usages. I'll explain you why a bit later. 110 | 111 | First, open the miniforge prompt (in `base`) and type the following command: 112 | 113 | ```none 114 | mamba create -n scipro --clone base 115 | ``` 116 | 117 | If asked to confirm, type "yes". 118 | 119 | What did we just do? We created a new conda environment called "`scipro`" (this is the 120 | purpose of the option `-n`) which clones all packages available in `base` with the option `--clone base` 121 | (this last part is optional: if you omit it, your new environment will be completely empty and you'll have to reinstall jupyter to be able to 122 | use it). 123 | 124 | You can now activate your new environment with `mamba activate scipro`. 125 | 126 | ```{exercise} 127 | Activate the new environment. What changed in comparison to `(base)`? Now ask the prompt again about where to find the commands 128 | `ipython` and `jupyter-lab`. Can you see the difference to `base`? 129 | ``` 130 | 131 | [Conda environments](https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/environments.html) are a very simple 132 | and elegant way to manage different installations of python packages. They allow to clearly separate different installations 133 | and, more importantly, **conda environments allow us to make mistakes**. 134 | 135 | Since "environments" are nothing else than folders on your computer, they allow setups such as: 136 | - `(base)`: python v3.10, jupyter-lab, ipython 137 | - `(scipro)`: same as `(base)` + numpy, scipy, matplotlib, etc. 138 | - `(test)`: python 3.11 139 | - `(complex)`: same as `(base)` + numpy "beta version" + complicated package 140 | - etc. 141 | 142 | You can switch between environments with `mamba activate env_name` and leave the current environment with `mamba deactivate`. 143 | 144 | ```{important} 145 | 146 | When an environment is active, you can see it with the `(base)` or `(scipro)` indicator in front of the prompt. **In the 147 | active environment, ALL mamba commands refer to this specific environment**. 148 | 149 | For example, to list the packages available in `scipro`, you need to activate it first (`mamba activate scipro`) and 150 | *then* list the packages with `mamba list`. 151 | 152 | To open jupyterlab and have access the packages installed in `scipro`, activate the environment first and *then* start `jupyter-lab`. 153 | ``` 154 | 155 | When one of your environments becomes "broken" or obsolete, you can simply delete it with `mamba remove -n ENVNAME --all`. 156 | This will delete the corresponding folder and all packages in it. Creating, activating and deleting environments is super 157 | easy, and this is why I recommend their use. 158 | 159 | ```{admonition} Mamba/conda cheat sheet: 160 | :class: note 161 | 162 | - `mamba create -n scipro --clone base` : create an environment called "`scipro`" with the same packages in it as `base` 163 | - `mamba create -n scipro` : same as above, but empty 164 | - `mamba activate scipro` : activate the `scipro` environment 165 | - `mamba deactivate` : leave the current environment 166 | - `mamba info --envs` : get a list of all environments 167 | - `mamba list` : list the currently installed packages in a specific environment 168 | - `mamba remove -n scipro --all` : delete the scipro environment and all packages in it. 169 | ``` 170 | 171 | Visit the [conda documentation](https://conda.io/projects/conda/en/latest/user-guide/getting-started.html#managing-environments) for more commands 172 | (just replace all "`conda`" commands with "`mamba`"). 173 | 174 | ### Installing additional python packages in the active environment 175 | 176 | In the course of your studies, you will be lead to install many (many) python packages, for example [xarray](https://docs.xarray.dev) for gridded data analysis or [MetPy](https://unidata.github.io/MetPy) for meteorology. 177 | 178 | Almost always, the install procedure will be: 179 | 1. open the miniforge prompt 180 | 2. (optional but recommended) activate the environment where you want to install the package 181 | 3. install the package with `mamba install` 182 | 183 | For now, I ask you to install the following python packages: 184 | - [numpy](https://numpy.org), the fundamental package for scientific computing with Python 185 | - [scipy](https://scipy.org), fundamental algorithms for scientific computing in Python 186 | - [matplotlib](https://matplotlib.org), data visualization with Python 187 | 188 | To install these, activate the `scipro` environment (recommended) or use `base`, and type: 189 | 190 | ```none 191 | mamba install numpy scipy matplotlib 192 | ``` 193 | 194 | Answer "yes" to confirm the installation. Note that `mamba` will install several additional packages. These 195 | automatically installed packages are called "**dependencies**": they are required for the other packages 196 | to function properly. 197 | 198 | To test if the installation worked properly, open an ipython interpreter and type: 199 | 200 | ```python 201 | In [1]: import numpy as np 202 | In [2]: np.arange(1, 11, 2) 203 | ``` 204 | 205 | The output should be: 206 | 207 | ```python 208 | Out[2]: array([1, 3, 5, 7, 9]) 209 | ``` 210 | 211 | Congratulations! You are ready for the rest of the lecture. 212 | 213 | 214 | ## Learning checklist 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | -------------------------------------------------------------------------------- /book/week_02/02-ipython-spyder.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Scientific development environment: ipython and spyder" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Reminder: the \"standard\" python interpreter" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Let's open a python interpreter and type in some commands:\n", 22 | "\n", 23 | "```python\n", 24 | ">>> a = 2\n", 25 | ">>> b = 3\n", 26 | ">>> print(a+b)\n", 27 | "5\n", 28 | ">>> a*b\n", 29 | "6\n", 30 | ">>> a**3\n", 31 | "```" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "As it turns out, you will rarely use the python interpreter, but actually used a \"more advanced\" one: `ipython`" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "## ipython: interactive python " 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "The default command-line interpreter comes with some functionality (for example, if you use the up and down arrows you can navigate in your previous commands) but this functionality is very sparse. Some important functionalities are missing: some are cosmetic (like colors), some are very useful (like **automatic code completion** with ``[TAB]`` or the possibility to paste several lines of code at once). For all these reasons, the default python command-line interpreter is only used for very small tasks like testing a command or two. Scientists need more interactivity when analyzing their data: this is why ``ipython`` was created." 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "[ipython](http://ipython.readthedocs.io/en/stable/index.html) is a standard command-line interpreter, but enhanced with many interactive features. Let's open it (``ipython`` command) and type in some commands:\n", 60 | "\n", 61 | "```python\n", 62 | "In [1]: a = 2\n", 63 | "\n", 64 | "In [2]: b = 3\n", 65 | "\n", 66 | "In [3]: print(a + b)\n", 67 | "5\n", 68 | "\n", 69 | "In [4]: a*b\n", 70 | "Out[4]: 6\n", 71 | "```" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "What are the differences? The ```>>>``` prompt has been replaced by ``In [1]:``. Now there *is* a slight difference between a line with or without ``print()`` (can you find it?). At first sight the differences are small, but it's in the everyday interactive coding that ``ipython`` becomes useful. \n", 79 | "\n", 80 | "**From now on, we will always use the ipython command-line interpreter.** I will now describe a small sample of its most useful capabilities." 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "### Automatic code completion " 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "In ipython, type ``s`` and then ``[TAB]``. The tool will give you a list of suggestions to choose from (useful when you forgot how a function was called exactly!). Now type ``so`` and ``[TAB]``. Here the only solution will be selected automatically." 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "### Getting help" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "Type ``sorted?``. A text help should be printed, telling you what this function is about." 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "metadata": {}, 114 | "source": [ 115 | "### Copy paste " 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "Copy the following lines and paste them in your ipython interpreter:\n", 123 | "\n", 124 | "```python\n", 125 | "s = 'the jungle book'\n", 126 | "print(s)\n", 127 | "l = sorted(s)\n", 128 | "print(l)\n", 129 | "l = ''.join(l)\n", 130 | "print(l)\n", 131 | "```\n", 132 | "\n", 133 | "(note that ``[CTRL-V]`` doesn't work in a linux terminal: you'll have to use `[CTRL-SHIFT-V]`, the mouse with `right click -> paste`, or - even better - the magical linux middle mouse button)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "## Running scripts" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "### Reminder: running a python script from the terminal " 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "Python scripts are simple text files. Per convention we like to give them a suffix: ``.py``. You can run python scripts by calling python in a terminal:\n", 155 | "\n", 156 | "```\n", 157 | "$ python my_python_script.py\n", 158 | "```" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "### Running a python script from the ipython interpreter" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": {}, 171 | "source": [ 172 | "Running a script from the ipython interpreter is as easy as:" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": null, 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [ 181 | "%run my_python_script.py" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "(remember the ``[TAB]`` functionality in ipython? You can use it on files as well). Running a script this way is different from running a script in the terminal. Indeed, after running the script, the variables defined in it are still available in the current session. Try computing ``a + b`` to check it out:" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": null, 194 | "metadata": {}, 195 | "outputs": [], 196 | "source": [ 197 | "a + b" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "### Importing a python file " 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "metadata": {}, 210 | "source": [ 211 | "Another way to \"run\" a script in a python interpreter is via the ``import`` mechanism:" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": null, 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [ 220 | "import my_python_script" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "Although both ``%run`` and ``import`` execute the script (the output is printed on screen), the two mechanisms are different. As we are going to see, the ``import`` mechanism is an important feature of the python language (also something which you might not be used to from other languages). ``import`` is usually *not* used as a replacement for ``%run``. The biggest difference with ``%run`` is that the variables defined in the script are *not* available at the command line directly. Instead, they can now be addressed as following:" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": {}, 234 | "outputs": [], 235 | "source": [ 236 | "my_python_script.a * my_python_script.b" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "... but this is already going too far: more on this in the next lesson." 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "## Spyder " 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "" 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": {}, 263 | "source": [ 264 | "[Spyder](https://www.spyder-ide.org) is an open-source integrated development environment ([IDE](https://www.spyder-ide.org/)) for scientific pythom, and it is the best for beginners. To open spyder, simply open your conda environment and type:\n", 265 | "\n", 266 | "```\n", 267 | "spyder\n", 268 | "```\n", 269 | "\n", 270 | "This should open a window like the above. \n", 271 | "\n", 272 | "I think that you will learn spyder fast enough while coding, but if you want some tips to get started, the [official documentation](https://docs.spyder-ide.org/current/videos/first-steps-with-spyder.html) is a good place to go first! " 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "## Learning checklist" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "\n", 287 | " \n", 288 | " " 289 | ] 290 | } 291 | ], 292 | "metadata": { 293 | "hide_input": false, 294 | "kernelspec": { 295 | "display_name": "Python 3 (ipykernel)", 296 | "language": "python", 297 | "name": "python3" 298 | }, 299 | "language_info": { 300 | "codemirror_mode": { 301 | "name": "ipython", 302 | "version": 3 303 | }, 304 | "file_extension": ".py", 305 | "mimetype": "text/x-python", 306 | "name": "python", 307 | "nbconvert_exporter": "python", 308 | "pygments_lexer": "ipython3", 309 | "version": "3.10.6" 310 | }, 311 | "latex_envs": { 312 | "LaTeX_envs_menu_present": true, 313 | "autoclose": false, 314 | "autocomplete": true, 315 | "bibliofile": "biblio.bib", 316 | "cite_by": "apalike", 317 | "current_citInitial": 1, 318 | "eqLabelWithNumbers": true, 319 | "eqNumInitial": 1, 320 | "hotkeys": { 321 | "equation": "Ctrl-E", 322 | "itemize": "Ctrl-I" 323 | }, 324 | "labels_anchors": false, 325 | "latex_user_defs": false, 326 | "report_style_numbering": false, 327 | "user_envs_cfg": false 328 | }, 329 | "nbTranslate": { 330 | "displayLangs": [ 331 | "*" 332 | ], 333 | "hotkey": "alt-t", 334 | "langInMainMenu": true, 335 | "sourceLang": "en", 336 | "targetLang": "fr", 337 | "useGoogleTranslate": true 338 | }, 339 | "toc": { 340 | "base_numbering": 3, 341 | "nav_menu": {}, 342 | "number_sections": true, 343 | "sideBar": true, 344 | "skip_h1_title": false, 345 | "title_cell": "Table of Contents", 346 | "title_sidebar": "Contents", 347 | "toc_cell": true, 348 | "toc_position": {}, 349 | "toc_section_display": "block", 350 | "toc_window_display": true 351 | } 352 | }, 353 | "nbformat": 4, 354 | "nbformat_minor": 4 355 | } 356 | -------------------------------------------------------------------------------- /book/week_02/Assignment-02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Assignment #02" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Today's exercises have to be done with help of the python **standard library** alone! **No external module can be used**.\n", 15 | "\n", 16 | ":::{tip}\n", 17 | "Can you remember what the \"standard library\" is? [Here](https://docs.python.org/3/library/) is a list of all the modules and built-ins it provides: so quite a lot!\n", 18 | ":::" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "## #02-01: cryptography for Roman Emperors" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "A very simple encryption technique is called the [Caesar cipher](http://en.wikipedia.org/wiki/Caesar_cipher). The basic idea is that each letter is replaced by a letter that is a certain number of letters away, so for example if the shift was 2, then A would become C, B would become D, and Z will become B.\n", 33 | "\n", 34 | "**A. Write a function that given a string and a shift, will produce the encrypted string for that shift**. The rules are:\n", 35 | "- you should accept lowercase and uppercase letters, and return letters of the same case\n", 36 | "- spaces or other punctuation characters should not be changed.\n", 37 | "\n", 38 | "There are several ways to reach this result. Just pick the one which makes more sense to you. Then, decrypt the following message, which was encrypted with a shift of 13:\n", 39 | " \n", 40 | " Pbatenghyngvbaf, lbh unir fhpprrqrq va qrpelcgvat gur fgevat.\n", 41 | " \n", 42 | "**B. Now write a decoding script** `caesar.py` which, when run in the command line, prints the decoded phrase after asking the user to enter a phrase and a shift with which it was encrypted. Test your script on the sentence above. \n", 43 | "\n", 44 | ":::{tip}\n", 45 | "Make use of python's [input()](https://docs.python.org/3/library/functions.html#input) function\n", 46 | ":::\n", 47 | "\n", 48 | "**C. Now try to decrypt this sentence, for which the shift is unknown**:\n", 49 | " \n", 50 | " Gwc uivioml bw nqvl bpm zqopb apqnb.\n", 51 | " \n", 52 | "One way to solve this problem involves human decision, but another way can be fully automated (and implies more work). Pick the one you want! You can also add the decryption option to your script if you want." 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "## #02-02: the substitution cipher" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "Since we managed to crack the code above so easily, maybe we should use a better cryptographic algorithm from now on. \n", 67 | "\n", 68 | "The Caesar cipher is a special case of the more general [substitution cipher](https://en.wikipedia.org/wiki/Substitution_cipher). In this cipher, any new combined alphabet (or sequence of bits, characters, numbers...) can be used in place of the regular alphabet.\n", 69 | "\n", 70 | "To simplify things a bit, we will use python's `random` module and in particular it's [shuffle](https://docs.python.org/3/library/random.html#random.shuffle) function to do the job. To generate a random (but predictable) substitution table we can use the mechanism of \"seeds\", which can be seen as a secret key that you'll have to (privately) give to your forbidden lover for him/her to decipher your message. This works as following: " 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "alphabet = 'abcdefghijklmnopqrstuvwxyz'\n", 80 | "seed = 42 # the secret key known by both parties \n", 81 | "\n", 82 | "import random\n", 83 | "random.seed(seed)\n", 84 | "\n", 85 | "new_alphabet = list(alphabet)\n", 86 | "random.shuffle(new_alphabet)\n", 87 | "print(new_alphabet)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "**A. Building upon the template from the previous exercise, create a script `substitute.py` which now asks:**\n", 95 | " 1. for a phrase to encrypt or decrypt\n", 96 | " 2. whether the script should Encrypt (E) or Decrypt (D) the phrase\n", 97 | " 3. which seed should be used (it should always be an integer, but can be any integer)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "**B. Since your cipher still preserves the location of punctuation (space, points, exclamation marks, etc.), it is easier to crack. Can you think of a very simple way to substitute these characters as well for better encoding?** (minimal change in code)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [] 113 | } 114 | ], 115 | "metadata": { 116 | "hide_input": false, 117 | "kernelspec": { 118 | "display_name": "Python 3 (ipykernel)", 119 | "language": "python", 120 | "name": "python3" 121 | }, 122 | "language_info": { 123 | "codemirror_mode": { 124 | "name": "ipython", 125 | "version": 3 126 | }, 127 | "file_extension": ".py", 128 | "mimetype": "text/x-python", 129 | "name": "python", 130 | "nbconvert_exporter": "python", 131 | "pygments_lexer": "ipython3", 132 | "version": "3.10.6" 133 | }, 134 | "latex_envs": { 135 | "LaTeX_envs_menu_present": true, 136 | "autoclose": false, 137 | "autocomplete": true, 138 | "bibliofile": "biblio.bib", 139 | "cite_by": "apalike", 140 | "current_citInitial": 1, 141 | "eqLabelWithNumbers": true, 142 | "eqNumInitial": 1, 143 | "hotkeys": { 144 | "equation": "Ctrl-E", 145 | "itemize": "Ctrl-I" 146 | }, 147 | "labels_anchors": false, 148 | "latex_user_defs": false, 149 | "report_style_numbering": false, 150 | "user_envs_cfg": false 151 | }, 152 | "nbTranslate": { 153 | "displayLangs": [ 154 | "*" 155 | ], 156 | "hotkey": "alt-t", 157 | "langInMainMenu": true, 158 | "sourceLang": "en", 159 | "targetLang": "fr", 160 | "useGoogleTranslate": true 161 | }, 162 | "toc": { 163 | "base_numbering": 1, 164 | "nav_menu": {}, 165 | "number_sections": false, 166 | "sideBar": true, 167 | "skip_h1_title": false, 168 | "title_cell": "Table of Contents", 169 | "title_sidebar": "Contents", 170 | "toc_cell": false, 171 | "toc_position": {}, 172 | "toc_section_display": "block", 173 | "toc_window_display": false 174 | } 175 | }, 176 | "nbformat": 4, 177 | "nbformat_minor": 4 178 | } 179 | -------------------------------------------------------------------------------- /book/week_02/my_python_script.py: -------------------------------------------------------------------------------- 1 | a = 3 2 | b = 6 3 | c = a * b 4 | print('The product of {} and {} is {}'.format(a, b, c)) 5 | -------------------------------------------------------------------------------- /book/week_03/02-Strings-Paths.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# String formatting and file paths" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "A scientific data analysis workflow almost invariably implies downloading, manipulating and opening files. Often, it also implies writing new files (for example with post-processed data). Fortunately, python comes with many handy tools to format strings." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## String Formatting" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "\"String formatting\" refers to formatting the content of variables (strings, numbers, paths, etc.) into strings, for example to display them on screen or to write them to a text file. Unfortunately, there are more than one way to format strings in python (actually there are at least 4!). This short section will guide you to the ones you should preferably use." 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "### The modern way: formatted string (\"f-string\") literals" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "Consider the following example:" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "name = 'Assane'\n", 52 | "print(f'Hello {name}!')" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "The important bits here is the **`f` prefix** to the string literal which indicates to the interpreter that the string might contain **curly braces**, which contain variable names that will be replaced with their values.\n", 60 | "\n", 61 | "Not only strings can be formatted into a string. Numbers can too:" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": null, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "n = 3\n", 71 | "print(f'{name} has {n} cats.')" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "pi = 3.14 \n", 81 | "print(f'pi ≈ {pi}')" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "f-strings are quite powerful. They can evaluate arbitrary expressions:" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "print(f'2 pi ≈ {2 * pi}')" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "This feature is to be used with care, you probably don't want to have very complicated expressions within your f-strings!" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "### Formatting numbers in strings" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "Very often, you want your strings to be of predictable length and format. For example, you may want float numbers to be printed only with a chosen precision:" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "frac = 2 / 3\n", 128 | "print(f'Set free: {frac}')\n", 129 | "print(f'Formatted to 2 decimals: {frac:.2f}') # f is for \"float\"\n", 130 | "print(f'Formatted to an integer: {int(frac)}')\n", 131 | "print(f'Formatted to a rounded integer: {round(frac)}')\n", 132 | "print(f'Formatted to a rounded integer with leading spaces: {round(frac):4d}') # d is for \"int\"\n", 133 | "print(f'Formatted to a rounded integer with leading zeros: {round(frac):04d}')" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "### Formatting dates in strings " 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "Also possible:" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [ 156 | "import datetime\n", 157 | "now = datetime.datetime.now()\n", 158 | "\n", 159 | "print(now)\n", 160 | "print(f'{now:%Y-%m-%d %H:%M}')" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "### The old ways to format strings" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": {}, 173 | "source": [ 174 | "f-strings have been added to python in version 3.6 (end of 2016: that's only a few years back!). Before that, two other string formatting tools were available and are still used today. Therefore, you should learn them as well." 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "#### The \"not too old\" way: `.format()` (still useful!)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "This string formatting method was introduced with python 3 (so that's already a bit older). It works with appending a call to the `.format()` method to a string:" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": null, 194 | "metadata": {}, 195 | "outputs": [], 196 | "source": [ 197 | "print('Hello, {}!'.format(name))" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "No `f` prefix here (don't mix them up!). Otherwise, it works more or less the same:" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": null, 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "print('{} has {} cats.'.format(name, n))\n", 214 | "print('{name_of_person} has {n_cats:02d} cats, right?'.format(n_cats=n, name_of_person=name))" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "You see where this is going! This is pretty much the same syntax, but a bit more verbose. f-strings are generally more readable and should be preferred, but the `.format()` can be useful in very specific cases, as shown in the `greetings` example from the previous lesson which I adapt here: " 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": null, 227 | "metadata": {}, 228 | "outputs": [], 229 | "source": [ 230 | "template_string = 'Hey {name}! I think you should come over to {city} and visit {place}.'\n", 231 | "# some code ommitted here...\n", 232 | "print(template_string.format(name='Lesedi', city='Cape Town', place='Table Mountain'))" 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "The nice thing in the example above is that you can create a template string early in your script, that you can \"fill\" with values later in your program." 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "#### The very old way: the `%` operator" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "This was the standard in python 2 but is still working today (and will continue to work in the future):" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": null, 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [ 262 | "print('Hello %s!' % name)" 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "metadata": {}, 268 | "source": [ 269 | "We don't like this way, but you may encounter it in some older code." 270 | ] 271 | }, 272 | { 273 | "cell_type": "markdown", 274 | "metadata": {}, 275 | "source": [ 276 | "### Take home: string formatting " 277 | ] 278 | }, 279 | { 280 | "cell_type": "markdown", 281 | "metadata": {}, 282 | "source": [ 283 | "- there is probably *no string format* you can think of that doesn't have a formatting solution (people are manipulating strings all the time)\n", 284 | "- use f-strings! They are great.\n", 285 | "- sometimes, use `.format()`. This is also great.\n", 286 | "- if you want more examples, read [this tutorial from the python docs](https://docs.python.org/3/tutorial/inputoutput.html)" 287 | ] 288 | }, 289 | { 290 | "cell_type": "markdown", 291 | "metadata": {}, 292 | "source": [ 293 | "## Path handling in python" 294 | ] 295 | }, 296 | { 297 | "cell_type": "markdown", 298 | "metadata": {}, 299 | "source": [ 300 | "Handling file paths is one of the not-so-fun parts of a scientist's job, I agree. But we just have to do it, there is no way around it.\n", 301 | "\n", 302 | "Please read this [short blog post](https://medium.com/@ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f) which is a good entry-level tutorial to path handling." 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "### Take home: path handling" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": {}, 315 | "source": [ 316 | "- never use `+` and other string shenanigans to handle your file paths\n", 317 | "- [os.path](https://docs.python.org/3/library/os.path.html) and, in particular, `os.path.join` is a simple way to deal with paths as strings\n", 318 | "- [pathlib](https://docs.python.org/3/library/pathlib.html) is the new cool kid in the block. It works very well but might be a bit confusing at first." 319 | ] 320 | } 321 | ], 322 | "metadata": { 323 | "hide_input": false, 324 | "kernelspec": { 325 | "display_name": "Python 3 (ipykernel)", 326 | "language": "python", 327 | "name": "python3" 328 | }, 329 | "language_info": { 330 | "codemirror_mode": { 331 | "name": "ipython", 332 | "version": 3 333 | }, 334 | "file_extension": ".py", 335 | "mimetype": "text/x-python", 336 | "name": "python", 337 | "nbconvert_exporter": "python", 338 | "pygments_lexer": "ipython3", 339 | "version": "3.10.6" 340 | }, 341 | "latex_envs": { 342 | "LaTeX_envs_menu_present": true, 343 | "autoclose": false, 344 | "autocomplete": true, 345 | "bibliofile": "biblio.bib", 346 | "cite_by": "apalike", 347 | "current_citInitial": 1, 348 | "eqLabelWithNumbers": true, 349 | "eqNumInitial": 1, 350 | "hotkeys": { 351 | "equation": "Ctrl-E", 352 | "itemize": "Ctrl-I" 353 | }, 354 | "labels_anchors": false, 355 | "latex_user_defs": false, 356 | "report_style_numbering": false, 357 | "user_envs_cfg": false 358 | }, 359 | "nbTranslate": { 360 | "displayLangs": [ 361 | "*" 362 | ], 363 | "hotkey": "alt-t", 364 | "langInMainMenu": true, 365 | "sourceLang": "en", 366 | "targetLang": "fr", 367 | "useGoogleTranslate": true 368 | }, 369 | "toc": { 370 | "base_numbering": "8", 371 | "nav_menu": {}, 372 | "number_sections": true, 373 | "sideBar": true, 374 | "skip_h1_title": false, 375 | "title_cell": "Table of Contents", 376 | "title_sidebar": "Contents", 377 | "toc_cell": true, 378 | "toc_position": { 379 | "height": "calc(100% - 180px)", 380 | "left": "10px", 381 | "top": "150px", 382 | "width": "384px" 383 | }, 384 | "toc_section_display": "block", 385 | "toc_window_display": true 386 | } 387 | }, 388 | "nbformat": 4, 389 | "nbformat_minor": 4 390 | } 391 | -------------------------------------------------------------------------------- /book/week_03/03-Tips-and-Tricks.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Tips and tricks" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "It is very early in the lecture, but you are now already well equipped to write more complex scripts and algorithms: you know (almost ;)) everything you need to know about the syntax, but you might be a little lost in all the possibilities that the python language is now offering to you. *Which tool should I use to do this and that? How was this function signature again?* are very common interrogations, and it is normal to feel a bit overwhelmed.\n", 15 | "\n", 16 | "This chapter will give you some brief tips to help you during your assignments." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## Batteries included " 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "You may have read this expression (\"batteries included\") during your research about the Python language. It is widely used to indicate that if you choose python, it's not only for its syntax: it's for all the tools that come with it. And there are PLENTY. Spend a minute or two to scroll through the list of tools available in the [standard library](https://docs.python.org/3/library/index.html). The **standard library** is available per default with any python installation. It is very robust and provides the core functionality of the language.\n", 31 | "\n", 32 | "As you will see, there is not much for us scientists in this list though (no plotting library for example). This is why **third party packages** were developed. And there are so many of them that one can easily be overwhelmed: for this week's assignment we will continue to use the standard library only, but starting next week we will use external packages as well." 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "## Getting help" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "In order to become a good programmer, **nothing is more important than being able to find help**, either online or with your friends and colleagues." 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "### Online resources " 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "Fortunately, getting help has become quite easy nowadays. Here are some resources to get you started:\n", 61 | "- the [official python documentation](https://docs.python.org/3/) is definitely the best place to look for everything related to the standard library. It is also THE reference, and as such can sometimes be too detailed or complex. If this fails, then you'll have to refer to \n", 62 | "- **your favorite search engine**. Ask your question as precisely as possible! With some experience you'll learn to detect the sources you can trust, and learn to filter out outdated information (e.g. related to the old python 2 language). Very often your web search will lead you to\n", 63 | "- [Stackoverflow](https://stackoverflow.com/). In recent years, the Stack Exchange community of sites has emerged as a major resource for answering technical and other questions and is even the preferred forum for many open-source projects. Their system of rewards for well formulated questions and correct answers clearly makes finding the right solution easier than in many other forums." 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "Here is a general rule. When you encounter a problem, ask yourself the following question: **is it likely that someone else had this problem before me?** In a large majority of the cases, the answer will be \"yes\" (believe me on this one). The trick is to find the answer to your question: use as few words as possible, but put in all the keywords, that makes your question unambiguous. Here is the example outcome of two google searches:\n", 71 | "- [how to download a file python](https://www.google.at/search?q=how+to+download+a+file+python)\n", 72 | "- [how to download a file python3](https://www.google.at/search?q=how+to+download+a+file+python3)\n", 73 | "\n", 74 | "The latter is the one you want and will lead you to the right answer. With time, you are going to get used to using the search engine more efficiently, but if everything else fails, then you might have to ask a question yourself:" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "### Ask questions the smart way" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "If you want to ask a question on stackoverflow or somewhere else (e.g. by sending an email to your teacher), please spend some time reading [this resource](http://www.catb.org/esr/faqs/smart-questions.html). Believe me, this will help you to get better answers!" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "### Getting help VS learning " 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "I'll be honest: without an internet connection, I am a very bad python programmer. I don't expect you to learn all the python commands by heart for the exam (yes, you will have an internet access during the exam). Similarly, I encourage you to use the internet to find help to solve the weekly exercises.\n", 103 | "\n", 104 | "However, there is a difference between \"getting help to find how to realize a specific task\" and \"asking for someone to do my homework for me\". The border line between the two might be subtle, but I'm sure you'll know when you cross it: at the moment you stopped using your brain and simply copy/pasted code from one site to your assignment script, you crossed it." 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "## Python IDEs\n", 112 | "" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "While it is possible to write python programs using a basic text editor, the majority of you will find it useful to use an [integrated development environment](https://en.wikipedia.org/wiki/Integrated_development_environment) (short: IDE) instead. The advantages of IDEs is that they ship with tools that make your life easier when programming. \n", 120 | "\n", 121 | "Here is a non-exhaustive (and subjective) list of the tools a good IDE should have:\n", 122 | "- linter: automatic detection of syntax errors and style violations\n", 123 | "- [debugger](https://en.wikipedia.org/wiki/Debugger): helping you find where the errors in your program come from\n", 124 | "- [intelligent code completion](https://en.wikipedia.org/wiki/Intelligent_code_completion): automatic `TAB` completion, module recognition, etc.\n", 125 | "- project explorer and code exploration tools\n", 126 | "- for interactive languages: a command line and variable explorer\n", 127 | "- appealing colors and graphics\n", 128 | "- ...\n", 129 | "\n", 130 | "For more advanced uses, you will also look for version control tools and automated build and testing tools.\n", 131 | "\n", 132 | "There are many Python IDEs around, but here are some of the ones that come up most often as recommendations on forums or blogposts (they are all free):\n", 133 | "- [Spyder](https://www.spyder-ide.org): the IDE made by and for scientists. It resembles the Matlab IDE and has many nice features such as variable exploration and an integrated ipython console. It is very easy to install with conda, and is therefore available on linux at the university (``$ spyder &``). **It is my recommended IDE for this semester's course**.\n", 134 | "- [PyCharm](https://www.jetbrains.com/pycharm/): this is my current personal favorite. It is however quite heavyweight and can be confusing for beginners. The community version of PyCharm is free, and has enough functionality for most use cases.\n", 135 | "- [Visual Studio Code](https://code.visualstudio.com/) (from Microsoft) is one of the newest \"cool kids\" around. I have not tested it myself, but people seem to be very happy with it. It's very minimal, and you'll have to install plugins for it to become a true python IDE.\n", 136 | "- [vim](https://www.vim.org/) or [emacs](https://www.gnu.org/software/emacs/): for people allergic to the mouse. Not recommended for most of us, but the people who learned to use them are (allegedly) the fastest programmers ever." 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "## Debugging code" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "**It is impossible to write bug free code in one go** (just accept it and get used to it). There will always be mistakes. Syntax errors and other obvious problems (e.g. `NameError`) will most often be picked up by your IDE's linter. The other \"easier ones\" (e.g. `TypeError`) are also likely to be corrected after a first couple of test runs of your code. But then, sometimes, the error is not obvious: the code runs, but doesn't produce the expected output. What to do in these cases? " 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "- use `print` statements (bad): with this method, you can inspect variables at run time by printing their values. It is a quick and dirty way to debug, and should not be used too often because error prone (indeed, by adding print statements you modify your code, might forget about them afterwards, and they are often not very useful). If you need more than 3 or 4 print statements to track the source of an issue, it's time for opening a debugger.\n", 158 | "- **use a debugger** (good): a [debugger](https://en.wikipedia.org/wiki/Debugger) will allow to interrupt a program's execution and *give you access to the program's state* at a predefined location (called a **break point**). It is extremely more useful than `print`, because it allows you to explore many variables at once. Spyder comes with a debugger [included](https://docs.spyder-ide.org/5/panes/debugging.html), and we will make a short demo in class.\n", 159 | "- go for a walk (very good): think about something else for a while and come back with a fresh, open mind\n", 160 | "- talk to your colleagues about your problem (very good): they might see the issue (or typo) from a different angle and detect the problem easily\n", 161 | "- if you have no one around to talk to, [talk to a duck](https://en.m.wikipedia.org/wiki/Rubber_duck_debugging) (very good)" 162 | ] 163 | } 164 | ], 165 | "metadata": { 166 | "hide_input": false, 167 | "kernelspec": { 168 | "display_name": "Python 3 (ipykernel)", 169 | "language": "python", 170 | "name": "python3" 171 | }, 172 | "language_info": { 173 | "codemirror_mode": { 174 | "name": "ipython", 175 | "version": 3 176 | }, 177 | "file_extension": ".py", 178 | "mimetype": "text/x-python", 179 | "name": "python", 180 | "nbconvert_exporter": "python", 181 | "pygments_lexer": "ipython3", 182 | "version": "3.10.6" 183 | }, 184 | "latex_envs": { 185 | "LaTeX_envs_menu_present": true, 186 | "autoclose": false, 187 | "autocomplete": true, 188 | "bibliofile": "biblio.bib", 189 | "cite_by": "apalike", 190 | "current_citInitial": 1, 191 | "eqLabelWithNumbers": true, 192 | "eqNumInitial": 1, 193 | "hotkeys": { 194 | "equation": "Ctrl-E", 195 | "itemize": "Ctrl-I" 196 | }, 197 | "labels_anchors": false, 198 | "latex_user_defs": false, 199 | "report_style_numbering": false, 200 | "user_envs_cfg": false 201 | }, 202 | "nbTranslate": { 203 | "displayLangs": [ 204 | "*" 205 | ], 206 | "hotkey": "alt-t", 207 | "langInMainMenu": true, 208 | "sourceLang": "en", 209 | "targetLang": "fr", 210 | "useGoogleTranslate": true 211 | }, 212 | "toc": { 213 | "base_numbering": "8", 214 | "nav_menu": {}, 215 | "number_sections": true, 216 | "sideBar": true, 217 | "skip_h1_title": false, 218 | "title_cell": "Table of Contents", 219 | "title_sidebar": "Contents", 220 | "toc_cell": true, 221 | "toc_position": { 222 | "height": "calc(100% - 180px)", 223 | "left": "10px", 224 | "top": "150px", 225 | "width": "384px" 226 | }, 227 | "toc_section_display": "block", 228 | "toc_window_display": true 229 | } 230 | }, 231 | "nbformat": 4, 232 | "nbformat_minor": 4 233 | } 234 | -------------------------------------------------------------------------------- /book/week_03/04-Good-practices.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Good practices, programming style and conventions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Python is a very versatile programming language, and it has a very flexible syntax. Therefore, it is also very easy to write *ugly code* in python. This chapter reviews a number of guidelines and conventions widely accepted by the community. At this stage of the lecture we won't go in depth for each of these points, but try to keep them in mind while writing your assignments and also while checking my suggested solution to the assignments." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## Use PEP 8" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "[PEP 8](https://www.python.org/dev/peps/pep-0008/) is a style guide for python code. It introduces new rules where the syntax leaves them open. As an example:" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "# Syntax OK but not PEP8 compliant \n", 38 | "x=2+1\n", 39 | "x= x * 2-1\n", 40 | "# Syntax OK *and* PEP8 compliant\n", 41 | "x = 2 + 1\n", 42 | "x = x * 2 - 1" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "PEP8 gives *guidelines*, not strict rules. It is your choice to comply with them or not. As a matter of fact however, many open source projects have adopted PEP8 and require to use it if you want to contribute. If you want to use it too, a good choice is to turn on PEP8 checking in your favorite IDE (in Spyder: ``Tools -> Preferences -> Completion and linting -> Code style -> `` Tick ``Enable code style linting`` for PEP8 style analysis)." 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "## Give meaningful names to variables and functions" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "The text space that code is taking is less important than your time. Give meaningful name to your variables and functions, even if it makes them quite long! For example, prefer:" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "```python\n", 71 | "def fahrenheit_to_celsius(temp_fahrenheit):\n", 72 | "```" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "to the shorter but less explicit:" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "```python\n", 87 | "def f2c(tf):\n", 88 | "```" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "The same rule applies for variables. Prefer `language` over `l` and `area` over `a`." 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "## Prefer writing many small functions instead of monolithic code blocks" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "[Separation of concerns](https://en.wikipedia.org/wiki/Separation_of_concerns) is an important design principle for separating a computer program into distinct sections, such that each section addresses a separate concern. This increases the code readability and facilitates unit testing (as we will see).\n", 110 | "\n", 111 | "For example, a scientific script can often be organized in well defined steps:\n", 112 | "- data input (read a file)\n", 113 | "- data pre-processing (filtering missing data, discarding useless variables)\n", 114 | "- data processing (actual computation)\n", 115 | "- output (writing the processed data to a file for later use)\n", 116 | "- data visualization (producing a plot)\n", 117 | "\n", 118 | "Each of these steps and sub-steps should be separated in different functions, maybe even in different scripts. When discussing your assignment solutions, we will always discuss how your code is organized for readability and testing." 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "## Document your code " 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "Write comments in your code, and document the functions with docstrings. We will have a full lecture about it, but it's good to start early. Here again, there are [some conventions](https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard) that I recommend to follow.\n", 133 | "\n", 134 | "Note that this is useful even if you don't plan to share your code. At the very last there is at least one person reading your code: yourself! And it's always a good idea to be nice to yourself! (see the related [Abstruse Goose comic](http://abstrusegoose.com/432))" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "## The Zen of Python " 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "import this" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [] 159 | } 160 | ], 161 | "metadata": { 162 | "hide_input": false, 163 | "kernelspec": { 164 | "display_name": "Python 3 (ipykernel)", 165 | "language": "python", 166 | "name": "python3" 167 | }, 168 | "language_info": { 169 | "codemirror_mode": { 170 | "name": "ipython", 171 | "version": 3 172 | }, 173 | "file_extension": ".py", 174 | "mimetype": "text/x-python", 175 | "name": "python", 176 | "nbconvert_exporter": "python", 177 | "pygments_lexer": "ipython3", 178 | "version": "3.10.6" 179 | }, 180 | "latex_envs": { 181 | "LaTeX_envs_menu_present": true, 182 | "autoclose": false, 183 | "autocomplete": true, 184 | "bibliofile": "biblio.bib", 185 | "cite_by": "apalike", 186 | "current_citInitial": 1, 187 | "eqLabelWithNumbers": true, 188 | "eqNumInitial": 1, 189 | "hotkeys": { 190 | "equation": "Ctrl-E", 191 | "itemize": "Ctrl-I" 192 | }, 193 | "labels_anchors": false, 194 | "latex_user_defs": false, 195 | "report_style_numbering": false, 196 | "user_envs_cfg": false 197 | }, 198 | "nbTranslate": { 199 | "displayLangs": [ 200 | "*" 201 | ], 202 | "hotkey": "alt-t", 203 | "langInMainMenu": true, 204 | "sourceLang": "en", 205 | "targetLang": "fr", 206 | "useGoogleTranslate": true 207 | }, 208 | "toc": { 209 | "base_numbering": "9", 210 | "nav_menu": {}, 211 | "number_sections": true, 212 | "sideBar": true, 213 | "skip_h1_title": false, 214 | "title_cell": "Table of Contents", 215 | "title_sidebar": "Contents", 216 | "toc_cell": true, 217 | "toc_position": { 218 | "height": "calc(100% - 180px)", 219 | "left": "10px", 220 | "top": "150px", 221 | "width": "384px" 222 | }, 223 | "toc_section_display": "block", 224 | "toc_window_display": true 225 | } 226 | }, 227 | "nbformat": 4, 228 | "nbformat_minor": 4 229 | } 230 | -------------------------------------------------------------------------------- /book/week_03/Assignment-03.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Assignment #03" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Today's exercises have to be done with help of the python standard library alone! No external module can be used (except for the `--plot` option where you can use the helper packages)." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## Exercise #03-01: automated data download" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "hidePrompt": true 28 | }, 29 | "source": [ 30 | "[SRTM](https://en.wikipedia.org/wiki/Shuttle_Radar_Topography_Mission) is a digital elevation model (DEM) at ~90 m resolution covering almost the entire globe (up to $\\pm$ 60° latitude). The data is organized in 5°x5° tiles. To see a map of the tiles have a look at [this download page](http://srtm.csi.cgiar.org/srtmdata/). This tool is nice to use if you know which tile you want, but not very useful if you want more than one tile at different places of the globe.\n", 31 | "\n", 32 | "Fortunately, the entire dataset is available on this server: `srtm.csi.cgiar.org/wp-content/uploads/files/srtm_5x5/TIFF/`\n", 33 | "\n", 34 | "In order to protect from disruptive \"download all\" behavior, the files stored here cannot be listed (i.e. automated data scraping won't work). Fortunately for us, the file naming convention is very simple:\n", 35 | "- example file: [http://srtm.csi.cgiar.org/wp-content/uploads/files/srtm_5x5/TIFF/srtm_39_04.zip](http://srtm.csi.cgiar.org/wp-content/uploads/files/srtm_5x5/TIFF/srtm_39_04.zip)\n", 36 | "- the first two digits give the location of the tile in the longitudes (starting at 180° West)\n", 37 | "- the last two digits give the location of the tile in the latitudes (starting at 60° North and going southwards)\n", 38 | "\n", 39 | "Here are some examples of locations and their associated tile:\n", 40 | "- (-179, 59) -> 'srtm_01_01.zip'\n", 41 | "- (-179, 51) -> 'srtm_01_02.zip'\n", 42 | "- (-174, 54) -> 'srtm_02_02.zip'\n", 43 | "- (-180, 60) -> 'srtm_01_01.zip' (upper-left corner case)\n", 44 | "\n", 45 | "And so forth.\n", 46 | "\n", 47 | "**A. Write a script which, given a longitude and a latitude as arguments, downloads the corresponding file** in the current directory. The tool should print an error message when the given location is not valid and exit the script. \n", 48 | "\n", 49 | "*Hint A1: define \"valid\" locations first: some are easy to catch, some cannot be caught automatically. Do we really have to deal with those?*\n", 50 | "\n", 51 | "*Hint A2: unlike last week where we asked for user input, here I'm asking for a script with [command line arguments](https://docs.python.org/3/tutorial/stdlib.html?highlight=sys%20argv#command-line-arguments). The command `%run download_srtm.py 9 44` (in an ipython interpreter) should download the file at the location 9°E, 44°N.*\n", 52 | "\n", 53 | "*Hint A3: although I agree with the python documentation that `argparse` is more robust (and much better) than parsing `sys.argv` yourself, for the purpose of learning I would like you to use `sys.argv` today and handle the command line arguments yourselves.*\n", 54 | "\n", 55 | "**B.** Extend this script to be a bit more clever: **download the data file only if the file isn't already available in the current directory**. This is particularly useful because the server is not very fast. \n", 56 | "\n", 57 | "**C.** Extend this script to be even more clever: **given a range of longitudes and latitudes, it should download all the files covering this area**. For example, the range 9°E to 18°E and 44°N to 47°N would download 6 files. **In order to avoid manual mistakes, warn the user by announcing the number of files that the command is going to download, and ask for confirmation before going on** (by using the `input()` function).\n", 58 | "\n", 59 | "*Hint C1: the command should now be `%run download_srtm.py 9 44 18 47`.*\n", 60 | "\n", 61 | "**D.** Let's add even more functionality to our script:\n", 62 | "\n", 63 | "**D1.** **print a message to the user announcing if the file was downloaded or was already on disk**\n", 64 | "\n", 65 | "**D2.** **print the size of the file after download** - choose [an appropriate unit](https://wiki.ubuntu.com/UnitsPolicy) for the text\n", 66 | "\n", 67 | "**D3.** let's assume that the user actually wants to download the files in a specific folder instead of the current directory (i.e. a folder where she stores all her DEMs). Let's give this opportunity to her by adding two options to the script:\n", 68 | "- `--output-dir /path/to/a/dir` should **write the file (and check for its existence) in the directory given as argument**\n", 69 | "- if the target directory does not exist, print a message to the screen indicating that you can't continue and exit the script\n", 70 | "\n", 71 | "**D4.** (optional: see the warning about windows below) **if the user asks for a plot, write a plot to disk** (alongside the DEM file) with the file name `srtm_39_04.png` (if the tile 39, 4 was asked for). The user may ask for a plot by adding a `--plot` to the list of arguments. I.e. `%run download_srtm.py 9 44 18 47 --plot` would write 6 plots in the user-chosen default directory. \n", 72 | "\n", 73 | "Here is a template for your plotting function:\n", 74 | "\n", 75 | "```python\n", 76 | "import matplotlib.pyplot as plt\n", 77 | "import rioxarray as rioxr\n", 78 | "\n", 79 | "# This is some real magic: we open the file from within the zip\n", 80 | "f = 'zip://srtm_39_04.zip!srtm_39_04.tif'\n", 81 | "\n", 82 | "# Read the data and plot\n", 83 | "with rioxr.open_rasterio(f) as da:\n", 84 | " da.sel(band=1).plot.imshow(cmap='terrain', vmin=0)\n", 85 | " plt.ylabel('Lat (°)'); plt.xlabel('Lon (°)'); plt.title('srtm_39_04'); \n", 86 | "plt.savefig('srtm_39_04.png')\n", 87 | "plt.close()\n", 88 | "```\n", 89 | "\n", 90 | "No need to add more features to the plot than that: we haven't talked about these libraries yet! Just make sure that the filename is set to the correct value.\n", 91 | "\n", 92 | "```{tip}\n", 93 | "You'll need the matplotlib, xarray and rioxarray libraries installed for this to work. Remember how to install libraries?\n", 94 | "```" 95 | ] 96 | } 97 | ], 98 | "metadata": { 99 | "hide_input": false, 100 | "kernelspec": { 101 | "display_name": "Python 3 (ipykernel)", 102 | "language": "python", 103 | "name": "python3" 104 | }, 105 | "language_info": { 106 | "codemirror_mode": { 107 | "name": "ipython", 108 | "version": 3 109 | }, 110 | "file_extension": ".py", 111 | "mimetype": "text/x-python", 112 | "name": "python", 113 | "nbconvert_exporter": "python", 114 | "pygments_lexer": "ipython3", 115 | "version": "3.10.6" 116 | }, 117 | "latex_envs": { 118 | "LaTeX_envs_menu_present": true, 119 | "autoclose": false, 120 | "autocomplete": true, 121 | "bibliofile": "biblio.bib", 122 | "cite_by": "apalike", 123 | "current_citInitial": 1, 124 | "eqLabelWithNumbers": true, 125 | "eqNumInitial": 1, 126 | "hotkeys": { 127 | "equation": "Ctrl-E", 128 | "itemize": "Ctrl-I" 129 | }, 130 | "labels_anchors": false, 131 | "latex_user_defs": false, 132 | "report_style_numbering": false, 133 | "user_envs_cfg": false 134 | }, 135 | "nbTranslate": { 136 | "displayLangs": [ 137 | "*" 138 | ], 139 | "hotkey": "alt-t", 140 | "langInMainMenu": true, 141 | "sourceLang": "en", 142 | "targetLang": "fr", 143 | "useGoogleTranslate": true 144 | }, 145 | "toc": { 146 | "base_numbering": 1, 147 | "nav_menu": {}, 148 | "number_sections": false, 149 | "sideBar": true, 150 | "skip_h1_title": false, 151 | "title_cell": "Table of Contents", 152 | "title_sidebar": "Contents", 153 | "toc_cell": false, 154 | "toc_position": {}, 155 | "toc_section_display": "block", 156 | "toc_window_display": false 157 | } 158 | }, 159 | "nbformat": 4, 160 | "nbformat_minor": 4 161 | } 162 | -------------------------------------------------------------------------------- /book/week_03/greetings.py: -------------------------------------------------------------------------------- 1 | """A module to say hi in several langages.""" 2 | 3 | base = { 4 | 'en': 'Hi {}!', 5 | 'fr': 'Salut {}!', 6 | 'cn': '你好 {}!', 7 | } 8 | 9 | 10 | def say_hi(name, language='en'): 11 | """Say hi in the current language.""" 12 | print(base[language].format(name)) 13 | 14 | 15 | if __name__ == '__main__': 16 | # execute only if run as a script 17 | import sys 18 | nargs = len(sys.argv) 19 | if nargs == 3: 20 | say_hi(sys.argv[1], language=sys.argv[2]) 21 | else: 22 | print('Syntax:') 23 | print('%run greetings.py name language') 24 | print('Languages available: {}'.format(list(base))) 25 | -------------------------------------------------------------------------------- /book/week_04/Assignment-04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Assignment #04" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Exercise #04-01: a glimpse in the C language" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "This exercise can be done on a linux machine only! \n", 22 | "\n", 23 | "```{tip}\n", 24 | "You can use MyBinder's terminal if you don't have Linux!\n", 25 | "```\n", 26 | "\n", 27 | "Here is the C code sample from the lecture:\n", 28 | "\n", 29 | "```c\n", 30 | "#include \n", 31 | "int main ()\n", 32 | "{\n", 33 | " int a = 2;\n", 34 | " int b = 3;\n", 35 | " int c = a + b;\n", 36 | " printf (\"Sum of two numbers : %d \\n\", c);\n", 37 | "}\n", 38 | "```\n", 39 | "\n", 40 | "**Write this code in a C code file, compile and run it.**\n", 41 | "\n", 42 | "**Now, replace the line ``int b = 3`` with ``char b[] = \"Hello\";``. Compile and run the program again (ignore warnings at compilation). Does the output match your expectations? Can you explain what happens? Compare this behavior to python's, and try to explain why this behavior can lead to faster execution times.**" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "(content:montecarlo)=\n", 50 | "## Exercise #04-02: Monte-Carlo estimation of $\\pi$" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "A simple way to estimate $\\pi$ using a computer is based on a [Monte-Carlo](https://en.wikipedia.org/wiki/Monte_Carlo_method) method. By drawing a sample of N points with random 2D coordinates (x, y) in the ``[0, 1[`` range, the ratio of points that fall within the unit circle divided by the total number of points (N) gives an estimate of $\\pi / 4$.\n", 58 | "\n", 59 | "**Provide two implementations of the monte-carlo estimation of $\\pi$: a pure python version (standard library) and a vectorized version using numpy. Time their execution for N = [1e2, 1e3, ..., 1e7]. Optional: plot the numpy speed-up as a function of N.**\n", 60 | "\n", 61 | "**Optional: try the numpy version with N=1e8 and above. Make conclusions about a new trade-off happening for large values of N.**\n", 62 | "\n", 63 | "```{tip}\n", 64 | "\n", 65 | "To time the execution of a function, you can use the `time` module ([stackoverflow](https://stackoverflow.com/a/1557584/4057931)) \n", 66 | "\n", 67 | "```" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "## Exercise #04-03: a new format based on fixed point binary numbers" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "Write a function which converts binary strings to decimal numbers. The function should handle unsigned (positive) numbers only. Examples:\n", 82 | "- ``'101010'`` $\\rightarrow$ ``42``\n", 83 | "- ``'10000.1'`` $\\rightarrow$ ``16.5``\n", 84 | "- ``'1011101101.10101011'`` $\\rightarrow$ ``749.66796875``" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "Now let's develop a new standard based on this representation. Dots cannot be represented by 0s and 1s, so that if we want the position of the dot to be flexible we need an additional memory slot to store this position. Let's define our new format as a 32 bits long sequence of bits, the first 5 bits (starting from the left) being used to give the position of the dot, and the remaining 27 bits used to represent the number. Examples:\n", 92 | "- ``'10101010101010101010101010101010'`` $\\rightarrow$ ``699050.65625``. \n", 93 | "- ``'00000001100110011001100110011001'`` $\\rightarrow$ ``0.19999999552965164``. \n", 94 | "\n", 95 | "Explanation for example 1: the first five digits are `'10101'` which gives the number 21. The second part of the string therefore becomes a dot at position 21: ``'010101010101010101010.101010'``. This binary number is then converted to decimal." 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "Let's name this standard \"BSE\" (for \"best standard ever\"), and try to convince the *Institute of Electrical and Electronics Engineers* to adopt it in place of the old IEEE 754 standard. We have to answer the following questions:\n", 103 | "- what is the smallest number the BSE can represent? The largest?\n", 104 | "- what is the maximal accuracy of the BSE? (in other words, what is the difference between the smallest positive number and zero?)\n", 105 | "- what is the lowest accuracy of our standard? (in other words, what is the difference between the largest number we can represent and the second largest?)\n", 106 | "- does the difference between two nearest representable change, when the dot position doesn't?\n", 107 | "- now compute the precision of our format for a range of possible values of the BSE \n", 108 | "- for these values, compare the BSE to the IEEE754 ``binary32`` format (or its numpy equivalent ``np.float32``) using [numpy.nextafter](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.nextafter.html).\n", 109 | "- (optional: you can also use matplotlib and a log-log plot to produce a graphic similar to the [wikipedia page on IEEE 754](https://en.wikipedia.org/wiki/IEEE_754#Basic_and_interchange_formats))\n", 110 | "\n", 111 | "Conclude. Do you think we should try to convince the *Institute of Electrical and Electronics Engineers* and [present them our results](https://xkcd.com/541/)?\n", 112 | "\n", 113 | "```{warning} \n", 114 | "\n", 115 | "The BSE format **is not** the IEEE754 format. The BSE is a fun format explaining *some* (but not all) of the underlying concepts behind floating point numbers. I'm just saying, because some people got confused during the exam and remembered the BSE better than the real floating point representation...\n", 116 | "```" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "## Exercise #04-04: exponential error growth" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "The number `e` can be defined as the sum of the infinite series:\n", 131 | "\n", 132 | "$$e = \\sum_{n=0}^{\\infty} \\frac{1}{n!}$$" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "We are going to approximate this number by truncating the sum to a finite value. We use the **standard library** and it's math module: " 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "import math\n", 149 | "n = 100" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [ 158 | "e1 = 0\n", 159 | "for i in range(n + 1):\n", 160 | " e1 += 1. / math.factorial(i)\n", 161 | "e1" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "Close enough! Now let's compute it with the same values, but summed from n=100 to n=0:" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [ 177 | "e2 = 0\n", 178 | "for i in range(n + 1)[::-1]:\n", 179 | " e2 += 1. / math.factorial(i)\n", 180 | "e2" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "Seems reasonable too! Are they different?" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [ 196 | "e1 - e2" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "**Which of the two values is closest to the actual e? Explain why this occurs, and what we can learn from this experiment.**" 204 | ] 205 | } 206 | ], 207 | "metadata": { 208 | "hide_input": false, 209 | "kernelspec": { 210 | "display_name": "Python 3 (ipykernel)", 211 | "language": "python", 212 | "name": "python3" 213 | }, 214 | "language_info": { 215 | "codemirror_mode": { 216 | "name": "ipython", 217 | "version": 3 218 | }, 219 | "file_extension": ".py", 220 | "mimetype": "text/x-python", 221 | "name": "python", 222 | "nbconvert_exporter": "python", 223 | "pygments_lexer": "ipython3", 224 | "version": "3.10.6" 225 | }, 226 | "latex_envs": { 227 | "LaTeX_envs_menu_present": true, 228 | "autoclose": false, 229 | "autocomplete": true, 230 | "bibliofile": "biblio.bib", 231 | "cite_by": "apalike", 232 | "current_citInitial": 1, 233 | "eqLabelWithNumbers": true, 234 | "eqNumInitial": 1, 235 | "hotkeys": { 236 | "equation": "Ctrl-E", 237 | "itemize": "Ctrl-I" 238 | }, 239 | "labels_anchors": false, 240 | "latex_user_defs": false, 241 | "report_style_numbering": false, 242 | "user_envs_cfg": false 243 | }, 244 | "nbTranslate": { 245 | "displayLangs": [ 246 | "*" 247 | ], 248 | "hotkey": "alt-t", 249 | "langInMainMenu": true, 250 | "sourceLang": "en", 251 | "targetLang": "fr", 252 | "useGoogleTranslate": true 253 | }, 254 | "toc": { 255 | "base_numbering": 1, 256 | "nav_menu": {}, 257 | "number_sections": false, 258 | "sideBar": true, 259 | "skip_h1_title": false, 260 | "title_cell": "Table of Contents", 261 | "title_sidebar": "Contents", 262 | "toc_cell": false, 263 | "toc_position": {}, 264 | "toc_section_display": "block", 265 | "toc_window_display": false 266 | } 267 | }, 268 | "nbformat": 4, 269 | "nbformat_minor": 4 270 | } 271 | -------------------------------------------------------------------------------- /book/week_05/02-Scientific-Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The scientific python stack" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Unlike Matlab, the set of Python tools used by scientists does not come from one single source. It is the result of a non-coordinated, chaotic and creative development process originating from a community of volunteers and professionals. \n", 15 | "\n", 16 | "In this chapter I will shortly describe some of the essential tools that every scientific python programmer should know about. It is not representative or complete: it's just a list of packages I happen to know about, and I surely missed many of them." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## Python's scientific ecosystem" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "The set of python scientific packages is sometimes referred to as the \"scientific python ecosystem\". I didn't find an official explanation for this name, but I guess that it has something to do with the fact that many packages rely on the others to build new features on top of them, like a natural ecosystem.\n", 31 | "\n", 32 | "Jake Vanderplas made a great graphic in a [2015 presentation](https://speakerdeck.com/jakevdp/the-state-of-the-stack-scipy-2015-keynote) (the video of the presentation is also [available here](https://www.youtube.com/watch?v=5GlNDD7qbP4) if you are interested), and I took the liberty to adapt it a little bit:" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "![img](https://fabienmaussion.info/acinn_python_workshop/figures/scipy_ecosystem.png)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "## The core packages " 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "\n", 54 | "- **numpy**: [documentation](https://docs.scipy.org/doc/), [code repository](https://github.com/numpy/numpy)\n", 55 | "- **scipy**: [documentation](https://docs.scipy.org/doc/scipy/reference/), [code repository](https://github.com/scipy/scipy)\n", 56 | "- **matplotlib**: [documentation](https://matplotlib.org/), [code repository](https://github.com/matplotlib/matplotlib)\n", 57 | "\n", 58 | "Numpy provides the N-dimensional arrays necessary to do fast computations, and SciPy adds the fundamental scientific tools to it. SciPy is a very large package and covers many aspects of the scientific workflow. It is organized in submodules, all dedicated to a specific aspect of data processing. For example: [scipy.integrate](https://docs.scipy.org/doc/scipy/reference/integrate.html), [scipy.optimize](https://docs.scipy.org/doc/scipy/reference/optimize.html), or [scipy.linalg](https://docs.scipy.org/doc/scipy/reference/linalg.html). Matplotlib is the traditional package to make graphics in python." 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "## Essential numpy \"extensions\"" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "There are two packages which I consider essential when it comes to data processing:\n", 73 | "- **pandas** provides data structures designed to make working with labeled data both easy and intuitive ([documentation](http://pandas.pydata.org/pandas-docs/stable/), [code repository](https://github.com/pandas-dev/pandas)). \n", 74 | "- **xarray** extends pandas to N-dimensional arrays ([documentation](http://xarray.pydata.org), [code repository](https://github.com/pydata/xarray)).\n", 75 | "\n", 76 | "They both add a layer of abstraction to numpy arrays, giving \"names\" and \"labels\" to their dimensions and the data they contain. We will talk about them in the lecture, and most importantly, you will use both of them during the climate and cryosphere master lectures." 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "## Domain specific packages " 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "There are so many of them! I can't list them all, but here are a few that you will probably come across in your career:\n", 91 | "\n", 92 | "**Geosciences/Meteorology**:\n", 93 | "- [MetPy](https://unidata.github.io/MetPy/latest/index.html): the meteorology toolbox\n", 94 | "- [Cartopy](https://scitools.org.uk/cartopy): maps and map projections\n", 95 | "- [xESMF](https://xesmf.readthedocs.io/en/latest/): Universal Regridder for Geospatial Data\n", 96 | "- [xgcm](https://xgcm.readthedocs.io/en/latest/): General Circulation Model Postprocessing with xarray\n", 97 | "- [GeoPandas](http://geopandas.org/): Pandas for vector data\n", 98 | "- [Rasterio](https://rasterio.readthedocs.io/en/latest/): geospatial raster data I/O\n", 99 | "\n", 100 | "**Statistics/Machine Learning**:\n", 101 | "- [Statsmodels](https://www.statsmodels.org/stable/index.html): statistic toolbox for models and tests\n", 102 | "- [Seaborn](https://seaborn.pydata.org/index.html): statistical data visualization\n", 103 | "- [Scikit-learn](http://scikit-learn.org/): machine learning tools\n", 104 | "- [TensorFlow](https://www.tensorflow.org/): Google's brain\n", 105 | "- [PyTorch](https://pytorch.org/): Facebook's brain\n", 106 | "\n", 107 | "**Miscellaneous**:\n", 108 | "- [Scikit-image](https://scikit-image.org/): image processing\n", 109 | "- [Bokeh](https://bokeh.pydata.org/en/latest/): interactive plots\n", 110 | "- [Dask](http://docs.dask.org/en/latest/): parallel computing\n", 111 | "- ..." 112 | ] 113 | } 114 | ], 115 | "metadata": { 116 | "hide_input": false, 117 | "kernelspec": { 118 | "display_name": "Python 3 (ipykernel)", 119 | "language": "python", 120 | "name": "python3" 121 | }, 122 | "language_info": { 123 | "codemirror_mode": { 124 | "name": "ipython", 125 | "version": 3 126 | }, 127 | "file_extension": ".py", 128 | "mimetype": "text/x-python", 129 | "name": "python", 130 | "nbconvert_exporter": "python", 131 | "pygments_lexer": "ipython3", 132 | "version": "3.10.6" 133 | }, 134 | "latex_envs": { 135 | "LaTeX_envs_menu_present": true, 136 | "autoclose": false, 137 | "autocomplete": true, 138 | "bibliofile": "biblio.bib", 139 | "cite_by": "apalike", 140 | "current_citInitial": 1, 141 | "eqLabelWithNumbers": true, 142 | "eqNumInitial": 1, 143 | "hotkeys": { 144 | "equation": "Ctrl-E", 145 | "itemize": "Ctrl-I" 146 | }, 147 | "labels_anchors": false, 148 | "latex_user_defs": false, 149 | "report_style_numbering": false, 150 | "user_envs_cfg": false 151 | }, 152 | "nbTranslate": { 153 | "displayLangs": [ 154 | "*" 155 | ], 156 | "hotkey": "alt-t", 157 | "langInMainMenu": true, 158 | "sourceLang": "en", 159 | "targetLang": "fr", 160 | "useGoogleTranslate": true 161 | }, 162 | "toc": { 163 | "base_numbering": "15", 164 | "nav_menu": {}, 165 | "number_sections": true, 166 | "sideBar": true, 167 | "skip_h1_title": false, 168 | "title_cell": "Table of Contents", 169 | "title_sidebar": "Contents", 170 | "toc_cell": true, 171 | "toc_position": { 172 | "height": "calc(100% - 180px)", 173 | "left": "10px", 174 | "top": "150px", 175 | "width": "384px" 176 | }, 177 | "toc_section_display": "block", 178 | "toc_window_display": true 179 | } 180 | }, 181 | "nbformat": 4, 182 | "nbformat_minor": 4 183 | } 184 | -------------------------------------------------------------------------------- /book/week_05/Assignment-05.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Assignment #05" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[numpy](http://www.numpy.org/) and [matplotlib](https://matplotlib.org/) are two fundamental pillars of the scientific python stack. You will find *numerous* tutorials for both libraries online. I am asking you to learn the basics of both tools by yourself, at the pace that suits you. I can recommend these two tutorials:\n", 15 | "- [numpy tutorial](http://scipy-lectures.org/intro/numpy/index.html)\n", 16 | "- [matplotlib tutorial](http://scipy-lectures.org/intro/matplotlib/index.html)\n", 17 | "\n", 18 | "They can be quite long if you are new to numpy - I'm not asking to do them all today! Sections 1.4.1.1 to 1.4.1.5 in the [numpy tutorial](http://scipy-lectures.org/intro/numpy/index.html) should get you enough information for today's assignments, or you can try without it and learn on the fly - your choice!" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "## Exercise #05-01: numpy cycles" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "Monthly averages of temperature data at Innsbruck can be downloaded from this lecture's github via:" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "from urllib.request import Request, urlopen\n", 42 | "import json\n", 43 | "\n", 44 | "# Parse the given url\n", 45 | "url = 'https://raw.githubusercontent.com/fmaussion/scientific_programming/master/data/innsbruck_temp.json'\n", 46 | "req = urlopen(Request(url)).read()\n", 47 | "# Read the data\n", 48 | "inn_data = json.loads(req.decode('utf-8'))" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "*(original data obtained from NOAA's [Global Surface Summary of the Day](https://catalog.data.gov/dataset/global-surface-summary-of-the-day-gsod))*" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "**Explore the inn_data variable. What is the type of \"inn_data\", and of the data it contains? Convert the data series to numpy arrays.**\n", 63 | "\n", 64 | "**Using numpy/scipy, matplotlib, and the standard library only, compute and plot the mean monthly annual cycle for 1981-2010 and the mean annual temperature timeseries for 1977-2017. Compute the linear trend (using scipy.stats.linregress) of the average annual temperature over 1977-2017. Repeat with winter (DJF) and summer (JJA) trends.**" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "*Tip 1: to select part of an array (indexing) based on a condition, you can use the following syntax:*" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "import numpy as np\n", 81 | "x = np.arange(10)\n", 82 | "y = x**2\n", 83 | "y[x > 4] # select y based on the values in x" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "*Tip 2: there is more than one way to compute the annual and monthly means. Some use loops, some use reshaping on the original 1D array.*" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "## Exercise #05-02: indexing" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "Given a 2D numpy array defined as:" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "import numpy as np\n", 114 | "x = np.array([[1, 2, 3],\n", 115 | " [4, 5, 6]])" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "The following indexing operations all select the same values out of the array:\n", 123 | "- ``x[:, 1]``\n", 124 | "- ``x[slice(0, 2, 1), 1]``\n", 125 | "- ``x[(slice(0, 2, 1), 1)]``\n", 126 | "- ``x[slice(0, 2, 1), slice(1, 2, 1)]``\n", 127 | "- ``x[..., 1]``\n", 128 | "- ``x[::1, 1]``\n", 129 | "- ``x[[0, 1], 1]``\n", 130 | "- ``x[:, -2]``\n", 131 | "- ``x[:, 1:2]``\n", 132 | "- ``x[:, [1]]``" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "This can be checked with the following test:" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "from numpy.testing import assert_equal\n", 149 | "\n", 150 | "ref = 7\n", 151 | "\n", 152 | "assert_equal(ref, x[:, 1].sum())\n", 153 | "assert_equal(ref, x[..., 1].sum())\n", 154 | "assert_equal(ref, x[::1, 1].sum())\n", 155 | "assert_equal(ref, x[slice(0, 2, 1), 1].sum())\n", 156 | "assert_equal(ref, x[(slice(0, 2, 1), 1)].sum())\n", 157 | "assert_equal(ref, x[slice(0, 2, 1), slice(1, 2, 1)].sum())\n", 158 | "assert_equal(ref, x[[0, 1], 1].sum())\n", 159 | "assert_equal(ref, x[:, -2].sum())\n", 160 | "assert_equal(ref, x[:, 1:2].sum())\n", 161 | "assert_equal(ref, x[:, [1]].sum())" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "**Questions:**\n", 169 | "- **What is the ``...`` syntax doing? Again, it is the literal equivalent of an actual python object: what is it?**\n", 170 | "- **some of these indexing operations are truly equivalent to the \"obvious\" one, ``x[:, 1]``. List them.**\n", 171 | "- **Classify these operations (i) in basic and advanced operations, and (ii) by the shape of their output. Explain.**\n", 172 | "- **I'd like my array ``a = x[:, 1:2]`` to have a shape of (2, ) like most of the other operations listed above. What can I do to reshape it?**" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "## Exercise #05-03: the difference" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "Consider the following example:" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "metadata": {}, 193 | "outputs": [], 194 | "source": [ 195 | "a = np.array([1, 2, 3])\n", 196 | "b = a\n", 197 | "c = a\n", 198 | "\n", 199 | "b = a - 10\n", 200 | "c -= 100" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "**What will be the values printed by ``print(a, b, c)`` after this code snippet? Explain.**" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "## Exercise #05-04: Greenwich" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "[ERA-Interim reanalysis](https://www.ecmwf.int/en/forecasts/datasets/archive-datasets/reanalysis-datasets/era-interim) provides global atmospheric fields from 1979 to today. Someone prepared a grid of average temperature available here:" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": null, 227 | "metadata": {}, 228 | "outputs": [], 229 | "source": [ 230 | "from urllib.request import Request, urlopen\n", 231 | "from io import BytesIO\n", 232 | "import json\n", 233 | "\n", 234 | "# Parse the given url\n", 235 | "url = 'https://github.com/fmaussion/scientific_programming/raw/master/data/monthly_temp.npz'\n", 236 | "req = urlopen(Request(url)).read()\n", 237 | "with np.load(BytesIO(req)) as data:\n", 238 | " temp = data['temp']\n", 239 | " lon = data['lon']\n", 240 | " lat = data['lat']" 241 | ] 242 | }, 243 | { 244 | "cell_type": "markdown", 245 | "metadata": {}, 246 | "source": [ 247 | "However, the data is not well processed! The longitudes are ranging from 0 to 360°, thus cutting UK and Africa in half! Reorganize the data and the corresponding coordinate to obtain a plot similar to this one:" 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "metadata": {}, 253 | "source": [ 254 | "" 255 | ] 256 | }, 257 | { 258 | "cell_type": "markdown", 259 | "metadata": {}, 260 | "source": [ 261 | "## Exercise #05-05: ACINN meteorological data" 262 | ] 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "metadata": {}, 267 | "source": [ 268 | "```{warning}\n", 269 | "Online access to ACINN data does not work anymore. For a similar excercise I recommend to have a look at GeoSphere Austria's API. I may update the resources here in the future.\n", 270 | "```\n", 271 | "\n", 272 | "The institute website provides raw data from several stations around Innsbruck using a live feed at the following addresses:\n", 273 | "- https://acinn-data.uibk.ac.at/innsbruck/3 for the three days data\n", 274 | "- https://acinn-data.uibk.ac.at/innsbruck/7 for the seven days data\n", 275 | "\n", 276 | "The datasets for the other stations are available, per analogy:\n", 277 | "- https://acinn-data.uibk.ac.at/ellboegen/3\n", 278 | "- https://acinn-data.uibk.ac.at/obergurgl/3\n", 279 | "- https://acinn-data.uibk.ac.at/sattelberg/3\n", 280 | "\n", 281 | "The data is shared by ACINN under a [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/).\n", 282 | "\n", 283 | "\n", 284 | " \n", 285 | "" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "metadata": {}, 291 | "source": [ 292 | "The data is provided in the [json](https://www.json.org/) format, often used for web applications. Fortunately, this is very easy to read in python:" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "metadata": {}, 299 | "outputs": [], 300 | "source": [ 301 | "from urllib.request import Request, urlopen\n", 302 | "import json\n", 303 | "\n", 304 | "url = 'https://acinn-data.uibk.ac.at/innsbruck/3'\n", 305 | "# Parse the given url\n", 306 | "req = urlopen(Request(url)).read()\n", 307 | "# Read the data\n", 308 | "data = json.loads(req.decode('utf-8'))" 309 | ] 310 | }, 311 | { 312 | "cell_type": "markdown", 313 | "metadata": {}, 314 | "source": [ 315 | "Now I will help you to parse the timestamp of the data:" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": null, 321 | "metadata": {}, 322 | "outputs": [], 323 | "source": [ 324 | "from datetime import datetime, timedelta\n", 325 | "data['time'] = [datetime(1970, 1, 1) + timedelta(milliseconds=ds) for ds in data['datumsec']]" 326 | ] 327 | }, 328 | { 329 | "cell_type": "markdown", 330 | "metadata": {}, 331 | "source": [ 332 | "And make a first plot to get you started:" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "metadata": {}, 339 | "outputs": [], 340 | "source": [ 341 | "import matplotlib.pyplot as plt\n", 342 | "plt.plot(data['time'], data['dd'], '.');\n", 343 | "plt.ylabel('Wind direction (°)'); \n", 344 | "plt.title('Wind direction at Innsbruck');" 345 | ] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "metadata": {}, 350 | "source": [ 351 | "You will get much more time to get used to these data in the mid-semester projects. For today, I'm asking to **write a script that takes the station and number of days as input (either as command line arguments or user input, your choice) and prints the following information in the terminal:**\n", 352 | "\n", 353 | "\n", 354 | " At station XXX, over the last X days, the dominant wind direction was XX (xx% of the time). \n", 355 | " The second most dominant wind direction was XX (xx% of the time), the least dominant wind\n", 356 | " direction was XX (xx% of the time). The maximum wind speed was XX m/s (DATE and TIME), \n", 357 | " while the strongest wind speed averaged over an hour was XX m/s (DATE and TIME).\n", 358 | "\n", 359 | "\n", 360 | "With the wind directions being of 8 classes: N, NW, W, SW, S, SE, E, NE." 361 | ] 362 | } 363 | ], 364 | "metadata": { 365 | "hide_input": false, 366 | "kernelspec": { 367 | "display_name": "Python 3 (ipykernel)", 368 | "language": "python", 369 | "name": "python3" 370 | }, 371 | "language_info": { 372 | "codemirror_mode": { 373 | "name": "ipython", 374 | "version": 3 375 | }, 376 | "file_extension": ".py", 377 | "mimetype": "text/x-python", 378 | "name": "python", 379 | "nbconvert_exporter": "python", 380 | "pygments_lexer": "ipython3", 381 | "version": "3.12.4" 382 | }, 383 | "latex_envs": { 384 | "LaTeX_envs_menu_present": true, 385 | "autoclose": false, 386 | "autocomplete": true, 387 | "bibliofile": "biblio.bib", 388 | "cite_by": "apalike", 389 | "current_citInitial": 1, 390 | "eqLabelWithNumbers": true, 391 | "eqNumInitial": 1, 392 | "hotkeys": { 393 | "equation": "Ctrl-E", 394 | "itemize": "Ctrl-I" 395 | }, 396 | "labels_anchors": false, 397 | "latex_user_defs": false, 398 | "report_style_numbering": false, 399 | "user_envs_cfg": false 400 | }, 401 | "nbTranslate": { 402 | "displayLangs": [ 403 | "*" 404 | ], 405 | "hotkey": "alt-t", 406 | "langInMainMenu": true, 407 | "sourceLang": "en", 408 | "targetLang": "fr", 409 | "useGoogleTranslate": true 410 | }, 411 | "toc": { 412 | "base_numbering": 1, 413 | "nav_menu": {}, 414 | "number_sections": false, 415 | "sideBar": true, 416 | "skip_h1_title": false, 417 | "title_cell": "Table of Contents", 418 | "title_sidebar": "Contents", 419 | "toc_cell": false, 420 | "toc_position": {}, 421 | "toc_section_display": "block", 422 | "toc_window_display": false 423 | } 424 | }, 425 | "nbformat": 4, 426 | "nbformat_minor": 4 427 | } 428 | -------------------------------------------------------------------------------- /book/week_06/Assignment-06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Assignment #06: exceptions and testing" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "This week you have learned two important new concepts: unit testing and exceptions handling. In today's assignment we will revisit old code in order to properly test it and make it more robust." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## Exercise #06-01: refactor and test old code" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "- **Write a test suite for the following past exercises: \"#02: cryptography\", \"#03: automated data download\", and \"#05-05: wind statistics\"** (one ``test_*.py`` file per module). The test suite must contain unit tests for each function you programmed. While doing so, you might find it very useful to refactor your code following the [good practices](../week_03/04-Good-practices): write small functions with a task clearly defined, which you can test for. If you can't test it, the function is possibly not written the right way: either too broad, or not useful enough.\n", 29 | "- **Apply the exception handling methods learned in the [Exceptions chapter](01-Exceptions.ipynb) to the download function(s) of exercise #03**. Add a test to your test suite to verify that exceptions are raised when an invalid tile is requested for download." 30 | ] 31 | } 32 | ], 33 | "metadata": { 34 | "hide_input": false, 35 | "kernelspec": { 36 | "display_name": "Python 3 (ipykernel)", 37 | "language": "python", 38 | "name": "python3" 39 | }, 40 | "language_info": { 41 | "codemirror_mode": { 42 | "name": "ipython", 43 | "version": 3 44 | }, 45 | "file_extension": ".py", 46 | "mimetype": "text/x-python", 47 | "name": "python", 48 | "nbconvert_exporter": "python", 49 | "pygments_lexer": "ipython3", 50 | "version": "3.8.5" 51 | }, 52 | "latex_envs": { 53 | "LaTeX_envs_menu_present": true, 54 | "autoclose": false, 55 | "autocomplete": true, 56 | "bibliofile": "biblio.bib", 57 | "cite_by": "apalike", 58 | "current_citInitial": 1, 59 | "eqLabelWithNumbers": true, 60 | "eqNumInitial": 1, 61 | "hotkeys": { 62 | "equation": "Ctrl-E", 63 | "itemize": "Ctrl-I" 64 | }, 65 | "labels_anchors": false, 66 | "latex_user_defs": false, 67 | "report_style_numbering": false, 68 | "user_envs_cfg": false 69 | }, 70 | "nbTranslate": { 71 | "displayLangs": [ 72 | "*" 73 | ], 74 | "hotkey": "alt-t", 75 | "langInMainMenu": true, 76 | "sourceLang": "en", 77 | "targetLang": "fr", 78 | "useGoogleTranslate": true 79 | }, 80 | "toc": { 81 | "base_numbering": 1, 82 | "nav_menu": {}, 83 | "number_sections": false, 84 | "sideBar": true, 85 | "skip_h1_title": false, 86 | "title_cell": "Table of Contents", 87 | "title_sidebar": "Contents", 88 | "toc_cell": false, 89 | "toc_position": {}, 90 | "toc_section_display": "block", 91 | "toc_window_display": false 92 | } 93 | }, 94 | "nbformat": 4, 95 | "nbformat_minor": 4 96 | } 97 | -------------------------------------------------------------------------------- /book/week_07/02-ClimVis.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Term project: the ClimVis package" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "This is the main project of this semester, and will count for 30% of the final grade. The submission is going to happen in three stages:\n", 15 | "\n", 16 | "**Careful!!! New deadlines after the student vote on November 30.**\n", 17 | "\n", 18 | "- **First submission deadline: Monday 09.01.2023 at 00H00** (midnight on Sunday): one ``.zip`` file per group containing their version of the climvis package\n", 19 | "- Code review by me and ~30 min feedback round per group: (online) meeting between January 10th and 13th, to be scheduled individually\n", 20 | "- **Second submission deadline: Tuesday 31.01.2023 at 00H00** (midnight on Monday). Same project with reviews addressed (reviews are small requested changes to the existing project, not a big change) \n", 21 | "- **Bonus:** on Tuesday 31.01, I will ask all the groups to shortly \"showcase\" their project, i.e. run their app for the class and show what it can be used for. This is not graded, and is more for fun and exchange than anything else!\n", 22 | "\n", 23 | "Here is an *indicative* table I'll use for the grading:\n", 24 | "\n", 25 | "**Group grade (10 points):**\n", 26 | "- Documentation: 3 *(project documentation + narrative report + individual contributions' docstrings, i.e. no individual grade here)*\n", 27 | "- Integration tests: 1 *(tool / project level tests)*\n", 28 | "- pep8: 1 *(compliance to pep8)*\n", 29 | "- Coherence: 2 *(how do the individual contributions fit together, i.e. was there an effort of documentation at least)*\n", 30 | "- Traceability: 1 *(who did what, are the author(s) name(s) provided in the function's docstrings)*\n", 31 | "- Response to reviews: 2 *(reaction to the code reviews and updates: an inadequate or failure to respond to the review can lead to a refusal)*\n", 32 | "\n", 33 | "**Individual grade (10 points):**\n", 34 | "- Narrative self-report: 2 *(quality and honesty of the self-report)*\n", 35 | "- Complexity: 3 *(level and complexity of the implemented tools, i.e. amount of programming work)*\n", 36 | "- Originality: 3 *(did you do something more than following the list of suggestions)*\n", 37 | "- Unit tests: 2 *(individual, function tests)*\n", 38 | "\n", 39 | "```{important}\n", 40 | "I will try and test the code (using `pytest`) on my computer - any new data or library that is necessary should be documented in the code, or in the command line utility documentation!\n", 41 | "```" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "## The climvis package" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "I've written a small package called **climvis**. Its design is based on the [template package](https://github.com/fmaussion/scispack), but this time it really does something.\n", 56 | "\n", 57 | "**Download the zipped package from [here](https://github.com/fmaussion/climvis/archive/master.zip) and extract it. Read the README and the package requirements first. You will have to install the [motionless](https://github.com/ryancox/motionless) package (`mamba install motionless`) for it to work.**\n", 58 | "\n", 59 | "**Now install climvis in development mode. If you are working on your own computer run:**\n", 60 | "\n", 61 | " pip install -e .\n", 62 | " \n", 63 | "from the root directory. **If you are working on a computer on which you have no admin rights, run:**\n", 64 | "\n", 65 | " pip install --user -e .\n", 66 | " \n", 67 | "instead." 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "**Try the command line interface (``cruvis -h``) from a terminal. Now explore ``setup.py``: can you identify what makes the command available from the terminal? Where is the code executing the command ``cruvis``?**" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "**Familiarize yourself with the tool. Can you understand what the role of each function is?**" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "**Can you run the tests successfully? Probably not. Somewhere in ``cfg.py``, a hard coded path is pointing to a non-existing directory.**\n", 89 | "\n", 90 | "**If you are working on your own computer, change the path to a local folder of yours and download the necessary data:**\n", 91 | "- [temperature](https://crudata.uea.ac.uk/cru/data/hrg/cru_ts_4.03/cruts.1905011326.v4.03/tmp/cru_ts4.03.1901.2018.tmp.dat.nc.gz)\n", 92 | "- [precipitation](https://crudata.uea.ac.uk/cru/data/hrg/cru_ts_4.03/cruts.1905011326.v4.03/pre/cru_ts4.03.1901.2018.pre.dat.nc.gz)\n", 93 | "- [elevation](https://cluster.klima.uni-bremen.de/~fmaussion/teaching/scipro/cru_cl1_topography.nc)\n", 94 | "\n", 95 | "**Careful! The total decompressed output is about 6GB large.**" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "Make sure you are able to run the command succesfully before going on. For example, ``cruvis -l 12 47`` should work fine and display a page in your browser." 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "## Guided exercise: make the tool more robust" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "Here are a couple of smaller tasks to get you started:\n", 117 | "- The hard coded path in ``cfg.py`` is very ugly. At the same time, we really need the CRU data for the tool to run! Add a safety check in the code in order to make sure the files are available. If the CRU files are not available, print the following message and exit the program:\n", 118 | "\n", 119 | " ``The CRU files are not available on this system. For cruvis (part of the climvis package) to work properly, please create a file called \".climvis\" in your HOME directory, and indicate the path to the CRU directory in it.``\n", 120 | "\n", 121 | "- Add a small function in ``cfg.py`` which parses this new ``.climvis`` file and uses the given directory in place of the hard-coded one. Tip: you could make good use of [os.path.expanduser](https://docs.python.org/3/library/os.path.html#os.path.expanduser) for this function. Optional: you can even write a small command line program (e.g. `cruvis_setup`) which asks the user for the path of the CRU folder, checks if the data is available and if yes, write the file for the user.\n", 122 | "\n", 123 | "- Selecting a location over the oceans will raise a very uninformative python error message. Make the ``write_html`` function more robust: if the data contains NaNs, print a warning and exit the program." 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "## Project: make the tool better" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "Now you should be ready to contribute to this great package! **Add at least N+1 simple functionalities** to it, where N is your group size. This functionality can be anything you want, as long as it makes you write some code. \n", 138 | "\n", 139 | "Here are some ideas of functionalities that you can adapt at wish.\n", 140 | "- you could add a plot of the 1901-2018 timeseries for this grid point, and make a robust trend analysis for the period.\n", 141 | "- you could add [other data from CRU](https://crudata.uea.ac.uk/cru/data/hrg/cru_ts_4.03/) to the plot.\n", 142 | "- you could add a new command to the tool called ``uibkvis``, which instead plots the data from the UIBK weather stations. For example, you could plot the temperature for all stations, or even better: give choices to the user as to what they want to plot. You'll have to document its command line options of course. You can get inspiration from the wind statistics we did together, but with more variables.\n", 143 | "- you could use a python [windrose](https://github.com/python-windrose/windrose) package to plot a windrose from the uibk data, and add the plot to a webpage\n", 144 | "- you could add an option to ``cruvis``, which accepts a city name instead of the lon and lat of the point. You could use the file shipped with the package for this (`world_cities.csv`: careful - it is quite a badly maintained file - i.e. duplicates and missing cities - and you might want to find another one).\n", 145 | "- you could add a new command line tool to compare the climate at two locations in one webpage.\n", 146 | "- you could parse any other data you'd find interesting from the web and plot it! For example, [upper air soundings](http://weather.uwyo.edu/upperair/sounding.html)? \n", 147 | "- you could use an interactive plotting library like [Bokeh](https://bokeh.pydata.org) (possibly by making use of higher level tools like [holoviews](http://holoviews.org/)) to make reactive plots in your HTML page. For example to visualize the ACINN data interactively?\n", 148 | "- **any idea you find interesting**! You can discuss some ideas with me beforehand if you are unsure if your idea is not enough or too much. Examples from previous years include:\n", 149 | " - a \"fly safe\" info webpage, which scrapped data from the web specifically for flying\n", 150 | " - an interactive bokeh app to visualize CRU climate trends in various cities\n", 151 | " - an El Niño advisory page - warning people when El Niño is likely to occur\n", 152 | " - a bokeh tool to visualize radio soundings\n", 153 | " - and more..." 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "## Expectations " 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "**New in WS22/23: each group member and the entire group needs to write a short narrative self-report (a few lines per point) addressing the following point:**\n", 168 | "- what was your programming objective when you started? (in the case of a true group work: what task was assigned to you when you started?)\n", 169 | "- what changed during the implementation? I.e., what is the outcome of the project?\n", 170 | "- what was the biggest challenge you faced in the implementation?\n", 171 | "- group question: what was the biggest challenge when merging everyone's work into one package?\n", 172 | "\n", 173 | "**The narrative report should be written in the README in markdown format, and will count towards grading in the elements \"Documentation\" and \"Coherence\" for the group grade, and \"self-report\" in the individual grade.**\n", 174 | "\n", 175 | "Some tasks in the list above are significantly simpler than others. Some of you might pick a project which is too ambitious and then reduce their expectations with time. Others might pick one of the simpler task and be finished very quickly.\n", 176 | "\n", 177 | "If you struggle to know what is expected of you, please just be honest with yourselves and with me. I am reluctant to impose thresholds on the number of lines of code, because I find this measure silly (many lines of code is NOT a measure of quality). My goal is that you all get better at programming after the project then when you started, regardless of your starting level. \n", 178 | "\n", 179 | "The list of possible topics should help you to decide on what to code, but otherwise I can provide a vague rule of thumb: if you coded 10 lines of code (without comments), that's probably not enough (unless they are very clever lines of code). If you coded 1000 lines of code, that's probably too much.\n", 180 | "\n", 181 | "I don't mind if you take an easy task, as long as you write something on your own, and write good code. The difficulty of the task will decide on the base grade, and the quality of the code / tests will add points on top. If you pick an \"easy\" task, you may pick two or three tasks instead of just one. Try to split the tasks within the group to get a coherent product!\n", 182 | "\n", 183 | "You all have very different backgrounds and interest. Try to find a project you like, and convince me that you pushed your own programming limit a little bit and that you learned something in the process.\n", 184 | "\n", 185 | "Ah! And **don't forget to write tests.**" 186 | ] 187 | } 188 | ], 189 | "metadata": { 190 | "hide_input": false, 191 | "kernelspec": { 192 | "display_name": "Python 3 (ipykernel)", 193 | "language": "python", 194 | "name": "python3" 195 | }, 196 | "language_info": { 197 | "codemirror_mode": { 198 | "name": "ipython", 199 | "version": 3 200 | }, 201 | "file_extension": ".py", 202 | "mimetype": "text/x-python", 203 | "name": "python", 204 | "nbconvert_exporter": "python", 205 | "pygments_lexer": "ipython3", 206 | "version": "3.10.6" 207 | }, 208 | "latex_envs": { 209 | "LaTeX_envs_menu_present": true, 210 | "autoclose": false, 211 | "autocomplete": true, 212 | "bibliofile": "biblio.bib", 213 | "cite_by": "apalike", 214 | "current_citInitial": 1, 215 | "eqLabelWithNumbers": true, 216 | "eqNumInitial": 1, 217 | "hotkeys": { 218 | "equation": "Ctrl-E", 219 | "itemize": "Ctrl-I" 220 | }, 221 | "labels_anchors": false, 222 | "latex_user_defs": false, 223 | "report_style_numbering": false, 224 | "user_envs_cfg": false 225 | }, 226 | "nbTranslate": { 227 | "displayLangs": [ 228 | "*" 229 | ], 230 | "hotkey": "alt-t", 231 | "langInMainMenu": true, 232 | "sourceLang": "en", 233 | "targetLang": "fr", 234 | "useGoogleTranslate": true 235 | }, 236 | "toc": { 237 | "base_numbering": 1, 238 | "nav_menu": {}, 239 | "number_sections": false, 240 | "sideBar": true, 241 | "skip_h1_title": false, 242 | "title_cell": "Table of Contents", 243 | "title_sidebar": "Contents", 244 | "toc_cell": false, 245 | "toc_position": {}, 246 | "toc_section_display": "block", 247 | "toc_window_display": false 248 | } 249 | }, 250 | "nbformat": 4, 251 | "nbformat_minor": 4 252 | } 253 | -------------------------------------------------------------------------------- /book/week_09/Assignment-07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Assignment #07: Pydemic" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Pydemic](https://github.com/fmaussion/pydemic) is a simple [agent-based model](https://en.wikipedia.org/wiki/Agent-based_model) of a pandemic.\n", 15 | "\n", 16 | "Agents are perfect to teach the basics of object-oriented programming. Note that I wrote the program with this goal in mind, *not* for performance or accuracy. I also just wrote the program in a few hours, so there is room for improvement. You are welcome to help me out!\n", 17 | "\n", 18 | "Start by getting an overview of the model structure.\n", 19 | "\n", 20 | "**Sensitivity analyses**: starting from the default parameter values, vary one parameter at a time and study the consequences for the temporal trajectory of the pandemic. Of particular relevance are the following: (i) agent density (number of agents per pixel), (ii) initial number of infected agents (is it possible to start a pandemic with a single infected individual? What are the settings that favor this?), (iii) agent mobility, (iv) probability of infection, (v) time until recover.\n", 21 | "\n", 22 | "**Add at least one functionality to the model.** Ideas:\n", 23 | "- add a timer for immunity loss: agents loose their immunity after N days (this can be complexified by computing the probability of infection as a function of time since infection / vaccination).\n", 24 | "- add vaccination as a way to gain immunity\n", 25 | "- add a healthcare threshold: above a certain number of people in the hospital, the hospitalized patients are more likely to die.\n", 26 | "- add politics: when a certain contamination threshold is reached, impose a lockdown (reduce agent mobility).\n", 27 | "- etc.\n", 28 | "\n", 29 | "You don't have to add too many of these options (unless you have a lot of fun). Pydemic will keep us busy for the next couple of weeks, so take it easy!" 30 | ] 31 | } 32 | ], 33 | "metadata": { 34 | "hide_input": false, 35 | "kernelspec": { 36 | "display_name": "Python 3 (ipykernel)", 37 | "language": "python", 38 | "name": "python3" 39 | }, 40 | "language_info": { 41 | "codemirror_mode": { 42 | "name": "ipython", 43 | "version": 3 44 | }, 45 | "file_extension": ".py", 46 | "mimetype": "text/x-python", 47 | "name": "python", 48 | "nbconvert_exporter": "python", 49 | "pygments_lexer": "ipython3", 50 | "version": "3.10.8" 51 | }, 52 | "latex_envs": { 53 | "LaTeX_envs_menu_present": true, 54 | "autoclose": false, 55 | "autocomplete": true, 56 | "bibliofile": "biblio.bib", 57 | "cite_by": "apalike", 58 | "current_citInitial": 1, 59 | "eqLabelWithNumbers": true, 60 | "eqNumInitial": 1, 61 | "hotkeys": { 62 | "equation": "Ctrl-E", 63 | "itemize": "Ctrl-I" 64 | }, 65 | "labels_anchors": false, 66 | "latex_user_defs": false, 67 | "report_style_numbering": false, 68 | "user_envs_cfg": false 69 | }, 70 | "nbTranslate": { 71 | "displayLangs": [ 72 | "*" 73 | ], 74 | "hotkey": "alt-t", 75 | "langInMainMenu": true, 76 | "sourceLang": "en", 77 | "targetLang": "fr", 78 | "useGoogleTranslate": true 79 | }, 80 | "toc": { 81 | "base_numbering": 1, 82 | "nav_menu": {}, 83 | "number_sections": false, 84 | "sideBar": true, 85 | "skip_h1_title": false, 86 | "title_cell": "Table of Contents", 87 | "title_sidebar": "Contents", 88 | "toc_cell": false, 89 | "toc_position": {}, 90 | "toc_section_display": "block", 91 | "toc_window_display": false 92 | } 93 | }, 94 | "nbformat": 4, 95 | "nbformat_minor": 4 96 | } 97 | -------------------------------------------------------------------------------- /book/week_10/Assignment-08.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Assignment #08: Pydemic - second wave" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Pydemic](https://github.com/fmaussion/pydemic) is a simple [agent-based model](https://en.wikipedia.org/wiki/Agent-based_model) of a pandemic.\n", 15 | "\n", 16 | "We already used it for the previous assignment.\n", 17 | "\n", 18 | "This time, we want to illustrate how to use inheritance in OOP. Your task is to develop a child class of `NaiveAgent` called `PolicyAwareAgent`. These agents behave much like `NaiveAgents`, with one exception: they can go into quarantine. After a given incubation time (say 3 days) during which the agents are contagious but unaware that they are sick, the agents will go into quarantine and therefore are out of the domain and do not pose a risk to the other agents.\n", 19 | "\n", 20 | "*Other possible enhancements:*\n", 21 | "- *`PolicyAwareAgents` are aware of the number of contagious agents at a given time and decide to quarantine themselves when the infection rate is high*\n", 22 | "- *the simulation is a mix of `NaiveAgents` and `PolicyAwareAgents`*\n", 23 | "- *etc.*" 24 | ] 25 | } 26 | ], 27 | "metadata": { 28 | "hide_input": false, 29 | "kernelspec": { 30 | "display_name": "Python 3 (ipykernel)", 31 | "language": "python", 32 | "name": "python3" 33 | }, 34 | "language_info": { 35 | "codemirror_mode": { 36 | "name": "ipython", 37 | "version": 3 38 | }, 39 | "file_extension": ".py", 40 | "mimetype": "text/x-python", 41 | "name": "python", 42 | "nbconvert_exporter": "python", 43 | "pygments_lexer": "ipython3", 44 | "version": "3.10.8" 45 | }, 46 | "latex_envs": { 47 | "LaTeX_envs_menu_present": true, 48 | "autoclose": false, 49 | "autocomplete": true, 50 | "bibliofile": "biblio.bib", 51 | "cite_by": "apalike", 52 | "current_citInitial": 1, 53 | "eqLabelWithNumbers": true, 54 | "eqNumInitial": 1, 55 | "hotkeys": { 56 | "equation": "Ctrl-E", 57 | "itemize": "Ctrl-I" 58 | }, 59 | "labels_anchors": false, 60 | "latex_user_defs": false, 61 | "report_style_numbering": false, 62 | "user_envs_cfg": false 63 | }, 64 | "nbTranslate": { 65 | "displayLangs": [ 66 | "*" 67 | ], 68 | "hotkey": "alt-t", 69 | "langInMainMenu": true, 70 | "sourceLang": "en", 71 | "targetLang": "fr", 72 | "useGoogleTranslate": true 73 | }, 74 | "toc": { 75 | "base_numbering": 1, 76 | "nav_menu": {}, 77 | "number_sections": false, 78 | "sideBar": true, 79 | "skip_h1_title": false, 80 | "title_cell": "Table of Contents", 81 | "title_sidebar": "Contents", 82 | "toc_cell": false, 83 | "toc_position": {}, 84 | "toc_section_display": "block", 85 | "toc_window_display": false 86 | } 87 | }, 88 | "nbformat": 4, 89 | "nbformat_minor": 4 90 | } 91 | -------------------------------------------------------------------------------- /book/week_10/ascii_art.py: -------------------------------------------------------------------------------- 1 | cat_trick = (' /\\\n \ \\\n ' 2 | '\ \\\n / /\n / /\n ' 3 | '_\ \_/\/\\\n / * \@@ =\n | |Y/\n ' 4 | ' | |~\n \ /_\ /\n \\ //\n ' 5 | ' |||\n _|||_\n ( / \ )') 6 | -------------------------------------------------------------------------------- /book/welcome.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Welcome" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "```{figure} img/logo_acinn_small.jpg\n", 15 | "---\n", 16 | "figclass: margin\n", 17 | "---\n", 18 | "```\n", 19 | "```{figure} img/logo_uob_small.jpg\n", 20 | "---\n", 21 | "figclass: margin\n", 22 | "---\n", 23 | "```\n", 24 | "\n", 25 | "Lecture notes of the master lecture [707716 - Scientific Programming](https://lfuonline.uibk.ac.at/public/lfuonline_lv.details?sem_id_in=22W&lvnr_id_in=707716&sprache_in=en), taught to 1st semester students following a [Master in Atmospheric Sciences](https://www.uibk.ac.at/acinn/studies/master-in-atmospheric-sciences.html.en) curriculum from the Department of Atmospheric and Cryospheric Sciences ([ACINN](https://www.uibk.ac.at/acinn)) at the University of Innsbruck.\n", 26 | "\n", 27 | "This lecture is tailored for a 15 weeks-long class, but these notes might also be of interest to other (geo-)scientists wanting to learn the fundamentals of scientific programming. **Welcome to you all!**" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "![](img/welcome_img.jpg)" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "```{note}\n", 42 | "👨‍🏫 I gave this class in Innsbruck for the last time in the Winter 2022/23, before moving to Bristol. This content will remain online for the foreseable future, and I hope it will be useful to you. Enjoy!\n", 43 | "\n", 44 | "If you find errors, broken links, etc. Please report it [on Github](https://github.com/fmaussion/scientific_programming).\n", 45 | "```" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "## Authors" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "- [Fabien Maussion](http://fabienmaussion.info/) wrote these notes (unless specified otherwise, e.g. for some tutorials)\n", 60 | "- [Matthias Göbel](https://www.uibk.ac.at/acinn/people/matthias-goebel.html.en) and [Lilian Schuster](https://www.uibk.ac.at/acinn/people/lilian-schuster.html.en) (student assistants in 2018 and 2019) reviewed and proof read them" 61 | ] 62 | } 63 | ], 64 | "metadata": { 65 | "hide_input": false, 66 | "kernelspec": { 67 | "display_name": "Python 3 (ipykernel)", 68 | "language": "python", 69 | "name": "python3" 70 | }, 71 | "language_info": { 72 | "codemirror_mode": { 73 | "name": "ipython", 74 | "version": 3 75 | }, 76 | "file_extension": ".py", 77 | "mimetype": "text/x-python", 78 | "name": "python", 79 | "nbconvert_exporter": "python", 80 | "pygments_lexer": "ipython3", 81 | "version": "3.12.4" 82 | }, 83 | "latex_envs": { 84 | "LaTeX_envs_menu_present": true, 85 | "autoclose": false, 86 | "autocomplete": true, 87 | "bibliofile": "biblio.bib", 88 | "cite_by": "apalike", 89 | "current_citInitial": 1, 90 | "eqLabelWithNumbers": true, 91 | "eqNumInitial": 1, 92 | "hotkeys": { 93 | "equation": "Ctrl-E", 94 | "itemize": "Ctrl-I" 95 | }, 96 | "labels_anchors": false, 97 | "latex_user_defs": false, 98 | "report_style_numbering": false, 99 | "user_envs_cfg": false 100 | }, 101 | "nbTranslate": { 102 | "displayLangs": [ 103 | "*" 104 | ], 105 | "hotkey": "alt-t", 106 | "langInMainMenu": true, 107 | "sourceLang": "en", 108 | "targetLang": "fr", 109 | "useGoogleTranslate": true 110 | }, 111 | "toc": { 112 | "base_numbering": 1, 113 | "nav_menu": {}, 114 | "number_sections": false, 115 | "sideBar": true, 116 | "skip_h1_title": false, 117 | "title_cell": "Table of Contents", 118 | "title_sidebar": "Contents", 119 | "toc_cell": false, 120 | "toc_position": {}, 121 | "toc_section_display": "block", 122 | "toc_window_display": true 123 | } 124 | }, 125 | "nbformat": 4, 126 | "nbformat_minor": 4 127 | } 128 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | jupyter-book clean --html book 4 | jupyter-book build book 5 | -------------------------------------------------------------------------------- /data/innsbruck_temp.json: -------------------------------------------------------------------------------- 1 | {"YEAR": [1977, 1977, 1977, 1977, 1977, 1977, 1977, 1977, 1977, 1977, 1977, 1977, 1978, 1978, 1978, 1978, 1978, 1978, 1978, 1978, 1978, 1978, 1978, 1978, 1979, 1979, 1979, 1979, 1979, 1979, 1979, 1979, 1979, 1979, 1979, 1979, 1980, 1980, 1980, 1980, 1980, 1980, 1980, 1980, 1980, 1980, 1980, 1980, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1982, 1982, 1982, 1982, 1982, 1982, 1982, 1982, 1982, 1982, 1982, 1982, 1983, 1983, 1983, 1983, 1983, 1983, 1983, 1983, 1983, 1983, 1983, 1983, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1985, 1985, 1985, 1985, 1985, 1985, 1985, 1985, 1985, 1985, 1985, 1985, 1986, 1986, 1986, 1986, 1986, 1986, 1986, 1986, 1986, 1986, 1986, 1986, 1987, 1987, 1987, 1987, 1987, 1987, 1987, 1987, 1987, 1987, 1987, 1987, 1988, 1988, 1988, 1988, 1988, 1988, 1988, 1988, 1988, 1988, 1988, 1988, 1989, 1989, 1989, 1989, 1989, 1989, 1989, 1989, 1989, 1989, 1989, 1989, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017], "TEMP": [-1.6559139784946235, 2.5833333333333326, 7.684587813620072, 6.9407407407407415, 13.130824372759854, 16.694444444444443, 17.33691756272401, 16.125448028673834, 12.807407407407407, 11.057347670250895, 3.135185185185186, -0.5537634408602152, -1.5734767025089604, -1.0912698412698414, 5.129032258064515, 7.944444444444445, 11.415770609318995, 15.320370370370373, 16.213261648745522, 16.048387096774192, 12.831481481481482, 7.951612903225805, 1.2592592592592595, -1.1845878136200714, -4.2616487455197145, 1.5357142857142858, 5.430107526881721, 6.711111111111111, 12.86200716845878, 16.314814814814817, 16.51254480286738, 16.31362007168459, 14.225925925925932, 11.021505376344082, 1.4740740740740745, 1.068100358422939, -3.8870967741935485, 2.1283524904214555, 4.786738351254479, 5.803703703703704, 11.498207885304659, 14.988888888888887, 16.32258064516129, 18.892473118279565, 15.048148148148153, 8.062724014336919, 1.7203703703703705, -3.865591397849462, -4.971326164874552, -2.6924603174603177, 6.722222222222223, 9.775925925925923, 13.048387096774194, 16.337037037037035, 16.351254480286737, 17.053763440860216, 14.7962962962963, 8.822580645161288, 1.8388888888888895, -1.6039426523297486, -2.07168458781362, -0.9067460317460314, 3.976702508960573, 7.199999999999999, 13.410394265232977, 17.04259259259259, 18.939068100358423, 17.18817204301075, 16.433333333333334, 9.603942652329751, 5.177777777777778, -0.12186379928315415, -0.5842293906810033, -2.4305555555555562, 5.288530465949822, 10.155555555555557, 12.657706093189965, 16.32962962962963, 20.682795698924732, 17.838709677419356, 13.962962962962964, 9.415770609318995, 2.390740740740741, -1.2222222222222225, -0.8655913978494623, -2.2586206896551726, 2.5645161290322576, 7.437037037037036, 11.589605734767025, 15.305555555555555, 17.555555555555557, 16.829749103942653, 12.309259259259258, 10.173835125448027, 5.3500000000000005, -0.15412186379928364, -6.455197132616488, -1.4424603174603179, 3.682795698924732, 8.325925925925926, 14.51254480286738, 14.750000000000004, 19.491039426523304, 17.528673835125446, 15.288888888888888, 8.698924731182796, 0.2592592592592597, 1.003584229390681, -1.4121863799283159, -3.82936507936508, 4.267025089605735, 9.114814814814817, 15.587813620071685, 16.62222222222222, 17.72222222222222, 17.716845878136198, 14.35555555555556, 10.45340501792115, 4.024074074074074, -1.0537634408602157, -5.750896057347669, 0.6746031746031748, 1.442652329749104, 10.053703703703706, 10.833333333333334, 15.627777777777778, 18.91218637992832, 17.236559139784948, 16.26666666666667, 11.259856630824375, 4.290740740740741, 0.6379928315412192, 2.313620071684588, 0.8352490421455938, 3.0985663082437274, 10.199999999999998, 14.89426523297491, 15.66666666666667, 18.91218637992832, 17.707885304659495, 13.857407407407408, 11.523297491039425, 1.137037037037037, 0.5304659498207891, -0.8189964157706093, 3.009920634920635, 7.654121863799284, 9.364814814814814, 13.442652329749103, 14.135185185185183, 17.679211469534053, 17.56989247311828, 14.325925925925928, 9.437275985663081, 2.370370370370371, -1.0340501792114694, -2.5143369175627246, 3.486111111111111, 7.381720430107526, 7.7296296296296285, 14.985663082437279, 15.901851851851852, 17.867383512544805, 18.34408602150538, 12.85, 11.646953405017921, 3.374074074074074, -2.1308243727598573, -0.9354838709677415, -1.8472222222222232, 8.405017921146955, 7.6648148148148145, 9.45878136200717, 15.455555555555556, 19.52329749103943, 18.460573476702507, 16.535185185185185, 8.652329749103943, 2.5444444444444447, -2.437275985663083, -2.5465949820788527, 0.6494252873563217, 4.854838709677419, 7.890740740740741, 15.405017921146952, 16.6537037037037, 19.315412186379934, 20.774193548387093, 14.746296296296293, 7.747311827956992, 4.355555555555556, -1.1810035842293904, 0.6200716845878135, -0.7063492063492064, 2.6182795698924735, 10.424074074074076, 16.250896057347667, 16.674074074074074, 16.668458781362006, 17.22043010752688, 13.646296296296299, 9.401433691756273, 2.2499999999999987, 0.3189964157706092, 1.494623655913979, 1.4027777777777781, 8.980286738351255, 8.127777777777776, 14.089605734767028, 17.12037037037037, 20.2741935483871, 19.216845878136198, 14.948148148148148, 8.74731182795699, 7.2, 0.021505376344086325, -2.6290322580645156, 2.8174603174603186, 3.3387096774193545, 8.862962962962962, 13.564516129032256, 14.242592592592588, 20.22222222222222, 16.88709677419355, 12.316666666666665, 11.372759856630823, 2.462962962962962, -0.8691756272401434, -0.0770609318996414, -0.7605363984674327, 3.114695340501792, 9.325925925925924, 13.519713261648743, 17.57592592592592, 17.406810035842295, 16.770609318996417, 10.950000000000001, 8.976702508960575, 4.50185185185185, -2.112903225806452, -1.317204301075269, 2.388888888888889, 6.297491039426522, 6.720370370370372, 14.078853046594983, 16.694444444444446, 16.453405017921146, 18.32795698924731, 15.179629629629627, 8.517921146953409, 4.896296296296296, 2.1415770609319, 0.3315412186379931, 3.043650793650794, 4.405017921146954, 9.272222222222222, 14.521505376344086, 17.390740740740743, 18.155913978494624, 18.005376344086017, 13.648148148148149, 9.136200716845881, 0.9962962962962961, -1.6236559139784945, -0.42407407407407405, -2.1250000000000004, 5.64516129032258, 9.09074074074074, 14.983870967741934, 15.411111111111108, 18.439068100358426, 17.48207885304659, 16.283333333333335, 10.401433691756274, 1.1944444444444435, -1.3566308243727598, -3.516129032258065, 2.0727969348659, 5.403225806451614, 10.911111111111115, 15.713261648745519, 18.32777777777778, 16.254480286738353, 18.58781362007168, 14.435185185185187, 11.198924731182794, 4.949999999999998, 2.317204301075269, 0.22580645161290322, 1.9801587301587298, 7.3566308243727585, 7.259259259259258, 16.206093189964157, 15.142592592592592, 18.444444444444443, 19.06810035842294, 11.181481481481482, 11.94982078853047, 1.6944444444444442, -3.2598566308243724, -2.806451612903226, 4.170634920634919, 6.9426523297491025, 8.999999999999998, 15.250896057347672, 18.91296296296297, 18.706093189964154, 17.878136200716845, 12.511111111111115, 9.028673835125446, 6.4, 1.9068100358422935, -1.8942652329749101, -2.835317460317461, 5.885304659498208, 8.898148148148149, 15.691756272401435, 20.916666666666668, 19.324372759856633, 21.21505376344086, 13.683333333333332, 6.817204301075268, 5.048148148148147, 0.16666666666666682, -1.3853046594982081, 0.5536398467432949, 4.32437275985663, 9.974074074074073, 12.132616487455198, 16.053703703703707, 18.014336917562723, 18.95340501792115, 14.479629629629626, 11.654121863799283, 2.5314814814814808, -1.9390681003584225, -3.3261648745519716, -2.134920634920635, 4.360215053763441, 10.057407407407403, 14.440860215053762, 17.840740740740735, 17.937275985663078, 15.9247311827957, 15.337037037037037, 9.838709677419352, 2.379629629629631, -2.1917562724014337, -5.5555555555555545, -1.39484126984127, 2.4211469534050174, 9.427777777777777, 13.539426523297491, 17.253703703703703, 21.406810035842298, 15.164874551971325, 17.122222222222216, 11.539426523297491, 4.616666666666668, 0.06272401433691731, 1.6899641577060924, 3.4722222222222228, 6.163082437275986, 13.455555555555557, 15.121863799283158, 18.222222222222225, 19.00179211469534, 17.315412186379927, 12.124074074074072, 8.598566308243727, 2.398148148148148, -1.0609318996415769, 1.2849462365591398, 2.8007662835249043, 4.460573476702508, 8.500000000000002, 15.863799283154119, 17.86296296296296, 17.639784946236556, 18.340501792114694, 13.16851851851852, 9.754480286738353, 4.505555555555556, -0.19892473118279555, -2.811827956989247, 0.2281746031746032, 3.9713261648745513, 12.144444444444444, 15.657706093189967, 16.22962962962963, 18.40322580645161, 19.315412186379923, 15.67962962962963, 8.982078853046596, 5.266666666666667, -0.4695340501792115, -2.9068100358422937, 0.5892857142857145, 4.96415770609319, 9.86111111111111, 12.320788530465952, 17.249999999999996, 20.222222222222218, 17.297491039426525, 13.3, 8.956989247311826, 4.627777777777778, -1.7204301075268817, -1.154121863799283, 0.8174603174603176, 6.793906810035843, 12.435185185185187, 15.155913978494626, 17.28148148148148, 16.758064516129032, 19.31182795698924, 15.80925925925926, 8.286738351254478, 3.662962962962964, 0.41756272401433686, -1.1845878136200716, -4.586206896551725, 6.910394265232974, 9.72777777777778, 14.57347670250896, 18.412962962962965, 18.189964157706093, 19.14695340501792, 14.553703703703706, 9.433691756272399, 4.7481481481481485, -0.2580645161290325, -0.18817204301075247, -1.3809523809523812, 3.973118279569893, 10.412962962962963, 12.813620071684587, 16.472222222222225, 20.738351254480293, 19.225806451612904, 14.111111111111109, 10.681003584229392, 3.1925925925925935, 0.7724014336917562, 1.7921146953405018, 4.190476190476189, 7.286738351254481, 10.90740740740741, 13.182795698924732, 17.874074074074073, 18.56810035842294, 16.492831541218635, 14.824074074074073, 11.403225806451612, 7.764814814814815, 1.4336917562724016, 0.4784946236559143, -0.17261904761904787, 5.883512544802868, 9.42222222222222, 14.0089605734767, 18.159259259259258, 21.48207885304659, 20.191756272401435, 13.494444444444445, 9.668458781362006, 5.35925925925926, 0.7419354838709677, -0.048387096774193485, 3.7605363984674334, 5.430107526881722, 10.351851851851853, 13.413978494623654, 17.25, 19.060931899641577, 18.566308243727597, 16.288888888888888, 9.275985663082439, 4.821839080459769, -1.0824372759856629, -4.951612903225805, 3.5634920634920633, 8.46415770609319, 8.855555555555556, 15.222222222222225, 19.95, 18.72759856630824, 19.27240143369176, 12.133333333333335, 9.946236559139786, 3.2111111111111135, -0.7114695340501794], "MONTH": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]} -------------------------------------------------------------------------------- /data/monthly_temp.npz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmaussion/scientific_programming/d655004f4ac97fedd0d07c3c17ee3860f581d6a0/data/monthly_temp.npz -------------------------------------------------------------------------------- /data/rgi_intersects.json: -------------------------------------------------------------------------------- 1 | {"RGIId_2": ["RGI60-06.00316", "RGI60-06.00326", "RGI60-06.00321", "RGI60-06.00314", "RGI60-06.00317", "RGI60-06.00318", "RGI60-06.00326", "RGI60-06.00313", "RGI60-06.00320", "RGI60-06.00319", "RGI60-06.00321", "RGI60-06.00321", "RGI60-06.00313", "RGI60-06.00323", "RGI60-06.00323", "RGI60-06.00325", "RGI60-06.00325", "RGI60-06.00326", "RGI60-06.00319", "RGI60-06.00324", "RGI60-06.00322", "RGI60-06.00321", "RGI60-06.00313", "RGI60-06.00314", "RGI60-06.00326", "RGI60-06.00315", "RGI60-06.00322", "RGI60-06.00322", "RGI60-06.00320", "RGI60-06.00317", "RGI60-06.00315", "RGI60-06.00325", "RGI60-06.00325", "RGI60-06.00325", "RGI60-06.00319", "RGI60-06.00324", "RGI60-06.00316", "RGI60-06.00316", "RGI60-06.00313", "RGI60-06.00317", "RGI60-06.00324", "RGI60-06.00319", "RGI60-06.00319", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00326", "RGI60-06.00322", "RGI60-06.00321", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00320", "RGI60-06.00320", "RGI60-06.00320", "RGI60-06.00323", "RGI60-06.00318", "RGI60-06.00313", "RGI60-06.00317", "RGI60-06.00314", "RGI60-06.00330", "RGI60-06.00347", "RGI60-06.00344", "RGI60-06.00345", "RGI60-06.00341", "RGI60-06.00340", "RGI60-06.00339", "RGI60-06.00343", "RGI60-06.00332", "RGI60-06.00340", "RGI60-06.00344", "RGI60-06.00328", "RGI60-06.00337", "RGI60-06.00333", "RGI60-06.00339", "RGI60-06.00329", "RGI60-06.00343", "RGI60-06.00346", "RGI60-06.00346", "RGI60-06.00331", "RGI60-06.00339", "RGI60-06.00346", "RGI60-06.00343", "RGI60-06.00345", "RGI60-06.00342", "RGI60-06.00341", "RGI60-06.00339", "RGI60-06.00331", "RGI60-06.00339", "RGI60-06.00342", "RGI60-06.00340", "RGI60-06.00337", "RGI60-06.00333", "RGI60-06.00331", "RGI60-06.00346", "RGI60-06.00336", "RGI60-06.00343", "RGI60-06.00341", "RGI60-06.00328", "RGI60-06.00338", "RGI60-06.00342", "RGI60-06.00329", "RGI60-06.00343", "RGI60-06.00345", "RGI60-06.00328", "RGI60-06.00347", "RGI60-06.00336", "RGI60-06.00328", "RGI60-06.00339", "RGI60-06.00335", "RGI60-06.00338", "RGI60-06.00345", "RGI60-06.00340", "RGI60-06.00334", "RGI60-06.00329", "RGI60-06.00332", "RGI60-06.00340", "RGI60-06.00339", "RGI60-06.00328", "RGI60-06.00345", "RGI60-06.00330", "RGI60-06.00328", "RGI60-06.00335", "RGI60-06.00344", "RGI60-06.00342", "RGI60-06.00328", "RGI60-06.00340", "RGI60-06.00334", "RGI60-06.00333", "RGI60-06.00333", "RGI60-06.00339", "RGI60-06.00341", "RGI60-06.00328", "RGI60-06.00357", "RGI60-06.00350", "RGI60-06.00352", "RGI60-06.00359", "RGI60-06.00355", "RGI60-06.00357", "RGI60-06.00359", "RGI60-06.00349", "RGI60-06.00353", "RGI60-06.00359", "RGI60-06.00349", "RGI60-06.00351", "RGI60-06.00356", "RGI60-06.00350", "RGI60-06.00359", "RGI60-06.00354", "RGI60-06.00564", "RGI60-06.00349", "RGI60-06.00350", "RGI60-06.00352", "RGI60-06.00355", "RGI60-06.00350", "RGI60-06.00349", "RGI60-06.00364", "RGI60-06.00363", "RGI60-06.00362", "RGI60-06.00362", "RGI60-06.00368", "RGI60-06.00366", "RGI60-06.00369", "RGI60-06.00367", "RGI60-06.00365", "RGI60-06.00366", "RGI60-06.00369", "RGI60-06.00365", "RGI60-06.00368", "RGI60-06.00365", "RGI60-06.00356", "RGI60-06.00365"], "RGIId_1": ["RGI60-06.00313", "RGI60-06.00313", "RGI60-06.00313", "RGI60-06.00313", "RGI60-06.00313", "RGI60-06.00314", "RGI60-06.00314", "RGI60-06.00314", "RGI60-06.00315", "RGI60-06.00315", "RGI60-06.00316", "RGI60-06.00316", "RGI60-06.00316", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00317", "RGI60-06.00318", "RGI60-06.00318", "RGI60-06.00319", "RGI60-06.00319", "RGI60-06.00319", "RGI60-06.00319", "RGI60-06.00319", "RGI60-06.00320", "RGI60-06.00320", "RGI60-06.00320", "RGI60-06.00320", "RGI60-06.00320", "RGI60-06.00321", "RGI60-06.00321", "RGI60-06.00321", "RGI60-06.00321", "RGI60-06.00321", "RGI60-06.00322", "RGI60-06.00322", "RGI60-06.00322", "RGI60-06.00322", "RGI60-06.00323", "RGI60-06.00323", "RGI60-06.00323", "RGI60-06.00324", "RGI60-06.00324", "RGI60-06.00324", "RGI60-06.00325", "RGI60-06.00325", "RGI60-06.00325", "RGI60-06.00325", "RGI60-06.00325", "RGI60-06.00326", "RGI60-06.00326", "RGI60-06.00326", "RGI60-06.00326", "RGI60-06.00326", "RGI60-06.00328", "RGI60-06.00328", "RGI60-06.00328", "RGI60-06.00328", "RGI60-06.00328", "RGI60-06.00328", "RGI60-06.00328", "RGI60-06.00329", "RGI60-06.00329", "RGI60-06.00329", "RGI60-06.00330", "RGI60-06.00330", "RGI60-06.00331", "RGI60-06.00331", "RGI60-06.00331", "RGI60-06.00332", "RGI60-06.00332", "RGI60-06.00333", "RGI60-06.00333", "RGI60-06.00333", "RGI60-06.00333", "RGI60-06.00334", "RGI60-06.00334", "RGI60-06.00335", "RGI60-06.00335", "RGI60-06.00336", "RGI60-06.00336", "RGI60-06.00337", "RGI60-06.00337", "RGI60-06.00338", "RGI60-06.00338", "RGI60-06.00339", "RGI60-06.00339", "RGI60-06.00339", "RGI60-06.00339", "RGI60-06.00339", "RGI60-06.00339", "RGI60-06.00339", "RGI60-06.00339", "RGI60-06.00340", "RGI60-06.00340", "RGI60-06.00340", "RGI60-06.00340", "RGI60-06.00340", "RGI60-06.00340", "RGI60-06.00341", "RGI60-06.00341", "RGI60-06.00341", "RGI60-06.00341", "RGI60-06.00342", "RGI60-06.00342", "RGI60-06.00342", "RGI60-06.00342", "RGI60-06.00343", "RGI60-06.00343", "RGI60-06.00343", "RGI60-06.00343", "RGI60-06.00343", "RGI60-06.00343", "RGI60-06.00344", "RGI60-06.00344", "RGI60-06.00344", "RGI60-06.00345", "RGI60-06.00345", "RGI60-06.00345", "RGI60-06.00345", "RGI60-06.00345", "RGI60-06.00346", "RGI60-06.00346", "RGI60-06.00346", "RGI60-06.00346", "RGI60-06.00347", "RGI60-06.00347", "RGI60-06.00349", "RGI60-06.00349", "RGI60-06.00349", "RGI60-06.00349", "RGI60-06.00350", "RGI60-06.00350", "RGI60-06.00350", "RGI60-06.00350", "RGI60-06.00351", "RGI60-06.00352", "RGI60-06.00352", "RGI60-06.00353", "RGI60-06.00354", "RGI60-06.00355", "RGI60-06.00355", "RGI60-06.00356", "RGI60-06.00356", "RGI60-06.00357", "RGI60-06.00357", "RGI60-06.00359", "RGI60-06.00359", "RGI60-06.00359", "RGI60-06.00359", "RGI60-06.00362", "RGI60-06.00362", "RGI60-06.00363", "RGI60-06.00364", "RGI60-06.00365", "RGI60-06.00365", "RGI60-06.00365", "RGI60-06.00366", "RGI60-06.00366", "RGI60-06.00367", "RGI60-06.00368", "RGI60-06.00368", "RGI60-06.00369", "RGI60-06.00369", "RGI60-06.00564", "RGI60-06.00565"]} -------------------------------------------------------------------------------- /linkcheck.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | jupyter-book build book --builder linkcheck 4 | 5 | -------------------------------------------------------------------------------- /push.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ghp-import -n -p -f book/_build/html 4 | git push origin master 5 | 6 | --------------------------------------------------------------------------------