Skip to content

Commit

Permalink
Adding post_datapoints_frame (#112)
Browse files Browse the repository at this point in the history
* Adding post_datapoints_frame
  • Loading branch information
trygvekk authored Sep 17, 2018
1 parent 1f29a12 commit 350ac3b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 16 deletions.
5 changes: 3 additions & 2 deletions cognite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@

from cognite.data_transfer_service import DataTransferService

__all__ = ["v04", "v05", "v06", "preprocessing", "config", "data_transfer_service"]
__version__ = "0.10.0"
__all__ = ["v04", "v05", "v06", "preprocessing",
"config", "data_transfer_service"]
__version__ = "0.10.1"
29 changes: 29 additions & 0 deletions cognite/v05/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,35 @@ def _get_datapoints_frame_user_defined_limit(time_series, aggregates, granularit
return df


def post_datapoints_frame(data, **kwargs):
"""Write a dataframe
Args:
dataframe (DataFrame): Pandas DataFrame Object containing the timeseries
Keyword Args:
api_key (str): Your api-key.
project (str): Project name.
Returns:
An empty response.
"""
api_key, project = config.get_config_variables(kwargs.get("api_key"), kwargs.get("project"))

try:
timestamp = data.timestamp
names = data.drop(['timestamp'], axis=1).columns
except:
raise _utils.InputError('DataFrame not on a correct format')

for name in names:
data_points = [Datapoint(int(timestamp[i]), data[name].iloc[i]) for i in range(0, len(data))]
res = post_datapoints(name, data_points, api_key=api_key, project=project)

return res


def get_timeseries(prefix=None, description=None, include_metadata=False, asset_id=None, path=None, **kwargs):
"""Returns a TimeseriesObject containing the requested timeseries.
Expand Down
75 changes: 61 additions & 14 deletions tests/v05/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def ts_name():
class TestTimeseries:
@pytest.fixture(scope="class", params=[True, False])
def get_timeseries_response_obj(self, request):
yield timeseries.get_timeseries(prefix=TS_NAME, limit=1, include_metadata=request.param)
yield timeseries.get_timeseries(
prefix=TS_NAME, limit=1, include_metadata=request.param
)

def test_post_timeseries(self):
tso = dto.TimeSeries(TS_NAME)
Expand Down Expand Up @@ -58,9 +60,14 @@ def test_delete_timeseries(self):
res = timeseries.delete_time_series(TS_NAME)
assert res == {}

def test_get_timeseries_with_config_variables_from_argument(self, unset_config_variables):
def test_get_timeseries_with_config_variables_from_argument(
self, unset_config_variables
):
ts = timeseries.get_timeseries(
prefix=TS_NAME, limit=1, api_key=unset_config_variables[0], project=unset_config_variables[1]
prefix=TS_NAME,
limit=1,
api_key=unset_config_variables[0],
project=unset_config_variables[1],
)
assert ts

Expand Down Expand Up @@ -89,12 +96,28 @@ def test_post_datapoints(self):
res = timeseries.post_datapoints(TS_NAME, datapoints=dps)
assert res == {}

def test_post_datapoints_frame(self):
data = pd.DataFrame()
data["timestamp"] = [int(1537208777557 + 1000 * i) for i in range(0, 100)]
X = data["timestamp"].values.astype(float)
data["X"] = X ** 2
data["Y"] = 1.0 / (1 + X)

for name in data.drop(["timestamp"], axis=1).columns:
ts = dto.TimeSeries(name=name, description="To be deleted")
try:
timeseries.post_time_series([ts])
except:
pass

res = timeseries.post_datapoints_frame(data)
assert res == {}

def test_get_datapoints(self, get_dps_response_obj):
from cognite.v05.dto import DatapointsResponse

assert isinstance(get_dps_response_obj, DatapointsResponse)


def test_get_dps_output_formats(self, get_dps_response_obj):
assert isinstance(get_dps_response_obj.to_ndarray(), np.ndarray)
assert isinstance(get_dps_response_obj.to_pandas(), pd.DataFrame)
Expand All @@ -114,9 +137,15 @@ def test_get_dps_with_end_now(self):
res = timeseries.get_datapoints(name="constant", start=0, end="now", limit=100)
assert len(res.to_json().get("datapoints")) == 100

def test_get_dps_with_limit_with_config_variables_from_argument(self, unset_config_variables):
def test_get_dps_with_limit_with_config_variables_from_argument(
self, unset_config_variables
):
res = timeseries.get_datapoints(
name="constant", start=0, limit=1, api_key=unset_config_variables[0], project=unset_config_variables[1]
name="constant",
start=0,
limit=1,
api_key=unset_config_variables[0],
project=unset_config_variables[1],
)
assert len(res.to_json().get("datapoints")) == 1

Expand Down Expand Up @@ -164,11 +193,17 @@ def test_get_dps_frame_correctly_spaced(self, get_datapoints_frame_response_obj)

def test_get_dps_frame_with_limit(self):
df = timeseries.get_datapoints_frame(
time_series=["constant"], aggregates=["avg"], granularity="1m", start=0, limit=1
time_series=["constant"],
aggregates=["avg"],
granularity="1m",
start=0,
limit=1,
)
assert df.shape[0] == 1

def test_get_dps_frame_with_limit_with_config_values_from_argument(self, unset_config_variables):
def test_get_dps_frame_with_limit_with_config_values_from_argument(
self, unset_config_variables
):
df = timeseries.get_datapoints_frame(
time_series=["constant"],
aggregates=["avg"],
Expand All @@ -180,7 +215,9 @@ def test_get_dps_frame_with_limit_with_config_values_from_argument(self, unset_c
)
assert df.shape[0] == 1

def test_get_dps_frame_with_config_values_from_argument(self, unset_config_variables):
def test_get_dps_frame_with_config_values_from_argument(
self, unset_config_variables
):
res = timeseries.get_datapoints_frame(
time_series=["constant"],
start=1522188000000,
Expand Down Expand Up @@ -237,19 +274,25 @@ def test_post_multitag_datapoints(self):
)
assert post_request_mock.call_count == 2

def test_get_multi_time_series_dps_output_format(self, get_multi_time_series_dps_response_obj):
def test_get_multi_time_series_dps_output_format(
self, get_multi_time_series_dps_response_obj
):
from cognite.v05.dto import DatapointsResponse

assert isinstance(get_multi_time_series_dps_response_obj, list)

for dpr in get_multi_time_series_dps_response_obj:
assert isinstance(dpr, DatapointsResponse)

def test_get_multi_time_series_dps_response_length(self, get_multi_time_series_dps_response_obj):
def test_get_multi_time_series_dps_response_length(
self, get_multi_time_series_dps_response_obj
):
assert len(list(get_multi_time_series_dps_response_obj)) == 2

@pytest.mark.xfail(strict=True)
def test_get_multi_timeseries_dps_correctly_spaced(self, get_multi_time_series_dps_response_obj):
def test_get_multi_timeseries_dps_correctly_spaced(
self, get_multi_time_series_dps_response_obj
):
m = list(get_multi_time_series_dps_response_obj)
timestamps = m[0].to_pandas().timestamp.values
deltas = np.diff(timestamps, 1)
Expand All @@ -271,14 +314,18 @@ def test_split_TimeseriesWithDatapoints_if_over_limit():
name="test", datapoints=[Datapoint(x, x) for x in range(1000)]
)

result: List[TimeseriesWithDatapoints] = _split_TimeseriesWithDatapoints_if_over_limit(
result: List[
TimeseriesWithDatapoints
] = _split_TimeseriesWithDatapoints_if_over_limit(
timeseries_with_datapoints_over_limit, 100
)

assert isinstance(result[0], TimeseriesWithDatapoints)
assert len(result) == 10

result = _split_TimeseriesWithDatapoints_if_over_limit(timeseries_with_datapoints_over_limit, 1000)
result = _split_TimeseriesWithDatapoints_if_over_limit(
timeseries_with_datapoints_over_limit, 1000
)

assert isinstance(result[0], TimeseriesWithDatapoints)
assert len(result) == 1

0 comments on commit 350ac3b

Please sign in to comment.