kaczmarj / tumor-microenvironment-analysis

Code related to the analysis of tumor microenvironment in digital whole slide images.
0 stars 0 forks source link

do not consider boundaries inside a tumor #11

Closed kaczmarj closed 3 years ago

kaczmarj commented 3 years ago

we should only be considering distances from the edge of the tumor.

kaczmarj commented 3 years ago

here's one way we can tackle this problem... we get the exterior of the unioned tumor multipolygon. we convert the exterior of the biomarker-positive multipolygon to a multilinestring, and we keep all of the lines that are contained in the tumor exterior. we do the same with biomarker-negative.

import shapely.geometry

def get_exterior_of_multigeom(multigeom):
    return shapely.geometry.MultiLineString([g.exterior for g in multigeom.geoms])

def exterior_to_multilinestring(exterior):
    lines = []
    for t in exterior:
        lines.extend(zip(t.coords[:-1], t.coords[1:]))
    lines = shapely.geometry.MultiLineString(lines)
    return lines

def get_exterior(multigeom, big_geom):
    exterior = get_exterior_of_multigeom(multigeom)
    lines = exterior_to_multilinestring(exterior)
    big_geom_ext = get_exterior_of_multigeom(big_geom)
    big_geom_ext = big_geom_ext.buffer(1)
    # big_geom_lines = exterior_to_multilinestring(big_geom_ext)
    lines = shapely.geometry.MultiLineString(
        [g for g in lines if big_geom_ext.contains(g)])
    return lines

mpos_edges = get_exterior(mpos_geoms, tumor_geoms)
mneg_edges = get_exterior(mneg_geoms, tumor_geoms)