Skip to content

Commit 13d3894

Browse files
committed
Updated multi_param_minimization.py to print out more details about the scores for each experiment.
1 parent a2c37c6 commit 13d3894

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

multi_param_minimization.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def __init__( self, params, expts, weights, pool, modelFile ):
9494
self.weights = weights
9595
self.pool = pool # pool of available CPUs
9696
self.modelFile = modelFile
97+
self.score = []
9798

9899
def doEval( self, x ):
99100
ret = []
@@ -109,9 +110,9 @@ def doEval( self, x ):
109110

110111
for k in self.expts:
111112
ret.append( self.pool.apply_async( findSim.innerMain, (k,), dict(modelFile = self.modelFile, hidePlot=True, silent=True, scaleParam=paramList), callback = reportReturn ) )
112-
score = [ i.get() for i in ret ]
113-
sumScore = sum([ s*w for s,w in zip(score, self.weights) if s>=0.0])
114-
sumWts = sum( [ w for s,w in zip(score, self.weights) if s>=0.0 ] )
113+
self.score = [ i.get() for i in ret ]
114+
sumScore = sum([ s*w for s,w in zip(self.score, self.weights) if s>=0.0])
115+
sumWts = sum( [ w for s,w in zip(self.score, self.weights) if s>=0.0 ] )
115116
return sumScore/sumWts
116117

117118
def optCallback( x ):
@@ -157,6 +158,10 @@ def main():
157158
else:
158159
bounds.append( (0.0, 30 ) ) # Concs, Kfs and Kbs can be zero.
159160
ev = EvalFunc( params, fnames, weights, pool, modelFile )
161+
# Generate the score for each expt for the initial condition
162+
ev.doEval( [1.0]* len( params ) )
163+
initScore = ev.score
164+
# Do the minimization
160165
results = optimize.minimize( ev.doEval, np.ones( len(params) ), method='L-BFGS-B', tol = TOLERANCE, callback = optCallback, bounds = bounds )
161166
print( "\n----------- Completed in {:.3f} sec ---------- ".format(time.time() - t0 ) )
162167
print( "\n----- Score= {:.4f} ------ ".format(results.fun ) )
@@ -165,18 +170,32 @@ def main():
165170
if len( args.file ) > 0:
166171
fp = open( args.file, "w" )
167172
dumpData = True
168-
analyzeResults( fp, dumpData, results, params )
173+
analyzeResults( fp, dumpData, results, params, ev, initScore )
169174
if dumpData:
170175
fp.close()
171176

172-
def analyzeResults( fp, dumpData, results, params ):
177+
def analyzeResults( fp, dumpData, results, params, evalObj, initScore ):
173178
#assert( len(results.x) == len( results.fun ) )
174179
assert( len(results.x) == len( params ) )
180+
out = []
175181
for p,x, in zip(params, results.x):
176-
outputStr = "Parameter = {},\toptimized scale={:.3f}".format(p, x)
177-
print( outputStr )
182+
out.append( "Parameter = {:40s}scale = {:.3f}".format(p, x) )
183+
out.append( "\n{:40s}{:>12s}{:>12s}{:>12s}".format( "File", "initScore", "finalScore", "weight" ) )
184+
initSum = 0.0
185+
finalSum = 0.0
186+
numSum = 0.0
187+
assert( len( evalObj.expts ) == len( initScore ) )
188+
for e, i, f, w in zip( evalObj.expts, initScore, evalObj.score, evalObj.weights ):
189+
out.append( "{:40s}{:12.3f}{:12.3f}{:12.3f}".format( e, i, f, w ) )
190+
if i >= 0:
191+
initSum += i * w
192+
finalSum += f * w
193+
numSum += w
194+
out.append( "\nInit score = {:.4f}, final = {:.4f}".format(initSum/numSum, finalSum / numSum) )
195+
for i in out:
196+
print( i )
178197
if dumpData:
179-
fp.write( outputStr + '\n' )
198+
fp.write( i + '\n' )
180199

181200
# Run the 'main' if this script is executed standalone.
182201
if __name__ == '__main__':

0 commit comments

Comments
 (0)