<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def polygon_to_mask(coords, dims, z=None):
 """ Given a list of pairs of points which define a polygon, return a binary mask covering the interior of the polygon with dimensions dim """


    bounds = array(coords).astype('int')
    path = Path(bounds)

    grid = meshgrid(range(dims[1]), range(dims[0]))
    grid_flat = zip(grid[0].ravel(), grid[1].ravel())

    mask = path.contains_points(grid_flat).reshape(dims[0:2]).astype('int')

    if z is not None:
        if len(dims) < 3:
            raise Exception('Dims must have three-dimensions for embedding z-index')
        if z >= dims[2]:
            raise Exception('Z-index %g exceeds third dimension %g' % (z, dims[2]))
        tmp = zeros(dims)
        tmp[:, :, z] = mask
        mask = tmp

    return mask