示例#1
0
    def test_start_fail(self):
        """`Timer.start` should fail if it was already called before for the given key."""
        timer = Timer()

        with timer.record('my_key'):
            pass

        with pytest.raises(ValueError):
            timer.start('my_key')
示例#2
0
    def polynomial_regression(self, env, result, degrees):
        """
        Create and train a polynomial regression function with {degrees} degrees
        of freedom. Check if the Mean Square Error (MSE) and time to train the
        model are within their thresholds. Display the train data and the model
        on a plot.
        """
        # This example was based on
        # http://scikit-learn.org/stable/auto_examples/model_selection/plot_underfitting_overfitting.html

        # Create the pipeline to train a polynomial regression with varying
        # degrees of freedom.
        polynomial_features = PolynomialFeatures(degree=degrees,
                                                 include_bias=False)
        pipeline = Pipeline([
            ("polynomial_features", polynomial_features),
            ("linear_regression", LinearRegression()),
        ])

        # Train the model and record how long this takes.
        timer = Timer()
        with timer.record("train_model"):
            pipeline.fit(self.x[:, np.newaxis], self.y)
        scores = cross_val_score(
            pipeline,
            self.x[:, np.newaxis],
            self.y,
            scoring="neg_mean_squared_error",
            cv=10,
        )

        # Check the Mean Square Error (MSE) and time to train the model are
        # within their thresholds.
        result.less(
            -scores.mean(),
            0.05,
            description="Mean Square Error threshold on test data",
        )
        result.less(
            timer["train_model"].elapsed,
            1,
            description="How long did the model take to train?",
        )

        # Display the train data and the model on a plot.
        create_scatter_plot(
            title="{} degrees of freedom model & Train data".format(degrees),
            x=self.x,
            y=self.y,
            label="Samples",
            c="black",
        )
        y_test = pipeline.predict(self.x_test[:, np.newaxis])
        plot.plot(self.x_test, y_test, label="Model")
        plot.legend(loc="best")
        result.matplot(plot)
示例#3
0
    def test_record(self):
        """`Timer.record` should record an interval for the given context."""
        sleep_duration = 1
        sleeper_delta = 0.25  # func call lasts a little bit longer

        def sleeper():
            time.sleep(sleep_duration)

        timer = Timer()
        with timer.record('my_key'):
            sleeper()
示例#4
0
    def test_start(self):
        """`Timer.start` should create an `Interval` that has `start` attribute set."""
        timer = Timer()

        assert 'my_key' not in timer

        prev_now = utcnow()
        time.sleep(0.001)

        timer.start('my_key')

        assert prev_now < timer['my_key'].start
        assert timer['my_key'].end is None
示例#5
0
    def test_end(self):
        """`timer.end` should update the matching `Interval.end` for the given key."""
        timer = Timer()

        # Explicitly set value for testing
        # don't care about start
        timer['my_key'] = Interval('foo', None)

        prev_now = utcnow()

        time.sleep(0.001)

        timer.end('my_key')

        assert prev_now < timer['my_key'].end
示例#6
0
    def test_end_overwrite(self):
        """`timer.end` should overwrite previous `end` value for the given key."""
        timer = Timer()

        with timer.record('my_key'):
            pass

        prev_end = timer['my_key'].end

        time.sleep(0.001)

        timer.end('my_key')

        new_end = timer['my_key'].end

        assert new_end > prev_end
示例#7
0
    def test_end_fail(self):
        """`record_end` must fail when no entry if found for a given key."""
        timer = Timer()

        with pytest.raises(KeyError):
            timer.end('my_key')