3. tofu.pathfile

Created on Wed Jul 30 14:37:31 2014

@author: didiervezinet

tofu.pathfile.Find_Rootpath(Path='/afs/ipp-garching.mpg.de/home/d/didiv/Python/tofu/doc', substr='/tofu')[source]

Return the absolute path of the root directory of ToFu, searching for a pattern in the provided input path

Parameters:
  • Path (str) – An absolute path in which a pattern is to be looked for
  • substr (str) – The pattern to look for
Returns:

root (str) – The absolute path containing the pattern

tofu.pathfile.get_DefaultPaths(RootPath=None, Plugin=None, lSubs=[], PathInp=None, PathObj=None, PathOut=None)[source]

Return the default paths for input loading, object and output saving depending on the root path used

tofu.pathfile.get_Default_dtimeFmt(dtime=None, dtFormat='D%Y%m%d_T%H%M%S')[source]

Return the default datetime value and format

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

Quickly convert distance units between meters, centimeters and millimeters

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)

class tofu.pathfile.ID(Cls, Name, Type=None, Deg=None, Exp=None, Diag=None, shot=None, SaveName=None, SavePath=None, USRdict=None, LObj=None, dtime=None, dtFormat='D%Y%m%d_T%H%M%S', dtimeIn=False)[source]

A class used by all ToFu objects as an attribute, storing all relevant data for the identification of created instances and providing default path and names for saving objects

Each detector created in ToFu should be identifiable thanks to user-friendly criteria, like its name, the diagnostic and experiment it is attached to, the shot number from from it was physically installed on the experiement... Users can also freely add some information they consider relevant, such as some characteristics of the detector (size, manufacturer, date of last calibration...)

Parameters:
  • Cls (str) –
    Flag indicating which class is the object fro which the ID instance is being created, available class flags are:
    • From tofu.geom: ‘Ves’, ‘Struct’, ‘LOS’, ‘GLOS’, ‘Apert’, ‘Lens’, ‘Detect’ and ‘GDetect’
    • From tofu.mesh: ‘Mesh1D’, ‘Mesh2D’, ‘LBF1D’ and ‘LBF2D’ (to be finished)
    • From tofu.: (to be completed)
  • Name (str) – Name to be assigned to the created instance, should be a str without ‘ ‘ or ‘_’ (spaces and underscores will be automatically removed if present)
  • Type (None / str) – If provided (necessary for some objects, but not for all), specifies the Type of object (i.e.: ‘Tor’ or ‘Lin’ for a Ves instance)
  • Deg (None / int) – If provided (necessary only for objects of class ‘LBF1D’ and ‘LBF2D’), specifies the degree of the b-splines constituting the tofu.mesh object
  • Exp (None / str) – A short 3-4 letters max flag specifying the experiment to which the created instance belongs (e.g.: ‘AUG’, ‘ITER’, ‘TCV’, ‘JET’...)
  • Diag (None / str) – A short flag indicating which diagnostic the the created instance belongs to (e.g.: ‘SXR’, ‘HXR’, ‘Bolo’...)
  • shot (None / int) – A shot number from which the created instance can be considered valid (useful for tracking geometry changes in the case of Ves and Detect objects)
  • SaveName (None / str) – If provided, overrides the automatically generated name for saving the created instance (not recommended)
  • SavePath (None / str) – If provided, overrides the automatically generated path for saving the created instance
  • USRdict (None / dict) – If provided, a user-defined dictionary containing information about the instance considered relevant (e.g.: thickness of the diode, date of installation...)
  • LObj (None / dict / list) –
    If provided, 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
  • dtime (None / dtm.datetime) – If provided, a time reference to be used to identify this particular instance (used for debugging mostly)
  • dtFormat (None / str) – If provided, the format in which dtime should be written in the automatically generated saving name for the created instance
  • dtimeIn (bool) – Flag indicating whether the dtm.datetime should be included in the automatically generated saving name for the created instance
Returns:

Id ID instance – The created ID instance, with all necessary computed attributes and methods

set_LObj(LObj=None)[source]

Set the LObj attribute to store the list of ID of other objects the created instance depends on

A Detect object depends on a vessel and some optics (e.g.: a list of apertures). It is necessary that the link between the created Detect object and the already-existing Ves and Apert objects be stored somewhere, so that even after saving and closing the session, this correspondence can be retrieved and the Detect object can be re-loaded with links to the proper Ves and Apert objects, themselves beoing possibly saved elsewhere (so their respective SavePath must also be stored). The LObj parameter does this: it stores all information necessary about each of the other objects the created instance depends on, mostly by storing their ID attributes as dictionaries.

Parameters:LObj (None / dict / list) –
If provided, 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
set_Name(Name, SaveName=None, dtimeIn=None)[source]

Set the Name of the created instance, automatically updating the SaveName

When the name is changed (reminding it should not include space (‘ ‘) or underscore (‘_’) characters), the name used for saving the object is automatically changed

Parameters:
  • Name (str) – Name to be assigned to the created instance, should be a str without ‘ ‘ or ‘_’ (spaces and underscores will be automatically removed if present)
  • SaveName (None / str) – If provided, overrides the automatically generated name for saving the created instance (not recommended)
  • dtimeIn (None / bool) – If provided, modifies the value of dtimeIn provided upon creation of the ID object
set_SaveName(SaveName=None, dtimeIn=None)[source]

Enables either to automatically compute a ToFu-consistent saving name for the created instance, or to override that default saving name with the user-provided SaveName

When creating an object of any class, an ID object is assigned to it that automatically computes a saving name in case the user wants to save the object. This automatically generated saving name includes not only the name given to the instance but also useful classifying info such as the class of the object, its Type, experiment, diagnostic... It is not recommended to override this automatically generated saving name (because some routines rely on it for fast identification of saved files), but it is made possible for flexibility.

Parameters:
  • SaveName (None / str) – If provided, a str that overrides the automatically generated saving name
  • dtimeIn (None / bool) – If provided, modifies the value of dtimeIn provided upon creation of the ID object
set_SavePath(SavePath=None)[source]

Enables to automatically generate a saving path for the created object, or to override that default path with the user-provided SavePath

Similarily to SaveName, ToFu automatically generates a saving path for any created object. This saving path can be overriden if desired. This is less crucial than SaveName, changing the SavePath has little consequences as long as you remember what you are doing.

Parameters:SavePath (None / str) – If provided, a str that overrides the automatically generated saving path
set_USRdict(USRdict=None)[source]

Set the USRdict to enable the user to store arbitrary information about the instance created

It may sometimes be useful to store unforeseen arbitrary info about some objects created, like the manufacturing date of a diode, the material used for a filter... The USRdict attribute is a user-defined dictionary for this purpose.

Parameters:USRdict (None / dict) – If provided, a user-defined dictionary containing information about the instance considered relevant (e.g.: thickness of the diode, date of installation...)
tofu.pathfile.CheckSameObj(obj0, obj1, LFields=None)[source]

Check two variable refer to the same instance of a ToFu class by checking some key attributes

Sometimes two different variables can refer to the same instance (for example if an object was created and assigned to obj0, then later saved and loaded and assigned to obj1). Occasionally it may be useful to check whether two variables really represent the same instance, according to important criteria from the point of view of ToFu.

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 any class (but the same class as obj0)
  • LFields (None / str / list) –
    The criteria against which the two objects are evaluated, if not None, must be str matching an attribute of the ID class or an attribute of the object class itself (or a list of such)
    • None: True is returned
    • str or list: tests whether the attributes have the same value or not and only returns True if all do
Returns:

A (bool)

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

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

ToFu provides built-in saving and loading functions for ToFu objects. They can be saved at their default SavePath under their default SaveName or user-defined values can be forced if necessary. Saving can be done in two ways :

  • by direct object saving using cPickle (straightforward but heavy)
  • by mapping the key object attributes to a dictionary and using numpy.savez_compressed() (faster and lighter, recommended)

ToFu now automatically saves information on smaller objects on which the object of interest depends (like apertures for detectors), so that all info is stored in a single file. In particular, provided the Ves object is saved separately, a whole camera can be saved in a single file (i.e.: all detectors and apertures).

Parameters:
  • SaveName (str) – The name to be used to for the saved file, if None (recommended) uses obj.Id.SaveName
  • Path (str) – Path specifying where to save the file, if None (recommended) uses obj.Id.SavePath
  • Mode (str) – Flag specifying whether to save the object as a numpy array file (‘.npz’, recommended) or an object using cPickle (not recommended, may cause retro-compatibility issues with later versions)
  • compressed (bool) – Flag, used when Mode=’npz’, indicating whether to use np.savez or np.savez_compressed (slower saving and loading but smaller files)
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)[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

3.1. Indices and tables