Skip to content

Commit e67b010

Browse files
committed
Statistics str, rich_repr
1 parent 45841d6 commit e67b010

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/physt/_construction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,13 @@ def calculate_1d_frequencies(
479479
stats = Statistics()
480480
else:
481481
stats = Statistics(
482-
sum=(data_array * weights_array).sum(),
483-
sum2=(data_array**2 * weights_array).sum(),
482+
sum=float((data_array * weights_array).sum()),
483+
sum2=float((data_array**2 * weights_array).sum()),
484484
min=float(data_array.min()),
485485
max=float(data_array.max()),
486486
weight=float(weights_array.sum()),
487487
# TODO: Support median with weights?
488-
median=np.median(data_array) if equal_weights else np.nan,
488+
median=float(np.median(data_array)) if equal_weights else np.nan,
489489
)
490490
return frequencies, errors2, underflow, overflow, stats
491491

src/physt/statistics.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ class Statistics:
4242
def mean(self) -> float:
4343
"""Statistical mean of all values entered into histogram (weighted)."""
4444
try:
45-
return self.sum / self.weight
45+
return float(self.sum / self.weight)
4646
except ZeroDivisionError:
4747
return np.nan
4848

4949
@property
5050
def std(self) -> float:
5151
"""Standard deviation of all values entered into histogram."""
52-
return np.sqrt(self.variance)
52+
return float(np.sqrt(self.variance))
5353

5454
@property
5555
def variance(self) -> float:
@@ -59,7 +59,7 @@ def variance(self) -> float:
5959
separate from bin contents.
6060
"""
6161
if self.weight > 0:
62-
return (self.sum2 - self.sum**2 / self.weight) / self.weight
62+
return float((self.sum2 - self.sum**2 / self.weight) / self.weight)
6363
return np.nan
6464

6565
def __add__(self, other: Any) -> Statistics:
@@ -97,6 +97,17 @@ def __eq__(self, other):
9797
and np.array_equal(self.median, other.median, equal_nan=True)
9898
)
9999

100+
def __rich_repr__(self):
101+
yield "mean", self.mean
102+
yield "std", self.std
103+
yield "min", self.min
104+
yield "max", self.max
105+
yield "total", self.weight
106+
107+
def __str__(self):
108+
rich_str = ", ".join(f"{key}={value}" for key, value in self.__rich_repr__())
109+
return f"Statistics({rich_str})"
110+
100111

101112
INVALID_STATISTICS: Statistics = Statistics(
102113
sum=np.nan, sum2=np.nan, min=np.nan, max=np.nan, weight=np.nan

tests/test_stats.py renamed to tests/test_statistics.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -26,7 +28,7 @@ def simple_h1_without_stats(simple_h1: Histogram1D) -> Histogram1D:
2628

2729
@pytest.mark.parametrize("range_", [None, pytest.param((1.5, 2.5), id="limiting")])
2830
@pytest.mark.parametrize("use_weights", [False, True])
29-
class TestStatisticsComputation:
31+
class TestStatistics:
3032
@pytest.fixture
3133
def histogram(
3234
self,
@@ -83,6 +85,12 @@ def test_std(self, histogram, use_weights):
8385
else:
8486
assert np.allclose(histogram.statistics.std, np.sqrt(5 / 4))
8587

88+
def test_str(self, histogram, use_weights):
89+
str_repr = str(histogram.statistics)
90+
pattern = re.compile(r"^Statistics\(mean=[0-9.\-]+, std=[0-9.\-]+, min=[0-9.\-]+, max=[0-9.\-]+, total=[0-9.\-]+\)$")
91+
match = pattern.match(str_repr)
92+
x = 7
93+
8694

8795
class TestEmptyHistogram:
8896
def test_zero_statistics(self, empty_h1):

0 commit comments

Comments
 (0)