reimplemented distthreshold in python