forked from gadomski/pyisd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure library into record and batch
- Loading branch information
Showing
23 changed files
with
449 additions
and
484 deletions.
There are no files selected for viewing
This file contains 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 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 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,5 @@ | ||
batch | ||
===== | ||
|
||
.. automodule:: isd.batch | ||
:members: |
This file contains 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,5 +1,5 @@ | ||
isd.errors | ||
========== | ||
errors | ||
====== | ||
|
||
.. automodule:: isd.errors | ||
:members: |
This file was deleted.
Oops, something went wrong.
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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,5 +1,5 @@ | ||
isd.record | ||
========== | ||
record | ||
====== | ||
|
||
.. automodule:: isd.record | ||
:members: |
This file contains 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,30 +1,44 @@ | ||
"""Given a directory of ISD files, checks that all timesteps monotonically increase.""" | ||
|
||
import os | ||
import logging | ||
from pathlib import Path | ||
import sys | ||
from typing import Union | ||
|
||
import tqdm | ||
|
||
import isd.io | ||
|
||
directory = sys.argv[1] | ||
paths = [os.path.join(directory, file_name) for file_name in os.listdir(directory)] | ||
all_monotonic = True | ||
bad_paths = [] | ||
for path in tqdm.tqdm(paths): | ||
data_frame = isd.io.read_to_data_frame(path) | ||
min = data_frame.timestamp.min() | ||
max = data_frame.timestamp.max() | ||
is_monotonic = data_frame.timestamp.is_monotonic | ||
if not is_monotonic: | ||
all_monotonic = False | ||
bad_paths.append(path) | ||
tqdm.tqdm.write(f"{path}: min={min}, max={max}, is_monotonic={is_monotonic}") | ||
|
||
if all_monotonic: | ||
print("All files have monotonically increasing timestamps!") | ||
else: | ||
print("Not all files have monotonically increasing timestamps, here they are:") | ||
for path in bad_paths: | ||
print(f" - {path}") | ||
sys.exit(1) | ||
from src.isd import Batch | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def main(path: Union[str, Path]) -> None: | ||
path = Path(path) | ||
file_names = list(path.glob("*")) | ||
all_monotonic = True | ||
bad_files = [] | ||
for file_name in tqdm.tqdm(file_names): | ||
df = Batch.from_path(file_name).to_df() | ||
ts_min = df.datetime.min() | ||
ts_max = df.datetime.max() | ||
is_monotonic = df.datetime.is_monotonic_increasing | ||
if not is_monotonic: | ||
all_monotonic = False | ||
bad_files.append(file_name) | ||
log.info( | ||
f"{file_name}: min={ts_min}, max={ts_max}, is_monotonic={is_monotonic}" | ||
) | ||
|
||
if all_monotonic: | ||
print("All files have monotonically increasing timestamps!") | ||
else: | ||
print("Not all files have monotonically increasing timestamps, here they are:") | ||
for file_name in bad_files: | ||
print(f" - {file_name}") | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
directory = sys.argv[1] | ||
main(directory) |
This file contains 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,4 +1,4 @@ | ||
from isd.errors import IsdError | ||
from isd.record import Record | ||
from src.isd.errors import IsdError | ||
from src.isd.batch import Batch, Record | ||
|
||
__all__ = ["IsdError", "Record"] | ||
__all__ = ["IsdError", "Batch", "Record"] |
This file contains 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,90 @@ | ||
import gzip | ||
import json | ||
from io import BytesIO | ||
from pathlib import Path | ||
from dataclasses import dataclass | ||
from typing import List, Union, Optional, Dict, Any | ||
import datetime as dt | ||
|
||
from src.isd.record import Record | ||
|
||
import pandas as pd | ||
|
||
|
||
@dataclass | ||
class Batch: | ||
records: List[Record] | ||
|
||
def __len__(self) -> int: | ||
return len(self.records) | ||
|
||
def __getitem__(self, index: int) -> Record: | ||
return self.records[index] | ||
|
||
def __eq__(self, other: "Batch"): | ||
if not isinstance(other, Batch): | ||
return False | ||
return self.records == other.records | ||
|
||
@classmethod | ||
def from_path(cls, path: Union[str, Path]) -> "Batch": | ||
"""Opens a local ISD file and returns an iterator over its records. | ||
If the path has a .gz extension, this function will assume it has gzip | ||
compression and will attempt to open it using `gzip.open`. | ||
""" | ||
path = Path(path) | ||
if path.suffix == ".gz": | ||
with gzip.open(path) as gzip_file: | ||
return cls( | ||
[ | ||
Record.from_string(gzip_line.decode("utf-8")) | ||
for gzip_line in gzip_file | ||
] | ||
) | ||
else: | ||
with open(path) as uncompressed_file: | ||
return cls( | ||
[ | ||
Record.from_string(uncompressed_line) | ||
for uncompressed_line in uncompressed_file | ||
] | ||
) | ||
|
||
@classmethod | ||
def from_string(cls, string: Union[str, BytesIO]) -> "Batch": | ||
"""Reads records from a text io stream.""" | ||
if isinstance(string, BytesIO): | ||
string = string.read().decode("utf-8") | ||
return cls([Record.from_string(line) for line in string.splitlines()]) | ||
|
||
def filter_by_datetime( | ||
self, | ||
start_date: Optional[dt.datetime] = None, | ||
end_date: Optional[dt.datetime] = None, | ||
) -> "Batch": | ||
"""Returns an iterator over records filtered by start and end datetimes (both optional).""" | ||
return Batch([ | ||
record | ||
for record in self.records | ||
if (not start_date or record.datetime >= start_date) | ||
and (not end_date or record.datetime < end_date) | ||
]) | ||
|
||
def to_dict(self) -> List[Dict[str, Any]]: | ||
"""Returns a list of dictionaries, one for each record.""" | ||
return [record.to_dict() for record in self.records] | ||
|
||
def to_json(self, indent: int = 4) -> str: | ||
"""Returns a JSON string of all records.""" | ||
data = [] | ||
for d in self.to_dict(): | ||
d["datetime"] = d["datetime"].isoformat() | ||
data.append(d) | ||
return json.dumps(data, indent=indent) | ||
|
||
def to_df(self) -> pd.DataFrame: | ||
"""Reads a local ISD file into a DataFrame.""" | ||
import pandas as pd | ||
|
||
return pd.DataFrame([record.to_dict() for record in self.records]) |
This file contains 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.