Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor data read #4

Open
cdeil opened this issue Oct 9, 2021 · 1 comment
Open

Refactor data read #4

cdeil opened this issue Oct 9, 2021 · 1 comment

Comments

@cdeil
Copy link
Member

cdeil commented Oct 9, 2021

The data read should be refactored so that reading overall only opens the file once, not twice or more fore header and data read. The conversion from int to datetime should be moved to a property so that it's optional, e.g. if one just wants to write the int times exporting Whisper to Parquet. An example for this dump to Parquet should be added to the README.

def read_whisper_archive(
path: str, info: WhisperArchiveMeta, dtype: str = "float32"
) -> pd.Series:
data = np.fromfile(path, dtype=FMT_POINT, count=info.points, offset=info.offset)
# That's right, señor. We remove all points with `time==0`.
# The spec doesn't say, but apparamento this is what it takes.
data = data[data["time"] != 0]
# The type cast for the values is needed to avoid this error later on
# ValueError: Big-endian buffer not supported on little-endian compiler
val = data["val"].astype(dtype)
# Workaround for a performance bug on pandas versions < 1.3
# https://github.com/pandas-dev/pandas/issues/42606
# int32 max value can represent times up to year 2038
index = data["time"].astype("int32")
index = pd.to_datetime(index, unit="s", utc=True)
return pd.Series(val, index).sort_index()
def read_whisper_archive_dataframe(
path: str, archive_id: int, dtype: str = "float32"
) -> pd.DataFrame:
info = WhisperFileMeta.read(path).archives[archive_id]
data = np.fromfile(path, dtype=FMT_POINT, count=info.points, offset=info.offset)
data = data[data["time"] != 0]
value = data["val"].astype(dtype)
time = data["time"].astype("uint32")
df = pd.DataFrame({"timestamp": time, "value": value}).sort_values("timestamp")
return df

@cdeil
Copy link
Member Author

cdeil commented Apr 24, 2022

Partially done in d616adf

Next the WhisperArchive as_numpy, as_dataframe, as_series should be refactored to offer flexible access to any data format the user wants (including raw e.g. for debugging, but in correct byte order)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant