Skip to content

Commit

Permalink
Updates to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasteuwen committed Sep 2, 2024
1 parent 85f90f9 commit 001c3e9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 110 deletions.
104 changes: 11 additions & 93 deletions examples/annotations_to_mask.py
Original file line number Diff line number Diff line change
@@ -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()
8 changes: 1 addition & 7 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions tests/test_slide_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 231 in tests/test_slide_annotations.py

View check run for this annotation

Codecov / codecov/patch

tests/test_slide_annotations.py#L228-L231

Added lines #L228 - L231 were not covered by tests

for polygon in halo_annotations.layers.polygons:
polygon.index = 1

Check warning on line 234 in tests/test_slide_annotations.py

View check run for this annotation

Codecov / codecov/patch

tests/test_slide_annotations.py#L234

Added line #L234 was not covered by tests
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

Check warning on line 239 in tests/test_slide_annotations.py

View check run for this annotation

Codecov / codecov/patch

tests/test_slide_annotations.py#L236-L239

Added lines #L236 - L239 were not covered by tests

def test_reexpert_dlup_xml(self):
Expand Down
6 changes: 1 addition & 5 deletions third_party/meson.build
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit 001c3e9

Please sign in to comment.