class TestOptionsWriter(unittest.TestCase):

    def setUp(self):
        unittest.TestCase.setUp(self)

        self.writer = OptionsWriter()

        self.obj = Options(name="Test")

        self.obj.programs.add(DummyProgram())

        self.obj.beam.energy_eV = 1234

        self.obj.detectors['bse'] = BackscatteredElectronEnergyDetector(1000, (0, 1234))
        self.obj.limits.add(ShowersLimit(5678))
        self.obj.models.add(ELASTIC_CROSS_SECTION.rutherford)

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    def testcan_convert(self):
        self.assertTrue(self.writer.can_convert(self.obj))

    def testconvert(self):
        self.writer.convert(self.obj)
        element = self.writer.get()

        self.assertEqual('Test', element.get('name'))

        self.assertEqual(self.obj.uuid, element.get('uuid'))

        children = list(element.find('programs'))
        self.assertEqual(1, len(children))

        children = list(element.find('beam'))
        self.assertEqual(1, len(children))

        children = list(element.find('geometry'))
        self.assertEqual(1, len(children))

        children = list(element.find('detectors'))
        self.assertEqual(1, len(children))

        children = list(element.find('limits'))
        self.assertEqual(1, len(children))

        children = list(element.find('models'))
        self.assertEqual(1, len(children))

    def testconvert2(self):
        uuid = self.obj.uuid
        self.writer.convert(self.obj)
        element = self.writer.get()
        self.assertEqual(uuid, element.get('uuid'))
Beispiel #2
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))
Beispiel #3
0
 def toelement(self):
     from pymontecarlo.fileformat.options.options import OptionsWriter
     writer = OptionsWriter()
     writer.convert(self)
     return writer.get()
Beispiel #4
0
 def _write_options(self, options, group):
     writer = OptionsWriter()
     writer.convert(options)
     element = writer.get()
     group.attrs['options'] = etree.tostring(element)