Skip to content

Commit 53bc788

Browse files
authored
Fix csp.stats for NumPy scalar arrays which have 0-dimensions (#576)
Signed-off-by: Adam Glustein <[email protected]>
1 parent 4b9600f commit 53bc788

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

cpp/csp/python/npstatsimpl.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class NumPyIterator
5252
NumPyIterator()
5353
{
5454
m_nd = 0;
55-
m_size = 0;
5655
m_index = 0;
5756
m_data = nullptr;
5857
m_strides = nullptr;
@@ -131,15 +130,10 @@ class NumPyIterator
131130
for( int i = 0; i < m_nd; ++i )
132131
m_stridedDimensions.emplace_back( m_strides[i] * ( m_dims[i] - 1 ) );
133132
m_data = reinterpret_cast<char*>( PyArray_DATA( arr ) );
134-
if( m_nd == 0 )
135-
m_size = 0;
136-
else
137-
m_size = std::accumulate( m_dims, m_dims + m_nd, 1, std::multiplies<int64_t>() );
138-
m_valid = ( m_size > 0 );
133+
m_valid = ( PyArray_SIZE( arr ) > 0 );
139134
}
140135

141136
int64_t m_nd;
142-
int64_t m_size;
143137
int64_t m_index;
144138
char* m_data;
145139
npy_intp* m_strides;
@@ -164,10 +158,7 @@ struct PyShape
164158
npy_intp* dims = PyArray_DIMS( arr );
165159
for( int64_t j = 0; j < nd; j++ )
166160
m_dims.emplace_back( dims[j] );
167-
if( nd == 0 )
168-
m_n = 0;
169-
else
170-
m_n = std::accumulate( std::begin( m_dims ), std::end( m_dims ), 1, std::multiplies<int64_t>() );
161+
m_n = PyArray_SIZE( arr );
171162
}
172163

173164
PyShape( PyObject* arr ) : PyShape( ( PyArrayObject* ) arr ) { }

csp/tests/test_stats.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3783,6 +3783,33 @@ def graph():
37833783
check_names=False,
37843784
)
37853785

3786+
def test_scalar_arrays(self):
3787+
# np scalar arrays have no dimensions i.e. shape=(), but do contain a valid value
3788+
def scalar_graph():
3789+
raw_data = csp.count(csp.timer(timedelta(seconds=1), True))
3790+
zero_dim_array_data = csp.apply(raw_data, lambda x: np.array(float(x)), np.ndarray)
3791+
ema = csp.stats.ema(zero_dim_array_data, halflife=timedelta(seconds=10))
3792+
sum = csp.stats.sum(zero_dim_array_data, interval=10, min_window=1)
3793+
3794+
csp.add_graph_output("ema", ema)
3795+
csp.add_graph_output("sum", sum)
3796+
3797+
N_DATA_POINTS = 50
3798+
res = csp.run(
3799+
scalar_graph, starttime=datetime(2020, 1, 1), endtime=timedelta(seconds=N_DATA_POINTS), output_numpy=True
3800+
)
3801+
data = pd.Series(range(1, 51))
3802+
3803+
self.assertTrue(res["ema"][1][0].shape == tuple()) # 0-dimension shape is preserved
3804+
self.assertTrue(res["sum"][1][0].shape == tuple()) # 0-dimension shape is preserved
3805+
assert_series_equal(
3806+
pd.Series([res["ema"][1][k].item() for k in range(N_DATA_POINTS)]), data.ewm(halflife=10).mean()
3807+
)
3808+
assert_series_equal(
3809+
pd.Series([res["sum"][1][k].item() for k in range(N_DATA_POINTS)]),
3810+
data.rolling(window=10, min_periods=1).sum(),
3811+
)
3812+
37863813

37873814
if __name__ == "__main__":
37883815
unittest.main()

0 commit comments

Comments
 (0)