def setup_labels(self, size=None, color=None, shadow=None):
        """Sets up coordinates for labels wrt SVG file (2D flatmap)"""
        # Recursive call for multiple layers
        if self.layer == 'multi_layer':
            label_layers = []
            for L in self.layer_names:
                label_layers.append(self.layers[L].setup_labels())
                self.svg.getroot().insert(0, label_layers[-1])
            return label_layers
        if self.layer in config.sections():
            dlayer = self.layer
        else:
            # Unknown display layer; default to values for ROIs
            import warnings
            warnings.warn('No defaults set for display layer %s; Using defaults for ROIs in options.cfg file'%self.layer)
            dlayer = 'rois'                
        if size is None:
            size = config.get(dlayer, "labelsize")
        if color is None:
            color = tuple(map(float, config.get(dlayer, "labelcolor").split(",")))
        if shadow is None:
            shadow = self.shadow

        alpha = color[3]
        color = "rgb(%d, %d, %d)"%(color[0]*255, color[1]*255, color[2]*255)

        try:
            layer = _find_layer(self.svg, "%s_labels"%self.layer)
        except __HOLE__: # Changed in _find_layer below... AssertionError: # Why assertion error? 
            layer = _make_layer(self.svg.getroot(), "%s_labels"%self.layer)

        labelpos, candidates = [], []
        for roi in list(self.rois.values()):
            for i, pos in enumerate(roi.get_labelpos()):
                labelpos.append(pos)
                candidates.append((roi, i))

        w, h = self.svgshape
        nolabels = set(candidates)
        txtstyle = "font-family:sans;font-size:%s;font-weight:bold;font-style:italic;fill:%s;fill-opacity:%f;text-anchor:middle;"%(size, color, alpha)
        for text in layer.findall(".//{%s}text"%svgns):
            x = float(text.get('x'))
            y = float(text.get('y'))
            text.attrib['style'] = txtstyle
            text.attrib['data-ptidx'] = str(self.kdt.query((x / w, 1-(y / h)))[1])
            pts, cand = [], []
            for p, c in zip(labelpos, candidates):
                if c[0].name == text.text:
                    pts.append((p[0]*w, (1-p[1])*h))
                    cand.append(c)
            d, idx = cKDTree(pts).query((x,y))
            nolabels.remove(cand[idx])

        for roi, i in nolabels:
            x, y = roi.get_labelpos()[i]
            text = etree.SubElement(layer, "{%s}text"%svgns)
            text.text = roi.name
            text.attrib["x"] = str(x*w)
            text.attrib["y"] = str((1-y)*h)
            if self.shadow > 0:
                text.attrib['filter'] = "url(#dropshadow)"
            text.attrib['style'] = txtstyle
            text.attrib['data-ptidx'] = str(self.kdt.query((x, y))[1])

        self.labels = layer
        return layer
dataset/ETHPy150Open gallantlab/pycortex/cortex/svgroi.py/ROIpack.setup_labels