/
workspace.py
3131 lines (2599 loc) · 133 KB
/
workspace.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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Defines the Workspace class and supporting functionality.
"""
#***************************************************************************************************
# Copyright 2015, 2019 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
# Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government retains certain rights
# in this software.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0 or in the LICENSE file in the root pyGSTi directory.
#***************************************************************************************************
import collections as _collections
import inspect as _inspect
import itertools as _itertools
import os as _os
import pickle as _pickle
# import uuid as _uuid
import random as _random
import shutil as _shutil
import subprocess as _subprocess
from pprint import pprint as _pprint
import numpy as _np
from pygsti.report import merge_helpers as _merge
from pygsti.report import plotly_plot_ex as _plotly_ex
from pygsti import baseobjs as _baseobjs
from pygsti.baseobjs.smartcache import CustomDigestError as _CustomDigestError
from pygsti.baseobjs import _compatibility as _compat
_PYGSTI_WORKSPACE_INITIALIZED = False
VALIDATE_PLOTLY = False # False increases performance of report rendering; set to True to debug
def in_ipython_notebook():
"""
Returns true if called from within an IPython/jupyter notebook
Returns
-------
bool
"""
try:
# 'ZMQInteractiveShell' in a notebook, 'TerminalInteractiveShell' in IPython REPL, and fails elsewhere.
shell = get_ipython().__class__.__name__
return shell == 'ZMQInteractiveShell'
except NameError:
return False
def display_ipynb(content):
"""
Render HTML content to an IPython notebook cell display
Parameters
----------
content : str
HTML content to insert.
Returns
-------
None
"""
from IPython.core.display import display, HTML
display(HTML(content))
def enable_plotly_pickling():
"""
Hacks the plotly python library so that figures may be pickled and un-pickled.
This hack should be used only temporarily - so all pickling
and un-pickling should be done between calls to
:func:`enable_plotly_pickling` and :func:`disable_plotly_pickling`.
Returns
-------
None
"""
import plotly
if int(plotly.__version__.split(".")[0]) >= 3: # plotly version 3 or higher
BLT = plotly.basedatatypes.BaseLayoutType
def fix_getattr(self, prop):
if '_subplotid_props' not in self.__dict__:
self._subplotid_props = set()
return self.__saved_getattr__(prop)
if hasattr(BLT, '__getattr__'):
BLT.__saved_getattr__ = BLT.__getattr__
del BLT.__getattr__
BLT.__getattr__ = fix_getattr
else:
def setitem(self, key, value, _raise=True):
"""Sets an item of a dict using the standard dict's __setitem__
to restore normal dict behavior"""
return dict.__setitem__(self, key, value)
plotlyDictClass = plotly.graph_objs.Figure.__bases__[0]
if hasattr(plotlyDictClass, '__setitem__'):
plotlyDictClass.__saved_setitem__ = plotlyDictClass.__setitem__
if hasattr(plotlyDictClass, '__getattr__'):
plotlyDictClass.__saved_getattr__ = plotlyDictClass.__getattr__
del plotlyDictClass.__getattr__
if hasattr(plotlyDictClass, '__setattr__'):
plotlyDictClass.__saved_setattr__ = plotlyDictClass.__setattr__
del plotlyDictClass.__setattr__
plotlyDictClass.__setitem__ = setitem
def disable_plotly_pickling():
"""
Reverses the effect of :func:`enable_plotly_pickling`
Returns
-------
None
"""
import plotly
if int(plotly.__version__.split(".")[0]) >= 3: # plotly version 3 or higher
BLT = plotly.basedatatypes.BaseLayoutType
if hasattr(BLT, '__saved_getattr__'):
BLT.__getattr__ = BLT.__saved_getattr__
del BLT.__saved_getattr__
else:
plotlyDictClass = plotly.graph_objs.Figure.__bases__[0]
if hasattr(plotlyDictClass, '__saved_setitem__'):
plotlyDictClass.__setitem__ = plotlyDictClass.__saved_setitem__
del plotlyDictClass.__saved_setitem__
if hasattr(plotlyDictClass, '__saved_getattr__'):
plotlyDictClass.__getattr__ = plotlyDictClass.__saved_getattr__
del plotlyDictClass.__saved_getattr__
if hasattr(plotlyDictClass, '__saved_setattr__'):
plotlyDictClass.__setattr__ = plotlyDictClass.__saved_setattr__
del plotlyDictClass.__saved_setattr__
def ws_custom_digest(md5, v):
"""
A "digest" function for hashing several special types
Parameters
----------
md5 : hashlib.HASH
The MD5 hash object.
v : object
the value to add to `md5` (using `md5.update(...)`).
Returns
-------
None
"""
if isinstance(v, NotApplicable):
md5.update("NOTAPPLICABLE".encode('utf-8'))
elif isinstance(v, SwitchValue):
md5.update(v.base.tostring()) # don't recurse to parent switchboard
else:
raise _CustomDigestError()
def random_id():
"""
Returns a random document-objet-model (DOM) ID
Returns
-------
str
"""
return str(int(1000000 * _random.random()))
#return str(_uuid.uuid4().hex) #alternative
class Workspace(object):
"""
Central to data analysis, Workspace objects facilitate the building of reports and dashboards.
In particular, they serve as a:
- factory for tables, plots, and other types of output
- cache manager to optimize the construction of such output
- serialization manager for saving and loading analysis variables
Workspace objects are typically used either 1) within an ipython
notebook to interactively build a report/dashboard, or 2) within
a script to build a hardcoded ("fixed") report/dashboard.
Parameters
----------
cachefile : str, optional
filename with cached workspace results
"""
def __init__(self, cachefile=None):
"""
Initialize a Workspace object.
Parameters
----------
cachefile : str, optional
filename with cached workspace results
"""
self._register_components(False)
self.smartCache = _baseobjs.SmartCache()
if cachefile is not None:
self.load_cache(cachefile)
self.smartCache.add_digest(ws_custom_digest)
def save_cache(self, cachefile, show_unpickled=False):
"""
Save this Workspace's cache to a file.
Parameters
----------
cachefile : str
The filename to save the cache to.
show_unpickled : bool, optional
Whether to print quantities (keys) of cache that could not be
saved because they were not pickle-able.
Returns
-------
None
"""
with open(cachefile, 'wb') as outfile:
enable_plotly_pickling()
_pickle.dump(self.smartCache, outfile)
disable_plotly_pickling()
if show_unpickled:
print('Unpickled keys:')
_pprint(self.smartCache.unpickleable)
def load_cache(self, cachefile):
"""
Load this Workspace's cache from `cachefile`.
Parameters
----------
cachefile : str
The filename to load the cache from.
Returns
-------
None
"""
with open(cachefile, 'rb') as infile:
enable_plotly_pickling()
oldCache = _pickle.load(infile).cache
disable_plotly_pickling()
for v in oldCache.values():
if isinstance(v, WorkspaceOutput): # hasattr(v,'ws') == True for plotly dicts (why?)
print('Updated {} object to set ws to self'.format(type(v)))
v.ws = self
self.smartCache.cache.update(oldCache)
def __getstate__(self):
return {'smartCache': self.smartCache}
def __setstate__(self, state_dict):
self._register_components(False)
self.smartCache = state_dict['smartCache']
def _makefactory(self, cls, autodisplay): # , printer=_objs.VerbosityPrinter(1)):
# XXX this indirection is so wild -- can we please rewrite directly?
#Manipulate argument list of cls.__init__
argspec = _inspect.getfullargspec(cls.__init__)
argsig = _inspect.signature(cls.__init__)
argnames = argspec.args
assert(argnames[0] == 'self' and argnames[1] == 'ws'), \
"__init__ must begin with (self, ws, ...)"
# Strip default values out of parameters so that we don't override the true incoming values with defaults
newargparams = [p.replace(default=_inspect.Parameter.empty) for p in argsig.parameters.values()][2:]
newargsig = _inspect.Signature(newargparams)
signature = str(newargsig)
signature = signature[1:-1] # strip off parenthesis from ends of "(signature)"
if autodisplay:
factory_func_def = (
'def factoryfn(%(signature)s):\n'
' ret = cls(self, %(signature)s); ret.display(); return ret' %
{'signature': signature})
else:
factory_func_def = (
'def factoryfn(%(signature)s):\n'
' return cls(self, %(signature)s)' %
{'signature': signature})
#print("FACTORY FN DEF = \n",new_func)
exec_globals = {'cls': cls, 'self': self}
exec(factory_func_def, exec_globals)
factoryfn = exec_globals['factoryfn']
#Copy cls.__init__ info over to factory function
factoryfn.__name__ = cls.__init__.__name__
factoryfn.__doc__ = cls.__init__.__doc__
factoryfn.__module__ = cls.__init__.__module__
factoryfn.__dict__ = cls.__init__.__dict__
factoryfn.__defaults__ = cls.__init__.__defaults__
return factoryfn
def _register_components(self, autodisplay):
# "register" components
from . import workspacetables as _wt
from . import workspaceplots as _wp
from . import workspacetexts as _wtxt
def makefactory(cls): return self._makefactory(cls, autodisplay)
self.Switchboard = makefactory(Switchboard)
self.NotApplicable = makefactory(NotApplicable)
#Tables
# Circuits
self.CircuitTable = makefactory(_wt.CircuitTable)
# Spam & Gates
self.SpamTable = makefactory(_wt.SpamTable)
self.SpamParametersTable = makefactory(_wt.SpamParametersTable)
self.GatesTable = makefactory(_wt.GatesTable)
self.ChoiTable = makefactory(_wt.ChoiTable)
# Spam & Gates vs. a target
self.SpamVsTargetTable = makefactory(_wt.SpamVsTargetTable)
self.ModelVsTargetTable = makefactory(_wt.ModelVsTargetTable)
self.GatesVsTargetTable = makefactory(_wt.GatesVsTargetTable)
self.GatesSingleMetricTable = makefactory(_wt.GatesSingleMetricTable)
self.GateEigenvalueTable = makefactory(_wt.GateEigenvalueTable)
self.ErrgenTable = makefactory(_wt.ErrgenTable)
self.GaugeRobustErrgenTable = makefactory(_wt.GaugeRobustErrgenTable)
self.GaugeRobustModelTable = makefactory(_wt.GaugeRobustModelTable)
self.GaugeRobustMetricTable = makefactory(_wt.GaugeRobustMetricTable)
self.NQubitErrgenTable = makefactory(_wt.NQubitErrgenTable)
self.StandardErrgenTable = makefactory(_wt.StandardErrgenTable)
# Specific to 1Q gates
self.GateDecompTable = makefactory(_wt.GateDecompTable)
self.old_GateDecompTable = makefactory(_wt.OldGateDecompTable)
self.old_RotationAxisVsTargetTable = makefactory(_wt.OldRotationAxisVsTargetTable)
self.old_RotationAxisTable = makefactory(_wt.OldRotationAxisTable)
# goodness of fit
self.FitComparisonTable = makefactory(_wt.FitComparisonTable)
self.WildcardBudgetTable = makefactory(_wt.WildcardBudgetTable)
self.WildcardSingleScaleBarPlot = makefactory(_wp.WildcardSingleScaleBarPlot)
#Specifically designed for reports
self.BlankTable = makefactory(_wt.BlankTable)
self.DataSetOverviewTable = makefactory(_wt.DataSetOverviewTable)
self.GaugeOptParamsTable = makefactory(_wt.GaugeOptParamsTable)
self.MetadataTable = makefactory(_wt.MetadataTable)
self.SoftwareEnvTable = makefactory(_wt.SoftwareEnvTable)
self.ProfilerTable = makefactory(_wt.ProfilerTable)
self.ExampleTable = makefactory(_wt.ExampleTable)
#Plots
self.ColorBoxPlot = makefactory(_wp.ColorBoxPlot)
self.BoxKeyPlot = makefactory(_wp.BoxKeyPlot)
self.MatrixPlot = makefactory(_wp.MatrixPlot)
self.GateMatrixPlot = makefactory(_wp.GateMatrixPlot)
self.PolarEigenvaluePlot = makefactory(_wp.PolarEigenvaluePlot)
self.ProjectionsBoxPlot = makefactory(_wp.ProjectionsBoxPlot)
self.ChoiEigenvalueBarPlot = makefactory(_wp.ChoiEigenvalueBarPlot)
self.GramMatrixBarPlot = makefactory(_wp.GramMatrixBarPlot)
self.FitComparisonBarPlot = makefactory(_wp.FitComparisonBarPlot)
self.FitComparisonBoxPlot = makefactory(_wp.FitComparisonBoxPlot)
self.DatasetComparisonHistogramPlot = makefactory(_wp.DatasetComparisonHistogramPlot)
self.DatasetComparisonSummaryPlot = makefactory(_wp.DatasetComparisonSummaryPlot)
self.RandomizedBenchmarkingPlot = makefactory(_wp.RandomizedBenchmarkingPlot)
#Text blocks
self.StdoutText = makefactory(_wtxt.StdoutText)
#Extras
from ..extras import idletomography as _idt
self.IdleTomographyIntrinsicErrorsTable = makefactory(_idt.IdleTomographyIntrinsicErrorsTable)
self.IdleTomographyObservedRatePlot = makefactory(_idt.IdleTomographyObservedRatePlot)
self.IdleTomographyObservedRatesTable = makefactory(_idt.IdleTomographyObservedRatesTable)
self.IdleTomographyObservedRatesForIntrinsicRateTable = makefactory(
_idt.IdleTomographyObservedRatesForIntrinsicRateTable)
from ..extras.drift import driftreport as _driftrpt
self.DriftSummaryTable = makefactory(_driftrpt.DriftSummaryTable)
self.DriftDetailsTable = makefactory(_driftrpt.DriftDetailsTable)
self.PowerSpectraPlot = makefactory(_driftrpt.PowerSpectraPlot)
self.ProbTrajectoriesPlot = makefactory(_driftrpt.ProbTrajectoriesPlot)
self.GermFiducialProbTrajectoriesPlot = makefactory(_driftrpt.GermFiducialProbTrajectoriesPlot)
self.GermFiducialPowerSpectraPlot = makefactory(_driftrpt.GermFiducialPowerSpectraPlot)
def init_notebook_mode(self, connected=False, autodisplay=False):
"""
Initialize this Workspace for use in an iPython notebook environment.
This function should be called prior to using the Workspace when
working within an iPython notebook.
Parameters
----------
connected : bool , optional
Whether to assume you are connected to the internet. If you are,
then setting this to `True` allows initialization to rely on web-
hosted resources which will reduce the overall size of your
notebook.
autodisplay : bool , optional
Whether to automatically display workspace objects after they are
created.
Returns
-------
None
"""
if not in_ipython_notebook():
raise ValueError('Only run `init_notebook_mode` from inside an IPython Notebook.')
global _PYGSTI_WORKSPACE_INITIALIZED
script = ""
if not connected:
_merge.rsync_offline_dir(_os.getcwd())
#If offline, add JS to head that will load local requireJS and/or
# jQuery if needed (jupyter-exported html files always use CDN
# for these).
if not connected:
script += "<script src='offline/jupyterlibload.js'></script>\n"
#Load our custom plotly extension functions
script += _merge.insert_resource(connected, None, "pygsti_plotly_ex.js")
script += "<script type='text/javascript'> window.plotman = new PlotManager(); </script>"
#jQueryUI_CSS = "https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"
jQueryUI_CSS = "https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css"
script += _merge.insert_resource(connected, jQueryUI_CSS, "smoothness-jquery-ui.css")
# Load style sheets for displaying tables
script += _merge.insert_resource(connected, None, "pygsti_dataviz.css")
#To fix the UI tooltips within Jupyter (b/c they use an old/custom JQueryUI css file)
if connected:
imgURL = "https://code.jquery.com/ui/1.12.1/themes/smoothness/images/ui-icons_222222_256x240.png"
else:
imgURL = "offline/images/ui-icons_222222_256x240.png"
script += "<style>\n" + \
".tooltipbuttons .ui-button { padding: 0; border: 0; background: transparent; }\n" + \
".tooltipbuttons .ui-icon { background-image: url(\"%s\"); margin-top: 0; }\n" % imgURL + \
"</style>"
# Note: within a jupyter notebook, the requireJS base path appears
# to be "/static", so just setting the path to "offline/myfile"
# would attempt to load "/static/offline/myfile.js" which points
# somewhere like .../site-packages/notebook/static/offline/myfile".
# So:
# - when in a notebook, the path needs to be "../files" followed
# by the notebook's path, which we can obtain via the notebook JS
# object.
# - when *not* in a notebook, the requireJS base defaults to the
# current page, so just using "offline/myfile" works fine then.
#Tell require.js where jQueryUI and Katex are
if connected:
reqscript = (
"<script>"
"console.log('ONLINE - using CDN paths');"
"requirejs.config({{ "
" paths: {{ 'jquery-UI': ['{jqueryui}'],"
" 'katex': ['{katex}'],"
" 'autorender': ['{auto}'] }},"
"}});"
"require(['jquery', 'jquery-UI'],function($,ui) {{"
" window.jQueryUI=ui; console.log('jquery-UI loaded'); }});"
"require(['katex', 'autorender'],function(katex,auto) {{"
" window.katex=katex; console.log('Katex loaded'); }});"
"</script>"
).format(jqueryui="https://code.jquery.com/ui/1.12.1/jquery-ui.min",
katex="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.js",
auto="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/contrib/auto-render.min.js")
else:
reqscript = (
"<script>"
"var pth;"
"if(typeof IPython !== 'undefined') {{"
" var nb = IPython.notebook;"
" var relpath = nb.notebook_path.substr(0, nb.notebook_path.lastIndexOf('/') + 1 );"
" jqueryui_pth = '../files' + nb.base_url + relpath + '{jqueryui}';"
" katex_pth = '../files' + nb.base_url + relpath + '{katex}';"
" auto_pth = '../files' + nb.base_url + relpath + '{auto}';"
" console.log('IPYTHON DETECTED - using path ' + jqueryui_pth);"
"}}"
"else {{"
" jqueryui_pth = '{jqueryui}';"
" katex_pth = '{katex}';"
" auto_pth = '{auto}';"
" console.log('NO IPYTHON DETECTED - using path ' + jqueryui_pth);"
"}}"
"requirejs.config({{ "
" paths: {{ 'jquery-UI': [jqueryui_pth], 'katex': [katex_pth], 'autorender': [auto_pth] }},"
"}});"
"require(['jquery', 'jquery-UI'],function($,ui) {{"
" window.jQueryUI=ui; console.log('jquery & jquery-UI loaded'); }});"
"require(['katex', 'autorender'],function(katex,auto) {{"
" window.katex=katex; console.log('Katex loaded'); }});"
"</script>"
).format(jqueryui="offline/jquery-ui.min",
katex="offline/katex.min",
auto="offline/auto-render.min")
script += reqscript
#Initialize Katex as a fallback if MathJax is unavailable (offline), OR,
# if MathJax is loaded, wait for plotly to load before rendering SVG text
# so math shows up properly in plots (maybe we could just use a require
# statement for this instead of polling?)
script += _merge.insert_resource(
connected, "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css",
"katex.css")
script += (
"\n<script>"
"require(['jquery','katex','autorender'],function($,katex,renderMathInElement) {\n"
" var mathjaxTimer = setInterval( function() {\n"
" if(document.readyState === 'complete' || document.readyState === 'loaded') {\n"
" clearInterval(mathjaxTimer);\n"
" if(typeof MathJax === 'undefined') {\n"
" console.log('MATHJAX not found - attempting to typeset with Katex');\n"
" renderMathInElement(document.body, { delimiters: [\n"
" {left: '$$', right: '$$', display: true},\n"
" {left: '$', right: '$', display: false},\n"
" ] } );\n"
" }\n"
" else { //Mathjax is alive - wait for plotly\n"
" var waitForPlotly = setInterval( function() {\n"
" if( typeof(window.Plotly) !== 'undefined' ){\n"
" MathJax.Hub.Config({ SVG: { font: 'STIX-Web' }, displayAlign: 'center' });\n"
" MathJax.Hub.Queue(['setRenderer', MathJax.Hub, 'SVG']);\n"
" clearInterval(waitForPlotly);\n"
" }\n"
" }, 500 );\n"
" }\n"
" } //end readyState check \n"
" }, 500); //end setInterval \n"
"});\n"
'</script>\n')
# Initialize Plotly libraries
script += _plotly_ex.init_notebook_mode_ex(connected)
# Perform check to see what has been loaded
script += (
"<div id='notebook_load_status' style='font-style:italic;color:blue'>Loading...</div>\n"
"<script type='text/javascript'>\n"
" require(['jquery','jquery-UI','plotly','katex', 'autorender'],\n"
" function($,ui,Plotly,katex,auto) {\n"
" $(document).ready( function() {\n"
" var txt = '';\n"
" if( typeof($('#notebook_load_status').resizable) === 'undefined') {\n"
" txt += '<span class=\"failmsg\">JQueryUI not loaded correctly</span><br>';\n"
" }\n"
" if( typeof(Plotly.newPlot) === 'undefined') {\n"
" txt += '<span class=\"failmsg\">Plotly not loaded correctly</span><br>';\n"
" }\n"
" if(txt.length == 0) {\n"
" txt += '<span class=\"successmsg\">Notebook Initialization Complete</span>';\n"
" if( typeof MathJax !== 'undefined') {\n"
" txt += '<span class=\"successmsg2\"> (+MathJax)</span>';\n"
" } else {\n"
" txt += '<span class=\"successmsg2\"> (+KaTeX)</span>';\n"
" }\n"
" }\n"
" $('#notebook_load_status').html(txt);\n"
" }); });\n"
"</script>\n")
display_ipynb(script) # single call to display keeps things simple
_PYGSTI_WORKSPACE_INITIALIZED = True
self._register_components(autodisplay)
return
def switched_compute(self, fn, *args):
"""
Calls a compute function with special handling of :class:`SwitchedValue` arguments.
This is similar to calling `fn`, given its name and arguments, when
some or all of those arguments are :class:`SwitchedValue` objects.
Caching is employed to avoid duplicating function evaluations which have
the same arguments. Note that the function itself doesn't need to deal
with SwitchValue objects, as this routine resolves such objects into a
series of function evaluations using the underlying value(s) within the
SwitchValue. This routine is primarily used internally for the
computation of tables and plots.
if any of the arguments is an instance of `NotApplicable` then `fn`
is *not* evaluated and the instance is returned as the evaluation
result. If multiple arguments are `NotApplicable` instances, the
first is used as the result.
Parameters
----------
fn : function
The function to evaluate
Returns
-------
fn_values : list
The function return values for all relevant sets of arguments.
Denote the length of this list by N.
switchboards : list
A list of all the relevant Switchboards used during the function
evaluation. Denote the length of this list by M.
switchboard_switch_indices : list
A list of length M whose elements are tuples containing the 0-based
indices of the relevant switches (i.e. those used by any of the
arguments) for each switchboard (element of `switchboards`).
switchpos_map : dict
A dictionary whose keys are switch positions, and whose values are
integers between 0 and N which index the element of `fn_values`
corresponding to the given switch positions. Each
"switch positions" key is a tuple of length M whose elements (one
per switchboard) are tuples of 0-based switch-position indices
indicating the position of the relevant switches of that
switchboard. Thus,
`len(key[i]) = len(switchboard_switch_indices[i])`, where `key`
is a dictionary key.
"""
# Computation functions get stripped-down *value* args
# (strip SwitchedValue stuff away)
switchboards = []
switchBdInfo = []
nonSwitchedArgs = []
switchpos_map = {}
storedKeys = {}
resultValues = []
for i, arg in enumerate(args):
if isinstance(arg, SwitchValue):
isb = None
for j, sb in enumerate(switchboards):
if arg.parent is sb:
isb = j; break
else:
isb = len(switchboards)
switchboards.append(arg.parent)
switchBdInfo.append({
'argument indices': [], # indices of arguments that are children of this switchboard
'value names': [], # names of switchboard value correspond to each argument index
'switch indices': set() # indices of the switches that are actually used by the args
})
assert(isb is not None)
info = switchBdInfo[isb]
info['argument indices'].append(i)
info['value names'].append(arg.name)
info['switch indices'].update(arg.dependencies)
else:
nonSwitchedArgs.append((i, arg))
#print("DB: %d arguments" % len(args))
#print("DB: found %d switchboards" % len(switchboards))
#print("DB: switchBdInfo = ", switchBdInfo)
#print("DB: nonSwitchedArgs = ", nonSwitchedArgs)
#Create a list of lists, each list holding all of the relevant switch positions for each board
switch_positions = []
for isb, sb in enumerate(switchboards):
info = switchBdInfo[isb]
info['switch indices'] = list(info['switch indices']) # set -> list so definite order
switch_ranges = [list(range(len(sb.positionLabels[i])))
for i in info['switch indices']]
sb_switch_positions = list(_itertools.product(*switch_ranges))
# a list of all possible positions for the switches being
# used for the *single* board sb
switch_positions.append(sb_switch_positions)
#loop over all relevant switch configurations (across multiple switchboards)
for pos in _itertools.product(*switch_positions):
# pos[i] gives the switch configuration for the i-th switchboard
#fill in the arguments for our function call
argVals = [None] * len(args)
#first, iterate over all the switchboards
for sw_pos, sb, info in zip(pos, switchboards, switchBdInfo):
# sw_pos is a tuple of the info['switch indices'] switch positions for sb
sis = info['switch indices']
for nm, j in zip(info["value names"], info["argument indices"]):
value_swpos = [sw_pos[sis.index(k)] for k in sb[nm].dependencies]
# potentially a subset of sw_pos, contains only the switch positions
# relevant to the particular SwitchedValue named nm (also the j-th argument)
argVals[j] = sb[nm][tuple(value_swpos)] # tuple needed for proper indexing
#next, fill in the non-switched arguments
for j, arg in nonSwitchedArgs:
argVals[j] = arg
for v in argVals:
if isinstance(v, NotApplicable):
key = "NA"; result = v; break
else:
key, result = self.smartCache.cached_compute(fn, argVals)
if key not in storedKeys or key == 'INEFFECTIVE':
switchpos_map[pos] = len(resultValues)
storedKeys[key] = len(resultValues)
resultValues.append(result)
else:
switchpos_map[pos] = storedKeys[key]
switchboard_switch_indices = [info['switch indices'] for info in switchBdInfo]
return resultValues, switchboards, switchboard_switch_indices, switchpos_map
class Switchboard(_collections.OrderedDict):
"""
Encapsulates a render-able set of user-interactive switches for controlling visualized output.
Outwardly a Switchboard looks like a dictionary of SwitchValue
objects, which in turn look like appropriately sized numpy arrays
of values for some quantity. Different switch positions select
different values and thereby what data is visualized in various
outputs (e.g. tables and plots).
Parameters
----------
ws : Workspace
The containing (parent) workspace.
switches : list
A list of switch names. The length of this list is
the number of switches.
positions : list
Elements are lists of position labels, one per switch.
Length must be equal to `len(switches)`.
types : list of {'buttons','dropdown','slider','numslider'}
A list of switch-type strings specifying what type of switch
each switch is.
- 'buttons': a set of toggle buttons
- 'dropdown': a drop-down (or combo-box)
- 'slider': a horizontal slider (equally spaced items)
- 'numslider': a horizontal slider (spaced by numeric value)
initial_pos : list or None (optional)
A list of 0-based integer indices giving the initial
position of each of the `len(switches)` switches. None
defaults to the first (0-th) position for each switch.
descriptions : list (optional)
A string description for each of the `len(switches)` switches.
show : list (optional)
A list of boolean (one for each of the `len(switches)` switches)
indicating whether or not that switch should be rendered. The
special values "all" and "none" show all or none of the switches,
respectively.
id : str (optional)
A DOM identifier to use when rendering this Switchboard to HTML.
Usually leaving this value as `None` is best, in which case a
random identifier is created.
use_loadable_items : bool, optional
Whether "loadable" items are being used. When using loadable items,
elements of a web page are explicitly told when to initialize themselves
by a "load_loadable_item" signal instead of loading as soon as the DOM
is ready (a traditional on-ready handler). Using this option increases
the performance of large/complex web pages.
"""
def __init__(self, ws, switches, positions, types, initial_pos=None,
descriptions=None, show="all", id=None, use_loadable_items=False):
"""
Create a new Switchboard.
Parameters
----------
switches : list
A list of switch names. The length of this list is
the number of switches.
positions : list
Elements are lists of position labels, one per switch.
Length must be equal to `len(switches)`.
types : list of {'buttons','dropdown','slider','numslider'}
A list of switch-type strings specifying what type of switch
each switch is.
- 'buttons': a set of toggle buttons
- 'dropdown': a drop-down (or combo-box)
- 'slider': a horizontal slider (equally spaced items)
- 'numslider': a horizontal slider (spaced by numeric value)
initial_pos : list or None (optional)
A list of 0-based integer indices giving the initial
position of each of the `len(switches)` switches. None
defaults to the first (0-th) position for each switch.
descriptions : list (optional)
A string description for each of the `len(switches)` switches.
show : list (optional)
A list of boolean (one for each of the `len(switches)` switches)
indicating whether or not that switch should be rendered. The
special values "all" and "none" show all or none of the switches,
respectively.
id : str (optional)
A DOM identifier to use when rendering this Switchboard to HTML.
Usually leaving this value as `None` is best, in which case a
random identifier is created.
use_loadable_items : bool, optional
Whether "loadable" items are being used. When using loadable items,
elements of a web page are explicitly told when to initialize themselves
by a "load_loadable_item" signal instead of loading as soon as the DOM
is ready (a traditional on-ready handler). Using this option increases
the performance of large/complex web pages.
"""
# Note: intentionally leave off ws argument desc. in docstring
assert(len(switches) == len(positions))
self.ID = random_id() if (id is None) else id
self.ws = ws # Workspace
self.switchNames = switches
self.switchTypes = types
self.switchIDs = ["switchbd%s_%d" % (self.ID, i)
for i in range(len(switches))]
self.positionLabels = positions
self.use_loadable_items = use_loadable_items
if initial_pos is None:
self.initialPositions = _np.array([0] * len(switches), _np.int64)
else:
assert(len(initial_pos) == len(switches))
self.initialPositions = _np.array(initial_pos, _np.int64)
self.descriptions = descriptions
if show == "all":
self.show = [True] * len(switches)
elif show == "none":
self.show = [False] * len(switches)
else:
assert(len(show) == len(switches))
self.show = show
self.widget = None
super(Switchboard, self).__init__([])
def add(self, varname, dependencies):
"""
Adds a new switched-value to this Switchboard.
Parameters
----------
varname : str
A name for the variable being added. This name will be used to
access the new variable (as either a dictionary key or as an
object member).
dependencies : list or tuple
The (0-based) switch-indices specifying which switch positions
the new variable is dependent on. For example, if the Switchboard
has two switches, one for "amplitude" and one for "frequency", and
this value is only dependent on frequency, then `dependencies`
should be set to `(1,)` or `[1]`.
Returns
-------
None
"""
super(Switchboard, self).__setitem__(varname, SwitchValue(self, varname, dependencies))
def add_unswitched(self, varname, value):
"""
Adds a new non-switched-value to this Switchboard.
This can be convenient for attaching related non-switched data to
a :class:`Switchboard`.
Parameters
----------
varname : str
A name for the variable being added. This name will be used to
access the new variable (as either a dictionary key or as an
object member).
value : object
The un-switched value to associate with `varname`.
Returns
-------
None
"""
super(Switchboard, self).__setitem__(varname, value)
def __setitem__(self, key, val):
raise KeyError("Use add(...) to add an item to this swichboard")
def render(self, typ="html"):
"""
Render this Switchboard into the requested format.
The returned string(s) are intended to be used to embedded a
visualization of this object within a larger document.
Parameters
----------
typ : {"html"}
The format to render as. Currently only HTML is supported.
Returns
-------
dict
A dictionary of strings whose keys indicate which portion of
the embeddable output the value is. Keys will vary for different
`typ`. For `"html"`, keys are `"html"` and `"js"` for HTML and
and Javascript code, respectively.
"""
return self._render_base(typ, None, self.show)
def _render_base(self, typ, view_suffix, show):
"""
Break off this implementation so SwitchboardViews can use.
"""
assert(typ == "html"), "Can't render Switchboards as anything but HTML"
switch_html = []; switch_js = []
for name, baseID, styp, posLbls, ipos, bShow in zip(
self.switchNames, self.switchIDs, self.switchTypes,
self.positionLabels, self.initialPositions, show):
ID = (baseID + view_suffix) if view_suffix else baseID
style = "" if bShow else " style='display: none'"
if styp == "buttons":
html = "<div class='switch_container'%s>\n" % style
html += "<fieldset id='%s'>\n" % ID
if name:
html += "<legend>%s: </legend>\n" % name
for k, lbl in enumerate(posLbls):
checked = " checked='checked'" if k == ipos else ""
html += "<label for='%s-%d'>%s</label>\n" % (ID, k, lbl)
html += "<input type='radio' name='%s' id='%s-%d' value=%d%s>\n" \
% (ID, ID, k, k, checked)
html += "</fieldset></div>\n"
js = " $('#%s > input').checkboxradio({ icon: false });" % ID
if view_suffix:
js += "\n".join((
"function connect_%s_to_base(){" % ID,
" if( $('#%s').hasClass('initializedSwitch') ) {" % baseID, # "if base switch is ready"
" $('#%s').on('change', function(event, ui) {" % baseID,
" var v = $(\"#%s > input[name='%s']:checked\").val();" % (baseID, baseID),
" var el = $(\"#%s > input[name='%s'][value=\" + v + \"]\");" % (ID, ID),
" if( el.is(':checked') == false ) { ",
" el.click();",
" }",
" });"
" $('#%s').on('change', function(event, ui) {" % ID,
" var v = $(\"#%s > input[name='%s']:checked\").val();" % (ID, ID),
" var el = $(\"#%s > input[name='%s'][value=\" + v + \"]\");" % (baseID, baseID),
" if( el.is(':checked') == false ) { ",
" el.click();",
" }",
" });",
" $('#%s').trigger('change');" % baseID,
" }",
" else {", # need to wait for base switch
" setTimeout(connect_%s_to_base, 500);" % ID,
" console.log('%s base NOT initialized: Waiting...');" % ID,
" }",
"};",
"connect_%s_to_base();" % ID # start trying to connect
))
elif styp == "dropdown":
html = "<div class='switch_container'%s><fieldset>\n" % style
if name:
html += "<label for='%s'>%s</label>\n" % (ID, name)
html += "<select name='%s' id='%s'>\n" % (ID, ID)
for k, lbl in enumerate(posLbls):
selected = " selected='selected'" if k == ipos else ""
html += "<option value=%d%s>%s</option>\n" % (k, selected, lbl)
html += "</select>\n</fieldset></div>\n"