Beispiel #1
0
def gdx_to_list(gams_dir, filename, varname='all', verbose=False):
    """
    This function loads the gdx with the results of the simulation
    All results are stored in an unordered list

    :param gams_dir:    Gams working directory
    :param filename:    Path to the gdx file to be read
    :param varname:     In case online one variable is needed, specify it name (otherwise specify 'all')
    :returns:        Dictionary with all the collected values (within lists)
    """



    from gdxcc import gdxSymbolInfo, gdxCreateD, gdxOpenRead, GMS_SSSIZE, gdxDataReadDone, new_gdxHandle_tp, \
        gdxDataReadStr, gdxFindSymbol, gdxErrorStr, gdxDataReadStrStart, gdxGetLastError
    out = {}
    tgdx = tm.time()
    gdxHandle = new_gdxHandle_tp()
    gdxCreateD(gdxHandle, gams_dir, GMS_SSSIZE)

    # make sure the file path is properly formatted:
    filename = filename.replace('/', os.path.sep).replace(
        '\\\\', os.path.sep).replace('\\', os.path.sep)
    filename = str(filename)  # removing possible unicode formatting

    if not os.path.isfile(filename):
        logging.critical('Gdx file "' + filename + '" does not exist')
        sys.exit(1)

    gdxOpenRead(gdxHandle, filename)

    if varname == 'all':
        # go through all the symbols one by one and add their data to the dict
        symNr = 0
        SymbolInfo = gdxSymbolInfo(gdxHandle, 0)
        while SymbolInfo[0] > 0:
            ret, nrRecs = gdxDataReadStrStart(gdxHandle, symNr)
            assert ret, "Error in gdx data string" + gdxErrorStr(
                gdxHandle, gdxGetLastError(gdxHandle))[1]

            res = []
            for i in range(nrRecs):
                ret, elements, values, afdim = gdxDataReadStr(gdxHandle)
                res.append(elements + [values[0]])
            out[SymbolInfo[1]] = res
            symNr += 1
            SymbolInfo = gdxSymbolInfo(gdxHandle, symNr)
    else:
        # find the number of the required symbol:
        ret, symNr = gdxFindSymbol(gdxHandle, varname)
        assert ret, "Symbol not found"

        ret, nrRecs = gdxDataReadStrStart(gdxHandle, symNr)
        assert ret, "Error in gdx data string" + gdxErrorStr(
            gdxHandle, gdxGetLastError(gdxHandle))[1]

        res = []
        for i in range(nrRecs):
            ret, elements, values, afdim = gdxDataReadStr(gdxHandle)
            res.append(elements + [values[0]])
        out[varname] = res

    gdxDataReadDone(gdxHandle)
    if verbose:
        logging.info("Loading gdx file " + filename +
                     " took {}s".format(tm.time() - tgdx))
    return out
Beispiel #2
0
 def query(self, name, reshape=RESHAPE_DEFAULT, filt=None, idval=None, idxlower=True):
     '''
     Query attribute `idval` from symbol `name`, and return a data structure shaped according to `reshape`.
     '''
     gdx_handle = self.gdx_handle
     ret, symNr = gdxcc.gdxFindSymbol(gdx_handle, name)
     assert ret, "Symbol '%s' not found in GDX '%s'" % (name, self.internal_filename)
     sinfo = self.get_sid_info(symNr)
     dim = sinfo['dim']
     symType = sinfo['stype']
     ret, nrRecs = gdxcc.gdxDataReadStrStart(gdx_handle, symNr)
     assert ret, get_last_error('gdxDataReadStrStart', gdx_handle)
     if idval is None:
         if symType == gdxcc.GMS_DT_EQU:
             idval = gdxcc.GMS_VAL_MARGINAL
         else:
             idval = gdxcc.GMS_VAL_LEVEL
     ifilt = None
     vtable = [None]*(nrRecs)
     rcounter = 0
     rowtype = None
     if filt != None:
         if isinstance(filt,list):
             filt = '^({0})$'.format('|'.join([re.escape(x) for x in filt]))
         if isinstance(filt, str):
             filt_func = re.compile(filt, re.IGNORECASE).match
         else:
             filt_func = filt
     for i in range(nrRecs):
         vrow = [None]*(dim+1)
         ret, elements, values, afdim = gdxcc.gdxDataReadStr(gdx_handle)
         assert ret, get_last_error('gdxDataReadStr', gdx_handle)
         if (filt != None):
             match_filt = False
             for e in elements:
                 m = filt_func(e)
                 if m != None:
                     match_filt = True
                     break
             if not match_filt:
                 continue
         d = -1
         for d in range(dim):
             try:
                 vrow[d] = int(elements[d])
             except:
                 vrow[d] = elements[d].lower() if idxlower else elements[d]
         vrow[d+1] = values[idval]
         vtable[rcounter] = vrow
         rcounter += 1
     gdxcc.gdxDataReadDone(gdx_handle)
     cols = ['s%d' % x for x in range(dim)]+['val',]
     df = pd.DataFrame(vtable[:rcounter],columns=cols)
     logger.debug("%d / %d records read from <%s>" % (rcounter, nrRecs, self.internal_filename))
     if symType == gdxcc.GMS_DT_SET:
         reshape = RESHAPE_SERIES
     df = dfreshape(df, reshape)
     if symType == gdxcc.GMS_DT_SET:
         df = df.index
     self.data = df
     return df
Beispiel #3
0
    def query(self, name, reshape=RESHAPE_DEFAULT, gamsdir=None, filt=None, idval=None):
        if __gdxpy_mode__ == GDX_MODE_API:
            gdxHandle = self.gdxHandle
            ret, symNr = gdxcc.gdxFindSymbol(gdxHandle, name)
            assert ret, "Symbol '%s' not found in GDX '%s'" % (name,self.internal_filename)
            sinfo = self.get_sid_info(symNr)
            dim = sinfo['dim']
            # assert dim>0, "Symbol '%s' is a scalar, not supported" % (name)
            symType = sinfo['stype']
            ret, nrRecs = gdxcc.gdxDataReadStrStart(gdxHandle, symNr)
            assert ret, "Error in gdxDataReadStrStart: "+gdxcc.gdxErrorStr(gdxHandle,gdxGetLastError(gdxHandle))[1]
            if idval is None:
                if symType == gdxcc.GMS_DT_EQU:
                    idval = gdxcc.GMS_VAL_MARGINAL
                else:
                    idval = gdxcc.GMS_VAL_LEVEL
            ifilt = None
            vtable = []
            rcounter = 0
            rowtype = None
            if filt != None:
                if isinstance(filt,list):
                    filt = '^({0})$'.format('|'.join([re.escape(x) for x in filt]))
                filt_regex = re.compile(filt, re.IGNORECASE)
            for i in range(nrRecs):
                vrow = [None]*(dim+1)
                ret, elements, values, afdim = gdxcc.gdxDataReadStr(gdxHandle)
                assert ret, "Error in gdxDataReadStr: "+gdxcc.gdxErrorStr(gdxHandle,gdxGetLastError(gdxHandle))[1]
                if (filt != None):
                    match_filt = False
                    for e in elements:
                        m = filt_regex.match(e)
                        if m != None:
                            match_filt = True
                            break
                    if not match_filt:
                        continue
                d = -1
                for d in range(dim):
                    try:
                        vrow[d] = int(elements[d])
                    except:
                        vrow[d] = elements[d].lower()
                vrow[d+1] = values[idval]
                vtable.append(vrow)
                rcounter += 1
            gdxcc.gdxDataReadDone(gdxHandle)
            #    ifilt = 1
            cols = ['s%d' % x for x in range(dim)]+['val',]
            #print vtable[:5]
            #print cols
            df = pd.DataFrame(vtable,columns=cols)
            #print df
            #if ifilt != None:
            #    df = df.drop(df.columns[ifilt], axis=1)
            #print "%d / %d records read from <%s>" % (rcounter, nrRecs, self.internal_filename)

        elif __gdxpy_mode__ == GDX_MODE_SHELL:
            cmdline = r'gdxdump.exe {0} symb={1} Format=csv NoHeader'.format(self.internal_filename, name)
            p = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
            # TODO check for return code
            # p = subprocess.Popen(cmdline +' | tr "[:upper:]" "[:lower:]"', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
            strdata = p.communicate()[0] #.replace("'.","',").replace(', \r\n','\r\n').replace(" ",",")
            sepchar = ','
            strdata = strdata.replace('eps','1e-16')
            csvfile = StringIO.StringIO(strdata)
            #print strdata[:500]
            df = pd.read_csv(csvfile,sep=sepchar,quotechar='"',prefix='s',header=None,error_bad_lines=False).dropna()
        else:
            raise Exception('Function "get_symbols_list" not available outside GDX API/SHELL mode')

        #raise e,  None, sys.exc_info()[2]
        #print_traceback(e)
        #print df.columns.values
        #print df.columns.values[-1], list(df.columns.values[:-2]), df.columns.values[-2]
        #print df
        if symType == gdxcc.GMS_DT_SET:
            reshape = RESHAPE_SERIES
        df = dfreshape(df, reshape)
        if symType == gdxcc.GMS_DT_SET:
            df = df.index
        self.data = df
        return df
Beispiel #4
0
    def _read_symbol(self, symno: int):
        """Read a GAMS symbol as a Pandas Series object

        Args:
            symno: Symbol number

        Raises:
            RuntimeError
        """

        # Get symbol info
        ret, _sym, dim, symtype = gdxcc.gdxSymbolInfo(self._h, symno)
        if not ret:
            warn("Symbol not found!")
            return None

        # Get domain of symbol
        domain = [(d if d != '*' else None) for d in self._get_domain(symno)]
        if not any(domain):
            domain = None

        # Get explanatory text
        expl_text = self._get_expl_text(symno)

        # Get length of longest label
        _label_maxlen = gdxcc.gdxSymbIndxMaxLength(self._h, symno)[0]

        # Start reading symbol
        ret, recs = gdxcc.gdxDataReadStrStart(self._h, symno)
        if not ret:
            raise Exception(
                gdxcc.gdxErrorStr(self._h, gdxcc.gdxGetLastError(self._h))[1])

        # Initialize keys and values arrays
        keys = recs * [tuple()]
        values = recs * [float()]
        if gdxcc.gdxSetHasText(self._h, symno):
            assoc_texts = recs * [str()]
        else:
            assoc_texts = None

        # Read GDX data
        # Code inside the loop is as small as possible for performance,
        # so the if statements are outside.
        if symtype == GMS_DT_SET and assoc_texts:
            for i in range(recs):
                _ret, key_arr, value_arr, _afdim = gdxcc.gdxDataReadStr(
                    self._h)
                keys[i] = (key_arr[0] if dim == 1 else tuple(key_arr)
                           )  # Squeze out dimension of 1-dim keys
                assoc_texts[i] = self._get_set_assoc_text(
                    value_arr[GMS_VAL_LEVEL])
        elif symtype == GMS_DT_SET and not assoc_texts:
            for i in range(recs):
                _ret, key_arr, _value_arr, _afdim = gdxcc.gdxDataReadStr(
                    self._h)
                keys[i] = (key_arr[0] if dim == 1 else tuple(key_arr)
                           )  # Squeze out dimension of 1-dim keys
        elif symtype == GMS_DT_PAR:
            for i in range(recs):
                _ret, key_arr, value_arr, _afdim = gdxcc.gdxDataReadStr(
                    self._h)
                keys[i] = (key_arr[0] if dim == 1 else tuple(key_arr)
                           )  # Squeze out dimension of 1-dim keys
                val = value_arr[GMS_VAL_LEVEL]
                values[i] = SPECIAL_VALUES.get(val, val)

        # Done reading
        gdxcc.gdxDataReadDone(self._h)

        # Construct GAMS symbols and return
        if symtype == GMS_DT_SET:
            return GAMSSet(keys,
                           domain,
                           expl_text=expl_text,
                           assoc_texts=assoc_texts)
        elif symtype == GMS_DT_PAR:
            if dim == 0:
                return GAMSScalar(values[0], expl_text=expl_text)
            else:
                return GAMSParameter(dict(zip(keys, values)),
                                     domain=domain,
                                     expl_text=expl_text)