Skip to content

Commit

Permalink
[BinnedST] tolerance correction in the shared time interval and 'to_s…
Browse files Browse the repository at this point in the history
…parse_array()' deprecation (NeuralEnsemble#377)

* tolerance correction in the shared [t_start, t_stop] interval

* deprecate to_sparse_array
  • Loading branch information
dizcza committed Nov 4, 2020
1 parent e9afcab commit d1f2fd5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions elephant/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,8 +1128,8 @@ def _intersection_matrix(spiketrains, spiketrains_y, bin_size, t_start_x,
t_start=t_start_y, t_stop=t_stop_y)

# Compute imat by matrix multiplication
bsts_x = spiketrains_binned.to_sparse_array()
bsts_y = spiketrains_binned_y.to_sparse_array()
bsts_x = spiketrains_binned.sparse_matrix
bsts_y = spiketrains_binned_y.sparse_matrix

# Compute the number of spikes in each bin, for both time axes
# 'A1' property returns self as a flattened ndarray.
Expand Down
16 changes: 13 additions & 3 deletions elephant/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def _detect_rounding_errors(values, tolerance):
Returns True for values that are within tolerance of the next integer.
Works for both scalars and numpy arrays.
"""
if tolerance is None:
if tolerance is None or tolerance == 0:
return np.zeros_like(values, dtype=bool)
# same as '1 - (values % 1) <= tolerance' but faster
return 1 - tolerance <= values % 1
Expand Down Expand Up @@ -375,6 +375,7 @@ def _resolve_input_parameters(self, spiketrains, tolerance):
t_start=self.t_start,
t_stop=self.t_stop)
except ValueError as er:
# different t_start/t_stop
raise ValueError(er, "If you want to bin over the shared "
"[t_start, t_stop] interval, provide "
"shared t_start and t_stop explicitly, "
Expand All @@ -388,7 +389,13 @@ def _resolve_input_parameters(self, spiketrains, tolerance):
if self.t_stop is None:
self.t_stop = spiketrains[0].t_stop
start_shared, stop_shared = get_common_start_stop_times(spiketrains)
if self.t_start < start_shared or self.t_stop > stop_shared:
if tolerance is None:
tolerance = 0
# At this point, all spiketrains, t_start/stop and shared t_start/stop
# share the same units.
tolerance_units = tolerance * spiketrains[0].units
if self.t_start < start_shared - tolerance_units \
or self.t_stop > stop_shared + tolerance_units:
raise ValueError("'t_start' ({t_start}) or 't_stop' ({t_stop}) is "
"outside of the shared [{start_shared}, "
"{stop_shared}] interval".format(
Expand Down Expand Up @@ -487,6 +494,9 @@ def to_sparse_array(self):
to_array
"""
warnings.warn("'.to_sparse_array()' function is deprecated; "
"use '.sparse_matrix' attribute directly",
DeprecationWarning)
return self.sparse_matrix

def to_sparse_bool_array(self):
Expand Down Expand Up @@ -555,7 +565,7 @@ def spike_indices(self):
... t_start=0 * pq.s)
>>> print(x.spike_indices)
[[0, 0, 1, 3, 4, 5, 6]]
>>> print(x.to_sparse_array().nonzero()[1])
>>> print(x.sparse_matrix.nonzero()[1])
[0 1 3 4 5 6]
>>> print(x.to_array())
[[2, 1, 0, 1, 1, 1, 1, 0, 0, 0]]
Expand Down
7 changes: 3 additions & 4 deletions elephant/test/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ def test_binned_spiketrain_sparse(self):
x = cv.BinnedSpikeTrain([a, b], n_bins=nbins, bin_size=bin_size,
t_start=0 * pq.s)
x_sparse = [2, 1, 2, 1]
s = x.to_sparse_array()
assert_array_equal(s.data, x_sparse)
assert_array_equal(x.sparse_matrix.data, x_sparse)
assert_array_equal(x.spike_indices, [[1, 1, 4], [1, 1, 4]])

def test_binned_spiketrain_shape(self):
Expand Down Expand Up @@ -462,8 +461,8 @@ def test_binned_spiketrain_different_units(self):
xb = cv.BinnedSpikeTrain(b, bin_size=bin_size.rescale(pq.ms))
assert_array_equal(xa.to_array(), xb.to_array())
assert_array_equal(xa.to_bool_array(), xb.to_bool_array())
assert_array_equal(xa.to_sparse_array().data,
xb.to_sparse_array().data)
assert_array_equal(xa.sparse_matrix.data,
xb.sparse_matrix.data)
assert_array_equal(xa.bin_edges, xb.bin_edges)

def test_binary_to_binned_matrix(self):
Expand Down

0 comments on commit d1f2fd5

Please sign in to comment.