Changeset 35488b2 in sasview for src/sas


Ignore:
Timestamp:
Aug 9, 2016 10:13:13 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:
0b1a677
Parents:
514a00e
Message:

Perform BSL file conversion on a separate thread

Location:
src/sas
Files:
1 added
1 edited

Legend:

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

    r514a00e r35488b2  
    1919from sas.sasgui.guiframe.documentation_window import DocumentationWindow 
    2020from sas.sasgui.guiframe.dataFitting import Data1D 
    21 from sas.sascalc.dataloader.data_info import Data2D 
    2221from sas.sasgui.guiframe.utils import check_float 
    2322from sas.sasgui.perspectives.file_converter.cansas_writer import CansasWriter 
    24 from sas.sascalc.dataloader.readers.red2d_reader import Reader as Red2DWriter 
     23from sas.sascalc.file_converter.convert_bsl_thread import ConvertBSLThread 
    2524from sas.sasgui.perspectives.file_converter.otoko_loader import OTOKOLoader 
    26 from sas.sascalc.file_converter.bsl_loader import BSLLoader 
    2725from sas.sascalc.dataloader.data_info import Detector 
    2826from sas.sascalc.dataloader.data_info import Sample 
     
    5856        self.parent = parent 
    5957        self.meta_frames = [] 
     58        self.bsl_thread = None 
    6059 
    6160        # GUI inputs 
     
    6463        self.output = None 
    6564        self.radiation_input = None 
     65        self.convert_btn = None 
    6666        self.metadata_section = None 
    6767 
     
    115115                writer.write(destination, [frame_data], 
    116116                    sasentry_attrs=entry_attrs) 
    117  
    118     def convert_to_red2d(self, filepath, x, y, frame_data): 
    119         """ 
    120         Writes Data2D objects to Red2D .dat files. If more than one frame is 
    121         provided, the frame number will be appended to the filename of each 
    122         file written. 
    123  
    124         :param filepath: The filepath to write to 
    125         :param x: The x column of the data 
    126         :param y: The y column of the data 
    127         :param frame_data: A dictionary of the form frame_number: data, where 
    128         data is a 2D numpy array containing the intensity data 
    129         """ 
    130         filename = os.path.split(filepath)[-1] 
    131         filepath = os.path.split(filepath)[0] 
    132         writer = Red2DWriter() 
    133  
    134         for i, frame in frame_data.iteritems(): 
    135             # If more than 1 frame is being exported, append the frame 
    136             # number to the filename 
    137             if len(frame_data) > 1: 
    138                 frame_filename = filename.split('.') 
    139                 frame_filename[0] += str(i+1) 
    140                 frame_filename = '.'.join(frame_filename) 
    141             else: 
    142                 frame_filename = filename 
    143  
    144             data_i = frame.reshape((len(x),1)) 
    145             data_info = Data2D(data=data_i, qx_data=x, qy_data=y) 
    146             writer.write(os.path.join(filepath, frame_filename), data_info) 
    147117 
    148118    def extract_ascii_data(self, filename): 
     
    212182 
    213183        return qdata, iqdata 
    214  
    215     def extract_bsl_data(self, filename): 
    216         """ 
    217         Extracts data from a 2D BSL file 
    218  
    219         :param filename: The header file to extract the data from 
    220         :return x_data: A 1D array containing all the x coordinates of the data 
    221         :return y_data: A 1D array containing all the y coordinates of the data 
    222         :return frame_data: A dictionary of the form frame_number: data, where 
    223         data is a 2D numpy array containing the intensity data 
    224         """ 
    225         loader = BSLLoader(filename) 
    226         frames = [0] 
    227         should_continue = True 
    228  
    229         if loader.n_frames > 1: 
    230             params = self.ask_frame_range(loader.n_frames) 
    231             frames = params['frames'] 
    232         elif loader.n_rasters == 1 and loader.n_frames == 1: 
    233             message = ("The selected file is an OTOKO file. Please select the " 
    234             "'OTOKO 1D' option if you wish to convert it.") 
    235             dlg = wx.MessageDialog(self, 
    236             message, 
    237             'Error!', 
    238             wx.OK | wx.ICON_WARNING) 
    239             dlg.ShowModal() 
    240             should_continue = False 
    241             dlg.Destroy() 
    242         else: 
    243             message = ("The selected data file only has 1 frame, it might be" 
    244                 " a multi-frame OTOKO file.\nContinue conversion?") 
    245             dlg = wx.MessageDialog(self, 
    246             message, 
    247             'Warning!', 
    248             wx.YES_NO | wx.ICON_WARNING) 
    249             should_continue = (dlg.ShowModal() == wx.ID_YES) 
    250             dlg.Destroy() 
    251  
    252         if not should_continue: 
    253             return None, None, None 
    254  
    255         frame_data = {} 
    256  
    257         for frame in frames: 
    258             loader.frame = frame 
    259             frame_data[frame] = loader.load_data() 
    260  
    261         # TODO: Tidy this up 
    262         # Prepare axes values (arbitrary scale) 
    263         x_data = [] 
    264         y_data = range(loader.n_pixels) * loader.n_rasters 
    265         for i in range(loader.n_rasters): 
    266             x_data += [i] * loader.n_pixels 
    267  
    268         return x_data, y_data, frame_data 
    269184 
    270185    def ask_frame_range(self, n_frames): 
     
    320235            return 
    321236 
     237        if self.bsl_thread is not None and self.bsl_thread.isrunning(): 
     238            self.bsl_thread.stop() 
     239            self.conversion_complete(success=False) 
     240            return 
     241 
    322242        self.sample.ID = self.title 
    323243 
     
    329249                qdata, iqdata = self.extract_otoko_data(self.q_input.GetPath()) 
    330250            else: # self.data_type == 'bsl' 
    331                 x_data, y_data, frame_data = self.extract_bsl_data( 
    332                     self.iq_input.GetPath()) 
    333  
    334                 if x_data == None and y_data == None and frame_data == None: 
    335                     wx.PostEvent(self.parent.manager.parent, 
    336                         StatusEvent(status="Conversion cancelled.")) 
    337                     return 
    338  
    339                 file_path = self.output.GetPath() 
    340                 self.convert_to_red2d(file_path, x_data, y_data, frame_data) 
    341  
    342                 wx.PostEvent(self.parent.manager.parent, 
    343                     StatusEvent(status="Conversion completed.")) 
     251                self.bsl_thread = ConvertBSLThread(self, self.iq_input.GetPath(), 
     252                    self.output.GetPath(), updatefn=self.conversion_update, 
     253                    completefn=self.conversion_complete) 
     254                self.bsl_thread.queue() 
     255                self.convert_btn.SetLabel("Stop Conversion") 
    344256                return 
    345257 
     
    408320            StatusEvent(status="Conversion completed.")) 
    409321 
     322    def conversion_update(self, msg="", exception=None): 
     323        if exception is not None: 
     324            msg = str(exception) 
     325            wx.PostEvent(self.parent.manager.parent, 
     326                StatusEvent(status=msg, info='error')) 
     327        else: 
     328            wx.PostEvent(self.parent.manager.parent, 
     329                StatusEvent(status=msg)) 
     330 
     331    def conversion_complete(self, success=True): 
     332        self.convert_btn.SetLabel("Convert") 
     333        msg = "Conversion " 
     334        if success: 
     335            msg += "completed" 
     336        else: 
     337            msg += "failed" 
     338        wx.PostEvent(self.parent.manager.parent, 
     339            StatusEvent(status=msg)) 
     340 
     341 
    410342    def on_help(self, event): 
    411343        """ 
     
    424356            msg += "n Intensity input file." 
    425357        elif self.output.GetPath() == '': 
    426             msg += "destination for the converted file." 
     358            msg += " destination for the converted file." 
    427359        if msg != "You must select a": 
    428360            wx.PostEvent(self.parent.manager.parent, 
     
    597529        y += 1 
    598530 
    599         convert_btn = wx.Button(self, wx.ID_OK, "Convert") 
    600         input_grid.Add(convert_btn, (y,0), (1,1), wx.ALL, 5) 
    601         convert_btn.Bind(wx.EVT_BUTTON, self.on_convert) 
     531        self.convert_btn = wx.Button(self, wx.ID_OK, "Stop Converstion") 
     532        self.convert_btn.SetLabel("Convert") 
     533        input_grid.Add(self.convert_btn, (y,0), (1,1), wx.ALL, 5) 
     534        self.convert_btn.Bind(wx.EVT_BUTTON, self.on_convert) 
    602535 
    603536        help_btn = wx.Button(self, -1, "HELP") 
Note: See TracChangeset for help on using the changeset viewer.