Skip to content

Commit

Permalink
Fast covariance() and Pearson corrcoef() (NeuralEnsemble#274)
Browse files Browse the repository at this point in the history
clarified the difference in the implementation of CrossCorrHist.cross_corr_coef() and the reference book "Analysis of parallel spike trains", 2010
  • Loading branch information
dizcza committed Nov 25, 2019
1 parent 59e6412 commit b116804
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 209 deletions.
44 changes: 37 additions & 7 deletions elephant/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from __future__ import division, print_function

import warnings
from copy import deepcopy

import neo
import numpy as np
Expand Down Expand Up @@ -492,7 +493,7 @@ def _check_init_params(self, binsize, num_bins, t_start, t_stop):
"""
# Check if num_bins is an integer (special case)
if num_bins is not None:
if not isinstance(num_bins, int):
if not np.issubdtype(type(num_bins), int):
raise TypeError("num_bins is not an integer!")
# Check if all parameters can be calculated, otherwise raise ValueError
if t_start is None:
Expand Down Expand Up @@ -777,7 +778,7 @@ def to_bool_array(self):
scipy.sparse.csr_matrix
scipy.sparse.csr_matrix.toarray
"""
return abs(scipy.sign(self.to_array())).astype(bool)
return self.to_array().astype(bool)

def to_array(self, store_array=False):
"""
Expand Down Expand Up @@ -835,13 +836,42 @@ def remove_stored_array(self):
"""
self._mat_u = None

def binarize(self):
def binarize(self, copy=True):
"""
In-place clipping the internal array to have 0 or 1 values.
Clip the internal array (no. of spikes in a bin) to have `0` or `1`
values only.
Parameters
----------
copy : bool
Make the clipping in-place (False) or with a copy (True).
Default is True.
Returns
-------
bst : BinnedSpikeTrain
Binarized `BinnedSpikeTrain`.
"""
self._sparse_mat_u.data.clip(max=1, out=self._sparse_mat_u.data)
if self._mat_u is not None:
self._mat_u.clip(max=1, out=self._mat_u)
if copy:
bst = deepcopy(self)
else:
bst = self
bst._sparse_mat_u.data.clip(max=1, out=bst._sparse_mat_u.data)
if bst._mat_u is not None:
bst._mat_u.clip(max=1, out=bst._mat_u)
return bst

@property
def sparsity(self):
"""
Returns
-------
float
Matrix sparsity, defined as matrix size, divided by no. of
nonzero elements.
"""
return np.prod(self._sparse_mat_u.shape) / len(self._sparse_mat_u.data)

def _convert_to_binned(self, spiketrains):
"""
Expand Down

0 comments on commit b116804

Please sign in to comment.