forked from Qucs/qucs-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_equations.py
659 lines (522 loc) · 16.3 KB
/
run_equations.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#!/usr/bin/env python
'''
This is a simple random constrained test generator for all functions in src/applications.h.
The purpose is is to test (and collect coverage of) the Qucs equation system and everything below it.
In short, it works as follows:
- For each entry in src/application.h.
- Generate random (or constrained) arguments (double, complex, matrix...)
- Create a test equation and computes the expected result.
- Save test equation into a netlist
- Run the netlist and read the simulator output
- Compare simulator output to the expected result.
TODO:
- At the moment it does not raise an error in Travis, only used for coverage.
'''
import argparse
import re
import os
import sys
import pickle
from collections import defaultdict
import numpy as np
# ease the use of eval, pull everything into the namespace
from numpy import *
# format Numpy to Qucs equation vector/matrix formats
from qmat import qmat
from random import randint
import subprocess
from qucstest.qucsdata import QucsData
def argTypeToMath(op, argc, argsTypes, prec='%.12f'):
'''
map argument types to numbers that can be computed
input:
- qucs operation
- number of arguments
- type of arguments
- precision
return:
- qucs Equation content
- expected numerical result
TODO
- some sort of random constrained number generation
- how to handle speciall cases, such as
branch cuts on hyperbolic functions...
'''
import collections
# bypass random generation
# FIXME these functions need further attention have
constr = collections.defaultdict(dict)
constr['arcsin']['TAG_DOUBLE'] = -1.0
constr['arcsin']['TAG_COMPLEX'] = complex(3,4)
constr['arcsin']['TAG_VECTOR'] = -1 * np.ones(3)
constr['arccos']['TAG_DOUBLE'] = -1.0
constr['arccos']['TAG_COMPLEX'] = complex(3,4)
constr['arccos']['TAG_VECTOR'] = -1 * np.ones(3)
constr['tanh']['TAG_DOUBLE'] = -1.0
constr['tanh']['TAG_COMPLEX'] = complex(3,4)
constr['tanh']['TAG_VECTOR'] = -1 * np.ones(3)
constr['artanh']['TAG_DOUBLE'] = 0
constr['artanh']['TAG_COMPLEX'] = complex(3,4)
constr['artanh']['TAG_VECTOR'] = np.zeros(3)
# Create list of arguments based on possible argument types
Eqn = []
pyEqn = []
for arg in argsTypes:
if arg == 'TAG_DOUBLE':
# value as string
#value = 0.3
value = np.random.rand()+1
# inject constraint
if op in constr.keys():
value = constr[op]['TAG_DOUBLE']
Eqn.append(repr( float(prec % value) ));
pyEqn.append(repr(value));
elif arg == 'TAG_COMPLEX':
# value as string
#value = 1.2+2.1j
a = np.random.rand()
b = np.random.rand()
value = complex(a,b)
# inject constraint
if op in constr.keys():
value = constr[op][arg]
formatComplex = '{0:.%s} {1} {2:.%s}j' %(prec[2:], prec[2:])
sign = '+' if value.imag >= 0 else '-'
valueC = formatComplex.format(value.real, sign , abs(value.imag))
Eqn.append( '(%s)' %valueC.replace('j','*j') )
pyEqn.append(repr(value));
elif arg == 'TAG_VECTOR':
value = np.linspace(1.,2.,3)
# inject constraint
if op in constr.keys():
value = constr[op][arg]
# format qucs equation string
qucsString = qmat(value, format=prec)
Eqn.append( qucsString );
pyEqn.append(repr(value));
elif arg == 'TAG_MATRIX':
#pyValue = np.ones((2,2))
value = np.random.rand(2,2) #random matrix
# format qucs equation string
qucsString = qmat(value, format=prec)
Eqn.append( qucsString );
pyEqn.append(repr(value));
elif arg == 'TAG_UNKNOWN':
# no argument?
value = ''
Eqn.append(repr(value));
pyEqn.append(repr(value));
elif arg == 'TAG_BOOLEAN':
# 0 or 1
value = random.randint(0,2)
Eqn.append(repr(value));
pyEqn.append(repr(value));
# TAG_RANGE, an slice of a vector?
# map qucs operator to python operators
# to compute the expected result
#print pyEqn
opMap={}
opMap['avg'] = 'average'
opMap['length'] = 'len'
opMap['random'] = 'random.rand'
opMap['arsinh'] = 'arcsinh'
opMap['arcosh'] = 'arccosh'
opMap['artanh'] = 'arctanh'
opMap['>'] = 'greater'
opMap['<'] = 'less'
opMap['>='] = 'greater_equal'
opMap['<='] = 'less_equal'
opMap['arg'] = 'angle' # Qucs alias
opMap['angle'] = 'angle'
opMap['mag'] = 'abs'
# local helper to set angle in degree
def angleDeg(arg):
return np.angle(arg, deg=True)
def ceilHelp(arg):
if (type(arg) == complex) :
return complex(ceil(real(arg)), ceil(imag(arg)))
else:
return ceil(arg)
def fixHelp(arg):
if (type(arg) == complex) :
return complex(fix(real(arg)), fix(imag(arg)))
else:
return fix(arg)
def hypotHelp(x1, x2):
if (type(x1) == complex) :
x1 = abs(x1)
if (type(x2) == complex) :
x2 = abs(x2)
return hypot(x1, x2)
def dB_Help(x):
if type(x) == float:
return 10.0*log10(abs(x))
else:
return 10.0*log10(abs(x)**2)
def sqr_Help(x):
if type(x) == ndarray and x.ndim > 1:
return linalg.matrix_power(a,2)
else:
return square(x)
def mult_Help(x):
if type(a) == ndarray and a.ndim > 1:
if type(b) == ndarray and b.ndim > 1:
return dot(a, b)
else:
return a*b
def norm_Help(x):
return linalg.norm(x)**2
def sinc_Help(x):
return sinc(x/pi)
def modulus_Help(x,y):
if type(x) == complex or type(y) == complex:
return x - y * floor (x / y);
else:
return x%y
def hat_Help(x,y):
if type(x) == ndarray and x.ndim >= 1:
return linalg.matrix_power(x,y)
else:
return x**y
opMap['phase'] = 'angleDeg'
opMap['ceil'] = 'ceilHelp'
opMap['fix'] = 'fixHelp'
opMap['hypot'] = 'hypotHelp'
opMap['dB'] = 'dB_Help'
opMap['sqr'] = 'sqr_help'
opMap['*'] = 'mult_help'
opMap['norm'] ='norm_help'
opMap['sinc'] ='sinc_help'
opMap['%'] ='modulus_help'
opMap['^'] ='hat_help'
# check for operator mapping
pyOp = op
if op in opMap:
pyOp = opMap[op]
#
# Build expression for Qucs
# Build expression for Python
#
# No argument ex. random()
if argc == 0:
expression = 'check="%s()"' %(op)
pyCode = '%s()' %(pyOp)
# Unary operation, ie op(arg)
# ex. +y , real(value)
if argc == 1:
expression = 'check="%s(%s)"' %(op, Eqn[0])
pyCode = '%s(%s)' %(pyOp, pyEqn[0])
#print op
#print pyCode
# Binary operation ex. 1+1
# or function(arg, arg)
# Assumptions for functions:
# - name is longer than 1 character
# - one (see above) or more arguments
# what about '>', need to map to np.greater( a , b)
if argc == 2:
if len(op) == 1 : #unary
expression = 'check="(%s)"' %(op.join(Eqn))
pyCode = '%s' %(pyOp.join(pyEqn))
else: #function
expression = 'check="%s(%s)"' %(op, ','.join(Eqn))
pyCode = '%s(%s)' %(pyOp, ','.join(pyEqn))
# handle special case
# a > b --> greater(a,b)
# a < b --> less(a,b)
# if complex:
# a > b --> greater(abs(a), abs(b))
# a < b --> less(abs(a), abs(b))
if op == '>' or op == '<' or op == '>=' or op == '<=':
expression = 'check="(%s)"' %(op.join(Eqn))
pyCode = '%s(%s)' %(pyOp, ','.join(['abs(%s)' %arg for arg in pyEqn]))
if op == '==' or op == '!=' :
expression = 'check="(%s)"' %(op.join(Eqn))
pyCode = '%s' %(pyOp.join(pyEqn))
# ?: ifthenelse
# a?b:c
# BUG FIXME
if argc == 3:
expression = 'check="%s?%s:%s"' %(Eqn[0], Eqn[1], Eqn[2])
pyCode = '%s if %s else %s' %(Eqn[1], Eqn[0], Eqn[2])
#print pyCode
# Try compute the result with Python
pyResult = 0
try :
pyResult = eval(pyCode)
except :
pyResult = " >> MISSING Python operation: %s %s" %(op, argsTypes)
return expression, pyCode, pyResult
def testNetlist(qucsExpression):
'''
returns a netlist including the test expression.
'''
#net = '# Qucs 0.0.19 Generated by Qucs-Test scripts.\n'
net = '.DC:DC1 Temp="26.85" reltol="0.001" abstol="1 pA" vntol="1 uV"' \
'saveOPs="no" MaxIter="150" saveAll="no" convHelper="none" Solver="CroutLU"\n'
net += 'Eqn:Eqn1 %s Export="yes"\n' %qucsExpression
return net
#=============================
#
# Operations missing Python verification
# Skip them for now
#
skip =[
'arctan', # can have one or two arguments?
'length', # don't know how to handle matvec input
'array', # ??
'?:', # ifthenesle, needs work
'random', # no reaso to check, only run
'srandom', # same as above
'unwrap',
'coth',
'sech',
'Rollet',
'Mu',
'Mu2',
'StabFactor',
'StabMeasure',
'linspace',
'logspace',
'NoiseCircle',
'NoiseCircle',
'NoiseCircle',
'NoiseCircle',
'NoiseCircle',
'NoiseCircle',
'StabCircleL',
'StabCircleS',
'GaCircle',
'GpCircle',
'GpCircle',
'GpCircle',
'GpCircle',
'PlotVs',
'PlotVs',
'PlotVs',
'interpolate',
'receiver',
'diff',
'stos',
'stoy',
'stoz',
'ytos',
'ztos',
'ytoz',
'ztoy',
'ztor',
'rtoz',
'ytor',
'rtoy',
'rtoswr',
'twoport', #twoport(X, from, to) Y, Z, H, G, A, S, T
'det',
'!',
'!=',
'==', # partially working, issue with check2: check="[1.0,1.5,2.0]==1.5" check2="1.5==[1.0,1.5,2.0]"
'vector',
'matrix',
'assert',
'bugon',
'floor',
'limexp',
'ln',
'cot',
'arccot',
'sec',
'arcsec',
'cosec',
'arccosec',
'arcoth',
'sech,',
'arsech',
'cosech',
'arcosech',
'dbm',
'integrate',
'dbw2w',
'w2dbm',
'dbm2w',
'w2dbm',
'polar',
'bessely',
'besseli0',
'besselj',
'stddev',
'variance',
'cumavg',
'rms',
'round',
'erf',
'erfc',
'erfinv',
'erfcinv',
'runavg',
'vt',
'kbd',
'step',
'fft',
'ifft',
'dft',
'idft',
'Time2Freq',
'Freq2Time',
'fftshift',
'xvalue',
'yvalue',
'adjoint',
'signum',
'inverse',
]
# implemented test support by return type
implemented = [
'TAG_BOOLEAN',
'TAG_DOUBLE',
'TAG_COMPLEX',
'TAG_VECTOR',
#'TAG_MATRIX', # parser hacked to support it, need work
#'TAG_MATVEC', # don't know how to handle this
#'TAG_CHAR', # see 'array'
#'TAG_STRING', # see '+'
#
]
# FIXME
# RANGE?
# [ PASS ] TEST 335 [ avg ] ['TAG_VECTOR', 'TAG_RANGE']
# =============================================================================
# Let us do some work...
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Qucs testing script.')
parser.add_argument('--prefix', type=str,
help='prefix of installed Qucs (default: /usr/local/bin/)')
parser.add_argument('--operation', type=str,
help='test one particular operation, ex. \'+\')')
args = parser.parse_args()
#print args
# prefix = ''
if args.prefix:
if os.path.isfile(os.path.join(args.prefix, 'qucsator')):
prefix = args.prefix
print '\nFound Qucsator in: %s\n' %(prefix)
else:
sys.exit('Oh dear, Qucsator not found in: %s' %(prefix))
else:
sys.exit('\n Error, missing --prefix')
# Get list of Qucs operations and their details
# Picke/unpickle data application.h data
# operations, applications = parseApplications("/Users/guitorri/git/qucs/qucs-core/src/applications.h")
operations = pickle.load( open( 'qucs_operations.p', "rb" ) )
applications = pickle.load( open( 'qucs_applications.p', "rb" ) )
if args.operation in operations:
if args.operation in operations:
operations = [args.operation]
else:
sys.exit('Operation not supported: %s' %args.operation)
# Well, these are counters
testCount =0
skipCount = 0
passCount = 0
failCount = 0
# Toggle if any test fail
returnStatus = 0
# Process every operation
for op in operations:
# cound skipped operations
if op in skip:
skipped = len(applications[op])
print ' >> SKIP [%s] %s tests' %(op, skipped)
skipCount += skipped
continue
for app in applications[op]:
testCount +=1
# unpack data
retType = app[0]
func = app[1]
argc = int(app[2])
argTypes = app[3]
# skip if test not yet supported
if retType not in implemented:
#print ' >> MISSING [ %s ] return type %s' %(op, retType)
skipCount += 1
continue
# get Quscs string and Python result
try:
expression, pyExpression, pyResult = argTypeToMath(op, argc, argTypes, prec='%.12f')
except:
print ' >> ISSUES with %s' %op
print expression, pyResult
# if pyResult is a string, report and move to next
if isinstance(pyResult, str):
#print ' >> PYTHON ERROR [ %s ] ==> [%s] = %s | PyCode >> %s' %(op, retType, argTypes, pyExpression)
print ' >> PYTHON ERROR [ %s ] ==> [%s] = %s' %(op, retType, argTypes)
skipCount += 1
continue
# save netlist /temp/qucstest/test###.txt
# run, save data /temp/qucstest/test###.dat, handle crash
# read dat
# compare with expected
# PASS, FAILL
tmpDir = '/tmp/qucstest/'
inNet = tmpDir + 'test%03i.txt' %testCount
outDat = tmpDir + 'test%03i.dat' %testCount
if not os.path.exists(tmpDir):
os.makedirs(tmpDir)
net = testNetlist(expression)
with open(inNet, 'w') as myTest:
myTest.write( net )
cmd = [os.path.join(prefix, "qucsator"), "-i", inNet, "-o", outDat]
print 'Running [qucsator]: ', ' '.join(cmd)
retval = subprocess.call(cmd, stdout=open(os.devnull, "w"), stderr=subprocess.STDOUT)
sim = 'PASS'
if retval:
sim = 'FAIL'
print "\n +++ Qucsator ERROR +++ \n"
print 'Return code: ', retval
print 'netlist: ', inNet
sys.exit('Error on qucsator.')
# Qucsator data parser (hacked to support matrix)
#import parse_result as parse
if os.path.exists(outDat):
test_data = QucsData(outDat)
else:
print " \n\n +++ no result +++ \n\n:", outDat
test = test_data.data['check']
stat = ''
if np.allclose(pyResult, test, rtol=1e-5, atol=1e-8):
stat = 'PASS'
passCount +=1
else:
stat = ' >FAIL'
print "[ %s ] TEST %03i [ %s ] ==> [%s] = %s" %(stat, testCount, op, retType, argTypes)
if stat != 'PASS' or sim =='FAIL':
# fail due do numerical error or qucsator fail
returnStatus -1
failCount +=1
print '---'
print net
print "Expected:"
print pyExpression
print ' ', pyResult
print "Got:"
with open(outDat, 'r') as myDat:
for line in myDat:
print ' ', line,
print '---\n'
total = 0
for key in operations:
total += len(applications[key])
print ''
print '-'*15
print '-- PASS = ', passCount
print '-- FAIL = ', failCount
print '-- SKIP = ', skipCount
print '='*15
print '-- TOTAL = ', total
print '-'*15
if returnStatus:
status = 'FAIL'
else:
status = 'PASS'
print '\n'
print ('############### Done. Return status: %s ###############' %status )
sys.exit(returnStatus)