def get_all_projects():
    try:
        projects = [
            project.to_dict(show=["datasets"])
            for project in Project.query.all()
        ]

        return jsonify(projects)
    except Exception as e:
        return handle_exception(e)
def get_data(rid: str):
    try:
        record = DatasetRecord.query.filter_by(id=rid).first()
        pid = record.project_id
        file = BytesIO(record.data)
        data = pd.read_parquet(file)

        col_infos = DatasetMeta.query.filter_by(project_id=pid).all()

        column_info = {}
        columns = []
        categorical_columns = []
        numeric_columns = []
        label_column = None

        for c in col_infos:
            columns.append(c.key)

            c_type = c.data_type

            if c_type == "label":
                label_column = c.key
            elif c_type == "numeric":
                numeric_columns.append(c.key)
            elif c_type == "categorical":
                categorical_columns.append(c.key)

            col = c.to_dict()
            column_info[c.key] = col

        d = data.T.to_json()

        return (
            jsonify({
                "values": list(json.loads(d).values()),
                "columnInfo": column_info,
                "numericColumns": numeric_columns,
                "categoricalColumns": categorical_columns,
                "labelColumn": label_column,
                "columns": columns,
                "id": rid,
                "version": record.version,
            }),
            200,
        )
    except Exception as e:
        raise e
        return handle_exception(e)
def compute_state():
    try:
        body = request.get_json()

        if body is None:
            raise Exception("Resources incorrect!")

        provenance = body["provenance"]
        data = pd.DataFrame(body["data"])
        graph = Graph(**provenance)

        return jsonify(
            json.loads(json.dumps(graph.states(data), default=lambda o: o.toJSON()))
        )
    except Exception as ex:
        raise ex
        return handle_exception(ex)
def create_project():
    try:
        body = request.get_json()

        if body is None:
            raise Exception("No project name specified")

        p_name = body["p_name"]

        project = Project(name=p_name)
        db.session.add(project)
        db.session.commit()

        project = project.to_dict()

        return jsonify(project), 200
    except Exception as e:
        return handle_exception(e)
def compute_compare():
    try:
        body = request.get_json()

        if body is None:
            raise Exception("Resources incorrect!")

        provenance = body["provenance"]
        base = pd.DataFrame(body["base"])
        updated = pd.DataFrame(body["target"])
        graph = Graph(**provenance)

        compare = graph.compare(base, updated)

        return jsonify(compare)
    except Exception as ex:
        raise ex
        return handle_exception(ex)
Beispiel #6
0
def get_members_route():
    try:
        body = request.get_json()

        if body is None:
            raise Exception("No intent provided")

        data = body["data"]
        intent = body["intent"]
        algorithm = body["algorithm"]
        params = body["params"]
        dimensions = body["dimensions"]
        info = body["info"]

        data = pd.DataFrame(data)

        return jsonify(
            get_members(data, dimensions, intent, algorithm, params, info))
    except Exception as e:
        return handle_exception(e)
Beispiel #7
0
def get_predictions():
    try:
        body = request.get_json()

        if body is None:
            raise Exception("No interactions found")

        data = pd.DataFrame(body["data"])
        dimensions = body["dimensions"]
        selections = body["selections"]

        if len(selections) == 0:
            return jsonify([])

        inference = Inference(data, selections, dimensions, [])

        predictions = inference.predict()

        return jsonify(predictions)
    except Exception as e:
        raise e
        return handle_exception(e)
def add_category_column():
    try:
        body = request.get_json()

        if body is None:
            raise Exception("No data specified")

        pid = body["pid"]
        column_name = body["c_name"]
        options = body["options"]

        letters = string.ascii_uppercase
        shorts = [
            c.short for c in DatasetMeta.query.filter_by(project_id=pid).all()
        ]
        shorts = list(sorted(shorts))

        last_short = shorts[-1]
        next_short = None

        if last_short == "Z":
            raise Exception("Too many categories")
        else:
            next_short = letters[letters.index(last_short) + 1]

        new_category_column = DatasetMeta(
            project_id=pid,
            key=get_column_key(column_name),
            fullname=column_name,
            short=next_short,
            data_type="categorical",
            options=options,
        )
        db.session.add(new_category_column)
        db.session.commit()

        col_infos = DatasetMeta.query.filter_by(project_id=pid).all()

        column_info = {}
        columns = []
        categorical_columns = []
        numeric_columns = []
        label_column = None

        for c in col_infos:
            columns.append(c.key)

            c_type = c.data_type

            if c_type == "label":
                label_column = c.key
            elif c_type == "numeric":
                numeric_columns.append(c.key)
            elif c_type == "categorical":
                categorical_columns.append(c.key)

            col = c.to_dict()
            column_info[c.key] = col

        return jsonify({
            "column_info": column_info,
            "numeric_columns": numeric_columns,
            "categorical_columns": categorical_columns,
            "label_column": label_column,
            "columns": columns,
        })
    except Exception as e:
        return handle_exception(e)