Merge "Moving bruker opus reader to python 3"

This commit is contained in:
Tomasz Dyczek 2017-10-11 06:53:15 +00:00 committed by Gerrit Code Review
commit 1731188222
4 changed files with 31 additions and 31 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
*.pyc
*.csv
*.csv

View File

@ -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

View File

@ -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_metadata(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):
@ -37,7 +37,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:
@ -51,19 +51,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
@ -72,15 +72,15 @@ def read_all_spectras(buff):
fxv_all, lxv_all = filter_unpaired(fxv_all, lxv_all)
# Number of wavepoints
npt = [unpack_from("<i", buff, adr)[0] for adr in npt_all]
# "end_spc is vector of offsets where spectra start"
npt = [unpack_from('<i', buff, adr)[0] for adr in npt_all]
# 'end_spc is vector of offsets where spectra start'
end_spc = end[np.where(np.diff(end) > 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("<i", buff, adr)[0] for adr in param_spc["npt"]]
npt_spc = [unpack_from('<i', buff, adr)[0] for adr in param_spc['npt']]
npt_spc = np.array(npt_spc)
mask = npt_spc > 0
@ -89,14 +89,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
@ -113,17 +113,17 @@ def generate_wavelengths(lxv_spc, fxv_spc, npt_spc):
def get_metadata(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 <NIR/MIR>
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,

View File

@ -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__':