Skip to content

Commit

Permalink
psuedocode for conversions. Tests for saving dataframe to file.
Browse files Browse the repository at this point in the history
  • Loading branch information
misi9170 committed Sep 3, 2024
1 parent 39dbb38 commit 8a142de
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
32 changes: 29 additions & 3 deletions flasc/flasc_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def __init__(self, *args, name_map=None, **kwargs):
Args:
*args: arguments to pass to the DataFrame constructor
name_map (dict): Dictionary of column names to map from the user format to the FLASC
format.
format, where the key string is the user format and the value string is the FLASC
equivalent. Defaults to None.
**kwargs: keyword arguments to pass to the DataFrame constructor
"""
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -59,13 +60,31 @@ def __str__(self):

def convert_to_user_format(self, inplace=False):
"""Convert the DataFrame to the format that the user expects, given the name_map."""
# Convert the format
if self._user_format == "long":
self._convert_wide_to_long() # Should this be assigned to something?
elif self._user_format == "semiwide":
self._convert_wide_to_semiwide() # Should this be assigned to something?
elif self._user_format == "wide":
pass

# Convert column names and return
if self.name_map is not None:
return self.rename(columns={v: k for k, v in self.name_map.items()}, inplace=inplace)
else:
return None if inplace else self.copy()

def convert_to_flasc_format(self, inplace=False):
"""Convert the DataFrame to the format that FLASC expects."""
# Convert the format
if self._user_format == "long":
self._convert_long_to_wide() # Should this be assigned to something?
elif self._user_format == "semiwide":
self._convert_semiwide_to_wide() # Should this be assigned to something?
elif self._user_format == "wide":
pass

# Convert column names and return
if self.name_map is not None:
return self.rename(columns=self.name_map, inplace=inplace)
else:
Expand All @@ -88,7 +107,14 @@ def _convert_wide_to_long(self):

def _convert_wide_to_semiwide(self):
"""Convert a wide format DataFrame to a semiwide format DataFrame."""
pass
if "time" not in self.columns:
raise ValueError("Column 'time' must be present in the DataFrame")

# Should have columns:
# time
# turbine_id (as specified by the user)
# variable
# value


# Likely this will be used for testing, later but it's convenient for prototyping here
Expand Down Expand Up @@ -143,6 +169,6 @@ def _convert_wide_to_semiwide(self):
- One column for time stamp
- One column for each channel for each turbine
Converting between semilong and wide should be relatively straightforward.
Converting between semiwide and wide should be relatively straightforward.
Actually, neither of these should be too bad
"""
46 changes: 45 additions & 1 deletion tests/flasc_dataframe_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
import os

import pandas as pd

from flasc.flasc_dataframe import FlascDataFrame

test_data_dict = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}

test_name_map = {"a": "AA"}


def test_type():
df = FlascDataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}, name_map={"a": "AA"})
df = FlascDataFrame(test_data_dict, name_map=test_name_map)
assert isinstance(df, FlascDataFrame)

# Assert df is a pandas DataFrame
assert isinstance(df, pd.DataFrame)


def test_pickle():
df = FlascDataFrame(test_data_dict)
df.name_map = test_name_map
df.to_pickle("test_pickle.pkl")

df2 = pd.read_pickle("test_pickle.pkl")
assert isinstance(df2, FlascDataFrame)
assert df2.name_map == test_name_map

os.remove("test_pickle.pkl")


def test_feather():
df = FlascDataFrame(test_data_dict, name_map=test_name_map)
df.to_feather("test_feather.ftr")

df2 = pd.read_feather("test_feather.ftr")
# Loaded DataFrame is a pandas DataFrame, not a FlascDataFrame
assert not isinstance(df2, FlascDataFrame)
assert isinstance(df2, pd.DataFrame)
assert not hasattr(df2, "name_map")

os.remove("test_feather.ftr")


def test_csv():
df = FlascDataFrame(test_data_dict, name_map=test_name_map)
df.to_csv("test_csv.csv")

df2 = pd.read_csv("test_csv.csv")
# Loaded DataFrame is a pandas DataFrame, not a FlascDataFrame
assert not isinstance(df2, FlascDataFrame)
assert isinstance(df2, pd.DataFrame)
assert not hasattr(df2, "name_map")

os.remove("test_csv.csv")

0 comments on commit 8a142de

Please sign in to comment.