Exemple #1
0
    def MacOpenFiles(self, filenames):
        """On OSX, support opening files via context menu, and files dropped
        on the application icon.
        """

        # OverlayList has not yet been created -
        # queue the files to open them later
        # in SetOverlayListAndDisplayContext
        if self.__overlayList is None:
            self.__filesToOpen.extend(filenames)
            return

        import fsleyes.actions.loadoverlay as loadoverlay
        import fsleyes.autodisplay as autodisplay

        def onLoad(paths, overlays):

            if len(overlays) == 0:
                return

            self.__overlayList.extend(overlays)

            if self.__displayCtx.autoDisplay:
                for overlay in overlays:
                    autodisplay.autoDisplay(overlay, self.__overlayList,
                                            self.__displayCtx)

        loadoverlay.loadOverlays(filenames,
                                 onLoad=onLoad,
                                 inmem=self.__displayCtx.loadInMemory)
Exemple #2
0
    def MacOpenFiles(self, filenames):
        """On OSX, support opening files via context menu, and files dropped
        on the application icon.
        """

        if self.__overlayList is None:
            return

        import fsleyes.actions.loadoverlay as loadoverlay
        import fsleyes.autodisplay         as autodisplay

        def onLoad(paths, overlays):

            if len(overlays) == 0:
                return

            self.__overlayList.extend(overlays)

            if self.__displayCtx.autoDisplay:
                for overlay in overlays:
                    autodisplay.autoDisplay(overlay,
                                            self.__overlayList,
                                            self.__displayCtx)

        loadoverlay.loadOverlays(
            filenames,
            onLoad=onLoad,
            inmem=self.__displayCtx.loadInMemory)
    def __load(self, new, old, callback=None):
        """Called by :meth:`show`. Loads the files specified in ``new``, then
        passes them (along with the ``old``) to the :meth:`__show` method.

        :arg new:      Dict of ``{fileid : file}`` mappings, containing the
                       files to load.

        :arg old:      Dict of ``{fileid : overlay}`` mappings, containing the
                       existing overlays which will be replaced with the new
                       ones.

        :arg callback: No-args function which will be called after the new
                       overlays have been loaded.

        """

        # Remove any files that are
        # in the cache, and don't
        # need to be re-loaded
        cache = self.__cache
        toload = [(fid, f) for fid, f in new.items() if fid not in cache]
        loadfids = [f[0] for f in toload]
        loadfiles = [f[1] for f in toload]

        def onLoad(ovlidxs, ovls):

            # Gather the overlays to be shown,
            # either from the ones that were
            # just loaded, or from the cache.
            toshow = collections.OrderedDict()

            for fid, fname in new.items():

                # Is this overlay in the cache?
                if fname in cache:
                    toshow[fid] = cache[fname]

                # Or has it just been loaded?
                else:
                    idx = loadfids.index(fid)
                    toshow[fid] = ovls[idx]
                    cache.put(fname, ovls[idx])

            self.__show(toshow, old)

            if callback is not None:
                callback()

        loadoverlay.loadOverlays(loadfiles, onLoad=onLoad)
Exemple #4
0
    def load_png_image_from_path(self,
                                 image_path,
                                 is_mask=False,
                                 add_to_overlayList=True,
                                 colormap="greyscale"):
        """
        This function converts a 2D image into a NIfTI image and loads it as an overlay.
        The parameter add_to_overlayList allows to display the overlay into FSLeyes.
        :param image_path: The location of the image, including the name and the .extension
        :type image_path: Path
        :param is_mask: (optional) Whether or not this is a segmentation mask. It will be treated as a normal
        image by default.
        :type is_mask: bool
        :param add_to_overlayList: (optional) Whether or not to add the image to the overlay list. If so, the image will
        be displayed in the application. This parameter is True by default.
        :type add_to_overlayList: bool
        :param colormap: (optional) the colormap of image that will be displayed. This parameter is set to greyscale by
        default.
        :type colormap: string
        :return: the FSLeyes overlay corresponding to the loaded image.
        :rtype: overlay
        """

        # Open the 2D image
        img_png2D = ads_utils.imread(image_path)

        if is_mask is True:
            img_png2D = img_png2D // params.intensity[
                'binary']  # Segmentation masks should be binary

        # Flip the image on the Y axis so that the morphometrics file shows the right coordinates
        img_png2D = np.flipud(img_png2D)

        # Convert image data into a NIfTI image
        # Note: PIL and NiBabel use different axis conventions, so some array manipulation has to be done.
        img_NIfTI = nib.Nifti1Image(np.rot90(img_png2D, k=1, axes=(1, 0)),
                                    np.eye(4))

        # Save the NIfTI image in a temporary directory
        img_name = image_path.stem
        out_file = self.ads_temp_dir.__str__() + "/" + img_name + ".nii.gz"
        nib.save(img_NIfTI, out_file)

        # Load the NIfTI image as an overlay
        img_overlay = ovLoad.loadOverlays(paths=[out_file],
                                          inmem=True,
                                          blocking=True)[0]

        # Display the overlay
        if add_to_overlayList is True:
            self.overlayList.append(img_overlay)
            opts = self.displayCtx.getOpts(img_overlay)
            opts.cmap = colormap

        return img_overlay