From 3bcb947ab559747674c210f849542ea8407df09f Mon Sep 17 00:00:00 2001 From: dvm-shlee Date: Wed, 5 Jul 2023 16:12:12 -0400 Subject: [PATCH 1/5] syntax: is not -> != --- brkraw/lib/backup.py | 8 ++++---- brkraw/lib/errors.py | 14 +++++++------- brkraw/lib/loader.py | 30 +++++++++++++++--------------- brkraw/lib/orient.py | 2 +- brkraw/lib/pvobj.py | 2 +- brkraw/lib/utils.py | 22 ++++++++++------------ brkraw/scripts/brkraw.py | 18 ++++++++++-------- 7 files changed, 48 insertions(+), 48 deletions(-) diff --git a/brkraw/lib/backup.py b/brkraw/lib/backup.py index 214775b..3530a5a 100644 --- a/brkraw/lib/backup.py +++ b/brkraw/lib/backup.py @@ -137,7 +137,7 @@ def set_arc(self, arc_fname, arc_dir, raw_dir): garbage = True crashed = True - if raw_dname is not None: + if raw_dname != None: r = self.get_rpath_obj(raw_dname) else: r = None @@ -265,7 +265,7 @@ def _parse_info(self): # update raw dataset information (raw dataset cache will remain even its removed) print('\nScanning raw dataset cache...') for r in tqdm.tqdm(self.raw_data[:], bar_format=_bar_fmt): - if r.path is not None: + if r.path != None: if not os.path.exists(os.path.join(self._rpath, r.path)): if not r.removed: r.removed = True @@ -300,7 +300,7 @@ def _parse_info(self): def is_same_as_raw(self, filename): arc = BrukerLoader(os.path.join(self._apath, filename)) - if arc.pvobj.path is not None: + if arc.pvobj.path != None: raw_path = os.path.join(self._rpath, arc.pvobj.path) if os.path.exists(raw_path): raw = BrukerLoader(raw_path) @@ -495,7 +495,7 @@ def clean(self): print('\nStart removing {} archived data...'.format(label.upper())) if len(dset.items()): for raw_dname, arcs in dset.items(): - if raw_dname is not None: + if raw_dname != None: raw_path = os.path.join(self._rpath, raw_dname) if os.path.exists(raw_path): r_size, r_unit = get_dirsize(raw_path) diff --git a/brkraw/lib/errors.py b/brkraw/lib/errors.py index 81d3572..d4be753 100644 --- a/brkraw/lib/errors.py +++ b/brkraw/lib/errors.py @@ -22,13 +22,13 @@ def __init__(self, file_name=None, data_type=None): self.file_name = None self.data_type = None - if file_name is not None: + if file_name != None: self.file_name = os.path.basename(file_name) if os.path.isdir(file_name): object_type = 'directory' else: object_type = 'file' - if data_type is not None: + if data_type != None: self.data_type = data_type self.message = "The {} '{}' is not valid {}".format(object_type, self.file_name, @@ -45,7 +45,7 @@ class ArchiveFailedError(Error): file_name = None def __init__(self, file_name=None): - if file_name is not None: + if file_name != None: self.file_name = os.path.basename(file_name) self.message = "The data '{}' is not archived".format( self.file_name) @@ -58,7 +58,7 @@ class RemoveFailedError(Error): file_name = None def __init__(self, file_name=None): - if file_name is not None: + if file_name != None: self.file_name = os.path.basename(file_name) self.message = "The file '{}' is not removed".format( self.file_name) @@ -72,11 +72,11 @@ class RenameFailedError(Error): file2_name = None def __init__(self, file1_name=None, file2_name=None): - if file1_name is not None: + if file1_name != None: self.file1_name = os.path.basename(file1_name) - if file2_name is not None: + if file2_name != None: self.file2_name = os.path.basename(file2_name) - if (self.file1_name is not None) and (self.file2_name is not None): + if (self.file1_name != None) and (self.file2_name != None): self.message = "Rename failed to execute from:'{}' to:'{}'".format(self.file1_name, self.file2_name) else: diff --git a/brkraw/lib/loader.py b/brkraw/lib/loader.py index 961602f..7ae171a 100644 --- a/brkraw/lib/loader.py +++ b/brkraw/lib/loader.py @@ -255,7 +255,7 @@ def swap_slice_axis(group_id_, dataobj_): dataobj_ = np.swapaxes(dataobj_, 2, slice_axis_) return dataobj_ - if fg_info['frame_type'] is not None: + if fg_info['frame_type'] != None: if group_id[0] == 'FG_SLICE': pass @@ -327,9 +327,9 @@ def get_niftiobj(self, scan_id, reco_id, crop=None, slope=False, offset=False): affine = self._get_affine(visu_pars, method) data_slp, data_off = self._get_dataslp(visu_pars) - if isinstance(data_slp, list) and slope is not None: + if isinstance(data_slp, list) and slope != None: slope = True - if isinstance(data_off, list) and offset is not None: + if isinstance(data_off, list) and offset != None: offset = True imgobj = self.get_dataobj(scan_id, reco_id, slope=slope, offset=offset) @@ -359,7 +359,7 @@ def get_niftiobj(self, scan_id, reco_id, crop=None, slope=False, offset=False): f = multiply_all(imgobj_.shape[3:]) # all converted nifti must be 4D imgobj_ = imgobj_.reshape([x, y, z, f]) - if crop is not None: + if crop != None: if crop[0] is None: niiobj_ = Nifti1Image(imgobj_[..., :crop[1]], affine) elif crop[1] is None: @@ -377,7 +377,7 @@ def get_niftiobj(self, scan_id, reco_id, crop=None, slope=False, offset=False): f = multiply_all(imgobj.shape[3:]) # all converted nifti must be 4D imgobj = imgobj.reshape([x, y, z, f]) - if crop is not None: + if crop != None: if crop[0] is None: niiobj = Nifti1Image(imgobj[..., :crop[1]], affine) elif crop[1] is None: @@ -511,7 +511,7 @@ def _parse_json(self, scan_id, reco_id, metadata=None): val = meta_get_value(v, acqp, method, visu_pars) if k in ['PhaseEncodingDirection', 'SliceEncodingDirection']: # Convert the encoding direction meta data into BIDS format - if val is not None: + if val != None: if isinstance(val, int): val = encdir_dic[val] else: @@ -551,7 +551,7 @@ def _parse_json(self, scan_id, reco_id, metadata=None): def save_json(self, scan_id, reco_id, filename, dir='./', metadata=None, condition=None): json_obj = self._parse_json(scan_id, reco_id, metadata) - if condition is not None: + if condition != None: code, idx = condition if code == 'me': # multi-echo if 'EchoTime' in json_obj.keys(): @@ -599,7 +599,7 @@ def get_scan_time(self, visu_pars=None): # date date = dt.datetime.strptime(re.sub(pattern_1, r'\2', subject_date), '%d %b %Y').date() # end time - if visu_pars is not None: + if visu_pars != None: last_scan_time = get_value(visu_pars, 'VisuAcqDate') last_scan_time = dt.time(*map(int, re.sub(pattern_1, r'\1', last_scan_time).split(':'))) acq_time = get_value(visu_pars, 'VisuAcqScanTime') / 1000.0 @@ -616,7 +616,7 @@ def get_scan_time(self, visu_pars=None): date = dt.date(*map(int, re.sub(pattern_2, r'\1', subject_date).split('-'))) # end date - if visu_pars is not None: + if visu_pars != None: scan_time = get_value(visu_pars, 'VisuCreationDate')[0] scan_time = dt.time(*map(int, re.sub(pattern_2, r'\2', scan_time).split(':'))) return dict(date=date, @@ -785,7 +785,7 @@ def _set_nifti_header(self, niiobj, visu_pars, method, slope, offset): else: niiobj.header.set_xyzt_units('mm') if not slope: - if slope is not None: + if slope != None: if isinstance(data_slp, list): raise InvalidApproach('Invalid slope size;' 'The vector type scl_slope cannot be set in nifti header.') @@ -795,7 +795,7 @@ def _set_nifti_header(self, niiobj, visu_pars, method, slope, offset): else: niiobj.header['scl_slope'] = 1 if not offset: - if offset is not None: + if offset != None: if isinstance(data_off, list): raise InvalidApproach('Invalid offset size;' 'The vector type scl_offset cannot be set in nifti header.') @@ -814,7 +814,7 @@ def _get_temp_info(self, visu_pars): total_time = get_value(visu_pars, 'VisuAcqScanTime') fg_info = self._get_frame_group_info(visu_pars) parser = [] - if fg_info['frame_type'] is not None: + if fg_info['frame_type'] != None: for id, fg in enumerate(fg_info['group_id']): if not re.search('slice', fg, re.IGNORECASE): parser.append(fg_info['matrix_shape'][id]) @@ -1004,7 +1004,7 @@ def get_axis_orient(orient_matrix): orient_matrix = get_value(visu_pars, 'VisuCoreOrientation').tolist() slice_info = self._get_slice_info(visu_pars) slice_position = get_value(visu_pars, 'VisuCorePosition') - if self._override_position is not None: # add option to override + if self._override_position != None: # add option to override subj_position = self._override_position else: subj_position = get_value(visu_pars, 'VisuSubjectPosition') @@ -1062,7 +1062,7 @@ def get_axis_orient(orient_matrix): oorder_parser = get_axis_orient(omatrix_parser) vposition_parser = slice_position - if self._override_type is not None: # add option to override + if self._override_type != None: # add option to override subj_type = self._override_type else: subj_type = get_value(visu_pars, 'VisuSubjectType') @@ -1147,7 +1147,7 @@ def _get_matrix_size(self, visu_pars, dataobj=None): if num_temporal_frame > 1: matrix_size.append(num_temporal_frame) - if dataobj is not None: + if dataobj != None: # matrix size inspection dataobj_shape = dataobj.shape[0] if multiply_all(matrix_size) != dataobj_shape: diff --git a/brkraw/lib/orient.py b/brkraw/lib/orient.py index 73fd01f..a68a98a 100644 --- a/brkraw/lib/orient.py +++ b/brkraw/lib/orient.py @@ -146,7 +146,7 @@ def get_origin(slice_position, gradient_orient): max_delta_axis = np.argmax([dx, dy, dz]) rx, ry, rz = [None, None, None] - if gradient_orient is not None: + if gradient_orient != None: zmat = np.zeros(gradient_orient[0].shape) for cid, col in enumerate(gradient_orient[0].T): yid = np.argmax(abs(col)) diff --git a/brkraw/lib/pvobj.py b/brkraw/lib/pvobj.py index f4c4e09..3631345 100644 --- a/brkraw/lib/pvobj.py +++ b/brkraw/lib/pvobj.py @@ -33,7 +33,7 @@ def _reset(self): self._2dseq = dict() def _update_studyinfo(self): - if self._subject is not None: + if self._subject != None: subject = self._subject self.user_account = subject.headers['OWNER'] self.subj_id = get_value(subject, 'SUBJECT_id') diff --git a/brkraw/lib/utils.py b/brkraw/lib/utils.py index 048bedc..ef96b30 100644 --- a/brkraw/lib/utils.py +++ b/brkraw/lib/utils.py @@ -1,4 +1,4 @@ -from .errors import UnexpectedError +from .errors import * from .reference import * import re import os @@ -30,7 +30,7 @@ def load_param(stringlist): for line_num, line in enumerate(stringlist): regex_obj = re.match(ptrn_param, line) # if line is key=value pair - if regex_obj is not None: + if regex_obj != None: # parse key and value key = re.sub(ptrn_param, r'\g', line) value = re.sub(ptrn_param, r'\g', line) @@ -105,7 +105,7 @@ def convert_data_to(data, shape): cont_parser = [] for cont in map(str.strip, parsed.group('contents').split(',')): cont = convert_data_to(cont, -1) - if cont is not None: + if cont != None: cont_parser.append(cont) if key not in parser.keys(): parser[key] = [] @@ -120,7 +120,7 @@ def convert_data_to(data, shape): else: is_array = re.findall(ptrn_array, data) # parse data shape - if shape is not -1: + if shape != -1: shape = re.sub(ptrn_array, r'\g', shape) if ',' in shape: shape = [convert_string_to(c) for c in shape.split(',')] @@ -191,7 +191,7 @@ def meta_get_value(value, acqp, method, visu_pars): max_index = len(value) - 1 for i, vi in enumerate(value): val = meta_get_value(vi, acqp, method, visu_pars) - if val is not None: + if val != None: if val == vi: if i == max_index: parser.append(val) @@ -228,7 +228,7 @@ def is_express(value): def meta_check_where(value, acqp, method, visu_pars): val = meta_get_value(value['key'], acqp, method, visu_pars) - if val is not None: + if val != None: if isinstance(value['where'], str): if value['where'] not in val: return None @@ -243,12 +243,12 @@ def meta_check_where(value, acqp, method, visu_pars): def meta_check_index(value, acqp, method, visu_pars): val = meta_get_value(value['key'], acqp, method, visu_pars) - if val is not None: + if val != None: if isinstance(value['idx'], int): return val[value['idx']] else: idx = meta_get_value(value['idx'], acqp, method, visu_pars) - if idx is not None: + if idx != None: return val[idx] else: return None @@ -335,7 +335,6 @@ def get_filesize(file_path): def bids_validation(df, idx, key, val, num_char_allowed, dtype=None): import string - from shleeh.errors import InvalidValueInField col = string.ascii_uppercase[df.columns.tolist().index(key)] special_char = re.compile(r'[^0-9a-zA-Z]') str_val = str(val) @@ -344,13 +343,13 @@ def bids_validation(df, idx, key, val, num_char_allowed, dtype=None): message = "{} You can't use more than {} characters.".format(loc, num_char_allowed) raise InvalidValueInField(message) matched = special_char.search(str_val) - if matched is not None: + if matched != None: if ' ' in matched.group(): message = "{} Empty string is not allowed.".format(loc) else: message = "{} Special characters are not allowed.".format(loc) raise InvalidValueInField(message) - if dtype is not None: + if dtype != None: try: dtype(val) except: @@ -361,7 +360,6 @@ def bids_validation(df, idx, key, val, num_char_allowed, dtype=None): def get_bids_ref_obj(ref_path, row): import json - from shleeh.errors import InvalidApproach if os.path.exists(ref_path) and ref_path.lower().endswith('.json'): ref_data = json.load(open(ref_path)) ref = ref_data['common'] diff --git a/brkraw/scripts/brkraw.py b/brkraw/scripts/brkraw.py index b49e6c7..c7f2bb5 100644 --- a/brkraw/scripts/brkraw.py +++ b/brkraw/scripts/brkraw.py @@ -296,7 +296,7 @@ def main(): except: dset = None - if dset is not None: + if dset != None: if dset.is_pvdataset: pvobj = dset.pvobj @@ -305,7 +305,7 @@ def main(): # make subj_id bids appropriate subj_id = cleanSubjectID(subj_id) - + sess_id = pvobj.session_id # make sess_id bids appropriate @@ -327,15 +327,15 @@ def main(): item['modality'] = m item['Start'] = s item['End'] = e - df = df.append(item, ignore_index=True) + df = pd.concat([df, pd.DataFrame([item])], ignore_index=True) elif datatype == 'dwi': item['modality'] = 'dwi' - df = df.append(item, ignore_index=True) + df = pd.concat([df, pd.DataFrame([item])], ignore_index=True) elif datatype == 'anat' and re.search('MSME', method, re.IGNORECASE): item['modality'] = 'MESE' - df = df.append(item, ignore_index=True) + df = pd.concat([df, pd.DataFrame([item])], ignore_index=True) else: - df = df.append(item, ignore_index=True) + df = pd.concat([df, pd.DataFrame([item])], ignore_index=True) if 'xlsx' in ds_format: df.to_excel(output, index=None) elif 'csv' in ds_format: @@ -490,6 +490,8 @@ def cleanSubjectID(subj_id): import warnings + subj_id = str(subj_id) + # underscore will mess up bids output if '_' in subj_id: subj_id = subj_id.replace('_', 'Underscore') @@ -721,7 +723,7 @@ def is_localizer(pvobj, scan_id, reco_id): def override_header(pvobj, subjtype, position): """override subject position and subject type""" import warnings - if position is not None: + if position != None: try: pvobj.override_position(position) except: @@ -729,7 +731,7 @@ def override_header(pvobj, subjtype, position): "The position variable can be defiend as _," + \ "available BodyParts are (Head, Foot, Tail) and sides are (Supine, Prone, Left, Right). (e.g. Head_Supine)" raise InvalidApproach(msg) - if subjtype is not None: + if subjtype != None: try: pvobj.override_subjtype(subjtype) except: From 94b4efbd0437f076111ba677672d3c8541a6d5c7 Mon Sep 17 00:00:00 2001 From: dvm-shlee Date: Wed, 5 Jul 2023 16:13:43 -0400 Subject: [PATCH 2/5] v0.3.9 --- brkraw/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brkraw/__init__.py b/brkraw/__init__.py index acfe095..ab8fffe 100644 --- a/brkraw/__init__.py +++ b/brkraw/__init__.py @@ -1,6 +1,6 @@ from .lib import * -__version__ = '0.3.8' +__version__ = '0.3.9' __all__ = ['BrukerLoader', '__version__'] From 88c06bb6369e3e50168a9203eaf6ee5bb3bca774 Mon Sep 17 00:00:00 2001 From: dvm-shlee Date: Wed, 5 Jul 2023 16:29:16 -0400 Subject: [PATCH 3/5] patch bids template filename last chr missing --- brkraw/scripts/brkraw.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/brkraw/scripts/brkraw.py b/brkraw/scripts/brkraw.py index c7f2bb5..509141e 100644 --- a/brkraw/scripts/brkraw.py +++ b/brkraw/scripts/brkraw.py @@ -347,8 +347,7 @@ def main(): raise InvalidApproach('Invalid input for datasheet!') if make_json: - fname = output[:-5] - json_fname = '{}.json'.format(fname) + json_fname = '{}.json'.format(ds_fname) print('Creating JSON syntax template for parsing the BIDS required metadata ' '(BIDS v{}): {}'.format(_supporting_bids_ver, json_fname)) with open(json_fname, 'w') as f: From c7f3f9b72a4dce900118bad2dde44134f67db9d6 Mon Sep 17 00:00:00 2001 From: dvm-shlee Date: Wed, 5 Jul 2023 16:30:05 -0400 Subject: [PATCH 4/5] 0.3.10 --- brkraw/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brkraw/__init__.py b/brkraw/__init__.py index ab8fffe..b5d6a9d 100644 --- a/brkraw/__init__.py +++ b/brkraw/__init__.py @@ -1,6 +1,6 @@ from .lib import * -__version__ = '0.3.9' +__version__ = '0.3.10' __all__ = ['BrukerLoader', '__version__'] From 3870e500d6e6ca3e2d57978e23107e1a890df733 Mon Sep 17 00:00:00 2001 From: dvm-shlee Date: Wed, 5 Jul 2023 17:08:26 -0400 Subject: [PATCH 5/5] numpy '!= None' error --- brkraw/lib/loader.py | 2 +- brkraw/lib/orient.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/brkraw/lib/loader.py b/brkraw/lib/loader.py index 7ae171a..b3bff6e 100644 --- a/brkraw/lib/loader.py +++ b/brkraw/lib/loader.py @@ -1147,7 +1147,7 @@ def _get_matrix_size(self, visu_pars, dataobj=None): if num_temporal_frame > 1: matrix_size.append(num_temporal_frame) - if dataobj != None: + if isinstance(dataobj, np.ndarray): # matrix size inspection dataobj_shape = dataobj.shape[0] if multiply_all(matrix_size) != dataobj_shape: diff --git a/brkraw/lib/orient.py b/brkraw/lib/orient.py index a68a98a..2b9f1e4 100644 --- a/brkraw/lib/orient.py +++ b/brkraw/lib/orient.py @@ -146,7 +146,7 @@ def get_origin(slice_position, gradient_orient): max_delta_axis = np.argmax([dx, dy, dz]) rx, ry, rz = [None, None, None] - if gradient_orient != None: + if isinstance(gradient_orient, np.ndarray): zmat = np.zeros(gradient_orient[0].shape) for cid, col in enumerate(gradient_orient[0].T): yid = np.argmax(abs(col))