forked from camicroscope/SlideLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageReader.py
More file actions
112 lines (93 loc) · 2.93 KB
/
ImageReader.py
File metadata and controls
112 lines (93 loc) · 2.93 KB
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
# Please note: if you would like to import a specific reader,
# you should "import ImageReader" first, even if you won't use it,
# to avoid a cyclic dependency error
from abc import ABCMeta, abstractmethod
# Allow near drop-in replacements for OpenSlide-Python
class ImageReader(metaclass=ABCMeta):
# currently: "openslide", "bioformats"
@staticmethod
@abstractmethod
def reader_name():
pass
@staticmethod
@abstractmethod
def extensions_set():
pass
# Raises exception on error
@abstractmethod
def __init__(self):
pass
@property
@abstractmethod
def level_count(self):
pass
@property
@abstractmethod
def dimensions(self):
pass
@property
@abstractmethod
def level_dimensions(self):
pass
@abstractmethod
def associated_images(self):
pass
@abstractmethod
def read_region(self, location, level, size):
pass
@abstractmethod
def get_thumbnail(self, max_size):
pass
# raises exception
@abstractmethod
def get_basic_metadata(self, extended):
pass
from OpenSlideReader import OpenSlideReader
from BioFormatsReader import BioFormatsReader
# Decreasing order of importance
#readers = [OpenSlideReader, BioFormatsReader]
readers = [BioFormatsReader]
# Replaces the constructor of the abstract class
# Usage:
# image = ImageReader.construct_reader("/file/path")
# Returns a reader
# Otherwise raises an object with attribute "error"
def construct_reader(imagepath):
import threading
for thread in threading.enumerate():
print(thread.name)
print("these are the threads", flush=True)
print(threading.get_native_id())
print(threading.get_ident())
print("my thread^", flush=True)
relevant_readers = []
extension = imagepath.split(".")[-1].lower()
print("starting readers loop,", flush=True)
for r in readers:
print("inloop1,", flush=True)
print(r.reader_name, flush=True)
print("inloop12", flush=True)
print(r.extensions_set, flush=True)
print(r.extensions_set(), flush=True)
print(extension, flush=True)
if extension in r.extensions_set():
print("inloop2,", flush=True)
relevant_readers.append(r)
print("ending readers loop,", flush=True)
if len(relevant_readers) == 0:
raise RuntimeError({"error": "File extension unsupported, no readers are compatible"})
image_type = []
reader = None
errors = []
print("starting relevant_readers loop,", flush=True)
for r in relevant_readers:
try:
reader = r(imagepath)
break
except Exception as e:
image_type.append(r.reader_name())
errors.append(r.reader_name() + ": " + str(e))
continue
if reader is None:
raise RuntimeError({"type": ",".join(image_type), "error": ",".join(errors)})
return reader