Skip to content

Commit

Permalink
Merge pull request #73 from sebi06/work_in_progress
Browse files Browse the repository at this point in the history
Work in progress
  • Loading branch information
sebi06 authored Dec 11, 2024
2 parents 01e54c9 + b02d39b commit 17eb787
Show file tree
Hide file tree
Showing 17 changed files with 581 additions and 162 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,5 @@ data/nuc_small.czi
data/nuc_small.czi
data/nuc_small_green.czi
data/nuc_small.czi
data/Tumor_HE_Orig_small_disp.czi
data/Tumor_HE_RGB_disp.czi
19 changes: 19 additions & 0 deletions demo/testing/pass_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-

from pylibCZIrw import czi as pyczi

filepath = r"F:\Testdata_Zeiss\CZI_Testfiles\DAPI_GFP.czi"


class ZenReader:

def __init__(self, filepath):
self.filepath = filepath
self._czidoc = pyczi.CziReader(self.filepath)

def read_czi(self):
total_bounding_box = self._czidoc.total_bounding_box
print(total_bounding_box)

def __del__(self):
self._czidoc.close()
85 changes: 85 additions & 0 deletions demo/testing/read_write_czi_display_setting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-

#################################################################
# File : read_write_czi.py
# Author : sebi06
#
# Disclaimer: This code is purely experimental. Feel free to
# use it at your own risk.
#
#################################################################

from pylibCZIrw import czi as pyczi
from czitools.metadata_tools.czi_metadata import CziChannelInfo
import numpy as np


# def hex_to_rgb(hex_string: str) -> tuple[int, int, int]:
# """
# Convert a hexadecimal color string to an RGB tuple.
# Args:
# hex_string (str): A string representing a color in hexadecimal format (e.g., '#RRGGBB').
# Returns:
# tuple: A tuple containing the RGB values (r, g, b) as integers.
# """
# # Remove the '#' character
# hex_string = hex_string.lstrip("#")

# # Convert hex string to integer values
# r = int(hex_string[0:2], 16)
# g = int(hex_string[2:4], 16)
# b = int(hex_string[4:6], 16)

# return r, g, b

# filepath = r"F:\Github\czitools\data\Tumor_HE_Orig_small.czi"
# filepath = r"F:\Github\czitools\data\CellDivision_T3_Z5_CH2_X240_Y170.czi"
filepath = r"F:\Github\czitools\data\Al2O3_SE_020_sp.czi"
# filepath = r"F:\Github\czitools\data\w96_A1+A2.czi"
filepath_new = filepath[:-4] + "_disp.czi"

write_new = False

ch_info = CziChannelInfo(filepath, verbose=True)

channel_names_dict = {index: value for index, value in enumerate(ch_info.names)}

display_settings_dict = ch_info.czi_disp_settings

# display_settings_dict = {}

# for channel_index in range(len(ch_info.names)):

# # inside the CZI metadata_tools colors are defined as ARGB hexstring
# r, g, b = hex_to_rgb(ch_info.colors[channel_index][3:])

# display_settings_dict[channel_index] = pyczi.ChannelDisplaySettingsDataClass(
# True,
# pyczi.TintingMode.Color,
# pyczi.Rgb8Color(np.uint8(r), np.uint8(g), np.uint8(b)),
# ch_info.clims[channel_index][0],
# ch_info.clims[channel_index][1],
# )


with pyczi.open_czi(filepath) as czidoc:

ch0 = czidoc.read(plane={"T": 0, "Z": 0, "C": 0})
# ch1 = czidoc.read(plane={"T": 0, "Z": 0, "C": 1})

print(f"ch_info: {ch_info}")

if write_new:

with pyczi.create_czi(filepath_new, exist_ok=True) as czidoc:

# write the 2D plane to the new CZI file
czidoc.write(data=ch0, plane={"T": 0, "Z": 0, "C": 0})
# czidoc.write(data=ch1, plane={"T": 0, "Z": 0, "C": 1})

# write the document title, channel names and dispaly settings
czidoc.write_metadata(
document_name="test",
channel_names=channel_names_dict,
display_settings=display_settings_dict,
)
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = czitools
version = 0.7.3
version = 0.7.4
author = Sebastian Rhode
author_email = [email protected]
url = https://github.com/sebi06/czitools
Expand Down
2 changes: 1 addition & 1 deletion src/czitools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# __init__.py
# version of the czitools package
__version__ = "0.7.3"
__version__ = "0.7.4"
154 changes: 124 additions & 30 deletions src/czitools/_tests/test_channelinfo.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,137 @@
from pathlib import Path
from czitools.metadata_tools import czi_metadata as czimd
import pytest
from typing import List
from typing import List, Dict
from pylibCZIrw import czi as pyczi


basedir = Path(__file__).resolve().parents[3]


@pytest.mark.parametrize(
"czifile, clims, colors, gamma, names, dyes",
"czifile, clims, colors, gamma, names, dyes, ch_disp, is_rgb, is_consistent, pxtypes",
[
("CellDivision_T3_Z5_CH2_X240_Y170.czi",
[[0.0, 0.05983062485694667], [0.0, 0.24975967040512703]],
["#FFFF7E00", "#FF00FF33"],
[0.7999999999999998, 0.7999999999999998],
["LED555", "LED470"],
["AF555", "AF488"]),
("Al2O3_SE_020_sp.czi",
[[0.0, 0.5]],
["#80808000"],
[0.85],
["CH1"],
["Dye-CH1"]),
("w96_A1+A2.czi",
[[0.000871455799315693, 0.044245974575704575], [0.000881881329185286, 0.05011349562051524]],
['#FFFF1800', '#FF00FF33'],
[0.7999999999999998, 0.7999999999999998],
['AF568', 'AF488'],
['AF568', 'AF488'])
]
(
"Tumor_HE_Orig_small.czi",
[[0.0, 0.5]],
["#80808000"],
[0.85],
["CH1"],
["Brigh"],
{
0: pyczi.ChannelDisplaySettingsDataClass(
True,
pyczi.TintingMode.none,
pyczi.Rgb8Color(r=128, g=128, b=0),
0.0,
1.0,
)
},
{0: True},
True,
{0: "Bgr24"},
),
(
"CellDivision_T3_Z5_CH2_X240_Y170.czi",
[[0.0, 0.05983062485694667], [0.0, 0.24975967040512703]],
["#FFFF7E00", "#FF00FF33"],
[0.7999999999999998, 0.7999999999999998],
["LED555", "LED470"],
["AF555", "AF488"],
{
0: pyczi.ChannelDisplaySettingsDataClass(
True,
pyczi.TintingMode.Color,
pyczi.Rgb8Color(r=255, g=126, b=0),
0.0,
0.05983062485694667,
),
1: pyczi.ChannelDisplaySettingsDataClass(
True,
pyczi.TintingMode.Color,
pyczi.Rgb8Color(r=0, g=255, b=51),
0.0,
0.24975967040512703,
),
},
{0: False, 1: False},
True,
{0: "Gray16", 1: "Gray16"},
),
(
"Al2O3_SE_020_sp.czi",
[[0.0, 0.5]],
["#80808000"],
[0.85],
["CH1"],
["Dye-CH1"],
{
0: pyczi.ChannelDisplaySettingsDataClass(
True,
pyczi.TintingMode.Color,
pyczi.Rgb8Color(r=128, g=128, b=0),
0.0,
0.5,
)
},
{0: False},
True,
{0: "Gray8"},
),
(
"w96_A1+A2.czi",
[
[0.000871455799315693, 0.044245974575704575],
[0.000881881329185286, 0.05011349562051524],
],
["#FFFF1800", "#FF00FF33"],
[0.7999999999999998, 0.7999999999999998],
["AF568", "AF488"],
["AF568", "AF488"],
{
0: pyczi.ChannelDisplaySettingsDataClass(
True,
pyczi.TintingMode.Color,
pyczi.Rgb8Color(r=255, g=24, b=0),
0.000871455799315693,
0.044245974575704575,
),
1: pyczi.ChannelDisplaySettingsDataClass(
True,
pyczi.TintingMode.Color,
pyczi.Rgb8Color(r=0, g=255, b=51),
0.000881881329185286,
0.05011349562051524,
),
},
{0: False, 1: False},
True,
{0: "Gray16", 1: "Gray16"},
),
],
)
def test_channelinfo(czifile: str, clims: List, colors: List, gamma: List, names: List, dyes: List) -> None:
def test_channelinfo(
czifile: str,
clims: List[List[float]],
colors: List[str],
gamma: List[float],
names: List[str],
dyes: List[str],
ch_disp: Dict[int, pyczi.ChannelDisplaySettingsDataClass],
is_rgb: Dict[int, bool],
is_consistent: bool,
pxtypes: Dict[int, str],
) -> None:
# get the CZI filepath
filepath = basedir / "data" / czifile
czi_channels = czimd.CziChannelInfo(filepath)
czi_channels = czimd.CziChannelInfo(filepath, verbose=False)

assert (czi_channels.clims == clims)
assert (czi_channels.colors == colors)
assert (czi_channels.gamma == gamma)
assert (czi_channels.names == names)
assert (czi_channels.dyes == dyes)
assert czi_channels.clims == clims
assert czi_channels.colors == colors
assert czi_channels.gamma == gamma
assert czi_channels.names == names
assert czi_channels.dyes == dyes
assert czi_channels.czi_disp_settings == ch_disp
assert czi_channels.isRGB == is_rgb
assert czi_channels.consistent_pixeltypes == is_consistent
assert czi_channels.pixeltypes == pxtypes
17 changes: 8 additions & 9 deletions src/czitools/_tests/test_czimd_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
[
"has_customattr",
"has_exp",
"has_disp",
"has_hardware",
"has_scale",
"has_instrument",
Expand All @@ -27,16 +26,16 @@
"has_doc",
"has_image",
"has_scenes",
"has_dims"
]
"has_dims",
],
)
def test_general_attr(attribute: str) -> None:

filepath = defaultdir / "CellDivision_T3_Z5_CH2_X240_Y170.czi"

czibox = czimd.get_czimd_box(filepath)

assert (hasattr(czibox, attribute))
assert hasattr(czibox, attribute)


@pytest.mark.parametrize(
Expand All @@ -51,11 +50,11 @@ def test_general_attr(attribute: str) -> None:
("FOV7_HV110_P0500510000.czi", True),
("Tumor_HE_RGB.czi", True),
("WP96_4Pos_B4-10_DAPI.czi", True),
("S3_1Pos_2Mosaic_T2_Z3_CH2_sm.czi", True)
]
("S3_1Pos_2Mosaic_T2_Z3_CH2_sm.czi", True),
],
)
def test_box(czifile: List[str], expected: bool) -> None:

# def test_box(czifile: List[str], expected: bool) -> None:
def test_box(czifile: str, expected: bool) -> None:
filepath = defaultdir / czifile

try:
Expand All @@ -64,4 +63,4 @@ def test_box(czifile: List[str], expected: bool) -> None:
except Exception:
ok = False

assert (ok == expected)
assert ok == expected
15 changes: 10 additions & 5 deletions src/czitools/_tests/test_md_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,26 @@ def test_read_metadata_links(link: str) -> None:


@pytest.mark.parametrize(
"czifile, px, rgb, maxvalue",
"czifile, px, is_rgb, maxvalue",
[
("LLS7_small.czi", {0: "Gray16", 1: "Gray16"}, False, [65535, 65535]),
("Tumor_HE_RGB.czi", {0: "Bgr24"}, True, [255]),
(
"LLS7_small.czi",
{0: "Gray16", 1: "Gray16"},
{0: False, 1: False},
[65535, 65535],
),
("Tumor_HE_RGB.czi", {0: "Bgr24"}, {0: True}, [255]),
],
)
def test_pixeltypes_1(czifile: str, px: Dict, rgb: bool, maxvalue: int) -> None:
def test_pixeltypes_1(czifile: str, px: Dict, is_rgb: bool, maxvalue: int) -> None:

# get the CZI filepath
filepath = basedir / "data" / czifile
md = czimd.CziMetadata(filepath)

assert md.pixeltypes == px
assert md.maxvalue_list == maxvalue
assert md.isRGB is rgb
assert md.isRGB == is_rgb


@pytest.mark.parametrize(
Expand Down
13 changes: 13 additions & 0 deletions src/czitools/_tests/test_read_mdarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@
def test_read_mdarray(
czifile: str, shape: Optional[Tuple[int]], use_dask: bool, chunk_zyx: bool
) -> None:
"""
Test the reading of a multidimensional array from a CZI file.
Parameters:
czifile (str): The name of the CZI file to read.
shape (Optional[Tuple[int]]): The expected shape of the multidimensional array.
If an exception type is provided, the test will check
if the exception is raised.
use_dask (bool): Whether to use Dask for reading the array.
chunk_zyx (bool): Whether to chunk the array along the Z, Y, and X dimensions.
Returns:
None
"""

# get the CZI filepath
filepath = basedir / "data" / czifile

Expand Down
Loading

0 comments on commit 17eb787

Please sign in to comment.