1 """ plugin to send figures to SciDAVis
2
3 Copyright 2008-2015 Research Foundation State University of New York
4 This file is part of QUB Express.
5
6 QUB Express is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 QUB Express is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License,
17 named LICENSE.txt, in the QUB Express program directory. If not, see
18 <http://www.gnu.org/licenses/>.
19
20
21 """
22
23 import os
24 import shutil
25 import tempfile
26 import time
27 import traceback
28
29 import numpy
30
31 import qubx.GTK
32 import qubx.notebook
33 import qubx.pyenv
34 import qubx.remote_ctl
35
36 from Queue import Empty
37
38 SOCKID_PATH = os.path.join(tempfile.gettempdir(), 'scidavis.sockid')
39 WIN32 = SOCKID_PATH.find('\\') >= 0
40 if WIN32:
41 from _winreg import *
42
43 TIMEOUT = 5.0
44
45 SCIDAVIS = os.environ.get('QUBX_SCIDAVIS') or 'scidavis'
46
54 try:
55 self.scidavis.reval('1', timeout=TIMEOUT)
56 except:
57 self.scidavis = None
58 if self.scidavis is None:
59 try:
60 self.scidavis = qubx.remote_ctl.RemoteProxy(sockid_path=SOCKID_PATH)
61 except:
62 if self.start_scidavis():
63 try:
64 self.scidavis = qubx.remote_ctl.RemoteProxy(sockid_path=SOCKID_PATH)
65 except:
66 print "Can't connect to SciDAVis; is it running?"
67 return self.scidavis
77 print 'running',qtipath,'...'
78 app_path = qubx.pyenv.env.globals['app_path']
79 script_path = os.path.join(app_path, 'scidavis')
80
81 if WIN32:
82 os.startfile(qtipath)
83 else:
84
85 os.system("'%s' -x '%s'&" % (qtipath, os.path.join(script_path, 'start_remote_control.py')))
86 time.sleep(TIMEOUT)
88 Nr, Nc = item.get_shape()
89 arr = numpy.zeros(shape=(Nr,Nc))
90 for c in xrange(Nc):
91 try:
92 col = item.get_col(c)
93 arr[:len(col),c] = item.get_col(c)
94 except:
95 pass
96 scidavis.set_array(arr, 'qub_express_table', timeout=TIMEOUT)
97 app_path = qubx.pyenv.env.globals['app_path']
98 scidavis.rexec(open(os.path.join(app_path, 'scidavis', 'table.py'), 'r').read(), timeout=TIMEOUT)
99 if show:
100 scidavis.rexec('qubx_qti_table = add_table("%s", qub_express_table, %s)' % (item.get_caption(), repr(item.get_headers())))
102 scidavis = self.nb_init()
103 app_path = qubx.pyenv.env.globals['app_path']
104 if not scidavis:
105 return
106 if isinstance(item, qubx.notebook.NbItems):
107 for it in item:
108 self.nb_send(it)
109 return
110 if isinstance(item, qubx.notebook.NbTrace):
111 self.send_table(scidavis, item)
112 scidavis.rexec(open(os.path.join(app_path, 'scidavis', 'trace.py'), 'r').read(), timeout=TIMEOUT)
113 scidavis.rexec("""add_trace_plot("%s", "%s", "%s", qubx_qti_table, %s)""" %
114 (item.get_caption(), item.get_xlabel(), item.get_ylabel(),
115 ', '.join([str(series.ycol) for series in item.get_trace_series()])))
116 return
117 if isinstance(item, qubx.notebook.NbChart):
118 series = item.get_series()
119 lines = [s for s in series if s.ser_type == qubx.notebook.LINES]
120 if series and series[0].ser_type == qubx.notebook.HISTOGRAM:
121 self.send_table(scidavis, item)
122 scidavis.rexec(open(os.path.join(app_path, 'scidavis', 'histogram.py'), 'r').read(), timeout=TIMEOUT)
123 scidavis.rexec("""add_hist(qubx_qti_table, "%s", "%s", "%s", %i, %i, []%s)""" %
124 (item.get_caption(), item.get_xlabel(), item.get_ylabel(),
125 series[0].xcol+1, series[0].ycol+1,
126 lines and (", %s"%", ".join([str(ser.ycol+1) for ser in lines]))
127 or ""))
128 elif series and series[0].ser_type == qubx.notebook.DOTS:
129 scidavis.rexec(open(os.path.join(app_path, 'scidavis', 'histogram.py'), 'r').read(), timeout=TIMEOUT)
130 scidavis.rexec(open(os.path.join(app_path, 'scidavis', 'scatter.py'), 'r').read(), timeout=TIMEOUT)
131 ser = series[0]
132 nrow = ser.nrow
133 if nrow < 0: nrow = item.get_shape()[0]
134 line = lines and lines[0] or ser
135 line_nrow = line.nrow
136 if line_nrow < 0: line_nrow = nrow
137 hseries = [s for s in series if s.ser_type == qubx.notebook.HISTOGRAM]
138 if hseries:
139 hser = hseries[0]
140 hnrow = hser.nrow
141 if hnrow < 0: hnrow = item.get_shape()[0]
142 self.send_table(scidavis, item)
143 scidavis.rexec("""qubx_qti_table.setNumRows(%i)""" % hnrow)
144 scidavis.rexec("""hist_layer = add_hist(qubx_qti_table, "%s", "%s", "%s", %i, %i, []).activeLayer()""" %
145 (item.get_caption()+"_hist", item.get_xlabel(), item.get_ylabel(), hser.xcol+1, hser.ycol+1))
146 else:
147 scidavis.rexec("""hist_layer = None""")
148 self.send_table(scidavis, item, show=False)
149 scidavis.rexec("""add_scatter("%s", "%s", "%s", qub_express_table[%d:%d,%d].flatten(), qub_express_table[%d:%d,%d].flatten(),
150 %s, [], qub_express_table[%d:%d,%d].flatten(), hist_layer%s)"""%
151 (item.get_caption(), item.get_xlabel(), item.get_ylabel(),
152 ser.first_row, ser.first_row+nrow, ser.xcol,
153 ser.first_row, ser.first_row+nrow, ser.ycol,
154 item.get_color_indices() or [0]*nrow,
155 line.first_row, line.first_row + line_nrow, line.xcol,
156 lines and (", %s"%", ".join(["qub_express_table[%d:%d,%d].flatten()" %
157 (line.first_row, line.first_row+line_nrow, line.ycol)
158 for line in lines]))
159 or ""))
160 elif series:
161 self.send_table(scidavis, item)
162 pass
163 return
164 if isinstance(item, qubx.notebook.NbTable):
165 self.send_table(scidavis, item)
166 return
167
170
171
172
173
174
175
176