def test_point(self): """Single data point """ for kind in ('linear', 'spline'): interp_1d = RegularInterpolator(x=(1.,), y=(2.,), kind=kind) self.assertEqual(interp_1d(1), 2) self.assertEqual(RegularInterpolator.eval(x=1, xp=(1.,), yp=(2.,), kind=kind), 2) self.assertEqual(interp_1d(5), 2) # value outside domain self.assertEqual(RegularInterpolator.eval(x=5, xp=(1.,), yp=(2.,), kind=kind), 2) assert_allclose(interp_1d(x=(1, 1, 1)), (2, 2, 2)) assert_allclose(interp_1d(x=(0, 1, 2)), (2, 2, 2)) # some values outside domain assert_allclose(interp_1d(np.array((1, 1, 1))), (2, 2, 2)) # numpy array assert_allclose(interp_1d(()), ()) # empty table interp_2d = RegularInterpolator(x=(1.,), y=(2.,), z=(3.,), kind=kind) self.assertEqual(interp_2d(1, 2), 3) self.assertEqual(interp_2d(5, 0), 3) # value outside domain assert_allclose(interp_2d((1, 1, 1), (2, 2, 2)), (3, 3, 3)) test_y = RegularInterpolator.eval(x=(1, 1, 1), y=(2, 2, 2), xp=(1.,), yp=(2.,), zp=(3.,), kind=kind) assert_allclose(test_y, (3, 3, 3)) assert_allclose(interp_2d((0, 1, 2), (0, 1, 2)), (3, 3, 3)) # some values outside domain assert_allclose(interp_2d(np.array((1, 1, 1)), np.array((2, 2, 2))), (3, 3, 3)) # numpy array assert_allclose(interp_2d((), ()), ()) # empty table
def test_2d_2xnel(self): """ Should partially fallback to linear interpolation """ for kind in ('linear', 'spline'): interp_2d = RegularInterpolator(x=(1., 3., 10.), y=(-2.0, 2.0), z=((2, 2), (3, 3), (4, 4)), kind=kind) assert_allclose(interp_2d(1, -2), 2) assert_allclose(interp_2d(1, 0), 2) assert_allclose(interp_2d(1, 2), 2) assert_allclose(interp_2d(3, -2), 3) assert_allclose(interp_2d(3, 0), 3) assert_allclose(interp_2d(3, 2), 3) assert_allclose(interp_2d(x=(3, 3, 3), y=(-2, 0, 2)), (3, 3, 3)) assert_allclose(interp_2d(2, -2), 2.5, atol=0.1) assert_allclose(interp_2d(2, 0), 2.5, atol=0.1) assert_allclose(interp_2d(2, 2), 2.5, atol=0.1) assert_allclose(RegularInterpolator.eval(x=2, y=2, xp=(1., 3., 10.), yp=(-2.0, 2.0), zp=((2, 2), (3, 3), (4, 4)), kind=kind), 2.5, atol=0.1) interp_2d = RegularInterpolator(x=(1., 3.), y=(-2.0, 2.0, 10.0), z=((2, 2, 2), (3, 3, 3)), kind=kind) assert_allclose(interp_2d(1, -2), 2) assert_allclose(interp_2d(1, 0), 2) assert_allclose(interp_2d(1, 2), 2) assert_allclose(interp_2d(1, 10), 2) assert_allclose(interp_2d(3, -2), 3) assert_allclose(interp_2d(3, 0), 3) assert_allclose(interp_2d(3, 2), 3) assert_allclose(interp_2d(3, 10), 3) assert_allclose(interp_2d(2, -2), 2.5, atol=0.1) assert_allclose(interp_2d(2, 0), 2.5, atol=0.1) assert_allclose(interp_2d(2, 2), 2.5, atol=0.1) assert_allclose(interp_2d(2, 10), 2.5, atol=0.1)
def test_2d_nxnel(): for kind in ('linear', 'spline'): interp_2d = RegularInterpolator(x=(1., 3., 10.), y=(-2.0, 0.0, 2.0), z=((2, 2, 2), (3, 3, 3), (4, 4, 4)), kind=kind) assert_allclose(interp_2d(1, -2), 2) assert_allclose(interp_2d(1, 0), 2) assert_allclose(interp_2d(1, 2), 2) assert_allclose(interp_2d(3, -2), 3) assert_allclose(interp_2d(3, 0), 3) assert_allclose(interp_2d(3, 2), 3) assert_allclose(interp_2d(x=(3, 3, 3), y=(-2, 0, 2)), (3, 3, 3)) assert_allclose(interp_2d(2, -2), 2.5, atol=0.1) assert_allclose(interp_2d(2, 0), 2.5, atol=0.1) assert_allclose(interp_2d(2, 2), 2.5, atol=0.1) assert_allclose(RegularInterpolator.eval(x=2, y=2, xp=(1., 3., 10.), yp=(-2.0, 2.0), zp=((2, 2), (3, 3), (4, 4)), kind=kind), 2.5, atol=0.1) interp_2d = RegularInterpolator(x=(1., 3.), y=(-2.0, 2.0, 10.0), z=((2, 2, 2), (3, 3, 3)), kind=kind) assert_allclose(interp_2d(1, -2), 2) assert_allclose(interp_2d(1, 0), 2) assert_allclose(interp_2d(1, 2), 2) assert_allclose(interp_2d(1, 10), 2) assert_allclose(interp_2d(3, -2), 3) assert_allclose(interp_2d(3, 0), 3) assert_allclose(interp_2d(3, 2), 3) assert_allclose(interp_2d(3, 10), 3) assert_allclose(interp_2d(2, -2), 2.5, atol=0.1) assert_allclose(interp_2d(2, 0), 2.5, atol=0.1) assert_allclose(interp_2d(2, 2), 2.5, atol=0.1) assert_allclose(interp_2d(2, 10), 2.5, atol=0.1)
def test_1d_2el(self): """ Should fallback to linear interpolation """ for kind in ('linear', 'spline'): interp_1d = RegularInterpolator(x=(1., 3.), y=(-2.0, 2.0), kind=kind) self.assertEqual(interp_1d(1), -2) self.assertEqual(interp_1d(1.25), -1.5) self.assertEqual(interp_1d(1.5), -1) self.assertEqual(interp_1d(2), 0) self.assertEqual(interp_1d(2.25), 0.5) self.assertEqual(interp_1d(2.5), 1) self.assertEqual(interp_1d(2.75), 1.5) self.assertEqual(interp_1d(3), 2) self.assertEqual(interp_1d(5), 2) # value outside domain self.assertEqual(interp_1d(0), -2) # value outside domain assert_allclose(interp_1d((1, 1, 1)), (-2, -2, -2)) assert_allclose(interp_1d((0, 1, 4)), (-2, -2, 2)) # some values outside domain assert_allclose(interp_1d(np.array((1, 1, 1))), (-2, -2, -2)) # numpy array assert_allclose(interp_1d(np.linspace(1, 3, 10)), np.linspace(-2, 2, 10)) assert_allclose(interp_1d(()), ()) # empty table
def get_ddd_grid(self, energy_list, n): """ TODO: documentation """ energy = [] dist = [] data = [] ddd_e = self.ddd.keys() ddd_e = sorted(ddd_e) for e in energy_list: idx = np.where((np.array(ddd_e) >= e))[0][0] - 1 d_lower = self.ddd[ddd_e[idx]] d_upper = self.ddd[ddd_e[idx + 1]] lower_idx = np.where(max(d_lower[1, :]) == d_lower[1, :])[0][0] upper_idx = np.where(max(d_upper[1, :]) == d_upper[1, :])[0][0] offset = 1 / (ddd_e[idx + 1] - ddd_e[idx]) * (e - ddd_e[idx + 1]) x_offset = (d_upper[0, upper_idx] - d_lower[0, lower_idx]) * offset y_offset = 1 + (1 - d_upper[1, upper_idx] / d_lower[1, lower_idx]) * offset depth = d_upper[0, :] + x_offset ddd = d_upper[1, :] * y_offset xi = np.linspace(0, depth[-1], n) spl = RegularInterpolator(x=depth, y=ddd) data.extend(spl(xi)) dist.extend(xi) energy.extend([e] * n) out = [dist, energy, data] return np.reshape(np.transpose(out), (len(energy_list), n, 3))
def __init__(self, gd_filename, _dataset=0, dat_filename=None): """ :params str gd_filename: full path to bevlet file, including file extension. :params str dat_filename: optional full path to output file name. """ if not os.path.isfile(gd_filename): raise IOError("Could not find file " + gd_filename) if _dataset > 2: print("DOS: Error- only 0,1,2 OER set available. Got:", _dataset) from pkg_resources import resource_string model_files = ('OER_furusawa_V79_C12.dat', 'OER_furusawa_HSG_C12.dat', 'OER_barendsen.dat') model_data = resource_string( 'pytrip', os.path.join('data', model_files[_dataset])) lines = model_data.decode('ascii').split('\n') x = np.asarray([float(line.split()[0]) for line in lines if line]) y = np.asarray([float(line.split()[1]) for line in lines if line]) us = RegularInterpolator(x, y, kind='linear') with open(gd_filename, 'r') as gd_file: gd_lines = gd_file.readlines() first = True ignore_rest = False if dat_filename is not None: out_fd = open(dat_filename, 'w') else: out_fd = sys.stdout for line in gd_lines: if not line[0].isdigit(): tmp_string = "#" + line if not first: ignore_rest = True else: first = False if ignore_rest: tmp_string = "#" + line else: let = float(line.split()[7]) oer = us(let) tmp_string = "" for item in line.split(): tmp_string = tmp_string + item + " " tmp_string = tmp_string + str(oer) + "\n" out_fd.write(tmp_string) if dat_filename is not None: out_fd.close()
def test_point(self): """Single data point """ for kind in ('linear', 'spline'): interp_1d = RegularInterpolator(x=(1., ), y=(2., ), kind=kind) self.assertEqual(interp_1d(1), 2) self.assertEqual( RegularInterpolator.eval(x=1, xp=(1., ), yp=(2., ), kind=kind), 2) self.assertEqual(interp_1d(5), 2) # value outside domain self.assertEqual( RegularInterpolator.eval(x=5, xp=(1., ), yp=(2., ), kind=kind), 2) assert_allclose(interp_1d(x=(1, 1, 1)), (2, 2, 2)) assert_allclose(interp_1d(x=(0, 1, 2)), (2, 2, 2)) # some values outside domain assert_allclose(interp_1d(np.array((1, 1, 1))), (2, 2, 2)) # numpy array assert_allclose(interp_1d(()), ()) # empty table interp_2d = RegularInterpolator(x=(1., ), y=(2., ), z=(3., ), kind=kind) self.assertEqual(interp_2d(1, 2), 3) self.assertEqual(interp_2d(5, 0), 3) # value outside domain assert_allclose(interp_2d((1, 1, 1), (2, 2, 2)), (3, 3, 3)) test_y = RegularInterpolator.eval(x=(1, 1, 1), y=(2, 2, 2), xp=(1., ), yp=(2., ), zp=(3., ), kind=kind) assert_allclose(test_y, (3, 3, 3)) assert_allclose(interp_2d((0, 1, 2), (0, 1, 2)), (3, 3, 3)) # some values outside domain assert_allclose(interp_2d(np.array((1, 1, 1)), np.array( (2, 2, 2))), (3, 3, 3)) # numpy array assert_allclose(interp_2d((), ()), ()) # empty table
def import_hlut(self): """ Imports the Hounsfield lookup table and stores it into self.hlut_data object self.hlut_data is trained linear interpolator, it can be later called to get interpolated values """ fp = open(self.hlut_file, "r") lines = fp.read() fp.close() lines = lines.split('\n') x_data = [] y_data = [] for line in lines: a = line.split() if a: x_data.append(float(a[0])) y_data.append(float(a[3])) self.hlut_data = RegularInterpolator(np.array(x_data), np.array(y_data), kind='linear')
def test_2d_nxnel(self): for kind in ('linear', 'spline'): interp_2d = RegularInterpolator(x=(1., 3., 10.), y=(-2.0, 0.0, 2.0), z=((2, 2, 2), (3, 3, 3), (4, 4, 4)), kind=kind) assert_allclose(interp_2d(1, -2), 2) assert_allclose(interp_2d(1, 0), 2) assert_allclose(interp_2d(1, 2), 2) assert_allclose(interp_2d(3, -2), 3) assert_allclose(interp_2d(3, 0), 3) assert_allclose(interp_2d(3, 2), 3) assert_allclose(interp_2d(x=(3, 3, 3), y=(-2, 0, 2)), (3, 3, 3)) assert_allclose(interp_2d(2, -2), 2.5, atol=0.1) assert_allclose(interp_2d(2, 0), 2.5, atol=0.1) assert_allclose(interp_2d(2, 2), 2.5, atol=0.1) assert_allclose(RegularInterpolator.eval(x=2, y=2, xp=(1., 3., 10.), yp=(-2.0, 2.0), zp=((2, 2), (3, 3), (4, 4)), kind=kind), 2.5, atol=0.1) interp_2d = RegularInterpolator(x=(1., 3.), y=(-2.0, 2.0, 10.0), z=((2, 2, 2), (3, 3, 3)), kind=kind) assert_allclose(interp_2d(1, -2), 2) assert_allclose(interp_2d(1, 0), 2) assert_allclose(interp_2d(1, 2), 2) assert_allclose(interp_2d(1, 10), 2) assert_allclose(interp_2d(3, -2), 3) assert_allclose(interp_2d(3, 0), 3) assert_allclose(interp_2d(3, 2), 3) assert_allclose(interp_2d(3, 10), 3) assert_allclose(interp_2d(2, -2), 2.5, atol=0.1) assert_allclose(interp_2d(2, 0), 2.5, atol=0.1) assert_allclose(interp_2d(2, 2), 2.5, atol=0.1) assert_allclose(interp_2d(2, 10), 2.5, atol=0.1)
def test_2d_2x2el(self): """ Should fallback to linear interpolation """ for kind in ('linear', 'spline'): interp_2d = RegularInterpolator(x=(1., 3.), y=(-2.0, 2.0), z=((2, 2), (3, 3)), kind=kind) self.assertEqual(interp_2d(1, -2), 2) self.assertEqual(interp_2d(1, 0), 2) self.assertEqual(interp_2d(1, 2), 2) self.assertEqual(interp_2d(3, -2), 3) self.assertEqual(interp_2d(3, 0), 3) self.assertEqual(interp_2d(3, 2), 3) self.assertEqual(interp_2d(2, -2), 2.5) self.assertEqual(interp_2d(2, 0), 2.5) self.assertEqual(interp_2d(2, 2), 2.5)
def load_ddd(self, directory): """ Loads all .ddd files found in 'directory' :params str directory: directory where the .ddd files are found. """ x_data = [] y_data = [] points = [[], []] ddd = {} max_dist = [] items = glob.glob(directory) for item in items: x_data = [] y_data = [] with open(item, 'r') as f: data = f.read() lines = data.split('\n') for n, line in enumerate(data.split("\n")): if line.find("energy") is not -1: energy = float(line.split()[1]) if line.find('!') is -1 and line.find('#') is -1: break for i in range(n, len(lines)): if len(lines[i]) < 3: continue point = [float(s) for s in lines[i].split()] x_data.append(point[0] * 10) y_data.append(point[1]) ddd[energy] = np.array([x_data, y_data]) max_dist.append([energy, x_data[-1]]) max_dist = np.array(sorted(max_dist, key=lambda x: x[0])) self.max_dist = RegularInterpolator(x=max_dist[:, 0], y=max_dist[:, 1]) ddd_list = [] for key, value in ddd.items(): points[0].extend(value[0]) points[1].extend(len(value[0]) * [key]) ddd_list.extend(value[1]) self.ddd_list = ddd_list self.ddd = ddd self.points = np.transpose(points)
def test_1d_nel(self): logger.info("Testing data sampled from constant function") for kind in ('linear', 'spline'): interp_1d = RegularInterpolator(x=(1., 3., 4., 5., 10.), y=(2.0, 2.0, 2.0, 2.0, 2.0), kind=kind) assert_allclose(interp_1d(np.linspace(-1, 10, 100)), np.ones(100) * 2.0) interp_1d = RegularInterpolator(x=(1., 3., 4., 5.), y=(2.0, 2.0, 2.0, 2.0), kind=kind) assert_allclose(interp_1d(np.linspace(-1, 10, 100)), np.ones(100) * 2.0) interp_1d = RegularInterpolator(x=(1., 3., 4.), y=(2.0, 2.0, 2.0), kind=kind) assert_allclose(interp_1d(np.linspace(-1, 10, 100)), np.ones(100) * 2.0) logger.info("Testing some random data") data_x = (1., 3., 4., 5., 10.) data_y = (0.0, 10.0, 5.0, 0.0, -5.0) interp_1d = RegularInterpolator(x=data_x, y=data_y, kind='linear') assert_allclose(interp_1d(data_x), data_y) self.assertEqual(interp_1d(2), 5) self.assertEqual(interp_1d(2.5), 7.5) self.assertEqual(interp_1d(3.5), 7.5) self.assertEqual(interp_1d(7.5), -2.5) interp_1d = RegularInterpolator(x=data_x, y=data_y, kind='spline') assert_allclose(interp_1d(data_x), data_y, atol=1e-12) self.assertGreater(interp_1d(2), 10) # spline interpolation making strange "hops" self.assertGreater(interp_1d(2.5), 10) assert_allclose(interp_1d(3.5), 7.5, atol=0.4) self.assertLess(interp_1d(7.5), -5) logger.info("Testing data sampled from identity function") data_y = data_x interp_1d = RegularInterpolator(x=data_x, y=data_y, kind='spline') assert_allclose(interp_1d(data_x), data_y, atol=1e-12) test_x = np.linspace(start=data_x[0], stop=data_x[-1], num=100) assert_allclose(interp_1d(test_x), test_x, atol=1e-12)