Você está na página 1de 26

Industrial

Engineering
with Python
A Brief Tutorial with
Engineering Examples
Tools Used in IE Classes

● Xpress-MP for solving LP, IP, MLP


● MS Access for database development
● Simio for simulation
● Crystal Ball, Excel for financial analysis
● Microstrategy, Tableau, Rapidminer, SAS
for business intelligence

What do these tools have in common?


Proprietary Software

● Expensive
● Subject Matter Specific
● Each has its own "language"

As a graduating student, I want tools that


are:
● recognized
● flexible
● powerful
● portable
Python Programming Language

Python is a general-purpose, high-level


programming language whose design
philosophy emphasizes code readability.
● Beautiful is better than ugly
● Explicit is better than implicit
● Simple is better than complex
● Complex is better than complicated
● Readability counts

http://en.wikipedia.org/wiki/Python_(programming_language)
Sample Python Code
Where Can You Start Learning?

Learn Code the Hard Way


● Command Line Interface
○ http://cli.learncodethehardway.org/book/
○ get familiar with interacting with terminal
applications and installing open source software
● Python
○ http://learnpythonthehardway.org/book/
○ numbers, variables, control statements, lists, files,
functions, classes, modules, etc... everything you
need
● Regular Expressions, SQL
○ same web site, more great exercises
Python Programming Environment

● Text Editors
○ http://www.sublimetext.com/
○ http://notepad-plus-plus.org/
● Windows
○ http://www.python.org/download/releases/2.7.3/
● Mac OSX and Linux
○ find the terminal and type "python"
Python in your Browser

● PythonAnywhere
○ PythonAnywhere is a Python development and hosting
environment that displays in your web browser and runs on our
servers. They're already set up with everything you need. It's easy
to use, fast, and powerful.
○ Website hosting with
Django, Flask and other frameworks
○ https://www.pythonanywhere.com/
Python in your Browser

● Wakari by Continuum.io
○ Web-based Python Data Analysis Environment
○ Wakari is a collaborative analytics platform in the browser. It's
Wordpress, Github, and Youtube for science, engineering, and
business data analytics. It allows scientists and data analysts to
easily publish and share their analyses as interactive dashboards,
visualizations, and scientific notebooks that anyone can then
modify.
○ Based on Anaconda, a collection of Python
libraries supported by Continuum.io
○ http://continuum.io/
○ https://www.wakari.io/
Python for IE: Table of Contents
1. Numpy and Scientific Python
a. Quality Control Example
2. Data Manipulation
a. Supply Chain Example
3. Database Integration
4. Graph and Network Problems
a. Web Mining Example
5. Linear Programming
6. Discrete Event Simulation
7. MapReduce
8. Modern Web Applications
a. Client Side Example
b. Server Side Example
9. Summary and Additional Resources
Numpy and Scientific Python

● Numpy
● Key Concepts
○ python is slower compared to compiled lang
○ faster when it comes to working with humans
○ it provides MATLAB like array functions written in
fast C-code, but accessible through a Python
interface
● Numpy QC simple gap simulation example
○ https://github.com/mdlthree/pyIEM/blob/master/qcGapSim.py
○ I was able to compute 10 million samples almost instantly
○ 50 million samples started causing page faults
Data Manipulation

● IPython
● Matplotlib
○ basic version time series
● Pandas
○ easy version time series
● Further Reading
○ http://matplotlib.org/gallery.html
○ http://blog.wesmckinney.com/
○ http://ipython.org/
Time Series Example

● Input data: comma separated values


● Program Flow
○ Read data into a data frame
○ plot raw
○ compute averages, sd, max capacity req'd
○ plot cusum + average
● Modules
○ pandas
○ matplotlib
● SC Time Series Example
○ https://github.com/mdlthree/pyIEM/blob/master/scExample.py
IEM 5763: Supply Chain Strategy
Database Integration

● SQLite 3
○ http://www.sqlite.org/
○ Included with the Python Standard Library
○ File Based Database, no server required
● SQLAlchemy
○ http://www.sqlalchemy.org/features.html
○ DB-API abstraction
○ write code once
○ implement with many popular DBs
■ Open Source: PostgreSQL, MySQL, SQLite
■ Proprietary: Microsoft SQL, Oracle

IEM 5723: Data, Process and Object Modeling


Graph and Network Problems

● NetworkX
○ http://networkx.github.com/
○ NetworkX is a Python language software package for the creation,
manipulation, and study of the structure, dynamics, and functions
of complex networks.
● Algorithms
○ Breadth and Depth First Search, All Pairs Shortest Path, Kruskal's
Minimum Spanning Tree, PageRank, Ford-Fulkerson Maximum
Flow, Topological Sort, Matching and many more!
○ http://networkx.github.com/documentation/latest/reference/

IEM 6013: Network and Combinatorial Optimization


Web Mining Example

● Networkx Example
○ http://www.the-adam.com/
○ A personal blog by Adam Rosenburg Ph.d.
Operations Research
○ A interesting read for Math/OR/IE types with
many personal opinions
○ Problem? Difficult navigation due to lack of any
content management. No direct link to many
pages. You can only discover them through links
deep in the site, and some only by dissecting
URL
○ Lets map ALL THE LINKS!
MSIS 5633: Business Intelligence Tools & Techniques
Web Mining Example (continued...)

● Modules
○ urllib2: retrieving web pages
○ lxml.html: finding <a> tags and storing data
○ NetworkX: page=node, <a>=edge
○ Matplotlib: print the resulting graph
● Mining Strategy
○ Start with the root page { http://www.the-adam.com/ }
○ Add page to nodeList
○ foreach page in nodeList
■ parse HTML, find <a>
■ if <a> is a new page, add it to nodeList
■ add an edge from page to <a>
● https://github.com/mdlthree/pyIEM/blob/master/webMining.py
Linear Programming

Gurobi Optimization Software


● includes a Python interface for its solver
○ proprietary software with licenses available for
academic and trial use
○ installation easy
○ license management tricky
● Solvers for Linear, Quadratic, Integer
programming domains
● http://www.gurobi.com/documentation/5.1/quick-start-guide/
Usef

SUse
IEM 5013: Intro to Mathematical Programming
Discrete Event Simulation

● SimPy
● https://simpy.readthedocs.org/en/latest
● Looks like a promising alternative to
existing simulation platforms, even if it
doesn't have the fancy 3D animation.

IEM 5703: Discrete Event Simulation


MapReduce

● Disco
○ a distributed computing framework like Hadoop
but written in Python, founded by Nokia
○ http://discoproject.org/
○ http://pyvideo.org/video/961/the-disco-mapreduce-framework
Powered by Python

● HTML5 technologies are extremely easy


to harness to produce decision support
tools
○ HTML, CSS, JavaScript
○ http://www.html5rocks.com/
● Create client-server applications where
HTML5 delivers the user experience and
Python drives the server
● Python Web Frameworks
○ Django, Flask, Pyramid, and others ...
Modern Web Applications

● Client Side Software


○ Rendered in the browser
○ HTML Document Object Model
○ Javascript
■ jQuery for DOM manipulation
■ MVC frameworks
■ AJAX to power communication with servers
○ CSS
■ Defines how the content looks
● Server Side Software
○ Interacts via HTTP protocol to deliver content
depending on the URL given to the server
Server Side Python

● These example use the 'Flask' Python


Web Micro-Framework
○ http://flask.pocoo.org/
○ hosted on PythonAnywhere
● http://mdlthree.pythonanywhere.com/hello
○ a hello world example
● http://mdlthree.pythonanywhere.com/user/IEM-Test-USER
○ this will emit the string after user/{...} along with a datestamp in
JSON format
● http://mdlthree.pythonanywhere.com/inverse/123253108
○ given a string of single digits, compute and return the inverse
● code at https://github.com/mdlthree/pyIEM/blob/master/flask_app.py
Python Summary

● Fast proto-typing time


○ I wrote the web mining example yesterday and
this morning
○ Inverse example in the last two hours
● Well supported scientific libraries
● Active community and tons of online
resources
● Free, open source
Additional Resources

● Project Euler
● XKCD
● PyVideo
● Standard Python Library Docs
● Wikipedia
● Hacker News
Poster

Você também pode gostar