From 08f329d57236df458878adecce416e653968b389 Mon Sep 17 00:00:00 2001 From: overegneered Date: Wed, 16 Oct 2024 13:58:57 +0100 Subject: [PATCH] Implement from_csv() The suggested signature was adjusted to allow for path-like objects as well (as these are also accepted by pandas). --- uuv_mission/dynamic.py | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/uuv_mission/dynamic.py b/uuv_mission/dynamic.py index c7c7ad53..146ab203 100644 --- a/uuv_mission/dynamic.py +++ b/uuv_mission/dynamic.py @@ -1,7 +1,10 @@ from __future__ import annotations + +import os from dataclasses import dataclass import numpy as np import matplotlib.pyplot as plt +import pandas as pd from .terrain import generate_reference_and_limits class Submarine: @@ -74,9 +77,41 @@ def random_mission(cls, duration: int, scale: float): return cls(reference, cave_height, cave_depth) @classmethod - def from_csv(cls, file_name: str): - # You are required to implement this method - pass + def from_csv(cls, file_name: str | os.PathLike) -> Mission: + """Read mission data from a CSV file and return a Mission instance from it. + + The CSV should have three headings: ``reference``, ``cave_height``, and + ``cave_depth``. + + Parameters + ---------- + file_name : str | os.PathLike + A filepath to the CSV file. Can be absolute, relative, or a URL. + + Returns + ------- + Mission + A new ``Mission`` instance initialised with the mission data from the CSV + file. + + Raises + ------ + FileNotFoundError + If the file pointed to by ``file_name`` does not exist. + ValueError + If the CSV file is malformed. + """ + mission_data = pd.read_csv(file_name) + try: + return Mission( + reference=mission_data["reference"].values, + cave_height=mission_data["cave_height"].values, + cave_depth=mission_data["cave_depth"].values, + ) + except KeyError: + missing_columns = {"reference", "cave_height", "cave_depth"}.difference(set(mission_data.columns)) + missing_columns_str = ", ".join(map(lambda column: f"`{column}`", missing_columns)) + raise ValueError(f"Passed CSV file `{file_name}` is malformed. Missing columns: {missing_columns_str}") class ClosedLoop: