Skip to content

easyscience.Datasets

xarray

EasyScienceDataarrayAccessor

Accessor to extend an xarray DataArray to EasyScience. These functions can be accessed by obj.EasyScience.func.

compute_func property writable

Get the computational function which will be executed during a fit

:return: Computational function applied to the DataArray :rtype: Callable

core_object property writable

Get the core object associated to a DataArray. Note that this is called from a weakref. If the EasyScience obj is garbage collected, None will be returned.

:return: EasyScience object associated with the DataArray :rtype: Any

postcompute_func property writable

Get the post-computational function which will be executed after a fit

:return: Computational function applied to the DataArray after fitting :rtype: Callable

precompute_func property writable

Get the pre-computational function which will be executed before a fit

:return: Computational function applied to the DataArray before fitting :rtype: Callable

fit(fitter, *args, fit_kwargs=None, fn_kwargs=None, vectorize=False, dask='forbidden', **kwargs)

Perform a fit on the given DataArray. This fit utilises a given fitter from EasyScience.fitting.Fitter, though there are a few differences to a standard EasyScience fit. In particular, key-word arguments to control the optimisation algorithm go in the fit_kwargs dictionary, fit function key-word arguments go in the fn_kwargs and given key-word arguments control the xarray.apply_ufunc function.

:param fitter: Fitting object which controls the fitting :type fitter: EasyScience.fitting.Fitter :param args: Arguments to go to the fit function :type args: Any :param dask: Dask control string. See xarray.apply_ufunc documentation :type dask: str :param fit_kwargs: Dictionary of key-word arguments to be supplied to the Fitting control :type fit_kwargs: dict :param fn_kwargs: Dictionary of key-words to be supplied to the fit function :type fn_kwargs: dict :param vectorize: Should the fit function be given dependents in a single object or split :type vectorize: bool :param kwargs: Key-word arguments for xarray.apply_ufunc. See xarray.apply_ufunc documentation :type kwargs: Any :return: Results of the fit :rtype: FitResults

fit_prep(func_in, bdims=None, dask_chunks=None)

Generate broadcasted coordinates for fitting and reform the fitting function into one which can handle xarrays.

:param func_in: Function to be wrapped and made xarray fitting compatible. :type func_in: Callable :param bdims: Optional precomputed broadcasted dimensions. :type bdims: xarray.DataArray :param dask_chunks: How to split the broadcasted dimensions for dask. :type dask_chunks: Tuple[int..] :return: Tuple of broadcasted fit arrays and wrapped fit function. :rtype: xarray.DataArray, Callable

generate_points()

Generate an expanded DataArray of points which corresponds to broadcasted dimensions (all_x) which have been concatenated along the second axis (fit_dim).

:return: Broadcasted and concatenated coordinates :rtype: xarray.DataArray

EasyScienceDatasetAccessor

Accessor to extend an xarray DataSet to EasyScience. These functions can be accessed by `obj.EasyScience.func`.
The objective for this class accessor is to facilitate:
  • Creation of nd datasets by making and assigning axes and data more accessible
  • To add and keep track of errors in the form of sigma for datasets.py
  • To perform fitting on one or more data arrays in the dataset simultaneously.
  • To facilitate dask computation if required.

core_object property writable

Get the core object associated to a DataSet. Note that this is called from a weakref. If the EasyScience obj is garbage collected, None will be returned.

:return: EasyScience object associated with the DataSet :rtype: Any

description property writable

Get a description of the DataSet

:return: Description of the DataSet :rtype: str

name property writable

Get the common name of the DataSet.

:return: Common name of the DataSet :rtype: str

url property writable

Get the url of the DataSet

:return: URL of the DataSet (empty if no URL) :rtype: str

__init__(xarray_obj)

This is called whenever you access obj.EasyScience, hence the attributes in the obj should only be written if they have not been previously instantiated.

:param xarray_obj: DataSet which is called by obj.EasyScience :type xarray_obj: xarray.Dataset

add_coordinate(coordinate_name, coordinate_values, unit='')

Add a coordinate to the DataSet. This can be then be assigned to one or more DataArrays.

:param coordinate_name: Name of the coordinate e.g. x :type coordinate_name: str :param coordinate_values: Points for the coordinates :type coordinate_values: Union[List[T_], numpy.ndarray] :param unit: Unit associated with the coordinate :type unit: str :return: None :rtype: None

add_variable(variable_name, variable_coordinates, variable_values, variable_sigma=None, unit='', auto_sigma=False)

Create a DataArray from known coordinates and data, assign it to the dataset under a given name. Variances can be calculated assuming gaussian distribution to 1 sigma.

:param variable_name: Name of the DataArray which will be created and added to the dataset :type variable_name: str :param variable_coordinates: List of coordinates used in the supplied data array. :type variable_coordinates: str, List[str] :param variable_values: Numpy or list of data which will be assigned to the DataArray :type variable_values: Union[numpy.ndarray, list] :param variable_sigma: If the sigmas of the dataset are known, they can be supplied here. :type variable_sigma: Union[numpy.ndarray, list] :param unit: Unit associated with the DataArray :type unit: str :param auto_sigma: Should the sigma DataArray be automatically calculated assuming gaussian probability? :type auto_sigma: bool :return: None :rtype: None

fit(fitter, data_arrays, *args, dask='forbidden', fit_kwargs=None, fn_kwargs=None, vectorized=False, **kwargs)

Perform a fit on one or more DataArrays. This fit utilises a given fitter from EasyScience.fitting.Fitter, though there are a few differences to a standard EasyScience fit. In particular, key-word arguments to control the optimisation algorithm go in the fit_kwargs dictionary, fit function key-word arguments go in the fn_kwargs and given key-word arguments control the xarray.apply_ufunc function.

:param fitter: Fitting object which controls the fitting :type fitter: EasyScience.fitting.Fitter :param args: Arguments to go to the fit function :type args: Any :param dask: Dask control string. See xarray.apply_ufunc documentation :type dask: str :param fit_kwargs: Dictionary of key-word arguments to be supplied to the Fitting control :type fit_kwargs: dict :param fn_kwargs: Dictionary of key-words to be supplied to the fit function :type fn_kwargs: dict :param vectorized: Should the fit function be given dependents in a single object or split :type vectorized: bool :param kwargs: Key-word arguments for xarray.apply_ufunc. See xarray.apply_ufunc documentation :type kwargs: Any :return: Results of the fit :rtype: List[FitResults]

generate_points(coordinates)

    Generate an expanded DataArray of points which corresponds to broadcasted dimensions (`all_x`) which have been
    concatenated along the second axis (`fit_dim`).

    :param coordinates: List of coordinate names to broadcast and concatenate along
    :type coordinates: List[str]
    :return: Broadcasted and concatenated coordinates
    :rtype: xarray.DataArray

.. code-block:: python

 x = [1, 2], y = [3, 4]
 d = xr.DataArray()
 d.EasyScience.add_coordinate('x', x)
 d.EasyScience.add_coordinate('y', y)
 points = d.EasyScience.generate_points(['x', 'y'])
 print(points)

remove_coordinate(coordinate_name)

Remove a coordinate from the DataSet. Note that this will not remove the coordinate from DataArrays which have already used the it!

:param coordinate_name: Name of the coordinate to be removed :type coordinate_name: str :return: None :rtype: None

remove_variable(variable_name)

Remove a DataArray from the DataSet by supplied name.

:param variable_name: Name of DataArray to be removed :type variable_name: str :return: None :rtype: None

sigma_attach(variable_label, sigma_values, label_prefix=None)

Attach an array of sigmas to the DataSet.

:param variable_label: Name of the DataArray to perform the calculation on :type variable_label: str :param sigma_values: Array of sigmas in list, numpy or DataArray form :type sigma_values: Union[List[T_], numpy.ndarray, xarray.DataArray] :param label_prefix: What prefix should be used to designate a sigma DataArray from a data DataArray :type label_prefix: str :return: None :rtype: None

sigma_generator(variable_label, sigma_func=lambda x: np.sqrt(np.abs(x)), label_prefix=None)

Generate sigmas off of a DataArray based on a function.

:param variable_label: Name of the DataArray to perform the calculation on :type variable_label: str :param sigma_func: Function to generate the sigmas. Must be of the form f(x) and return an array of the same shape as the input. Default sqrt(|x|) :type sigma_func: Callable :param label_prefix: What prefix should be used to designate a sigma DataArray from a data DataArray :type label_prefix: str :return: None :rtype: None

check_sanity_multiple(fit_results, originals)

Convert the multifit FitResults from a fitter compatible state to a list of recognizable DataArray states.

:param fit_results: Results of a fit to be modified :type fit_results: FitResults :param originals: List of DataArrays which were fitted against, so we can resize and re-chunk the results :type originals: List[xr.DataArray] :return: Modified fit results :rtype: List[FitResults]

check_sanity_single(fit_results)

Convert the FitResults from a fitter compatible state to a recognizable DataArray state.

:param fit_results: Results of a fit to be modified :type fit_results: FitResults :return: Modified fit results :rtype: FitResults