-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlotLyPlottingMod.py
288 lines (262 loc) · 10.3 KB
/
PlotLyPlottingMod.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
# coding: utf-8
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
from plotly.offline import plot
from plotly.graph_objs import Scatter
import plotly as py
import pandas as pd
from plotly import session, tools, utils
import uuid
import json
from plotly import tools
import plotly.graph_objs as go
init_notebook_mode()
import LHCclass
import madxmodule
import pandas as pd
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
import csv
import glob
import datetime
import collections
import time
import subprocess
import os
from scipy import optimize as opt
from scipy import constants as const
from StringIO import StringIO
from matplotlib import rc,rcParams
from matplotlib.patches import Rectangle
import itertools
# simdata
from pandas.tools.plotting import autocorrelation_plot
from pandas.tools.plotting import lag_plot
from pandas.tools.plotting import scatter_matrix
# function definitions
# ********************
# -----------------------------------------------------------------------------------------------------
# plotly javascript
# loading of the javascript
# -----------------------------------------------------------------------------------------------------
def get_plotlyjs():
path = os.path.join('plotly.min.js')
plotlyjs = resource_string('plotly', path).decode('utf-8')
return plotlyjs
# -----------------------------------------------------------------------------------------------------
# plotly offline plotting function
# allows to generate offline plots wiht plotly
# -----------------------------------------------------------------------------------------------------
def new_iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',validate=True):
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
width = figure.get('layout', {}).get('width', '100%')
height = figure.get('layout', {}).get('height', 525)
try:
float(width)
except (ValueError, TypeError):
pass
else:
width = str(width) + 'px'
try:
float(width)
except (ValueError, TypeError):
pass
else:
width = str(width) + 'px'
plotdivid = uuid.uuid4()
jdata = json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
jlayout = json.dumps(figure.get('layout', {}), cls=utils.PlotlyJSONEncoder)
config = {}
config['showLink'] = show_link
config['linkText'] = link_text
jconfig = json.dumps(config)
plotly_platform_url = session.get_session_config().get('plotly_domain',
'https://plot.ly')
if (plotly_platform_url != 'https://plot.ly' and
link_text == 'Export to plot.ly'):
link_domain = plotly_platform_url .replace('https://', '') .replace('http://', '')
link_text = link_text.replace('plot.ly', link_domain)
script = '\n'.join([
'Plotly.plot("{id}", {data}, {layout}, {config}).then(function() {{',
' $(".{id}.loading").remove();',
'}})'
]).format(id=plotdivid,
data=jdata,
layout=jlayout,
config=jconfig)
html="""<div class="{id} loading" style="color: rgb(50,50,50);">
</div>
<div id="{id}" style="height: {height}; width: {width};"
class="plotly-graph-div">
</div>
<script type="text/javascript">
{script}
</script>
""".format(id=plotdivid, script=script,
height=height, width=width)
return html
# -----------------------------------------------------------------------------------------------------
# plotly layout template
# defines a layout template to use with plotly plotting
# returs basically a dictionary but can be a nested dictionary
# -----------------------------------------------------------------------------------------------------
def layouttemplate(
titlein = '',
titlefontin = dict(
family = 'Arial, sans-serif',
size = 16,
color = 'black'
),
widthin = 0,
heightin = 0,
xaxisdc = dict(
title = '',
titlefont = dict(
family = 'Arial, sans-serif',
size = 16,
color = 'black'
),
range = [-1600,1600],
linewidth = 4,
gridwidth = 2,
# gridcolor = 'rgb(140,140,140)',
autotick = False,
ticks = 'outside',
tick0 = 0, # location of first tick
dtick = 100, # distance between ticks
ticklen = 10, # length of the tick line
tickwidth = 0.5, # width of the tick line
tickcolor = '#000',
tickangle = 0,
showticklabels = True,
tickfont = dict(
family = 'Arial, sans-serif',
size = 12,
color = 'black'
),
zeroline = True,
zerolinewidth = 2
# showgrid=True
),
yaxisdc = dict(
title = '',
titlefont = dict(
family = 'Arial, sans-serif',
size = 16,
color = 'black'
),
range = [-1600,1600],
linewidth = 4,
gridwidth = 2,
# gridcolor = 'rgb(140,140,140)',
autotick = False,
ticks = 'outside',
tick0 = 0, # location of first tick
dtick = 100, # distance between ticks
ticklen = 10, # length of the tick line
tickwidth = 0.5, # width of the tick line
tickcolor = '#000',
tickangle = 0,
showticklabels = True,
tickfont = dict(
family = 'Arial, sans-serif',
size = 12,
color = 'black'
),
zeroline = True,
zerolinewidth = 2
# showgrid=True
),
shapeslst = []
):
try:
float(widthin)
except (ValueError, TypeError):
pas
try:
float(heightin)
except (ValueError, TypeError):
pass
if ((widthin > 0) & (heightin > 0)):
layout = go.Layout(
title = titlein,
titlefont = titlefontin,
autosize = False,
width = widthin,
height= heightin,
xaxis= xaxisdc,
yaxis= yaxisdc,
shapes = shapeslst
)
else:
layout = go.Layout(
title = titlein,
titlefont = titlefontin,
autosize = True,
xaxis= xaxisdc,
yaxis= yaxisdc,
shapes = shapeslst
)
return layout
# -----------------------------------------------------------------------------------------------------
# transforming data to be plotted with plotly
# basically returns a list of traces from the input data that can be plotted using plotly plot or iplot
# and the above defined new_iplot
# -----------------------------------------------------------------------------------------------------
def tracelist(xlist,ylist,legendname=None,hovertext=None,setmode='scatter',hover=True):
trace = {
'x' : xlist,
'y' : ylist,
'mode' : setmode,
'name' : legendname,
'text' : hovertext,
'hoverinfo' : hover
}
return trace
# -----------------------------------------------------------------------------------------------------
# reading twiss outfile *.tfs
# function that removes the headers from the tfs file and returns a dataframe containing only the data
# -----------------------------------------------------------------------------------------------------
def readtfs(tfsfile):
df = pd.read_csv(tfsfile,skiprows=range(45),nrows=2,delim_whitespace=True)
df = df[df['NAME']!='%s']
colsdf = list(df.columns[1:])
df = pd.read_csv(tfsfile,skiprows=range(47),delim_whitespace=True,names=colsdf,index_col=False)
return df
# -----------------------------------------------------------------------------------------------------
# plotly shapes
# definition of beamlinegraphic element rectangle for MB and MQ
# -----------------------------------------------------------------------------------------------------
def rectangledict(x0,y0,x1,y1,rgbcolorlinestr='rgb(10,10,10)',width=1,rgbfillcolor='rgb(100,250,250)'):
out = {
'type' : 'rect',
'xref' : 'x',
'yref' : 'y',
'x0' : x0,
'y0' : y0,
'x1' : x1,
'y1' : y1,
'line' : {
'color' : rgbcolorlinestr,
'width' : width,
},
'fillcolor' : rgbfillcolor,
'opacity' :0.9
}
return out
# -----------------------------------------------------------------------------------------------------
# plotly shapes
# definition of beamlinegraphic element hoverinfo
# ---------------------------------------------------------------------------------------------------
def shapeannotations(xin,yin,textin):
out = go.Scatter(
x = [xin],
y = [yin],
text = textin,
mode = 'markers',
opacity = 0,
hoverinfo = 'text',
showlegend=False
)
return out