def test_non_unique_arg_member(self): """ test how the non-unique membership operation performs """ arr1 = numpy.array([[1, 1, 1], [1, 1, -1], [0, 0, 0]]) las2 = lexarrayset.create([[0, 1], [0, 1], [0, 0]]) members = lexarrayset.nonunique_member(arr1, las2.data) assert_array_equal(members, [True, True, False]) arr1 = numpy.array([[1, 3, 3, 1, 1, 1, 1], [2, 2, 2, 2, 1, 1, -1], [3, 1, 1, 3, 0, 0, 0]]) las2 = lexarrayset.create([[0, 1, -1, 1], [0, 1, -1, 2], [0, 0, -1, 3]]) members = lexarrayset.nonunique_member(arr1, las2.data) assert_array_equal(members, [True, False, False, True, True, True, False, ])
def test_non_unique_indices_query_offset(self): states = [[0, 0, 1, 1, 2, 7, 2], [0, 1, 0, 1, 1, 0, 2]] goal_order = [0, 2, 5, 1, 3, 4, 6] assert_array_equal(numpy.lexsort(states), goal_order) enum = state_enum.create(states) # apply offset offset = 42 enum.offset = offset # verify that looking up the indices # for a collection of non-unique query states # returns a correctly sized index array with the correct # corresponding indices query_states = [[7, 2, 1, 2, 1, 7, 7, 2, 1], [0, 1, 0, 2, 0, 0, 0, 2, 1]] indices = enum.indices(query_states) assert_array_equal(indices - offset, [2, 5, 1, 6, 1, 2, 2, 6, 4])
def _mask_tester(norm_instance, vals): """ Checks mask handling """ masked_array = np.ma.array(vals) masked_array[0] = np.ma.masked assert_array_equal(masked_array.mask, norm_instance(masked_array).mask)
def test_non_unique_contains_query(self): states = [[0, 0, 1, 1, 2, 7, 2], [0, 1, 0, 1, 1, 0, 2]] enum = state_enum.create(states) query_states = [[-1, 7, 2, 1, 2, 9, 1, 7, 7, 2, -1, 1], [-1, 0, 1, 0, 2, 9, 0, 0, 0, 2, -1, 1]] member_flags = enum.contains(query_states) goal_member_flags = [False, True, True, True, True, False, True, True, True, True, False, True] assert_array_equal(member_flags, goal_member_flags)
def test_cmap_and_norm_from_levels_and_colors2(): levels = [-1, 2, 2.5, 3] colors = ["red", (0, 1, 0), "blue", (0.5, 0.5, 0.5), (0.0, 0.0, 0.0, 1.0)] clr = mcolors.colorConverter.to_rgba_array(colors) bad = (0.1, 0.1, 0.1, 0.1) no_color = (0.0, 0.0, 0.0, 0.0) masked_value = "masked_value" # Define the test values which are of interest. # Note: levels are lev[i] <= v < lev[i+1] tests = [ ("both", None, {-2: clr[0], -1: clr[1], 2: clr[2], 2.25: clr[2], 3: clr[4], 3.5: clr[4], masked_value: bad}), ("min", -1, {-2: clr[0], -1: clr[1], 2: clr[2], 2.25: clr[2], 3: no_color, 3.5: no_color, masked_value: bad}), ("max", -1, {-2: no_color, -1: clr[0], 2: clr[1], 2.25: clr[1], 3: clr[3], 3.5: clr[3], masked_value: bad}), ( "neither", -2, {-2: no_color, -1: clr[0], 2: clr[1], 2.25: clr[1], 3: no_color, 3.5: no_color, masked_value: bad}, ), ] for extend, i1, cases in tests: cmap, norm = mcolors.from_levels_and_colors(levels, colors[0:i1], extend=extend) cmap.set_bad(bad) for d_val, expected_color in cases.items(): if d_val == masked_value: d_val = np.ma.array([1], mask=True) else: d_val = [d_val] assert_array_equal( expected_color, cmap(norm(d_val))[0], "Wih extend={0!r} and data " "value={1!r}".format(extend, d_val) ) assert_raises(ValueError, mcolors.from_levels_and_colors, levels, colors)
def test_MRData_clean_nans(): data = np.array([[1, 2, 3, np.nan], [np.nan, 4, 5, 6]]) result = MRData()._clean_nans(data) expected_result = np.array([[1, 2, 3, 6], [1, 4, 5, 6]]) assert_array_equal(result, expected_result)
def test_strictly_decreasing_but_less_than_threshold(self): data = np.linspace(1.05, 1.0, 10) result = zigzag.peak_valley_pivots(data, 0.1, -0.1) expected_result = np.zeros_like(data) expected_result[0], expected_result[-1] = PEAK, VALLEY assert_array_equal(result, expected_result)
def test_strictly_increasing(self): data = np.linspace(1, 10, 10) result = zigzag.peak_valley_pivots(data, 0.1, -0.1) expected_result = np.zeros_like(data) expected_result[0], expected_result[-1] = VALLEY, PEAK assert_array_equal(result, expected_result)
def test_rgba(self): actual = dmp(self.arr3, self.arr_rgba) ind = [0, 1, 5] expected = (self.arr3.take(ind).compressed(), self.arr_rgba.take(ind, axis=0)) assert_array_equal(actual[0], expected[0]) assert_array_equal(actual[1], expected[1])
def test_set_density_simulator(): scen, control = test_ctm.small_scenario() set_densities = {1: [.5] * scen.T} sim = ctm_admm.ControlledCtmSimulator(set_densities) state = sim.simulate(scen, control) state.plot() assert_array_equal(state.density[set_densities.keys(), :-1], set_densities.values())
def test_meg_field_interpolation_helmet(): """Test interpolation of MEG field onto helmet """ evoked = read_evoked(evoked_fname, setno='Left Auditory') info = evoked.info surf = get_meg_helmet_surf(info) # let's reduce the number of channels by a bunch to speed it up info['bads'] = info['ch_names'][:200] # bad ch_type assert_raises(ValueError, make_surface_mapping, info, surf, 'foo') # bad mode assert_raises(ValueError, make_surface_mapping, info, surf, 'meg', mode='foo') # no picks evoked_eeg = pick_types_evoked(evoked, meg=False, eeg=True) assert_raises(RuntimeError, make_surface_mapping, evoked_eeg.info, surf, 'meg') # bad surface def nn = surf['nn'] del surf['nn'] assert_raises(KeyError, make_surface_mapping, info, surf, 'meg') surf['nn'] = nn cf = surf['coord_frame'] del surf['coord_frame'] assert_raises(KeyError, make_surface_mapping, info, surf, 'meg') surf['coord_frame'] = cf # now do it data = make_surface_mapping(info, surf, 'meg', mode='fast') assert_array_equal(data.shape, (304, 106)) # data onto surf
def compare_wcs(w1, w2, exclude_keywords=None): """ Compare two WCSs. Parameters ---------- w1, w2 : `astropy.wcs.WCS` objects exclude_keywords : list List of keywords to excude from comparison. """ exclude_ctype = False keywords = ['crval', 'crpix', 'cd'] if exclude_keywords is not None: exclude_keywords = [kw.lower() for kw in exclude_keywords] if 'ctype' in exclude_keywords: exclude_ctype = True exclude_keywords.remove('ctype') for kw in exclude_keywords: keywords.remove(kw) for kw in keywords: kw1 = getattr(w1.wcs, kw) kw2 = getattr(w2.wcs, kw) utils.assert_allclose(kw1, kw2, 1e-10) #utils.assert_allclose(w1.wcs.crpix, w2.wcs.crpix, 1e-10) #utils.assert_allclose(w1.wcs.cd, w2.wcs.cd, 1e-10) if not exclude_ctype: utils.assert_array_equal(np.array(w1.wcs.ctype), np.array(w2.wcs.ctype))
def testFileOpenAndPostProcess(): raw = rawpy.imread(rawTestPath) assert_array_equal(raw.raw_image.shape, [2844, 4288]) rgb = raw.postprocess(no_auto_bright=True, user_wb=raw.daylight_whitebalance) assert_array_equal(rgb.shape, [2844, 4284, 3]) print_stats(rgb) save('test_8daylight.tiff', rgb) print('daylight white balance multipliers:', raw.daylight_whitebalance) rgb = raw.postprocess(no_auto_bright=True, user_wb=raw.daylight_whitebalance) print_stats(rgb) save('test_8daylight2.tiff', rgb) rgb = raw.postprocess(no_auto_bright=True, user_wb=raw.daylight_whitebalance, output_bps=16) print_stats(rgb) save('test_16daylight.tiff', rgb) # linear images are more useful for science (=no gamma correction) # see http://www.mit.edu/~kimo/blog/linear.html rgb = raw.postprocess(no_auto_bright=True, user_wb=raw.daylight_whitebalance, gamma=(1,1), output_bps=16) print_stats(rgb) save('test_16daylight_linear.tiff', rgb)
def test_rotation_matrix(): from ..angles import rotation_matrix assert_array_equal(rotation_matrix(0*u.deg, 'x'), np.eye(3)) assert_allclose(rotation_matrix(90*u.deg, 'y'), [[ 0, 0,-1], [ 0, 1, 0], [ 1, 0, 0]], atol=1e-12) assert_allclose(rotation_matrix(-90*u.deg, 'z'), [[ 0,-1, 0], [ 1, 0, 0], [ 0, 0, 1]], atol=1e-12) assert_allclose(rotation_matrix(45*u.deg, 'x'), rotation_matrix(45*u.deg, [1, 0, 0])) assert_allclose(rotation_matrix(125*u.deg, 'y'), rotation_matrix(125*u.deg, [0, 1, 0])) assert_allclose(rotation_matrix(-30*u.deg, 'z'), rotation_matrix(-30*u.deg, [0, 0, 1])) assert_allclose(np.dot(rotation_matrix(180*u.deg, [1, 1, 0]).A, [1, 0, 0]), [0, 1, 0], atol=1e-12) #make sure it also works for very small angles assert_allclose(rotation_matrix(0.000001*u.deg, 'x'), rotation_matrix(0.000001*u.deg, [1, 0, 0]))
def testMargin(self): graph = Graph() vol = np.zeros((100, 110, 10), dtype=np.float32) # draw a big plus sign vol[50:70, :, :] = 1.0 vol[:, 60:80, :] = 1.0 vol = vigra.taggedView(vol, axistags="xyz").withAxes(*"txyzc") labels = np.zeros((100, 110, 10), dtype=np.uint32) labels[45:75, 55:85, 3:4] = 1 labels = vigra.taggedView(labels, axistags="xyz").withAxes(*"txyzc") op = OpObjectsSegment(graph=graph) piper = OpArrayPiper(graph=graph) piper.Input.setValue(vol) op.Prediction.connect(piper.Output) op.LabelImage.setValue(labels) # without margin op.Margin.setValue(np.asarray((0, 0, 0))) out = op.Output[...].wait() out = vigra.taggedView(out, axistags=op.Output.meta.axistags) out = out.withAxes(*"xyz") vol = vol.withAxes(*"xyz") assert_array_equal(out[50:70, 60:80, 3] > 0, vol[50:70, 60:80, 3] > 0.5) assert np.all(out[:45, ...] == 0) # with margin op.Margin.setValue(np.asarray((5, 5, 0))) out = op.Output[...].wait() out = vigra.taggedView(out, axistags=op.Output.meta.axistags) out = out.withAxes(*"xyz") assert_array_equal(out[45:75, 55:85, 3] > 0, vol[45:75, 55:85, 3] > 0.5) assert np.all(out[:40, ...] == 0)
def test_ndarray_subclass_norm(recwarn): # Emulate an ndarray subclass that handles units # which objects when adding or subtracting with other # arrays. See #6622 and #8696 class MyArray(np.ndarray): def __isub__(self, other): raise RuntimeError def __add__(self, other): raise RuntimeError data = np.arange(-10, 10, 1, dtype=float) for norm in [mcolors.Normalize(), mcolors.LogNorm(), mcolors.SymLogNorm(3, vmax=5, linscale=1), mcolors.PowerNorm(1)]: assert_array_equal(norm(data.view(MyArray)), norm(data)) if isinstance(norm, mcolors.PowerNorm): assert len(recwarn) == 1 warn = recwarn.pop(UserWarning) assert ('Power-law scaling on negative values is ill-defined' in str(warn.message)) else: assert len(recwarn) == 0 recwarn.clear()
def test_pims_images(image_uid): header = db[image_uid] images = get_images(header, 'img') images[:5] # smoke test assert images.pixel_type == np.float64 assert_array_equal(images.frame_shape, images[0].shape) assert len(images) == image_and_scalar.num1
def test_read_allow_secondary(tickstore_lib): data = [{'ASK': 1545.25, 'ASKSIZE': 1002.0, 'BID': 1545.0, 'BIDSIZE': 55.0, 'CUMVOL': 2187387.0, 'DELETED_TIME': 0, 'INSTRTYPE': 'FUT', 'PRICE': 1545.0, 'SIZE': 1.0, 'TICK_STATUS': 0, 'TRADEHIGH': 1561.75, 'TRADELOW': 1537.25, 'index': 1185076787070}, {'CUMVOL': 354.0, 'DELETED_TIME': 0, 'PRICE': 1543.75, 'SIZE': 354.0, 'TRADEHIGH': 1543.75, 'TRADELOW': 1543.75, 'index': 1185141600600}] tickstore_lib.write('FEED::SYMBOL', data) with patch('pymongo.collection.Collection.find', side_effect=tickstore_lib._collection.find) as find: with patch('pymongo.collection.Collection.with_options', side_effect=tickstore_lib._collection.with_options) as with_options: with patch.object(tickstore_lib, '_read_preference', side_effect=tickstore_lib._read_preference) as read_pref: df = tickstore_lib.read('FEED::SYMBOL', columns=['BID', 'ASK', 'PRICE'], allow_secondary=True) assert read_pref.call_args_list == [call(True)] assert with_options.call_args_list == [call(read_preference=ReadPreference.NEAREST)] assert find.call_args_list == [call({'sy': 'FEED::SYMBOL'}, sort=[('s', 1)], projection={'s': 1, '_id': 0}), call({'sy': 'FEED::SYMBOL', 's': {'$lte': dt(2007, 8, 21, 3, 59, 47, 70000)}}, projection={'sy': 1, 'cs.PRICE': 1, 'i': 1, 'cs.BID': 1, 's': 1, 'im': 1, 'v': 1, 'cs.ASK': 1})] assert_array_equal(df['ASK'].values, np.array([1545.25, np.nan])) assert tickstore_lib._collection.find_one()['c'] == 2
def test_pims_images(): header = db[-1] images = Images(header, 'img') images[:5] # smoke test assert_equal(images.pixel_type, np.float64) assert_array_equal(images.frame_shape, images[0].shape) assert_equal(len(images), image_and_scalar.num1)
def testBBMerge(self): bb1 = BoundingBox(latSouth=-55, lonWest=95, latNorth=-45, lonEast=109) bb2 = BoundingBox(latSouth=44, lonWest=-164, latNorth=74, lonEast=-35) bb = BoundingBox.mergedBoundingBoxes([bb1,bb2]) assert_array_equal([bb.latSouth,bb.latNorth,bb.lonWest,bb.lonEast], [bb1.latSouth,bb2.latNorth,bb1.lonWest,bb2.lonEast]) assert_array_almost_equal(bb.center, [21.136113246, -150])
def test_datetime(self): actual = dmp(self.arr_dt, self.arr3) ind = [0, 1, 5] expected = (self.arr_dt2.take(ind), self.arr3.take(ind).compressed()) assert_array_equal(actual[0], expected[0]) assert_array_equal(actual[1], expected[1])
def test_event_monitor_no_record(): # Check that you can switch off recording spike times/indices G = NeuronGroup(3, '''dv/dt = rate : 1 rate: Hz''', events={'my_event': 'v>1'}, threshold='v>1', reset='v=0') # We don't use 100 and 1000Hz, because then the membrane potential would # be exactly at 1 after 10 resp. 100 timesteps. Due to floating point # issues this will not be exact, G.rate = [101, 0, 1001] * Hz event_mon = EventMonitor(G, 'my_event', record=False) event_mon2 = EventMonitor(G, 'my_event', variables='rate', record=False) spike_mon = SpikeMonitor(G, record=False) spike_mon2 = SpikeMonitor(G, variables='rate', record=False) net = Network(G, event_mon, event_mon2, spike_mon, spike_mon2) net.run(10*ms) # i and t should not be there assert 'i' not in event_mon.variables assert 't' not in event_mon.variables assert 'i' not in spike_mon.variables assert 't' not in spike_mon.variables assert_array_equal(event_mon.count, np.array([1, 0, 10])) assert_array_equal(spike_mon.count, np.array([1, 0, 10])) assert spike_mon.num_spikes == sum(spike_mon.count) assert event_mon.num_events == sum(event_mon.count) # Other variables should still have been recorded assert len(spike_mon2.rate) == spike_mon.num_spikes assert len(event_mon2.rate) == event_mon.num_events
def testBufferOpen(): with open(rawTestPath, 'rb') as rawfile: with rawpy.imread(rawfile) as raw: assert_array_equal(raw.raw_image.shape, [2844, 4288]) rgb = raw.postprocess() print_stats(rgb) save('test_buffer.tiff', rgb)
def test_cad_msip(self): # collecting cad output output_msip = cad.\ cell_assembly_detection(data=self.msip, maxlag=self.maxlag) elements_msip = [] occ_msip = [] lags_msip = [] for out in output_msip: elements_msip.append(out['neurons']) occ_msip.append(out['times']) lags_msip.append(list(out['lags'])) elements_msip = sorted(elements_msip, key=lambda d: len(d)) occ_msip = sorted(occ_msip, key=lambda d: len(d)) lags_msip = sorted(lags_msip, key=lambda d: len(d)) elements_msip = [sorted(e) for e in elements_msip] # check neurons in the patterns assert_array_equal(elements_msip, self.elements_msip) # check the occurrences time of the patters assert_array_equal(occ_msip[0], self.occ_msip[0]) assert_array_equal(occ_msip[1], self.occ_msip[1]) assert_array_equal(occ_msip[2], self.occ_msip[2]) lags_msip = [sorted(e) for e in lags_msip] # check the lags assert_array_equal(lags_msip, self.lags_msip)
def test_XOR(self): ds = self._buildXORData() self.nu.train(ds) results, _ = self.nu.activate(ds) assert_array_equal(results, [[2, 0, 100], [0, 2, 100]])
def test_volsurf_projections(self): white = surf.generate_plane((0, 0, 0), (0, 1, 0), (0, 0, 1), 10, 10) pial = white + np.asarray([[1, 0, 0]]) above = pial + np.asarray([[3, 0, 0]]) vg = volgeom.VolGeom((10, 10, 10), np.eye(4)) vs = volsurf.VolSurfMaximalMapping(vg, white, pial) dx = pial.vertices - white.vertices for s, w in ((white, 0), (pial, 1), (above, 4)): xyz = s.vertices ws = vs.surf_project_weights(True, xyz) delta = vs.surf_unproject_weights_nodewise(ws) - xyz assert_array_equal(delta, np.zeros((100, 3))) assert_true(np.all(w == ws)) vs = volsurf.VolSurfMaximalMapping(vg, white, pial, nsteps=2) n2vs = vs.get_node2voxels_mapping() assert_equal(n2vs, dict((i, {i: 0.0, i + 100: 1.0}) for i in xrange(100))) nd = 17 ds_mm_expected = np.sum((above.vertices - pial.vertices[nd, :]) ** 2, 1) ** 0.5 ds_mm = vs.coordinates_to_grey_distance_mm(nd, above.vertices) assert_array_almost_equal(ds_mm_expected, ds_mm) ds_mm_nodewise = vs.coordinates_to_grey_distance_mm(True, above.vertices) assert_array_equal(ds_mm_nodewise, np.ones((100,)) * 3)
def test_poly1d_multiple_sets(self): p1 = models.Polynomial1D(3, n_models=3) utils.assert_equal(p1.parameters, [0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) utils.assert_array_equal(p1.c0, [0, 0, 0]) p1.c0 = [10, 10, 10] utils.assert_equal(p1.parameters, [10.0, 10.0, 10.0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
def test_mixed_string_and_quantity(): a1 = Angle(['1d', 1. * u.deg]) assert_array_equal(a1.value, [1., 1.]) assert a1.unit == u.deg a2 = Angle(['1d', 1 * u.rad * np.pi, '3d']) assert_array_equal(a2.value, [1., 180., 3.]) assert a2.unit == u.deg
def test_LogNorm(): """ LogNorm ignored clip, now it has the same behavior as Normalize, e.g., values > vmax are bigger than 1 without clip, with clip they are 1. """ ln = mcolors.LogNorm(clip=True, vmax=5) assert_array_equal(ln([1, 6]), [0, 1.0])
def test_scatter_gather_numpy(self): import numpy from numpy.testing.utils import assert_array_equal, assert_array_almost_equal view = self.client[:] a = numpy.arange(64) view.scatter('a', a) b = view.gather('a', block=True) assert_array_equal(b, a)
def test_time_histogram_output(self): # Normalization mean histogram = statistics.time_histogram(self.spiketrains, bin_size=pq.s, output='mean') targ = np.array([4, 2, 1, 1, 2, 2, 1, 0, 1, 0], dtype=float) / 2 assert_array_equal(targ.reshape(targ.size, 1), histogram.magnitude) # Normalization rate histogram = statistics.time_histogram(self.spiketrains, bin_size=pq.s, output='rate') assert_array_equal(histogram.view(pq.Quantity), targ.reshape(targ.size, 1) * 1 / pq.s) # Normalization unspecified, raises error self.assertRaises(ValueError, statistics.time_histogram, self.spiketrains, bin_size=pq.s, output=' ')
def test_spade_msip_3d(self): output_msip = spade.spade(self.msip, self.bin_size, self.winlen, approx_stab_pars=dict( n_subsets=self.n_subset, stability_thresh=self.stability_thresh), n_surr=self.n_surr, spectrum='3d#', alpha=self.alpha, psr_param=self.psr_param, stat_corr='no', output_format='patterns')['patterns'] elements_msip = [] occ_msip = [] lags_msip = [] # collecting spade output for out in output_msip: elements_msip.append(out['neurons']) occ_msip.append(list(out['times'].magnitude)) lags_msip.append(list(out['lags'].magnitude)) elements_msip = sorted(elements_msip, key=len) occ_msip = sorted(occ_msip, key=len) lags_msip = sorted(lags_msip, key=len) # check neurons in the patterns assert_array_equal(elements_msip, self.elements_msip) # check the occurrences time of the patters assert_array_equal(occ_msip, self.occ_msip) # check the lags assert_array_equal(lags_msip, self.lags_msip)
def test_minimal_dataset(self): vol_shape = (10, 10, 10, 3) vol_affine = np.identity(4) vg = volgeom.VolGeom(vol_shape, vol_affine) data = np.random.normal(size=vol_shape) msk = np.ones(vol_shape[:3]) msk[:, 1:-1:2, :] = 0 ni_data = nb.Nifti1Image(data, vol_affine) ni_msk = nb.Nifti1Image(msk, vol_affine) ds = fmri_dataset(ni_data, mask=ni_msk) sphere_density = 20 outer = surf.generate_sphere(sphere_density) * 10. + 5 inner = surf.generate_sphere(sphere_density) * 7. + 5 radius = 10 sel = surf_voxel_selection.run_voxel_selection(radius, ds, inner, outer) sel_fids = set.union(*(set(sel[k]) for k in sel.keys())) ds_vox = map(tuple, ds.fa.voxel_indices) vg = sel.volgeom sel_vox = map(tuple, vg.lin2ijk(np.asarray(list(sel_fids)))) fid_mask = np.asarray([v in sel_vox for v in ds_vox]) assert_array_equal(fid_mask, sel.get_dataset_feature_mask(ds)) # check if it raises errors ni_neg_msk = nb.Nifti1Image(1 - msk, vol_affine) neg_ds = fmri_dataset(ni_data, mask=ni_neg_msk) # inverted mask assert_raises(ValueError, sel.get_dataset_feature_mask, neg_ds) min_ds = sel.get_minimal_dataset(ds) assert_array_equal(min_ds.samples, ds[:, fid_mask].samples)
def test_spade_msip(self): output_msip = spade.spade(self.msip, self.binsize, self.winlen, n_subsets=self.n_subset, stability_thresh=self.stability_thresh, n_surr=self.n_surr, alpha=self.alpha, psr_param=self.psr_param, output_format='patterns')['patterns'] elements_msip = [] occ_msip = [] lags_msip = [] # collecting spade output for out in output_msip: elements_msip.append(out['neurons']) occ_msip.append(list(out['times'].magnitude)) lags_msip.append(list(out['lags'].magnitude)) elements_msip = sorted(elements_msip, key=lambda d: len(d)) occ_msip = sorted(occ_msip, key=lambda d: len(d)) lags_msip = sorted(lags_msip, key=lambda d: len(d)) # check neurons in the patterns assert_array_equal(elements_msip, self.elements_msip) # check the occurrences time of the patters assert_array_equal(occ_msip, self.occ_msip) # check the lags assert_array_equal(lags_msip, self.lags_msip)
def test_date_range_BST(tickstore_lib): DUMMY_DATA = [ {'a': 1., 'b': 2., 'index': dt(2013, 6, 1, 12, 00, tzinfo=mktz('Europe/London')) }, {'a': 3., 'b': 4., 'index': dt(2013, 6, 1, 13, 00, tzinfo=mktz('Europe/London')) }, ] tickstore_lib._chunk_size = 1 tickstore_lib.write('SYM', DUMMY_DATA) df = tickstore_lib.read('SYM', columns=None) assert_array_equal(df['b'].values, np.array([2., 4.])) # df = tickstore_lib.read('SYM', columns=None, date_range=DateRange(dt(2013, 6, 1, 12), # dt(2013, 6, 1, 13))) # assert_array_equal(df['b'].values, np.array([2., 4.])) df = tickstore_lib.read('SYM', columns=None, date_range=DateRange(dt(2013, 6, 1, 12, tzinfo=mktz('Europe/London')), dt(2013, 6, 1, 13, tzinfo=mktz('Europe/London')))) assert_array_equal(df['b'].values, np.array([2., 4.])) df = tickstore_lib.read('SYM', columns=None, date_range=DateRange(dt(2013, 6, 1, 12, tzinfo=mktz('UTC')), dt(2013, 6, 1, 13, tzinfo=mktz('UTC')))) assert_array_equal(df['b'].values, np.array([4., ]))
def test_push_pull_recarray(self): """push/pull recarrays""" import numpy from numpy.testing.utils import assert_array_equal view = self.client[-1] R = numpy.array([ (1, 'hi', 0.), (2**30, 'there', 2.5), (-99999, 'world', -12345.6789), ], [('n', int), ('s', '|S10'), ('f', float)]) view['RR'] = R R2 = view['RR'] r_dtype, r_shape = view.apply_sync(interactive(lambda : (RR.dtype, RR.shape))) self.assertEqual(r_dtype, R.dtype) self.assertEqual(r_shape, R.shape) self.assertEqual(R2.dtype, R.dtype) self.assertEqual(R2.shape, R.shape) assert_array_equal(R2, R)
def test_spade_cpp(self): output_cpp = spade.spade(self.cpp, self.bin_size, 1, approx_stab_pars=dict( n_subsets=self.n_subset, stability_thresh=self.stability_thresh), n_surr=self.n_surr, alpha=self.alpha, psr_param=self.psr_param, stat_corr='no', output_format='patterns')['patterns'] elements_cpp = [] lags_cpp = [] # collecting spade output for out in output_cpp: elements_cpp.append(sorted(out['neurons'])) lags_cpp.append(list(out['lags'].magnitude)) # check neurons in the patterns assert_array_equal(elements_cpp, [range(self.n_neu)]) # check the lags assert_array_equal(lags_cpp, [np.array([0] * (self.n_neu - 1))])
def test_numpy(): import numpy from numpy.testing.utils import assert_array_equal for shape in SHAPES: for dtype in DTYPES: A = numpy.empty(shape, dtype=dtype) _scrub_nan(A) bufs = serialize_object(A) B, r = unserialize_object(bufs) yield nt.assert_equals(r, []) yield nt.assert_equals(A.shape, B.shape) yield nt.assert_equals(A.dtype, B.dtype) yield assert_array_equal(A,B)
def testComplete(self): graph = Graph() op = OpObjectsSegment(graph=graph) piper = OpArrayPiper(graph=graph) piper.Input.setValue(self.vol) op.Prediction.connect(piper.Output) piper = OpArrayPiper(graph=graph) piper.Input.setValue(self.labels) op.LabelImage.connect(piper.Output) # get whole volume out = op.CachedOutput[...].wait() out = vigra.taggedView(out, axistags=op.Output.meta.axistags) # check whether no new blocks introduced mask = np.where(self.labels > 0, 0, 1) masked = out * mask assert_array_equal(masked, 0 * masked) # check whether the interior was labeled 1 assert np.all(out[:, 22:38, 22:38, 22:38, :] > 0) assert np.all(out[:, 62:78, 62:78, 62:78, :] > 0)
def test_Normalize(): norm = mcolors.Normalize() vals = np.arange(-10, 10, 1, dtype=float) _inverse_tester(norm, vals) _scalar_tester(norm, vals) _mask_tester(norm, vals) # Handle integer input correctly (don't overflow when computing max-min, # i.e. 127-(-128) here). vals = np.array([-128, 127], dtype=np.int8) norm = mcolors.Normalize(vals.min(), vals.max()) assert_array_equal(np.asarray(norm(vals)), [0, 1]) # Don't lose precision on longdoubles (float128 on Linux): # for array inputs... vals = np.array([1.2345678901, 9.8765432109], dtype=np.longdouble) norm = mcolors.Normalize(vals.min(), vals.max()) assert_array_equal(np.asarray(norm(vals)), [0, 1]) # and for scalar ones. eps = np.finfo(np.longdouble).resolution norm = plt.Normalize(1, 1 + 100 * eps) assert_equal(norm(1 + 50 * eps), .5)
def test_pattern_set_reduction(self): output_msip = spade.spade(self.patt_psr, self.binsize, self.winlen, n_subsets=self.n_subset, stability_thresh=self.stability_thresh, n_surr=self.n_surr, spectrum='3d#', alpha=self.alpha, psr_param=self.psr_param, output_format='patterns')['patterns'] elements_msip = [] occ_msip = [] lags_msip = [] # collecting spade output for out in output_msip: elements_msip.append(sorted(out['neurons'])) occ_msip.append(list(out['times'].magnitude)) lags_msip.append(list(out['lags'].magnitude)) elements_msip = sorted(elements_msip, key=lambda d: len(d)) occ_msip = sorted(occ_msip, key=lambda d: len(d)) lags_msip = sorted(lags_msip, key=lambda d: len(d)) # check neurons in the patterns assert_array_equal(elements_msip, [range(len(self.lags3) + 1)]) # check the occurrences time of the patters assert_array_equal(len(occ_msip[0]), self.n_occ3)
def test_make_field_map_eeg(): """Test interpolation of EEG field onto head """ evoked = read_evokeds(evoked_fname, condition='Left Auditory') evoked.info['bads'] = ['MEG 2443', 'EEG 053'] # add some bads surf = get_head_surf('sample', subjects_dir=subjects_dir) # we must have trans if surface is in MRI coords assert_raises(ValueError, _make_surface_mapping, evoked.info, surf, 'eeg') evoked.pick_types(meg=False, eeg=True) fmd = make_field_map(evoked, trans_fname, subject='sample', subjects_dir=subjects_dir) # trans is necessary for EEG only assert_raises(RuntimeError, make_field_map, evoked, None, subject='sample', subjects_dir=subjects_dir) fmd = make_field_map(evoked, trans_fname, subject='sample', subjects_dir=subjects_dir) assert_true(len(fmd) == 1) assert_array_equal(fmd[0]['data'].shape, (642, 59)) # maps data onto surf assert_true(len(fmd[0]['ch_names']), 59)
def testScatterGatherNumpy(self): try: import numpy from numpy.testing.utils import assert_array_equal, assert_array_almost_equal except: return else: self.addEngine(4) a = numpy.arange(16) d = self.multiengine.scatter('a', a) d.addCallback(lambda r: self.multiengine.gather('a')) d.addCallback(lambda r: assert_array_equal(r, a)) return d
def test_event_monitor(): G = NeuronGroup(3, '''dv/dt = rate : 1 rate: Hz''', events={'my_event': 'v>1'}) G.run_on_event('my_event', 'v=0') # We don't use 100 and 1000Hz, because then the membrane potential would # be exactly at 1 after 10 resp. 100 timesteps. Due to floating point # issues this will not be exact, G.rate = [101, 0, 1001] * Hz mon = EventMonitor(G, 'my_event') net = Network(G, mon) net.run(10 * ms) event_trains = mon.event_trains() assert_allclose(mon.t[mon.i == 0], [9.9] * ms) assert len(mon.t[mon.i == 1]) == 0 assert_allclose(mon.t[mon.i == 2], np.arange(10) * ms + 0.9 * ms) assert_allclose(mon.t_[mon.i == 0], np.array([9.9 * float(ms)])) assert len(mon.t_[mon.i == 1]) == 0 assert_allclose(mon.t_[mon.i == 2], (np.arange(10) + 0.9) * float(ms)) assert_allclose(event_trains[0], [9.9] * ms) assert len(event_trains[1]) == 0 assert_allclose(event_trains[2], np.arange(10) * ms + 0.9 * ms) assert_array_equal(mon.count, np.array([1, 0, 10])) i, t = mon.it i_, t_ = mon.it_ assert_array_equal(i, mon.i) assert_array_equal(i, i_) assert_array_equal(t, mon.t) assert_array_equal(t_, mon.t_) assert_raises(KeyError, lambda: event_trains[3]) assert_raises(KeyError, lambda: event_trains[-1]) assert_raises(KeyError, lambda: event_trains['string'])
def test_writeStructredFile(self): filePath = self.PATH + "test_structured.csv" data = { "A": { "value": [1, 2, 3], "quality": [-1, -1, -1] }, "B": { "value": [4, 5, 6], "quality": [-2, -2, -2] }, "C": { "value": [7, 8, 9], "quality": [-3, -3, -3] } } self.reader.writeStructredFile(filePath, data) if isfile(filePath): read = self.reader.readEEGFile(filePath) for key, values in data.iteritems(): assert_array_equal(values["value"], read.getColumn(key)) self.removeFile(filePath)
def test_non_unique_arg_member(self): """ test how the non-unique membership operation performs """ arr1 = numpy.array([[1, 1, 1], [1, 1, -1], [0, 0, 0]]) las2 = lexarrayset.create([[0, 1], [0, 1], [0, 0]]) members = lexarrayset.nonunique_member(arr1, las2.data) assert_array_equal(members, [True, True, False]) arr1 = numpy.array([[1, 3, 3, 1, 1, 1, 1], [2, 2, 2, 2, 1, 1, -1], [3, 1, 1, 3, 0, 0, 0]]) las2 = lexarrayset.create([[0, 1, -1, 1], [0, 1, -1, 2], [0, 0, -1, 3]]) members = lexarrayset.nonunique_member(arr1, las2.data) assert_array_equal(members, [ True, False, False, True, True, True, False, ])
def test_cad_msip(self): # collecting cad output output_msip = cad.cell_assembly_detection(binned_spiketrain=self.msip, max_lag=self.max_lag) for i, out in enumerate(output_msip): assert_array_equal(out['times'], self.occ_msip[i] * self.bin_size) assert_array_equal( sorted(out['lags']) * pq.s, self.lags_msip[i] * self.bin_size) assert_array_equal(sorted(out['neurons']), self.elements_msip[i])
def test_spike_monitor(): G = NeuronGroup(3, '''dv/dt = rate : 1 rate: Hz''', threshold='v>1', reset='v=0') # We don't use 100 and 1000Hz, because then the membrane potential would # be exactly at 1 after 10 resp. 100 timesteps. Due to floating point # issues this will not be exact, G.rate = [101, 0, 1001] * Hz mon = SpikeMonitor(G) assert_raises(ValueError, lambda: SpikeMonitor(G, order=1)) # need to specify 'when' as well run(10*ms) spike_trains = mon.spike_trains() assert_allclose(mon.t[mon.i == 0], [9.9]*ms) assert len(mon.t[mon.i == 1]) == 0 assert_allclose(mon.t[mon.i == 2], np.arange(10)*ms + 0.9*ms) assert_allclose(mon.t_[mon.i == 0], np.array([9.9*float(ms)])) assert len(mon.t_[mon.i == 1]) == 0 assert_allclose(mon.t_[mon.i == 2], (np.arange(10) + 0.9)*float(ms)) assert_allclose(spike_trains[0], [9.9]*ms) assert len(spike_trains[1]) == 0 assert_allclose(spike_trains[2], np.arange(10)*ms + 0.9*ms) assert_array_equal(mon.count, np.array([1, 0, 10])) i, t = mon.it i_, t_ = mon.it_ assert_array_equal(i, mon.i) assert_array_equal(i, i_) assert_array_equal(t, mon.t) assert_array_equal(t_, mon.t_) assert_raises(KeyError, lambda: spike_trains[3]) assert_raises(KeyError, lambda: spike_trains[-1]) assert_raises(KeyError, lambda: spike_trains['string'])
def testMargin(self): graph = Graph() vol = np.zeros((100, 110, 10), dtype=np.float32) # draw a big plus sign vol[50:70, :, :] = 1.0 vol[:, 60:80, :] = 1.0 vol = vigra.taggedView(vol, axistags='zyx').withAxes(*'tzyxc') labels = np.zeros((100, 110, 10), dtype=np.uint32) labels[45:75, 55:85, 3:4] = 1 labels = vigra.taggedView(labels, axistags='zyx').withAxes(*'tzyxc') op = OpObjectsSegment(graph=graph) piper = OpArrayPiper(graph=graph) piper.Input.setValue(vol) op.Prediction.connect(piper.Output) op.LabelImage.setValue(labels) # without margin op.MarginZYX.setValue(np.asarray((0, 0, 0))) out = op.Output[...].wait() out = vigra.taggedView(out, axistags=op.Output.meta.axistags) out = out.withAxes(*'zyx') vol = vol.withAxes(*'zyx') assert_array_equal(out[50:70, 60:80, 3] > 0, vol[50:70, 60:80, 3] > .5) assert np.all(out[:45, ...] == 0) # with margin op.MarginZYX.setValue(np.asarray((5, 5, 0))) out = op.Output[...].wait() out = vigra.taggedView(out, axistags=op.Output.meta.axistags) out = out.withAxes(*'zyx') assert_array_equal(out[45:75, 55:85, 3] > 0, vol[45:75, 55:85, 3] > .5) assert np.all(out[:40, ...] == 0)
def test_make_field_map_meg(): """Test interpolation of MEG field onto helmet """ evoked = read_evoked(evoked_fname, setno='Left Auditory') info = evoked.info surf = get_meg_helmet_surf(info) # let's reduce the number of channels by a bunch to speed it up info['bads'] = info['ch_names'][:200] # bad ch_type assert_raises(ValueError, _make_surface_mapping, info, surf, 'foo') # bad mode assert_raises(ValueError, _make_surface_mapping, info, surf, 'meg', mode='foo') # no picks evoked_eeg = pick_types_evoked(evoked, meg=False, eeg=True) assert_raises(RuntimeError, _make_surface_mapping, evoked_eeg.info, surf, 'meg') # bad surface def nn = surf['nn'] del surf['nn'] assert_raises(KeyError, _make_surface_mapping, info, surf, 'meg') surf['nn'] = nn cf = surf['coord_frame'] del surf['coord_frame'] assert_raises(KeyError, _make_surface_mapping, info, surf, 'meg') surf['coord_frame'] = cf # now do it with make_field_map evoked = pick_types_evoked(evoked, meg=True, eeg=False) fmd = make_field_map(evoked, trans_fname=None, subject='sample', subjects_dir=subjects_dir) assert_true(len(fmd) == 1) assert_array_equal(fmd[0]['data'].shape, (304, 106)) # maps data onto surf assert_true(len(fmd[0]['ch_names']), 106) assert_raises(ValueError, make_field_map, evoked, ch_type='foobar')
def testScatterGatherNumpyNonblocking(self): try: import numpy from numpy.testing.utils import assert_array_equal, assert_array_almost_equal except: return else: self.addEngine(4) a = numpy.arange(16) d = self.multiengine.scatter('a', a, block=False) d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True)) d.addCallback(lambda r: self.multiengine.gather('a', block=False)) d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True)) d.addCallback(lambda r: assert_array_equal(r, a)) return d
def test_parameters_3d(self): # test min_spikes parameter output_msip_min_spikes = spade.spade( self.msip, self.bin_size, self.winlen, min_spikes=self.min_spikes, approx_stab_pars=dict( n_subsets=self.n_subset), n_surr=self.n_surr, spectrum='3d#', alpha=self.alpha, psr_param=self.psr_param, stat_corr='no', output_format='patterns')['patterns'] # collecting spade output elements_msip_min_spikes = [] for out in output_msip_min_spikes: elements_msip_min_spikes.append(out['neurons']) elements_msip_min_spikes = sorted( elements_msip_min_spikes, key=len) lags_msip_min_spikes = [] for out in output_msip_min_spikes: lags_msip_min_spikes.append(list(out['lags'].magnitude)) lags_msip_min_spikes = sorted( lags_msip_min_spikes, key=len) # check the lags assert_array_equal(lags_msip_min_spikes, [ l for l in self.lags_msip if len(l) + 1 >= self.min_spikes]) # check the neurons in the patterns assert_array_equal(elements_msip_min_spikes, [ el for el in self.elements_msip if len(el) >= self.min_neu and len( el) >= self.min_spikes]) # test min_occ parameter output_msip_min_occ = spade.spade( self.msip, self.bin_size, self.winlen, min_occ=self.min_occ, approx_stab_pars=dict( n_subsets=self.n_subset), n_surr=self.n_surr, spectrum='3d#', alpha=self.alpha, psr_param=self.psr_param, stat_corr='no', output_format='patterns')['patterns'] # collect spade output occ_msip_min_occ = [] for out in output_msip_min_occ: occ_msip_min_occ.append(list(out['times'].magnitude)) occ_msip_min_occ = sorted(occ_msip_min_occ, key=len) # test occurrences time assert_array_equal(occ_msip_min_occ, [ occ for occ in self.occ_msip if len(occ) >= self.min_occ])
def test_simple_two_model_class_compose_1d(): """ Shift and Scale are two of the simplest models to test model composition with. """ S1 = Shift | Scale # First shift then scale assert issubclass(S1, Model) assert S1.n_inputs == 1 assert S1.n_outputs == 1 s1 = S1(2, 3) # Shift by 2 and scale by 3 assert s1(1) == 9.0 S2 = Scale | Shift # First scale then shift assert issubclass(S2, Model) assert S2.n_inputs == 1 assert S2.n_outputs == 1 s2 = S2(2, 3) # Scale by 2 then shift by 3 assert s2(1) == 5.0 # Test with array inputs assert_array_equal(s2([1, 2, 3]), [5.0, 7.0, 9.0])
def test_getField(self): self._fillDto(count=self.winSize) assert_array_equal(self.dto.getValue("X"), range(self.winSize)) assert_array_equal(self.dto.getQuality("X"), np.array(range(self.winSize)) * 2) testVal = np.array([17]) * self.winSize self.dto.addNewField("X", "test", testVal) assert_array_equal(self.dto.getField("X", "test"), testVal)
def test_rate_monitor_get_states(): G = NeuronGroup(5, 'v : 1', threshold='v>1') # no reset G.v = 1.1 # All neurons spike every time step rate_mon = PopulationRateMonitor(G) run(10*defaultclock.dt) all_states = rate_mon.get_states() assert set(all_states.keys()) == {'rate', 't', 'N'} assert_array_equal(all_states['rate'], rate_mon.rate[:]) assert_array_equal(all_states['t'], rate_mon.t[:]) assert_array_equal(all_states['N'], rate_mon.N)
def test_cad_single_sip(self): # collecting cad output output_single = cad.cell_assembly_detection( binned_spiketrain=self.bin_patt1, max_lag=self.max_lag) # check neurons in the pattern assert_array_equal(sorted(output_single[0]['neurons']), self.elements1) # check the occurrences time of the patter assert_array_equal(output_single[0]['times'], self.occ1) # check the lags assert_array_equal(sorted(output_single[0]['lags']), self.output_lags1)
def test_numpy_in_dict(): import numpy from numpy.testing.utils import assert_array_equal for shape in SHAPES: for dtype in DTYPES: A = numpy.empty(shape, dtype=dtype) _scrub_nan(A) bufs = serialize_object(dict(a=A,b=1,c=list(range(20)))) canned = pickle.loads(bufs[0]) yield nt.assert_true(canned['a'], CannedArray) d, r = unserialize_object(bufs) B = d['a'] yield nt.assert_equals(r, []) yield nt.assert_equals(A.shape, B.shape) yield nt.assert_equals(A.dtype, B.dtype) yield assert_array_equal(A,B)
def test_read_multiple_symbols(tickstore_lib): data1 = [ { 'ASK': 1545.25, 'ASKSIZE': 1002.0, 'BID': 1545.0, 'BIDSIZE': 55.0, 'CUMVOL': 2187387.0, 'DELETED_TIME': 0, 'INSTRTYPE': 'FUT', 'PRICE': 1545.0, 'SIZE': 1.0, 'TICK_STATUS': 0, 'TRADEHIGH': 1561.75, 'TRADELOW': 1537.25, 'index': 1185076787070 }, ] data2 = [{ 'CUMVOL': 354.0, 'DELETED_TIME': 0, 'PRICE': 1543.75, 'SIZE': 354.0, 'TRADEHIGH': 1543.75, 'TRADELOW': 1543.75, 'index': 1185141600600 }] tickstore_lib.write('BAR', data2) tickstore_lib.write('FOO', data1) df = tickstore_lib.read(['FOO', 'BAR'], columns=['BID', 'ASK', 'PRICE']) assert all(df['SYMBOL'].values == ['FOO', 'BAR']) assert_array_equal(df['ASK'].values, np.array([1545.25, np.nan])) assert_array_equal(df['BID'].values, np.array([1545, np.nan])) assert_array_equal(df['PRICE'].values, np.array([1545, 1543.75])) assert_array_equal( df.index.values, np.array([ '2007-07-22T04:59:47.070000000+0100', '2007-07-22T23:00:00.600000000+0100' ], dtype='datetime64[ns]')) assert tickstore_lib._collection.find_one()['c'] == 1