Showing posts with label NumPy. Show all posts
Showing posts with label NumPy. Show all posts

Tuesday, February 17, 2015

Web Interface for Python

An ongoing project I've been wanting propose to a certain very large government agency involves creating a front-end for Python algorithms.  The first question is what should the interface be:
  • GUI - Would be great, but would need to be developed for all OS's including Mobile in my vision.  How fancy do I get, do I use Tcl/Tk, or even Qt?  I'm starting to feel overwhelmed already with all the Python GUI frameworks.  
  • Web - This would be ideal, as long as it follows standard HTML standards, then one deployment should work for everyone with Internet access.  But how?  Django doesn't seem to be interactive enough for my needs and also seems a bit of an overkill with the ORM.  Flask seems to be too light.  Hmm, and then I got some advice from the author of http://pythonprogramming.net on Reddit.
He pointed me to two amazing projects, and I'm seriously thinking about diving back into this proposal.  He recommended I look into the following projects:

"Brython is designed to replace Javascript as the scripting language for the Web. As such, it is a Python 3 implementation (you can take it for a test drive through a web console), adapted to the HTML5 environment, that is to say with an interface to the DOM objects and events"
Trinket:
"Trinket lets you run and write code in any browser, on any device.
Trinkets work instantly, with no need to log in, download plugins, or install software.
Easily share or embed the code with your changes when you're done."


These look to be two amazing projects that I think I could leverage.  Here, check it out.

Trinket: Brython:

Sunday, January 11, 2015

EDIT: Use scipy.linalg over numpy.linalg.

Per my previous post, I mistakenly referenced numpy.linalg and scipy.linalg as if they were the same.  Upon looking deeper at the documentation for scipy.linalg it clearly states the following:

scipy.linalg contains all the functions in numpy.linalg. plus some other more advanced ones not contained in numpy.linalg
Another advantage of using scipy.linalg over numpy.linalg is that it is always compiled with BLAS/LAPACK support, while for numpy this is optional. Therefore, the scipy version might be faster depending on how numpy was installed.
Therefore, unless you don’t want to add scipy as a dependency to your numpy program, use scipy.linalg instead of numpy.linalg
So for all my purposes, I will only use scipy.linalg.

LAPACK in Python and R

EDIT: See my follow-up post as well!

While porting my dissertation work that I wrote in R to Python, I need to leverage some of the great features of R such as its easy to use wrappers of LAPACK, specifically the Cholesky Decomposition to calculate the inverse of my covariance matrix.  For those not familiar with LAPACK, it's a free open source library for calculating Linear Algebra routines.  I'm talking all of them, and it's included in many open source scientific software due to its wide range of applicability and free and open nature.  The one caveat... It's written in Fortran*.

Now, not to hate on Fortran, but not many people are programming their software or running their data analysis using it.  Fortunately for us, some very good computer nerds out there wrote awesome wrappers in R and in Python (through NumPy and Scipy) to access them.  It's been recommended to me that one should run optimized LAPACK libraries for your processor and Operating System, and build R (and probably NumPy and SciPy) pointing to those optimized routines.  I'll let you read through the R Administration Manual to decide for yourself.

You can find the R discussion on LAPACK routines in the R Extensions Manual here.

As for Python, just check out the documentation for numpy.linalg or scipy.linalg.

Okay, now the real point of this note to myself, is because Googling access to LAPACK in Python led me to this awesome Blog Post:

Linear Solve in Python (NumPy and SciPy)

  did an awesome tutorial on using Cholesky Decomposition, and I thought I'd pass it along to anybody interested in leveraging these routines.

* I vaguely recall reading somewhere that LAPACK is usually compiled in C after porting LAPACK from Fortran to C using f2c.

Friday, January 9, 2015

Vectorize your functions in NumPy

One of the features I loved in R, was that I could easily put a matrix into a unitary function.  Picture this, I have a spatial covariance function which relies on the distances.  All I would need to do is write the spatial covariance function, and then just put in the distance matrix.

Maybe it will be easier to see some sample code:

code /code

Feel free to read a little more here, but it's a great way to avoid writing loops, especially when setting covariance matrices.