def model_init(self, model, objectives, dataset_map, model_path=None, model_props=None, hyperparams=None): """ Instantiate model or remote endpoint wrapper. Call initialize, load_data, build_model on model. Output model status. :param model: model module/package or service endpoint URL :param objectives: model objectives :param dataset_map: dict of train/test dataset URIs :param model_path: local folder path of saved model files :param model_props: A JSON dictionary of model properties to override. :param hyperparams: A JSON dictionary of model hyperparameters to override. """ if model.startswith('http:'): self._model_service = model_service_wrapper.ModelServiceWrapper( os.path.join(model, 'v1/mistk')) else: self._model_service = ModelInstanceEndpoint() path = model.rsplit('.', 1) module = importlib.import_module(path[0]) model_impl = getattr(module, path[1])() assert isinstance(model_impl, AbstractModel) self._model_service.model = model_impl model_impl.endpoint_service = self._model_service ip = ModelInstanceInitParams(objectives=objectives, model_properties=model_props, hyperparameters=hyperparams) self._model_service.initialize_model(ip) self.wait_for_state(self._model_service, 'initialize', 'initialized') self._model_service.build_model(model_path) self.wait_for_state(self._model_service, 'build_model', 'ready') if dataset_map: bindings = {} if 'train' in dataset_map: training_data = MistkDataset( object_info=ObjectInfo(name='train-data'), data_path=dataset_map['train']) bindings['train'] = training_data if 'test' in dataset_map: test_data = MistkDataset( object_info=ObjectInfo(name='test-data'), data_path=dataset_map['test']) bindings['test'] = test_data self._model_service.load_data(bindings) self.wait_for_state(self._model_service, 'load_data', 'ready')
def transform(self, transform, input_dirs, output_dir, properties): """ Creates a transform service wrapper and performs the data transform on the dataset(s) provided. :param transform: The Transform that will be formed. Will be one of the following forms: - URL of running transform instance (ie. http://localhost:8080) - Python package and module (ie. mypackage.mymodule.MyTransformPluginClass :param input_dirs: A list of directory paths to be used for input in the transformation :param output_dir: A directory path to where all of the output files should be stored :param properties: A JSON dictionary of properties relevant to this transformation """ if transform.startswith('http:'): self._transform_service = transform_service_wrapper.TransformServiceWrapper( os.path.join(transform, 'v1/mistk/transform')) else: self._transform_service = TransformPluginEndpoint() path = transform.rsplit('.', 1) module = importlib.import_module(path[0]) transform_impl = getattr(module, path[1])() assert isinstance(transform_impl, AbstractTransformPlugin) self._transform_service.transform_plugin = transform_impl transform_impl.endpoint_service = self._transform_service st = self._transform_service.get_status().state if st == 'start': self.wait_for_state(self._transform_service, 'start', 'started') elif st == 'started' or st == 'ready': pass else: assert False, ("Invalid state to start transform: %s" % st) mistk_input_dirs = [] for input_dir in input_dirs: mistk_input_dir = MistkDataset(object_info=ObjectInfo( name=os.path.basename(os.path.normpath(input_dir))), data_path=input_dir) mistk_input_dirs.append(mistk_input_dir) mistk_output_dir = MistkDataset(object_info=ObjectInfo( name=os.path.basename(os.path.normpath(output_dir))), data_path=output_dir) ip = TransformSpecificationInitParams(input_datasets=mistk_input_dirs, output_dataset=mistk_output_dir, properties=properties) self._transform_service.transform(ip) self.wait_for_state(self._transform_service, 'transform', 'ready')
def __init__(self): """ Initializes the Model Instance Endpoint service """ initializeEndpointController(self, model_instance_endpoint_controller) self.app = cx.FlaskApp('mistk_server') self.app.app.json_encoder = datautils.PresumptiveJSONEncoder self.app.add_api(self._load_api_spec()) self.http_server = None self._state_machine = None self._model = None self._current_task = None self._status_lock = RWLock() self._task_lock = RWLock() self._response_queue = Queue() self._old_tasks = list() self._thread_pool = ThreadPoolExecutor() info = ObjectInfo('ModelInstanceStatus', resource_version=1) self._status = ModelInstanceStatus(object_info=info, state='started')
def __init__(self): """ Initializes the Model Instance Endpoint service """ initializeEndpointController(self, transform_plugin_endpoint_controller) self.app = cx.FlaskApp('mistk.transform.server') self.app.app.json_encoder = mistk.data.utils.PresumptiveJSONEncoder self.app.add_api(self._load_api_spec()) self.http_server = None self._state_machine = None self._transform_plugin = None self._current_task = None self._status_lock = RWLock() self._task_lock = RWLock() self._old_tasks = list() self._thread_pool = ThreadPoolExecutor() info = ObjectInfo('TransformInstanceStatus', resource_version=1) self._status = TransformInstanceStatus(object_info=info, state='started') logger.info('Transform Plugin initialized')
def update_state(self, state=None, payload=None): """ Updates the state of the EvaluationEndpointService :param state: The new state. If not given, the current state will be used. :param payload: Additional data to attach to the state """ try: with self._status_lock.writer_lock: state = state or self._status.state ver = self._status.object_info.resource_version + 1 info = ObjectInfo('EvaluationInstanceStatus', resource_version=ver) self._status = EvaluationInstanceStatus(info, state=state, payload=payload) watch_manager.notify_watch('status', item=self._status) except RuntimeError as inst: msg = "Runtime Error while updating state of EvaluationEndpointService: %s" % str( inst) logger.exception(msg) return ServiceError(500, msg), 500 except Exception as ex: msg = "Exception while updating state of EvaluationPluginService: %s" % str( ex) logger.exception(msg) return ServiceError(500, msg), 500
def model_predict(self, predictions_path=None): """ Call predict and, if prediction_path is supplied, save_predictions on model. Output model status. """ self._model_service.predict() self.wait_for_state('predict', 'ready') if predictions_path: dataset = MistkDataset( object_info=ObjectInfo(name='predictions-data'), data_path=predictions_path) self._model_service.save_predictions(dataset) self.wait_for_state('save_predictions', 'ready')
def update_state(self, state=None, payload=None): """ Updates the state of the ModelEndpointService :param state: The new state. If not given, the current state will be used. :param payload: Additional data to attach to the state """ try: with self._status_lock.writer_lock: state = state or self._status.state ver = self._status.object_info.resource_version + 1 info = ObjectInfo('ModelInstanceStatus', resource_version=ver) self._status = ModelInstanceStatus(info, state=state, payload=payload) watch_manager.notify_watch('status', item=self._status) except RuntimeError as inst: return ServiceError(500, str(inst)), 500