コード例 #1
0
def test_read_subblock_meta(data_dir, fname, expected):
    with open(data_dir / fname, 'rb') as fp:
        czi = CziFile(czi_filename=fp)
        data = czi.read_subblock_metadata()
        assert expected in data[0][1]
コード例 #2
0
def test_read_unified_subblock_meta(data_dir, fname, expected):
    with open(data_dir / fname, 'rb') as fp:
        czi = CziFile(czi_filename=fp)
        data = czi.read_subblock_metadata(unified_xml=True)
        assert expected in etree.tostring(data)
コード例 #3
0
ファイル: misc.py プロジェクト: sebi06/czi_demos
def get_planetable(czifile: str,
                   norm_time: bool = True,
                   savetable: bool = False,
                   separator: str = ',',
                   index: bool = True) -> Tuple[pd.DataFrame, Optional[str]]:

    # get the czi metadata
    metadata = czimd.CziMetadata(czifile)
    aicsczi = CziFile(czifile)

    # initialize the plane table
    df_czi = pd.DataFrame(columns=[
        'Subblock', 'Scene', 'Tile', 'T', 'Z', 'C', 'X[micron]', 'Y[micron]',
        'Z[micron]', 'Time[s]', 'xstart', 'ystart', 'width', 'height'
    ])

    # define subblock counter
    sbcount = -1

    # check if dimensions are None (because they do not exist for that image)
    sizeC = check_dimsize(metadata.image.SizeC, set2value=1)
    sizeZ = check_dimsize(metadata.image.SizeZ, set2value=1)
    sizeT = check_dimsize(metadata.image.SizeT, set2value=1)
    sizeS = check_dimsize(metadata.image.SizeS, set2value=1)
    sizeM = check_dimsize(metadata.image.SizeM, set2value=1)

    def getsbinfo(subblock: Any) -> Tuple[float, float, float, float]:
        try:
            # time = sb.xpath('//AcquisitionTime')[0].text
            time = subblock.findall(".//AcquisitionTime")[0].text
            timestamp = dt.parse(time).timestamp()
        except IndexError as e:
            timestamp = 0.0

        try:
            # xpos = np.double(sb.xpath('//StageXPosition')[0].text)
            xpos = np.double(subblock.findall(".//StageXPosition")[0].text)
        except IndexError as e:
            xpos = 0.0

        try:
            # ypos = np.double(sb.xpath('//StageYPosition')[0].text)
            ypos = np.double(subblock.findall(".//StageYPosition")[0].text)
        except IndexError as e:
            ypos = 0.0

        try:
            # zpos = np.double(sb.xpath('//FocusPosition')[0].text)
            zpos = np.double(subblock.findall(".//FocusPosition")[0].text)
        except IndexError as e:
            zpos = 0.0

        return timestamp, xpos, ypos, zpos

    # in case the CZI has the M-Dimension
    if metadata.ismosaic:

        for s, m, t, z, c in product(range(sizeS), range(sizeM), range(sizeT),
                                     range(sizeZ), range(sizeC)):
            sbcount += 1
            print("Reading sublock : ", sbcount)

            # get x, y, width and height for a specific tile
            tilebbox = aicsczi.get_mosaic_tile_bounding_box(S=s,
                                                            M=m,
                                                            T=t,
                                                            Z=z,
                                                            C=c)

            # read information from subblock
            sb = aicsczi.read_subblock_metadata(unified_xml=True,
                                                B=0,
                                                S=s,
                                                M=m,
                                                T=t,
                                                Z=z,
                                                C=c)

            # get information from subblock
            timestamp, xpos, ypos, zpos = getsbinfo(sb)

            df_czi = df_czi.append(
                {
                    'Subblock': sbcount,
                    'Scene': s,
                    'Tile': m,
                    'T': t,
                    'Z': z,
                    'C': c,
                    'X[micron]': xpos,
                    'Y[micron]': ypos,
                    'Z[micron]': zpos,
                    'Time[s]': timestamp,
                    'xstart': tilebbox.x,
                    'ystart': tilebbox.y,
                    'width': tilebbox.w,
                    'height': tilebbox.h
                },
                ignore_index=True)

    if not metadata.ismosaic:

        for s, t, z, c in product(range(sizeS), range(sizeT), range(sizeZ),
                                  range(sizeC)):
            sbcount += 1

            # get x, y, width and height for a specific tile
            tilebbox = aicsczi.get_tile_bounding_box(S=s, T=t, Z=z, C=c)

            # read information from subblocks
            sb = aicsczi.read_subblock_metadata(unified_xml=True,
                                                B=0,
                                                S=s,
                                                T=t,
                                                Z=z,
                                                C=c)

            # get information from subblock
            timestamp, xpos, ypos, zpos = getsbinfo(sb)

            df_czi = df_czi.append(
                {
                    'Subblock': sbcount,
                    'Scene': s,
                    'Tile': 0,
                    'T': t,
                    'Z': z,
                    'C': c,
                    'X[micron]': xpos,
                    'Y[micron]': ypos,
                    'Z[micron]': zpos,
                    'Time[s]': timestamp,
                    'xstart': tilebbox.x,
                    'ystart': tilebbox.y,
                    'width': tilebbox.w,
                    'height': tilebbox.h
                },
                ignore_index=True)

    # cast data  types
    df_czi = df_czi.astype(
        {
            'Subblock': 'int32',
            'Scene': 'int32',
            'Tile': 'int32',
            'T': 'int32',
            'Z': 'int32',
            'C': 'int16',
            'X[micron]': 'float',
            'Y[micron]': 'float',
            'Z[micron]': 'float',
            'xstart': 'int32',
            'ystart': 'int32',
            'width': 'int32',
            'height': 'int32'
        },
        copy=False,
        errors='ignore')

    # normalize time stamps
    if norm_time:
        df_czi = norm_columns(df_czi, colname='Time[s]', mode='min')

    # save planetable as CSV file
    if savetable:
        csvfile = save_planetable(df_czi,
                                  czifile,
                                  separator=separator,
                                  index=index)
    if not savetable:
        csvfile = None

    return df_czi, csvfile