background preloader

Python Quick Hacks and Codes

Facebook Twitter

The Binomial Distribution, Python and Bisulphite Sequencing. This is Part 2 in a series on Bisulphite Sequencing. You can return to Part 1 (Post Processing Bismark Bisulphite Sequencing Data) or skip to Part 3 (Simple Visualisation of Bisulphite Sequencing Data). Today I'm going to describe how the binomial distribution can be applied to bisulphite sequencing data in order to accurately determine the number of CpG sites which exhibit methylation in a sample.

The binomial distribution is actually pretty straightforward. If you have a coin that has a probability to come up heads then the binomial distribution can tell us the probability that if you throw coins then what is the probability that you will get heads. We call this the probability mass function. Let's do the simplest example first. If I throw one coin and it has a probability of 0.5 (an unbiased coin) then we have two outcomes; we throw a heads or we throw a tails. Ok, that's fairly trivial. Let's add a second coin toss. Let's change the odds. Which in scipy is: Note again that we use . A Newbie Guide to pygame — Pygame v1.9.2 documentation. Or Things I learned by trial and error so you don’t have to, or How I learned to stop worrying and love the blit. Pygame is a python wrapper for SDL, written by Pete Shinners. What this means is that, using pygame, you can write games or other multimedia applications in Python that will run unaltered on any of SDL’s supported platforms (Windows, Unix, Mac, beOS and others).

Pygame may be easy to learn, but the world of graphics programming can be pretty confusing to the newcomer. Get comfortable working in Python. The most important thing is to feel confident using python. This may sound incredibly dull, but the confidence you’ll gain through your familiarity with python will work wonders when it comes time to write your game. Recognize which parts of pygame you really need. Looking at the jumble of classes at the top of the pygame Documentation index may be confusing. Know what a surface is. The most important part of pygame is the surface. So how do you create surfaces? Do: It’s that easy.

Using PyInstaller to make EXEs from Python scripts (and a 48-hour game design compo) | Irwin Kwan. Here’s how you use PyInstaller and PyGame to create a single-file executable from a project that has a data directory that contains resources like images, fonts, and music. Get PyInstaller.On Windows, you might also need pywin32 (and possibly MinGW if you don’t have Visual Studio).On Mac OS X, you will need XCode’s command line tools. To install the Command Line tools, first install XCode from the App Store, then go to Preferences – Downloads and there is an option to download them there.Modify your code so that whenever you refer to your data directory, you wrap it using the following function: def resource_path(relative): if hasattr(sys, "_MEIPASS"): return os.path.join(sys. Phew! That took me a long time – the better part of a few hours to figure out. This post on the PyInstaller list really helped. So why was I trying to package a Python executable file anyway?

This weekend, I decided to participate in a 48-hour game design “competition”. Here’s the I used for py2app. Importing data into Python | Programming For Biologists. Intelligently handle dropbox link · Issue #141 · ipython/nbviewer. 1.4. Matplotlib: plotting — Scipy lecture notes. 1.4.2. Simple plot Tip In this section, we want to draw the cosine and sine functions on the same plot. Starting from the default settings, we’ll enrich the figure step by step to make it nicer. First step is to get the data for the sine and cosine functions: import numpy as np X = np.linspace(-np.pi, np.pi, 256, endpoint=True)C, S = np.cos(X), np.sin(X) X is now a numpy array with 256 values ranging from -π to +π (included). To run the example, you can type them in an IPython interactive session: This brings us to the IPython prompt: IPython 0.13 -- An enhanced Interactive Python. ? You can also download each of the examples and run it using regular python, but you will loose interactive data manipulation: You can get source for each step by clicking on the corresponding figure.

Matplotlib comes with a set of default settings that allow customizing all kinds of properties. * 1.1, X.max() * 1.1)pl.ylim(C.min() * 1.1, C.max() * 1.1)... Hint. Adjust settings for matplotlib using rc and matplotlibrc « Py2Exe and MatPlotLib: Plot won't Appear. How to create Windows executable (.exe) from Python script. Python is a simple and powerful language for scripting and even application development. Various GUI packages available for Python makes it suitable for developing full fledged applications in python. Ok that is fine, but ever thought of creating an executable file from the python script you wrote? This seems to be a nice idea, there are many reasons why! You can redistribute your application without python. The end user needn't to install python on his machine. Introduction Python is a simple and powerful language for scripting and even application development.

This tutorial will give step by step instruction on how to create Win32 executable from Python script. Python. A console application The following code prints a header and numbers 1 to 10 to console output. Save this code in to (or any name with .py extension) file. Our Python script is ready. The code is simple and straight forward. Building the executable is finished. A GUI Application So far so good.. Downloads. Importing Python Modules. January 07, 1999 | Updated February 02, 2001 | Fredrik Lundh Introduction The import and from-import statements are a constant cause of serious confusion for newcomers to Python. Luckily, once you’ve figured out what they really do, you’ll never have problems with them again. This note tries to sort out some of the more common issues related to import and from-import and everything.

There are Many Ways to Import a Module Python provides at least three different ways to import modules. Anyway, here’s how these statements and functions work: import X imports the module X, and creates a reference to that module in the current namespace. Which Way Should I Use? Short answer: always use import. As usual, there are a number of exceptions to this rule: The Module Documentation Tells You To Use from-import. What Does Python Do to Import a Module?

When Python imports a module, it first checks the module registry (sys.modules) to see if the module is already imported. Import Gotchas Circular Imports. Python - Functions. A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. As you already know, Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions. Defining a Function You can define functions to provide the required functionality. Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).Any input parameters or arguments should be placed within these parentheses. Syntax def functionname( parameters ): "function_docstring" function_suite return [expression] By default, parameters have a positional behavior and you need to inform them in the same order that they were defined.

Example The following function takes a string as input parameter and prints it on standard screen. Calling a Function #! I'm first call to user defined function! #! #! #! 2.7. Mathematical optimization: finding minima of functions — Scipy lecture notes. Mathematical optimization deals with the problem of finding numerically minimums (or maximums or zeros) of a function. In this context, the function is called cost function, or objective function, or energy. Here, we are interested in using scipy.optimize for black-box optimization: we do not rely on the mathematical expression of the function that we are optimizing.

Note that this expression can often be used for more efficient, non black-box, optimization. Prerequisites Numpy, ScipyIPythonmatplotlib References Mathematical optimization is very ... mathematical. Convex Optimization by Boyd and Vandenberghe (pdf available free online).Numerical Optimization, by Nocedal and Wright. 2.7.1. Not all optimization problems are equal. Dimensionality of the problem The scale of an optimization problem is pretty much set by the dimensionality of the problem, i.e. the number of scalar variables on which the search is performed. Optimizing convex functions is easy. Note Noisy gradients. Python - Specifying constraints for fmin_cobyla in scipy.


Math with Python. Graphing. Data Munging. Python Tools. Python for Scientific Computing. Python.