Popular recipes by Kaushik Ghose http://code.activestate.com/recipes/users/4166965/2011-07-02T15:14:33-07:00ActiveState Code RecipesSpeeding up computations using a lookup table part I (Python) 2011-07-02T15:14:33-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577776-speeding-up-computations-using-a-lookup-table-part/ <p style="color: grey"> Python recipe 577776 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/optimization/">optimization</a>). </p> <p>I needed to use the cumulative normal distribution and normal probability density functions repeatedly for some data analysis. I found that I could speed things up drastically by using a lookup table and matplotlib's builtin interpolation function.</p> Conjunction select using foreign keys (Text) 2011-06-11T02:43:22-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577747-conjunction-select-using-foreign-keys/ <p style="color: grey"> Text recipe 577747 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/conjunction/">conjunction</a>, <a href="/recipes/tags/foreign/">foreign</a>, <a href="/recipes/tags/key/">key</a>, <a href="/recipes/tags/select/">select</a>, <a href="/recipes/tags/sql/">sql</a>). </p> <p>Say we have a table (notes) containing rows we want to select. Each note has one or more keywords (stored in a table of the same name). We want to select notes that have a conjunction of keywords (AND). notes and keywords are linked through a foreign key table notes_keywords. The following SQL statement allows us to do this</p> Mandelbrot trajectories (Python) 2011-04-06T18:18:48-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577642-mandelbrot-trajectories/ <p style="color: grey"> Python recipe 577642 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/interactive/">interactive</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/plotting/">plotting</a>, <a href="/recipes/tags/widget/">widget</a>). </p> <p>An interactive graph to plot the trajectory of points on and off the mandelbrot set. Illustrates the use of sliders in matplotlib</p> Simplest example of serving a browser request (Python) 2010-10-15T21:17:30-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577428-simplest-example-of-serving-a-browser-request/ <p style="color: grey"> Python recipe 577428 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/webserver/">webserver</a>). </p> <p>This is the simplest way I have found of sending information from a python program to a browser</p> Simple example of embeding plots in wx and running them interactively (Python) 2010-09-21T17:30:30-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577402-simple-example-of-embeding-plots-in-wx-and-running/ <p style="color: grey"> Python recipe 577402 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/interactive/">interactive</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/wx/">wx</a>). </p> <p>Simple code to show how to incorporate a pylab plot into wx and then interact with it. This can form the basis of windows/apps that plot various variables that can be changing in the background.</p> Blocking, Polling, Pipes, Queues in multiprocessing and how they are affected by the OS's time-slicing schedule (Python) 2010-05-08T18:18:01-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577223-blocking-polling-pipes-queues-in-multiprocessing-a/ <p style="color: grey"> Python recipe 577223 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/events/">events</a>, <a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/multitasking/">multitasking</a>, <a href="/recipes/tags/polling/">polling</a>, <a href="/recipes/tags/timeslice/">timeslice</a>). </p> <p>This short code demonstrates how blocking calls to a Queue, while consuming less CPU, are limited in their response time by the minimum time slice the OS is willing to allocate (typically 10ms for Mac OS X and Linux). Non-blocking calls to Pipe, using poll() to check if there is data, on the other hand, give us millisecond or less response times, though they consume more CPU. In this respect doing a blocking call to a CPU is no different than adding sleep(.01) statements to a polling loop. In a way, if you execute a sleep(.01) only when you have no events in your poll you will be more efficient than if you had a blocking call pull events off your Queue one by one - because each call to Queue.get() consumes a time-slice, whereas the sleep(.01) only occurs once. </p> A server that waits through dropped connections (Python) 2010-05-19T18:37:10-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577232-a-server-that-waits-through-dropped-connections/ <p style="color: grey"> Python recipe 577232 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>The code is a barebones example of how to write a loop for a server so that it gracefully detects when the client has dropped the connection and goes back to listening for a new client.</p> Another use for lambdas and function references (Python) 2009-02-27T06:20:33-08:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576672-another-use-for-lambdas-and-function-references/ <p style="color: grey"> Python recipe 576672 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/function_reference/">function_reference</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/loop/">loop</a>). </p> <p>I was writing code to do bootstrapping on a set of data. I wanted a test case where if I asked for one bootstrap I would be returned the original data. lambdas and function references saved me from inefficient code.</p> Generating random numbers with arbitrary distribution (Python) 2008-11-05T07:52:39-08:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576556-generating-random-numbers-with-arbitrary-distribut/ <p style="color: grey"> Python recipe 576556 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/random_number/">random_number</a>). </p> <p>This is a class that allows you to set up an arbitrary probability distribution function and generate random numbers that follow that arbitrary distribution.</p> Generating correlated random numbers (Python) 2008-09-21T21:21:52-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576512-generating-correlated-random-numbers/ <p style="color: grey"> Python recipe 576512 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/random_number/">random_number</a>). </p> <p>From this great <a href="http://www.sitmo.com/doc/Generating_Correlated_Random_Numbers">tutorial</a></p> <p>For two corelated variables, the formula is much as one would get from intuition about the meaning of correlation with some twist due to normalizing the standard deviation: $X_3 = \alpha X_1 + \sqrt{1-\alpha^2} X_2$ Where $X_1$ and $X_2$ are two independent random variables, and $\alpha$ is the coefficient of correlation between $X_1$ and $X_3$.</p> <p>In a more general sense: <br /> Let $C$ be the correlation matrix desired. Let $X_1, X_2..., X_N$ be $N$ independent random variables arranged in a row matrix $R = [X_1, X_2,....,X_N]$. Then $Q = RU$ where $U^TU = C$ gives us $N$ random variables $Q = [Y_1, Y_2, ..., Y_N]$ with the required property.</p> Using the python imaging library to generate degraded letter stimuli (Python) 2008-09-06T05:24:33-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576490-using-the-python-imaging-library-to-generate-degra/ <p style="color: grey"> Python recipe 576490 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/imaging/">imaging</a>, <a href="/recipes/tags/library/">library</a>, <a href="/recipes/tags/science/">science</a>, <a href="/recipes/tags/stimuli/">stimuli</a>, <a href="/recipes/tags/visual/">visual</a>). </p> <p>Code to generate degraded letter stimuli, as used in the paper "The remarkable inefficiency of word recognition" (Pelli et al. 2003). Utilizes the Python Imaging Library, and is an example of the usage of this library.</p> Polynomial explorer (Python) 2008-09-11T07:21:11-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576501-polynomial-explorer/ <p style="color: grey"> Python recipe 576501 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/interactive_graphs/">interactive_graphs</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>). Revision 2. </p> <p>Polynomial explorer. You tell this module what order of polynomial you want and it will set up a figure with a graph of that polynomial plotted with x = -1 to +1. It will set up a second figure with a set of coefficient axes. You can click on the coeffiecient axes to set the coefficents. The graph will then update to reflect the values of the new coefficients.</p> <p>This code illustrates the use of mouse interaction using matplotlib</p> Customizing polar plots in matplotlib (Python) 2008-09-06T05:31:02-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576493-customizing-polar-plots-in-matplotlib/ <p style="color: grey"> Python recipe 576493 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/plots/">plots</a>, <a href="/recipes/tags/polar/">polar</a>). </p> <p>This snipped illustrates the use of thetagrids and rgrids to customize the polar plot grid</p> Matlab code for displaying 'struct' details (Python) 2008-09-05T13:31:54-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576489-matlab-code-for-displaying-struct-details/ <p style="color: grey"> Python recipe 576489 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/matlab/">matlab</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/structure/">structure</a>, <a href="/recipes/tags/tree/">tree</a>). </p> <p>This code, when passed a MATLAB structure, will recursively go into it and print out the form of the struct.</p> Getting version number of modules (Python) 2008-09-06T05:28:46-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576491-getting-version-number-of-modules/ <p style="color: grey"> Python recipe 576491 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/modules/">modules</a>). </p> <p>How to print out the version number of a module</p>