Posts

Showing posts with the label Python

Making boxplot with custom statistical values for the boxes

Image
We will see here how to draw boxplots, using Matplotlib, when we have a set of values that represent the box statistics, such as median, mean, minimum, maximum, first quartile, and third quartile. In some case we may also have confidence interval of the median values. We will use the .bxp attribute of the plot axes in pyplot.subplots . We begin first by importing the necessary packages to draw the boxplots, namely, NumPy and Matplotlib . import numpy as np import matplotlib.pyplot as plt The data Let’s take an example of a data. Show in the table below is the data which can be read from a excel or csv file into a pandas dataframe ( df ). df label whislo q1 med mean q3 whishi cilo cihi 0 A 8 26 56 49 96 116 54 62 1 B 7 21 53 45 96 122 51 59 2 C 10 22 57 54 101 120 55 63 3 D 9 26 54 50 98 116 52 60 Convert data into list of dictionaries For making the plot...

Running python code from file using exec()

Image
The exec function in python can be used to execute strings as python code. One of the simplest example can be as follows: a=2 exec("print(a)") 2 Note that we have not directly used the print() function to print the value of variable a , but have given the code print(a) as a string input to the exec function. The ability to excute strings as python code can be used to excute code stored in text files. A larger software tool can generrate a code in terms of variable that can be used in later steps of the program. It can also, be used to perfrom repetative tasks. Let’s say we have to run some imports statements and some code everytime you start a new session of python. We want those imported packages and variables available on the workspace console instead of inside a module. In this case you can just make a file that contains all those commands and run those commands via exec . We will see an example here. Let’s say we want to import matplotlib, numpy, pandas, s...

Fill missing values using SimpleImputer

Data often would contain missing values. Sometime it makes sense to fill the missing values with some appropriate value. For example we may want to fill the missing value with, say, mean of the available values. We can fill such missing values by calculating the mean of the column and using the fillna() function. However, if several columns have missing values then we might have to repeat this process several times or write a loop. Scikit-learn offers functionality called as SimpleImputer to easily fill the missing values . 

Python classes - Inheritance

In the previous post we saw how to create python classes and methods under them. We create a DNA class representing a DNA sequence. However, we can treat a DNA sequence as a string. A special kind of string that consists of only four letters, namely, ‘A’, ‘T’, ‘G’, and ‘C’ representing the nucleotides adenine, thiamine, guanine and cytosine, respectively. The DNA sequence should not contain any other characters. For ease of use we will allow entry of small and capital case letters which would be converted to capital case letter inside the class definition. Here we will create a class that is inherits properties from the built-in str class. class subclass(parent_class): # class definition To do so we just have to put the parent class in brackets while defining our current class. We can create as many subclasses that are themselves inherited from other subclasses in this way. class subclass(parent_class): # class definition class subclass_2(subclass): # class def...

Python classes : introduction

Classes are user-defined objects. Python has several built-in object types such as integers, float and strings. The programmers can create objects required for their program. We have seen some of the python objects previously. For example: a = 3 print ( type (a)) <class 'int'> Here, a is an int type of object.

Functions in python - args and kwargs

Earlier we saw how to write functions in python and how to call them in our program. For those functions, the number of inputs or arguments were defined. They took fixed number of positional or keyword arguments or had a default value assigned to one or more of the arguments. 

Functions in python

Functions are sets of statements that perform a specific task. They can be called by their names, more than once in a program. Inputs can be given to functions based on which they will perform a task and can give back the result. Functions avoid same code to be written over and over again, thus, reducing the redundancy of the code. They also modularize programs by assigining one task for one function. It also makes a program easy to correct. We would need to correct the code at only one place when a function is not working as desired as compared to situation where all the places where that set of code was wirtten has to be edited. The basic syntax of a function is as follows: def func(inputs): statments 1 . . . return result For example, a function can be written that return the result of addition of two numbers. It takes two numbers, a and b as input and returns the result. It can be written in a few ways: def add_num(a,b): c = a + ...

How to plot product concentrations in different strains using python?

Image
The most common type of graphs that we, as experimental biologists make, are bar graphs. When we want to compare: - the amount of a product secreted by different conditions or cells - The enzyme activity in different conditions or cells or similar cases when we want to compare the value of an observation at different conditions we typically plot a bar graph. Also, with replicates of experiments we plot the mean and standard deviations of the experiment. Excel is perhaps the quickest way to draw a single such graph but in case you want to make similar graphs for several observation or plot two or more such graphs in one figure as subplots, using python may be a better choice unless we want to spend time in adjusting ech graph into a powerpoint slide of in inkscape to make a collage. Here we will see how to plot these kind of graphs using python. We will use numpy , pandas and matplotlib packages to do this. We will take an example of observations depicting the concentration (g/l) ...