Skip to content

Instantly share code, notes, and snippets.

@saulpw
Created April 30, 2022 18:59
Show Gist options
  • Save saulpw/e28f882b73022dc22e136fab1bf9effc to your computer and use it in GitHub Desktop.
Save saulpw/e28f882b73022dc22e136fab1bf9effc to your computer and use it in GitHub Desktop.
Time alignment problem
from collections import defaultdict
def get_time_offsets(events):
'''events: list of time pairs (dev1, t1, dev2, t2) such that event at t1 guaranteed happens before event at t2
Return dict of dev:(offset, error)
then, adding offset to timestamps from its dev translates them into a common timespace.
'''
devs = defaultdict(lambda: ([], [])) # [dev] -> (events with dev time before, events with dev time after)
for e in events:
dev1, t1, dev2, t2 = e
devs[dev1][0].append(e)
devs[dev2][1].append(e)
highs = defaultdict(dict)
lows = defaultdict(dict)
for dev1, t1, dev2, t2 in events:
highs[dev1][dev2] = min(t2-t1, highs[dev1][dev2])
lows[dev2][dev1] = max(t1-t2, lows[dev2][dev1])
print(highs)
print(lows)
return {}
A,B,C='ABC'
# timeline:
# A 17
# B 18 20 22
# C 30
events = [
(B, 18, C, 30), # B+18 < C+30
(C, 30, B, 20), # C+30 < B+20
(B, 20, A, 17), # B+20 < A+17
(A, 17, B, 22), # A+17 < B+22
]
#C+10 < B < C+12
# B < A-3
#A < B+5
golden_offsets = [{
A: (4, 1),
B: (0, 0),
C: (-11, 1)
}, {
A: (0, 1),
B: (-4, 0),
C: (-15, 1)
}, {
A: (15, 1),
B: (11, 0),
C: (0, 1)
}]
assert get_time_offsets(events) in golden_offsets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment