Matlab®To Python: a Migration Guide 6
Total Page:16
File Type:pdf, Size:1020Kb
White Paper MATLAB® to Python: A Migration Guide www.enthought.com Copyright © 2017 Enthought, Inc. written by alexandre chabot-leclerc All Rights Reserved. Use only permitted under license. Copying, sharing, redistributing or other unauthorized use strictly prohibited. All trademarks and registered trademarks are the property of their respective owners. matlab and Simulink are registered trademark of The MathWorks, Inc. Enthought, Inc. 200 W Cesar Chavez St Suite 202 Austin TX 78701 United States www.enthought.com Version 1.0, September 2017 Contents 1 Introduction5 Why Python6 Getting Started8 2 Differences Between Python and MATLAB® 10 Fundamental Data Types 10 Organizing Code in Packages, not Toolboxes 11 Syntax 12 Indexing and Slicing: Why Zero-Based Indexing 14 NumPy Arrays Are Not Matrices 16 Programming Paradigm: Object-Oriented vs. Procedural 19 3 How Do I? 22 Load Data 22 Signal processing 24 Linear algebra 25 Machine learning 25 Statistical Analysis 26 Image Processing and Computer Vision 26 Optimization 27 Natural Language Processing 27 Data Visualization 27 Save Data 30 What Else? 31 4 4 Strategies for Converting to Python 33 From the Bottom Up: Converting One Function at a Time 33 From the Top Down: Calling Python from MATLAB® 41 5 What Next? 46 Appendix 47 Code Example: Proling Contiguous Array Operations 47 Complete Version of main.py, in Chapter4 47 Anti-Patterns 49 References 51 1 Introduction This document will guide you through your transition from matlab® to Python. The first section presents some reasons why you would want to to do so as well as how to get started quickly by installing Enthought Canopy. Download Enthought Canopy at https: The second section highlights some of the most important dierences //store.enthought.com/downloads/. between the two languages, including the fundamental data types; how code is organized in packages; an overview of the syntax dierences; how indexing and slicing work; NumPy arrays; and how Python mainly uses an object-oriented programming paradigm. The third section is structured around vignettes for common tasks when doing data analysis or running simulations. The vignettes highlight the most common packages used for each task, such as loading data, cleaning and reformatting data, performing analysis or simulation, plotting, and saving data. The fourth section introduces two strategies to gradually transition to Python. Both rely on testing to validate that the new Python code works the same way (or is broken in the same way!) as your matlab® code. They approach the problem by either converting all function directly to Python or by calling Python from matlab®. You should use the one that is most convenient for your project. Who Is This Guide For This guide is for long time matlab® users who want to migrate to Python, either partially or completely. I once was such a person. Who Is This Document Not For If you rely heavily on the Simulink® graph- For a good discussion about Simulink® ical programming environment, you’re out of luck. There is no good alternatives, I recommend the article Free and commercial alternatives to Simulink on Simulink® equivalent in the Python ecosystem. If you have very special Mike Croucher’s blog, Walking Randomly. hardware integration needs, you might be able to find a package that works for you on the Python Package Index, but there is no guarantee that it will be actively maintained or that it will support all the features you need. However, there is nothing stopping you from implementing the fea- tures you need and sharing them with the world. Someone, somewhere, © Enthought, Inc. Accelerate your Python migration with Enthought’s Python for Scientists and Engineers training course! https://www.enthought.com/training/course/python-for-scientists-and-engineers matlab®to python: a migration guide 6 would surely be thankful for it. And finally, this is not the right document for you if you are new to programming. Hardware and Soware Requirements Nothing in this guide is platform specific. All the code is written to run under Python 3. At the time of writ- ing, the latest version is 3.6 but there is nothing in this guide that uses version-specific features. Conventions Used in This Document • Italic text is used for new terms, emphasis, and variables that do not appear in any code listing. • Constant width text is used for program listings, as well as within paragraphs to refer to program variables, functions, data types, key- words, etc. Why Python If you are reading this guide, I assume that you do not need to be con- vinced too hard to transition to Python. I will nonetheless go through some of the reasons why someone might want to switch from matlab® to Python. At the very least, it might help you convince someone else. I split the reasons into five dierent categories: financial, freedom, technical, social, and curiosity. Financial: Cost is oen the first reason given for switching away from matlab®. It is certainly a good reason. Licensing fees add up quickly, es- pecially if you rely on many toolboxes, and they might be a significant part of your budget if you are in a small organization. Python certainly has the appeal of being free, both as in “free beer”, or gratis and as in “free speech”. I’ll cover the free speech aspect in a moment. But to be honest, and to re- peat the adage: “There is no such thing as a free lunch”. It is true that when using Python you do not have to pay a license fee from anyone, and that you have access to many free open source packages. However, be aware of the transition “fee”. One day you are a good matlab® developer, and the next you are a mediocre (for now, but I am sure you will improve!) Python developer. The gamble is that Python will allow you to be more agile and productive in the long term, which many individuals and companies have shown. Freedom: Choosing Python, or any other open source language, lets you run your code “forever”. You are not locked-in with a given provider. There is no need to pay a license fee in order to keep your soware running. More importantly, it means that your colleagues, and people you don’t even © Enthought, Inc. Accelerate your Python migration with Enthought’s Python for Scientists and Engineers training course! https://www.enthought.com/training/course/python-for-scientists-and-engineers matlab®to python: a migration guide 7 know, can run your Python code without themselves buying a license. This can greatly improve the chances of survival of your project. Technical: Python has the benefit of being first and foremost a general purpose programming language. It does happen to be a great language for scientific computing, and it even has some features specifically for that, but it’s not only a scientific computing language. It can be used to do everything from building a file synchronization system (Dropbox), a photo- sharing service (Instagram), a 3-D modeling and video-editing application (Blender), a video hosting platform (YouTube), to discovering gravitational waves.1 1 Python was used in most components The consequence of such varied uses is that you can find tools to do of the Laser Interferometer Gravitational- Wave Observatory (LIGO) project. They almost all common tasks. It allows you to use Python for your entire ap- have created a very interesting collection plication, from hardware control and number crunching, to web API and of tutorials. desktop application. And for the cases when a feature or a library exists only in another language, Python is an excellent “glue” language. It can easily interface with C/C++ and Fortran libraries, and there are Python im- plementations for some of the major other languages, such as IronPython for C# and Jython for Java. Social: Maybe you have met a funny, clever and irresistible Pythonista at one of the many local meet-ups and thought you would check out their programming language of choice. The Python community is certainly a great reason to pick the language. There are the multiple PyCon confer- ences around the world, from the main one in North American, to PyCon Zimbabwe, Pycon Pakistan, and Kiwi PyCon. There are also the various SciPy conferences, which focus on the scientific Python ecosystem, or the PyData events about data science. I would be remiss if I failed to mention the PyLadies and Django Girls chapters around the world. Pick your lo- Django is a high-level Web framework, cation and your topic of choice and I am sure you will find a Python user and one of the most widely used Python packages. It is to Python what Rails group nearby. Another aspect of having a vibrant community is the large is to Ruby, if that means something number of libraries available. As of August 18, 2017, there are 114,910 pack- to you. More details at https://www. djangoproject.com. ages on the Python Package Index, the oicial repository for the Python You can find a calendar of upcoming language. This number does not include all the packages available on code Python events at https://www.python. hosting sites such as GitHub or Bitbucket. Finally, there are the 800,000+ org/events/. questions tagged with “Python” on Stack Overflow and the countless num- matlab® has about 74,000 questions on ber of articles, books, and blog posts about Python. Stack Overflow right now. Curiosity: Finally, you might just be curious. There is nothing wrong with that. © Enthought, Inc. Accelerate your Python migration with Enthought’s Python for Scientists and Engineers training course! https://www.enthought.com/training/course/python-for-scientists-and-engineers matlab®to python: a migration guide 8 Getting Started The most straightforward way to start using Python, especially for nu- merical computing, is to install the free Enthought Canopy application.