def rawniml2string(p, form='text'): if type(p) is list: nb = '\n'.encode() return nb.join(rawniml2string(v, form) for v in p) if not form in ['text', 'binary', 'base64']: raise ValueError("Illegal form %s" % form) q = p.copy() # make a shallow copy if 'nodes' in q: s_body = rawniml2string(q.pop('nodes'), form) # recursion else: data = q.pop('data') data = types.nimldataassupporteddtype( data) # ensure the data format is supported by NIML s_body = _data2string(data, form) if form == 'text': q.pop('ni_form', None) # defaults to text, remove if already there else: byteorder = types.data2ni_form(data, form) if byteorder: q['ni_form'] = byteorder # remove some unncessary fields for f in ['vec_typ', 'vec_len', 'vec_num']: q.pop(f, None) s_name = q.pop('name', None).encode() s_header = _header2string(q) d = map(lambda x: x.encode(), ['<', '\n', ' >', '</', '>']) return b''.join( (d[0], s_name, d[1], s_header, d[2], s_body, d[3], s_name, d[4]))
def rawniml2string(p, form='text'): if type(p) is list: nb = '\n'.encode() return nb.join(rawniml2string(v, form) for v in p) if not form in ['text', 'binary', 'base64']: raise ValueError("Illegal form %s" % form) q = p.copy() # make a shallow copy if 'nodes' in q: s_body = rawniml2string(q.pop('nodes'), form) # recursion else: data = q.pop('data') data = types.nimldataassupporteddtype(data) # ensure the data format is supported by NIML s_body = _data2string(data, form) if form == 'text': q.pop('ni_form', None) # defaults to text, remove if already there else: byteorder = types.data2ni_form(data, form) if byteorder: q['ni_form'] = byteorder # remove some unncessary fields for f in ['vec_typ', 'vec_len', 'vec_num']: q.pop(f, None) s_name = q.pop('name', None).encode() s_header = _header2string(q) d = map(lambda x:x.encode(), ['<', '\n', ' >', '</', '>']) return b''.join((d[0], s_name, d[1], s_header, d[2], s_body, d[3], s_name, d[4]))
def _dset2rawniml_complete(r): '''adds any missing information and ensures data is formatted properly''' # if data is a list of strings, join it and store it as a string # otherwise leave data untouched if types.numpy_data_isstring(r['data']): r['data'] = list(r['data']) while True: data = r['data'] tp = type(data) if types.numpy_data_isstring(r['data']): r['data'] = list(r['data']) elif tp is list: r['data'] = ";".join(data) else: break # we're done if issubclass(tp, basestring): r['ni_dimen'] = '1' r['ni_type'] = 'String' elif tp is np.ndarray: data = types.nimldataassupporteddtype(data) if len(data.shape) == 1: data = np.reshape(data, (data.shape[0], 1)) r['data'] = data # ensure we store a supported type nrows, ncols = data.shape r['ni_dimen'] = str(nrows) tpstr = types.numpy_type2name(data.dtype) r['ni_type'] = '%d*%s' % (ncols, tpstr) if nrows > 1 else tpstr else: raise TypeError('Illegal type %r in %r' % (tp, data)) if not 'name' in r: r['name'] = 'AFNI_atr' return r
def _dset2rawniml_complete(r): '''adds any missing information and ensures data is formatted properly''' # if data is a list of strings, join it and store it as a string # otherwise leave data untouched if types.numpy_data_isstring(r['data']): r['data'] = list(r['data']) while True: data = r['data'] tp = type(data) if types.numpy_data_isstring(r['data']): r['data'] = list(r['data']) elif tp is list: if all(isinstance(d, basestring) for d in data): r['data'] = ";".join(data) else: tp = 'mixed' break else: break # we're done if tp == 'mixed': #data = [types.nimldataassupporteddtype(d) for d in data] #r['data'] = data nrows, ncols = _dset_nrows_ncols(data) r['ni_dimen'] = str(nrows) tpstrs = [] for d in data: if isinstance(d, basestring) or \ (type(d) is list and all(isinstance(di, basestring) for di in d)): tpstr = 'String' elif isinstance(d, np.ndarray): tpstr = types.numpy_type2name(d.dtype) else: raise ValueError('unrecognized type %s' % type(d)) tpstrs.append(tpstr) r['ni_type'] = ','.join(tpstrs) elif issubclass(tp, basestring): r['ni_dimen'] = '1' r['ni_type'] = 'String' elif tp is np.ndarray: data = types.nimldataassupporteddtype(data) if len(data.shape) == 1: data = np.reshape(data, (data.shape[0], 1)) r['data'] = data # ensure we store a supported type nrows, ncols = data.shape r['ni_dimen'] = str(nrows) tpstr = types.numpy_type2name(data.dtype) r['ni_type'] = '%d*%s' % (ncols, tpstr) if nrows > 1 else tpstr elif not data is None: raise TypeError('Illegal type %r in %r' % (tp, data)) if not 'name' in r: r['name'] = 'AFNI_atr' return r
def rawniml2string(p, form='text'): '''Converts a raw NIML element to string representation Parameters ---------- niml: dict Raw NIML element form: 'text', 'binary', 'base64' Output form of niml Returns ------- s: bytearray String representation of niml in output form 'form'. ''' if type(p) is list: nb = '\n'.encode() return nb.join(rawniml2string(v, form) for v in p) if not form in ['text', 'binary', 'base64']: raise ValueError("Illegal form %s" % form) q = p.copy() # make a shallow copy has_body = True if 'nodes' in q: s_body = rawniml2string(q.pop('nodes'), form) # recursion elif 'data' in q: data = q.pop('data') data = types.nimldataassupporteddtype( data) # ensure the data format is supported by NIML s_body = _data2string(data, form) if form == 'text': q.pop('ni_form', None) # defaults to text, remove if already there else: byteorder = types.data2ni_form(data, form) if byteorder: q['ni_form'] = byteorder # remove some unncessary fields for f in ['vec_typ', 'vec_len', 'vec_num']: q.pop(f, None) else: has_body = False s_name = q.pop('name', None).encode() s_header = _header2string(q) if has_body: delim = ['<', '\n', ' >', '</', '>'] values = [s_name, s_header, s_body, s_name] else: delim = ['<', '\n', '/>'] values = [s_name, s_header] delim_enc = map(lambda x: x.encode(), delim) n_delim = len(delim_enc) assert (n_delim == len(values) + 1) # zip with unequal length elems = [] for i in xrange(n_delim): elems.append(delim_enc[i]) if i + 1 < n_delim: # one element less than the number of delimeters elems.append(values[i]) return b''.join(elems)
def rawniml2string(p, form='text'): '''Converts a raw NIML element to string representation Parameters ---------- niml: dict Raw NIML element form: 'text', 'binary', 'base64' Output form of niml Returns ------- s: bytearray String representation of niml in output form 'form'. ''' if type(p) is list: nb = '\n'.encode() return nb.join(rawniml2string(v, form) for v in p) if not form in ['text', 'binary', 'base64']: raise ValueError("Illegal form %s" % form) q = p.copy() # make a shallow copy has_body = True if 'nodes' in q: s_body = rawniml2string(q.pop('nodes'), form) # recursion elif 'data' in q: data = q.pop('data') data = types.nimldataassupporteddtype(data) # ensure the data format is supported by NIML s_body = _data2string(data, form) if form == 'text': q.pop('ni_form', None) # defaults to text, remove if already there else: byteorder = types.data2ni_form(data, form) if byteorder: q['ni_form'] = byteorder # remove some unncessary fields for f in ['vec_typ', 'vec_len', 'vec_num']: q.pop(f, None) else: has_body = False s_name = q.pop('name', None).encode() s_header = _header2string(q) if has_body: delim = ['<', '\n', ' >', '</', '>'] values = [s_name, s_header, s_body, s_name] else: delim = ['<', '\n', '/>'] values = [s_name, s_header] delim_enc = map(lambda x: x.encode(), delim) n_delim = len(delim_enc) assert (n_delim == len(values) + 1) # zip with unequal length elems = [] for i in xrange(n_delim): elems.append(delim_enc[i]) if i + 1 < n_delim: # one element less than the number of delimeters elems.append(values[i]) return b''.join(elems)
def _dset2rawniml_complete(r): '''adds any missing information and ensures data is formatted properly''' # if data is a list of strings, join it and store it as a string # otherwise leave data untouched if types.numpy_data_isstring(r['data']): r['data'] = list(r['data']) while True: data = r['data'] tp = type(data) if types.numpy_data_isstring(r['data']): r['data'] = list(r['data']) elif tp is list: if all(isinstance(d, basestring) for d in data): r['data'] = ";".join(data) else: tp = 'mixed' break else: break # we're done if tp == 'mixed': #data = [types.nimldataassupporteddtype(d) for d in data] #r['data'] = data nrows, ncols = _dset_nrows_ncols(data) r['ni_dimen'] = str(nrows) tpstrs = [] for d in data: if isinstance(d, basestring) or \ (type(d) is list and all(isinstance(di, basestring) for di in d)): tpstr = 'String' elif isinstance(d, np.ndarray): tpstr = types.numpy_type2name(d.dtype) else: raise ValueError('unrecognized type %s' % type(d)) tpstrs.append(tpstr) r['ni_type'] = ','.join(tpstrs) elif issubclass(tp, basestring): r['ni_dimen'] = '1' r['ni_type'] = 'String' elif tp is np.ndarray: data = types.nimldataassupporteddtype(data) if len(data.shape) == 1: data = np.reshape(data, (data.shape[0], 1)) r['data'] = data # ensure we store a supported type nrows, ncols = data.shape r['ni_dimen'] = str(nrows) tpstr = types.numpy_type2name(data.dtype) r['ni_type'] = '%d*%s' % (ncols, tpstr) if nrows > 1 else tpstr elif not data is None: raise TypeError('Illegal type %r in %r' % (tp, data)) if not 'name' in r: r['name'] = 'AFNI_atr' return r
def _dset2rawniml_complete(r): """adds any missing information and ensures data is formatted properly""" # if data is a list of strings, join it and store it as a string # otherwise leave data untouched if types.numpy_data_isstring(r["data"]): r["data"] = list(r["data"]) while True: data = r["data"] tp = type(data) if types.numpy_data_isstring(r["data"]): r["data"] = list(r["data"]) elif tp is list: if all(isinstance(d, basestring) for d in data): r["data"] = ";".join(data) else: tp = "mixed" break else: break # we're done if tp == "mixed": # data = [types.nimldataassupporteddtype(d) for d in data] # r['data'] = data nrows, ncols = _dset_nrows_ncols(data) r["ni_dimen"] = str(nrows) tpstrs = [] for d in data: if isinstance(d, basestring) or (type(d) is list and all(isinstance(di, basestring) for di in d)): tpstr = "String" elif isinstance(d, np.ndarray): tpstr = types.numpy_type2name(d.dtype) else: raise ValueError("unrecognized type %s" % type(d)) tpstrs.append(tpstr) r["ni_type"] = ",".join(tpstrs) elif issubclass(tp, basestring): r["ni_dimen"] = "1" r["ni_type"] = "String" elif tp is np.ndarray: data = types.nimldataassupporteddtype(data) if len(data.shape) == 1: data = np.reshape(data, (data.shape[0], 1)) r["data"] = data # ensure we store a supported type nrows, ncols = data.shape r["ni_dimen"] = str(nrows) tpstr = types.numpy_type2name(data.dtype) r["ni_type"] = "%d*%s" % (ncols, tpstr) if nrows > 1 else tpstr elif data is not None: raise TypeError("Illegal type %r in %r" % (tp, data)) if not "name" in r: r["name"] = "AFNI_atr" return r