tofu package

ToFu: A python library for Tomography for Fusion research

Provides
  1. Objects to handle 3D geometry of diagnostics

  2. Built-in methods for computing key quantities, data treatment, inversions…

  3. Visualisation tools

How to use the documentation

Documentation is available in two forms:
  1. Docstrings provided with the code

  2. Online sphinx-generated documentation, available from

ToFu’s homepage.

Available subpackages

geom

Geometry classes to model the 3D geometry (vacuum vessel, structural elements, LOS, etc.)

data

Data-handling classes (storing, processing, plotting, etc.)

dumpro

Package for dust movie processing

dust

Dust module

imas2tofu

The imas-compatibility module of tofu (optional)

mag

Magnetic field lines package (optional)

Available modules

_plot

Module providing a basic routine for plotting a shot overview

defaults

Store most default parameters of tofu

pathfile

Provide a class for identification of all tofu objects, and functions for path and file handling

utils

Miscellaneous helper functions

Utilities

tests

tofu’s unit-tests

__version__

tofu version string

Subpackages

Submodules

tofu._physics module

tofu._physics.compute_bremzeff(Te=None, ne=None, zeff=None, lamb=None)[source]

Return the bremsstrahlun spectral radiance at lamb

The plasma conditions are set by:
  • Te (eV)

  • ne (/m3)

  • zeff (adim.)

The wavelength is set by the diagnostics
  • lamb (m)

The vol. spectral emis. is returned in ph / (s.m3.sr.m)

The computation requires an intermediate : gff(Te, zeff)

tofu._physics.compute_fangle(BR=None, BPhi=None, BZ=None, ne=None, lamb=None)[source]

The the vector quantity to be integrated on LOS to get faraday angle

fangle = int_LOS ( abs(sca(quant, u_LOS)) ) Where:

quant = C * lamb**2 * ne * Bv

With:
  • C = 2.615e-13 (1/T)

  • ne (/m3)

  • lamb (m)

  • Bv (T)

The resulting faraday angle (after integration) will be in radians

tofu._plot module

Module providing a basic routine for plotting a shot overview

tofu._plot.plot_shotoverview(db, ntMax=3, indt=0, config=None, inct=[1, 5], dcol=None, lct=[(0.12156862745098039, 0.4666666666666667, 0.7058823529411765), (1.0, 0.4980392156862745, 0.054901960784313725), (0.17254901960784313, 0.6274509803921569, 0.17254901960784313), (0.6823529411764706, 0.7803921568627451, 0.9098039215686274), (1.0, 0.7333333333333333, 0.47058823529411764), (0.596078431372549, 0.8745098039215686, 0.5411764705882353)], fmt_t='06.3f', fs=None, dmargin=None, tit=None, wintit=None, fontsize=8, labelpad=0, sharet=True, sharey=True, shareRZ=True, connect=True, draw=True)[source]

tofu.defaults module

This module stores all the default setting of ToFu Including in particular computing parameters, dictionnaries and figures

tofu.defaults.AllowedExp = [None, 'AUG', 'MISTRAL', 'JET', 'ITER', 'TCV', 'TS', 'WEST', 'KSTAR', 'Misc', 'Test']

Defaults settings of ToFu_Geom

tofu.defaults.BF2_DVect_DefR(Points)[source]
tofu.defaults.BF2_DVect_DefTor(Points)[source]
tofu.defaults.BF2_DVect_DefZ(Points)[source]
tofu.defaults.InvSigmad = {'facecolor': (0.8, 0.8, 0.8, 0.7), 'lw': 0.0}
tofu.defaults.Plot_3D_plt_Tor_DefAxes(a4=False)[source]
tofu.defaults.Plot_BF2_interp_DefAxes()[source]
tofu.defaults.Plot_BF2_sum_DefAxes(a4=False)[source]
tofu.defaults.Plot_BFunc_SuppMax_PolProj_DefAxes()[source]
tofu.defaults.Plot_BSplineFit_DefAxes(Mode)[source]
tofu.defaults.Plot_BSpline_DefAxes(Mode)[source]
tofu.defaults.Plot_BSpline_Deriv_DefAxes(Deg)[source]
tofu.defaults.Plot_BaseFunc2D_BFuncMesh_DefAxes()[source]
tofu.defaults.Plot_CrossSlice_SAngNb_DefAxes(VType='Tor', a4=False)[source]
tofu.defaults.Plot_Eq2D_DefAxes(VType='Tor', cbar=False, a4=False)[source]
tofu.defaults.Plot_Eq2D_Inter_DefAxes(VType='Tor', cbar=False, a4=False)[source]
tofu.defaults.Plot_Eq2D_Vs_DefAxes(VType='Tor', a4=False)[source]
tofu.defaults.Plot_Etendue_AlongLOS_DefAxes(kMode='rel', a4=False)[source]
tofu.defaults.Plot_Etendues_GDetect_DefAxes(a4=False)[source]
tofu.defaults.Plot_FFTChan_Def(a4=False)[source]
tofu.defaults.Plot_FFTInter_Def(a4=False)[source]
tofu.defaults.Plot_GDetect_Resolution_DefAxes(VType='Tor', a4=False)[source]
tofu.defaults.Plot_GeomMatrix_Mesh_DefAxes(a4=False)[source]
tofu.defaults.Plot_HorSlice_SAngNb_DefAxes(a4=False)[source]
tofu.defaults.Plot_Impact_DefAxes(Proj, Ang='theta', AngUnit='rad', a4=False, Sketch=True)[source]
tofu.defaults.Plot_Inv_Anim_DefAxes(SXR=True, TMat=True, Chi2N=True, Mu=True, R=True, Nit=True)[source]
tofu.defaults.Plot_Inv_Basic_DefAxes(a4=False, dpi=80)[source]
tofu.defaults.Plot_Inv_Compare_Basic_DefAxes(N=2, a4=False, dpi=80)[source]
tofu.defaults.Plot_Inv_Compare_Profiles_DefAxes(N=2, NL=4, a4=False, dpi=80)[source]
tofu.defaults.Plot_Inv_Compare_Technical_DefAxes(N=2, a4=False, dpi=80)[source]
tofu.defaults.Plot_Inv_FFTPow_DefAxes(NPts=6, a4=False)[source]
tofu.defaults.Plot_Inv_Profiles_DefAxes(NL=4, a4=False, dpi=80)[source]
tofu.defaults.Plot_Inv_Technical_DefAxes(a4=False, dpi=80)[source]
tofu.defaults.Plot_LOSProj_DefAxes(Mode, Type='Tor', a4=False)[source]
tofu.defaults.Plot_Lens_Alone_DefAxes(a4=False)[source]
tofu.defaults.Plot_Mesh1D_DefAxes(a4=False)[source]
tofu.defaults.Plot_Mesh2D_DefAxes(VType='Tor', a4=False)[source]
tofu.defaults.Plot_Noise_Def(a4=False)[source]
tofu.defaults.Plot_Res_Mesh1D_DefAxes(a4=False)[source]
tofu.defaults.Plot_Res_Mesh2D_DefAxes(a4=False, VType='Tor')[source]
tofu.defaults.Plot_SAng_Plane_DefAxes(a4=False)[source]
tofu.defaults.Plot_Sig_GDetect_DefAxes(a4=False)[source]
tofu.defaults.Plot_SynDiag_DefAxes(a4=False)[source]
tofu.defaults.Plot_TreatSig_Def(a4=False, nMax=4)[source]
tofu.defaults.kpVsPolProjAng(x)[source]

tofu.pathfile module

Created on Wed Jul 30 14:37:31 2014

@author: didiervezinet

tofu.pathfile.CheckSameObj(obj0, obj1, LFields=None)[source]

Check if two variables are the same instance of a ToFu class

Checks a list of attributes, provided by LFields

Parameters
  • obj0 (tofu object) – A variable refering to a ToFu object of any class

  • obj1 (tofu object) – A variable refering to a ToFu object of the same class as obj0

  • LFields (None / str / list) –

    The criteria against which the two objects are evaluated:
    • None: True is returned

    • str or list: tests whether all listed attributes have the same value

Returns

A (bool) – True only is LFields is None or a list of attributes that all match

class tofu.pathfile.ID(Cls=None, Name=None, Type=None, Deg=None, Exp=None, Diag=None, shot=None, SaveName=None, SavePath='/home/lasofivec/tofu/doc/source', USRdict={}, LObj=None, fromdict=None, Include=['Mod', 'Cls', 'Type', 'Exp', 'Deg', 'Diag', 'Name', 'shot'])[source]

Bases: object

A class used by all ToFu objects as an attribute

It stores all relevant data for the identification of instances Stored info can be the name of the instance, the experiment and diagnostics it belongs to, or other user-defined info Also provides default names for saving the instances

Parameters
  • Cls (str) – Class of the object on which info should be stored:

  • Name (str) – Name of the instance (user-defined) Should be a str without space ‘ ‘ or underscore ‘_’ (automatically removed if present)

  • Type (None / str) – Type of object (i.e.: ‘Tor’ or ‘Lin’ for a Ves)

  • Deg (None / int) – Degree of the b-splines constituting the tofu.mesh object

  • Exp (None / str) – Flag specifying the experiment (e.g.: ‘WEST’, ‘AUG’, ‘ITER’, ‘JET’…)

  • Diag (None / str) – Flag indicating the diagnostic (e.g.: ‘SXR’, ‘HXR’, ‘Bolo’…)

  • shot (None / int) – A shot number from which the instance is valid (for tracking changes)

  • SaveName (None / str) – Overrides the default file name for saving (not recommended)

  • SavePath (None / str) – Absolute path where the instance should be saved

  • USRdict (None / dict) – A user-defined dictionary containing information about the instance All info considered relevant can be passed here (e.g.: thickness of the diode, date of installation…)

  • LObj (None / dict / list) –

    Either:
    • list: list of other ID instances of objects on which the created object depends (this list will then be sorted by class and formatted into a dictionary storign key attributes)

    • dict: a ready-made such dictionary

property Cls
property Diag
property Exp
property LObj
property Name
property NameLTX
property SaveName
property SavePath
property Type
property USRdict
_check_inputs(Cls=None, Name=None, Type=None, Deg=None, Exp=None, Diag=None, shot=None, SaveName=None, SavePath=None, USRdict=None, LObj=None, version=None, usr=None, fromdict=None, Include=None)[source]
_fromdict(fd)[source]
_todict()[source]
set_LObj(LObj=None)[source]

Set the LObj attribute, storing objects the instance depends on

For example: A Detect object depends on a vessel and some apertures That link between should be stored somewhere (for saving/loading). LObj does this: it stores the ID (as dict) of all objects depended on.

Parameters

LObj (None / dict / ID / list of such) –

Provide either:
  • A dict (derived from _todict())

  • A ID instance

  • A list of dict or ID instances

set_Name(Name, SaveName=None, Include=['Mod', 'Cls', 'Type', 'Exp', 'Deg', 'Diag', 'Name', 'shot'], ForceUpdate=False)[source]

Set the Name of the instance, automatically updating the SaveName

The name should be a str without spaces or underscores (removed) When the name is changed, if SaveName (i.e. the name used for saving) was not user-defined, it is automatically updated

Parameters
  • Name (str) – Name of the instance, without ‘ ‘ or ‘_’ (automatically removed)

  • SaveName (None / str) – If provided, overrides the default name for saving (not recommended)

  • Include (list) – Controls how te default SaveName is generated Each element of the list is a key str indicating whether an element should be present in the SaveName

set_SaveName(SaveName=None, Include=['Mod', 'Cls', 'Type', 'Exp', 'Deg', 'Diag', 'Name', 'shot'], ForceUpdate=False)[source]

Set the name for saving the instance (SaveName)

SaveName can be either:
  • provided by the user (no constraint) - not recommended

  • automatically generated from Name and key attributes (cf. Include)

Parameters
  • SaveName (None / str) – If provided, overrides the default name for saving (not recommended)

  • Include (list) – Controls how te default SaveName is generated Each element of the list is a key str indicating whether an element should be present in the SaveName

  • ForceUpdate (bool) –

    Flag indicating the behaviour when SaveName=None:
    • True : A new SaveName is generated, overriding the old one

    • False : The former SaveName is preserved (default)

set_USRdict(USRdict={})[source]

Set the USRdict, containing user-defined info about the instance

Useful for arbitrary info (e.g.: manufacturing date, material…)

Parameters

USRdict (dict) – A user-defined dictionary containing info about the instance

property shot
tofu.pathfile.Open(pathfileext=None, shot=None, t=None, Dt=None, Mesh=None, Deg=None, Deriv=None, Sep=True, Pos=True, OutPath=None, ReplacePath=None, Ves=None, out='full', Verb=False, Print=True)[source]

Open a ToFu object saved file

This generic open function identifies the required loading routine by detecting how the object was saved from the file name extension. Also, it uses FindSolFile() to identify the relevant file in case key criteria such as shot, Deg… are provided instead of the file name itself. Finally, once all the relevant data is loaded from the file, a ToFu object is re-created, if necessary by implicitly loading all other objects it may depend on (i.e.: vessel, apertures…)

If pathfileext is not provided (None), then the following keyword arguments are fed to FindSolFile(): shot, t, Dt, Mesh, Deg, Deriv, Sep, Pos

Parameters
  • pathfileext (None / str) – If provided, the name of the file to load

  • OutPath (None / str) – If provided, the absolute path where the file is to be found

  • ReplacePath (str) – If provided, ? (to finish)

  • Ves (None /) – If provided, the tofu.geom.Ves object that shall be used to reconstruct the object (if not provided, the appropriate vessel will be loaded).

  • out (str) – Flag indicating whether the object should be loaded completely (‘full’), in a light dismissing the heaviest attributes (‘light’) or whether only the Id or a list of Id should be returned (‘Id’), valid only for ‘.npz’

  • Verb (bool) – Flag indicating whether to pring intermediate comments on the loading procedure

Returns

obj ToFu object – The loaded and re-created ToFu object

tofu.pathfile.SaveName_Conv(Mod=None, Cls=None, Type=None, Name=None, Deg=None, Exp=None, Diag=None, shot=None, version=None, usr=None, Include=['Mod', 'Cls', 'Type', 'Exp', 'Deg', 'Diag', 'Name', 'shot'])[source]

Return a default name for saving the object

Includes key info for fast identification of the object from file name Used on object creation by ID It is recommended to use this default name.

tofu.pathfile.Save_Generic(obj, SaveName=None, Path='./', Mode='npz', compressed=False, Print=True)[source]

Save a ToFu object under file name SaveName, in folder Path

ToFu provides built-in saving and loading functions for ToFu objects. There is now only one saving mode:

  • ‘npz’: saves a dict of key attributes using numpy.savez()

Good practices are:
  • save Ves and Struct

  • intermediate optics (Apert and Lens) generally do not need to be saved Indeed, they will be autoamtically included in larger objects like Detect or Cam objects

Parameters
  • SaveName (str) – The file name, if None (recommended) uses obj.Id.SaveName

  • Path (str) – Path where to save the file

  • Mode (str) –

    Flag specifying the saving mode
    • ’npz’: Only mode currently available (‘pck’ deprecated)

  • compressed (bool) – Indicate whether to use np.savez_compressed (slower but smaller files)

tofu.pathfile.SelectFromListId(LId, Val=None, Crit='Name', PreExp=None, PostExp=None, Log='any', InOut='In', Out=<class 'bool'>)[source]

Return the indices or instances of all LOS matching criteria

The selection can be done according to 2 different mechanisms

Mechanism (1): provide the value (Val) a criterion (Crit) should match The criteria are typically attributes of ID (i.e.: name, or user-defined attributes like the camera head…)

Mechanism (2): (used if Val=None) Provide a str expression (or a list of such) to be fed to eval() Used to check on quantitative criteria.

  • PreExp: placed before the criterion value (e.g.: ‘not ‘ or ‘<=’)

  • PostExp: placed after the criterion value

  • you can use both

Other parameters are used to specify logical operators for the selection (match any or all the criterion…) and the type of output.

Parameters
  • Crit (str) –

    Flag indicating which criterion to use for discrimination Can be set to:

    • any attribute of ID (e.g.: ‘Name’,’SaveName’,’SavePath’…)

    • any key of ID.USRdict (e.g.: ‘Exp’…)

  • Val (None / list / str) – The value to match for the chosen criterion, can be a list Used for selection mechanism (1)

  • PreExp (None / list / str) – A str (or list of such) expression to be fed to eval(), Placed before the criterion value Used for selection mechanism (2)

  • PostExp (None / list / str) – A str (or list of such) expression to be fed to eval() Placed after the criterion value Used for selection mechanism (2)

  • Log (str) –

    Flag indicating whether the criterion shall match:
    • ’all’: all provided values

    • ’any’: at least one of them

  • InOut (str) –

    Flag indicating whether the returned indices are:
    • ’In’: the ones matching the criterion

    • ’Out’: the ones not matching it

  • Out (type / str) –

    Flag indicating in which form to return the result:
    • int: as an array of integer indices

    • bool: as an array of boolean indices

    • ’Name’: as a list of names

    • ’LOS’: as a list of LOS instances

Returns

ind (list / np.ndarray) – The computed output, of nature defined by parameter Out

tofu.pathfile.convert_units(P, In='cm', Out='m')[source]

Quickly convert distance units between meters, centimeters and millimeters

tofu.pathfile.get_FileFromInfos(Path='./', Mod=None, Cls=None, Type=None, Name=None, Exp=None, Diag=None, shot=None, Deg=None, version=None, usr=None)[source]
tofu.pathfile.get_InfoFromFileName(PathFileExt)[source]
tofu.pathfile.get_PolyFromPolyFileObj(PolyFileObj, SavePathInp=None, units='m', comments='#', skiprows=0, shape0=2)[source]

Return a polygon as a np.ndarray, extracted from a txt file or from a ToFu object, with appropriate units

Useful for tofu.plugins.AUG.Ves._create()

Parameters
  • PolyFileObj (str / tofu.geom object / np.ndarray) –

    The source where the polygon is to be found, either:
    • str: the name of a file containing the coorindates of a polygon to be loaded with numpy.loadtxt()

    • A tofu.geom object: with attribute ‘Poly’

    • np.ndarray: an 2-dimensional array containing the 2D cartesian coordinates of a polygon

  • SavePathInp (str / None) – The absolute path where the input file is stored

  • units (str) – Flag indicating in which units the polygon coordinates is expressed in the input file / object / array (will be converted to meters)

  • comments (str) – Parameter to be fed to numpy.loadtxt() if PolyFileObj is a file name

  • skiprows (int) – Parameter to be fed to numpy.loadtxt() if PolyFileObj is a file name

  • shape0 (int) – Specifies whether the loaded array is a (2,N) or (3,N) array (transposed it if necessary)

Returns

  • Poly (np.ndarray) – (2,N) np.ndarray containing the 2D cartesian coordinates of the polygon, where N is the number of points

  • addInfo (dict) – Dictionaryb containing information on the origin of the polygon, for the record (e.g.: the name and absolute path of the file from which it was extracted)

tofu.utils module

class tofu.utils.DChans(dchans, fromdict=None)[source]

Bases: object

Base class for handling event on tofu interactive figures

_check_inputs(fd)[source]
_fromdict(fd)[source]
_todict()[source]
property dchans

Return the dchans dict

property nch

Return the dchans dict

select(key=None, val=None, log='any', out=<class 'bool'>)[source]

The the indices of all channels matching the (key,val) pairs

class tofu.utils.Data2DBase(fromdict=None, **kwdargs)[source]

Bases: tofu.utils.ToFuObjectBase

Provide a a dict of sequences depending on 2 indices

_dstrip = {'allowed': None, 'strip': None}
class tofu.utils.Dictattr(extra, *args, **kwdargs)[source]

Bases: dict

tofu.utils.FileNotFoundMsg(pattern, path, lF, nocc=1, ntab=0)[source]
tofu.utils.FindFilePattern(pattern, path, nocc=1, ntab=0)[source]
class tofu.utils.ID(Cls=None, Name=None, Type=None, Deg=None, Exp=None, Diag=None, shot=None, SaveName=None, SavePath=None, usr=None, dUSR=None, lObj=None, fromdict=None, include=None, sep=None)[source]

Bases: tofu.utils.ToFuObjectBase

A class used by all ToFu objects as an attribute

It stores all relevant data for the identification of instances Stored info can be the name of the instance, the experiment and diagnostics it belongs to, or other user-defined info Also provides default names for saving the instances

Parameters
  • Cls (str) – Class of the object on which info should be stored:

  • Name (str) – Name of the instance (user-defined) Should be a str without space ‘ ‘ or underscore ‘_’ (automatically removed if present)

  • Type (None / str) – Type of object (i.e.: ‘Tor’ or ‘Lin’ for a Ves)

  • Deg (None / int) – Degree of the b-splines constituting the tofu.mesh object

  • Exp (None / str) – Flag specifying the experiment (e.g.: ‘WEST’, ‘AUG’, ‘ITER’, ‘JET’…)

  • Diag (None / str) – Flag indicating the diagnostic (e.g.: ‘SXR’, ‘HXR’, ‘Bolo’…)

  • shot (None / int) – A shot number from which the instance is valid (for tracking changes)

  • SaveName (None / str) – Overrides the default file name for saving (not recommended)

  • SavePath (None / str) – Absolute path where the instance should be saved

  • dUSR (None / dict) – A user-defined dictionary containing information about the instance All info considered relevant can be passed here (e.g.: thickness of the diode, date of installation…)

  • lObj (None / dict / list) –

    Either:
    • list: list of other ID instances of objects on which the created object depends (this list will then be sorted by class and formatted into a dictionary storign key attributes)

    • dict: a ready-made such dictionary

property Cls
property Deg
property Diag
property Exp
property Mod
property Name
property NameLTX
property SaveName
static SaveName_Conv(Mod=None, Cls=None, Type=None, Name=None, Deg=None, Exp=None, Diag=None, shot=None, version=None, usr=None, include=None)[source]

Return a default name for saving the object

Includes key info for fast identification of the object from file name Used on object creation by ID It is recommended to use this default name.

property SavePath
property Type
static _checkformat_inputs_dall(usr=None, Cls=None, Type=None, SavePath=None, Exp=None, Diag=None, shot=None, Deg=None, Name=None, SaveName=None, include=None, lObj=None, dUSR=None)[source]
_dModes = {'data': 'TFD', 'geom': 'TFG'}
_dPref = {'Deg': 'Deg', 'Diag': 'Dg', 'Exp': 'Exp', 'shot': 'sh', 'usr': 'U', 'version': 'Vers'}
_defInclude = ['Mod', 'Cls', 'Type', 'Exp', 'Deg', 'Diag', 'Name', 'shot']
_dstrip = {'allowed': [0], 'strip': None}
static _extract_ModClsFrom_class(Cls)[source]
_from_dict(fd)[source]
static _get_keys_dall()[source]
static _get_largs_dall()[source]
_init(usr=None, Cls=None, Type=None, SavePath=None, Exp=None, Diag=None, shot=None, Deg=None, Name=None, SaveName=None, include=None, lObj=None, dUSR=None, **kwdargs)[source]

To be overloaded

_rebuild_dall(lkeep=[])[source]
_reset()[source]

To be overloaded

_set_dall(usr=None, Cls=None, Type=None, SavePath=None, Exp=None, Diag=None, shot=None, Deg=None, Name=None, SaveName=None, include=None, lObj=None, dUSR=None)[source]
_strip(strip=0)[source]
_strip_dall(lkeep=[])[source]
classmethod _strip_init()[source]
_to_dict()[source]
property dUSR
property dall
generate_SaveName(include=None)[source]
property lObj
set_Name(Name, SaveName=None, include=None, ForceUpdate=False)[source]

Set the Name of the instance, automatically updating the SaveName

The name should be a str without spaces or underscores (removed) When the name is changed, if SaveName (i.e. the name used for saving) was not user-defined, it is automatically updated

Parameters
  • Name (str) – Name of the instance, without ‘ ‘ or ‘_’ (automatically removed)

  • SaveName (None / str) – If provided, overrides the default name for saving (not recommended)

  • include (list) – Controls how te default SaveName is generated Each element of the list is a key str indicating whether an element should be present in the SaveName

set_SaveName(SaveName=None, include=None, ForceUpdate=False)[source]

Set the name for saving the instance (SaveName)

SaveName can be either:
  • provided by the user (no constraint) - not recommended

  • automatically generated from Name and key attributes (cf. include)

Parameters
  • SaveName (None / str) – If provided, overrides the default name for saving (not recommended)

  • include (list) – Controls how te default SaveName is generated Each element of the list is a key str indicating whether an element should be present in the SaveName

  • ForceUpdate (bool) –

    Flag indicating the behaviour when SaveName=None:
    • True : A new SaveName is generated, overriding the old one

    • False : The former SaveName is preserved (default)

set_dUSR(dUSR={})[source]

Set the dUSR, containing user-defined info about the instance

Useful for arbitrary info (e.g.: manufacturing date, material…)

Parameters

dUSR (dict) – A user-defined dictionary containing info about the instance

set_lObj(lObj=None)[source]

Set the lObj attribute, storing objects the instance depends on

For example: A Detect object depends on a vessel and some apertures That link between should be stored somewhere (for saving/loading). lObj does this: it stores the ID (as dict) of all objects depended on.

Parameters

lObj (None / dict / ID / list of such) –

Provide either:
  • A dict (derived from _todict())

  • A ID instance

  • A list of dict or ID instances

property shot
strip(strip=0)[source]

Remove non-essential attributes to save memory / disk usage

Useful to save a very compressed version of the object The higher strip => the more stripped the object Use strip=0 to recover all atributes

See the difference by doing:

> self.get_nbytes() > self.strip(-1) > self.get_nbytes()

Parameters

strip (int) –

Flag indicating how much to strip from the object

0: Make complete (retrieve all attributes)

-1: Equivalent to strip=0

property usr
property version
class tofu.utils.KeyHandler_mpl(can=None, dgroup=None, dref=None, ddata=None, dobj=None, dax=None, lax_fix=[], groupinit='time', follow=True)[source]

Bases: object

classmethod _checkformat_dgrouprefaxobj(dgroup, dref, dobj, dax, ddata)[source]
static _get_dcur_init(dgroup, dax, groupinit='time')[source]
_get_dkeys()[source]
classmethod _get_dmovkeys(Type, inc, invert=False)[source]
_ltypesref = ['x', 'y', '2d']
_msgdobj = " Arg dobj must be a dict\n    The keys are handles to matplotlib Artist objects (e.g.: lines, imshow...)\n\n    For each object oo, dobj[oo] is itself a dict with (key,value) pairs:\n        - 'ref'   : a 1D flat np.ndarray, used as reference\n        - 'lgroup': list of int, indicating to which groups of indices oo belongs\n        - 'lind'  : list of int, indicating the index of oo in each group\n        - 'update': a callable (function), to be called when updating\n    "
_set_dBck(lax)[source]
_update_dcur()[source]
_update_dobj()[source]
_update_dref(excluderef=True)[source]
_warn_ifnotInteractive()[source]
connect()[source]
disconnect()[source]
disconnect_old(force=False)[source]
init(dgroup=None, ngroup=None, dobj=None)[source]
mouseclic(event)[source]
mouserelease(event)[source]
onkeypress(event)[source]
reconnect_old()[source]
resize(event)[source]
update(excluderef=True)[source]
class tofu.utils.ToFuObject(fromdict=None, **kwdargs)[source]

Bases: tofu.utils.ToFuObjectBase

property Id

The ID instance of the object

The ID class in tofu objects contains all information used for identifying the instance and automatically managing saving / loading In particular, it yields information such as:

  • Name of the object

  • Name that would be used for the file in case of saving

  • Path under which it would be saved

  • Experiment the object is associated to

  • Diagnostic the object is associated to

  • Type of the object

  • shot the object is associated to

  • version of tofu used for creating the object

static _checkformat_Narray(aa, name='aa', N=3, dim=2, extramsg='')[source]
static _checkformat_scalar(ss, name='ss', extramsg='')[source]
_checkformat_set_move(move, param, kwdargs)[source]
_dstrip = {'allowed': None, 'strip': None}
_get_dId(sep=None)[source]

To be overloaded

static _get_ind12r_n12(ind1=None, ind2=None, n1=None, n2=None)[source]
_move(param, dictname='')[source]
_reset()[source]

To be overloaded

classmethod _rotate_pts_vectors_around_3daxis(pts=None, vect=None, angle=None, axis=None)[source]
classmethod _rotate_pts_vectors_around_torusaxis(pts=None, vect=None, angle=None)[source]
classmethod _rotate_pts_vectors_in_poloidal_plane(pts=None, vect=None, angle=None, phi=None, axis_rz=None)[source]
classmethod _rotate_pts_vectors_in_poloidal_plane_2D(pts_rz=None, vect_rz=None, angle=None, axis_rz=None)[source]
_set_Id(Id=None, Name=None, SaveName=None, SavePath=None, Type=None, Deg=None, Exp=None, Diag=None, shot=None, usr=None, dUSR=None, lObj=None, include=None, **kwdargs)[source]

To be overloaded

_set_dlObj(lObj, din={})[source]
classmethod _translate_pts_3d(pts=None, direction=None, distance=None)[source]
classmethod _translate_pts_poloidal_plane(pts=None, phi=None, direction_rz=None, distance=None)[source]
classmethod _translate_pts_poloidal_plane_2D(pts_rz=None, direction_rz=None, distance=None)[source]
save(path=None, name=None, strip=None, sep=None, deep=True, mode='npz', compressed=False, verb=True, return_pfe=False)[source]

Save the ToFu object

ToFu provides built-in saving and loading functions for ToFu objects. Specifying saving path ad name is optional (defaults recommended) The mode refers to the file format

Good practices are:
  • save all struct objects

Parameters
  • obj (ToFuObject subclass instance) – The object to be saved

  • path (None / str) – The folder where to save, if None (recommended), uses obj.Id.SavePath

  • name (None / str) – The file name, if None (recommended), uses obj.Id.SaveName

  • mode (str) –

    Flag specifying the saving mode
    • ’npz’: numpy file

    • ’mat’: matlab file

  • strip (int) – Flag indicating how stripped the saved object should be See docstring of self.strip()

  • deep (bool) –

    Flag, used when the object has other tofu objects as attributes Indicated whether these attribute object should be:

    • True: converted to dict themselves in order to be saved inside

      the same file as attributes (-> uses self.to_dict(deep=’dict’))

    • False: not converted, in that the strategy would be to save them

      separately and store only the reference to the saved files instead of the objects themselves. To do this, you must:

      1/ Save all object attributes independently 2/ Store only the reference by doing self.strip(-1)

      The strip() method will check they have been saved before removing them, and throw an Exception otherwise

      3/ self.save(deep=False)

  • compressed (bool) – Flag indicating whether to compress the file (slower, not recommended)

  • verb (bool) – Flag indicating whether to print a summary (recommended)

class tofu.utils.ToFuObjectBase(fromdict=None, **kwdargs)[source]

Bases: object

static _check_Fields4Rebuild(dd, lkeep=[], dname='')[source]
static _check_InputsGeneric(ld, tab=0)[source]
_dstrip = {'allowed': None, 'strip': None}
static _extract_kwdargs(din, largs)[source]
_get_dId(sep=None)[source]

To be overloaded

static _get_largs_Id()[source]
classmethod _get_summary(lar, lcol, sep='  ', line='-', just='l', table_sep=None, verb=True, return_=False)[source]

Summary description of the object content as a np.array of str

static _getcharray(ar, col=None, sep='  ', line='-', just='l', msg=True)[source]
abstract _init(**kwdargs)[source]

To be overloaded

abstract _reset()[source]

To be overloaded

_set_Id(*args, **kwdargs)[source]

To be overloaded

_set_arrayorder(arrayorder='C', sep=None, verb=True)[source]
static _strip_dict(dd, lkeep=[])[source]
classmethod _strip_init()[source]

To be overloaded

static _test_Rebuild(dd, lkeep=[])[source]
copy(strip=None, deep='ref')[source]

Return another instance of the object, with the same attributes

If deep=True, all attributes themselves are also copies

from_dict(fd, sep=None, strip=None)[source]

Populate the instances attributes using an input dict

The input dict must be properly formatted In practice it should be the return output of a similar class to_dict()

Parameters
  • fd (dict) – The properly formatted ditionnary from which to read the attributes

  • sep (str) – The separator that was used to format fd keys (cf. self.to_dict())

  • strip (int) – Flag indicating how stripped the resulting object shouyld be (cf. self.strip())

get_nbytes()[source]

Compute and return the object size in bytes (i.e.: octets)

A flat dict containing all the objects attributes is first created The size of each attribute is then estimated with np.asarray().nbytes

Note :

if the attribute is a tofu object, get_nbytes() is recursive

Returns

  • total (int) – The total object estimated size, in bytes

  • dsize (dict) – A dictionnary giving the size of each attribute

strip(strip=0, **kwdargs)[source]

Remove non-essential attributes to save memory / disk usage

Useful to save a very compressed version of the object The higher strip => the more stripped the object Use strip=0 to recover all atributes

See the difference by doing:

> self.get_nbytes() > self.strip(-1) > self.get_nbytes()

Parameters

strip (int) –

Flag indicating how much to strip from the object

0: Make complete (retrieve all attributes){0}

-1: Equivalent to strip={1}

to_dict(strip=None, sep=None, deep='ref')[source]

Return a flat dict view of the object’s attributes

Useful for:
  • displaying all attributes

  • saving to file

  • exchaning data with non-tofu libraries

Parameters
  • strip (int) – Flag indicating how stripped the object should be Fed to self.strip()

  • sep (str) – Separator char used for flattening the dict The output dict is flat (i.e.: no nested dict) Keys are created from the keys of nested dict, separated by sep

  • deep (str) –

    Flag indicating how to behave when an attribute is itself a tofu object. The associated field in the exported dict can be:

    • ’ref’ : a simple reference to the object

    • ’copy’: a tofu object itself (i.e.: a copy of the original)

    • ’dict’: the tofu object is itself exported as a dict

      (using also self.to_dict())

Returns

dout (dict) – Flat dict containing all the objects attributes

tofu.utils._check_InputsGeneric(ld, tab=0)[source]
tofu.utils._check_notNone(dd, lk)[source]
tofu.utils._filefind(name, path=None, lmodes=['.npz', '.mat'])[source]
tofu.utils._get_attrdictfromobj(obj, dd)[source]
tofu.utils._get_exception(q, ids, qtype='quantity')[source]
tofu.utils._get_load_npzmat_dict(out, pfe, mode='npz', exclude_keys=[])[source]
tofu.utils._load_from_txt(name, pfe, Name=None, Exp=None)[source]
tofu.utils._load_mat(pfe)[source]
tofu.utils._load_npz(pfe, allow_pickle=None)[source]
tofu.utils._reshape_dict(ss, vv, dinit={}, sep=None)[source]
tofu.utils._save_mat(dd, pathfileext, sep=None, compressed=False)[source]
tofu.utils._save_npz(dd, pathfileext, sep=None, compressed=False)[source]
tofu.utils._save_npzmat_dict(dd, sep=None)[source]
tofu.utils._set_arrayorder(obj, arrayorder='C', sep=None)[source]

Set the memory order of all np.ndarrays in a tofu object

tofu.utils.calc_from_imas(shot=None, run=None, user=None, database=None, version=None, shot_eq=None, run_eq=None, user_eq=None, database_eq=None, shot_prof=None, run_prof=None, user_prof=None, database_prof=None, ids=None, Name=None, out=None, tlim=None, config=None, occ=None, indch=None, description_2d=None, equilibrium=None, dsig=None, data=None, X=None, t0=None, dextra=None, Brightness=None, res=None, interp_t=None, extra=None, plot=None, plot_compare=True, sharex=False, input_file=None, output_file=None, coefs=None, bck=True, indch_auto=True, t=None, init=None)[source]

Calculate syntehtic signal for a diagnostic

Read the geometry from an idd (database, user, shot, run) Read the equilibrium from the same / another idd Read the profile from the same / another idd

tofu.utils.dict_cmp(d1, d2)[source]
tofu.utils.flatten_dict(d, parent_key='', sep=None, deep='ref', lexcept_key=[])[source]
tofu.utils.from_txt_extract_params(pfe=None, lparams=None, comments=None)[source]

Extract key parameters stored as commented lines at top of file

tofu saves some data to txt files with the following formatting:
  • a series of commented (#) lines, each with format:

    # key: value

  • a 1d or 2d np.ndarray

This routine extracts the desired parameters from the commented lines

tofu.utils.get_figuresize(fs, fsdef=(12, 6), orient='landscape', method='xrandr')[source]

Generic function to return figure size in inches

Useful for str-based flags such as:
  • ‘a4’ : use orient=’portrait’ or ‘landscape’

  • ‘full’: to get screen size

    use method=’xrandr’ (recommended), as ‘xdpyinfo’ tends to be wrong

tofu.utils.get_fupdate(obj, typ, n12=None, norm=None, bstr=None)[source]
tofu.utils.get_ind_fromkey(dmovkeys={}, nn=[], is2d=False)[source]
tofu.utils.get_ind_frompos(Type='x', ref=None, ref2=None, otherid=None, indother=None)[source]
tofu.utils.get_indncurind(dind, linds)[source]
tofu.utils.get_indrefind(dind, linds, drefid)[source]
tofu.utils.get_param_from_file_name(pfe=None, lparams=None, test=True)[source]

Try to extract desired parameter from file name

tofu typically saves files with names formatted as:

XXX_Key0param0_Key1param1_Key2param2…

Where:
  • XXX corresponds to the sub-package that created the file

  • Keyiparami are (key, value) parameter pairs, when possible

tofu.utils.get_pathfileext(path=None, name=None, path_def='./', name_def='dummy', mode='npz')[source]
tofu.utils.get_pos_fromind(ref=None, ref2=None, otherid=None, indother=None)[source]
tofu.utils.get_todictfields(ld, ls)[source]
tofu.utils.get_valf(val, lrids, linds)[source]
tofu.utils.load(name, path=None, strip=None, verb=True, allow_pickle=None)[source]

Load a tofu object file

Can load from .npz or .txt files

In future versions, will also load from .mat

The file must have been saved with tofu (i.e.: must be tofu-formatted) The associated tofu object will be created and returned

Parameters
  • name (str) – Name of the file to load from, can include the path

  • path (None / str) – Path where the file is located (if not provided in name), defaults ‘./’

  • strip (None / int) –

    FLag indicating whether to strip the object of some attributes

    => see the docstring of the class strip() method for details

  • verb (bool) – Flag indocating whether to print a summary of the loaded file

tofu.utils.load_from_imas(shot=None, run=None, user=None, database=None, version=None, ids=None, Name=None, returnas=None, tlim=None, occ=None, indch=None, description_2d=None, equilibrium=None, dsig=None, data=None, X=None, t0=None, dextra=None, plot=True, plot_sig=None, plot_X=None, sharex=False, invertx=None, extra=True, bck=True, indch_auto=True, t=None, config=None, tosoledge3x=None, mag_init_pts=None, mag_sep_dR=None, mag_sep_nbpts=None)[source]
tofu.utils.reshape_dict(d, sep=None, lcls=[])[source]
tofu.utils.save(obj, path=None, name=None, sep=None, deep=False, mode='npz', strip=None, compressed=False, verb=True, return_pfe=False)[source]

Save the ToFu object

ToFu provides built-in saving and loading functions for ToFu objects. Specifying saving path ad name is optional (defaults recommended) The mode refers to the file format

Good practices are:
  • save all struct objects

Parameters
  • obj (ToFuObject subclass instance) – The object to be saved

  • path (None / str) – The folder where to save, if None (recommended), uses obj.Id.SavePath

  • name (None / str) – The file name, if None (recommended), uses obj.Id.SaveName

  • mode (str) –

    Flag specifying the saving mode
    • ’npz’: numpy file

    • ’mat’: matlab file

  • strip (int) – Flag indicating how stripped the saved object should be See docstring of self.strip()

  • deep (bool) –

    Flag, used when the object has other tofu objects as attributes Indicated whether these attribute object should be:

    • True: converted to dict themselves in order to be saved inside

      the same file as attributes (-> uses self.to_dict(deep=’dict’))

    • False: not converted, in that the strategy would be to save them

      separately and store only the reference to the saved files instead of the objects themselves. To do this, you must:

      1/ Save all object attributes independently 2/ Store only the reference by doing self.strip(-1)

      The strip() method will check they have been saved before removing them, and throw an Exception otherwise

      3/ self.save(deep=False)

  • compressed (bool) – Flag indicating whether to compress the file (slower, not recommended)

  • verb (bool) – Flag indicating whether to print a summary (recommended)

tofu.version module