@@ -17,7 +17,7 @@ class Configuration(object):
17
17
heavily abridged version of what is found in postprocessing.
18
18
"""
19
19
def __init__ (self , config_file ):
20
- if os .access (config_file , os .R_OK ) == False :
20
+ if os .access (config_file , os .R_OK ) is False :
21
21
raise RuntimeError ("Configuration file doesn't exist or is not readable: %s" % config_file )
22
22
with open (config_file , 'r' ) as cfg :
23
23
json_encoded = cfg .read ()
@@ -31,13 +31,14 @@ def __init__(self, config_file):
31
31
self .publisher_username = config ['publisher_username' ] if 'publisher_username' in config else ''
32
32
self .publisher_password = config ['publisher_password' ] if 'publisher_password' in config else ''
33
33
34
+
34
35
def _determine_config_file (config_file ):
35
36
# put together the list of all choices
36
37
choices = [config_file , CONFIG_FILE , CONFIG_FILE_ALTERNATE ]
37
38
38
39
# filter out bad choices
39
40
choices = [name for name in choices
40
- if not name is None ]
41
+ if name is not None ]
41
42
choices = [name for name in choices
42
43
if len (name ) > 0 ]
43
44
choices = [name for name in choices
@@ -49,6 +50,7 @@ def _determine_config_file(config_file):
49
50
else :
50
51
return None
51
52
53
+
52
54
def read_configuration (config_file = None ):
53
55
"""
54
56
Returns a new configuration object for a given
@@ -62,6 +64,7 @@ def read_configuration(config_file=None):
62
64
logging .info ('Loading configuration \' %s\' ' % config_file )
63
65
return Configuration (config_file )
64
66
67
+
65
68
def _loadDiv (filename ):
66
69
if not os .path .exists (filename ):
67
70
raise RuntimeError ('\' %s\' does not exist' % filename )
@@ -70,21 +73,23 @@ def _loadDiv(filename):
70
73
div = handle .read ()
71
74
return div
72
75
76
+
73
77
def _getURL (url_template , instrument , run_number ):
74
78
import string
75
- url_template = string .Template (url_template )
79
+ url_template = string .Template (url_template )
76
80
url = url_template .substitute (instrument = instrument ,
77
81
run_number = str (run_number ))
78
82
return url
79
83
84
+
80
85
def publish_plot (instrument , run_number , files , config = None ):
81
86
# read the configuration if one isn't provided
82
87
if config is None :
83
88
config = read_configuration ()
84
89
# verify that it has an attribute that matters
85
90
try :
86
91
config .publish_url
87
- except AttributeError : # assume that it is a filename
92
+ except AttributeError : # assume that it is a filename
88
93
config = read_configuration (config )
89
94
90
95
run_number = str (run_number )
@@ -103,17 +108,148 @@ def publish_plot(instrument, run_number, files, config=None):
103
108
files = files , verify = False )
104
109
return request
105
110
111
+
112
+ def plot1d (run_number , data_list , data_names = None , x_title = '' , y_title = '' ,
113
+ x_log = False , y_log = False , instrument = '' , show_dx = True , title = '' , publish = True ):
114
+ """
115
+ Produce a 1D plot
116
+ @param data_list: list of traces [ [x1, y1], [x2, y2], ...]
117
+ @param data_names: name for each trace, for the legend
118
+ """
119
+ from plotly .offline import plot
120
+ import plotly .graph_objs as go
121
+
122
+ # Create traces
123
+ if not isinstance (data_list , list ):
124
+ raise RuntimeError ("plot1d: data_list parameter is expected to be a list" )
125
+
126
+ # Catch the case where the list is in the format [x y]
127
+ data = []
128
+ show_legend = False
129
+ if len (data_list ) == 2 and not isinstance (data_list [0 ], list ):
130
+ label = ''
131
+ if isinstance (data_names , list ) and len (data_names ) == 1 :
132
+ label = data_names [0 ]
133
+ show_legend = True
134
+ data = [go .Scatter (name = label , x = data_list [0 ], y = data_list [1 ])]
135
+ else :
136
+ for i in range (len (data_list )):
137
+ label = ''
138
+ if isinstance (data_names , list ) and len (data_names ) == len (data_list ):
139
+ label = data_names [i ]
140
+ show_legend = True
141
+ err_x = {}
142
+ err_y = {}
143
+ if len (data_list [i ]) >= 3 :
144
+ err_y = dict (type = 'data' , array = data_list [i ][2 ], visible = True )
145
+ if len (data_list [i ]) >= 4 :
146
+ err_x = dict (type = 'data' , array = data_list [i ][3 ], visible = True )
147
+ if show_dx is False :
148
+ err_x ['thickness' ] = 0
149
+ data .append (go .Scatter (name = label , x = data_list [i ][0 ], y = data_list [i ][1 ],
150
+ error_x = err_x , error_y = err_y ))
151
+
152
+ x_layout = dict (title = x_title , zeroline = False , exponentformat = "power" ,
153
+ showexponent = "all" , showgrid = True ,
154
+ showline = True , mirror = "all" , ticks = "inside" )
155
+ if x_log :
156
+ x_layout ['type' ] = 'log'
157
+ y_layout = dict (title = y_title , zeroline = False , exponentformat = "power" ,
158
+ showexponent = "all" , showgrid = True ,
159
+ showline = True , mirror = "all" , ticks = "inside" )
160
+ if y_log :
161
+ y_layout ['type' ] = 'log'
162
+
163
+ layout = go .Layout (
164
+ showlegend = show_legend ,
165
+ autosize = True ,
166
+ width = 600 ,
167
+ height = 400 ,
168
+ margin = dict (t = 40 , b = 40 , l = 80 , r = 40 ), # noqa: E741
169
+ hovermode = 'closest' ,
170
+ bargap = 0 ,
171
+ xaxis = x_layout ,
172
+ yaxis = y_layout ,
173
+ title = title
174
+ )
175
+
176
+ fig = go .Figure (data = data , layout = layout )
177
+ plot_div = plot (fig , output_type = 'div' , include_plotlyjs = False , show_link = False )
178
+ if publish :
179
+ try :
180
+ return publish_plot (instrument , run_number , files = {'file' : plot_div })
181
+ except : # noqa: E722
182
+ logging .error ("Publish plot failed: %s" , sys .exc_value )
183
+ return None
184
+ else :
185
+ return plot_div
186
+
187
+
188
+ def plot_heatmap (run_number , x , y , z , x_title = '' , y_title = '' , surface = False ,
189
+ x_log = False , y_log = False , instrument = '' , title = '' , publish = True ):
190
+ """
191
+ Produce a 2D plot
192
+ """
193
+ from plotly .offline import plot
194
+ import plotly .graph_objs as go
195
+
196
+ x_layout = dict (title = x_title , zeroline = False , exponentformat = "power" ,
197
+ showexponent = "all" , showgrid = True ,
198
+ showline = True , mirror = "all" , ticks = "inside" )
199
+ if x_log :
200
+ x_layout ['type' ] = 'log'
201
+
202
+ y_layout = dict (title = y_title , zeroline = False , exponentformat = "power" ,
203
+ showexponent = "all" , showgrid = True ,
204
+ showline = True , mirror = "all" , ticks = "inside" )
205
+ if y_log :
206
+ y_layout ['type' ] = 'log'
207
+
208
+ layout = go .Layout (
209
+ showlegend = False ,
210
+ autosize = True ,
211
+ width = 600 ,
212
+ height = 500 ,
213
+ margin = dict (t = 40 , b = 40 , l = 80 , r = 40 ), # noqa: E741
214
+ hovermode = 'closest' ,
215
+ bargap = 0 ,
216
+ xaxis = x_layout ,
217
+ yaxis = y_layout ,
218
+ title = title
219
+ )
220
+
221
+ colorscale = [[0 , "rgb(0,0,131)" ], [0.125 , "rgb(0,60,170)" ], [0.375 , "rgb(5,255,255)" ],
222
+ [0.625 , "rgb(255,255,0)" ], [0.875 , "rgb(250,0,0)" ], [1 , "rgb(128,0,0)" ]]
223
+ plot_type = 'surface' if surface else 'heatmap'
224
+ trace = go .Heatmap (z = z , x = x , y = y , autocolorscale = False , # type=plot_type,
225
+ hoverinfo = "none" , colorscale = colorscale )
226
+ fig = go .Figure (data = [trace ], layout = layout )
227
+ plot_div = plot (fig , output_type = 'div' , include_plotlyjs = False , show_link = False )
228
+
229
+ # The following would remove the hover options, which are not accessible through python
230
+ # https://github.com/plotly/plotly.js/blob/master/src/components/modebar/buttons.js
231
+ # plot_div = plot_div.replace('modeBarButtonsToRemove:[]',
232
+ # 'modeBarButtonsToRemove:["hoverClosestCartesian",
233
+ # "hoverCompareCartesian"]')
234
+
235
+ if publish :
236
+ try :
237
+ return publish_plot (instrument , run_number , files = {'file' : plot_div })
238
+ except : # noqa: E722
239
+ logging .error ("Publish plot failed: %s" , sys .exc_value )
240
+ return None
241
+ else :
242
+ return plot_div
243
+
244
+
106
245
if __name__ == '__main__' :
107
246
import sys
108
247
div = _loadDiv (sys .argv [1 ])
109
- #print('**********')
110
- #print(div)
111
248
112
249
# run information is generated from the filename
113
- name = os .path .split (sys .argv [1 ])[- 1 ]
250
+ name = os .path .split (sys .argv [1 ])[- 1 ]
114
251
(instr , runnumber ) = name .split ('_' )[:2 ]
115
252
116
253
config = read_configuration ('post_processing.conf' )
117
- #config = read_configuration('post_processing_full.conf')
118
- request = publish_plot (instr , runnumber , {'file' :div }, config )
254
+ request = publish_plot (instr , runnumber , {'file' : div }, config )
119
255
print ('request returned' , request .status_code )
0 commit comments