# Copyright 2010 David Owen <dsowen@fugue88.ws>
# Licensed under the Academic Free License version 3.0

import math

class Emar(object):

    def __init__(self, period):
        self.last = None
        self.count = 0.0
        self.period = float(period)

    def _advance_to_time(self, t):
        if self.last is not None:
            r = (self.last - t) / self.period
            self.count *= math.exp(r)
        self.last = t

    def read_at_time(self, t):
        self._advance_to_time(t)
        return self.count / self.period

    def observed_at_time(self, t):
        self._advance_to_time(t)
        self.count = self.count + 1
        return self.count / self.period 
