Ejemplo n.º 1
0
def append(results, filepath):
    with FileLock(filepath), h5py.File(filepath, 'r+') as hdf5file:
        # Check UUID of base options
        source = BytesIO(hdf5file.attrs['options'])
        reader = OptionsReader()
        reader.read(source)
        options = reader.get()

        if options.uuid != results.options.uuid:
            raise ValueError('UUID of base options do not match: %s != %s' % \
                             (options.uuid, results.options.uuid))

        # Save results
        identifiers = np.array(hdf5file.attrs['identifiers'], 'U').tolist()
        for container in results:
            identifier = container.options.uuid
            identifiers.append(identifier)

            group = hdf5file.create_group('result-' + identifier)

            # Save each result
            for key, result in container.items():
                subgroup = group.create_group(key)
                handler = find_convert_handler('pymontecarlo.fileformat.results.result',
                                               result, subgroup)
                handler.convert(result, subgroup)

            # Save options
            writer = OptionsWriter()
            writer.convert(container.options)
            element = writer.get()
            group.attrs['options'] = etree.tostring(element)

        # Update identifiers
        del hdf5file.attrs['identifiers']
        hdf5file.attrs.create('identifiers', identifiers,
                              dtype=h5py.special_dtype(vlen=str))
Ejemplo n.º 2
0
 def read(cls, source):
     from pymontecarlo.fileformat.options.options import OptionsReader
     reader = OptionsReader()
     reader.read(source)
     return reader.get()