Data Module¶
penred data module
- pyPenred.data.configFile2YAML(arg0: str) str¶
Reads a configuration file and returns a YAML string with the read information.
- Parameters:
filename (str) – File to be read.
- Returns:
String containing the information in YAML format
- pyPenred.data.dict2SectionString(conf: dict) str¶
Converts a dictionary to a compatible penRed configuration section string.
- Parameters:
conf (dict) – configuration dictionary to be converted.
- Returns:
String containing the converted dictionary
- pyPenred.data.printResults(filename: str, values: Annotated[numpy.typing.ArrayLike, numpy.float64], sigma: Annotated[numpy.typing.ArrayLike, numpy.float64], info: list, value_header: str = 'Value', print_sigmas: SupportsInt | SupportsIndex = 2, print_coordinates: bool = True, print_bins: bool = True) None¶
Print the provided data as standard penRed’s results file.
- Parameters:
filename (str) – Results filename.
values (vector) – Numpy vector with the values to be saved
sigma (vector) – Numpy vector with the uncertainty of each value corresponding to one sigma
info (list) – List of tuples where each element contains the information of the corresponding dimension. Each element must use the following format: (min, max, header), where min and max are the minimum and maximum grid value for this dimension, respectively, and header the text header for dimension’s column.
value_header (str) – Header assigned to values. An example for a energetic spectrum could be ‘E (eV)’.
print_sigmas (int) – Specify the number of printed sigmas, i.e. the uncertainties column will be transformed as print_sigmas*sigma
print_coordinates (bool) – If enabled, the coordinates of each dimension will be printed
print_bins (bool) – If enabled, the bin number of each dimension will be printed
- Returns:
None
- Raises:
TypeError – Incompatible types have been provided.
ValueError – Dimensions mismatch or incompatible values have been provided.
RuntimeError – Unable to open output file
- pyPenred.data.readResults(filename: str, extract_info: bool = False) tuple¶
Reads a penRed standard results file and convert it to numpy arrays.
- Parameters:
- Returns:
A tuple containing the data, uncertainties and, if enabled, information of each dimension (lower grid limit, upper grid limits, dimension header), value header, file description
Results classes¶
The module exposes the following classes with different maximum dimensionality:
Class |
Stored data |
|---|---|
|
1-dimensional buffer |
|
2-dimensional buffer |
|
3-dimensional buffer |
|
4-dimensional buffer |
|
5-dimensional buffer |
|
6-dimensional buffer |
|
7-dimensional buffer |
|
8-dimensional buffer |
|
9-dimensional buffer |
|
10-dimensional buffer |
|
buffer with maximum dimensions available (30) |
They all share the same interface; only the dimensionality of the
underlying buffer differs. See results below
for the full method reference.
- class pyPenred.data.results¶
Bases:
pybind11_objectResults container for a maximum of 30D data.
Stores values and their uncertainties on a 30-dimensional grid.
- bins(self: results, dimension: SupportsInt | SupportsIndex = 31) int¶
- Parameters:
dimension (int) – The dimension for which the number of bins is requested. If this value exceeds the maximum number of dimensions, or it is not provided, the total number of bins across all dimensions is returned instead.
- Returns:
The number of bins in the specified dimension, or the total number of bins if the dimension is out of bounds.
- Return type:
- Raises:
None –
- data(self: results) tuple¶
Extract result’s data as numpy arrays
- Parameters:
None
- Returns:
A tuple containing three elements. First, two numpy arrays with the values and uncertainties respectivelly, and then a list with the dimension’s information.
- Raises:
ValueError – If transformation fails. If that happens, please, report the error.
- description(self: results) str¶
Reads the results’ description
- Parameters:
None
- Returns:
A string containing the results’ description
- Raises:
None –
- extractSpectrum1D(self: results, spectrum_dim: SupportsInt | SupportsIndex, positions: Annotated[Sequence[SupportsFloat | SupportsIndex], 'FixedSize(29)']) tuple¶
Creates a 1D spectrum for the specified dimension and position within the grid interpolation values and uncertainties in each dimension
- Parameters:
- Returns:
A tuple containing the interpolated spectrum values and uncertainties.
- Raises:
ValueError – If the interpolation fails.
- extractValue(self: results, position: Annotated[Sequence[SupportsFloat | SupportsIndex], 'FixedSize(30)']) tuple¶
Interpolate the value within the grid at the specified position in each dimension
- Parameters:
position (list) – List of positions for each dimension within the grid.
- Returns:
A tuple containing the interpolated value and uncertainty.
- Raises:
ValueError – If the interpolation fails.
- fill(self: results, values: Annotated[numpy.typing.ArrayLike, numpy.float64], sigma: Annotated[numpy.typing.ArrayLike, numpy.float64], info: list, value_header: str = 'Value') None¶
Fills the results object with the provided data.
- Parameters:
values (vector) – Numpy vector with the values to be saved
sigma (vector) – Numpy vector with the uncertainty of each value corresponding to one standard deviation
info (list) – List of tuples where each element contains the information of the corresponding dimension. Each element must use the following format: (min, max, header), where min and max are the minimum and maximum grid value for this dimension, respectively, and header the text header for dimension’s column.
value_header (str) – Header assigned to values. An example for a energetic spectrum could be ‘E (eV)’.
- Returns:
None
- Raises:
TypeError – Incompatible types have been provided.
ValueError – Dimensions mismatch or incompatible values have been provided.
- load(self: results, filename: str) None¶
Load results from a text file
- Parameters:
filename (str) – Data file.
- Returns:
None
- Raises:
ValueError – If file parsing fails or the fail is unrecheable.
- maxDim(self: results) int¶
- Parameters:
None
- Returns:
The maximum number of dimensions
- Raises:
None –
- normalize(self: results) None¶
Normalize the data in place, so that Σ y_i = 1. Requires all bin values to be non-negative. If any bin has a negative value the object is left untouched.
Uncertainties are propagated assuming independent bins. Because the normalization sum S = Σ x_i is itself estimated from the data, the normalized bins become negatively correlated:
σ²(y_i) = [ σ_i² − 2 y_i σ_i² + y_i² σ_S² ] / S²
with σ_S² = Σ_j σ_j² and y_i = x_i / S.
- Parameters:
None
- Returns:
None
- Raises:
ValueError – If a negative value is found or the data is empty (Σ y_i = 0).
- relative(self: results, y: results, sub: SupportsFloat | SupportsIndex = 0.0, zero: SupportsFloat | SupportsIndex = 0.0, threshold: SupportsFloat | SupportsIndex = 1e-20) results¶
Compute the bin-by-bin relative comparison between this histogram (x) and a reference histogram (y):
res_i = x_i / y_x - sub
with the first-order (delta-method) uncertainty, valid for statistically independent x and y:
σ(dres_i)² = (σ_x_i / y_i)² + (x_i σ_y_i / y_i²)²
- Parameters:
- Returns:
A results object with the division values and uncertainties. The binning and metadata is copied form x.
- Raises:
ValueError – If objects’ grid dimensions mismatch.
- save(self: results, filename: str, print_sigmas: SupportsInt | SupportsIndex = 2, print_coordinates: bool = True, print_bins: bool = True, only_effective: bool = True) None¶
Writes the stored data as standard penRed’s results file.
- Parameters:
filename (str) – Results filename.
print_sigmas (int) – Specify the number of printed sigmas, i.e. the uncertainties column will be transformed as print_sigmas*sigma
print_coordinates (bool) – If enabled, the coordinates of each dimension will be printed
print_bins (bool) – If enabled, the bin number of each dimension will be printed
only_effective (bool) – If enabled, only effective dimensions (those with more than a single bin), will be printed
- Returns:
None
- Raises:
ValueError – If file parsing fails or the fail is unrecheable.
- subtract(self: results, y: results) results¶
Compute the bin-by-bin difference res_i = x_i - y_i, where x is the current histogram and y the provided parameter.
- Parameters:
y (obj) – Results object to subtract.
- Returns:
A results object with the subtraction values and uncertainties. Binning, headers, description, and other metadata of the output are copied from x
- Raises:
ValueError – If objects’ grid dimensions mismatch.
- title(self: results) str¶
Reads the results’ title
- Parameters:
None
- Returns:
A string containing the results title
- Raises:
None –
- to1D(self: results) results1D¶
Create a result object with 1 dimension with the data contained in the current object. The latter must contain 1 effective dimension (dimensions with a number of bins greater than 1).
- Parameters:
None
- Returns:
A 1D results object.
- Raises:
ValueError – If the effective dimensions are not 1.
- to2D(self: results) results2D¶
Create a result object with 2 dimensions with the data contained in the current object. The latter must contain 2 effective dimensions (dimensions with a number of bins greater than 1).
- Parameters:
None
- Returns:
A 2D results object.
- Raises:
ValueError – If the effective dimensions are not 2.
Dimension-specific methods¶
results1D¶
- class pyPenred.data.results1D¶
Bases:
pybind11_objectResults container for a maximum of 1D data.
Stores values and their uncertainties on a 1-dimensional grid.
- extractValue1D(self: results1D, x: SupportsFloat | SupportsIndex) tuple¶
Interpolate the value within the 1D grid at the specified position
- Parameters:
x (float) – Positions within the grid.
- Returns:
A tuple containing the interpolated value and uncertainty.
- Raises:
ValueError – If the interpolation fails.
results2D¶
- class pyPenred.data.results2D¶
Bases:
pybind11_objectResults container for a maximum of 2D data.
Stores values and their uncertainties on a 2-dimensional grid.
- extractValue2D(self: results2D, x: SupportsFloat | SupportsIndex, y: SupportsFloat | SupportsIndex) tuple¶
Interpolate the value within the 2D grid at the specified position
- Parameters:
- Returns:
A tuple containing the interpolated value and uncertainty.
- Raises:
ValueError – If the interpolation fails.
results3D¶
- class pyPenred.data.results3D¶
Bases:
pybind11_objectResults container for a maximum of 3D data.
Stores values and their uncertainties on a 3-dimensional grid.
- extractValue3D(self: results3D, x: SupportsFloat | SupportsIndex, y: SupportsFloat | SupportsIndex, z: SupportsFloat | SupportsIndex) tuple¶
Interpolate the value within the 3D grid at the specified position
- Parameters:
- Returns:
A tuple containing the interpolated value and uncertainty.
- Raises:
ValueError – If the interpolation fails.