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)
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)
def __init__(self): PluginBase.__init__(self, name="Pr Inversion") ## Simulation window manager self.simview = None ## State data self.alpha = self.DEFAULT_ALPHA self.nfunc = self.DEFAULT_NFUNC self.max_length = self.DEFAULT_DMAX self.q_min = None self.q_max = None self.has_bck = False self.slit_height = 0 self.slit_width = 0 ## Remember last plottable processed self.last_data = "" self._current_file_data = None ## Time elapsed for last computation [sec] # Start with a good default self.elapsed = 0.022 self.iq_data_shown = False ## Current invertor self.invertor = None self.pr = None self.data_id = IQ_DATA_LABEL # Copy of the last result in case we need to display it. self._last_pr = None self._last_out = None self._last_cov = None ## Calculation thread self.calc_thread = None ## Estimation thread self.estimation_thread = None ## Result panel self.control_panel = None ## Currently views plottable self.current_plottable = None ## Number of P(r) points to display on the output plot self._pr_npts = 51 self._normalize_output = False self._scale_output_unity = False ## List of added P(r) plots self._added_plots = {} self._default_Iq = {} self.list_plot_id = [] # Associate the inversion state reader with .prv files from inversion_state import Reader # Create a CanSAS/Pr reader self.state_reader = Reader(self.set_state) self._extensions = '.prv' l = Loader() l.associate_file_reader('.prv', self.state_reader) #l.associate_file_reader(".svs", self.state_reader) # Log startup logging.info("Pr(r) plug-in started")
def get_panels(self, parent): """ Create and return the list of wx.Panels for your plug-in. Define the plug-in perspective. Panels should inherit from DefaultPanel defined below, or should present the same interface. They must define "window_caption" and "window_name". :param parent: parent window :return: list of panels """ # # Save a reference to the parent self.parent = parent self.frame = MDIFrame(self.parent, None, 'None', (100, 200)) self.invariant_panel = InvariantPanel(parent=self.frame) self.frame.set_panel(self.invariant_panel) self._frame_set_helper() self.invariant_panel.set_manager(manager=self) self.perspective.append(self.invariant_panel.window_name) # Create reader when fitting panel are created self.state_reader = reader(self.set_state) # append that reader to list of available reader loader = Loader() loader.associate_file_reader(".inv", self.state_reader) # loader.associate_file_reader(".svs", self.state_reader) # Return the list of panels return [self.invariant_panel]
def fit_single(self, isdream=False): fitter = Fit() data = Loader().load("testdata_line.txt") data.name = data.filename fitter.set_data(data,1) # Receives the type of model for the fitting model1 = LineModel() model1.name = "M1" model = Model(model1,data) pars1= ['A','B'] fitter.set_model(model,1,pars1) fitter.select_problem_for_fit(id=1,value=1) result1, = fitter.fit(handler=FitHandler()) # The target values were generated from the following statements p,s,fx = result1.pvec, result1.stderr, result1.fitness #print "p0,p1,s0,s1,fx = %g, %g, %g, %g, %g"%(p[0],p[1],s[0],s[1],fx) p0,p1,s0,s1,fx_ = 3.68353, 2.61004, 0.336186, 0.105244, 1.20189 if isdream: # Dream is not a minimizer: just check that the fit is within # uncertainty self.assertTrue( abs(p[0]-p0) <= s0 ) self.assertTrue( abs(p[1]-p1) <= s1 ) else: self.assertTrue( abs(p[0]-p0) <= 1e-5 ) self.assertTrue( abs(p[1]-p1) <= 1e-5 ) self.assertTrue( abs(fx-fx_) <= 1e-5 )
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): """ initialize data""" self.data1=Loader().load("cyl_400_20.txt") self.data2=Loader().load("cyl_400_40.txt") # Receives the type of model for the fitting from sas.models.CylinderModel import CylinderModel cyl1 = CylinderModel() cyl1.name = "C1" self.model1 = Model(cyl1) self.model1.set(scale= 1.0) self.model1.set(radius=18) self.model1.set(length=200) self.model1.set(sldCyl=3e-006, sldSolv=0.0) self.model1.set(background=0.0) cyl2 = CylinderModel() cyl2.name = "C2" self.model2 = Model(cyl2) self.model2.set(scale= 1.0) self.model2.set(radius=37) self.model2.set(length=300) self.model2.set(sldCyl=3e-006, sldSolv=0.0) self.model2.set(background=0.0)
def on_click_save(self, event): """ Save change into a file """ if not self._data: return self.on_change_run(event=None) self.on_change_title(event=None) path = None wildcard = "CanSAS 1D files(*.xml)|*.xml" dlg = wx.FileDialog(self, "Choose a file", self._default_save_location, "", wildcard , wx.SAVE) for data in self._data: if issubclass(data.__class__, Data2D): msg = "No conventional writing format for \n\n" msg += "Data2D at this time.\n" dlg = wx.MessageDialog(None, msg, 'Error Loading File', wx.OK | wx.ICON_EXCLAMATION) dlg.ShowModal() else: if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() mypath = os.path.basename(path) loader = Loader() format = ".xml" if os.path.splitext(mypath)[1].lower() == format: loader.save(path, data, format) try: self._default_save_location = os.path.dirname(path) except: pass dlg.Destroy() event.Skip()
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)
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 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"
def fit_single(self, fitter_name, isdream=False): fitter = Fit(fitter_name) data = Loader().load("testdata_line.txt") data.name = data.filename fitter.set_data(data, 1) # Receives the type of model for the fitting model1 = LineModel() model1.name = "M1" model = Model(model1, data) pars1 = ['A', 'B'] fitter.set_model(model, 1, pars1) fitter.select_problem_for_fit(id=1, value=1) result1, = fitter.fit(handler=FitHandler()) # The target values were generated from the following statements p, s, fx = result1.pvec, result1.stderr, result1.fitness #print "p0,p1,s0,s1,fx = %g, %g, %g, %g, %g"%(p[0],p[1],s[0],s[1],fx) p0, p1, s0, s1, fx_ = 3.68353, 2.61004, 0.336186, 0.105244, 1.20189 if isdream: # Dream is not a minimizer: just check that the fit is within # uncertainty self.assertTrue(abs(p[0] - p0) <= s0) self.assertTrue(abs(p[1] - p1) <= s1) else: self.assertTrue(abs(p[0] - p0) <= 1e-5) self.assertTrue(abs(p[1] - p1) <= 1e-5) self.assertTrue(abs(fx - fx_) <= 1e-5)
def test_singleton(self): """ Testing whether Loader is truly a singleton """ # Create a 'new' Loader b = Loader() self.assertEqual(self.loader._get_registry_creation_time(), b._get_registry_creation_time())
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 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())
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.)
def setUp(self): # NIST sample data self.data = Loader().load("CMSphere5.txt") # NIST smeared sphere w/ param values below self.answer = Loader().load("CMSphere5smearsphere.txt") # call spheremodel self.model = SphereModel() # setparams consistent with Igor default self.model.setParam('scale', 1.0) self.model.setParam('background', 0.01) self.model.setParam('radius', 60.0) self.model.setParam('sldSolv', 6.3e-06) self.model.setParam('sldSph', 1.0e-06)
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.)
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)
def test_bad_pars(self): fitter = Fit('bumps') data = Loader().load("testdata_line.txt") data.name = data.filename fitter.set_data(data, 1) model1 = LineModel() model1.name = "M1" model = Model(model1, data) pars1 = ['param1', 'param2'] try: fitter.set_model(model, 1, pars1) except ValueError, exc: #print "ValueError was correctly raised: "+str(msg) assert str(exc).startswith('parameter param1')
def find_extension(): """ Describe the extensions that can be read by the current application """ try: list = [] EXCEPTION_LIST = ['*', '.', ''] from sas.dataloader.loader import Loader wild_cards = Loader().get_wildcards() for item in wild_cards: #['All (*.*)|*.*'] file_type, ext = string.split(item, "|*.", 1) if ext.strip() not in EXCEPTION_LIST and ext.strip() not in list: list.append(ext) except: pass try: file_type, ext = string.split(local_config.APPLICATION_WLIST, "|*.", 1) if ext.strip() not in EXCEPTION_LIST and ext.strip() not in list: list.append(ext) except: pass try: for item in local_config.PLUGINS_WLIST: file_type, ext = string.split(item, "|*.", 1) if ext.strip() not in EXCEPTION_LIST and ext.strip() not in list: list.append(ext) except: pass return list
def test_slits(self): """ Check slit data """ filename = "cansas1d_slit.xml" data = Loader().load(filename) self.data = data[0] self.assertEqual(self.data.filename, filename) self.assertEqual(self.data.run[0], "1234") # Data self.assertEqual(len(self.data.x), 2) self.assertEqual(self.data.x_unit, '1/A') self.assertEqual(self.data.y_unit, '1/cm') self.assertEqual(self.data.x[0], 0.02) self.assertEqual(self.data.y[0], 1000) self.assertEqual(self.data.dxl[0], 0.005) self.assertEqual(self.data.dxw[0], 0.001) self.assertEqual(self.data.dy[0], 3) self.assertEqual(self.data.x[1], 0.03) self.assertAlmostEquals(self.data.y[1], 1001.0) self.assertEqual(self.data.dx, None) self.assertEqual(self.data.dxl[1], 0.005) self.assertEqual(self.data.dxw[1], 0.001) self.assertEqual(self.data.dy[1], 4) self.assertEqual(self.data.run_name['1234'], 'run name') self.assertEqual(self.data.title, "Test title")
def test_cylinder_fit(self): """ Simple cylinder model fit """ out = Loader().load("cyl_400_20.txt") fitter = Fit('bumps') # Receives the type of model for the fitting from sas.models.CylinderModel import CylinderModel model = CylinderModel() model.setParam('sldCyl', 1) model.setParam('sldSolv', 0) model.setParam('scale', 1e-10) pars1 = ['length', 'radius', 'scale'] fitter.set_data(out, 1) fitter.set_model(model, 1, pars1, constraints=()) fitter.select_problem_for_fit(id=1, value=1) result1, = fitter.fit() #print result1 #print result1.__dict__ self.assert_(result1) self.assertTrue(len(result1.pvec) > 0 or len(result1.pvec) == 0) self.assertTrue(len(result1.stderr) > 0 or len(result1.stderr) == 0) self.assertTrue( math.fabs(result1.pvec[0] - 400.0) / 3.0 < result1.stderr[0]) self.assertTrue( math.fabs(result1.pvec[1] - 20.0) / 3.0 < result1.stderr[1]) self.assertTrue( math.fabs(result1.pvec[2] - 9.0e-12) / 3.0 < result1.stderr[2]) self.assertTrue(result1.fitness < 1.0)
def test_without_resolution(self): """ Simple cylinder model fit """ out=Loader().load("cyl_400_20.txt") # This data file has not error, add them #out.dy = out.y fitter = Fit() fitter.set_data(out,1) # Receives the type of model for the fitting model1 = CylinderModel() model1.setParam("scale", 1.0) model1.setParam("radius",18) model1.setParam("length", 397) model1.setParam("sldCyl",3e-006 ) model1.setParam("sldSolv",0.0 ) model1.setParam("background", 0.0) model = Model(model1) pars1 =['length','radius','scale'] fitter.set_model(model,1,pars1) # What the hell is this line for? fitter.select_problem_for_fit(id=1,value=1) result1, = fitter.fit() #print "result1",result1 self.assert_(result1) self.assertTrue(len(result1.pvec) > 0) self.assertTrue(len(result1.stderr) > 0) self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) self.assertTrue( math.fabs(result1.pvec[2]-1)/3.0 < result1.stderr[2] ) self.assertTrue( result1.fitness < 1.0 )
def test_sectorphi_full(self): """ Test sector averaging I(phi) When considering the whole azimuthal range (2pi), the answer should be the same as ring averaging. The test data was not generated by IGOR. """ from sas.dataloader.manipulations import SectorPhi import math nbins = 19 phi_min = math.pi / (nbins + 1) phi_max = math.pi * 2 - phi_min r = SectorPhi(r_min=.005, r_max=.01, phi_min=phi_min, phi_max=phi_max, nbins=nbins) o = r(self.data) answer = Loader().load('ring_testdata.txt') for i in range(len(o.x)): self.assertAlmostEqual(o.x[i], answer.x[i], 4) self.assertAlmostEqual(o.y[i], answer.y[i], 4) self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
def test_bad_pars(self): fitter = Fit() data = Loader().load("testdata_line.txt") data.name = data.filename fitter.set_data(data,1) model1 = LineModel() model1.name = "M1" model = Model(model1, data) pars1= ['param1','param2'] try: fitter.set_model(model,1,pars1) except ValueError,exc: #print "ValueError was correctly raised: "+str(msg) assert str(exc).startswith('parameter param1')
def _reset_helper(self, path=None, npts=NPTS): """ Set value to fitter and prepare inputs for map function """ for i in range(npts): data = Loader().load(path) fitter = Fit() #create model model = CylinderModel() model.setParam('scale', 1.0) model.setParam('radius', 20.0) model.setParam('length', 400.0) model.setParam('sldCyl', 4e-006) model.setParam('sldSolv', 1e-006) model.setParam('background', 0.0) for param in model.dispersion.keys(): model.set_dispersion(param, self.polydisp['gaussian']()) model.setParam('cyl_phi.width', 10) model.setParam('cyl_phi.npts', 3) model.setParam('cyl_theta.nsigmas', 10) # for 2 data cyl_theta = 60.0 [deg] cyl_phi= 60.0 [deg] fitter.set_model(model, i, self.param_to_fit, self.list_of_constraints) #smear data current_smearer = smear_selection(data, model) import cPickle p = cPickle.dumps(current_smearer) sm = cPickle.loads(p) fitter.set_data(data=data, id=i, smearer=current_smearer, qmin=self.qmin, qmax=self.qmax) fitter.select_problem_for_fit(id=i, value=1) self.list_of_fitter.append(copy.deepcopy(fitter)) self.list_of_function.append('fit') self.list_of_mapper.append(classMapper)
def _dispersion(self, fitter): # Load data # This data is for a cylinder with # length=400, radius=20, radius disp=5, scale=1e-10 out = Loader().load("cyl_400_20_disp5r.txt") out.dy = numpy.zeros(len(out.y)) for i in range(len(out.y)): out.dy[i] = math.sqrt(out.y[i]) # Receives the type of model for the fitting model1 = CylinderModel() model1.setParam("scale", 10.0) model1.setParam("radius", 18) model1.setParam("length", 397) model1.setParam("sldCyl", 3e-006) model1.setParam("sldSolv", 0.0) model1.setParam("background", 0.0) # Dispersion parameters model1.dispersion['radius']['width'] = 0.25 model1.dispersion['radius']['npts'] = 50 model = Model(model1) pars1 = ['length', 'radius', 'scale', 'radius.width'] fitter.set_data(out, 1) fitter.set_model(model, 1, pars1) fitter.select_problem_for_fit(id=1, value=1) #import time; T0 = time.time() result1, = fitter.fit() #print "time",time.time()-T0,fitter._engine.__class__.__name__ self.assert_(result1) self.assertTrue(len(result1.pvec) > 0) self.assertTrue(len(result1.stderr) > 0) #print [z for z in zip(result1.param_list,result1.pvec,result1.stderr)] self.assertTrue( math.fabs(result1.pvec[0] - 399.8) / 3.0 < result1.stderr[0]) self.assertTrue( math.fabs(result1.pvec[1] - 17.5) / 3.0 < result1.stderr[1]) self.assertTrue( math.fabs(result1.pvec[2] - 11.1) / 3.0 < result1.stderr[2]) self.assertTrue( math.fabs(result1.pvec[3] - 0.276) / 3.0 < result1.stderr[3]) self.assertTrue(result1.fitness < 1.0)
def choose_data_file(self, location=None): """ Open a file dialog to allow loading a file """ path = None if location == None: location = os.getcwd() l = Loader() cards = l.get_wildcards() wlist = '|'.join(cards) dlg = wx.FileDialog(self, "Choose a file", location, "", wlist, wx.OPEN) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() mypath = os.path.basename(path) dlg.Destroy() return path
def test_units(self): """ Check units. Note that not all units are available. """ filename = "cansas1d_units.xml" data = Loader().load(filename) self.data = data[0] self.assertEqual(self.data.filename, filename) self._checkdata()
def setUp(self): data = Loader().load("latex_smeared.xml") self.data_res = data[0] self.data_slit = data[1] self.sphere = SphereModel() self.sphere.setParam('background', 0) self.sphere.setParam('radius', 5000.0) self.sphere.setParam('scale', 0.4) self.sphere.setParam('sldSolv',0) self.sphere.setParam('sldSph',1e-6)
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)
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)
def _dispersion(self, fitter): # Load data # This data is for a cylinder with # length=400, radius=20, radius disp=5, scale=1e-10 out=Loader().load("cyl_400_20_disp5r.txt") out.dy = numpy.zeros(len(out.y)) for i in range(len(out.y)): out.dy[i] = math.sqrt(out.y[i]) # Receives the type of model for the fitting model1 = CylinderModel() model1.setParam("scale", 10.0) model1.setParam("radius",18) model1.setParam("length", 397) model1.setParam("sldCyl",3e-006 ) model1.setParam("sldSolv",0.0 ) model1.setParam("background", 0.0) # Dispersion parameters model1.dispersion['radius']['width'] = 0.25 model1.dispersion['radius']['npts'] = 50 model = Model(model1) pars1 =['length','radius','scale','radius.width'] fitter.set_data(out,1) fitter.set_model(model,1,pars1) fitter.select_problem_for_fit(id=1,value=1) #import time; T0 = time.time() result1, = fitter.fit() self.assert_(result1) self.assertTrue(len(result1.pvec)>0) self.assertTrue(len(result1.stderr)>0) #print [z for z in zip(result1.param_list,result1.pvec,result1.stderr)] self.assertTrue( math.fabs(result1.pvec[0]-399.8)/3.0 < result1.stderr[0] ) self.assertTrue( math.fabs(result1.pvec[1]-17.5)/3.0 < result1.stderr[1] ) self.assertTrue( math.fabs(result1.pvec[2]-11.1)/3.0 < result1.stderr[2] ) self.assertTrue( math.fabs(result1.pvec[3]-0.276)/3.0 < result1.stderr[3] ) self.assertTrue( result1.fitness < 1.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)
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()
class designtest(unittest.TestCase): def setUp(self): self.loader = Loader() def test_singleton(self): """ Testing whether Loader is truly a singleton """ # Create a 'new' Loader b = Loader() self.assertEqual(self.loader._get_registry_creation_time(), b._get_registry_creation_time())
def test_writer(self): from sas.dataloader.readers.cansas_reader import Reader r = Reader() x = numpy.ones(5) y = numpy.ones(5) dy = numpy.ones(5) filename = "write_test.xml" r.write(filename, self.data) data = Loader().load(filename) self.data = data[0] self.assertEqual(self.data.filename, filename) self._checkdata()
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 setUp(self): """ initialize data""" self.data = Loader().load("cyl_400_20.txt") # Create model that fitting engine understands from sas.models.CylinderModel import CylinderModel self.model = CylinderModel() self.model.setParam("scale", 1.0) self.model.setParam("radius",18) self.model.setParam("length", 397) self.model.setParam("sldCyl",3e-006 ) self.model.setParam("sldSolv",0.0 ) self.model.setParam("background", 0.0) #select parameters to fit self.pars1 =['length','radius','scale']
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")
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
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)
def setUp(self): self.L=Loader() self.L.find_plugins('../plugins')
def __init__(self): PluginBase.__init__(self, name="DataLoader") # Default location self._default_save_location = DEFAULT_OPEN_FOLDER self.loader = Loader() self._data_menu = None
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)
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)
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))
def setUp(self): self.loader = Loader()
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