示例#1
0
def _new_cells_from_series(self, series, name, param, source):

    if is_valid_name(name):
        pass
    else:
        if is_valid_name(series.name):
            name = series.name

    cells = self.new_cells(name=name,
                           formula=get_param_func(
                               _get_param_names(series, param)),
                           source=source)

    for i, v in series.items():
        cells.set_value(tuplize_key(cells, i),
                        v.item() if isinstance(v, np.generic) else v)

    return cells
示例#2
0
    def new_space_from_excel(
        self,
        book,
        range_,
        sheet=None,
        name=None,
        names_row=None,
        param_cols=None,
        space_param_order=None,
        cells_param_order=None,
        transpose=False,
        names_col=None,
        param_rows=None,
        call_id=None
    ):

        import modelx.io.excel as xl

        if space_param_order is None:
            # if [] then dynamic space without params
            param_order = cells_param_order
        else:
            param_order = space_param_order + cells_param_order

        cellstable = xl.CellsTable(
            book,
            range_,
            sheet,
            names_row,
            param_cols,
            param_order,
            transpose,
            names_col,
            param_rows,
        )

        if space_param_order is None:
            cells_params = cellstable.param_names
            param_func = None
        else:
            space_params = cellstable.param_names[:len(space_param_order)]
            cells_params = cellstable.param_names[len(space_param_order):]
            param_func = get_param_func(space_params)

        source = {
            "method": "new_space_from_excel",
            "args": [str(pathlib.Path(book).absolute()), range_],
            "kwargs": {
                "sheet": sheet,
                "name": name,
                "names_row": names_row,
                "param_cols": param_cols,
                "space_param_order": space_param_order,
                "cells_param_order": cells_param_order,
                "transpose": transpose,
                "names_col": names_col,
                "param_rows": param_rows,
                "call_id": call_id or str(uuid.uuid4()),
            }
        }
        space = self.new_space(name=name, formula=param_func, source=source)

        for cellsdata in cellstable.items():
            space.new_cells(name=cellsdata.name,
                            formula=get_param_func(cells_params))

        # Split for-loop to avoid clearing the preceding cells
        # each time a new cells is created in the base space.

        if space_param_order is None:
            for cellsdata in cellstable.items():
                for args, value in cellsdata.items():
                    cells = space.cells[cellsdata.name]
                    cells.set_value(args, value)
        else:
            for cellsdata in cellstable.items():
                for args, value in cellsdata.items():
                    space_args = args[:len(space_params)]
                    cells_args = args[len(space_params):]
                    subspace = space.get_dynspace(space_args)
                    cells = subspace.cells[cellsdata.name]
                    cells.set_value(cells_args, value)

        return space
示例#3
0
def new_space_from_pandas(self, obj, space, cells, param, space_params,
                          cells_params, source):

    param_names = _get_param_names(obj, param)

    def normalize_params(paramlist):
        """Covert index elements to str"""
        return [p if isinstance(p, str) else param_names[p] for p in paramlist]

    if space_params is None:
        if cells_params is None:
            cells_params = param_names
        else:
            cells_params = normalize_params(cells_params)
            if set(param_names) == set(cells_params):
                pass
            else:
                raise ValueError("invalid cells_params")
    else:
        space_params = normalize_params(space_params)
        if cells_params is None:
            cells_params = [p for p in param_names if p not in space_params]
        else:
            cells_params = normalize_params(cells_params)

            if (set(space_params) | set(cells_params) == set(param_names)
                    and len(set(space_params) & set(cells_params)) == 0):
                pass
            else:
                raise ValueError("invalid cells_params")

    if space_params is None:
        space_func = None
    else:
        space_func = get_param_func(space_params)

    newspace = self.new_space(name=space, formula=space_func, source=source)

    if isinstance(obj, pd.Series):
        obj = obj.to_frame()

    cells_names = _overwrite_colnames(self, obj, names=cells)

    for c in cells_names:
        newspace.new_cells(name=c, formula=get_param_func(cells_params))

    cells_paramidxs = [
        param_names.index(p) for p in cells_params if p in param_names
    ]

    if space_params is not None:
        space_paramsidxs = [
            param_names.index(p) for p in space_params if p in param_names
        ]

    def idx_to_arg(idx):

        cargs = tuple(idx[i] for i in cells_paramidxs)
        if space_params is not None:
            sargs = tuple(idx[i] for i in space_paramsidxs)
        else:
            sargs = None

        return sargs, cargs

    if space_params is None:
        for idx in obj.index:
            _, cargs = idx_to_arg(idx if obj.index.nlevels > 1 else (idx, ))
            for i, col in enumerate(obj.columns):
                v = obj.at[idx, col]
                cells = newspace.cells[cells_names[i]]
                cells.set_value(cargs,
                                v.item() if isinstance(v, np.generic) else v)
    else:
        for idx in obj.index:
            sargs, cargs = idx_to_arg(idx if obj.index.nlevels > 1 else (
                idx, ))
            for i, col in enumerate(obj.columns):
                v = obj.at[idx, col]
                subspace = newspace.get_dynspace(sargs)
                cells = subspace.cells[cells_names[i]]
                cells.set_value(cargs,
                                v.item() if isinstance(v, np.generic) else v)

    return newspace