def determine_sampling_interval(duration, loop): """Determines the loop sampling interval in milliseconds Args: duration: loop duration in milliseconds loop: loop dictionary Returns: float: the sampling interval in milliseconds """ interval = get_from_dict_if_exists('dPeriod', loop) avg_interval = get_from_dict_if_exists('dAvgPeriodDiff', loop) if interval is None or interval <= 0: interval = avg_interval else: avg_interval_set = avg_interval is not None and avg_interval > 0 if round(avg_interval) != round(interval) and avg_interval_set: message = ("Reported average frame interval (%.1f ms) doesn't" " match the set interval (%.1f ms). Using the average" " now.") warnings.warn(message % (avg_interval, interval), RuntimeWarning) interval = avg_interval if interval is None or interval <= 0: # In some cases, both keys are not saved. Then try to calculate it. interval = guess_sampling_from_loops(duration, loop) return interval
def test_get_from_dict_if_exists(self): test_dict = { six.b('existing'): 'test', 'string': 'test2' } self.assertIsNone(get_from_dict_if_exists('nowhere', test_dict)) self.assertEqual(get_from_dict_if_exists('existing', test_dict), 'test') self.assertEqual(get_from_dict_if_exists('string', test_dict, convert_key_to_binary=False), 'test2')
def _determine_sampling_interval(duration, loop): """Determines the loop sampling interval in milliseconds Args: duration: loop duration in milliseconds loop: loop dictionary Returns: float: the sampling interval in milliseconds """ interval = get_from_dict_if_exists('dPeriod', loop) if interval is None or interval <= 0: # Use a fallback if it is still not found interval = get_from_dict_if_exists('dAvgPeriodDiff', loop) if interval is None or interval <= 0: # In some cases, both keys are not saved. Then try to calculate it. interval = RawMetadata._guess_sampling_from_loops(duration, loop) return interval
def _parse_loop_data(self, loop_data): """Parse the experimental loop data Args: loop_data: dictionary of experiment loops Returns: list: list of the parsed loops """ loops = get_loops_from_data(loop_data) # take into account the absolute time in ms time_offset = 0 parsed_loops = [] for loop in loops: # duration of this loop duration = get_from_dict_if_exists('dDuration', loop) or 0 interval = determine_sampling_interval(duration, loop) # if duration is not saved, infer it duration = self.get_duration_from_interval_and_loops( duration, interval, loop) # uiLoopType == 6 is a stimulation loop is_stimulation = get_from_dict_if_exists('uiLoopType', loop) == 6 parsed_loop = { 'start': time_offset, 'duration': duration, 'stimulation': is_stimulation, 'sampling_interval': interval } parsed_loops.append(parsed_loop) # increase the time offset time_offset += duration return parsed_loops
def guess_sampling_from_loops(duration, loop): """ In some cases, both keys are not saved. Then try to calculate it. Args: duration: the total duration of the loop loop: the raw loop data Returns: float: the guessed sampling interval in milliseconds """ number_of_loops = get_from_dict_if_exists('uiCount', loop) number_of_loops = number_of_loops if number_of_loops is not None and number_of_loops > 0 else 1 interval = duration / number_of_loops return interval
def get_duration_from_interval_and_loops(self, duration, interval, loop): """Infers the duration of the loop from the number of measurements and the interval Args: duration: loop duration in milliseconds duration: measurement interval in milliseconds loop: loop dictionary Returns: float: the loop duration in milliseconds """ if duration == 0 and interval > 0: number_of_loops = get_from_dict_if_exists('uiCount', loop) number_of_loops = number_of_loops if number_of_loops is not None and number_of_loops > 0 else 1 duration = interval * number_of_loops return duration