Changeset ba65aff in sasview for src/sas


Ignore:
Timestamp:
Aug 8, 2016 10:09:38 AM (8 years ago)
Author:
lewis
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
9e9f848
Parents:
d6bf064
Message:

Add docstrings & comments, and rename old bsl_loader to otoko_loader

Location:
src/sas/sasgui/perspectives/file_converter
Files:
5 edited
1 moved

Legend:

Unmodified
Added
Removed
  • src/sas/sasgui/perspectives/file_converter/converter_panel.py

    rd6bf064 rba65aff  
    2323from sas.sasgui.perspectives.file_converter.cansas_writer import CansasWriter 
    2424from sas.sascalc.dataloader.readers.red2d_reader import Reader as Red2DWriter 
    25 from sas.sasgui.perspectives.file_converter.bsl_loader import BSLLoader as OTOKOLoader 
     25from sas.sasgui.perspectives.file_converter.otoko_loader import OTOKOLoader 
    2626from sas.sascalc.file_converter.bsl_loader import BSLLoader 
    2727from sas.sascalc.dataloader.data_info import Detector 
     
    4545 
    4646class ConverterPanel(ScrolledPanel, PanelBase): 
     47    """ 
     48    This class provides the File Converter GUI 
     49    """ 
    4750 
    4851    def __init__(self, parent, base=None, *args, **kwargs): 
     
    5659        self.meta_frames = [] 
    5760 
     61        # GUI inputs 
    5862        self.q_input = None 
    5963        self.iq_input = None 
     
    6165        self.radiation_input = None 
    6266        self.metadata_section = None 
     67 
    6368        self.data_type = "ascii" 
    6469 
     70        # Metadata values 
    6571        self.title = None 
    6672        self.run = None 
     
    8086 
    8187    def convert_to_cansas(self, frame_data, filename, single_file): 
    82         reader = CansasWriter() 
     88        """ 
     89        Saves an array of Data1D objects to a single CanSAS file with multiple 
     90        <SasData> elements, or to multiple CanSAS files, each with one 
     91        <SasData> element. 
     92 
     93        :param frame_data: The Data1D object to save 
     94        :param filename: Where to save the CanSAS file 
     95        :param single_file: If true, array is saved as a single file, if false, 
     96        each item in the array is saved to it's own file 
     97        """ 
     98        writer = CansasWriter() 
    8399        entry_attrs = None 
    84100        if self.run_name is not None: 
    85101            entry_attrs = { 'name': self.run_name } 
    86102        if single_file: 
    87             reader.write(filename, frame_data, 
     103            writer.write(filename, frame_data, 
    88104                sasentry_attrs=entry_attrs) 
    89105        else: 
     
    92108            name = filename.replace(ext, '') 
    93109            for i in range(len(frame_data)): 
    94                 f_name = "{}{}{}".format(name, i+1, ext) 
    95                 reader.write(f_name, [frame_data[i]], 
     110                # TODO: Change i to actual frame number, not consecutive numbers 
     111                # (maybe use info in params from frame select dialog, ie 
     112                # increment. Alternatively, change frame_data to a dictionary 
     113                # with the frame number as key and use frame_data.iteritems() 
     114                # (would need to update CansasWriter to deal with this) 
     115                f_name = name + str(i) + ext 
     116                writer.write(f_name, [frame_data[i]], 
    96117                    sasentry_attrs=entry_attrs) 
    97118 
    98119    def extract_ascii_data(self, filename): 
     120        """ 
     121        Extracts data from a single-column ASCII file 
     122 
     123        :param filename: The file to load data from 
     124        :return: A numpy array containing the extracted data 
     125        """ 
    99126        data = np.loadtxt(filename, dtype=str) 
    100127 
     
    122149 
    123150    def extract_otoko_data(self, filename): 
     151        """ 
     152        Extracts data from a 1D OTOKO file 
     153 
     154        :param filename: The OTOKO file to load the data from 
     155        :return: A numpy array containing the extracted data 
     156        """ 
    124157        loader = OTOKOLoader(self.q_input.GetPath(), 
    125158            self.iq_input.GetPath()) 
     
    139172 
    140173    def ask_frame_range(self, n_frames): 
     174        """ 
     175        Display a dialog asking the user to input the range of frames they 
     176        would like to export 
     177 
     178        :param n_frames: How many frames the loaded data file has 
     179        :return: A dictionary containing the parameters input by the user 
     180        """ 
    141181        valid_input = False 
    142182        is_bsl = (self.data_type == 'bsl') 
     
    179219 
    180220    def on_convert(self, event): 
     221        """Called when the Convert button is clicked""" 
    181222        if not self.validate_inputs(): 
    182223            return 
     
    191232                qdata, iqdata = self.extract_otoko_data(self.q_input.GetPath()) 
    192233            else: # self.data_type == 'bsl' 
     234                # TODO: Refactor this into an extract_bsl_data method 
    193235                loader = BSLLoader(self.iq_input.GetPath()) 
    194236                frames = [0] 
     
    243285        # Standard file has 3 frames: SAS, calibration and WAS 
    244286        if n_frames > 3: 
    245             # File has multiple frames 
     287            # File has multiple frames - ask the user which ones they want to 
     288            # export 
    246289            params = self.ask_frame_range(n_frames) 
    247290            frames = params['frames'] 
     
    254297        output_path = self.output.GetPath() 
    255298 
     299        # Prepare the metadata for writing to a file 
    256300        if self.run is None: 
    257301            self.run = [] 
     
    293337 
    294338    def on_help(self, event): 
     339        """ 
     340        Show the File Converter documentation 
     341        """ 
    295342        tree_location = ("user/sasgui/perspectives/file_converter/" 
    296343            "file_converter_help.html") 
     
    314361 
    315362    def show_detector_window(self, event): 
     363        """ 
     364        Show the window for inputting :class:`~sas.sascalc.dataloader.data_info.Detector~` metadata 
     365        """ 
    316366        if self.meta_frames != []: 
    317367            for frame in self.meta_frames: 
     
    325375 
    326376    def show_sample_window(self, event): 
     377        """ 
     378        Show the window for inputting :class:`~sas.sascalc.dataloader.data_info.Sample~` metadata 
     379        """ 
    327380        if self.meta_frames != []: 
    328381            for frame in self.meta_frames: 
     
    336389 
    337390    def show_source_window(self, event): 
     391        """ 
     392        Show the window for inputting :class:`~sas.sascalc.dataloader.data_info.Source~` metadata 
     393        """ 
    338394        if self.meta_frames != []: 
    339395            for frame in self.meta_frames: 
     
    347403 
    348404    def on_collapsible_pane(self, event): 
     405        """ 
     406        Resize the scrollable area to fit the metadata pane when it's 
     407        collapsed or expanded 
     408        """ 
    349409        self.Freeze() 
    350410        self.SetupScrolling() 
     
    353413 
    354414    def datatype_changed(self, event): 
     415        """ 
     416        Update the UI and self.data_type when a data type radio button is 
     417        pressed 
     418        """ 
    355419        event.Skip() 
    356420        dtype = event.GetEventObject().GetName() 
     
    527591 
    528592class ConverterWindow(widget.CHILD_FRAME): 
     593    """Displays ConverterPanel""" 
    529594 
    530595    def __init__(self, parent=None, title='File Converter', base=None, 
  • src/sas/sasgui/perspectives/file_converter/converter_widgets.py

    raf84162 rba65aff  
    6363 
    6464    def SetValue(self, vector): 
     65        """ 
     66        Set the value of the vector input 
     67 
     68        :param vector: A Vector object 
     69        """ 
    6570        directions = ['x', 'y'] 
    6671        if self.z_enabled: directions.append('z') 
  • src/sas/sasgui/perspectives/file_converter/file_converter.py

    r77d92cd rba65aff  
    88 
    99class Plugin(PluginBase): 
     10    """ 
     11    This class defines the interface for a Plugin class 
     12    for File Converter perspective 
     13    """ 
    1014 
    1115    def __init__(self): 
  • src/sas/sasgui/perspectives/file_converter/frame_select_dialog.py

    r05595c4 rba65aff  
    22 
    33class FrameSelectDialog(wx.Dialog): 
     4    """ 
     5    This class provides a wx.Dialog subclass for selecting which frames of a 
     6    multi-frame file to export 
     7    """ 
    48 
    59    def __init__(self, n_frames, is_bsl=False): 
  • src/sas/sasgui/perspectives/file_converter/meta_panels.py

    r503cc34 rba65aff  
    2121 
    2222class MetadataPanel(ScrolledPanel, PanelBase): 
     23    """ 
     24    A common base class to be extended by panels that deal with metadata input. 
     25    Handles input validation and passing inputted data back to ConverterPanel. 
     26    """ 
    2327 
    2428    def __init__(self, parent, metadata, base=None, *args, **kwargs): 
     
    3034        self.base = base 
    3135        self.parent = parent 
    32         self._to_validate = [] 
    33         self._vectors = [] 
     36        self._to_validate = [] # An list of inputs that should contain floats 
     37        self._vectors = [] # A list of VectorInputs to be validated 
    3438        self.metadata = metadata 
    3539 
  • src/sas/sasgui/perspectives/file_converter/otoko_loader.py

    r4b862c4 rba65aff  
    1515        self.__dict__.update(kwds) 
    1616 
    17 class BSLParsingError(Exception): 
     17class OTOKOParsingError(Exception): 
    1818    pass 
    1919 
    20 class BSLData: 
     20class OTOKOData: 
    2121    def __init__(self, q_axis, data_axis): 
    2222        self.q_axis = q_axis 
    2323        self.data_axis = data_axis 
    2424 
    25 class BSLLoader(object): 
     25class OTOKOLoader(object): 
    2626 
    2727    def __init__(self, qaxis_path, data_path): 
     
    3636 
    3737        Given the paths of two header files, this function will load each axis in 
    38         turn.  If loading is successfull then an instance of the BSLData class 
     38        turn.  If loading is successfull then an instance of the OTOKOData class 
    3939        will be returned, else an exception will be raised. 
    4040 
     
    5050        data_axis = self._load_bsl_axis(self.data_path) 
    5151 
    52         return BSLData(q_axis, data_axis) 
     52        return OTOKOData(q_axis, data_axis) 
    5353 
    5454    def _load_bsl_axis(self, header_path): 
     
    6060        """ 
    6161        if not os.path.exists(header_path): 
    62             raise BSLParsingError("The header file %s does not exist." % header_path) 
     62            raise OTOKOParsingError("The header file %s does not exist." % header_path) 
    6363 
    6464        binary_file_info_list = [] 
     
    6969            lines = header_file.readlines() 
    7070            if len(lines) < 4: 
    71                 raise BSLParsingError("Expected more lines in %s." % header_path) 
     71                raise OTOKOParsingError("Expected more lines in %s." % header_path) 
    7272 
    7373            info = lines[0] + lines[1] 
     
    8585 
    8686                if len(indicators) != 10: 
    87                     raise BSLParsingError( 
     87                    raise OTOKOParsingError( 
    8888                        "Expected 10 integer indicators on line 3 of %s." \ 
    8989                        % header_path) 
    9090                if not all([i.isdigit() for i in indicators]): 
    91                     raise BSLParsingError( 
     91                    raise OTOKOParsingError( 
    9292                        "Expected all indicators on line 3 of %s to be integers." \ 
    9393                        % header_path) 
     
    115115        all_n_channels = [info.n_channels for info in binary_file_info_list] 
    116116        if not all(all_n_channels[0] == c for c in all_n_channels): 
    117             raise BSLParsingError( 
     117            raise OTOKOParsingError( 
    118118                "Expected all binary files listed in %s to have the same number of channels." % header_path) 
    119119 
     
    123123        for info in binary_file_info_list: 
    124124            if not os.path.exists(info.file_path): 
    125                 raise BSLParsingError( 
     125                raise OTOKOParsingError( 
    126126                    "The data file %s does not exist." % info.file_path) 
    127127 
Note: See TracChangeset for help on using the changeset viewer.