-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandInterp.py
637 lines (554 loc) · 23.5 KB
/
CommandInterp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
import os.path
import PyQt5.QtCore
from PyQt5.QtCore import QProcess, QObject, pyqtSignal
import sys
from sys import platform as _platform
from math import *
from tokenize import *
from numpy import *
from numpy.fft import *
import skimage
from skimage import data
import imghdr
import ntpath
import re
import numpy as np
import h5py
import EngEvaluate
import ReadSpiceRaw
import SpiceVarExpr
import ImgMplCanvas
# di show available vars from raw file
# show list user-assigned variables
# push go into raw file subcircuit
# pop exit subcircuit
# gs graph on same axes
# set ylin
# set ylog
# set xl 1k 10k
# label time, date
# ticks, grid, number of divisions
# TODO: Look at how subcircuits are implemented for di and voltage graphing, consider push/pop
# TODO:
# implement HDF5 file reader
# implement Python program output as transcript
# implement save-all as ascii
# implement restore-all as ascii
# save state from one run to the next
# command to reset state to default
# implement gr x .vs y
# implement freq instead of time as an axis, vdb, idb, vm, im
# AC analysis - complex numbers from raw file
# 2D plots for thermal solutions
# Add web server to inject remote commands via http
# gr 3
# Can't get length of integer.
# Needs to check that expr is an array, or better extend int to array.
# NGSpice raw file has all the waveform names in lower case.
# Would like option to make graphing them case insensitive.
# Path to NGSpice should be configured in a pull-down from the Main Window.
# Layout for canvas: http://matplotlib.org/users/tight_layout_guide.html
class CommandInterp:
def __init__(self):
# TODO: Move xvar to graphics delegate
self._globals= globals()
self.evaluator= EngEvaluate.EngEvaluate(self._globals)
self.pyCode= ''
self.sve= SpiceVarExpr.SpiceVarExpr()
# Move to simulator delegate
self.rawFileName= ''
self.spiceFileName= ''
self.simulationBaseName= ''
self.lastGraphCommand1= ''
self.lastGraphCommand2= ''
# Move to graphics delegate
#self.xvar= 't'
#self.title=''
return
def executeCmd(self, cmdText):
cmdText=cmdText.rstrip()
cmdText=cmdText.lstrip()
print("Command: " + cmdText)
message= ''
longCmd= re.match(r'^(ci|gr|gs|set|si|history|readh5|include|\.|img|delete) (.*)', cmdText)
shortCmd= re.match(r'^(ci|set|si|di|history|autoscale)$', cmdText)
if longCmd is not None:
message= self.executeLongCommand(longCmd, cmdText)
elif shortCmd is not None:
message= self.executeShortCommand(shortCmd)
else:
cmdText= self.sve.fixWaveFormVariableNames(cmdText)
message= self.evaluator.evaluate(cmdText)
self.pyCode= self.evaluator.logPyCode
return message
def executeShortCommand(self, shortCmd):
message= ''
cmd= shortCmd.group(1)
if cmd == 'ci':
if self.simulationBaseName == '':
message= "No simulation specified"
self.pyCode= ''
else:
message= "Circuit is: " + self.simulationBaseName
self.pyCode= "sim.setCircuitName(" + self.simulationBaseName + ")"
elif cmd == 'set':
message = self.showSettings()
self.pyCode= ''
elif cmd == 'si':
if self.simulationBaseName != '':
self.pyCode= "sim.simulate()"
message= self.simulate('')
self.readRawFile()
else:
message= "Circuit name has not been set yet"
elif cmd == 'di':
message = self.displaySweepVar()
message = message + self.displayVoltages()
message = message + self.displayCurrents()
self.pyCode= "print(r.getVoltageNames())\nprint(r.getCurrentNames())"
elif cmd == 'history':
print("History not implemented yet.")
elif cmd == 'autoscale':
print("Autoscale last curve on this graph")
self.sc.setAutoscale()
self.sc.plt.hold(False)
if (self.lastGraphCommand1 != ""):
self.graphExpr(self.lastGraphCommand1, self.lastGraphCommand2)
self.pyCode= "graph.setAutoscale()\ngraphExpr('" + str(self.lastGraphCommand2) + "')"
return message
def showSettings(self):
self.pyCode= ''
message = "Parameters are:\n Sweep variable: " + str(self.sc.get_xvar())
message += "\n Title: " + str(self.sc.get_title())
xLimitsMessage= "auto"
if not self.sc.get_xauto():
xLimitsMessage= str(self.sc.get_xlimlow()) + " " + str(self.sc.get_xlimhigh())
message += "\n X Limits: " + xLimitsMessage
yLimitsMessage= "auto"
if not self.sc.yauto:
yLimitsMessage= str(self.sc.get_ylimlow()) + " " + str(self.sc.get_ylimhigh())
message += "\n Y Limits: " + yLimitsMessage
if self.sc.xlog:
message += "\n X axis is logarithmic"
else:
message += "\n X axis is linear"
if self.sc.ylog:
message += "\n Y axis is logarithmic"
else:
message += "\n Y axis is linear"
currentCanvasName= self.sc.get_name()
message += "\n Current graph name: " + str(currentCanvasName)
return message
def displaySweepVar(self):
if self.sc.get_xvar() is not None:
message= "Sweep variable: " + str(self.sc.get_xvar()) + "\n"
else:
message= "# No sweep variable\n"
return message
def displayVoltages(self):
if 'r' in globals() and r is not None:
message= ''
for voltage in r.getVoltageNames():
message= message + "v(" + str(voltage) + ")\n"
else:
message= "# No voltage variables\n"
return message
def displayCurrents(self):
if 'r' in globals() and r is not None:
message= ''
for voltage in r.getCurrentNames():
message = message + "i(" + str(voltage) + ")\n"
else:
message= "# No current variables\n"
return message
def executeLongCommand(self, longCmd, cmdText):
cmd= longCmd.group(1)
arg= longCmd.group(2)
self.pyCode= ''
message= ''
if cmd == 'si':
self.pyCode= '''sim.setCircuitName("''' + str(arg) + '''")\nsim.simulate()'''
message= self.setCircuitName(arg)
message= message + "\n" + self.simulate(arg)
elif cmd == 'ci':
self.pyCode= '''sim.setCircuitName("''' + str(arg) + '''")'''
message= self.setCircuitName(arg)
self.readRawFile()
elif cmd == 'gr':
# self.sc.plt.hold(False)
message= self.graphExpr(arg, cmdText)
if message == '':
self.pyCode= "graph.graph("+ self.evaluator.logPyCode + ")"
self.lastGraphCommand1= str(arg)
self.lastGraphCommand2= str(cmdText)
else:
print(message)
self.pyCode= "# " + message
elif cmd == 'gs':
self.sc.plt.hold(True)
message= self.graphExpr(arg, cmdText)
if message == '':
self.pyCode= "graph.graphSameAxes("+ self.evaluator.logPyCode + ")"
self.lastGraphCommand1= str(arg)
self.lastGraphCommand2= str(cmdText)
else:
print(message)
self.pyCode= "# " + message
elif cmd == 'set':
self.setPostParameter(arg)
# self.pyCode= "graph.set(" + arg + ")"
elif cmd == 'history':
print("History not implemented yet.")
elif cmd == 'readh5':
print("Read hdf5 file: " + arg)
if os.path.isfile(arg):
self._globals['h']= h5py.File(arg, "r")
self.pyCode= 'h= h5py.File(' + arg + ', "r")'
else:
message= 'file "' + arg + '" not found'
print(message)
self.pyCode= "# " + message
elif cmd == 'include' or cmd == '.':
if os.path.isfile(arg):
self.include(arg)
self.pyCode='includeFile(' + arg + ')'
else:
message= 'Include file "' + arg + '" not found in command: ' + str(cmdText)
print(message)
self.pyCode= "# " + message
elif cmd == 'delete':
print(str(arg))
self.graphs.remove(arg)
elif cmd == 'img':
canvasType= str(type(self.sc))
if (canvasType != "<class 'ImgMplCanvas.ImgMplCanvas'>"):
message= "Error: can't draw image except on an image canvas.\nTo create an image canvas see set imgdev."
print(message)
elif os.path.isfile(arg):
print("Display image: " + str(arg))
fileType= imghdr.what(arg)
fileExtension= '.' + fileType
if (arg.endswith(fileExtension)):
varName= ntpath.basename(arg)
varName, extension= os.path.splitext(varName)
else:
varName= 'image1'
print("Image is in variable: " + str(varName))
self._globals[varName]= skimage.data.imread(arg)
# self.setPostParameter("graphdev " + str(varName))
self.sc.displayImageWindow(arg, imageName=varName)
# self.sc.plt.imshow(self._globals[varName])
# self.sc.show()
else:
message= 'Image file "' + arg + '" not found in command: ' + str(cmdText)
print(message)
self.pyCode= "# " + message
return message
def isEngrNumber(self, expression):
if self.isfloat(expression):
return True, float(expression)
m= re.search(r'a$', expression, flags=re.IGNORECASE)
if m is not None:
return self.floatifyEngr(m, expression, 1e-18)
m= re.search(r'f$', expression, flags=re.IGNORECASE)
if m is not None:
return self.floatifyEngr(m, expression, 1e-15)
m= re.search(r'p$', expression, flags=re.IGNORECASE)
if m is not None:
return self.floatifyEngr(m, expression, 1e-12)
m= re.search(r'n$', expression, flags=re.IGNORECASE)
if m is not None:
return self.floatifyEngr(m, expression, 1e-9)
m= re.search(r'u$', expression, flags=re.IGNORECASE)
if m is not None:
return self.floatifyEngr(m, expression, 1e-6)
m= re.search(r'm$', expression)
if m is not None:
return self.floatifyEngr(m, expression, 1e-3)
m= re.search(r'k$', expression, flags=re.IGNORECASE)
if m is not None:
return self.floatifyEngr(m, expression, 1e3)
m= re.search(r'meg$', expression, flags=re.IGNORECASE)
if m is not None:
return self.floatifyEngr(m, expression, 1e6)
m= re.search(r'M$', expression)
if m is not None:
return self.floatifyEngr(m, expression, 1e6)
m= re.search(r'G$', expression, flags=re.IGNORECASE)
if m is not None:
return self.floatifyEngr(m, expression, 1e9)
m= re.search(r'T$', expression, flags=re.IGNORECASE)
if m is not None:
return self.floatifyEngr(m, expression, 1e12)
return False, 0.0
def isfloat(self, value):
try:
float(value)
return True
except ValueError:
return False
def floatifyEngr(self, m, expression, exponent):
if m is not None:
mantissa= expression[:m.start()]
if self.isfloat(mantissa):
value= float(mantissa) * exponent
return True, value
else:
return False, 0.0
def setCircuitName(self, arg):
if (arg != ''):
self.simulationBaseName= arg
self.rawFileName= arg + ".raw"
self.spiceFileName= arg + ".cki"
self.getTitleLine()
message= "Circuit is set to: " + arg
else:
message= "Circuit name has not been set"
return message
def readRawFile(self):
# TODO: There should be a function in ReadSpiceRaw to find the proper value for the x variable,
# instead of assuming that it is named 't'
# After reading the raw file, emit a message such as 'sweep variable is t'.
rawReader= ReadSpiceRaw.spice_read(self.rawFileName)
if rawReader is None:
print("file " + str(self.rawFileName) + " not found. File not read.")
else:
rawReader.loadSpiceVoltages()
self._globals['r']= rawReader
self.sc.set_xvar('t')
self._globals['t']= r.t()
def readHDF5File(self):
hdf5Reader = ReadHDF5.hdf5_read(self.hdf5FileName)
if hdf5Reader is not None:
hdf5Reader.loadHDF5Data()
self._globals['h']= hdf5Reader
def getTitleLine(self):
if os.path.isfile(self.spiceFileName):
with open(self.spiceFileName, 'r') as f:
title = f.readline()
title = title.replace('\n', '')
title = title.replace('\r', '')
self.sc.set_title(title)
# TODO: Move this to MainWindow so that the Qt calls aren't used here.
# TODO: Make the simulator path and command configurable.
# TODO: Should probably have a delegate for the simulator.
# Xyce runtime command: runxyce -r xtest.raw -a -gui ngtest.cki
def simulate(self, arg):
if arg != '':
self.setCircuitName(arg)
if self.spiceFileName != '':
message= "Run simulator on " + self.spiceFileName + " to produce " + self.rawFileName
self.process= QProcess()
self.process.finished.connect(self.processCompleted)
if _platform == "linux" or _platform == "linux2":
self.process.start('/usr/bin/ngspice -r ' + self.rawFileName + ' -b ' + self.spiceFileName)
elif _platform == "darwin":
# MAC OS X
self.process.start('/usr/local/bin/ngspice -r ' + self.rawFileName + ' -b ' + self.spiceFileName)
elif _platform == "win32":
# Windows
pass
else:
message= "No simulation specified"
return message
def processCompleted(self):
# self.ui.statusText.setText("Finished")
print("Finished simulating")
self.readRawFile()
print("Finished reading raw file")
def include(self, arg):
print("Include file: " + str(arg))
if not os.path.isfile(arg):
print("Include file " + str(arg) + " not found")
return
with open(arg, 'r') as handle:
for cmdText in handle:
self.executeCmd(cmdText)
# TODO: Might need two types of plots, one for images and one for graphs
# There are bugs when one goes in place of the other.
def graphExpr(self, arg, cmdText):
userPlotExpr= arg
res= None
fsz= 12
message= ''
plotExpr= self.sve.fixWaveFormVariableNames(userPlotExpr)
grPattern= re.compile(r"g[rs] ([iv]\([^)]+\))")
res, success= self.evaluator.runEval(cmdText, res, plotExpr)
if not success:
badGraphCmd= grPattern.match(cmdText)
if badGraphCmd:
vfunction= badGraphCmd.group(1)
return "Error: unrecognized waveform: " + str(vfunction)
else:
return "Error evaluating plot expression: " + cmdText
if self.sc.get_xvar() in self._globals:
typeXAxis= str(type(self._globals[self.sc.get_xvar()]))
if "<type 'numpy.ndarray'>" in typeXAxis:
xAxisPts= len(self._globals[self.sc.get_xvar()])
else:
xAxisPts= 1
else:
typeXAxis= None
typeYAxis= str(type(res))
if (typeYAxis == "<type 'numpy.ndarray'>" and len(shape(res))==3):
message= "Graph image: " + str(userPlotExpr)
self.sc.plt.imshow(res)
self.sc.show()
self.pyCode = 'graph.graphImage()'
return message
else:
if "<type 'numpy.ndarray'>" in typeYAxis:
yAxisPts= len(res)
else:
yAxisPts= 1
if typeXAxis == None:
self.sc.plotYList(res, arg, self.sc.get_title())
else:
if xAxisPts == yAxisPts:
self.sc.plotXYList(self._globals[self.sc.get_xvar()], res, self.sc.get_xvar(), arg, self.sc.get_title())
else:
plX= 'point' if xAxisPts == 1 else 'points'
plY= 'point' if yAxisPts == 1 else 'points'
badGraphCmd= grPattern.match(cmdText)
if badGraphCmd:
vfunction= badGraphCmd.group(1)
message= "Error: unrecognized waveform: " + str(vfunction)
else:
message = "Error: X-axis: " + str(self.sc.get_xvar()) + " has " + str(xAxisPts) + " " + plX + "\n"
message += " Y-axis: " + str(cmdText) + " has " + str(yAxisPts) + " " + plY
return message
def setPostParameter(self, arg):
if (self.setPostFlag(arg) == True):
return
regexSet= re.match(r'^(xname|title|xl|yl|graphdev|imgdev) (.*)', arg)
if regexSet is not None:
setcmd= regexSet.group(1)
setarg= regexSet.group(2)
if setcmd == 'xname':
self.sc.set_xvar(setarg)
self.pyCode= 'graph.xname("'+ self.sc.get_xvar() + '")'
message = "Set x variable to " + str(self.sc.get_xvar())
elif setcmd == 'title':
self.sc.set_title(setarg)
self.pyCode= 'graph.title("'+ self.sc.get_title() + '")'
elif setcmd == 'yl':
if str(setarg) == 'auto':
self.sc.set_yauto(True)
self.pyCode = 'graph.ylimAuto()'
else:
self.sc.set_yauto(False)
regexRange= re.match(r'^\s*(\S+)\s+(\S+)', setarg)
if regexRange is not None:
loflg, lo= self.isEngrNumber(regexRange.group(1))
hiflg, hi= self.isEngrNumber(regexRange.group(2))
if loflg and hiflg:
if (lo == hi):
lo= lo * 0.99
hi= lo * 1.01
if (lo > hi):
limtmp= lo
lo= hi
hi= limtmp
self.sc.ylimlow= lo
self.sc.ylimhigh= hi
self.pyCode = 'graph.set_ylim(' + str(lo) + ',' + str(hi) + ')'
self.sc.plt.set_ylim(lo, hi)
self.sc.draw()
elif setcmd == 'xl':
if str(setarg) == 'auto':
self.sc.set_xauto(True)
self.pyCode = 'graph.xlimAuto()'
else:
self.sc.set_xauto(False)
regexRange= re.match(r'^\s*(\S+)\s+(\S+)', setarg)
if regexRange is not None:
loflg, lo= self.isEngrNumber(regexRange.group(1))
hiflg, hi= self.isEngrNumber(regexRange.group(2))
if loflg and hiflg:
self.sc.xlimlow= lo
self.sc.xlimhigh= hi
if loflg and hiflg:
if (lo == hi):
lo= lo * 0.99
hi= lo * 1.01
if (lo > hi):
limtmp= lo
lo= hi
hi= limtmp
self.sc.xlimlow= lo
self.sc.xlimhigh= hi
self.sc.plt.set_xlim(lo, hi)
self.pyCode = 'graph.set_xlim(' + str(lo) + ',' + str(hi) + ')'
self.sc.draw()
elif setcmd == 'graphdev':
currentCanvasName= self.sc.get_name()
if setarg != currentCanvasName:
if setarg not in self.graphs.cd:
print("Need a new graph called " + str(setarg))
self.graphs.create(canvasName=str(setarg))
else:
print("Need to set active graph to " + str(setarg))
self.graphs.setActive(setarg)
self.setGraphicsActiveDelegate(self.graphs.getActiveCanvas())
elif setcmd == 'imgdev':
currentCanvasName= self.sc.get_name()
if setarg != currentCanvasName:
if setarg not in self.graphs.cd:
print("Need a new graph called " + str(setarg))
self.graphs.createImg(canvasName=str(setarg))
else:
print("Need to set active graph to " + str(setarg))
self.graphs.setActive(setarg)
self.setGraphicsActiveDelegate(self.graphs.getActiveCanvas())
else:
message = "Unrecognized set command: " + setcmd
else:
message= "Unrecognized setting: " + arg
def setPostFlag(self, arg):
regexSet= re.match(r'^(xlog|xlin|ylog|ylin)$', arg)
if regexSet is not None:
setcmd= regexSet.group(1)
if setcmd == False:
return False
if setcmd == 'xlog':
print("Setting x-axis to log plot")
self.sc.set_xlog(True)
if setcmd == 'xlin':
print("Setting x-axis to linear plot")
self.sc.set_xlog(False)
if setcmd == 'ylog':
print("Setting y-axis to log plot")
self.sc.set_ylog(True)
if setcmd == 'ylin':
print("Setting y-axis to linear plot")
self.sc.set_ylog(False)
return True
return False
def setGraphicsActiveDelegate(self, sc):
self.sc= sc
# The graphics canvas sends back data from markers in the form of globals.
# self.sc._globals= self._globals
# self.marker= EngMarker.EngMarker(self.sc)
def setGraphicsWindowsDelegate(self, graphs):
self.graphs= graphs
# Marker delegate protocol implementation:
def setMarkerDelegate(self, obj):
self.markerDelegate= obj
def set_markerX(self, val):
self._globals['markerX']= val
def set_markerY(self, val):
self._globals['markerY']= val
def set_deltaMarkerX(self, val):
self._globals['deltaMarkerX']= val
def set_deltaMarkerY(self, val):
self._globals['deltaMarkerY']= val
def get_markerX(self):
if 'markerX' in self._globals.keys():
return self._globals['markerX']
else:
return None;
def get_markerY(self):
if 'markerY' in self._globals.keys():
return self._globals['markerY']
else:
return None;