def test_liblinear_predict(): """ Test liblinear predict Sanity check, test that predict implemented in python returns the same as the one in libliblinear """ # multi-class case clf = svm.LinearSVC().fit(iris.data, iris.target) weights = clf.coef_.T bias = clf.intercept_ H = np.dot(iris.data, weights) + bias assert_array_equal(clf.predict(iris.data), H.argmax(axis=1)) # binary-class case X = [[2, 1], [3, 1], [1, 3], [2, 3]] y = [0, 0, 1, 1] clf = svm.LinearSVC().fit(X, y) weights = np.ravel(clf.coef_) bias = clf.intercept_ H = np.dot(X, weights) + bias assert_array_equal(clf.predict(X), (H > 0).astype(int))
def test_basic(self): f = [[50, 50, 50, 50, 50, 92, 18, 27, 65, 46], [50, 50, 50, 50, 50, 0, 72, 77, 68, 66], [50, 50, 50, 50, 50, 46, 47, 19, 64, 77], [50, 50, 50, 50, 50, 42, 15, 29, 95, 35], [50, 50, 50, 50, 50, 46, 34, 9, 21, 66], [70, 97, 28, 68, 78, 77, 61, 58, 71, 42], [64, 53, 44, 29, 68, 32, 19, 68, 24, 84], [ 3, 33, 53, 67, 1, 78, 74, 55, 12, 83], [ 7, 11, 46, 70, 60, 47, 24, 43, 61, 26], [32, 61, 88, 7, 39, 4, 92, 64, 45, 61]] d = signal.medfilt(f, [7, 3]) e = signal.medfilt2d(np.array(f, np.float), [7, 3]) assert_array_equal(d, [[ 0, 50, 50, 50, 42, 15, 15, 18, 27, 0], [ 0, 50, 50, 50, 50, 42, 19, 21, 29, 0], [50, 50, 50, 50, 50, 47, 34, 34, 46, 35], [50, 50, 50, 50, 50, 50, 42, 47, 64, 42], [50, 50, 50, 50, 50, 50, 46, 55, 64, 35], [33, 50, 50, 50, 50, 47, 46, 43, 55, 26], [32, 50, 50, 50, 50, 47, 46, 45, 55, 26], [ 7, 46, 50, 50, 47, 46, 46, 43, 45, 21], [ 0, 32, 33, 39, 32, 32, 43, 43, 43, 0], [ 0, 7, 11, 7, 4, 4, 19, 19, 24, 0]]) assert_array_equal(d, e)
def test_append_diag(): # Routine for appending diagonal elements assert_array_equal(append_diag(np.diag([2,3,1]), [1]), np.diag([2,3,1,1])) assert_array_equal(append_diag(np.diag([2,3,1]), [1,1]), np.diag([2,3,1,1,1])) aff = np.array([[2,0,0], [0,3,0], [0,0,1], [0,0,1]]) assert_array_equal(append_diag(aff, [5], [9]), [[2,0,0,0], [0,3,0,0], [0,0,0,1], [0,0,5,9], [0,0,0,1]]) assert_array_equal(append_diag(aff, [5,6], [9,10]), [[2,0,0,0,0], [0,3,0,0,0], [0,0,0,0,1], [0,0,5,0,9], [0,0,0,6,10], [0,0,0,0,1]]) aff = np.array([[2,0,0,0], [0,3,0,0], [0,0,0,1]]) assert_array_equal(append_diag(aff, [5], [9]), [[2,0,0,0,0], [0,3,0,0,0], [0,0,0,5,9], [0,0,0,0,1]]) # Length of starts has to match length of steps assert_raises(ValueError, append_diag, aff, [5,6], [9])
def test_precision_recall_f1_score_multiclass(): """Test Precision Recall and F1 Score for multiclass classification task""" y_true, y_pred, _ = make_prediction(binary=False) # compute scores with default labels introspection p, r, f, s = precision_recall_fscore_support(y_true, y_pred) assert_array_almost_equal(p, [0.82, 0.55, 0.47], 2) assert_array_almost_equal(r, [0.92, 0.17, 0.90], 2) assert_array_almost_equal(f, [0.87, 0.26, 0.62], 2) assert_array_equal(s, [25, 30, 20]) # individual scoring function that can be used for grid search: in the # multiclass case the score is the wieghthed average of the individual # class values hence f1_score is not necessary between precision_score and # recall_score ps = precision_score(y_true, y_pred) assert_array_almost_equal(ps, 0.62, 2) rs = recall_score(y_true, y_pred) assert_array_almost_equal(rs, 0.61, 2) fs = f1_score(y_true, y_pred) assert_array_almost_equal(fs, 0.56, 2) # same prediction but with and explicit label ordering p, r, f, s = precision_recall_fscore_support( y_true, y_pred, labels=[0, 2, 1]) assert_array_almost_equal(p, [0.82, 0.47, 0.55], 2) assert_array_almost_equal(r, [0.92, 0.90, 0.17], 2) assert_array_almost_equal(f, [0.87, 0.62, 0.26], 2) assert_array_equal(s, [25, 20, 30])
def test_imsave(): # The goal here is that the user can specify an output logical DPI # for the image, but this will not actually add any extra pixels # to the image, it will merely be used for metadata purposes. # So we do the traditional case (dpi == 1), and the new case (dpi # == 100) and read the resulting PNG files back in and make sure # the data is 100% identical. from numpy import random random.seed(1) data = random.rand(256, 128) buff_dpi1 = io.BytesIO() plt.imsave(buff_dpi1, data, dpi=1) buff_dpi100 = io.BytesIO() plt.imsave(buff_dpi100, data, dpi=100) buff_dpi1.seek(0) arr_dpi1 = plt.imread(buff_dpi1) buff_dpi100.seek(0) arr_dpi100 = plt.imread(buff_dpi100) assert arr_dpi1.shape == (256, 128, 4) assert arr_dpi100.shape == (256, 128, 4) assert_array_equal(arr_dpi1, arr_dpi100)
def test_D8_D4_fill(): """ Tests the functionality of D4 filling. """ lfD8.map_depressions(pits=None, reroute_flow=False) lfD4.map_depressions(pits=None, reroute_flow=False) assert_equal(lfD8.number_of_lakes, 1) assert_equal(lfD4.number_of_lakes, 3) correct_D8_lake_map = np.empty(7*7, dtype=int) correct_D8_lake_map.fill(XX) correct_D8_lake_map[lake_nodes] = 10 correct_D4_lake_map = correct_D8_lake_map.copy() correct_D4_lake_map[lake_nodes[5:]] = 32 correct_D4_lake_map[lake_nodes[-2]] = 38 correct_D8_depths = np.zeros(7*7, dtype=float) correct_D8_depths[lake_nodes] = 2. correct_D4_depths = correct_D8_depths.copy() correct_D4_depths[lake_nodes[5:]] = 4. correct_D4_depths[lake_nodes[-2]] = 3. assert_array_equal(lfD8.lake_map, correct_D8_lake_map) assert_array_equal(lfD4.lake_map, correct_D4_lake_map) assert_array_almost_equal(mg1.at_node['depression__depth'], correct_D8_depths) assert_array_almost_equal(mg2.at_node['depression__depth'], correct_D4_depths)
def test_D8_D4_route(): """ Tests the functionality of D4 routing. """ frD8.route_flow() frD4.route_flow() lfD8.map_depressions() lfD4.map_depressions() assert_equal(lfD8.number_of_lakes, 1) assert_equal(lfD4.number_of_lakes, 3) flow_recD8 = np.array([ 0, 1, 2, 3, 4, 5, 6, 7, 16, 10, 16, 10, 18, 13, 14, 14, 15, 16, 10, 18, 20, 21, 16, 16, 16, 18, 33, 27, 28, 28, 24, 24, 24, 32, 34, 35, 35, 38, 32, 32, 32, 41, 42, 43, 44, 45, 46, 47, 48]) flow_recD4 = np.array([ 0, 1, 2, 3, 4, 5, 6, 7, 7, 10, 17, 10, 11, 13, 14, 14, 15, 16, 17, 18, 20, 21, 21, 16, 17, 18, 33, 27, 28, 28, 29, 24, 31, 32, 34, 35, 35, 36, 37, 32, 33, 41, 42, 43, 44, 45, 46, 47, 48]) assert_array_equal(mg1.at_node['flow__receiver_node'], flow_recD8) assert_array_equal(mg2.at_node['flow__receiver_node'], flow_recD4) assert_array_almost_equal(mg1.at_node['drainage_area'].reshape((7,7))[:, 0].sum(), mg2.at_node['drainage_area'].reshape((7,7))[:, 0].sum())
def test_X_argsorted(): """Test X_argsorted argument. """ # test X_argsorted with different layout and dtype clf = tree.DecisionTreeClassifier() X_argsorted = np.argsort(np.array(X).T, axis=1).T clf.fit(X, y, X_argsorted=X_argsorted) assert_array_equal(clf.predict(T), true_result)
def test_initial_routing(): """ Test the action of fr.route_flow() on the grid. """ fr.route_flow() assert_array_equal(mg.at_node['flow__receiver_node'], r_old) assert_array_almost_equal(mg.at_node['drainage_area'], A_old)
def test_add_source_space_distances_limited(): """Test adding distances to source space with a dist_limit.""" tempdir = _TempDir() src = read_source_spaces(fname) src_new = read_source_spaces(fname) del src_new[0]['dist'] del src_new[1]['dist'] n_do = 200 # limit this for speed src_new[0]['vertno'] = src_new[0]['vertno'][:n_do].copy() src_new[1]['vertno'] = src_new[1]['vertno'][:n_do].copy() out_name = op.join(tempdir, 'temp-src.fif') try: add_source_space_distances(src_new, dist_limit=0.007) except RuntimeError: # what we throw when scipy version is wrong raise SkipTest('dist_limit requires scipy > 0.13') write_source_spaces(out_name, src_new) src_new = read_source_spaces(out_name) for so, sn in zip(src, src_new): assert_array_equal(so['dist_limit'], np.array([-0.007], np.float32)) assert_array_equal(sn['dist_limit'], np.array([0.007], np.float32)) do = so['dist'] dn = sn['dist'] # clean out distances > 0.007 in C code do.data[do.data > 0.007] = 0 do.eliminate_zeros() # make sure we have some comparable distances assert np.sum(do.data < 0.007) > 400 # do comparison over the region computed d = (do - dn)[:sn['vertno'][n_do - 1]][:, :sn['vertno'][n_do - 1]] assert_allclose(np.zeros_like(d.data), d.data, rtol=0, atol=1e-6)
def test_dimension(self): """ Check the dimension. """ nptest.assert_array_equal(self.lambda_emulate.shape, ((self.num_l_emulate/comm.size) + (comm.rank < \ self.num_l_emulate%comm.size), 3))
def test_origin_affine(): hdr = Spm99AnalyzeHeader() aff = hdr.get_origin_affine() assert_array_equal(aff, hdr.get_base_affine()) hdr.set_data_shape((3, 5, 7)) hdr.set_zooms((3, 2, 1)) assert_true(hdr.default_x_flip) assert_array_almost_equal( hdr.get_origin_affine(), # from center of image [[-3., 0., 0., 3.], [ 0., 2., 0., -4.], [ 0., 0., 1., -3.], [ 0., 0., 0., 1.]]) hdr['origin'][:3] = [3,4,5] assert_array_almost_equal( hdr.get_origin_affine(), # using origin [[-3., 0., 0., 6.], [ 0., 2., 0., -6.], [ 0., 0., 1., -4.], [ 0., 0., 0., 1.]]) hdr['origin'] = 0 # unset origin hdr.set_data_shape((3, 5)) assert_array_almost_equal( hdr.get_origin_affine(), [[-3., 0., 0., 3.], [ 0., 2., 0., -4.], [ 0., 0., 1., -0.], [ 0., 0., 0., 1.]]) hdr.set_data_shape((3, 5, 7)) assert_array_almost_equal( hdr.get_origin_affine(), # from center of image [[-3., 0., 0., 3.], [ 0., 2., 0., -4.], [ 0., 0., 1., -3.], [ 0., 0., 0., 1.]])
def test_add_patch_info(): """Test adding patch info to source space.""" # let's setup a small source space src = read_source_spaces(fname_small) src_new = read_source_spaces(fname_small) for s in src_new: s['nearest'] = None s['nearest_dist'] = None s['pinfo'] = None # test that no patch info is added for small dist_limit try: add_source_space_distances(src_new, dist_limit=0.00001) except RuntimeError: # what we throw when scipy version is wrong pass else: assert all(s['nearest'] is None for s in src_new) assert all(s['nearest_dist'] is None for s in src_new) assert all(s['pinfo'] is None for s in src_new) # now let's use one that works add_source_space_distances(src_new) for s1, s2 in zip(src, src_new): assert_array_equal(s1['nearest'], s2['nearest']) assert_allclose(s1['nearest_dist'], s2['nearest_dist'], atol=1e-7) assert_equal(len(s1['pinfo']), len(s2['pinfo'])) for p1, p2 in zip(s1['pinfo'], s2['pinfo']): assert_array_equal(p1, p2)
def test_check_symmetric(): arr_sym = np.array([[0, 1], [1, 2]]) arr_bad = np.ones(2) arr_asym = np.array([[0, 2], [0, 2]]) test_arrays = {'dense': arr_asym, 'dok': sp.dok_matrix(arr_asym), 'csr': sp.csr_matrix(arr_asym), 'csc': sp.csc_matrix(arr_asym), 'coo': sp.coo_matrix(arr_asym), 'lil': sp.lil_matrix(arr_asym), 'bsr': sp.bsr_matrix(arr_asym)} # check error for bad inputs assert_raises(ValueError, check_symmetric, arr_bad) # check that asymmetric arrays are properly symmetrized for arr_format, arr in test_arrays.items(): # Check for warnings and errors assert_warns(UserWarning, check_symmetric, arr) assert_raises(ValueError, check_symmetric, arr, raise_exception=True) output = check_symmetric(arr, raise_warning=False) if sp.issparse(output): assert_equal(output.format, arr_format) assert_array_equal(output.toarray(), arr_sym) else: assert_array_equal(output, arr_sym)
def test_zero_byte_string(): # Tests hack to allow chars of non-zero length, but 0 bytes # make reader-like thing str_io = cStringIO() r = _make_readerlike(str_io, boc.native_code) c_reader = m5u.VarReader5(r) tag_dt = np.dtype([('mdtype', 'u4'), ('byte_count', 'u4')]) tag = np.zeros((1,), dtype=tag_dt) tag['mdtype'] = mio5p.miINT8 tag['byte_count'] = 1 hdr = m5u.VarHeader5() # Try when string is 1 length hdr.set_dims([1,]) _write_stream(str_io, tag.tostring() + asbytes(' ')) str_io.seek(0) val = c_reader.read_char(hdr) assert_equal(val, ' ') # Now when string has 0 bytes 1 length tag['byte_count'] = 0 _write_stream(str_io, tag.tostring()) str_io.seek(0) val = c_reader.read_char(hdr) assert_equal(val, ' ') # Now when string has 0 bytes 4 length str_io.seek(0) hdr.set_dims([4,]) val = c_reader.read_char(hdr) assert_array_equal(val, [' '] * 4)
def test_rotate_axis0_input(self): kws = self.default_kws.copy() kws['rotate'] = True kws['axis'] = 0 p = mat._DendrogramPlotter(self.df_norm.T, **kws) npt.assert_array_equal(p.reordered_ind, self.x_norm_leaves)
def test_vectorizer_unicode(): # tests that the count vectorizer works with cyrillic. document = ( "\xd0\x9c\xd0\xb0\xd1\x88\xd0\xb8\xd0\xbd\xd0\xbd\xd0\xbe\xd0" "\xb5 \xd0\xbe\xd0\xb1\xd1\x83\xd1\x87\xd0\xb5\xd0\xbd\xd0\xb8\xd0" "\xb5 \xe2\x80\x94 \xd0\xbe\xd0\xb1\xd1\x88\xd0\xb8\xd1\x80\xd0\xbd" "\xd1\x8b\xd0\xb9 \xd0\xbf\xd0\xbe\xd0\xb4\xd1\x80\xd0\xb0\xd0\xb7" "\xd0\xb4\xd0\xb5\xd0\xbb \xd0\xb8\xd1\x81\xd0\xba\xd1\x83\xd1\x81" "\xd1\x81\xd1\x82\xd0\xb2\xd0\xb5\xd0\xbd\xd0\xbd\xd0\xbe\xd0\xb3" "\xd0\xbe \xd0\xb8\xd0\xbd\xd1\x82\xd0\xb5\xd0\xbb\xd0\xbb\xd0" "\xb5\xd0\xba\xd1\x82\xd0\xb0, \xd0\xb8\xd0\xb7\xd1\x83\xd1\x87" "\xd0\xb0\xd1\x8e\xd1\x89\xd0\xb8\xd0\xb9 \xd0\xbc\xd0\xb5\xd1\x82" "\xd0\xbe\xd0\xb4\xd1\x8b \xd0\xbf\xd0\xbe\xd1\x81\xd1\x82\xd1\x80" "\xd0\xbe\xd0\xb5\xd0\xbd\xd0\xb8\xd1\x8f \xd0\xb0\xd0\xbb\xd0\xb3" "\xd0\xbe\xd1\x80\xd0\xb8\xd1\x82\xd0\xbc\xd0\xbe\xd0\xb2, \xd1\x81" "\xd0\xbf\xd0\xbe\xd1\x81\xd0\xbe\xd0\xb1\xd0\xbd\xd1\x8b\xd1\x85 " "\xd0\xbe\xd0\xb1\xd1\x83\xd1\x87\xd0\xb0\xd1\x82\xd1\x8c\xd1\x81\xd1" "\x8f.") vect = CountVectorizer() X_counted = vect.fit_transform([document]) assert_equal(X_counted.shape, (1, 15)) vect = HashingVectorizer(norm=None, non_negative=True) X_hashed = vect.transform([document]) assert_equal(X_hashed.shape, (1, 2 ** 20)) # No collisions on such a small dataset assert_equal(X_counted.nnz, X_hashed.nnz) # When norm is None and non_negative, the tokens are counted up to # collisions assert_array_equal(np.sort(X_counted.data), np.sort(X_hashed.data))
def test_make_Calculator_user_mods_with_cpi_flags(paramsfile): with open(paramsfile.name) as pfile: params = json.load(pfile) ppo = Parameters(parameter_dict=params, start_year=1991, num_years=len(irates), inflation_rates=irates) calc = Calculator(params=ppo, records=tax_dta_path, start_year=1991, inflation_rates=irates) user_mods = {1991: {"_almdep": [7150, 7250, 7400], "_almdep_cpi": True, "_almsep": [40400, 41050], "_almsep_cpi": False, "_rt5": [0.33], "_rt7": [0.396]}} calc.params.implement_reform(user_mods) inf_rates = [irates[1991 + i] for i in range(0, 12)] exp_almdep = expand_array(np.array([7150, 7250, 7400]), inflate=True, inflation_rates=inf_rates, num_years=12) act_almdep = getattr(calc.params, '_almdep') assert_array_equal(act_almdep, exp_almdep) exp_almsep_values = [40400] + [41050] * 11 exp_almsep = np.array(exp_almsep_values) act_almsep = getattr(calc.params, '_almsep') assert_array_equal(act_almsep, exp_almsep)
def test_sgd(self): """Check that SGD gives any results :-)""" clf = self.factory(penalty="l2", alpha=0.01, fit_intercept=True, n_iter=10, shuffle=True) clf.fit(X, Y) # assert_almost_equal(clf.coef_[0], clf.coef_[1], decimal=7) assert_array_equal(clf.predict(T), true_result)
def test_arithmetics_mask_func(): def mask_sad_func(mask1, mask2, fun=0): if fun > 0.5: return mask2 else: return mask1 meta1 = {'a': 1} meta2 = {'a': 3, 'b': 2} mask1 = [True, False, True] mask2 = [True, False, False] uncertainty1 = StdDevUncertainty([1, 2, 3]) uncertainty2 = StdDevUncertainty([1, 2, 3]) wcs1 = 99.99 wcs2 = 100 data1 = [1, 1, 1] data2 = [1, 1, 1] nd1 = NDDataArithmetic(data1, meta=meta1, mask=mask1, wcs=wcs1, uncertainty=uncertainty1) nd2 = NDDataArithmetic(data2, meta=meta2, mask=mask2, wcs=wcs2, uncertainty=uncertainty2) nd3 = nd1.add(nd2, handle_mask=mask_sad_func) assert_array_equal(nd3.mask, nd1.mask) nd4 = nd1.add(nd2, handle_mask=mask_sad_func, mask_fun=1) assert_array_equal(nd4.mask, nd2.mask) with pytest.raises(KeyError): nd1.add(nd2, handle_mask=mask_sad_func, fun=1)
def test_raw_events(): """Test creating stim channel from raw SQD file.""" def evts(a, b, c, d, e, f=None): out = [[269, a, b], [281, b, c], [1552, c, d], [1564, d, e]] if f is not None: out.append([2000, e, f]) return out raw = read_raw_kit(sqd_path) assert_array_equal(find_events(raw, output='step', consecutive=True), evts(255, 254, 255, 254, 255, 0)) raw = read_raw_kit(sqd_path, slope='+') assert_array_equal(find_events(raw, output='step', consecutive=True), evts(0, 1, 0, 1, 0)) raw = read_raw_kit(sqd_path, stim='<', slope='+') assert_array_equal(find_events(raw, output='step', consecutive=True), evts(0, 128, 0, 128, 0)) raw = read_raw_kit(sqd_path, stim='<', slope='+', stim_code='channel') assert_array_equal(find_events(raw, output='step', consecutive=True), evts(0, 160, 0, 160, 0)) raw = read_raw_kit(sqd_path, stim=range(160, 162), slope='+', stim_code='channel') assert_array_equal(find_events(raw, output='step', consecutive=True), evts(0, 160, 0, 160, 0))
def test02b(self): """Testing `reshape()` (ndim -> ndim, II)""" a = np.arange(24, dtype="i4").reshape((2,3,4)) c = blz.arange(24, dtype="i4").reshape((4,3,2)) b = c.reshape((2,3,4)) #print "b->", `b` assert_array_equal(a, b, "Arrays are not equal")
def test02(self): """Testing `iter()` (w/ start, stop, step)""" a = np.ones((3,), dtype="i4") b = blz.ones((1000,3), dtype="i4") #print "b->", `b` for r in b.iter(15, 100, 3): assert_array_equal(a, r, "Arrays are not equal")
def test02(self): """Testing `append()` method (several rows)""" a = np.ones((4,3), dtype="i4")*3 b = blz.fill((1,3), 3, dtype="i4", rootdir=self.rootdir) b.append([(3,3,3)]*3) #print "b->", `b` assert_array_equal(a, b, "Arrays are not equal")
def test01(self): """Testing `resize()` (enlarge)""" a = np.ones((4,3), dtype="i4") b = blz.ones((3,3), dtype="i4", rootdir=self.rootdir) b.resize(4) #print "b->", `b` assert_array_equal(a, b, "Arrays are not equal")
def test_check_median_stat_length(self): a = np.arange(100).astype('f') a[1] = 2. a[97] = 96. a = pad(a, (25, 20), 'median', stat_length=(3, 5)) b = np.array( [ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 0., 2., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., 60., 61., 62., 63., 64., 65., 66., 67., 68., 69., 70., 71., 72., 73., 74., 75., 76., 77., 78., 79., 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90., 91., 92., 93., 94., 95., 96., 96., 98., 99., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96.] ) assert_array_equal(a, b)
def test00b(self): """Testing `append()` method (correct shape, single row)""" a = np.ones((2,300), dtype="i4")*3 b = blz.fill((1,300), 3, dtype="i4", rootdir=self.rootdir) b.append((3,)*300) #print "b->", `b` assert_array_equal(a, b, "Arrays are not equal")
def test01(self): """Testing `reshape()` (ndim -> unidim)""" a = np.ones(12, dtype="i4") c = blz.ones(12, dtype="i4").reshape((3,4)) b = c.reshape(12) #print "b->", `b` assert_array_equal(a, b, "Arrays are not equal")
def test_ada_fit_sample_nn_obj(): """Test fit-sample with nn object""" # Resample the data nn = NearestNeighbors(n_neighbors=6) ada = ADASYN(random_state=RND_SEED, n_neighbors=nn) X_resampled, y_resampled = ada.fit_sample(X, Y) X_gt = np.array([[0.11622591, -0.0317206], [0.77481731, 0.60935141], [1.25192108, -0.22367336], [0.53366841, -0.30312976], [1.52091956, -0.49283504], [-0.28162401, -2.10400981], [0.83680821, 1.72827342], [0.3084254, 0.33299982], [0.70472253, -0.73309052], [0.28893132, -0.38761769], [1.15514042, 0.0129463], [0.88407872, 0.35454207], [1.31301027, -0.92648734], [-1.11515198, -0.93689695], [-0.18410027, -0.45194484], [0.9281014, 0.53085498], [-0.14374509, 0.27370049], [-0.41635887, -0.38299653], [0.08711622, 0.93259929], [1.70580611, -0.11219234], [0.29427267, 0.21740707], [0.68118697, -0.25220353], [1.37180201, 0.37279378], [-0.59243851, -0.80715327]]) y_gt = np.array([ 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0 ]) assert_array_almost_equal(X_resampled, X_gt) assert_array_equal(y_resampled, y_gt)
def test_arithmetics_data_unit_not_identical(data1, data2): nd1 = NDDataArithmetic(data1) nd2 = NDDataArithmetic(data2) # Addition should not be possible with pytest.raises(UnitsError): nd1.add(nd2) # Subtraction should not be possible with pytest.raises(UnitsError): nd1.subtract(nd2) # Multiplication is possible nd3 = nd1.multiply(nd2) ref = data1 * data2 ref_unit, ref_data = ref.unit, ref.value assert_array_equal(ref_data, nd3.data) assert nd3.unit == ref_unit # Division is possible nd4 = nd1.divide(nd2) ref = data1 / data2 ref_unit, ref_data = ref.unit, ref.value assert_array_equal(ref_data, nd4.data) assert nd4.unit == ref_unit for nd in [nd3, nd4]: # Check all other attributes are not set assert nd.uncertainty is None assert nd.mask is None assert len(nd.meta) == 0 assert nd.wcs is None
def test_scatter_data(self): p = lm._RegressionPlotter(self.df.x, self.df.y) x, y = p.scatter_data npt.assert_array_equal(x, self.df.x) npt.assert_array_equal(y, self.df.y) p = lm._RegressionPlotter(self.df.d, self.df.y) x, y = p.scatter_data npt.assert_array_equal(x, self.df.d) npt.assert_array_equal(y, self.df.y) p = lm._RegressionPlotter(self.df.d, self.df.y, x_jitter=.1) x, y = p.scatter_data assert (x != self.df.d).any() npt.assert_array_less(np.abs(self.df.d - x), np.repeat(.1, len(x))) npt.assert_array_equal(y, self.df.y) p = lm._RegressionPlotter(self.df.d, self.df.y, y_jitter=.05) x, y = p.scatter_data npt.assert_array_equal(x, self.df.d) npt.assert_array_less(np.abs(self.df.y - y), np.repeat(.1, len(y)))
def test_survival_table_to_events_casts_to_float(): T, C = (np.array([1, 2, 3, 4, 4, 5]), np.array([True, False, True, True, True, True])) d = utils.survival_table_from_events(T, C, np.zeros_like(T)) npt.assert_array_equal(d["censored"].values, np.array([0.0, 0.0, 1.0, 0.0, 0.0, 0.0])) npt.assert_array_equal(d["removed"].values, np.array([0.0, 1.0, 1.0, 1.0, 2.0, 1.0]))
def test_survival_table_to_events(): T, C = np.array([1, 2, 3, 4, 4, 5]), np.array([1, 0, 1, 1, 1, 1]) d = utils.survival_table_from_events(T, C, np.zeros_like(T)) T_, C_ = utils.survival_events_from_table(d[["censored", "observed"]]) npt.assert_array_equal(T, T_) npt.assert_array_equal(C, C_)
def test_check_events(): """Test the function which tests that the events data describes a valid experimental paradigm. """ events = basic_paradigm() # Errors checkins # Missing onset missing_onset = events.drop(columns=['onset']) with pytest.raises(ValueError, match='The provided events data has no onset column.'): check_events(missing_onset) # Missing duration missing_duration = events.drop(columns=['duration']) with pytest.raises(ValueError, match='The provided events data has no duration column.'): check_events(missing_duration) # Warnings checkins # Missing trial type with pytest.warns(UserWarning, match="'trial_type' column not found"): ttype, onset, duration, modulation = check_events(events) # Check that missing trial type yields a 'dummy' array assert_array_equal(ttype, np.repeat('dummy', len(events))) # Check that missing modulation yields an array one ones assert_array_equal(modulation, np.ones(len(events))) # Modulation is provided events['modulation'] = np.ones(len(events)) with pytest.warns(UserWarning, match="'modulation' column found in the given events data."): check_events(events) # An unexpected field is provided events = events.drop(columns=['modulation']) events['foo'] = np.zeros(len(events)) with pytest.warns(UserWarning, match="Unexpected key `foo` in events will be ignored."): ttype2, onset2, duration2, modulation2 = check_events(events) assert_array_equal(ttype, ttype2) assert_array_equal(onset, onset2) assert_array_equal(duration, duration2) assert_array_equal(modulation, modulation2)
def check_einsum_sums(self, dtype, do_opt=False): # Check various sums. Does many sizes to exercise unrolled loops. # sum(a, axis=-1) for n in range(1, 17): a = np.arange(n, dtype=dtype) assert_equal(np.einsum("i->", a, optimize=do_opt), np.sum(a, axis=-1).astype(dtype)) assert_equal(np.einsum(a, [0], [], optimize=do_opt), np.sum(a, axis=-1).astype(dtype)) for n in range(1, 17): a = np.arange(2 * 3 * n, dtype=dtype).reshape(2, 3, n) assert_equal( np.einsum("...i->...", a, optimize=do_opt), np.sum(a, axis=-1).astype(dtype), ) assert_equal( np.einsum(a, [Ellipsis, 0], [Ellipsis], optimize=do_opt), np.sum(a, axis=-1).astype(dtype), ) # sum(a, axis=0) for n in range(1, 17): a = np.arange(2 * n, dtype=dtype).reshape(2, n) assert_equal( np.einsum("i...->...", a, optimize=do_opt), np.sum(a, axis=0).astype(dtype), ) assert_equal( np.einsum(a, [0, Ellipsis], [Ellipsis], optimize=do_opt), np.sum(a, axis=0).astype(dtype), ) for n in range(1, 17): a = np.arange(2 * 3 * n, dtype=dtype).reshape(2, 3, n) assert_equal( np.einsum("i...->...", a, optimize=do_opt), np.sum(a, axis=0).astype(dtype), ) assert_equal( np.einsum(a, [0, Ellipsis], [Ellipsis], optimize=do_opt), np.sum(a, axis=0).astype(dtype), ) # trace(a) for n in range(1, 17): a = np.arange(n * n, dtype=dtype).reshape(n, n) assert_equal(np.einsum("ii", a, optimize=do_opt), np.trace(a).astype(dtype)) assert_equal(np.einsum(a, [0, 0], optimize=do_opt), np.trace(a).astype(dtype)) # multiply(a, b) assert_equal(np.einsum("..., ...", 3, 4), 12) # scalar case for n in range(1, 17): a = np.arange(3 * n, dtype=dtype).reshape(3, n) b = np.arange(2 * 3 * n, dtype=dtype).reshape(2, 3, n) assert_equal(np.einsum("..., ...", a, b, optimize=do_opt), np.multiply(a, b)) assert_equal( np.einsum(a, [Ellipsis], b, [Ellipsis], optimize=do_opt), np.multiply(a, b), ) # inner(a,b) for n in range(1, 17): a = np.arange(2 * 3 * n, dtype=dtype).reshape(2, 3, n) b = np.arange(n, dtype=dtype) assert_equal(np.einsum("...i, ...i", a, b, optimize=do_opt), np.inner(a, b)) assert_equal( np.einsum(a, [Ellipsis, 0], b, [Ellipsis, 0], optimize=do_opt), np.inner(a, b), ) for n in range(1, 11): a = np.arange(n * 3 * 2, dtype=dtype).reshape(n, 3, 2) b = np.arange(n, dtype=dtype) assert_equal(np.einsum("i..., i...", a, b, optimize=do_opt), np.inner(a.T, b.T).T) assert_equal( np.einsum(a, [0, Ellipsis], b, [0, Ellipsis], optimize=do_opt), np.inner(a.T, b.T).T, ) # outer(a,b) for n in range(1, 17): a = np.arange(3, dtype=dtype) + 1 b = np.arange(n, dtype=dtype) + 1 assert_equal(np.einsum("i,j", a, b, optimize=do_opt), np.outer(a, b)) assert_equal(np.einsum(a, [0], b, [1], optimize=do_opt), np.outer(a, b)) # Suppress the complex warnings for the 'as f8' tests with suppress_warnings() as sup: sup.filter(np.ComplexWarning) # matvec(a,b) / a.dot(b) where a is matrix, b is vector for n in range(1, 17): a = np.arange(4 * n, dtype=dtype).reshape(4, n) b = np.arange(n, dtype=dtype) assert_equal(np.einsum("ij, j", a, b, optimize=do_opt), np.dot(a, b)) assert_equal(np.einsum(a, [0, 1], b, [1], optimize=do_opt), np.dot(a, b)) c = np.arange(4, dtype=dtype) np.einsum("ij,j", a, b, out=c, dtype="f8", casting="unsafe", optimize=do_opt) assert_equal( c, np.dot(a.astype("f8"), b.astype("f8")).astype(dtype)) c[...] = 0 np.einsum( a, [0, 1], b, [1], out=c, dtype="f8", casting="unsafe", optimize=do_opt, ) assert_equal( c, np.dot(a.astype("f8"), b.astype("f8")).astype(dtype)) for n in range(1, 17): a = np.arange(4 * n, dtype=dtype).reshape(4, n) b = np.arange(n, dtype=dtype) assert_equal(np.einsum("ji,j", a.T, b.T, optimize=do_opt), np.dot(b.T, a.T)) assert_equal(np.einsum(a.T, [1, 0], b.T, [1], optimize=do_opt), np.dot(b.T, a.T)) c = np.arange(4, dtype=dtype) np.einsum( "ji,j", a.T, b.T, out=c, dtype="f8", casting="unsafe", optimize=do_opt, ) assert_equal( c, np.dot(b.T.astype("f8"), a.T.astype("f8")).astype(dtype)) c[...] = 0 np.einsum( a.T, [1, 0], b.T, [1], out=c, dtype="f8", casting="unsafe", optimize=do_opt, ) assert_equal( c, np.dot(b.T.astype("f8"), a.T.astype("f8")).astype(dtype)) # matmat(a,b) / a.dot(b) where a is matrix, b is matrix for n in range(1, 17): if n < 8 or dtype != "f2": a = np.arange(4 * n, dtype=dtype).reshape(4, n) b = np.arange(n * 6, dtype=dtype).reshape(n, 6) assert_equal(np.einsum("ij,jk", a, b, optimize=do_opt), np.dot(a, b)) assert_equal( np.einsum(a, [0, 1], b, [1, 2], optimize=do_opt), np.dot(a, b)) for n in range(1, 17): a = np.arange(4 * n, dtype=dtype).reshape(4, n) b = np.arange(n * 6, dtype=dtype).reshape(n, 6) c = np.arange(24, dtype=dtype).reshape(4, 6) np.einsum("ij,jk", a, b, out=c, dtype="f8", casting="unsafe", optimize=do_opt) assert_equal( c, np.dot(a.astype("f8"), b.astype("f8")).astype(dtype)) c[...] = 0 np.einsum( a, [0, 1], b, [1, 2], out=c, dtype="f8", casting="unsafe", optimize=do_opt, ) assert_equal( c, np.dot(a.astype("f8"), b.astype("f8")).astype(dtype)) # matrix triple product (note this is not currently an efficient # way to multiply 3 matrices) a = np.arange(12, dtype=dtype).reshape(3, 4) b = np.arange(20, dtype=dtype).reshape(4, 5) c = np.arange(30, dtype=dtype).reshape(5, 6) if dtype != "f2": assert_equal(np.einsum("ij,jk,kl", a, b, c, optimize=do_opt), a.dot(b).dot(c)) assert_equal( np.einsum(a, [0, 1], b, [1, 2], c, [2, 3], optimize=do_opt), a.dot(b).dot(c), ) d = np.arange(18, dtype=dtype).reshape(3, 6) np.einsum( "ij,jk,kl", a, b, c, out=d, dtype="f8", casting="unsafe", optimize=do_opt, ) tgt = a.astype("f8").dot(b.astype("f8")) tgt = tgt.dot(c.astype("f8")).astype(dtype) assert_equal(d, tgt) d[...] = 0 np.einsum( a, [0, 1], b, [1, 2], c, [2, 3], out=d, dtype="f8", casting="unsafe", optimize=do_opt, ) tgt = a.astype("f8").dot(b.astype("f8")) tgt = tgt.dot(c.astype("f8")).astype(dtype) assert_equal(d, tgt) # tensordot(a, b) if np.dtype(dtype) != np.dtype("f2"): a = np.arange(60, dtype=dtype).reshape(3, 4, 5) b = np.arange(24, dtype=dtype).reshape(4, 3, 2) assert_equal( np.einsum("ijk, jil -> kl", a, b), np.tensordot(a, b, axes=([1, 0], [0, 1])), ) assert_equal( np.einsum(a, [0, 1, 2], b, [1, 0, 3], [2, 3]), np.tensordot(a, b, axes=([1, 0], [0, 1])), ) c = np.arange(10, dtype=dtype).reshape(5, 2) np.einsum( "ijk,jil->kl", a, b, out=c, dtype="f8", casting="unsafe", optimize=do_opt, ) assert_equal( c, np.tensordot(a.astype("f8"), b.astype("f8"), axes=([1, 0], [0, 1])).astype(dtype), ) c[...] = 0 np.einsum( a, [0, 1, 2], b, [1, 0, 3], [2, 3], out=c, dtype="f8", casting="unsafe", optimize=do_opt, ) assert_equal( c, np.tensordot(a.astype("f8"), b.astype("f8"), axes=([1, 0], [0, 1])).astype(dtype), ) # logical_and(logical_and(a!=0, b!=0), c!=0) a = np.array([1, 3, -2, 0, 12, 13, 0, 1], dtype=dtype) b = np.array([0, 3.5, 0.0, -2, 0, 1, 3, 12], dtype=dtype) c = np.array([True, True, False, True, True, False, True, True]) assert_equal( np.einsum("i,i,i->i", a, b, c, dtype="?", casting="unsafe", optimize=do_opt), np.logical_and(np.logical_and(a != 0, b != 0), c != 0), ) assert_equal( np.einsum(a, [0], b, [0], c, [0], [0], dtype="?", casting="unsafe"), np.logical_and(np.logical_and(a != 0, b != 0), c != 0), ) a = np.arange(9, dtype=dtype) assert_equal(np.einsum(",i->", 3, a), 3 * np.sum(a)) assert_equal(np.einsum(3, [], a, [0], []), 3 * np.sum(a)) assert_equal(np.einsum("i,->", a, 3), 3 * np.sum(a)) assert_equal(np.einsum(a, [0], 3, [], []), 3 * np.sum(a)) # Various stride0, contiguous, and SSE aligned variants for n in range(1, 25): a = np.arange(n, dtype=dtype) if np.dtype(dtype).itemsize > 1: assert_equal(np.einsum("...,...", a, a, optimize=do_opt), np.multiply(a, a)) assert_equal(np.einsum("i,i", a, a, optimize=do_opt), np.dot(a, a)) assert_equal(np.einsum("i,->i", a, 2, optimize=do_opt), 2 * a) assert_equal(np.einsum(",i->i", 2, a, optimize=do_opt), 2 * a) assert_equal(np.einsum("i,->", a, 2, optimize=do_opt), 2 * np.sum(a)) assert_equal(np.einsum(",i->", 2, a, optimize=do_opt), 2 * np.sum(a)) assert_equal( np.einsum("...,...", a[1:], a[:-1], optimize=do_opt), np.multiply(a[1:], a[:-1]), ) assert_equal( np.einsum("i,i", a[1:], a[:-1], optimize=do_opt), np.dot(a[1:], a[:-1]), ) assert_equal(np.einsum("i,->i", a[1:], 2, optimize=do_opt), 2 * a[1:]) assert_equal(np.einsum(",i->i", 2, a[1:], optimize=do_opt), 2 * a[1:]) assert_equal(np.einsum("i,->", a[1:], 2, optimize=do_opt), 2 * np.sum(a[1:])) assert_equal(np.einsum(",i->", 2, a[1:], optimize=do_opt), 2 * np.sum(a[1:])) # An object array, summed as the data type a = np.arange(9, dtype=object) b = np.einsum("i->", a, dtype=dtype, casting="unsafe") assert_equal(b, np.sum(a)) assert_equal(b.dtype, np.dtype(dtype)) b = np.einsum(a, [0], [], dtype=dtype, casting="unsafe") assert_equal(b, np.sum(a)) assert_equal(b.dtype, np.dtype(dtype)) # A case which was failing (ticket #1885) p = np.arange(2) + 1 q = np.arange(4).reshape(2, 2) + 3 r = np.arange(4).reshape(2, 2) + 7 assert_equal(np.einsum("z,mz,zm->", p, q, r), 253) # singleton dimensions broadcast (gh-10343) p = np.ones((10, 2)) q = np.ones((1, 2)) assert_array_equal( np.einsum("ij,ij->j", p, q, optimize=True), np.einsum("ij,ij->j", p, q, optimize=False), ) assert_array_equal(np.einsum("ij,ij->j", p, q, optimize=True), [10.0] * 2) # a blas-compatible contraction broadcasting case which was failing # for optimize=True (ticket #10930) x = np.array([2.0, 3.0]) y = np.array([4.0]) assert_array_equal(np.einsum("i, i", x, y, optimize=False), 20.0) assert_array_equal(np.einsum("i, i", x, y, optimize=True), 20.0) # all-ones array was bypassing bug (ticket #10930) p = np.ones((1, 5)) / 2 q = np.ones((5, 5)) / 2 for optimize in (True, False): assert_array_equal( np.einsum("...ij,...jk->...ik", p, p, optimize=optimize), np.einsum("...ij,...jk->...ik", p, q, optimize=optimize), ) assert_array_equal( np.einsum("...ij,...jk->...ik", p, q, optimize=optimize), np.full((1, 5), 1.25), ) # Cases which were failing (gh-10899) x = np.eye(2, dtype=dtype) y = np.ones(2, dtype=dtype) assert_array_equal(np.einsum("ji,i->", x, y, optimize=optimize), [2.0]) # contig_contig_outstride0_two assert_array_equal(np.einsum("i,ij->", y, x, optimize=optimize), [2.0]) # stride0_contig_outstride0_two assert_array_equal(np.einsum("ij,i->", x, y, optimize=optimize), [2.0]) # contig_stride0_outstride0_two
def test_info_no_rename_no_reorder_no_pdf(): """Test private renaming, reordering and partial construction option.""" for pdf, config, hs in zip(pdf_fnames, config_fnames, hs_fnames): info, bti_info = _get_bti_info(pdf_fname=pdf, config_fname=config, head_shape_fname=hs, rotation_x=0.0, translation=(0.0, 0.02, 0.11), convert=False, ecg_ch='E31', eog_ch=('E63', 'E64'), rename_channels=False, sort_by_ch_name=False) info2, bti_info = _get_bti_info(pdf_fname=None, config_fname=config, head_shape_fname=hs, rotation_x=0.0, translation=(0.0, 0.02, 0.11), convert=False, ecg_ch='E31', eog_ch=('E63', 'E64'), rename_channels=False, sort_by_ch_name=False) assert_equal(info['ch_names'], [ch['ch_name'] for ch in info['chs']]) assert_equal([n for n in info['ch_names'] if n.startswith('A')][:5], ['A22', 'A2', 'A104', 'A241', 'A138']) assert_equal([n for n in info['ch_names'] if n.startswith('A')][-5:], ['A133', 'A158', 'A44', 'A134', 'A216']) info = pick_info(info, pick_types(info, meg=True, stim=True, resp=True)) info2 = pick_info(info2, pick_types(info2, meg=True, stim=True, resp=True)) assert (info['sfreq'] is not None) assert (info['lowpass'] is not None) assert (info['highpass'] is not None) assert (info['meas_date'] is not None) assert_equal(info2['sfreq'], None) assert_equal(info2['lowpass'], None) assert_equal(info2['highpass'], None) assert_equal(info2['meas_date'], None) assert_equal(info['ch_names'], info2['ch_names']) assert_equal(info['ch_names'], info2['ch_names']) for key in ['dev_ctf_t', 'dev_head_t', 'ctf_head_t']: assert_array_equal(info[key]['trans'], info2[key]['trans']) assert_array_equal(np.array([ch['loc'] for ch in info['chs']]), np.array([ch['loc'] for ch in info2['chs']])) # just check reading data | corner case raw1 = read_raw_bti(pdf_fname=pdf, config_fname=config, head_shape_fname=None, sort_by_ch_name=False, preload=True) # just check reading data | corner case raw2 = read_raw_bti(pdf_fname=pdf, config_fname=config, head_shape_fname=None, rename_channels=False, sort_by_ch_name=True, preload=True) sort_idx = [raw1.bti_ch_labels.index(ch) for ch in raw2.bti_ch_labels] raw1._data = raw1._data[sort_idx] assert_array_equal(raw1._data, raw2._data) assert_array_equal(raw2.bti_ch_labels, raw2.ch_names)
def test_wikipedia_example(): input_ = np.array([1, 0, 1, 0, 0, 1, 1, 0], dtype=np.float64) copy = input_.copy() H = hadamard(8) cyfht(input_) npt.assert_array_equal(np.dot(copy, H), input_)
def test_numeric_bins(self): p = lm._RegressionPlotter(self.df.x, self.df.y) x_binned, bins = p.bin_predictor(self.bins_numeric) npt.assert_equal(len(bins), self.bins_numeric) npt.assert_array_equal(np.unique(x_binned), bins)
def test_permutation_connectivity_equiv(): """Test cluster level permutations with and without connectivity """ try: try: from sklearn.feature_extraction.image import grid_to_graph except ImportError: from scikits.learn.feature_extraction.image import grid_to_graph except ImportError: return rng = np.random.RandomState(0) # subjects, time points, spatial points X = rng.randn(7, 2, 10) # add some significant points X[:, 0:2, 0:2] += 10 # span two time points and two spatial points X[:, 1, 5:9] += 10 # span four time points max_steps = [1, 1, 1, 2] # This will run full algorithm in two ways, then the ST-algorithm in 2 ways # All of these should give the same results conns = [None, grid_to_graph(2, 10), grid_to_graph(1, 10), grid_to_graph(1, 10)] stat_map = None thresholds = [2, dict(start=0.5, step=0.5)] sig_counts = [2, 8] sdps = [0, 0.05, 0.05] ots = ['mask', 'mask', 'indices'] for thresh, count in zip(thresholds, sig_counts): cs = None ps = None for max_step, conn in zip(max_steps, conns): for stat_fun in [ttest_1samp_no_p, partial(ttest_1samp_no_p, sigma=1e-3)]: for sdp, ot in zip(sdps, ots): t, clusters, p, H0 = \ permutation_cluster_1samp_test(X, threshold=thresh, connectivity=conn, n_jobs=2, max_step=max_step, stat_fun=stat_fun, step_down_p=sdp, out_type=ot) # make sure our output datatype is correct if ot == 'mask': assert_true(isinstance(clusters[0], np.ndarray)) assert_true(clusters[0].dtype == bool) assert_array_equal(clusters[0].shape, X.shape[1:]) else: # ot == 'indices' assert_true(isinstance(clusters[0], tuple)) # make sure all comparisons were done; for TFCE, no perm # should come up empty if count == 8: assert_true(not np.any(H0 == 0)) inds = np.where(p < 0.05)[0] assert_true(len(inds) == count) this_cs = [clusters[ii] for ii in inds] this_ps = p[inds] this_stat_map = np.zeros((2, 10), dtype=bool) for ci, c in enumerate(this_cs): if isinstance(c, tuple): this_c = np.zeros((2, 10), bool) for x, y in zip(c[0], c[1]): this_stat_map[x, y] = True this_c[x, y] = True this_cs[ci] = this_c c = this_c this_stat_map[c] = True if cs is None: ps = this_ps cs = this_cs if stat_map is None: stat_map = this_stat_map assert_array_equal(ps, this_ps) assert_true(len(cs) == len(this_cs)) for c1, c2 in zip(cs, this_cs): assert_array_equal(c1, c2) assert_array_equal(stat_map, this_stat_map)
def test_provided_bins(self): p = lm._RegressionPlotter(self.df.x, self.df.y) x_binned, bins = p.bin_predictor(self.bins_given) npt.assert_array_equal(np.unique(x_binned), self.bins_given)
def test_margin_grid_from_dataframe(self): g = ag.JointGrid("x", "y", self.data) npt.assert_array_equal(g.x, self.x) npt.assert_array_equal(g.y, self.y)
def test_cluster_permutation_with_connectivity(): """Test cluster level permutations with connectivity matrix.""" try: try: from sklearn.feature_extraction.image import grid_to_graph except ImportError: from scikits.learn.feature_extraction.image import grid_to_graph except ImportError: return n_pts = condition1_1d.shape[1] # we don't care about p-values in any of these, so do fewer permutations args = dict(seed=None, max_step=1, exclude=None, step_down_p=0, t_power=1, threshold=1.67, check_disjoint=False, n_permutations=50) did_warn = False for X1d, X2d, func, spatio_temporal_func in \ [(condition1_1d, condition1_2d, permutation_cluster_1samp_test, spatio_temporal_cluster_1samp_test), ([condition1_1d, condition2_1d], [condition1_2d, condition2_2d], permutation_cluster_test, spatio_temporal_cluster_test)]: out = func(X1d, **args) connectivity = grid_to_graph(1, n_pts) out_connectivity = func(X1d, connectivity=connectivity, **args) assert_array_equal(out[0], out_connectivity[0]) for a, b in zip(out_connectivity[1], out[1]): assert_array_equal(out[0][a], out[0][b]) assert_true(np.all(a[b])) # test spatio-temporal w/o time connectivity (repeat spatial pattern) connectivity_2 = sparse.coo_matrix( linalg.block_diag(connectivity.asfptype().todense(), connectivity.asfptype().todense())) if isinstance(X1d, list): X1d_2 = [np.concatenate((x, x), axis=1) for x in X1d] else: X1d_2 = np.concatenate((X1d, X1d), axis=1) out_connectivity_2 = func(X1d_2, connectivity=connectivity_2, **args) # make sure we were operating on the same values split = len(out[0]) assert_array_equal(out[0], out_connectivity_2[0][:split]) assert_array_equal(out[0], out_connectivity_2[0][split:]) # make sure we really got 2x the number of original clusters n_clust_orig = len(out[1]) assert_true(len(out_connectivity_2[1]) == 2 * n_clust_orig) # Make sure that we got the old ones back data_1 = set([np.sum(out[0][b[:n_pts]]) for b in out[1]]) data_2 = set([np.sum(out_connectivity_2[0][a[:n_pts]]) for a in out_connectivity_2[1][:]]) assert_true(len(data_1.intersection(data_2)) == len(data_1)) # now use the other algorithm if isinstance(X1d, list): X1d_3 = [np.reshape(x, (-1, 2, 350)) for x in X1d_2] else: X1d_3 = np.reshape(X1d_2, (-1, 2, 350)) out_connectivity_3 = spatio_temporal_func( X1d_3, n_permutations=50, connectivity=connectivity, max_step=0, threshold=1.67, check_disjoint=True) # make sure we were operating on the same values split = len(out[0]) assert_array_equal(out[0], out_connectivity_3[0][0]) assert_array_equal(out[0], out_connectivity_3[0][1]) # make sure we really got 2x the number of original clusters assert_true(len(out_connectivity_3[1]) == 2 * n_clust_orig) # Make sure that we got the old ones back data_1 = set([np.sum(out[0][b[:n_pts]]) for b in out[1]]) data_2 = set([np.sum(out_connectivity_3[0][a[0], a[1]]) for a in out_connectivity_3[1]]) assert_true(len(data_1.intersection(data_2)) == len(data_1)) # test new versus old method out_connectivity_4 = spatio_temporal_func( X1d_3, n_permutations=50, connectivity=connectivity, max_step=2, threshold=1.67) out_connectivity_5 = spatio_temporal_func( X1d_3, n_permutations=50, connectivity=connectivity, max_step=1, threshold=1.67) # clusters could be in a different order sums_4 = [np.sum(out_connectivity_4[0][a]) for a in out_connectivity_4[1]] sums_5 = [np.sum(out_connectivity_4[0][a]) for a in out_connectivity_5[1]] sums_4 = np.sort(sums_4) sums_5 = np.sort(sums_5) assert_array_almost_equal(sums_4, sums_5) assert_raises(ValueError, spatio_temporal_func, X1d_3, n_permutations=1, connectivity=connectivity, max_step=1, threshold=1.67, n_jobs=-1000) # not enough TFCE params assert_raises(KeyError, spatio_temporal_func, X1d_3, connectivity=connectivity, threshold=dict(me='hello')) # too extreme a start threshold with warnings.catch_warnings(True) as w: spatio_temporal_func(X1d_3, connectivity=connectivity, threshold=dict(start=10, step=1)) if not did_warn: assert_true(len(w) == 1) did_warn = True # too extreme a start threshold assert_raises(ValueError, spatio_temporal_func, X1d_3, connectivity=connectivity, tail=-1, threshold=dict(start=1, step=-1)) assert_raises(ValueError, spatio_temporal_func, X1d_3, connectivity=connectivity, tail=-1, threshold=dict(start=-1, step=1)) # wrong type for threshold assert_raises(TypeError, spatio_temporal_func, X1d_3, connectivity=connectivity, threshold=[]) # wrong value for tail assert_raises(ValueError, spatio_temporal_func, X1d_3, connectivity=connectivity, tail=2) # make sure it actually found a significant point out_connectivity_6 = spatio_temporal_func( X1d_3, n_permutations=50, connectivity=connectivity, max_step=1, threshold=dict(start=1, step=1)) assert_true(np.min(out_connectivity_6[2]) < 0.05)
def test_margin_grid_from_lists(self): g = ag.JointGrid(self.x.tolist(), self.y.tolist()) npt.assert_array_equal(g.x, self.x) npt.assert_array_equal(g.y, self.y)
def test_hue_order(self): order = list("dcab") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map(plt.plot) for line, level in zip(g.axes[1, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "y"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_diag(plt.plot) for line, level in zip(g.axes[0, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "x"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_lower(plt.plot) for line, level in zip(g.axes[1, 0].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "x"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "y"]) plt.close("all") g = ag.PairGrid(self.df, hue="a", hue_order=order) g.map_upper(plt.plot) for line, level in zip(g.axes[0, 1].lines, order): x, y = line.get_xydata().T npt.assert_array_equal(x, self.df.loc[self.df.a == level, "y"]) npt.assert_array_equal(y, self.df.loc[self.df.a == level, "x"]) plt.close("all")
def test_adj_list(self): expected = [[1, 2], [1], [1], [3]] for G in self.graphs: adj_list = G.adj_list() for a, e in zip(adj_list, expected): assert_array_equal(a, e)
def test_margin_grid_from_series(self): g = ag.JointGrid(self.data.x, self.data.y) npt.assert_array_equal(g.x, self.x) npt.assert_array_equal(g.y, self.y)
def test_degree(self): for G in self.graphs: in_degree = G.degree('in', weighted=False) out_degree = G.degree('out', weighted=False) assert_array_equal(in_degree, [0, 3, 1, 1]) assert_array_equal(out_degree, [2, 1, 1, 1])
def test_normal_axes(self): null = np.empty(0, object).flat g = ag.FacetGrid(self.df) npt.assert_array_equal(g._bottom_axes, g.axes.flat) npt.assert_array_equal(g._not_bottom_axes, null) npt.assert_array_equal(g._left_axes, g.axes.flat) npt.assert_array_equal(g._not_left_axes, null) npt.assert_array_equal(g._inner_axes, null) g = ag.FacetGrid(self.df, col="c") npt.assert_array_equal(g._bottom_axes, g.axes.flat) npt.assert_array_equal(g._not_bottom_axes, null) npt.assert_array_equal(g._left_axes, g.axes[:, 0].flat) npt.assert_array_equal(g._not_left_axes, g.axes[:, 1:].flat) npt.assert_array_equal(g._inner_axes, null) g = ag.FacetGrid(self.df, row="c") npt.assert_array_equal(g._bottom_axes, g.axes[-1, :].flat) npt.assert_array_equal(g._not_bottom_axes, g.axes[:-1, :].flat) npt.assert_array_equal(g._left_axes, g.axes.flat) npt.assert_array_equal(g._not_left_axes, null) npt.assert_array_equal(g._inner_axes, null) g = ag.FacetGrid(self.df, col="a", row="c") npt.assert_array_equal(g._bottom_axes, g.axes[-1, :].flat) npt.assert_array_equal(g._not_bottom_axes, g.axes[:-1, :].flat) npt.assert_array_equal(g._left_axes, g.axes[:, 0].flat) npt.assert_array_equal(g._not_left_axes, g.axes[:, 1:].flat) npt.assert_array_equal(g._inner_axes, g.axes[:-1, 1:].flat)
def test_to_networkx(self): for G in self.graphs + [self.weighted]: nx = G.to_networkx() adj = networkx.to_numpy_matrix(nx) assert_array_equal(G.matrix('dense'), adj)
def test_degree_weighted(self): in_degree = self.weighted.degree(kind='in', weighted=True) out_degree = self.weighted.degree(kind='out', weighted=True) assert_array_equal(in_degree, [0, 3, 2, 3]) assert_array_equal(out_degree, [3, 1, 1, 3])
def test_nearest_two(self): assert_array_equal( self.kdtree.query((0, 0, 0.1), 2), ([0.1, 0.9], [0, 1]))
def test_copy(self): for G in self.graphs: gg = G.copy() self.assertIsNot(gg, G) assert_array_equal(gg.matrix('dense'), G.matrix('dense')) assert_array_equal(gg.pairs(), G.pairs())
def test_len0_arrays(kdtree_type): # make sure len-0 arrays are handled correctly # in range queries (gh-5639) np.random.seed(1234) X = np.random.rand(10, 2) Y = np.random.rand(10, 2) tree = kdtree_type(X) # query_ball_point (single) d, i = tree.query([.5, .5], k=1) z = tree.query_ball_point([.5, .5], 0.1*d) assert_array_equal(z, []) # query_ball_point (multiple) d, i = tree.query(Y, k=1) mind = d.min() z = tree.query_ball_point(Y, 0.1*mind) y = np.empty(shape=(10, ), dtype=object) y.fill([]) assert_array_equal(y, z) # query_ball_tree other = kdtree_type(Y) y = tree.query_ball_tree(other, 0.1*mind) assert_array_equal(10*[[]], y) # count_neighbors y = tree.count_neighbors(other, 0.1*mind) assert_(y == 0) # sparse_distance_matrix y = tree.sparse_distance_matrix(other, 0.1*mind, output_type='dok_matrix') assert_array_equal(y == np.zeros((10, 10)), True) y = tree.sparse_distance_matrix(other, 0.1*mind, output_type='coo_matrix') assert_array_equal(y == np.zeros((10, 10)), True) y = tree.sparse_distance_matrix(other, 0.1*mind, output_type='dict') assert_equal(y, {}) y = tree.sparse_distance_matrix(other, 0.1*mind, output_type='ndarray') _dtype = [('i', np.intp), ('j', np.intp), ('v', np.float64)] res_dtype = np.dtype(_dtype, align=True) z = np.empty(shape=(0, ), dtype=res_dtype) assert_array_equal(y, z) # query_pairs d, i = tree.query(X, k=2) mind = d[:, -1].min() y = tree.query_pairs(0.1*mind, output_type='set') assert_equal(y, set()) y = tree.query_pairs(0.1*mind, output_type='ndarray') z = np.empty(shape=(0, 2), dtype=np.intp) assert_array_equal(y, z)
def test_split(self): less, greater = self.rect.split(0, 0.1) assert_array_equal(less.maxes, [0.1, 1]) assert_array_equal(less.mins, [0, 0]) assert_array_equal(greater.maxes, [1, 1]) assert_array_equal(greater.mins, [0.1, 0])
def test_shc_default_euclidean(): """Test default parameters of SHC, using euclidean distance.""" X, _ = generate_data(supervised=False, affinity=False) clusterer = ScipyHierarchicalClustering(n_clusters=4) labels = clusterer.fit_predict(X) assert_array_equal([25, 25, 25, 25], np.bincount(labels))
def test_nearest(self): assert_array_equal( self.kdtree.query((0, 0, 0.1), 1), (0.1, 0))
def test_oneclass_score_samples(): X_train = [[1, 1], [1, 2], [2, 1]] clf = svm.OneClassSVM(gamma=1).fit(X_train) assert_array_equal(clf.score_samples([[2., 2.]]), clf.decision_function([[2., 2.]]) + clf.offset_)
def test_shaper(): """Test for Shaper""" X = np.array([[0, 1, 2], [3, 4, 5]], dtype=np.int) Xt = Shaper((-1, 1)).fit_transform(X) assert_array_equal(Xt, [[0], [1], [2], [3], [4], [5]]) assert_array_equal(Xt.shape, (6, 1)) Xt = Shaper((-1, )).fit_transform(X) assert_array_equal(Xt, [0, 1, 2, 3, 4, 5]) assert_array_equal(Xt.shape, (6, )) Xt = Shaper((-1, 1), order="F").fit_transform(X) assert_array_equal(Xt, [[0], [3], [1], [4], [2], [5]]) assert_array_equal(Xt.shape, (6, 1)) assert np.isfortran(Xt)
def test_dtype_keyword(as_iterable, values, dst_type): array = _infer_data_type(as_iterable(values), dtype=dst_type) assert array.dtype == dst_type assert_array_equal(array, values)
def test_precomputed(): # SVC with a precomputed kernel. # We test it with a toy dataset and with iris. clf = svm.SVC(kernel='precomputed') # Gram matrix for train data (square matrix) # (we use just a linear kernel) K = np.dot(X, np.array(X).T) clf.fit(K, Y) # Gram matrix for test data (rectangular matrix) KT = np.dot(T, np.array(X).T) pred = clf.predict(KT) assert_raises(ValueError, clf.predict, KT.T) assert_array_equal(clf.dual_coef_, [[-0.25, .25]]) assert_array_equal(clf.support_, [1, 3]) assert_array_equal(clf.intercept_, [0]) assert_array_almost_equal(clf.support_, [1, 3]) assert_array_equal(pred, true_result) # Gram matrix for test data but compute KT[i,j] # for support vectors j only. KT = np.zeros_like(KT) for i in range(len(T)): for j in clf.support_: KT[i, j] = np.dot(T[i], X[j]) pred = clf.predict(KT) assert_array_equal(pred, true_result) # same as before, but using a callable function instead of the kernel # matrix. kernel is just a linear kernel kfunc = lambda x, y: np.dot(x, y.T) clf = svm.SVC(gamma='scale', kernel=kfunc) clf.fit(X, Y) pred = clf.predict(T) assert_array_equal(clf.dual_coef_, [[-0.25, .25]]) assert_array_equal(clf.intercept_, [0]) assert_array_almost_equal(clf.support_, [1, 3]) assert_array_equal(pred, true_result) # test a precomputed kernel with the iris dataset # and check parameters against a linear SVC clf = svm.SVC(kernel='precomputed') clf2 = svm.SVC(kernel='linear') K = np.dot(iris.data, iris.data.T) clf.fit(K, iris.target) clf2.fit(iris.data, iris.target) pred = clf.predict(K) assert_array_almost_equal(clf.support_, clf2.support_) assert_array_almost_equal(clf.dual_coef_, clf2.dual_coef_) assert_array_almost_equal(clf.intercept_, clf2.intercept_) assert_almost_equal(np.mean(pred == iris.target), .99, decimal=2) # Gram matrix for test data but compute KT[i,j] # for support vectors j only. K = np.zeros_like(K) for i in range(len(iris.data)): for j in clf.support_: K[i, j] = np.dot(iris.data[i], iris.data[j]) pred = clf.predict(K) assert_almost_equal(np.mean(pred == iris.target), .99, decimal=2) clf = svm.SVC(gamma='scale', kernel=kfunc) clf.fit(iris.data, iris.target) assert_almost_equal(np.mean(pred == iris.target), .99, decimal=2)