Example #1
0
    def _simplify_feature_value(self, name, value):
        """Return simplified and more pythonic feature values."""
        if name == "prefix":
            channel_modes, channel_chars = value.split(")")
            channel_modes = channel_modes[1:]

            # [::-1] to reverse order and go from lowest to highest privs
            value = OrderedDict(list(zip(channel_modes, channel_chars))[::-1])

            return value

        elif name == "chanmodes":
            value = value.split(",")
            return value

        elif name == "targmax":
            max_available = {}
            for sort in value.split(","):
                command, limit = sort.split(":")
                command = command.casefold()
                max_available[command] = limit_to_number(limit)

            return max_available

        elif name == "chanlimit":
            limit_available = {}
            for sort in value.split(","):
                chan_types, limit = sort.split(":")
                for prefix in chan_types:
                    limit_available[prefix] = limit_to_number(limit)

            return limit_available

        elif name in _limits:
            value = limit_to_number(value)
            return value

        else:
            return value
Example #2
0
def read(filename):
    """Create a |Geoset| instance from a DS9 region file.

    .. note:: This will break if the region file was not written using
       `write`!

    Parameters
    ----------
    filename : str
        Path to the DS9 region file to be loaded.

    Returns
    -------
    out : |Geoset|
        A |Geoset| instance build using the polygons stored in the DS9 region
        file.

    """

    def parse_ds9_attrs(attrstr, global_attrs=False):
        attrstr = attrstr + " junk"
        if not global_attrs:
            attrstr = "junk " + attrstr

        j = [0] + [i for i in range(len(attrstr)) if attrstr[i] == "="] + [-1]

        keys, vals = [], []
        for j1, j2 in zip(j[:-1], j[1:]):
            val, key = attrstr[j1 + 1 : j2].rsplit(" ", 1)
            if (val[0], val[-1]) == ('"', '"') or (val[0], val[-1]) == ("{", "}"):
                val = val[1:-1]
            keys.append(key)
            vals.append(val)
        return zip(keys[:-1], vals[1:])

    with open(filename, "r") as file:
        lines = file.readlines()

    geoset = _geoset.Geoset(None)
    i0, j0, k0 = None, None, None
    for line in lines:
        line = line.split("\n")[0]
        # Skip comments and blank lines:
        if line.startswith("#") or not line:
            continue

        # Parse global attributes:
        elif line.startswith("global"):
            attrs = parse_ds9_attrs(line, global_attrs=True)
            attrs = OrderedDict(attrs)
            geoset.attrs = attrs

        # Parse coordinate system:
        elif line in ["physical", "fk5"]:
            if geoset.attrs is None:
                geoset.attrs = OrderedDict()
            geoset.attrs["coordsys"] = line

        # Build geoset:
        elif line.startswith("polygon"):
            coords, delim, attrs = line.partition("(")[-1].partition(")")
            coords, attrs = coords.replace(",", " "), attrs.split("#")[-1]

            # Make polygon
            xy = [float(i) for i in coords.split()]
            x, y = xy[0::2], xy[1::2]
            poly = geometry.Polygon(zip(x, y))

            # Test if polygon is a hole:
            is_hole = False
            if "background" in attrs:
                is_hole = True
                attrs = attrs.split("background")[-1]

            # Get item/geo/poly structure indices:
            idx_list = parse_ds9_attrs(attrs)
            for idx in idx_list:
                tag, n = idx[1].split()
                n = int(n)
                if tag == "item":
                    i = n
                elif tag == "geo":
                    j = n
                elif tag == "poly":
                    k = n

            if i != i0:
                geo = _geoset.Geo(poly)
                item = _geoset.Item(geo)
                geoset.items.append(item)
            elif j != j0:
                geo = _geoset.Geo(poly)
                item.geos.append(geo)
            elif k != k0:
                geo.geo = geo.geo.union(poly)
            elif is_hole:
                geo.geo = geo.geo.difference(poly)
            i0, j0, k0 = i, j, k

    return geoset
Example #3
0
def gdx_to_df(gdx_file, symbol, type="L", domain_info=None):
    def __get_set(gdx_file, set_name):
        if sys.platform in ["linux2", "darwin"]:
            proc = subprocess.Popen(
                ["gdxdump %s Symb=%s Format=csv NoHeader" % (gdx_file, set_name), ""],
                stdout=subprocess.PIPE,
                shell=True,
                stderr=subprocess.STDOUT,
            )
        elif sys.platform in ["win32"]:
            proc = subprocess.Popen(
                ["gdxdump", "%s" % gdx_file, "Symb=%s" % set_name, "Format=csv", "NoHeader", ""],
                stdout=subprocess.PIPE,
                shell=True,
                stderr=subprocess.STDOUT,
            )
        else:
            raise GamsAddOnException("ERROR {platform} not handled".format(platform=sys.platform))
        (out, err) = proc.communicate()
        try:
            csv_in = csv.reader(out.split("\n"), delimiter=",")
            return [int(row[0]) for row in csv_in if row]
        except ValueError:
            csv_in = csv.reader(out.split("\n"), delimiter=",")
            return [row[0] for row in csv_in if row]

    def __int(v):
        try:
            return int(v)
        except ValueError:
            return v

    def __float(v):
        try:
            return float(v)
        except ValueError:
            return v

    if domain_info is None:
        domain_info = DomainInfo(gdx_file)
    if symbol not in domain_info.symbols:
        raise GamsAddOnException('"%s" not in Domain of "%s"' % (symbol, gdx_file))

    sets = domain_info.get_sets(symbol)
    index = OrderedDict()
    if sets is None:
        index["Idx"] = [1]
        sets = ["Idx"]
    else:
        for s in sets:
            if s in domain_info.symbols:
                set_values = __get_set(gdx_file, s)
                set_name = s
                while set_name in index.keys():
                    set_name = set_name + s
                index[set_name] = set_values
            elif s == "*":
                index[s] = ["---PLACEHOLDER---"]
            else:
                raise GamsAddOnException('Set "%s" of "%s" not in Domain of "%s"' % (s, symbol, gdx_file))

    # print index.values()+[['l', 'm']]

    if domain_info.symbols[symbol][0] in ["Var", "Equ"]:
        multi_index = MultiIndex.from_product([index[s] for s in sets] + [["L", "M", "LO", "UP", "SCALE"]])
        df = DataFrame(0.0, index=multi_index, columns=[symbol])
        df.index.names = index.keys() + ["Type"]
    else:
        multi_index = MultiIndex.from_product([index[s] for s in index.keys()])
        df = DataFrame(0.0, index=multi_index, columns=[symbol])
        df.index.names = index.keys()
    if sys.platform in ["linux2", "darwin"]:
        proc = subprocess.Popen(
            ["gdxdump %s Symb=%s FilterDef=N" % (gdx_file, symbol), ""],
            stdout=subprocess.PIPE,
            shell=True,
            stderr=subprocess.STDOUT,
        )
    elif sys.platform in ["win32"]:
        proc = subprocess.Popen(
            ["gdxdump", "%s" % gdx_file, "Symb=%s" % symbol, "FilterDef=N", ""],
            stdout=subprocess.PIPE,
            shell=True,
            stderr=subprocess.STDOUT,
        )
    else:
        raise GamsAddOnException("ERROR {platform} not handled".format(platform=sys.platform))
    (out, err) = proc.communicate()
    out = out.replace("\n", "")
    content = re.search(r"/.*/", out).group(0)[1:-1].replace("'", "").strip().split(",")

    if err:
        raise GamsAddOnException("ERROR: {err}".format(err=err))
    elif content is []:
        raise GamsAddOnException("ERROR: No content found for {symbol}".format(symbol=symbol))
    else:
        indices = []
        values = []
        if domain_info.symbols[symbol][0] in ["Set"]:
            df[symbol] = False
            for data in content:
                if "." in data:
                    indices.append(tuple([__int(d) for d in data.strip().split(".")]))
                else:
                    indices.append(__int(data.strip()))
                values.append(True)
        else:
            for data in content:
                data = data.strip().split(" ")
                if len(data) == 1:
                    indices.append(1)
                    values.append(__float(data[0]))
                else:
                    index = data[0]
                    if "." in index:
                        index = tuple([__int(i) for i in index.split(".")])
                        indices.append(index)
                    elif index in ["L", "M", "UP", "LO", "SCALE"]:
                        index = (1, index)
                        indices.append(index)
                    else:
                        indices.append(__int(index))
                    values.append(__float(data[1]))
        try:
            # print 'NAME:', symbol, indices, values, len(values), df
            if len(values) == 1 and values[0] == "":
                return df
            else:
                # df.loc[indices, symbol] = values
                # print df.index
                df.loc[indices, symbol] = values
                # for i,idx in enumerate(indices):
                #     df.loc[idx, symbol] = values[i]
        except KeyError as ke:
            print "Warning", ke
            if "*" in df.index.names:
                for i, idx in enumerate(indices):
                    df.loc[idx, symbol] = True
                df.drop("---PLACEHOLDER---", inplace=True)

        if type is not None and "Type" in df.index.names:
            # print type
            # print df.head()
            return df.query('Type == "%s"' % type)
        else:
            return df