Example #1
0
    def isEqual(self, other, epsilon=None):
        """
        Enables to compare the table to an other one.
        Compares two tables. Returns 1 if they are completely identical, otherwise - 0.
        epsilon is the equality evaluator
        """
        memberShip(other, DataTable)
        Ok = 0

        nbc1 = len(self.getColumnNames())
        nbc2 = len(other.getColumnNames())

        if nbc1 != nbc2: return 0
        nbu1 = len(self.getColumnUnits())
        nbu2 = len(other.getColumnUnits())
        if nbu1 != nbu2: return 0
        for i in range(nbc1):
            if self.getColumnNames()[i].lower().strip()!=\
               other.getColumnNames()[i].lower().strip():
                return 0
            pass
        for i in range(nbu1):
            if self.getColumnUnits()[i].lower().strip()!=\
               other.getColumnUnits()[i].lower().strip():
                return 0
            pass

        nbc1 = self.getNbColumns()
        nbc2 = other.getNbColumns()
        if nbc1 != nbc2: return 0
        nbl1 = self.getNbColumns()
        nbl2 = other.getNbColumns()
        if nbl1 != nbl2: return 0
        for i in range(nbl1):
            for j in range(nbc1):
                v1 = self.getItem(j, i)
                v2 = other.getItem(j, i)
                if not epsilon:
                    if v1 != v2:
                        return 0
                    pass
                elif not areClose(float(v1), float(v2), epsilon, 'rel'):
                    return 0
                pass
            pass
        return 1
Example #2
0
 def isEqual(self,other,epsilon=None):
     """
     Enables to compare the table to an other one.
     Compares two tables. Returns 1 if they are completely identical, otherwise - 0.
     epsilon is the equality evaluator
     """
     memberShip(other,DataTable)
     Ok=0
     
     nbc1=len(self.getColumnNames())
     nbc2=len(other.getColumnNames())
     
     if nbc1!=nbc2: return 0
     nbu1 = len(self.getColumnUnits())
     nbu2 = len(other.getColumnUnits())
     if nbu1!=nbu2: return 0
     for i in range(nbc1):
         if self.getColumnNames()[i].lower().strip()!=\
            other.getColumnNames()[i].lower().strip():
             return 0
         pass
     for i in range(nbu1):
         if self.getColumnUnits()[i].lower().strip()!=\
            other.getColumnUnits()[i].lower().strip():
             return 0
         pass
     
     nbc1=self.getNbColumns()
     nbc2=other.getNbColumns()
     if nbc1!=nbc2: return 0
     nbl1=self.getNbColumns()
     nbl2=other.getNbColumns()
     if nbl1!=nbl2: return 0
     for i in range(nbl1):
         for j in range(nbc1):
             v1=self.getItem(j,i)
             v2=other.getItem(j,i)
             if not epsilon:
                 if v1!=v2:
                     return 0
                 pass
             elif not areClose(float(v1),float(v2),epsilon,'rel'):
                 return 0
             pass
         pass
     return 1
Example #3
0
 def extractSubTable(self,**dico):
     """ Creates a subtable in different ways ordered by dico keys.
     1/ if somebody wants a table with several columns, the possibilities are:
          column = a number
          columns = a number (for only one column)
                  or
                  a list of numbers (where can be only
                                     one number)
          fromcolumn = a number
          tocolumn = a number
          columnname = 'a good column name'
          columnnames = a list of column names
     2/ if somebody wants a table with several rows, the possibilities are:
          row   = a number
          rows  = a number (for only one column)
                  or
                  a list of numbers (where can be only
                                     one number)
          fromrow = a number
          torow = a number
          withvaluesincolumn = a tuple (list of values, number of column
                                        or its name, a precision if wanted)
     """
     #
     if not len(dico):
         return self.copy()
     elif len(dico)>1:
         msg='only one argument allowed.'
         raise msg
     #
     cle=list(dico.keys())[0]
     val=dico[cle]
     cle=cle.lower()
     if cle=='withvaluesincolumn':
         model,numeroColonne=val[0],val[1]
         listTypeCheck(model,[IntType,FloatType])
         model=[float(x) for x in model]
         verifyType(numeroColonne,[StringType,\
                    IntType,FloatType])
         tttitres=self.getColumnNames()
         ttunits = self.getColumnUnits()
         if type(numeroColonne) is StringType:
             for tit in tttitres:
                 if tit==numeroColonne: numCol=tttitres.index(tit)
                 pass
         else:
             if numeroColonne>=len(tttitres):
                 msg='The table does not have\n'
                 msg+='that number of columns : %s'%numeroColonne
                 raise msg
             numCol=numeroColonne
             pass
             
         if len(val)==3:
             eps=val[2]
             pass
         else:
             eps=1.e-15
             pass
         new=Table(self.getName(),tttitres,ttunits)
         nlig=self.getNbRows()
         ip=0
         comp=[float(x) for x in self.getColumn(numCol)]
         for ip in range(len(model)):
             for i in range(len(comp)):
                 value=comp[i]
                 if ip==1 and i==len(comp)-1:
                     pass
                 if areClose(value,model[ip],eps,'rel'):
                     new.addRow(self.getRow(i))
                     pass
                 if ip==len(model):
                     break
                 pass
             pass
         pass           
     else:
         valeurs=toList(val)
         tttitres=self.getColumnNames()
         ttunits = self.getColumnUnits()
         for st in ['row','column']:
             if st=='row':nn=self.getNbRows()
             if st=='column':nn=self.getNbColumns()
             if cle.find(st)>=0:
                 cleOk=st
                 pass
             if cle==st:
                 if len(valeurs) != 1:
                     raise Exception(" list length problem within the extractSubTable function")
                 pass
             if cle=='from'+st:
                 valeurs=valeurs[valeurs[0]:nn-1]
                 pass
             if cle=='to'+st:
                 valeurs=valeurs[0:valeurs[0]]
                 pass
             if cle.find('name')>=0:
                 newv=[]
                 for v in valeurs:
                     for tit in tttitres:
                         if tit==v:
                             newv.append(tttitres.index(tit))
                             break
                         pass
                     pass
                 valeurs=newv
                 pass
             pass
         if cleOk=='row':
             newtitres=tttitres
             newunits = ttunits
             pass
         if cleOk=='column':
             newtitres=[]
             newunits = []
             for i in valeurs:
                 newtitres.append(tttitres[i])
                 if len(ttunits): newunits.append(ttunits[i])
                 pass
             pass
         new=Table(self.getName(),newtitres,newunits)
         for i in valeurs:
             if cleOk=='row':
                 liste=self.getRow(i)
                 new.addRow(liste)
                 pass
             if cleOk=='column':
                 liste=self.getColumn(i)
                 new.addColumnValues(liste)
                 pass
             pass
         pass
     return new