Optimal coding, invariance, noise and heterogeneity in an ON/OFF sensory pathway