-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from camicroscope/develop
For 3.11
- Loading branch information
Showing
19 changed files
with
981 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import image_reader | ||
import dev_utils | ||
import ome_types | ||
from file_extensions import BIOFORMATS_EXTENSIONS | ||
import BFBridge.python as bfbridge | ||
|
||
|
||
jvm = bfbridge.BFBridgeVM() | ||
|
||
class BioFormatsReader(image_reader.ImageReader): | ||
@staticmethod | ||
def reader_name(): | ||
return "bioformats" | ||
|
||
@staticmethod | ||
def extensions_set(): | ||
return BIOFORMATS_EXTENSIONS | ||
|
||
def __init__(self, imagepath): | ||
if not hasattr(dev_utils.keep_alive_for_thread, "bfthread"): | ||
dev_utils.keep_alive_for_thread.bfthread = bfbridge.BFBridgeThread(jvm) | ||
|
||
# Conventionally internal attributes start with underscore. | ||
# When using them without underscore, there's the risk that | ||
# a property has the same name as a/the getter, which breaks | ||
# the abstract class. Hence all internal attributes start with underscore. | ||
self._bfreader = bfbridge.BFBridgeInstance(dev_utils.keep_alive_for_thread.bfthread) | ||
if self._bfreader is None: | ||
raise RuntimeError("cannot make bioformats instance") | ||
self._image_path = imagepath | ||
code = self._bfreader.open(imagepath) | ||
if code < 0: | ||
raise IOError("Could not open file " + imagepath + ": " + self._bfreader.get_error_string()) | ||
# Note: actually stores the format, not the vendor ("Hamamatsu NDPI" instead of "Hamamatsu") | ||
self._vendor = self._bfreader.get_format() | ||
self._level_count = self._bfreader.get_resolution_count() | ||
self._dimensions = (self._bfreader.get_size_x(), self._bfreader.get_size_y()) | ||
self._level_dimensions = [self._dimensions] | ||
for l in range(1, self._level_count): | ||
self._bfreader.set_current_resolution(l) | ||
self._level_dimensions.append( \ | ||
(self._bfreader.get_size_x(), self._bfreader.get_size_y())) | ||
|
||
@property | ||
def level_count(self): | ||
return self._level_count | ||
|
||
@property | ||
def dimensions(self): | ||
return self._dimensions | ||
|
||
@property | ||
def level_dimensions(self): | ||
return self._level_dimensions | ||
|
||
@property | ||
def associated_images(self): | ||
return None | ||
|
||
def read_region(self, location, level, size): | ||
self._bfreader.set_current_resolution(level) | ||
return self._bfreader.open_bytes_pil_image(0, \ | ||
location[0], location[1], size[0], size[1]) | ||
|
||
def get_thumbnail(self, max_size): | ||
return self._bfreader.open_thumb_bytes_pil_image(0, max_size[0], max_size[1]) | ||
|
||
def get_basic_metadata(self, extended): | ||
metadata = {} | ||
|
||
try: | ||
ome_xml_raw = self._bfreader.dump_ome_xml_metadata() | ||
except BaseException as e: | ||
raise OverflowError("XML metadata too large for file considering the preallocated buffer length. " + str(e)) | ||
try: | ||
ome_xml = ome_types.from_xml(ome_xml_raw) | ||
except BaseException as e: | ||
raise RuntimeError("get_basic_metadata: OME-XML parsing of metadata failed, error: " + \ | ||
str(e) + " when parsing: " + ome_xml_raw) | ||
|
||
# https://www.openmicroscopy.org/Schemas/Documentation/Generated/OME-2016-06/ome_xsd.html | ||
# https://bio-formats.readthedocs.io/en/latest/metadata-summary.html | ||
|
||
if extended: | ||
return {"ome-xml": ome_xml_raw} | ||
|
||
metadata['width'] = str(self._dimensions[0]) | ||
metadata['height'] = str(self._dimensions[1]) | ||
try: | ||
metadata['mpp-x'] = str(ome_xml.images[0].pixels.physical_size_x) | ||
metadata['mpp-y'] = str(ome_xml.images[0].pixels.physical_size_y) | ||
except: | ||
metadata['mpp-x'] = "0" | ||
metadata['mpp-y'] = "0" | ||
metadata['vendor'] = self._vendor | ||
metadata['level_count'] = int(self._level_count) | ||
try: | ||
metadata['objective'] = ome_xml.instruments[0].objectives[0].nominal_magnification | ||
except: | ||
try: | ||
metadata['objective'] = ome_xml.instruments[0].objectives[0].calibrated_magnification | ||
except: | ||
metadata['objective'] = -1.0 | ||
|
||
metadata['comment'] = "" | ||
metadata['study'] = "" | ||
metadata['specimen'] = "" | ||
metadata['md5sum'] = dev_utils.file_md5(self._image_path) | ||
|
||
return metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import openslide | ||
import image_reader | ||
import dev_utils | ||
from file_extensions import OPENSLIDE_EXTENSIONS | ||
|
||
class OpenSlideReader(image_reader.ImageReader): | ||
@staticmethod | ||
def reader_name(): | ||
return "openslide" | ||
|
||
@staticmethod | ||
def extensions_set(): | ||
return OPENSLIDE_EXTENSIONS | ||
|
||
def __init__(self, imagepath): | ||
self._image_path = imagepath | ||
self._reader = openslide.OpenSlide(imagepath) | ||
|
||
@property | ||
def level_count(self): | ||
return self._reader.level_count | ||
|
||
@property | ||
def dimensions(self): | ||
return self._reader.dimensions | ||
|
||
@property | ||
def level_dimensions(self): | ||
return self._reader.level_dimensions | ||
|
||
@property | ||
def associated_images(self): | ||
return self._reader.associated_images | ||
|
||
def read_region(self, location, level, size): | ||
return self._reader.read_region(location, level, size) | ||
|
||
def get_thumbnail(self, max_size): | ||
return self._reader.get_thumbnail(max_size) | ||
|
||
def get_basic_metadata(self, extended): | ||
slideData = self._reader.properties | ||
if extended: | ||
metadata = {k:v for (k,v) in slideData.items()} | ||
else: | ||
metadata = {} | ||
metadata['width'] = slideData.get(openslide.PROPERTY_NAME_BOUNDS_WIDTH, None) \ | ||
or slideData.get( "openslide.level[0].width", None) | ||
metadata['height'] = slideData.get(openslide.PROPERTY_NAME_BOUNDS_HEIGHT, None) \ | ||
or slideData.get("openslide.level[0].height", None) | ||
metadata['mpp-x'] = slideData.get(openslide.PROPERTY_NAME_MPP_X, None) | ||
metadata['mpp-y'] = slideData.get(openslide.PROPERTY_NAME_MPP_Y, None) | ||
metadata['vendor'] = slideData.get(openslide.PROPERTY_NAME_VENDOR, None) | ||
metadata['level_count'] = int(self._reader.level_count) | ||
metadata['objective'] = float(slideData.get(openslide.PROPERTY_NAME_OBJECTIVE_POWER, 0) \ | ||
or slideData.get("aperio.AppMag", -1.0)) | ||
metadata['comment'] = slideData.get(openslide.PROPERTY_NAME_COMMENT, None) | ||
# caMicroscope expects some value for study and specimen for slides, add empty string as defauly. | ||
metadata['study'] = "" | ||
metadata['specimen'] = "" | ||
metadata['md5'] = dev_utils.file_md5(self._image_path) | ||
return metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.