Demo entry 6783508

m.ljñkñ

   

Submitted by anonymous on Feb 14, 2019 at 20:05
Language: Python 3. Code size: 2.9 kB.

#!/usr/bin/env python3

import click
from pathlib import Path

from optapp.approach import Approach
from optapp.project import Project
from optapp.data import Dataset, SubDataset
from optapp.result_report import ResultReport
from optapp.run import RunGenerator


__author__ = "Guillem Orellana Trullols"


@click.group()
def main():
    """
    Simple CLI for OptAPP
    """
    pass


@main.command()
@click.argument('project_name')
def new(project_name):
    if Path(project_name).is_dir():
        print("Project already exists")
        click.Abort()
        return

    Project(name=project_name)


@main.command()
@click.argument('item')
@click.option('--path', '-p', help="In case you want to add a DataSource, specify the path of it")
def add(item, path):
    proj = Project.load(str(Path(".")))

    if item == "dataset":
        if path is None:
            print("You must provide a path with -p or --path option")
            click.Abort()
            return

        path_to_dataset = str(Path(path).absolute())
        ds = Dataset.read_file(path_to_dataset)
        ds.set_project_path(proj.path)
        ds.save()
        print("Dataset with id {} created".format(ds.id))


def generate_subdataset(dataset, method, by):
    def parse_by(method, by):
        if not by:
            return None
        if method == "k_fold":
            return int(by)
        return float(by)

    if method not in {"k_fold", "train_test"}:
        print("Method must be either 'k_fold' or 'train_test")
        click.Abort()
        return

    path = str(Path("datasets/{}".format(dataset)))
    by = parse_by(method, by)
    params = dict(method=method, by=by)
    sbds = SubDataset(Dataset.load(path), **params)
    sbds.save()
    print("Subdataset with id {} created".format(sbds.id))


@main.command()
@click.argument("item")
@click.argument("identifier")
@click.option("--subdataset")
@click.option("--method", "-m")
@click.option("--by")
@click.option("--dataset", "-d")
def generate(item, identifier, subdataset, method, by, dataset):
    if item == "approach":
        approach_name = identifier
        # TODO: Generate approach for las subdataset if not set
        a = Approach(Project.load("."),
                     approach_name,
                     SubDataset.load(str(Path("subdatasets", subdataset + ".yml"))))
        a.save()

    if item == "subdataset":
        generate_subdataset(identifier, method, by)


@main.command()
@click.argument("identifier")
@click.argument("kind")
def run(identifier, kind):
    a = Approach.load(str(Path("approaches", identifier)))
    a.run(kind=kind)

    rr = ResultReport(
        results_path=Path(a.path, "results"),
        metrics=["precision", "recall"])

    print("See results.csv to check the evaluations")
    with open("results.csv", "w") as f:
        f.write(rr.as_dataframe().to_csv(index=False, line_terminator='\n'))


if __name__ == "__main__":
    main()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).