Skip to content

Commit

Permalink
Minor bug fixes with relation to path finding.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSafatli committed Feb 26, 2015
1 parent 9f6e869 commit 2155438
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pylogeny/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.3.9.9'
VERSION = '0.3.4.0'
2 changes: 0 additions & 2 deletions pylogeny/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

# Function Definitions

def LCS(str1,str2): return longest_common_substring(str1,str2)

def longest_common_substring(s1,s2):

''' Simplified, traditional LCS algorithm implementation. '''
Expand Down
2 changes: 1 addition & 1 deletion pylogeny/executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __exit__(self,*args): chdir(self.current)

class executable(object):

''' An interface for the instantation and running of a single instance for a
''' An abstract class for the instantation and running of a single instance for a
given application. '''

exeName = None
Expand Down
32 changes: 13 additions & 19 deletions pylogeny/landscape.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,47 +677,41 @@ def getBestImprovement(self,i):
a tree that leads to the best improvement of fitness function score
on the basis of likelihood. '''

nodes = self.graph.node
nodes = self.getNode
tree = self.getTree
ml = lambda d: tree(d).score[0]
if (not i in nodes):
ml = lambda d: tree(d).getScore()[0]
if (not i in self.getNodeNames()):
raise LookupError('No tree by that name in landscape.' % (i))
node = nodes[i]
node = nodes(i)
near = self.graph.neighbors(i)
if (len(near) == 0): return None
best = max(near,key=ml)
if (best != None and ml(best) > ml(i)): return best
if (best != None and ml(best) > ml(i)):
return best
else: return None

def getPathOfBestImprovement(self,i):

''' For a tree in the landscape, investigate neighbors iteratively until
a best path of score improvement is found on basis of likelihood. '''

path = []
nodes = self.graph.node
if (not i in nodes):
raise LookupError('No tree by that name in landscape.' % (i))
node = nodes[i]
curs = node
path = list()
node = self.getNode(i)
impr = self.getBestImprovement(i)
if not impr: return list()
else: besc = self.getTree(impr).score[0]
curs = node
if impr == None: return path

while (besc != None and besc > curs['tree'].score[0]):
while (impr != None):
path.append(impr)
curs = nodes[impr]
impr = self.getBestImprovement(i)
if not impr: besc = None
else: besc = self.getTree(impr).score[0]
impr = self.getBestImprovement(impr)

return path

def getAllPathsOfBestImprovement(self):

''' Return all paths of best improvement as a dictionary. '''

paths = {}
paths = dict()
for node in self.graph.node:
paths[node] = self.getPathOfBestImprovement(node)
return paths
Expand Down
8 changes: 7 additions & 1 deletion tests/landscapeTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def _exploreTreeByName(self,treeid):

def test_getTreeNewick(self):
treeNewick = self.landscape.getTree(0).getNewick()
self.assertTrue(type(treeNewick) == str and treeNewick != None)
self.assertTrue(type(treeNewick) == str and treeNewick != None)
self.assertTrue(len(treeNewick) > 0)

def test_addDuplicateTree(self):
randomTree = None
Expand Down Expand Up @@ -56,6 +57,11 @@ def test_getProperTrees(self):
for i in self.landscape.iterNodes():
stringList.append(self.landscape.getVertex(i).getProperNewick())
self.assertEquals(len(stringList),len(self.landscape))

def test_getPathOfBestImprovement(self):
ids = [0] + self.landscape.getPathOfBestImprovement(0)
ml = lambda i: self.landscape.getTree(i).getScore()[0]
self.assertTrue(all([ml(ids[i]) <= ml(ids[i+1]) for i in xrange(1,len(ids))]))

def test_readAndWrite(self):
writer = landscapeWriter(self.landscape,'al')
Expand Down

0 comments on commit 2155438

Please sign in to comment.