|
1 | | -# -*- coding: utf-8 -*- |
2 | | - |
| 1 | +# |
3 | 2 | # This program is free software; you can redistribute it and/or |
4 | 3 | # modify it under the terms of the GNU General Public License as |
5 | 4 | # published by the Free Software Foundation; either version 3, or |
|
34 | 33 | **********************************************************************/ |
35 | 34 |
|
36 | 35 | ''' |
37 | | -from __future__ import print_function, division |
| 36 | +from __future__ import print_function |
38 | 37 | import heapq |
39 | | -import matplotlib.pyplot as plt |
| 38 | +#import pylab |
40 | 39 | import numpy as np |
41 | 40 | import sys |
42 | 41 | import argparse |
|
46 | 45 | import ntpath |
47 | 46 | import time |
48 | 47 | 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 | + |
49 | 64 | convertTimeUnits = {('sec','s') : 1.0, |
50 | 65 | ('ms','millisec', 'msec') : 1e-3,('us','microsec') : 1e-6, |
51 | 66 | ('ns','nanosec') : 1e-9, ('min','m') : 60.0, |
@@ -281,11 +296,12 @@ def displayPlots( self, fname, modelLookup, stim, hideSubplots, exptType ): |
281 | 296 | elms = modelLookup[i] |
282 | 297 | for j in elms: |
283 | 298 | pp = PlotPanel( self, exptType, xlabel = j.name +'('+stim.quantityUnits+')' ) |
| 299 | + #pp.plotme( fname, pp.ylabel, joinSimPoints = True ) |
284 | 300 | try: |
285 | 301 | pp.plotme( fname, pp.ylabel, joinSimPoints = True ) |
286 | 302 | except Exception as e: |
287 | 303 | print('Warning: displayPlot: Failed to plot ' |
288 | | - '%s due to "%s"' % (fname,e)) |
| 304 | + '%s due to "%s"' % (fname,e)) |
289 | 305 | elif "barchart" in exptType: |
290 | 306 | for i in self.entities: |
291 | 307 | elms = modelLookup[i] |
@@ -325,17 +341,17 @@ def displayPlots( self, fname, modelLookup, stim, hideSubplots, exptType ): |
325 | 341 | sumvec += ypts |
326 | 342 | if (not hideSubplots) and (len( elms ) > 1): |
327 | 343 | # Plot summed components |
328 | | - plt.plot( xpts, ypts, 'r:', label = j.name ) |
| 344 | + pyplot.plot( xpts, ypts, 'r:', label = j.name ) |
329 | 345 |
|
330 | | - plt.plot( xpts, sumvec, 'r--' ) |
| 346 | + pyplot.plot( xpts, sumvec, 'r--' ) |
331 | 347 | ylabel = pp.ylabel |
332 | 348 | if self.field in ( epspFields + epscFields ): |
333 | 349 | if self.field in ( epspFields ): |
334 | | - plt.ylabel( '{} Vm ({})'.format( self.entities[0], tsUnits ) ) |
| 350 | + pyplot.ylabel( '{} Vm ({})'.format( self.entities[0], tsUnits ) ) |
335 | 351 | else: |
336 | | - plt.ylabel( '{} holding current ({})'.format( self.entities[0], tsUnits ) ) |
| 352 | + pyplot.ylabel( '{} holding current ({})'.format( self.entities[0], tsUnits ) ) |
337 | 353 |
|
338 | | - plt.figure(2) |
| 354 | + pyplot.figure(2) |
339 | 355 | if self.useNormalization: |
340 | 356 | ylabel = '{} Fold change'.format( self.field ) |
341 | 357 | pp.plotme( fname, ylabel ) |
@@ -596,7 +612,8 @@ def deleteItems( self, kinpath ): |
596 | 612 | def subsetItems( self, kinpath ): |
597 | 613 | nonContainers, directContainers, indirectContainers = [],[],[] |
598 | 614 | 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 ) ) |
600 | 617 | subsets = re.sub(r'\s', '', self.modelSubset).split(',') |
601 | 618 |
|
602 | 619 | for i in subsets: |
@@ -1254,39 +1271,39 @@ def convertBarChartLabels( self, readout, stim ): |
1254 | 1271 | def plotbar( self, readout, stim, scriptName ): |
1255 | 1272 | barpos = np.arange( len( self.sim ) ) |
1256 | 1273 | 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") |
1263 | 1280 | ticklabels = [ i[0] + '\n' for i in readout.data ] |
1264 | 1281 | assert len( ticklabels ) == len( barpos ) |
1265 | 1282 | ticklabels = self.convertBarChartLabels( readout, stim ) |
1266 | | - plt.xticks(barpos, ticklabels ) |
| 1283 | + pyplot.xticks(barpos, ticklabels ) |
1267 | 1284 |
|
1268 | 1285 | def plotme( self, scriptName, ylabel, joinSimPoints = False ): |
1269 | 1286 | sp = 'ro-' if joinSimPoints else 'ro' |
1270 | 1287 | if self.useXlog: |
1271 | 1288 | 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' ) |
1274 | 1291 | 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' ) |
1277 | 1294 | else: |
1278 | 1295 | 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' ) |
1281 | 1298 | 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' ) |
1285 | 1302 |
|
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") |
1290 | 1307 |
|
1291 | 1308 | ######################################################################## |
1292 | 1309 | def loadTsv( fname ): |
@@ -1595,10 +1612,13 @@ def innerMain( script, modelFile = "model/synSynth7.g", dumpFname = "", paramFna |
1595 | 1612 | if not hidePlot: |
1596 | 1613 | print( "Score = {:.3f} for\t{}\tElapsed Time = {:.1f} s".format( score, os.path.basename(script), elapsedTime ) ) |
1597 | 1614 | for i in readouts: |
1598 | | - plt.figure(1) |
| 1615 | + pyplot.figure(1) |
1599 | 1616 | i.displayPlots( script, model.modelLookup, stims[0], hideSubplots, expt.exptType ) |
1600 | 1617 |
|
1601 | | - plt.show() |
| 1618 | + pyplot.show() |
| 1619 | + #pyplot_fig = mpld3.fig_to_html(pyplot.figure(1)) |
| 1620 | + #mpld3.show() |
| 1621 | + |
1602 | 1622 | moose.delete( modelId ) |
1603 | 1623 | if moose.exists( '/library' ): |
1604 | 1624 | moose.delete( '/library' ) |
|
0 commit comments