Pythonize Yourself 1/74

Pythonize Yourself 1/74

Pythonize Yourself 1/74 Pythonize Yourself Pythonize Yourself Michele Dei [email protected] Integrated Circuits and Systems (ICAS) Instituto de Microelectrónica de Barcelona, IMB-CNM(CSIC) This work 'as-is' we provide. No warranty express or implied. May 2016 We've done our best, to debug and test. Liability for damages denied. Permission is granted hereby, to copy, share, and modify. Use as is fit, free or for profit. These rights, on this notice, rely. Image credit: V (1983 miniseries) M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 2/74IntroTools Language Packages4Science Examples 1 Introduction: Matlabholics Anonymous - How to redeem 2 Tools: How to get things done: Tools, IDEs, Installation 3 Language: Enjoy the Lego R 4 Packages4Science: Four Horsemen of the Apocalypse: NumPy, SciPy, Matplotlib, Sympy 5 Examples: Dirty work (but someone has to do it) M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 2/74Intro Tools Language Packages4Science Examples 1 Introduction: Matlabholics Anonymous - How to redeem 2 Tools: How to get things done: Tools, IDEs, Installation 3 Language: Enjoy the Lego R 4 Packages4Science: Four Horsemen of the Apocalypse: NumPy, SciPy, Matplotlib, Sympy 5 Examples: Dirty work (but someone has to do it) M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 3/74Intro Tools Language Packages4Science Examples Targeting my audience, being a target too You already know what is a(n): I 15 algorithm, array, argument, assignment, boolean, code, conditional statement (if-then-else), function, program, 25 variable 10 10 I Don’t expect me to: 20 be formal, be unbiased, be a guru I 15 I port your MATLAB/GNU Octave scripts. Some tools already have been designed to it, for ex.: https://github.com/victorlei/smop 10 https://sourceforge.net/projects/libermate/ You can also run your M-file directly into Python: https://pypi.python.org/pypi/oct2py M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 4/74Intro Tools Language Packages4Science Examples What is this about? I Introduce Python and its core scientific packages I Promote open source for scientific computing I Present a Python integrated development environment (IDE) similar to that of MATLAB R I Discuss the potentiality of Python through some practical examples of common tasks in the scientific routine The content of this presentation is inspired on: I ”Python for MATLAB Users” material by Kitware Staff http://www.kitware.com/cvpr2012.html I Python Course by Bernd Klein http://www.python-course.eu/ M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 5/74Intro Tools Language Packages4Science Examples Ethics! ”Universalism requires that sci- ence be independent of race, color, or creed and that it should be essentially international” ”Communalism requires that sci- entific knowledge should be pub- lic knowledge; [...] there should be freedom of exchange of sci- entific information between sci- entists everywhere” ”Disinterestedness requires that the results of bona fide scien- tific research should not be ma- nipulated to serve [...] personal profit, ideology, or expediency” ”Organized skepticism requires that statements should not be accepted on the word of author- ity, but that scientists should be R.H. Brown. The Wisdom of Science. free to question them [...]” Cambridge University Press. 1986. M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 5/74Intro Tools Language Packages4Science Examples 1 from mpl_toolkits.mplot3d import Axes3D 2 import matplotlib.pyplot as pp 3 4 fig = pp.figure(figsize=(5,5)) 5 ax = Axes3D(fig) 6 7 # https://en.wikipedia.org/wiki/Tetrahedron 8 X = ( 1, 1, 1), ( 1, -1, -1),\ 9 (-1, 1, -1), (-1, -1, 1) 10 11 keys ='s','color','ha','va','size','bbox' 12 bbox_props = dict(boxstyle="round4,pad=0.3",\ 13 fc="w", ec="1.0", alpha=0.7) 14 labels ='Universalism','Communalism',\ 15 'Desiterestedness','Organized\n skeptisism' 16 17 def pt2ln(p1, p2, **kwargs): 18 s = ('xs','ys','zs','color','linewidth','alpha') 19 p = lambda i: [p1[i], p2[i]] 20 return dict(zip(s, [p(0),p(1),p(2),'g',8,0.5])) 21 22 for i1 in range(len(X)): 23 for i2 in range(i1+1, len(X)): 24 ax.plot(**pt2ln(X[i1], X[i2])) 25 ax.text(*X[i1], **dict(zip(keys, [labels[i1], 26 'g','center','center', 18, bbox_props]))) 27 28 pp. title ('Mertonian paradigm',fontsize=16,fontweight='bold') 29 ax.xaxis.set_ticklabels([]) 30 ax.yaxis.set_ticklabels([]) 31 ax.zaxis.set_ticklabels([]) 32 pp. show () mertonian paradigm.py M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 6/74Intro Tools Language Packages4Science Examples Why not MATLAB R ? Nor MATLAB-like programs I Expensive I Influential establishment held by a pri- vate company ”MATLAB R , the language of technical computing, is a programming environment for algorithm development, data analysis, visualization, and numeric computation.” > 5000 academias as customers > 1 million users of MATLAB worldwide > 500 third-party solutions built on MATLAB/Simulink > 1700 MATLAB based books in 28 languages I Structurally poor language A major flaw is that promotes cut+paste coding by: Function-name = file-name. Ok for small code bases but discourages writing modular programs suitable for grown up projects, and at last the capability of your code to be shared and be useful for the rest of the community http://www.mathworks.com/company/factsheet.pdf http://www.mathworks.com/company/aboutus/index.html?s_tid=gn_loc_drop M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 7/74Intro Tools Language Packages4Science Examples Why not MATLAB R ? Nor MATLAB-like programs I Exercise #1 Provided that I have convinced you that MATLAB R is: 1. an influential establishment hold by a private company; 2. a structurally poor language; 3. expensive, in which sense its use in science offends the Mertonian paradigm: universalism, communalism, disinterestedness, organized skeptisism? I Exercise #2 Given that GNU Octave: 1. treats incompatibility with MATLAB R as a bug; 2. is distributed under a GLPv3-”Free as in Freedom” licence, discuss the following statement: GNU Octave is a free-MATLAB, but not MATLAB-free http://wiki.octave.org/FAQ M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 8/74Intro Tools Language Packages4Science Examples The Python remedy I Holistic (not just domain specific) I Open and encouraging your vocation to share (algorithms, methods, results...) I Exhibits excellent readability (if you can read it you can reuse it) I Implement modular programming since functions and classes are organized in namespaces I Has powerful built-in data structures I Scientific modules (and not only) are supported by a large and active community 1 # -*- coding: utf-8 -*- 2 import numpy as np 3 import matplotlib.pyplot as pp 4 5 x = np.linspace(0, 2, 1000) 6 pp. xkcd () 7 pp.figure(figsize=(3.5,3.5)) 8 pp.plot(x, np.sqrt(x),color='m',\ 9 label =r"Skiing:$\sqrt{t}$") 10 pp.plot(x,x**2,color='g',\ 11 label =r"Snowboarding: $t^2$") 12 Side effects? pp. xlabel ("Time $t$") ; pp.ylabel("Skill") 13 pp.legend(loc='upper center',fontsize=14,\ 14 fancybox=True,framealpha=0.25) 15 pp. show () learning curve.py https://www.stat.washington.edu/~hoytak/blog/whypython.html M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 9/74Intro Tools Language Packages4Science Examples More & less I Cython: optimising static compiler for Python, allows calls back and forth from and to C or C++ code natively at any point. easily tune readable Python code into plain C performance by adding static type declarations I Python can be integrated with other languages too (Fortran, Java, PHP ...): https://wiki.python.org/moin/IntegratingPythonWithOtherLanguages I What is Python used for? Google, Yahoo, Youtube, gaming... https://wiki.python.org/moin/OrganizationsUsingPython I Is used as extension language in Inkscape, Freecad, Klayout, Glade, Pymol: https://en.wikipedia.org/wiki/List_of_Python_software#Embedded_as_a_scripting_language I Build a compiled application optimizing speed and simplifying dependencies: https://sourcecontribute.com/2015/05/02/compiling-python-to-standalone-executables-on-linux-using-nuitka/ There is no 1 to 1 replacement of Simulink in Python Anyway we can take advantage of the interoperability of Scilab/Python and use the Xcos to design dynamical systems models. https://www.scilab.org/scilab/features/xcos The last example in this presentation is focused on this issue. M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 10/74Intro Tools Language Packages4Science Examples 1 Introduction: Matlabholics Anonymous - How to redeem 2 Tools: How to get things done: Tools, IDEs, Installation 3 Language: Enjoy the Lego R 4 Packages4Science: Four Horsemen of the Apocalypse: NumPy, SciPy, Matplotlib, Sympy 5 Examples: Dirty work (but someone has to do it) M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 11/74Intro Tools Language Packages4Science Examples Get the tools DIE HARD Build Python (latest version) , config Vi(m) or Emacs https://docs.python.org/2/using/unix.html apt-get install python, than pip modules. Get Ipython qt-console and configure it with fancy text editors The same as above but using Synaptic (Ubuntu based Linux) or other software manager. Main modules can also be installed from there (Numpy, Scipy, Matplotlib, Sympy...) Spyder is a complete IDE that can be used, integrating both: an editor tool and Ipython qt-console(s). You can rely on a Python package distribution provided by third par- ties: Continuum R Analytics Anaconda, Pyzo or Enthought Canopy. Download, Install and Launch. (Batteries included) Marshmallow https://www.continuum.io/downloads M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 12/74Intro Tools Language Packages4Science Examples IDE = Editor + Console I Python on terminal you may want to use it as command line tool M, Dei, IMB-CNM(CSIC) ICAS Tutorials Pythonize Yourself 12/74Intro Tools Language Packages4Science Examples IDE = Editor + Console I Python on terminal you may want to use it as command line tool I IPython on terminal Interactive with graphical capabilities, shell syntax, tab completion, and his- tory.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    79 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us