Changeset c1c14ba in sasview


Ignore:
Timestamp:
Mar 6, 2015 12:25:51 PM (9 years ago)
Author:
Doucet, Mathieu <doucetm@…>
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, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
8b21fa7
Parents:
c4f6851
Message:

pylint fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/perspectives/pr/pr.py

    r79492222 rc1c14ba  
    1  
     1""" 
     2    P(r) perspective for SasView 
     3""" 
    24################################################################################ 
    35#This software was developed by the University of Tennessee as part of the 
    46#Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
    5 #project funded by the US National Science Foundation.  
     7#project funded by the US National Science Foundation. 
    68# 
    79#See the license text in license.txt 
     
    1113 
    1214 
    13 # Make sure the option of saving each curve is available  
     15# Make sure the option of saving each curve is available 
    1416# Use the I(q) curve as input and compare the output to P(r) 
    1517 
    16 import os 
    1718import sys 
    1819import wx 
     
    2526from sas.guiframe.dataFitting import Data1D 
    2627from sas.guiframe.events import NewPlotEvent 
    27 from sas.guiframe.events import StatusEvent  
    28 from sas.guiframe.gui_style import GUIFRAME_ID    
     28from sas.guiframe.events import StatusEvent 
     29from sas.guiframe.gui_style import GUIFRAME_ID 
    2930from sas.pr.invertor import Invertor 
    3031from sas.dataloader.loader import Loader 
    3132import sas.dataloader 
    3233 
    33 from pr_widgets import load_error  
     34from pr_widgets import load_error 
    3435from sas.guiframe.plugin_base import PluginBase 
    3536 
    3637 
    37 PR_FIT_LABEL       = r"$P_{fit}(r)$" 
    38 PR_LOADED_LABEL    = r"$P_{loaded}(r)$" 
    39 IQ_DATA_LABEL      = r"$I_{obs}(q)$" 
    40 IQ_FIT_LABEL       = r"$I_{fit}(q)$" 
    41 IQ_SMEARED_LABEL   = r"$I_{smeared}(q)$" 
     38PR_FIT_LABEL = r"$P_{fit}(r)$" 
     39PR_LOADED_LABEL = r"$P_{loaded}(r)$" 
     40IQ_DATA_LABEL = r"$I_{obs}(q)$" 
     41IQ_FIT_LABEL = r"$I_{fit}(q)$" 
     42IQ_SMEARED_LABEL = r"$I_{smeared}(q)$" 
    4243GROUP_ID_IQ_DATA = r"$I_{obs}(q)$" 
    4344GROUP_ID_PR_FIT = r"$P_{fit}(r)$" 
     
    5152    DEFAULT_ALPHA = 0.0001 
    5253    DEFAULT_NFUNC = 10 
    53     DEFAULT_DMAX  = 140.0 
    54      
     54    DEFAULT_DMAX = 140.0 
     55 
    5556    def __init__(self, standalone=True): 
    5657        PluginBase.__init__(self, name="Pr Inversion", standalone=standalone) 
    5758        ## Simulation window manager 
    5859        self.simview = None 
    59         
     60 
    6061        ## State data 
    61         self.alpha      = self.DEFAULT_ALPHA 
    62         self.nfunc      = self.DEFAULT_NFUNC 
     62        self.alpha = self.DEFAULT_ALPHA 
     63        self.nfunc = self.DEFAULT_NFUNC 
    6364        self.max_length = self.DEFAULT_DMAX 
    64         self.q_min      = None 
    65         self.q_max      = None 
    66         self.has_bck    = False 
     65        self.q_min = None 
     66        self.q_max = None 
     67        self.has_bck = False 
    6768        self.slit_height = 0 
    68         self.slit_width  = 0 
     69        self.slit_width = 0 
    6970        ## Remember last plottable processed 
    70         self.last_data  = "sphere_60_q0_2.txt" 
     71        self.last_data = "" 
    7172        self._current_file_data = None 
    7273        ## Time elapsed for last computation [sec] 
     
    7475        self.elapsed = 0.022 
    7576        self.iq_data_shown = False 
    76          
     77 
    7778        ## Current invertor 
    78         self.invertor    = None 
    79         self.pr          = None 
    80         self.data_id = IQ_DATA_LABEL  
     79        self.invertor = None 
     80        self.pr = None 
     81        self.data_id = IQ_DATA_LABEL 
    8182        # Copy of the last result in case we need to display it. 
    82         self._last_pr    = None 
    83         self._last_out   = None 
    84         self._last_cov   = None 
     83        self._last_pr = None 
     84        self._last_out = None 
     85        self._last_cov = None 
    8586        ## Calculation thread 
    8687        self.calc_thread = None 
     
    9798        self._normalize_output = False 
    9899        self._scale_output_unity = False 
    99          
     100 
    100101        ## List of added P(r) plots 
    101102        self._added_plots = {} 
    102         self._default_Iq  = {} 
     103        self._default_Iq = {} 
    103104        self.list_plot_id = [] 
    104          
     105 
    105106        # Associate the inversion state reader with .prv files 
    106107        from inversion_state import Reader 
    107           
     108 
    108109        # Create a CanSAS/Pr reader 
    109110        self.state_reader = Reader(self.set_state) 
     
    112113        l.associate_file_reader('.prv', self.state_reader) 
    113114        #l.associate_file_reader(".svs", self.state_reader) 
    114                  
     115 
    115116        # Log startup 
    116117        logging.info("Pr(r) plug-in started") 
    117          
     118 
    118119    def delete_data(self, data_id): 
    119120        """ 
     
    121122        """ 
    122123        self.control_panel.clear_panel() 
    123         
     124 
    124125    def get_data(self): 
    125126        """ 
     
    127128        """ 
    128129        return self.current_plottable 
    129      
     130 
    130131    def set_state(self, state=None, datainfo=None): 
    131132        """ 
    132133        Call-back method for the inversion state reader. 
    133134        This method is called when a .prv file is loaded. 
    134          
     135 
    135136        :param state: InversionState object 
    136137        :param datainfo: Data1D object [optional] 
    137          
     138 
    138139        """ 
    139140        try: 
     
    146147                data = datainfo 
    147148            if data is None: 
    148                 msg =  "Pr.set_state: datainfo parameter cannot " 
     149                msg = "Pr.set_state: datainfo parameter cannot " 
    149150                msg += "be None in standalone mode" 
    150151                raise RuntimeError, msg 
    151              
     152 
    152153            # Ensuring that plots are coordinated correctly 
    153154            t = time.localtime(data.meta_data['prstate'].timestamp) 
    154155            time_str = time.strftime("%b %d %H:%M", t) 
    155              
     156 
    156157            # Check that no time stamp is already appended 
    157158            max_char = data.meta_data['prstate'].file.find("[") 
    158159            if max_char < 0: 
    159160                max_char = len(data.meta_data['prstate'].file) 
    160              
    161             datainfo.meta_data['prstate'].file =\ 
     161 
     162            datainfo.meta_data['prstate'].file = \ 
    162163                data.meta_data['prstate'].file[0:max_char]\ 
    163164                + ' [' + time_str + ']' 
    164              
     165 
    165166            data.filename = data.meta_data['prstate'].file 
    166167            # TODO: 
     
    170171            self.current_plottable = self.parent.create_gui_data(data, None) 
    171172            self.current_plottable.group_id = data.meta_data['prstate'].file 
    172              
     173 
    173174            # Make sure the user sees the P(r) panel after loading 
    174             #self.parent.set_perspective(self.perspective)   
    175             self.on_perspective(event=None)    
     175            #self.parent.set_perspective(self.perspective) 
     176            self.on_perspective(event=None) 
    176177            # Load the P(r) results 
    177178            #state = self.state_reader.get_state() 
     
    179180            self.parent.add_data(data_list=data_dict) 
    180181            wx.PostEvent(self.parent, NewPlotEvent(plot=self.current_plottable, 
    181                                         title=self.current_plottable.title)) 
     182                                                   title=self.current_plottable.title)) 
    182183            self.control_panel.set_state(state) 
    183184        except: 
    184185            logging.error("prview.set_state: %s" % sys.exc_value) 
    185186 
    186    
     187 
    187188    def help(self, evt): 
    188189        """ 
    189         Show a general help dialog.  
    190          
     190        Show a general help dialog. 
     191 
    191192        :TODO: replace the text with a nice image 
    192          
     193 
    193194        """ 
    194195        from inversion_panel import HelpDialog 
     
    198199        else: 
    199200            dialog.Destroy() 
    200      
     201 
    201202    def _fit_pr(self, evt): 
    202203        """ 
     
    204205        # Generate P(r) for sphere 
    205206        radius = 60.0 
    206         d_max  = 2*radius 
    207          
    208         r = pylab.arange(0.01, d_max, d_max/51.0) 
     207        d_max = 2 * radius 
     208 
     209        r = pylab.arange(0.01, d_max, d_max / 51.0) 
    209210        M = len(r) 
    210211        y = numpy.zeros(M) 
    211212        pr_err = numpy.zeros(M) 
    212          
    213         sum = 0.0 
     213 
     214        total = 0.0 
    214215        for j in range(M): 
    215216            value = self.pr_theory(r[j], radius) 
    216             sum += value 
     217            total += value 
    217218            y[j] = value 
    218219            pr_err[j] = math.sqrt(y[j]) 
    219220 
    220         y = y/sum*d_max/len(r) 
     221        y = y / total * d_max / len(r) 
    221222 
    222223        # Perform fit 
     
    230231        for i in range(len(out)): 
    231232            print "%g +- %g" % (out[i], math.sqrt(cov[i][i])) 
    232          
     233 
    233234        # Show input P(r) 
    234235        title = "Pr" 
     
    236237        new_plot.name = "P_{obs}(r)" 
    237238        new_plot.xaxis("\\rm{r}", 'A') 
    238         new_plot.yaxis("\\rm{P(r)} ","cm^{-3}") 
     239        new_plot.yaxis("\\rm{P(r)} ", "cm^{-3}") 
    239240        new_plot.group_id = "P_{obs}(r)" 
    240241        new_plot.id = "P_{obs}(r)" 
    241242        new_plot.title = title 
    242         self.parent.update_theory(data_id=self.data_id,  theory=new_plot) 
     243        self.parent.update_theory(data_id=self.data_id, theory=new_plot) 
    243244        wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=title)) 
    244245 
    245246        # Show P(r) fit 
    246247        self.show_pr(out, pr) 
    247          
     248 
    248249        # Show I(q) fit 
    249         q = pylab.arange(0.001, 0.1, 0.01/51.0) 
     250        q = pylab.arange(0.001, 0.1, 0.01 / 51.0) 
    250251        self.show_iq(out, pr, q) 
    251          
     252 
    252253    def show_shpere(self, x, radius=70.0, x_range=70.0): 
    253254        """ 
     
    258259        sum_true = 0.0 
    259260        for i in range(len(x)): 
    260             y_true[i] = self.pr_theory(x[i], radius)             
     261            y_true[i] = self.pr_theory(x[i], radius) 
    261262            sum_true += y_true[i] 
    262              
    263         y_true = y_true/sum_true*x_range/len(x) 
    264          
     263 
     264        y_true = y_true / sum_true * x_range / len(x) 
     265 
    265266        # Show the theory P(r) 
    266267        new_plot = Data1D(x, y_true) 
     
    268269        new_plot.name = "P_{true}(r)" 
    269270        new_plot.xaxis("\\rm{r}", 'A') 
    270         new_plot.yaxis("\\rm{P(r)} ","cm^{-3}") 
     271        new_plot.yaxis("\\rm{P(r)} ", "cm^{-3}") 
    271272        new_plot.id = "P_{true}(r)" 
    272273        new_plot.group_id = "P_{true}(r)" 
    273274        self.parent.update_theory(data_id=self.data_id, theory=new_plot) 
    274         #Put this call in plottables/guitools     
     275        #Put this call in plottables/guitools 
    275276        wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, 
    276                                                 title="Sphere P(r)")) 
    277         
    278          
     277                                               title="Sphere P(r)")) 
     278 
    279279    def get_npts(self): 
    280280        """ 
     
    285285        except: 
    286286            return 0 
    287          
     287 
    288288    def show_iq(self, out, pr, q=None): 
    289289        """ 
    290         """   
     290            Display computed I(q) 
     291        """ 
    291292        qtemp = pr.x 
    292293        if not q == None: 
     
    296297        maxq = -1 
    297298        for q_i in qtemp: 
    298             if q_i>maxq: 
    299                 maxq=q_i 
    300                  
     299            if q_i > maxq: 
     300                maxq = q_i 
     301 
    301302        minq = 0.001 
    302          
     303 
    303304        # Check for user min/max 
    304305        if not pr.q_min == None: 
     
    306307        if not pr.q_max == None: 
    307308            maxq = pr.q_max 
    308                  
    309         x = pylab.arange(minq, maxq, maxq/301.0) 
     309 
     310        x = pylab.arange(minq, maxq, maxq / 301.0) 
    310311        y = numpy.zeros(len(x)) 
    311312        err = numpy.zeros(len(x)) 
     
    318319                err[i] = 1.0 
    319320                print "Error getting error", value, x[i] 
    320                  
     321 
    321322        new_plot = Data1D(x, y) 
    322323        new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM 
    323324        new_plot.name = IQ_FIT_LABEL 
    324325        new_plot.xaxis("\\rm{Q}", 'A^{-1}') 
    325         new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") 
     326        new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}") 
    326327        title = "I(q)" 
    327328        new_plot.title = title 
    328          
     329 
    329330        # If we have a group ID, use it 
    330331        if pr.info.has_key("plot_group_id"): 
     
    332333        new_plot.id = IQ_FIT_LABEL 
    333334        self.parent.update_theory(data_id=self.data_id, theory=new_plot) 
    334          
     335 
    335336        wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=title)) 
    336          
     337 
    337338        # If we have used slit smearing, plot the smeared I(q) too 
    338339        if pr.slit_width > 0 or pr.slit_height > 0: 
    339             x = pylab.arange(minq, maxq, maxq/301.0) 
     340            x = pylab.arange(minq, maxq, maxq / 301.0) 
    340341            y = numpy.zeros(len(x)) 
    341342            err = numpy.zeros(len(x)) 
     
    348349                    err[i] = 1.0 
    349350                    print "Error getting error", value, x[i] 
    350                      
     351 
    351352            new_plot = Data1D(x, y) 
    352353            new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM 
    353354            new_plot.name = IQ_SMEARED_LABEL 
    354355            new_plot.xaxis("\\rm{Q}", 'A^{-1}') 
    355             new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") 
     356            new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}") 
    356357            # If we have a group ID, use it 
    357358            if pr.info.has_key("plot_group_id"): 
    358               new_plot.group_id = pr.info["plot_group_id"] 
     359                new_plot.group_id = pr.info["plot_group_id"] 
    359360            new_plot.id = IQ_SMEARED_LABEL 
    360361            new_plot.title = title 
    361362            self.parent.update_theory(data_id=self.data_id, theory=new_plot) 
    362363            wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=title)) 
    363          
     364 
    364365    def _on_pr_npts(self, evt): 
    365366        """ 
    366367        Redisplay P(r) with a different number of points 
    367         """    
     368        """ 
    368369        from inversion_panel import PrDistDialog 
    369370        dialog = PrDistDialog(None, -1) 
    370371        dialog.set_content(self._pr_npts) 
    371372        if dialog.ShowModal() == wx.ID_OK: 
    372             self._pr_npts= dialog.get_content() 
     373            self._pr_npts = dialog.get_content() 
    373374            dialog.Destroy() 
    374375            self.show_pr(self._last_out, self._last_pr, self._last_cov) 
    375376        else: 
    376377            dialog.Destroy() 
    377          
    378          
     378 
     379 
    379380    def show_pr(self, out, pr, cov=None): 
    380381        """ 
    381         """      
     382        """ 
    382383        # Show P(r) 
    383         x = pylab.arange(0.0, pr.d_max, pr.d_max/self._pr_npts) 
    384      
     384        x = pylab.arange(0.0, pr.d_max, pr.d_max / self._pr_npts) 
     385 
    385386        y = numpy.zeros(len(x)) 
    386387        dy = numpy.zeros(len(x)) 
    387388        y_true = numpy.zeros(len(x)) 
    388389 
    389         sum = 0.0 
     390        total = 0.0 
    390391        pmax = 0.0 
    391392        cov2 = numpy.ascontiguousarray(cov) 
    392          
     393 
    393394        for i in range(len(x)): 
    394             if cov2==None: 
     395            if cov2 == None: 
    395396                value = pr.pr(out, x[i]) 
    396397            else: 
    397398                (value, dy[i]) = pr.pr_err(out, cov2, x[i]) 
    398             sum += value*pr.d_max/len(x) 
    399              
     399            total += value * pr.d_max / len(x) 
     400 
    400401            # keep track of the maximum P(r) value 
    401             if value>pmax: 
     402            if value > pmax: 
    402403                pmax = value 
    403                  
     404 
    404405            y[i] = value 
    405                  
    406         if self._normalize_output==True: 
    407             y = y/sum 
    408             dy = dy/sum 
    409         elif self._scale_output_unity==True: 
    410             y = y/pmax 
    411             dy = dy/pmax 
    412          
    413         if cov2==None: 
     406 
     407        if self._normalize_output == True: 
     408            y = y / total 
     409            dy = dy / total 
     410        elif self._scale_output_unity == True: 
     411            y = y / pmax 
     412            dy = dy / pmax 
     413 
     414        if cov2 == None: 
    414415            new_plot = Data1D(x, y) 
    415416            new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM 
     
    418419        new_plot.name = PR_FIT_LABEL 
    419420        new_plot.xaxis("\\rm{r}", 'A') 
    420         new_plot.yaxis("\\rm{P(r)} ","cm^{-3}") 
     421        new_plot.yaxis("\\rm{P(r)} ", "cm^{-3}") 
    421422        new_plot.title = "P(r) fit" 
    422423        new_plot.id = PR_FIT_LABEL 
    423424        # Make sure that the plot is linear 
    424425        new_plot.xtransform = "x" 
    425         new_plot.ytransform = "y"   
     426        new_plot.ytransform = "y" 
    426427        new_plot.group_id = GROUP_ID_PR_FIT 
    427         self.parent.update_theory(data_id=self.data_id, theory=new_plot)     
     428        self.parent.update_theory(data_id=self.data_id, theory=new_plot) 
    428429        wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="P(r) fit")) 
    429430        return x, pr.d_max 
    430            
     431 
    431432    def load(self, data): 
    432433        """ 
     
    434435        by our standard DataLoader class. 
    435436        """ 
    436         class FileData: 
     437        class FileData(object): 
    437438            x = None 
    438439            y = None 
    439440            err = None 
    440441            path = None 
    441              
     442 
    442443            def __init__(self, path): 
    443444                self.path = path 
    444                  
     445 
    445446        self._current_file_data = FileData(data.path) 
    446          
     447 
    447448        # Use data loader to load file 
    448         #dataread = Loader().load(path) 
    449449        dataread = data 
    450450        # Notify the user if we could not read the file 
    451451        if dataread is None: 
    452452            raise RuntimeError, "Invalid data" 
    453              
     453 
    454454        x = None 
    455455        y = None 
     
    460460            err = dataread.dy 
    461461        else: 
    462             if isinstance(dataread, list) and len(dataread)>0: 
     462            if isinstance(dataread, list) and len(dataread) > 0: 
    463463                x = dataread[0].x 
    464464                y = dataread[0].y 
    465465                err = dataread[0].dy 
    466466                msg = "PrView only allows a single data set at a time. " 
    467                 msg += "Only the first data set was loaded."  
     467                msg += "Only the first data set was loaded." 
    468468                wx.PostEvent(self.parent, StatusEvent(status=msg)) 
    469469            else: 
     
    471471                    return x, y, err 
    472472                raise RuntimeError, "This tool can only read 1D data" 
    473          
     473 
    474474        self._current_file_data.x = x 
    475475        self._current_file_data.y = y 
    476476        self._current_file_data.err = err 
    477477        return x, y, err 
    478                  
    479     def load_columns(self, path = "sphere_60_q0_2.txt"): 
     478 
     479    def load_columns(self, path="sphere_60_q0_2.txt"): 
    480480        """ 
    481481        Load 2- or 3- column ascii 
    482482        """ 
    483483        # Read the data from the data file 
    484         data_x   = numpy.zeros(0) 
    485         data_y   = numpy.zeros(0) 
     484        data_x = numpy.zeros(0) 
     485        data_y = numpy.zeros(0) 
    486486        data_err = numpy.zeros(0) 
    487         scale    = None 
    488         min_err  = 0.0 
     487        scale = None 
     488        min_err = 0.0 
    489489        if not path == None: 
    490             input_f = open(path,'r') 
    491             buff    = input_f.read() 
    492             lines   = buff.split('\n') 
     490            input_f = open(path, 'r') 
     491            buff = input_f.read() 
     492            lines = buff.split('\n') 
    493493            for line in lines: 
    494494                try: 
     
    496496                    x = float(toks[0]) 
    497497                    y = float(toks[1]) 
    498                     if len(toks)>2: 
     498                    if len(toks) > 2: 
    499499                        err = float(toks[2]) 
    500500                    else: 
    501                         if scale==None: 
    502                             scale = 0.05*math.sqrt(y) 
     501                        if scale == None: 
     502                            scale = 0.05 * math.sqrt(y) 
    503503                            #scale = 0.05/math.sqrt(y) 
    504                             min_err = 0.01*y 
    505                         err = scale*math.sqrt(y)+min_err 
     504                            min_err = 0.01 * y 
     505                        err = scale * math.sqrt(y) + min_err 
    506506                        #err = 0 
    507                          
     507 
    508508                    data_x = numpy.append(data_x, x) 
    509509                    data_y = numpy.append(data_y, y) 
    510510                    data_err = numpy.append(data_err, err) 
    511511                except: 
    512                     pass 
    513                     
    514         if not scale==None: 
     512                    logging.error(sys.exc_value) 
     513 
     514        if not scale == None: 
    515515            message = "The loaded file had no error bars, statistical errors are assumed." 
    516516            wx.PostEvent(self.parent, StatusEvent(status=message)) 
    517517        else: 
    518518            wx.PostEvent(self.parent, StatusEvent(status='')) 
    519                          
    520         return data_x, data_y, data_err      
    521          
     519 
     520        return data_x, data_y, data_err 
     521 
    522522    def load_abs(self, path): 
    523523        """ 
    524524        Load an IGOR .ABS reduced file 
    525          
     525 
    526526        :param path: file path 
    527          
     527 
    528528        :return: x, y, err vectors 
    529          
     529 
    530530        """ 
    531531        # Read the data from the data file 
    532         data_x   = numpy.zeros(0) 
    533         data_y   = numpy.zeros(0) 
     532        data_x = numpy.zeros(0) 
     533        data_y = numpy.zeros(0) 
    534534        data_err = numpy.zeros(0) 
    535         scale    = None 
    536         min_err  = 0.0 
    537          
     535        scale = None 
     536        min_err = 0.0 
     537 
    538538        data_started = False 
    539539        if not path == None: 
    540             input_f = open(path,'r') 
    541             buff    = input_f.read() 
    542             lines   = buff.split('\n') 
     540            input_f = open(path, 'r') 
     541            buff = input_f.read() 
     542            lines = buff.split('\n') 
    543543            for line in lines: 
    544544                if data_started == True: 
     
    547547                        x = float(toks[0]) 
    548548                        y = float(toks[1]) 
    549                         if len(toks)>2: 
     549                        if len(toks) > 2: 
    550550                            err = float(toks[2]) 
    551551                        else: 
    552552                            if scale == None: 
    553                                 scale = 0.05*math.sqrt(y) 
     553                                scale = 0.05 * math.sqrt(y) 
    554554                                #scale = 0.05/math.sqrt(y) 
    555                                 min_err = 0.01*y 
    556                             err = scale*math.sqrt(y)+min_err 
     555                                min_err = 0.01 * y 
     556                            err = scale * math.sqrt(y) + min_err 
    557557                            #err = 0 
    558                              
     558 
    559559                        data_x = numpy.append(data_x, x) 
    560560                        data_y = numpy.append(data_y, y) 
    561561                        data_err = numpy.append(data_err, err) 
    562562                    except: 
    563                         pass 
    564                 elif line.find("The 6 columns")>=0: 
    565                     data_started = True       
    566                     
     563                        logging.error(sys.exc_value) 
     564                elif line.find("The 6 columns") >= 0: 
     565                    data_started = True 
     566 
    567567        if not scale == None: 
    568568            message = "The loaded file had no error bars, statistical errors are assumed." 
     
    570570        else: 
    571571            wx.PostEvent(self.parent, StatusEvent(status='')) 
    572                          
    573         return data_x, data_y, data_err      
    574          
     572 
     573        return data_x, data_y, data_err 
     574 
    575575    def pr_theory(self, r, R): 
    576         """   
    577             Return P(r) of a sphere for a given R  
     576        """ 
     577            Return P(r) of a sphere for a given R 
    578578            For test purposes 
    579579        """ 
    580         if r <= 2*R: 
    581             return 12.0* ((0.5*r/R)**2) * ((1.0-0.5*r/R)**2) * ( 2.0 + 0.5*r/R ) 
     580        if r <= 2 * R: 
     581            return 12.0 * ((0.5 * r / R) ** 2) * ((1.0 - 0.5 * r / R) ** 2) * (2.0 + 0.5 * r / R) 
    582582        else: 
    583583            return 0.0 
     
    586586        """ 
    587587        Get the context menu items available for P(r) 
    588          
     588 
    589589        :param graph: the Graph object to which we attach the context menu 
    590          
     590 
    591591        :return: a list of menu items with call-back function 
    592          
     592 
    593593        """ 
    594594        graph = plotpanel.graph 
     
    603603            change_n_hint += " points on the P(r) output" 
    604604            change_n_label = "Change number of P(r) points" 
    605             m_list = [[change_n_label, change_n_hint , self._on_pr_npts]] 
     605            m_list = [[change_n_label, change_n_hint, self._on_pr_npts]] 
    606606 
    607607            if self._scale_output_unity or self._normalize_output: 
    608608                hint = "Let the output P(r) keep the scale of the data" 
    609                 m_list.append(["Disable P(r) scaling", hint,  
     609                m_list.append(["Disable P(r) scaling", hint, 
    610610                               self._on_disable_scaling]) 
    611611            if not self._scale_output_unity: 
    612                 m_list.append(["Scale P_max(r) to unity",  
    613                                "Scale P(r) so that its maximum is 1",  
     612                m_list.append(["Scale P_max(r) to unity", 
     613                               "Scale P(r) so that its maximum is 1", 
    614614                               self._on_scale_unity]) 
    615615            if not self._normalize_output: 
    616                 m_list.append(["Normalize P(r) to unity",  
    617                                "Normalize the integral of P(r) to 1",  
     616                m_list.append(["Normalize P(r) to unity", 
     617                               "Normalize the integral of P(r) to 1", 
    618618                               self._on_normalize]) 
    619                  
     619 
    620620            return m_list 
    621               
    622         elif item.id in [PR_LOADED_LABEL, IQ_DATA_LABEL, IQ_FIT_LABEL, 
    623                           IQ_SMEARED_LABEL]: 
     621 
     622        elif item.id in [PR_LOADED_LABEL, IQ_DATA_LABEL, IQ_FIT_LABEL, IQ_SMEARED_LABEL]: 
    624623            return [] 
    625624        elif item.id == graph.selected_plottable: 
    626            if not self.standalone and issubclass(item.__class__, Data1D): 
    627                 return [["Compute P(r)",  
    628                              "Compute P(r) from distribution",  
    629                              self._on_context_inversion]]       
    630                  
     625            if not self.standalone and issubclass(item.__class__, Data1D): 
     626                return [["Compute P(r)", 
     627                         "Compute P(r) from distribution", 
     628                         self._on_context_inversion]] 
    631629        return [] 
    632630 
     
    634632        """ 
    635633        Disable P(r) scaling 
    636              
     634 
    637635        :param evt: Menu event 
    638          
     636 
    639637        """ 
    640638        self._normalize_output = False 
    641639        self._scale_output_unity = False 
    642640        self.show_pr(self._last_out, self._last_pr, self._last_cov) 
    643          
     641 
    644642        # Now replot the original added data 
    645643        for plot in self._added_plots: 
    646644            self._added_plots[plot].y = numpy.copy(self._default_Iq[plot]) 
    647             wx.PostEvent(self.parent,  
    648                          NewPlotEvent(plot=self._added_plots[plot],  
     645            wx.PostEvent(self.parent, 
     646                         NewPlotEvent(plot=self._added_plots[plot], 
    649647                                      title=self._added_plots[plot].name, 
    650                                                    update=True))         
    651          
     648                                      update=True)) 
     649 
    652650        # Need the update flag in the NewPlotEvent to protect against 
    653651        # the plot no longer being there... 
    654          
     652 
    655653    def _on_normalize(self, evt): 
    656654        """ 
    657655        Normalize the area under the P(r) curve to 1. 
    658656        This operation is done for all displayed plots. 
    659          
     657 
    660658        :param evt: Menu event 
    661          
     659 
    662660        """ 
    663661        self._normalize_output = True 
    664662        self._scale_output_unity = False 
    665              
     663 
    666664        self.show_pr(self._last_out, self._last_pr, self._last_cov) 
    667          
     665 
    668666        # Now scale the added plots too 
    669667        for plot in self._added_plots: 
    670             sum = numpy.sum(self._added_plots[plot].y) 
     668            total = numpy.sum(self._added_plots[plot].y) 
    671669            npts = len(self._added_plots[plot].x) 
    672             sum *= self._added_plots[plot].x[npts-1]/npts 
    673             y = self._added_plots[plot].y/sum 
    674              
     670            total *= self._added_plots[plot].x[npts - 1] / npts 
     671            y = self._added_plots[plot].y / total 
     672 
    675673            new_plot = Data1D(self._added_plots[plot].x, y) 
    676674            new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM 
     
    680678            new_plot.name = self._added_plots[plot].name 
    681679            new_plot.xaxis("\\rm{r}", 'A') 
    682             new_plot.yaxis("\\rm{P(r)} ","cm^{-3}") 
    683             self.parent.update_theory(data_id=self.data_id, theory=new_plot)         
    684             wx.PostEvent(self.parent,  
     680            new_plot.yaxis("\\rm{P(r)} ", "cm^{-3}") 
     681            self.parent.update_theory(data_id=self.data_id, theory=new_plot) 
     682            wx.PostEvent(self.parent, 
    685683                         NewPlotEvent(plot=new_plot, update=True, 
    686                                          title=self._added_plots[plot].name)) 
    687          
     684                                      title=self._added_plots[plot].name)) 
     685 
    688686    def _on_scale_unity(self, evt): 
    689687        """ 
    690688        Scale the maximum P(r) value on each displayed plot to 1. 
    691          
     689 
    692690        :param evt: Menu event 
    693          
     691 
    694692        """ 
    695693        self._scale_output_unity = True 
    696694        self._normalize_output = False 
    697              
     695 
    698696        self.show_pr(self._last_out, self._last_pr, self._last_cov) 
    699          
     697 
    700698        # Now scale the added plots too 
    701699        for plot in self._added_plots: 
    702700            _max = 0 
    703701            for y in self._added_plots[plot].y: 
    704                 if y>_max:  
     702                if y > _max: 
    705703                    _max = y 
    706             y = self._added_plots[plot].y/_max 
    707              
     704            y = self._added_plots[plot].y / _max 
     705 
    708706            new_plot = Data1D(self._added_plots[plot].x, y) 
    709707            new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM 
    710708            new_plot.name = self._added_plots[plot].name 
    711709            new_plot.xaxis("\\rm{r}", 'A') 
    712             new_plot.yaxis("\\rm{P(r)} ","cm^{-3}") 
    713             self.parent.update_theory(data_id=self.data_id,theory=new_plot)         
    714             wx.PostEvent(self.parent,  
     710            new_plot.yaxis("\\rm{P(r)} ", "cm^{-3}") 
     711            self.parent.update_theory(data_id=self.data_id, theory=new_plot) 
     712            wx.PostEvent(self.parent, 
    715713                         NewPlotEvent(plot=new_plot, update=True, 
    716                                 title=self._added_plots[plot].name))         
    717                  
     714                                      title=self._added_plots[plot].name)) 
     715 
    718716    def start_thread(self): 
    719717        """ 
     718            Start a calculation thread 
    720719        """ 
    721720        from pr_thread import CalcPr 
    722          
     721 
    723722        # If a thread is already started, stop it 
    724723        if self.calc_thread != None and self.calc_thread.isrunning(): 
    725724            self.calc_thread.stop() 
    726             ## stop just raises the flag -- the thread is supposed to  
     725            ## stop just raises the flag -- the thread is supposed to 
    727726            ## then kill itself. In August 2014 it was shown that this is 
    728             ## incorrectly handled by fitting.py and a fix implemented.  
     727            ## incorrectly handled by fitting.py and a fix implemented. 
    729728            ## It is not clear whether it is properly used here, but the 
    730729            ## "fix" of waiting for the previous thread to end breaks the 
     
    735734            ## are being properly handled. 
    736735            ## 
    737             ##    -PDB January 25, 2015                   
    738                  
     736            ##    -PDB January 25, 2015 
     737 
    739738        pr = self.pr.clone() 
    740739        self.calc_thread = CalcPr(pr, self.nfunc, 
    741                                    error_func=self._thread_error,  
    742                                    completefn=self._completed, updatefn=None) 
     740                                  error_func=self._thread_error, 
     741                                  completefn=self._completed, updatefn=None) 
    743742        self.calc_thread.queue() 
    744743        self.calc_thread.ready(2.5) 
    745      
     744 
    746745    def _thread_error(self, error): 
    747746        """ 
     747            Call-back method for calculation errors 
    748748        """ 
    749749        wx.PostEvent(self.parent, StatusEvent(status=error)) 
    750      
     750 
    751751    def _estimate_completed(self, alpha, message, elapsed): 
    752752        """ 
    753         Parameter estimation completed,  
     753        Parameter estimation completed, 
    754754        display the results to the user 
    755          
     755 
    756756        :param alpha: estimated best alpha 
    757757        :param elapsed: computation time 
    758          
     758 
    759759        """ 
    760760        # Save useful info 
    761761        self.elapsed = elapsed 
    762762        self.control_panel.alpha_estimate = alpha 
    763         if not message==None: 
     763        if not message == None: 
    764764            wx.PostEvent(self.parent, StatusEvent(status=str(message))) 
    765765        self.perform_estimateNT() 
    766      
     766 
    767767    def _estimateNT_completed(self, nterms, alpha, message, elapsed): 
    768768        """ 
    769         Parameter estimation completed,  
     769        Parameter estimation completed, 
    770770        display the results to the user 
    771          
     771 
    772772        :param alpha: estimated best alpha 
    773773        :param nterms: estimated number of terms 
    774774        :param elapsed: computation time 
    775          
     775 
    776776        """ 
    777777        # Save useful info 
     
    779779        self.control_panel.nterms_estimate = nterms 
    780780        self.control_panel.alpha_estimate = alpha 
    781         if not message==None: 
     781        if not message == None: 
    782782            wx.PostEvent(self.parent, StatusEvent(status=str(message))) 
    783      
     783 
    784784    def _completed(self, out, cov, pr, elapsed): 
    785785        """ 
    786786        wxCallAfter Method called with the results when the inversion 
    787787        is done 
    788          
     788 
    789789        :param out: output coefficient for the base functions 
    790790        :param cov: covariance matrix 
     
    795795        # Without CallAfter, it will freeze with wx >= 2.9. 
    796796        wx.CallAfter(self._completed_call, out, cov, pr, elapsed) 
    797          
     797 
    798798    def _completed_call(self, out, cov, pr, elapsed): 
    799799        """ 
    800800        Method called with the results when the inversion 
    801801        is done 
    802          
     802 
    803803        :param out: output coefficient for the base functions 
    804804        :param cov: covariance matrix 
    805805        :param pr: Invertor instance 
    806806        :param elapsed: time spent computing 
    807          
     807 
    808808        """ 
    809809        # Save useful info 
    810810        self.elapsed = elapsed 
    811811        # Keep a copy of the last result 
    812         self._last_pr  = pr.clone() 
     812        self._last_pr = pr.clone() 
    813813        self._last_out = out 
    814814        self._last_cov = cov 
    815          
     815 
    816816        # Save Pr invertor 
    817817        self.pr = pr 
    818          
    819         #message = "Computation completed in" 
    820         #message +=  %g seconds [chi2=%g]" % (elapsed, pr.chi2) 
    821         #wx.PostEvent(self.parent, StatusEvent(status=message)) 
    822  
    823818        cov = numpy.ascontiguousarray(cov) 
    824819 
     
    828823        self.control_panel.oscillation = pr.oscillations(out) 
    829824        self.control_panel.positive = pr.get_positive(out) 
    830         self.control_panel.pos_err  = pr.get_pos_err(out, cov) 
     825        self.control_panel.pos_err = pr.get_pos_err(out, cov) 
    831826        self.control_panel.rg = pr.rg(out) 
    832827        self.control_panel.iq0 = pr.iq0(out) 
    833828        self.control_panel.bck = pr.background 
    834                          
     829 
    835830        # Show I(q) fit 
    836831        self.show_iq(out, self.pr) 
    837          
     832 
    838833        # Show P(r) fit 
    839         x_values, x_range = self.show_pr(out, self.pr, cov)   
    840                  
     834        self.show_pr(out, self.pr, cov) 
     835 
    841836    def show_data(self, path=None, data=None, reset=False): 
    842837        """ 
    843838        Show data read from a file 
    844          
     839 
    845840        :param path: file path 
    846841        :param reset: if True all other plottables will be cleared 
    847          
     842 
    848843        """ 
    849844        #if path is not None: 
     
    855850                wx.PostEvent(self.parent, StatusEvent(status=status)) 
    856851                raise RuntimeError, status 
    857                  
     852 
    858853            # If the file contains nothing, just return 
    859854            if pr is None: 
    860855                raise RuntimeError, "Loaded data is invalid" 
    861              
     856 
    862857            self.pr = pr 
    863         
     858 
    864859        # Make a plot of I(q) data 
    865860        if self.pr.err == None: 
     
    870865        new_plot.name = IQ_DATA_LABEL 
    871866        new_plot.xaxis("\\rm{Q}", 'A^{-1}') 
    872         new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") 
     867        new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}") 
    873868        new_plot.interactive = True 
    874869        new_plot.group_id = GROUP_ID_IQ_DATA 
    875870        new_plot.id = self.data_id 
    876         new_plot.title = "I(q)"    
    877         wx.PostEvent(self.parent,  
     871        new_plot.title = "I(q)" 
     872        wx.PostEvent(self.parent, 
    878873                     NewPlotEvent(plot=new_plot, title="I(q)", reset=reset)) 
    879          
     874 
    880875        self.current_plottable = new_plot 
    881876        # Get Q range 
    882877        self.control_panel.q_min = min(self.pr.x) 
    883878        self.control_panel.q_max = max(self.pr.x) 
    884              
     879 
    885880    def save_data(self, filepath, prstate=None): 
    886881        """ 
    887882        Save data in provided state object. 
    888          
    889         :TODO: move the state code away from inversion_panel and move it here.  
     883 
     884        :TODO: move the state code away from inversion_panel and move it here. 
    890885                Then remove the "prstate" input and make this method private. 
    891                  
     886 
    892887        :param filepath: path of file to write to 
    893         :param prstate: P(r) inversion state  
    894          
     888        :param prstate: P(r) inversion state 
     889 
    895890        """ 
    896891        #TODO: do we need this or can we use DataLoader.loader.save directly? 
    897          
     892 
    898893        # Add output data and coefficients to state 
    899894        prstate.coefficients = self._last_out 
    900895        prstate.covariance = self._last_cov 
    901          
     896 
    902897        # Write the output to file 
    903898        # First, check that the data is of the right type 
    904899        if issubclass(self.current_plottable.__class__, 
    905                        sas.dataloader.data_info.Data1D): 
     900                      sas.dataloader.data_info.Data1D): 
    906901            self.state_reader.write(filepath, self.current_plottable, prstate) 
    907902        else: 
    908903            msg = "pr.save_data: the data being saved is not a" 
    909             msg += " sas.data_info.Data1D object"  
     904            msg += " sas.data_info.Data1D object" 
    910905            raise RuntimeError, msg 
    911          
    912          
    913     def setup_plot_inversion(self, alpha, nfunc, d_max, q_min=None, q_max=None,  
     906 
     907    def setup_plot_inversion(self, alpha, nfunc, d_max, q_min=None, q_max=None, 
    914908                             bck=False, height=0, width=0): 
    915909        """ 
     910            Set up inversion from plotted data 
    916911        """ 
    917912        self.alpha = alpha 
     
    922917        self.has_bck = bck 
    923918        self.slit_height = height 
    924         self.slit_width  = width 
    925          
     919        self.slit_width = width 
     920 
    926921        try: 
    927922            pr = self._create_plot_pr() 
    928             if not pr==None: 
     923            if not pr == None: 
    929924                self.pr = pr 
    930925                self.perform_inversion() 
     
    932927            wx.PostEvent(self.parent, StatusEvent(status=sys.exc_value)) 
    933928 
    934     def estimate_plot_inversion(self, alpha, nfunc, d_max,  
    935                                 q_min=None, q_max=None,  
     929    def estimate_plot_inversion(self, alpha, nfunc, d_max, 
     930                                q_min=None, q_max=None, 
    936931                                bck=False, height=0, width=0): 
    937932        """ 
     933            Estimate parameters from plotted data 
    938934        """ 
    939935        self.alpha = alpha 
     
    944940        self.has_bck = bck 
    945941        self.slit_height = height 
    946         self.slit_width  = width 
    947          
     942        self.slit_width = width 
     943 
    948944        try: 
    949945            pr = self._create_plot_pr() 
    950             if not pr==None: 
     946            if not pr == None: 
    951947                self.pr = pr 
    952948                self.perform_estimate() 
    953949        except: 
    954             wx.PostEvent(self.parent, StatusEvent(status=sys.exc_value))             
     950            wx.PostEvent(self.parent, StatusEvent(status=sys.exc_value)) 
    955951 
    956952    def _create_plot_pr(self, estimate=False): 
     
    958954        Create and prepare invertor instance from 
    959955        a plottable data set. 
    960          
    961         :param path: path of the file to read in  
    962          
     956 
     957        :param path: path of the file to read in 
     958 
    963959        """ 
    964960        # Sanity check 
    965961        if self.current_plottable is None: 
    966962            msg = "Please load a valid data set before proceeding." 
    967             wx.PostEvent(self.parent, StatusEvent(status=msg))   
    968             return None    
    969          
     963            wx.PostEvent(self.parent, StatusEvent(status=msg)) 
     964            return None 
     965 
    970966        # Get the data from the chosen data set and perform inversion 
    971967        pr = Invertor() 
     
    979975        pr.slit_height = self.slit_height 
    980976        pr.slit_width = self.slit_width 
    981          
     977 
    982978        # Keep track of the plot window title to ensure that 
    983979        # we can overlay the plots 
    984980        pr.info["plot_group_id"] = self.current_plottable.group_id 
    985          
     981 
    986982        # Fill in errors if none were provided 
    987983        err = self.current_plottable.dy 
    988984        all_zeros = True 
    989985        if err == None: 
    990             err = numpy.zeros(len(pr.y))  
    991         else:     
     986            err = numpy.zeros(len(pr.y)) 
     987        else: 
    992988            for i in range(len(err)): 
    993                 if err[i]>0: 
     989                if err[i] > 0: 
    994990                    all_zeros = False 
    995          
    996         if all_zeros:         
     991 
     992        if all_zeros: 
    997993            scale = None 
    998994            min_err = 0.0 
    999995            for i in range(len(pr.y)): 
    1000996                # Scale the error so that we can fit over several decades of Q 
    1001                 if scale==None: 
    1002                     scale = 0.05*math.sqrt(pr.y[i]) 
    1003                     min_err = 0.01*pr.y[i] 
    1004                 err[i] = scale*math.sqrt( math.fabs(pr.y[i]) ) + min_err 
     997                if scale == None: 
     998                    scale = 0.05 * math.sqrt(pr.y[i]) 
     999                    min_err = 0.01 * pr.y[i] 
     1000                err[i] = scale * math.sqrt(math.fabs(pr.y[i])) + min_err 
    10051001            message = "The loaded file had no error bars, " 
    10061002            message += "statistical errors are assumed." 
     
    10081004 
    10091005        pr.err = err 
    1010          
     1006 
    10111007        return pr 
    10121008 
    1013            
    10141009    def setup_file_inversion(self, alpha, nfunc, d_max, data, 
    1015                              path=None, q_min=None, q_max=None,  
     1010                             path=None, q_min=None, q_max=None, 
    10161011                             bck=False, height=0, width=0): 
    10171012        """ 
     1013            Set up inversion 
    10181014        """ 
    10191015        self.alpha = alpha 
     
    10241020        self.has_bck = bck 
    10251021        self.slit_height = height 
    1026         self.slit_width  = width 
    1027          
     1022        self.slit_width = width 
     1023 
    10281024        try: 
    1029             #pr = self._create_file_pr(path) 
    10301025            pr = self._create_file_pr(data) 
    1031             if not pr==None: 
     1026            if not pr == None: 
    10321027                self.pr = pr 
    10331028                self.perform_inversion() 
    10341029        except: 
    10351030            wx.PostEvent(self.parent, StatusEvent(status=sys.exc_value)) 
    1036            
     1031 
    10371032    def estimate_file_inversion(self, alpha, nfunc, d_max, data, 
    1038                                 path=None, q_min=None, q_max=None,  
     1033                                path=None, q_min=None, q_max=None, 
    10391034                                bck=False, height=0, width=0): 
    10401035        """ 
     1036            Estimate parameters for inversion 
    10411037        """ 
    10421038        self.alpha = alpha 
     
    10471043        self.has_bck = bck 
    10481044        self.slit_height = height 
    1049         self.slit_width  = width 
    1050          
     1045        self.slit_width = width 
     1046 
    10511047        try: 
    10521048            pr = self._create_file_pr(data) 
    1053             #pr = self._create_file_pr(path) 
    10541049            if not pr is None: 
    10551050                self.pr = pr 
     
    10571052        except: 
    10581053            wx.PostEvent(self.parent, StatusEvent(status=sys.exc_value)) 
    1059                  
    1060            
     1054 
    10611055    def _create_file_pr(self, data): 
    10621056        """ 
    10631057        Create and prepare invertor instance from 
    10641058        a file data set. 
    1065          
    1066         :param path: path of the file to read in  
    1067          
    1068         """ 
    1069         # Load data 
    1070         #if os.path.isfile(path): 
    1071         """     
    1072         if self._current_file_data is not None \ 
    1073             and self._current_file_data.path==path: 
    1074             # Protect against corrupted data from  
    1075             # previous failed load attempt 
    1076             if self._current_file_data.x is None: 
    1077                 return None 
    1078             x = self._current_file_data.x 
    1079             y = self._current_file_data.y 
    1080             err = self._current_file_data.err 
    1081              
    1082             message = "The data from this file has already been loaded." 
    1083             wx.PostEvent(self.parent, StatusEvent(status=message)) 
    1084         else: 
     1059 
     1060        :param path: path of the file to read in 
     1061 
    10851062        """ 
    10861063        # Reset the status bar so that we don't get mixed up 
    1087         # with old messages.  
     1064        # with old messages. 
    10881065        #TODO: refactor this into a proper status handling 
    10891066        wx.PostEvent(self.parent, StatusEvent(status='')) 
    10901067        try: 
    1091             class FileData: 
     1068            class FileData(object): 
    10921069                x = None 
    10931070                y = None 
     
    10961073                def __init__(self, path): 
    10971074                    self.path = path 
    1098                  
     1075 
    10991076            self._current_file_data = FileData(data.path) 
    11001077            self._current_file_data.x = data.x 
     
    11051082            load_error(sys.exc_value) 
    11061083            return None 
    1107          
     1084 
    11081085        # If the file contains no data, just return 
    11091086        if x is None or len(x) == 0: 
     
    11261103                message += "statistical errors are assumed." 
    11271104                wx.PostEvent(self.parent, StatusEvent(status=message)) 
    1128          
     1105 
    11291106        try: 
    11301107            # Get the data from the chosen data set and perform inversion 
     
    11441121            load_error(sys.exc_value) 
    11451122        return None 
    1146          
     1123 
    11471124    def perform_estimate(self): 
    11481125        """ 
     1126            Perform parameter estimation 
    11491127        """ 
    11501128        from pr_thread import EstimatePr 
    1151         from copy import deepcopy 
    1152          
     1129 
    11531130        # If a thread is already started, stop it 
    11541131        if self.estimation_thread != None and \ 
    11551132            self.estimation_thread.isrunning(): 
    11561133            self.estimation_thread.stop() 
    1157             ## stop just raises the flag -- the thread is supposed to  
     1134            ## stop just raises the flag -- the thread is supposed to 
    11581135            ## then kill itself. In August 2014 it was shown that this is 
    1159             ## incorrectly handled by fitting.py and a fix implemented.  
     1136            ## incorrectly handled by fitting.py and a fix implemented. 
    11601137            ## It is not clear whether it is properly used here, but the 
    11611138            ## "fix" of waiting for the previous thread to end breaks the 
     
    11661143            ## are being properly handled. 
    11671144            ## 
    1168             ##    -PDB January 25, 2015                   
    1169                  
    1170                  
     1145            ##    -PDB January 25, 2015 
    11711146        pr = self.pr.clone() 
    11721147        self.estimation_thread = EstimatePr(pr, self.nfunc, 
    1173                                              error_func=self._thread_error,  
    1174                                          completefn = self._estimate_completed,  
    1175                                             updatefn   = None) 
     1148                                            error_func=self._thread_error, 
     1149                                            completefn=self._estimate_completed, 
     1150                                            updatefn=None) 
    11761151        self.estimation_thread.queue() 
    11771152        self.estimation_thread.ready(2.5) 
    1178      
     1153 
    11791154    def perform_estimateNT(self): 
    11801155        """ 
     1156            Perform parameter estimation 
    11811157        """ 
    11821158        from pr_thread import EstimateNT 
    1183         from copy import deepcopy 
    1184          
     1159 
    11851160        # If a thread is already started, stop it 
    11861161        if self.estimation_thread != None and self.estimation_thread.isrunning(): 
    11871162            self.estimation_thread.stop() 
    1188             ## stop just raises the flag -- the thread is supposed to  
     1163            ## stop just raises the flag -- the thread is supposed to 
    11891164            ## then kill itself. In August 2014 it was shown that this is 
    1190             ## incorrectly handled by fitting.py and a fix implemented.  
     1165            ## incorrectly handled by fitting.py and a fix implemented. 
    11911166            ## It is not clear whether it is properly used here, but the 
    11921167            ## "fix" of waiting for the previous thread to end breaks the 
     
    11971172            ## are being properly handled. 
    11981173            ## 
    1199             ##    -PDB January 25, 2015                   
    1200                                  
     1174            ##    -PDB January 25, 2015 
    12011175        pr = self.pr.clone() 
    12021176        # Skip the slit settings for the estimation 
    12031177        # It slows down the application and it doesn't change the estimates 
    12041178        pr.slit_height = 0.0 
    1205         pr.slit_width  = 0.0 
    1206         self.estimation_thread = EstimateNT(pr, self.nfunc,  
    1207                                             error_func=self._thread_error,  
    1208                                         completefn = self._estimateNT_completed,  
    1209                                             updatefn   = None) 
     1179        pr.slit_width = 0.0 
     1180        self.estimation_thread = EstimateNT(pr, self.nfunc, 
     1181                                            error_func=self._thread_error, 
     1182                                            completefn=self._estimateNT_completed, 
     1183                                            updatefn=None) 
    12101184        self.estimation_thread.queue() 
    12111185        self.estimation_thread.ready(2.5) 
    1212          
     1186 
    12131187    def perform_inversion(self): 
    12141188        """ 
    1215         """ 
    1216         # Time estimate 
    1217         #estimated = self.elapsed*self.nfunc**2 
    1218         #message = "Computation time may take up to %g seconds" % self.elapsed 
    1219         #wx.PostEvent(self.parent, StatusEvent(status=message)) 
    1220          
    1221         # Start inversion thread 
     1189            Perform inversion 
     1190        """ 
    12221191        self.start_thread() 
    1223         return 
    1224          
    1225         out, cov = self.pr.lstsq(self.nfunc) 
    1226          
    1227         # Save useful info 
    1228         self.elapsed = self.pr.elapsed 
    1229          
    1230         for i in range(len(out)): 
    1231             try: 
    1232                 print "%d: %g +- %g" % (i, out[i], 
    1233                                          math.sqrt(math.fabs(cov[i][i]))) 
    1234             except:  
    1235                 print "%d: %g +- ?" % (i, out[i])         
    1236          
    1237         # Make a plot of I(q) data 
    1238         new_plot = Data1D(self.pr.x, self.pr.y, dy=self.pr.err) 
    1239         new_plot.name = "I_{obs}(q)" 
    1240         new_plot.xaxis("\\rm{Q}", 'A^{-1}') 
    1241         new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") 
    1242         wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Iq")) 
    1243         # Show I(q) fit 
    1244         self.show_iq(out, self.pr) 
    1245         # Show P(r) fit 
    1246         x_values, x_range = self.show_pr(out, self.pr, cov=cov) 
    1247          
     1192 
    12481193    def _on_context_inversion(self, event): 
    12491194        """ 
     1195            Call-back method for plot context menu 
    12501196        """ 
    12511197        panel = event.GetEventObject() 
     
    12591205            logging.info("Prview Error: No data is available") 
    12601206            return 
    1261          
     1207 
    12621208        # Store a reference to the current plottable 
    12631209        # If we have a suggested value, use it. 
     
    12681214            self.control_panel.alpha = self.alpha 
    12691215            logging.info("Prview :Alpha Not estimate yet") 
    1270             pass 
    12711216        try: 
    12721217            estimate = int(self.control_panel.nterms_estimate) 
     
    12751220            self.control_panel.nfunc = self.nfunc 
    12761221            logging.info("Prview : ntemrs Not estimate yet") 
    1277             pass 
    1278          
     1222 
    12791223        self.current_plottable = panel.plots[panel.graph.selected_plottable] 
    12801224        self.set_data([self.current_plottable]) 
     
    12841228        #self.parent.set_perspective(self.perspective) 
    12851229        self.control_panel._on_invert(None) 
    1286              
     1230 
    12871231    def get_panels(self, parent): 
    12881232        """ 
     
    12901234        """ 
    12911235        from inversion_panel import InversionControl 
    1292          
     1236 
    12931237        self.parent = parent 
    1294         self.frame = MDIFrame(self.parent, None, 'None', (100, 200))      
    1295         self.control_panel = InversionControl(self.frame, -1,  
     1238        self.frame = MDIFrame(self.parent, None, 'None', (100, 200)) 
     1239        self.control_panel = InversionControl(self.frame, -1, 
    12961240                                              style=wx.RAISED_BORDER, 
    12971241                                              standalone=self.standalone) 
     
    13041248        self.perspective = [] 
    13051249        self.perspective.append(self.control_panel.window_name) 
    1306       
     1250 
    13071251        return [self.control_panel] 
    1308          
     1252 
    13091253    def set_data(self, data_list=None): 
    13101254        """ 
     
    13261270                            data_1d_list.append(data) 
    13271271                        else: 
    1328                             error_msg += " %s  type %s \n" % (str(data.name), 
    1329                                              str(data.__class__.__name__)) 
     1272                            error_msg += " %s type %s \n" % (str(data.name), 
     1273                                                             str(data.__class__.__name__)) 
    13301274                            data_2d_list.append(data) 
    13311275                if len(data_2d_list) > 0: 
     
    13331277                    msg += error_msg 
    13341278                if len(data_1d_list) == 0: 
    1335                     wx.PostEvent(self.parent,  
    1336                     StatusEvent(status=msg, info='error')) 
     1279                    wx.PostEvent(self.parent, StatusEvent(status=msg, info='error')) 
    13371280                    return 
    13381281                msg += "Prview does not allow multiple data!\n" 
     
    13481291            if data is None: 
    13491292                msg += "PrView receives no data. \n" 
    1350                 wx.PostEvent(self.parent,  
    1351                      StatusEvent(status=msg, info='error')) 
     1293                wx.PostEvent(self.parent, StatusEvent(status=msg, info='error')) 
    13521294                return 
    13531295            if issubclass(data.__class__, Data1D): 
    13541296                try: 
    1355                     wx.PostEvent(self.parent, NewPlotEvent(action='remove', 
    1356                                                 group_id=GROUP_ID_IQ_DATA,  
    1357                                                 id=self.data_id)) 
    1358                                                
     1297                    wx.PostEvent(self.parent, 
     1298                                 NewPlotEvent(action='remove', 
     1299                                              group_id=GROUP_ID_IQ_DATA, 
     1300                                              id=self.data_id)) 
    13591301                    self.data_id = data.id 
    13601302                    self.control_panel._change_file(evt=None, data=data) 
    13611303                except: 
    13621304                    msg = "Prview Set_data: " + str(sys.exc_value) 
    1363                     wx.PostEvent(self.parent, StatusEvent(status=msg, 
    1364                                                             info="error")) 
    1365             else:     
     1305                    wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) 
     1306            else: 
    13661307                msg = "Pr cannot be computed for data of " 
    13671308                msg += "type %s" % (data_list[0].__class__.__name__) 
    1368                 wx.PostEvent(self.parent,  
    1369                          StatusEvent(status=msg, info='error')) 
     1309                wx.PostEvent(self.parent, StatusEvent(status=msg, info='error')) 
    13701310        else: 
    13711311            msg = "Pr contain no data" 
    13721312            wx.PostEvent(self.parent, StatusEvent(status=msg, info='warning')) 
    1373              
     1313 
    13741314    def post_init(self): 
    13751315        """ 
     
    13791319        if self.standalone: 
    13801320            self.parent.set_perspective(self.perspective) 
    1381    
    1382 if __name__ == "__main__": 
    1383     i = Plugin() 
    1384     print i.perform_estimateNT() 
    1385      
    1386      
    1387      
    1388      
Note: See TracChangeset for help on using the changeset viewer.