Skip to content

Commit

Permalink
1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cdgatenbee committed Oct 6, 2023
1 parent 2bdf3fd commit edc9dfa
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 27 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ examples/expected_results/registered_slides
tests/tmp39
tests/tmp310
tests/tmp311
tests/39
tests/310
tests/311
tests/docker
tests/test_difficult_cases.py
tests/test_read_czi_jpgxr.py
tests/test_tiler.py
Expand All @@ -197,3 +201,11 @@ Dockerfile_torch_debian
torch_pyproject.toml

tests/test_micro_rigid.py

pip_poetry.lock

pip_pyproject.toml

docker_run_tests.sh

docker_run_tests_by_line.sh
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
**********

Version 1.0.1 (October 6, 2023)
-------------------------------------
#. Bug fixes to functions related to saving slides as ome.tiff
#. Address numba deprecation warnings

Version 1.0.0 (October 4, 2023)
-------------------------------------
#. Added option for high resolution rigid registration using the :code:`micro_rigid_registrar.MicroRigidRegistrar` class. To use this option, pass an uninstantiated :code:`micro_rigid_registrar.MicroRigidRegistrar` to :code:`micro_rigid_registrar_cls` when initializing the :code:`Valis` object. This class refines the rigid registration by detecting and matching features in higher resolution images warped using the initial rigid transforms. This should result in more accurate rigid registration, error estimates, and hopefully fewer unwanted non-rigid deformations.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "valis-wsi"
version = "1.0.0"
version = "1.0.1"
description = ""
authors = ["Chandler Gatenbee <[email protected]>"]
license = "MIT"
Expand Down
30 changes: 18 additions & 12 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

import shutil
import sys
import os
# sys.path.append("/Users/gatenbcd/Dropbox/Documents/image_processing/valis_project/valis")
from valis import registration, valtils
from valis.micro_rigid_registrar import MicroRigidRegistrar
Expand All @@ -74,11 +75,15 @@ def cnames_from_filename(src_f):
f = valtils.get_name(src_f)
return ["DAPI"] + f.split(" ")

parent_dir = get_parent_dir()
# parent_dir = get_parent_dir()
parent_dir = "/Users/gatenbcd/Dropbox/Documents/image_processing/valis_project"
datasets_src_dir = os.path.join(parent_dir, "valis/examples/example_datasets/")
results_dst_dir = os.path.join(parent_dir, f"valis/tests/tmp{sys.version_info.major}{sys.version_info.minor}")


in_container = sys.platform == "linux" and os.getcwd() == '/usr/local/src'
if in_container:
results_dst_dir = os.path.join(parent_dir, f"valis/tests/docker")
else:
results_dst_dir = os.path.join(parent_dir, f"valis/tests/{sys.version_info.major}{sys.version_info.minor}")

def register_hi_rez(src_dir):
high_rez_dst_dir = os.path.join(results_dst_dir, "high_rez")
Expand Down Expand Up @@ -108,9 +113,9 @@ def register_hi_rez(src_dir):
registrar.draw_matches(matches_dst_dir)


def register_ihc(max_error=45):
def test_register_ihc(max_error=50):
"""Tests registration and lossy jpeg2000 compression"""
ihc_src_dir = os.path.join(datasets_src_dir, "ihc")
ihc_dst_dir = os.path.join(results_dst_dir, "ihc")
try:
registrar = registration.Valis(ihc_src_dir, results_dst_dir)
rigid_registrar, non_rigid_registrar, error_df = registrar.register()
Expand All @@ -122,8 +127,7 @@ def register_ihc(max_error=45):
assert False, f"error was {avg_error} but should be below {max_error}"

registered_slide_dst_dir = os.path.join(registrar.dst_dir, "registered_slides", registrar.name)
registrar.warp_and_save_slides(registered_slide_dst_dir)
# registration.kill_jvm()
registrar.warp_and_save_slides(dst_dir=registered_slide_dst_dir, Q=90, compression="jp2k")

# shutil.rmtree(ihc_dst_dir, ignore_errors=True)

Expand All @@ -138,7 +142,6 @@ def test_register_cycif(max_error=3):


cycif_src_dir = os.path.join(datasets_src_dir, "cycif")
cycif_dst_dir = os.path.join(results_dst_dir, "cycif")
try:
registrar = registration.Valis(cycif_src_dir, results_dst_dir)
rigid_registrar, non_rigid_registrar, error_df = registrar.register()
Expand All @@ -155,7 +158,8 @@ def test_register_cycif(max_error=3):
dst_f = os.path.join(registrar.dst_dir, "registered_slides", f"{registrar.name}.ome.tiff")
merged_img, channel_names, ome_xml = registrar.warp_and_merge_slides(dst_f,
channel_name_dict=channel_name_dict,
drop_duplicates=True)
drop_duplicates=True,
Q=90)

# registration.kill_jvm()

Expand All @@ -178,6 +182,8 @@ def test_register_hi_rez_cycif():
register_hi_rez(src_dir=cycif_src_dir)


# if __name__ == "__main__":
# test_register_hi_rez_ihc()
# test_register_hi_rez_cycif()
if __name__ == "__main__" and in_container:
# test_register_cycif()
# test_register_ihc()
test_register_hi_rez_ihc()
test_register_hi_rez_cycif()
4 changes: 4 additions & 0 deletions tests/test_warp_fxns.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,7 @@ def test_img_warp(max_mi=0.5, max_px_d=0.1):
passed = pt_d < max_px_d and mi < max_mi

assert passed


if __name__ == "__main__":
test_img_warp()
2 changes: 1 addition & 1 deletion valis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.0.0"
__version__ = "1.0.1"

from . import affine_optimizer
from . import feature_detectors
Expand Down
Binary file modified valis/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file modified valis/__pycache__/affine_optimizer.cpython-39.pyc
Binary file not shown.
Binary file modified valis/__pycache__/registration.cpython-39.pyc
Binary file not shown.
Binary file modified valis/__pycache__/valtils.cpython-39.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions valis/affine_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def update_joint_H(binned_moving, binned_fixed, H, M, sample_pts, pos=0,
return H


@nba.jit(nopython=False)
# @nba.jit(nopython=False)
def get_neighborhood(im, i, j, r):
"""
Get values in a neighborhood
Expand All @@ -317,7 +317,7 @@ def get_neighborhood(im, i, j, r):
return im[i - r:i + r + 1, j - r:j + r + 1].flatten()


@nba.jit(nopython=False)
# @nba.jit(nopython=False)
def build_P(A, B, r, mask):
hood_size = (2 * r + 1) ** 2
d = 2 * hood_size
Expand Down
28 changes: 18 additions & 10 deletions valis/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from copy import deepcopy
from pprint import pformat
import json
from colorama import Fore

from . import feature_matcher
from . import serial_rigid
Expand Down Expand Up @@ -973,6 +974,8 @@ def warp_and_save_slide(self, dst_f, level=0, non_rigid=True,
Q factor for lossy compression
"""
if src_f is None:
src_f = self.src_f

warped_slide = self.warp_slide(level=level, non_rigid=non_rigid,
crop=crop,
Expand Down Expand Up @@ -2103,10 +2106,10 @@ def get_imgs_in_dir(self):

elif len(matching_f) > 1:
msg = f"found {len(matching_f)} matches for {dir_name}: {', '.join(matching_f)}"
valtils.print_warning(msg)
valtils.print_warning(msg, rgb=Fore.RED)
elif len(matching_f) == 0:
msg = f"Can't find slide file associated with {dir_name}"
valtils.print_warning(msg)
valtils.print_warning(msg, rgb=Fore.RED)

def set_dst_paths(self):
"""Set paths to where the results will be saved.
Expand Down Expand Up @@ -2189,7 +2192,7 @@ def get_slide(self, src_f):
f"the dictionary. Either key (filenmae) or value (assigned name) will work:\n"
f"{pformat(possible_names_dict)}")

valtils.print_warning(msg)
valtils.print_warning(msg, rgb=Fore.RED)
slide_obj = None

return slide_obj
Expand Down Expand Up @@ -2233,7 +2236,7 @@ def get_img_names(self, img_list):
z = len(str(len(dup_paths)))

msg = f"Detected {len(dup_paths)} images that would be named {dup_name}"
valtils.print_warning(msg)
valtils.print_warning(msg, rgb=Fore.RED)

for j, p in enumerate(dup_paths):
new_name = f"{names_dict[p]}_{str(j).zfill(z)}"
Expand Down Expand Up @@ -2279,15 +2282,15 @@ def convert_imgs(self, series=None, reader_cls=None):
slide_reader_cls = slide_io.get_slide_reader(f, series=series)
except Exception as e:
msg = f"Attempting to get reader for {f} created the following error:\n{e}"
valtils.print_warning(msg)
valtils.print_warning(msg, rgb=Fore.RED)
else:
slide_reader_cls = reader_cls

try:
reader = slide_reader_cls(f, series=series)
except Exception as e:
msg = f"Attempting to read {f} created the following error:\n{e}"
valtils.print_warning(msg)
valtils.print_warning(msg, rgb=Fore.RED)

slide_dims = reader.metadata.slide_dimensions
levels_in_range = np.where(slide_dims.max(axis=1) < self.max_image_dim_px)[0]
Expand Down Expand Up @@ -2994,7 +2997,7 @@ def rigid_register(self):

if rigid_registrar is False:
msg = "Rigid registration failed"
valtils.print_warning(msg)
valtils.print_warning(msg, rgb=Fore.RED)

return False

Expand Down Expand Up @@ -4147,7 +4150,7 @@ def register(self, brightfield_processing_cls=DEFAULT_BRIGHTFIELD_CLASS,
error_df.to_csv(data_f_out, index=False)

except Exception as e:
valtils.print_warning(e)
valtils.print_warning(e, rgb=Fore.RED)
print(traceback.format_exc())
kill_jvm()
return None, None, None
Expand Down Expand Up @@ -4451,7 +4454,7 @@ def warp_and_save_slides(self, dst_dir, level=0, non_rigid=True,
crop=True,
colormap=None,
interp_method="bicubic",
tile_wh=None, compression="lzw"):
tile_wh=None, compression="lzw", Q=100):

f"""Warp and save all slides
Expand Down Expand Up @@ -4492,6 +4495,9 @@ def warp_and_save_slides(self, dst_dir, level=0, non_rigid=True,
Compression method used to save ome.tiff . Default is lzw, but can also
be jpeg or jp2k. See pyips for more details.
Q : int
Q factor for lossy compression
"""
pathlib.Path(dst_dir).mkdir(exist_ok=True, parents=True)

Expand All @@ -4508,12 +4514,14 @@ def warp_and_save_slides(self, dst_dir, level=0, non_rigid=True,
valtils.print_warning(msg)

dst_f = os.path.join(dst_dir, slide_obj.name + ".ome.tiff")

slide_obj.warp_and_save_slide(dst_f=dst_f, level=level,
non_rigid=non_rigid,
crop=crop,
src_f=slide_obj.src_f,
interp_method=interp_method,
colormap=slide_cmap,
tile_wh=tile_wh, compression=compression)
tile_wh=tile_wh, compression=compression, Q=Q)

@valtils.deprecated_args(perceputally_uniform_channel_colors="colormap")
def warp_and_merge_slides(self, dst_f=None, level=0, non_rigid=True,
Expand Down
2 changes: 1 addition & 1 deletion valis/valtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
color_init()


def print_warning(msg, warning_type=UserWarning, rgb=Fore.RED):
def print_warning(msg, warning_type=UserWarning, rgb=Fore.YELLOW):
"""Print warning message with color
"""
warning_msg = f"{rgb}{msg}{Style.RESET_ALL}"
Expand Down

0 comments on commit edc9dfa

Please sign in to comment.