def save_txt(fname='nda.txt', arr=None, cmts=(), fmt='%.1f', verbos=False, addmetad=True) : """Save n-dimensional numpy array to text file with metadata. - fname - file name for text file, - arr - numpy array, - cmts -list of comments which will be saved in the file header. """ #recs = ['# %03d %s' % (i,cmt) for i, cmt in enumerate(cmts)] recs = ['# %s' % cmt for cmt in cmts] recs.append('\n# HOST %s' % gu.get_hostname()) recs.append('# WORK_DIR %s' % gu.get_cwd()) recs.append('# FILE_NAME %s' % fname) recs.append('# DATE_TIME %s' % gu.str_tstamp(fmt='%Y-%m-%dT%H:%M:%S')) recs.append('# UID %s' % gu.get_login()) recs.append('# SHAPE %s' % str(arr.shape).replace(' ','')) recs.append('# DATATYPE %s' % str(arr.dtype)) if addmetad : recs.append('\n# DTYPE %s' % str(arr.dtype)) recs.append('# NDIM %s' % len(arr.shape)) for i in range(len(arr.shape)) : recs.append('# DIM:%d %s' % (i, arr.shape[i])) arr2d = nu.reshape_nda_to_2d(arr) # pretty formatting recs.append('' if len(arr.shape)>1 else '\n') nline = '\n' if len(arr.shape)>1 else ' ' hdr = '\n'.join(recs) #print(hdr) np.savetxt(fname, arr, fmt, delimiter=' ', newline=nline, header=hdr, comments='') #, footer='\n') #, comments='# ') if verbos : print('File %s is saved' % fname)
def set_metadata_values(self): """Sets metadata values associated with self.data_nda """ logger.debug('in set_metadata_values') model = self.model nda = self.data_nda colk, colv = 0, 1 for row in range(model.rowCount()): key = model.item(row, colk).text() if key == 'data_size': model.item(row, colv).setText(str(nda.size)) elif key == 'data_dtype': model.item(row, colv).setText(str(nda.dtype)) elif key == 'data_ndim': model.item(row, colv).setText(str(nda.ndim)) elif key == 'data_shape': model.item(row, colv).setText(str(nda.shape)) elif key == 'host': model.item(row, colv).setText(gu.get_hostname()) elif key == 'uid': model.item(row, colv).setText(gu.get_login()) elif key == 'cwd': model.item(row, colv).setText(gu.get_cwd()) logger.info('Model document content:\n %s\n%s' % (self.info_model_dicdoc(), info_ndarr(self.data_nda, 'data n-d array ')))
def msg_to_log(runs=[]): """Returns (str) message to the log file for list of (str) runs. """ if len(runs) == 0: return None tstamp = gu.str_tstamp('%Y-%m-%dT%H:%M:%S', time()) login = gu.get_login() cwd = gu.get_cwd() host = gu.get_hostname() cmd = sys.argv[0].split('/')[-1] recs = [ '%s %s %s %s cwd:%s cmd:%s' % (s, tstamp, login, host, cwd, cmd) for s in runs ] text = '\n'.join(recs) return text + '\n'
def _history_record(dcmts): """Returns history record made of dictionary comments and system info """ user = gu.get_login() host = gu.get_hostname() tstamp = gu.str_tstamp(fmt='%Y-%m-%dT%H:%M:%S zone:%Z') rnum = '%04d' % dcmts.get('run') exp = '%s' % dcmts.get('exp') ifname = '%s' % dcmts.get('ifname') ofname = '%s' % dcmts.get('fname') app = '%s' % dcmts.get('app') cmt = '%s' % dcmts.get('comment') return 'file:%s copy_of:%s exp:%s run:%s app:%s user:%s host:%s cptime:%s comment:%s\n' % \ (ofname.ljust(14), ifname, exp.ljust(8), rnum.ljust(4), app.ljust(10), user, host, tstamp.ljust(29), cmt)
def docdic(data, dataid, **kwargs): """Returns dictionary for db document in style of JSON object. """ doc = { 'experiment': kwargs.get('experiment', None), 'run': kwargs.get('run', '0'), 'run_end': kwargs.get('run_end', 'end'), 'detector': kwargs.get('detector', None), 'ctype': kwargs.get('ctype', None), 'time_sec': kwargs.get('time_sec', None), 'time_stamp': kwargs.get('time_stamp', None), 'version': kwargs.get('version', 'v00'), 'comment': kwargs.get('comment', ''), 'extpars': kwargs.get('extpars', None), 'uid': gu.get_login(), 'host': gu.get_hostname(), 'cwd': gu.get_cwd(), 'id_data': dataid, } if isinstance(data, np.ndarray): doc['data_type'] = 'ndarray' doc['data_dtype'] = str(data.dtype) doc['data_size'] = '%d' % data.size doc['data_ndim'] = '%d' % data.ndim doc['data_shape'] = str(data.shape) elif isinstance(data, str): doc['data_type'] = 'str' doc['data_size'] = '%d' % len(data) else: doc['data_type'] = 'any' logger.debug('doc data type: %s' % doc['data_type']) return doc