def test_nddataset_swapdims(nd1d, nd2d, ref_ds, ds1): nd1 = nd1d.copy() nd2 = nd2d.copy() nd3 = ds1.copy() # swapdims needs 2D at least assert nd1.shape == (10,) nd1s = nd1.swapdims(1, 0) assert_equal(nd1s.data, nd1.data) nd2s = nd2.swapdims(1, 0) assert nd2s.dims == nd2.dims[::-1] assert nd3.shape == ref_ds.shape nd3s = nd3.swapdims(1, 0) ref = ref_ds refs = np.swapaxes(ref, 1, 0) assert nd3.shape == ref.shape # original unchanged assert nd3s.shape == refs.shape assert nd3s is not nd3 assert nd3s.dims[:2] == nd3.dims[:2][::-1] nd3s = nd3.swapdims(1, 0, inplace=True) assert nd3.shape == refs.shape # original changed assert nd3s is nd3 # objects should be the same # use of the numpy method nd3s = np.swapaxes(nd3, 1, 0) assert nd3.shape == refs.shape # original unchanged (but was already # swapped) assert nd3s.shape == ref.shape assert nd3s is not nd3 # TODO: add check for swapdims of all elements # of a dataset such as meta
def test_ndcomplex_real_imag(): np.random.seed(12345) d = np.random.random((2, 2)) * np.exp(.1j) d3 = NDComplexArray(d) new = d3.copy() new.data = d3.real.data + 1j * d3.imag.data assert_equal(d3.data, new.data)
def test_nmr_1D_apodization(NMR_dataset_1D): dataset = NMR_dataset_1D.copy() dataset /= dataset.real.data.max() # normalize lb = 0 arr, apod = dataset.em(lb=lb, retapod=True) # arr and dataset should be equal as no em was applied assert_equal(dataset.data, arr.data) lb = 0.0 gb = 0.0 arr, apod = dataset.gm(lb=lb, gb=gb, retapod=True) # arr and dataset should be equal as no em was applied assert_equal(dataset.data, arr.data) lb = 100 arr, apod = dataset.em(lb=lb, retapod=True) # arr and dataset should not be equal as inplace=False assert not np.array_equal(dataset.data, arr.data) assert_array_almost_equal(apod[1], 0.9987, decimal=4) # inplace=True dataset.plot(xlim=(0.0, 6000.0)) dataset.em(lb=100.0 * ur.Hz, inplace=True) dataset.plot(c="r", data_only=True, clear=False) # successive call dataset.em(lb=200.0 * ur.Hz, inplace=True) dataset.plot(c="g", data_only=True, clear=False) dataset = NMR_dataset_1D.copy() dataset.plot() dataset.em(100.0 * ur.Hz, inplace=True) dataset.plot(c="r", data_only=True, clear=False) # first test gm dataset = NMR_dataset_1D.copy() dataset /= dataset.real.data.max() # normalize dataset.plot(xlim=(0.0, 6000.0)) dataset, apod = dataset.gm(lb=-100.0 * ur.Hz, gb=100.0 * ur.Hz, retapod=True) dataset.plot(c="b", data_only=True, clear=False) apod.plot(c="r", data_only=True, clear=False) show()
def test_nddataset_slicing_by_label(ds1): da = ds1 # selection planeb = da['b'] assert type(planeb) == type(da) plane1 = da[1] assert_equal(planeb.data, plane1.data) assert planeb.ndim == 3 assert planeb.size == 300 bd = da['b':'f'] # the last index is included assert bd.shape == (5, 100, 3) b1 = da[1:6] assert_equal(bd.data, b1.data) bc = da['b':'f', :, "hot"] assert bc.shape == (5, 100, 1) assert bc.z.labels[0] == 'b' da[..., "hot"] # TODO: find a way to use such syntax # hot2 = da[ # # "x.hot"] # assert hot == hot2
def test_ndarray_slicing(refarray, ndarray): ref = refarray nd = ndarray.copy() assert not nd.is_masked assert nd.dims == ['y', 'x'] # slicing is different in scpy than with numpy. We always return # unsqueezed dimensions, except for array of size 1, which are considered as scalar nd1 = nd[0, 0] assert_equal(nd1.data, nd.data[0:1, 0:1]) assert nd1 is not nd[0, 0] assert nd1.ndim == 2 # array not reduced assert nd1.size == 1 assert nd1.shape == (1, 1) assert isinstance(nd1, NDArray) assert isinstance(nd1.data, np.ndarray) assert isinstance(nd1.values, TYPE_FLOAT) nd1b, id = nd.__getitem__((0, 0), return_index=True) assert nd1b == nd1 nd1a = nd[0, 0:2] assert_equal(nd1a.data, nd.data[0:1, 0:2]) assert nd1a is not nd[0, 0:2] assert nd1a.ndim == 2 assert nd1a.size == 2 assert nd1a.shape == (1, 2) assert isinstance(nd1a, NDArray) assert nd1a.dims == ['y', 'x'] # returning none if empty when slicing nd1b = nd[11:, 11:] assert nd1b is None # nd has been changed, restore it before continuing nd = ndarray.copy() nd2 = nd[7:10] assert_equal(nd2.data, nd.data[7:10]) assert not nd.is_masked nd3 = nd2[1] assert nd3.shape == (1, ref.shape[1]) assert nd3.dims == ['y', 'x'] nd4 = nd2[:, 1] assert nd4.shape == (3, 1) assert nd4.dims == ['y', 'x'] # squezzing nd5 = nd4.squeeze() assert nd5.shape == (3, ) assert nd5.dims == ['y'] # set item nd[1] = 2. assert nd[1, 0] == 2 # set item mask nd[1] = MASKED assert nd.is_masked # boolean indexing nd = ndarray.copy() nd[nd.data > 0] # fancy indexing df = nd.data[[-1, 1]] ndf = nd[[-1, 1]] assert_array_equal(ndf.data, df) ndf = nd[[ -1, 1 ], INPLACE] # TODO: check utility of this (I remember it should be related to setitem) assert_array_equal(ndf.data, df) # use with selection from other numpy functions am = np.argmax(nd.data, axis=1) assert_array_equal(am, np.array([7, 3])) amm = nd.data[..., am] assert_array_equal(nd[..., am].data, amm)