Skip to content

Commit

Permalink
Update code format
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiggi committed Apr 6, 2024
1 parent 8f35a7e commit 90aa416
Show file tree
Hide file tree
Showing 34 changed files with 51 additions and 60 deletions.
7 changes: 7 additions & 0 deletions tstore/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""tstore."""
import contextlib
from importlib.metadata import PackageNotFoundError, version

from tstore.tsdf import TSDF
from tstore.tsdf.extensions.array import TSArray
from tstore.tsdf.extensions.ts_dtype import TSDtype
Expand All @@ -12,3 +15,7 @@
"TS",
"TSDF",
]

# Get version
with contextlib.suppress(PackageNotFoundError):
__version__ = version("tstore")
1 change: 0 additions & 1 deletion tstore/archive/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 22:23:06 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/archive/attributes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 23:21:19 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/archive/attributes/dask.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 23:22:06 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/archive/attributes/geopandas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 23:22:11 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/archive/attributes/pandas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 23:22:02 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/archive/attributes/polars.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 23:22:22 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/archive/attributes/pyarrow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 23:22:16 2023.
Expand Down
17 changes: 7 additions & 10 deletions tstore/archive/checks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 21:32:37 2023.
Expand Down Expand Up @@ -36,14 +35,12 @@ def check_ts_variables(ts_variables, base_dir):
if isinstance(ts_variables, type(None)):
ts_variables = available_ts_variables
ts_variables = np.array(ts_variables)
unvalid_ts_variables = ts_variables[
np.isin(ts_variables, available_ts_variables, invert=True)
]
unvalid_ts_variables = ts_variables[np.isin(ts_variables, available_ts_variables, invert=True)]
ts_variables = ts_variables.tolist()
unvalid_ts_variables = unvalid_ts_variables.tolist()
if len(unvalid_ts_variables) > 0:
raise ValueError(
f"Valid ts_variables are {ts_variables}. Unvalid: {unvalid_ts_variables}."
f"Valid ts_variables are {ts_variables}. Invalid: {unvalid_ts_variables}.",
)
return ts_variables

Expand Down Expand Up @@ -76,13 +73,13 @@ def check_time(time):
datetime.datetime object.
"""
# TODO: adapt to return the more approriate object !!!
# TODO: adapt to return the more appropriate object !!!
if not isinstance(
time, (datetime.datetime, datetime.date, np.datetime64, np.ndarray, str)
time,
(datetime.datetime, datetime.date, np.datetime64, np.ndarray, str),
):
raise TypeError(
"Specify time with datetime.datetime objects or a "
"string of format 'YYYY-MM-DD hh:mm:ss'."
"Specify time with datetime.datetime objects or a " "string of format 'YYYY-MM-DD hh:mm:ss'.",
)
# If numpy array with datetime64 (and size=1)
if isinstance(time, np.ndarray):
Expand Down Expand Up @@ -117,5 +114,5 @@ def check_start_end_time(start_time, end_time):
# Check start_time and end_time are chronological
if start_time is not None and end_time is not None:
if start_time > end_time:
raise ValueError("Provide start_time occuring before of end_time")
raise ValueError("Provide start_time occurring before of end_time")
return (start_time, end_time)
1 change: 1 addition & 0 deletions tstore/archive/io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""TStore I/O tools."""

import glob
import os
import shutil
Expand Down
3 changes: 1 addition & 2 deletions tstore/archive/readers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 15:35:06 2023.
Expand All @@ -12,7 +11,7 @@

def _read_yaml_metadata(fpath):
"""Read metadata YAML file."""
with open(fpath, "r") as file:
with open(fpath) as file:
metadata = yaml.safe_load(file)
return metadata

Expand Down
1 change: 0 additions & 1 deletion tstore/archive/ts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 22:22:27 2023.
Expand Down
5 changes: 2 additions & 3 deletions tstore/archive/ts/dask.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 22:24:07 2023.
Expand Down Expand Up @@ -29,14 +28,14 @@ def open_ts(

# Define Apache Arrow settings
arrow_to_pandas = {
"zero_copy_only": False, # Default is False. If True, raise error if doing copys
"zero_copy_only": False, # Default is False. If True, raise error if doing copies
"strings_to_categorical": False,
"date_as_object": False, # Default is True. If False convert to datetime64[ns]
"timestamp_as_object": False, # Default is True. If False convert to np.datetime64[ns]
"use_threads": True, # parallelize the conversion using multiple threads.
"safe": True,
"split_blocks": False,
"ignore_metadata": False, # Default False. If False, use the pandas metadata to get the Index
"ignore_metadata": False, # Default False. If False, use the 'pandas' metadata to get the Index
"types_mapper": pd.ArrowDtype, # Ensure pandas is created with Arrow dtype
}

Expand Down
1 change: 0 additions & 1 deletion tstore/archive/ts/filtering.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 22:27:07 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/archive/ts/partitioning.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 22:21:47 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/archive/ts/pyarrow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 22:19:51 2023.
Expand Down
1 change: 1 addition & 0 deletions tstore/archive/writers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""TStore writers."""

import yaml

from tstore.archive.io import (
Expand Down
7 changes: 6 additions & 1 deletion tstore/tsdf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""TSDF."""

import pandas as pd


Expand All @@ -9,7 +10,11 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def to_tstore(
self, base_dir, partition_str=None, tstore_structure="id-var", overwrite=True
self,
base_dir,
partition_str=None,
tstore_structure="id-var",
overwrite=True,
):
"""Write TStore from TSDF object."""
from tstore.tsdf.writer import write_tstore
Expand Down
9 changes: 6 additions & 3 deletions tstore/tsdf/extensions/array.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 11 22:47:54 2023.
Expand Down Expand Up @@ -28,6 +27,7 @@

# https://itnext.io/guide-to-pandas-extension-types-and-how-to-create-your-own-3b213d689c86


####--------------------------------------------------------------------------.
def get_tabular_object_type(obj):
"""Get inner class of the TS object."""
Expand Down Expand Up @@ -73,7 +73,7 @@ def __str__(self):
return str(self._data)

def __repr__(self):
"""repr representation."""
"""Repr representation."""
n = len(self._data)
return f"TSArray composed of {n} TS objects."

Expand Down Expand Up @@ -173,6 +173,9 @@ def take(self, indices, allow_fill=False, fill_value=None):
fill_value = self.dtype.na_value

result = pd.core.algorithms.take(
self._data, indices, allow_fill=allow_fill, fill_value=fill_value
self._data,
indices,
allow_fill=allow_fill,
fill_value=fill_value,
)
return self._from_sequence(result)
3 changes: 1 addition & 2 deletions tstore/tsdf/extensions/ts_dtype.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 16:23:54 2023.
Expand Down Expand Up @@ -91,7 +90,7 @@ def construct_from_string(cls, string: str):
Example:
-------
>>> TSDtype.construct_from_string('TS[pandas]')
>>> TSDtype.construct_from_string("TS[pandas]")
TS['pandas']
"""
if not isinstance(string, str):
Expand Down
6 changes: 1 addition & 5 deletions tstore/tsdf/reader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 15:48:39 2023.
Expand Down Expand Up @@ -31,10 +30,7 @@ def _read_tsarray(base_dir, ts_variable):
def _read_tsarrays(base_dir, metadata):
"""Read list of TSArrays."""
ts_variables = metadata["ts_variables"]
list_ts_series = [
_read_tsarray(base_dir=base_dir, ts_variable=ts_variable)
for ts_variable in ts_variables
]
list_ts_series = [_read_tsarray(base_dir=base_dir, ts_variable=ts_variable) for ts_variable in ts_variables]
return list_ts_series


Expand Down
5 changes: 2 additions & 3 deletions tstore/tsdf/ts_class.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 22:17:59 2023.
Expand All @@ -19,8 +18,8 @@ def add_partitioning_columns(df, partitioning_str):
"""Add partitioning columns to the dataframe."""
# TODO: as function of TS_partitioning_string (YYYY/MM/DD) or (YY/DOY/HH)
# dayofyear, dayofweek, hour, minute, ...
df["month"] = df.index.dt.month.values
df["year"] = df.index.dt.year.values
df["month"] = df.index.dt.month.to_numpy()
df["year"] = df.index.dt.year.to_numpy()
partition_on = ["year", "month"]
return df, partition_on

Expand Down
19 changes: 12 additions & 7 deletions tstore/tsdf/writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 15:49:54 2023.
Expand All @@ -18,9 +17,7 @@
def _get_ts_variables(df):
"""Get list of TSArray columns."""
columns = np.array(list(df.columns))
ts_variables = columns[
[isinstance(df[column].dtype, TSDtype) for column in columns]
].tolist()
ts_variables = columns[[isinstance(df[column].dtype, TSDtype) for column in columns]].tolist()
return ts_variables


Expand Down Expand Up @@ -62,7 +59,9 @@ def _write_tsarrays(df, base_dir, tstore_structure):
tsarray_columns = _get_ts_variables(df)
for column in tsarray_columns:
_write_ts_series(
ts_series=df[column], base_dir=base_dir, tstore_structure=tstore_structure
ts_series=df[column],
base_dir=base_dir,
tstore_structure=tstore_structure,
)


Expand All @@ -72,12 +71,18 @@ def _write_metadata(df, base_dir, tstore_structure):

ts_variables = _get_ts_variables(df)
write_metadata(
base_dir=base_dir, ts_variables=ts_variables, tstore_structure=tstore_structure
base_dir=base_dir,
ts_variables=ts_variables,
tstore_structure=tstore_structure,
)


def write_tstore(
df, base_dir, partition_str=None, tstore_structure="id-var", overwrite=True
df,
base_dir,
partition_str=None,
tstore_structure="id-var",
overwrite=True,
):
"""Write TStore from TSDF object."""
# Checks
Expand Down
1 change: 0 additions & 1 deletion tstore/tsdt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 16:36:54 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/tslong/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 16:37:13 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/tslong/dask.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 17:57:53 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/tslong/lance.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 17:58:15 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/tslong/pandas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 17:57:40 2023.
Expand Down
1 change: 0 additions & 1 deletion tstore/tslong/polars.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 17:58:05 2023.
Expand Down
5 changes: 3 additions & 2 deletions tstore/tslong/pyarrow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 17:57:14 2023.
Expand Down Expand Up @@ -81,7 +80,9 @@ def _join_tables(left_table, right_table):
"""Joining functions of pyarrow tables."""
# TODO: update keys to 'time'
return left_table.join(
right_table, keys=["timestamp", "tstore_id"], join_type="full outer"
right_table,
keys=["timestamp", "tstore_id"],
join_type="full outer",
)


Expand Down
1 change: 0 additions & 1 deletion tstore/tswide/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 16:37:25 2023.
Expand Down
1 change: 0 additions & 1 deletion tutorials/00_example_ts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 11 20:56:07 2023.
Expand Down
Loading

0 comments on commit 90aa416

Please sign in to comment.