From b66a5f3cf23087a5953ec36040c8f1e9e25ae132 Mon Sep 17 00:00:00 2001 From: zacharyburnett Date: Sat, 5 Mar 2022 09:00:49 -0500 Subject: [PATCH] update predicts parameter handling --- packetraven/__main__.py | 4 ++-- packetraven/gui/base.py | 4 ++-- packetraven/packets/tracks.py | 33 ++++++++++++++++----------------- packetraven/predicts.py | 16 +++++++++------- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/packetraven/__main__.py b/packetraven/__main__.py index e0e8f746..79a93c42 100644 --- a/packetraven/__main__.py +++ b/packetraven/__main__.py @@ -26,7 +26,7 @@ from packetraven.packets import APRSPacket from packetraven.packets.tracks import APRSTrack, LocationPacketTrack from packetraven.packets.writer import write_packet_tracks -from packetraven.predicts import get_predictions, PredictionAPIURL, PredictionError +from packetraven.predicts import packet_track_predictions, PredictionAPIURL, PredictionError from packetraven.utilities import ( ensure_datetime_timezone, get_logger, @@ -429,7 +429,7 @@ def main(): if prediction_filename is not None: try: predictions.update( - get_predictions( + packet_track_predictions( packet_tracks, **{ key.replace('prediction_', ''): value diff --git a/packetraven/gui/base.py b/packetraven/gui/base.py index 515b33d2..7160024f 100644 --- a/packetraven/gui/base.py +++ b/packetraven/gui/base.py @@ -21,7 +21,7 @@ from packetraven.packets import APRSPacket, LocationPacket from packetraven.packets.tracks import LocationPacketTrack, PredictedTrajectory from packetraven.packets.writer import write_packet_tracks -from packetraven.predicts import get_predictions, PredictionError +from packetraven.predicts import packet_track_predictions, PredictionError from packetraven.utilities import get_logger @@ -773,7 +773,7 @@ async def retrieve_packets(self): if self.toggles['prediction_file']: try: self.__predictions.update( - get_predictions( + packet_track_predictions( self.packet_tracks, **{ key.replace('prediction_', ''): value diff --git a/packetraven/packets/tracks.py b/packetraven/packets/tracks.py index 122ccb62..ef8dd722 100644 --- a/packetraven/packets/tracks.py +++ b/packetraven/packets/tracks.py @@ -1,14 +1,13 @@ from copy import copy from datetime import datetime, timedelta from os import PathLike -from typing import Iterable, List, Union +from typing import Any, Dict, Iterable, List, Union from dateutil.parser import parse as parse_date import geojson import numpy from pandas import DataFrame from pyproj import CRS -import typepigeon from packetraven.model import ( FREEFALL_DESCENT_RATE, @@ -425,34 +424,26 @@ class PredictedTrajectory(LocationPacketTrack): def __init__( self, packets: List[LocationPacket], - prediction_time: datetime, - dataset_time: datetime, + parameters: Dict[str, Any], + metadata: Dict[str, Any], crs: CRS = None, **attributes, ): """ :param packets: iterable of packets - :param prediction_time: time of completed prediction - :param dataset_time: time of last dataset update + :param parameters: prediction parameters + :param metadata: prediction completion metadata :param crs: coordinate reference system to use """ if 'name' not in attributes: attributes['name'] = 'predicted trajectory' - if not isinstance(prediction_time, datetime): - prediction_time = typepigeon.convert_value(prediction_time, datetime) - - if not isinstance(dataset_time, datetime): - dataset_time = typepigeon.convert_value(dataset_time, datetime) + self.__parameters = parameters + self.__metadata = metadata LocationPacketTrack.__init__( - self, - packets=packets, - prediction_time=prediction_time, - dataset_time=dataset_time, - crs=crs, - **attributes, + self, packets=packets, crs=crs, **attributes, ) @property @@ -470,3 +461,11 @@ def from_file(cls, filename: PathLike) -> List['PredictedTrajectory']: ) return tracks + + @property + def parameters(self) -> Dict[str, Any]: + return self.__parameters + + @property + def metadata(self) -> Dict[str, Any]: + return self.__metadata diff --git a/packetraven/predicts.py b/packetraven/predicts.py index fcafd879..97009991 100644 --- a/packetraven/predicts.py +++ b/packetraven/predicts.py @@ -278,8 +278,10 @@ def get(self) -> Dict[str, Any]: try: error = response.json()['error']['description'] except: - error = '' - raise ConnectionError(f'connection raised error {response.status_code} for {response.url} - {error}') + error = 'no message' + raise ConnectionError( + f'connection raised error {response.status_code} for {response.url} - {error}' + ) @property def predict(self) -> PredictedTrajectory: @@ -305,8 +307,8 @@ def predict(self) -> PredictedTrajectory: ) for point in points ], - prediction_time=response['metadata']['complete_datetime'], - dataset_time=response['request']['dataset'], + parameters=response['request'], + metadata=response['metadata'], ) else: raise PredictionError(response['error']['description']) @@ -388,7 +390,7 @@ def query(self) -> Dict[str, Any]: return query -def get_predictions( +def packet_track_predictions( packet_tracks: Dict[str, LocationPacketTrack], start_location: Tuple[float, float, float] = None, start_time: datetime = None, @@ -475,7 +477,7 @@ def get_predictions( if float_altitude is not None and not packet_track.falling: packets_at_float_altitude = packet_track[ numpy.abs(float_altitude - packet_track.altitudes) < float_altitude_uncertainty - ] + ] if ( len(packets_at_float_altitude) > 0 and packets_at_float_altitude[-1].time == packet_track.times[-1] @@ -485,7 +487,7 @@ def get_predictions( elif packet_track.ascent_rates[-1] >= 0: prediction_float_start_time = prediction_start_time + timedelta( seconds=(float_altitude - prediction_start_location[2]) - / prediction_ascent_rate + / prediction_ascent_rate ) descent_only = False else: