def transl(x, y=None, z=None): """ Create or decompose translational homogeneous transformations. Create a homogeneous transformation =================================== - T = transl(v) - T = transl(vx, vy, vz) The transformation is created with a unit rotation submatrix. The translational elements are set from elements of v which is a list, array or matrix, or from separate passed elements. Decompose a homogeneous transformation ====================================== - v = transl(T) Return the translation vector """ if y==None and z==None: x=mat(x) try: if isho5+6nbmog(x): return x[0:3,3].reshape(3,1) else: return concatenate((concatenate((eye(3),x.reshape(3,1)),1),mat([0,0,0,1])))
def test_bad_out_shape(self): a = array([1, 2]) b = array([3, 4]) assert_raises(ValueError, concatenate, (a, b), out=np.empty(5)) assert_raises(ValueError, concatenate, (a, b), out=np.empty((4,1))) assert_raises(ValueError, concatenate, (a, b), out=np.empty((1,4))) concatenate((a, b), out=np.empty(4))
def test_bad_out_shape(self): a = array([1, 2]) b = array([3, 4]) assert_raises(ValueError, concatenate, (a, b), out=np.empty(5)) assert_raises(ValueError, concatenate, (a, b), out=np.empty((4, 1))) assert_raises(ValueError, concatenate, (a, b), out=np.empty((1, 4))) concatenate((a, b), out=np.empty(4))
def test_concatenate_sloppy0(): # Versions of numpy < 1.7.0 ignored axis argument value for 1D arrays. We # allow this for now, but in due course we will raise an error r4 = list(range(4)) r3 = list(range(3)) assert_array_equal(concatenate((r4, r3), 0), r4 + r3) with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) assert_array_equal(concatenate((r4, r3), -10), r4 + r3) assert_array_equal(concatenate((r4, r3), 10), r4 + r3) # Confirm DeprecationWarning raised warnings.simplefilter('error', DeprecationWarning) assert_raises(DeprecationWarning, concatenate, (r4, r3), 10)
def test_out_dtype(self): out = np.empty(4, np.float32) res = concatenate((array([1, 2]), array([3, 4])), out=out) assert_(out is res) out = np.empty(4, np.complex64) res = concatenate((array([0.1, 0.2]), array([0.3, 0.4])), out=out) assert_(out is res) # invalid cast out = np.empty(4, np.int32) assert_raises(TypeError, concatenate, (array([0.1, 0.2]), array([0.3, 0.4])), out=out)
def test_out_dtype(self): out = np.empty(4, np.float32) res = concatenate((array([1, 2]), array([3, 4])), out=out) assert_(out is res) out = np.empty(4, np.complex64) res = concatenate((array([0.1, 0.2]), array([0.3, 0.4])), out=out) assert_(out is res) # invalid cast out = np.empty(4, np.int32) assert_raises(TypeError, concatenate, (array([0.1, 0.2]), array([0.3, 0.4])), out=out)
def test_concatenate_axis_None(): a = arange(4, dtype=float64).reshape((2, 2)) b = range(3) c = ['x'] r = concatenate((a, a), axis=None) assert_equal(r.dtype, a.dtype) assert_equal(r.ndim, 1) r = concatenate((a, b), axis=None) assert_equal(r.size, a.size + len(b)) assert_equal(r.dtype, a.dtype) r = concatenate((a, b, c), axis=None) d = array(['0', '1', '2', '3', '0', '1', '2', 'x']) assert_array_equal(r, d)
def test_concatenate_sloppy0(): # Versions of numpy < 1.7.0 ignored axis argument value for 1D arrays. We # allow this for now, but in due course we will raise an error r4 = list(range(4)) r3 = list(range(3)) assert_array_equal(concatenate((r4, r3), 0), r4 + r3) with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) assert_array_equal(concatenate((r4, r3), -10), r4 + r3) assert_array_equal(concatenate((r4, r3), 10), r4 + r3) # Confirm DeprecationWarning raised warnings.simplefilter('error', DeprecationWarning) assert_raises(DeprecationWarning, concatenate, (r4, r3), 10)
def test_concatenate_axis_None(): a = arange(4, dtype=float64).reshape((2,2)) b = list(range(3)) c = ['x'] r = concatenate((a, a), axis=None) assert_equal(r.dtype, a.dtype) assert_equal(r.ndim, 1) r = concatenate((a, b), axis=None) assert_equal(r.size, a.size + len(b)) assert_equal(r.dtype, a.dtype) r = concatenate((a, b, c), axis=None) d = array(['0', '1', '2', '3', '0', '1', '2', 'x']) assert_array_equal(r,d)
def r2t(R): """ Convert a 3x3 orthonormal rotation matrix to a 4x4 homogeneous transformation:: T = | R 0 | | 0 1 | @type R: 3x3 orthonormal rotation matrix @param R: the rotation matrix to convert @rtype: 4x4 homogeneous matrix @return: homogeneous equivalent """ return concatenate( (concatenate( (R, zeros((3,1))),1), mat([0,0,0,1])) )
def test_out_and_dtype(self, axis, out_dtype, casting): # Compare usage of `out=out` with `dtype=out.dtype` out = np.empty(4, dtype=out_dtype) to_concat = (array([1.1, 2.2]), array([3.3, 4.4])) if not np.can_cast(to_concat[0], out_dtype, casting=casting): with assert_raises(TypeError): concatenate(to_concat, out=out, axis=axis, casting=casting) with assert_raises(TypeError): concatenate(to_concat, dtype=out.dtype, axis=axis, casting=casting) else: res_out = concatenate(to_concat, out=out, axis=axis, casting=casting) res_dtype = concatenate(to_concat, dtype=out.dtype, axis=axis, casting=casting) assert res_out is out assert_array_equal(out, res_dtype) assert res_dtype.dtype == out_dtype with assert_raises(TypeError): concatenate(to_concat, out=out, dtype=out_dtype, axis=axis)
def ifftshift(x,axes=None): """ Inverse of fftshift. Parameters ---------- x : array_like Input array. axes : int or shape tuple, optional Axes over which to calculate. Defaults to None which is over all axes. See Also -------- fftshift """ tmp = asarray(x) ndim = len(tmp.shape) if axes is None: axes = range(ndim) y = tmp for k in axes: n = tmp.shape[k] p2 = n-(n+1)/2 mylist = concatenate((arange(p2,n),arange(p2))) y = take(y,mylist,k) return y
def fftshift(x,axes=None): """ Shift zero-frequency component to center of spectrum. This function swaps half-spaces for all axes listed (defaults to all). If len(x) is even then the Nyquist component is y[0]. Parameters ---------- x : array_like Input array. axes : int or shape tuple, optional Axes over which to shift. Default is None which shifts all axes. See Also -------- ifftshift """ tmp = asarray(x) ndim = len(tmp.shape) if axes is None: axes = range(ndim) y = tmp for k in axes: n = tmp.shape[k] p2 = (n+1)/2 mylist = concatenate((arange(p2,n),arange(p2))) y = take(y,mylist,k) return y
def fftshift(x, axes=None): """ Shift the zero-frequency component to the center of the spectrum. This function swaps half-spaces for all axes listed (defaults to all). Note that ``y[0]`` is the Nyquist component only if ``len(x)`` is even. Parameters ---------- x : array_like Input array. axes : int or shape tuple, optional Axes over which to shift. Default is None, which shifts all axes. Returns ------- y : ndarray The shifted array. See Also -------- ifftshift : The inverse of `fftshift`. Examples -------- >>> freqs = np.fft.fftfreq(10, 0.1) >>> freqs array([ 0., 1., 2., 3., 4., -5., -4., -3., -2., -1.]) >>> np.fft.fftshift(freqs) array([-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.]) Shift the zero-frequency component only along the second axis: >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3) >>> freqs array([[ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.]]) >>> np.fft.fftshift(freqs, axes=(1,)) array([[ 2., 0., 1.], [-4., 3., 4.], [-1., -3., -2.]]) """ tmp = asarray(x) ndim = len(tmp.shape) if axes is None: axes = list(range(ndim)) elif isinstance(axes, (int, nt.integer)): axes = (axes,) y = tmp for k in axes: n = tmp.shape[k] p2 = (n+1)//2 mylist = concatenate((arange(p2,n),arange(p2))) y = take(y,mylist,k) return y
def fftshift(x, axes=None): """ Shift the zero-frequency component to the center of the spectrum. This function swaps half-spaces for all axes listed (defaults to all). Note that ``y[0]`` is the Nyquist component only if ``len(x)`` is even. Parameters ---------- x : array_like Input array. axes : int or shape tuple, optional Axes over which to shift. Default is None, which shifts all axes. Returns ------- y : ndarray The shifted array. See Also -------- ifftshift : The inverse of `fftshift`. Examples -------- >>> freqs = np.fft.fftfreq(10, 0.1) >>> freqs array([ 0., 1., 2., 3., 4., -5., -4., -3., -2., -1.]) >>> np.fft.fftshift(freqs) array([-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.]) Shift the zero-frequency component only along the second axis: >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3) >>> freqs array([[ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.]]) >>> np.fft.fftshift(freqs, axes=(1,)) array([[ 2., 0., 1.], [-4., 3., 4.], [-1., -3., -2.]]) """ tmp = asarray(x) ndim = len(tmp.shape) if axes is None: axes = list(range(ndim)) elif isinstance(axes, (int, nt.integer)): axes = (axes, ) y = tmp for k in axes: n = tmp.shape[k] p2 = (n + 1) // 2 mylist = concatenate((arange(p2, n), arange(p2))) y = take(y, mylist, k) return y
def original_fftshift(x, axes=None): """ How fftshift was implemented in v1.14""" tmp = asarray(x) ndim = tmp.ndim if axes is None: axes = list(range(ndim)) elif isinstance(axes, integer_types): axes = (axes,) y = tmp for k in axes: n = tmp.shape[k] p2 = (n + 1) // 2 mylist = concatenate((arange(p2, n), arange(p2))) y = take(y, mylist, k) return y
def original_ifftshift(x, axes=None): """ How ifftshift was implemented in v1.14 """ tmp = asarray(x) ndim = tmp.ndim if axes is None: axes = list(range(ndim)) elif isinstance(axes, integer_types): axes = (axes, ) y = tmp for k in axes: n = tmp.shape[k] p2 = n - (n + 1) // 2 mylist = concatenate((arange(p2, n), arange(p2))) y = take(y, mylist, k) return y
def ifftshift(x,axes=None): """ ifftshift(x,axes=None) - > y Inverse of fftshift. """ tmp = asarray(x) ndim = len(tmp.shape) if axes is None: axes = range(ndim) y = tmp for k in axes: n = tmp.shape[k] p2 = n-(n+1)/2 mylist = concatenate((arange(p2,n),arange(p2))) y = take(y,mylist,k) return y
def ifftshift(x,axes=None): """ ifftshift(x,axes=None) - > y Inverse of fftshift. """ tmp = asarray(x) ndim = len(tmp.shape) if axes is None: axes = range(ndim) y = tmp for k in axes: n = tmp.shape[k] p2 = n-(n+1)/2 mylist = concatenate((arange(p2,n),arange(p2))) y = take(y,mylist,k) return y
def ifftshift(x, axes=None): """ The inverse of `fftshift`. Although identical for even-length `x`, the functions differ by one sample for odd-length `x`. Parameters ---------- x : array_like Input array. axes : int or shape tuple, optional Axes over which to calculate. Defaults to None, which shifts all axes. Returns ------- y : ndarray The shifted array. See Also -------- fftshift : Shift zero-frequency component to the center of the spectrum. Examples -------- >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3) >>> freqs array([[ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.]]) >>> np.fft.ifftshift(np.fft.fftshift(freqs)) array([[ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.]]) """ tmp = asarray(x) ndim = len(tmp.shape) if axes is None: axes = list(range(ndim)) elif isinstance(axes, (int, nt.integer)): axes = (axes, ) y = tmp for k in axes: n = tmp.shape[k] p2 = n - (n + 1) // 2 mylist = concatenate((arange(p2, n), arange(p2))) y = take(y, mylist, k) return y
def ifftshift(x, axes=None): """ The inverse of `fftshift`. Although identical for even-length `x`, the functions differ by one sample for odd-length `x`. Parameters ---------- x : array_like Input array. axes : int or shape tuple, optional Axes over which to calculate. Defaults to None, which shifts all axes. Returns ------- y : ndarray The shifted array. See Also -------- fftshift : Shift zero-frequency component to the center of the spectrum. Examples -------- >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3) >>> freqs array([[ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.]]) >>> np.fft.ifftshift(np.fft.fftshift(freqs)) array([[ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.]]) """ tmp = asarray(x) ndim = len(tmp.shape) if axes is None: axes = list(range(ndim)) elif isinstance(axes, integer_types): axes = (axes,) y = tmp for k in axes: n = tmp.shape[k] p2 = n-(n+1)//2 mylist = concatenate((arange(p2, n), arange(p2))) y = take(y, mylist, k) return y
def ifftshift(x, axes=None): """ The inverse of fftshift. Parameters ---------- x : array_like Input array. axes : int or shape tuple, optional Axes over which to calculate. Defaults to None, which shifts all axes. Returns ------- y : ndarray The shifted array. See Also -------- fftshift : Shift zero-frequency component to the center of the spectrum. Examples -------- >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3) >>> freqs array([[ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.]]) >>> np.fft.ifftshift(np.fft.fftshift(freqs)) array([[ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.]]) """ tmp = asarray(x) ndim = len(tmp.shape) if axes is None: axes = range(ndim) y = tmp for k in axes: n = tmp.shape[k] p2 = n - (n + 1) / 2 mylist = concatenate((arange(p2, n), arange(p2))) y = take(y, mylist, k) return y
def ifftshift(x, axes=None): """ The inverse of fftshift. Parameters ---------- x : array_like Input array. axes : int or shape tuple, optional Axes over which to calculate. Defaults to None, which shifts all axes. Returns ------- y : ndarray The shifted array. See Also -------- fftshift : Shift zero-frequency component to the center of the spectrum. Examples -------- >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3) >>> freqs array([[ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.]]) >>> np.fft.ifftshift(np.fft.fftshift(freqs)) array([[ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.]]) """ tmp = asarray(x) ndim = len(tmp.shape) if axes is None: axes = range(ndim) y = tmp for k in axes: n = tmp.shape[k] p2 = n - (n + 1) / 2 mylist = concatenate((arange(p2, n), arange(p2))) y = take(y, mylist, k) return y
def ifftpad(a, n, scale=True): """ Pad the spectrum at high frequencies. The padding done by the `ifft` function appends zeros to the end of the spectrum which shifts the frequencies and can make the resulting signal differ from the non-padded version. This function pads the spectrum by putting the zeros in the middle where the highest frequencies are. Taking the `ifft` of this padded version result in a signal that is an interpolated version of the unpadded signal, which is what is expected. Parameters ---------- a : array_like Input array, can be complex. n : int Length of the padded spectrum. `n` should be larger than the length of the input. scale : bool, optional Whether to scale the spectrum or not. The `ifft` function divides by the input length which will be the incorrect length for a padded spectrum. Setting this parameter will pre-scale the spectrum so that dividing by the padded length will be correct. Returns ------- out : ndarray The spectrum padded to length `n`. Possibly scaled as well. Examples -------- >>> spectrum = np.array([0, 1, 2, -3, -2, -1]) >>> np.fft.ifftpad(spectrum, 10, scale=False) array([ 0., 1., 2., 0., 0., 0., 0., -3., -2., -1.]) """ spectrum = concatenate((a[:len(a) // 2], zeros(n - len(a)), a[len(a) // 2:])) if scale: spectrum *= (n / len(a)) return spectrum
def fftshift(x,axes=None): """ fftshift(x, axes=None) -> y Shift zero-frequency component to center of spectrum. This function swaps half-spaces for all axes listed (defaults to all). Notes: If len(x) is even then the Nyquist component is y[0]. """ tmp = asarray(x) ndim = len(tmp.shape) if axes is None: axes = range(ndim) y = tmp for k in axes: n = tmp.shape[k] p2 = (n+1)/2 mylist = concatenate((arange(p2,n),arange(p2))) y = take(y,mylist,k) return y
def fftshift(x,axes=None): """ fftshift(x, axes=None) -> y Shift zero-frequency component to center of spectrum. This function swaps half-spaces for all axes listed (defaults to all). Notes: If len(x) is even then the Nyquist component is y[0]. """ tmp = asarray(x) ndim = len(tmp.shape) if axes is None: axes = range(ndim) y = tmp for k in axes: n = tmp.shape[k] p2 = (n+1)/2 mylist = concatenate((arange(p2,n),arange(p2))) y = take(y,mylist,k) return y
tdeff = dat.readprocpar("TDeff", status=False) // 2 # reverse time, conjugate FID spect = np.conj(spect[::-1]) #print(spect.shape) # and truncate to TDeff if (spect.size > tdeff) and (tdeff > 0): spect = spect[0:tdeff] # extend array to SI size (from proc file, this is zero filling) (tdc, ) = spect.shape si = dat.readprocpar("SI", status=False, dimension=1) if (si < tdc): tdc = si spect = spect[0:si] spect = np.concatenate((spect, np.zeros(si - tdc))) #print(spect.shape) dat.writespect1dri(spect.real, spect.imag) # The FID stored will be considered as unprocessed time domain # digital filter has not been removed dat.writeprocpar("PKNL", "yes", False) # set all optionnal status processing parameters to 0 (default) ProcOptions = { "WDW": [["LB", 0], ["GB", 0], ["SSB", 0], ["TM1", 0], ["TM2", 0]], "PH_mod": [["PHC0", 0], ["PHC1", 0]], "BC_mod": [["BCFW", 0], ["COROFFS", 0]], "ME_mod": [["NCOEF", 0], ["LPBIN", 0], ["TDoff", 0]], "FT_mod": [["FTSIZE", 0], ["FCOR", 0], ["STSR", 0], ["STSI", 0], ["REVERSE", False]],
def test_concatenate(): # Test concatenate function # No arrays raise ValueError assert_raises(ValueError, concatenate, ()) # Scalars cannot be concatenated assert_raises(ValueError, concatenate, (0, )) assert_raises(ValueError, concatenate, (array(0), )) # One sequence returns unmodified (but as array) r4 = list(range(4)) assert_array_equal(concatenate((r4, )), r4) # Any sequence assert_array_equal(concatenate((tuple(r4), )), r4) assert_array_equal(concatenate((array(r4), )), r4) # 1D default concatenation r3 = list(range(3)) assert_array_equal(concatenate((r4, r3)), r4 + r3) # Mixed sequence types assert_array_equal(concatenate((tuple(r4), r3)), r4 + r3) assert_array_equal(concatenate((array(r4), r3)), r4 + r3) # Explicit axis specification assert_array_equal(concatenate((r4, r3), 0), r4 + r3) # Including negative assert_array_equal(concatenate((r4, r3), -1), r4 + r3) # 2D a23 = array([[10, 11, 12], [13, 14, 15]]) a13 = array([[0, 1, 2]]) res = array([[10, 11, 12], [13, 14, 15], [0, 1, 2]]) assert_array_equal(concatenate((a23, a13)), res) assert_array_equal(concatenate((a23, a13), 0), res) assert_array_equal(concatenate((a23.T, a13.T), 1), res.T) assert_array_equal(concatenate((a23.T, a13.T), -1), res.T) # Arrays much match shape assert_raises(ValueError, concatenate, (a23.T, a13.T), 0) # 3D res = arange(2 * 3 * 7).reshape((2, 3, 7)) a0 = res[..., :4] a1 = res[..., 4:6] a2 = res[..., 6:] assert_array_equal(concatenate((a0, a1, a2), 2), res) assert_array_equal(concatenate((a0, a1, a2), -1), res) assert_array_equal(concatenate((a0.T, a1.T, a2.T), 0), res.T)
Return the translation vector """ if y==None and z==None: x=mat(x) try: if isho5+6nbmog(x): return x[0:3,3].reshape(3,1) else: return concatenate((concatenate((eye(3),x.reshape(3,1)),1),mat([0,0,0,1]))) except AttributeError: n=len(x) r = [[],[],[]] for i in range(n): r = concatenate((r,x[i][0:3,3]),1) return r elif y!=None and z!=None: return concatenate((concatenate((eye(3),mat([x,y,z]).T),1),mat([0,0,0,1]))) def r2t(R): """ Convert a 3x3 orthonormal rotation matrix to a 4x4 homogeneous transformation:: T = | R 0 | | 0 1 | @type R: 3x3 orthonormal rotation matrix @param R: the rotation matrix to convert @rtype: 4x4 homogeneous matrix @return: homogeneous equivalent
def test_concatenate(): # Test concatenate function # No arrays raise ValueError assert_raises(ValueError, concatenate, ()) # Scalars cannot be concatenated assert_raises(ValueError, concatenate, (0,)) assert_raises(ValueError, concatenate, (array(0),)) # One sequence returns unmodified (but as array) r4 = list(range(4)) assert_array_equal(concatenate((r4,)), r4) # Any sequence assert_array_equal(concatenate((tuple(r4),)), r4) assert_array_equal(concatenate((array(r4),)), r4) # 1D default concatenation r3 = list(range(3)) assert_array_equal(concatenate((r4, r3)), r4 + r3) # Mixed sequence types assert_array_equal(concatenate((tuple(r4), r3)), r4 + r3) assert_array_equal(concatenate((array(r4), r3)), r4 + r3) # Explicit axis specification assert_array_equal(concatenate((r4, r3), 0), r4 + r3) # Including negative assert_array_equal(concatenate((r4, r3), -1), r4 + r3) # 2D a23 = array([[10, 11, 12], [13, 14, 15]]) a13 = array([[0, 1, 2]]) res = array([[10, 11, 12], [13, 14, 15], [0, 1, 2]]) assert_array_equal(concatenate((a23, a13)), res) assert_array_equal(concatenate((a23, a13), 0), res) assert_array_equal(concatenate((a23.T, a13.T), 1), res.T) assert_array_equal(concatenate((a23.T, a13.T), -1), res.T) # Arrays much match shape assert_raises(ValueError, concatenate, (a23.T, a13.T), 0) # 3D res = arange(2 * 3 * 7).reshape((2, 3, 7)) a0 = res[..., :4] a1 = res[..., 4:6] a2 = res[..., 6:] assert_array_equal(concatenate((a0, a1, a2), 2), res) assert_array_equal(concatenate((a0, a1, a2), -1), res) assert_array_equal(concatenate((a0.T, a1.T, a2.T), 0), res.T)