Пример #1
0
    if hasattr(material,'space_group'):
        peaks = from_unitcell(x.mean(axis=0),counts.mean(axis=0),material.unit_cell,
                            material.space_group,qmin,qmax,name=material.name,
                            ymin_on_ymax=ymin_on_ymax)
    else:
        peaks=[]
        for mat in material:
            peaks.extend(from_unitcell(x.mean(axis=0),counts.mean(axis=0),mat.unit_cell,
                            mat.space_group,qmin,qmax,name=mat.name,
                            ymin_on_ymax=ymin_on_ymax))
                            
    print 'fitting %i peaks'%len(peaks)
    print [p.name for p in peaks]
    
    fc = FitterCollection()
    fc.create_many(['element_%i'%i for i in range(counts.shape[0])],x,peaks,**kwargs)   
    fc.backup()    
    fc(counts,reestimate=False)    
    yield fc.lastfit_condensed,fc.lastresult_condensed,info
    
    for counts,info in source:
        fc.restore()
        fc(counts,reestimate=reestimate)
        yield fc.lastfit_condensed,fc.lastresult_condensed,info
       
def calcstrain(source,phi,Q0=None,allow_shear=True):

    if len(phi) > 2:
        take = np.ones(len(phi)).astype(bool)
        take[5] = take[8] = False
    else:
Пример #2
0
    def test_FitterCollection(self):
        

        #create one fitter to add
        fitter1 = ChunkFitter(self.x,makepeaks(self.x,self.y))         
        
        #create the collection with one fitter
        fc = FitterCollection(detector1=fitter1)
        
        #now make a second and add it to the collection
        fitter2 = ChunkFitter(self.x,makepeaks(self.x,self.y))
        fc.add('detector2',fitter2)
        
        #make a set of fitters and add all at once
        fitters = [('detector%i'%i,ChunkFitter(self.x,makepeaks(self.x,self.y))) for i in range(2,6)]
        fc.add_many(fitters)        

        #check create works 
        fc.create('detector6',self.x,makepeaks(self.x,self.y))
        
        #check create fails if we send 2d data        
        self.assertRaises(FitterError,fc.create,'detector6',np.tile(self.x,(3,1)),makepeaks(self.x,self.y))              
        
        #check create many works with ideal data
        names = ['detector%i'%i for i in range(7,10)]
        xs = np.tile(self.x,(len(names),1)) 
        fc.create_many(names,xs,makepeaks(self.x,self.y))
        
        #check we raise an error if we add with mismatching names and data shape
        xs = np.tile(self.x,(2,1)) #fake 2D data set only 2 wide (should be 1 or 4)
        names = ['detector%i'%i for i in range(4)]
        self.assertRaises(FitterError,fc.create_many,names,xs,makepeaks(self.x,self.y))

        detnames = ['detector1', 'detector2', 'detector3', 'detector4', 'detector5',
                    'detector6','detector7','detector8','detector9']
        parnames = ['centre','width','area','eta','asymmetry','background']
        peaknames = ['LaB6_110', 'LaB6_111', 'LaB6_200', 'LaB6_210', 
                     'LaB6_211', 'LaB6_220', 'LaB6_300', 'LaB6_310']
                     
        self.assertListEqual(fc.keys(),detnames)        

        bck = copy.deepcopy(fc)

#        #make sure refine fails if we send y data of wrong shape
#        ys = np.tile(self.y,(4,1)) 
#        self.assertRaises(FitterError,fc.refine,ys)
        
        #this checks that refine with 2D data works
        ys = np.tile(self.y,(9,1)) 
        fc.refine(ys)
        result = fc.lastresult #check results by accessing attributes
        yfit = fc.lastfit
        
        self.assertEqual(len(yfit),9)        
        self.assertListEqual(yfit.keys(),detnames)
        self.assertTupleEqual(yfit['detector1'].shape,(4096,))
        
        self.assertEqual(len(result),9)
        self.assertListEqual(result.keys(),detnames)  
        self.assertListEqual(result['detector1'].keys(),peaknames)
        self.assertListEqual(sorted(result['detector1']['LaB6_110'].keys()),sorted(parnames))
        
        cens = [result['detector1'][peaknames[i]]['centre'].nominal_value for i in range(6)]
        self.assertAlmostEquals(cens[0],2.1391,4)
        self.assertAlmostEquals(cens[1],2.6195,4)
        self.assertAlmostEquals(cens[2],3.0249,4)
        self.assertAlmostEquals(cens[3],3.3815,4)     
        self.assertAlmostEquals(cens[4],3.7053,4)     
        self.assertAlmostEquals(cens[5],4.2773,4)             
        
        fc = copy.deepcopy(bck)
        #check ok with 9 columns instead of rows
        yfit,result = fc.refine(ys.transpose()) #check results from returns    
        
        self.assertEqual(len(yfit),9)        
        self.assertListEqual(yfit.keys(),detnames)
        self.assertTupleEqual(yfit['detector1'].shape,(4096,))
        
        self.assertEqual(len(result),9)
        self.assertListEqual(result.keys(),detnames)  
        self.assertListEqual(result['detector1'].keys(),peaknames)
        self.assertListEqual(sorted(result['detector1']['LaB6_110'].keys()),sorted(parnames))

        cens = [result['detector1'][peaknames[i]]['centre'].nominal_value for i in range(6)]
        self.assertAlmostEquals(cens[0],2.1391,4)
        self.assertAlmostEquals(cens[1],2.6195,4)
        self.assertAlmostEquals(cens[2],3.0249,4)
        self.assertAlmostEquals(cens[3],3.3815,4)     
        self.assertAlmostEquals(cens[4],3.7053,4)     
        self.assertAlmostEquals(cens[5],4.2773,4)
        
        fc = copy.deepcopy(bck)
        #check ok with dictionary
        yd ={detnames[i] : ys[i] for i in range(len(ys))}
        yfit,result = fc.refine(yd) #check results from returns    
        
        self.assertEqual(len(yfit),9)        
        self.assertListEqual(yfit.keys(),detnames)
        self.assertTupleEqual(yfit['detector1'].shape,(4096,))
        
        self.assertEqual(len(result),9)
        self.assertListEqual(result.keys(),detnames)  
        self.assertListEqual(result['detector1'].keys(),peaknames)
        self.assertListEqual(sorted(result['detector1']['LaB6_110'].keys()),sorted(parnames))

        cens = [result['detector1'][peaknames[i]]['centre'].nominal_value for i in range(6)]
        self.assertAlmostEquals(cens[0],2.1391,4)
        self.assertAlmostEquals(cens[1],2.6195,4)
        self.assertAlmostEquals(cens[2],3.0249,4)
        self.assertAlmostEquals(cens[3],3.3815,4)     
        self.assertAlmostEquals(cens[4],3.7053,4)     
        self.assertAlmostEquals(cens[5],4.2773,4)