introduction au python pour des scientifiques module1 ...dkhvoros/ptraining/supports/journees... ·...

20
Introduction au Python pour des scientifiques Module1: Initiation Vincent Noel, Dmitry Khvorostyanov, Jean-Yves Peterschmitt avec des contributions précieuses de Ludmila Klenov, Yohann Morille LMD, IPSL/CNRS-X-ENS-UPMC Journée de formation IPSL – 14 octobre 2013 Ecole Polytechnique, Palaiseau, France

Upload: phungxuyen

Post on 19-Jul-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Introduction au Python pour des scientifiques Module1: Initiation

Vincent Noel, Dmitry Khvorostyanov, Jean-Yves Peterschmitt

avec des contributions précieuses de

Ludmila Klenov, Yohann Morille

LMD, IPSL/CNRS-X-ENS-UPMC

Journée de formation IPSL – 14 octobre 2013Ecole Polytechnique, Palaiseau, France

Programme de la journée

Matin (maintenant)

9:30 accueil café

10h00-12h30: Introduction générale à Python (amphi Becquerel)

Midi

12:30 – 14:00 repas: salle Club Magnan

Après-midi 14H – 17h30: Travaux Pratiques (salles informatiques 31 et 33 de l'X)

Presentation outline

● Introduction to Python (Dmitry)

● Language syntax introduction (Jean-Yves)

● Coffee break/Questions

● Scientific Python libraries (Vincent)

● Practical sessions overview (Dmitry)

Python: Brief History

● February 1991, the 1st published version (0.9.0): classes, exception handling, functions, core datatypes (list, dict, str...), module system

● January 1994, v1.0: functional programming paradigm tools lambda, map, filter and reduce.

October 1996, v1.4: keyword arguments [ex.: c = jump(high = True)], complex numbers

● October, 2000, v2.0: list comprehensions (we'll see one today!), a garbage collection system (makes life easier with memory management)

● December, 2001, v2.2: new-style classes (unification of Python's types (written in C) and classes (written in Python) into one hierarchy; generators

● December, 2008, v3.0: "Reduce feature duplication by removing old ways of doing things" => no more backward compatibility!

● June, 2009, v3.1, July, 2010, v2.7. Python 2.7 was released to coincide with Python 3.1, included some features 3.1, as well as warnings for old features. The 2.7 was the last 2.x release.

Van Rossum at OSCON 2006

Photo taken from WikiPedia

Guido Van Rossum: "Benevolent Dictator For Life"

Python creator, he oversees the development process,

making decisions

Why Python?● High-level flexible programming language

● Development speed

● Code readability

● Collaborative development, program component (re-)usability

combined with

● Rich variety of libraries for wide range of applications:

● Scientific computing

– Data analysis and statistics, input/output

– Data visualization and plotting

– Image/signal processing, data mining, machine learning....

● Shell programming (a readable bash alternative!)

● Web / Databases

● Human-computer interaction (graphical user interfaces, virtual reality...)

● Video-games, networking, sound/text processing, linguistics, neurosciences...

● Embedded systems

● Free, open-source, permissive license (PSFL)

● Large, friendly, dynamically growing community

- AFPY: rencontres, forums, formations, documents éducatifs

- "Planets" et "User Groups" de Python: forums, blogs, groupes thématiques...Stack Overflow: réponses à presque toutes vos questions!

- EuroSciPy, PyCon, EuroPython, PyCon UK, OSCON, ... - échanges de compétences, tutoriaux, événements sociaux

- Google, BitTorrent, SpamBayes, Plone CMS, MailMan, Zope, Django - Python-powered!

Des initiatives humanitaires dans des pays en développement: - Argentine Py Group: CDPedia - One Laptop Per Child: Sugar - Bungeni: système d'information pour les parlements africains + ouverts et accessibles

- Python African Tour: Formations Maroc, Sénégal, Zambie, Nigéria...

Large, friendly, dynamically growing community

map(lambda x: round(x**2, 1), [0.1, 0.4, 5]) >> [0.0, 0.2, 25.0]filter(lambda x: x % 3 == 0, [1,2,6,5,7]) >> [6]reduce(lambda x, y: x * y, [1,2,3,4]) >> 24

● Functional

class ncVar: def __init__(self, file): self.var = [] self.nc =

netCDF4.Dataset(file) def read(self, varname): self.var = self.nc.variables

[varname][:]

V = ncVar('O3.nc')V.read('tem2')print(V.var.shape)

● Object-oriented

def filter_div(v,N) : y = [] for x in v: if (x % N == 0): y.append(x) return y

filter_div([1,2,6,5,7],3) >> [6]

●Imperative

High-level and flexible programming language

dates = array([datetime(*map(int,row[:5])) for row in data])

●Multiple ways to do the same thing, often with very compact syntax:

Three programming paradigms

=> see TP1 for three other ways to do this !

●Can get very quickly what you need with a simple straightforward code:

import cartopy.crs as ccrsimport netCDF4

nc = netCDF4.Dataset('sinwave.nc')lons = nc.variables['lon'][:]lats = nc.variables['lat'][:]data = nc.variables['data'][:]

ax = plt.axes(projection=ccrs.Mollweide())ax.contourf(lons, lats, data, transform=ccrs.PlateCarree(),cmap='spectral') ax.coastlines()ax.set_global()show()

High-level and flexible language + libraries

import cartopy.crs as ccrsimport matplotlib.pyplot as pltax = plt.axes(projection=ccrs.Mollweide())ax.stock_img()plt.show()

...And create complex software architectures :

Twisted : ~ 208,000 lines of code (network framework)

Zope : ~ 404,000 (web application server & framework)

Plone : ~ 573,000 (content managment system)

High-level and flexible language + libraries

Python est utilisé dans de nombreux domaines:

programmation système, réseaux, automatisation, ingénierie, serveurs web, bases de données, images de synthèse, manipulation de texte, jeux vidéos, data mining, applications commerciales...

... et en sciences ( Ouah ! )

Python s’adapte à plusieurs types de programmation :

• Séquentielle / parallèle• Procédurale/ fonctionnele / orientée objet • Test-driven• Bête/intuitive (populaire dans les labos)

High-level and flexible language: résumé

● Available with Linux (standard distribution included), Mac OS X (standard included), Windows, Android, etc.

● Interactive mode (python, ipython, notebook) or automatic (scripts, libraries, ressources)

● Interpreters and compilers (CPython, PyPy, Cython, py2exe)

● Interfaces with other languages : Fortran (f2py), C/C++, Java (Jython, JPype, Jepp)....

High-level and flexible language + framework

Est-ce que Python va dominer le monde? [ du travail ]

Percentage of jobs in web job listings since 2005(from indeed.com)

% growth% job postings

Distributions Python

• python.org = python + librairies standard

o numpy, etc à installer soi-meme : complexe

• distributions "intégrées" python scientifique

o python + librairie standard + numpy + lecture/écriture + etc.

o plus facile, à privilégier si possible

o vérifier la license... (parfois payant)

• CANOPY (Enthought Python Distribution, conseillé) http://www.enthought.com

• Anaconda (http://www.continuum.io/downloads)

• UV-CDAT (http://uvcdat.llnl.gov)

• SAGE (calcul formel) http://www.sagemath.org

• python(x,y) http://www.pythonxy.com

II. La syntaxe : brève intro

Presentation of Jean-Yves goes here !

III. Python pour la science : outils et librairies

Presentation of Vincent goes here !

•Restez pour l'après-midi

•Google : Python, science, numpy, matplotlib, tutorials...

•Bouquins (souvent en anglais,mais pas toujours)

Pour aller plus loin

TPs de cet après-midi

login machine python2... python35

Mot de pass: sera communiqué discrètement

Actualiser les PATHs:

$ export $HOME/../python1/set_paths.sh

Données, énoncés, solutions (seront communiqué au fur et à mésure de votre progréssion): cp $HOME/../python1/TPx . ou x=['Intro, 1, 2, 3, 'Bonus']

Merci pour votre attentionQuestions ?