def __init__(self, N, dims=None, dir=0, sampling=1., device='cpu', togpu=(False, False), tocpu=(False, False), dtype=torch.float32): # convert dtype to torch.dtype dtype = torchtype_from_numpytype(dtype) h = torch.torch.tensor([0.5, 0, -0.5], dtype=dtype).to(device) / sampling self.device = device self.togpu = togpu self.tocpu = tocpu self.shape = (N, N) self.dtype = dtype self.explicit = False self.Op = Convolve1D(N, h, offset=1, dims=dims, dir=dir, zero_edges=True, device=device, togpu=togpu, tocpu=tocpu, dtype=self.dtype)
def __init__(self, A, dims=None, device='cpu', togpu=(False, False), tocpu=(False, False), dtype=torch.float32): # convert A to torch tensor if provided as numpy array numpy if not isinstance(A, (torch.Tensor, ComplexTensor)): dtype = numpytype_from_torchtype(dtype) self.A = \ torch.from_numpy(A.astype(numpytype_from_torchtype(dtype))).to(device) self.complex = True if np.iscomplexobj(A) else False else: self.complex = True if isinstance(A, ComplexTensor) else False self.A = A if dims is None: self.reshape = False self.shape = A.shape else: if isinstance(dims, int): dims = (dims, ) self.reshape = True self.dims = np.array(dims, dtype=np.int) self.shape = (A.shape[0]*np.prod(self.dims), A.shape[1]*np.prod(self.dims)) self.newshape = \ (tuple(np.insert([np.prod(self.dims)], 0, self.A.shape[1])), tuple(np.insert([np.prod(self.dims)], 0, self.A.shape[0]))) self.complex = True if isinstance(A, ComplexTensor) else False if self.complex: self.Ac = conj(A).t() self.device = device self.togpu = togpu self.tocpu = tocpu self.dtype = torchtype_from_numpytype(dtype) self.explicit = True self.Op = None
def __init__(self, diag, dims=None, dir=0, device='cpu', togpu=(False, False), tocpu=(False, False), dtype=torch.float32): if not isinstance(diag, (torch.Tensor, ComplexTensor)): self.complex = True if np.iscomplexobj(diag) else False self.diag = \ complextorch_fromnumpy(diag.flatten()) if self.complex \ else torch.from_numpy(diag.flatten()) else: self.complex = True if isinstance(diag, ComplexTensor) else False self.diag = flatten(diag) if self.complex else diag.flatten() if dims is None: self.shape = (len(self.diag), len(self.diag)) self.dims = None self.reshape = False else: diagdims = [1] * len(dims) diagdims[dir] = dims[dir] self.diag = reshape(self.diag, diagdims) if self.complex \ else self.diag.reshape(diagdims) self.shape = (np.prod(dims), np.prod(dims)) self.dims = dims self.reshape = True self.device = device self.togpu = togpu self.tocpu = tocpu self.dtype = torchtype_from_numpytype(dtype) self.explicit = False self.Op = None
def __init__(self, M, iava, dims=None, dir=0, inplace=True, device='cpu', togpu=(False, False), tocpu=(False, False), dtype=torch.float32): self.M = M self.dir = dir self.iava = iava if dims is None: self.N = len(iava) self.dims = (self.M, ) self.reshape = False else: if np.prod(dims) != self.M: raise ValueError('product of dims must equal M!') else: self.dims = dims # model dimensions self.dimsd = list(dims) # data dimensions self.dimsd[self.dir] = len(iava) self.iavareshape = [1] * self.dir + [len(self.iava)] + \ [1] * (len(self.dims) - self.dir - 1) self.N = np.prod(self.dimsd) self.reshape = True self.inplace = inplace self.shape = (self.N, self.M) self.device = device self.togpu = togpu self.tocpu = tocpu self.dtype = torchtype_from_numpytype(dtype) self.explicit = True self.Op = None
def test_typeconversion(): """Verify numpy to torch (and viceversa) type conversions """ numpytypes = [np.float32, np.float64, np.int16, np.int32] torchtypes = [torch.float32, torch.float64, torch.int16, torch.int32] for numpytype, torchtype in zip(numpytypes, torchtypes): torchtype_check = torchtype_from_numpytype(numpytype) numpytype_check = numpytype_from_torchtype(torchtype) assert numpytype_check == numpytype assert torchtype_check == torchtype
def __init__(self, N, h, offset=0, dims=None, dir=0, zero_edges=False, device='cpu', togpu=(False, False), tocpu=(False, False), dtype=torch.float32): # convert dtype to torch.dtype if not isinstance(dtype, torch.dtype): dtype = torchtype_from_numpytype(dtype) # convert h to torch if numpy if not isinstance(h, torch.Tensor): h = torch.from_numpy(h).to(device) self.nh = h.size()[0] self.h = h.reshape(1, 1, self.nh) self.offset = 2*(self.nh // 2 - int(offset)) if self.offset != 0: self.h = pad(self.h, (self.offset if self.offset > 0 else 0, -self.offset if self.offset < 0 else 0), mode='constant') self.padding = int(self.nh // 2 + np.abs(self.offset) // 2) self.dir = dir if dims is None: self.dims = (N, ) self.reshape = False elif len(dims) == 1: self.dims = dims self.reshape = False else: if np.prod(dims) != N: raise ValueError('product of dims must equal N!') else: self.dims = tuple(dims) self.otherdims = list(dims) self.otherdims.pop(self.dir) self.otherdims_prod = np.prod(self.dims) // self.dims[self.dir] self.dims_permute = list(self.dims) self.dims_permute[self.dir], self.dims_permute[-1] = \ self.dims_permute[-1], self.dims_permute[self.dir] self.dims_permute = tuple(self.dims_permute) self.permute = np.arange(0, len(self.dims)) self.permute[self.dir], self.permute[-1] = \ self.permute[-1], self.permute[self.dir] self.permute = tuple(self.permute) self.reshape = True self.shape = (np.prod(self.dims), np.prod(self.dims)) self.zero_edges = zero_edges self.device = device self.togpu = togpu self.tocpu = tocpu self.dtype = dtype self.explicit = False self.Op = None
def test_Identity_noinplace(par): """Dot-test, forward and adjoint for Identity operator (not in place) """ print('complex', True if par['imag'] == 1j else False) Iop = Identity(par['ny'], par['nx'], complex=True if par['imag'] == 1j else False, dtype=torchtype_from_numpytype(par['dtype']), inplace=False) assert dottest(Iop, par['ny'], par['nx'], complexflag=0 if par['imag'] == 0 else 3) x = np.ones(par['nx'], dtype='float32') + \ par['imag'] * np.ones(par['nx'], dtype='float32') if par['imag'] == 0: x = torch.from_numpy(x).to(dev) else: x = complextorch_fromnumpy(x).to(dev) y = Iop * x x1 = Iop.H * y if par['imag'] == 0: x = x.cpu().numpy() y = y.cpu().numpy() x1 = x1.cpu().numpy() else: x = complexnumpy_fromtorch(x) y = complexnumpy_fromtorch(y) x1 = complexnumpy_fromtorch(x1) assert_array_almost_equal(x[:min(par['ny'], par['nx'])], y[:min(par['ny'], par['nx'])], decimal=4) assert_array_almost_equal(x[:min(par['ny'], par['nx'])], x1[:min(par['ny'], par['nx'])], decimal=4) # change value in x and check it doesn't change in y x[0] = 10 assert x[0] != y[0]
def test_Identity_inplace(par): """Dot-test, forward and adjoint for Identity operator """ Iop = Identity(par['ny'], par['nx'], complex=True if par['imag'] == 1j else False, dtype=torchtype_from_numpytype(par['dtype']), inplace=True) assert dottest(Iop, par['ny'], par['nx'], complexflag=0 if par['imag'] == 0 else 3) x = np.ones(par['nx'], dtype='float32') + \ par['imag'] * np.ones(par['nx'], dtype='float32') if par['imag'] == 0: x = torch.from_numpy(x).to(dev) else: x = complextorch_fromnumpy(x).to(dev) y = Iop * x x1 = Iop.H * y if par['imag'] == 0: x = x.cpu().numpy() y = y.cpu().numpy() x1 = x1.cpu().numpy() else: x = complexnumpy_fromtorch(x) y = complexnumpy_fromtorch(y) x1 = complexnumpy_fromtorch(x1) assert_array_almost_equal(x[:min(par['ny'], par['nx'])], y[:min(par['ny'], par['nx'])], decimal=4) assert_array_almost_equal(x[:min(par['ny'], par['nx'])], x1[:min(par['ny'], par['nx'])], decimal=4)