Skip to content

Commit

Permalink
replacing with timezone aware defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollman committed Oct 13, 2023
1 parent 12a3be7 commit 42a12a4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
13 changes: 7 additions & 6 deletions caveclient/chunkedgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _process_timestamp(self, timestamp):
if self._default_timestamp is not None:
return self._default_timestamp
else:
return datetime.datetime.utcnow()
return datetime.datetime.now(datetime.timezone.utc)
else:
return timestamp

Expand Down Expand Up @@ -816,11 +816,10 @@ def get_latest_roots(self, root_id, timestamp=None, timestamp_future=None):
timestamp = timestamp_future

if timestamp is None:
timestamp = datetime.datetime.utcnow()
timestamp = datetime.datetime.now(datetime.timezone.utc)

# or if timestamp_root is less than timestamp_future

if timestamp is None or timestamp_root < timestamp:
if (timestamp is None) or (timestamp_root < timestamp):
lineage_graph = self.get_lineage_graph(
root_id,
timestamp_past=timestamp_root,
Expand Down Expand Up @@ -1114,14 +1113,16 @@ def get_past_ids(self, root_ids, timestamp_past=None, timestamp_future=None):
def get_delta_roots(
self,
timestamp_past: datetime.datetime,
timestamp_future: datetime.datetime = datetime.datetime.utcnow(),
timestamp_future: datetime.datetime = datetime.datetime.now(
datetime.timezone.utc
),
):
"""get the list of roots that have changed between timetamp_past and timestamp_future
Args:
timestamp_past (datetime.datetime): past timepoint to query
timestamp_future (datetime.datetime, optional): future timepoint to query. Defaults to datetime.datetime.utcnow().
timestamp_future (datetime.datetime, optional): future timepoint to query. Defaults to datetime.datetime.now(datetime.timezone.utc).
Returns:
old_roots (np.ndarray): roots that have expired in that interval
Expand Down
8 changes: 4 additions & 4 deletions caveclient/materializationengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def concatenate_position_columns(df, inplace=False):

def convert_timestamp(ts: datetime):
if ts == "now":
ts = datetime.utcnow()
ts = datetime.now(timezone.utc)

if isinstance(ts, datetime):
if ts.tzinfo is None:
Expand Down Expand Up @@ -1181,7 +1181,7 @@ def live_live_query(
allow_missing_lookups (bool, optional): If there are annotations without supervoxels and rootids yet, allow results. Defaults to False.
random_sample: (int, optional) : if given, will do a tablesample of the table to return that many annotations
Example:
live_live_query("table_name",datetime.datetime.utcnow(),
live_live_query("table_name",datetime.datetime.now(datetime.timezone.utc),
joins=[[table_name, table_column, joined_table, joined_column],
[joined_table, joincol2, third_table, joincol_third]]
suffixes={
Expand Down Expand Up @@ -1350,7 +1350,7 @@ def live_query(
Args:
table: 'str'
timestamp (datetime.datetime): time to materialize (in utc)
pass datetime.datetime.utcnow() for present time
pass datetime.datetime.now(datetime.timezone.utc) for present time
filter_in_dict (dict , optional):
keys are column names, values are allowed entries.
Defaults to None.
Expand Down Expand Up @@ -1850,7 +1850,7 @@ def live_live_query(
allow_invalid_root_ids (bool, optional): If True, ignore root ids not valid at the given timestamp, otherwise raise an Error. Defaults to False.
random_sample (int, optional): If given, will do a tablesample of the table to return that many annotations
Example:
live_live_query("table_name",datetime.datetime.utcnow(),
live_live_query("table_name",datetime.datetime.now(datetime.timezone.utc),
joins=[[table_name, table_column, joined_table, joined_column],
[joined_table, joincol2, third_table, joincol_third]]
suffixes={
Expand Down
10 changes: 5 additions & 5 deletions docs/guide/materialization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,15 @@ that is more recent that the most recent version available. For convience, you


to automatically update the results of your query to a time in the future, such as now.
For example, to pass now, use ```datetime.datetime.utcnow```. Note all timestamps are in UTC
For example, to pass now, use ```datetime.datetime.now(datetime.timezone.utc)```. Note all timestamps are in UTC
throughout the codebase.

.. code:: python
import datetime
synapse_table = client.info.get_datastack_info()['synapse_table']
df=client.materialize.live_query(synapse_table,
datetime.datetime.utcnow(),
datetime.datetime.now(datetime.timezone.utc),
filter_equal_dict = {'post_pt_root_id': MYID})
This will raise an ValueError exception if the IDs passed in your filters are not valid at the timestamp given
Expand All @@ -232,11 +232,11 @@ You can also pass a timestamp directly to query_table and it will call live_quer
import datetime
synapse_table = client.info.get_datastack_info()['synapse_table']
df=client.materialize.query_table(synapse_table,
timestamp=datetime.datetime.utcnow(),
timestamp=datetime.datetime.now(datetime.timezone.utc),
filter_equal_dict = {'post_pt_root_id': MYID})
Also, keep in mind if you run multiple queries and at each time pass ``datetime.datetime.utcnow()``,
Also, keep in mind if you run multiple queries and at each time pass ``datetime.datetime.now(datetime.timezone.utc)``,
there is no gauruntee that the IDs will be consistent from query to query, as proofreading might be happening
at any time. For larger scale analysis constraining oneself to a materialized version will ensure consistent results.

Expand Down Expand Up @@ -312,7 +312,7 @@ The one required argument for ``live_query`` is the timestamp.
nuc_df = client.materialize.tables.nucleus_detection_v0(
id=my_ids
).live_query(
timestamp=datetime.datetime.utcnow(),
timestamp=datetime.datetime.now(datetime.timezone.utc),
)
The live query functions have similar but slightly different arguments: ``timestamp`` (required), ``offset``, ``limit``, ``split_positions``,
Expand Down
8 changes: 4 additions & 4 deletions tests/test_chunkedgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_get_roots(self, myclient):
url = chunkedgraph_endpoints_v1["get_roots"].format_map(endpoint_mapping)
svids = np.array([97557743795364048, 75089979126506763], dtype=np.uint64)
root_ids = np.array([864691135217871271, 864691135566275148], dtype=np.uint64)
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
query_d = package_timestamp(now)
qurl = url + "?" + urlencode(query_d)
responses.add(
Expand Down Expand Up @@ -231,7 +231,7 @@ def test_delta_roots(self, myclient):
endpoint_mapping = self._default_endpoint_map
url = chunkedgraph_endpoints_v1["delta_roots"].format_map(endpoint_mapping)

now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
timestamp_past = now - datetime.timedelta(days=1)
query_d = package_timestamp(timestamp_past, name="timestamp_past")
query_d.update(package_timestamp(now, name="timestamp_future"))
Expand Down Expand Up @@ -434,7 +434,7 @@ def test_past_ids(self, myclient):
"864691136577570580": [864691136721486702, 864691133958789149],
},
}
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
timestamp_past = now - datetime.timedelta(days=7)

query_d = package_timestamp(timestamp_past, name="timestamp_past")
Expand Down Expand Up @@ -470,7 +470,7 @@ def test_lineage_graph(self, myclient):
url = chunkedgraph_endpoints_v1["handle_lineage_graph"].format_map(
endpoint_mapping
)
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
timestamp_past = now - datetime.timedelta(days=7)

query_d = package_timestamp(timestamp_past, name="timestamp_past")
Expand Down

0 comments on commit 42a12a4

Please sign in to comment.