Using MATLAB With Python

Transcription

Using MATLAB with PythonHeather Gorr, PhD & Yann Debray1

2

Example: Build Air Quality App using MATLAB and Python3

Follow along with the code 4

5

Strategies6

Data Access7

Access Data from a Web cution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment8

What type of data?Numerical, Textual, Geolocalized, Timeseries, DataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeploymenthttps://openweathermap.org/9

Store & transfer tabular data between languagesUse Apache Parquet files for memory efficient data accessDataAccess Working with Parquet files in MATLABMATLAB library for Apache Arrow on GitHubCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment10

Co-execution11

Given: Existing Python CodeData preparationModelingDeploymentaccessing & preparing weather dataDataAccessWeather DataCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment12

Call Python from MATLABDataAccessData preparationModelingDeploymentWeather DataCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment13

Call MATLAB from PythonDataAccessData preparationModelingDeploymentWeather DataCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment14

Deploy:Data preparationModelingDeploymentMATLAB Analytics into PythonDataAccessWeather DataMy Weather Pagewww.myweather.com/stats.htmlCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment15

Call Python from MATLAB16

Why Call Python from MATLAB?DataAccessCo-ExecutionAlready working in MATLAB, and: Want to reuse existing Python code Need functionality available in Python Want to collaborate with Python users Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment17

Calling Python libraries from MATLABDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment18

First steps to connect to on Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment19

Call the weather service with Python and import data into MATLABDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment20

Live TasksDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment21

Live TasksDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment22

Train air quality prediction model in MATLABDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment23

Use air quality prediction model on Python dataDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment24

DataAccessCo-Execution Call Pythonfrom MATLABRecap: Calling Python from MATLAB Call MATLABfrom PythonDeployment25

Data are automatically converted where possibleOtherwise convert explicitlyDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom matlab external/passing-data-to-python.html26

Note the syntax differences when calling Python fromMATLABDataAccessCo-Execution Call Pythonfrom MATLABPythonMATLAB import math math.sqrt(42) py.math.sqrt(42) Call MATLABfrom Python print('hello','world',sep ', ')Deployment py.print('hello','world',.pyargs('sep',', '))27

Model InteroperabilityDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom s/deep-learning/models.html28

Learn more aboutData Science with MATLABFind out more:AI in EngineeringDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment29

Call MATLAB from Python30

Why call MATLAB from Python?DataAccessCo-ExecutionAlready working in Python, and: Want to reuse existing MATLAB code Need functionality available in MATLAB Want to collaborate with MATLAB users Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment31

Call MATLAB from PythonTo perform advanced analyticsDataAccess Calling MATLAB from Python– via MATLAB Engine APICo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment32

Call MATLAB from PythonTo perform advanced analyticsDataAccess Calling MATLAB from Python– via MATLAB Engine APICo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment33

Call MATLAB from PythonTo perform advanced analyticsDataAccess Calling MATLAB from Python– via MATLAB Engine APICo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeploymentMore in Deployment:– via MATLAB Runtime(MATLAB Compiler SDK)– via MATLAB Production Server34

DataAccessCo-ExecutionRecap: Calling MATLAB from Python Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment35

Note the syntax differences when calling MATLABfrom PythonDataAccessCo-ExecutionMATLABPython [s,sidx] sort(x) s eng.sort(x,nargout 2) foo(x) eng.foo(x,nargout 0) C A B C eng.plus(A,B) Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment36

Data are automatically converted where possibleDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom matlab external/pass-data-to-matlab-from-python.html37

Deployment38

Generate Python library from MATLAB functionsDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment39

Generate Python library from MATLAB functionsDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment40

Generate Python library from MATLAB functionsDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeploymentInstall library41

Execute Python library from MATLAB functionsDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment42

MATLAB Production ServerAccess functions as web servicesDataAccessMATLAB Production ServerWorker processesCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonCalling our tting the ,"mwtype":"char"}]}43

Execute Python library from MATLAB functionsDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment44

Share MATLAB App in the Web – Central DeploymentDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment45

MATLAB App DesignerApp development for Non-ProgrammersDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment46

MATLAB Web App Server – Central DeploymentDataAccessCo-ExecutionWeb App Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment47

Integrate your Production System in an IT ecosystemDataAccessProduction SystemDataAnalytics DevelopmentMATLABCompiler SDKMATLAB Production ServerCo-ExecutionRequestBroker Call MATLABfrom ge LayerTableauSpotfirePowerBIQlik{ RESTful API }Enterprise AppBig DataFilesModelBusiness DecisionsDashboardsDeploymentPackage &DeployWeb App Call Pythonfrom MATLABWorker processesStreamingdata48

Use MATLAB Reference Architectures for easy cloudsetup, Dockerfiles, and interfaces to OSSDataAccess rfileCo-Execution Call Pythonfrom MATLAB Call MATLABfrom rch49

MATLAB Integration for JupyterDataAccessCo-Execution Call Pythonfrom MATLAB Call MATLABfrom PythonDeploymentMATLAB Integration for Jupyter (mathworks.com)50

DataAccessCo-ExecutionFind out more:Cloud and IOT Call Pythonfrom MATLAB Call MATLABfrom PythonDeployment51

Summary:Using MATLAB with Python Access Data– Weather App example Interoperability– Calling libraries written in Python from MATLAB– Calling MATLAB from Python Deploy Apps & Algos– Web App– Production API52

Resources CheatsheetExample on GitHubBlog postVideos– Calling Python from MATLAB– Calling MATLAB from Python– Using MATLAB with Python Q&A (YouTubelive stream recording) Documentation– Calling Python from MATLAB– Calling MATLAB from Python via: MATLAB Engine API MATLAB Compiler SDK MATLAB Production Server– Data ab/matlab-and-python.html Data type conversions Working with Parquet files MATLAB library for Apache Arrow onGitHub– Deep Learning (TensorFlow, PyTorch, etc)53

Thank youMATLAB Python 2021 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarksfor a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.54

- Calling Python from MATLAB - Calling MATLAB from Python - Using MATLAB with Python Q&A (YouTube live stream recording) Documentation - Calling Python from MATLAB - Calling MATLAB from Python via: MATLAB Engine API MATLAB Compiler SDK MATLAB Production Server - Data management: Data type conversions Working with Parquet files