Merge "Moving bruker opus reader to python 3"
This commit is contained in:
commit
1731188222
|
@ -1,2 +1,2 @@
|
||||||
*.pyc
|
*.pyc
|
||||||
*.csv
|
*.csv
|
||||||
|
|
|
@ -44,8 +44,8 @@ class OpusData(object):
|
||||||
csv_reader = csv.reader(csv_file)
|
csv_reader = csv.reader(csv_file)
|
||||||
x_r = np.array(map(lambda x: float(x[1:]), next(csv_reader)[2:]))
|
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:]))
|
y_r = np.array(map(lambda x: float(x), next(csv_reader)[2:]))
|
||||||
dist = linalg.norm(y_r-self.interpolated_data[1], ord=1)
|
dist = linalg.norm(y_r - self.interpolated_data[1], ord=1)
|
||||||
print "L1 distance between rbart and python versions:", dist
|
print("L1 distance between rbart and python versions:", dist)
|
||||||
plt.plot(x_r, y_r, 'bo')
|
plt.plot(x_r, y_r, 'bo')
|
||||||
plt.plot(self.interpolated_data[0],
|
plt.plot(self.interpolated_data[0],
|
||||||
self.interpolated_data[1],
|
self.interpolated_data[1],
|
||||||
|
@ -56,7 +56,7 @@ class OpusData(object):
|
||||||
wunit = (conf.wave_start - conf.wave_end) / (float(conf.iwsize - 1))
|
wunit = (conf.wave_start - conf.wave_end) / (float(conf.iwsize - 1))
|
||||||
a = conf.wave_start + wunit
|
a = conf.wave_start + wunit
|
||||||
iwavenumber = [0] * conf.iwsize
|
iwavenumber = [0] * conf.iwsize
|
||||||
for i in xrange(len(iwavenumber)):
|
for i in range(len(iwavenumber)):
|
||||||
a -= wunit
|
a -= wunit
|
||||||
iwavenumber[i] = a
|
iwavenumber[i] = a
|
||||||
return iwavenumber
|
return iwavenumber
|
||||||
|
|
|
@ -18,11 +18,11 @@ def opus_reader(filepath):
|
||||||
# Choose best ab spectra
|
# Choose best ab spectra
|
||||||
ab_spectra, ab_wavenumbers = choose_ab(fxv_spc, spc, wavenumbers)
|
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)
|
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):
|
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:
|
if np.average(spc[x]) > 0.25:
|
||||||
ab.append(x)
|
ab.append(x)
|
||||||
if len(ab) > 1:
|
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))
|
max_avg_index = spc_avg.index(max(spc_avg))
|
||||||
ab_p = ab[max_avg_index]
|
ab_p = ab[max_avg_index]
|
||||||
elif len(ab) == 1:
|
elif len(ab) == 1:
|
||||||
|
@ -51,19 +51,19 @@ def choose_ab(fxv_spc, spc, wavenumbers):
|
||||||
|
|
||||||
|
|
||||||
def keyword_positions(buff):
|
def keyword_positions(buff):
|
||||||
end = np.array(list(find_all("END", buff))) + 12
|
end = np.array(list(find_all(b'END', buff))) + 12
|
||||||
npt_all = np.array(list(find_all("NPT", buff))) + 8
|
npt_all = np.array(list(find_all(b'NPT', buff))) + 8
|
||||||
fxv_all = np.array(list(find_all("FXV", buff))) + 8
|
fxv_all = np.array(list(find_all(b'FXV', buff))) + 8
|
||||||
lxv_all = np.array(list(find_all("LXV", buff))) + 8
|
lxv_all = np.array(list(find_all(b'LXV', buff))) + 8
|
||||||
return end, npt_all, fxv_all, lxv_all
|
return end, npt_all, fxv_all, lxv_all
|
||||||
|
|
||||||
|
|
||||||
def filter_unpaired(fxv_all, lxv_all):
|
def filter_unpaired(fxv_all, lxv_all):
|
||||||
if len(fxv_all) != len(lxv_all):
|
if len(fxv_all) != len(lxv_all):
|
||||||
prod = product(fxv_all, lxv_all)
|
prod = product(fxv_all, lxv_all)
|
||||||
correct_adressess = zip(*filter(lambda d: (d[1] - d[0]) == 16, prod))
|
corr_adr = list(zip(*filter(lambda d: (d[1] - d[0]) == 16, prod)))
|
||||||
fxv_all = np.array(correct_adressess[0])
|
fxv_all = np.array(corr_adr[0])
|
||||||
lxv_all = np.array(correct_adressess[1])
|
lxv_all = np.array(corr_adr[1])
|
||||||
return fxv_all, lxv_all
|
return fxv_all, lxv_all
|
||||||
|
|
||||||
|
|
||||||
|
@ -72,15 +72,15 @@ def read_all_spectras(buff):
|
||||||
fxv_all, lxv_all = filter_unpaired(fxv_all, lxv_all)
|
fxv_all, lxv_all = filter_unpaired(fxv_all, lxv_all)
|
||||||
|
|
||||||
# Number of wavepoints
|
# Number of wavepoints
|
||||||
npt = [unpack_from("<i", buff, adr)[0] for adr in npt_all]
|
npt = [unpack_from('<i', buff, adr)[0] for adr in npt_all]
|
||||||
# "end_spc is vector of offsets where spectra start"
|
# 'end_spc is vector of offsets where spectra start'
|
||||||
end_spc = end[np.where(np.diff(end) > 4 * min(npt))]
|
end_spc = end[np.where(np.diff(end) > 4 * min(npt))]
|
||||||
spc_param_list = {'npt': npt_all, 'fxv': fxv_all, 'lxv': lxv_all}
|
spc_param_list = {'npt': npt_all, 'fxv': fxv_all, 'lxv': lxv_all}
|
||||||
|
|
||||||
# Filtering some corrupted series
|
# Filtering some corrupted series
|
||||||
param_spc = filter_spc_params(end_spc, spc_param_list, npt_all)
|
param_spc = filter_spc_params(end_spc, spc_param_list, npt_all)
|
||||||
# Number of points in correct spectras
|
# 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)
|
npt_spc = np.array(npt_spc)
|
||||||
|
|
||||||
mask = npt_spc > 0
|
mask = npt_spc > 0
|
||||||
|
@ -89,14 +89,14 @@ def read_all_spectras(buff):
|
||||||
npt_spc = npt_spc[mask]
|
npt_spc = npt_spc[mask]
|
||||||
|
|
||||||
def read_spec(x):
|
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):
|
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))
|
spc = list(map(read_spec, zip(param_spc['end'], npt_spc)))
|
||||||
fxv_spc = np.array(map(read_waves, param_spc["fxv"]))
|
fxv_spc = np.array([read_waves(x) for x in param_spc['fxv']])
|
||||||
lxv_spc = map(lambda x: unpack_from("<2d", buff, x)[0], param_spc["lxv"])
|
lxv_spc = [unpack_from('<2d', buff, x)[0] for x in param_spc['lxv']]
|
||||||
wavenumbers = generate_wavelengths(lxv_spc, fxv_spc, npt_spc)
|
wavenumbers = generate_wavelengths(lxv_spc, fxv_spc, npt_spc)
|
||||||
|
|
||||||
return fxv_spc, spc, wavenumbers
|
return fxv_spc, spc, wavenumbers
|
||||||
|
@ -113,17 +113,17 @@ def generate_wavelengths(lxv_spc, fxv_spc, npt_spc):
|
||||||
|
|
||||||
def get_metadata(buff):
|
def get_metadata(buff):
|
||||||
# Getting source of instruments
|
# Getting source of instruments
|
||||||
all_ins = tuple(find_all('INS', buff))
|
all_ins = tuple(find_all(b'INS', buff))
|
||||||
inst = unpack_from("<3s", buff, all_ins[-1] + 8)[0]
|
inst = unpack_from('<3s', buff, all_ins[-1] + 8)[0]
|
||||||
# Getting source of infrared <NIR/MIR>
|
# Getting source of infrared <NIR/MIR>
|
||||||
all_src = tuple(find_all('SRC', buff))
|
all_src = tuple(find_all(b'SRC', buff))
|
||||||
src = unpack_from("<3s", buff, all_src[-1] + 5)[0]
|
src = unpack_from('<3s', buff, all_src[-1] + 5)[0]
|
||||||
|
|
||||||
dat = buff.find('DAT') + 8
|
dat = buff.find(b'DAT') + 8
|
||||||
scandate = unpack_from("10s", buff, dat)[0]
|
scandate = unpack_from('10s', buff, dat)[0]
|
||||||
|
|
||||||
snm = buff.find('SNM') + 8
|
snm = buff.find(b'SNM') + 8
|
||||||
snm_lab_material = unpack_from("22s", buff, snm)[0]
|
snm_lab_material = unpack_from('22s', buff, snm)[0]
|
||||||
|
|
||||||
meta = {'ins': inst,
|
meta = {'ins': inst,
|
||||||
'src': src,
|
'src': src,
|
||||||
|
|
|
@ -19,7 +19,7 @@ def main(path_to_opus_files):
|
||||||
od.plot_raw()
|
od.plot_raw()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
i += 1
|
i += 1
|
||||||
print i, ":", name, str(e)
|
print(i, ":", name, str(e))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue