epsproc.calc.density module¶
Density matrix routines
30/08/21 v3 Debugged and updated docstrings. 27/08/21 v2 Updated dim handling for renaming multi-index levels + working HV plotting routines. 26/08/21 v1 Initial implementation
Dev code:
- http://100.86.127.24/jupyter/user/paul/doc/tree/github/ePSproc/notebooks/in_progress/ePSdev-PEMtk_correlations_den-mats_fn-def_260821.ipynb
- http://100.86.127.24/jupyter/user/paul/doc/tree/github/ePSproc/notebooks/in_progress/ePSdev-PEMtk_correlations_den-mats_basic-tests_220821.ipynb
-
epsproc.calc.density.
densityCalc
(da, denDims='LM', sumDims=None, keepDims=None, selDims=None, thres=None, squeeze=True, keepNaNs=False, compute=True)[source]¶ General density matrix from Xarray.
Compute density matrix as (outer product) da[denDims]*da[denDims].conj(), where dim specifies the dimension(s) to use. This is, essentially, the density matrix :math:row = |denDims\rangle \langle denDims|:math:
Parameters: - da (xarray) – Data array to check & restack
- denDims (str, list, dict, optional, default = 'LM') – Dimensions to use as “state vector” from da. If a single dim (including stacked dims), which exists, this will be used directly. If multiple dims, will be restacked to a new dimension. Note that this can mix stacked and unstacked dims, and will restack if necessary.
- sumDims (str, list, bool, optional, default = None) – Set specific dims to sum (“trace”) over. If sumDims = True all dims, apart from denDims and keepDims, will be summed over.
- keepDims (str, list, optional, default = None) – Define dims to keep (won’t be summed over). Only used if sumDims = True.
- selDims (str, list, optional, default = None) – Dimensions to subselect from.
- thres (float, optional, default = None) – Threshold value. If set, used for both input and output datasets.
- squeeze (bool, optional, default = True) – Squeeze out singleton dims if True.
- keepNaNs (bool, optional, default = False) – Keep NaNs in result if false. Otherwise, set to zero. (Note this is currently implemented indirectly via xr.sum(skipna = ~keepNaNs).) Note: in some cases this may cause NaNs to propagate and give all-NaN results.
- compute (bool, optional, default = True) – Compute density matrix as outer product? Otherwise just set from restacked Xarray.
Notes
selDims, thres and squeeze are passed to the standard
matEleSelector()
function.Examples
# Calculate for standard matE dataset, restacked for [l,m,mu]. >>> daOut, daDot = densityCalc(matE, denDims = [‘LM’, ‘mu’], selDims = {‘Type’:’L’}, thres = 1e-1) # Calculate for standard matE, no restack and sum all other dims. >>> daOut, daDot = densityCalc(matE, denDims = ‘LM’, selDims = {‘Type’:’L’}, thres = 1e-1, sumDims = True) # Calculate for standard matE, no restack and sum all other dims, except Sym. >>> daOut, daDot = densityCalc(matE, denDims = ‘LM’, selDims = {‘Type’:’L’}, thres = 1e-1, sumDims = True, keepDims = ‘Sym’)
-
epsproc.calc.density.
dimRestack
(da, stackDims=[])[source]¶ General Xarray restacker including multi-indexes.
Check dims in da, and restack according to refDims if necessary
Parameters: - da (xarray) – Data array to check & restack
- stackDims (str, list, dict, optional, default = []) – Dimensions to check in da, and restack along if not already stacked. Note that this can mix stacked and unstacked dims, and will restack if necessary
Returns: - daOut (Xarray) – Data array with restacked dim.
- stackedDim (str) – Name of new stacked dim
- rsMap (dict) – Dictionary mapping for new stacked dim
- dimCheck (dict) – Full output from
ep.util.misc.checkDims()
Examples
>>> # Assuming matE is a standard array of matrix elements >>> daOut, stackedDim, rsMap, dimCheck = dimRestack(matE) # OK, returns input + dim check results >>> daOut, stackedDim, rsMap, dimCheck = dimRestack(matE, stackDims='LM') # OK, returns original dims >>> daOut, stackedDim, rsMap, dimCheck = dimRestack(matE, stackDims=['LM','mu']) # OK, restacks dims
Notes
Passing new mappings as stackDims is currently not supported, e.g. dMap = {‘NewDim’:[d1,d2…]} will fail. For this case, just use the native da.stack(dMap).
See also
multiDimXrToPD()
-
epsproc.calc.density.
matPlot
(da, kdims=None, pTypes=['r', 'i', 'a'], negQs=True, thres=None, returnType='plot', verbose=1)[source]¶ General matrix (2D) plot + stacked dims plotter with HoloViews.
Parameters: - da (Xarray) – Input dataset for plotting. Assumed to contain a “matrix” (2D) to plot, with other dims stacked to HV plot widgets.
- kdims (list, optional, default = None) – Key dims for plotting. If None, - will be taken from da.attrs[kdims] if it is set. - otherwise, uses da.dims[-2:]
- pTypes (list, optional, default = ['r','i','a']) – List of plot types to set (via :py:func:`epsproc.plotTypeSelector`_ These will be available via the HV plot widgets.
- negQs (NOT YET IMPLEMENTED) – Include -ve valued QNs in plot?
- thres (NOT YET IMPLEMENTED) – optional, float, default = None Threshold for plot.
- returnType (str, optional, default = 'plot') – If ‘plot’ return plot object only. Otherwise, returns plot + datasets = (hvmap, hvds, daPlotDS)
- verbose (int, bool, optional, default = 1) – Print additional info if true.
Returns: If ‘plot’; otherwise, returns plot + datasets = (hvmap, hvds, daPlotDS)
Return type: Holoviews holomap object
TODO:
- update & consolidate dim-handling routines from densityCalc() to provide easier plotting routines here (without manual restack etc. for other data types).