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

Basic HAPI CSV codec #167

Merged
merged 9 commits into from
Jan 7, 2025
Merged

Basic HAPI CSV codec #167

merged 9 commits into from
Jan 7, 2025

Conversation

jeandet
Copy link
Member

@jeandet jeandet commented Dec 13, 2024

This is a basic CSV codec for HAPI. Many bits are still missing, in order to complete this codec we need to refactor and standardize SpeasyVariable metadata interface. @brenard-irap, we have two options:

  1. Rebase this PR once Speasy API is ready
  2. Merge "AS-IS" and update all codecs while we improve the Speasy API

I would be in favor of 2 mostly to avoid merge conflict nightmare and since this code doesn't break any user API.

Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
IOBase can't know how to close and readline should directly call inner
impl readline.

Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
Most products have a fill value so it makes sense to expose it from
SpeasyVariable interface.
Checking if several SpeasyVariable share the same axis is a quite common
operation when writing them into a file.

Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
for var_name in variables:
parameter = hapi_csv_file.get_parameter(var_name)
if parameter is not None:
loaded_vars[var_name] = SpeasyVariable(axes=[time_axis], values=DataContainer(parameter.values,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's intended, I only wanted to handle simple 1D cases first.


def _extract_data(file: io.IOBase) -> pds.DataFrame:
file.seek(0)
return pds.read_csv(file, comment='#', header=None, skiprows=0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With Python 3.12.7 and pandas 2.2.3:

import pandas as pds

df = pds.read_csv("tests/resources/HAPI_sample_csv.csv", comment='#', header=None, skiprows=0)

print(df.index.values)

The result is:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47])

=> Dataframe index is not the "time" column and there is something wrong with (cf. line 55 in load_hapi_csv method):

hapi_csv_file.create_parameter(data.index.to_numpy(dtype="datetime64[ns]"), meta=headers["parameters"][0])

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import speasy as spz
from speasy.core.codecs import get_codec

hapi_csv_codec = get_codec('hapi/csv')

v = hapi_csv_codec.load_variable("Magnitude", "tests/resources/HAPI_sample_csv.csv", disable_cache=True)

print(v.time)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed.

Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
Copy link

sonarqubecloud bot commented Jan 6, 2025

@jeandet jeandet merged commit eabe900 into SciQLop:main Jan 7, 2025
19 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants