-
Notifications
You must be signed in to change notification settings - Fork 303
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
Change combine_metadata to average any 'time' fields #473
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
09dee51
Fix start_time in ahi_hsd to be the scheduled time
djhoese d42f269
Add documentation for ahi_hsd use of scheduled time
djhoese ca5e7d0
Add time averaging to combine_metadata
djhoese 1dea078
Fix ahi_hsd documentation and timestamp lambda functions
djhoese 71453f3
Add ability to turn off datetime average in combine_metadata
djhoese cbcefae
Fix python 2 compatibility in combine_metadata kwargs
djhoese d0a994e
Improve combine_metadata documentation
djhoese File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,11 @@ | |
"""Dataset objects. | ||
""" | ||
|
||
import sys | ||
import logging | ||
import numbers | ||
from collections import namedtuple | ||
from datetime import datetime | ||
|
||
import numpy as np | ||
|
||
|
@@ -46,6 +48,30 @@ def id(self): | |
return DatasetID.from_dict(self.attrs) | ||
|
||
|
||
def average_datetimes(dt_list): | ||
"""Average a series of datetime objects. | ||
|
||
.. note:: | ||
|
||
This function assumes all datetime objects are naive and in the same | ||
time zone (UTC). | ||
|
||
Args: | ||
dt_list (iterable): Datetime objects to average | ||
|
||
Returns: Average datetime as a datetime object | ||
|
||
""" | ||
if sys.version_info < (3, 3): | ||
# timestamp added in python 3.3 | ||
import time | ||
timestamp_func = lambda x: time.mktime(x.timetuple()) | ||
else: | ||
timestamp_func = lambda x: x.timestamp() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E731 do not assign a lambda expression, use a def |
||
total = [timestamp_func(dt) for dt in dt_list] | ||
return datetime.fromtimestamp(sum(total) / len(total)) | ||
|
||
|
||
def combine_metadata(*metadata_objects): | ||
"""Combine the metadata of two or more Datasets. | ||
|
||
|
@@ -58,8 +84,7 @@ def combine_metadata(*metadata_objects): | |
""" | ||
shared_keys = None | ||
info_dicts = [] | ||
# grab all of the dictionary objects provided and make a set of the shared | ||
# keys | ||
# grab all of the dictionary objects provided and make a set of the shared keys | ||
for metadata_object in metadata_objects: | ||
if isinstance(metadata_object, dict): | ||
metadata_dict = metadata_object | ||
|
@@ -82,6 +107,8 @@ def combine_metadata(*metadata_objects): | |
if any_arrays: | ||
if all(np.all(val == values[0]) for val in values[1:]): | ||
shared_info[k] = values[0] | ||
elif 'time' in k and isinstance(values[0], datetime): | ||
shared_info[k] = average_datetimes(values) | ||
elif all(val == values[0] for val in values[1:]): | ||
shared_info[k] = values[0] | ||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E731 do not assign a lambda expression, use a def