# -*- coding: utf-8 -*- """ @file temporal.py @brief provides functionality relating to searching the catalog temporally @par History - Brian Helgans, Dec 02 2019, updated to add log name as input """ from typing import NamedTuple from Sample_Execution.fileid import Catalog_File from datetime import timedelta class SearchResult(NamedTuple): ''' A named tuple that can be accessed like searchresult.first to obtain items in the tuple. It terms of appearance its similar to using a struct in C ''' interval: timedelta first: Catalog_File middle: Catalog_File last: Catalog_File def search_for_neighbors(file_middle, catalog, max_search_distance=9, epsilon_for_interval = timedelta(seconds=60)): ''' Searches for Neighbors on each side within a given a search distance within the timestamp sorted catalog :param file_middle: middle file used for the timestamp of the search. :type file_middle: Sample_Execution.fileid.Catalog_File :param catalog: catalog for the search :type catalog: Sample_Execution.fileid.Catalog :param max_search_distance: max distance on either side of the timestamp sorted catalog :type max_search_distance: int :param epsilon_for_interval: timedelta to estimate when times are close enough to being equal. :type epsilon_for_interval: datetime.timedelta ''' neighbors = [] #we need to search some distance from the middle image for a match for search_distance in range(1,max_search_distance+1): #find it in the catalogue middle_index = catalog.time_index(file_middle.id.timestring) #attempt to find the first and last in the catalogue prior_index = middle_index - search_distance next_index = middle_index + search_distance if prior_index < 0: continue file_first = catalog.get_with_time_index(time_index=prior_index, catid=file_middle.id) file_last = catalog.get_with_time_index(time_index=next_index, catid=file_middle.id) if not file_first or not file_last: continue timedelta1 = file_middle.datetime - file_first.datetime timedelta2 = file_last.datetime - file_middle.datetime if (timedelta1 - timedelta2) > epsilon_for_interval : continue maxtimedelta = max(timedelta1,timedelta2) minutes_interval = round(maxtimedelta.total_seconds() / 60.0) neighbors.append( SearchResult( interval=minutes_interval, first=file_first, middle=file_middle, last=file_last)) return neighbors