Changeset 2289


Ignore:
Timestamp:
Feb 8, 2010 5:13:22 PM (10 years ago)
Author:
wehart
Message:

Setting up 'import' tests for the other simple examples.
These seem to work, but the internal code is too ugly. I'm
going to clean that up next to help explain what is going on...

Location:
coopr.pyomo/trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/data/TableData.py

    r2285 r2289  
    6161
    6262    def _set_data(self, headers, rows):
     63        #print "_SET_DATA"
    6364        if self.options.symbol_map is None:
    6465            mapped_headers = map(str,list(headers))
    6566        else:
    6667            mapped_headers = [self.options.symbol_map[tuple(header)] if tuple(header) in self.options.symbol_map else header for header in map(str,list(headers))]
    67         #print "X",mapped_headers,self.options.symbol_map,self.options.index
     68        #print "X",mapped_headers,self.options.symbol_map,self.options.index,self.options.format
    6869
    6970        if not self.options.set is None:
     
    7273              for row in rows:
    7374                self._info = self._info + list(row)
    74            elif self.options.format is "set_array":
     75           elif self.options.format == "set_array":
    7576              self._info = ["set",self.options.set, ":"]
    7677              self._info = self._info + list(mapped_headers[1:])
     
    8283
    8384        elif self.options.index is not None and self.options.param is None:
     85           #print "Z1"
    8486           self._info = ["param",":",self.options.index,":"]
    85            self._info = self._info + mapped_headers[1:]
     87           self._info = self._info + mapped_headers[self.options.nindex:]
    8688           self._info.append(":=")
    8789           for row in rows:
     
    8991
    9092        elif self.options.index is not None and self.options.param is not None:
     93           #print "Z2"
    9194           self._info = ["param",":",self.options.index,":"]
    9295           if type(self.options.param) is str:
     
    101104
    102105        elif self.options.param is not None and self.options.format is not None:
    103            if self.options.format is "transposed_array":
     106           #print "Z3"
     107           if self.options.format == "transposed_array":
    104108              self._info = ["param",self.options.param,"(tr)",":"] + mapped_headers[1:]
    105            elif self.options.format is "array":
     109           elif self.options.format == "array":
    106110              self._info = ["param",self.options.param,":"] + mapped_headers[1:]
    107111           else:
     
    112116
    113117        else:
     118           #print "Z4"
    114119           if len(mapped_headers) == 1:
    115120              self._info = ["set",mapped_headers[0],":="]
  • coopr.pyomo/trunk/coopr/pyomo/data/parse_datacmds.py

    r2285 r2289  
    104104    return t
    105105
    106 t_NONWORD   = r"[^\.A-Za-z0-9,;:<>\(\)\#{}\[\] \n\t\r]+"
     106t_NONWORD   = r"[^\.A-Za-z0-9,;:=<>\(\)\#{}\[\] \n\t\r]+"
    107107
    108108# Error handling rule
     
    178178            p[0] = [tmp, p[3], p[4]]
    179179        else:
     180            tmp.update(p[2])
    180181            p[0] = [tmp, [], p[4]]
    181182    elif len(p) == 6:
     
    187188def p_import_options(p):
    188189    '''import_options : WORD EQ STRING import_options
     190                      | WORD EQ STRING
    189191                      | WORD EQ QUOTEDSTRING import_options
    190                       | WORD EQ STRING
    191192                      | WORD EQ QUOTEDSTRING
     193                      | WORD EQ WORD import_options
     194                      | WORD EQ WORD
     195                      | WORD EQ PARAM import_options
     196                      | WORD EQ PARAM
    192197    '''
    193198    tmp = {p[1]:p[3]}
  • coopr.pyomo/trunk/coopr/pyomo/data/process_data.py

    r2285 r2289  
    462462    data = DataManagerFactory(tmp)
    463463    set_name=None
     464    param_name=None
    464465    #
    465466    # Create symbol map
    466467    #
    467468    symb_map = {}
     469    index=None
     470    nindex=1
    468471    for key in cmd[3]:
    469472        symb_map[ tuple(cmd[3][key]) ] = key
     
    473476            set_name=cmd[3].keys()[0]
    474477    else:
     478        nindex=0
    475479        tmp = []
    476480        for item in cmd[2]:
     
    478482                if item == key[0]:
    479483                    tmp.append(symb_map[key])
    480         index=' '.join(tmp)
     484                    nindex += len(key)
     485        if len(tmp) > 0:
     486            index=' '.join(tmp)
     487        if nindex == 0:
     488            nindex = 1
    481489        if len(tmp) == 0 and len(cmd[3]) == 1:
    482490            set_name=cmd[3].keys()[0]
    483     data.initialize(options.filename, index=index, symbol_map=symb_map, set=set_name)
     491    if options.format == 'transposed_array' or options.format == 'array':
     492        if len(symb_map) > 1:
     493            raise IOError, "Cannot specify multiple parameter names for array data: %s" % str(symb_map)
     494        if len(symb_map) == 0:
     495            raise IOError, "Must specify a parameter name for a array data"
     496        param_name = symb_map[symb_map.keys()[0]]
     497        set_name = None
     498    data.initialize(options.filename, index=index, nindex=nindex, symbol_map=symb_map, set=set_name, format=options.format, param=param_name)
    484499    data.open()
    485500    data.read()
  • coopr.pyomo/trunk/coopr/pyomo/data/sheet.py

    r2274 r2289  
    3737            return
    3838        tmp = self.sheet.get_range(self.options.range, raw=True)
    39         if type(tmp) in (int,long,float):
    40             self._info = ["param",self.options.param,":=",tmp]
     39        if len(tmp) == 0:
     40            raise IOError, "Empty range '%s'" % self.options.range
     41        elif type(tmp) in (int,long,float):
     42            if not self.options.param is None:
     43                self._info = ["param",self.options.param,":=",tmp[0][0]]
     44            elif len(self.options.symbol_map) == 1:
     45                self._info = ["param",self.options.symbol_map[self.options.         symbol_map.keys()[0]],":=",tmp]
     46            else:
     47                raise IOError, "Data looks like a parameter, but multiple parameter names have been specified: %s" % str(self.options.symbol_map)
    4148        else:
    4249            self._set_data(tmp[0], tmp[1:])
  • coopr.pyomo/trunk/coopr/pyomo/data/text.py

    r2274 r2289  
    4040            raise IOError, "Empty *.tab file"
    4141        elif len(tmp) == 1:
    42             self._info = ["param",self.options.param,":=",tmp[0][0]]
     42            if not self.options.param is None:
     43                self._info = ["param",self.options.param,":=",tmp[0][0]]
     44            elif len(self.options.symbol_map) == 1:
     45                self._info = ["param",self.options.symbol_map[self.options.symbol_map.keys()[0]],":=",tmp[0][0]]
     46            else:
     47                raise IOError, "Data looks like a parameter, but multiple parameter names have been specified: %s" % str(self.options.symbol_map)
    4348        else:
    4449            self._set_data(tmp[0], tmp[1:])
  • coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_modeldata.py

    r2285 r2289  
    320320        os.remove(currdir+'importC.dat')
    321321
    322     def Xtest_tableD(self):
     322    def test_tableD(self):
    323323        """Importing a 2D array of data as a set."""
    324324        pyutilib.misc.setup_redirect(currdir+'importD.dat')
     
    328328        model.C = Set(dimen=2)
    329329        instance = model.create(currdir+'importD.dat')
    330         self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A1',2), ('A1',3), ('A2',1), ('A2',2), ('A2',3), ('A3',1), ('A3',2), ('A3',3)]))
    331         os.remove(currdir+'importC.dat')
    332 
    333     def test_tableS(self):
    334         """Importing a table, but only reporting the values for the non-index
    335         parameter columns.  The first column is assumed to represent an
    336         index column.  A missing value is represented in the column data."""
    337         pyutilib.misc.setup_redirect(currdir+'importS.dat')
    338         print "import "+os.path.abspath(example_dir+'S.tab')+" ;"
     330        self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A2',2), ('A3',3)]))
     331        os.remove(currdir+'importD.dat')
     332
     333    def test_tableZ(self):
     334        """Importing a single parameter"""
     335        pyutilib.misc.setup_redirect(currdir+'importZ.dat')
     336        print "import "+os.path.abspath(example_dir+'Z.tab')+" format=param: Z ;"
     337        pyutilib.misc.reset_redirect()
     338        model=Model()
     339        model.Z = Param(default=99.0)
     340        instance = model.create(currdir+'importZ.dat')
     341        self.failUnlessEqual(instance.Z.value, 1.1)
     342        os.remove(currdir+'importZ.dat')
     343
     344    def test_tableY(self):
     345        """Same as tableXW."""
     346        pyutilib.misc.setup_redirect(currdir+'importY.dat')
     347        print "import "+os.path.abspath(example_dir+'Y.tab')+" ;"
    339348        pyutilib.misc.reset_redirect()
    340349        model=Model()
    341350        model.A = Set(initialize=['A1','A2','A3','A4'])
    342         model.S = Param(model.A)
    343         instance = model.create(currdir+'importS.dat')
     351        model.Y = Param(model.A)
     352        instance = model.create(currdir+'importY.dat')
    344353        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
    345         self.failUnlessEqual(instance.S.data(), {'A1':3.3,'A3':3.5})
    346         os.remove(currdir+'importS.dat')
     354        self.failUnlessEqual(instance.Y.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     355        os.remove(currdir+'importY.dat')
    347356
    348357    def test_tableXW_1(self):
     
    408417        os.remove(currdir+'importXW.dat')
    409418
    410     def test_tableY(self):
    411         """Same as tableXW."""
    412         pyutilib.misc.setup_redirect(currdir+'importY.dat')
    413         print "import "+os.path.abspath(example_dir+'Y.tab')+" ;"
     419    def test_tableT(self):
     420        """Importing a 2D array of parameters that are transposed."""
     421        pyutilib.misc.setup_redirect(currdir+'importT.dat')
     422        print "import "+os.path.abspath(example_dir+'T.tab')+" format=transposed_array : T;"
     423        pyutilib.misc.reset_redirect()
     424        model=Model()
     425        model.A = Set()
     426        model.B = Set()
     427        model.T = Param(model.A, model.B)
     428        instance = model.create(currdir+'importT.dat')
     429        self.failUnlessEqual(instance.T.data(), {('A2', 'I1'): 2.3, ('A1', 'I2'): 1.4, ('A1', 'I3'): 1.5, ('A1', 'I4'): 1.6, ('A1', 'I1'): 1.3, ('A3', 'I4'): 3.6, ('A2', 'I4'): 2.6, ('A3', 'I1'): 3.3, ('A2', 'I3'): 2.5, ('A3', 'I2'): 3.4, ('A2', 'I2'): 2.4, ('A3', 'I3'): 3.5})
     430        os.remove(currdir+'importT.dat')
     431
     432    def test_tableU(self):
     433        """Importing a 2D array of parameters."""
     434        pyutilib.misc.setup_redirect(currdir+'importU.dat')
     435        print "import "+os.path.abspath(example_dir+'U.tab')+" format=array : U;"
     436        pyutilib.misc.reset_redirect()
     437        model=Model()
     438        model.A = Set()
     439        model.B = Set()
     440        model.U = Param(model.A, model.B)
     441        instance = model.create(currdir+'importU.dat')
     442        self.failUnlessEqual(instance.U.data(), {('I2', 'A1'): 1.4, ('I3', 'A1'): 1.5, ('I3', 'A2'): 2.5, ('I4', 'A1'): 1.6, ('I3', 'A3'): 3.5, ('I1', 'A2'): 2.3, ('I4', 'A3'): 3.6, ('I1', 'A3'): 3.3, ('I4', 'A2'): 2.6, ('I2', 'A3'): 3.4, ('I1', 'A1'): 1.3, ('I2', 'A2'): 2.4})
     443        os.remove(currdir+'importU.dat')
     444
     445    def test_tableS(self):
     446        """Importing a table, but only reporting the values for the non-index
     447        parameter columns.  The first column is assumed to represent an
     448        index column.  A missing value is represented in the column data."""
     449        pyutilib.misc.setup_redirect(currdir+'importS.dat')
     450        print "import "+os.path.abspath(example_dir+'S.tab')+" ;"
    414451        pyutilib.misc.reset_redirect()
    415452        model=Model()
    416453        model.A = Set(initialize=['A1','A2','A3','A4'])
    417         model.Y = Param(model.A)
    418         instance = model.create(currdir+'importY.dat')
     454        model.S = Param(model.A)
     455        instance = model.create(currdir+'importS.dat')
    419456        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
    420         self.failUnlessEqual(instance.Y.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
    421         os.remove(currdir+'importY.dat')
     457        self.failUnlessEqual(instance.S.data(), {'A1':3.3,'A3':3.5})
     458        os.remove(currdir+'importS.dat')
     459
     460    def test_tablePO(self):
     461        """Importing a table that has multiple indexing columns"""
     462        pyutilib.misc.setup_redirect(currdir+'importPO.dat')
     463        print "import "+os.path.abspath(example_dir+'PO.tab')+" : [A,B] J:A,B P O;"
     464        pyutilib.misc.reset_redirect()
     465        model=Model()
     466        model.J = Set(dimen=2)
     467        model.P = Param(model.J)
     468        model.O = Param(model.J)
     469        instance = model.create(currdir+'importPO.dat')
     470        self.failUnlessEqual(instance.J.data(), set([('A3', 'B3'), ('A1', 'B1'), ('A2', 'B2')]) )
     471        self.failUnlessEqual(instance.P.data(), {('A3', 'B3'): 4.5, ('A1', 'B1'): 4.3, ('A2', 'B2'): 4.4} )
     472        self.failUnlessEqual(instance.O.data(), {('A3', 'B3'): 5.5, ('A1', 'B1'): 5.3, ('A2', 'B2'): 5.4})
     473        os.remove(currdir+'importPO.dat')
    422474
    423475
  • coopr.pyomo/trunk/examples/pyomo/tutorials/table.py

    r2227 r2289  
    121121data.add("tab/U.tab", param="U", format="array")
    122122data.add("tab/S.tab")
    123 data.add("tab/RQ.tab", index="H")
     123data.add("tab/RQ.tab", index="H", param=("R","Q"))
    124124data.add("tab/PO.tab", index="J", param=("P","O"))
    125125data.add("tab/PP.tab", param="PP")
Note: See TracChangeset for help on using the changeset viewer.