-
Notifications
You must be signed in to change notification settings - Fork 10
Converting MFP CSV to YAML schedule #111
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
5c5bf9a
draft script for converting MFP CSV to YAML schedule
iuryt 40c7ff9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 366dede
add openpyxl
iuryt d046444
add mfp_to_yaml function
iuryt e3199fa
add new command to init to accept mfp file as input
iuryt d9fe46a
delete files from scripts/
iuryt 7dc9bd7
deleted scripts files
iuryt a79433c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 11332f8
export the schedule body instead of saving file
iuryt ad54992
change name of cli param and adapt for new mfp_to_yaml function
iuryt 66adb18
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2672afa
add warning message for time entry on yaml
iuryt a370641
change to pydantic and change name of variables
iuryt b87d944
add XBT
iuryt eba08b8
accept nonetype time
iuryt c0a52ac
change to Waypoint to BaseModel and add field_serializer for instrume…
iuryt 526d2af
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c51043d
remove restriction for version
iuryt f3daaa7
add checking for columns from excel file
iuryt 4c59420
add unit tests
iuryt b67b15d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6f63cd4
Add update comments and var naming
VeckoTheGecko 222df85
Remove buffering from mfp conversion
VeckoTheGecko c94567b
update references to Waypoint
VeckoTheGecko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ dependencies: | |
| - pip | ||
| - pyyaml | ||
| - copernicusmarine >= 2 | ||
| - openpyxl | ||
|
|
||
| # linting | ||
| - pre-commit | ||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -9,3 +9,4 @@ class InstrumentType(Enum): | |
| CTD = "CTD" | ||
| DRIFTER = "DRIFTER" | ||
| ARGO_FLOAT = "ARGO_FLOAT" | ||
| XBT = "XBT" | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,16 +1,23 @@ | ||
| """Waypoint class.""" | ||
|
|
||
| from dataclasses import dataclass | ||
| from datetime import datetime | ||
|
|
||
| from pydantic import BaseModel, field_serializer | ||
|
|
||
| from ..location import Location | ||
| from .instrument_type import InstrumentType | ||
|
|
||
|
|
||
| @dataclass | ||
| class Waypoint: | ||
| class Waypoint(BaseModel): | ||
| """A Waypoint to sail to with an optional time and an optional instrument.""" | ||
|
|
||
| location: Location | ||
| time: datetime | None = None | ||
| instrument: InstrumentType | list[InstrumentType] | None = None | ||
|
|
||
| @field_serializer("instrument") | ||
| def serialize_instrument(self, instrument): | ||
| """Ensure InstrumentType is serialized as a string (or list of strings).""" | ||
| if isinstance(instrument, list): | ||
| return [inst.value for inst in instrument] | ||
| return instrument.value if instrument else None |
iuryt marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,102 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| import pandas as pd | ||
| import pytest | ||
|
|
||
| from virtualship.expedition.instrument_type import InstrumentType | ||
| from virtualship.expedition.schedule import Schedule | ||
| from virtualship.utils import mfp_to_yaml | ||
|
|
||
| # Sample correct MFP data | ||
| VALID_MFP_DATA = pd.DataFrame( | ||
| { | ||
| "Station Type": ["A", "B", "C"], | ||
| "Name": ["Station1", "Station2", "Station3"], | ||
| "Latitude": [30, 31, 32], | ||
| "Longitude": [-44, -45, -46], | ||
| "Instrument": ["CTD, DRIFTER", "ARGO_FLOAT", "XBT, CTD, DRIFTER"], | ||
ammedd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| ) | ||
|
|
||
| # Missing required columns | ||
| MISSING_HEADERS_DATA = pd.DataFrame( | ||
| {"Station Type": ["A"], "Name": ["Station1"], "Latitude": [10.5]} | ||
| ) | ||
|
|
||
| # Extra unexpected columns | ||
| EXTRA_HEADERS_DATA = VALID_MFP_DATA.copy() | ||
| EXTRA_HEADERS_DATA["Unexpected Column"] = ["Extra1", "Extra2", "Extra3"] | ||
|
|
||
|
|
||
| @patch("pandas.read_excel", return_value=VALID_MFP_DATA) | ||
| def test_mfp_to_yaml_success(mock_read_excel, tmp_path): | ||
| """Test that mfp_to_yaml correctly processes a valid MFP Excel file.""" | ||
| yaml_output_path = tmp_path / "schedule.yaml" | ||
|
|
||
| # Run function (No need to mock open() for YAML, real file is created) | ||
| mfp_to_yaml("mock_file.xlsx", yaml_output_path) | ||
|
|
||
| # Ensure the YAML file was written | ||
| assert yaml_output_path.exists() | ||
|
|
||
| # Load YAML and validate contents | ||
| data = Schedule.from_yaml(yaml_output_path) | ||
|
|
||
| assert len(data.waypoints) == 3 | ||
| assert data.waypoints[0].instrument == [InstrumentType.CTD, InstrumentType.DRIFTER] | ||
| assert data.waypoints[1].instrument == [InstrumentType.ARGO_FLOAT] | ||
| assert data.waypoints[2].instrument == [ | ||
| InstrumentType.XBT, | ||
| InstrumentType.CTD, | ||
| InstrumentType.DRIFTER, | ||
| ] | ||
|
|
||
|
|
||
| @patch("pandas.read_excel", return_value=MISSING_HEADERS_DATA) | ||
| def test_mfp_to_yaml_missing_headers(mock_read_excel, tmp_path): | ||
| """Test that mfp_to_yaml raises an error when required columns are missing.""" | ||
| yaml_output_path = tmp_path / "schedule.yaml" | ||
|
|
||
| with pytest.raises( | ||
| ValueError, match="Error: Found columns .* but expected columns .*" | ||
| ): | ||
| mfp_to_yaml("mock_file.xlsx", yaml_output_path) | ||
|
|
||
|
|
||
| @patch("pandas.read_excel", return_value=EXTRA_HEADERS_DATA) | ||
| @patch("builtins.print") # Capture printed warnings | ||
| def test_mfp_to_yaml_extra_headers(mock_print, mock_read_excel, tmp_path): | ||
| """Test that mfp_to_yaml prints a warning when extra columns are found.""" | ||
| yaml_output_path = tmp_path / "schedule.yaml" | ||
|
|
||
| # Run function | ||
| mfp_to_yaml("mock_file.xlsx", yaml_output_path) | ||
|
|
||
| # Ensure a warning message was printed | ||
| mock_print.assert_any_call( | ||
| "Warning: Found additional unexpected columns ['Unexpected Column']. " | ||
| "Manually added columns have no effect. " | ||
| "If the MFP export format changed, please submit an issue: " | ||
| "https://github.com/OceanParcels/virtualship/issues." | ||
| ) | ||
|
|
||
|
|
||
| @patch("pandas.read_excel", return_value=VALID_MFP_DATA) | ||
| def test_mfp_to_yaml_instrument_conversion(mock_read_excel, tmp_path): | ||
| """Test that instruments are correctly converted into InstrumentType enums.""" | ||
| yaml_output_path = tmp_path / "schedule.yaml" | ||
|
|
||
| # Run function | ||
| mfp_to_yaml("mock_file.xlsx", yaml_output_path) | ||
|
|
||
| # Load the generated YAML | ||
| data = Schedule.from_yaml(yaml_output_path) | ||
|
|
||
| assert isinstance(data.waypoints[0].instrument, list) | ||
| assert data.waypoints[0].instrument == [InstrumentType.CTD, InstrumentType.DRIFTER] | ||
| assert data.waypoints[1].instrument == [InstrumentType.ARGO_FLOAT] | ||
| assert data.waypoints[2].instrument == [ | ||
| InstrumentType.XBT, | ||
| InstrumentType.CTD, | ||
| InstrumentType.DRIFTER, | ||
| ] | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.