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 _data2string(data, form): '''Converts a data element to binary, text or base64 representation''' if isinstance(data, basestring): return ('"%s"' % encode_escape(data)).encode() elif type(data) is np.ndarray: if form == 'text' or types.numpy_data_isstring(data): f = types.numpy_data2printer(data) nrows, ncols = data.shape return _TEXT_ROWSEP.join([ _TEXT_COLSEP.join([f(data[row, col]) for col in xrange(ncols)]) for row in xrange(nrows) ]).encode() elif form == 'binary': data_reshaped = data.reshape((data.shape[1], data.shape[0])) r = data_reshaped.tostring() debug( 'NIML', 'Binary encoding (len %d -> %d): [%s]' % (data_reshaped.size, len(r), _partial_string(r, 0))) return r elif form == 'base64': data_reshaped = data.reshape((data.shape[1], data.shape[0])) r = base64.b64encode(data_reshaped.tostring()) debug('NIML', 'Encoding ok: [%s]', _partial_string(r, 0)) return r else: raise ValueError("illegal format %s" % format) elif type(data) is list: # mixed types, each column in its own container # always use text output format, even if requested form is binary of base64 ncols = len(data) if ncols == 0: return "".encode() else: nrows = len(data[0]) # separate formatter functions for each column # if list of strings then take first element of the list to get a string formattr # else use the entire np array to get a numeric formatter fs = [ types.numpy_data2printer(d[0] if type(d) is list else d) for d in data ] return _TEXT_ROWSEP.join([ _TEXT_COLSEP.join( [fs[col](data[col][row]) for col in xrange(ncols)]) for row in xrange(nrows) ]).encode() else: raise TypeError("Unknown type %r" % type(data))
def _data2string(data, form): '''Converts a data element to binary, text or base64 representation''' if isinstance(data, basestring): return ('"%s"' % encode_escape(data)).encode() elif type(data) is np.ndarray: if form == 'text' or types.numpy_data_isstring(data): f = types.numpy_data2printer(data) nrows, ncols = data.shape return _TEXT_ROWSEP.join([_TEXT_COLSEP.join([f(data[row, col]) for col in xrange(ncols)]) for row in xrange(nrows)]).encode() elif form == 'binary': data_reshaped = data.reshape((data.shape[1], data.shape[0])) r = data_reshaped.tostring() debug('NIML', 'Binary encoding (len %d -> %d): [%s]' % (data_reshaped.size, len(r), _partial_string(r, 0))) return r elif form == 'base64': data_reshaped = data.reshape((data.shape[1], data.shape[0])) r = base64.b64encode(data_reshaped.tostring()) debug('NIML', 'Encoding ok: [%s]', _partial_string(r, 0)) return r else: raise ValueError("illegal format %s" % format) elif type(data) is list: # mixed types, each column in its own container # always use text output format, even if requested form is binary of base64 ncols = len(data) if ncols == 0: return "".encode() else: nrows = len(data[0]) # separate formatter functions for each column # if list of strings then take first element of the list to get a string formattr # else use the entire np array to get a numeric formatter fs = [types.numpy_data2printer(d[0] if type(d) is list else d) for d in data] return _TEXT_ROWSEP.join([_TEXT_COLSEP.join([fs[col](data[col][row]) for col in xrange(ncols)]) for row in xrange(nrows)]).encode() else: raise TypeError("Unknown type %r" % type(data))
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