Esempio n. 1
0
 def __init__(self,coef,timeInterval,typeFunction='linear'):
     if typeFunction not in ['linear','exponential','logarithmic','polynomial','power']: raise Exception("check the time function")
     verifyType(coef, list)
     verifyType(timeInterval,tuple)
     self.coef=coef
     self.timeInterval=timeInterval
     self.typeFunction=typeFunction
Esempio n. 2
0
def makeTableFromLinearFunction(function,coords,time_list,name=None,columnUnits=None):
    """Constructs a table consisting of two columns.
    The first column contains a time list and the second column contains function values for space coordinates coords and times defined in the time list."""
    verifyType(time_list,ListType)
    from functions import LinearFunction
    memberShip(function,LinearFunction)
    nb_coeff = function.getNbCoefficients()
    coeffs = function.getCoefficients()
    if name:
        verifyType(name,StringType)
        tab=Table(name)
        pass
    else:
        tab = Table('Table')
        pass
    if columnUnits:
        listTypeCheck(columnUnits, StringType)
        if len(columnUnits) !=2:
            raise Exception("makeTableFromLinearFunction creates a two columns table : time and value. You have to give two units")
        tab.setColumnUnits(columnUnits)
        pass
    value_list = []
    for t in time_list:
        coo = coords + [t]
        value = function.eval(coo)
        value_list.append(value)
        pass
    tab.addColumn('time',time_list)
    tab.addColumn('value',value_list)
    return tab
Esempio n. 3
0
 def __init__(self, coef, timeInterval, typeFunction='linear'):
     if typeFunction not in [
             'linear', 'exponential', 'logarithmic', 'polynomial', 'power'
     ]:
         raise Exception("check the time function")
     verifyType(coef, list)
     verifyType(timeInterval, tuple)
     self.coef = coef
     self.timeInterval = timeInterval
     self.typeFunction = typeFunction
Esempio n. 4
0
def makeTableFromFile(ffile,name=None,nameInFile='No',\
                      columnsNames=None,columnsNamesInFile='Yes',
                      columnsUnits=None,columnsUnitsInFile='No'):
    """Makes a table from the given file. Reads table's names (if they are present) from the file.
    Line of Name in file should begin with #TITLE:
    Line of ColumnsName in file should begin with #COLUMN_TITLES: and colum names should be separate by |"""
    from exceptions import IOError
    t = None
    try:
        file = open(ffile, 'r')
    except:
        msg = "can't open file <%s>...\n" % ffile
        raise IOError(msg)
    if name and nameInFile.lower() == 'yes':
        raise Warning("You give a table name and ask to get name from file")
    if columnsNames and columnsNameInFile.lower() == 'yes':
        raise Warning(
            "You ask for colum names in the file while already defining them")
    if columnsUnits and columnsUnitsInFile.lower() == 'yes':
        raise Warning(
            "You ask for units in the file while already defining them")
    # table creation
    if name:
        verifyType(name, StringType)
        t = DataTable(name)
        pass
    else:
        t = DataTable('table')
        pass
    # affect columns names if necessary
    if columnsNames:
        t.setColumnNames(columnsNames)
        pass
    # affect columns units if necessary
    if columnsUnits:
        t.setColumnUnits(columnsUnits)
        pass
    t.readFromFile(ffile,
                   columnsNames=columnsNamesInFile,
                   name=nameInFile,
                   colonnesnames=columnsNames,
                   columnsUnits=columnsUnitsInFile)

    return t
Esempio n. 5
0
 def __init__(self,coefList):
     """constructor"""
     verifyType(coefList, list)
     length = len(coefList)
     if length !=3:
         raise Exception("wrong dimension:\n"+\
         "the coefList for a polynomial function should be:"+\
         "[(an,an-1,...,a1,a0),(bn,bn-1,...,b1,b0),(cn,cn-1,...,c1,c0)] ")
     coefList_x = coefList[0]
     coefList_y = coefList[1]
     coefList_z = coefList[2]
     len_coefList = len(coefList_x)
     if ((len_coefList != len(coefList_y)) or
         (len_coefList != len(coefList_z))):
         raise Exception("check the coefList length for the PolynomialFunction")
     self.coefList_x = coefList_x
     self.coefList_y = coefList_y
     self.coefList_z = coefList_z
     self.coefList = coefList_x + coefList_y + coefList_z
     return
Esempio n. 6
0
 def __init__(self, coefList):
     """constructor"""
     verifyType(coefList, list)
     length = len(coefList)
     if length != 3:
         raise Exception("wrong dimension:\n"+\
         "the coefList for a polynomial function should be:"+\
         "[(an,an-1,...,a1,a0),(bn,bn-1,...,b1,b0),(cn,cn-1,...,c1,c0)] ")
     coefList_x = coefList[0]
     coefList_y = coefList[1]
     coefList_z = coefList[2]
     len_coefList = len(coefList_x)
     if ((len_coefList != len(coefList_y))
             or (len_coefList != len(coefList_z))):
         raise Exception(
             "check the coefList length for the PolynomialFunction")
     self.coefList_x = coefList_x
     self.coefList_y = coefList_y
     self.coefList_z = coefList_z
     self.coefList = coefList_x + coefList_y + coefList_z
     return
Esempio n. 7
0
def makeTableFromFile(ffile,name=None,nameInFile='No',\
                      columnsNames=None,columnsNamesInFile='Yes',
                      columnsUnits=None,columnsUnitsInFile='No'):
    """Makes a table from the given file. Reads table's names (if they are present) from the file.
    Line of Name in file should begin with #TITLE:
    Line of ColumnsName in file should begin with #COLUMN_TITLES: and colum names should be separate by |"""
    from exceptions import IOError
    t = None
    try:
        file = open(ffile, 'r')
    except :
        msg="can't open file <%s>...\n"%ffile
        raise IOError(msg)
    if name and nameInFile.lower()=='yes':
        raise Warning("You give a table name and ask to get name from file") 
    if columnsNames and columnsNameInFile.lower()=='yes':
        raise Warning("You ask for colum names in the file while already defining them")
    if columnsUnits and columnsUnitsInFile.lower()=='yes':
        raise Warning("You ask for units in the file while already defining them")
    # table creation
    if name:
        verifyType(name,StringType)
        t=Table(name)
        pass
    else:
        t=Table('table')
        pass
    # affect columns names if necessary
    if columnsNames:
        t.setColumnNames(columnsNames)
        pass
    # affect columns units if necessary
    if columnsUnits:
        t.setColumnUnits(columnsUnits)
        pass
    t.readFromFile( ffile,
                    columnsNames=columnsNamesInFile,name=nameInFile,
                    colonnesnames=columnsNames,columnsUnits=columnsUnitsInFile)

    return t
Esempio n. 8
0
def makeTableFromLinearFunction(function,coords,time_list,name=None,columnUnits=None):
    """Constructs a table consisting of two columns.
    The first column contains a time list and the second column contains function values for space coordinates coords and times defined in the time list."""
    verifyType(time_list,ListType)
    from functions import LinearFunction
    memberShip(function,LinearFunction)
    nb_coeff = function.getNbCoefficients()
    coeffs = function.getCoefficients()
    if name:
        verifyType(name,StringType)
        tab=Table(name)
        pass
    else:
        tab = Table('Table')
        pass
    if columnUnits:
        listTypeCheck(columnUnits, StringType)
        if len(columnUnits) !=2:
            raise Exception("makeTableFromLinearFunction creates a two columns table : time and value. You have to give two units")
        tab.setColumnUnits(columnUnits)
        pass
    value_list = []
    for t in time_list:
        coo = coords + [t]
        value = function.eval(coo)
        value_list.append(value)
        pass
    tab.addColumn('time',time_list)
    tab.addColumn('value',value_list)
    return tab







        
            
Esempio n. 9
0
    def __init__(self, coefList = None, timeCoef = None):
        """
        - coefList : list of real or integer
        - the timecoeff is a table
        """
        if coefList:
#            print coefList,type(coefList)
            verifyType(coefList, [list])
            for coef in coefList:
#                print " c ",c
                verifyType(coef, [float,int])
                pass
            self.coefList = coefList
            pass
        else:
            self.coefList =[]
            pass
            
        if timeCoef:
            memberShip(timeCoef, Table)
            pass
        self.timecoeff = timeCoef
        return
Esempio n. 10
0
    def __init__(self, coefList=None, timeCoef=None):
        """
        - coefList : list of real or integer
        - the timecoeff is a table
        """
        if coefList:
            #            print coefList,type(coefList)
            verifyType(coefList, [list])
            for coef in coefList:
                #                print " c ",c
                verifyType(coef, [float, int])
                pass
            self.coefList = coefList
            pass
        else:
            self.coefList = []
            pass

        if timeCoef:
            memberShip(timeCoef, Table)
            pass
        self.timecoeff = timeCoef
        return
Esempio n. 11
0
    def deriveFromIntegral(self,inverse='rectangle',t0=0.,newName='',newColumnNames=[]):
        """ Calculates the derivate table of the current table on the first
            colomn and return a new table:
            table        = t[i]   f[i]
            table_derive = t[i]   N[i]
                with :
                   if inverse=='rectangle' :
                       N[i] = (f[i]-f[i-1]) / (t[i]-t[i-1])
        """
        from exceptions import Exception
        if not inverse.lower()=='rectangle':
            msg="cannot yet derive without supposing\n"
            msg+='that the integration was first made\n'
            msg+='using rectangle method.'
            raise Exception(msg)
        verifyType(newName,StringType)
        if not len(newName):
            newName=self.getName()
            pass
        verifyType(newColumnNames,ListType)
        len_ini=len(newColumnNames)
        for i in range(len(self.getColumnNames())):
            if i<len_ini:
                titre=newColumnNames[i]
                verifyType(titre,StringType)
                if not len(titre):
                    newColumnNames[i]=self.getColumnNames()[i]
                    pass
                pass
            else:
                newColumnNames.append(self.getColumnNames()[i])
                pass
            pass
        #  real beginning :
        new=Table(newName)
        nlig=self.getNbRows()
        ncol=self.getNbColumns()

        time = self.getColumn(0)
        time=[t0]+time
        tt=t0
        for ii in range(len(time[1:])):
            tsup=time[1+ii]
            if tt>=tsup:
                msg='Time does not increase in table %s'%self.getName()
                msg+=' ;\n cannot derive !'
                msg+='\n\n info : t[%s] = %e\n        t[%s] = %e\n'\
                      %(ii,time[ii],1+ii,time[1+ii])
                raise Exception(msg)
            tt=tsup
            pass
        new.addColumn(newColumnNames[0],time)
        # for each other column, cumulate values                             
        for i in range(1,self.getNbColumns()):
##             print 'LONGUEUR self.getColumn(i):',len(self.getColumn(i))
            c = self.getColumn(i)
##             print 'LONGUEUR c : len(c)',len(c)
##             print 'type(c)',type(c)
##             c = [0.]+c    NE MARCHE PAS CAR type(c) = array
##             N = []
##             print 'LONGUEUR c : len(c)',len(c)
            N=[c[0]/(time[1]-time[0])]
            for j in range(1,len(c)):
                n =  (c[j]-c[j-1])/(time[j]-time[j-1])
                N.append(n)
                pass
##             print 'LONGUEUR N ,self.getColumn(i):',len(N),len(self.getColumn(i))
            new.addColumn(newColumnNames[i],N)
            pass

        return new
Esempio n. 12
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
Esempio n. 13
0
 def getCoefficient(self, i):
     """
     get ith coefficient
     """
     verifyType(i, int)
     return self.coefList[i]
Esempio n. 14
0
 def getCoefficient(self, i):
     """
     get ith coefficient
     """
     verifyType(i, int)
     return self.coefList[i]