Skip to content

Commit c1dad5c

Browse files
authored
Merge pull request #13 from hrani/stable
convert pylab to pyplot for mpld3
2 parents 527d9bf + 66df833 commit c1dad5c

File tree

2 files changed

+55
-34
lines changed

2 files changed

+55
-34
lines changed

findSim.py

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# -*- coding: utf-8 -*-
2-
1+
#
32
# This program is free software; you can redistribute it and/or
43
# modify it under the terms of the GNU General Public License as
54
# published by the Free Software Foundation; either version 3, or
@@ -34,9 +33,9 @@
3433
**********************************************************************/
3534
3635
'''
37-
from __future__ import print_function, division
36+
from __future__ import print_function
3837
import heapq
39-
import matplotlib.pyplot as plt
38+
#import pylab
4039
import numpy as np
4140
import sys
4241
import argparse
@@ -46,6 +45,22 @@
4645
import ntpath
4746
import time
4847
import imp # This is apparently deprecated in Python 3.4 and up
48+
49+
import matplotlib.pyplot as pyplot,mpld3
50+
51+
#mpld3 hack
52+
# suggested: https://github.com/mpld3/mpld3/issues/434
53+
import json
54+
class NumpyEncoder(json.JSONEncoder):
55+
def default(self, obj):
56+
import numpy as np
57+
if isinstance(obj, np.ndarray):
58+
return obj.tolist()
59+
return json.JSONEncoder.default(self, obj)
60+
from mpld3 import _display
61+
_display.NumpyEncoder = NumpyEncoder
62+
63+
4964
convertTimeUnits = {('sec','s') : 1.0,
5065
('ms','millisec', 'msec') : 1e-3,('us','microsec') : 1e-6,
5166
('ns','nanosec') : 1e-9, ('min','m') : 60.0,
@@ -281,11 +296,12 @@ def displayPlots( self, fname, modelLookup, stim, hideSubplots, exptType ):
281296
elms = modelLookup[i]
282297
for j in elms:
283298
pp = PlotPanel( self, exptType, xlabel = j.name +'('+stim.quantityUnits+')' )
299+
#pp.plotme( fname, pp.ylabel, joinSimPoints = True )
284300
try:
285301
pp.plotme( fname, pp.ylabel, joinSimPoints = True )
286302
except Exception as e:
287303
print('Warning: displayPlot: Failed to plot '
288-
'%s due to "%s"' % (fname,e))
304+
'%s due to "%s"' % (fname,e))
289305
elif "barchart" in exptType:
290306
for i in self.entities:
291307
elms = modelLookup[i]
@@ -325,17 +341,17 @@ def displayPlots( self, fname, modelLookup, stim, hideSubplots, exptType ):
325341
sumvec += ypts
326342
if (not hideSubplots) and (len( elms ) > 1):
327343
# Plot summed components
328-
plt.plot( xpts, ypts, 'r:', label = j.name )
344+
pyplot.plot( xpts, ypts, 'r:', label = j.name )
329345

330-
plt.plot( xpts, sumvec, 'r--' )
346+
pyplot.plot( xpts, sumvec, 'r--' )
331347
ylabel = pp.ylabel
332348
if self.field in ( epspFields + epscFields ):
333349
if self.field in ( epspFields ):
334-
plt.ylabel( '{} Vm ({})'.format( self.entities[0], tsUnits ) )
350+
pyplot.ylabel( '{} Vm ({})'.format( self.entities[0], tsUnits ) )
335351
else:
336-
plt.ylabel( '{} holding current ({})'.format( self.entities[0], tsUnits ) )
352+
pyplot.ylabel( '{} holding current ({})'.format( self.entities[0], tsUnits ) )
337353

338-
plt.figure(2)
354+
pyplot.figure(2)
339355
if self.useNormalization:
340356
ylabel = '{} Fold change'.format( self.field )
341357
pp.plotme( fname, ylabel )
@@ -596,7 +612,8 @@ def deleteItems( self, kinpath ):
596612
def subsetItems( self, kinpath ):
597613
nonContainers, directContainers, indirectContainers = [],[],[]
598614
for i in ['moregraphs', 'info', 'graphs']:
599-
directContainers.append( moose.element( kinpath + '/' + i ) )
615+
if moose.exists( kinpath + '/' + i ):
616+
directContainers.append( moose.element( kinpath + '/' + i ) )
600617
subsets = re.sub(r'\s', '', self.modelSubset).split(',')
601618

602619
for i in subsets:
@@ -1254,39 +1271,39 @@ def convertBarChartLabels( self, readout, stim ):
12541271
def plotbar( self, readout, stim, scriptName ):
12551272
barpos = np.arange( len( self.sim ) )
12561273
width = 0.35 # A reasonable looking bar width
1257-
exptBar = plt.bar(barpos - width/2, self.expt, width, yerr=self.yerror, color='SkyBlue', label='Experiment')
1258-
simBar = plt.bar(barpos + width/2, self.sim, width, color='IndianRed', label='Simulation')
1259-
plt.xlabel( "Stimulus combinations" )
1260-
plt.ylabel( self.ylabel )
1261-
plt.title(scriptName)
1262-
plt.legend(fontsize="small",loc="upper left")
1274+
exptBar = pyplot.bar(barpos - width/2, self.expt, width, yerr=self.yerror, color='SkyBlue', label='Experiment')
1275+
simBar = pyplot.bar(barpos + width/2, self.sim, width, color='IndianRed', label='Simulation')
1276+
pyplot.xlabel( "Stimulus combinations" )
1277+
pyplot.ylabel( self.ylabel )
1278+
pyplot.title(scriptName)
1279+
pyplot.legend(fontsize="small",loc="upper left")
12631280
ticklabels = [ i[0] + '\n' for i in readout.data ]
12641281
assert len( ticklabels ) == len( barpos )
12651282
ticklabels = self.convertBarChartLabels( readout, stim )
1266-
plt.xticks(barpos, ticklabels )
1283+
pyplot.xticks(barpos, ticklabels )
12671284

12681285
def plotme( self, scriptName, ylabel, joinSimPoints = False ):
12691286
sp = 'ro-' if joinSimPoints else 'ro'
12701287
if self.useXlog:
12711288
if self.useYlog:
1272-
plt.loglog( self.xpts, self.expt, 'bo-', label = 'expt', linewidth='2' )
1273-
plt.loglog( self.xpts, self.sim, sp, label = 'sim', linewidth='2' )
1289+
pyplot.loglog( self.xpts, self.expt, 'bo-', label = 'expt', linewidth='2' )
1290+
pyplot.loglog( self.xpts, self.sim, sp, label = 'sim', linewidth='2' )
12741291
else:
1275-
plt.semilogx( self.xpts, self.expt, 'bo-', label = 'expt', linewidth='2' )
1276-
plt.semilogx( self.xpts, self.sim, sp, label = 'sim', linewidth='2' )
1292+
pyplot.semilogx( self.xpts, self.expt, 'bo-', label = 'expt', linewidth='2' )
1293+
pyplot.semilogx( self.xpts, self.sim, sp, label = 'sim', linewidth='2' )
12771294
else:
12781295
if self.useYlog:
1279-
plt.semilogy( self.xpts, self.expt, 'bo-', label = 'expt', linewidth='2' )
1280-
plt.semilogy( self.xpts, self.sim, sp, label = 'sim', linewidth='2' )
1296+
pyplot.semilogy( self.xpts, self.expt, 'bo-', label = 'expt', linewidth='2' )
1297+
pyplot.semilogy( self.xpts, self.sim, sp, label = 'sim', linewidth='2' )
12811298
else:
1282-
plt.plot( self.xpts, self.expt,'bo-', label = 'experiment', linewidth='2' )
1283-
plt.errorbar( self.xpts, self.expt, yerr=self.yerror )
1284-
plt.plot( self.xpts, self.sim, sp, label = 'sim', linewidth='2' )
1299+
pyplot.plot( self.xpts, self.expt,'bo-', label = 'experiment', linewidth='2' )
1300+
pyplot.errorbar( self.xpts, self.expt, yerr=self.yerror )
1301+
pyplot.plot( self.xpts, self.sim, sp, label = 'sim', linewidth='2' )
12851302

1286-
plt.xlabel( self.xlabel )
1287-
plt.ylabel( ylabel )
1288-
plt.title(scriptName)
1289-
plt.legend(fontsize="small",loc="lower right")
1303+
pyplot.xlabel( self.xlabel )
1304+
pyplot.ylabel( ylabel )
1305+
pyplot.title(scriptName)
1306+
pyplot.legend(fontsize="small",loc="lower right")
12901307

12911308
########################################################################
12921309
def loadTsv( fname ):
@@ -1595,10 +1612,13 @@ def innerMain( script, modelFile = "model/synSynth7.g", dumpFname = "", paramFna
15951612
if not hidePlot:
15961613
print( "Score = {:.3f} for\t{}\tElapsed Time = {:.1f} s".format( score, os.path.basename(script), elapsedTime ) )
15971614
for i in readouts:
1598-
plt.figure(1)
1615+
pyplot.figure(1)
15991616
i.displayPlots( script, model.modelLookup, stims[0], hideSubplots, expt.exptType )
16001617

1601-
plt.show()
1618+
pyplot.show()
1619+
#pyplot_fig = mpld3.fig_to_html(pyplot.figure(1))
1620+
#mpld3.show()
1621+
16021622
moose.delete( modelId )
16031623
if moose.exists( '/library' ):
16041624
moose.delete( '/library' )

test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ else
1616
$PYTHON -m pip install matplotlib --upgrade
1717
$PYTHON -m pip install --upgrade
1818
fi
19-
19+
$PYTHON -m pip install Jinja2
20+
$PYTHON -m pip install mpld3
2021
$PYTHON -m pip install pymoose --pre --upgrade
2122
$PYTHON -m pip install pylint numpy --upgrade
2223

0 commit comments

Comments
 (0)