def __compute__(fnc, classes, arguments, classification, version): if classification is None: if version is Average.micro: # Micro-average numerator, denominator = 0, 0 for classification in classes: arguments['classification'] = classification arguments['return_parts'] = True class_numerator, class_denominator = fnc(**arguments) numerator += class_numerator denominator += class_denominator if numerator == 0: return 0 else: return Decimal(numerator) / denominator elif version is Average.macro: # Macro-average values = list() for classification in classes: arguments['classification'] = classification value = fnc(**arguments) values.append(value) return mean(values) else: raise SegmentationMetricError('Unrecognized type of averaging;\ expected Average.micro or Average.macro') else: return fnc(**arguments)
def summarize(pairs): ''' Takes a list of values and returns the mean, standard deviation, variance, standard error, and number of values. :param pairs: List of numerical values :type pairs: list ''' return mean(pairs.values()), std(pairs.values()), var(pairs.values()), \ stderr(pairs.values()), len(pairs)
def __compute_window_size__(reference, fnc_round, boundary_format): ''' Compute a window size from a dict of segment masses. :param masses: A dict of segment masses. :type masses: dict ''' all_masses = list() # Define fnc def __list_coder_masses__(inner_coder_masses): ''' Recursively collect all masses. :param inner_coder_masses: Either a dict of dicts, or dict of a list of masses. :type inner_coder_masses: dict or list ''' if hasattr(inner_coder_masses, 'items'): for cur_inner_coder_masses in inner_coder_masses.values(): __list_coder_masses__(cur_inner_coder_masses) elif hasattr(inner_coder_masses, '__iter__') and not isinstance(inner_coder_masses, str): all_masses.extend(inner_coder_masses) else: raise SegmentationMetricError('Expected either a dict-like \ collection of segmentations or a segmentation as a list-like object') if boundary_format == BoundaryFormat.position: reference = convert_positions_to_masses(reference) # Recurse and list all masses __list_coder_masses__(reference) # Convert to floats all_masses = [Decimal(mass) for mass in all_masses] # Calculate avg = mean(all_masses) / Decimal('2') window_size = int(fnc_round(avg)) return window_size if window_size > 1 else 2
def test_mean_0(self): ''' Tests population mean. ''' self.assertEqual(0, mean([]))
def test_mean(self): ''' Tests population mean. ''' self.assertEqual(5, mean([2, 4, 4, 4, 5, 5, 7, 9]))