Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.0.0 #3

Merged
merged 128 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
a9d3961
refactor everything again
jopo666 Feb 7, 2023
14bc890
cli done
jopo666 Feb 8, 2023
8229725
add logo
jopo666 Feb 14, 2023
049946f
downsample -> multiply
jopo666 Feb 15, 2023
87c4b23
add tissue mask cleaning for dearraying
jopo666 Feb 15, 2023
43b18d4
reformat
jopo666 Feb 15, 2023
d127042
add dearraying
jopo666 Feb 15, 2023
61f2d97
move and unify saving functions, reformat dataclasses
jopo666 Feb 15, 2023
2d7c04f
update deps
jopo666 Feb 15, 2023
c074ede
fix level>0 reading
jopo666 Feb 15, 2023
ce5387d
update init
jopo666 Feb 15, 2023
662b596
update reader methods
jopo666 Feb 20, 2023
ab9ded1
ignore test slides
jopo666 Feb 23, 2023
6046476
add license
jopo666 Feb 23, 2023
d83dd7e
update readers
jopo666 Feb 23, 2023
c3ca99f
remove catch errors
jopo666 Feb 23, 2023
aae8b00
add error import
jopo666 Feb 23, 2023
ca0855e
update functionals
jopo666 Feb 23, 2023
620939e
update cli
jopo666 Feb 23, 2023
2e00714
update tests
jopo666 Feb 23, 2023
bd2956b
fix pad tiles bug and tissue test
jopo666 Feb 27, 2023
b7e5b8f
fix read_region and add tests for it
jopo666 Mar 3, 2023
60a37a1
rename tissue functional and fix clean mask
jopo666 Mar 3, 2023
b9d8fd7
fix dearray and add tests
jopo666 Mar 3, 2023
db1b93c
rename tissue mask functional
jopo666 Mar 3, 2023
f36032f
add xywh tests
jopo666 Mar 3, 2023
6e3759d
add test utils
jopo666 Mar 3, 2023
8b679b2
add message
jopo666 Mar 3, 2023
ad0fd32
bugfix
jopo666 Mar 3, 2023
258b275
bugfixes to xywh handling
jopo666 Mar 3, 2023
b390313
openslide tries to open unexistent files
jopo666 Mar 3, 2023
3722c4e
add basic reader tests
jopo666 Mar 3, 2023
ad3151a
refator
jopo666 Mar 3, 2023
9efd407
remove "spot_" from thumbnail
jopo666 Mar 9, 2023
4f4946b
rstrip to removesuffix
jopo666 Mar 9, 2023
b118e92
bugfixes and better variable names
jopo666 Mar 9, 2023
a5afd1b
forgot mask folder
jopo666 Mar 9, 2023
c760335
bugfix when there are no contours
jopo666 Mar 9, 2023
182e180
bugfix: tissue mask is empty/cleaned completely
jopo666 Mar 9, 2023
28d4c55
add tests for empty masks
jopo666 Mar 9, 2023
1d1b824
default to no metrics with tma spots
jopo666 Mar 9, 2023
59ddef0
add reader tests
jopo666 Mar 9, 2023
93f60fe
fuck autoimport
jopo666 Mar 9, 2023
42763ba
add stain norm/adjust/separate
jopo666 Mar 10, 2023
608cbc6
and process module with stainnorm classes
jopo666 Mar 10, 2023
bd5d1fa
remove public attributes
jopo666 Mar 10, 2023
a3c8316
add prerelease build
jopo666 Mar 10, 2023
a8daa40
update to 3.10 annotations
jopo666 Mar 10, 2023
928eb39
always ignore white and black pixels with otsu's method
jopo666 Mar 10, 2023
e703be3
add ipywidgets to dev as tqdm keeps complaining
jopo666 Mar 10, 2023
8d5675d
add TileMetadata processing class
jopo666 Mar 10, 2023
c0e1fb7
simplify and add reference stain matrix & max-c
jopo666 Mar 13, 2023
b557f19
outlier adding
jopo666 Mar 13, 2023
1cf8f14
replace=False with random collage
jopo666 Mar 13, 2023
9058667
return array with n_images>0 and update docstring
jopo666 Mar 13, 2023
087d232
move worker init to separate file; rename save fn
jopo666 Mar 13, 2023
be42483
add read tile function; fix import bug
jopo666 Mar 13, 2023
fca3cd7
bugfix in czi (forgot to test read_level)
jopo666 Mar 13, 2023
30d49ec
remove czi warning
jopo666 Mar 13, 2023
07fa444
add yield tiles
jopo666 Mar 13, 2023
a048f11
new build
jopo666 Mar 13, 2023
2f6d1f2
move pytest ini and add doctests
jopo666 Mar 13, 2023
559c164
add doctest
jopo666 Mar 13, 2023
b75556b
non-overlapping tiles, overlap index & area
jopo666 Mar 13, 2023
f046274
simplify metric calculation
jopo666 Mar 13, 2023
e582d8e
add docstring; change output type; estimate mean&std
jopo666 Mar 13, 2023
2079517
add torch dataset for slidereader
jopo666 Mar 13, 2023
3404d38
new build
jopo666 Mar 13, 2023
c92fa88
remove unused import
jopo666 Mar 13, 2023
bcf2323
process -> utils
jopo666 Mar 13, 2023
ae838fb
new build
jopo666 Mar 13, 2023
046a459
bugfix to overlap index
jopo666 Mar 13, 2023
0db5f6d
simplify torch dataset
jopo666 Mar 14, 2023
fc74c4b
better name & typo fixes
jopo666 Mar 14, 2023
d99bd83
new build
jopo666 Mar 14, 2023
ffd4672
big decoupling, reformatting
jopo666 Mar 16, 2023
df7de8a
update backend tests
jopo666 Mar 16, 2023
a2a2330
update tests
jopo666 Mar 16, 2023
4dc14d2
new test data
jopo666 Mar 16, 2023
36faf5d
rename and simplify
jopo666 Mar 16, 2023
4848a5d
new test data
jopo666 Mar 16, 2023
9808b14
add spot number to filename, simplify backend loading
jopo666 Mar 16, 2023
8a9d507
make name and path properties
jopo666 Mar 16, 2023
3e6ef66
update repr
jopo666 Mar 16, 2023
b7ac6fe
bugfixes
jopo666 Mar 16, 2023
87d2f13
update error msg
jopo666 Mar 16, 2023
f3ca2e7
update norm tests; normalize_stains to private
jopo666 Mar 16, 2023
01ace7d
add image to utils and take better crops
jopo666 Mar 16, 2023
2068fef
update tissue test
jopo666 Mar 16, 2023
72d56f5
update metric tests
jopo666 Mar 16, 2023
a5f7573
update dearray tests
jopo666 Mar 16, 2023
2c2eb26
update metadata and tests
jopo666 Mar 16, 2023
6c38a53
update import and tissue mask
jopo666 Mar 16, 2023
77b5aca
update tile tests
jopo666 Mar 16, 2023
12c7c14
remove non_overlapping_regions
jopo666 Mar 16, 2023
d71a4be
update cli & tests & deps; new build
jopo666 Mar 16, 2023
dd6b368
add raise exception to mean and std
jopo666 Mar 17, 2023
59477e7
bugfix
jopo666 Mar 17, 2023
9a8a91b
add tile image dataset
jopo666 Mar 18, 2023
9702f81
add tissue mask thumbnail
jopo666 Mar 18, 2023
5c4d6ac
fix depracation warning
jopo666 Mar 18, 2023
45244e5
new build
jopo666 Mar 21, 2023
bbeda1b
fix typo
jopo666 Mar 21, 2023
90be39c
more typo fixes
jopo666 Mar 21, 2023
be8c88c
bugfixes
jopo666 Mar 21, 2023
d0bde7f
add mean_and_std from paths/images
jopo666 Mar 22, 2023
3c7e4cd
image collage to functionals; change n_* -> num_*
jopo666 Mar 24, 2023
11f2232
fix cli test
jopo666 Mar 24, 2023
5f1d898
new build
jopo666 Mar 24, 2023
59f2f6c
pass kwargs to scatter
jopo666 Apr 4, 2023
fa49f85
fix overlap area
jopo666 Apr 4, 2023
afe8f5e
nicer error message
jopo666 Apr 4, 2023
79a6a31
rename functions and improve docstrings
jopo666 Apr 11, 2023
98867b6
rename openslide formats
jopo666 Apr 11, 2023
15a2895
fix typos
jopo666 Apr 11, 2023
12556a0
update docstrings
jopo666 Apr 11, 2023
d6bf21e
add suffix, rename basebackend, update docstrings
jopo666 Apr 11, 2023
8cec077
update docstrings
jopo666 Apr 11, 2023
2ca1d22
update docstrings
jopo666 Apr 12, 2023
b226863
add coordinates
jopo666 Apr 12, 2023
e3e0e59
update readme
jopo666 Apr 12, 2023
044c834
move backend to a single file
jopo666 Apr 12, 2023
fa39ad1
split torch to submodule
jopo666 Apr 12, 2023
0f0500b
split stain norm to submodule
jopo666 Apr 12, 2023
0f49c7b
metadata -> process submodule
jopo666 Apr 12, 2023
18027d2
move functionals to utils normalize
jopo666 Apr 12, 2023
80f4a92
reformat for api documentation
jopo666 Apr 12, 2023
3ac7ec1
generate api docs and update gitignore
jopo666 Apr 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add mean_and_std from paths/images
  • Loading branch information
jopo666 committed Mar 22, 2023
commit d0bde7ff57206419e9ebe189bd5edd4f7c7b8eb9
Binary file modified dist/histoprep-2.0.1a0-py3-none-any.whl
Binary file not shown.
Binary file modified dist/histoprep-2.0.1a0.tar.gz
Binary file not shown.
8 changes: 2 additions & 6 deletions histoprep/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,8 @@ def get_mean_and_std(
num_workers=num_workers,
return_exception=not raise_exception,
)
return F._get_mean_and_std(
images=(
(tile, xywh)
for tile, xywh in iterable
if not isinstance(tile, Exception)
)
return F.get_mean_and_std_from_images(
images=(tile for tile, __ in iterable if not isinstance(tile, Exception))
)

def save_regions(
Expand Down
76 changes: 64 additions & 12 deletions histoprep/functional/_mean_std.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,64 @@
__all__ = ["_get_mean_and_std"]
from __future__ import annotations

__all__ = [
"get_mean_and_std_from_images",
"get_mean_and_std_from_paths",
"_get_mean_and_std",
]

from collections.abc import Iterable
from pathlib import Path

import mpire
import numpy as np
from PIL import Image

from ._check import check_image

MEAN = tuple[float, ...]
STD = tuple[float, ...]


def get_mean_and_std_from_images(images: Iterable[np.ndarray]) -> tuple[MEAN, STD]:
"""Get channel mean and std values for an iterable of images.

Args:
images: Iterable of array images.
verbose: Enable progress bar. Defaults to False.

Returns:
Tuple of channel mean and std values.
"""
mean, std = [], []
for img in images:
m, s = _get_mean_and_std(img)
mean.append(m)
std.append(s)
return tuple(np.array(mean).mean(0)), tuple(np.array(std).mean(0))


def _get_mean_and_std(
images: Iterable[np.ndarray],
) -> tuple[tuple[float, ...], tuple[float, ...]]:
def get_mean_and_std_from_paths(
filepaths: Iterable[str | Path], num_workers: int = 1
) -> tuple[MEAN, STD]:
"""Get channel mean and std values for an iterable of image paths.

Args:
images: Iterable of array image paths.
num_workers: Number of image reading processes.

Returns:
Tuple of channel mean and std values.
"""
if num_workers <= 1:
return get_mean_and_std_from_images(_read_image(x) for x in filepaths)
with mpire.WorkerPool(n_jobs=num_workers) as pool:
images = pool.imap(
_read_image, ((x,) for x in filepaths), iterable_len=len(filepaths)
)
return get_mean_and_std_from_images(images)


def _get_mean_and_std(image: np.ndarray) -> tuple[MEAN, STD]:
"""Calculate mean and standard deviation for each image channel (between [0, 1]).

Args:
Expand All @@ -18,11 +67,14 @@ def _get_mean_and_std(
Returns:
Mean and standard deviation for each channel.
"""
mean, std = [], []
for image, __ in images:
check_image(image)
if image.ndim == 2: # noqa
image = np.expand_dims(tile, -1) # noqa
mean.append([image[..., i].mean() for i in range(image.shape[-1])])
std.append([image[..., i].std() for i in range(image.shape[-1])])
return tuple(np.array(mean).mean(0) / 255), tuple(np.array(std).mean(0) / 255)
check_image(image)
if image.ndim == 2: # noqa
image = np.expand_dims(tile, -1) # noqa
return (
tuple([image[..., i].mean() / 255 for i in range(image.shape[-1])]),
tuple([image[..., i].std() / 255 for i in range(image.shape[-1])]),
)


def _read_image(path: str | Path) -> np.ndarray:
return np.array(Image.open(path))
25 changes: 25 additions & 0 deletions tests/mean_and_std_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import histoprep.functional as F
from histoprep import SlideReader

from ._utils import SLIDE_PATH_JPEG, TMP_DIRECTORY, clean_temporary_directory


def test_mean_and_std_from_images() -> None:
reader = SlideReader(SLIDE_PATH_JPEG)
tile_coords = reader.get_tile_coordinates(None, 512)
images = (tile for tile, __ in reader.yield_regions(tile_coords))
mean, std = F.get_mean_and_std_from_images(images)
assert [round(x, 2) for x in mean] == [0.84, 0.70, 0.78]
assert [round(x, 2) for x in std] == [0.14, 0.19, 0.14]


def test_mean_and_std_from_paths() -> None:
reader = SlideReader(SLIDE_PATH_JPEG)
tile_coords = reader.get_tile_coordinates(None, 512)
clean_temporary_directory()
reader.save_regions(TMP_DIRECTORY, tile_coords)
paths = list((TMP_DIRECTORY / "slide" / "tiles").iterdir())
mean, std = F.get_mean_and_std_from_paths(paths)
clean_temporary_directory()
assert [round(x, 2) for x in mean] == [0.84, 0.70, 0.78]
assert [round(x, 2) for x in std] == [0.14, 0.19, 0.14]
4 changes: 2 additions & 2 deletions tests/reader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,5 +472,5 @@ def test_save_regions_with_metrics() -> None:
def test_estimate_mean_and_std() -> None:
reader = SlideReader(SLIDE_PATH_JPEG)
mean, std = reader.get_mean_and_std(reader.get_tile_coordinates(None, 512))
assert mean == (0.8447404647527956, 0.7014915950999541, 0.779204397164139)
assert std == (0.1367226593250863, 0.18658047647561957, 0.1402206641594302)
assert [round(x, 2) for x in mean] == [0.84, 0.70, 0.78]
assert [round(x, 2) for x in std] == [0.14, 0.19, 0.14]