-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathviews.py
712 lines (610 loc) · 32.1 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
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
# Copyright (c) 2019, CRS4
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import logging
import math
import os
from distutils.util import strtobool
from . import settings
from .dzi_adapter import DZIAdapterFactory
from .dzi_adapter.errors import InvalidAttribute, InvalidColorPalette
from .dzi_adapter.shapes import DBScanClusterizer
from .dzi_adapter.shapes import get_dataset as get_ds
from .dzi_adapter.shapes import get_shape_converter, shapes_to_json
from .ome_data import (datasets_files, mirax_files, original_files,
projects_datasets, tags_data)
from .ome_data.mirax_files import InvalidMiraxFile, InvalidMiraxFolder
from .ome_data.original_files import (DuplicatedEntryError, get_original_file,
get_original_file_by_id)
from .slides_manager import RenderingEngineFactory
try:
import simplejson as json
except ImportError:
import json
from django.http import (HttpResponse, HttpResponseBadRequest,
HttpResponseNotFound, HttpResponseServerError)
from django.shortcuts import render
from omeroweb.webclient.decorators import login_required
logger = logging.getLogger(__name__)
def check_app(request):
return HttpResponse("ome_seadragon working!")
def check_repository(request):
if settings.IMGS_REPOSITORY:
return HttpResponse(settings.IMGS_REPOSITORY)
else:
return HttpResponse("No repository has been configured")
@login_required()
def start_connection(request, conn=None, **kwargs):
public_user_allowed = bool(strtobool(request.GET.get('allow_public_user', default='true')))
if not public_user_allowed:
if settings.OME_PUBLIC_USER is None:
return HttpResponseServerError('"omero.web.ome_seadragon.ome_public_user" property was not configured on the server')
if conn.getUser().getName() == settings.OME_PUBLIC_USER:
return HttpResponse('Unauthorized', status=401)
return HttpResponse(status=204)
@login_required()
def check_image_path(request, image_id, conn=None, **kwargs):
rendering_engine = RenderingEngineFactory().get_tiles_rendering_engine(image_id, conn)
return HttpResponse(rendering_engine._get_image_path())
def get_example_viewer(request, image_id):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
mirax = bool(strtobool(request.GET.get('mirax_image', default='false')))
return render(request, 'ome_seadragon/test/test_viewer.html',
{'image_id': image_id, 'host_name': base_url, 'mirax': mirax})
def get_example_viewer_json(request, image_id):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
mirax = bool(strtobool(request.GET.get('mirax_image', default='false')))
return render(request, 'ome_seadragon/test/test_viewer_json.html',
{'image_id': image_id, 'host_name': base_url, 'mirax': mirax})
def get_example_sequence_viewer(request, dataset_id):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
return render(request, 'ome_seadragon/test/test_sequence_viewer.html',
{'dataset_id': dataset_id, 'host_name': base_url})
def get_example_double_viewer(request, image_a_id, image_b_id):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
# no MIRAX images support right now
return render(request, 'ome_seadragon/test/test_double_viewer.html',
{'image_a_id': image_a_id, 'image_b_id': image_b_id,
'host_name': base_url})
def get_example_annotations(request, image_id):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
mirax = bool(strtobool(request.GET.get('mirax_image', default='false')))
return render(request, 'ome_seadragon/test/test_annotations.html',
{'image_id': image_id, 'host_name': base_url, 'mirax': mirax})
def get_example_ome_rois(request, image_id):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
return render(request, 'ome_seadragon/test/test_ome_roi.html',
{'image_id': image_id, 'host_name': base_url})
def get_example_interactive_markers(request, image_id):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
mirax = bool(strtobool(request.GET.get('mirax_image', default='false')))
return render(request, 'ome_seadragon/test/test_markers.html',
{'image_id': image_id, 'host_name': base_url, 'mirax': mirax})
def get_example_interactive_polygons(request, image_id):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
mirax = bool(strtobool(request.GET.get('mirax_image', default='false')))
return render(request, 'ome_seadragon/test/test_polygons.html',
{'image_id': image_id, 'host_name': base_url, 'mirax': mirax})
def get_example_interactive_rulers(request, image_id):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
mirax = bool(strtobool(request.GET.get('mirax_image', default='false')))
return render(request, 'ome_seadragon/test/test_rulers.html',
{'image_id': image_id, 'host_name': base_url, 'mirax': mirax})
def get_example_interactive_freehand(request, image_id):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
mirax = bool(strtobool(request.GET.get('mirax_image', default='false')))
return render(request, 'ome_seadragon/test/test_freehand_drawing.html',
{'image_id': image_id, 'host_name': base_url, 'mirax': mirax})
def get_example_array_viewer(request, dataset_label):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
return render(request, 'ome_seadragon/test/test_array_viewer.html',
{'dataset_label': dataset_label, 'host_name': base_url})
def get_example_overlay_viewer(request, image_id, dataset_label):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
mirax = bool(strtobool(request.GET.get('mirax_image', default='false')))
return render(request, 'ome_seadragon/test/test_overlay_viewer.html',
{'image_id': image_id, 'dataset_label': dataset_label, 'host_name': base_url ,
'mirax': mirax})
def get_example_dataset_shapes_viewer(request, image_id, dataset_id):
base_url = '%s://%s' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'])
mirax = bool(strtobool(request.GET.get('mirax_image', default='false')))
return render(request, 'ome_seadragon/test/test_dataset_shapes.html',
{'image_id': image_id, 'dataset_id': dataset_id, 'host_name': base_url,
'mirax': mirax})
@login_required()
def get_projects(request, conn=None, **kwargs):
try:
fetch_datasets = bool(strtobool(request.GET.get('datasets')))
except (ValueError, AttributeError):
fetch_datasets = False
projects = projects_datasets.get_projects(conn, fetch_datasets)
return HttpResponse(json.dumps(projects), content_type='application/json')
@login_required()
def get_project(request, project_id, conn=None, **kwargs):
try:
fetch_datasets = bool(strtobool(request.GET.get('datasets')))
except (ValueError, AttributeError):
fetch_datasets = False
try:
fetch_images = bool(strtobool(request.GET.get('images')))
except (ValueError, AttributeError):
fetch_images = False
try:
expand_series = bool(strtobool(request.GET.get('full_series')))
except (ValueError, AttributeError):
expand_series = False
project = projects_datasets.get_project(conn, project_id, fetch_datasets, fetch_images,
expand_series)
return HttpResponse(json.dumps(project), content_type='application/json')
@login_required()
def get_dataset(request, dataset_id, conn=None, **kwargs):
try:
fetch_images = bool(strtobool(request.GET.get('images')))
except (ValueError, AttributeError):
fetch_images = False
try:
expand_series = bool(strtobool(request.GET.get('full_series')))
except (ValueError, AttributeError):
expand_series = False
dataset = projects_datasets.get_dataset(conn, dataset_id, fetch_images,
expand_series)
return HttpResponse(json.dumps(dataset), content_type='application/json')
@login_required()
def get_image(request, image_id, conn=None, **kwargs):
try:
fetch_rois = bool(strtobool(request.GET.get('rois')))
except (ValueError, AttributeError):
fetch_rois = False
image = projects_datasets.get_image(conn, image_id, fetch_rois)
return HttpResponse(json.dumps(image), content_type='application/json')
@login_required()
def get_images_quick_list(request, conn=None, **kwargs):
try:
expand_series = bool(strtobool(request.GET.get('full_series')))
except (ValueError, AttributeError):
expand_series = False
images_list = projects_datasets.get_images_quick_list(conn, expand_series)
return HttpResponse(json.dumps(images_list), content_type='application/json')
@login_required()
def get_annotations(request, conn=None, **kwargs):
try:
fetch_images = bool(strtobool(request.GET.get('fetch_imgs')))
except (ValueError, AttributeError):
fetch_images = False
annotations = tags_data.get_annotations_list(conn, fetch_images)
return HttpResponse(json.dumps(annotations), content_type='application/json')
@login_required()
def get_tagset(request, tagset_id, conn=None, **kwargs):
try:
fetch_tags = bool(strtobool(request.GET.get('tags')))
except (ValueError, AttributeError):
fetch_tags = False
try:
fetch_images = bool(strtobool(request.GET.get('images')))
except (ValueError, AttributeError):
fetch_images = False
tagset = tags_data.get_tagset(conn, tagset_id, fetch_tags, fetch_images)
return HttpResponse(json.dumps(tagset), content_type='application/json')
@login_required()
def get_tag(request, tag_id, conn=None, **kwargs):
try:
fetch_images = bool(strtobool(request.GET.get('images')))
except (ValueError, AttributeError):
fetch_images = False
tag = tags_data.get_tag(conn, tag_id, fetch_images)
return HttpResponse(json.dumps(tag), content_type='application/json')
@login_required()
def find_annotations(request, conn=None, **kwargs):
search_pattern = request.GET.get('query')
try:
fetch_images = bool(strtobool(request.GET.get('fetch_imgs')))
except (ValueError, AttributeError):
fetch_images = False
logger.debug('"fetch_imgs" value %r', fetch_images)
annotations = tags_data.find_annotations(search_pattern, conn, fetch_images)
return HttpResponse(json.dumps(annotations), content_type='application/json')
@login_required()
def get_image_dzi(request, image_id, fetch_original_file=False, file_mimetype=None, conn=None, **kwargs):
try:
tile_size = int(request.GET.get('tile_size'))
except TypeError:
tile_size = None
try:
limit_bounds = bool(strtobool(request.GET.get('limit_bounds')))
except AttributeError:
limit_bounds = None
rf = RenderingEngineFactory()
rendering_engine = rf.get_primary_tiles_rendering_engine(image_id, conn)
try:
dzi_metadata = rendering_engine.get_dzi_description(fetch_original_file, file_mimetype,
tile_size, limit_bounds)
except Exception as e:
rendering_engine = rf.get_secondary_tiles_rendering_engine(image_id, conn)
if rendering_engine:
dzi_metadata = rendering_engine.get_dzi_description(fetch_original_file, file_mimetype,
tile_size, limit_bounds)
else:
raise e
if dzi_metadata:
return HttpResponse(dzi_metadata, content_type='application/xml')
else:
return HttpResponseNotFound('No image with ID %s' % image_id)
@login_required()
def get_image_json(request, image_id, fetch_original_file=False, file_mimetype=None, conn=None, **kwargs):
try:
tile_size = int(request.GET.get('tile_size'))
except TypeError:
tile_size = None
rf = RenderingEngineFactory()
rendering_engine = rf.get_primary_tiles_rendering_engine(image_id, conn)
resource_path = request.build_absolute_uri('%s_files/' % image_id)
try:
json_metadata = rendering_engine.get_json_description(resource_path, fetch_original_file,
file_mimetype, tile_size)
except Exception as e:
rendering_engine = rf.get_secondary_tiles_rendering_engine(image_id, conn)
if rendering_engine:
json_metadata = rendering_engine.get_json_description(resource_path, fetch_original_file,
file_mimetype, tile_size)
else:
raise e
if json_metadata:
return HttpResponse(json.dumps(json_metadata), content_type='application/json')
else:
return HttpResponseNotFound('No image with ID %s' % image_id)
@login_required()
def get_image_metadata(request, image_id, fetch_original_file=False, file_mimetype=None, conn=None, **kwargs):
try:
tile_size = int(request.GET.get('tile_size'))
except TypeError:
tile_size = None
rf = RenderingEngineFactory()
rendering_engine = rf.get_primary_tiles_rendering_engine(image_id, conn)
resource_path = request.build_absolute_uri('%s_files/' % image_id)
try:
img_metadata = rendering_engine.get_image_description(resource_path, fetch_original_file,
file_mimetype, tile_size)
except Exception as e:
rendering_engine = rf.get_secondary_tiles_rendering_engine(image_id, conn)
if rendering_engine:
img_metadata = rendering_engine.get_image_description(resource_path, fetch_original_file,
file_mimetype, tile_size)
else:
raise e
if img_metadata:
return HttpResponse(json.dumps(img_metadata), content_type='application/json')
else:
return HttpResponseNotFound('No image with ID %s' % image_id)
@login_required()
def get_image_thumbnail(request, image_id, fetch_original_file=False,
file_mimetype=None, conn=None, **kwargs):
rf = RenderingEngineFactory()
rendering_engine = rf.get_primary_thumbnails_rendering_engine(image_id, conn)
try:
thumbnail, image_format = rendering_engine.get_thumbnail(int(request.GET.get('size')),
fetch_original_file, file_mimetype)
except Exception as e:
rendering_engine = rf.get_secondary_thumbnails_rendering_engine(image_id, conn)
if rendering_engine:
thumbnail, image_format = rendering_engine.get_thumbnail(int(request.GET.get('size')),
fetch_original_file, file_mimetype)
else:
raise e
if thumbnail:
response = HttpResponse(content_type="image/%s" % image_format)
thumbnail.save(response, image_format)
return response
else:
return HttpResponseServerError('Unable to load thumbnail')
@login_required()
def get_tile(request, image_id, level, column, row, tile_format,
fetch_original_file=False, file_mimetype=None, conn=None, **kwargs):
try:
tile_size = int(request.GET.get('tile_size'))
except TypeError:
tile_size = None
try:
limit_bounds = bool(strtobool(request.GET.get('limit_bounds')))
except AttributeError:
limit_bounds = None
if tile_format != settings.DEEPZOOM_FORMAT:
return HttpResponseServerError("Format %s not supported by the server" % tile_format)
rf = RenderingEngineFactory()
rendering_engine = rf.get_primary_tiles_rendering_engine(image_id, conn)
try:
tile, image_format = rendering_engine.get_tile(int(level), int(column), int(row), fetch_original_file,
file_mimetype, tile_size, limit_bounds)
except Exception as e:
logger.error(e)
rendering_engine = rf.get_secondary_tiles_rendering_engine(image_id, conn)
if rendering_engine:
tile, image_format = rendering_engine.get_tile(int(level), int(column), int(row), fetch_original_file,
file_mimetype, tile_size, limit_bounds)
else:
raise e
if tile:
response = HttpResponse(content_type='image/%s' % image_format)
tile.save(response, image_format)
return response
else:
return HttpResponseNotFound('No tile can be found')
@login_required()
def get_image_mpp(request, image_id, fetch_original_file=False, file_mimetype=None, conn=None, **kwargs):
rf = RenderingEngineFactory()
rendering_engine = rf.get_primary_tiles_rendering_engine(image_id, conn)
try:
image_mpp = rendering_engine.get_openseadragon_config(fetch_original_file, file_mimetype)['mpp']
except Exception as e:
rendering_engine = rf.get_secondary_tiles_rendering_engine(image_id, conn)
if rendering_engine:
image_mpp = rendering_engine.get_openseadragon_config(fetch_original_file, file_mimetype)['mpp']
else:
raise e
return HttpResponse(json.dumps({'image_mpp': image_mpp}), content_type='application/json')
@login_required()
def get_slide_bounds(request, image_id, fetch_original_file=False, file_mimetype=None, conn=None, **kwargs):
rf = RenderingEngineFactory()
rendering_engine = rf.get_primary_tiles_rendering_engine(image_id, conn)
try:
slide_bounds = rendering_engine.get_slide_bounds(fetch_original_file, file_mimetype)
except Exception as e:
rendering_engine = rf.get_secondary_tiles_rendering_engine(image_id, conn)
if rendering_engine:
slide_bounds = rendering_engine.get_slide_bounds(fetch_original_file, file_mimetype)
else:
raise e
if slide_bounds:
return HttpResponse(json.dumps(slide_bounds), content_type='application/json')
else:
return HttpResponseNotFound('No image with ID %s' % image_id)
@login_required()
def register_original_file(request, conn=None, **kwargs):
error_on_duplicated = bool(strtobool(request.GET.get('error_on_duplicated', default='false')))
try:
fname = request.GET.get('name')
if not original_files.is_valid_filename(fname):
return HttpResponseBadRequest('Invalid file name received: %s' % fname)
fpath = request.GET.get('path')
fmtype = request.GET.get('mimetype')
if not all([fname, fpath, fmtype]):
return HttpResponseBadRequest('Mandatory field missing')
file_id, file_created = original_files.save_original_file(conn, fname, fpath, fmtype,
int(request.GET.get('size', default=-1)),
request.GET.get('sha1', default='UNKNOWN'),
error_on_duplicated)
return HttpResponse(json.dumps({'omero_id': file_id, 'file_created': file_created}),
content_type='application/json')
except DuplicatedEntryError as dee:
return HttpResponseServerError('%s' % dee)
@login_required()
def register_mirax_slide(request, conn=None, **kwargs):
sname = request.GET.get('slide_name')
error_on_duplicated = bool(strtobool(request.GET.get('error_on_duplicated', default='false')))
if not original_files.is_valid_filename(sname):
return HttpResponseServerError('Invalid slide name received: %s' % sname)
try:
mirax_paths = mirax_files.get_mirax_files_paths(sname)
try:
mirax_file_id, mirax_file_created = original_files.save_original_file(conn, sname, mirax_paths[0],
'mirax/index', -1, 'UNKNOWN',
error_on_duplicated)
try:
mirax_folder_id, mirax_folder_created = original_files.save_original_file(conn, sname, mirax_paths[1],
'mirax/datafolder',
-1, 'UNKNOWN',
error_on_duplicated)
return HttpResponse(
json.dumps({
'mirax_index_omero_id': mirax_file_id,
'mirax_index_created': mirax_file_created,
'mirax_folder_omero_id': mirax_folder_id,
'mirax_folder_created': mirax_folder_created
}),
content_type='application/json'
)
except DuplicatedEntryError as dee:
original_files.delete_original_files(conn, sname, 'mirax/index')
return HttpResponseServerError('{0}'.format(dee))
except DuplicatedEntryError as dee:
return HttpResponseServerError('{0}'.format(dee))
except InvalidMiraxFile as imf:
return HttpResponseServerError('{0}'.format(imf))
except InvalidMiraxFolder as imf:
return HttpResponseServerError('{0}'.format(imf))
except settings.ServerConfigError as sce:
return HttpResponseServerError('{0}'.format(sce))
@login_required()
def get_mirax_slide_details(request, slide_id, conn=None, **kwargs):
mirax_data_folder = get_original_file(conn, slide_id, 'mirax/datafolder')
mirax_index_file = get_original_file(conn, slide_id, 'mirax/index')
slide_details = {
'slide_label': slide_id,
'index_file': {
'omero_id': mirax_index_file.getId(),
'label': mirax_index_file.getName(),
'mimetype': mirax_index_file.getMimetype(),
'hash': mirax_index_file.getHash()
},
'data_folder': {
'omero_id': mirax_data_folder.getId(),
'label': mirax_data_folder.getName(),
'mimetype': mirax_data_folder.getMimetype(),
'hash': mirax_data_folder.getHash()
}
}
return HttpResponse(json.dumps(slide_details), content_type='application/json')
@login_required()
def list_array_datasets(request, conn=None, **kwargs):
datasets = datasets_files.get_datasets(conn)
return HttpResponse(json.dumps(datasets), content_type='application/json')
@login_required()
def register_array_dataset(request, conn=None, **kwargs):
dataset_label = request.GET.get('dataset_label')
error_on_duplicated = bool(strtobool(request.GET.get('error_on_duplicated', default='false')))
if not original_files.is_valid_filename(dataset_label):
return HttpResponseServerError('Invalid dataset name received: {0}'.format(dataset_label))
try:
dataset_path, is_dir = datasets_files.get_dataset_file_path(dataset_label)
if not is_dir:
if bool(strtobool(request.GET.get('extract_archive', default='true'))) == False:
dataset_label, dataset_path = datasets_files.rename_archive(dataset_path)
else:
try:
keep_archive = bool(strtobool(request.GET.get('keep_archive', default='false')))
dataset_label, dataset_path = datasets_files.extract_archive(dataset_path,
keep_archive=keep_archive)
is_dir = True
except datasets_files.DatasetPathAlreadyExistError as dpe:
return HttpResponseServerError('{0}'.format(dpe))
try:
mtype = datasets_files.check_dataset(dataset_path, is_dir)
dataset_id, dataset_created = original_files.save_original_file(conn, dataset_label, dataset_path, mtype,
int(request.GET.get('size', default=-1)),
request.GET.get('sha1', default='UNKNOWN'),
error_on_duplicated)
return HttpResponse(
json.dumps({
'omero_id': dataset_id,
'created': dataset_created,
'mimetype': mtype,
'label': dataset_label,
'path': dataset_path
}),
content_type='application/json'
)
except datasets_files.DatasetFormatError as dfe:
return HttpResponseServerError('{0}'.format(dfe))
except DuplicatedEntryError as dee:
return HttpResponseServerError('{0}'.format(dee))
except datasets_files.InvalidDatasetPath as idp:
return HttpResponseServerError('{0}'.format(idp))
except settings.ServerConfigError as sce:
return HttpResponseServerError('{0}'.format(sce))
@login_required()
def get_original_file_infos(request, file_name, conn=None, **kwargs):
fmtype = request.GET.get('mimetype')
if fmtype is None:
return HttpResponseServerError('Missing mandatory mimetype value to complete the request')
details = original_files.get_original_file_infos(conn, file_name, fmtype)
return HttpResponse(json.dumps(details), content_type='application/json')
@login_required()
def delete_original_file(request, file_name, conn=None, **kwargs):
fmtype = request.GET.get('mimetype')
if fmtype is None:
return HttpResponseServerError('Missing mandatory mimetype value to complete the request')
status, count = original_files.delete_original_files(conn, file_name, fmtype)
return HttpResponse(json.dumps({'success': status, 'deleted_count': count}),
content_type='application/json')
@login_required()
def delete_original_files(request, file_name, conn=None, **kwargs):
status, count = original_files.delete_original_files(conn, file_name)
return HttpResponse(json.dumps({'success': status, 'deleted_count': count}),
content_type='application/json')
def _get_dataset_dzi_description(original_file):
if original_file and original_file.mimetype == 'dataset-folder/tiledb':
dzi_adapter = DZIAdapterFactory('TILEDB').get_adapter(original_file.name)
return dzi_adapter.get_dzi_description()
else:
return None
@login_required()
def get_array_dataset_dzi_by_label(request, dataset_label, conn=None, **kwargs):
try:
original_file = get_original_file(conn, dataset_label)
except DuplicatedEntryError as de_err:
return HttpResponseServerError(str(de_err))
dzi_metadata = _get_dataset_dzi_description(original_file)
if dzi_metadata is not None:
return HttpResponse(dzi_metadata, content_type='application/xml')
else:
return HttpResponseNotFound(f'There is not a valid array dataset with label {dataset_label}')
@login_required()
def get_array_dataset_dzi_by_id(request, dataset_id, conn=None, **kwargs):
original_file = get_original_file_by_id(conn, dataset_id)
dzi_metadata = _get_dataset_dzi_description(original_file)
if dzi_metadata is not None:
return HttpResponse(dzi_metadata, content_type='application/xml')
else:
return HttpResponseNotFound(f'There is not a valid array dataset with ID {dataset_id}')
def _get_tile_from_dataset(original_file, level, row, column, color_palette, threshold):
if original_file and original_file.mimetype == 'dataset-folder/tiledb':
dzi_adapter = DZIAdapterFactory('TILEDB').get_adapter(original_file.name)
return dzi_adapter.get_tile(level, int(row), int(column), color_palette, threshold)
else:
return None
@login_required()
def get_array_dataset_tile_by_label(request, dataset_label, level, row, column, conn=None, **kwargs):
color_palette = request.GET.get('palette')
threshold = request.GET.get('threshold')
if color_palette is None:
return HttpResponseBadRequest('Missing mandatory palette value to complete the request')
try:
original_file = get_original_file(conn, dataset_label)
tile = _get_tile_from_dataset(original_file, level, row, column, color_palette, threshold)
if tile:
response = HttpResponse(content_type='image/png')
tile.save(response, 'png')
return response
else:
return HttpResponseNotFound(f'There is not a valid array dataset with label {dataset_label}')
except DuplicatedEntryError as de_err:
return HttpResponseServerError(str(de_err))
except InvalidColorPalette as cp_error:
return HttpResponseBadRequest(cp_error)
except InvalidAttribute as a_error:
return HttpResponseBadRequest(a_error)
@login_required()
def get_array_dataset_tile_by_id(request, dataset_id, level, row, column, conn=None, **kwargs):
color_palette = request.GET.get('palette')
threshold = request.GET.get('threshold')
if color_palette is None:
return HttpResponseBadRequest('Missing mandatory palette value to complete the request')
try:
original_file = get_original_file_by_id(conn, dataset_id)
tile = _get_tile_from_dataset(original_file, level, row, column, color_palette, threshold)
if tile:
response = HttpResponse(content_type='image/png')
tile.save(response, 'png')
return response
else:
return HttpResponseNotFound(f'There is not a valid array dataset with ID {dataset_id}')
except InvalidColorPalette as cp_error:
return HttpResponseBadRequest(cp_error)
except InvalidAttribute as a_error:
return HttpResponseBadRequest(a_error)
@login_required()
def get_array_dataset_shapes(request, dataset_id, conn=None, **kwargs):
threshold = float(request.GET.get("threshold", 0.6))
cluster_min_distance = float(request.GET.get("cluster_min_distance", 0))
cluster_min_area = float(request.GET.get("cluster_min_area", 1))
shape_mode = request.GET.get("shape_mode", "contour")
original_file = get_original_file_by_id(conn, dataset_id)
logger.info("retrieving shapes for dataset %s", original_file.name)
dataset = get_ds(os.path.join(settings.DATASETS_REPOSITORY, original_file.name))
shape_converter = get_shape_converter(shape_mode)
shapes = shape_converter.convert(dataset, threshold * 100)
if cluster_min_distance:
diagonal = math.sqrt(2) * dataset.zoom_factor()
clusterizer = DBScanClusterizer(cluster_min_distance * diagonal)
shapes = clusterizer.cluster(shapes)
if cluster_min_area > 1:
min_area = cluster_min_area * ( dataset.zoom_factor() ** 2)
shapes = list(filter(lambda s: s.area >= min_area, shapes))
return HttpResponse(
shapes_to_json(shapes),
content_type="application/json",
)