Converter Utilites¶
One goal for this project is to allow users to use serpentTools
as a
pipeline for taking SERPENT data to other programs.
The first demonstration of this is creating binary .mat
files that can
be read into MATLAB, when a large text file would cause issues, done with
the MatlabConverter
.
Classes intending do first look for specific methods for gathering data
on each object. These are obscured from the public API as they don’t serve
a direct purpose besides a simple method by which to translate data
from one place to the next.
This collection is called inside the primary convert
method,
which is reponsible for gathering and writing the data into the new
form.
Keeping with the MatlabConverter
as an example, it looks for
_gather_matlab
method. This method simply places data into
a dictionary, but do to the layout of each object, must be reimplemented
as a unique method.
Due to the simplicity of scipy.io.savemat()
, the main writing function,
serpentTools.io.base.MatlabConverter.convert()
, is rather light.
However, some data forms may require more depth to ensure fidelity in the
converted data.
There may be some cases where a conversion does not make sense for all data types. Detector objects are the few pieces of data that can have a spatial grid attached, yet this grid varies across detectors. It may be advantageous to implement converters directly as instance methods. The decision will be up to the developer and the review process in that case.
Converters should also make sure, prior to potentially expensive collection
steps are taken, that the user is in the best position to execute this
operation. As of this writing and 0.6.1, scipy
is not a requirement for this package, yet it is required for MATLAB
conversion. Therefore, the MatlabConverter
implements a check for scipy
in checkImports()
. The other
check method, checkContainerReq()
,
is responsible for checking the container has the required data gathering
routines, e.g. _gather_matlab
.
These checks are called at creation of the converter object.
-
class
serpentTools.io.base.
MatlabConverter
(container, output)¶ Class for assisting in writing container data to MATLAB
- Parameters
obj (
serpentTools
container) – Parser or container to be written to filefileP (str or file-like object) – Name of the file to write
See also
serpentTools.io.toMatlab()
- high level function for writing to MATLAB that uses this class
-
checkContainerReq
(container)¶ Ensure that data from the container can be collected.
-
convert
(reconvert, append=True, format='5', longNames=True, compress=True, oned='row')¶ Save contents of object to
.mat
file- Parameters
fileP (str or file-like object) – Name of the file to write
reconvert (bool) – If this evaluates to true, convert values back into their original form as they appear in the output file, e.g.
MAT_TOTAL_ING_TOX
. Otherwise, maintain themixedCase
style,total_ingTox
.append (bool) – If true and a file exists under
output
, append to that file. Otherwise the file will be overwrittenformat ({'5', '4'}) – Format of file to write.
'5'
for MATLAB 5 to 7.2,'4'
for MATLAB 4longNames (bool) – If true, allow variable names to reach 63 characters, which works with MATLAB 7.6+. Otherwise, maximum length is 31 characters
compress (bool) – If true, compress matrices on write
oned ({'row', 'col'}:) – Write one-dimensional arrays as row vectors if
oned=='row'
(default), or column vectors
See also