def test_to_data_frame(self):
        # Given
        time_series_row1 = TimeSeriesRow(datetime(1960, 8, 1, 11, 0, 0), 112.0)
        time_series_row2 = TimeSeriesRow(datetime(1960, 8, 1, 12, 0, 0), 118.0)
        time_series_row3 = TimeSeriesRow(datetime(1960, 8, 1, 13, 0, 0), 132.0)
        time_series = TimeSeries(
            [time_series_row1, time_series_row2, time_series_row3], "Date",
            "Passengers", "yyyy-MM-dd HH:mm:ss")

        # When
        actual_data_frame = time_series.to_data_frame()

        # Then
        initial_data = {
            "Date": [
                datetime.strptime("1960-08-01 11:00:00", "%Y-%m-%d %H:%M:%S") +
                timedelta(hours=i) for i in range(0, 3)
            ],
            "Passengers": [112.0, 118.0, 132.0]
        }
        expected_data_frame = pd.DataFrame.from_dict(initial_data)
        assert_frame_equal(expected_data_frame, actual_data_frame)
    def test_from_json(self):
        # Given
        json = dict(dateColumnName='Date',
                    valueColumnName='Passengers',
                    dateFormat='yyyy-MM-dd HH:mm:ss',
                    rows=[
                        dict(date='1949-01-01 11:00:01', value=112.0),
                        dict(date='1949-01-01 11:00:02', value=118.0),
                        dict(date='1949-01-01 11:00:03', value=132.0)
                    ])

        # When
        actual_time_series = TimeSeries.from_json(json)

        # Then
        time_series_row1 = TimeSeriesRow(datetime(1949, 1, 1, 11, 0, 1), 112.0)
        time_series_row2 = TimeSeriesRow(datetime(1949, 1, 1, 11, 0, 2), 118.0)
        time_series_row3 = TimeSeriesRow(datetime(1949, 1, 1, 11, 0, 3), 132.0)
        expected_time_series = TimeSeries(
            [time_series_row1, time_series_row2, time_series_row3], "Date",
            "Passengers", "yyyy-MM-dd HH:mm:ss")
        assert expected_time_series == actual_time_series
    def test_to_json(self):
        # Given
        time_series_row1 = TimeSeriesRow(datetime(1949, 1, 1), 112.0)
        time_series_row2 = TimeSeriesRow(datetime(1949, 2, 1), 118.0)
        time_series_row3 = TimeSeriesRow(datetime(1949, 3, 1), 132.0)
        time_series = TimeSeries(
            [time_series_row1, time_series_row2, time_series_row3], "Date",
            "Passengers", "yyyy-MM")

        # When
        actual_json = time_series.to_json()

        # Then
        expected_json = dict(dateColumnName='Date',
                             valueColumnName='Passengers',
                             dateFormat='yyyy-MM',
                             rows=[
                                 dict(date='1949-01', value=112.0),
                                 dict(date='1949-02', value=118.0),
                                 dict(date='1949-03', value=132.0)
                             ])
        assert expected_json == actual_json
Beispiel #4
0
    def test_predict(self):
        # Given
        body = dict(timeSeries=dict(rows=[
            dict(date="1949-01", value=112.0),
            dict(date="1949-02", value=118.0),
            dict(date="1949-03", value=132.0),
            dict(date="1949-04", value=129.0),
            dict(date="1949-05", value=121.0),
            dict(date="1949-06", value=135.0),
            dict(date="1949-07", value=148.0),
            dict(date="1949-08", value=148.0),
            dict(date="1949-09", value=136.0),
            dict(date="1949-10", value=119.0),
            dict(date="1949-11", value=104.0),
            dict(date="1949-12", value=118.0),
            dict(date="1950-01", value=115.0),
            dict(date="1950-02", value=126.0)
        ],
                                    dateColumnName='Date',
                                    valueColumnName='Passengers',
                                    dateFormat='yyyy-MM'),
                    numberOfValues=2)
        body_as_string = json.dumps(body)

        # When
        response = test_app.post('/time-series-analysis/predict',
                                 data=body_as_string,
                                 content_type='application/json')
        actual_time_series = TimeSeries.from_json(json.loads(response.data))

        # Then
        time_series_row = TimeSeriesRow(datetime(1950, 3, 1), 125.0)
        time_series_row1 = TimeSeriesRow(datetime(1950, 4, 1), 124.0)
        expected_time_series = TimeSeries([time_series_row, time_series_row1],
                                          "Date", "Passengers", "yyyy-MM")
        assert expected_time_series == actual_time_series
    def test_from_json(self):
        # Given
        json = dict(timeSeries=dict(dateColumnName='Date',
                                    valueColumnName='Passengers',
                                    dateFormat='yyyy-MM',
                                    rows=[dict(date='1949-01', value=112.0),
                                          dict(date='1949-02', value=118.0),
                                          dict(date='1949-03', value=132.0)]),
                    numberOfValues=5)

        # When
        actual_time_series_analysis_request = TimeSeriesAnalysisRequest.from_json(json)

        # Then
        time_series_row1 = TimeSeriesRow(datetime(1949, 1, 1), 112.0)
        time_series_row2 = TimeSeriesRow(datetime(1949, 2, 1), 118.0)
        time_series_row3 = TimeSeriesRow(datetime(1949, 3, 1), 132.0)
        time_series = TimeSeries([time_series_row1, time_series_row2, time_series_row3],
                                 "Date", "Passengers", "yyyy-MM")
        expected_time_series_analysis_request = TimeSeriesAnalysisRequest(time_series, 5)
        assert expected_time_series_analysis_request == actual_time_series_analysis_request
def predict() -> str:
    """
    Predict exact values.

    Arguments
        - time_series_analysis_request_json (str) : json corresponding to time_series_analysis_request_json
                                                    file path of the csv input file

    Returns
        time_series -> time_series corresponding to the predicted values and dates.
    """

    try:
        get_logger().info("[Start] predict request")
        time_series_analysis_request = TimeSeriesAnalysisRequest.from_json(request.json)

        time_series = time_series_analysis_request.get_time_series()
        data = time_series.to_data_frame()
        date_column_name = time_series.get_date_column_name()
        value_column_name = time_series.get_value_column_name()
        number_of_values = time_series_analysis_request.get_number_of_values()
        date_format = time_series.get_date_format()
        time_series_analysis_service = TimeSeriesAnalysisService(data, date_column_name, value_column_name,
                                                                 number_of_values)

        predicted_data_frame = time_series_analysis_service.predict()

        predicted_time_series = TimeSeries.from_data_frame(predicted_data_frame, date_column_name,
                                                           value_column_name, date_format)

        return json.dumps(predicted_time_series, cls=JsonComplexEncoder)

    except Exception as exception:
        error_message = "Exception %s raised while predicting: %s" % (type(exception).__name__, exception)
        get_logger().error(error_message)
        get_logger().exception(exception)
        raise EngineComputationException(error_message)

    finally:
        get_logger().info("[End] predict request")
 def from_json(cls, data: dict):
     time_series = TimeSeries.from_json(data["timeSeries"])
     return cls(time_series, int(data["numberOfValues"]))