Changeset 2302


Ignore:
Timestamp:
Feb 9, 2010 10:25:42 PM (10 years ago)
Author:
wehart
Message:

Adding tests for loading sets and parameters from spreadsheets.

Location:
coopr.pyomo/trunk/coopr/pyomo
Files:
3 edited

Legend:

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

    r2292 r2302  
    467467    #
    468468    symb_map = cmd[3]
    469     #for key in cmd[3]:
    470         #symb_map[ cmd[3][key] ] = key
    471469    if len(symb_map) == 0:
    472470        raise IOError, "Must specify at least one set or parameter name that will be imported"
     
    495493        param_name = None
    496494    #
    497     data.initialize(options.filename, index=index, index_name=index_name, param_name=symb_map, set=set_name, param=param_name, format=options.format)
     495    data.initialize(options.filename, index=index, index_name=index_name, param_name=symb_map, set=set_name, param=param_name, format=options.format, range=options.range, query=options.query, using=options.using)
    498496    #
    499497    data.open()
  • coopr.pyomo/trunk/coopr/pyomo/data/sheet.py

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

    r2295 r2302  
    1515currdir=dirname(abspath(__file__))+os.sep
    1616example_dir=coopr_dir+os.sep+".."+os.sep+"examples"+os.sep+"pyomo"+os.sep+"tutorials"+os.sep+"tab"+os.sep
     17tutorial_dir=coopr_dir+os.sep+".."+os.sep+"examples"+os.sep+"pyomo"+os.sep+"tutorials"+os.sep
    1718
    1819try:
     
    494495
    495496
    496 #class TestSpreadsheet(unittest.TestCase):
    497 class TestSpreadsheet(object):
     497
     498class TestSpreadsheet(unittest.TestCase):
     499
     500    def run(self, result=None):
     501        """ Disable the tests if win32com is not available """
     502        if not _win32com:
     503           return
     504        unittest.TestCase.run(self,result)
    498505
    499506    def test_tableA1(self):
    500507        """Importing a single column of data"""
    501508        pyutilib.misc.setup_redirect(currdir+'importA1.dat')
    502         print "import "+os.path.abspath(example_dir+'A.tab')+" format=set: A;"
     509        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Atable format=set: A;"
    503510        pyutilib.misc.reset_redirect()
    504511        model=Model()
     
    511518        """Importing a single column of data"""
    512519        pyutilib.misc.setup_redirect(currdir+'importA2.dat')
    513         print "import "+os.path.abspath(example_dir+'A.tab')+" ;"
     520        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Atable ;"
    514521        pyutilib.misc.reset_redirect()
    515522        model=Model()
     
    525532        """Importing a single column of data"""
    526533        pyutilib.misc.setup_redirect(currdir+'importA3.dat')
    527         print "import "+os.path.abspath(example_dir+'A.tab')+" : A ;"
     534        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Atable : A ;"
    528535        pyutilib.misc.reset_redirect()
    529536        model=Model()
     
    539546        """Same as test_tableA"""
    540547        pyutilib.misc.setup_redirect(currdir+'importB.dat')
    541         print "import "+os.path.abspath(example_dir+'B.tab')+" format=set:B;"
     548        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Btable format=set:B;"
    542549        pyutilib.misc.reset_redirect()
    543550        model=Model()
     
    551558        treated as values for a set with tuple values."""
    552559        pyutilib.misc.setup_redirect(currdir+'importC.dat')
    553         print "import "+os.path.abspath(example_dir+'C.tab')+" format=set: C ;"
     560        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Ctable format=set: C ;"
    554561        pyutilib.misc.reset_redirect()
    555562        model=Model()
     
    562569        """Importing a 2D array of data as a set."""
    563570        pyutilib.misc.setup_redirect(currdir+'importD.dat')
    564         print "import "+os.path.abspath(example_dir+'D.tab')+" format=set_array: C ;"
     571        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Dtable format=set_array: C ;"
    565572        pyutilib.misc.reset_redirect()
    566573        model=Model()
     
    573580        """Importing a single parameter"""
    574581        pyutilib.misc.setup_redirect(currdir+'importZ.dat')
    575         print "import "+os.path.abspath(example_dir+'Z.tab')+" format=param: Z ;"
     582        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Zparam format=param: Z ;"
    576583        pyutilib.misc.reset_redirect()
    577584        model=Model()
     
    584591        """Same as tableXW."""
    585592        pyutilib.misc.setup_redirect(currdir+'importY.dat')
    586         print "import "+os.path.abspath(example_dir+'Y.tab')+" : [A] Y;"
     593        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Ytable : [A] Y;"
    587594        pyutilib.misc.reset_redirect()
    588595        model=Model()
     
    599606        index column."""
    600607        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
    601         print "import "+os.path.abspath(example_dir+'XW.tab')+": [A] X W;"
     608        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=XWtable : [A] X W;"
    602609        pyutilib.misc.reset_redirect()
    603610        model=Model()
     
    614621        """Like test_tableXW_1, except that set A is not defined."""
    615622        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
    616         print "import "+os.path.abspath(example_dir+'XW.tab')+": [A] X W;"
     623        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=XWtable : [A] X W;"
    617624        pyutilib.misc.reset_redirect()
    618625        model=Model()
     
    629636        """Like test_tableXW_1, except that set A is defined in the import statment."""
    630637        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
    631         print "import "+os.path.abspath(example_dir+'XW.tab')+": A=[A] X W;"
     638        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=XWtable: A=[A] X W;"
    632639        pyutilib.misc.reset_redirect()
    633640        model=Model()
     
    644651        """Like test_tableXW_1, except that set A is defined in the import statment and all values are mapped."""
    645652        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
    646         print "import "+os.path.abspath(example_dir+'XW.tab')+": B=[A] R=X S=W;"
     653        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=XWtable: B=[A] R=X S=W;"
    647654        pyutilib.misc.reset_redirect()
    648655        model=Model()
     
    659666        """Importing a 2D array of parameters that are transposed."""
    660667        pyutilib.misc.setup_redirect(currdir+'importT.dat')
    661         print "import "+os.path.abspath(example_dir+'T.tab')+" format=transposed_array : T;"
     668        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Ttable format=transposed_array : T;"
    662669        pyutilib.misc.reset_redirect()
    663670        model=Model()
     
    672679        """Importing a 2D array of parameters."""
    673680        pyutilib.misc.setup_redirect(currdir+'importU.dat')
    674         print "import "+os.path.abspath(example_dir+'U.tab')+" format=array : U;"
     681        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Utable format=array : U;"
    675682        pyutilib.misc.reset_redirect()
    676683        model=Model()
     
    687694        index column.  A missing value is represented in the column data."""
    688695        pyutilib.misc.setup_redirect(currdir+'importS.dat')
    689         print "import "+os.path.abspath(example_dir+'S.tab')+": [A] S ;"
     696        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Stable : [A] S ;"
    690697        pyutilib.misc.reset_redirect()
    691698        model=Model()
     
    700707        """Importing a table that has multiple indexing columns"""
    701708        pyutilib.misc.setup_redirect(currdir+'importPO.dat')
    702         print "import "+os.path.abspath(example_dir+'PO.tab')+" : J=[A,B] P O;"
     709        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=POtable : J=[A,B] P O;"
    703710        pyutilib.misc.reset_redirect()
    704711        model=Model()
Note: See TracChangeset for help on using the changeset viewer.