Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Python 3 #3

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Code repository corresponding to paper "Detecting differential growth of microbi
* python (virtualenv recommended)

#### Download this repository
`git clone https://github.com/ptonner/gp_growth_phenotype.git`
`git clone https://github.com/SirRujak/gp_growth_phenotype.git`

#### Setup a local python environment (optional)
`virtualenv .`
Expand All @@ -24,6 +24,8 @@ Code repository corresponding to paper "Detecting differential growth of microbi

## Examples

For a basic example that does not use jupyter, look at example.py in the notebooks folder.

Input to the B-GREAT method should come from two dataframes, *data* (n x p) and *meta* (p x k) where n, p, and k are:
* n: number of time-points
* p: number of time-course measurements (replicates)
Expand Down
12 changes: 8 additions & 4 deletions bgreat.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import absolute_import
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import scipy, GPy, os, ast
import pandas as pd
import matplotlib as mpl
from six.moves import range
from six.moves import zip

meta = data = parent = condition = control = None

Expand All @@ -14,13 +18,13 @@

def plotSamples(samples,x=None,color='b',colors=None,plotMethod=None,label=None,*args,**kwargs):
if x is None:
x = range(samples.shape[0])
x = list(range(samples.shape[0]))
if colors is None:
colors = [color]*samples.shape[1]
if plotMethod is None:
plotMethod = plt.plot

for i,c in zip(range(samples.shape[1]),colors):
for i,c in zip(list(range(samples.shape[1])),colors):
if not label is None:
plotMethod(x,samples[:,i],color=c,label=label,*args,**kwargs)
label = None
Expand All @@ -38,7 +42,7 @@ def setGlobals(_data=None,_meta=None,_parent=None,_condition=None,_control=None)
global data
data = _data

data.columns = range(data.shape[1])
data.columns = list(range(data.shape[1]))

if not _meta is None:
global meta
Expand Down Expand Up @@ -234,7 +238,7 @@ def testMutants(mutants,numPerm=10,timeThin=4,dims=[],nullDim='strain-regression
for i,m in enumerate(mutants):

if (i + 1)%10 == 0:
print 1.*i/len(mutants),m
print(1.*i/len(mutants),m)

select = ((meta.Condition==control) | (meta.Condition==condition)) & (meta.strain.isin([parent,m]))
if m in results:
Expand Down
14 changes: 9 additions & 5 deletions gp_growth/analysis.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from __future__ import absolute_import
from __future__ import print_function
import pandas as pd
import numpy as np
from ConfigParser import ConfigParser
from six.moves.configparser import ConfigParser
from registry import registry
from gp_growth import storage
import os, gc
from six.moves import range
from six.moves import zip

class Analysis():

Expand Down Expand Up @@ -111,16 +115,16 @@ def run(self,verbosity=None):
continue

# build a dictionary of key-value pairs for selecting data
kwargs = dict(zip(self.groupby,vals))
kwargs = dict(list(zip(self.groupby,vals)))

temp = self.data.select(**kwargs)

if verbosity > 0:
print kwargs, temp.key.shape[0]
print(kwargs, temp.key.shape[0])

if temp.key.shape[0] > self.maxSize:
if verbosity > 0:
print "Number of samples to large (%d > %d)" % (temp.key.shape[0],self.maxSize)
print("Number of samples to large (%d > %d)" % (temp.key.shape[0],self.maxSize))
continue

regression = temp.getData("gp")
Expand All @@ -133,7 +137,7 @@ def run(self,verbosity=None):
# del temp
collected = gc.collect()
if verbosity > 0:
print "Garbage collector: collected %d objects." % (collected)
print("Garbage collector: collected %d objects." % (collected))

if __name__ == "__main__":
import sys, getopt
Expand Down
8 changes: 5 additions & 3 deletions gp_growth/categorical.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import
from __future__ import print_function
from GPy.kern import Kern,RBF
from GPy.core.parameterization.transformations import Logexp
from GPy.core.parameterization import Param
Expand Down Expand Up @@ -37,15 +39,15 @@ def update_gradients_full(self, dL_dK, X, X2=None):
self.variance.gradient = np.nansum(dL_dK * np.where(X == X2.T,1,0))

def update_gradients_diag(self, dL_dKdiag, X):
print "update_gradients_diag"
print("update_gradients_diag")
self.variance.gradient = np.sum(dL_dKdiag * np.ones(X.shape[0]))

def gradients_X(self, dL_dK, X, X2):
print "gradients_X"
print("gradients_X")
if X2 is None:
X2 = X
return dL_dK * np.where(X == X2.T,1,0)

def gradients_X_diag(self, dL_dKdiag, X):
print "gradients_X_diag"
print("gradients_X_diag")
return dL_dKdiag * np.ones(X.shape[0])
9 changes: 6 additions & 3 deletions gp_growth/changepoint.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import absolute_import
from __future__ import print_function
from gp_growth import factory
from gp_growth.categorical import Categorical
import GPy
from GPy.core.parameterization import Param
from GPy.core.parameterization.transformations import Logexp
import numpy as np
from six.moves import range

CP_TOL = 1e-5

Expand Down Expand Up @@ -130,7 +133,7 @@ class ChangepointCross(GPy.kern._src.kern.CombinationKernel):
"""Kernel for points across a changepoint. K(X,Y) = kc * K1(X,cp) * K2(Y,cp) """

def __init__(self,cp,kc = 1,k=1,**kwargs):
ad = [0] + range(2,k+1)
ad = [0] + list(range(2,k+1))

super(ChangepointCross,self).__init__([GPy.kern.RBF(k,active_dims=ad,ARD=True,**kwargs),GPy.kern.RBF(k,active_dims=ad,ARD=True,**kwargs)],"changepoint")
self.k = k
Expand All @@ -157,7 +160,7 @@ def Kdiag(self,X):
return self.kc * self.parts[0].Kdiag(X) * self.parts[1].Kdiag(X)

def update_gradients_full(self, dL_dK, X, X2=None):
print "cp_cross update_gradients_full"
print("cp_cross update_gradients_full")
k = self.K(X,X2)*dL_dK
# try:
for p in self.parts:
Expand Down Expand Up @@ -208,7 +211,7 @@ def buildKernelPreprocess(self,):
def buildKernel(self,):

k = len(self.inputDimensions) - 1
ad = [0] + range(2,k+1)
ad = [0] + list(range(2,k+1))

cp = self.cp
if self.normalize:
Expand Down
11 changes: 7 additions & 4 deletions gp_growth/changepoint_multidim.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import absolute_import
from __future__ import print_function
from gp_growth import factory
from gp_growth.categorical import Categorical
import GPy
from GPy.core.parameterization import Param
from GPy.core.parameterization.transformations import Logexp
from GPy.kern import Kern, RBF
import numpy as np
from six.moves import range

CP_TOL = 1e-5

Expand Down Expand Up @@ -51,7 +54,7 @@ def __init__(self,input_dim=1,k1=None,k2=None,kc=None,xc=None,cpDim=None,*args,*
self.cpDim = cpDim
if self.cpDim is None:
self.cpDim = 0
self.otherDims = range(self.input_dim)
self.otherDims = list(range(self.input_dim))
self.otherDims.remove(self.cpDim)
self.otherDims = np.array(self.otherDims)

Expand Down Expand Up @@ -298,7 +301,7 @@ class ChangepointCross(GPy.kern._src.kern.CombinationKernel):
"""Kernel for points across a changepoint. K(X,Y) = kc * K1(X,cp) * K2(Y,cp) """

def __init__(self,cp,kc = 1,k=1,**kwargs):
ad = [0] + range(2,k+1)
ad = [0] + list(range(2,k+1))

super(ChangepointCross,self).__init__([GPy.kern.RBF(k,active_dims=ad,ARD=True,**kwargs),GPy.kern.RBF(k,active_dims=ad,ARD=True,**kwargs)],"changepoint")
self.k = k
Expand All @@ -325,7 +328,7 @@ def Kdiag(self,X):
return self.kc * self.parts[0].Kdiag(X) * self.parts[1].Kdiag(X)

def update_gradients_full(self, dL_dK, X, X2=None):
print "cp_cross update_gradients_full"
print("cp_cross update_gradients_full")
k = self.K(X,X2)*dL_dK
# try:
for p in self.parts:
Expand Down Expand Up @@ -376,7 +379,7 @@ def buildKernelPreprocess(self,):
def buildKernel(self,):

k = len(self.inputDimensions) - 1
ad = [0] + range(2,k+1)
ad = [0] + list(range(2,k+1))

cp = self.cp
if self.normalize:
Expand Down
5 changes: 3 additions & 2 deletions gp_growth/classical.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import
import numpy as np
from scipy.optimize import curve_fit

Expand Down Expand Up @@ -26,7 +27,7 @@ def meanSquareError(time,od,model=gompertz):

predict = np.array([gompertz(t,*popt) for t in time])
mse = 1./n * sum(((od - predict)**2),)
except RuntimeError, e:
except RuntimeError as e:
mse = np.nan# 1./n * sum(((od - np.mean(od))**2),)

return mse
Expand Down Expand Up @@ -62,7 +63,7 @@ def optimize(time,od,model=gompertz,p0=None,cv=None):

predict = np.array([model(t,*popt) for t in test_time])
mse = 1./test_od.shape[0] * sum(((test_od - predict)**2),)
except RuntimeError, e:
except RuntimeError as e:
m,A,l = np.nan, np.nan, np.nan
mse = np.nan

Expand Down
3 changes: 2 additions & 1 deletion gp_growth/data/filter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import growth
from __future__ import absolute_import
from . import growth

class DataFilter(object):

Expand Down
37 changes: 21 additions & 16 deletions gp_growth/data/growth.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from __future__ import absolute_import
from __future__ import print_function
import matplotlib.pyplot as plt
import seaborn as sns
import os, time, datetime
import pandas as pd
import numpy as np
import itertools
import six
from six.moves import range
from six.moves import zip

class GrowthData(object):

Expand Down Expand Up @@ -64,7 +69,7 @@ def plot(self,output="",fixed_y=True,title_index=None,colorby=None,fixed_colors=
condition = condition.tolist()

if title_index is None:
title_index = range(len(condition))
title_index = list(range(len(condition)))

if newFig is None:
newFig = True
Expand All @@ -82,9 +87,9 @@ def plot(self,output="",fixed_y=True,title_index=None,colorby=None,fixed_colors=
if colorby:
categories = tuple(self.key[colorby].unique())
if colors is None:
color_lookup = dict(zip(categories,sns.color_palette("hls", len(categories))))
color_lookup = dict(list(zip(categories,sns.color_palette("hls", len(categories)))))
else:
color_lookup = dict(zip(categories,colors))
color_lookup = dict(list(zip(categories,colors)))

ncols = min(ncols,groups.ngroups)
nrows = 1
Expand All @@ -97,12 +102,12 @@ def plot(self,output="",fixed_y=True,title_index=None,colorby=None,fixed_colors=
plt.figure(figsize=(ncols*figWidth,nrows*figHeight))

if groups.ngroups > maxgroups:
print "Error, number of groups (%d) is greater than max (%d)." % (groups.ngroups,maxgroups)
print("Error, number of groups (%d) is greater than max (%d)." % (groups.ngroups,maxgroups))
return

groupList = list(groups.groups.iteritems())
groupList = list(six.iteritems(groups.groups))
if groupOrder is None:
groupOrder = range(len(groupList))
groupOrder = list(range(len(groupList)))
for i,j in enumerate(groupOrder):
val = groupList[j]

Expand All @@ -119,10 +124,10 @@ def plot(self,output="",fixed_y=True,title_index=None,colorby=None,fixed_colors=

if not fixed_colors:
categories = tuple(temp_key[colorby].unique())
color_lookup = dict(zip(categories,sns.color_palette("hls", len(categories))))
color_lookup = dict(list(zip(categories,sns.color_palette("hls", len(categories)))))

if colorby:
color_usage = dict(zip(categories,[False]*len(categories)))
color_usage = dict(list(zip(categories,[False]*len(categories))))

#print temp_key.Well,temp_data.head()

Expand All @@ -132,7 +137,7 @@ def plot(self,output="",fixed_y=True,title_index=None,colorby=None,fixed_colors=
ylim = (min(temp_data.min()),max(temp_data.max()))

if colorby:
for well,temp_od in temp_data.iteritems():
for well,temp_od in six.iteritems(temp_data):

#remove na's
select = ~temp_od.isnull()
Expand All @@ -152,7 +157,7 @@ def plot(self,output="",fixed_y=True,title_index=None,colorby=None,fixed_colors=
if len(categories) <= max_legend:
plt.legend(loc="best",**legend_kwargs)
else:
for well,temp_od in temp_data.iteritems():
for well,temp_od in six.iteritems(temp_data):

#remove na's
select = ~temp_od.isnull()
Expand Down Expand Up @@ -263,7 +268,7 @@ def _stack_data(self,meta=None,groupby=None,thinning=None):
x = []
y = []

for vals,ind in g.groups.iteritems():
for vals,ind in six.iteritems(g.groups):
select = reg[groupby] == vals
temp = reg[select]
x.append(reg[['time']+meta])
Expand Down Expand Up @@ -308,7 +313,7 @@ def select(self,**kwargs):

selection = [True]*self.key.shape[0]

for k,v in kwargs.iteritems():
for k,v in six.iteritems(kwargs):
if k in key_copy.columns:
# if v is np.nan:
# if np.isnan(v):
Expand All @@ -317,7 +322,7 @@ def select(self,**kwargs):
if np.isnan(v):
selection = np.all((selection,key_copy[k].isnull()),0)
checked = True
except TypeError,e:
except TypeError as e:
pass
if not checked:
selection = np.all((selection,key_copy[k]==v),0)
Expand Down Expand Up @@ -368,7 +373,7 @@ def poly_scale(self,p,ind=None,groupby=None):

time = self.data.time.iloc[:ind]

for k,index in group.groups.iteritems():
for k,index in six.iteritems(group.groups):
temp = self.data.loc[:,index]
od = temp.values[:ind,:].ravel()

Expand All @@ -380,13 +385,13 @@ def poly_scale(self,p,ind=None,groupby=None):
def _parse_time(t):
try:
return time.struct_time(time.strptime(t,'%H:%M:%S'))
except ValueError, e:
except ValueError as e:
try:
t = time.strptime(t,'%d %H:%M:%S')
t = list(t)
t[2]+=1
return time.struct_time(t)
except ValueError, e:
except ValueError as e:
raise Exception("Time format unknown")

def _expand_data_row(r,thinning=0):
Expand Down
Loading