functional MLmask routine producing mask and inverse mask