From f69130140976f338fa6f5757b3f90111d68adefa Mon Sep 17 00:00:00 2001 From: f-idiris Date: Tue, 26 Sep 2023 13:59:06 +0200 Subject: [PATCH] update mapping --- chem_spectra/lib/converter/jcamp/base.py | 36 ++++++++++---- .../lib/converter/jcamp/data_type.json | 40 ++++++--------- chem_spectra/lib/converter/jcamp/ni.py | 49 ++++++++++--------- 3 files changed, 68 insertions(+), 57 deletions(-) diff --git a/chem_spectra/lib/converter/jcamp/base.py b/chem_spectra/lib/converter/jcamp/base.py index fdaea8a3..cea573a3 100644 --- a/chem_spectra/lib/converter/jcamp/base.py +++ b/chem_spectra/lib/converter/jcamp/base.py @@ -2,6 +2,9 @@ import json from chem_spectra.lib.converter.share import parse_params, parse_solvent +import os + +data_type_json = os.path.join(os.path.dirname(__file__), 'data_type.json') class JcampBaseConverter: @@ -44,21 +47,35 @@ def __read(self, path): def __set_datatype(self): dts = self.datatypes - - with open('data_type.json', 'r') as mapping_file: + dt_dict = { + 'NMR': 'NMR SPECTRUM', + 'INFRARED': 'INFRARED SPECTRUM', + 'RAMAN': 'RAMAN SPECTRUM', + 'MS': 'MASS SPECTRUM', + 'HPLC UVVIS': 'HPLC UV/VIS SPECTRUM', + 'UVVIS': 'UV/VIS SPECTRUM', + } + + with open(data_type_json, 'r') as mapping_file: data_type_mappings = json.load(mapping_file)["datatypes"] - for dt in dts: - if dt in data_type_mappings: - return data_type_mappings[dt] + for key, values in data_type_mappings.items(): + for dt in dts: + if dt in values and key in dt_dict: + return dt_dict[key] + elif dt in values and not key in dt_dict: + return key return '' def __typ(self): dt = self.datatype - with open('data_type.json', 'r') as mapping_file: + with open(data_type_json, 'r') as mapping_file: data_type_mappings = json.load(mapping_file)["datatypes"] - return data_type_mappings.get(dt, '') + for key, values in data_type_mappings.items(): + if dt in values: + return key + return '' def __set_dataclass(self): data_class = self.dataclasses @@ -79,10 +96,9 @@ def __is_em_wave(self): return self.typ in ['INFRARED', 'RAMAN', 'UVVIS'] def __non_nmr(self): - with open('data_type.json', 'r') as mapping_file: + with open(data_type_json, 'r') as mapping_file: data_type_mappings = json.load(mapping_file).get("datatypes") - dts = set(data_type_mappings.values()) - dts.discard('NMR') + dts = [dt for dt in data_type_mappings.keys() if dt != 'NMR'] return self.typ in dts def __is_ir(self): diff --git a/chem_spectra/lib/converter/jcamp/data_type.json b/chem_spectra/lib/converter/jcamp/data_type.json index adcfbb46..5c135ad6 100644 --- a/chem_spectra/lib/converter/jcamp/data_type.json +++ b/chem_spectra/lib/converter/jcamp/data_type.json @@ -1,27 +1,19 @@ { - "datatypes": { - "NMR SPECTRUM": "NMR", - "NMRSPECTRUM": "NMR", - "INFRARED SPECTRUM": "INFRARED", - "RAMAN SPECTRUM": "RAMAN", - "MASS SPECTRUM": "MS", - "HPLC UV/VIS SPECTRUM": "HPLC UVVIS", - "HPLC UV-VIS": "HPLC UVVIS", - "UV/VIS SPECTRUM": "UVVIS", - "UV-VIS": "UVVIS", - "ULTRAVIOLET SPECTRUM": "UVVIS", - "THERMOGRAVIMETRIC ANALYSIS": "THERMOGRAVIMETRIC ANALYSIS", - "X-RAY DIFFRACTION": "X-RAY DIFFRACTION", - "CYCLIC VOLTAMMETRY": "CYCLIC VOLTAMMETRY", - "SIZE EXCLUSION CHROMATOGRAPHY": "SIZE EXCLUSION CHROMATOGRAPHY", - "CIRCULAR DICHROISM SPECTROSCOPY": "CIRCULAR DICHROISM SPECTROSCOPY", - "SORPTION-DESORPTION MEASUREMENT": "SORPTION-DESORPTION MEASUREMENT", - "Emissions": "Emissions", - "EMISSIONS": "Emissions", - "FLUORESCENCE SPECTRUM": "Emissions", - "FL SPECTRUM": "Emissions", - "DLS ACF": "DLS ACF", - "DLS INTENSITY": "DLS intensity", - "DLS intensity": "DLS intensity" + "datatypes": { + "NMR": ["NMR SPECTRUM", "NMRSPECTRUM"], + "INFRARED": ["INFRARED SPECTRUM"], + "RAMAN": ["RAMAN SPECTRUM"], + "MS": ["MASS SPECTRUM"], + "HPLC UVVIS": ["HPLC UV/VIS SPECTRUM", "HPLC UV-VIS"], + "UVVIS": ["UV/VIS SPECTRUM", "UV-VIS", "ULTRAVIOLET SPECTRUM"], + "THERMOGRAVIMETRIC ANALYSIS": ["THERMOGRAVIMETRIC ANALYSIS"], + "X-RAY DIFFRACTION": ["X-RAY DIFFRACTION"], + "CYCLIC VOLTAMMETRY": ["CYCLIC VOLTAMMETRY"], + "SIZE EXCLUSION CHROMATOGRAPHY": ["SIZE EXCLUSION CHROMATOGRAPHY"], + "CIRCULAR DICHROISM SPECTROSCOPY": ["CIRCULAR DICHROISM SPECTROSCOPY"], + "SORPTION-DESORPTION MEASUREMENT": ["SORPTION-DESORPTION MEASUREMENT"], + "Emissions": ["Emissions", "EMISSIONS", "FLUORESCENCE SPECTRUM", "FL SPECTRUM"], + "DLS ACF": ["DLS ACF"], + "DLS intensity": ["DLS INTENSITY", "DLS intensity"] } } diff --git a/chem_spectra/lib/converter/jcamp/ni.py b/chem_spectra/lib/converter/jcamp/ni.py index e859c4ff..88c8620a 100644 --- a/chem_spectra/lib/converter/jcamp/ni.py +++ b/chem_spectra/lib/converter/jcamp/ni.py @@ -5,6 +5,7 @@ from chem_spectra.lib.shared.calc import to_float from chem_spectra.lib.converter.jcamp.data_parse import make_ni_data_ys, make_ni_data_xs import json +import os THRESHOLD_IR = 0.93 THRESHOLD_RAMAN = 0.07 @@ -14,7 +15,7 @@ THRESHOLD_TGA = 1.05 THRESHOLD_XRD = 1.00 THRESHOLD_EMISSION = 0.5 - +data_type_json = os.path.join(os.path.dirname(__file__), 'data_type.json') class JcampNIConverter: # nmr & IR def __init__(self, base): @@ -74,31 +75,33 @@ def __init__(self, base): def __thres(self): dt = self.datatype - with open('data_type.json', 'r') as mapping_file: + threshold_values = { + "NMR": THRESHOLD_NMR, + "INFRARED": THRESHOLD_IR, + "RAMAN": THRESHOLD_RAMAN, + "MS": THRESHOLD_MS, + "HPLC UVVIS": THRESHOLD_UVVIS, + "UVVIS": THRESHOLD_UVVIS, + "THERMOGRAVIMETRIC ANALYSIS": THRESHOLD_TGA, + "DLS ACF": THRESHOLD_TGA, + "X-RAY DIFFRACTION": THRESHOLD_XRD, + "CIRCULAR DICHROISM SPECTROSCOPY": THRESHOLD_XRD, + "CYCLIC VOLTAMMETRY": THRESHOLD_XRD, + "SORPTION-DESORPTION MEASUREMENT": THRESHOLD_XRD, + "DLS intensity": THRESHOLD_XRD, + "Emissions": THRESHOLD_EMISSION + } + with open(data_type_json, 'r') as mapping_file: data_type_mappings = json.load(mapping_file)["datatypes"] - - if data_type_mappings.get(dt) == "NMR": - return THRESHOLD_NMR - elif data_type_mappings.get(dt) == "INFRARED": - return THRESHOLD_IR - elif data_type_mappings.get(dt) == "RAMAN": - return THRESHOLD_RAMAN - elif data_type_mappings.get(dt) == "MS": - return THRESHOLD_MS - elif data_type_mappings.get(dt) in ["HPLC UVVIS", 'UVVIS']: - return THRESHOLD_UVVIS - elif data_type_mappings.get(dt) in ['THERMOGRAVIMETRIC ANALYSIS', 'DLS ACF']: - return THRESHOLD_TGA - elif data_type_mappings.get(dt) in ['X-RAY DIFFRACTION', 'CIRCULAR DICHROISM SPECTROSCOPY', 'CYCLIC VOLTAMMETRY', - 'SORPTION-DESORPTION MEASUREMENT', 'DLS intensity']: - return THRESHOLD_XRD - elif data_type_mappings.get(dt) in ['Emissions']: - return THRESHOLD_EMISSION - return 0.5 + key = next((k for k, v in data_type_mappings.items() if dt in v), None) + + return threshold_values.get(key, 0.5) + def __index_target(self): - with open('data_type.json', 'r') as mapping_file: - target_topics = json.load(mapping_file).get("datatypes") + with open(data_type_json, 'r') as mapping_file: + target = json.load(mapping_file).get("datatypes").values() + target_topics = [value for values in target for value in values] for tp in target_topics: if tp in self.datatypes: