Esempio n. 1
0
def to_file(obj, fname, fmt=None, overwrite=True, warn=True, **kwargs):
    """Dispatch correct file writer based on fmt (if specified) or guess
    based on file name's extension"""
    if fmt is None:
        rootname, ext = os.path.splitext(fname)
        ext = ext.replace('.', '').lower()
    else:
        rootname = fname
        ext = fmt.lower()

    if ext in ZIP_EXTS or ext in XOR_EXTS:
        rootname, inner_ext = os.path.splitext(rootname)
        inner_ext = inner_ext.replace('.', '').lower()
        ext = inner_ext

    if ext in jsons.JSON_EXTS:
        return jsons.to_json(obj,
                             fname,
                             overwrite=overwrite,
                             warn=warn,
                             **kwargs)
    elif ext in hdf.HDF5_EXTS:
        return hdf.to_hdf(obj, fname, overwrite=overwrite, warn=warn, **kwargs)
    elif ext in PKL_EXTS:
        return to_pickle(obj, fname, overwrite=overwrite, warn=warn, **kwargs)
    elif ext in TXT_EXTS:
        if kwargs:
            raise ValueError('Following additional keyword arguments not'
                             ' accepted when writing to text file: %s' %
                             kwargs.keys())
        return to_txt(obj, fname)
    else:
        errmsg = 'Unrecognized file type/extension: ' + ext
        log.logging.error(errmsg)
        raise TypeError(errmsg)
Esempio n. 2
0
def to_file(obj, fname, fmt=None):
    """Dispatch correct file writer based on fmt (if specified) or guess
    based on file name's extension"""
    if fmt is None:
        base, ext = os.path.splitext(fname)
        ext = ext.replace('.', '').lower()
    else:
        ext = fmt.lower()
    if ext in JSON_EXTS:
        return jsons.to_json(obj, fname)
    elif ext in HDF5_EXTS:
        return hdf.to_hdf(obj, fname)
    elif ext in PKL_EXTS:
        return cPickle.dump(obj, file(fname, 'wb'))
    else:
        errmsg = 'Unrecognized file type/extension: ' + ext
        logging.error(errmsg)
        raise TypeError(errmsg)
Esempio n. 3
0
File: utils.py Progetto: lkijmj/pisa
def to_file(obj, fname, fmt=None):
    """Dispatch correct file writer based on fmt (if specified) or guess
    based on file name's extension"""
    if fmt is None:
        base, ext = os.path.splitext(fname)
        ext = ext.replace('.', '').lower()
    else:
        ext = fmt.lower()
    if ext in JSON_EXTS:
        return jsons.to_json(obj, fname)
    elif ext in HDF5_EXTS:
        return hdf.to_hdf(obj, fname)
    elif ext in PKL_EXTS:
        return cPickle.dump(obj, file(fname, 'wb'))
    else:
        errmsg = 'Unrecognized file type/extension: ' + ext
        logging.error(errmsg)
        raise TypeError(errmsg)
Esempio n. 4
0
 def save(self, fname, **kwargs):
     hdf.to_hdf(self, fname, attrs=self.metadata, **kwargs)
Esempio n. 5
0
 def store_kernels(self, filename, fmt='hdf5'):
     """Store reconstruction kernels in HDF5 format"""
     if fmt.lower() in ['hdf', 'h5', 'hdf5']:
         hdf.to_hdf(self.kernels, filename)
     else:
         raise NotImplementedError('Only hdf5 is implemented')
Esempio n. 6
0
            print "Skipping file: ", filename
            continue

        if not minimizer_settings:
            minimizer_settings = data['minimizer_settings']

        if not template_settings:
            template_settings = data['template_settings']

        if not pseudo_data_settings:
            try:
                pseudo_data_settings = data['pseudo_data_settings']
            except:
                pass

        for trial in data['trials']:
            trials = process_trial(trials, trial)

    data = {
        'minimizer_settings': minimizer_settings,
        'template_settings': template_settings,
        'trials': trials
    }

    if pseudo_data_settings:
        data['pseudo_data_settings'] = pseudo_data_settings

    to_hdf(data_dict=data, tgt=args.outfile)

    print "Time to process files: %s" % (datetime.now() - start_time)
Esempio n. 7
0
            print "Skipping file: ",filename
            continue
    
        if not minimizer_settings:
            minimizer_settings = data['minimizer_settings']
    
        if not template_settings:
            template_settings = data['template_settings']
    
        if not pseudo_data_settings:
            try:
                pseudo_data_settings = data['pseudo_data_settings']
            except:
                pass
    
        for trial in data['trials']:
            trials = process_trial(trials,trial)
    
    data = {
        'minimizer_settings': minimizer_settings,
        'template_settings': template_settings,
        'trials': trials
    }
    
    if pseudo_data_settings:
        data['pseudo_data_settings'] = pseudo_data_settings
    
    to_hdf(data_dict=data, tgt=args.outfile)
    
    print "Time to process files: %s"%(datetime.now() - start_time)