From 495556a2f5ae9a2c13372322c41bbfc4bfb048f7 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Fri, 15 Jul 2016 08:21:57 -0500 Subject: [PATCH] Cleanup openmc.data based on pylint --- openmc/data/ace.py | 65 +++++++++++++----------------- openmc/data/angle_distribution.py | 6 +-- openmc/data/angle_energy.py | 2 - openmc/data/container.py | 6 +-- openmc/data/correlated.py | 15 ++++--- openmc/data/data.py | 12 +++--- openmc/data/energy_distribution.py | 21 +++++++--- openmc/data/kalbach_mann.py | 9 +++-- openmc/data/library.py | 2 +- openmc/data/neutron.py | 22 ++++------ openmc/data/reaction.py | 6 +-- openmc/data/thermal.py | 11 +++-- openmc/data/urr.py | 6 +-- 13 files changed, 89 insertions(+), 94 deletions(-) diff --git a/openmc/data/ace.py b/openmc/data/ace.py index 2a39e208a..1a56c165e 100644 --- a/openmc/data/ace.py +++ b/openmc/data/ace.py @@ -20,7 +20,6 @@ import io from os import SEEK_CUR import struct import sys -from warnings import warn import numpy as np @@ -170,7 +169,7 @@ class Library(object): sb = b''.join([fh.readline() for i in range(10)]) # Try to decode it with ascii - sd = sb.decode('ascii') + sb.decode('ascii') # No exception so proceed with ASCII - reopen in non-binary fh.close() @@ -182,13 +181,13 @@ class Library(object): fh = open(filename, 'rb') self._read_binary(fh, table_names, verbose) - def _read_binary(self, fh, table_names, verbose=False, + def _read_binary(self, ace_file, table_names, verbose=False, recl_length=4096, entries=512): """Read a binary (Type 2) ACE table. Parameters ---------- - fh : file + ace_file : file Open ACE file table_names : None, str, or iterable Tables from the file to read in. If None, reads in all of the @@ -204,25 +203,25 @@ class Library(object): """ while True: - start_position = fh.tell() + start_position = ace_file.tell() # Check for end-of-file - if len(fh.read(1)) == 0: + if len(ace_file.read(1)) == 0: return - fh.seek(start_position) + ace_file.seek(start_position) # Read name, atomic mass ratio, temperature, date, comment, and # material name, atomic_weight_ratio, temperature, date, comment, mat = \ - struct.unpack(str('=10sdd10s70s10s'), fh.read(116)) + struct.unpack(str('=10sdd10s70s10s'), ace_file.read(116)) name = name.decode().strip() # Read ZAID/awr combinations - data = struct.unpack(str('=' + 16*'id'), fh.read(192)) + data = struct.unpack(str('=' + 16*'id'), ace_file.read(192)) pairs = list(zip(data[::2], data[1::2])) # Read NXS - nxs = list(struct.unpack(str('=16i'), fh.read(64))) + nxs = list(struct.unpack(str('=16i'), ace_file.read(64))) # Determine length of XSS and number of records length = nxs[0] @@ -230,20 +229,20 @@ class Library(object): # verify that we are supposed to read this table in if (table_names is not None) and (name not in table_names): - fh.seek(start_position + recl_length*(n_records + 1)) + ace_file.seek(start_position + recl_length*(n_records + 1)) continue if verbose: - temperature_in_K = round(temperature * 1e6 / 8.617342e-5) - print("Loading nuclide {0} at {1} K".format(name, temperature_in_K)) + kelvin = round(temperature * 1e6 / 8.617342e-5) + print("Loading nuclide {0} at {1} K".format(name, kelvin)) # Read JXS - jxs = list(struct.unpack(str('=32i'), fh.read(128))) + jxs = list(struct.unpack(str('=32i'), ace_file.read(128))) # Read XSS - fh.seek(start_position + recl_length) + ace_file.seek(start_position + recl_length) xss = list(struct.unpack(str('={0}d'.format(length)), - fh.read(length*8))) + ace_file.read(length*8))) # Insert zeros at beginning of NXS, JXS, and XSS arrays so that the # indexing will be the same as Fortran. This makes it easier to @@ -263,14 +262,14 @@ class Library(object): self.tables.append(table) # Advance to next record - fh.seek(start_position + recl_length*(n_records + 1)) + ace_file.seek(start_position + recl_length*(n_records + 1)) - def _read_ascii(self, fh, table_names, verbose=False): + def _read_ascii(self, ace_file, table_names, verbose=False): """Read an ASCII (Type 1) ACE table. Parameters ---------- - fh : file + ace_file : file Open ACE file table_names : None, str, or iterable Tables from the file to read in. If None, reads in all of the @@ -282,26 +281,23 @@ class Library(object): tables_seen = set() - lines = [fh.readline() for i in range(13)] + lines = [ace_file.readline() for i in range(13)] - while (0 != len(lines)) and (lines[0] != ''): + while len(lines) != 0 and lines[0] != '': # Read name of table, atomic mass ratio, and temperature. If first # line is empty, we are at end of file # check if it's a 2.0 style header if lines[0].split()[0][1] == '.': words = lines[0].split() - version = words[0] name = words[1] - if len(words) == 3: - source = words[2] words = lines[1].split() atomic_weight_ratio = float(words[0]) temperature = float(words[1]) commentlines = int(words[3]) for i in range(commentlines): lines.pop(0) - lines.append(fh.readline()) + lines.append(ace_file.readline()) else: words = lines[0].split() name = words[0] @@ -325,24 +321,21 @@ class Library(object): # verify that we are suppossed to read this table in if (table_names is not None) and (name not in table_names): - fh.seek(n_bytes, SEEK_CUR) - fh.readline() - lines = [fh.readline() for i in range(13)] + ace_file.seek(n_bytes, SEEK_CUR) + ace_file.readline() + lines = [ace_file.readline() for i in range(13)] continue # read and fix over-shoot - lines += fh.readlines(n_bytes) + lines += ace_file.readlines(n_bytes) if 12 + n_lines < len(lines): goback = sum([len(line) for line in lines[12+n_lines:]]) lines = lines[:12+n_lines] - fh.seek(-goback, SEEK_CUR) + ace_file.seek(-goback, SEEK_CUR) if verbose: - temperature_in_K = round(temperature * 1e6 / 8.617342e-5) - print("Loading nuclide {0} at {1} K".format(name, temperature_in_K)) - - # Read comment - comment = lines[1].strip() + kelvin = round(temperature * 1e6 / 8.617342e-5) + print("Loading nuclide {0} at {1} K".format(name, kelvin)) # Insert zeros at beginning of NXS, JXS, and XSS arrays so that the # indexing will be the same as Fortran. This makes it easier to @@ -358,7 +351,7 @@ class Library(object): self.tables.append(table) # Read all data blocks - lines = [fh.readline() for i in range(13)] + lines = [ace_file.readline() for i in range(13)] class Table(object): diff --git a/openmc/data/angle_distribution.py b/openmc/data/angle_distribution.py index 3f086a99c..709be8496 100644 --- a/openmc/data/angle_distribution.py +++ b/openmc/data/angle_distribution.py @@ -5,7 +5,7 @@ import numpy as np import openmc.checkvalue as cv from openmc.stats import Univariate, Tabular, Uniform -from .container import interpolation_scheme +from .container import INTERPOLATION_SCHEME class AngleDistribution(object): @@ -125,7 +125,7 @@ class AngleDistribution(object): else: n = data.shape[1] - j - interp = interpolation_scheme[interpolation[i]] + interp = INTERPOLATION_SCHEME[interpolation[i]] mu_i = Tabular(data[0, j:j+n], data[1, j:j+n], interp) mu_i.c = data[2, j:j+n] @@ -189,7 +189,7 @@ class AngleDistribution(object): data = ace.xss[idx + 2:idx + 2 + 3*n_points] data.shape = (3, n_points) - mu_i = Tabular(data[0], data[1], interpolation_scheme[intt]) + mu_i = Tabular(data[0], data[1], INTERPOLATION_SCHEME[intt]) mu_i.c = data[2] else: # Isotropic angular distribution diff --git a/openmc/data/angle_energy.py b/openmc/data/angle_energy.py index ff9f41a44..c20c5f0ff 100644 --- a/openmc/data/angle_energy.py +++ b/openmc/data/angle_energy.py @@ -1,7 +1,5 @@ from abc import ABCMeta, abstractmethod -import numpy as np - import openmc.data diff --git a/openmc/data/container.py b/openmc/data/container.py index b2b323ddd..d9902794c 100644 --- a/openmc/data/container.py +++ b/openmc/data/container.py @@ -5,7 +5,7 @@ import numpy as np import openmc.checkvalue as cv -interpolation_scheme = {1: 'histogram', 2: 'linear-linear', 3: 'linear-log', +INTERPOLATION_SCHEME = {1: 'histogram', 2: 'linear-linear', 3: 'linear-log', 4: 'log-linear', 5: 'log-log'} @@ -262,8 +262,8 @@ class Tabulated1D(object): Function read from dataset """ - x = dataset.value[0,:] - y = dataset.value[1,:] + x = dataset.value[0, :] + y = dataset.value[1, :] breakpoints = dataset.attrs['breakpoints'] interpolation = dataset.attrs['interpolation'] return cls(x, y, breakpoints, interpolation) diff --git a/openmc/data/correlated.py b/openmc/data/correlated.py index f82a28b75..c93a0d706 100644 --- a/openmc/data/correlated.py +++ b/openmc/data/correlated.py @@ -1,11 +1,12 @@ from collections import Iterable from numbers import Real, Integral +from warnings import warn import numpy as np import openmc.checkvalue as cv -from openmc.stats import Tabular, Univariate, Discrete, Mixture -from .container import interpolation_scheme +from openmc.stats import Tabular, Univariate, Discrete, Mixture, Uniform +from .container import INTERPOLATION_SCHEME from .angle_energy import AngleEnergy @@ -237,7 +238,7 @@ class CorrelatedAngleEnergy(AngleEnergy): # Create continuous distribution if m < n: - interp = interpolation_scheme[interpolation[i]] + interp = INTERPOLATION_SCHEME[interpolation[i]] x = dset_eout[0, offset_e+m:offset_e+n] p = dset_eout[1, offset_e+m:offset_e+n] @@ -275,7 +276,7 @@ class CorrelatedAngleEnergy(AngleEnergy): if interp_code == 0: mu_ij = Discrete(x, p) else: - mu_ij = Tabular(x, p, interpolation_scheme[interp_code], + mu_ij = Tabular(x, p, INTERPOLATION_SCHEME[interp_code], ignore_negative=True) mu_ij.c = c mu_i.append(mu_ij) @@ -285,8 +286,6 @@ class CorrelatedAngleEnergy(AngleEnergy): energy_out.append(eout_i) mu.append(mu_i) - j += n - return cls(energy_breakpoints, energy_interpolation, energy, energy_out, mu) @@ -357,7 +356,7 @@ class CorrelatedAngleEnergy(AngleEnergy): # Create continuous distribution eout_continuous = Tabular(data[0][n_discrete_lines:], data[1][n_discrete_lines:], - interpolation_scheme[intt], + INTERPOLATION_SCHEME[intt], ignore_negative=True) eout_continuous.c = data[2][n_discrete_lines:] @@ -390,7 +389,7 @@ class CorrelatedAngleEnergy(AngleEnergy): data = ace.xss[idx + 2:idx + 2 + 3*n_cosine] data.shape = (3, n_cosine) - mu_ij = Tabular(data[0], data[1], interpolation_scheme[intt]) + mu_ij = Tabular(data[0], data[1], INTERPOLATION_SCHEME[intt]) mu_ij.c = data[2] else: # Isotropic distribution diff --git a/openmc/data/data.py b/openmc/data/data.py index e5aa55285..42c92cff9 100644 --- a/openmc/data/data.py +++ b/openmc/data/data.py @@ -150,9 +150,9 @@ reaction_name = {1: '(n,total)', 2: '(n,elastic)', 4: '(n,level)', 5: '(n,misc)' 198: '(n,n3p)', 199: '(n,3n2pa)', 200: '(n,5n2p)', 444: '(n,damage)', 649: '(n,pc)', 699: '(n,dc)', 749: '(n,tc)', 799: '(n,3Hec)', 849: '(n,ac)'} -reaction_name.update({i: '(n,n{})'.format(i-50) for i in range(50,91)}) -reaction_name.update({i: '(n,p{})'.format(i-600) for i in range(600,649)}) -reaction_name.update({i: '(n,d{})'.format(i-650) for i in range(650,699)}) -reaction_name.update({i: '(n,t{})'.format(i-700) for i in range(700,749)}) -reaction_name.update({i: '(n,3He{})'.format(i-750) for i in range(750,799)}) -reaction_name.update({i: '(n,a{})'.format(i-800) for i in range(800,849)}) +reaction_name.update({i: '(n,n{})'.format(i-50) for i in range(50, 91)}) +reaction_name.update({i: '(n,p{})'.format(i-600) for i in range(600, 649)}) +reaction_name.update({i: '(n,d{})'.format(i-650) for i in range(650, 699)}) +reaction_name.update({i: '(n,t{})'.format(i-700) for i in range(700, 749)}) +reaction_name.update({i: '(n,3He{})'.format(i-750) for i in range(750, 799)}) +reaction_name.update({i: '(n,a{})'.format(i-800) for i in range(800, 849)}) diff --git a/openmc/data/energy_distribution.py b/openmc/data/energy_distribution.py index 09b0c6ebf..a82a6c64a 100644 --- a/openmc/data/energy_distribution.py +++ b/openmc/data/energy_distribution.py @@ -3,10 +3,9 @@ from collections import Iterable from numbers import Integral, Real from warnings import warn -import h5py import numpy as np -from openmc.data.container import Tabulated1D, interpolation_scheme +from openmc.data.container import Tabulated1D, INTERPOLATION_SCHEME from openmc.stats.univariate import Univariate, Tabular, Discrete, Mixture import openmc.checkvalue as cv @@ -78,11 +77,12 @@ class ArbitraryTabulated(EnergyDistribution): """ def __init__(self, energy, pdf): + super(ArbitraryTabulated, self).__init__() self.energy = energy self.pdf = pdf def to_hdf5(self, group): - NotImplementedError + raise NotImplementedError class GeneralEvaporation(EnergyDistribution): @@ -114,6 +114,7 @@ class GeneralEvaporation(EnergyDistribution): """ def __init__(self, theta, g, u): + super(GeneralEvaporation, self).__init__() self.theta = theta self.g = g self.u = u @@ -121,6 +122,10 @@ class GeneralEvaporation(EnergyDistribution): def to_hdf5(self, group): raise NotImplementedError + @classmethod + def from_ace(cls, ace, idx=0): + raise NotImplementedError + class MaxwellEnergy(EnergyDistribution): r"""Simple Maxwellian fission spectrum represented as @@ -147,6 +152,7 @@ class MaxwellEnergy(EnergyDistribution): """ def __init__(self, theta, u): + super(MaxwellEnergy, self).__init__() self.theta = theta self.u = u @@ -254,6 +260,7 @@ class Evaporation(EnergyDistribution): """ def __init__(self, theta, u): + super(Evaporation, self).__init__() self.theta = theta self.u = u @@ -364,6 +371,7 @@ class WattEnergy(EnergyDistribution): """ def __init__(self, a, b, u): + super(WattEnergy, self).__init__() self.a = a self.b = b self.u = u @@ -504,6 +512,7 @@ class MadlandNix(EnergyDistribution): """ def __init__(self, efl, efh, tm): + super(MadlandNix, self).__init__() self.efl = efl self.efh = efh self.tm = tm @@ -966,7 +975,7 @@ class ContinuousTabular(EnergyDistribution): # Create continuous distribution if m < n: - interp = interpolation_scheme[interpolation[i]] + interp = INTERPOLATION_SCHEME[interpolation[i]] eout_continuous = Tabular(data[0, j+m:j+n], data[1, j+m:j+n], interp) eout_continuous.c = data[2, j+m:j+n] @@ -1048,8 +1057,8 @@ class ContinuousTabular(EnergyDistribution): # Create continuous distribution eout_continuous = Tabular(data[0][n_discrete_lines:], - data[1][n_discrete_lines:], - interpolation_scheme[intt]) + data[1][n_discrete_lines:], + INTERPOLATION_SCHEME[intt]) eout_continuous.c = data[2][n_discrete_lines:] # If discrete lines are present, create a mixture distribution diff --git a/openmc/data/kalbach_mann.py b/openmc/data/kalbach_mann.py index 7899ae2a6..8799831bf 100644 --- a/openmc/data/kalbach_mann.py +++ b/openmc/data/kalbach_mann.py @@ -1,11 +1,12 @@ from collections import Iterable from numbers import Real, Integral +from warnings import warn import numpy as np import openmc.checkvalue as cv from openmc.stats import Tabular, Univariate, Discrete, Mixture -from .container import Tabulated1D, interpolation_scheme +from .container import Tabulated1D, INTERPOLATION_SCHEME from .angle_energy import AngleEnergy @@ -228,7 +229,7 @@ class KalbachMann(AngleEnergy): # Create continuous distribution if m < n: - interp = interpolation_scheme[interpolation[i]] + interp = INTERPOLATION_SCHEME[interpolation[i]] eout_continuous = Tabular(data[0, j+m:j+n], data[1, j+m:j+n], interp) eout_continuous.c = data[2, j+m:j+n] @@ -318,8 +319,8 @@ class KalbachMann(AngleEnergy): # Create continuous distribution eout_continuous = Tabular(data[0][n_discrete_lines:], - data[1][n_discrete_lines:], - interpolation_scheme[intt]) + data[1][n_discrete_lines:], + INTERPOLATION_SCHEME[intt]) eout_continuous.c = data[2][n_discrete_lines:] # If discrete lines are present, create a mixture distribution diff --git a/openmc/data/library.py b/openmc/data/library.py index 58f32609e..748a01889 100644 --- a/openmc/data/library.py +++ b/openmc/data/library.py @@ -13,7 +13,7 @@ class DataLibrary(object): h5file = h5py.File(filename, 'r') materials = [] - for name, group in h5file.items(): + for name in h5file: materials.append(name) library = {'path': filename, 'type': filetype, 'materials': materials} diff --git a/openmc/data/neutron.py b/openmc/data/neutron.py index ad5e370f1..463aa26bb 100644 --- a/openmc/data/neutron.py +++ b/openmc/data/neutron.py @@ -1,25 +1,17 @@ from __future__ import division, unicode_literals -import io import sys -from warnings import warn from collections import OrderedDict, Iterable, Mapping -from copy import deepcopy from numbers import Integral, Real -import sys import numpy as np -from numpy.polynomial import Polynomial import h5py -from . import atomic_number, atomic_symbol +from . import atomic_symbol from .ace import Table, get_table from .container import Tabulated1D -from .energy_distribution import * from .product import Product from .reaction import Reaction, _get_photon_products -from .thermal import CoherentElastic from .urr import ProbabilityTables -from openmc.stats import Tabular, Discrete, Uniform, Mixture import openmc.checkvalue as cv if sys.version_info[0] >= 3: @@ -243,7 +235,7 @@ class IncidentNeutron(object): f.close() @classmethod - def from_hdf5(self, group_or_filename): + def from_hdf5(cls, group_or_filename): """Generate continuous-energy neutron interaction data from HDF5 group Parameters @@ -255,7 +247,7 @@ class IncidentNeutron(object): Returns ------- - openmc.data.ace.IncidentNeutron + openmc.data.IncidentNeutron Continuous-energy neutron interaction data """ @@ -272,8 +264,8 @@ class IncidentNeutron(object): atomic_weight_ratio = group.attrs['atomic_weight_ratio'] temperature = group.attrs['temperature'] - data = IncidentNeutron(name, atomic_number, mass_number, metastable, - atomic_weight_ratio, temperature) + data = cls(name, atomic_number, mass_number, metastable, + atomic_weight_ratio, temperature) # Read energy grid data.energy = group['energy'].value @@ -362,8 +354,8 @@ class IncidentNeutron(object): else: name = '{}{}.{}'.format(element, mass_number, xs) - data = IncidentNeutron(name, Z, mass_number, metastable, - ace.atomic_weight_ratio, ace.temperature) + data = cls(name, Z, mass_number, metastable, + ace.atomic_weight_ratio, ace.temperature) # Read energy grid n_energy = ace.nxs[3] diff --git a/openmc/data/reaction.py b/openmc/data/reaction.py index 40e0910ba..195dcc341 100644 --- a/openmc/data/reaction.py +++ b/openmc/data/reaction.py @@ -103,7 +103,7 @@ def _get_fission_products(ace): idx = ace.jxs[25] n_group = ace.nxs[8] total_group_probability = 0. - for i, group in enumerate(range(n_group)): + for group in range(n_group): delayed_neutron = Product('neutron') delayed_neutron.emission_mode = 'delayed' delayed_neutron.decay_rate = ace.xss[idx] @@ -201,14 +201,14 @@ def _get_photon_products(ace, mt): n_energy = int(ace.xss[idx + 1]) photon._xs = ace.xss[idx + 2:idx + 2 + n_energy] - # Determine yield based on ratio of cross sections + # TODO: Determine yield based on ratio of cross sections energy = ace.xss[ace.jxs[1] + threshold_idx: ace.jxs[1] + threshold_idx + n_energy] photon.yield_ = Tabulated1D(energy, photon._xs) else: raise ValueError("MFTYPE must be 12, 13, 16. Got {0}".format( - mftype)) + mftype)) # ================================================================== # Photon energy distribution diff --git a/openmc/data/thermal.py b/openmc/data/thermal.py index 63e573a01..71ef190ab 100644 --- a/openmc/data/thermal.py +++ b/openmc/data/thermal.py @@ -8,7 +8,10 @@ import h5py import openmc.checkvalue as cv from .ace import Table, get_table +from .angle_energy import AngleEnergy from .container import Tabulated1D +from .correlated import CorrelatedAngleEnergy +from openmc.stats import Discrete, Tabular _THERMAL_NAMES = {'al': 'c_Al27', 'al27': 'c_Al27', @@ -37,7 +40,7 @@ _THERMAL_NAMES = {'al': 'c_Al27', 'al27': 'c_Al27', class CoherentElastic(object): - """Coherent elastic scattering data from a crystalline material + r"""Coherent elastic scattering data from a crystalline material Parameters ---------- @@ -212,7 +215,7 @@ class ThermalScattering(object): self.inelastic_dist.to_hdf5(inelastic_group) @classmethod - def from_hdf5(self, group): + def from_hdf5(cls, group): """Generate thermal scattering data from HDF5 group Parameters @@ -229,7 +232,7 @@ class ThermalScattering(object): name = group.name[1:] atomic_weight_ratio = group.attrs['atomic_weight_ratio'] temperature = group.attrs['temperature'] - table = ThermalScattering(name, atomic_weight_ratio, temperature) + table = cls(name, atomic_weight_ratio, temperature) table.zaids = group.attrs['zaids'] # Read thermal elastic scattering @@ -363,7 +366,7 @@ class ThermalScattering(object): # Create correlated angle-energy distribution breakpoints = [n_energy] interpolation = [2] - energy = inelastic_xs.x + energy = table.inelastic_xs.x table.inelastic_dist = CorrelatedAngleEnergy( breakpoints, interpolation, energy, energy_out, mu_out) diff --git a/openmc/data/urr.py b/openmc/data/urr.py index b010a6c47..052da6612 100644 --- a/openmc/data/urr.py +++ b/openmc/data/urr.py @@ -7,7 +7,7 @@ import openmc.checkvalue as cv class ProbabilityTables(object): - """Unresolved resonance region probability tables. + r"""Unresolved resonance region probability tables. Parameters ---------- @@ -18,7 +18,7 @@ class ProbabilityTables(object): where N is the number of energies and M is the number of bands. The second dimension indicates whether the value is for the cumulative probability (0), total (1), elastic (2), fission (3), :math:`(n,\gamma)` - (4), or heating number (6). + (4), or heating number (5). interpolation : {2, 5} Interpolation scheme between tables inelastic_flag : int @@ -45,7 +45,7 @@ class ProbabilityTables(object): where N is the number of energies and M is the number of bands. The second dimension indicates whether the value is for the cumulative probability (0), total (1), elastic (2), fission (3), :math:`(n,\gamma)` - (4), or heating number (6). + (4), or heating number (5). interpolation : {2, 5} Interpolation scheme between tables inelastic_flag : int