示例#1
0
    def testread(self):
        # Section with digit
        lines = ['[1section1]', 'option=value1', 'option2=value2',
                 '[section2]', 'option3=value3', 'option4=value4']
        sobj = StringIO('\n'.join(lines))
        c = ConfigParser()
        self.assertRaises(IOError, c.read, sobj, False)

        # Option with digit
        lines = ['[section1]', '1option=value1', 'option2=value2',
                 '[section2]', 'option3=value3', 'option4=value4']
        sobj = StringIO('\n'.join(lines))
        c = ConfigParser()
        self.assertRaises(IOError, c.read, sobj, False)

        # Skip
        lines = ['[1section1]', 'option=value1', 'option2=value2',
                 '[section2]', '1option3=value3', 'option4=value4']
        sobj = StringIO('\n'.join(lines))
        c = ConfigParser()
        c.read(sobj, ignore_errors=True)

        self.assertEqual(1, len(list(c)))
        self.assertTrue('section2' in c)
        self.assertTrue('option4' in c.section2)
示例#2
0
    def write(self):
        dirpath = os.path.join(os.path.expanduser('~'), '.pymontecarlo')
        if not os.path.exists(dirpath):
            os.mkdir(dirpath)

        with open(os.path.join(dirpath, 'settings.cfg'), 'w') as fileobj:
            ConfigParser.write(self, fileobj)

        reload_settings()
示例#3
0
    def load(cls, source):
        """
        Loads results from a results ZIP.
        
        :arg source: filepath or file-object
        
        :return: results container
        """
        zipfile = ZipFile(source, 'r')

        # Check version
        if zipfile.comment != 'version=%s' % VERSION:
            raise IOError, "Incorrect version of results. Only version %s is accepted" % \
                    VERSION

        # Read compositions
        reader = csv.reader(zipfile.open('compositions.csv', 'r'))

        header = reader.next()
        zs = map(int, header[1:])

        compositions = []
        for row in reader:
            wfs = map(float, row[1:])
            composition = defaultdict(float)
            composition.update(dict(zip(zs, wfs)))
            compositions.append(composition)

        # Read stats
        config = ConfigParser()
        config.read(zipfile.open('stats.cfg', 'r'))

        section = config.stats
        elapsed_time_s = float(section.elapsed_time_s)
        iterations = int(section.iterations)
        max_iterations = int(section.max_iterations)
        iterator = section.iterator
        convergor = section.convergor

        assert iterations == len(compositions)

        zipfile.close()

        return cls(compositions, elapsed_time_s, max_iterations,
                   iterator, convergor)
示例#4
0
    def load(cls, source):
        """
        Loads results from a results ZIP.
        
        :arg source: filepath or file-object
        
        :return: results container
        """
        zipfile = ZipFile(source, 'r')

        # Check version
        if zipfile.comment != 'version=%s' % VERSION:
            raise IOError, "Incorrect version of results. Only version %s is accepted" % \
                    VERSION

        # Read compositions
        reader = csv.reader(zipfile.open('compositions.csv', 'r'))

        header = reader.next()
        zs = map(int, header[1:])

        compositions = []
        for row in reader:
            wfs = map(float, row[1:])
            composition = defaultdict(float)
            composition.update(dict(zip(zs, wfs)))
            compositions.append(composition)

        # Read stats
        config = ConfigParser()
        config.read(zipfile.open('stats.cfg', 'r'))

        section = config.stats
        elapsed_time_s = float(section.elapsed_time_s)
        iterations = int(section.iterations)
        max_iterations = int(section.max_iterations)
        iterator = section.iterator
        convergor = section.convergor

        assert iterations == len(compositions)

        zipfile.close()

        return cls(compositions, elapsed_time_s, max_iterations, iterator,
                   convergor)
示例#5
0
    def setUp(self):
        TestCase.setUp(self)

        lines = ['[section1]', 'option1=value1', 'option2=value2',
                 '[section2]', 'option3=value3', 'option4=value4']
        sobj = StringIO('\n'.join(lines))

        self.c = ConfigParser()
        self.c.read(sobj)
示例#6
0
    def _update_noversion(self, filepath):
        logging.debug('Updating from "no version"')

        oldzip = ZipFile(filepath, 'r')
        newzip = ZipFile(filepath + ".new", 'w')

        # Update keys.ini
        config = ConfigParser()

        fp = oldzip.open(KEYS_INI_FILENAME, 'r')
        config.read(StringIO(fp.read().decode('ascii')))

        for section, option, value in config:
            value = value.replace('pymontecarlo.result.base.result.', '')
            setattr(getattr(config, section), option, value)

        fp = StringIO()
        config.write(fp)
        newzip.writestr(KEYS_INI_FILENAME, fp.getvalue())

        # Add other files to new zip
        for zipinfo in oldzip.infolist():
            if zipinfo.filename == KEYS_INI_FILENAME:
                continue

            data = oldzip.read(zipinfo)
            newzip.writestr(zipinfo, data)

        # Add version
        newzip.comment = b'version=2'

        oldzip.close()
        newzip.close()

        # Remove old zip and replace with new one
        os.remove(filepath)
        os.rename(filepath + ".new", filepath)

        return self._update_version2(filepath)
示例#7
0
    def _update_version1(self, filepath):
        logging.debug('Updating from "version 1"')

        oldzip = ZipFile(filepath, 'r')
        newzip = ZipFile(filepath + ".new", 'w')

        # Update stats.cfg
        config = ConfigParser()
        config.read(oldzip.open('stats.cfg', 'r'))

        limit = config.stats.convergence_limit
        del config.stats.convergence_limit
        config.stats.convergor = '<CompositionConvergor(limit=%s)>' % limit

        fp = StringIO()
        config.write(fp)
        newzip.writestr('stats.cfg', fp.getvalue())

        # Add other files to new zip
        for zipinfo in oldzip.infolist():
            if zipinfo.filename == 'stats.cfg':
                continue

            data = oldzip.read(zipinfo)
            newzip.writestr(zipinfo, data)

        # Add version
        newzip.comment = 'version=%s' % VERSION

        oldzip.close()
        newzip.close()

        # Remove old zip and replace with new one
        os.remove(filepath)
        os.rename(filepath + ".new", filepath)

        return filepath
示例#8
0
    def save(self, source):
        """
        Saves results in a results ZIP.
        
        :arg source: filepath or file-object
        """
        zipfile = ZipFile(source, 'w', compression=ZIP_DEFLATED)
        zipfile.comment = 'version=%s' % VERSION

        # Save compositions
        fp = StringIO()
        writer = csv.writer(fp)

        zs = self._compositions[0].keys()
        writer.writerow(['iteration'] + zs)

        for i, composition in enumerate(self._compositions):
            writer.writerow([i + 1] + [composition.get(z, 0.0) for z in zs])

        zipfile.writestr('compositions.csv', fp.getvalue())

        # Save stats
        config = ConfigParser()
        section = config.add_section('stats')

        section.elapsed_time_s = self.elapsed_time_s
        section.iterations = self.iterations
        section.max_iterations = self.max_iterations
        section.iterator = self.iterator
        section.convergor = self.convergor

        fp = StringIO()
        config.write(fp)
        zipfile.writestr('stats.cfg', fp.getvalue())

        zipfile.close()
示例#9
0
    def save(self, source):
        """
        Saves results in a results ZIP.
        
        :arg source: filepath or file-object
        """
        zipfile = ZipFile(source, 'w', compression=ZIP_DEFLATED)
        zipfile.comment = 'version=%s' % VERSION

        # Save compositions
        fp = StringIO()
        writer = csv.writer(fp)

        zs = self._compositions[0].keys()
        writer.writerow(['iteration'] + zs)

        for i, composition in enumerate(self._compositions):
            writer.writerow([i + 1] + [composition.get(z, 0.0) for z in zs])

        zipfile.writestr('compositions.csv', fp.getvalue())

        # Save stats
        config = ConfigParser()
        section = config.add_section('stats')

        section.elapsed_time_s = self.elapsed_time_s
        section.iterations = self.iterations
        section.max_iterations = self.max_iterations
        section.iterator = self.iterator
        section.convergor = self.convergor

        fp = StringIO()
        config.write(fp)
        zipfile.writestr('stats.cfg', fp.getvalue())

        zipfile.close()
示例#10
0
    def _update_version1(self, filepath):
        logging.debug('Updating from "version 1"')

        oldzip = ZipFile(filepath, 'r')
        newzip = ZipFile(filepath + ".new", 'w')

        # Update stats.cfg
        config = ConfigParser()
        config.read(oldzip.open('stats.cfg', 'r'))

        limit = config.stats.convergence_limit
        del config.stats.convergence_limit
        config.stats.convergor = '<CompositionConvergor(limit=%s)>' % limit

        fp = StringIO()
        config.write(fp)
        newzip.writestr('stats.cfg', fp.getvalue())

        # Add other files to new zip
        for zipinfo in oldzip.infolist():
            if zipinfo.filename == 'stats.cfg':
                continue

            data = oldzip.read(zipinfo)
            newzip.writestr(zipinfo, data)

        # Add version
        newzip.comment = 'version=%s' % VERSION

        oldzip.close()
        newzip.close()

        # Remove old zip and replace with new one
        os.remove(filepath)
        os.rename(filepath + ".new", filepath)

        return filepath
示例#11
0
class TestConfigParser(TestCase):

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

        lines = ['[section1]', 'option1=value1', 'option2=value2',
                 '[section2]', 'option3=value3', 'option4=value4']
        sobj = StringIO('\n'.join(lines))

        self.c = ConfigParser()
        self.c.read(sobj)

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

    def testskeleton(self):
        self.assertEqual('value1', self.c.section1.option1)
        self.assertEqual('value2', self.c.section1.option2)
        self.assertEqual('value3', self.c.section2.option3)
        self.assertEqual('value4', self.c.section2.option4)

        self.assertRaises(AttributeError, getattr, self.c, 'section3')
        self.assertRaises(AttributeError, getattr, self.c.section1, 'option3')

    def testread(self):
        # Section with digit
        lines = ['[1section1]', 'option=value1', 'option2=value2',
                 '[section2]', 'option3=value3', 'option4=value4']
        sobj = StringIO('\n'.join(lines))
        c = ConfigParser()
        self.assertRaises(IOError, c.read, sobj, False)

        # Option with digit
        lines = ['[section1]', '1option=value1', 'option2=value2',
                 '[section2]', 'option3=value3', 'option4=value4']
        sobj = StringIO('\n'.join(lines))
        c = ConfigParser()
        self.assertRaises(IOError, c.read, sobj, False)

        # Skip
        lines = ['[1section1]', 'option=value1', 'option2=value2',
                 '[section2]', '1option3=value3', 'option4=value4']
        sobj = StringIO('\n'.join(lines))
        c = ConfigParser()
        c.read(sobj, ignore_errors=True)

        self.assertEqual(1, len(list(c)))
        self.assertTrue('section2' in c)
        self.assertTrue('option4' in c.section2)

    def test__contains__(self):
        self.assertTrue('section1' in self.c)
        self.assertTrue('section2' in self.c)
        self.assertFalse('section3' in self.c)

        self.assertTrue('option1' in self.c.section1)
        self.assertTrue('option2' in self.c.section1)
        self.assertFalse('option3' in self.c.section1)

    def testwrite(self):
        # Add non-string value
        self.c.section1.option3 = 1

        # Write
        output = StringIO()
        self.c.write(output)

        # Read and test
        fp = StringIO(output.getvalue())
        parser = SafeConfigParser()
        parser.read_file(fp)

        self.assertTrue(parser.has_section('section1'))
        self.assertTrue(parser.has_option('section1', 'option1'))
        self.assertTrue(parser.has_option('section1', 'option2'))
        self.assertTrue(parser.has_option('section1', 'option3'))
        self.assertEqual('value1', parser.get('section1', 'option1'))
        self.assertEqual('value2', parser.get('section1', 'option2'))
        self.assertEqual('1', parser.get('section1', 'option3'))

        self.assertTrue(parser.has_section('section2'))
        self.assertTrue(parser.has_option('section2', 'option3'))
        self.assertTrue(parser.has_option('section2', 'option4'))
        self.assertEqual('value3', parser.get('section2', 'option3'))
        self.assertEqual('value4', parser.get('section2', 'option4'))

    def test__setattr__(self):
        self.assertEqual('value1', self.c.section1.option1)
        self.c.section1.option1 = 'value99'
        self.assertEqual('value99', self.c.section1.option1)

    def testadd_section(self):
        self.c.add_section('section3')
        self.assertTrue('section3' in self.c)
示例#12
0
 def __init__(self):
     ConfigParser.__init__(self)
     self.add_section('pymontecarlo')
示例#13
0
    def _update_version3(self, filepath):
        logging.debug('Updating from "version 3"')

        manager = {}

        def _load_photonintensity(zipfile, key):
            fp = zipfile.open(key + '.csv', 'r')
            reader = csv.reader(StringIO(fp.read().decode('ascii')))
            next(reader)

            intensities = {}
            for row in reader:
                transition = from_string(row[0])
                # skip row[1] (energy)

                intensities[PhotonKey(transition, False, PhotonKey.P)] = \
                    (float(row[6]), float(row[7]))
                intensities[PhotonKey(transition, False, PhotonKey.C)] = \
                    (float(row[2]), float(row[3]))
                intensities[PhotonKey(transition, False, PhotonKey.B)] = \
                    (float(row[4]), float(row[5]))
                intensities[PhotonKey(transition, False, PhotonKey.T)] = \
                    (float(row[8]), float(row[9]))
                intensities[PhotonKey(transition, True, PhotonKey.P)] = \
                    (float(row[14]), float(row[15]))
                intensities[PhotonKey(transition, True, PhotonKey.C)] = \
                    (float(row[10]), float(row[11]))
                intensities[PhotonKey(transition, True, PhotonKey.B)] = \
                    (float(row[12]), float(row[13]))
                intensities[PhotonKey(transition, True, PhotonKey.T)] = \
                    (float(row[16]), float(row[17]))

            return PhotonIntensityResult(intensities)

        manager['PhotonIntensityResult'] = _load_photonintensity

        def _load_photonspectrum(zipfile, key):
            fp = zipfile.open(key + '.csv', 'r')
            reader = csv.reader(StringIO(fp.read().decode('ascii')))
            next(reader)

            energies_eV = []
            total_val = []
            total_unc = []
            background_val = []
            background_unc = []
            for row in reader:
                energies_eV.append(float(row[0]))
                total_val.append(float(row[1]))
                total_unc.append(float(row[2]))
                background_val.append(float(row[3]))
                background_unc.append(float(row[4]))

            total = np.array([energies_eV, total_val, total_unc]).T
            background = np.array([energies_eV, background_val, background_unc]).T

            return PhotonSpectrumResult(total, background)

        manager['PhotonSpectrumResult'] = _load_photonspectrum

        def _load_phirhoz(zipfile, key):
            # Find all phi-rho-z files
            arcnames = [name for name in zipfile.namelist() if name.startswith(key)]

            # Read files
            distributions = {}
            for arcname in arcnames:
                parts = os.path.splitext(arcname)[0].split('+')
                transition = from_string(parts[-2].replace('_', ' '))
                suffix = parts[-1]

                absorption = suffix.startswith('e')
                if suffix[1:] == 'nf':
                    flag = PhotonKey.PRIMARY
                elif suffix[1:] == 'cf':
                    flag = PhotonKey.CHARACTERISTIC_FLUORESCENCE
                elif suffix[1:] == 'bf':
                    flag = PhotonKey.BREMSSTRAHLUNG_FLUORESCENCE
                elif suffix[1:] == 't':
                    flag = PhotonKey.TOTAL
                elif suffix[1:] == 'f':
                    flag = PhotonKey.FLUORESCENCE
                key = PhotonKey(transition, absorption, flag)

                fp = zipfile.open(arcname, 'r')
                reader = csv.reader(StringIO(fp.read().decode('ascii')))
                next(reader)

                zs = []
                values = []
                uncs = []
                for row in reader:
                    zs.append(float(row[0]))
                    values.append(float(row[1]))
                    uncs.append(float(row[2]))

                datum = np.array([zs, values, uncs]).T
                distributions[key] = datum

            return PhotonDepthResult(distributions)

        manager['PhiRhoZResult'] = _load_phirhoz

        def _load_time(zipfile, key):
            element = xmlutil.parse(zipfile.open(key + '.xml', 'r'))

            child = element.find('time')
            if child is not None:
                simulation_time = float(child.get('val', 0.0))
            else:
                simulation_time = 0.0

            child = element.find('speed')
            if child is not None:
                simulation_speed = \
                    float(child.get('val', 0.0)), float(child.get('unc', 0.0))
            else:
                simulation_speed = (0.0, 0.0)

            return TimeResult(simulation_time, simulation_speed)

        manager['TimeResult'] = _load_time

        def _load_showersstatistics(zipfile, key):
            element = xmlutil.parse(zipfile.open(key + '.xml', 'r').read())

            child = element.find('showers')
            if child is not None:
                showers = float(child.get('val', 0))
            else:
                showers = 0

            return ShowersStatisticsResult(showers)

        manager['ShowersStatisticsResult'] = _load_showersstatistics

        def _load_electronfraction(zipfile, key):
            element = xmlutil.parse(zipfile.open(key + '.xml', 'r'))

            child = element.find('absorbed')
            if child is not None:
                absorbed = \
                    float(child.get('val', 0.0)), float(child.get('unc', 0.0))
            else:
                absorbed = (0.0, 0.0)

            child = element.find('backscattered')
            if child is not None:
                backscattered = \
                    float(child.get('val', 0.0)), float(child.get('unc', 0.0))
            else:
                backscattered = (0.0, 0.0)

            child = element.find('transmitted')
            if child is not None:
                transmitted = \
                    float(child.get('val', 0.0)), float(child.get('unc', 0.0))
            else:
                transmitted = (0.0, 0.0)

            return ElectronFractionResult(absorbed, backscattered, transmitted)

        manager['ElectronFractionResult'] = _load_electronfraction

        def _load_trajectory(zipfile, key):
            tmpdir = tempfile.mkdtemp()
            filename = key + '.h5'
            zipfile.extract(filename, tmpdir)

            hdf5file = h5py.File(os.path.join(tmpdir, filename))

            particles_ref = list(PARTICLES)
            particles_ref = dict(zip(map(str, particles_ref), particles_ref))

            collisions_ref = list(COLLISIONS)
            collisions_ref = dict(zip(map(str, collisions_ref), collisions_ref))

            trajectories = []

            for dataset in hdf5file['trajectories'].values():
                primary = bool(dataset.attrs['primary'])
                particle = particles_ref.get(dataset.attrs['particle'].decode('ascii'))
                collision = collisions_ref.get(dataset.attrs['collision'].decode('ascii'))
                exit_state = int(dataset.attrs['exit_state'])
                interactions = dataset[:]

                trajectory = Trajectory(primary, particle, collision,
                                        exit_state, interactions)
                trajectories.append(trajectory)

            hdf5file.close()
            shutil.rmtree(tmpdir, ignore_errors=True)

            return TrajectoryResult(trajectories)

        manager['TrajectoryResult'] = _load_trajectory

        def _load_backscatteredelectronenergy(zipfile, key):
            data = np.loadtxt(zipfile.open(key + '.csv', 'r'), delimiter=',')
            return BackscatteredElectronEnergyResult(data)

        manager['BackscatteredElectronEnergyResult'] = _load_backscatteredelectronenergy

        def _load_transmittedelectronenergy(zipfile, key):
            data = np.loadtxt(zipfile.open(key + '.csv', 'r'), delimiter=',')
            return TransmittedElectronEnergyResult(data)

        manager['TransmittedElectronEnergyResult'] = _load_transmittedelectronenergy

        # Create HDF5
        newfilepath = os.path.splitext(filepath)[0] + '.h5'
        hdf5file = h5py.File(newfilepath, 'w')
        hdf5file.attrs['version'] = b'4'

        zipfile = ZipFile(filepath, 'r', allowZip64=True)

        ## Read options
        try:
            zipinfo = zipfile.getinfo(OPTIONS_FILENAME)
        except KeyError:
            raise IOError("Zip file (%s) does not contain a %s" % \
                          (filepath, OPTIONS_FILENAME))
        with zipfile.open(zipinfo, 'r') as fp:
            source = fp.read()
        source = _update_options(source)
        hdf5file.attrs['options'] = source

        ## Parse keys.ini
        try:
            zipinfo = zipfile.getinfo(KEYS_INI_FILENAME)
        except KeyError:
            raise IOError("Zip file (%s) does not contain a %s" % \
                          (filepath, KEYS_INI_FILENAME))

        config = ConfigParser()
        config.read(StringIO(zipfile.open(zipinfo, 'r').read().decode('ascii')))

        ## Load each results
        items = list(getattr(config, SECTION_KEYS))

        for key, tag in items:
            loader = manager[tag]
            result = loader(zipfile, key)

            handler = find_convert_handler('pymontecarlo.fileformat.results.result', result)

            group = hdf5file.create_group(key)
            handler.convert(result, group)

        zipfile.close()
        hdf5file.close()

        # Create raw ZIP
        oldzip = ZipFile(filepath, 'r')

        if any([filename.startswith('raw/') for filename in oldzip.namelist()]):
            zipfilepath = os.path.splitext(filepath)[0] + '_raw.zip'
            newzip = ZipFile(zipfilepath, 'w', compression=ZIP_DEFLATED)

            for filename in oldzip.namelist():
                if not filename.startswith('raw/'): continue
                data = oldzip.read(filename)
                newzip.writestr(filename[4:], data)

            newzip.close()

        oldzip.close()

        return self._update_version4(newfilepath)