generated from cvxgrp/simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright 2023 Stanford University Convex Optimization Group | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from io import BytesIO | ||
from typing import Any | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import pyarrow as pa | ||
|
||
|
||
def encode(data: np.ndarray | pd.DataFrame) -> Any: | ||
""" | ||
Encode a numpy array or a pandas DataFrame | ||
Args: | ||
data: The numpy array or pandas DataFrame | ||
Returns: object converted into bytes | ||
""" | ||
if isinstance(data, np.ndarray): | ||
tensor = pa.Tensor.from_numpy(obj=data) | ||
buffer = pa.BufferOutputStream() | ||
pa.ipc.write_tensor(tensor, buffer) | ||
return bytes(buffer.getvalue().to_pybytes()) | ||
|
||
if isinstance(data, pd.DataFrame): | ||
return data.to_parquet() | ||
|
||
raise TypeError(f"Invalid Datatype {type(data)}") | ||
|
||
|
||
def decode(data: bytes) -> np.ndarray | pd.DataFrame: | ||
""" | ||
Decode the bytes back into numpy array or pandas DataFrame | ||
Args: | ||
data: bytes | ||
Returns: | ||
The array or the frame | ||
""" | ||
try: | ||
return pa.ipc.read_tensor(data).to_numpy() | ||
except pa.ArrowInvalid: | ||
return pd.read_parquet(BytesIO(data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
from cvx.bson.file import from_bson, to_bson | ||
|
||
|
||
@pytest.fixture() | ||
def data(): | ||
return { | ||
"frame": pd.DataFrame(data=np.random.rand(5, 2)), | ||
"numpy": np.random.rand(5, 2), | ||
"frame_with_time": pd.DataFrame( | ||
data=np.random.rand(2, 2), | ||
index=[pd.Timestamp("2020-01-01"), pd.Timestamp("2022-01-01")], | ||
), | ||
} | ||
|
||
|
||
def assert_equal(obj1, obj2): | ||
assert type(obj1) == type(obj2) | ||
|
||
if isinstance(obj1, pd.DataFrame): | ||
pd.testing.assert_frame_equal(obj1, obj2) | ||
|
||
if isinstance(obj1, np.ndarray): | ||
np.testing.assert_array_equal(obj1, obj2) | ||
|
||
|
||
def test_roundtrip(data): | ||
""" | ||
Testing the roundtrip | ||
Args: | ||
data: Fixture exposing a dictionary of data | ||
""" | ||
reproduced = from_bson(to_bson(data)) | ||
for key, value in reproduced.items(): | ||
assert_equal(value, data[key]) | ||
|
||
|
||
def test_file(data, tmp_path): | ||
with open(file=tmp_path / "xxx.bson", mode="wb") as bson_file: | ||
bson_file.write(to_bson(data)) | ||
|
||
with open(file=tmp_path / "xxx.bson", mode="rb") as bson_file: | ||
reproduced = from_bson(bson_file.read()) | ||
|
||
for key, value in reproduced.items(): | ||
assert_equal(reproduced[key], data[key]) |