Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasteuwen committed Aug 31, 2024
1 parent b9405b4 commit a0ffb8a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .spin/cmds.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
import site
import subprocess
import webbrowser
from pathlib import Path
import os

import click
from spin.cmds import meson

Expand Down
2 changes: 1 addition & 1 deletion dlup/annotations_experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ def as_dlup_xml(
extra_annotation_params["tags"] = tags

dlup_annotations = XMLDlupAnnotations(metadata=metadata, geometries=geometries, **extra_annotation_params)
config = SerializerConfig(indent=indent)
config = SerializerConfig(pretty_print=True)
serializer = XmlSerializer(config=config)
return serializer.render(dlup_annotations)

Expand Down
12 changes: 11 additions & 1 deletion examples/annotations_to_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"""This code provides an example of how to convert annotations to a mask."""
import json
from pathlib import Path

import PIL.Image

from dlup.annotations_experimental import SlideAnnotations

d_fn = Path("TCGA-E9-A1R4-01Z-00-DX1.B04D5A22-8CE5-49FD-8510-14444F46894D.json")
Expand Down Expand Up @@ -36,10 +38,18 @@
region = annotations.read_region((0, 0), scaling, bbox[1])
LUT = annotations.color_lut
print(region.polygons)

print("Getting geometries")

for polygon in region.polygons.get_geometries():
print(polygon)

mask = LUT[region.polygons.to_mask()]
PIL.Image.fromarray(mask).save("mask.png")

for polygon in region.polygons.as_geometries():
print("Getting geometries")

for polygon in region.polygons.get_geometries():
print(polygon)

with open("test.xml", "w") as f:
Expand Down
1 change: 0 additions & 1 deletion src/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ PYBIND11_MODULE(_geometry, m) {
.def_property_readonly("boxes", &AnnotationRegion::getBoxes)
.def_property_readonly("points", &AnnotationRegion::getPoints);


py::register_exception<GeometryError>(m, "GeometryError");
py::register_exception<GeometryIntersectionError>(m, "GeometryIntersectionError");
py::register_exception<GeometryTransformationError>(m, "GeometryTransformationError");
Expand Down
44 changes: 44 additions & 0 deletions src/geometry/polygon_collection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef DLUP_POLYGON_COLLECTION_H
#define DLUP_POLYGON_COLLECTION_H
#pragma once

#include "factory.h"
#include <mutex>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>

class Polygon;
class Box;
class Point;

class PolygonCollection {
public:
PolygonCollection(std::vector<std::shared_ptr<Polygon>> polygons, std::tuple<int, int> mask_size)
: polygons_(std::move(polygons)), mask_size_(std::move(mask_size)) {}

std::vector<py::object> getGeometries() const {
std::vector<py::object> py_objects;
py_objects.reserve(polygons_.size());
for (const auto &polygon : polygons_) {
py_objects.push_back(FactoryManager<Polygon>::callFactoryFunction(polygon));
}
return py_objects;
}

py::array_t<int> toMask(int default_value = 0) const {
auto mask = generateMaskFromAnnotations(polygons_, mask_size_, default_value);

int width = std::get<0>(mask_size_);
int height = std::get<1>(mask_size_);

return py::array_t<int>({height, width}, mask->data());
}

private:
std::vector<std::shared_ptr<Polygon>> polygons_;
std::tuple<int, int> mask_size_;
};

#endif // DLUP_POLYGON_COLLECTION_H
39 changes: 6 additions & 33 deletions src/geometry/region.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once

#include "factory.h"
#include "polygon_collection.h"
#include <mutex>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
Expand All @@ -13,34 +14,6 @@ class Polygon;
class Box;
class Point;

class PolygonCollection {
public:
PolygonCollection(std::vector<std::shared_ptr<Polygon>> polygons, std::tuple<int, int> mask_size)
: polygons_(std::move(polygons)), mask_size_(std::move(mask_size)) {}

std::vector<py::object> getGeometries() const {
std::vector<py::object> py_objects;
py_objects.reserve(polygons_.size());
for (const auto &polygon : polygons_) {
py_objects.push_back(FactoryManager<Polygon>::callFactoryFunction(polygon));
}
return py_objects;
}

py::array_t<int> toMask(int default_value = 0) const {
auto mask = generateMaskFromAnnotations(polygons_, mask_size_, default_value);

int width = std::get<0>(mask_size_);
int height = std::get<1>(mask_size_);

return py::array_t<int>({height, width}, mask->data());
}

private:
std::vector<std::shared_ptr<Polygon>> polygons_;
std::tuple<int, int> mask_size_;
};

template <typename T>
class AnnotationRegionBase {
public:
Expand All @@ -64,17 +37,17 @@ class AnnotationRegionBase {
class AnnotationRegion {
public:
AnnotationRegion(std::function<AnnotationRegion()> region_generator)
: region_generator_(region_generator), initialized_(false), polygon_region_({}, {0, 0}), point_region_({}),
: region_generator_(region_generator), initialized_(false), polygon_collection_({}, {0, 0}), point_region_({}),
box_region_({}) {}

AnnotationRegion(std::vector<std::shared_ptr<Polygon>> polygons, std::vector<std::shared_ptr<Box>> boxes,
std::vector<std::shared_ptr<Point>> points, std::tuple<int, int> mask_size)
: polygon_region_(std::move(polygons), std::move(mask_size)), box_region_(std::move(boxes)),
: polygon_collection_(std::move(polygons), std::move(mask_size)), box_region_(std::move(boxes)),
point_region_(std::move(points)), initialized_(true) {}

PolygonCollection getPolygons() {
ensureInitialized();
return polygon_region_;
return polygon_collection_;
}

std::vector<py::object> getPoints() {
Expand All @@ -91,7 +64,7 @@ class AnnotationRegion {
void ensureInitialized() {
if (!initialized_) {
AnnotationRegion generated_region = region_generator_();
polygon_region_ = std::move(generated_region.polygon_region_);
polygon_collection_ = std::move(generated_region.polygon_collection_);
point_region_ = std::move(generated_region.point_region_);
box_region_ = std::move(generated_region.box_region_);
initialized_ = true;
Expand All @@ -100,7 +73,7 @@ class AnnotationRegion {

std::function<AnnotationRegion()> region_generator_;
bool initialized_;
PolygonCollection polygon_region_;
PolygonCollection polygon_collection_;
AnnotationRegionBase<Point> point_region_;
AnnotationRegionBase<Box> box_region_;
};
Expand Down
1 change: 0 additions & 1 deletion src/opencv.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <unordered_map>
#include <vector>


std::shared_ptr<std::vector<int>> generateMaskFromAnnotations(const std::vector<std::shared_ptr<Polygon>> &annotations,
const std::tuple<int, int> &mask_size,
int default_value) {
Expand Down

0 comments on commit a0ffb8a

Please sign in to comment.