Skip to content

Commit

Permalink
upd: Changed Pandas references to native
Browse files Browse the repository at this point in the history
Made alternatives to pd.Interval, pd.Timestamp and pd.Timedelta
  • Loading branch information
Miksus committed Jul 12, 2022
1 parent 840463d commit 3ee56a2
Show file tree
Hide file tree
Showing 19 changed files with 214 additions and 132 deletions.
10 changes: 5 additions & 5 deletions rocketry/core/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import threading
from queue import Empty

import pandas as pd
from pydantic import BaseModel, Field, PrivateAttr, validator

from rocketry._base import RedBase
from rocketry.core.condition import BaseCondition, AlwaysFalse, All
from rocketry.core.time import TimePeriod
from rocketry.core.parameters import Parameters
from rocketry.core.log import TaskAdapter
from rocketry.core.time.utils import to_timedelta
from rocketry.core.utils import is_pickleable, filter_keyword_args, is_main_subprocess
from rocketry.exc import SchedulerRestart, SchedulerExit, TaskInactionException, TaskTerminationException
from rocketry.core.meta import _register
Expand Down Expand Up @@ -91,7 +91,7 @@ class Task(RedBase, BaseModel):
>= 40 if they require loaded tasks,
>= 50 if they require loaded extensions.
By default 0
timeout : str, int, pd.Timedelta, optional
timeout : str, int, timedelta, optional
If the task has not run in given timeout
the task will be terminated. Only applicable
for tasks with execution='process' or
Expand Down Expand Up @@ -165,7 +165,7 @@ class Config:
force_run: bool = False
force_termination: bool = False
status: Optional[Literal['run', 'fail', 'success', 'terminate', 'inaction']] = Field(description="Latest status of the task")
timeout: Optional[pd.Timedelta]
timeout: Optional[datetime.timedelta]

parameters: Parameters = Parameters()

Expand Down Expand Up @@ -222,9 +222,9 @@ def parse_logger_name(cls, value, values):
@validator('timeout', pre=True, always=True)
def parse_timeout(cls, value, values):
if value == "never":
return pd.Timedelta.max.to_pytimedelta()
return datetime.timedelta.max
elif value is not None:
return pd.Timedelta(value).to_pytimedelta()
return to_timedelta(value)
else:
return value

Expand Down
30 changes: 14 additions & 16 deletions rocketry/core/time/anchor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from typing import Union
from abc import abstractmethod

import pandas as pd

from .utils import to_nanoseconds, timedelta_to_str, to_dict
from .utils import to_nanoseconds, timedelta_to_str, to_dict, to_timedelta
from .base import TimeInterval


Expand Down Expand Up @@ -76,7 +74,7 @@ def anchor_dict(self, d, **kwargs):
kwargs = {key: val for key, val in d.items() if key in comps}
return to_nanoseconds(**kwargs)

def anchor_dt(self, dt: Union[datetime, pd.Timestamp], **kwargs) -> int:
def anchor_dt(self, dt: datetime, **kwargs) -> int:
"Turn datetime to nanoseconds according to the scope (by removing higher time elements)"
components = self.components
components = components[components.index(self._scope) + 1:]
Expand Down Expand Up @@ -113,7 +111,7 @@ def set_end(self, val, time_point=False):

@property
def start(self):
delta = pd.Timedelta(self._start, unit="ns")
delta = to_timedelta(self._start, unit="ns")
repr_scope = self.components[self.components.index(self._scope) + 1]
return timedelta_to_str(delta, default_scope=repr_scope)

Expand All @@ -123,7 +121,7 @@ def start(self, val):

@property
def end(self):
delta = pd.Timedelta(self._end, unit="ns")
delta = to_timedelta(self._end, unit="ns")
repr_scope = self.components[self.components.index(self._scope) + 1]
return timedelta_to_str(delta, default_scope=repr_scope)

Expand Down Expand Up @@ -198,7 +196,7 @@ def next_start(self, dt):
# dt
# -->----------<----------->--------------<-
# start | end start | end
offset = pd.Timedelta(int(ns_start) - int(ns), unit="ns")
offset = to_timedelta(int(ns_start) - int(ns), unit="ns")
else:
# not in period, later than start
# dt
Expand All @@ -215,7 +213,7 @@ def next_start(self, dt):
# --<---------->-----------<-------------->--
# end | start end | start
ns_scope = self.get_scope_forward(dt)
offset = pd.Timedelta(int(ns_start) - int(ns) + ns_scope, unit="ns")
offset = to_timedelta(int(ns_start) - int(ns) + ns_scope, unit="ns")
return dt + offset

def next_end(self, dt):
Expand All @@ -239,7 +237,7 @@ def next_end(self, dt):
# dt
# --<---------->-----------<-------------->--
# end | start end | start
offset = pd.Timedelta(int(ns_end) - int(ns), unit="ns")
offset = to_timedelta(int(ns_end) - int(ns), unit="ns")
else:
# not in period, over night
# dt
Expand All @@ -256,7 +254,7 @@ def next_end(self, dt):
# -->----------<----------->--------------<-
# start | end start | end
ns_scope = self.get_scope_forward(dt)
offset = pd.Timedelta(int(ns_end) - int(ns) + ns_scope, unit="ns")
offset = to_timedelta(int(ns_end) - int(ns) + ns_scope, unit="ns")
return dt + offset

def prev_start(self, dt):
Expand All @@ -281,7 +279,7 @@ def prev_start(self, dt):
# -->----------<----------->--------------<-
# start | end start | end
ns_scope = self.get_scope_back(dt)
offset = pd.Timedelta(int(ns_start) - int(ns) - ns_scope, unit="ns")
offset = to_timedelta(int(ns_start) - int(ns) - ns_scope, unit="ns")
else:
# not in period, later than start
# dt
Expand All @@ -297,7 +295,7 @@ def prev_start(self, dt):
# dt
# --<---------->-----------<-------------->--
# end | start end | start
offset = pd.Timedelta(int(ns_start) - int(ns), unit="ns")
offset = to_timedelta(int(ns_start) - int(ns), unit="ns")
return dt + offset

def prev_end(self, dt):
Expand All @@ -322,7 +320,7 @@ def prev_end(self, dt):
# --<---------->-----------<-------------->--
# end | start end | start
ns_scope = self.get_scope_back(dt)
offset = pd.Timedelta(int(ns_end) - int(ns) - ns_scope, unit="ns")
offset = to_timedelta(int(ns_end) - int(ns) - ns_scope, unit="ns")
else:
# not in period, over night
# dt
Expand All @@ -338,7 +336,7 @@ def prev_end(self, dt):
# dt
# -->----------<----------->--------------<-
# start | end start | end
offset = pd.Timedelta(int(ns_end) - int(ns), unit="ns")
offset = to_timedelta(int(ns_end) - int(ns), unit="ns")

return dt + offset

Expand All @@ -354,8 +352,8 @@ def __str__(self):
scope = self._scope
repr_scope = self.components[self.components.index(self._scope) + 1]

to_start = pd.Timedelta(start_ns, unit="ns")
to_end = pd.Timedelta(end_ns, unit="ns")
to_start = to_timedelta(start_ns, unit="ns")
to_end = to_timedelta(end_ns, unit="ns")

start_str = timedelta_to_str(to_start, default_scope=repr_scope)
end_str = timedelta_to_str(to_end, default_scope=repr_scope)
Expand Down
65 changes: 32 additions & 33 deletions rocketry/core/time/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
from typing import Callable, Dict, List, Pattern, Union
import itertools

import pandas as pd

from rocketry._base import RedBase
from rocketry.core.meta import _add_parser
from rocketry.pybox.time import to_datetime, to_timedelta, Interval
from rocketry.session import Session

PARSERS: Dict[Union[str, Pattern], Union[Callable, 'TimePeriod']] = {}
Expand All @@ -34,7 +33,7 @@ class TimePeriod(RedBase, metaclass=_TimeMeta):
is in a given time span.
"""

resolution = pd.Timestamp.resolution
resolution = datetime.timedelta.resolution
min = datetime.datetime(1970, 1, 3, 2, 0)
max = datetime.datetime(2260, 1, 1, 0, 0)

Expand Down Expand Up @@ -133,7 +132,7 @@ def prev_end(self, dt):
raise NotImplementedError("Contains not implemented.")

@abstractmethod
def from_between(start, end) -> pd.Interval:
def from_between(start, end) -> Interval:
raise NotImplementedError("__between__ not implemented.")

def rollforward(self, dt):
Expand All @@ -142,21 +141,21 @@ def rollforward(self, dt):
start = self.rollstart(dt)
end = self.next_end(dt)

start = pd.Timestamp(start)
end = pd.Timestamp(end)
start = to_datetime(start)
end = to_datetime(end)

return pd.Interval(start, end, closed="both")
return Interval(start, end, closed="both")

def rollback(self, dt) -> pd.Interval:
def rollback(self, dt) -> Interval:
"Get previous time interval of the period"

end = self.rollend(dt)
start = self.prev_start(dt)

start = pd.Timestamp(start)
end = pd.Timestamp(end)
start = to_datetime(start)
end = to_datetime(end)

return pd.Interval(start, end, closed="both")
return Interval(start, end, closed="both")

def __eq__(self, other):
"Test whether self and other are essentially the same periods"
Expand Down Expand Up @@ -188,8 +187,8 @@ def __init__(self, past=None, future=None, kws_past=None, kws_future=None):
kws_past = {} if kws_past is None else kws_past
kws_future = {} if kws_future is None else kws_future

self.past = abs(pd.Timedelta(past, **kws_past))
self.future = abs(pd.Timedelta(future, **kws_future))
self.past = abs(to_timedelta(past, **kws_past))
self.future = abs(to_timedelta(future, **kws_future))

@abstractmethod
def __contains__(self, dt):
Expand All @@ -202,16 +201,16 @@ def __contains__(self, dt):
def rollback(self, dt):
"Get previous interval (including currently ongoing)"
start = dt - abs(self.past)
start = pd.Timestamp(start)
end = pd.Timestamp(dt)
return pd.Interval(start, end)
start = to_datetime(start)
end = to_datetime(dt)
return Interval(start, end)

def rollforward(self, dt):
"Get next interval (including currently ongoing)"
end = dt + abs(self.future)
start = pd.Timestamp(dt)
end = pd.Timestamp(end)
return pd.Interval(start, end)
start = to_datetime(dt)
end = to_datetime(end)
return Interval(start, end)

def __eq__(self, other):
"Test whether self and other are essentially the same periods"
Expand All @@ -232,7 +231,7 @@ def __str__(self):
def __repr__(self):
return f"TimeDelta(past={repr(self.past)}, future={repr(self.future)})"

def all_overlap(times:List[pd.Interval]):
def all_overlap(times:List[Interval]):
return all(a.overlaps(b) for a, b in itertools.combinations(times, 2))

def get_overlapping(times):
Expand All @@ -246,7 +245,7 @@ def get_overlapping(times):

start = max(starts)
end = min(ends)
return pd.Interval(start, end)
return Interval(start, end)

class All(TimePeriod):

Expand Down Expand Up @@ -349,7 +348,7 @@ def rollback(self, dt):
period.rollback(start - datetime.datetime.resolution)
for period in self.periods
]
if any(pd.Interval(start, end).overlaps(interval) for interval in next_intervals):
if any(Interval(start, end).overlaps(interval) for interval in next_intervals):
# Example:
# A: <-->
# B: <---> <--->
Expand All @@ -358,7 +357,7 @@ def rollback(self, dt):
extended = self.rollback(start - datetime.datetime.resolution)
start = extended.left

return pd.Interval(start, end)
return Interval(start, end)

def rollforward(self, dt):
intervals = [
Expand All @@ -377,7 +376,7 @@ def rollforward(self, dt):
for period in self.periods
]

if any(pd.Interval(start, end).overlaps(interval) for interval in next_intervals):
if any(Interval(start, end).overlaps(interval) for interval in next_intervals):
# Example:
# A: <-->
# B: <---> <--->
Expand All @@ -386,7 +385,7 @@ def rollforward(self, dt):
extended = self.rollforward(end + datetime.datetime.resolution)
end = extended.right

return pd.Interval(start, end)
return Interval(start, end)

def __eq__(self, other):
# self | other
Expand All @@ -404,20 +403,20 @@ def __init__(self, start=None, end=None):
self.end = end if end is not None else self.max

def rollback(self, dt):
dt = pd.Timestamp(dt)
start = pd.Timestamp(self.start)
dt = to_datetime(dt)
start = to_datetime(self.start)
if start > dt:
# The actual interval is in the future
return pd.Interval(self.min, self.min)
return pd.Interval(start, dt)
return Interval(self.min, self.min)
return Interval(start, dt)

def rollforward(self, dt):
dt = pd.Timestamp(dt)
end = pd.Timestamp(self.end)
dt = to_datetime(dt)
end = to_datetime(self.end)
if end < dt:
# The actual interval is already gone
return pd.Interval(self.max, self.max, closed="both")
return pd.Interval(dt, end, closed="both")
return Interval(self.max, self.max, closed="both")
return Interval(dt, end, closed="both")

@property
def is_max_interval(self):
Expand Down
7 changes: 4 additions & 3 deletions rocketry/pybox/query/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Iterable, Iterator
import datetime
import pandas as pd

from rocketry.pybox.time.convert import to_datetime, to_timedelta

class QueryBase:

Expand Down Expand Up @@ -105,10 +106,10 @@ def __invert__(self):
return Not(self)

def _to_comparable(self, left, right):
dt_cls = (datetime.datetime, pd.Timestamp) # Note we test pd.Timestamp due to test mocking
dt_cls = (datetime.datetime,)
is_datetime = isinstance(left, dt_cls) or isinstance(right, dt_cls)
if is_datetime:
return pd.Timestamp(left), pd.Timestamp(right)
return to_datetime(left), to_datetime(right)
else:
return left, right

Expand Down
3 changes: 2 additions & 1 deletion rocketry/pybox/time/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .convert import to_nanoseconds, to_timedelta
from .convert import to_nanoseconds, to_timedelta, to_datetime
from .interval import Interval
Loading

0 comments on commit 3ee56a2

Please sign in to comment.