def __init__(self, files, parser): if not issubclass(parser, BaseReader): raise SamplerError( "parser argument must be a subclass of BaseReader, not " "{}".format(parser.__name__)) self.settings = rc.getReaderSettings('sampler') self.files = extendFiles(files) if not self.files: raise SamplerError("No files stored on Sampler.") self.__parserCls = parser self.parsers = set() self.map = {} self.read()
def loadFromContainer(self, container): """ Copy data from a similar container. Parameters ---------- container: Incoming container from which to take data. Raises ------ serpentTools.messages.MismatchedContainersError If the incoming container is not the expected type declared at construction serpentTools.messages.SamplerError If more containers have been loaded than specified at construction """ if not isinstance(container, self.__expectedClass): raise MismatchedContainersError( 'Sampled container expects {} type containers, not ' '{}'.format(self.__expectedClass, type(container))) if self._index == self.N: name = self.name if hasattr(self, 'name') else '' otherName = container.__class__.__name__ msg = ("{} {} has already loaded {} {} objects and cannot exceed " "this bound ".format(self.__class__.__name__, name, self.N, otherName)) raise SamplerError(msg) self._loadFromContainer(container) self._index += 1
def _finalize(self): if self.allTallies is None: raise SamplerError( "Detector data has not been loaded and cannot be processed") N = self._index self.tallies = self.allTallies[:N].mean(axis=0) self.__computeErrors(N) self.scores = (self.allScores[:N].mean( axis=0) if self.allScores is not None else None) self._map = {'tallies': self.tallies, 'errors': self.errors, 'scores': self.scores}
def spreadPlot(self, xUnits, yUnits, isotope=None, zai=None, sampleKwargs=None, meanKwargs=None, timePoints=None, ax=None, xlabel=None, ylabel=None, logx=False, logy=False, loglog=False, legend=True): """ Plot the mean quantity and data from all sampled files. Parameters ---------- xUnits : str name of x value to obtain, e.g. ``'days'``, ``'burnup'`` yUnits : str name of y value to return, e.g. ``'adens'``, ``'burnup'`` isotope : str, optional Plot data for this isotope zai : int, optional Plot data for this isotope. Not allowed if ``isotope`` given. sampleKwargs : dict, optional Additional matplotlib-acceptable arguments to be passed into the plot when plotting data from unique runs, e.g. ``{"c": k, "alpha": 0.5}``. meanKwargs : dict, optional Additional matplotlib-acceptable argumentst to be used when plotting the mean value, e.g. ``{"c": "b", "marker": "o"}`` timePoints : list or None If given, select the time points according to those specified here. Otherwise, select all points {ax} {xlabel} {ylabel} {logx} {logy} {loglog} {legend} Returns ------- {rax} """ if not self.allData: raise SamplerError("Data from all sampled files has been freed " "and cannot be used in this plot method") if isotope is not None and zai is not None: raise ValueError("Please specify isotope name or zai, not both") elif isotope is None and zai is None: raise ValueError("Isotope name or zai needed") if sampleKwargs is None: sampleKwargs = {"c": "k", "alpha": 0.5, "marker": ""} if meanKwargs is None: meanKwargs = {"c": "#0173b2", "marker": "o"} ax = ax or pyplot.gca() if xUnits not in ('days', 'burnup'): raise KeyError("Plot method only uses x-axis data from <days> " "and <burnup>, not {}".format(xUnits)) xVals = timePoints if timePoints is not None else ( self.days if xUnits == 'days' else self.burnup) if isotope is not None: rows = self._getRowIndices("names", [isotope]) else: rows = self._getRowIndices("zai", [zai]) cols = self._getColIndices(xUnits, timePoints) primaryData = self._slice(self.data[yUnits], rows, cols)[0] for data in self.allData[yUnits][:self._index]: plotData = self._slice(data, rows, cols)[0] ax.plot(xVals, plotData, **sampleKwargs) ax.plot(xVals, primaryData, label='Mean value', **meanKwargs) ax = sigmaLabel(ax, xlabel or DEPLETION_PLOT_LABELS[xUnits], ylabel or DEPLETION_PLOT_LABELS[yUnits]) formatPlot(ax, legend=legend, logx=logx, logy=logy, loglog=loglog) return ax
def spreadPlot(self, xdim=None, fixed=None, ax=None, xlabel=None, ylabel=None, logx=False, logy=False, loglog=False, autolegend=True): """ Plot the mean tally value against all sampled detector data. Parameters ---------- xdim: str Bin index to place on the x-axis fixed: None or dict Dictionary controlling the reduction in data down to one dimension {ax} {xlabel} {ylabel} {logx} {logy} {loglog} autolegend: bool If true, apply a label to this plot. Returns ------- {rax} Raises ------ SamplerError If ``allTallies`` is None, indicating this object has been instructed to free up data from all sampled files SerpentToolsException If data to be plotted, after applying ``fixed``, is not one dimensional """ if self.allTallies is None: raise SamplerError("Data from all sampled files has been freed " "and cannot be used in this plot method") samplerData = self.slice(fixed, 'tallies') slices = self._getSlices(fixed) if len(samplerData.shape) != 1: raise SerpentToolsException( 'Data must be constrained to 1D, not {}'.format( samplerData.shape)) xdata, autoX = self._getPlotXData(xdim, samplerData) xlabel = xlabel or autoX ax = ax or pyplot.axes() N = self._index allTallies = self.allTallies.copy(order='F') for n in range(N): plotData = allTallies[n][slices] ax.plot(xdata, plotData, **SPREAD_PLOT_KWARGS) ax.plot(xdata, samplerData, label='Mean value - N={}'.format(N)) if autolegend: ax.legend() ax.set_xlabel(xlabel) ax.set_ylabel(ylabel or "Tally Data") if loglog or logx: ax.set_xscale('log') if loglog or logy: ax.set_yscale('log') return ax
def spreadPlot(self, xUnits, yUnits, isotope, timePoints=None, ax=None, xlabel=None, ylabel=None, logx=False, logy=False, loglog=False, legend=True): """ Plot the mean quantity and data from all sampled files. Parameters ---------- xUnits: str name of x value to obtain, e.g. ``'days'``, ``'burnup'`` yUnits: str name of y value to return, e.g. ``'adens'``, ``'burnup'`` isotope: str Plot data for this isotope timePoints: list or None If given, select the time points according to those specified here. Otherwise, select all points {ax} {xlabel} {ylabel} {logx} {logy} {loglog} {legend} Returns ------- {rax} Raises ------ SamplerError If ``self.allData`` is empty. Sampler was instructed to free all materials and does not retain data from all containers See Also -------- :py:meth:`~serpentTools.samplers.depletion.SampledDepletedMaterial.plot` """ if not self.allData: raise SamplerError("Data from all sampled files has been freed " "and cannot be used in this plot method") ax = ax or pyplot.axes() if xUnits not in ('days', 'burnup'): raise KeyError("Plot method only uses x-axis data from <days> " "and <burnup>, not {}".format(xUnits)) xVals = timePoints if timePoints is not None else ( self.days if xUnits == 'days' else self.burnup) rows = self._getRowIndices([isotope]) cols = self._getColIndices(xUnits, timePoints) primaryData = self._slice(self.data[yUnits], rows, cols)[0] N = self._index sampledData = self.allData[yUnits][:N] for n in range(N): plotData = self._slice(sampledData[n], rows, cols)[0] ax.plot(xVals, plotData, **SPREAD_PLOT_KWARGS) ax.plot(xVals, primaryData, label='Mean value') ax = sigmaLabel(ax, xlabel or self.PLOT_XLABELS[xUnits], ylabel or yUnits) if loglog or logx: ax.set_xscale('log') if loglog or logy: ax.set_yscale('log') if legend: ax.legend() return ax