def test_detect_format(self):
     self.assertTrue(
             OpenSlide.detect_format(file_path('__missing_file')) is None)
     self.assertTrue(
             OpenSlide.detect_format(file_path('../setup.py')) is None)
     self.assertEqual(
             OpenSlide.detect_format(file_path('boxes.tiff')),
             'generic-tiff')
Exemple #2
0
 def test_detect_format(self):
     self.assertTrue(
         OpenSlide.detect_format(file_path('__missing_file')) is None)
     self.assertTrue(
         OpenSlide.detect_format(file_path('../setup.py')) is None)
     self.assertEqual(OpenSlide.detect_format(file_path('boxes.tiff')),
                      'generic-tiff')
Exemple #3
0
def _get_slides(basedir, search='', relpath=''):
    ### Returns recalculatable=True if in this folder there is a .recalulatable file
    children = []
    for name in sorted(os.listdir(os.path.join(basedir, relpath))):
        cur_relpath = os.path.join(relpath, name)
        cur_path = os.path.join(basedir, cur_relpath)
        if os.path.isdir(cur_path):
            cur_dir = _get_slides(basedir, search, cur_relpath)
            if cur_dir:
                children.append({
                    "name":
                    cur_relpath,
                    "children":
                    cur_dir,
                    "is_folder":
                    True,
                    "recalculatable":
                    True if os.path.isfile(
                        os.path.join(cur_path, '.recalculate')) else False
                })
        elif OpenSlide.detect_format(cur_path):
            children.append({
                "name":
                os.path.join(relpath, os.path.basename(cur_path)),
                "is_folder":
                False
            })
    return sorted(filter(lambda x: search in x['name'], children),
                  key=lambda x: x['name'])
Exemple #4
0
def _fnfilter(filename):
    filename = filename.lower()
    if OpenSlide.detect_format(filename):
        return True
    elif imghdr.what(filename):
        return True
    elif ".tmap" in filename:
        return True
    return False
 def __init__(self, basedir, relpath=''):
     self.name = os.path.basename(relpath)
     self.children = []
     for name in sorted(os.listdir(os.path.join(basedir, relpath))):
         cur_relpath = os.path.join(relpath, name)
         cur_path = os.path.join(basedir, cur_relpath)
         if os.path.isdir(cur_path):
             cur_dir = _Directory(basedir, cur_relpath)
             if cur_dir.children:
                 self.children.append(cur_dir)
         elif OpenSlide.detect_format(cur_path):
             self.children.append(_SlideFile(cur_relpath))
Exemple #6
0
 def __init__(self, basedir, relpath=''):
     self.name = os.path.basename(relpath)
     self.children = []
     for name in sorted(os.listdir(os.path.join(basedir, relpath))):
         cur_relpath = os.path.join(relpath, name)
         cur_path = os.path.join(basedir, cur_relpath)
         if os.path.isdir(cur_path):
             cur_dir = _Directory(basedir, cur_relpath)
             if cur_dir.children:
                 self.children.append(cur_dir)
         elif OpenSlide.detect_format(cur_path):
             self.children.append(_SlideFile(cur_relpath))
 def __init__(self, basedir, results_file, relpath=''):
     filename = []
     if results_file != '':
         with open(results_file, 'rb') as handle:
             results = pickle.load(handle)
         filename = [file.split('/')[-1] for file in results['slides']]
     self.name = os.path.basename(relpath)
     self.children = []
     for name in sorted(os.listdir(os.path.join(basedir, relpath))):
         cur_relpath = os.path.join(relpath, name)
         cur_path = os.path.join(basedir, cur_relpath)
         if os.path.isdir(cur_path):
             cur_dir = _Directory(basedir, app.results, cur_relpath)
             if cur_dir.children:
                 self.children.append(cur_dir)
         elif results_file != '' and cur_relpath.split('/')[-1] not in filename:
             continue
         elif OpenSlide.detect_format(cur_path):
             self.children.append(_SlideFile(cur_relpath))
Exemple #8
0
 def __init__(self, basedir, relpath=''):
     self.name = os.path.basename(relpath)
     self.full_name = '.' if relpath == '' else './' + relpath
     self.children = []
     self.children_masks = []
     for name in sorted(os.listdir(os.path.join(basedir, relpath))):
         cur_relpath = os.path.join(relpath, name)
         cur_path = os.path.join(basedir, cur_relpath)
         if os.path.isdir(cur_path):
             cur_dir = _Directory(basedir, cur_relpath)
             if cur_dir.children:
                 self.children.append(cur_dir)
         elif OpenSlide.detect_format(cur_path):
             if not ('dgai-mask' in os.path.basename(cur_path)) and not (
                     'dgai-uncertainty' in os.path.basename(cur_path)):
                 #liver-slide-1-slide.tiff -> liver-slide-1-mask.tiff
                 if get_mask_path(cur_path):
                     self.children.append(_SlideFile(cur_relpath, True))
                 else:
                     self.children.append(_SlideFile(cur_relpath, False))
    def __init__(self, basedir, relpath='', max_depth=4):
        self.name = os.path.basename(relpath)
        self.children = []
        if max_depth != 0:
            try:
                for name in sorted(os.listdir(os.path.join(basedir, relpath))):
                    cur_relpath = os.path.join(relpath, name)
                    cur_path = os.path.join(basedir, cur_relpath)
                    if os.path.isdir(cur_path):
                        cur_dir = _Directory(basedir,
                                             cur_relpath,
                                             max_depth=max_depth - 1)
                        if cur_dir.children:
                            self.children.append(cur_dir)
                    elif OpenSlide.detect_format(cur_path):
                        self.children.append(_SlideFile(cur_relpath))
                    elif imghdr.what(cur_path):
                        self.children.append(_SlideFile(cur_relpath))

            except:
                pass
Exemple #10
0
def napari_get_reader(path):
    """A basic implementation of the napari_get_reader hook specification.

    Parameters
    ----------
    path : str or list of str
        Path to file, or list of paths.

    Returns
    -------
    function or None
        If the path is a recognized format, return a function that accepts the
        same path or list of paths, and returns a list of layer data tuples.
    """
    if isinstance(path, list):
        # Don't handle multiple paths
        return None

    if OpenSlide.detect_format(path) is None:
        return None

    try:
        slide = OpenSlide(path)
    except OpenSlideUnsupportedFormatError:
        return None

    description = slide.properties.get(PROPERTY_NAME_COMMENT)
    # Don't try to handle OME-TIFF
    # https://github.com/cgohlke/tifffile/blob/b346e3bd7de81de512a6715b01124c8f6d60a707/tifffile/tifffile.py#L5781
    if description and description[-4:] == "OME>":
        return None

    # Don't try to handle files that aren't multiscale.
    if slide.level_count == 1:
        return None

    slide.close()

    return reader_function
    def loadndpi(self):
        self.init_scene()
        formats = '*.ndpi*;;*.svs*;;*.tif*;;*.scn*;;*.mrxs*;;*.tiff*;;*.svslide*;;*.bif*'
        self.path, _ = QFileDialog.getOpenFileName(
            parent=self,
            caption='Open file',
            directory="/Users/callum/Desktop/",
            filter=formats)
        if self.path:
            self.output = os.path.splitext(self.path)[0] + '_split'
            if not os.path.exists(self.output):  # make output directory
                os.mkdir(self.output)

            self.name = os.path.split(self.output)[-1]
            self.nameLineEdit.setText(self.name)
            self.load_ndpi.setStyleSheet("background-color: rgb(0,90,0)")
            try:
                self.image = OpenSlide(self.path)
            except Exception as e:
                self.loadndpi()
            print(self.path + ' read to memory')
            print('    slide format = ' +
                  str(OpenSlide.detect_format(self.path)))
            if str(OpenSlide.detect_format(self.path)) == "aperio":
                try:
                    print(
                        '    Magnification = ' +
                        str(self.image.properties['openslide.objective-power'])
                    )  # TODO
                    print('    Date = ' +
                          str(self.image.properties['aperio.Date']))
                    print('    dimensions = ' + str(self.image.dimensions))
                    print('    level_downsamples = ' +
                          str(self.image.level_downsamples))
                except KeyError:
                    pass
            if str(OpenSlide.detect_format(self.path)) == "hamamatsu":
                try:
                    self.formatLineEdit.setText("Hamamatsu")
                    self.scanDateLineEdit.setText(
                        str(self.image.properties['tiff.DateTime'][:10]))
                    self.dimensionsLineEdit.setText(str(self.image.dimensions))
                    self.magnificationLineEdit.setText(
                        str(self.image.properties['hamamatsu.SourceLens']))
                    self.show_info(
                        f"""Magnification = {str(self.image.properties['hamamatsu.SourceLens'])}
Date = {str(self.image.properties['tiff.DateTime'])}\ndimensions = {str(self.image.dimensions)}
level_downsamples = {str(self.image.level_downsamples)}""")
                    print('    Magnification = ' +
                          str(self.image.properties['hamamatsu.SourceLens']))
                    print('    Date = ' +
                          str(self.image.properties['tiff.DateTime']))
                    print('    dimensions = ' + str(self.image.dimensions))
                    print('    level_downsamples = ' +
                          str(self.image.level_downsamples))
                    self.macro_image = self.image.associated_images['macro']
                except KeyError:
                    pass
            self.overview_level_width = 3000
            self.activate([
                self.nameLabel, self.nameLineEdit, self.formatLabel,
                self.formatLineEdit, self.magnificationLabel,
                self.magnificationLineEdit, self.scanDateLabel,
                self.scanDateLineEdit, self.dimensionsLabel,
                self.dimensionsLineEdit, self.overlayLevelLabel,
                self.overlayLevelLineEdit, self.graphicsView, self.overlaySave,
                self.groupBox_2, self.removesmallobjects
            ])
            self.get_overview()
            if os.path.exists(os.path.splitext(self.path)[0] + '.xlsx'):
                self.excelpath = os.path.splitext(self.path)[0] + '.xlsx'
                self.read_excel()
            else:
                self.excelpath = False