From a9bcd34bed204ddddff3ec7f815e81da0689a6c1 Mon Sep 17 00:00:00 2001 From: t2 Date: Tue, 10 Oct 2017 16:51:05 +0200 Subject: [PATCH] Moving bruker opus reader to python 3 Change-Id: Icb9c708d3f956f79a706cc32a4144085daa43f5b --- .gitignore | 2 +- brukeropusreader/opus_data.py | 6 ++-- brukeropusreader/opus_reader.py | 52 ++++++++++++++++----------------- example.py | 2 +- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 85cc01c..b26a5b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ *.pyc -*.csv \ No newline at end of file +*.csv diff --git a/brukeropusreader/opus_data.py b/brukeropusreader/opus_data.py index 7c4ab27..37c2040 100644 --- a/brukeropusreader/opus_data.py +++ b/brukeropusreader/opus_data.py @@ -44,8 +44,8 @@ class OpusData(object): csv_reader = csv.reader(csv_file) x_r = np.array(map(lambda x: float(x[1:]), next(csv_reader)[2:])) y_r = np.array(map(lambda x: float(x), next(csv_reader)[2:])) - dist = linalg.norm(y_r-self.interpolated_data[1], ord=1) - print "L1 distance between rbart and python versions:", dist + dist = linalg.norm(y_r - self.interpolated_data[1], ord=1) + print("L1 distance between rbart and python versions:", dist) plt.plot(x_r, y_r, 'bo') plt.plot(self.interpolated_data[0], self.interpolated_data[1], @@ -56,7 +56,7 @@ class OpusData(object): wunit = (conf.wave_start - conf.wave_end) / (float(conf.iwsize - 1)) a = conf.wave_start + wunit iwavenumber = [0] * conf.iwsize - for i in xrange(len(iwavenumber)): + for i in range(len(iwavenumber)): a -= wunit iwavenumber[i] = a return iwavenumber diff --git a/brukeropusreader/opus_reader.py b/brukeropusreader/opus_reader.py index bf09b10..531c6a0 100644 --- a/brukeropusreader/opus_reader.py +++ b/brukeropusreader/opus_reader.py @@ -18,11 +18,11 @@ def opus_reader(filepath): # Choose best ab spectra ab_spectra, ab_wavenumbers = choose_ab(fxv_spc, spc, wavenumbers) - wave_num_abs_pair = reversed(zip(ab_wavenumbers, ab_spectra)) + wave_num_abs_pair = reversed(list(zip(ab_wavenumbers, ab_spectra))) meta = get_meta_data(buff) - return OpusData(zip(*wave_num_abs_pair), meta=meta) + return OpusData(list(zip(*wave_num_abs_pair)), meta=meta) def choose_ab(fxv_spc, spc, wavenumbers): @@ -36,7 +36,7 @@ def choose_ab(fxv_spc, spc, wavenumbers): if np.average(spc[x]) > 0.25: ab.append(x) if len(ab) > 1: - spc_avg = map(lambda x: np.average(spc[x]), ab) + spc_avg = [np.average(spc[x]) for x in ab] max_avg_index = spc_avg.index(max(spc_avg)) ab_p = ab[max_avg_index] elif len(ab) == 1: @@ -50,19 +50,19 @@ def choose_ab(fxv_spc, spc, wavenumbers): def keyword_positions(buff): - end = np.array(list(find_all("END", buff))) + 12 - npt_all = np.array(list(find_all("NPT", buff))) + 8 - fxv_all = np.array(list(find_all("FXV", buff))) + 8 - lxv_all = np.array(list(find_all("LXV", buff))) + 8 + end = np.array(list(find_all(b'END', buff))) + 12 + npt_all = np.array(list(find_all(b'NPT', buff))) + 8 + fxv_all = np.array(list(find_all(b'FXV', buff))) + 8 + lxv_all = np.array(list(find_all(b'LXV', buff))) + 8 return end, npt_all, fxv_all, lxv_all def filter_unpaired(fxv_all, lxv_all): if len(fxv_all) != len(lxv_all): prod = product(fxv_all, lxv_all) - correct_adressess = zip(*filter(lambda d: (d[1] - d[0]) == 16, prod)) - fxv_all = np.array(correct_adressess[0]) - lxv_all = np.array(correct_adressess[1]) + corr_adr = list(zip(*filter(lambda d: (d[1] - d[0]) == 16, prod))) + fxv_all = np.array(corr_adr[0]) + lxv_all = np.array(corr_adr[1]) return fxv_all, lxv_all @@ -71,15 +71,15 @@ def read_all_spectras(buff): fxv_all, lxv_all = filter_unpaired(fxv_all, lxv_all) # Number of wavepoints - npt = [unpack_from(" 4 * min(npt))] spc_param_list = {'npt': npt_all, 'fxv': fxv_all, 'lxv': lxv_all} # Filtering some corrupted series param_spc = filter_spc_params(end_spc, spc_param_list, npt_all) # Number of points in correct spectras - npt_spc = [unpack_from(" 0 @@ -88,14 +88,14 @@ def read_all_spectras(buff): npt_spc = npt_spc[mask] def read_spec(x): - return np.array(unpack_from("<" + str(x[1]) + "f", buff, x[0] - 4)) + return np.array(unpack_from('<' + str(x[1]) + 'f', buff, x[0] - 4)) def read_waves(x): - return unpack_from("<2d", buff, x)[0] + return unpack_from('<2d', buff, x)[0] - spc = map(read_spec, zip(param_spc['end'], npt_spc)) - fxv_spc = np.array(map(read_waves, param_spc["fxv"])) - lxv_spc = map(lambda x: unpack_from("<2d", buff, x)[0], param_spc["lxv"]) + spc = list(map(read_spec, zip(param_spc['end'], npt_spc))) + fxv_spc = np.array([read_waves(x) for x in param_spc['fxv']]) + lxv_spc = [unpack_from('<2d', buff, x)[0] for x in param_spc['lxv']] wavenumbers = generate_wavelengths(lxv_spc, fxv_spc, npt_spc) return fxv_spc, spc, wavenumbers @@ -112,17 +112,17 @@ def generate_wavelengths(lxv_spc, fxv_spc, npt_spc): def get_meta_data(buff): # Getting source of instruments - all_ins = tuple(find_all('INS', buff)) - inst = unpack_from("<3s", buff, all_ins[-1] + 8)[0] + all_ins = tuple(find_all(b'INS', buff)) + inst = unpack_from('<3s', buff, all_ins[-1] + 8)[0] # Getting source of infrared - all_src = tuple(find_all('SRC', buff)) - src = unpack_from("<3s", buff, all_src[-1] + 5)[0] + all_src = tuple(find_all(b'SRC', buff)) + src = unpack_from('<3s', buff, all_src[-1] + 5)[0] - dat = buff.find('DAT') + 8 - scandate = unpack_from("10s", buff, dat)[0] + dat = buff.find(b'DAT') + 8 + scandate = unpack_from('10s', buff, dat)[0] - snm = buff.find('SNM') + 8 - snm_lab_material = unpack_from("22s", buff, snm)[0] + snm = buff.find(b'SNM') + 8 + snm_lab_material = unpack_from('22s', buff, snm)[0] meta = {'ins': inst, 'src': src, diff --git a/example.py b/example.py index 014f3ee..1156638 100644 --- a/example.py +++ b/example.py @@ -19,7 +19,7 @@ def main(path_to_opus_files): od.plot_raw() except Exception as e: i += 1 - print i, ":", name, str(e) + print(i, ":", name, str(e)) if __name__ == '__main__':