Ejemplo n.º 1
0
def test_latest_date_before_should_return_input_if_start_equals_ub():

    starting_date = pd.Timestamp("8 June 2016")
    upper_bound = pd.Timestamp("8 June 2016")
    time_step = pd.Timedelta("7D")

    result = latest_date_before(starting_date, upper_bound, time_step)

    assert result == starting_date
Ejemplo n.º 2
0
def test_latest_date_before_should_shift_forward_n_weeks_input_as_required():

    starting_date = pd.Timestamp("10 June 2016")
    expected_date = pd.Timestamp("27 January 2017")
    upper_bound = pd.Timestamp("29 January 2017")
    time_step = pd.Timedelta("7D")

    result = latest_date_before(starting_date, upper_bound, time_step)

    assert result == expected_date
Ejemplo n.º 3
0
def test_latest_date_before_should_shift_backward_ne_week_input_as_required():

    starting_date = pd.Timestamp("10 June 2016")
    expected_date = pd.Timestamp("3 June 2016")
    upper_bound = pd.Timestamp("8 June 2016")
    time_step = pd.Timedelta("7D")

    result = latest_date_before(starting_date, upper_bound, time_step)

    assert result == expected_date
Ejemplo n.º 4
0
def test_latest_date_before_should_shift_forward_until_upper_bound():

    # here the upper bound IS the expected date => makes sure we go up to
    # thsi ons
    starting_date = pd.Timestamp("10 June 2016")
    upper_bound = pd.Timestamp("24 June 2016")
    time_step = pd.Timedelta("7D")

    result = latest_date_before(starting_date, upper_bound, time_step)

    assert result == upper_bound
Ejemplo n.º 5
0
    def __init__(self, clock, seed, config):
        """
        This should not be used, only child classes

        :type clock: Clock
        :param clock: the master clock driving this simulator

        :type seed: int
        :param seed: seed for random number generator, default None
        :return: A new TimeProfiler is created
        """
        DependentGenerator.__init__(self)
        self._state = RandomState(seed)
        self.config = config
        self.clock = clock

        # "macro" time shift: we shift the whole profile n times in the future
        # or the past until it overlaps with the current clock date
        init_date = latest_date_before(
            starting_date=config.start_date,
            upper_bound=clock.current_date,
            time_step=pd.Timedelta(config.profile_time_steps) *
            len(config.profile))

        # Un-scaled weight profile. We artificially adds a nan to force the
        # up-sclaling to multiply the last element
        profile_idx = pd.date_range(start=init_date,
                                    freq=config.profile_time_steps,
                                    periods=len(config.profile) + 1)
        profile_ser = pd.Series(data=config.profile + [np.nan],
                                index=profile_idx)

        # scaled weight profile, s.t. one clock step == one profile value
        profile_ser = profile_ser.resample(rule=clock.step_duration).pad()[:-1]

        self.n_time_bin = profile_ser.shape[0]

        profile_cdf = (profile_ser / profile_ser.sum()).cumsum()
        self.profile = pd.DataFrame({
            "cdf": profile_cdf,

            # for debugging
            "timeframe": np.arange(len(profile_cdf))
        })

        # "micro" time shift,: we step forward along the profile until it is
        # align with the current date
        while self.profile.index[0] < clock.current_date:
            self.increment()

        # makes sure we'll get notified when the clock goes forward
        clock.register_increment_listener(self)