Ejemplo n.º 1
0
def get_stub():
    server_channel_address = os.environ.get('TA2_SERVER_CONN')
    # complain in the return if we didn't get an address to connect to
    if server_channel_address is None:
        tangelo.http_status(500)
        return {'error': 'TA2_SERVER_CONN environment variable is not set!'}
    channel = grpc.insecure_channel(server_channel_address)
    stub = core_pb2_grpc.CoreStub(channel)
    return stub
Ejemplo n.º 2
0
    def test_session(self):
        channel = grpc.insecure_channel('localhost:45042')
        stub = core_pb2_grpc.CoreStub(channel)
        msg = core_pb2.SessionRequest(user_agent="unittest", version="Foo")
        session = stub.StartSession(msg)
        self.assertTrue(session.response_info.status.code == core_pb2.OK)

        session_end_response = stub.EndSession(session.context)
        self.assertTrue(session_end_response.status.code == core_pb2.OK)

        # Try to end a session that does not exist
        fake_context = core_pb2.SessionContext(session_id="fake context")
        session_end_response = stub.EndSession(fake_context)
        self.assertTrue(
            session_end_response.status.code == core_pb2.SESSION_UNKNOWN)
Ejemplo n.º 3
0
def post(port=None):
    # get the protocol version
    version = core_pb2.DESCRIPTOR.GetOptions().Extensions[
        core_pb2.protocol_version]

    # get the address from an environment variable.  This must be set in the executing shell.
    # During automated evaluation runs, the environment variable will be set by Kubernetes
    server_channel_address = os.environ.get('TA2_SERVER_CONN')

    # complain in the return if we didn't get an address to connect to
    if server_channel_address is None:
        tangelo.http_status(500)
        return {'error': 'TA2_SERVER_CONN environment variable is not set!'}

    #channel = grpc.insecure_channel('localhost:%d' % (int(port)))
    channel = grpc.insecure_channel(server_channel_address)
    stub = core_pb2_grpc.CoreStub(channel)

    resp = stub.StartSession(
        core_pb2.SessionRequest(user_agent='modsquad', version=version))

    return MessageToJson(resp)
Ejemplo n.º 4
0
    def get_grpc_stub():
        """Make sure the gRPC channel is initialized and return the CoreStub."""
        global _GRPC_CORE_STUB
        if _GRPC_CORE_STUB is not None:  # channel already initialized
            return _GRPC_CORE_STUB, None

        if not settings.TA2_TEST_SERVER_URL:
            """No server set!!!"""
            return None, ('No TA2 server url was found in'
                          ' the TwoRavens settings (see'
                          ' "TA2_TEST_SERVER_URL")')

        try:
            channel = grpc.insecure_channel(settings.TA2_TEST_SERVER_URL)
        except Exception as ex:
            return None, 'Could not initialize gRPC channel.(1) %s' % ex

        try:
            _GRPC_CORE_STUB = cpb_grpc.CoreStub(channel)
        except Exception as ex:
            return None, 'Could not initialize gRPC channel.(2) %s' % ex

        return _GRPC_CORE_STUB, None
Ejemplo n.º 5
0
    def test_pipeline(self):
        "Tries setting up a new pipeline"
        channel = grpc.insecure_channel('localhost:45042')
        stub = core_pb2_grpc.CoreStub(channel)
        msg = core_pb2.SessionRequest(user_agent="unittest", version="Foo")
        session = stub.StartSession(msg)
        self.assertTrue(session.response_info.status.code == core_pb2.OK)

        pipeline_request = core_pb2.PipelineCreateRequest(
            context=session.context,
            dataset_uri=
            "file:///home/sheath/projects/D3M/cmu-ta3/test-data/185_baseball/TRAIN/dataset_TRAIN/datasetDoc.json",
            task=core_pb2.TASK_TYPE_UNDEFINED,
            task_subtype=core_pb2.TASK_SUBTYPE_UNDEFINED,
            task_description="",
            output=core_pb2.OUTPUT_TYPE_UNDEFINED,
            metrics=[],
            target_features=[],
            predict_features=[],
            max_pipelines=10)
        p = stub.CreatePipelines(pipeline_request)
        for response in p:
            self.assertTrue(response.response_info.status.code == core_pb2.OK)
Ejemplo n.º 6
0
def run():
    channel = grpc.insecure_channel('localhost:45042')
    stub = crpc.CoreStub(channel)
    dstub = drpc.DataExtStub(channel)
    dfstub = dfrpc.DataflowExtStub(channel)

    # Start Session
    session_response = stub.StartSession(
        core.SessionRequest(user_agent="xxx", version="1.0"))
    session_context = session_response.context
    print("Session started (%s)" % str(session_context.session_id))

    # Send pipeline creation request
    dataset_uri = "file:///tmp/data/185_baseball/185_baseball_dataset/datasetDoc.json"
    some_features = [
        core.Feature(resource_id="0", feature_name="d3mIndex"),
        core.Feature(resource_id="0", feature_name="Games_played"),
        core.Feature(resource_id="0", feature_name="Runs"),
        core.Feature(resource_id="0", feature_name="Hits"),
        core.Feature(resource_id="0", feature_name="Home_runs")
    ]
    target_features = [
        core.Feature(resource_id="0", feature_name="Hall_of_Fame")
    ]
    task = core.TaskType.Value('CLASSIFICATION')
    task_subtype = core.TaskSubtype.Value('MULTICLASS')
    task_description = "Classify Hall of Fame"
    output = core.OutputType.Value('OUTPUT_TYPE_UNDEFINED')
    metrics = [
        core.PerformanceMetric.Value('F1_MICRO'),
        core.PerformanceMetric.Value('F1_MACRO')
    ]
    max_pipelines = 10

    pipeline_ids = []

    print("Training with some features")
    pc_request = core.PipelineCreateRequest(context=session_context,
                                            dataset_uri=dataset_uri,
                                            predict_features=some_features,
                                            task=task,
                                            task_subtype=task_subtype,
                                            task_description=task_description,
                                            output=output,
                                            metrics=metrics,
                                            target_features=target_features,
                                            max_pipelines=max_pipelines)
    '''
    # Iterate over results
    for pcr in stub.CreatePipelines(pc_request):
        print(str(pcr))
        if len(pcr.pipeline_info.scores) > 0:
            pipeline_ids.append(pcr.pipeline_id)

    print("Training with some features")
    pc_request = core.PipelineCreateRequest(
        context = session_context,
        train_features = some_features,
        task = task,
        task_subtype = task_subtype,
        task_description = task_description,
        output = output,
        metrics = metrics,
        target_features = target_features,
        max_pipelines = max_pipelines
    )
    '''

    result = stub.CreatePipelines(pc_request)

    # Iterate over results
    for pcr in result:
        print(str(pcr))
        '''
        for gdr in dfstub.GetDataflowResults(dfext.PipelineReference(context = session_context,
                pipeline_id = pcr.pipeline_id)):
            print(gdr)
        '''
        if len(pcr.pipeline_info.scores) > 0:
            pipeline_id = pcr.pipeline_id
            pipeline_ids.append(pipeline_id)
            dflow = dfstub.DescribeDataflow(
                dfext.PipelineReference(context=session_context,
                                        pipeline_id=pipeline_id))
            print(dflow)

            exres = stub.ExportPipeline(
                core.PipelineExportRequest(
                    context=session_context,
                    pipeline_id=pipeline_id,
                    pipeline_exec_uri="file:///tmp/{}".format(pipeline_id)))
            print(exres)
            '''
            if pcr.pipeline_info.predict_result_uri is not None:
                df = pandas.read_csv(pcr.pipeline_info.predict_result_uri, index_col="d3mIndex")
                print(df)
            '''

    print("************** Executing/Testing Pipelines")

    # Execute pipelines
    for pipeline_id in pipeline_ids:
        print("Executing Pipeline %s" % pipeline_id)
        ep_request = core.PipelineExecuteRequest(context=session_context,
                                                 pipeline_id=pipeline_id,
                                                 dataset_uri=dataset_uri)
        for ecr in stub.ExecutePipeline(ep_request):
            print(str(ecr))
            if ecr.result_uri is not None:
                df = pandas.read_csv(ecr.result_uri, index_col="d3mIndex")
                print(df)

    list_request = core.PipelineListRequest(context=session_context)
    lrr = stub.ListPipelines(list_request)
    print(lrr.pipeline_ids)

    print("************** Cached pipeline create results")
    pcrr = core.PipelineCreateResultsRequest(context=session_context,
                                             pipeline_ids=lrr.pipeline_ids)
    for gcpr in stub.GetCreatePipelineResults(pcrr):
        print(str(gcpr))

    print("************** Cached pipeline execute results")
    perr = core.PipelineExecuteResultsRequest(context=session_context,
                                              pipeline_ids=lrr.pipeline_ids)
    for gepr in stub.GetExecutePipelineResults(perr):
        print(str(gepr))

    print("*********** Updating Metric to Accuracy.. Create pipelines again")
    metric = core.PerformanceMetric.Value('ACCURACY')
    ups_request = core.SetProblemDocRequest(
        context=session_context,
        updates=[
            core.SetProblemDocRequest.ReplaceProblemDocField(metric=metric)
        ])

    print(stub.SetProblemDoc(ups_request))
    print("********** Re-running pipeline creation")
    for pcr in stub.CreatePipelines(
            core.PipelineCreateRequest(context=session_context)):
        print(str(pcr))

    stub.EndSession(session_context)
Ejemplo n.º 7
0
 def setUp(self):
     self.protocol_version = core_pb2.DESCRIPTOR.GetOptions().Extensions[
         core_pb2.protocol_version]
     channel = grpc.insecure_channel('localhost:50052')
     self.stub = core_pb2_grpc.CoreStub(channel)