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

Add regression test for example tide prediction #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions pytides/observations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import abc
import csv
import datetime

from itertools import tee

import pytz
import numpy


class Observations(object):
__metaclass__ = abc.ABCMeta

@staticmethod
def from_csv(filename):
return CsvObservations(filename)

@staticmethod
def separate_datetimes_heights(observations):
"""
Return a pair of generators: datetimes and heights from a single
generator of (datetime, height) tuples.
"""
x, y = tee(observations, 2) # copy the generator, can't rewind it.
return (tup[0] for tup in x), (tup[1] for tup in y)

@abc.abstractmethod
def to_numpy_arrays(self):
"""
Return (datetimes, heights) where both are numpy arrays.
"""
pass

@abc.abstractmethod
def __iter__(self):
"""
Yield observations values as (datetime, height) tuples.

This must iterate from the start of the observations every time it's
called.
"""
pass


class CsvObservations(Observations):
def __init__(self, filename):
self.filename = filename

def __iter__(self):
return self.observations()

def to_numpy_arrays(self):
datetimes, heights = Observations.separate_datetimes_heights(
self.observations())
return (
numpy.array(list(datetimes)),
numpy.array(list(heights))
)

def observations(self):
with open(self.filename, 'r') as f:
reader = csv.DictReader(
line for line in f if not line.startswith('#'))
for row in reader:
yield (CsvObservations.parse_datetime(row['datetime']),
float(row['height']))

@staticmethod
def parse_datetime(string):
return datetime.datetime.strptime(
string, '%Y-%m-%dT%H:%M:%S+00:00').replace(tzinfo=pytz.UTC)
Empty file added pytides/tests/__init__.py
Empty file.
Loading