From d6557d2cfe2e445f5dd34c1dcc066079049ae8d7 Mon Sep 17 00:00:00 2001 From: jasonych99 Date: Tue, 9 Oct 2018 14:28:44 +0200 Subject: [PATCH 1/8] jcampdx.read retrun all spctra data --- nmrglue/fileio/jcampdx.py | 47 +++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/nmrglue/fileio/jcampdx.py b/nmrglue/fileio/jcampdx.py index 2bc0cb5d..35418050 100644 --- a/nmrglue/fileio/jcampdx.py +++ b/nmrglue/fileio/jcampdx.py @@ -461,7 +461,7 @@ def find_yfactors(dic): return (factor_r, factor_i) -def _getdataarray(dic): +def _getdataarray(dic, show_all_data=False): ''' Main function for data array parsing, input is the raw dictionary from _readrawdic @@ -491,19 +491,23 @@ def _getdataarray(dic): idatalist.append(data) else: rdatalist.append(data) - if len(rdatalist) > 1: - warn("NTUPLES: multiple real arrays, returning first one only") - if len(idatalist) > 1: - warn("NTUPLES: multiple imaginary arrays, \ - returning first one only") - if rdatalist: - if idatalist: - data = [rdatalist[0], idatalist[0]] - else: - data = rdatalist[0] + + if show_all_data: + data = { 'real': rdatalist, 'imaginary': idatalist } else: - if idatalist: - data = [None, idatalist[0]] + if len(rdatalist) > 1: + warn("NTUPLES: multiple real arrays, returning first one only") + if len(idatalist) > 1: + warn("NTUPLES: multiple imaginary arrays, \ + returning first one only") + if rdatalist: + if idatalist: + data = [rdatalist[0], idatalist[0]] + else: + data = rdatalist[0] + else: + if idatalist: + data = [None, idatalist[0]] if data is None: # XYDATA try: @@ -526,6 +530,11 @@ def _getdataarray(dic): yfactor_r, yfactor_i = find_yfactors(dic) if yfactor_r is None or yfactor_r is None: warn("NTUPLES: YFACTORs not applied, parsing failed") + elif show_all_data: + for i, _ in enumerate(data['real']): + data['real'][i] = data['real'][i] * yfactor_r + for i, _ in enumerate(data['imaginary']): + data['imaginary'][i] = data['imaginary'][i] * yfactor_i else: data[0] = data[0] * yfactor_r data[1] = data[1] * yfactor_i @@ -541,7 +550,7 @@ def _getdataarray(dic): return data -def read(filename): +def read(filename, show_all_data=False): """ Read JCAMP-DX file @@ -574,6 +583,16 @@ def read(filename): # and go with first that has proper data: data = None correctdic = None + + # find and parse NMR data array from raw dic + data = _getdataarray(dic, show_all_data) + + # remove data tables from dic + try: + del dic["XYDATA"] + except KeyError: + pass + try: subdiclist = dic["_datatype_NMRSPECTRUM"] for subdic in subdiclist: From d86d06e6cec56d44b73a1b9a44db93e405d5c72f Mon Sep 17 00:00:00 2001 From: jasonych99 Date: Tue, 9 Oct 2018 16:39:36 +0200 Subject: [PATCH 2/8] jcampdx.read has an option for read errors --- nmrglue/fileio/jcampdx.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nmrglue/fileio/jcampdx.py b/nmrglue/fileio/jcampdx.py index 35418050..ae954396 100644 --- a/nmrglue/fileio/jcampdx.py +++ b/nmrglue/fileio/jcampdx.py @@ -37,7 +37,7 @@ def _getkey(keystr): .replace("-", "").replace("_", "").replace("/", "")) -def _readrawdic(filename): +def _readrawdic(filename, read_err=None): ''' Reads JCAMP-DX file to dictionary, from which actual data is parsed later. Dictionary contains each data @@ -46,7 +46,7 @@ def _readrawdic(filename): diclist = [] # for separating multiple data sections (multiple ##END tags) dic = {"_comments": []} # create empty dictionary - filein = open(filename, 'r') + filein = open(filename, 'r', errors=read_err) currentkey = None currentvaluestrings = [] @@ -550,7 +550,7 @@ def _getdataarray(dic, show_all_data=False): return data -def read(filename, show_all_data=False): +def read(filename, show_all_data=False, read_err=None): """ Read JCAMP-DX file @@ -576,7 +576,7 @@ def read(filename, show_all_data=False): # first read everything (including data array) to "raw" dictionary, # in which data values are read as raw strings including whitespace # and newlines - dic = _readrawdic(filename) + dic = _readrawdic(filename, read_err) # select the relevant data section. # first try to parse NMRSPECTRUM sections in order, From c258f7aebe107bab168af7e57d3cf71a455143c9 Mon Sep 17 00:00:00 2001 From: jasonych99 Date: Wed, 27 Mar 2019 12:34:20 +0100 Subject: [PATCH 3/8] read DATA TABLE= (XY..XY) --- nmrglue/fileio/jcampdx.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/nmrglue/fileio/jcampdx.py b/nmrglue/fileio/jcampdx.py index ae954396..11678ce4 100644 --- a/nmrglue/fileio/jcampdx.py +++ b/nmrglue/fileio/jcampdx.py @@ -199,6 +199,8 @@ def _detect_format(dataline): firstvalue_re = re.compile( r"(\s)*([+-]?\d+\.?\d*|[+-]?\.\d+)([eE][+-]?\d+)?(\s)*") + xy_re = re.compile('^[0-9\.]+, [0-9\.]+$') + index = firstvalue_re.match(dataline).end() if index is None: return -1 @@ -213,6 +215,10 @@ def _detect_format(dataline): return 1 if firstchar in _DUP_DIGITS: return 1 + + if re.search(xy_re, dataline): + return 2 + return 0 @@ -395,6 +401,17 @@ def _parse_pseudo(datalines): return data +def _parse_xy_xy(datalines): + pts = [] + for dataline in datalines: + if not dataline: + continue + x, y = dataline.split(', ') + pts.append([float(x), float(y)]) + + return pts + + def _parse_data(datastring): ''' Creates numpy array from datalines @@ -412,6 +429,8 @@ def _parse_data(datastring): data = _parse_pseudo(datalines) elif mode == 0: data = _parse_affn_pac(datalines) + elif mode == 2: + data = _parse_xy_xy(datalines) else: return None if data is None: From f3c74946371436b8a7407261c6cb57e5f360e43c Mon Sep 17 00:00:00 2001 From: JasonYCHuang Date: Wed, 19 Aug 2020 10:49:36 +0200 Subject: [PATCH 4/8] fix comma as decimal separators --- nmrglue/fileio/jcampdx.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nmrglue/fileio/jcampdx.py b/nmrglue/fileio/jcampdx.py index 11678ce4..80084f82 100644 --- a/nmrglue/fileio/jcampdx.py +++ b/nmrglue/fileio/jcampdx.py @@ -416,6 +416,10 @@ def _parse_data(datastring): ''' Creates numpy array from datalines ''' + probe_data = datastring[80:320] + if ',' in probe_data and not('.' in probe_data): # fix comma as decimal points + datastring = datastring.replace(',', '.') + datalines = datastring.split("\n") headerline = datalines[0] From f019bafee60a5ea204ab23a77d06089e79abe7fe Mon Sep 17 00:00:00 2001 From: Lan Le Date: Tue, 10 Aug 2021 16:44:48 +0200 Subject: [PATCH 5/8] fix parsing xy values --- nmrglue/fileio/jcampdx.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/nmrglue/fileio/jcampdx.py b/nmrglue/fileio/jcampdx.py index 80084f82..614fa6e2 100644 --- a/nmrglue/fileio/jcampdx.py +++ b/nmrglue/fileio/jcampdx.py @@ -199,7 +199,7 @@ def _detect_format(dataline): firstvalue_re = re.compile( r"(\s)*([+-]?\d+\.?\d*|[+-]?\.\d+)([eE][+-]?\d+)?(\s)*") - xy_re = re.compile('^[0-9\.]+, [0-9\.]+$') + xy_re = re.compile('^[0-9\.]+, [0-9\.]+') index = firstvalue_re.match(dataline).end() if index is None: @@ -406,10 +406,13 @@ def _parse_xy_xy(datalines): for dataline in datalines: if not dataline: continue - x, y = dataline.split(', ') - pts.append([float(x), float(y)]) + xy_re = re.compile('[^ ][0-9\.]+, [0-9\.]+') + group_data = re.findall(xy_re, dataline) + for data in group_data: + x, y = data.split(', ') + pts.append([float(x), float(y)]) - return pts + return [pts] def _parse_data(datastring): @@ -545,8 +548,16 @@ def _getdataarray(dic, show_all_data=False): except KeyError: warn("XYDATA not found ") - if data is None: - return None + if data is None: # PEAK TABLE + try: + valuelist = dic["PEAKTABLE"] + if len(valuelist) == 1: + data, datatype = _parse_data(valuelist[0]) + else: + warn("Multiple PEAKTABLE arrays in JCAMP-DX file, \ + returning first one only") + except KeyError: + warn("PEAKTABLE not found ") # apply YFACTOR to data if available if is_ntuples: @@ -615,7 +626,7 @@ def read(filename, show_all_data=False, read_err=None): del dic["XYDATA"] except KeyError: pass - + try: subdiclist = dic["_datatype_NMRSPECTRUM"] for subdic in subdiclist: From 7ee8ed9f165ec664713f806a194aeaea7363d17f Mon Sep 17 00:00:00 2001 From: Lan Le Date: Wed, 25 Aug 2021 09:58:51 +0200 Subject: [PATCH 6/8] update parsing xy data --- nmrglue/fileio/jcampdx.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/nmrglue/fileio/jcampdx.py b/nmrglue/fileio/jcampdx.py index 614fa6e2..5abc717d 100644 --- a/nmrglue/fileio/jcampdx.py +++ b/nmrglue/fileio/jcampdx.py @@ -403,16 +403,21 @@ def _parse_pseudo(datalines): def _parse_xy_xy(datalines): pts = [] + len_group_data = 0 for dataline in datalines: if not dataline: continue xy_re = re.compile('[^ ][0-9\.]+, [0-9\.]+') group_data = re.findall(xy_re, dataline) + len_group_data = len(group_data) for data in group_data: x, y = data.split(', ') pts.append([float(x), float(y)]) - return [pts] + if len_group_data > 1: + return [pts] + else: + return pts def _parse_data(datastring): From 9ba9ea92c3737a3a13f7efcfa5edf46bde0fd6de Mon Sep 17 00:00:00 2001 From: Lan Le Date: Wed, 26 Jan 2022 11:38:35 +0100 Subject: [PATCH 7/8] store xydata format before delete it --- nmrglue/fileio/jcampdx.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nmrglue/fileio/jcampdx.py b/nmrglue/fileio/jcampdx.py index 5abc717d..5da1edcb 100644 --- a/nmrglue/fileio/jcampdx.py +++ b/nmrglue/fileio/jcampdx.py @@ -628,6 +628,7 @@ def read(filename, show_all_data=False, read_err=None): # remove data tables from dic try: + dic['XYDATA_OLD'] = dic["XYDATA"] del dic["XYDATA"] except KeyError: pass From 5111113a4a097a47adedd389d629e9ad3375634d Mon Sep 17 00:00:00 2001 From: Lan Le Date: Wed, 25 Oct 2023 09:59:09 +0200 Subject: [PATCH 8/8] refactor: remove ci for python 3.6 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6dbf0178..693c7e64 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.6, 3.7, 3.8] + python-version: [3.7, 3.8] steps: - uses: actions/checkout@v2