diff --git a/examples/annotations_to_mask.py b/examples/annotations_to_mask.py index 78a3ae2a..c8c8f35d 100644 --- a/examples/annotations_to_mask.py +++ b/examples/annotations_to_mask.py @@ -1,108 +1,26 @@ # Copyright (c) dlup contributors """This code provides an example of how to convert annotations to a mask.""" -import json from pathlib import Path -import numpy as np import PIL.Image from dlup.annotations_experimental import SlideAnnotations -d_fn = Path("TCGA-E9-A1R4-01Z-00-DX1.B04D5A22-8CE5-49FD-8510-14444F46894D.json") -Z_INDICES = { - "tissue (area)": 0, - "artefact mechanical expansion (area)": 1, - "artefact out of focus (area)": 2, - "artefact edge margin ink (area)": 3, - "artefact mechanical compression (area)": 3, - "artefact other (area)": 4, - "artefact air bubble (area)": 5, - "artefact foreign object (area)": 5, - "artefact coverslip (area)": 6, - "artefact pen marking (area)": 7, -} +def convert_annotations_to_mask() -> None: + scaling = 0.02 + annotations = SlideAnnotations.from_dlup_xml(Path(__file__).parent / "files" / "dlup_annotation_test.xml") -index_map = { - "tissue (area)": 1, - "artefact air bubble (area)": 2, - "artefact mechanical expansion (area)": 3, - "artefact mechanical compression (area)": 4, - "artefact out of focus (area)": 5, - "artefact pen marking (area)": 6, -} -annotations = SlideAnnotations.from_darwin_json(d_fn, z_indices=Z_INDICES, sorting="Z_INDEX") -scaling = 0.02 + bbox = annotations.bounding_box_at_scaling(scaling) -bbox = annotations.bounding_box_at_scaling(scaling) -annotations.reindex_polygons(index_map) -region = annotations.read_region((0, 0), scaling, bbox[1]) -LUT = annotations.color_lut -import time + region = annotations.read_region((0, 0), scaling, bbox[1]) + LUT = annotations.color_lut -start_time = time.time() -curr_mask = region.polygons_eager.to_mask() -print(f"Time to compute mask eagerly: {time.time() - start_time}") + bbox = annotations.bounding_box_at_scaling(scaling) + curr_mask = region.polygons.to_mask().numpy() + print(curr_mask.shape) + PIL.Image.fromarray(LUT[curr_mask]).save("output.png") -print(region, "region") -print(region.polygons, "region.polygons") # This should be lazy -# for polygon in region.polygons.get_geometries(): -# print(polygon) -polys = region.polygons.get_geometries() # This should start computing - -import time - -start_time = time.time() -curr_mask = region.polygons.to_mask().numpy() -print(f"Time to compute mask lazily: {time.time() - start_time}") - - -print(curr_mask) -print(np.asarray(curr_mask).shape) - -mask_itself = region.polygons.to_mask().numpy() - -mask = LUT[mask_itself] - -PIL.Image.fromarray(mask).save("mask.png") -from dlup.geometry import Box, GeometryCollection - -collection = GeometryCollection() -polygon = Box((1, 1), (4, 4)).as_polygon() -polygon.index = 2 -collection.add_polygon(polygon) - -region = collection.read_region((0, 0), 1.0, (5, 5)) - -print("Python: Getting geometries") -# region.polygons.get_geometries() -print("Python: got geometries") -print("Python: Computing mask") -mask = np.asarray(region.polygons.to_mask()) -print("Python: Got mask") -# print(mask) -# assert mask.sum() == 16 * 2 -print("Python: Done") -mask = np.asarray(region.polygons.to_mask()) - - -# print("Getting geometries") - -# # for polygon in region.polygons.get_geometries(): -# # print(polygon) - -# with open("test.xml", "w") as f: -# f.write(annotations.as_dlup_xml()) - - -# with open("test.geojson", "w") as f: -# f.write(json.dumps(annotations.as_geojson(), indent=2)) - -# annotations2 = SlideAnnotations.from_dlup_xml("test.xml") -# region2 = annotations2.read_region((0, 0), scaling, bbox[1]) -# LUT = annotations2.color_lut - -# mask = LUT[region.polygons.to_mask().numpy()] -# PIL.Image.fromarray(mask).save("mask2.png") +convert_annotations_to_mask() diff --git a/meson.build b/meson.build index b0d0a144..da01a7cc 100644 --- a/meson.build +++ b/meson.build @@ -4,14 +4,8 @@ project('dlup', 'cpp', 'cython', ninja = find_program('ninja', required : true) -# Import Python module - py_mod = import('python') -if host_machine.system() == 'darwin' - py = py_mod.find_installation(pure: false) -else - py = py_mod.find_installation() -endif +py = py_mod.find_installation(pure: false) py_dep = py.dependency() # Base compiler and linker arguments diff --git a/tests/test_slide_annotations.py b/tests/test_slide_annotations.py index bf5e8636..0f9922dc 100644 --- a/tests/test_slide_annotations.py +++ b/tests/test_slide_annotations.py @@ -225,15 +225,17 @@ def test_conversion_halo_geojson(self): def test_halo_annotations(self): halo_annotations = self.halo_annotations.copy() - offset, _ = halo_annotations.bounding_box - assert halo_annotations.bounding_box[0] == (-29349.0, 50000.55808864343) + bounding_box = halo_annotations.bounding_box + assert bounding_box[0] == (-29349.0, 50000.55808864343) halo_annotations.set_offset((29349.0, -50000.55808864343)) assert halo_annotations.bounding_box[0] == (0, 0) + for polygon in halo_annotations.layers.polygons: polygon.index = 1 - halo_mask = halo_annotations.read_region((0, 0), 0.01, (522, 374)).polygons.to_mask() - output_color_mask = halo_annotations.color_lut[halo_mask] - assert halo_mask.sum() == 87709 + + new_bbox = halo_annotations.bounding_box_at_scaling(0.01) + region = halo_annotations.read_region(new_bbox[0], 0.01, new_bbox[1]) + output_color_mask = halo_annotations.color_lut[region.polygons.to_mask().numpy()] assert output_color_mask.sum() == 51485183 def test_reexpert_dlup_xml(self): diff --git a/third_party/meson.build b/third_party/meson.build index 6baf2fba..293b5ab6 100644 --- a/third_party/meson.build +++ b/third_party/meson.build @@ -1,11 +1,7 @@ ### third_party/meson.build ### py_mod = import('python') -if host_machine.system() == 'darwin' - py = py_mod.find_installation(pure: false) -else - py = py_mod.find_installation() -endif +py = py_mod.find_installation(pure: false) py_dep = py.dependency() python_path = run_command(py, ['-c', 'import sys; print(sys.executable)'], check: true).stdout().strip()