Example #1
0
def project_model_perturbations():

    updated_author = create_author()
    updated_author.email = "*****@*****.**"

    updated_study = create_study("project-1", "study-1")
    updated_study.name = "updated"

    return [
        ({
            "name": "updated"
        }, lambda db: [], does_not_raise()),
        ({
            "description": "updated"
        }, lambda db: [], does_not_raise()),
        (
            {
                "authors": [create_author(), updated_author]
            },
            lambda db: [
                db.query(models.Author.email).count() == 2,
                db.query(author_projects_table).count() == 2,
            ],
            does_not_raise(),
        ),
        # Delete a study.
        (
            {
                "studies": [create_study("project-1", "study-1")]
            },
            lambda db: [db.query(models.Study.id).count() == 1],
            does_not_raise(),
        ),
        # Update a study.
        (
            {
                "studies":
                [updated_study,
                 create_study("project-1", "study-2")]
            },
            lambda db: [db.query(models.Study.id).count() == 2],
            does_not_raise(),
        ),
        # Add a study.
        (
            {
                "studies": [
                    create_study("project-1", f"study-{index + 1}")
                    for index in range(3)
                ]
            },
            lambda db: [db.query(models.Study.id).count() == 3],
            does_not_raise(),
        ),
    ]
Example #2
0
    def create_model(cls, include_children=False, index=1):

        project = create_project(f"project-{index}")

        if include_children:
            project.studies = [
                create_study(project.id, "study-1"),
                create_study(project.id, "study-2"),
            ]

        return project
Example #3
0
    def create_model(cls, include_children=False, index=1):

        study = create_study("project-1", f"study-{index}")

        if include_children:

            study.optimizations = [
                create_optimization(
                    "project-1",
                    study.id,
                    "optimization-1",
                    [
                        create_evaluator_target("evaluator-target-1",
                                                ["data-set-1"])
                    ],
                )
            ]
            study.benchmarks = [
                create_benchmark(
                    "project-1",
                    study.id,
                    "benchmark-1",
                    ["data-set-1"],
                    None,
                    create_force_field(),
                )
            ]

        return study
Example #4
0
def test_study_no_children(requests_mock):

    study = create_study("project-1", "study-1")
    mock_get_study(requests_mock, study)

    with temporary_cd():

        InputFactory.generate(study, "test-env", "12:34", "lilac-dask", 8000, 1, False)
    def _create_model(cls, db, create_dependencies=True):

        project_id = "project-1"

        if create_dependencies:
            project = commit_project(db)
            project_id = project.id

        study = create_study(project_id, "study-1")

        return study, {"project_id": project_id, "study_id": study.id}
Example #6
0
def test_project_with_children(requests_mock):

    project = create_project(project_id="project-1")
    project.studies = [create_study(project.id, "study-1")]

    mock_get_project(requests_mock, project)
    mock_get_study(requests_mock, project.studies[0])

    with temporary_cd():

        InputFactory.generate(
            project, "test-env", "12:34", "lilac-dask", 8000, 1, False
        )

        assert os.path.isdir(project.id)
        assert os.path.isdir(os.path.join(project.id, "studies"))
Example #7
0
def commit_study(db: Session) -> Tuple[Project, Study]:
    """Commits a new project to the current session and appends an empty
    study onto it.

    Parameters
    ----------
    db
        The current data base session.
    """
    project = create_project("project-1")
    project.studies = [create_study(project.id, "study-1")]

    db_project = ProjectCRUD.create(db, project)
    db.add(db_project)
    db.commit()

    project = ProjectCRUD.db_to_model(db_project)
    return project, project.studies[0]
Example #8
0
def test_study_with_children(requests_mock, monkeypatch):

    # Overwrite the child factories so we don't need to provide
    # sensible children and wait for them to be buit.
    def mock_generate(model, **_):
        os.makedirs(model.id, exist_ok=True)

    monkeypatch.setattr(OptimizationInputFactory, "generate", mock_generate)
    monkeypatch.setattr(BenchmarkInputFactory, "generate", mock_generate)

    mock_get_data_set(requests_mock, create_data_set("data-set-1"))

    optimization = create_optimization(
        "project-1",
        "study-1",
        "optimization-1",
        targets=[create_evaluator_target("evaluator-target", ["data-set-1"])],
    )
    benchmark = create_benchmark(
        "project-1",
        "study-1",
        "benchmark-1",
        ["data-set-1"],
        None,
        create_force_field(),
    )

    study = create_study("project-1", "study-1")
    study.optimizations = [optimization]
    study.benchmarks = [benchmark]

    mock_get_study(requests_mock, study)

    with temporary_cd():

        InputFactory.generate(study, "test-env", "12:34", "lilac-dask", 8000, 1, False)

        assert os.path.isdir(study.id)
        assert os.path.isdir(os.path.join(study.id, "optimizations"))
        assert os.path.isdir(os.path.join(study.id, "benchmarks"))

        assert os.path.isdir(os.path.join(study.id, "optimizations", optimization.id))
        assert os.path.isdir(os.path.join(study.id, "benchmarks", benchmark.id))
Example #9
0
def commit_optimization(
    db: Session,
) -> Tuple[Project, Study, Optimization, DataSetCollection,
           QCDataSetCollection]:
    """Commits a new project and study to the current session and appends an
    empty optimization onto it. Additionally, this function commits two data sets
    to the session to use as the training set.

    Parameters
    ----------
    db
        The current data base session.
    """

    training_set = commit_data_set_collection(db)
    training_set_ids = [x.id for x in training_set.data_sets]

    qc_data_set = commit_qc_data_set_collection(db)
    qc_data_set_ids = [x.id for x in qc_data_set.data_sets]

    study = create_study("project-1", "study-1")
    study.optimizations = [
        create_optimization(
            "project-1",
            "study-1",
            "optimization-1",
            [
                create_evaluator_target("evaluator-target-1",
                                        training_set_ids),
                create_recharge_target("recharge-target-1", qc_data_set_ids),
            ],
        )
    ]

    project = create_project(study.project_id)
    project.studies = [study]

    db_project = ProjectCRUD.create(db, project)
    db.add(db_project)
    db.commit()

    project = ProjectCRUD.db_to_model(db_project)
    return project, study, study.optimizations[0], training_set, qc_data_set
Example #10
0
def create_dependencies(db: Session, dependencies: List[str]):
    """Create any dependencies such as parent studies, projects, or data sets and
    commit them to the database.

    Parameters
    ----------
    db
        The current database session.
    dependencies
        The required dependencies.
    """

    project = None
    data_set_ids = []
    qc_data_set_ids = []

    if "data-set" in dependencies:
        data_set_ids.append("data-set-1")
    if "qc-data-set" in dependencies:
        qc_data_set_ids.append("qc-data-set-1")

    for data_set_id in data_set_ids:
        data_set = create_data_set(data_set_id)
        db_data_set = DataSetCRUD.create(db, data_set)
        db.add(db_data_set)

    for qc_data_set_id in qc_data_set_ids:
        qc_data_set = create_qc_data_set(qc_data_set_id)
        db_qc_data_set = QCDataSetCRUD.create(db, qc_data_set)
        db.add(db_qc_data_set)

    db.commit()

    if ("project" in dependencies or "study" in dependencies
            or "evaluator-target" in dependencies
            or "recharge-target" in dependencies
            or "benchmark" in dependencies):
        project = create_project("project-1")

    if ("study" in dependencies or "evaluator-target" in dependencies
            or "recharge-target" in dependencies
            or "benchmark" in dependencies):
        project.studies = [create_study(project.id, "study-1")]

    if "evaluator-target" in dependencies or "recharge-target" in dependencies:

        targets = []

        if "evaluator-target" in dependencies:
            targets.append(
                create_evaluator_target("evaluator-target-1", ["data-set-1"]))
        if "recharge-target" in dependencies:
            targets.append(
                create_recharge_target("recharge-target-1", ["qc-data-set-1"]))

        optimization = create_optimization(project.id, project.studies[0].id,
                                           "optimization-1", targets)

        project.studies[0].optimizations = [optimization]

    if "benchmark" in dependencies:
        benchmark = create_benchmark(
            project.id,
            project.studies[0].id,
            "benchmark-1",
            ["data-set-1"],
            None,
            create_force_field(),
        )

        project.studies[0].benchmarks = [benchmark]

    if project is not None:
        db_project = ProjectCRUD.create(db, project)
        db.add(db_project)
        db.commit()