tofu package¶
ToFu: A python library for Tomography for Fusion research¶
- Provides
Objects to handle 3D geometry of diagnostics
Built-in methods for computing key quantities, data treatment, inversions…
Visualisation tools
How to use the documentation¶
- Documentation is available in two forms:
Docstrings provided with the code
Online sphinx-generated documentation, available from
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¶
- tofu.data package
- tofu.dumpro package
- tofu.dust package
- tofu.entrypoints package
- tofu.geom package
- Submodules
- tofu.geom._GG module
- tofu.geom._GG module
- tofu.geom._basic_geom_tools module
- tofu.geom._basic_geom_tools module
- tofu.geom._comp module
- tofu.geom._comp_optics module
- tofu.geom._comp_solidangles module
- tofu.geom._core module
- tofu.geom._core_optics module
- tofu.geom._def module
- tofu.geom._def_config module
- tofu.geom._distance_tools module
- tofu.geom._distance_tools module
- tofu.geom._openmp_tools module
- tofu.geom._openmp_tools module
- tofu.geom._plot module
- tofu.geom._plot_optics module
- tofu.geom._raytracing_tools module
- tofu.geom._raytracing_tools module
- tofu.geom._sampling_tools module
- tofu.geom._sampling_tools module
- tofu.geom._vignetting_tools module
- tofu.geom._vignetting_tools module
- tofu.geom.openmp_enabled module
- tofu.geom.utils module
- tofu.openadas2tofu package
- tofu.tests package
- Subpackages
- tofu.tests.tests00_root package
- tofu.tests.tests01_geom package
- tofu.tests.tests02_data package
- tofu.tests.tests03_openadas package
- tofu.tests.tests09_tutorials package
- 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.InvSigmad = {'facecolor': (0.8, 0.8, 0.8, 0.7), 'lw': 0.0}¶
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
objectExp (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]¶
- 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.
- 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
andStruct
intermediate optics (
Apert
andLens
) 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_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 nameskiprows (int) – Parameter to be fed to
numpy.loadtxt()
if PolyFileObj is a file nameshape0 (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
- property dchans¶
Return the dchans dict
- property nch¶
Return the dchans dict
- 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.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
objectExp (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}¶
- _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
- _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]¶
- property dUSR¶
- property dall¶
- 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.
- 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
- _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 "¶
- 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
…
- _dstrip = {'allowed': None, 'strip': None}¶
- 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
- 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
- _dstrip = {'allowed': None, 'strip': None}¶
- 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
- 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()
- 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._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.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_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.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.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)