Skip to content

Commit

Permalink
Applied PEP8
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Nov 10, 2024
1 parent 132be34 commit e5ac4d4
Showing 1 changed file with 61 additions and 35 deletions.
96 changes: 61 additions & 35 deletions openmc/tallies.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
_FILTER_CLASSES = (openmc.Filter, openmc.CrossFilter, openmc.AggregateFilter)

# Valid types of estimators
ESTIMATOR_TYPES = ['tracklength', 'collision', 'analog' , 'point']
ESTIMATOR_TYPES = ['tracklength', 'collision', 'analog', 'point']


class Tally(IDManagerMixin):
Expand Down Expand Up @@ -136,14 +136,16 @@ def __repr__(self):
parts.append('{: <15}=\t{}'.format('ID', self.id))
parts.append('{: <15}=\t{}'.format('Name', self.name))
if self.derivative is not None:
parts.append('{: <15}=\t{}'.format('Derivative ID', self.derivative.id))
parts.append('{: <15}=\t{}'.format(
'Derivative ID', self.derivative.id))
filters = ', '.join(type(f).__name__ for f in self.filters)
parts.append('{: <15}=\t{}'.format('Filters', filters))
nuclides = ' '.join(str(nuclide) for nuclide in self.nuclides)
parts.append('{: <15}=\t{}'.format('Nuclides', nuclides))
parts.append('{: <15}=\t{}'.format('Scores', self.scores))
parts.append('{: <15}=\t{}'.format('Estimator', self.estimator))
parts.append('{: <15}=\t{}'.format('Multiply dens.', self.multiply_density))
parts.append('{: <15}=\t{}'.format(
'Multiply dens.', self.multiply_density))
if self.positions is not None:
parts.append('{: <15}=\t{}'.format('positions', self.positions))
return '\n\t'.join(parts)
Expand All @@ -159,8 +161,8 @@ def name(self, name):

@property
def positions(self):
return self._positions
return self._positions

@positions.setter
def positions(self, positions):
self._positions = positions
Expand Down Expand Up @@ -192,7 +194,8 @@ def filters(self, filters):
raise ValueError(msg)
visited_filters.add(f)

self._filters = cv.CheckedList(_FILTER_CLASSES, 'tally filters', filters)
self._filters = cv.CheckedList(
_FILTER_CLASSES, 'tally filters', filters)

@property
def nuclides(self):
Expand Down Expand Up @@ -242,7 +245,7 @@ def scores(self, scores):
# Check to see if scores are deprecated before storing
for deprecated in ['scatter-', 'nu-scatter-', 'scatter-p',
'nu-scatter-p', 'scatter-y', 'nu-scatter-y',
'flux-y', 'total-y']:
'flux-y', 'total-y']:
if score.strip().startswith(deprecated):
msg = score.strip() + ' is no longer supported.'
raise ValueError(msg)
Expand Down Expand Up @@ -296,7 +299,8 @@ def num_realizations(self):
@num_realizations.setter
def num_realizations(self, num_realizations):
cv.check_type('number of realizations', num_realizations, Integral)
cv.check_greater_than('number of realizations', num_realizations, 0, True)
cv.check_greater_than('number of realizations',
num_realizations, 0, True)
self._num_realizations = num_realizations

@property
Expand Down Expand Up @@ -329,8 +333,10 @@ def _read_results(self):

# Convert NumPy arrays to SciPy sparse LIL matrices
if self.sparse:
self._sum = sps.lil_matrix(self._sum.flatten(), self._sum.shape)
self._sum_sq = sps.lil_matrix(self._sum_sq.flatten(), self._sum_sq.shape)
self._sum = sps.lil_matrix(
self._sum.flatten(), self._sum.shape)
self._sum_sq = sps.lil_matrix(
self._sum_sq.flatten(), self._sum_sq.shape)

# Indicate that Tally results have been read
self._results_read = True
Expand Down Expand Up @@ -457,7 +463,8 @@ def sparse(self, sparse):
# Convert NumPy arrays to SciPy sparse LIL matrices
if sparse and not self.sparse:
if self._sum is not None:
self._sum = sps.lil_matrix(self._sum.flatten(), self._sum.shape)
self._sum = sps.lil_matrix(
self._sum.flatten(), self._sum.shape)
if self._sum_sq is not None:
self._sum_sq = sps.lil_matrix(self._sum_sq.flatten(),
self._sum_sq.shape)
Expand Down Expand Up @@ -681,7 +688,8 @@ def can_merge(self, other):
can_merge_filters = self._can_merge_filters(other)
can_merge_nuclides = self._can_merge_nuclides(other)
can_merge_scores = self._can_merge_scores(other)
mergeability = [can_merge_filters, can_merge_nuclides, can_merge_scores]
mergeability = [can_merge_filters,
can_merge_nuclides, can_merge_scores]

if not all(mergeability):
return False
Expand Down Expand Up @@ -737,7 +745,8 @@ def merge(self, other):
for i, filter1 in enumerate(self.filters):
for filter2 in other.filters:
if filter1 != filter2 and filter1.can_merge(filter2):
other_copy._swap_filters(other_copy.filters[i], filter2)
other_copy._swap_filters(
other_copy.filters[i], filter2)
merged_tally.filters[i] = filter1.merge(filter2)
join_right = filter1 < filter2
merge_axis = i
Expand Down Expand Up @@ -800,7 +809,8 @@ def merge(self, other):
merged_sum_sq = np.concatenate((other_sum_sq, self_sum_sq),
axis=merge_axis)

merged_tally._sum_sq = np.reshape(merged_sum_sq, merged_tally.shape)
merged_tally._sum_sq = np.reshape(
merged_sum_sq, merged_tally.shape)

# Concatenate mean arrays if present in both tallies
if self.mean is not None and other.mean is not None:
Expand Down Expand Up @@ -828,7 +838,8 @@ def merge(self, other):
merged_std_dev = np.concatenate((other_std_dev, self_std_dev),
axis=merge_axis)

merged_tally._std_dev = np.reshape(merged_std_dev, merged_tally.shape)
merged_tally._std_dev = np.reshape(
merged_std_dev, merged_tally.shape)

# Sparsify merged tally if both tallies are sparse
merged_tally.sparse = self.sparse and other.sparse
Expand Down Expand Up @@ -1134,7 +1145,8 @@ def get_filter_indices(self, filters=[], filter_bins=[]):
for j, test_filter in enumerate(filters):
if type(self_filter) is test_filter:
bins = filter_bins[j]
indices = np.array([self_filter.get_bin_index(b) for b in bins])
indices = np.array(
[self_filter.get_bin_index(b) for b in bins])
break
else:
indices = np.arange(self_filter.num_bins)
Expand Down Expand Up @@ -1641,17 +1653,19 @@ def hybrid_product(self, other, binary_op, filter_product=None,
elif binary_op == '*':
with np.errstate(divide='ignore', invalid='ignore'):
self_rel_err = data['self']['std. dev.'] / data['self']['mean']
other_rel_err = data['other']['std. dev.'] / data['other']['mean']
other_rel_err = data['other']['std. dev.'] / \
data['other']['mean']
new_tally._mean = data['self']['mean'] * data['other']['mean']
new_tally._std_dev = np.abs(new_tally.mean) * \
np.sqrt(self_rel_err**2 + other_rel_err**2)
np.sqrt(self_rel_err**2 + other_rel_err**2)
elif binary_op == '/':
with np.errstate(divide='ignore', invalid='ignore'):
self_rel_err = data['self']['std. dev.'] / data['self']['mean']
other_rel_err = data['other']['std. dev.'] / data['other']['mean']
other_rel_err = data['other']['std. dev.'] / \
data['other']['mean']
new_tally._mean = data['self']['mean'] / data['other']['mean']
new_tally._std_dev = np.abs(new_tally.mean) * \
np.sqrt(self_rel_err**2 + other_rel_err**2)
np.sqrt(self_rel_err**2 + other_rel_err**2)
elif binary_op == '^':
with np.errstate(divide='ignore', invalid='ignore'):
mean_ratio = data['other']['mean'] / data['self']['mean']
Expand All @@ -1660,7 +1674,7 @@ def hybrid_product(self, other, binary_op, filter_product=None,
np.log(data['self']['mean']) * data['other']['std. dev.']
new_tally._mean = data['self']['mean'] ** data['other']['mean']
new_tally._std_dev = np.abs(new_tally.mean) * \
np.sqrt(first_term**2 + second_term**2)
np.sqrt(first_term**2 + second_term**2)

# Convert any infs and nans to zero
new_tally._mean[np.isinf(new_tally._mean)] = 0
Expand Down Expand Up @@ -1774,14 +1788,16 @@ def _align_tally_data(self, other, filter_product, nuclide_product,
for other_filter in other_missing_filters:
filter_copy = copy.deepcopy(other_filter)
other._mean = np.repeat(other.mean, filter_copy.num_bins, axis=0)
other._std_dev = np.repeat(other.std_dev, filter_copy.num_bins, axis=0)
other._std_dev = np.repeat(
other.std_dev, filter_copy.num_bins, axis=0)
other.filters.append(filter_copy)

# Add filters present in other but not in self to self
for self_filter in self_missing_filters:
filter_copy = copy.deepcopy(self_filter)
self._mean = np.repeat(self.mean, filter_copy.num_bins, axis=0)
self._std_dev = np.repeat(self.std_dev, filter_copy.num_bins, axis=0)
self._std_dev = np.repeat(
self.std_dev, filter_copy.num_bins, axis=0)
self.filters.append(filter_copy)

# Align other filters with self filters
Expand Down Expand Up @@ -1811,7 +1827,8 @@ def _align_tally_data(self, other, filter_product, nuclide_product,

# Add nuclides present in self but not in other to other
for nuclide in other_missing_nuclides:
other._mean = np.insert(other.mean, other.num_nuclides, 0, axis=1)
other._mean = np.insert(
other.mean, other.num_nuclides, 0, axis=1)
other._std_dev = np.insert(other.std_dev, other.num_nuclides, 0,
axis=1)
other.nuclides.append(nuclide)
Expand Down Expand Up @@ -1850,14 +1867,17 @@ def _align_tally_data(self, other, filter_product, nuclide_product,

# Add scores present in self but not in other to other
for score in other_missing_scores:
other._mean = np.insert(other.mean, other.num_scores, 0, axis=2)
other._std_dev = np.insert(other.std_dev, other.num_scores, 0, axis=2)
other._mean = np.insert(
other.mean, other.num_scores, 0, axis=2)
other._std_dev = np.insert(
other.std_dev, other.num_scores, 0, axis=2)
other.scores.append(score)

# Add scores present in other but not in self to self
for score in self_missing_scores:
self._mean = np.insert(self.mean, self.num_scores, 0, axis=2)
self._std_dev = np.insert(self.std_dev, self.num_scores, 0, axis=2)
self._std_dev = np.insert(
self.std_dev, self.num_scores, 0, axis=2)
self.scores.append(score)

# Align other scores with self scores
Expand Down Expand Up @@ -2238,7 +2258,8 @@ def __sub__(self, other):
new_tally.sparse = self.sparse

else:
msg = 'Unable to subtract "{}" from Tally ID="{}"'.format(other, self.id)
msg = 'Unable to subtract "{}" from Tally ID="{}"'.format(
other, self.id)
raise ValueError(msg)

return new_tally
Expand Down Expand Up @@ -2309,7 +2330,8 @@ def __mul__(self, other):
new_tally.sparse = self.sparse

else:
msg = 'Unable to multiply Tally ID="{}" by "{}"'.format(self.id, other)
msg = 'Unable to multiply Tally ID="{}" by "{}"'.format(
self.id, other)
raise ValueError(msg)

return new_tally
Expand Down Expand Up @@ -2380,7 +2402,8 @@ def __truediv__(self, other):
new_tally.sparse = self.sparse

else:
msg = 'Unable to divide Tally ID="{}" by "{}"'.format(self.id, other)
msg = 'Unable to divide Tally ID="{}" by "{}"'.format(
self.id, other)
raise ValueError(msg)

return new_tally
Expand Down Expand Up @@ -2455,7 +2478,8 @@ def __pow__(self, power):
new_tally.sparse = self.sparse

else:
msg = 'Unable to raise Tally ID="{}" to power "{}"'.format(self.id, power)
msg = 'Unable to raise Tally ID="{}" to power "{}"'.format(
self.id, power)
raise ValueError(msg)

return new_tally
Expand Down Expand Up @@ -2800,7 +2824,7 @@ def summation(self, scores=[], filter_type=None,
# Add AggregateFilter to the tally sum
if not remove_filter:
filter_sum = openmc.AggregateFilter(self_filter,
[tuple(filter_bins)], 'sum')
[tuple(filter_bins)], 'sum')
tally_sum.filters.append(filter_sum)

# Add a copy of each filter not summed across to the tally sum
Expand All @@ -2813,7 +2837,8 @@ def summation(self, scores=[], filter_type=None,

# Sum across any nuclides specified by the user
if len(nuclides) != 0:
nuclide_bins = [self.get_nuclide_index(nuclide) for nuclide in nuclides]
nuclide_bins = [self.get_nuclide_index(
nuclide) for nuclide in nuclides]
axis_index = self.num_filters
mean = np.take(mean, indices=nuclide_bins, axis=axis_index)
std_dev = np.take(std_dev, indices=nuclide_bins, axis=axis_index)
Expand Down Expand Up @@ -2953,7 +2978,7 @@ def average(self, scores=[], filter_type=None,
# Add AggregateFilter to the tally avg
if not remove_filter:
filter_sum = openmc.AggregateFilter(self_filter,
[tuple(filter_bins)], 'avg')
[tuple(filter_bins)], 'avg')
tally_avg.filters.append(filter_sum)

# Add a copy of each filter not averaged across to the tally avg
Expand All @@ -2966,7 +2991,8 @@ def average(self, scores=[], filter_type=None,

# Sum across any nuclides specified by the user
if len(nuclides) != 0:
nuclide_bins = [self.get_nuclide_index(nuclide) for nuclide in nuclides]
nuclide_bins = [self.get_nuclide_index(
nuclide) for nuclide in nuclides]
axis_index = self.num_filters
mean = np.take(mean, indices=nuclide_bins, axis=axis_index)
std_dev = np.take(std_dev, indices=nuclide_bins, axis=axis_index)
Expand Down

0 comments on commit e5ac4d4

Please sign in to comment.