Esempio n. 1
0
    def test4(self):
        """ fit 2 data concatenates with limited range of x and  one model """
        #load data
        l = Loader()
        data1 = l.load("testdata_line.txt")
        data1.name = data1.filename
        data2 = l.load("testdata_line1.txt")
        data2.name = data2.filename

        # Receives the type of model for the fitting
        model1 = LineModel()
        model1.name = "M1"
        model1.setParam("A", 1.0)
        model1.setParam("B", 1.0)
        model = Model(model1, data1)

        pars1 = ['A', 'B']
        #Importing the Fit module

        fitter = Fit('bumps')
        fitter.set_data(data1, 1, qmin=0, qmax=7)
        fitter.set_model(model, 1, pars1)
        fitter.set_data(data2, 1, qmin=1, qmax=10)
        fitter.select_problem_for_fit(id=1, value=1)
        result2, = fitter.fit(handler=FitHandler())

        self.assert_(result2)
        self.assertTrue(
            math.fabs(result2.pvec[0] - 4) / 3 <= result2.stderr[0])
        self.assertTrue(
            math.fabs(result2.pvec[1] - 2.5) / 3 <= result2.stderr[1])
        self.assertTrue(result2.fitness / len(data1.x) < 2)
Esempio n. 2
0
    def test2(self):
        """ fit 2 data and 2 model with no constrainst"""
        #load data
        l = Loader()
        data1 = l.load("testdata_line.txt")
        data1.name = data1.filename

        data2 = l.load("testdata_line1.txt")
        data2.name = data2.filename

        #Importing the Fit module
        fitter = Fit('bumps')
        # Receives the type of model for the fitting
        model11 = LineModel()
        model11.name = "M1"
        model22 = LineModel()
        model11.name = "M2"

        model1 = Model(model11, data1)
        model2 = Model(model22, data2)
        pars1 = ['A', 'B']
        fitter.set_data(data1, 1)
        fitter.set_model(model1, 1, pars1)
        fitter.select_problem_for_fit(id=1, value=0)
        fitter.set_data(data2, 2)
        fitter.set_model(model2, 2, pars1)
        fitter.select_problem_for_fit(id=2, value=0)

        try:
            result1, = fitter.fit(handler=FitHandler())
        except RuntimeError, msg:
            assert str(msg) == "Nothing to fit"
Esempio n. 3
0
    def test4(self):
        """ fit 2 data concatenates with limited range of x and  one model """
            #load data
        l = Loader()
        data1 = l.load("testdata_line.txt")
        data1.name = data1.filename
        data2 = l.load("testdata_line1.txt")
        data2.name = data2.filename

        # Receives the type of model for the fitting
        model1  = LineModel()
        model1.name= "M1"
        model1.setParam("A", 1.0)
        model1.setParam("B",1.0)
        model = Model(model1,data1)
      
        pars1= ['A','B']
        #Importing the Fit module

        fitter = Fit()
        fitter.set_data(data1,1,qmin=0, qmax=7)
        fitter.set_model(model,1,pars1)
        fitter.set_data(data2,1,qmin=1,qmax=10)
        fitter.select_problem_for_fit(id=1,value=1)
        result2, = fitter.fit(handler=FitHandler())
        
        self.assert_(result2)
        self.assertTrue( math.fabs(result2.pvec[0]-4)/3 <= result2.stderr[0] )
        self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3 <= result2.stderr[1] )
        self.assertTrue( result2.fitness/len(data1.x) < 2)
Esempio n. 4
0
    def test2(self):
        """ fit 2 data and 2 model with no constrainst"""
        #load data
        l = Loader()
        data1=l.load("testdata_line.txt")
        data1.name = data1.filename
      
        data2=l.load("testdata_line1.txt")
        data2.name = data2.filename
     
        #Importing the Fit module
        fitter = Fit()
        # Receives the type of model for the fitting
        model11  = LineModel()
        model11.name= "M1"
        model22  = LineModel()
        model11.name= "M2"
      
        model1 = Model(model11,data1)
        model2 = Model(model22,data2)
        pars1= ['A','B']
        fitter.set_data(data1,1)
        fitter.set_model(model1,1,pars1)
        fitter.select_problem_for_fit(id=1,value=0)
        fitter.set_data(data2,2)
        fitter.set_model(model2,2,pars1)
        fitter.select_problem_for_fit(id=2,value=0)

        try: result1, = fitter.fit(handler=FitHandler())
        except RuntimeError,msg:
            assert str(msg)=="Nothing to fit"
    def setUp(self):
        loader = Loader()
        ## IGOR/NIST computation
        self.output_gauss = loader.load('Gausssphere.txt')
        self.output_shulz = loader.load('Schulzsphere.txt')

        from sas.models.SphereModel import SphereModel
        self.model = SphereModel()

        self.model.setParam('scale', 0.01)
        self.model.setParam('radius', 60.0)
        self.model.setParam('sldSph', 1.e-6)
        self.model.setParam('sldSolv', 3.e-6)
        self.model.setParam('background', 0.001)
    def setUp(self):
        loader = Loader()
        ## IGOR/NIST computation
        self.output_gauss=loader.load('Gausssphere.txt')
        self.output_shulz=loader.load('Schulzsphere.txt')

        from sas.models.SphereModel import SphereModel
        self.model= SphereModel()

        self.model.setParam('scale', 0.01)
        self.model.setParam('radius', 60.0)
        self.model.setParam('sldSph', 1.e-6)
        self.model.setParam('sldSolv', 3.e-6)
        self.model.setParam('background', 0.001)
Esempio n. 7
0
 def test_double_trans_spectra(self):
     xmlreader = XMLreader(self.isis_1_1_doubletrans, self.schema_1_1)
     self.assertTrue(xmlreader.validate_xml())
     reader = Loader()
     data = reader.load(self.isis_1_1_doubletrans)
     for item in data:
         self._check_data(item)
Esempio n. 8
0
 def test_double_trans_spectra(self):
     xmlreader = XMLreader(self.isis_1_1_doubletrans, self.schema_1_1)
     self.assertTrue(xmlreader.validate_xml())
     reader = Loader()
     data = reader.load(self.isis_1_1_doubletrans)
     for item in data:
         self._check_data(item)
Esempio n. 9
0
 def test_old_cansas_files(self):
     reader1 = XMLreader(self.cansas1d, self.schema_1_0)
     self.assertTrue(reader1.validate_xml())
     file_loader = Loader()
     file1 = file_loader.load(self.cansas1d)
     reader2 = XMLreader(self.cansas1d_units, self.schema_1_0)
     self.assertTrue(reader2.validate_xml())
     reader3 = XMLreader(self.cansas1d_badunits, self.schema_1_0)
     self.assertTrue(reader3.validate_xml())
     reader4 = XMLreader(self.cansas1d_slit, self.schema_1_0)
     self.assertTrue(reader4.validate_xml())
Esempio n. 10
0
 def test_old_cansas_files(self):
     reader1 = XMLreader(self.cansas1d, self.schema_1_0)
     self.assertTrue(reader1.validate_xml())
     file_loader = Loader()
     file1 = file_loader.load(self.cansas1d)
     reader2 = XMLreader(self.cansas1d_units, self.schema_1_0)
     self.assertTrue(reader2.validate_xml())
     reader3 = XMLreader(self.cansas1d_badunits, self.schema_1_0)
     self.assertTrue(reader3.validate_xml())
     reader4 = XMLreader(self.cansas1d_slit, self.schema_1_0)
     self.assertTrue(reader4.validate_xml())
Esempio n. 11
0
    def test_constraints(self):
        """ fit 2 data and 2 model with 1 constrainst"""
        #load data
        l = Loader()
        data1 = l.load("testdata_line.txt")
        data1.name = data1.filename
        data2 = l.load("testdata_cst.txt")
        data2.name = data2.filename

        # Receives the type of model for the fitting
        model11 = LineModel()
        model11.name = "line"
        model11.setParam("A", 1.0)
        model11.setParam("B", 1.0)

        model22 = Constant()
        model22.name = "cst"
        model22.setParam("value", 1.0)

        model1 = Model(model11, data1)
        model2 = Model(model22, data2)
        model1.set(A=4)
        model1.set(B=3)
        # Constraint the constant value to be equal to parameter B (the real value is 2.5)
        #model2.set(value='line.B')
        pars1 = ['A', 'B']
        pars2 = ['value']

        #Importing the Fit module
        fitter = Fit('bumps')
        fitter.set_data(data1, 1)
        fitter.set_model(model1, 1, pars1)
        fitter.set_data(data2, 2, smearer=None)
        fitter.set_model(model2, 2, pars2, constraints=[("value", "line.B")])
        fitter.select_problem_for_fit(id=1, value=1)
        fitter.select_problem_for_fit(id=2, value=1)

        R1, R2 = fitter.fit(handler=FitHandler())
        self.assertTrue(math.fabs(R1.pvec[0] - 4.0) / 3. <= R1.stderr[0])
        self.assertTrue(math.fabs(R1.pvec[1] - 2.5) / 3. <= R1.stderr[1])
        self.assertTrue(R1.fitness / (len(data1.x) + len(data2.x)) < 2)
Esempio n. 12
0
 def test_constraints(self):
     """ fit 2 data and 2 model with 1 constrainst"""
     #load data
     l = Loader()
     data1= l.load("testdata_line.txt")
     data1.name = data1.filename
     data2= l.load("testdata_cst.txt")
     data2.name = data2.filename
    
     # Receives the type of model for the fitting
     model11  = LineModel()
     model11.name= "line"
     model11.setParam("A", 1.0)
     model11.setParam("B",1.0)
     
     model22  = Constant()
     model22.name= "cst"
     model22.setParam("value", 1.0)
     
     model1 = Model(model11,data1)
     model2 = Model(model22,data2)
     model1.set(A=4)
     model1.set(B=3)
     # Constraint the constant value to be equal to parameter B (the real value is 2.5)
     #model2.set(value='line.B')
     pars1= ['A','B']
     pars2= ['value']
     
     #Importing the Fit module
     fitter = Fit()
     fitter.set_data(data1,1)
     fitter.set_model(model1,1,pars1)
     fitter.set_data(data2,2,smearer=None)
     fitter.set_model(model2,2,pars2,constraints=[("value","line.B")])
     fitter.select_problem_for_fit(id=1,value=1)
     fitter.select_problem_for_fit(id=2,value=1)
     
     R1,R2 = fitter.fit(handler=FitHandler())
     self.assertTrue( math.fabs(R1.pvec[0]-4.0)/3. <= R1.stderr[0])
     self.assertTrue( math.fabs(R1.pvec[1]-2.5)/3. <= R1.stderr[1])
     self.assertTrue( R1.fitness/(len(data1.x)+len(data2.x)) < 2)
Esempio n. 13
0
class testLoader(unittest.TestCase):
    def setUp(self):
        self.L = Loader()
        self.L.find_plugins('../plugins')

    def testplugin(self):
        """ 
            test loading with a test reader only 
            found in the plugins directory
        """
        output = self.L.load('test_data.test')
        self.assertEqual(output.x[0], 1234.)
Esempio n. 14
0
class testLoader(unittest.TestCase):
    
    def setUp(self):
        self.L=Loader()
        self.L.find_plugins('../plugins')
            
    def testplugin(self):
        """ 
            test loading with a test reader only 
            found in the plugins directory
        """
        output = self.L.load('test_data.test')
        self.assertEqual(output.x[0], 1234.)
Esempio n. 15
0
 def test_cansas_xml(self):
     filename = "isis_1_1_write_test.xml"
     xmlreader = XMLreader(self.isis_1_1, self.schema_1_1)
     valid = xmlreader.validate_xml()
     xmlreader.set_processing_instructions()
     self.assertTrue(valid)
     fo = open(self.isis_1_1)
     str = fo.read()
     reader_generic = Loader()
     dataloader = reader_generic.load(self.isis_1_1)
     reader_cansas = Reader()
     cansasreader = reader_cansas.read(self.isis_1_1)
     for i in range(len(dataloader)):
         self._check_data(dataloader[i])
         self._check_data_1_1(dataloader[i])
         self._check_data(cansasreader[i])
         self._check_data_1_1(cansasreader[i])
         reader_generic.save(filename, dataloader[i], None)
         fo = open(filename)
         str = fo.read()
         reader2 = Loader()
         return_data = reader2.load(filename)
         written_data = return_data[0]
         self._check_data(written_data)
Esempio n. 16
0
 def test_cansas_xml(self):
     filename = "isis_1_1_write_test.xml"
     xmlreader = XMLreader(self.isis_1_1, self.schema_1_1)
     valid = xmlreader.validate_xml()
     xmlreader.set_processing_instructions()
     self.assertTrue(valid)
     fo = open(self.isis_1_1)
     str = fo.read()
     reader_generic = Loader()
     dataloader = reader_generic.load(self.isis_1_1)
     reader_cansas = Reader()
     cansasreader = reader_cansas.read(self.isis_1_1)
     for i in range(len(dataloader)):
         self._check_data(dataloader[i])
         self._check_data_1_1(dataloader[i])
         self._check_data(cansasreader[i])
         self._check_data_1_1(cansasreader[i])
         reader_generic.save(filename, dataloader[i], None)
         fo = open(filename)
         str = fo.read()
         reader2 = Loader()
         return_data = reader2.load(filename)
         written_data = return_data[0]
         self._check_data(written_data)
Esempio n. 17
0
class abs_reader(unittest.TestCase):
    def setUp(self):
        self.loader = Loader()

    def test_checkdata(self):
        """
            Test .DAT file loaded as IGOR/DAT 2D Q_map
        """
        f = self.loader.load("exp18_14_igor_2dqxqy.dat")
        # The length of the data is 10
        self.assertEqual(len(f.qx_data), 36864)
        self.assertEqual(f.qx_data[0], -0.03573497)
        self.assertEqual(f.qx_data[36863], 0.2908819)
        self.assertEqual(f.Q_unit, '1/A')
        self.assertEqual(f.I_unit, '1/cm')

        self.assertEqual(f.meta_data['loader'], "IGOR/DAT 2D Q_map")
Esempio n. 18
0
class abs_reader(unittest.TestCase):
    
    def setUp(self):
        self.loader = Loader()
        
    def test_checkdata(self):
        """
            Test .DAT file loaded as IGOR/DAT 2D Q_map
        """
        f = self.loader.load("exp18_14_igor_2dqxqy.dat")
        # The length of the data is 10
        self.assertEqual(len(f.qx_data),  36864)
        self.assertEqual(f.qx_data[0],-0.03573497)
        self.assertEqual(f.qx_data[36863],0.2908819)
        self.assertEqual(f.Q_unit, '1/A')
        self.assertEqual(f.I_unit, '1/cm')
        
        self.assertEqual(f.meta_data['loader'],"IGOR/DAT 2D Q_map")
Esempio n. 19
0
 def test_save_cansas_v1_0(self):
     filename = "isis_1_0_write_test.xml"
     xmlreader = XMLreader(self.isis_1_0, self.schema_1_0)
     valid = xmlreader.validate_xml()
     self.assertTrue(valid)
     reader_generic = Loader()
     dataloader = reader_generic.load(self.isis_1_0)
     reader_cansas = Reader()
     cansasreader = reader_cansas.read(self.isis_1_0)
     for i in range(len(dataloader)):
         self._check_data(dataloader[i])
         self._check_data(cansasreader[i])
         reader_generic.save(filename, dataloader[i], None)
         reader2 = Reader()
         return_data = reader2.read(filename)
         written_data = return_data[0]
         xmlwrite = XMLreader(filename, self.schema_1_0)
         valid = xmlreader.validate_xml()
         self.assertTrue(valid)
         self._check_data(written_data)
Esempio n. 20
0
 def test_save_cansas_v1_0(self):
     filename = "isis_1_0_write_test.xml"
     xmlreader = XMLreader(self.isis_1_0, self.schema_1_0)
     valid = xmlreader.validate_xml()
     self.assertTrue(valid)
     reader_generic = Loader()
     dataloader = reader_generic.load(self.isis_1_0)
     reader_cansas = Reader()
     cansasreader = reader_cansas.read(self.isis_1_0)
     for i in range(len(dataloader)):
         self._check_data(dataloader[i])
         self._check_data(cansasreader[i])
         reader_generic.save(filename, dataloader[i], None)
         reader2 = Reader()
         return_data = reader2.read(filename)
         written_data = return_data[0]
         xmlwrite = XMLreader(filename, self.schema_1_0)
         valid = xmlreader.validate_xml()
         self.assertTrue(valid)
         self._check_data(written_data)
Esempio n. 21
0
class DataReader(CalcThread):
    """
        Load a data given a filename
    """
    def __init__(self, path, completefn=None,
                 updatefn=None,
                 yieldtime=0.01,
                 worktime=0.01
                 ):
        CalcThread.__init__(self, completefn,
                 updatefn,
                 yieldtime,
                 worktime)
        self.path = path
        #Instantiate a loader 
        self.loader = Loader()
        self.starttime = 0

    def isquit(self):
        """
             @raise KeyboardInterrupt: when the thread is interrupted
        """
        try:
            CalcThread.isquit(self)
        except KeyboardInterrupt:
            raise KeyboardInterrupt


    def compute(self):
        """
            read some data
        """
        self.starttime = time.time()
        try:
            data = self.loader.load(self.path)
            self.complete(data=data)
        except KeyboardInterrupt:
            # Thread was interrupted, just proceed and re-raise.
            # Real code should not print, but this is an example...
            raise
Esempio n. 22
0
    def OnLoad(self, event):
        path = None
        dlg = wx.FileDialog(None, "Choose a file", os.getcwd(), "", "*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            mypath = os.path.basename(path)
            from sas.dataloader.loader import Loader

            # Instantiate a loader
            L = Loader()

            # Recieves data
            try:
                output = L.load(path)
            except:
                raise

            newplot = Data2D(image=output.data)
            newplot.setValues(output)
            self.plotpanel.add_image(newplot)

        dlg.Destroy()
Esempio n. 23
0
    def OnLoad(self, event):
        path = None
        dlg = wx.FileDialog(None, "Choose a file", os.getcwd(), "", "*",
                            wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            mypath = os.path.basename(path)
            from sas.dataloader.loader import Loader

            #Instantiate a loader
            L = Loader()

            #Recieves data
            try:
                output = L.load(path)
            except:
                raise

            newplot = Data2D(image=output.data)
            newplot.setValues(output)
            self.plotpanel.add_image(newplot)

        dlg.Destroy()
Esempio n. 24
0
class DataReader(CalcThread):
    """
        Load a data given a filename
    """
    def __init__(self,
                 path,
                 completefn=None,
                 updatefn=None,
                 yieldtime=0.01,
                 worktime=0.01):
        CalcThread.__init__(self, completefn, updatefn, yieldtime, worktime)
        self.path = path
        #Instantiate a loader
        self.loader = Loader()
        self.starttime = 0

    def isquit(self):
        """
             @raise KeyboardInterrupt: when the thread is interrupted
        """
        try:
            CalcThread.isquit(self)
        except KeyboardInterrupt:
            raise KeyboardInterrupt

    def compute(self):
        """
            read some data
        """
        self.starttime = time.time()
        try:
            data = self.loader.load(self.path)
            self.complete(data=data)
        except KeyboardInterrupt:
            # Thread was interrupted, just proceed and re-raise.
            # Real code should not print, but this is an example...
            raise
Esempio n. 25
0
class Plugin(PluginBase):

    def __init__(self):
        PluginBase.__init__(self, name="DataLoader")
        # Default location
        self._default_save_location = DEFAULT_OPEN_FOLDER
        self.loader = Loader()
        self._data_menu = None

    def populate_file_menu(self):
        """
        get a menu item and append it under file menu of the application
        add load file menu item and load folder item
        """
        # menu for data files
        menu_list = []
        data_file_hint = "load one or more data in the application"
        menu_list = [('&Load Data File(s)', data_file_hint, self.load_data)]
        gui_style = self.parent.get_style()
        style = gui_style & GUIFRAME.MULTIPLE_APPLICATIONS
        style1 = gui_style & GUIFRAME.DATALOADER_ON
        if style == GUIFRAME.MULTIPLE_APPLICATIONS:
            # menu for data from folder
            data_folder_hint = "load multiple data in the application"
            menu_list.append(('&Load Data Folder', data_folder_hint,
                              self._load_folder))
        return menu_list

    def load_data(self, event):
        """
        Load data
        """
        path = None
        self._default_save_location = self.parent._default_save_location
        if self._default_save_location == None:
            self._default_save_location = os.getcwd()

        cards = self.loader.get_wildcards()
        temp = [APPLICATION_WLIST] + PLUGINS_WLIST
        for item in temp:
            if item in cards:
                cards.remove(item)
        wlist = '|'.join(cards)
        style = wx.OPEN | wx.FD_MULTIPLE
        dlg = wx.FileDialog(self.parent,
                            "Choose a file",
                            self._default_save_location, "",
                            wlist,
                            style=style)
        if dlg.ShowModal() == wx.ID_OK:
            file_list = dlg.GetPaths()
            if len(file_list) >= 0 and not file_list[0] is None:
                self._default_save_location = os.path.dirname(file_list[0])
                path = self._default_save_location
        dlg.Destroy()

        if path is None or not file_list or file_list[0] is None:
            return
        self.parent._default_save_location = self._default_save_location
        self.get_data(file_list)


    def can_load_data(self):
        """
        if return True, then call handler to laod data
        """
        return True


    def _load_folder(self, event):
        """
        Load entire folder
        """
        path = None
        self._default_save_location = self.parent._default_save_location
        if self._default_save_location == None:
            self._default_save_location = os.getcwd()
        dlg = wx.DirDialog(self.parent, "Choose a directory",
                           self._default_save_location,
                           style=wx.DD_DEFAULT_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self._default_save_location = path
        dlg.Destroy()
        if path is not None:
            self._default_save_location = os.path.dirname(path)
        else:
            return
        file_list = self.get_file_path(path)
        self.get_data(file_list)
        self.parent._default_save_location = self._default_save_location

    def load_error(self, error=None):
        """
        Pop up an error message.

        :param error: details error message to be displayed
        """
        if error is not None or str(error).strip() != "":
            dial = wx.MessageDialog(self.parent, str(error), 'Error Loading File',
                                    wx.OK | wx.ICON_EXCLAMATION)
            dial.ShowModal()

    def get_file_path(self, path):
        """
        Receive a list containing folder then return a list of file
        """
        if os.path.isdir(path):
            return [os.path.join(os.path.abspath(path), filename) for filename in os.listdir(path)]

    def _process_data_and_errors(self, item, p_file, output, message):
        """
        Check to see if data set loaded with any errors. If so, append to
            error message to be sure user knows the issue.
        """
        data_error = False
        for error_data in item.errors:
            data_error = True
            message += "\tError: {0}\n".format(error_data)
        data = self.parent.create_gui_data(item, p_file)
        output[data.id] = data
        return output, message, data_error

    def get_data(self, path, format=None):
        """
        """
        message = ""
        log_msg = ''
        output = {}
        any_error = False
        data_error = False
        error_message = ""
        for p_file in path:
            info = "info"
            basename = os.path.basename(p_file)
            _, extension = os.path.splitext(basename)
            if extension.lower() in EXTENSIONS:
                any_error = True
                log_msg = "Data Loader cannot "
                log_msg += "load: %s\n" % str(p_file)
                log_msg += """Please try to open that file from "open project" """
                log_msg += """or "open analysis" menu\n"""
                error_message = log_msg + "\n"
                logging.info(log_msg)
                continue

            try:
                message = "Loading Data... " + str(p_file) + "\n"
                self.load_update(output=output, message=message, info=info)
                temp = self.loader.load(p_file, format)
                if temp.__class__.__name__ == "list":
                    for item in temp:
                        output, error_message, data_error = \
                            self._process_data_and_errors(item,
                                                          p_file,
                                                          output,
                                                          error_message)
                else:
                    output, error_message, data_error = \
                            self._process_data_and_errors(temp,
                                                          p_file,
                                                          output,
                                                          error_message)
            except:
                any_error = True
            if any_error or error_message != "":
                if error_message == "":
                    error = "Error: " + str(sys.exc_info()[1]) + "\n"
                    error += "while loading Data: \n%s\n" % str(basename)
                    error_message += "The data file you selected could not be loaded.\n"
                    error_message += "Make sure the content of your file"
                    error_message += " is properly formatted.\n\n"
                    error_message += "When contacting the SasView team, mention the"
                    error_message += " following:\n%s" % str(error)
                elif data_error:
                    base_message = "Errors occurred while loading "
                    base_message += "{0}\n".format(basename)
                    base_message += "The data file loaded but with errors.\n"
                    error_message = base_message + error_message
                else:
                    error_message += "%s\n" % str(p_file)
                info = "error"
        
        if any_error or error_message:
            self.load_update(output=output, message=error_message, info=info)
        else:
            message = "Loading Data Complete! "
        message += log_msg
        self.load_complete(output=output, error_message=error_message,
                           message=message, path=path, info='info')

    def load_update(self, output=None, message="", info="warning"):
        """
        print update on the status bar
        """
        if message != "":
            wx.PostEvent(self.parent, StatusEvent(status=message, info=info,
                                                  type="progress"))
    def load_complete(self, output, message="", error_message="", path=None,
                      info="warning"):
        """
         post message to  status bar and return list of data
        """
        wx.PostEvent(self.parent, StatusEvent(status=message,
                                              info=info,
                                              type="stop"))
        # if error_message != "":
        #    self.load_error(error_message)
        self.parent.add_data(data_list=output)
Esempio n. 26
0
        self.msg_txt.SetValue('No message available')
        self.sizer.Add(self.msg_txt, 1, wx.EXPAND | wx.ALL, 10)
        if self._data is not None:
            self.set_message(msg=self._data.__str__())

        self.SetSizer(self.sizer)

    def set_manager(self, manager):
        """
        Set the manager of this window
        """
        self._manager = manager

    def set_message(self, msg=""):
        """
        Display the message received
        """
        self.msg_txt.SetValue(str(msg))


if __name__ == "__main__":

    app = wx.App()
    # Instantiate a loader
    loader = Loader()
    # Load data
    test_data = loader.load("MAR07232_rest.ASC")
    dlg = ConsoleDialog(data=test_data)
    dlg.ShowModal()
    app.MainLoop()
Esempio n. 27
0
class testLoader(unittest.TestCase):
    logging.debug("Inside testLoad module")
    
    """ test fitting """
    def setUp(self):
        """
            Set up the initial conditions before _each_ test
            so that they all start from the same well-defined state. 
        """
        #Creating a loader
        self.L=Loader()
        
     
    def testLoad0(self):
        """test reading empty file"""
        self.assertRaises(RuntimeError, self.L.load, 'empty.txt')
        
    def testLoad1(self):
        """test reading 2 columns"""
        
        #Testing loading a txt file of 2 columns, the only reader should be read1 
        output=self.L.load('test_2_columns.txt') 
        x=[2.83954,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449,1.42857,1.63265]
        y=[0.6,3.44938, 5.82026,5.27591,5.2781,5.22531,7.47487,7.85852,10.2278]
        dx=[]
        dy=[]
        self.assertEqual(len(output.x),len(x))
        self.assertEqual(len(output.y),len(y))
        
        for i in range(len(x)):
            self.assertEqual(output.x[i],x[i])
            self.assertEqual(output.y[i],y[i])
       
    
    def testLoad2(self):
        """Testing loading a txt file of 3 columns"""
        output= self.L.load('test_3_columns.txt') 
        x=[0,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449]    
        y=[2.83954,3.44938,5.82026,5.27591,5.2781,5.22531,7.47487]
        dx=[]
        dy=[0.6,0.676531,0.753061,0.829592,0.906122,0.982653,1.05918]
        self.assertEqual(len(output.x),len(x))
        self.assertEqual(len(output.y),len(y))
        self.assertEqual(len(output.dy),len(dy))
        for i in range(len(x)):
            self.assertEqual(output.x[i],x[i])
            self.assertEqual(output.y[i],y[i])
            self.assertEqual(output.dy[i],dy[i])
       
    def testLoad2_uppercase(self):
        """Testing loading a txt file of 3 columns"""
        output= self.L.load('test_3_columns.TXT') 
        x=[0,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449]    
        y=[2.83954,3.44938,5.82026,5.27591,5.2781,5.22531,7.47487]
        dx=[]
        dy=[0.6,0.676531,0.753061,0.829592,0.906122,0.982653,1.05918]
        self.assertEqual(len(output.x),len(x))
        self.assertEqual(len(output.y),len(y))
        self.assertEqual(len(output.dy),len(dy))
        for i in range(len(x)):
            self.assertEqual(output.x[i],x[i])
            self.assertEqual(output.y[i],y[i])
            self.assertEqual(output.dy[i],dy[i])
       
    
    def testload3(self):
        """ Testing loading Igor data"""
        #tested good file.asc
        output= self.L.load('MAR07232_rest.ASC') 
        self.assertEqual(output.xmin,-0.018558945804750416)
        self.assertEqual(output.xmax, 0.016234058202440633,)
        self.assertEqual(output.ymin,-0.01684257151702391)
        self.assertEqual(output.ymax,0.017950440578015116)
       
        #tested corrupted file.asc
        try:self.L.load('AR07232_rest.ASC')
        except ValueError,msg:
           #logging.log(10,str(msg))
           logging.error(str(msg))
Esempio n. 28
0
from bumps.names import *

from sasmodels import core, bumps_model

if True: # fix when data loader exists
#    from sas.dataloader.readers\
    from sas.dataloader.loader import Loader
    loader = Loader()
    filename = 'sphere.ses'
    data = loader.load(filename)
    if data is None: raise IOError("Could not load file %r"%(filename,))
    data.x /= 10
#    print data
#    data = load_sesans('mydatfile.pz')
#    sans_data = load_sans('mysansfile.xml')

else:
    SElength = np.linspace(0, 2400, 61) # [A]
    data = np.ones_like(SElength)
    err_data = np.ones_like(SElength)*0.03

    class Sample:
        zacceptance = 0.1 # [A^-1]
        thickness = 0.2 # [cm]

    class SESANSData1D:
        #q_zmax = 0.23 # [A^-1]
        lam = 0.2 # [nm]
        x = SElength
        y = data
        dy = err_data
Esempio n. 29
0
        self.msg_txt.SetValue("No message available")
        self.sizer.Add(self.msg_txt, 1, wx.EXPAND | wx.ALL, 10)
        if self._data is not None:
            self.set_message(msg=self._data.__str__())

        self.SetSizer(self.sizer)

    def set_manager(self, manager):
        """
        Set the manager of this window
        """
        self._manager = manager

    def set_message(self, msg=""):
        """
        Display the message received
        """
        self.msg_txt.SetValue(str(msg))


if __name__ == "__main__":

    app = wx.App()
    # Instantiate a loader
    loader = Loader()
    # Load data
    test_data = loader.load("MAR07232_rest.ASC")
    dlg = ConsoleDialog(data=test_data)
    dlg.ShowModal()
    app.MainLoop()
Esempio n. 30
0
class abs_reader(unittest.TestCase):
    
    def setUp(self):
        self.loader = Loader()
        
    def test_checkdata(self):
        """
            Test .ABS file loaded as ascii
        """
        f = self.loader.load("ascii_test_1.txt")
        # The length of the data is 10
        self.assertEqual(len(f.x), 10)
        self.assertEqual(f.x[0],0.002618)
        self.assertEqual(f.x[9],0.0497)
        self.assertEqual(f.x_unit, '1/A')
        self.assertEqual(f.y_unit, '1/cm')
        
        self.assertEqual(f.meta_data['loader'],"ASCII")
        
    def test_truncated_1(self):
        """
            Test an ascii file with header and a 
            comment line in the middle of the data section.
            The business rule says that we should stop
            reading at the first comment once the data
            section has started (and treat the comment
            as though it were the start of a footer).
        """
        # Test .ABS file loaded as ascii
        f = self.loader.load("ascii_test_2.txt")
        # The length of the data is 10
        self.assertEqual(len(f.x), 5)
        self.assertEqual(f.x[0],0.002618)
        self.assertEqual(f.x[4],0.02356)
        
    def test_truncated_2(self):
        """
            Test a 6-col ascii file with header and a 
            line with only 2 columns in the middle of the data section.
            The business rule says that we should stop
            reading at the first inconsitent line.
        """
        # Test .ABS file loaded as ascii
        f = self.loader.load("ascii_test_3.txt")
        # The length of the data is 5
        self.assertEqual(len(f.x), 5)
        self.assertEqual(f.x[0],0.002618)
        self.assertEqual(f.x[4],0.02356)
        
    def test_truncated_3(self):
        """
            Test a 6-col ascii file with complex header and 
            many lines with 2 or 2 columns in the middle of the data section.
            The business rule says that we should stop
            reading at the last line of header.
        """
        # Test .ABS file loaded as ascii
        f = self.loader.load("ascii_test_4.abs")
        # The length of the data is 5
        self.assertEqual(len(f.x), 5)
        self.assertEqual(f.x[0],0.012654)
        self.assertEqual(f.x[4],0.02654)
        
    def test_truncated_4(self):
        """
            Test mix of 6-col and 2-col.
            Only the last 5 2-col lines should be read.
        """
        # Test .ABS file loaded as ascii
        f = self.loader.load("ascii_test_5.txt")
        # The length of the data is 5
        self.assertEqual(len(f.x), 5)
        self.assertEqual(f.x[0],0.02879)
        self.assertEqual(f.x[4],0.0497)
        
    def test_truncated_5(self):
        """
            Test a 6-col ascii file with complex header where one of them has a letter and 
            many lines with 2 or 2 columns in the middle of the data section.
            Only last four lines should be read.
        """
        # Test .ABS file loaded as ascii
        f = self.loader.load("ascii_test_6.txt")
        # The length of the data is 5
        self.assertEqual(len(f.x), 4)
        self.assertEqual(f.x[0],0.013534)
        self.assertEqual(f.x[3],0.022254)
Esempio n. 31
0
class Plugin(PluginBase):

    def __init__(self, standalone=False):
        PluginBase.__init__(self, name="DataLoader", standalone=standalone)
        # Default location
        self._default_save_location = DEFAULT_OPEN_FOLDER
        self.loader = Loader()
        self._data_menu = None

    def populate_file_menu(self):
        """
        get a menu item and append it under file menu of the application
        add load file menu item and load folder item
        """
        # menu for data files
        menu_list = []
        data_file_hint = "load one or more data in the application"
        menu_list = [('&Load Data File(s)', data_file_hint, self.load_data)]
        gui_style = self.parent.get_style()
        style = gui_style & GUIFRAME.MULTIPLE_APPLICATIONS
        style1 = gui_style & GUIFRAME.DATALOADER_ON
        if style == GUIFRAME.MULTIPLE_APPLICATIONS:
            # menu for data from folder
            data_folder_hint = "load multiple data in the application"
            menu_list.append(('&Load Data Folder', data_folder_hint,
                              self._load_folder))
        return menu_list

    def load_data(self, event):
        """
        Load data
        """
        path = None
        self._default_save_location = self.parent._default_save_location
        if self._default_save_location == None:
            self._default_save_location = os.getcwd()

        cards = self.loader.get_wildcards()
        temp = [APPLICATION_WLIST] + PLUGINS_WLIST
        for item in temp:
            if item in cards:
                cards.remove(item)
        wlist = '|'.join(cards)
        style = wx.OPEN | wx.FD_MULTIPLE
        dlg = wx.FileDialog(self.parent,
                            "Choose a file",
                            self._default_save_location, "",
                            wlist,
                            style=style)
        if dlg.ShowModal() == wx.ID_OK:
            file_list = dlg.GetPaths()
            if len(file_list) >= 0 and not file_list[0] is None:
                self._default_save_location = os.path.dirname(file_list[0])
                path = self._default_save_location
        dlg.Destroy()

        if path is None or not file_list or file_list[0] is None:
            return
        self.parent._default_save_location = self._default_save_location
        self.get_data(file_list)


    def can_load_data(self):
        """
        if return True, then call handler to laod data
        """
        return True


    def _load_folder(self, event):
        """
        Load entire folder
        """
        path = None
        self._default_save_location = self.parent._default_save_location
        if self._default_save_location == None:
            self._default_save_location = os.getcwd()
        dlg = wx.DirDialog(self.parent, "Choose a directory",
                           self._default_save_location,
                           style=wx.DD_DEFAULT_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self._default_save_location = path
        dlg.Destroy()
        if path is not None:
            self._default_save_location = os.path.dirname(path)
        else:
            return
        file_list = self.get_file_path(path)
        self.get_data(file_list)
        self.parent._default_save_location = self._default_save_location

    def load_error(self, error=None):
        """
        Pop up an error message.

        :param error: details error message to be displayed
        """
        if error is not None or str(error).strip() != "":
            dial = wx.MessageDialog(self.parent, str(error), 'Error Loading File',
                                    wx.OK | wx.ICON_EXCLAMATION)
            dial.ShowModal()

    def get_file_path(self, path):
        """
        Receive a list containing folder then return a list of file
        """
        if os.path.isdir(path):
            return [os.path.join(os.path.abspath(path), filename) for filename in os.listdir(path)]

    def _process_data_and_errors(self, item, p_file, output, message):
        """
        Check to see if data set loaded with any errors. If so, append to
            error message to be sure user knows the issue.
        """
        data_error = False
        for error_data in item.errors:
            data_error = True
            message += "\tError: {0}\n".format(error_data)
        data = self.parent.create_gui_data(item, p_file)
        output[data.id] = data
        return output, message, data_error

    def get_data(self, path, format=None):
        """
        """
        message = ""
        log_msg = ''
        output = {}
        any_error = False
        data_error = False
        error_message = ""
        for p_file in path:
            info = "info"
            basename = os.path.basename(p_file)
            _, extension = os.path.splitext(basename)
            if extension.lower() in EXTENSIONS:
                any_error = True
                log_msg = "Data Loader cannot "
                log_msg += "load: %s\n" % str(p_file)
                log_msg += """Please try to open that file from "open project" """
                log_msg += """or "open analysis" menu\n"""
                error_message = log_msg + "\n"
                logging.info(log_msg)
                continue

            try:
                message = "Loading Data... " + str(p_file) + "\n"
                self.load_update(output=output, message=message, info=info)
                temp = self.loader.load(p_file, format)
                if temp.__class__.__name__ == "list":
                    for item in temp:
                        output, error_message, data_error = \
                            self._process_data_and_errors(item,
                                                          p_file,
                                                          output,
                                                          error_message)
                else:
                    output, error_message, data_error = \
                            self._process_data_and_errors(temp,
                                                          p_file,
                                                          output,
                                                          error_message)
            except:
                any_error = True
            if any_error or error_message != "":
                if error_message == "":
                    error = "Error: " + str(sys.exc_info()[1]) + "\n"
                    error += "while loading Data: \n%s\n" % str(basename)
                    error_message += "The data file you selected could not be loaded.\n"
                    error_message += "Make sure the content of your file"
                    error_message += " is properly formatted.\n\n"
                    error_message += "When contacting the SasView team, mention the"
                    error_message += " following:\n%s" % str(error)
                elif data_error:
                    base_message = "Errors occurred while loading "
                    base_message += "{0}\n".format(basename)
                    base_message += "The data file loaded but with errors.\n"
                    error_message = base_message + error_message
                else:
                    error_message += "%s\n" % str(p_file)
                info = "error"
        
        if any_error or error_message:
            self.load_update(output=output, message=error_message, info=info)
        else:
            message = "Loading Data Complete! "
        message += log_msg
        self.load_complete(output=output, error_message=error_message,
                           message=message, path=path, info='info')

    def load_update(self, output=None, message="", info="warning"):
        """
        print update on the status bar
        """
        if message != "":
            wx.PostEvent(self.parent, StatusEvent(status=message, info=info,
                                                  type="progress"))
    def load_complete(self, output, message="", error_message="", path=None,
                      info="warning"):
        """
         post message to  status bar and return list of data
        """
        wx.PostEvent(self.parent, StatusEvent(status=message,
                                              info=info,
                                              type="stop"))
        # if error_message != "":
        #    self.load_error(error_message)
        self.parent.add_data(data_list=output)
Esempio n. 32
0
class abs_reader(unittest.TestCase):
    def setUp(self):
        self.loader = Loader()

    def test_checkdata(self):
        """
            Test .ABS file loaded as ascii
        """
        f = self.loader.load("ascii_test_1.txt")
        # The length of the data is 10
        self.assertEqual(len(f.x), 10)
        self.assertEqual(f.x[0], 0.002618)
        self.assertEqual(f.x[9], 0.0497)
        self.assertEqual(f.x_unit, '1/A')
        self.assertEqual(f.y_unit, '1/cm')

        self.assertEqual(f.meta_data['loader'], "ASCII")

    def test_truncated_1(self):
        """
            Test an ascii file with header and a 
            comment line in the middle of the data section.
            The business rule says that we should stop
            reading at the first comment once the data
            section has started (and treat the comment
            as though it were the start of a footer).
        """
        # Test .ABS file loaded as ascii
        f = self.loader.load("ascii_test_2.txt")
        # The length of the data is 10
        self.assertEqual(len(f.x), 5)
        self.assertEqual(f.x[0], 0.002618)
        self.assertEqual(f.x[4], 0.02356)

    def test_truncated_2(self):
        """
            Test a 6-col ascii file with header and a 
            line with only 2 columns in the middle of the data section.
            The business rule says that we should stop
            reading at the first inconsitent line.
        """
        # Test .ABS file loaded as ascii
        f = self.loader.load("ascii_test_3.txt")
        # The length of the data is 5
        self.assertEqual(len(f.x), 5)
        self.assertEqual(f.x[0], 0.002618)
        self.assertEqual(f.x[4], 0.02356)

    def test_truncated_3(self):
        """
            Test a 6-col ascii file with complex header and 
            many lines with 2 or 2 columns in the middle of the data section.
            The business rule says that we should stop
            reading at the last line of header.
        """
        # Test .ABS file loaded as ascii
        f = self.loader.load("ascii_test_4.abs")
        # The length of the data is 5
        self.assertEqual(len(f.x), 5)
        self.assertEqual(f.x[0], 0.012654)
        self.assertEqual(f.x[4], 0.02654)

    def test_truncated_4(self):
        """
            Test mix of 6-col and 2-col.
            Only the last 5 2-col lines should be read.
        """
        # Test .ABS file loaded as ascii
        f = self.loader.load("ascii_test_5.txt")
        # The length of the data is 5
        self.assertEqual(len(f.x), 5)
        self.assertEqual(f.x[0], 0.02879)
        self.assertEqual(f.x[4], 0.0497)

    def test_truncated_5(self):
        """
            Test a 6-col ascii file with complex header where one of them has a letter and 
            many lines with 2 or 2 columns in the middle of the data section.
            Only last four lines should be read.
        """
        # Test .ABS file loaded as ascii
        f = self.loader.load("ascii_test_6.txt")
        # The length of the data is 5
        self.assertEqual(len(f.x), 4)
        self.assertEqual(f.x[0], 0.013534)
        self.assertEqual(f.x[3], 0.022254)