from typing import Sequence


def extract_coordinates(dicom, field):
    """Given a field that is provided for a dicom, extract coordinates"""
    field = field.replace("from:", "", 1)
    coordinates = []
    if field not in dicom:
        return coordinates

    regions = []
    region = dicom.get(field)

    # First put list of attributes together
    if isinstance(region, Sequence):
        for entry in region:
            regions.append(entry)
    else:
        regions.append(region)

    # Now extract coordinates
    for region in regions:

        if (
            "RegionLocationMinX0" in region
            and "RegionLocationMinY0" in region
            and "RegionLocationMaxX1" in region
            and "RegionLocationMaxY1" in region
        ):

            # https://gist.github.com/vsoch/df6957be12c34e62b21000603f1687e5
            # minr, minc, maxr, maxc = coordinate
            # self.cleaned[minc:maxc, minr:maxr] = 0  # should fill with black
            # self.cleaned[A:B, C:D]
            # image[A:B,C:D]
            # A: refers to ymin
            # B: refers to ymax
            # C: refers xmin
            # D: refers to xmax
            # self.cleaned[ymin:ymax, xmin:xmax]
            # coordinate must be [xmin, ymin, xmax, ymax]
            # x0,y0,x1,y1.
            coordinates.append(
                "%s,%s,%s,%s"
                % (
                    region.RegionLocationMinX0,
                    region.RegionLocationMinY0,
                    region.RegionLocationMaxX1,
                    region.RegionLocationMaxY1,
                )
            )
    return coordinates