This repository has been archived by the owner on May 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
views.py
473 lines (429 loc) · 16.9 KB
/
views.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
from django.http import Http404, HttpResponse, HttpResponseBadRequest
from django.shortcuts import render
from forms import PreviewForm,AnnotationsForm,PlotForm
from plot import settings
import os
import re
from math import floor
from collections import defaultdict
import numpy as np
import pandas as pd
import json
import csv
from itertools import islice
import openpyxl
from PIL import Image
import tempfile
import shutil
import plotly.plotly as py
from plotly.graph_objs import *
import omero
from omeroweb.webclient.decorators import login_required
ANNPATH = '/home/omero/temp'
username = settings.PLOTLY_USERNAME
api_key = settings.PLOTLY_APIKEY
py.sign_in(username,api_key)
#ANNPATH = tempfile.mkdtemp(prefix='downloaded_annotations')
def plotly_graph(gdata, glayout):
"""
data = [ { xdata: [], ydata: [] }, trace1: {xdata: [], ydata: []} ]
layout = {plot_mode: '', title: '', xLabel: '', yLabel: '',
xmin: '', xmax: '', ymin: '', ymax: ''}
"""
traces = []
plot_mode = glayout['plot_mode']
xmin = float(glayout['xmin'].replace(u'\u2212','-'))
xmax = float(glayout['xmax'].replace(u'\u2212','-'))
ymin = float(glayout['ymin'].replace(u'\u2212','-'))
ymax = float(glayout['ymax'].replace(u'\u2212','-'))
xaxis_range = [xmin,xmax]
yaxis_range = [ymin,ymax]
print xaxis_range
print yaxis_range
for trace in gdata:
if 'bar' in plot_mode:
traces.append(Bar(
x=[int(xd) for xd in trace['xdata']],
y=[int(yd) for yd in trace['ydata']]))
elif plot_mode == 'lines':
traces.append(Scatter(
x=[int(xd) for xd in trace['xdata']],
y=[int(yd) for yd in trace['ydata']],
mode='lines'))
elif plot_mode == 'markers':
traces.append(Scatter(
x=[int(xd) for xd in trace['xdata']],
y=[int(yd) for yd in trace['ydata']],
mode='markers'))
elif plot_mode == 'lines+markers':
traces.append(Scatter(
x=[int(xd) for xd in trace['xdata']],
y=[int(yd) for yd in trace['ydata']],
mode='lines+markers'))
graph_layout = Layout(
title=glayout['title'],
xaxis=XAxis(
title=glayout['xLabel'],
showline=True,
ticks='outside',
mirror='all',
autorange=False,
range=xaxis_range
),
yaxis=YAxis(
title=glayout['yLabel'],
showline=True,
ticks='outside',
mirror='all',
autorange=False,
range=yaxis_range
)
)
print traces
graph_data = Data(traces)
fname = glayout['title'] + ".png"
output_dir = tempfile.mkdtemp(prefix='exported_plots')
path = os.path.join(output_dir,fname)
py.image.save_as({'data': graph_data, 'layout': graph_layout}, path)
return output_dir,path
def upload_plot(conn, path):
"""
This creates a new Image in OMERO using all the images in destination folder as Z-planes
"""
gid = conn.getGroupFromContext().getId()
conn.SERVICE_OPTS.setOmeroGroup(gid)
# Need to check whether we're dealing with RGB images (3 channels) or greyscale (1 channel)
img = Image.open(path)
img.load()
sizeC = len(img.split())
sizeZ = 1
sizeT = 1
imageName = os.path.basename(path)
print 'imageName',imageName
# Create a new Image in OMERO, with the jpeg images as a Z-stack.
# We need a generator to produce numpy planes in the order Z, C, T.
def plane_generator():
img = Image.open(path)
img.load() # need to get the data in hand before...
channels = img.split()
for channel in channels:
numpyPlane = np.asarray(channel)
yield numpyPlane
# Create the image
plane_gen = plane_generator()
newImg = conn.createImageFromNumpySeq(plane_gen, imageName, sizeZ=sizeZ, sizeC=sizeC, sizeT=sizeT)
print "New Image ID", newImg.getId()
return newImg
def d3_data(xdata,ydata):
# ydata is a list of lists - index of 0 is a short
# term fudge
d3data = []
for x,y in zip(xdata,ydata[0]):
d_dict = {}
d_dict['x'] = x
d_dict['y'] = y
d3data.append(d_dict)
return d3data
def flot_data(xdata,ydata):
# ydata is a list of lists
fdata = []
for yd in ydata:
sdata = []
for i,y in enumerate(yd):
sdata.append([xdata[i],y])
fdata.append(sdata)
return fdata
def saveas_csv(fpath,data):
filename, file_extension = os.path.splitext(fpath)
csv_path = filename + '.csv'
data.to_csv(csv_path)
return csv_path
def get_data(path,ext,header_row,sheet):
if ('xls' in ext):
with open(path) as t_in:
data = pd.read_excel(t_in,header=int(header_row),sheetname=sheet,\
engine='xlrd',
index_col=False)
else:
with open(path) as t_in:
data = pd.read_csv(t_in,header=int(header_row),\
sep=r'\t|,',engine='python',\
index_col=False)
return data
def get_column(path,ext,col,header_row,sheet):
try:
data = get_data(path,ext,header_row,sheet)
if type(col) != list:
return list(data[col].values)
else:
vals = []
for c in col:
vals.append(list(data[c].values))
return vals
except:
print 'there was a problem parsing the data'
return None
def preview_data(path,ext,sheet):
max_rows = 10
max_cols = 10
# try:
if '.xls' in ext:
wb = openpyxl.load_workbook(path)
sh = wb.get_sheet_names()[sheet]
shdata = wb.get_sheet_by_name(sh)
num_rows = shdata.get_highest_row() - 1
num_cols = shdata.get_highest_column()
data = []
for r in range(max_rows):
row_data = []
for c in range(max_cols):
row_data.append(shdata.cell(row=r+1, column=c+1).value)
data.append(row_data)
elif '.txt' in ext:
num_rows = sum(1 for line in open(path))
with open(path) as file:
data = [next(file)[:max_cols] for x in xrange(max_rows)]
elif '.csv' in ext:
num_rows = sum(1 for line in open(path))
with open(path,'rb') as file:
csv_file = csv.reader(file)
data = []
for i in range(max_rows):
data.append(csv_file.next()[:max_cols])
print num_rows
if num_rows < 1000:
return data
else:
return None
#except:
# print 'there was a problem parsing the data'
# return None
def parse_annotation(path,ext,header_row,sheet):
#try:
data = get_data(path,ext,header_row,sheet)
if 'csv' not in ext:
# save all files as csv as parsing with eventually be
# done with d3.js
csv_path = saveas_csv(path,data)
else:
csv_path = path
num_rows = len(data.index)
if num_rows < 1000:
columns = [(" ", " ")]
for col in data.columns.values:
columns.append((col,col))
message = "Sucessfully processed %s for plotting" % os.path.basename(path)
return columns,message,csv_path
else:
columns = None
message = "Unfortunately that dataset has too many columns for plotting"
return columns,message,csv_path
#except:
# print 'there was a problem parsing the data'
# return None
def download_annotation(ann):
"""
Downloads the specified file to and returns the path on the server
@param ann: the file annotation being downloaded
"""
if not os.path.exists(ANNPATH):
os.makedirs(ANNPATH)
file_path = os.path.join(ANNPATH, ann.getFile().getName())
if os.path.isfile(file_path):
return file_path
else:
f = open(str(file_path), 'w')
print "\nDownloading file to", file_path, "..."
try:
for chunk in ann.getFileInChunks():
f.write(chunk)
finally:
f.close()
print "File downloaded!"
return file_path
def find_duplicate_annotations(mylist):
D = defaultdict(list)
for i,item in enumerate(mylist):
D[item].append(i)
return {k:v for k,v in D.items() if len(v)>1}
def get_user_annotations(conn,extensions=('txt','csv','xls','xlsx')):
params = omero.sys.ParametersI()
params.exp(conn.getUser().getId()) # only show current user's Datasets
datasets = conn.getObjects("Dataset", params=params)
annotations = []
annotation_names = []
for dataset in datasets:
for dsAnn in dataset.listAnnotations():
if isinstance(dsAnn, omero.gateway.FileAnnotationWrapper):
annotations.append(dsAnn)
annotation_names.append(dsAnn.getFile().getName())
for image in dataset.listChildren():
for imAnn in image.listAnnotations():
if isinstance(imAnn, omero.gateway.FileAnnotationWrapper):
annotations.append(imAnn)
annotation_names.append(imAnn.getFile().getName())
filtered_anns = []
filtered_names = []
for ext in extensions:
filtered_anns.extend([ann[0] for ann in zip(annotations,annotation_names) if ext in ann[1]])
filtered_names.extend(["ID:"+str(ann[0].getId())+" "+ann[1] for ann in zip(annotations,annotation_names) if ext in ann[1]])
duplicates = find_duplicate_annotations(filtered_names)
for k,v in duplicates.iteritems():
dups = v[1:]
for d in dups:
filtered_anns.pop(d)
filtered_names.pop(d)
return filtered_anns,filtered_names
@login_required()
def index(request, conn=None, **kwargs):
userFullName = conn.getUser().getFullName()
anns,names = get_user_annotations(conn)
form_names = [(" "," ")]
for name in names:
form_names.append((name,name))
if request.POST:
form = AnnotationsForm(options=form_names,data=request.POST)
if form.is_valid():
selected = form.cleaned_data['annotation']
header_row = 0
if form.cleaned_data['header'] is not None:
header_row = form.cleaned_data['header']
sheet = 0
if form.cleaned_data['sheet'] is not None:
sheet = form.cleaned_data['sheet']
annId = selected.partition(' ')[0][3:]
request.session['annotation_id'] = annId
request.session['header'] = header_row
request.session['sheet'] = sheet
annotation = conn.getObject("Annotation",annId)
fpath = download_annotation(annotation)
fname, fextension = os.path.splitext(fpath)
cols,message,csv_path = parse_annotation(fpath,fextension,header_row,sheet)
if cols is not None:
rv = {'success':True,'message':message,'selected': selected,'columns': cols}
data = json.dumps(rv)
return HttpResponse(data, mimetype='application/json')
else:
rv = {'success':False,'message':message}
error = json.dumps(rv)
return HttpResponseBadRequest(error, mimetype='application/json')
else:
preview_form = PreviewForm(options=form_names,\
initial={'preview_sheet':0})
ann_form = AnnotationsForm(options=form_names,\
initial={'header':0,\
'sheet':0})
num_xls = len([name for name in names if '.xls' in name])
num_txt = len([name for name in names if '.txt' in name])
num_csv = len([name for name in names if '.csv' in name])
plot_form = PlotForm(options=(('x_data','x_data'),('y_data','y_data')))
context = {'userFullName': userFullName,
'annotations': anns,'num_annotations': len(anns),
'annotation_names': names, 'num_xls': num_xls,
'num_csv': num_csv, 'num_txt': num_txt,
'form': ann_form, 'plot_form': plot_form,\
'prev_form':preview_form}
return render(request, "plot/index.html", context)
@login_required()
def plot(request, conn=None, **kwargs):
annotation_id = request.session['annotation_id']
annotation = conn.getObject("Annotation",annotation_id)
fpath = download_annotation(annotation)
header_row = request.session['header']
sheet = request.session['sheet']
fname, fextension = os.path.splitext(fpath)
cols,message,csv_path = parse_annotation(fpath,fextension,header_row,sheet)
if request.POST:
form = PlotForm(options=cols,data=request.POST.copy())
if form.is_valid():
title = annotation.getFile().getName()
if form.cleaned_data['title']:
title = form.cleaned_data['title']
x = form.cleaned_data['x_data']
y = form.cleaned_data['y_data']
print 'first y',y
xLabel = x
if form.cleaned_data['x_Label']:
xLabel = form.cleaned_data['x_Label']
yLabel = y
if form.cleaned_data['y_Label']:
yLabel = form.cleaned_data['y_Label']
#tick_size = form.cleaned_data['tick_size']
plot_mode = form.cleaned_data['plot_mode']
xdata = [floor(xd) for xd in get_column(fpath,fextension,x,header_row,sheet)]
xmin = min(xdata)
xmax = max(xdata)
ydata = get_column(fpath,fextension,y,header_row,sheet)
#graph = flot_data(xdata,ydata)
#d3data = d3_data(xdata,ydata)
rv = {'message': message,\
'title': title, 'x' : x, 'y' : y,\
'xLabel': xLabel, 'yLabel': yLabel,\
'xdata': xdata, 'ydata': ydata,\
'num_series': len(ydata),'plot_mode':plot_mode,\
'xmin': xmin, 'xmax': xmax,'csv_path': csv_path}
data = json.dumps(rv)
return HttpResponse(data, mimetype='application/json')
@login_required()
def preview(request, conn=None, **kwargs):
anns,names = get_user_annotations(conn)
form_names = [(" "," ")]
for name in names:
form_names.append((name,name))
if request.POST:
form = PreviewForm(options=form_names,data=request.POST)
if form.is_valid():
selected = form.cleaned_data['preview_annotation']
sheet = 0
if form.cleaned_data['preview_sheet'] is not None:
sheet = form.cleaned_data['preview_sheet']
annId = selected.partition(' ')[0][3:]
annotation = conn.getObject("Annotation",annId)
fpath = download_annotation(annotation)
fname, fextension = os.path.splitext(fpath)
pdata = preview_data(fpath,fextension,sheet)
print len(pdata)
if pdata is not None:
rv = {'preview_data': pdata}
data = json.dumps(rv)
return HttpResponse(data, mimetype='application/json')
else:
rv = {'message':"Could not read file"}
error = json.dumps(rv)
return HttpResponseBadRequest(error, mimetype='application/json')
@login_required()
def save(request, conn=None, **kwargs):
if request.POST:
data = request.POST['plot_data']
layout = request.POST['plot_layout']
output_dir,path = plotly_graph(json.loads(data),json.loads(layout))
# and upload to omero
img = upload_plot(conn,path)
if img:
shutil.rmtree(output_dir)
rv = {'message':"Sucessfully saved"}
data = json.dumps(rv)
return HttpResponse(data, mimetype='application/json')
else:
rv = {'message':"Exporting failed"}
error = json.dumps(rv)
return HttpResponseBadRequest(error, mimetype='application/json')
@login_required()
def update(request, conn=None, **kwargs):
if request.POST:
annotation_id = request.session['annotation_id']
annotation = conn.getObject("Annotation",annotation_id)
fpath = download_annotation(annotation)
header_row = request.session['header']
sheet = request.session['sheet']
fname, fextension = os.path.splitext(fpath)
x = request.POST['x_column']
y = request.POST.getlist('y_column')
if type(y) != list:
y = [y]
xdata = [floor(xd) for xd in get_column(fpath,fextension,x,header_row,sheet)]
ydata = get_column(fpath,fextension,y,header_row,sheet)
rv = {'xdata': xdata, 'ydata': ydata,\
'num_series': len(ydata)}
data = json.dumps(rv)
return HttpResponse(data, mimetype='application/json')