Skip to content

Commit

Permalink
explain and ignore warnings during the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dizcza committed Jul 10, 2019
1 parent 5dfdab9 commit c8789ba
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 233 deletions.
59 changes: 27 additions & 32 deletions elephant/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,6 @@
# =============================================================================


def _xrange(x, *args):
'''
Auxiliary function to use both in python 3 and python 2 to have a range
function as a generator.
'''
try:
return xrange(x, *args)
except NameError:
return range(x, *args)


def _signals_same_tstart(signals):
'''
Check whether a list of signals (AnalogSignals or SpikeTrains) have same
Expand Down Expand Up @@ -248,7 +237,7 @@ def _transactions(spiketrains, binsize, t_start=None, t_stop=None, ids=None):

# Compute and return the transaction list
return [[train_id for train_id, b in zip(ids, filled_bins)
if bin_id in b] for bin_id in _xrange(Nbins)]
if bin_id in b] for bin_id in range(Nbins)]


def _analog_signal_step_interp(signal, times):
Expand Down Expand Up @@ -482,8 +471,8 @@ def intersection_matrix(

# Compute the intersection matrix imat
N_bins = len(spikes_per_bin_x)
imat = np.zeros((N_bins, N_bins)) * 1.
for ii in _xrange(N_bins):
imat = np.zeros((N_bins, N_bins), dtype=float)
for ii in range(N_bins):
# Compute the ii-th row of imat
bin_ii = bsts_x[:, ii].reshape(-1, 1)
imat[ii, :] = (bin_ii * bsts_y).sum(axis=0)
Expand All @@ -508,7 +497,7 @@ def intersection_matrix(
for y_id in ybins_equal_0:
imat[:, y_id] = 0

imat[_xrange(N_bins), _xrange(N_bins)] = 1.
np.fill_diagonal(imat, val=1.)

except MemoryError: # use the memory-efficient version
# Compute the list spiking neurons per bin, along both axes
Expand All @@ -520,8 +509,8 @@ def intersection_matrix(
# Generate the intersection matrix
N_bins = len(ids_per_bin_x)
imat = np.zeros((N_bins, N_bins))
for ii in _xrange(N_bins):
for jj in _xrange(N_bins):
for ii in range(N_bins):
for jj in range(N_bins):
if len(ids_per_bin_x[ii]) * len(ids_per_bin_y[jj]) != 0:
imat[ii, jj] = len(set(ids_per_bin_x[ii]).intersection(
set(ids_per_bin_y[jj])))
Expand Down Expand Up @@ -589,9 +578,11 @@ def _reference_diagonal(x_edges, y_edges):
if diag_id is None:
return diag_id, np.array([])
elif diag_id >= 0:
elements = np.array([_xrange(m - diag_id), _xrange(diag_id, m)]).T
elements = np.column_stack([np.arange(m - diag_id),
np.arange(diag_id, m)])
else:
elements = np.array([_xrange(diag_id, m), _xrange(m - diag_id)]).T
elements = np.column_stack([np.arange(diag_id, m),
np.arange(m - diag_id)])

return diag_id, elements

Expand Down Expand Up @@ -680,19 +671,23 @@ def _stretched_metric_2d(x, y, stretch, ref_angle):
D = scipy.spatial.distance_matrix(points, points)

# Compute the angular coefficients of the line between each pair of points
x_array = 1. * np.vstack([x for i in x])
y_array = 1. * np.vstack([y for i in y])
x_array = np.tile(x, reps=(len(x), 1))
y_array = np.tile(y, reps=(len(y), 1))
dX = x_array.T - x_array # dX[i,j]: x difference between points i and j
dY = y_array.T - y_array # dY[i,j]: y difference between points i and j
AngCoeff = dY / dX

# Compute the matrix Theta of angles between each pair of points
Theta = np.arctan(AngCoeff)
n = Theta.shape[0]
Theta[_xrange(n), _xrange(n)] = 0 # set angle to 0 if points identical
theta = np.arctan2(dY, dX)

# Transform [-pi, pi] back to [-pi/2, pi/2]
theta[theta < -np.pi / 2] += np.pi
theta[theta > np.pi / 2] -= np.pi
assert np.allclose(np.diagonal(theta), 0), \
"Diagonal elements should be zero due to `np.arctan2(0, 0) == 0` " \
"convention."

# Compute the matrix of stretching factors for each pair of points
stretch_mat = (1 + ((stretch - 1.) * np.abs(np.sin(alpha - Theta))))
stretch_mat = 1 + (stretch - 1.) * np.abs(np.sin(alpha - theta))

# Return the stretched distance matrix
return D * stretch_mat
Expand Down Expand Up @@ -877,7 +872,7 @@ def probability_matrix_montecarlo(
if verbose:
# todo: move to tqdm
print('pmat_bootstrap(): begin of bootstrap...')
for i in _xrange(n_surr): # For each surrogate id i
for i in range(n_surr): # For each surrogate id i
if verbose:
print(' surr %d' % i)
surrs_i = [st[i] for st in surrs] # Take each i-th surrogate
Expand Down Expand Up @@ -994,7 +989,7 @@ def probability_matrix_analytical(
# to absence of spikes beyond the borders. Replace the first and last
# (k//2) elements with the (k//2)-th / (n-k//2)-th ones, respectively
k2 = k // 2
for i in _xrange(fir_rate_x.shape[0]):
for i in range(fir_rate_x.shape[0]):
fir_rate_x[i, :k2] = fir_rate_x[i, k2]
fir_rate_x[i, -k2:] = fir_rate_x[i, -k2 - 1]
fir_rate_y[i, :k2] = fir_rate_y[i, k2]
Expand Down Expand Up @@ -1064,8 +1059,8 @@ def probability_matrix_analytical(
t_start_y=t_start_y)

pmat = np.zeros(imat.shape)
for i in _xrange(imat.shape[0]):
for j in _xrange(imat.shape[1]):
for i in range(imat.shape[0]):
for j in range(imat.shape[1]):
pmat[i, j] = scipy.stats.poisson.cdf(imat[i, j] - 1, Mu[i, j])

# Substitute 0.5 to the elements along the main diagonal
Expand Down Expand Up @@ -1123,7 +1118,7 @@ def _jsf_uniform_orderstat_3d(u, alpha, n):

# Define ranges [1,...,n], [2,...,n], ..., [d,...,n] for the mute variables
# used to compute the integral as a sum over several possibilities
lists = [_xrange(j, n + 1) for j in _xrange(d, 0, -1)]
lists = [range(j, n + 1) for j in range(d, 0, -1)]

# Compute the log of the integral's coefficient
logK = np.sum(np.log(np.arange(1, n + 1))) - n * np.log(1 - alpha)
Expand Down Expand Up @@ -1399,7 +1394,7 @@ def extract_sse(spiketrains, x_edges, y_edges, cmat, ids=None):

# Reconstruct each worm, link by link
sse_dict = {}
for k in _xrange(1, nr_worms + 1): # for each worm
for k in range(1, nr_worms + 1): # for each worm
worm_k = {} # worm k is a list of links (each link will be 1 sublist)
pos_worm_k = np.array(np.where(cmat == k)).T # position of all links
# if no link lies on the reference diagonal
Expand Down
1 change: 1 addition & 0 deletions elephant/signal_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def zscore(signal, inplace=True):
# Calculate mean and standard deviation
m = np.mean(np.concatenate(signal), axis=0)
s = np.std(np.concatenate(signal), axis=0)
s[s == 0] = 1e-9 # avoid diving by zero

if not inplace:
# Create new signal instance
Expand Down
5 changes: 3 additions & 2 deletions elephant/spike_train_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,13 @@ def homogeneous_poisson_process(rate, t_start=0.0 * ms, t_stop=1000.0 * ms,
"""
if not isinstance(t_start, Quantity) or not isinstance(t_stop, Quantity):
raise ValueError("t_start and t_stop must be of type pq.Quantity")
rate = rate.rescale((1 / t_start).units)
rate = rate.rescale(1 / t_start.units)
mean_interval = 1 / rate.magnitude
return _homogeneous_process(
np.random.exponential, (mean_interval,), rate, t_start, t_stop,
as_array)


def inhomogeneous_poisson_process(rate, as_array=False):
"""
Returns a spike train whose spikes are a realization of an inhomogeneous
Expand Down Expand Up @@ -469,7 +470,7 @@ def homogeneous_gamma_process(a, b, t_start=0.0 * ms, t_stop=1000.0 * ms,
"""
if not isinstance(t_start, Quantity) or not isinstance(t_stop, Quantity):
raise ValueError("t_start and t_stop must be of type pq.Quantity")
b = b.rescale((1 / t_start).units).simplified
b = b.rescale(1 / t_start.units).simplified
rate = b / a
k, theta = a, (1 / b.magnitude)
return _homogeneous_process(np.random.gamma, (k, theta), rate, t_start, t_stop, as_array)
Expand Down
21 changes: 14 additions & 7 deletions elephant/test/test_cubic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
:license: Modified BSD, see LICENSE.txt for details.
"""

import sys
import unittest
import elephant.cubic as cubic
import quantities as pq

import neo
import numpy
import quantities as pq

import elephant.cubic as cubic

python_version_major = sys.version_info.major


class CubicTestCase(unittest.TestCase):
Expand Down Expand Up @@ -104,10 +109,12 @@ def test_cubic(self):
# Check the output for test_aborted
self.assertEqual(test_aborted, False)

@unittest.skipUnless(python_version_major == 3, "assertWarns requires 3.2")
def test_cubic_ximax(self):
# Test exceeding ximax
xi_ximax, p_vals_ximax, k_ximax, test_aborted = cubic.cubic(
self.data_signal, alpha=1, ximax=self.ximax)
with self.assertWarns(UserWarning):
xi_ximax, p_vals_ximax, k_ximax, test_aborted = cubic.cubic(
self.data_signal, alpha=1, ximax=self.ximax)

self.assertEqual(test_aborted, True)
self.assertEqual(xi_ximax - 1, self.ximax)
Expand All @@ -121,12 +128,12 @@ def test_cubic_errors(self):
ValueError, cubic.cubic, neo.AnalogSignal(
[]*pq.dimensionless, sampling_period=10*pq.ms))

dummy_data = numpy.tile([1, 2, 3], reps=3)
# Multidimensional array
self.assertRaises(ValueError, cubic.cubic, neo.AnalogSignal(
[[1, 2, 3], [1, 2, 3]] * pq.dimensionless,
dummy_data * pq.dimensionless,
sampling_period=10 * pq.ms))
self.assertRaises(ValueError, cubic.cubic, numpy.array(
[[1, 2, 3], [1, 2, 3]]))
self.assertRaises(ValueError, cubic.cubic, dummy_data.copy())

# Negative alpha
self.assertRaises(ValueError, cubic.cubic, self.data_array, alpha=-0.1)
Expand Down
22 changes: 16 additions & 6 deletions elephant/test/test_pandas_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import numpy as np
from numpy.testing import assert_array_equal
import quantities as pq
import warnings

try:
import pandas as pd
Expand Down Expand Up @@ -46,12 +47,12 @@ def test__multiindex_from_dict(self):
'test2': 5,
'test3': 'test'}
targ = pd.MultiIndex(levels=[[6.5], [5], ['test']],
labels=[[0], [0], [0]],
codes=[[0], [0], [0]],
names=['test1', 'test2', 'test3'])
res0 = ep._multiindex_from_dict(inds)
self.assertEqual(targ.levels, res0.levels)
self.assertEqual(targ.names, res0.names)
self.assertEqual(targ.labels, res0.labels)
self.assertEqual(targ.codes, res0.codes)


def _convert_levels(levels):
Expand Down Expand Up @@ -2703,7 +2704,10 @@ def test_single_t_start(self):
res1_stop = res1.columns.get_level_values('t_stop').values

targ = self.obj.values
targ[targ < targ_start] = np.nan
with warnings.catch_warnings():
warnings.simplefilter("ignore")
# targ already has nan values, ignore comparing with nan
targ[targ < targ_start] = np.nan

self.assertFalse(res0 is targ)
self.assertFalse(res1 is targ)
Expand Down Expand Up @@ -2731,7 +2735,10 @@ def test_single_t_stop(self):
res1_stop = res1.columns.get_level_values('t_stop').unique().tolist()

targ = self.obj.values
targ[targ > targ_stop] = np.nan
with warnings.catch_warnings():
warnings.simplefilter("ignore")
# targ already has nan values, ignore comparing with nan
targ[targ > targ_stop] = np.nan

self.assertFalse(res0 is targ)
self.assertFalse(res1 is targ)
Expand All @@ -2757,8 +2764,11 @@ def test_single_both(self):
res0_stop = res0.columns.get_level_values('t_stop').unique().tolist()

targ = self.obj.values
targ[targ < targ_start] = np.nan
targ[targ > targ_stop] = np.nan
with warnings.catch_warnings():
warnings.simplefilter("ignore")
# targ already has nan values, ignore comparing with nan
targ[targ < targ_start] = np.nan
targ[targ > targ_stop] = np.nan

self.assertFalse(res0 is targ)

Expand Down
11 changes: 5 additions & 6 deletions elephant/test/test_signal_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,14 @@ def test_zscore_single_multidim_inplace(self):
t_start=0. * pq.ms, sampling_rate=1000. * pq.Hz, dtype=float)

m = np.mean(signal.magnitude, axis=0, keepdims=True)
s = np.std(signal.magnitude, axis=0, keepdims=True)
target = (signal.magnitude - m) / s
s = np.std(signal.magnitude, axis=0, keepdims=True) + 1e-9
ground_truth = (signal.magnitude - m) / s
result = elephant.signal_processing.zscore(signal, inplace=True)

assert_array_almost_equal(
elephant.signal_processing.zscore(
signal, inplace=True).magnitude, target, decimal=9)
assert_array_almost_equal(result.magnitude, ground_truth, decimal=8)

# Assert original signal is overwritten
self.assertEqual(signal[0, 0].magnitude, target[0, 0])
self.assertAlmostEqual(signal[0, 0].magnitude, ground_truth[0, 0])

def test_zscore_single_dup_int(self):
"""
Expand Down
30 changes: 19 additions & 11 deletions elephant/test/test_spade.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import division

import os
import sys
import unittest

import neo
Expand All @@ -21,6 +22,8 @@
from elephant.spade_src.fim_manager import download_spade_fim, \
_get_fim_lib_path

python_version_major = sys.version_info.major


class SpadeTestCase(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -167,19 +170,24 @@ def test_spade_msip(self):
# check the lags
assert_array_equal(lags_msip, self.lags_msip)

# test under different configuration of parameters than the default one
@unittest.skipUnless(python_version_major == 3, "assertWarns requires 3.2")
def test_parameters(self):
"""
Test under different configuration of parameters than the default one
"""
# test min_spikes parameter
output_msip_min_spikes = spade.spade(
self.msip,
self.binsize,
self.winlen,
min_spikes=self.min_spikes,
n_subsets=self.n_subset,
n_surr=0,
alpha=self.alpha,
psr_param=self.psr_param,
output_format='patterns')['patterns']
with self.assertWarns(UserWarning):
# n_surr=0 and alpha=0.05 spawns expected UserWarning
output_msip_min_spikes = spade.spade(
self.msip,
self.binsize,
self.winlen,
min_spikes=self.min_spikes,
n_subsets=self.n_subset,
n_surr=0,
alpha=self.alpha,
psr_param=self.psr_param,
output_format='patterns')['patterns']
# collecting spade output
elements_msip_min_spikes = []
for out in output_msip_min_spikes:
Expand Down
Loading

0 comments on commit c8789ba

Please sign in to comment.