Data#
- class zfit.data.Data(dataset, obs=None, name=None, weights=None, dtype=None, use_hash=None)[source]#
Bases:
ZfitUnbinnedData
,BaseDimensional
,BaseObject
,GraphCachable
Create a data holder from a
dataset
used to feed intomodels
.- Parameters
dataset (tf.data.Dataset | LightDataset) – A dataset storing the actual values
obs (ztyping.ObsTypeInput) – Observables where the data is defined in
name (str) – Name of the
Data
weights – Weights of the data
dtype (tf.DType) – The DType of the return value. Defaults to the zfit default (usually float64).
use_hash (bool) – Whether to use a hash for caching
- property weights#
Get the weights of the data.
- set_weights(weights)[source]#
Set (temporarily) the weights of the dataset. (deprecated)
Deprecated: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Do not set the weights on a data set, create a new one instead.
- Parameters
weights (
Union
[Tensor
,None
,ndarray
]) –
- classmethod from_pandas(df, obs=None, weights=None, name=None, dtype=None, use_hash=None)[source]#
Create a
Data
from a pandas DataFrame. Ifobs
isNone
, columns are used as obs.- Parameters
df (pd.DataFrame) – pandas DataFrame that contains the data. If
obs
isNone
, columns are used as obs. Can be a superset of obs.obs (ztyping.ObsTypeInput) – obs to use for the data. obs have to be the columns in the data frame. If
None
, columns are used as obs.weights (ztyping.WeightsInputType | str) – Weights of the data. Has to be 1-D and match the shape of the data (nevents) or a string that is a column in the dataframe.
name (str) –
dtype (tf.DType) – dtype of the data
use_hash (bool) – If
True
, a hash of the data is created and is used to identify it in caching.
- classmethod from_root(cls, path, treepath, obs=None, *, weights=None, obs_alias=None, name=None, dtype=None, root_dir_options=None, use_hash=None, branches=None, branches_alias=None)[source]#
Create a
Data
from a ROOT file. Arguments are passed touproot
. (deprecated arguments) (deprecated arguments)Deprecated: SOME ARGUMENTS ARE DEPRECATED:
(branches)
. They will be removed in a future version. Instructions for updating: Use obs instead.Deprecated: SOME ARGUMENTS ARE DEPRECATED:
(branches_alias)
. They will be removed in a future version. Instructions for updating: Use obs_alias instead and make sure to invert the logic! I.e. it’s a mapping from the observable name to the actual branch name.The arguments are passed to uproot directly.
- Parameters
path – Path to the root file.
treepath – Name of the tree in the root file.
obs – Observables of the data. This will also be the columns of the data if not obs_alias is given.
weights – Weights of the data. Has to be 1-D and match the shape of the data (nevents). Can be a column of the ROOT file by using a string corresponding to a column.
obs_alias – A mapping from the
obs
(as keys) to the actualbranches
(as values) in the root file. This allows to have differentobservable
names, independent of the branch name in the file.name –
root_dir_options –
- Returns
A
Data
object containing the unbinned data.- Return type
zfit.Data
- classmethod from_numpy(obs, array, weights=None, name=None, dtype=None, use_hash=None)[source]#
Create
Data
from anp.array
.- Parameters
obs (
Union
[str
,Iterable
[str
], zfit.Space]) – Observables of the data. They will be matched to the data in the same order.array (
ndarray
) – Numpy array containing the data.weights (
Union
[Tensor
,None
,ndarray
]) – Weights of the data. Has to be 1-D and match the shape of the data (nevents).name (
Optional
[str
]) – Name of the data.dtype (
Optional
[DType
]) – dtype of the data.use_hash – If
True
, a hash of the data is created and is used to identify it in caching.
- Returns
A
Data
object containing the unbinned data.- Return type
zfit.Data
- classmethod from_tensor(obs, tensor, weights=None, name=None, dtype=None, use_hash=None)[source]#
Create a
Data
from atf.Tensor
.Value
simply returns the tensor (in the right order).- Parameters
obs (
Union
[str
,Iterable
[str
], zfit.Space]) – Observables of the data. They will be matched to the data in the same order.tensor (
Tensor
) – Tensor containing the data.weights (
Union
[Tensor
,None
,ndarray
]) – Weights of the data. Has to be 1-D and match the shape of the data (nevents).name (
Optional
[str
]) – Name of the data.
- Returns
A
Data
object containing the unbinned data.- Return type
zfit.Data
- with_obs(obs)[source]#
Create a new
Data
with a subset of the data using the obs.- Parameters
obs – Observables to return. Has to be a subset of the original observables.
- Returns
A new
Data
object containing the subset of the data.- Return type
zfit.Data
- to_pandas(obs=None)[source]#
Create a
pd.DataFrame
fromobs
as columns and return it.- Parameters
obs (
Union
[str
,Iterable
[str
], zfit.Space,None
]) – The observables to use as columns. IfNone
, all observables are used.
Returns:
- unstack_x(obs=None, always_list=False)[source]#
Return the unstacked data: a list of tensors or a single Tensor.
- Parameters
obs (
Union
[str
,Iterable
[str
], zfit.Space,None
]) – which observables to returnalways_list (
bool
) – If True, always return a list (also if length 1)
- Returns
List(tf.Tensor)
- value(obs=None)[source]#
Return the data as a numpy-like object in
obs
order.- Parameters
obs (
Union
[str
,Iterable
[str
], zfit.Space,None
]) – Observables to return. IfNone
, all observables are returned. Can be a subset of the original observables. If a string is given, a 1-D array is returned with shape (nevents,). If a list of strings or azfit.Space
is given, a 2-D array is returned with shape (nevents, nobs).
Returns:
- add_cache_deps(cache_deps, allow_non_cachable=True)#
Add dependencies that render the cache invalid if they change.
- Parameters
cache_deps (
Union
[zfit.core.interfaces.ZfitGraphCachable,Iterable
[zfit.core.interfaces.ZfitGraphCachable]]) –allow_non_cachable (
bool
) – IfTrue
, allowcache_dependents
to be non-cachables. IfFalse
, anycache_dependents
that is not aZfitGraphCachable
will raise an error.
- Raises
TypeError – if one of the
cache_dependents
is not aZfitGraphCachable
_and_allow_non_cachable
ifFalse
.
- property name: str#
The name of the object.
- Return type
str
- register_cacher(cacher)#
Register a
cacher
that caches values produces by this instance; a dependent.- Parameters
cacher (
Union
[zfit.core.interfaces.ZfitGraphCachable,Iterable
[zfit.core.interfaces.ZfitGraphCachable]]) –
- reset_cache_self()#
Clear the cache of self and all dependent cachers.