예제 #1
0
파일: test_s3.py 프로젝트: indralab/emmaa
def test_load_model():
    # Local imports are recommended when using moto
    from emmaa.model import EmmaaModel
    client = setup_bucket(add_model=True)
    em = EmmaaModel.load_from_s3('test', bucket='test_bucket')
    assert isinstance(em, EmmaaModel)
    assert len(em.stmts) == 2, len(em.stmts)
    assert em.name == 'test'
예제 #2
0
def model_to_tests(model_name, upload=True, bucket=EMMAA_BUCKET_NAME):
    em = EmmaaModel.load_from_s3(model_name, bucket=bucket)
    em.run_assembly()
    tests = [
        StatementCheckingTest(stmt) for stmt in em.assembled_stmts
        if all(stmt.agent_list())
    ]
    date_str = make_date_str()
    test_description = (
        f'These tests were generated from the {em.human_readable_name} '
        f'on {date_str[:10]}')
    test_dict = {
        'test_data': {
            'description': test_description
        },
        'tests': tests
    }
    if upload:
        save_pickle_to_s3(test_dict, bucket,
                          f'tests/{model_name}_tests_{date_str}.pkl')
    return test_dict
예제 #3
0
      '6840@HGNC&type=Activation&format=html'),
     ('Active MAP2K1 activates MAPK1.',
      'https://db.indra.bio/statements/from_agents?subject=6840@HGNC&object='
      '6871@HGNC&type=Activation&format=html')]
}
processed_link = '<a href="https://db.indra.bio/statements/from_agents?'\
    'subject=1097@HGNC&object=6840@HGNC&type=Activation&format=html" '\
                 'target="_blank" class="status-link">'\
                 'BRAF activates MAP2K1.</a>'
query_not_appl = {
    2413475507:
    [('Query is not applicable for this model',
      'https://emmaa.readthedocs.io/en/latest/dashboard/response_codes.html')]
}
# Create a new ModelManager for tests instead of depending on S3 version
test_model = EmmaaModel.load_from_s3('test')
test_mm = ModelManager(test_model)


def test_load_model_manager_from_s3():
    mm = load_model_manager_from_s3('test')
    assert isinstance(mm, ModelManager)


def test_format_results():
    results = [('test', query_object, 'pysb', test_response, datetime.now())]
    formatted_results = format_results(results)
    assert len(formatted_results) == 1
    assert formatted_results[0]['model'] == 'test'
    assert formatted_results[0]['query'] == simple_query
    assert formatted_results[0]['mc_type'] == 'pysb'
예제 #4
0
def update_model_manager_on_s3(model_name, bucket=EMMAA_BUCKET_NAME):
    model = EmmaaModel.load_from_s3(model_name, bucket=bucket)
    mm = ModelManager(model)
    save_model_manager_to_s3(model_name, mm, bucket=bucket)
    return mm
예제 #5
0
import argparse
from emmaa.model import EmmaaModel
from emmaa.model_tests import ModelManager, save_model_manager_to_s3


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
            description='Script to update ModelManager stored on Amazon S3.')
    parser.add_argument('-m', '--model', help='Model name', required=True)
    args = parser.parse_args()

    model = EmmaaModel.load_from_s3(args.model)
    mm = ModelManager(model, mode='s3')
    mm.model.update_to_ndex()
    mm.save_assembled_statements()
    save_model_manager_to_s3(args.model, mm)
예제 #6
0
def run_model_tests_from_s3(model_name,
                            upload_mm=True,
                            upload_results=True,
                            upload_stats=True,
                            registered_queries=True,
                            db=None):
    """Run a given set of tests on a given model, both loaded from S3.

    After loading both the model and the set of tests, model/test overlap
    is determined using a ScopeTestConnector and tests are run.


    Parameters
    ----------
    model_name : str
        Name of EmmaaModel to load from S3.
    upload_mm : Optional[bool]
        Whether to upload a model manager instance to S3 as a pickle file.
        Default: True
    upload_results : Optional[bool]
        Whether to upload test results to S3 in JSON format. Can be set
        to False when running tests. Default: True
    upload_stats : Optional[bool]
        Whether to upload latest statistics about model and a test.
        Default: True
    registered_queries : Optional[bool]
        If True, registered queries are fetched from the database and
        executed, the results are then saved to the database. Default: True
    db : Optional[emmaa.db.manager.EmmaaDatabaseManager]
        If given over-rides the default primary database.

    Returns
    -------
    emmaa.model_tests.ModelManager
        Instance of ModelManager containing the model data, list of applied
        tests and the test results.
    emmaa.analyze_test_results.StatsGenerator
        Instance of StatsGenerator containing statistics about model and test.
    """
    model = EmmaaModel.load_from_s3(model_name)
    test_corpus = model.test_config.get('test_corpus',
                                        'large_corpus_tests.pkl')
    tests = load_tests_from_s3(test_corpus)
    mm = ModelManager(model)
    if upload_mm:
        save_model_manager_to_s3(model_name, mm)
    tm = TestManager([mm], tests)
    tm.make_tests(ScopeTestConnector())
    tm.run_tests()
    results_json_dict = mm.results_to_json()
    results_json_str = json.dumps(results_json_dict, indent=1)
    # Optionally upload test results to S3
    if upload_results:
        client = get_s3_client(unsigned=False)
        date_str = make_date_str()
        result_key = f'results/{model_name}/results_{date_str}.json'
        logger.info(f'Uploading test results to {result_key}')
        client.put_object(Bucket='emmaa',
                          Key=result_key,
                          Body=results_json_str.encode('utf8'))
    tr = TestRound(results_json_dict)
    sg = StatsGenerator(model_name, latest_round=tr)
    sg.make_stats()

    # Optionally upload statistics to S3
    if upload_stats:
        sg.save_to_s3()
    if registered_queries:
        qm = QueryManager(db=db, model_managers=[mm])
        qm.answer_registered_queries(model_name)
    return (mm, sg)