Skip to content

Commit a61ebf5

Browse files
committed
time_series.compstat returns values
It was previously writing directly to a file, preventing users from using the computation in their own scripts. The file is produced by time_series.cmd when using the command line.
1 parent 1a931a1 commit a61ebf5

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

stagpy/time_series.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ def compstat(sdat, *names, tstart=None, tend=None):
9292
available data.
9393
tend (float): ending time. Set to None to stop at the end of available
9494
data.
95+
Returns:
96+
:class:`pandas.DataFrame`: computed statistics with 'mean' and 'rms' as
97+
index and variable names as columns.
9598
"""
9699
stats = pd.DataFrame(columns=names, index=['mean', 'rms'])
97100
for name in names:
@@ -101,7 +104,7 @@ def compstat(sdat, *names, tstart=None, tend=None):
101104
stats.loc['mean', name] = mean
102105
stats.loc['rms', name] = np.sqrt(np.trapz((data - mean)**2, x=time) /
103106
delta_time)
104-
stats.to_csv('statistics.dat')
107+
return stats
105108

106109

107110
def cmd():
@@ -130,4 +133,6 @@ def cmd():
130133

131134
if conf.time.compstat:
132135
names = conf.time.compstat.replace(',', ' ').split()
133-
compstat(sdat, *names, tstart=conf.time.tstart, tend=conf.time.tend)
136+
stats = compstat(sdat, *names, tstart=conf.time.tstart,
137+
tend=conf.time.tend)
138+
stats.to_csv('statistics.dat')

tests/test_time_series.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ def test_get_time_series_extra(sdat):
1212
series, time, meta = sdat.tseries['dTdt']
1313
assert series.shape == time.shape == (sdat.tseries.time.size - 1,)
1414
assert isinstance(meta, stagpy.phyvars.Vart)
15+
16+
17+
def test_compstat(sdat):
18+
variables = ['Tmean', 'vrms']
19+
stats = stagpy.time_series.compstat(sdat, *variables)
20+
assert list(stats.columns) == variables
21+
assert list(stats.index) == ['mean', 'rms']

0 commit comments

Comments
 (0)