def samples(self): """Return all stored samples.""" with self._mutex: if self._visual_range_up_to_date: return list(self._samples) # calc visual range visual_range = _VisualRange() max_count = 0 for sample in self._samples: histogram = sample.value if histogram.count == 0: # ignore empty tensor continue max_count = max(histogram.count, max_count) visual_range.update(histogram.max, histogram.min) if visual_range.max == visual_range.min and not max_count: logger.info("Max equals to min. Count is zero.") bins = calc_histogram_bins(max_count) # update visual range logger.debug( "Visual histogram: min %s, max %s, bins %s, max_count %s.", visual_range.min, visual_range.max, bins, max_count) for sample in self._samples: histogram = sample.value histogram.set_visual_range(visual_range.max, visual_range.min, bins) self._visual_range_up_to_date = True return list(self._samples)
def __init__(self, buckets, max_val, min_val, count): self._visual_max = max_val self._visual_min = min_val self._count = count self._original_buckets = buckets # default bin number self._visual_bins = calc_histogram_bins(count) # Note that tuple is immutable, so sharing tuple is often safe. self._re_sampled_buckets = ()
def __init__(self, histogram_message: Summary.Histogram): self._msg = histogram_message original_buckets = [ Bucket(bucket.left, bucket.width, bucket.count) for bucket in self._msg.buckets ] # Ensure buckets are sorted from min to max. original_buckets.sort(key=lambda bucket: bucket.left) self._original_buckets = tuple(original_buckets) self._count = sum(bucket.count for bucket in self._original_buckets) self._max = _mask_invalid_number(histogram_message.max) self._min = _mask_invalid_number(histogram_message.min) self._visual_max = self._max self._visual_min = self._min # default bin number self._visual_bins = calc_histogram_bins(self._count) # Note that tuple is immutable, so sharing tuple is often safe. self._re_sampled_buckets = ()
def calc_original_buckets(np_value, stats): """ Calculate buckets from tensor data. Args: np_value (numpy.ndarray): An numpy.ndarray of tensor data. stats (Statistics): An instance of Statistics about tensor data. Returns: list, a list of bucket about tensor data. Raises: ParamValueError, If np_value or stats is None. """ if np_value is None or stats is None: raise ParamValueError("Invalid input. np_value or stats is None.") valid_count = stats.count - stats.nan_count - stats.neg_inf_count - stats.pos_inf_count if not valid_count: return [] bins = calc_histogram_bins(valid_count) first_edge, last_edge = stats.min, stats.max if not first_edge < last_edge: first_edge -= 0.5 last_edge += 0.5 bins = np.linspace(first_edge, last_edge, bins + 1, dtype=np_value.dtype) hists, edges = np.histogram(np_value, bins=bins) buckets = [] for hist, edge1, edge2 in zip(hists, edges, edges[1:]): bucket = Bucket(edge1, edge2 - edge1, hist) buckets.append(bucket) return buckets