Пример #1
0
def getTotal(imagefile,setflat=True):
    options = DynamicMetadataOptions()
    options.setBoolean("zeissczi.attachments", False)
    czireader = ZeissCZIReader()
    czireader.setFlattenedResolutions(setflat)
    czireader.setId(imagefile)
    seriesCount = czireader.getSeriesCount()
    czireader.close()
    return seriesCount
Пример #2
0
    def setCZIReaderOptions(imagefile, czi_options, setflatres=True):

        czireader = ZeissCZIReader()
        czireader.setFlattenedResolutions(setflatres)
        czireader.setMetadataOptions(czi_options)
        czireader.setId(imagefile)

        return czireader
Пример #3
0
def readczi(imagefile,
            stitchtiles=True,
            setflatres=False,
            readpylevel=0,
            setconcat=True,
            openallseries=True,
            showomexml=False,
            attach=False,
            autoscale=True):

    log.info('Filename : ' + imagefile)

    metainfo = {}
    # checking for thr file Extension
    metainfo['Extension'] = MiscTools.getextension(MiscTools.splitext_recurse(imagefile))
    log.info('Detected File Extension : ' + metainfo['Extension'])

    # initialize the reader and get the OME metadata
    reader = ImageReader()
    omeMeta = MetadataTools.createOMEXMLMetadata()
    #metainfo['ImageCount_OME'] = omeMeta.getImageCount()
    reader.setMetadataStore(omeMeta)
    reader.setId(imagefile)
    metainfo['SeriesCount_BF'] = reader.getSeriesCount()
    reader.close()

    # get the scaling for XYZ
    physSizeX = omeMeta.getPixelsPhysicalSizeX(0)
    physSizeY = omeMeta.getPixelsPhysicalSizeY(0)
    physSizeZ = omeMeta.getPixelsPhysicalSizeZ(0)

    if physSizeX is not None:
        metainfo['ScaleX'] = round(physSizeX.value(), 3)
        metainfo['ScaleY'] = round(physSizeX.value(), 3)

    if physSizeX is None:
        metainfo['ScaleX'] = None
        metainfo['ScaleY'] = None

    if physSizeZ is not None:
        metainfo['ScaleZ'] = round(physSizeZ.value(), 3)
    if physSizeZ is None:
        metainfo['ScaleZ'] = None

    options = DynamicMetadataOptions()
    options.setBoolean("zeissczi.autostitch", stitchtiles)
    options.setBoolean("zeissczi.attachments", attach)

    czireader = ZeissCZIReader()
    czireader.setFlattenedResolutions(setflatres)
    czireader.setMetadataOptions(options)
    czireader.setId(imagefile)

    # Set the preferences in the ImageJ plugin
    # Note although these preferences are applied, they are not refreshed in the UI
    Prefs.set("bioformats.zeissczi.allow.autostitch", str(stitchtiles).lower())
    Prefs.set("bioformats.zeissczi.include.attachments", str(attach).lower())

    # metainfo = {}
    metainfo['rescount'] = czireader.getResolutionCount()
    metainfo['SeriesCount_CZI'] = czireader.getSeriesCount()
    #metainfo['flatres'] = czireader.hasFlattenedResolutions()
    #metainfo['getreslevel'] = czireader.getResolution()

    # Dimensions
    metainfo['SizeT'] = czireader.getSizeT()
    metainfo['SizeZ'] = czireader.getSizeZ()
    metainfo['SizeC'] = czireader.getSizeC()
    metainfo['SizeX'] = czireader.getSizeX()
    metainfo['SizeY'] = czireader.getSizeY()

    # check for autostitching and possibility to read attachment
    metainfo['AllowAutoStitching'] = czireader.allowAutostitching()
    metainfo['CanReadAttachments'] = czireader.canReadAttachments()

    # read in and display ImagePlus(es) with arguments
    options = ImporterOptions()
    options.setOpenAllSeries(openallseries)
    options.setShowOMEXML(showomexml)
    options.setConcatenate(setconcat)
    options.setAutoscale(autoscale)
    options.setId(imagefile)

    # open the ImgPlus
    imps = BF.openImagePlus(options)
    metainfo['Pyramid Level Output'] = readpylevel + 1

    try:
        imp = imps[readpylevel]
        pylevelout = metainfo['SeriesCount_CZI']
    except:
        # fallback option
        log.info('PyLevel=' + str(readpylevel) + ' does not exist.')
        log.info('Using Pyramid Level = 0 as fallback.')
        imp = imps[0]
        pylevelout = 0
        metainfo['Pyramid Level Output'] = pylevelout

    # get the stack and some info
    imgstack = imp.getImageStack()
    metainfo['Output Slices'] = imgstack.getSize()
    metainfo['Output SizeX'] = imgstack.getWidth()
    metainfo['Output SizeY'] = imgstack.getHeight()

    # calc scaling in case of pyramid
    scale = float(metainfo['SizeX']) / float(metainfo['Output SizeX'])
    metainfo['Pyramid Scale Factor'] = scale
    metainfo['ScaleX Output'] = metainfo['ScaleX'] * scale
    metainfo['ScaleY Output'] = metainfo['ScaleY'] * scale

    # set the correct scaling
    imp = MiscTools.setscale(imp, scaleX=metainfo['ScaleX Output'],
                             scaleY=metainfo['ScaleX Output'],
                             scaleZ=metainfo['ScaleZ'],
                             unit="micron")

    # close czireader
    czireader.close()

    return imp, metainfo
Пример #4
0
    def readCZI(imagefile,
                metainfo,
                stitchtiles=False,
                setflatres=False,
                readpylevel=0,
                setconcat=False,
                openallseries=True,
                showomexml=False,
                attach=False,
                autoscale=True):

        options = DynamicMetadataOptions()
        options.setBoolean("zeissczi.autostitch", stitchtiles)
        options.setBoolean("zeissczi.attachments", attach)

        czireader = ZeissCZIReader()
        czireader.setFlattenedResolutions(setflatres)
        czireader.setMetadataOptions(options)
        czireader.setId(imagefile)

        # Set the preferences in the ImageJ plugin
        # Note although these preferences are applied, they are not refreshed in the UI
        Prefs.set("bioformats.zeissczi.allow.autostitch", str(stitchtiles).lower())
        Prefs.set("bioformats.zeissczi.include.attachments", str(attach).lower())

        # metainfo = {}
        metainfo['rescount'] = czireader.getResolutionCount()
        metainfo['SeriesCount_CZI'] = czireader.getSeriesCount()
        metainfo['flatres'] = czireader.hasFlattenedResolutions()
        # metainfo['getreslevel'] = czireader.getResolution()

        # Dimensions
        metainfo['SizeT'] = czireader.getSizeT()
        metainfo['SizeZ'] = czireader.getSizeZ()
        metainfo['SizeC'] = czireader.getSizeC()
        metainfo['SizeX'] = czireader.getSizeX()
        metainfo['SizeY'] = czireader.getSizeY()

        # check for autostitching and possibility to read attachment
        metainfo['AllowAutoStitching'] = czireader.allowAutostitching()
        metainfo['CanReadAttachments'] = czireader.canReadAttachments()

        # read in and display ImagePlus(es) with arguments
        options = ImporterOptions()
        options.setOpenAllSeries(openallseries)
        options.setShowOMEXML(showomexml)
        options.setConcatenate(setconcat)
        options.setAutoscale(autoscale)
        options.setId(imagefile)

        # open the ImgPlus
        imps = BF.openImagePlus(options)

        metainfo['Pyramid Level Output'] = readpylevel

        # read image data using the specified pyramid level
        imp, slices, width, height, pylevel = ImageTools.getImageSeries(imps, series=readpylevel)
        metainfo['Pyramid Level Output'] = pylevel

        metainfo['Output Slices'] = slices
        metainfo['Output SizeX'] = width
        metainfo['Output SizeY'] = height

        # calc scaling in case of pyramid
        # scale = float(metainfo['Output SizeX']) / float(metainfo['SizeX'])
        scale = float(metainfo['SizeX']) / float(metainfo['Output SizeX'])

        metainfo['Pyramid Scale Factor'] = scale
        metainfo['ScaleX Output'] = metainfo['ScaleX'] * scale
        metainfo['ScaleY Output'] = metainfo['ScaleY'] * scale

        """
        imp = MiscTools.setproperties(imp, scaleX=metainfo['ScaleX Output'],
                                      scaleY=metainfo['ScaleX Output'],
                                      scaleZ=metainfo['ScaleZ'],
                                      unit="micron",
                                      sizeC=metainfo['SizeC'],
                                      sizeZ=metainfo['SizeZ'],
                                      sizeT=metainfo['SizeT'])
        """

        imp = MiscTools.setscale(imp, scaleX=metainfo['ScaleX Output'],
                                 scaleY=metainfo['ScaleX Output'],
                                 scaleZ=metainfo['ScaleZ'],
                                 unit="micron")

        # close czireader
        czireader.close()

        return imp, metainfo
Пример #5
0
def check_fusion_settings(czi_path):
    """Check for fusion settings and asks confirmation to user if H5/XML fusion

    Parameters
    ----------
    czi_path : str
        Path to the CZI file

    Returns
    -------
    bool
        Bool for fusion
    str
        Method of RAM handling
    bool
        Bool for TIFF or H5/XML fusion
    """

    # Default values
    do_fusion = True
    fuse_tiff = True

    reader = ZeissCZIReader()
    m = DynamicMetadataOptions()
    m.setBoolean(ZeissCZIReader.ALLOW_AUTOSTITCHING_KEY, False)
    m.setBoolean(ZeissCZIReader.RELATIVE_POSITIONS_KEY, True)
    reader.setMetadataOptions(m)
    omeMeta = MetadataTools.createOMEXMLMetadata()
    reader.setMetadataStore(omeMeta)
    reader.setId(str(czi_path))

    nbr_tp = omeMeta.getTimestampAnnotationCount() + 1
    nbr_chnl = omeMeta.getChannelCount(0)

    # check the file size of the file to be fused and compare to the available RAM
    # h5_filesize = os.path.getsize(export_path_temp + ".h5")
    h5_filesize = os.path.getsize(czi_path) / 2
    free_memory = get_free_memory()

    print("h5 filesize " + convert_bytes(h5_filesize))
    print("free memory in ij " + convert_bytes(free_memory))

    # TODO: include in below calculation t_end, since only one t is fused at a time.
    if free_memory > (6 * h5_filesize / downsampling):
        ram_handling = "[Precompute Image]"
    else:
        ram_handling = "Virtual"
    print("fusion mode used " + str(ram_handling))

    # if autoselect_illuminations and nbr_ill > 1:
    #     ill_value = 2
    # else:
    #     ill_value = 1

    ram_requirement = 2 * h5_filesize / (nbr_tp * nbr_chnl * downsampling)
    print(ram_requirement)
    sufficient_ram = ram_requirement < free_memory / 10

    if not sufficient_ram:
        try:
            yn = YesNoCancelDialog(
                IJ.getInstance(),
                "Warning!",
                (
                    "File size is too big to use TIFF for fusion\n"
                    "Fusion will happen using H5/XML which might take weeks. Are you "
                    "sure you want to do fusion ?\n"
                    "All steps prior to fusion would still happen, allowing for manual "
                    "fusion and tile selection."
                ),
            )
            if yn.yesPressed():
                fuse_tiff = False
            else:
                do_fusion = False
        except Exception:
            # when running headless the above will raise a java.awt.HeadlessException,
            # so we simply fall back to the same behavior as if "No" was clicked:
            do_fusion = False
    return do_fusion, ram_handling, fuse_tiff
Пример #6
0
def getCZIinfo(imagefile, showimage=False, setreslevel=0, setflat2=False, openallseries=True, showomexml=False,setconcat=False,filepath1="./"):
    options = DynamicMetadataOptions()
    options.setBoolean("zeissczi.attachments", False)
    czireader = ZeissCZIReader()
    czireader.setFlattenedResolutions(setflat2)
    czireader.setMetadataOptions(options)
    czireader.setId(imagefile)
    lc = czireader.getSeriesCount()
    #get the first occurence of each pyramid stack
    location=list()
    for i in range(0, int(seriesCount)-2):
        location.append(czireader.coreIndexToSeries(i))
        c=0
    #log.info(location)
    loc2=list()
    for i,v in enumerate(location):
    	if i==0:
        	loc2.append(i)
    	elif i>0 and v!=c:
        	loc2.append(i)
        	c=v
    log.info(str(loc2))
    # get OME data
    omeMeta = MetadataTools.createOMEXMLMetadata()
    # Set the preferences in the ImageJ plugin
    Prefs.set("bioformats.zeissczi.include.attachments", str(True).lower())
    if showimage:

        # read in and display ImagePlus(es) with arguments
        options = ImporterOptions()
        options.setOpenAllSeries(openallseries)
        options.setShowOMEXML(showomexml)
        options.setConcatenate(setconcat)
        options.setId(imagefile)

        # open the ImgPlus
        imps = BF.openImagePlus(options)
        name_list=imagefile.split('/')
        name=name_list[len(name_list)-1]
        out_path=filepath1  + "/"+name+"_Preview.tif"
        log.info(name)
        imp=getImageSeries(imps, seriesCount-1)
        imp.show()
        IJ.run("RGB Color")
        imp.close()
        IJ.saveAs("tiff", out_path)
        IJ.run("Close")
        out_path=filepath1  + "/"+name+"_Label.tif"
        imp=getImageSeries(imps, (seriesCount-2))
        imp.show()
        IJ.run("RGB Color")
        imp.close()
        IJ.saveAs("tiff", out_path)
        IJ.run("Close")
        c=1
        for series in loc2:
        	out_path=filepath1  + "/"+name+"Scene_" + str(c) + ".tif"
        	imp=getImageSeries(imps, series)
        	imp.show()
        	IJ.run("RGB Color")
        	imp.close()
        	IJ.saveAs("tiff", out_path)
        	IJ.run("Close")
        	c+=1
    czireader.close()