from_txt#

classmethod Hist1d.from_txt(fname, data_layout='columns', idx_centers=0, idx_values=1, lower=None, upper=None, title='', xlabel='', ylabel='', **loadtxt_kwargs)[source]#

Initiate a Hist1d from a text file.

Assumes that the histogram is saved as bin-centers, bin-values. The file is loaded using numpy.loadtxt().

Parameters:
fnamestr or PathLike

The filename.

data_layout“rows” or “columns”, default “columns”

Specify if centers and values are saved in the text file in rows or in columns.

idx_centersint, default 0

The index that corresponds to the histogram bin centers.

idx_valuesint, default 1

The index that corresponds to the histogram values.

lower, upperfloat, optional

If the histogram bins do not have equal size, at least lower or upper has to be provided in order to properly calculate the bin edges.

See centers_to_edges().

titlestr, default “”

Optional title of the histogram.

xlabelstr, default “”

Optional x-label of the histogram.

ylabelstr, default “”

Optional y-label of the histogram.

Other Parameters:
**loadtxt_kwargs

Additional numpy.loadtxt() keyword arguments.

Examples

Assume a data.txt with:

# data.txt
0.5    1
1.5    2
2.5    3
3.5    4
4.5    5

Initiate a histogram from it:

>>> hist = ap.Hist1d.from_txt("data.txt")
>>> hist.values
array([1. 2. 3. 4. 5.])
>>> hist.edges
array([0. 1. 2. 3. 4. 5])

If the bins do not have a constant binsize, e.g.:

# data.txt
0.5    1
1.5    2
3.0    3
4.5    4
5.5    5

one can load it, but needs to specify either lower or upper:

>>> hist = ap.Hist1d.from_txt("data.txt", lower=0.0)
>>> hist.values
array([1. 2. 3. 4. 5.])
>>> hist.edges
array([0. 1. 2. 4. 5. 6])

If multiple datasets are within one textfile, e.g.:

# manydata.txt
# values1   values2   centers
  1         11        0.5
  2         12        1.5
  3         13        2.5
  4         14        3.5
  5         15        4.5

one can load specify which data to load using the idx_* keywords:

>>> hist1 = ap.Hist1d.from_txt("manydata.txt", idx_centers=2, idx_values=0)
>>> hist2 = ap.Hist1d.from_txt("manydata.txt", idx_centers=2, idx_values=1)
>>> hist1.values
array([1. 2. 3. 4. 5.])
>>> hist2.values
array([11. 12. 13. 14. 15.])