-
Notifications
You must be signed in to change notification settings - Fork 3
AB's proposal for refactoring file operations
Go back: Horace framework redesign ideas
AB also presented a proposal to rewrite (refactor) parts of the core Matlab Horace code:
This section will deal with a new proposed iSQW class to help with file handling.
Another section deals with his ideas for sqw.
This mainly involves rewriting the current object-oriented code to be better standards conforming. In particular the refactored classes will have a clearer inheritance structure which follows syntax introduced by Matlab since Horace was originally written.
As a concrete example, take the cut_sqw function which is used to rebin the data including integration over some specified axes.
This function branches depending on the type (class) of the first argument, which can be one of:
- A
dndobject - An
sqwobject - A filename (the file could contain either a
dndorsqwobject)
cut_sqw uses functions in the horace_function_utils folder to parse the argument and construct a structure which determines whether the input is a file or in-memory object and which type (sqw or dnd) it is.
- If the input is a
dndobject ordndfile,cut_sqwcalls thecutmethod of the relevantdndclass.- However, this method is just a wrapper around the
cutmethod of thesqwclass.
- However, this method is just a wrapper around the
- If the input is a
sqwobject orsqwfile,cut_sqwcalls thecutmethod of thesqwclass directly.
sqw.cut then calls the parsing function to parse its input again, in order to catch the case where it is called directly on a sqw in-memory object rather than from cut_sqw.
sqw.cut then branches again depending on the type of the input:
- It calls
cut_sqw_mainif the object is ansqw.- This in turn wraps either:
-
cut_data_from_file(if the input is a file) -
cut_data_from_array(if the input is an in-memory object)
-
- This in turn wraps either:
- It calls
cut_dnd_mainif the object is adnd.- If the input is a file,
cut_dnd_mainloads the entire file into memory. - It then calls
cut_dnd_calc_ubinsto do the rebin.
- If the input is a file,
This is an extreme example, but several other functions (e.g. head, refine_crystal/change_crystal, set_sample/set_instrument) have similar indirections to branch depending on class and whether the input is a file or in-memory object.
This makes the code convoluted, hard to read/understand, and hence hard to maintain.
The proposed change is to introduce an "abstract base class" called iSQW, which will have virtual methods for the usual sqw/dnd methods (e.g. plot, cut, plus, minus etc), in addition to two extra methods:
-
loadwill load data from a file, including a file-backed version (loads only headers not pixel information). save
This will formalise the current "dummy" structure generated by the routines in horace_function_utils when something like cut_sqw is called.
The new design, however, will have the sqw and dnd classes being derived from iSQW which means that these classes will be able to call load and save transparently.
sqw and dnd will still define their own methods separately, and calling cut on such an in-memory object will work the same as it does now.
However, calling these methods on files will folow a much simpler workflow:
-
cut_sqwchecks whether the input is a file or object.- If it is a file, it calls the
loadmethod ofiSQW.- If the file has pixel information, this
loadwill create ansqwobject, if not it creates adndobject.
- If the file has pixel information, this
-
cut_sqwthen calls thecutmethod ofiSQW.- Because
sqwanddndare derived fromiSQWand the objects are polymorphic, the correct concretecutmethods (of eithersqwordnd) are automatically called (sinceiSQW.cutis virtual).
- Because
- If it is a file, it calls the
It is planned that iSQW.load could also create a file-backed sqw object if the input file is sqw-type.
This means that it would not load the pixel information (only the headers and pre-calculated binned ["image"] data).
Implementation details is not clear, but it might be a new derived class, e.g. file_sqw
Go back: Horace framework redesign ideas