Permalink
Browse files

KDTree/Medline/NMR/NeuralNetwork: Some more PEP8 whitespace cleanup.

  • Loading branch information...
cbrueffer authored and peterjc committed Dec 11, 2012
1 parent b28d709 commit ee9fc123a0d183bed9bf384fb1323bb9b6deb43d
View
@@ -21,8 +21,8 @@
def _dist(p, q):
- diff=p-q
- return sqrt(sum(diff*diff))
+ diff = p - q
+ return sqrt(sum(diff * diff))
def _neighbor_test(nr_points, dim, bucket_size, radius):
@@ -37,23 +37,23 @@ def _neighbor_test(nr_points, dim, bucket_size, radius):
o radius - radius of search (typically 0.05 or so)
"""
# KD tree search
- kdt=_CKDTree.KDTree(dim, bucket_size)
- coords=random((nr_points, dim))
+ kdt = _CKDTree.KDTree(dim, bucket_size)
+ coords = random((nr_points, dim))
kdt.set_data(coords)
neighbors = kdt.neighbor_search(radius)
r = [neighbor.radius for neighbor in neighbors]
if r is None:
- l1=0
+ l1 = 0
else:
- l1=len(r)
+ l1 = len(r)
# now do a slow search to compare results
neighbors = kdt.neighbor_simple_search(radius)
r = [neighbor.radius for neighbor in neighbors]
if r is None:
- l2=0
+ l2 = 0
else:
- l2=len(r)
- if l1==l2:
+ l2 = len(r)
+ if l1 == l2:
print "Passed."
else:
print "Not passed: %i != %i." % (l1, l2)
@@ -70,23 +70,23 @@ def _test(nr_points, dim, bucket_size, radius):
o radius - radius of search (typically 0.05 or so)
"""
# kd tree search
- kdt=_CKDTree.KDTree(dim, bucket_size)
- coords=random((nr_points, dim))
- center=coords[0]
+ kdt = _CKDTree.KDTree(dim, bucket_size)
+ coords = random((nr_points, dim))
+ center = coords[0]
kdt.set_data(coords)
kdt.search_center_radius(center, radius)
- r=kdt.get_indices()
+ r = kdt.get_indices()
if r is None:
- l1=0
+ l1 = 0
else:
- l1=len(r)
- l2=0
+ l1 = len(r)
+ l2 = 0
# now do a manual search to compare results
for i in range(0, nr_points):
- p=coords[i]
- if _dist(p, center)<=radius:
- l2=l2+1
- if l1==l2:
+ p = coords[i]
+ if _dist(p, center) <= radius:
+ l2 = l2 + 1
+ if l1 == l2:
print "Passed."
else:
print "Not passed: %i != %i." % (l1, l2)
@@ -126,9 +126,9 @@ class KDTree(object):
"""
def __init__(self, dim, bucket_size=1):
- self.dim=dim
- self.kdt=_CKDTree.KDTree(dim, bucket_size)
- self.built=0
+ self.dim = dim
+ self.kdt = _CKDTree.KDTree(dim, bucket_size)
+ self.built = 0
# Set data
@@ -139,12 +139,12 @@ def set_coords(self, coords):
have dimensionality D and there are N points, the coords
array should be NxD dimensional.
"""
- if coords.min()<=-1e6 or coords.max()>=1e6:
+ if coords.min() <= -1e6 or coords.max() >= 1e6:
raise Exception("Points should lie between -1e6 and 1e6")
- if len(coords.shape)!=2 or coords.shape[1]!=self.dim:
+ if len(coords.shape) != 2 or coords.shape[1] != self.dim:
raise Exception("Expected a Nx%i NumPy array" % self.dim)
self.kdt.set_data(coords)
- self.built=1
+ self.built = 1
# Fixed radius search for a point
@@ -157,7 +157,7 @@ def search(self, center, radius):
"""
if not self.built:
raise Exception("No point set specified")
- if center.shape!=(self.dim,):
+ if center.shape != (self.dim,):
raise Exception("Expected a %i-dimensional NumPy array"
% self.dim)
self.kdt.search_center_radius(center, radius)
@@ -168,7 +168,7 @@ def get_radii(self):
Return the list of distances from center after
a neighbor search.
"""
- a=self.kdt.get_radii()
+ a = self.kdt.get_radii()
if a is None:
return []
return a
@@ -182,7 +182,7 @@ def get_indices(self):
For an index pair, the first index<second index.
"""
- a=self.kdt.get_indices()
+ a = self.kdt.get_indices()
if a is None:
return []
return a
@@ -219,16 +219,16 @@ def all_get_radii(self):
"""
return [neighbor.radius for neighbor in self.neighbors]
-if __name__=="__main__":
+if __name__ == "__main__":
- nr_points=100000
- dim=3
- bucket_size=10
- query_radius=10
+ nr_points = 100000
+ dim = 3
+ bucket_size = 10
+ query_radius = 10
- coords=(200*random((nr_points, dim)))
+ coords = (200 * random((nr_points, dim)))
- kdtree=KDTree(dim, bucket_size)
+ kdtree = KDTree(dim, bucket_size)
# enter coords
kdtree.set_coords(coords)
@@ -242,23 +242,23 @@ def all_get_radii(self):
# indices is a list of tuples. Each tuple contains the
# two indices of a point pair within query_radius of
# each other.
- indices=kdtree.all_get_indices()
- radii=kdtree.all_get_radii()
+ indices = kdtree.all_get_indices()
+ radii = kdtree.all_get_radii()
print "Found %i point pairs within radius %f." % (len(indices), query_radius)
# Do 10 individual queries
for i in range(0, 10):
# pick a random center
- center=random(dim)
+ center = random(dim)
# search neighbors
kdtree.search(center, query_radius)
# get indices & radii of points
- indices=kdtree.get_indices()
- radii=kdtree.get_radii()
+ indices = kdtree.get_indices()
+ radii = kdtree.get_radii()
- x, y, z=center
+ x, y, z = center
print "Found %i points in radius %f around center (%.2f, %.2f, %.2f)." % (len(indices), query_radius, x, y, z)
View
@@ -127,7 +127,7 @@ def parse(handle):
record = Record()
finished = False
while not finished:
- if line[:6]==" ": # continuation line
+ if line[:6] == " ": # continuation line
record[key].append(line[6:])
elif line:
key = line[:4].rstrip()
View
@@ -9,7 +9,7 @@
import xpktools
-def predictNOE(peaklist,originNuc,detectedNuc,originResNum,toResNum):
+def predictNOE(peaklist, originNuc, detectedNuc, originResNum, toResNum):
# Predict the i->j NOE position based on self peak (diagonal) assignments
#
# example predictNOE(peaklist,"N15","H1",10,12)
@@ -23,40 +23,39 @@ def predictNOE(peaklist,originNuc,detectedNuc,originResNum,toResNum):
# assumption holds true. Check your peaklist for errors and
# off diagonal peaks before attempting to use predictNOE.
- returnLine = "" # The modified line to be returned to the caller
+ returnLine = "" # The modified line to be returned to the caller
datamap = _data_map(peaklist.datalabels)
# Construct labels for keying into dictionary
- originAssCol = datamap[originNuc+".L"]+1
- originPPMCol = datamap[originNuc+".P"]+1
- detectedPPMCol = datamap[detectedNuc+".P"]+1
+ originAssCol = datamap[originNuc + ".L"] + 1
+ originPPMCol = datamap[originNuc + ".P"] + 1
+ detectedPPMCol = datamap[detectedNuc + ".P"] + 1
# Make a list of the data lines involving the detected
if str(toResNum) in peaklist.residue_dict(detectedNuc) \
and str(originResNum) in peaklist.residue_dict(detectedNuc):
- detectedList=peaklist.residue_dict(detectedNuc)[str(toResNum)]
- originList=peaklist.residue_dict(detectedNuc)[str(originResNum)]
- returnLine=detectedList[0]
+ detectedList = peaklist.residue_dict(detectedNuc)[str(toResNum)]
+ originList = peaklist.residue_dict(detectedNuc)[str(originResNum)]
+ returnLine = detectedList[0]
for line in detectedList:
-
- aveDetectedPPM = _col_ave(detectedList,detectedPPMCol)
- aveOriginPPM = _col_ave(originList,originPPMCol)
+ aveDetectedPPM = _col_ave(detectedList, detectedPPMCol)
+ aveOriginPPM = _col_ave(originList, originPPMCol)
originAss = originList[0].split()[originAssCol]
- returnLine=xpktools.replace_entry(returnLine,originAssCol+1,originAss)
- returnLine=xpktools.replace_entry(returnLine,originPPMCol+1,aveOriginPPM)
+ returnLine = xpktools.replace_entry(returnLine, originAssCol + 1, originAss)
+ returnLine = xpktools.replace_entry(returnLine, originPPMCol + 1, aveOriginPPM)
return returnLine
def _data_map(labelline):
# Generate a map between datalabels and column number
# based on a labelline
- i=0 # A counter
- datamap={} # The data map dictionary
- labelList=labelline.split() # Get the label line
+ i = 0 # A counter
+ datamap = {} # The data map dictionary
+ labelList = labelline.split() # Get the label line
# Get the column number for each label
for i in range(len(labelList)):
@@ -65,11 +64,11 @@ def _data_map(labelline):
return datamap
-def _col_ave(list,col):
+def _col_ave(list, col):
# Compute average values from a particular column in a string list
- total=0
- n=0
+ total = 0
+ n = 0
for element in list:
total += float(element.split()[col])
n += 1
- return total/n
+ return total / n
Oops, something went wrong.

0 comments on commit ee9fc12

Please sign in to comment.