def get_valids(groups, train_last_timestamps, test_summary, questions, chains): supercollisions = grouped_find_overlapping_segments( test_summary, questions, groups) test_first_timestamps = { x: summary[-2] for x, summary in test_summary.iteritems() } initial_valids = { question: { p for p in groups[proposed] if is_valid(infra.shift_train_timestamp(train_last_timestamps[p]), test_first_timestamps[question]) } for question, proposed in questions.iteritems() } valids = get_immediate_valids(groups, questions, initial_valids, supercollisions) singletons = {k for k, v in valids.iteritems() if len(v) == 1} while True: last = len(singletons) print last tainted_chains = [c for c in chains if set(c) & singletons] for chain in tainted_chains: x = list(set(chain) & singletons)[0] color = list(valids[x])[0] for x in chain: valids[x] = {color} valids = get_immediate_valids(groups, questions, valids, supercollisions) singletons = {k for k, v in valids.iteritems() if len(v) == 1} if len(singletons) == last: break return valids
def validate_questions(questions, train_last_timestamps, test_first_timestamps): return { test_id: is_valid(infra.shift_train_timestamp(train_last_timestamps[train_id]), test_first_timestamps[test_id]) for test_id, train_id in questions.iteritems() }
def validate_chain(chain, questions, train_last_timestamps, test_first_timestamps): first_timestamp = test_first_timestamps[chain[0]] return { test_id: (infra.shift_train_timestamp( is_validtrain_last_timestamps[questions[test_id]]), first_timestamp) for test_id in chain }
def match_test_to_train(train_last_timestamps, test_first_timestamps): samples = [(timestamp, devid) for devid, timestamp in test_first_timestamps.iteritems()] timeindex = TimeIndex(samples) train_to_test = {} for train_id, last_timestamp in train_last_timestamps.iteritems(): last_timestamp = infra.shift_train_timestamp(last_timestamp) available = [ devid for ts, devid in timeindex[last_timestamp + 0:last_timestamp + 1000] ] if len(available) == 1: train_to_test[train_id] = available[0] counts = Counter(train_to_test.itervalues()) result = { test_id: train_id for train_id, test_id in train_to_test.iteritems() if counts[test_id] == 1 } return result